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