CBEM_SETITEMW done by example from SETITEMA, converted SETITEMA to use
[wine] / dlls / comctl32 / comboex.c
1 /*
2  * ComboBoxEx control
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  *   1. Implemented message CB_SETITEMHEIGHT 
20  *   2. Implemented message WM_WINDOWPOSCHANGING
21  *   3. Implemented message WM_MEASUREITEM
22  *   4. Add code to WM_CREATE processing to set font of COMBOBOX and
23  *      issue the CB_SETITEMHEIGHT to start the correct sizing process.
24  * The above 4 changes allow the window rect for the comboboxex
25  * to be set properly, which in turn allows the height of the
26  * rebar control it *may* be imbeded in to be correct.
27  *   5. Rewrite CBEM_INSERTITEMA to save the information.
28  *   6. Implemented message WM_DRAWITEM. The code will handle images
29  *      but not "overlays" yet.
30  *   7. Fixed code in CBEM_SETIMAGELIST to resize control.
31  *   8. Add debugging code.
32  *
33  * Test vehicals were the ControlSpy modules (rebar.exe and comboboxex.exe)
34  *
35  */
36
37 #include "winbase.h"
38 #include "wine/winestring.h"
39 #include "commctrl.h"
40 #include "debugtools.h"
41 #include "wine/unicode.h"
42
43 DEFAULT_DEBUG_CHANNEL(comboex);
44 DECLARE_DEBUG_CHANNEL(message);
45
46 /* Item structure */
47 typedef struct
48 {
49     VOID *next;
50     UINT mask;
51     LPWSTR pszText;
52     int cchTextMax;
53     int iImage;
54     int iSelectedImage;
55     int iOverlay;
56     int iIndent;
57     LPARAM lParam;
58 } CBE_ITEMDATA;
59
60 /* ComboBoxEx structure */
61 typedef struct
62 {
63     HIMAGELIST   himl;
64     HWND         hwndCombo;
65     DWORD        dwExtStyle;
66     HFONT        font;
67     INT          nb_items;         /* Number of items */
68     CBE_ITEMDATA *items;           /* Array of items */
69 } COMBOEX_INFO;
70
71 #define ID_CB_EDIT    1001
72
73 /* Height in pixels of control over the amount of the selected font */
74 #define CBE_EXTRA     3
75
76 /* Indent amount per MS documentation */
77 #define CBE_INDENT    10
78
79 /* Offset in pixels from left side for start of image or text */
80 #define CBE_STARTOFFSET   6
81
82 /* Offset between image and text */
83 #define CBE_SEP   4
84
85 #define COMBOEX_GetInfoPtr(wndPtr) ((COMBOEX_INFO *)GetWindowLongA (hwnd, 0))
86
87
88 static void
89 COMBOEX_DumpItem (CBE_ITEMDATA *item)
90 {
91     if (TRACE_ON(comboex)){
92       TRACE("item %p - mask=%08x, pszText=%p, cchTM=%d, iImage=%d\n",
93             item, item->mask, item->pszText, item->cchTextMax,
94             item->iImage);
95       TRACE("item %p - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
96             item, item->iSelectedImage, item->iOverlay, item->iIndent, item->lParam);
97     }
98 }
99
100
101 inline static LRESULT
102 COMBOEX_Forward (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
103 {
104     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
105
106     FIXME("(0x%x 0x%x 0x%lx): stub\n", uMsg, wParam, lParam);
107
108     if (infoPtr->hwndCombo)    
109         return SendMessageA (infoPtr->hwndCombo, uMsg, wParam, lParam);
110
111     return 0;
112 }
113
114
115 static void
116 COMBOEX_ReSize (HWND hwnd, COMBOEX_INFO *infoPtr)
117 {
118     HFONT nfont, ofont;
119     HDC mydc;
120     SIZE mysize;
121     UINT cy;
122     IMAGEINFO iinfo;
123
124     mydc = GetDC (0); /* why the entire screen???? */
125     nfont = SendMessageA (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
126     ofont = (HFONT) SelectObject (mydc, nfont);
127     GetTextExtentPointA (mydc, "A", 1, &mysize);
128     SelectObject (mydc, ofont);
129     ReleaseDC (0, mydc);
130     cy = mysize.cy + CBE_EXTRA;
131     if (infoPtr->himl) {
132         ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
133         cy = max (iinfo.rcImage.bottom - iinfo.rcImage.top, cy);
134     }
135     TRACE("selected font hwnd=%08x, height=%d\n", nfont, cy);
136     SendMessageA (hwnd, CB_SETITEMHEIGHT, (WPARAM) -1, (LPARAM) cy);
137     if (infoPtr->hwndCombo)
138         SendMessageA (infoPtr->hwndCombo, CB_SETITEMHEIGHT,
139                       (WPARAM) 0, (LPARAM) cy);
140 }
141
142
143 /* ***  CBEM_xxx message support  *** */
144
145
146 /* << COMBOEX_DeleteItem >> */
147
148
149 inline static LRESULT
150 COMBOEX_GetComboControl (HWND hwnd, WPARAM wParam, LPARAM lParam)
151 {
152     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
153
154     TRACE("\n");
155
156     return (LRESULT)infoPtr->hwndCombo;
157 }
158
159
160 inline static LRESULT
161 COMBOEX_GetEditControl (HWND hwnd, WPARAM wParam, LPARAM lParam)
162 {
163     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
164
165     if ((GetWindowLongA (hwnd, GWL_STYLE) & CBS_DROPDOWNLIST) != CBS_DROPDOWN)
166         return 0;
167
168     TRACE("-- 0x%x\n", GetDlgItem (infoPtr->hwndCombo, ID_CB_EDIT));
169
170     return (LRESULT)GetDlgItem (infoPtr->hwndCombo, ID_CB_EDIT);
171 }
172
173
174 inline static LRESULT
175 COMBOEX_GetExtendedStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
176 {
177     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
178
179     return (LRESULT)infoPtr->dwExtStyle;
180 }
181
182
183 inline static LRESULT
184 COMBOEX_GetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
185 {
186     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
187
188     TRACE("(0x%08x 0x%08lx)\n", wParam, lParam);
189
190     return (LRESULT)infoPtr->himl;
191 }
192
193
194 /* << COMBOEX_GetItemA >> */
195
196 /* << COMBOEX_GetItemW >> */
197
198 /* << COMBOEX_GetUniCodeFormat >> */
199
200 /* << COMBOEX_HasEditChanged >> */
201
202
203 static LRESULT
204 COMBOEX_InsertItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
205 {
206     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
207     COMBOBOXEXITEMA *cit = (COMBOBOXEXITEMA *) lParam;
208     INT index;
209     CBE_ITEMDATA *item;
210
211
212     /* get real index of item to insert */
213     index = cit->iItem;
214     if (index == -1) index = infoPtr->nb_items;
215     if (index > infoPtr->nb_items) index = infoPtr->nb_items;
216
217     /* get space and chain it in */
218     item = (CBE_ITEMDATA *)COMCTL32_Alloc (sizeof (CBE_ITEMDATA));
219     item->next = NULL;
220     item->pszText = NULL;
221
222     /* locate position to insert new item in */
223     if (index == infoPtr->nb_items) {
224         /* fast path for iItem = -1 */
225         item->next = infoPtr->items;
226         infoPtr->items = item;
227     }
228     else {
229         int i = infoPtr->nb_items-1;
230         CBE_ITEMDATA *moving = infoPtr->items;
231
232         while (i > index && moving) {
233             moving = (CBE_ITEMDATA *)moving->next;
234         }
235         if (!moving) {
236             FIXME("COMBOBOXEX item structures broken. Please report!\n");
237             COMCTL32_Free(item);
238             return -1;
239         }
240         item->next = moving->next;
241         moving->next = item;
242     }
243
244     /* fill in our hidden item structure */
245     item->mask           = cit->mask;
246     if (item->mask & CBEIF_TEXT) {
247         LPSTR str;
248         INT len;
249
250         str = cit->pszText;
251         if (!str) str="";
252         len = MultiByteToWideChar (CP_ACP, 0, str, -1, NULL, 0);
253         if (len > 0) {
254             item->pszText = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
255             MultiByteToWideChar (CP_ACP, 0, str, -1, item->pszText, len);
256         }
257         item->cchTextMax   = cit->cchTextMax;
258     }
259     if (item->mask & CBEIF_IMAGE)
260       item->iImage         = cit->iImage;
261     if (item->mask & CBEIF_SELECTEDIMAGE)
262       item->iSelectedImage = cit->iSelectedImage;
263     if (item->mask & CBEIF_OVERLAY)
264       item->iOverlay       = cit->iOverlay;
265     if (item->mask & CBEIF_INDENT)
266       item->iIndent        = cit->iIndent;
267     if (item->mask & CBEIF_LPARAM)
268       item->lParam         = cit->lParam;
269     infoPtr->nb_items++;
270
271     COMBOEX_DumpItem (item);
272
273     SendMessageA (infoPtr->hwndCombo, CB_INSERTSTRING, 
274                   (WPARAM)cit->iItem, (LPARAM)item);
275
276     return index;
277
278 }
279
280
281 static LRESULT
282 COMBOEX_InsertItemW (HWND hwnd, WPARAM wParam, LPARAM lParam)
283 {
284     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
285     COMBOBOXEXITEMW *cit = (COMBOBOXEXITEMW *) lParam;
286     INT index;
287     CBE_ITEMDATA *item;
288
289     /* get real index of item to insert */
290     index = cit->iItem;
291     if (index == -1) index = infoPtr->nb_items;
292     if (index > infoPtr->nb_items) index = infoPtr->nb_items;
293
294     /* get space and chain it in */
295     item = (CBE_ITEMDATA *)COMCTL32_Alloc (sizeof (CBE_ITEMDATA));
296     item->next = NULL;
297     item->pszText = NULL;
298
299     /* locate position to insert new item in */
300     if (index == infoPtr->nb_items) {
301         /* fast path for iItem = -1 */
302         item->next = infoPtr->items;
303         infoPtr->items = item;
304     }
305     else {
306         INT i = infoPtr->nb_items-1;
307         CBE_ITEMDATA *moving = infoPtr->items;
308
309         while ((i > index) && moving) {
310             moving = (CBE_ITEMDATA *)moving->next;
311             i--;
312         }
313         if (!moving) {
314             FIXME("COMBOBOXEX item structures broken. Please report!\n");
315             COMCTL32_Free(item);
316             return -1;
317         }
318         item->next = moving->next;
319         moving->next = item;
320     }
321
322     /* fill in our hidden item structure */
323     item->mask           = cit->mask;
324     if (item->mask & CBEIF_TEXT) {
325         LPWSTR str;
326         INT len;
327
328         str = cit->pszText;
329         if (!str) str = (LPWSTR) L"";
330         len = strlenW (str);
331         if (len > 0) {
332             item->pszText = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
333             strcpyW (item->pszText, str);
334         }
335         item->cchTextMax   = cit->cchTextMax;
336     }
337     if (item->mask & CBEIF_IMAGE)
338       item->iImage         = cit->iImage;
339     if (item->mask & CBEIF_SELECTEDIMAGE)
340       item->iSelectedImage = cit->iSelectedImage;
341     if (item->mask & CBEIF_OVERLAY)
342       item->iOverlay       = cit->iOverlay;
343     if (item->mask & CBEIF_INDENT)
344       item->iIndent        = cit->iIndent;
345     if (item->mask & CBEIF_LPARAM)
346       item->lParam         = cit->lParam;
347     infoPtr->nb_items++;
348
349     COMBOEX_DumpItem (item);
350
351     SendMessageA (infoPtr->hwndCombo, CB_INSERTSTRING, 
352                   (WPARAM)cit->iItem, (LPARAM)item);
353
354     return index;
355
356 }
357
358
359 static LRESULT
360 COMBOEX_SetExtendedStyle (HWND hwnd, WPARAM wParam, LPARAM lParam)
361 {
362     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
363     DWORD dwTemp;
364
365     TRACE("(0x%08x 0x%08lx)\n", wParam, lParam);
366
367     dwTemp = infoPtr->dwExtStyle;
368
369     if ((DWORD)wParam) {
370         infoPtr->dwExtStyle = (infoPtr->dwExtStyle & ~(DWORD)wParam) | (DWORD)lParam;
371     }
372     else
373         infoPtr->dwExtStyle = (DWORD)lParam;
374
375     /* FIXME: repaint?? */
376
377     return (LRESULT)dwTemp;
378 }
379
380
381 inline static LRESULT
382 COMBOEX_SetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
383 {
384     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
385     HIMAGELIST himlTemp;
386
387     TRACE("(0x%08x 0x%08lx)\n", wParam, lParam);
388
389     himlTemp = infoPtr->himl;
390     infoPtr->himl = (HIMAGELIST)lParam;
391
392     COMBOEX_ReSize (hwnd, infoPtr);
393     InvalidateRect (hwnd, NULL, TRUE);
394
395     return (LRESULT)himlTemp;
396 }
397
398 static LRESULT
399 COMBOEX_SetItemW (HWND hwnd, WPARAM wParam, LPARAM lParam)
400 {
401     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
402     COMBOBOXEXITEMW *cit = (COMBOBOXEXITEMW *) lParam;
403     INT index;
404     INT i;
405     CBE_ITEMDATA *item;
406
407     /* get real index of item to insert */
408     index = cit->iItem;
409     if (index == -1) {
410         FIXME("NYI setting data for item in edit control\n");
411         return 0;
412     }
413
414     /* if item number requested does not exist then return failure */
415     if ((index > infoPtr->nb_items) || (index < 0)) return 0;
416
417     /* find the item in the list */
418     item = infoPtr->items;
419     i = infoPtr->nb_items - 1;
420     while (item && (i > index)) {
421         item = (CBE_ITEMDATA *)item->next;
422         i--;
423     }
424     if (!item || (i != index)) {
425         FIXME("COMBOBOXEX item structures broken. Please report!\n");
426         return 0;
427     }
428
429     /* add/change stuff to the internal item structure */ 
430     item->mask |= cit->mask;
431     if (cit->mask & CBEIF_TEXT) {
432         LPWSTR str;
433         INT len;
434         WCHAR emptystr[1] = {0};
435
436         str = cit->pszText;
437         if (!str) str=emptystr;
438         len = strlenW(str);
439         if (len > 0) {
440             item->pszText = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
441             strcpyW(item->pszText,str);
442         }
443         item->cchTextMax   = cit->cchTextMax;
444     }
445     if (cit->mask & CBEIF_IMAGE)
446       item->iImage         = cit->iImage;
447     if (cit->mask & CBEIF_SELECTEDIMAGE)
448       item->iSelectedImage = cit->iSelectedImage;
449     if (cit->mask & CBEIF_OVERLAY)
450       item->iOverlay       = cit->iOverlay;
451     if (cit->mask & CBEIF_INDENT)
452       item->iIndent        = cit->iIndent;
453     if (cit->mask & CBEIF_LPARAM)
454       cit->lParam         = cit->lParam;
455
456     COMBOEX_DumpItem (item);
457
458     return TRUE;
459 }
460
461 static LRESULT
462 COMBOEX_SetItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
463 {
464     COMBOBOXEXITEMA     *cit = (COMBOBOXEXITEMA *) lParam;
465     COMBOBOXEXITEMW     citW;
466     LRESULT             ret;
467
468     memcpy(&citW,cit,sizeof(COMBOBOXEXITEMA));
469     if (cit->mask & CBEIF_TEXT) {
470         LPSTR str;
471         INT len;
472
473         str = cit->pszText;
474         if (!str) str="";
475         len = MultiByteToWideChar (CP_ACP, 0, str, -1, NULL, 0);
476         if (len > 0) {
477             citW.pszText = (LPWSTR)COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
478             MultiByteToWideChar (CP_ACP, 0, str, -1, citW.pszText, len);
479         }
480     }
481     ret = COMBOEX_SetItemW(hwnd,wParam,(LPARAM)&citW);;
482
483     if (cit->mask & CBEIF_TEXT)
484         COMCTL32_Free(citW.pszText);
485     return ret;
486 }
487
488
489 /* << COMBOEX_SetUniCodeFormat >> */
490
491
492 /* ***  CB_xxx message support  *** */
493
494
495 static LRESULT
496 COMBOEX_SetItemHeight (HWND hwnd, WPARAM wParam, LPARAM lParam)
497 {
498     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
499     RECT cb_wrect, cbx_wrect, cbx_crect;
500     LRESULT ret = 0;
501     UINT height;
502
503     /* First, lets forward the message to the normal combo control
504        just like Windows.     */
505     if (infoPtr->hwndCombo)    
506        SendMessageA (infoPtr->hwndCombo, CB_SETITEMHEIGHT, wParam, lParam);
507
508     /* *** new *** */
509     GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
510     GetWindowRect (hwnd, &cbx_wrect);
511     GetClientRect (hwnd, &cbx_crect);
512     /* the height of comboex as height of the combo + comboex border */ 
513     height = cb_wrect.bottom-cb_wrect.top
514              + cbx_wrect.bottom-cbx_wrect.top
515              - (cbx_crect.bottom-cbx_crect.top);
516     TRACE("EX window=(%d,%d)-(%d,%d), client=(%d,%d)-(%d,%d)\n",
517           cbx_wrect.left, cbx_wrect.top, cbx_wrect.right, cbx_wrect.bottom,
518           cbx_crect.left, cbx_crect.top, cbx_crect.right, cbx_crect.bottom);
519     TRACE("CB window=(%d,%d)-(%d,%d), EX setting=(0,0)-(%d,%d)\n",
520           cb_wrect.left, cb_wrect.top, cb_wrect.right, cb_wrect.bottom,
521           cbx_wrect.right-cbx_wrect.left, height);
522     SetWindowPos (hwnd, HWND_TOP, 0, 0,
523                   cbx_wrect.right-cbx_wrect.left,
524                   height,
525                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
526     /* *** end new *** */
527
528     return ret;
529 }
530
531
532 /* ***  WM_xxx message support  *** */
533
534
535 static LRESULT
536 COMBOEX_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
537 {
538     LPCREATESTRUCTA cs = (LPCREATESTRUCTA) lParam;
539     COMBOEX_INFO *infoPtr;
540     DWORD dwComboStyle;
541     LOGFONTA mylogfont;
542
543     /* allocate memory for info structure */
544     infoPtr = (COMBOEX_INFO *)COMCTL32_Alloc (sizeof(COMBOEX_INFO));
545     if (infoPtr == NULL) {
546         ERR("could not allocate info memory!\n");
547         return 0;
548     }
549     infoPtr->items    = NULL;
550     infoPtr->nb_items = 0;
551
552     SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
553
554
555     /* initialize info structure */
556
557
558     /* create combo box */
559     dwComboStyle = GetWindowLongA (hwnd, GWL_STYLE) &
560                         (CBS_SIMPLE|CBS_DROPDOWN|CBS_DROPDOWNLIST|WS_CHILD);
561
562     infoPtr->hwndCombo = CreateWindowA ("ComboBox", "",
563                          /* following line added to match native */
564                          WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VSCROLL | CBS_NOINTEGRALHEIGHT | 
565                          /* was base and is necessary */
566                          WS_CHILD | WS_VISIBLE | CBS_OWNERDRAWFIXED | dwComboStyle,
567                         cs->y, cs->x, cs->cx, cs->cy, hwnd, (HMENU)0,
568                         GetWindowLongA (hwnd, GWL_HINSTANCE), NULL);
569
570     /* *** new *** */
571     SystemParametersInfoA (SPI_GETICONTITLELOGFONT, sizeof(mylogfont), &mylogfont, 0);
572     infoPtr->font = CreateFontIndirectA (&mylogfont);
573     SendMessageA (infoPtr->hwndCombo, WM_SETFONT, (WPARAM)infoPtr->font, 0);
574     COMBOEX_ReSize (hwnd, infoPtr);
575     /* *** end new *** */
576
577     return 0;
578 }
579
580
581 inline static LRESULT
582 COMBOEX_DrawItem (HWND hwnd, WPARAM wParam, LPARAM lParam)
583 {
584     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
585     DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lParam;
586     CBE_ITEMDATA *item;
587     SIZE txtsize;
588     COLORREF nbkc, ntxc;
589     RECT rect;
590     int drawimage;
591     UINT x, xbase, y;
592     UINT xioff = 0;               /* size and spacer of image if any */
593     IMAGEINFO iinfo;
594     INT len;
595
596     if (!IsWindowEnabled(infoPtr->hwndCombo)) return 0;
597
598     /* MSDN says:                                                       */
599     /*     "itemID - Specifies the menu item identifier for a menu      */
600     /*      item or the index of the item in a list box or combo box.   */
601     /*      For an empty list box or combo box, this member can be -1.  */
602     /*      This allows the application to draw only the focus          */
603     /*      rectangle at the coordinates specified by the rcItem        */
604     /*      member even though there are no items in the control.       */
605     /*      This indicates to the user whether the list box or combo    */
606     /*      box has the focus. How the bits are set in the itemAction   */
607     /*      member determines whether the rectangle is to be drawn as   */
608     /*      though the list box or combo box has the focus.             */
609     if (dis->itemID == 0xffffffff) {
610       if ( ( (dis->itemAction & ODA_FOCUS) && (dis->itemState & ODS_SELECTED)) ||
611            ( (dis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)) && (dis->itemState & ODS_FOCUS) ) ) { 
612         TRACE("drawing item -1 special focus, rect=(%d,%d)-(%d,%d)\n",
613               dis->rcItem.left, dis->rcItem.top,
614               dis->rcItem.right, dis->rcItem.bottom);
615         DrawFocusRect(dis->hDC, &dis->rcItem);
616         return 0;
617       }
618       else {
619         TRACE("NOT drawing item  -1 special focus, rect=(%d,%d)-(%d,%d), action=%08x, state=%08x\n",
620               dis->rcItem.left, dis->rcItem.top,
621               dis->rcItem.right, dis->rcItem.bottom,
622               dis->itemAction, dis->itemState);
623         return 0;
624       }
625     }
626
627     item = (CBE_ITEMDATA *)SendMessageA (infoPtr->hwndCombo, CB_GETITEMDATA, 
628                                          (WPARAM)dis->itemID, 0);
629     if (item == (CBE_ITEMDATA *)CB_ERR)
630     {
631         FIXME("invalid item for id %d \n",dis->itemID);
632         return 0;
633     }
634     if (!TRACE_ON(message)) {
635         TRACE("DRAWITEMSTRUCT: CtlType=0x%08x CtlID=0x%08x\n", 
636               dis->CtlType, dis->CtlID);
637         TRACE("itemID=0x%08x itemAction=0x%08x itemState=0x%08x\n", 
638               dis->itemID, dis->itemAction, dis->itemState);
639         TRACE("hWnd=0x%04x hDC=0x%04x (%d,%d)-(%d,%d) itemData=0x%08lx\n",
640               dis->hwndItem, dis->hDC, dis->rcItem.left, 
641               dis->rcItem.top, dis->rcItem.right, dis->rcItem.bottom, 
642               dis->itemData);
643     }
644     COMBOEX_DumpItem (item);
645
646     xbase = CBE_STARTOFFSET;
647     if (item->mask & CBEIF_INDENT)
648         xbase += (item->iIndent * CBE_INDENT);
649     if (item->mask & CBEIF_IMAGE) {
650         ImageList_GetImageInfo(infoPtr->himl, item->iImage, &iinfo);
651         xioff = (iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP);
652     }
653
654     switch (dis->itemAction) {
655     case ODA_FOCUS:
656         if (dis->itemState & ODS_SELECTED /*1*/) {
657             if ((item->mask & CBEIF_TEXT) && item->pszText) {
658                 len = strlenW (item->pszText);
659                 GetTextExtentPointW (dis->hDC, item->pszText, len, &txtsize);
660                 rect.left = xbase + xioff - 1;
661                 rect.top = dis->rcItem.top - 1 +
662                   (dis->rcItem.bottom - dis->rcItem.top - txtsize.cy) / 2;
663                 rect.right = rect.left + txtsize.cx + 2;
664                 rect.bottom = rect.top + txtsize.cy + 2;
665                 TRACE("drawing item %d focus, rect=(%d,%d)-(%d,%d)\n",
666                       dis->itemID, rect.left, rect.top,
667                       rect.right, rect.bottom);
668                 DrawFocusRect(dis->hDC, &rect);
669             }
670         }
671         break;
672     case ODA_SELECT:
673     case ODA_DRAWENTIRE:
674         drawimage = -1;
675         if (item->mask & CBEIF_IMAGE) drawimage = item->iImage;
676         if ((dis->itemState & ODS_SELECTED) && 
677             (item->mask & CBEIF_SELECTEDIMAGE))
678                 drawimage = item->iSelectedImage;
679         if (drawimage != -1) {
680             ImageList_Draw (infoPtr->himl, drawimage, dis->hDC, 
681                             xbase, dis->rcItem.top, 
682                             (dis->itemState & ODS_SELECTED) ? 
683                             ILD_SELECTED : ILD_NORMAL);
684         }
685         if ((item->mask & CBEIF_TEXT) && item->pszText) {
686             len = strlenW (item->pszText);
687             GetTextExtentPointW (dis->hDC, item->pszText, len, &txtsize);
688             nbkc = GetSysColor ((dis->itemState & ODS_SELECTED) ?
689                                 COLOR_HIGHLIGHT : COLOR_WINDOW);
690             SetBkColor (dis->hDC, nbkc);
691             ntxc = GetSysColor ((dis->itemState & ODS_SELECTED) ?
692                                 COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT);
693             SetTextColor (dis->hDC, ntxc);
694             x = xbase + xioff;
695             y = dis->rcItem.top +
696                 (dis->rcItem.bottom - dis->rcItem.top - txtsize.cy) / 2;
697             rect.left = x;
698             rect.right = x + txtsize.cx;
699             rect.top = y;
700             rect.bottom = y + txtsize.cy;
701             TRACE("drawing item %d text, rect=(%d,%d)-(%d,%d)\n",
702                   dis->itemID, rect.left, rect.top, rect.right, rect.bottom);
703             ExtTextOutW (dis->hDC, x, y, ETO_OPAQUE | ETO_CLIPPED,
704                          &rect, item->pszText, len, 0);
705             if (dis->itemState & ODS_FOCUS) {
706                 rect.top -= 1;
707                 rect.bottom += 1;
708                 rect.left -= 1;
709                 rect.right += 2;
710                 TRACE("drawing item %d focus, rect=(%d,%d)-(%d,%d)\n",
711                       dis->itemID, rect.left, rect.top, rect.right, rect.bottom);
712                 DrawFocusRect (dis->hDC, &rect);
713             }
714         }
715         break;
716     default:
717         FIXME("unknown action hwnd=%08x, wparam=%08x, lparam=%08lx, action=%d\n", 
718               hwnd, wParam, lParam, dis->itemAction);
719     }
720
721     return 0;
722 }
723
724
725 static LRESULT
726 COMBOEX_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
727 {
728     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
729
730     if (infoPtr->hwndCombo)
731         DestroyWindow (infoPtr->hwndCombo);
732
733     if (infoPtr->items) {
734         CBE_ITEMDATA *this, *next;
735
736         this = infoPtr->items;
737         while (this) {
738             next = (CBE_ITEMDATA *)this->next;
739             if ((this->mask & CBEIF_TEXT) && this->pszText)
740                 COMCTL32_Free (this->pszText);
741             COMCTL32_Free (this);
742             this = next;
743         }
744     }
745
746     DeleteObject (infoPtr->font);
747
748     /* free comboex info data */
749     COMCTL32_Free (infoPtr);
750     SetWindowLongA (hwnd, 0, 0);
751     return 0;
752 }
753
754
755 static LRESULT
756 COMBOEX_MeasureItem (HWND hwnd, WPARAM wParam, LPARAM lParam)
757 {
758     /*COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);*/
759     MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *) lParam;
760     HDC hdc;
761     SIZE mysize;
762
763     hdc = GetDC (0);
764     GetTextExtentPointA (hdc, "W", 1, &mysize);
765     ReleaseDC (0, hdc);
766     mis->itemHeight = mysize.cy + CBE_EXTRA;
767
768     TRACE("adjusted height hwnd=%08x, height=%d\n",
769           hwnd, mis->itemHeight);
770
771     return 0;
772 }
773
774
775 static LRESULT
776 COMBOEX_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
777 {
778     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
779     RECT rect;
780
781     GetClientRect (hwnd, &rect);
782
783     MoveWindow (infoPtr->hwndCombo, 0, 0, rect.right -rect.left,
784                   rect.bottom - rect.top, TRUE);
785
786     return 0;
787 }
788
789
790 static LRESULT
791 COMBOEX_WindowPosChanging (HWND hwnd, WPARAM wParam, LPARAM lParam)
792 {
793     COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
794     LRESULT ret;
795     RECT cbx_wrect, cbx_crect, cb_wrect;
796     UINT width;
797     WINDOWPOS *wp = (WINDOWPOS *)lParam;
798
799     ret = DefWindowProcA (hwnd, WM_WINDOWPOSCHANGING, wParam, lParam);
800     GetWindowRect (hwnd, &cbx_wrect);
801     GetClientRect (hwnd, &cbx_crect);
802     GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
803
804     /* width is winpos value + border width of comboex */
805     width = wp->cx
806             + cbx_wrect.right-cbx_wrect.left 
807             - (cbx_crect.right - cbx_crect.left); 
808
809     TRACE("EX window=(%d,%d)-(%d,%d), client=(%d,%d)-(%d,%d)\n",
810           cbx_wrect.left, cbx_wrect.top, cbx_wrect.right, cbx_wrect.bottom,
811           cbx_crect.left, cbx_crect.top, cbx_crect.right, cbx_crect.bottom);
812     TRACE("CB window=(%d,%d)-(%d,%d), EX setting=(0,0)-(%d,%d)\n",
813           cb_wrect.left, cb_wrect.top, cb_wrect.right, cb_wrect.bottom,
814           width, cb_wrect.bottom-cb_wrect.top);
815
816     SetWindowPos (infoPtr->hwndCombo, HWND_TOP, 0, 0,
817                   width,
818                   cb_wrect.bottom-cb_wrect.top,
819                   SWP_NOACTIVATE);
820
821     return 0;
822 }
823
824
825 static LRESULT WINAPI
826 COMBOEX_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
827 {
828     TRACE("hwnd=%x msg=%x wparam=%x lParam=%lx\n", hwnd, uMsg, wParam, lParam);
829     if (!COMBOEX_GetInfoPtr (hwnd) && (uMsg != WM_CREATE))
830         return DefWindowProcA (hwnd, uMsg, wParam, lParam);
831
832     switch (uMsg)
833     {
834 /*      case CBEM_DELETEITEM: */
835
836         case CBEM_GETCOMBOCONTROL:
837             return COMBOEX_GetComboControl (hwnd, wParam, lParam);
838
839         case CBEM_GETEDITCONTROL:
840             return COMBOEX_GetEditControl (hwnd, wParam, lParam);
841
842         case CBEM_GETEXTENDEDSTYLE:
843             return COMBOEX_GetExtendedStyle (hwnd, wParam, lParam);
844
845         case CBEM_GETIMAGELIST:
846             return COMBOEX_GetImageList (hwnd, wParam, lParam);
847
848 /*      case CBEM_GETITEMA:
849         case CBEM_GETITEMW:
850         case CBEM_GETUNICODEFORMAT:
851         case CBEM_HASEDITCHANGED:
852 */
853
854         case CBEM_INSERTITEMA:
855             return COMBOEX_InsertItemA (hwnd, wParam, lParam);
856
857         case CBEM_INSERTITEMW:
858             return COMBOEX_InsertItemW (hwnd, wParam, lParam);
859
860         case CBEM_SETEXSTYLE:   /* FIXME: obsoleted, should be the same as: */
861         case CBEM_SETEXTENDEDSTYLE:
862             return COMBOEX_SetExtendedStyle (hwnd, wParam, lParam);
863
864         case CBEM_SETIMAGELIST:
865             return COMBOEX_SetImageList (hwnd, wParam, lParam);
866
867         case CBEM_SETITEMA:
868             return COMBOEX_SetItemA (hwnd, wParam, lParam);
869
870         case CBEM_SETITEMW:
871             return COMBOEX_SetItemW (hwnd, wParam, lParam);
872
873 /*      case CBEM_SETUNICODEFORMAT:
874 */
875
876         case CB_DELETESTRING:
877         case CB_FINDSTRINGEXACT:
878         case CB_GETCOUNT:
879         case CB_GETCURSEL:
880         case CB_GETDROPPEDCONTROLRECT:
881         case CB_GETDROPPEDSTATE:
882         case CB_GETITEMDATA:
883         case CB_GETITEMHEIGHT:
884         case CB_GETLBTEXT:
885         case CB_GETLBTEXTLEN:
886         case CB_GETEXTENDEDUI:
887         case CB_LIMITTEXT:
888         case CB_RESETCONTENT:
889         case CB_SELECTSTRING:
890         case CB_SETCURSEL:
891         case CB_SETDROPPEDWIDTH:
892         case CB_SETEXTENDEDUI:
893         case CB_SETITEMDATA:
894         case CB_SHOWDROPDOWN:
895         case WM_SETTEXT:
896         case WM_GETTEXT:
897             return COMBOEX_Forward (hwnd, uMsg, wParam, lParam);
898
899         case CB_SETITEMHEIGHT:
900             return COMBOEX_SetItemHeight (hwnd, wParam, lParam);
901
902
903         case WM_CREATE:
904             return COMBOEX_Create (hwnd, wParam, lParam);
905
906         case WM_DRAWITEM:
907             return COMBOEX_DrawItem (hwnd, wParam, lParam);
908
909         case WM_DESTROY:
910             return COMBOEX_Destroy (hwnd, wParam, lParam);
911
912         case WM_MEASUREITEM:
913             return COMBOEX_MeasureItem (hwnd, wParam, lParam);
914
915         case WM_SIZE:
916             return COMBOEX_Size (hwnd, wParam, lParam);
917
918         case WM_WINDOWPOSCHANGING:
919             return COMBOEX_WindowPosChanging (hwnd, wParam, lParam);
920
921         default:
922             if (uMsg >= WM_USER)
923                 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
924                      uMsg, wParam, lParam);
925             return DefWindowProcA (hwnd, uMsg, wParam, lParam);
926     }
927     return 0;
928 }
929
930
931 VOID
932 COMBOEX_Register (void)
933 {
934     WNDCLASSA wndClass;
935
936     ZeroMemory (&wndClass, sizeof(WNDCLASSA));
937     wndClass.style         = CS_GLOBALCLASS;
938     wndClass.lpfnWndProc   = (WNDPROC)COMBOEX_WindowProc;
939     wndClass.cbClsExtra    = 0;
940     wndClass.cbWndExtra    = sizeof(COMBOEX_INFO *);
941     wndClass.hCursor       = LoadCursorA (0, IDC_ARROWA);
942     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
943     wndClass.lpszClassName = WC_COMBOBOXEXA;
944  
945     RegisterClassA (&wndClass);
946 }
947
948
949 VOID
950 COMBOEX_Unregister (void)
951 {
952     UnregisterClassA (WC_COMBOBOXEXA, (HINSTANCE)NULL);
953 }
954