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