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