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