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