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