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