Moved undocumented functions out of the exported commctrl.h.
[wine] / dlls / comctl32 / comboex.c
1 /*
2  * ComboBoxEx control
3  *
4  * Copyright 1998, 1999 Eric Kohl
5  * Copyright 2000, 2001, 2002 Guy Albertelli <galberte@neo.lrun.com>
6  * Copyright 2002 Dimitrie O. Paun
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * NOTE
23  * 
24  * This code was audited for completeness against the documented features
25  * of Comctl32.dll version 6.0 on Sep. 9, 2002, by Dimitrie O. Paun.
26  * 
27  * Unless otherwise noted, we belive this code to be complete, as per
28  * the specification mentioned above.
29  * If you discover missing features, or bugs, please note them below.
30  * 
31  */
32
33 #include <stdarg.h>
34 #include <string.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "commctrl.h"
41 #include "comctl32.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(comboex);
46
47 /* Item structure */
48 typedef struct
49 {
50     VOID         *next;
51     UINT         mask;
52     LPWSTR       pszText;
53     LPWSTR       pszTemp;
54     int          cchTextMax;
55     int          iImage;
56     int          iSelectedImage;
57     int          iOverlay;
58     int          iIndent;
59     LPARAM       lParam;
60 } CBE_ITEMDATA;
61
62 /* ComboBoxEx structure */
63 typedef struct
64 {
65     HIMAGELIST   himl;
66     HWND         hwndSelf;         /* my own hwnd */
67     HWND         hwndCombo;
68     HWND         hwndEdit;
69     WNDPROC      prevEditWndProc;  /* previous Edit WNDPROC value */
70     WNDPROC      prevComboWndProc; /* previous Combo WNDPROC value */
71     DWORD        dwExtStyle;
72     INT          selected;         /* index of selected item */
73     DWORD        flags;            /* WINE internal flags */
74     HFONT        defaultFont;
75     HFONT        font;
76     INT          nb_items;         /* Number of items */
77     BOOL         unicode;          /* TRUE if this window is Unicode   */
78     BOOL         NtfUnicode;       /* TRUE if parent wants notify in Unicode */
79     CBE_ITEMDATA *edit;            /* item data for edit item */
80     CBE_ITEMDATA *items;           /* Array of items */
81 } COMBOEX_INFO;
82
83 /* internal flags in the COMBOEX_INFO structure */
84 #define  WCBE_ACTEDIT           0x00000001  /* Edit active i.e.
85                                              * CBEN_BEGINEDIT issued
86                                              * but CBEN_ENDEDIT{A|W}
87                                              * not yet issued. */
88 #define  WCBE_EDITCHG           0x00000002  /* Edit issued EN_CHANGE */
89 #define  WCBE_EDITHASCHANGED    (WCBE_ACTEDIT | WCBE_EDITCHG)
90 #define  WCBE_EDITFOCUSED       0x00000004  /* Edit control has focus */
91 #define  WCBE_MOUSECAPTURED     0x00000008  /* Combo has captured mouse */
92 #define  WCBE_MOUSEDRAGGED      0x00000010  /* User has dragged in combo */
93
94 #define ID_CB_EDIT              1001
95
96
97 /*
98  * Special flag set in DRAWITEMSTRUCT itemState field. It is set by
99  * the ComboEx version of the Combo Window Proc so that when the
100  * WM_DRAWITEM message is then passed to ComboEx, we know that this
101  * particular WM_DRAWITEM message is for listbox only items. Any messasges
102  * without this flag is then for the Edit control field.
103  *
104  * We really cannot use the ODS_COMBOBOXEDIT flag because MSDN states that
105  * only version 4.0 applications will have ODS_COMBOBOXEDIT set.
106  */
107 #define ODS_COMBOEXLBOX         0x4000
108
109
110
111 /* Height in pixels of control over the amount of the selected font */
112 #define CBE_EXTRA               3
113
114 /* Indent amount per MS documentation */
115 #define CBE_INDENT              10
116
117 /* Offset in pixels from left side for start of image or text */
118 #define CBE_STARTOFFSET         6
119
120 /* Offset between image and text */
121 #define CBE_SEP                 4
122
123 #define COMBOEX_SUBCLASS_PROP   "CCComboEx32SubclassInfo"
124 #define COMBOEX_GetInfoPtr(hwnd) ((COMBOEX_INFO *)GetWindowLongW (hwnd, 0))
125
126
127 /* Things common to the entire DLL */
128 static LRESULT WINAPI
129 COMBOEX_EditWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
130 static LRESULT WINAPI
131 COMBOEX_ComboWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
132 static int CALLBACK
133 COMBOEX_PathWordBreakProc(LPWSTR lpch, int ichCurrent, int cch, int code);
134 static LRESULT COMBOEX_Destroy (COMBOEX_INFO *infoPtr);
135 typedef INT (WINAPI *cmp_func_t)(LPCWSTR, LPCWSTR);
136
137 inline static BOOL is_textW(LPCWSTR str)
138 {
139     return str && str != LPSTR_TEXTCALLBACKW;
140 }
141
142 inline static BOOL is_textA(LPCSTR str)
143 {
144     return str && str != LPSTR_TEXTCALLBACKA;
145 }
146
147 inline static LPCSTR debugstr_txt(LPCWSTR str)
148 {
149     if (str == LPSTR_TEXTCALLBACKW) return "(callback)";
150     return debugstr_w(str);
151 }
152
153 static void COMBOEX_DumpItem (CBE_ITEMDATA *item)
154 {
155     TRACE("item %p - mask=%08x, pszText=%p, cchTM=%d, iImage=%d\n",
156           item, item->mask, item->pszText, item->cchTextMax, item->iImage);
157     TRACE("item %p - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
158           item, item->iSelectedImage, item->iOverlay, item->iIndent, item->lParam);
159     if (item->mask & CBEIF_TEXT)
160         TRACE("item %p - pszText=%s\n", item, debugstr_txt(item->pszText));
161 }
162
163
164 static void COMBOEX_DumpInput (COMBOBOXEXITEMW *input)
165 {
166     TRACE("input - mask=%08x, iItem=%d, pszText=%p, cchTM=%d, iImage=%d\n",
167           input->mask, input->iItem, input->pszText, input->cchTextMax,
168           input->iImage);
169     if (input->mask & CBEIF_TEXT)
170         TRACE("input - pszText=<%s>\n", debugstr_txt(input->pszText));
171     TRACE("input - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
172           input->iSelectedImage, input->iOverlay, input->iIndent, input->lParam);
173 }
174
175
176 inline static CBE_ITEMDATA *get_item_data(COMBOEX_INFO *infoPtr, INT index)
177 {
178     return (CBE_ITEMDATA *)SendMessageW (infoPtr->hwndCombo, CB_GETITEMDATA,
179                                          (WPARAM)index, 0);
180 }
181
182 inline static cmp_func_t get_cmp_func(COMBOEX_INFO *infoPtr)
183 {
184     return infoPtr->dwExtStyle & CBES_EX_CASESENSITIVE ? lstrcmpW : lstrcmpiW;
185 }
186
187 static INT COMBOEX_Notify (COMBOEX_INFO *infoPtr, INT code, NMHDR *hdr)
188 {
189     hdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
190     hdr->hwndFrom = infoPtr->hwndSelf;
191     hdr->code = code;
192     if (infoPtr->NtfUnicode)
193         return SendMessageW (GetParent(infoPtr->hwndSelf), WM_NOTIFY, 0,
194                              (LPARAM)hdr);
195     else
196         return SendMessageA (GetParent(infoPtr->hwndSelf), WM_NOTIFY, 0,
197                              (LPARAM)hdr);
198 }
199
200
201 static INT
202 COMBOEX_NotifyItem (COMBOEX_INFO *infoPtr, INT code, NMCOMBOBOXEXW *hdr)
203 {
204     /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
205     if (infoPtr->NtfUnicode)
206         return COMBOEX_Notify (infoPtr, code, &hdr->hdr);
207     else {
208         LPWSTR wstr = hdr->ceItem.pszText;
209         LPSTR astr = 0;
210         INT ret, len = 0;
211
212         if ((hdr->ceItem.mask & CBEIF_TEXT) && is_textW(wstr)) {
213             len = WideCharToMultiByte (CP_ACP, 0, wstr, -1, 0, 0, NULL, NULL);
214             if (len > 0) {
215                 astr = (LPSTR)COMCTL32_Alloc ((len + 1)*sizeof(CHAR));
216                 if (!astr) return 0;
217                 WideCharToMultiByte (CP_ACP, 0, wstr, -1, astr, len, 0, 0);
218                 hdr->ceItem.pszText = (LPWSTR)astr;
219             }
220         }
221
222         if (code == CBEN_ENDEDITW) code = CBEN_ENDEDITA;
223         else if (code == CBEN_GETDISPINFOW) code = CBEN_GETDISPINFOA;
224         else if (code == CBEN_DRAGBEGINW) code = CBEN_DRAGBEGINA;
225
226         ret = COMBOEX_Notify (infoPtr, code, (NMHDR *)hdr);
227
228         if (astr && hdr->ceItem.pszText == (LPWSTR)astr)
229             hdr->ceItem.pszText = wstr;
230
231         if (astr) COMCTL32_Free(astr);
232
233         return ret;
234     }
235 }
236
237
238 static INT COMBOEX_NotifyEndEdit (COMBOEX_INFO *infoPtr, NMCBEENDEDITW *neew, LPCWSTR wstr)
239 {
240     /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
241     if (infoPtr->NtfUnicode) {
242         lstrcpynW(neew->szText, wstr, CBEMAXSTRLEN);
243         return COMBOEX_Notify (infoPtr, CBEN_ENDEDITW, &neew->hdr);
244     } else {
245         NMCBEENDEDITA neea;
246
247         memcpy (&neea.hdr, &neew->hdr, sizeof(NMHDR));
248         neea.fChanged = neew->fChanged;
249         neea.iNewSelection = neew->iNewSelection;
250         WideCharToMultiByte (CP_ACP, 0, wstr, -1, neea.szText, CBEMAXSTRLEN, 0, 0);
251         neea.iWhy = neew->iWhy;
252
253         return COMBOEX_Notify (infoPtr, CBEN_ENDEDITA, &neea.hdr);
254     }
255 }
256
257
258 static void COMBOEX_NotifyDragBegin(COMBOEX_INFO *infoPtr, LPCWSTR wstr)
259 {
260     /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
261     if (infoPtr->NtfUnicode) {
262         NMCBEDRAGBEGINW ndbw;
263
264         ndbw.iItemid = -1;
265         lstrcpynW(ndbw.szText, wstr, CBEMAXSTRLEN);
266         COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINW, &ndbw.hdr);
267     } else {
268         NMCBEDRAGBEGINA ndba;
269
270         ndba.iItemid = -1;
271         WideCharToMultiByte (CP_ACP, 0, wstr, -1, ndba.szText, CBEMAXSTRLEN, 0, 0);
272
273         COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINA, &ndba.hdr);
274     }
275 }
276
277
278 static void COMBOEX_FreeText (CBE_ITEMDATA *item)
279 {
280     if (is_textW(item->pszText)) COMCTL32_Free(item->pszText);
281     item->pszText = 0;
282     if (item->pszTemp) COMCTL32_Free(item->pszTemp);
283     item->pszTemp = 0;
284 }
285
286
287 static LPCWSTR COMBOEX_GetText(COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
288 {
289     NMCOMBOBOXEXW nmce;
290     LPWSTR text, buf;
291     INT len;
292
293     if (item->pszText != LPSTR_TEXTCALLBACKW)
294         return item->pszText;
295
296     ZeroMemory(&nmce, sizeof(nmce));
297     nmce.ceItem.mask = CBEIF_TEXT;
298     nmce.ceItem.lParam = item->lParam;
299     COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
300
301     if (is_textW(nmce.ceItem.pszText)) {
302         len = MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, NULL, 0);
303         buf = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
304         if (buf)
305             MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, buf, len);
306         if (nmce.ceItem.mask & CBEIF_DI_SETITEM) {
307             COMBOEX_FreeText(item);
308             item->pszText = buf;
309         } else {
310             if (item->pszTemp) COMCTL32_Free(item->pszTemp);
311             item->pszTemp = buf;
312         }
313         text = buf;
314     } else
315         text = nmce.ceItem.pszText;
316
317     if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
318         item->pszText = text;
319     return text;
320 }
321
322
323 static void COMBOEX_GetComboFontSize (COMBOEX_INFO *infoPtr, SIZE *size)
324 {
325     HFONT nfont, ofont;
326     HDC mydc;
327
328     mydc = GetDC (0); /* why the entire screen???? */
329     nfont = (HFONT)SendMessageW (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
330     ofont = (HFONT) SelectObject (mydc, nfont);
331     GetTextExtentPointA (mydc, "A", 1, size);
332     SelectObject (mydc, ofont);
333     ReleaseDC (0, mydc);
334     TRACE("selected font hwnd=%p, height=%ld\n", nfont, size->cy);
335 }
336
337
338 static void COMBOEX_CopyItem (CBE_ITEMDATA *item, COMBOBOXEXITEMW *cit)
339 {
340     if (cit->mask & CBEIF_TEXT) {
341         /*
342          * when given a text buffer actually use that buffer
343          */
344         if (cit->pszText) {
345             if (is_textW(item->pszText))
346                 lstrcpynW(cit->pszText, item->pszText, cit->cchTextMax);
347             else
348                 cit->pszText[0] = 0;
349         } else {
350             cit->pszText        = item->pszText;
351             cit->cchTextMax     = item->cchTextMax;
352         }
353     }
354     if (cit->mask & CBEIF_IMAGE)
355         cit->iImage         = item->iImage;
356     if (cit->mask & CBEIF_SELECTEDIMAGE)
357         cit->iSelectedImage = item->iSelectedImage;
358     if (cit->mask & CBEIF_OVERLAY)
359         cit->iOverlay       = item->iOverlay;
360     if (cit->mask & CBEIF_INDENT)
361         cit->iIndent        = item->iIndent;
362     if (cit->mask & CBEIF_LPARAM)
363         cit->lParam         = item->lParam;
364 }
365
366
367 static void COMBOEX_AdjustEditPos (COMBOEX_INFO *infoPtr)
368 {
369     SIZE mysize;
370     INT x, y, w, h, xioff;
371     RECT rect;
372
373     if (!infoPtr->hwndEdit) return;
374
375     if (infoPtr->himl && !(infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGEINDENT)) {
376         IMAGEINFO iinfo;
377         iinfo.rcImage.left = iinfo.rcImage.right = 0;
378         ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
379         xioff = iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP;
380     }  else xioff = 0;
381
382     GetClientRect (infoPtr->hwndCombo, &rect);
383     InflateRect (&rect, -2, -2);
384     InvalidateRect (infoPtr->hwndCombo, &rect, TRUE);
385
386     /* reposition the Edit control based on whether icon exists */
387     COMBOEX_GetComboFontSize (infoPtr, &mysize);
388     TRACE("Combo font x=%ld, y=%ld\n", mysize.cx, mysize.cy);
389     x = xioff + CBE_STARTOFFSET + 1;
390     w = rect.right-rect.left - x - GetSystemMetrics(SM_CXVSCROLL) - 1;
391     h = mysize.cy + 1;
392     y = rect.bottom - h - 1;
393
394     TRACE("Combo client (%ld,%ld)-(%ld,%ld), setting Edit to (%d,%d)-(%d,%d)\n",
395           rect.left, rect.top, rect.right, rect.bottom, x, y, x + w, y + h);
396     SetWindowPos(infoPtr->hwndEdit, HWND_TOP, x, y, w, h,
397                  SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER);
398 }
399
400
401 static void COMBOEX_ReSize (COMBOEX_INFO *infoPtr)
402 {
403     SIZE mysize;
404     UINT cy;
405     IMAGEINFO iinfo;
406
407     COMBOEX_GetComboFontSize (infoPtr, &mysize);
408     cy = mysize.cy + CBE_EXTRA;
409     if (infoPtr->himl && ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo)) {
410         cy = max (iinfo.rcImage.bottom - iinfo.rcImage.top, cy);
411         TRACE("upgraded height due to image:  height=%d\n", cy);
412     }
413     SendMessageW (infoPtr->hwndSelf, CB_SETITEMHEIGHT, (WPARAM)-1, (LPARAM)cy);
414     if (infoPtr->hwndCombo) {
415         SendMessageW (infoPtr->hwndCombo, CB_SETITEMHEIGHT,
416                       (WPARAM) 0, (LPARAM) cy);
417         if ( !(infoPtr->flags & CBES_EX_NOSIZELIMIT)) {
418             RECT comboRect;
419             if (GetWindowRect(infoPtr->hwndCombo, &comboRect)) {
420                 RECT ourRect;
421                 if (GetWindowRect(infoPtr->hwndSelf, &ourRect)) {
422                     if (comboRect.bottom > ourRect.bottom) {
423                         POINT pt = { ourRect.left, ourRect.top };
424                         if (ScreenToClient(infoPtr->hwndSelf, &pt))
425                             MoveWindow( infoPtr->hwndSelf, pt.x, pt.y, ourRect.right - ourRect.left,
426                                         comboRect.bottom - comboRect.top, FALSE);
427                     }
428                 }
429             }
430         }
431     }
432 }
433
434
435 static void COMBOEX_SetEditText (COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
436 {
437     if (!infoPtr->hwndEdit) return;
438     /* native issues the following messages to the {Edit} control */
439     /*      WM_SETTEXT (0,addr)     */
440     /*      EM_SETSEL32 (0,0)       */
441     /*      EM_SETSEL32 (0,-1)      */
442     if (item->mask & CBEIF_TEXT) {
443         SendMessageW (infoPtr->hwndEdit, WM_SETTEXT, 0, (LPARAM)COMBOEX_GetText(infoPtr, item));
444         SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, 0);
445         SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, -1);
446     }
447 }
448
449
450 static CBE_ITEMDATA * COMBOEX_FindItem(COMBOEX_INFO *infoPtr, INT index)
451 {
452     CBE_ITEMDATA *item;
453     INT i;
454
455     if ((index > infoPtr->nb_items) || (index < -1))
456         return 0;
457     if (index == -1)
458         return infoPtr->edit;
459     item = infoPtr->items;
460     i = infoPtr->nb_items - 1;
461
462     /* find the item in the list */
463     while (item && (i > index)) {
464         item = (CBE_ITEMDATA *)item->next;
465         i--;
466     }
467     if (!item || (i != index)) {
468         ERR("COMBOBOXEX item structures broken. Please report!\n");
469         return 0;
470     }
471     return item;
472 }
473
474
475 static inline BOOL COMBOEX_HasEdit(COMBOEX_INFO *infoPtr)
476 {
477     return infoPtr->hwndEdit ? TRUE : FALSE;
478 }
479
480
481 /* ***  CBEM_xxx message support  *** */
482
483
484 static INT COMBOEX_DeleteItem (COMBOEX_INFO *infoPtr, INT index)
485 {
486     CBE_ITEMDATA *item;
487
488     TRACE("(index=%d)\n", index);
489
490     /* if item number requested does not exist then return failure */
491     if ((index > infoPtr->nb_items) || (index < 0)) return CB_ERR;
492     if (!(item = COMBOEX_FindItem(infoPtr, index))) return CB_ERR;
493
494     /* doing this will result in WM_DELETEITEM being issued */
495     SendMessageW (infoPtr->hwndCombo, CB_DELETESTRING, (WPARAM)index, 0);
496
497     return infoPtr->nb_items;
498 }
499
500
501 static BOOL COMBOEX_GetItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW *cit)
502 {
503     INT index = cit->iItem;
504     CBE_ITEMDATA *item;
505
506     TRACE("(...)\n");
507
508     /* if item number requested does not exist then return failure */
509     if ((index > infoPtr->nb_items) || (index < -1)) return FALSE;
510
511     /* if the item is the edit control and there is no edit control, skip */
512     if ((index == -1) && !COMBOEX_HasEdit(infoPtr)) return FALSE;
513
514     if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
515
516     COMBOEX_CopyItem (item, cit);
517
518     return TRUE;
519 }
520
521
522 static BOOL COMBOEX_GetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA *cit)
523 {
524     COMBOBOXEXITEMW tmpcit;
525
526     TRACE("(...)\n");
527
528     tmpcit.mask = cit->mask;
529     tmpcit.iItem = cit->iItem;
530     tmpcit.pszText = 0;
531     if(!COMBOEX_GetItemW (infoPtr, &tmpcit)) return FALSE;
532
533     if (is_textW(tmpcit.pszText) && cit->pszText)
534         WideCharToMultiByte (CP_ACP, 0, tmpcit.pszText, -1,
535                              cit->pszText, cit->cchTextMax, NULL, NULL);
536     else if (cit->pszText) cit->pszText[0] = 0;
537     else cit->pszText = (LPSTR)tmpcit.pszText;
538
539     cit->iImage = tmpcit.iImage;
540     cit->iSelectedImage = tmpcit.iSelectedImage;
541     cit->iOverlay = tmpcit.iOverlay;
542     cit->iIndent = tmpcit.iIndent;
543     cit->lParam = tmpcit.lParam;
544
545     return TRUE;
546 }
547
548
549 inline static BOOL COMBOEX_HasEditChanged (COMBOEX_INFO *infoPtr)
550 {
551     return COMBOEX_HasEdit(infoPtr) &&
552            (infoPtr->flags & WCBE_EDITHASCHANGED) == WCBE_EDITHASCHANGED;
553 }
554
555
556 static INT COMBOEX_InsertItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW *cit)
557 {
558     INT index;
559     CBE_ITEMDATA *item;
560     NMCOMBOBOXEXW nmcit;
561
562     TRACE("\n");
563
564     if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
565
566     /* get real index of item to insert */
567     index = cit->iItem;
568     if (index == -1) index = infoPtr->nb_items;
569     if (index > infoPtr->nb_items) index = infoPtr->nb_items;
570
571     /* get zero-filled space and chain it in */
572     if(!(item = (CBE_ITEMDATA *)COMCTL32_Alloc (sizeof(*item)))) return -1;
573
574     /* locate position to insert new item in */
575     if (index == infoPtr->nb_items) {
576         /* fast path for iItem = -1 */
577         item->next = infoPtr->items;
578         infoPtr->items = item;
579     }
580     else {
581         INT i = infoPtr->nb_items-1;
582         CBE_ITEMDATA *moving = infoPtr->items;
583
584         while ((i > index) && moving) {
585             moving = (CBE_ITEMDATA *)moving->next;
586             i--;
587         }
588         if (!moving) {
589             ERR("COMBOBOXEX item structures broken. Please report!\n");
590             COMCTL32_Free(item);
591             return -1;
592         }
593         item->next = moving->next;
594         moving->next = item;
595     }
596
597     /* fill in our hidden item structure */
598     item->mask = cit->mask;
599     if (item->mask & CBEIF_TEXT) {
600         INT len = 0;
601
602         if (is_textW(cit->pszText)) len = strlenW (cit->pszText);
603         if (len > 0) {
604             item->pszText = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
605             if (!item->pszText) {
606                 COMCTL32_Free(item);
607                 return -1;
608             }
609             strcpyW (item->pszText, cit->pszText);
610         }
611         else if (cit->pszText == LPSTR_TEXTCALLBACKW)
612             item->pszText = LPSTR_TEXTCALLBACKW;
613         item->cchTextMax = cit->cchTextMax;
614     }
615     if (item->mask & CBEIF_IMAGE)
616         item->iImage = cit->iImage;
617     if (item->mask & CBEIF_SELECTEDIMAGE)
618         item->iSelectedImage = cit->iSelectedImage;
619     if (item->mask & CBEIF_OVERLAY)
620         item->iOverlay = cit->iOverlay;
621     if (item->mask & CBEIF_INDENT)
622         item->iIndent = cit->iIndent;
623     if (item->mask & CBEIF_LPARAM)
624         item->lParam = cit->lParam;
625     infoPtr->nb_items++;
626
627     if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
628
629     SendMessageW (infoPtr->hwndCombo, CB_INSERTSTRING,
630                   (WPARAM)cit->iItem, (LPARAM)item);
631
632     memset (&nmcit.ceItem, 0, sizeof(nmcit.ceItem));
633     COMBOEX_CopyItem (item, &nmcit.ceItem);
634     COMBOEX_NotifyItem (infoPtr, CBEN_INSERTITEM, &nmcit);
635
636     return index;
637
638 }
639
640
641 static INT COMBOEX_InsertItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA *cit)
642 {
643     COMBOBOXEXITEMW citW;
644     LPWSTR wstr = NULL;
645     INT ret;
646
647     memcpy(&citW,cit,sizeof(COMBOBOXEXITEMA));
648     if (cit->mask & CBEIF_TEXT && is_textA(cit->pszText)) {
649         INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
650         wstr = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
651         if (!wstr) return -1;
652         MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
653         citW.pszText = wstr;
654     }
655     ret = COMBOEX_InsertItemW(infoPtr, &citW);
656
657     if (wstr) COMCTL32_Free(wstr);
658
659     return ret;
660 }
661
662
663 static DWORD
664 COMBOEX_SetExtendedStyle (COMBOEX_INFO *infoPtr, DWORD mask, DWORD style)
665 {
666     DWORD dwTemp;
667
668     TRACE("(mask=x%08lx, style=0x%08lx)\n", mask, style);
669
670     dwTemp = infoPtr->dwExtStyle;
671
672     if (mask)
673         infoPtr->dwExtStyle = (infoPtr->dwExtStyle & ~mask) | style;
674     else
675         infoPtr->dwExtStyle = style;
676
677     /* see if we need to change the word break proc on the edit */
678     if ((infoPtr->dwExtStyle ^ dwTemp) & CBES_EX_PATHWORDBREAKPROC) {
679         SendMessageW(infoPtr->hwndEdit, EM_SETWORDBREAKPROC, 0,
680                      (infoPtr->dwExtStyle & CBES_EX_PATHWORDBREAKPROC) ?
681                          (LPARAM)COMBOEX_PathWordBreakProc : 0);
682     }
683
684     /* test if the control's appearance has changed */
685     mask = CBES_EX_NOEDITIMAGE | CBES_EX_NOEDITIMAGEINDENT;
686     if ((infoPtr->dwExtStyle & mask) != (dwTemp & mask)) {
687         /* if state of EX_NOEDITIMAGE changes, invalidate all */
688         TRACE("EX_NOEDITIMAGE state changed to %ld\n",
689               infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGE);
690         InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
691         COMBOEX_AdjustEditPos (infoPtr);
692         if (infoPtr->hwndEdit)
693             InvalidateRect (infoPtr->hwndEdit, NULL, TRUE);
694     }
695
696     return dwTemp;
697 }
698
699
700 static HIMAGELIST COMBOEX_SetImageList (COMBOEX_INFO *infoPtr, HIMAGELIST himl)
701 {
702     HIMAGELIST himlTemp = infoPtr->himl;
703
704     TRACE("(...)\n");
705
706     infoPtr->himl = himl;
707
708     COMBOEX_ReSize (infoPtr);
709     InvalidateRect (infoPtr->hwndCombo, NULL, TRUE);
710
711     /* reposition the Edit control based on whether icon exists */
712     COMBOEX_AdjustEditPos (infoPtr);
713     return himlTemp;
714 }
715
716 static BOOL COMBOEX_SetItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW *cit)
717 {
718     INT index = cit->iItem;
719     CBE_ITEMDATA *item;
720
721     if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
722
723     /* if item number requested does not exist then return failure */
724     if ((index > infoPtr->nb_items) || (index < -1)) return FALSE;
725
726     /* if the item is the edit control and there is no edit control, skip */
727     if ((index == -1) && !COMBOEX_HasEdit(infoPtr)) return FALSE;
728
729     if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
730
731     /* add/change stuff to the internal item structure */
732     item->mask |= cit->mask;
733     if (cit->mask & CBEIF_TEXT) {
734         INT len = 0;
735
736         COMBOEX_FreeText(item);
737         if (is_textW(cit->pszText)) len = strlenW(cit->pszText);
738         if (len > 0) {
739             item->pszText = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
740             if (!item->pszText) return FALSE;
741             strcpyW(item->pszText, cit->pszText);
742         } else if (cit->pszText == LPSTR_TEXTCALLBACKW)
743             item->pszText = LPSTR_TEXTCALLBACKW;
744         item->cchTextMax = cit->cchTextMax;
745     }
746     if (cit->mask & CBEIF_IMAGE)
747         item->iImage = cit->iImage;
748     if (cit->mask & CBEIF_SELECTEDIMAGE)
749         item->iSelectedImage = cit->iSelectedImage;
750     if (cit->mask & CBEIF_OVERLAY)
751         item->iOverlay = cit->iOverlay;
752     if (cit->mask & CBEIF_INDENT)
753         item->iIndent = cit->iIndent;
754     if (cit->mask & CBEIF_LPARAM)
755         cit->lParam = cit->lParam;
756
757     if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
758
759     /* if original request was to update edit control, do some fast foot work */
760     if (cit->iItem == -1) {
761         COMBOEX_SetEditText (infoPtr, item);
762         RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE | RDW_INVALIDATE);
763     }
764     return TRUE;
765 }
766
767 static BOOL COMBOEX_SetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA *cit)
768 {
769     COMBOBOXEXITEMW citW;
770     LPWSTR wstr = NULL;
771     BOOL ret;
772
773     memcpy(&citW, cit, sizeof(COMBOBOXEXITEMA));
774     if ((cit->mask & CBEIF_TEXT) && is_textA(cit->pszText)) {
775         INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
776         wstr = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
777         if (!wstr) return FALSE;
778         MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
779         citW.pszText = wstr;
780     }
781     ret = COMBOEX_SetItemW(infoPtr, &citW);
782
783     if (wstr) COMCTL32_Free(wstr);
784
785     return ret;
786 }
787
788
789 static BOOL COMBOEX_SetUnicodeFormat (COMBOEX_INFO *infoPtr, BOOL value)
790 {
791     BOOL bTemp = infoPtr->unicode;
792
793     TRACE("to %s, was %s\n", value ? "TRUE":"FALSE", bTemp ? "TRUE":"FALSE");
794
795     infoPtr->unicode = value;
796
797     return bTemp;
798 }
799
800
801 /* ***  CB_xxx message support  *** */
802
803 static INT
804 COMBOEX_FindStringExact (COMBOEX_INFO *infoPtr, INT start, LPCWSTR str)
805 {
806     INT i;
807     cmp_func_t cmptext = get_cmp_func(infoPtr);
808     INT count = SendMessageW (infoPtr->hwndCombo, CB_GETCOUNT, 0, 0);
809
810     /* now search from after starting loc and wrapping back to start */
811     for(i=start+1; i<count; i++) {
812         CBE_ITEMDATA *item = get_item_data(infoPtr, i);
813         if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
814     }
815     for(i=0; i<=start; i++) {
816         CBE_ITEMDATA *item = get_item_data(infoPtr, i);
817         if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
818     }
819     return CB_ERR;
820 }
821
822
823 static DWORD COMBOEX_GetItemData (COMBOEX_INFO *infoPtr, INT index)
824 {
825     CBE_ITEMDATA *item1, *item2;
826     DWORD ret = 0;
827
828     item1 = get_item_data(infoPtr, index);
829     if ((item1 != NULL) && ((LRESULT)item1 != CB_ERR)) {
830         item2 = COMBOEX_FindItem (infoPtr, index);
831         if (item2 != item1) {
832             ERR("data structures damaged!\n");
833             return CB_ERR;
834         }
835         if (item1->mask & CBEIF_LPARAM) ret = item1->lParam;
836         TRACE("returning 0x%08lx\n", ret);
837     } else {
838         ret = (DWORD)item1;
839         TRACE("non-valid result from combo, returning 0x%08lx\n", ret);
840     }
841     return ret;
842 }
843
844
845 static INT COMBOEX_SetCursel (COMBOEX_INFO *infoPtr, INT index)
846 {
847     CBE_ITEMDATA *item;
848     INT sel;
849
850     if (!(item = COMBOEX_FindItem(infoPtr, index)))
851         return SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, index, 0);
852
853     TRACE("selecting item %d text=%s\n", index, debugstr_txt(item->pszText));
854     infoPtr->selected = index;
855
856     sel = (INT)SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, index, 0);
857     COMBOEX_SetEditText (infoPtr, item);
858     return sel;
859 }
860
861
862 static DWORD COMBOEX_SetItemData (COMBOEX_INFO *infoPtr, INT index, DWORD data)
863 {
864     CBE_ITEMDATA *item1, *item2;
865
866     item1 = get_item_data(infoPtr, index);
867     if ((item1 != NULL) && ((LRESULT)item1 != CB_ERR)) {
868         item2 = COMBOEX_FindItem (infoPtr, index);
869         if (item2 != item1) {
870             ERR("data structures damaged!\n");
871             return CB_ERR;
872         }
873         item1->mask |= CBEIF_LPARAM;
874         item1->lParam = data;
875         TRACE("setting lparam to 0x%08lx\n", data);
876         return 0;
877     }
878     TRACE("non-valid result from combo 0x%08lx\n", (DWORD)item1);
879     return (LRESULT)item1;
880 }
881
882
883 static INT COMBOEX_SetItemHeight (COMBOEX_INFO *infoPtr, INT index, UINT height)
884 {
885     RECT cb_wrect, cbx_wrect, cbx_crect;
886
887     /* First, lets forward the message to the normal combo control
888        just like Windows.     */
889     if (infoPtr->hwndCombo)
890        if (SendMessageW (infoPtr->hwndCombo, CB_SETITEMHEIGHT,
891                          index, height) == CB_ERR) return CB_ERR;
892
893     GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
894     GetWindowRect (infoPtr->hwndSelf, &cbx_wrect);
895     GetClientRect (infoPtr->hwndSelf, &cbx_crect);
896     /* the height of comboex as height of the combo + comboex border */
897     height = cb_wrect.bottom-cb_wrect.top
898              + cbx_wrect.bottom-cbx_wrect.top
899              - (cbx_crect.bottom-cbx_crect.top);
900     TRACE("EX window=(%ld,%ld)-(%ld,%ld), client=(%ld,%ld)-(%ld,%ld)\n",
901           cbx_wrect.left, cbx_wrect.top, cbx_wrect.right, cbx_wrect.bottom,
902           cbx_crect.left, cbx_crect.top, cbx_crect.right, cbx_crect.bottom);
903     TRACE("CB window=(%ld,%ld)-(%ld,%ld), EX setting=(0,0)-(%ld,%d)\n",
904           cb_wrect.left, cb_wrect.top, cb_wrect.right, cb_wrect.bottom,
905           cbx_wrect.right-cbx_wrect.left, height);
906     SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0,
907                   cbx_wrect.right-cbx_wrect.left, height,
908                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
909
910     return 0;
911 }
912
913
914 /* ***  WM_xxx message support  *** */
915
916
917 static LRESULT COMBOEX_Create (HWND hwnd, LPCREATESTRUCTA cs)
918 {
919     WCHAR COMBOBOX[] = { 'C', 'o', 'm', 'b', 'o', 'B', 'o', 'x', 0 };
920     WCHAR EDIT[] = { 'E', 'D', 'I', 'T', 0 };
921     WCHAR NIL[] = { 0 };
922     COMBOEX_INFO *infoPtr;
923     LOGFONTW mylogfont;
924     RECT wnrc1, clrc1, cmbwrc;
925     INT i;
926
927     /* allocate memory for info structure */
928     infoPtr = (COMBOEX_INFO *)COMCTL32_Alloc (sizeof(COMBOEX_INFO));
929     if (!infoPtr) return -1;
930
931     /* initialize info structure */
932     /* note that infoPtr is allocated zero-filled */
933
934     infoPtr->hwndSelf = hwnd;
935     infoPtr->selected = -1;
936
937     infoPtr->unicode = IsWindowUnicode (hwnd);
938
939     i = SendMessageW(GetParent (hwnd), WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
940     if ((i != NFR_ANSI) && (i != NFR_UNICODE)) {
941         WARN("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n", i);
942         i = NFR_ANSI;
943     }
944     infoPtr->NtfUnicode = (i == NFR_UNICODE);
945
946     SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
947
948     /* create combo box */
949     GetWindowRect(hwnd, &wnrc1);
950     GetClientRect(hwnd, &clrc1);
951     TRACE("EX window=(%ld,%ld)-(%ld,%ld) client=(%ld,%ld)-(%ld,%ld)\n",
952           wnrc1.left, wnrc1.top, wnrc1.right, wnrc1.bottom,
953           clrc1.left, clrc1.top, clrc1.right, clrc1.bottom);
954
955     /* Native version of ComboEx creates the ComboBox with DROPDOWNLIST */
956     /* specified. It then creates it's own version of the EDIT control  */
957     /* and makes the ComboBox the parent. This is because a normal      */
958     /* DROPDOWNLIST does not have a EDIT control, but we need one.      */
959     /* We also need to place the edit control at the proper location    */
960     /* (allow space for the icons).                                     */
961
962     infoPtr->hwndCombo = CreateWindowW (COMBOBOX, NIL,
963                          /* following line added to match native */
964                          WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VSCROLL |
965                          CBS_NOINTEGRALHEIGHT | CBS_DROPDOWNLIST |
966                          /* was base and is necessary */
967                          WS_CHILD | WS_VISIBLE | CBS_OWNERDRAWFIXED |
968                          GetWindowLongW (hwnd, GWL_STYLE),
969                          cs->y, cs->x, cs->cx, cs->cy, hwnd,
970                          (HMENU) GetWindowLongW (hwnd, GWL_ID),
971                          (HINSTANCE)GetWindowLongW (hwnd, GWL_HINSTANCE), NULL);
972
973     /*
974      * native does the following at this point according to trace:
975      *  GetWindowThreadProcessId(hwndCombo,0)
976      *  GetCurrentThreadId()
977      *  GetWindowThreadProcessId(hwndCombo, &???)
978      *  GetCurrentProcessId()
979      */
980
981     /*
982      * Setup a property to hold the pointer to the COMBOBOXEX
983      * data structure.
984      */
985     SetPropA(infoPtr->hwndCombo, COMBOEX_SUBCLASS_PROP, hwnd);
986     infoPtr->prevComboWndProc = (WNDPROC)SetWindowLongW(infoPtr->hwndCombo,
987                                 GWL_WNDPROC, (LONG)COMBOEX_ComboWndProc);
988     infoPtr->font = (HFONT)SendMessageW (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
989
990
991     /*
992      * Now create our own EDIT control so we can position it.
993      * It is created only for CBS_DROPDOWN style
994      */
995     if ((cs->style & CBS_DROPDOWNLIST) == CBS_DROPDOWN) {
996         infoPtr->hwndEdit = CreateWindowExW (0, EDIT, NIL,
997                     WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | ES_AUTOHSCROLL,
998                     0, 0, 0, 0,  /* will set later */
999                     infoPtr->hwndCombo,
1000                     (HMENU) GetWindowLongW (hwnd, GWL_ID),
1001                     (HINSTANCE)GetWindowLongW (hwnd, GWL_HINSTANCE), NULL);
1002
1003         /* native does the following at this point according to trace:
1004          *  GetWindowThreadProcessId(hwndEdit,0)
1005          *  GetCurrentThreadId()
1006          *  GetWindowThreadProcessId(hwndEdit, &???)
1007          *  GetCurrentProcessId()
1008          */
1009
1010         /*
1011          * Setup a property to hold the pointer to the COMBOBOXEX
1012          * data structure.
1013          */
1014         SetPropA(infoPtr->hwndEdit, COMBOEX_SUBCLASS_PROP, hwnd);
1015         infoPtr->prevEditWndProc = (WNDPROC)SetWindowLongW(infoPtr->hwndEdit,
1016                                  GWL_WNDPROC, (LONG)COMBOEX_EditWndProc);
1017         infoPtr->font = (HFONT)SendMessageW(infoPtr->hwndCombo, WM_GETFONT, 0, 0);
1018     }
1019
1020     /*
1021      * Locate the default font if necessary and then set it in
1022      * all associated controls
1023      */
1024     if (!infoPtr->font) {
1025         SystemParametersInfoW (SPI_GETICONTITLELOGFONT, sizeof(mylogfont),
1026                                &mylogfont, 0);
1027         infoPtr->font = infoPtr->defaultFont = CreateFontIndirectW (&mylogfont);
1028     }
1029     SendMessageW (infoPtr->hwndCombo, WM_SETFONT, (WPARAM)infoPtr->font, 0);
1030     if (infoPtr->hwndEdit) {
1031         SendMessageW (infoPtr->hwndEdit, WM_SETFONT, (WPARAM)infoPtr->font, 0);
1032         SendMessageW (infoPtr->hwndEdit, EM_SETMARGINS, (WPARAM)EC_USEFONTINFO, 0);
1033     }
1034
1035     COMBOEX_ReSize (infoPtr);
1036
1037     /* Above is fairly certain, below is much less certain. */
1038
1039     GetWindowRect(hwnd, &wnrc1);
1040     GetClientRect(hwnd, &clrc1);
1041     GetWindowRect(infoPtr->hwndCombo, &cmbwrc);
1042     TRACE("EX window=(%ld,%ld)-(%ld,%ld) client=(%ld,%ld)-(%ld,%ld) CB wnd=(%ld,%ld)-(%ld,%ld)\n",
1043           wnrc1.left, wnrc1.top, wnrc1.right, wnrc1.bottom,
1044           clrc1.left, clrc1.top, clrc1.right, clrc1.bottom,
1045           cmbwrc.left, cmbwrc.top, cmbwrc.right, cmbwrc.bottom);
1046     SetWindowPos(infoPtr->hwndCombo, HWND_TOP,
1047                  0, 0, wnrc1.right-wnrc1.left, wnrc1.bottom-wnrc1.top,
1048                  SWP_NOACTIVATE | SWP_NOREDRAW);
1049
1050     GetWindowRect(infoPtr->hwndCombo, &cmbwrc);
1051     TRACE("CB window=(%ld,%ld)-(%ld,%ld)\n",
1052           cmbwrc.left, cmbwrc.top, cmbwrc.right, cmbwrc.bottom);
1053     SetWindowPos(hwnd, HWND_TOP,
1054                  0, 0, cmbwrc.right-cmbwrc.left, cmbwrc.bottom-cmbwrc.top,
1055                  SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
1056
1057     COMBOEX_AdjustEditPos (infoPtr);
1058
1059     /*
1060      * Create an item structure to represent the data in the
1061      * EDIT control. It is allocated zero-filled.
1062      */
1063     infoPtr->edit = (CBE_ITEMDATA *)COMCTL32_Alloc (sizeof (CBE_ITEMDATA));
1064     if (!infoPtr->edit) {
1065         COMBOEX_Destroy(infoPtr);
1066         return -1;
1067     }
1068
1069     return 0;
1070 }
1071
1072
1073 static LRESULT COMBOEX_Command (COMBOEX_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1074 {
1075     LRESULT lret;
1076     INT command = HIWORD(wParam);
1077     CBE_ITEMDATA *item = 0;
1078     WCHAR wintext[520];
1079     INT cursel, n, oldItem;
1080     NMCBEENDEDITW cbeend;
1081     DWORD oldflags;
1082     HWND parent = GetParent (infoPtr->hwndSelf);
1083
1084     TRACE("for command %d\n", command);
1085
1086     switch (command)
1087     {
1088     case CBN_DROPDOWN:
1089         SetFocus (infoPtr->hwndCombo);
1090         ShowWindow (infoPtr->hwndEdit, SW_HIDE);
1091         return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1092
1093     case CBN_CLOSEUP:
1094         SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1095         /*
1096          * from native trace of first dropdown after typing in URL in IE4
1097          *  CB_GETCURSEL(Combo)
1098          *  GetWindowText(Edit)
1099          *  CB_GETCURSEL(Combo)
1100          *  CB_GETCOUNT(Combo)
1101          *  CB_GETITEMDATA(Combo, n)
1102          *  WM_NOTIFY(parent, CBEN_ENDEDITA|W)
1103          *  CB_GETCURSEL(Combo)
1104          *  CB_SETCURSEL(COMBOEX, n)
1105          *  SetFocus(Combo)
1106          * the rest is supposition
1107          */
1108         ShowWindow (infoPtr->hwndEdit, SW_SHOW);
1109         InvalidateRect (infoPtr->hwndCombo, 0, TRUE);
1110         InvalidateRect (infoPtr->hwndEdit, 0, TRUE);
1111         cursel = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1112         if (cursel == -1) {
1113             cmp_func_t cmptext = get_cmp_func(infoPtr);
1114             /* find match from edit against those in Combobox */
1115             GetWindowTextW (infoPtr->hwndEdit, wintext, 520);
1116             n = SendMessageW (infoPtr->hwndCombo, CB_GETCOUNT, 0, 0);
1117             for (cursel = 0; cursel < n; cursel++){
1118                 item = get_item_data(infoPtr, cursel);
1119                 if ((INT)item == CB_ERR) break;
1120                 if (!cmptext(COMBOEX_GetText(infoPtr, item), wintext)) break;
1121             }
1122             if ((cursel == n) || ((INT)item == CB_ERR)) {
1123                 TRACE("failed to find match??? item=%p cursel=%d\n",
1124                       item, cursel);
1125                 if (infoPtr->hwndEdit)
1126                     SetFocus(infoPtr->hwndEdit);
1127                 return 0;
1128             }
1129         }
1130         else {
1131             item = get_item_data(infoPtr, cursel);
1132             if ((INT)item == CB_ERR) {
1133                 TRACE("failed to find match??? item=%p cursel=%d\n",
1134                       item, cursel);
1135                 if (infoPtr->hwndEdit)
1136                     SetFocus(infoPtr->hwndEdit);
1137                 return 0;
1138             }
1139         }
1140
1141         /* Save flags for testing and reset them */
1142         oldflags = infoPtr->flags;
1143         infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1144
1145         if (oldflags & WCBE_ACTEDIT) {
1146             cbeend.fChanged = (oldflags & WCBE_EDITCHG);
1147             cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1148                                                  CB_GETCURSEL, 0, 0);
1149             cbeend.iWhy = CBENF_DROPDOWN;
1150
1151             if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, COMBOEX_GetText(infoPtr, item))) return 0;
1152         }
1153
1154         /* if selection has changed the set the new current selection */
1155         cursel = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1156         if ((oldflags & WCBE_EDITCHG) || (cursel != infoPtr->selected)) {
1157             infoPtr->selected = cursel;
1158             SendMessageW (infoPtr->hwndSelf, CB_SETCURSEL, cursel, 0);
1159             SetFocus(infoPtr->hwndCombo);
1160         }
1161         return 0;
1162
1163     case CBN_SELCHANGE:
1164         /*
1165          * CB_GETCURSEL(Combo)
1166          * CB_GETITEMDATA(Combo)   < simulated by COMBOEX_FindItem
1167          * lstrlenA
1168          * WM_SETTEXT(Edit)
1169          * WM_GETTEXTLENGTH(Edit)
1170          * WM_GETTEXT(Edit)
1171          * EM_SETSEL(Edit, 0,0)
1172          * WM_GETTEXTLENGTH(Edit)
1173          * WM_GETTEXT(Edit)
1174          * EM_SETSEL(Edit, 0,len)
1175          * return WM_COMMAND to parent
1176          */
1177         oldItem = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1178         if (!(item = COMBOEX_FindItem(infoPtr, oldItem))) {
1179             ERR("item %d not found. Problem!\n", oldItem);
1180             break;
1181         }
1182         infoPtr->selected = oldItem;
1183         COMBOEX_SetEditText (infoPtr, item);
1184         return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1185
1186     case CBN_SELENDOK:
1187         /*
1188          * We have to change the handle since we are the control
1189          * issuing the message. IE4 depends on this.
1190          */
1191         return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1192
1193     case CBN_KILLFOCUS:
1194         /*
1195          * from native trace:
1196          *
1197          *  pass to parent
1198          *  WM_GETTEXT(Edit, 104)
1199          *  CB_GETCURSEL(Combo) rets -1
1200          *  WM_NOTIFY(CBEN_ENDEDITA) with CBENF_KILLFOCUS
1201          *  CB_GETCURSEL(Combo)
1202          *  InvalidateRect(Combo, 0, 0)
1203          *  return 0
1204          */
1205         SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1206         if (infoPtr->flags & WCBE_ACTEDIT) {
1207             GetWindowTextW (infoPtr->hwndEdit, wintext, 260);
1208             cbeend.fChanged = (infoPtr->flags & WCBE_EDITCHG);
1209             cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1210                                                  CB_GETCURSEL, 0, 0);
1211             cbeend.iWhy = CBENF_KILLFOCUS;
1212
1213             infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1214             if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, wintext)) return 0;
1215         }
1216         /* possible CB_GETCURSEL */
1217         InvalidateRect (infoPtr->hwndCombo, 0, 0);
1218         return 0;
1219
1220     default:
1221         /*
1222          * We have to change the handle since we are the control
1223          * issuing the message. IE4 depends on this.
1224          * We also need to set the focus back to the Edit control
1225          * after passing the command to the parent of the ComboEx.
1226          */
1227         lret = SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1228         if (infoPtr->hwndEdit)
1229             SetFocus(infoPtr->hwndEdit);
1230         return lret;
1231     }
1232     return 0;
1233 }
1234
1235
1236 static BOOL COMBOEX_WM_DeleteItem (COMBOEX_INFO *infoPtr, DELETEITEMSTRUCT *dis)
1237 {
1238     CBE_ITEMDATA *item, *olditem;
1239     NMCOMBOBOXEXW nmcit;
1240     UINT i;
1241
1242     TRACE("CtlType=%08x, CtlID=%08x, itemID=%08x, hwnd=%p, data=%08lx\n",
1243           dis->CtlType, dis->CtlID, dis->itemID, dis->hwndItem, dis->itemData);
1244
1245     if (dis->itemID >= infoPtr->nb_items) return FALSE;
1246
1247     olditem = infoPtr->items;
1248     i = infoPtr->nb_items - 1;
1249
1250     if (i == dis->itemID) {
1251         infoPtr->items = infoPtr->items->next;
1252     }
1253     else {
1254         item = olditem;
1255         i--;
1256
1257         /* find the prior item in the list */
1258         while (item->next && (i > dis->itemID)) {
1259             item = (CBE_ITEMDATA *)item->next;
1260             i--;
1261         }
1262         if (!item->next || (i != dis->itemID)) {
1263             ERR("COMBOBOXEX item structures broken. Please report!\n");
1264             return FALSE;
1265         }
1266         olditem = item->next;
1267         item->next = (CBE_ITEMDATA *)((CBE_ITEMDATA *)item->next)->next;
1268     }
1269     infoPtr->nb_items--;
1270
1271     memset (&nmcit.ceItem, 0, sizeof(nmcit.ceItem));
1272     COMBOEX_CopyItem (olditem, &nmcit.ceItem);
1273     COMBOEX_NotifyItem (infoPtr, CBEN_DELETEITEM, &nmcit);
1274
1275     COMBOEX_FreeText(olditem);
1276     COMCTL32_Free(olditem);
1277
1278     return TRUE;
1279 }
1280
1281
1282 static LRESULT COMBOEX_DrawItem (COMBOEX_INFO *infoPtr, DRAWITEMSTRUCT *dis)
1283 {
1284     WCHAR nil[] = { 0 };
1285     CBE_ITEMDATA *item = 0;
1286     SIZE txtsize;
1287     RECT rect;
1288     LPCWSTR str = nil;
1289     UINT xbase, x, y;
1290     INT len;
1291     COLORREF nbkc, ntxc, bkc, txc;
1292     int drawimage, drawstate, xioff;
1293
1294     if (!IsWindowEnabled(infoPtr->hwndCombo)) return 0;
1295
1296     TRACE("DRAWITEMSTRUCT: CtlType=0x%08x CtlID=0x%08x\n",
1297           dis->CtlType, dis->CtlID);
1298     TRACE("itemID=0x%08x itemAction=0x%08x itemState=0x%08x\n",
1299           dis->itemID, dis->itemAction, dis->itemState);
1300     TRACE("hWnd=%p hDC=%p (%ld,%ld)-(%ld,%ld) itemData=0x%08lx\n",
1301           dis->hwndItem, dis->hDC, dis->rcItem.left,
1302           dis->rcItem.top, dis->rcItem.right, dis->rcItem.bottom,
1303           dis->itemData);
1304
1305     /* MSDN says:                                                       */
1306     /*     "itemID - Specifies the menu item identifier for a menu      */
1307     /*      item or the index of the item in a list box or combo box.   */
1308     /*      For an empty list box or combo box, this member can be -1.  */
1309     /*      This allows the application to draw only the focus          */
1310     /*      rectangle at the coordinates specified by the rcItem        */
1311     /*      member even though there are no items in the control.       */
1312     /*      This indicates to the user whether the list box or combo    */
1313     /*      box has the focus. How the bits are set in the itemAction   */
1314     /*      member determines whether the rectangle is to be drawn as   */
1315     /*      though the list box or combo box has the focus.             */
1316     if (dis->itemID == 0xffffffff) {
1317         if ( ( (dis->itemAction & ODA_FOCUS) && (dis->itemState & ODS_SELECTED)) ||
1318              ( (dis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)) && (dis->itemState & ODS_FOCUS) ) ) {
1319
1320             TRACE("drawing item -1 special focus, rect=(%ld,%ld)-(%ld,%ld)\n",
1321                   dis->rcItem.left, dis->rcItem.top,
1322                   dis->rcItem.right, dis->rcItem.bottom);
1323         }
1324         else if ((dis->CtlType == ODT_COMBOBOX) &&
1325                  (dis->itemAction == ODA_DRAWENTIRE)) {
1326             /* draw of edit control data */
1327
1328             /* testing */
1329             {
1330                 RECT exrc, cbrc, edrc;
1331                 GetWindowRect (infoPtr->hwndSelf, &exrc);
1332                 GetWindowRect (infoPtr->hwndCombo, &cbrc);
1333                 edrc.left=edrc.top=edrc.right=edrc.bottom=-1;
1334                 if (infoPtr->hwndEdit)
1335                     GetWindowRect (infoPtr->hwndEdit, &edrc);
1336                 TRACE("window rects ex=(%ld,%ld)-(%ld,%ld), cb=(%ld,%ld)-(%ld,%ld), ed=(%ld,%ld)-(%ld,%ld)\n",
1337                       exrc.left, exrc.top, exrc.right, exrc.bottom,
1338                       cbrc.left, cbrc.top, cbrc.right, cbrc.bottom,
1339                       edrc.left, edrc.top, edrc.right, edrc.bottom);
1340             }
1341         }
1342         else {
1343             ERR("NOT drawing item  -1 special focus, rect=(%ld,%ld)-(%ld,%ld), action=%08x, state=%08x\n",
1344                 dis->rcItem.left, dis->rcItem.top,
1345                 dis->rcItem.right, dis->rcItem.bottom,
1346                 dis->itemAction, dis->itemState);
1347             return 0;
1348         }
1349     }
1350
1351     /* If draw item is -1 (edit control) setup the item pointer */
1352     if (dis->itemID == 0xffffffff) {
1353         item = infoPtr->edit;
1354
1355         if (infoPtr->hwndEdit) {
1356             INT len;
1357
1358             /* free previous text of edit item */
1359             COMBOEX_FreeText(item);
1360             item->mask &= ~CBEIF_TEXT;
1361             if( (len = GetWindowTextLengthW(infoPtr->hwndEdit)) ) {
1362                 item->mask |= CBEIF_TEXT;
1363                 item->pszText = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
1364                 if (item->pszText)
1365                     GetWindowTextW(infoPtr->hwndEdit, item->pszText, len+1);
1366
1367                TRACE("edit control hwndEdit=%p, text len=%d str=%s\n",
1368                      infoPtr->hwndEdit, len, debugstr_txt(item->pszText));
1369             }
1370         }
1371     }
1372
1373
1374     /* if the item pointer is not set, then get the data and locate it */
1375     if (!item) {
1376         item = get_item_data(infoPtr, dis->itemID);
1377         if (item == (CBE_ITEMDATA *)CB_ERR) {
1378             ERR("invalid item for id %d \n", dis->itemID);
1379             return 0;
1380         }
1381     }
1382
1383     if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
1384
1385     xbase = CBE_STARTOFFSET;
1386     if ((item->mask & CBEIF_INDENT) && (dis->itemState & ODS_COMBOEXLBOX)) {
1387         INT indent = item->iIndent;
1388         if (indent == I_INDENTCALLBACK) {
1389             NMCOMBOBOXEXW nmce;
1390             ZeroMemory(&nmce, sizeof(nmce));
1391             nmce.ceItem.mask = CBEIF_INDENT;
1392             nmce.ceItem.lParam = item->lParam;
1393             COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1394             if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
1395                 item->iIndent = nmce.ceItem.iIndent;
1396             indent = nmce.ceItem.iIndent;
1397         }
1398         xbase += (indent * CBE_INDENT);
1399     }
1400
1401     drawimage = -2;
1402     drawstate = ILD_NORMAL;
1403     if (item->mask & CBEIF_IMAGE)
1404         drawimage = item->iImage;
1405     if (dis->itemState & ODS_COMBOEXLBOX) {
1406         /* drawing listbox entry */
1407         if (dis->itemState & ODS_SELECTED) {
1408             if (item->mask & CBEIF_SELECTEDIMAGE)
1409                 drawimage = item->iSelectedImage;
1410             drawstate = ILD_SELECTED;
1411         }
1412     } else {
1413         /* drawing combo/edit entry */
1414         if (IsWindowVisible(infoPtr->hwndEdit)) {
1415             /* if we have an edit control, the slave the
1416              * selection state to the Edit focus state
1417              */
1418             if (infoPtr->flags & WCBE_EDITFOCUSED) {
1419                 if (item->mask & CBEIF_SELECTEDIMAGE)
1420                     drawimage = item->iSelectedImage;
1421                 drawstate = ILD_SELECTED;
1422             }
1423         } else {
1424             /* if we don't have an edit control, use
1425              * the requested state.
1426              */
1427             if (dis->itemState & ODS_SELECTED) {
1428                 if (item->mask & CBEIF_SELECTEDIMAGE)
1429                     drawimage = item->iSelectedImage;
1430                 drawstate = ILD_SELECTED;
1431             }
1432         }
1433     }
1434
1435     if (infoPtr->himl && !(infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGEINDENT)) {
1436         IMAGEINFO iinfo;
1437         iinfo.rcImage.left = iinfo.rcImage.right = 0;
1438         ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
1439         xioff = iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP;
1440     }  else xioff = 0;
1441
1442     /* setup pointer to text to be drawn */
1443     str = COMBOEX_GetText(infoPtr, item);
1444     if (!str) str = nil;
1445
1446     len = strlenW (str);
1447     GetTextExtentPoint32W (dis->hDC, str, len, &txtsize);
1448
1449     if (dis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)) {
1450         int overlay = item->iOverlay;
1451
1452         if (drawimage == I_IMAGECALLBACK) {
1453             NMCOMBOBOXEXW nmce;
1454             ZeroMemory(&nmce, sizeof(nmce));
1455             nmce.ceItem.mask = (drawstate == ILD_NORMAL) ? CBEIF_IMAGE : CBEIF_SELECTEDIMAGE;
1456             nmce.ceItem.lParam = item->lParam;
1457             COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1458             if (drawstate == ILD_NORMAL) {
1459                 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) item->iImage = nmce.ceItem.iImage;
1460                 drawimage = nmce.ceItem.iImage;
1461             } else if (drawstate == ILD_SELECTED) {
1462                 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) item->iSelectedImage = nmce.ceItem.iSelectedImage;
1463                 drawimage =  nmce.ceItem.iSelectedImage;
1464             } else ERR("Bad draw state = %d\n", drawstate);
1465         }
1466
1467         if (overlay == I_IMAGECALLBACK) {
1468             NMCOMBOBOXEXW nmce;
1469             ZeroMemory(&nmce, sizeof(nmce));
1470             nmce.ceItem.mask = CBEIF_OVERLAY;
1471             nmce.ceItem.lParam = item->lParam;
1472             COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1473             if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
1474                 item->iOverlay = nmce.ceItem.iOverlay;
1475             overlay = nmce.ceItem.iOverlay;
1476         }
1477
1478         if (drawimage >= 0 &&
1479             !(infoPtr->dwExtStyle & (CBES_EX_NOEDITIMAGE | CBES_EX_NOEDITIMAGEINDENT))) {
1480             if (overlay > 0) ImageList_SetOverlayImage (infoPtr->himl, overlay, 1);
1481             ImageList_Draw (infoPtr->himl, drawimage, dis->hDC, xbase, dis->rcItem.top,
1482                             drawstate | (overlay > 0 ? INDEXTOOVERLAYMASK(1) : 0));
1483         }
1484
1485         /* now draw the text */
1486         if (!IsWindowVisible (infoPtr->hwndEdit)) {
1487             nbkc = GetSysColor ((dis->itemState & ODS_SELECTED) ?
1488                                 COLOR_HIGHLIGHT : COLOR_WINDOW);
1489             bkc = SetBkColor (dis->hDC, nbkc);
1490             ntxc = GetSysColor ((dis->itemState & ODS_SELECTED) ?
1491                                 COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT);
1492             txc = SetTextColor (dis->hDC, ntxc);
1493             x = xbase + xioff;
1494             y = dis->rcItem.top +
1495                 (dis->rcItem.bottom - dis->rcItem.top - txtsize.cy) / 2;
1496             rect.left = x;
1497             rect.right = x + txtsize.cx;
1498             rect.top = dis->rcItem.top + 1;
1499             rect.bottom = dis->rcItem.bottom - 1;
1500             TRACE("drawing item %d text, rect=(%ld,%ld)-(%ld,%ld)\n",
1501                   dis->itemID, rect.left, rect.top, rect.right, rect.bottom);
1502             ExtTextOutW (dis->hDC, x, y, ETO_OPAQUE | ETO_CLIPPED,
1503                          &rect, str, len, 0);
1504             SetBkColor (dis->hDC, bkc);
1505             SetTextColor (dis->hDC, txc);
1506         }
1507     }
1508
1509     if (dis->itemAction & ODA_FOCUS) {
1510         rect.left = xbase + xioff - 1;
1511         rect.right = rect.left + txtsize.cx + 2;
1512         rect.top = dis->rcItem.top;
1513         rect.bottom = dis->rcItem.bottom;
1514         DrawFocusRect(dis->hDC, &rect);
1515     }
1516
1517     return 0;
1518 }
1519
1520
1521 static LRESULT COMBOEX_Destroy (COMBOEX_INFO *infoPtr)
1522 {
1523     if (infoPtr->hwndCombo)
1524         DestroyWindow (infoPtr->hwndCombo);
1525
1526     if (infoPtr->edit) {
1527         COMCTL32_Free (infoPtr->edit);
1528         infoPtr->edit = 0;
1529     }
1530
1531     if (infoPtr->items) {
1532         CBE_ITEMDATA *item, *next;
1533
1534         item = infoPtr->items;
1535         while (item) {
1536             next = (CBE_ITEMDATA *)item->next;
1537             COMBOEX_FreeText (item);
1538             COMCTL32_Free (item);
1539             item = next;
1540         }
1541         infoPtr->items = 0;
1542     }
1543
1544     if (infoPtr->defaultFont)
1545         DeleteObject (infoPtr->defaultFont);
1546
1547     /* free comboex info data */
1548     COMCTL32_Free (infoPtr);
1549     SetWindowLongW (infoPtr->hwndSelf, 0, 0);
1550     return 0;
1551 }
1552
1553
1554 static LRESULT COMBOEX_MeasureItem (COMBOEX_INFO *infoPtr, MEASUREITEMSTRUCT *mis)
1555 {
1556     SIZE mysize;
1557     HDC hdc;
1558
1559     hdc = GetDC (0);
1560     GetTextExtentPointA (hdc, "W", 1, &mysize);
1561     ReleaseDC (0, hdc);
1562     mis->itemHeight = mysize.cy + CBE_EXTRA;
1563
1564     TRACE("adjusted height hwnd=%p, height=%d\n",
1565           infoPtr->hwndSelf, mis->itemHeight);
1566
1567     return 0;
1568 }
1569
1570
1571 static LRESULT COMBOEX_NCCreate (HWND hwnd)
1572 {
1573     /* WARNING: The COMBOEX_INFO structure is not yet created */
1574     DWORD oldstyle, newstyle;
1575
1576     oldstyle = (DWORD)GetWindowLongW (hwnd, GWL_STYLE);
1577     newstyle = oldstyle & ~(WS_VSCROLL | WS_HSCROLL);
1578     if (newstyle != oldstyle) {
1579         TRACE("req style %08lx, reseting style %08lx\n",
1580               oldstyle, newstyle);
1581         SetWindowLongW (hwnd, GWL_STYLE, newstyle);
1582     }
1583     return 1;
1584 }
1585
1586
1587 static LRESULT COMBOEX_NotifyFormat (COMBOEX_INFO *infoPtr, LPARAM lParam)
1588 {
1589     if (lParam == NF_REQUERY) {
1590         INT i = SendMessageW(GetParent (infoPtr->hwndSelf),
1591                          WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1592         infoPtr->NtfUnicode = (i == NFR_UNICODE) ? 1 : 0;
1593     }
1594     return infoPtr->NtfUnicode ? NFR_UNICODE : NFR_ANSI;
1595 }
1596
1597
1598 static LRESULT COMBOEX_Size (COMBOEX_INFO *infoPtr, INT width, INT height)
1599 {
1600     TRACE("(width=%d, height=%d)\n", width, height);
1601
1602     MoveWindow (infoPtr->hwndCombo, 0, 0, width, height, TRUE);
1603
1604     COMBOEX_AdjustEditPos (infoPtr);
1605
1606     return 0;
1607 }
1608
1609
1610 static LRESULT COMBOEX_WindowPosChanging (COMBOEX_INFO *infoPtr, WINDOWPOS *wp)
1611 {
1612     RECT cbx_wrect, cbx_crect, cb_wrect;
1613     UINT width, height;
1614
1615     GetWindowRect (infoPtr->hwndSelf, &cbx_wrect);
1616     GetClientRect (infoPtr->hwndSelf, &cbx_crect);
1617     GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
1618
1619     /* width is winpos value + border width of comboex */
1620     width = wp->cx
1621             + (cbx_wrect.right-cbx_wrect.left)
1622             - (cbx_crect.right-cbx_crect.left);
1623
1624     TRACE("winpos=(%d,%d %dx%d) flags=0x%08x\n",
1625           wp->x, wp->y, wp->cx, wp->cy, wp->flags);
1626     TRACE("EX window=(%ld,%ld)-(%ld,%ld), client=(%ld,%ld)-(%ld,%ld)\n",
1627           cbx_wrect.left, cbx_wrect.top, cbx_wrect.right, cbx_wrect.bottom,
1628           cbx_crect.left, cbx_crect.top, cbx_crect.right, cbx_crect.bottom);
1629     TRACE("CB window=(%ld,%ld)-(%ld,%ld), EX setting=(0,0)-(%d,%ld)\n",
1630           cb_wrect.left, cb_wrect.top, cb_wrect.right, cb_wrect.bottom,
1631           width, cb_wrect.bottom-cb_wrect.top);
1632
1633     if (width) SetWindowPos (infoPtr->hwndCombo, HWND_TOP, 0, 0,
1634                              width,
1635                              cb_wrect.bottom-cb_wrect.top,
1636                              SWP_NOACTIVATE);
1637
1638     GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
1639
1640     /* height is combo window height plus border width of comboex */
1641     height =   (cb_wrect.bottom-cb_wrect.top)
1642              + (cbx_wrect.bottom-cbx_wrect.top)
1643              - (cbx_crect.bottom-cbx_crect.top);
1644     if (wp->cy < height) wp->cy = height;
1645     if (infoPtr->hwndEdit) {
1646         COMBOEX_AdjustEditPos (infoPtr);
1647         InvalidateRect (infoPtr->hwndCombo, 0, TRUE);
1648     }
1649
1650     return 0;
1651 }
1652
1653 static inline int is_delimiter(WCHAR c)
1654 {
1655     switch(c) {
1656         case '/':
1657         case '\\':
1658         case '.':
1659             return TRUE;
1660     }
1661     return FALSE;
1662 }
1663
1664 static int CALLBACK
1665 COMBOEX_PathWordBreakProc(LPWSTR lpch, int ichCurrent, int cch, int code)
1666 {
1667     if (code == WB_ISDELIMITER) {
1668         return is_delimiter(lpch[ichCurrent]);
1669     } else {
1670         int dir = (code == WB_LEFT) ? -1 : 1;
1671         for(; 0 <= ichCurrent && ichCurrent < cch; ichCurrent += dir)
1672             if (is_delimiter(lpch[ichCurrent])) return ichCurrent;
1673     }
1674     return ichCurrent;
1675 }
1676
1677 static LRESULT WINAPI
1678 COMBOEX_EditWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1679 {
1680     HWND hwndComboex = (HWND)GetPropA(hwnd, COMBOEX_SUBCLASS_PROP);
1681     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwndComboex);
1682     NMCBEENDEDITW cbeend;
1683     WCHAR edit_text[260];
1684     COLORREF obkc;
1685     HDC hDC;
1686     RECT rect;
1687     LRESULT lret;
1688
1689     TRACE("hwnd=%p msg=%x wparam=%x lParam=%lx, info_ptr=%p\n",
1690           hwnd, uMsg, wParam, lParam, infoPtr);
1691
1692     if (!infoPtr) return 0;
1693
1694     switch (uMsg)
1695     {
1696
1697         case WM_CHAR:
1698             /* handle (ignore) the return character */
1699             if (wParam == VK_RETURN) return 0;
1700             /* all other characters pass into the real Edit */
1701             return CallWindowProcW (infoPtr->prevEditWndProc,
1702                                    hwnd, uMsg, wParam, lParam);
1703
1704         case WM_ERASEBKGND:
1705             /*
1706              * The following was determined by traces of the native
1707              */
1708             hDC = (HDC) wParam;
1709             obkc = SetBkColor (hDC, GetSysColor (COLOR_WINDOW));
1710             GetClientRect (hwnd, &rect);
1711             TRACE("erasing (%ld,%ld)-(%ld,%ld)\n",
1712                   rect.left, rect.top, rect.right, rect.bottom);
1713             ExtTextOutW (hDC, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
1714             SetBkColor (hDC, obkc);
1715             return CallWindowProcW (infoPtr->prevEditWndProc,
1716                                    hwnd, uMsg, wParam, lParam);
1717
1718         case WM_KEYDOWN: {
1719             INT oldItem, selected, step = 1;
1720             CBE_ITEMDATA *item;
1721
1722             switch ((INT)wParam)
1723             {
1724             case VK_ESCAPE:
1725                 /* native version seems to do following for COMBOEX */
1726                 /*
1727                  *   GetWindowTextA(Edit,&?, 0x104)             x
1728                  *   CB_GETCURSEL to Combo rets -1              x
1729                  *   WM_NOTIFY to COMBOEX parent (rebar)        x
1730                  *     (CBEN_ENDEDIT{A|W}
1731                  *      fChanged = FALSE                        x
1732                  *      inewSelection = -1                      x
1733                  *      txt="www.hoho"                          x
1734                  *      iWhy = 3                                x
1735                  *   CB_GETCURSEL to Combo rets -1              x
1736                  *   InvalidateRect(Combo, 0)                   x
1737                  *   WM_SETTEXT to Edit                         x
1738                  *   EM_SETSEL to Edit (0,0)                    x
1739                  *   EM_SETSEL to Edit (0,-1)                   x
1740                  *   RedrawWindow(Combo, 0, 0, 5)               x
1741                  */
1742                 TRACE("special code for VK_ESCAPE\n");
1743
1744                 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1745
1746                 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1747                 cbeend.fChanged = FALSE;
1748                 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1749                                                      CB_GETCURSEL, 0, 0);
1750                 cbeend.iWhy = CBENF_ESCAPE;
1751
1752                 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) return 0;
1753                 oldItem = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1754                 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1755                 if (!(item = COMBOEX_FindItem(infoPtr, oldItem))) {
1756                     ERR("item %d not found. Problem!\n", oldItem);
1757                     break;
1758                 }
1759                 infoPtr->selected = oldItem;
1760                 COMBOEX_SetEditText (infoPtr, item);
1761                 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE |
1762                               RDW_INVALIDATE);
1763                 break;
1764
1765             case VK_RETURN:
1766                 /* native version seems to do following for COMBOEX */
1767                 /*
1768                  *   GetWindowTextA(Edit,&?, 0x104)             x
1769                  *   CB_GETCURSEL to Combo  rets -1             x
1770                  *   CB_GETCOUNT to Combo  rets 0
1771                  *   if >0 loop
1772                  *       CB_GETITEMDATA to match
1773                  * *** above 3 lines simulated by FindItem      x
1774                  *   WM_NOTIFY to COMBOEX parent (rebar)        x
1775                  *     (CBEN_ENDEDIT{A|W}                       x
1776                  *        fChanged = TRUE (-1)                  x
1777                  *        iNewSelection = -1 or selected        x
1778                  *        txt=                                  x
1779                  *        iWhy = 2 (CBENF_RETURN)               x
1780                  *   CB_GETCURSEL to Combo  rets -1             x
1781                  *   if -1 send CB_SETCURSEL to Combo -1        x
1782                  *   InvalidateRect(Combo, 0, 0)                x
1783                  *   SetFocus(Edit)                             x
1784                  *   CallWindowProc(406615a8, Edit, 0x100, 0xd, 0x1c0001)
1785                  */
1786
1787                 TRACE("special code for VK_RETURN\n");
1788
1789                 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1790
1791                 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1792                 selected = SendMessageW (infoPtr->hwndCombo,
1793                                          CB_GETCURSEL, 0, 0);
1794
1795                 if (selected != -1) {
1796                     cmp_func_t cmptext = get_cmp_func(infoPtr);
1797                     item = COMBOEX_FindItem (infoPtr, selected);
1798                     TRACE("handling VK_RETURN, selected = %d, selected_text=%s\n",
1799                           selected, debugstr_txt(item->pszText));
1800                     TRACE("handling VK_RETURN, edittext=%s\n",
1801                           debugstr_w(edit_text));
1802                     if (cmptext (COMBOEX_GetText(infoPtr, item), edit_text)) {
1803                         /* strings not equal -- indicate edit has changed */
1804                         selected = -1;
1805                     }
1806                 }
1807
1808                 cbeend.iNewSelection = selected;
1809                 cbeend.fChanged = TRUE;
1810                 cbeend.iWhy = CBENF_RETURN;
1811                 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) {
1812                     /* abort the change, restore previous */
1813                     TRACE("Notify requested abort of change\n");
1814                     COMBOEX_SetEditText (infoPtr, infoPtr->edit);
1815                     RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE |
1816                                   RDW_INVALIDATE);
1817                     return 0;
1818                 }
1819                 oldItem = SendMessageW (infoPtr->hwndCombo,CB_GETCURSEL, 0, 0);
1820                 if (oldItem != -1) {
1821                     /* if something is selected, then deselect it */
1822                     SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL,
1823                                   (WPARAM)-1, 0);
1824                 }
1825                 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1826                 SetFocus(infoPtr->hwndEdit);
1827                 break;
1828
1829             case VK_UP:
1830                 step = -1;
1831             case VK_DOWN:
1832                 /* by default, step is 1 */
1833                 oldItem = SendMessageW (infoPtr->hwndSelf, CB_GETCURSEL, 0, 0);
1834                 if (oldItem >= 0 && oldItem + step >= 0)
1835                     SendMessageW (infoPtr->hwndSelf, CB_SETCURSEL, oldItem + step, 0);
1836                 return 0;
1837             default:
1838                 return CallWindowProcW (infoPtr->prevEditWndProc,
1839                                        hwnd, uMsg, wParam, lParam);
1840             }
1841             return 0;
1842             }
1843
1844         case WM_SETFOCUS:
1845             /* remember the focus to set state of icon */
1846             lret = CallWindowProcW (infoPtr->prevEditWndProc,
1847                                    hwnd, uMsg, wParam, lParam);
1848             infoPtr->flags |= WCBE_EDITFOCUSED;
1849             return lret;
1850
1851         case WM_KILLFOCUS:
1852             /*
1853              * do NOTIFY CBEN_ENDEDIT with CBENF_KILLFOCUS
1854              */
1855             infoPtr->flags &= ~WCBE_EDITFOCUSED;
1856             if (infoPtr->flags & WCBE_ACTEDIT) {
1857                 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1858
1859                 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1860                 cbeend.fChanged = FALSE;
1861                 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1862                                                      CB_GETCURSEL, 0, 0);
1863                 cbeend.iWhy = CBENF_KILLFOCUS;
1864
1865                 COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text);
1866             }
1867             /* fall through */
1868
1869         default:
1870             return CallWindowProcW (infoPtr->prevEditWndProc,
1871                                    hwnd, uMsg, wParam, lParam);
1872     }
1873     return 0;
1874 }
1875
1876
1877 static LRESULT WINAPI
1878 COMBOEX_ComboWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1879 {
1880     HWND hwndComboex = (HWND)GetPropA(hwnd, COMBOEX_SUBCLASS_PROP);
1881     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwndComboex);
1882     NMCBEENDEDITW cbeend;
1883     NMMOUSE nmmse;
1884     COLORREF obkc;
1885     HDC hDC;
1886     HWND focusedhwnd;
1887     RECT rect;
1888     POINT pt;
1889     WCHAR edit_text[260];
1890
1891     TRACE("hwnd=%p msg=%x wparam=%x lParam=%lx, info_ptr=%p\n",
1892           hwnd, uMsg, wParam, lParam, infoPtr);
1893
1894     if (!infoPtr) return 0;
1895
1896     switch (uMsg)
1897     {
1898
1899     case WM_DRAWITEM:
1900             /*
1901              * The only way this message should come is from the
1902              * child Listbox issuing the message. Flag this so
1903              * that ComboEx knows this is listbox.
1904              */
1905             ((DRAWITEMSTRUCT *)lParam)->itemState |= ODS_COMBOEXLBOX;
1906             return CallWindowProcW (infoPtr->prevComboWndProc,
1907                                    hwnd, uMsg, wParam, lParam);
1908
1909     case WM_ERASEBKGND:
1910             /*
1911              * The following was determined by traces of the native
1912              */
1913             hDC = (HDC) wParam;
1914             obkc = SetBkColor (hDC, GetSysColor (COLOR_WINDOW));
1915             GetClientRect (hwnd, &rect);
1916             TRACE("erasing (%ld,%ld)-(%ld,%ld)\n",
1917                   rect.left, rect.top, rect.right, rect.bottom);
1918             ExtTextOutW (hDC, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
1919             SetBkColor (hDC, obkc);
1920             return CallWindowProcW (infoPtr->prevComboWndProc,
1921                                    hwnd, uMsg, wParam, lParam);
1922
1923     case WM_SETCURSOR:
1924             /*
1925              *  WM_NOTIFY to comboex parent (rebar)
1926              *   with NM_SETCURSOR with extra words of 0,0,0,0,0x02010001
1927              *  CallWindowProc (previous)
1928              */
1929             nmmse.dwItemSpec = 0;
1930             nmmse.dwItemData = 0;
1931             nmmse.pt.x = 0;
1932             nmmse.pt.y = 0;
1933             nmmse.dwHitInfo = lParam;
1934             COMBOEX_Notify (infoPtr, NM_SETCURSOR, (NMHDR *)&nmmse);
1935             return CallWindowProcW (infoPtr->prevComboWndProc,
1936                                    hwnd, uMsg, wParam, lParam);
1937
1938     case WM_LBUTTONDOWN:
1939             GetClientRect (hwnd, &rect);
1940             rect.bottom = rect.top + SendMessageW(infoPtr->hwndSelf,
1941                                                   CB_GETITEMHEIGHT, -1, 0);
1942             rect.left = rect.right - GetSystemMetrics(SM_CXVSCROLL);
1943             POINTSTOPOINT(pt, MAKEPOINTS(lParam));
1944             if (PtInRect(&rect, pt))
1945                 return CallWindowProcW (infoPtr->prevComboWndProc,
1946                                         hwnd, uMsg, wParam, lParam);
1947             infoPtr->flags |= WCBE_MOUSECAPTURED;
1948             SetCapture(hwnd);
1949             break;
1950
1951     case WM_LBUTTONUP:
1952             if (!(infoPtr->flags & WCBE_MOUSECAPTURED))
1953                 return CallWindowProcW (infoPtr->prevComboWndProc,
1954                                         hwnd, uMsg, wParam, lParam);
1955             ReleaseCapture();
1956             infoPtr->flags &= ~WCBE_MOUSECAPTURED;
1957             if (infoPtr->flags & WCBE_MOUSEDRAGGED) {
1958                 infoPtr->flags &= ~WCBE_MOUSEDRAGGED;
1959             } else {
1960                 SendMessageW(hwnd, CB_SHOWDROPDOWN, TRUE, 0);
1961             }
1962             break;
1963
1964     case WM_MOUSEMOVE:
1965             if ( (infoPtr->flags & WCBE_MOUSECAPTURED) &&
1966                 !(infoPtr->flags & WCBE_MOUSEDRAGGED)) {
1967                 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1968                 COMBOEX_NotifyDragBegin(infoPtr, edit_text);
1969                 infoPtr->flags |= WCBE_MOUSEDRAGGED;
1970             }
1971             return CallWindowProcW (infoPtr->prevComboWndProc,
1972                                     hwnd, uMsg, wParam, lParam);
1973
1974     case WM_COMMAND:
1975             switch (HIWORD(wParam)) {
1976
1977             case EN_UPDATE:
1978                 /* traces show that COMBOEX does not issue CBN_EDITUPDATE
1979                  * on the EN_UPDATE
1980                  */
1981                 return 0;
1982
1983             case EN_KILLFOCUS:
1984                 /*
1985                  * Native does:
1986                  *
1987                  *  GetFocus() retns AA
1988                  *  GetWindowTextA(Edit)
1989                  *  CB_GETCURSEL(Combo) (got -1)
1990                  *  WM_NOTIFY(CBEN_ENDEDITA) with CBENF_KILLFOCUS
1991                  *  CB_GETCURSEL(Combo) (got -1)
1992                  *  InvalidateRect(Combo, 0, 0)
1993                  *  WM_KILLFOCUS(Combo, AA)
1994                  *  return 0;
1995                  */
1996                 focusedhwnd = GetFocus();
1997                 if (infoPtr->flags & WCBE_ACTEDIT) {
1998                     GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1999                     cbeend.fChanged = (infoPtr->flags & WCBE_EDITCHG);
2000                     cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
2001                                                          CB_GETCURSEL, 0, 0);
2002                     cbeend.iWhy = CBENF_KILLFOCUS;
2003
2004                     infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
2005                     if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) return 0;
2006                 }
2007                 /* possible CB_GETCURSEL */
2008                 InvalidateRect (infoPtr->hwndCombo, 0, 0);
2009                 if (focusedhwnd)
2010                     SendMessageW (infoPtr->hwndCombo, WM_KILLFOCUS,
2011                                   (WPARAM)focusedhwnd, 0);
2012                 return 0;
2013
2014             case EN_SETFOCUS: {
2015                 /*
2016                  * For EN_SETFOCUS this issues the same calls and messages
2017                  *  as the native seems to do.
2018                  *
2019                  * for some cases however native does the following:
2020                  *   (noticed after SetFocus during LBUTTONDOWN on
2021                  *    on dropdown arrow)
2022                  *  WM_GETTEXTLENGTH (Edit);
2023                  *  WM_GETTEXT (Edit, len+1, str);
2024                  *  EM_SETSEL (Edit, 0, 0);
2025                  *  WM_GETTEXTLENGTH (Edit);
2026                  *  WM_GETTEXT (Edit, len+1, str);
2027                  *  EM_SETSEL (Edit, 0, len);
2028                  *  WM_NOTIFY (parent, CBEN_BEGINEDIT)
2029                  */
2030                 NMHDR hdr;
2031
2032                 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, 0);
2033                 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, -1);
2034                 COMBOEX_Notify (infoPtr, CBEN_BEGINEDIT, &hdr);
2035                 infoPtr->flags |= WCBE_ACTEDIT;
2036                 infoPtr->flags &= ~WCBE_EDITCHG; /* no change yet */
2037                 return 0;
2038                 }
2039
2040             case EN_CHANGE: {
2041                 /*
2042                  * For EN_CHANGE this issues the same calls and messages
2043                  *  as the native seems to do.
2044                  */
2045                 WCHAR edit_text[260];
2046                 LPCWSTR lastwrk;
2047                 cmp_func_t cmptext = get_cmp_func(infoPtr);
2048
2049                 INT selected = SendMessageW (infoPtr->hwndCombo,
2050                                              CB_GETCURSEL, 0, 0);
2051
2052                 /* lstrlenA( lastworkingURL ) */
2053
2054                 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
2055                 if (selected == -1) {
2056                     lastwrk = infoPtr->edit->pszText;
2057                 }
2058                 else {
2059                     CBE_ITEMDATA *item = COMBOEX_FindItem (infoPtr, selected);
2060                     lastwrk = COMBOEX_GetText(infoPtr, item);
2061                 }
2062
2063                 TRACE("handling EN_CHANGE, selected = %d, selected_text=%s\n",
2064                       selected, debugstr_w(lastwrk));
2065                 TRACE("handling EN_CHANGE, edittext=%s\n",
2066                       debugstr_w(edit_text));
2067
2068                 /* cmptext is between lastworkingURL and GetWindowText */
2069                 if (cmptext (lastwrk, edit_text)) {
2070                     /* strings not equal -- indicate edit has changed */
2071                     infoPtr->flags |= WCBE_EDITCHG;
2072                 }
2073                 SendMessageW ( GetParent(infoPtr->hwndSelf), WM_COMMAND,
2074                                MAKEWPARAM(GetDlgCtrlID (infoPtr->hwndSelf),
2075                                           CBN_EDITCHANGE),
2076                                (LPARAM)infoPtr->hwndSelf);
2077                 return 0;
2078                 }
2079
2080             case LBN_SELCHANGE:
2081                 /*
2082                  * Therefore from traces there is no additional code here
2083                  */
2084
2085                 /*
2086                  * Using native COMCTL32 gets the following:
2087                  *  1 == SHDOCVW.DLL  issues call/message
2088                  *  2 == COMCTL32.DLL  issues call/message
2089                  *  3 == WINE  issues call/message
2090                  *
2091                  *
2092                  * for LBN_SELCHANGE:
2093                  *    1  CB_GETCURSEL(ComboEx)
2094                  *    1  CB_GETDROPPEDSTATE(ComboEx)
2095                  *    1  CallWindowProc( *2* for WM_COMMAND(LBN_SELCHANGE)
2096                  *    2  CallWindowProc( *3* for WM_COMMAND(LBN_SELCHANGE)
2097                  **   call CBRollUp( xxx, TRUE for LBN_SELCHANGE, TRUE)
2098                  *    3  WM_COMMAND(ComboEx, CBN_SELENDOK)
2099                  *      WM_USER+49(ComboLB, 1,0)  <=============!!!!!!!!!!!
2100                  *    3  ShowWindow(ComboLB, SW_HIDE)
2101                  *    3  RedrawWindow(Combo,  RDW_UPDATENOW)
2102                  *    3  WM_COMMAND(ComboEX, CBN_CLOSEUP)
2103                  **   end of CBRollUp
2104                  *    3  WM_COMMAND(ComboEx, CBN_SELCHANGE)  (echo to parent)
2105                  *    ?  LB_GETCURSEL              <==|
2106                  *    ?  LB_GETTEXTLEN                |
2107                  *    ?  LB_GETTEXT                   | Needs to be added to
2108                  *    ?  WM_CTLCOLOREDIT(ComboEx)     | Combo processing
2109                  *    ?  LB_GETITEMDATA               |
2110                  *    ?  WM_DRAWITEM(ComboEx)      <==|
2111                  */
2112             default:
2113                 break;
2114             }/* fall through */
2115     default:
2116             return CallWindowProcW (infoPtr->prevComboWndProc,
2117                                    hwnd, uMsg, wParam, lParam);
2118     }
2119     return 0;
2120 }
2121
2122
2123 static LRESULT WINAPI
2124 COMBOEX_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2125 {
2126     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
2127
2128     TRACE("hwnd=%p msg=%x wparam=%x lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2129
2130     if (!infoPtr) {
2131         if (uMsg == WM_CREATE)
2132             return COMBOEX_Create (hwnd, (LPCREATESTRUCTA)lParam);
2133         if (uMsg == WM_NCCREATE)
2134             COMBOEX_NCCreate (hwnd);
2135         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2136     }
2137
2138     switch (uMsg)
2139     {
2140         case CBEM_DELETEITEM:
2141             return COMBOEX_DeleteItem (infoPtr, wParam);
2142
2143         case CBEM_GETCOMBOCONTROL:
2144             return (LRESULT)infoPtr->hwndCombo;
2145
2146         case CBEM_GETEDITCONTROL:
2147             return (LRESULT)infoPtr->hwndEdit;
2148
2149         case CBEM_GETEXTENDEDSTYLE:
2150             return infoPtr->dwExtStyle;
2151
2152         case CBEM_GETIMAGELIST:
2153             return (LRESULT)infoPtr->himl;
2154
2155         case CBEM_GETITEMA:
2156             return (LRESULT)COMBOEX_GetItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2157
2158         case CBEM_GETITEMW:
2159             return (LRESULT)COMBOEX_GetItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2160
2161         case CBEM_GETUNICODEFORMAT:
2162             return infoPtr->unicode;
2163
2164         case CBEM_HASEDITCHANGED:
2165             return COMBOEX_HasEditChanged (infoPtr);
2166
2167         case CBEM_INSERTITEMA:
2168             return COMBOEX_InsertItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2169
2170         case CBEM_INSERTITEMW:
2171             return COMBOEX_InsertItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2172
2173         case CBEM_SETEXSTYLE:
2174         case CBEM_SETEXTENDEDSTYLE:
2175             return COMBOEX_SetExtendedStyle (infoPtr, (DWORD)wParam, (DWORD)lParam);
2176
2177         case CBEM_SETIMAGELIST:
2178             return (LRESULT)COMBOEX_SetImageList (infoPtr, (HIMAGELIST)lParam);
2179
2180         case CBEM_SETITEMA:
2181             return COMBOEX_SetItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2182
2183         case CBEM_SETITEMW:
2184             return COMBOEX_SetItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2185
2186         case CBEM_SETUNICODEFORMAT:
2187             return COMBOEX_SetUnicodeFormat (infoPtr, wParam);
2188
2189         /*case CBEM_SETWINDOWTHEME:
2190             FIXME("CBEM_SETWINDOWTHEME: stub\n");*/
2191
2192         case WM_SETTEXT:
2193         case WM_GETTEXT:
2194             return SendMessageW(infoPtr->hwndEdit, uMsg, wParam, lParam);
2195
2196 /*   Combo messages we are not sure if we need to process or just forward */
2197         case CB_GETDROPPEDCONTROLRECT:
2198         case CB_GETITEMHEIGHT:
2199         case CB_GETLBTEXT:
2200         case CB_GETLBTEXTLEN:
2201         case CB_GETEXTENDEDUI:
2202         case CB_LIMITTEXT:
2203         case CB_RESETCONTENT:
2204         case CB_SELECTSTRING:
2205
2206 /*   Combo messages OK to just forward to the regular COMBO */
2207         case CB_GETCOUNT:
2208         case CB_GETCURSEL:
2209         case CB_GETDROPPEDSTATE:
2210         case CB_SETDROPPEDWIDTH:
2211         case CB_SETEXTENDEDUI:
2212         case CB_SHOWDROPDOWN:
2213             return SendMessageW (infoPtr->hwndCombo, uMsg, wParam, lParam);
2214
2215 /*   Combo messages we need to process specially */
2216         case CB_FINDSTRINGEXACT:
2217             return COMBOEX_FindStringExact (infoPtr, (INT)wParam, (LPCWSTR)lParam);
2218
2219         case CB_GETITEMDATA:
2220             return COMBOEX_GetItemData (infoPtr, (INT)wParam);
2221
2222         case CB_SETCURSEL:
2223             return COMBOEX_SetCursel (infoPtr, (INT)wParam);
2224
2225         case CB_SETITEMDATA:
2226             return COMBOEX_SetItemData (infoPtr, (INT)wParam, (DWORD)lParam);
2227
2228         case CB_SETITEMHEIGHT:
2229             return COMBOEX_SetItemHeight (infoPtr, (INT)wParam, (UINT)lParam);
2230
2231
2232
2233 /*   Window messages passed to parent */
2234         case WM_COMMAND:
2235             return COMBOEX_Command (infoPtr, wParam, lParam);
2236
2237         case WM_NOTIFY:
2238             if (infoPtr->NtfUnicode)
2239                 return SendMessageW (GetParent (hwnd), uMsg, wParam, lParam);
2240             else
2241                 return SendMessageA (GetParent (hwnd), uMsg, wParam, lParam);
2242
2243
2244 /*   Window messages we need to process */
2245         case WM_DELETEITEM:
2246             return COMBOEX_WM_DeleteItem (infoPtr, (DELETEITEMSTRUCT *)lParam);
2247
2248         case WM_DRAWITEM:
2249             return COMBOEX_DrawItem (infoPtr, (DRAWITEMSTRUCT *)lParam);
2250
2251         case WM_DESTROY:
2252             return COMBOEX_Destroy (infoPtr);
2253
2254         case WM_MEASUREITEM:
2255             return COMBOEX_MeasureItem (infoPtr, (MEASUREITEMSTRUCT *)lParam);
2256
2257         case WM_NOTIFYFORMAT:
2258             return COMBOEX_NotifyFormat (infoPtr, lParam);
2259
2260         case WM_SIZE:
2261             return COMBOEX_Size (infoPtr, LOWORD(lParam), HIWORD(lParam));
2262
2263         case WM_WINDOWPOSCHANGING:
2264             return COMBOEX_WindowPosChanging (infoPtr, (WINDOWPOS *)lParam);
2265
2266         default:
2267             if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2268                 ERR("unknown msg %04x wp=%08x lp=%08lx\n",uMsg,wParam,lParam);
2269             return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2270     }
2271     return 0;
2272 }
2273
2274
2275 void COMBOEX_Register (void)
2276 {
2277     WNDCLASSW wndClass;
2278
2279     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2280     wndClass.style         = CS_GLOBALCLASS;
2281     wndClass.lpfnWndProc   = (WNDPROC)COMBOEX_WindowProc;
2282     wndClass.cbClsExtra    = 0;
2283     wndClass.cbWndExtra    = sizeof(COMBOEX_INFO *);
2284     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2285     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2286     wndClass.lpszClassName = WC_COMBOBOXEXW;
2287
2288     RegisterClassW (&wndClass);
2289 }
2290
2291
2292 void COMBOEX_Unregister (void)
2293 {
2294     UnregisterClassW (WC_COMBOBOXEXW, NULL);
2295 }