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