wined3d: Use glFinish() for synchronisation when cleaning up a destroyed context...
[wine.git] / dlls / comctl32 / comboex.c
blob0f7e46949e474f2d7ed3a5f46bb39d468082470e
1 /*
2 * ComboBoxEx control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 2000, 2001, 2002 Guy Albertelli <galberte@neo.lrun.com>
6 * Copyright 2002 Dimitrie O. Paun
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
24 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "commctrl.h"
31 #include "comctl32.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(comboex);
36 /* Item structure */
37 typedef struct _CBE_ITEMDATA
39 struct _CBE_ITEMDATA *next;
40 UINT mask;
41 LPWSTR pszText;
42 LPWSTR pszTemp;
43 int cchTextMax;
44 int iImage;
45 int iSelectedImage;
46 int iOverlay;
47 int iIndent;
48 LPARAM lParam;
49 } CBE_ITEMDATA;
51 /* ComboBoxEx structure */
52 typedef struct
54 HIMAGELIST himl;
55 HWND hwndSelf; /* my own hwnd */
56 HWND hwndNotify; /* my parent hwnd */
57 HWND hwndCombo;
58 HWND hwndEdit;
59 DWORD dwExtStyle;
60 INT selected; /* index of selected item */
61 DWORD flags; /* WINE internal flags */
62 HFONT defaultFont;
63 HFONT font;
64 INT nb_items; /* Number of items */
65 BOOL unicode; /* TRUE if this window is Unicode */
66 BOOL NtfUnicode; /* TRUE if parent wants notify in Unicode */
67 CBE_ITEMDATA edit; /* item data for edit item */
68 CBE_ITEMDATA *items; /* Array of items */
69 } COMBOEX_INFO;
71 /* internal flags in the COMBOEX_INFO structure */
72 #define WCBE_ACTEDIT 0x00000001 /* Edit active i.e.
73 * CBEN_BEGINEDIT issued
74 * but CBEN_ENDEDIT{A|W}
75 * not yet issued. */
76 #define WCBE_EDITCHG 0x00000002 /* Edit issued EN_CHANGE */
77 #define WCBE_EDITHASCHANGED (WCBE_ACTEDIT | WCBE_EDITCHG)
78 #define WCBE_EDITFOCUSED 0x00000004 /* Edit control has focus */
79 #define WCBE_MOUSECAPTURED 0x00000008 /* Combo has captured mouse */
80 #define WCBE_MOUSEDRAGGED 0x00000010 /* User has dragged in combo */
82 #define ID_CB_EDIT 1001
86 * Special flag set in DRAWITEMSTRUCT itemState field. It is set by
87 * the ComboEx version of the Combo Window Proc so that when the
88 * WM_DRAWITEM message is then passed to ComboEx, we know that this
89 * particular WM_DRAWITEM message is for listbox only items. Any message
90 * without this flag is then for the Edit control field.
92 * We really cannot use the ODS_COMBOBOXEDIT flag because MSDN states that
93 * only version 4.0 applications will have ODS_COMBOBOXEDIT set.
95 #define ODS_COMBOEXLBOX 0x4000
99 /* Height in pixels of control over the amount of the selected font */
100 #define CBE_EXTRA 3
102 /* Indent amount per MS documentation */
103 #define CBE_INDENT 10
105 /* Offset in pixels from left side for start of image or text */
106 #define CBE_STARTOFFSET 6
108 /* Offset between image and text */
109 #define CBE_SEP 4
111 #define COMBO_SUBCLASSID 1
112 #define EDIT_SUBCLASSID 2
114 #define COMBOEX_GetInfoPtr(hwnd) ((COMBOEX_INFO *)GetWindowLongPtrW (hwnd, 0))
116 static LRESULT CALLBACK COMBOEX_EditWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
117 UINT_PTR uId, DWORD_PTR ref_data);
118 static LRESULT CALLBACK COMBOEX_ComboWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
119 UINT_PTR uId, DWORD_PTR ref_data);
120 static LRESULT COMBOEX_Destroy (COMBOEX_INFO *infoPtr);
121 typedef INT (WINAPI *cmp_func_t)(LPCWSTR, LPCWSTR);
123 static inline BOOL is_textW(LPCWSTR str)
125 return str && str != LPSTR_TEXTCALLBACKW;
128 static inline BOOL is_textA(LPCSTR str)
130 return str && str != LPSTR_TEXTCALLBACKA;
133 static inline LPCSTR debugstr_txt(LPCWSTR str)
135 if (str == LPSTR_TEXTCALLBACKW) return "(callback)";
136 return debugstr_w(str);
139 static void COMBOEX_DumpItem (CBE_ITEMDATA const *item)
141 TRACE("item %p - mask=%08x, pszText=%p, cchTM=%d, iImage=%d\n",
142 item, item->mask, item->pszText, item->cchTextMax, item->iImage);
143 TRACE("item %p - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
144 item, item->iSelectedImage, item->iOverlay, item->iIndent, item->lParam);
145 if (item->mask & CBEIF_TEXT)
146 TRACE("item %p - pszText=%s\n", item, debugstr_txt(item->pszText));
150 static void COMBOEX_DumpInput (COMBOBOXEXITEMW const *input)
152 TRACE("input - mask=%08x, iItem=%ld, pszText=%p, cchTM=%d, iImage=%d\n",
153 input->mask, input->iItem, input->pszText, input->cchTextMax,
154 input->iImage);
155 if (input->mask & CBEIF_TEXT)
156 TRACE("input - pszText=<%s>\n", debugstr_txt(input->pszText));
157 TRACE("input - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
158 input->iSelectedImage, input->iOverlay, input->iIndent, input->lParam);
162 static inline CBE_ITEMDATA *get_item_data(const COMBOEX_INFO *infoPtr, INT index)
164 return (CBE_ITEMDATA *)SendMessageW (infoPtr->hwndCombo, CB_GETITEMDATA,
165 index, 0);
168 static inline cmp_func_t get_cmp_func(COMBOEX_INFO const *infoPtr)
170 return infoPtr->dwExtStyle & CBES_EX_CASESENSITIVE ? lstrcmpW : lstrcmpiW;
173 static INT COMBOEX_Notify (const COMBOEX_INFO *infoPtr, INT code, NMHDR *hdr)
175 hdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
176 hdr->hwndFrom = infoPtr->hwndSelf;
177 hdr->code = code;
178 if (infoPtr->NtfUnicode)
179 return SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, 0, (LPARAM)hdr);
180 else
181 return SendMessageA (infoPtr->hwndNotify, WM_NOTIFY, 0, (LPARAM)hdr);
185 static INT
186 COMBOEX_NotifyItem (const COMBOEX_INFO *infoPtr, UINT code, NMCOMBOBOXEXW *hdr)
188 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
189 if (infoPtr->NtfUnicode)
190 return COMBOEX_Notify (infoPtr, code, &hdr->hdr);
191 else {
192 LPWSTR wstr = hdr->ceItem.pszText;
193 LPSTR astr = 0;
194 INT ret, len = 0;
196 if ((hdr->ceItem.mask & CBEIF_TEXT) && is_textW(wstr)) {
197 len = WideCharToMultiByte (CP_ACP, 0, wstr, -1, 0, 0, NULL, NULL);
198 if (len > 0) {
199 astr = Alloc ((len + 1)*sizeof(CHAR));
200 if (!astr) return 0;
201 WideCharToMultiByte (CP_ACP, 0, wstr, -1, astr, len, 0, 0);
202 hdr->ceItem.pszText = (LPWSTR)astr;
206 if (code == CBEN_ENDEDITW) code = CBEN_ENDEDITA;
207 else if (code == CBEN_GETDISPINFOW) code = CBEN_GETDISPINFOA;
208 else if (code == CBEN_DRAGBEGINW) code = CBEN_DRAGBEGINA;
210 ret = COMBOEX_Notify (infoPtr, code, (NMHDR *)hdr);
212 if (astr && hdr->ceItem.pszText == (LPWSTR)astr)
213 hdr->ceItem.pszText = wstr;
215 Free(astr);
217 return ret;
222 static INT COMBOEX_NotifyEndEdit (const COMBOEX_INFO *infoPtr, NMCBEENDEDITW *neew, LPCWSTR wstr)
224 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
225 if (infoPtr->NtfUnicode) {
226 lstrcpynW(neew->szText, wstr, CBEMAXSTRLEN);
227 return COMBOEX_Notify (infoPtr, CBEN_ENDEDITW, &neew->hdr);
228 } else {
229 NMCBEENDEDITA neea;
231 neea.hdr = neew->hdr;
232 neea.fChanged = neew->fChanged;
233 neea.iNewSelection = neew->iNewSelection;
234 WideCharToMultiByte (CP_ACP, 0, wstr, -1, neea.szText, CBEMAXSTRLEN, 0, 0);
235 neea.iWhy = neew->iWhy;
237 return COMBOEX_Notify (infoPtr, CBEN_ENDEDITA, &neea.hdr);
242 static void COMBOEX_NotifyDragBegin(const COMBOEX_INFO *infoPtr, LPCWSTR wstr)
244 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
245 if (infoPtr->NtfUnicode) {
246 NMCBEDRAGBEGINW ndbw;
248 ndbw.iItemid = -1;
249 lstrcpynW(ndbw.szText, wstr, CBEMAXSTRLEN);
250 COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINW, &ndbw.hdr);
251 } else {
252 NMCBEDRAGBEGINA ndba;
254 ndba.iItemid = -1;
255 WideCharToMultiByte (CP_ACP, 0, wstr, -1, ndba.szText, CBEMAXSTRLEN, 0, 0);
257 COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINA, &ndba.hdr);
262 static void COMBOEX_FreeText (CBE_ITEMDATA *item)
264 if (is_textW(item->pszText)) Free(item->pszText);
265 item->pszText = NULL;
266 Free(item->pszTemp);
267 item->pszTemp = NULL;
271 static INT COMBOEX_GetIndex(COMBOEX_INFO const *infoPtr, CBE_ITEMDATA const *item)
273 CBE_ITEMDATA const *moving;
274 INT index;
276 moving = infoPtr->items;
277 index = infoPtr->nb_items - 1;
279 while (moving && (moving != item)) {
280 moving = moving->next;
281 index--;
283 if (!moving || (index < 0)) {
284 ERR("COMBOBOXEX item structures broken. Please report!\n");
285 return -1;
287 return index;
291 static LPCWSTR COMBOEX_GetText(const COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
293 NMCOMBOBOXEXW nmce;
294 LPWSTR text, buf;
295 INT len;
297 if (item->pszText != LPSTR_TEXTCALLBACKW)
298 return item->pszText;
300 ZeroMemory(&nmce, sizeof(nmce));
301 nmce.ceItem.mask = CBEIF_TEXT;
302 nmce.ceItem.lParam = item->lParam;
303 nmce.ceItem.iItem = COMBOEX_GetIndex(infoPtr, item);
304 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
306 if (is_textW(nmce.ceItem.pszText)) {
307 len = MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, NULL, 0);
308 buf = Alloc ((len + 1)*sizeof(WCHAR));
309 if (buf)
310 MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, buf, len);
311 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) {
312 COMBOEX_FreeText(item);
313 item->pszText = buf;
314 } else {
315 Free(item->pszTemp);
316 item->pszTemp = buf;
318 text = buf;
319 } else
320 text = nmce.ceItem.pszText;
322 if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
323 item->pszText = text;
324 return text;
328 static void COMBOEX_GetComboFontSize (const COMBOEX_INFO *infoPtr, SIZE *size)
330 HFONT nfont, ofont;
331 HDC mydc;
333 mydc = GetDC (0); /* why the entire screen???? */
334 nfont = (HFONT)SendMessageW (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
335 ofont = SelectObject (mydc, nfont);
336 GetTextExtentPointW (mydc, L"A", 1, size);
337 SelectObject (mydc, ofont);
338 ReleaseDC (0, mydc);
339 TRACE("selected font hwnd=%p, height=%d\n", nfont, size->cy);
343 static void COMBOEX_CopyItem (const CBE_ITEMDATA *item, COMBOBOXEXITEMW *cit)
345 if (cit->mask & CBEIF_TEXT) {
347 * when given a text buffer actually use that buffer
349 if (cit->pszText) {
350 if (is_textW(item->pszText))
351 lstrcpynW(cit->pszText, item->pszText, cit->cchTextMax);
352 else
353 cit->pszText[0] = 0;
354 } else {
355 cit->pszText = item->pszText;
356 cit->cchTextMax = item->cchTextMax;
359 if (cit->mask & CBEIF_IMAGE)
360 cit->iImage = item->iImage;
361 if (cit->mask & CBEIF_SELECTEDIMAGE)
362 cit->iSelectedImage = item->iSelectedImage;
363 if (cit->mask & CBEIF_OVERLAY)
364 cit->iOverlay = item->iOverlay;
365 if (cit->mask & CBEIF_INDENT)
366 cit->iIndent = item->iIndent;
367 if (cit->mask & CBEIF_LPARAM)
368 cit->lParam = item->lParam;
372 static void COMBOEX_AdjustEditPos (const COMBOEX_INFO *infoPtr)
374 SIZE mysize;
375 INT x, y, w, h, xioff;
376 RECT rect;
378 if (!infoPtr->hwndEdit) return;
380 if (infoPtr->himl && !(infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGEINDENT)) {
381 IMAGEINFO iinfo;
382 iinfo.rcImage.left = iinfo.rcImage.right = 0;
383 ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
384 xioff = iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP;
385 } else xioff = 0;
387 GetClientRect (infoPtr->hwndCombo, &rect);
388 InflateRect (&rect, -2, -2);
389 InvalidateRect (infoPtr->hwndCombo, &rect, TRUE);
391 /* reposition the Edit control based on whether icon exists */
392 COMBOEX_GetComboFontSize (infoPtr, &mysize);
393 TRACE("Combo font x=%d, y=%d\n", mysize.cx, mysize.cy);
394 x = xioff + CBE_STARTOFFSET + 1;
395 w = rect.right-rect.left - x - GetSystemMetrics(SM_CXVSCROLL) - 1;
396 h = mysize.cy + 1;
397 y = rect.bottom - h - 1;
399 TRACE("Combo client (%s), setting Edit to (%d,%d)-(%d,%d)\n",
400 wine_dbgstr_rect(&rect), x, y, x + w, y + h);
401 SetWindowPos(infoPtr->hwndEdit, HWND_TOP, x, y, w, h,
402 SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER);
406 static void COMBOEX_ReSize (const COMBOEX_INFO *infoPtr)
408 SIZE mysize;
409 LONG cy;
410 IMAGEINFO iinfo;
412 COMBOEX_GetComboFontSize (infoPtr, &mysize);
413 cy = mysize.cy + CBE_EXTRA;
414 if (infoPtr->himl && ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo)) {
415 cy = max (iinfo.rcImage.bottom - iinfo.rcImage.top, cy);
416 TRACE("upgraded height due to image: height=%d\n", cy);
418 SendMessageW (infoPtr->hwndSelf, CB_SETITEMHEIGHT, -1, cy);
419 if (infoPtr->hwndCombo) {
420 SendMessageW (infoPtr->hwndCombo, CB_SETITEMHEIGHT, 0, cy);
421 if ( !(infoPtr->flags & CBES_EX_NOSIZELIMIT)) {
422 RECT comboRect, ourRect;
423 GetWindowRect(infoPtr->hwndCombo, &comboRect);
424 GetWindowRect(infoPtr->hwndSelf, &ourRect);
425 if (comboRect.bottom > ourRect.bottom)
426 SetWindowPos( infoPtr->hwndSelf, 0, 0, 0, ourRect.right - ourRect.left,
427 comboRect.bottom - comboRect.top,
428 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
434 static void COMBOEX_SetEditText (const COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
436 if (!infoPtr->hwndEdit) return;
438 if (item->mask & CBEIF_TEXT) {
439 SendMessageW (infoPtr->hwndEdit, WM_SETTEXT, 0, (LPARAM)COMBOEX_GetText(infoPtr, item));
440 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, 0);
441 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, -1);
446 static CBE_ITEMDATA *COMBOEX_FindItem(COMBOEX_INFO *infoPtr, INT_PTR index)
448 CBE_ITEMDATA *item;
449 INT i;
451 if ((index >= infoPtr->nb_items) || (index < -1))
452 return NULL;
453 if (index == -1)
454 return &infoPtr->edit;
455 item = infoPtr->items;
456 i = infoPtr->nb_items - 1;
458 /* find the item in the list */
459 while (item && (i > index)) {
460 item = item->next;
461 i--;
463 if (!item || (i != index)) {
464 ERR("COMBOBOXEX item structures broken. Please report!\n");
465 return 0;
467 return item;
470 /* *** CBEM_xxx message support *** */
472 static UINT COMBOEX_GetListboxText(COMBOEX_INFO *infoPtr, INT_PTR n, LPWSTR buf)
474 CBE_ITEMDATA *item;
475 LPCWSTR str;
477 item = COMBOEX_FindItem(infoPtr, n);
478 if (!item)
479 return 0;
481 str = COMBOEX_GetText(infoPtr, item);
482 if (!str)
484 if (buf)
486 if (infoPtr->unicode)
487 buf[0] = 0;
488 else
489 *((LPSTR)buf) = 0;
491 return 0;
494 if (infoPtr->unicode)
496 if (buf)
497 lstrcpyW(buf, str);
498 return lstrlenW(str);
500 else
502 UINT r;
503 r = WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)buf, 0x40000000, NULL, NULL);
504 if (r) r--;
505 return r;
510 static INT COMBOEX_DeleteItem (COMBOEX_INFO *infoPtr, INT_PTR index)
512 TRACE("(index=%ld)\n", index);
514 /* if item number requested does not exist then return failure */
515 if ((index >= infoPtr->nb_items) || (index < 0)) return CB_ERR;
516 if (!COMBOEX_FindItem(infoPtr, index)) return CB_ERR;
518 /* doing this will result in WM_DELETEITEM being issued */
519 SendMessageW (infoPtr->hwndCombo, CB_DELETESTRING, index, 0);
521 return infoPtr->nb_items;
525 static BOOL COMBOEX_GetItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW *cit)
527 INT_PTR index = cit->iItem;
528 CBE_ITEMDATA *item;
530 TRACE("\n");
532 /* if item number requested does not exist then return failure */
533 if ((index >= infoPtr->nb_items) || (index < -1)) return FALSE;
535 /* if the item is the edit control and there is no edit control, skip */
536 if ((index == -1) && !infoPtr->hwndEdit) return FALSE;
538 if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
540 COMBOEX_CopyItem (item, cit);
542 return TRUE;
546 static BOOL COMBOEX_GetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA *cit)
548 COMBOBOXEXITEMW tmpcit;
550 TRACE("\n");
552 tmpcit.mask = cit->mask;
553 tmpcit.iItem = cit->iItem;
554 tmpcit.pszText = 0;
555 if(!COMBOEX_GetItemW (infoPtr, &tmpcit)) return FALSE;
557 if (cit->mask & CBEIF_TEXT)
559 if (is_textW(tmpcit.pszText) && cit->pszText)
560 WideCharToMultiByte(CP_ACP, 0, tmpcit.pszText, -1,
561 cit->pszText, cit->cchTextMax, NULL, NULL);
562 else if (cit->pszText) cit->pszText[0] = 0;
563 else cit->pszText = (LPSTR)tmpcit.pszText;
566 if (cit->mask & CBEIF_IMAGE)
567 cit->iImage = tmpcit.iImage;
568 if (cit->mask & CBEIF_SELECTEDIMAGE)
569 cit->iSelectedImage = tmpcit.iSelectedImage;
570 if (cit->mask & CBEIF_OVERLAY)
571 cit->iOverlay = tmpcit.iOverlay;
572 if (cit->mask & CBEIF_INDENT)
573 cit->iIndent = tmpcit.iIndent;
574 if (cit->mask & CBEIF_LPARAM)
575 cit->lParam = tmpcit.lParam;
577 return TRUE;
581 static inline BOOL COMBOEX_HasEditChanged (COMBOEX_INFO const *infoPtr)
583 return infoPtr->hwndEdit && (infoPtr->flags & WCBE_EDITHASCHANGED) == WCBE_EDITHASCHANGED;
587 static INT COMBOEX_InsertItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW const *cit)
589 INT_PTR index;
590 CBE_ITEMDATA *item;
591 NMCOMBOBOXEXW nmcit;
593 TRACE("\n");
595 if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
597 /* get real index of item to insert */
598 index = cit->iItem;
599 if (index == -1) index = infoPtr->nb_items;
600 if (index > infoPtr->nb_items) return -1;
602 /* get zero-filled space and chain it in */
603 if(!(item = Alloc (sizeof(*item)))) return -1;
605 /* locate position to insert new item in */
606 if (index == infoPtr->nb_items) {
607 /* fast path for iItem = -1 */
608 item->next = infoPtr->items;
609 infoPtr->items = item;
611 else {
612 INT i = infoPtr->nb_items-1;
613 CBE_ITEMDATA *moving = infoPtr->items;
615 while ((i > index) && moving) {
616 moving = moving->next;
617 i--;
619 if (!moving) {
620 ERR("COMBOBOXEX item structures broken. Please report!\n");
621 Free(item);
622 return -1;
624 item->next = moving->next;
625 moving->next = item;
628 /* fill in our hidden item structure */
629 item->mask = cit->mask;
630 if (item->mask & CBEIF_TEXT) {
631 INT len = 0;
633 if (is_textW(cit->pszText)) len = lstrlenW (cit->pszText);
634 if (len > 0) {
635 item->pszText = Alloc ((len + 1)*sizeof(WCHAR));
636 if (!item->pszText) {
637 Free(item);
638 return -1;
640 lstrcpyW (item->pszText, cit->pszText);
642 else if (cit->pszText == LPSTR_TEXTCALLBACKW)
643 item->pszText = LPSTR_TEXTCALLBACKW;
644 item->cchTextMax = cit->cchTextMax;
646 if (item->mask & CBEIF_IMAGE)
647 item->iImage = cit->iImage;
648 if (item->mask & CBEIF_SELECTEDIMAGE)
649 item->iSelectedImage = cit->iSelectedImage;
650 if (item->mask & CBEIF_OVERLAY)
651 item->iOverlay = cit->iOverlay;
652 if (item->mask & CBEIF_INDENT)
653 item->iIndent = cit->iIndent;
654 if (item->mask & CBEIF_LPARAM)
655 item->lParam = cit->lParam;
656 infoPtr->nb_items++;
658 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
660 SendMessageW (infoPtr->hwndCombo, CB_INSERTSTRING, cit->iItem, (LPARAM)item);
662 memset (&nmcit.ceItem, 0, sizeof(nmcit.ceItem));
663 nmcit.ceItem.mask=~0;
664 COMBOEX_CopyItem (item, &nmcit.ceItem);
665 COMBOEX_NotifyItem (infoPtr, CBEN_INSERTITEM, &nmcit);
667 return index;
672 static INT COMBOEX_InsertItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA const *cit)
674 COMBOBOXEXITEMW citW;
675 LPWSTR wstr = NULL;
676 INT ret;
678 memcpy(&citW,cit,sizeof(COMBOBOXEXITEMA));
679 if (cit->mask & CBEIF_TEXT && is_textA(cit->pszText)) {
680 INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
681 wstr = Alloc ((len + 1)*sizeof(WCHAR));
682 if (!wstr) return -1;
683 MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
684 citW.pszText = wstr;
686 ret = COMBOEX_InsertItemW(infoPtr, &citW);
688 Free(wstr);
690 return ret;
694 static DWORD
695 COMBOEX_SetExtendedStyle (COMBOEX_INFO *infoPtr, DWORD mask, DWORD style)
697 DWORD dwTemp;
699 TRACE("(mask=x%08x, style=0x%08x)\n", mask, style);
701 dwTemp = infoPtr->dwExtStyle;
703 if (mask)
704 infoPtr->dwExtStyle = (infoPtr->dwExtStyle & ~mask) | style;
705 else
706 infoPtr->dwExtStyle = style;
708 /* see if we need to change the word break proc on the edit */
709 if ((infoPtr->dwExtStyle ^ dwTemp) & CBES_EX_PATHWORDBREAKPROC)
710 SetPathWordBreakProc(infoPtr->hwndEdit,
711 (infoPtr->dwExtStyle & CBES_EX_PATHWORDBREAKPROC) != 0);
713 /* test if the control's appearance has changed */
714 mask = CBES_EX_NOEDITIMAGE | CBES_EX_NOEDITIMAGEINDENT;
715 if ((infoPtr->dwExtStyle & mask) != (dwTemp & mask)) {
716 /* if state of EX_NOEDITIMAGE changes, invalidate all */
717 TRACE("EX_NOEDITIMAGE state changed to %d\n",
718 infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGE);
719 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
720 COMBOEX_AdjustEditPos (infoPtr);
721 if (infoPtr->hwndEdit)
722 InvalidateRect (infoPtr->hwndEdit, NULL, TRUE);
725 return dwTemp;
729 static HIMAGELIST COMBOEX_SetImageList (COMBOEX_INFO *infoPtr, HIMAGELIST himl)
731 HIMAGELIST himlTemp = infoPtr->himl;
733 TRACE("\n");
735 infoPtr->himl = himl;
737 COMBOEX_ReSize (infoPtr);
738 InvalidateRect (infoPtr->hwndCombo, NULL, TRUE);
740 /* reposition the Edit control based on whether icon exists */
741 COMBOEX_AdjustEditPos (infoPtr);
742 return himlTemp;
745 static BOOL COMBOEX_SetItemW (COMBOEX_INFO *infoPtr, const COMBOBOXEXITEMW *cit)
747 INT_PTR index = cit->iItem;
748 CBE_ITEMDATA *item;
750 if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
752 /* if item number requested does not exist then return failure */
753 if ((index >= infoPtr->nb_items) || (index < -1)) return FALSE;
755 /* if the item is the edit control and there is no edit control, skip */
756 if ((index == -1) && !infoPtr->hwndEdit) return FALSE;
758 if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
760 /* add/change stuff to the internal item structure */
761 item->mask |= cit->mask;
762 if (cit->mask & CBEIF_TEXT) {
763 INT len = 0;
765 COMBOEX_FreeText(item);
766 if (is_textW(cit->pszText)) len = lstrlenW(cit->pszText);
767 if (len > 0) {
768 item->pszText = Alloc ((len + 1)*sizeof(WCHAR));
769 if (!item->pszText) return FALSE;
770 lstrcpyW(item->pszText, cit->pszText);
771 } else if (cit->pszText == LPSTR_TEXTCALLBACKW)
772 item->pszText = LPSTR_TEXTCALLBACKW;
773 item->cchTextMax = cit->cchTextMax;
775 if (cit->mask & CBEIF_IMAGE)
776 item->iImage = cit->iImage;
777 if (cit->mask & CBEIF_SELECTEDIMAGE)
778 item->iSelectedImage = cit->iSelectedImage;
779 if (cit->mask & CBEIF_OVERLAY)
780 item->iOverlay = cit->iOverlay;
781 if (cit->mask & CBEIF_INDENT)
782 item->iIndent = cit->iIndent;
783 if (cit->mask & CBEIF_LPARAM)
784 item->lParam = cit->lParam;
786 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
788 /* if original request was to update edit control, do some fast foot work */
789 if (cit->iItem == -1 && cit->mask & CBEIF_TEXT) {
790 COMBOEX_SetEditText (infoPtr, item);
791 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE | RDW_INVALIDATE);
793 return TRUE;
796 static BOOL COMBOEX_SetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA const *cit)
798 COMBOBOXEXITEMW citW;
799 LPWSTR wstr = NULL;
800 BOOL ret;
802 memcpy(&citW, cit, sizeof(COMBOBOXEXITEMA));
803 if ((cit->mask & CBEIF_TEXT) && is_textA(cit->pszText)) {
804 INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
805 wstr = Alloc ((len + 1)*sizeof(WCHAR));
806 if (!wstr) return FALSE;
807 MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
808 citW.pszText = wstr;
810 ret = COMBOEX_SetItemW(infoPtr, &citW);
812 Free(wstr);
814 return ret;
818 static BOOL COMBOEX_SetUnicodeFormat (COMBOEX_INFO *infoPtr, BOOL value)
820 BOOL bTemp = infoPtr->unicode;
822 TRACE("to %s, was %s\n", value ? "TRUE":"FALSE", bTemp ? "TRUE":"FALSE");
824 infoPtr->unicode = value;
826 return bTemp;
830 /* *** CB_xxx message support *** */
832 static INT
833 COMBOEX_FindStringExact (const COMBOEX_INFO *infoPtr, INT start, LPCWSTR str)
835 INT i;
836 cmp_func_t cmptext = get_cmp_func(infoPtr);
837 INT count = SendMessageW (infoPtr->hwndCombo, CB_GETCOUNT, 0, 0);
839 /* now search from after starting loc and wrapping back to start */
840 for(i=start+1; i<count; i++) {
841 CBE_ITEMDATA *item = get_item_data(infoPtr, i);
842 if ((LRESULT)item == CB_ERR) continue;
843 if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
845 for(i=0; i<=start; i++) {
846 CBE_ITEMDATA *item = get_item_data(infoPtr, i);
847 if ((LRESULT)item == CB_ERR) continue;
848 if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
850 return CB_ERR;
854 static DWORD_PTR COMBOEX_GetItemData (COMBOEX_INFO *infoPtr, INT_PTR index)
856 CBE_ITEMDATA const *item1;
857 CBE_ITEMDATA const *item2;
858 DWORD_PTR ret = 0;
860 item1 = get_item_data(infoPtr, index);
861 if ((item1 != NULL) && ((LRESULT)item1 != CB_ERR)) {
862 item2 = COMBOEX_FindItem (infoPtr, index);
863 if (item2 != item1) {
864 ERR("data structures damaged!\n");
865 return CB_ERR;
867 if (item1->mask & CBEIF_LPARAM) ret = item1->lParam;
868 TRACE("returning 0x%08lx\n", ret);
869 } else {
870 ret = (DWORD_PTR)item1;
871 TRACE("non-valid result from combo, returning 0x%08lx\n", ret);
873 return ret;
877 static INT COMBOEX_SetCursel (COMBOEX_INFO *infoPtr, INT_PTR index)
879 CBE_ITEMDATA *item;
880 INT sel;
882 if (!(item = COMBOEX_FindItem(infoPtr, index)))
883 return SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, index, 0);
885 TRACE("selecting item %ld text=%s\n", index, debugstr_txt(item->pszText));
886 infoPtr->selected = index;
888 sel = (INT)SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, index, 0);
889 COMBOEX_SetEditText (infoPtr, item);
890 return sel;
894 static DWORD_PTR COMBOEX_SetItemData (COMBOEX_INFO *infoPtr, INT_PTR index, DWORD_PTR data)
896 CBE_ITEMDATA *item1;
897 CBE_ITEMDATA const *item2;
899 item1 = get_item_data(infoPtr, index);
900 if ((item1 != NULL) && ((LRESULT)item1 != CB_ERR)) {
901 item2 = COMBOEX_FindItem (infoPtr, index);
902 if (item2 != item1) {
903 ERR("data structures damaged!\n");
904 return CB_ERR;
906 item1->mask |= CBEIF_LPARAM;
907 item1->lParam = data;
908 TRACE("setting lparam to 0x%08lx\n", data);
909 return 0;
911 TRACE("non-valid result from combo %p\n", item1);
912 return (DWORD_PTR)item1;
916 static INT COMBOEX_SetItemHeight (COMBOEX_INFO const *infoPtr, INT index, UINT height)
918 RECT cb_wrect, cbx_wrect, cbx_crect;
920 /* First, lets forward the message to the normal combo control
921 just like Windows. */
922 if (infoPtr->hwndCombo)
923 if (SendMessageW (infoPtr->hwndCombo, CB_SETITEMHEIGHT,
924 index, height) == CB_ERR) return CB_ERR;
926 GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
927 GetWindowRect (infoPtr->hwndSelf, &cbx_wrect);
928 GetClientRect (infoPtr->hwndSelf, &cbx_crect);
929 /* the height of comboex as height of the combo + comboex border */
930 height = cb_wrect.bottom-cb_wrect.top
931 + cbx_wrect.bottom-cbx_wrect.top
932 - (cbx_crect.bottom-cbx_crect.top);
933 TRACE("EX window=(%s), client=(%s)\n",
934 wine_dbgstr_rect(&cbx_wrect), wine_dbgstr_rect(&cbx_crect));
935 TRACE("CB window=(%s), EX setting=(0,0)-(%d,%d)\n",
936 wine_dbgstr_rect(&cbx_wrect), cbx_wrect.right-cbx_wrect.left, height);
937 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0,
938 cbx_wrect.right-cbx_wrect.left, height,
939 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
941 return 0;
945 /* *** WM_xxx message support *** */
948 static LRESULT COMBOEX_Create (HWND hwnd, CREATESTRUCTA const *cs)
950 COMBOEX_INFO *infoPtr;
951 LOGFONTW mylogfont;
952 RECT win_rect;
953 INT i;
955 /* allocate memory for info structure */
956 infoPtr = Alloc (sizeof(COMBOEX_INFO));
957 if (!infoPtr) return -1;
959 /* initialize info structure */
960 /* note that infoPtr is allocated zero-filled */
962 infoPtr->hwndSelf = hwnd;
963 infoPtr->selected = -1;
965 infoPtr->unicode = IsWindowUnicode (hwnd);
966 infoPtr->hwndNotify = cs->hwndParent;
968 i = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
969 if ((i != NFR_ANSI) && (i != NFR_UNICODE)) {
970 WARN("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n", i);
971 i = NFR_ANSI;
973 infoPtr->NtfUnicode = (i == NFR_UNICODE);
975 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
977 if (TRACE_ON(comboex)) {
978 RECT client, rect;
979 GetWindowRect(hwnd, &rect);
980 GetClientRect(hwnd, &client);
981 TRACE("EX window=(%s), client=(%s)\n",
982 wine_dbgstr_rect(&rect), wine_dbgstr_rect(&client));
985 /* Native version of ComboEx creates the ComboBox with DROPDOWNLIST */
986 /* specified. It then creates its own version of the EDIT control */
987 /* and makes the ComboBox the parent. This is because a normal */
988 /* DROPDOWNLIST does not have an EDIT control, but we need one. */
989 /* We also need to place the edit control at the proper location */
990 /* (allow space for the icons). */
992 infoPtr->hwndCombo = CreateWindowW (WC_COMBOBOXW, L"",
993 WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VSCROLL |
994 CBS_NOINTEGRALHEIGHT | CBS_DROPDOWNLIST |
995 WS_CHILD | WS_VISIBLE | CBS_OWNERDRAWFIXED |
996 GetWindowLongW (hwnd, GWL_STYLE),
997 cs->y, cs->x, cs->cx, cs->cy, hwnd,
998 (HMENU) GetWindowLongPtrW (hwnd, GWLP_ID),
999 (HINSTANCE)GetWindowLongPtrW (hwnd, GWLP_HINSTANCE), NULL);
1001 SetWindowSubclass(infoPtr->hwndCombo, COMBOEX_ComboWndProc, COMBO_SUBCLASSID,
1002 (DWORD_PTR)hwnd);
1003 infoPtr->font = (HFONT)SendMessageW (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
1006 * Now create our own EDIT control so we can position it.
1007 * It is created only for CBS_DROPDOWN style
1009 if ((cs->style & CBS_DROPDOWNLIST) == CBS_DROPDOWN) {
1010 infoPtr->hwndEdit = CreateWindowExW (0, WC_EDITW, L"",
1011 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | ES_AUTOHSCROLL,
1012 0, 0, 0, 0, /* will set later */
1013 infoPtr->hwndCombo,
1014 (HMENU) GetWindowLongPtrW (hwnd, GWLP_ID),
1015 (HINSTANCE)GetWindowLongPtrW (hwnd, GWLP_HINSTANCE), NULL);
1017 SetWindowSubclass(infoPtr->hwndEdit, COMBOEX_EditWndProc, EDIT_SUBCLASSID,
1018 (DWORD_PTR)hwnd);
1020 infoPtr->font = (HFONT)SendMessageW(infoPtr->hwndCombo, WM_GETFONT, 0, 0);
1024 * Locate the default font if necessary and then set it in
1025 * all associated controls
1027 if (!infoPtr->font) {
1028 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, sizeof(mylogfont),
1029 &mylogfont, 0);
1030 infoPtr->font = infoPtr->defaultFont = CreateFontIndirectW (&mylogfont);
1032 SendMessageW (infoPtr->hwndCombo, WM_SETFONT, (WPARAM)infoPtr->font, 0);
1033 if (infoPtr->hwndEdit) {
1034 SendMessageW (infoPtr->hwndEdit, WM_SETFONT, (WPARAM)infoPtr->font, 0);
1035 SendMessageW (infoPtr->hwndEdit, EM_SETMARGINS, EC_USEFONTINFO, 0);
1038 COMBOEX_ReSize (infoPtr);
1040 /* Above is fairly certain, below is much less certain. */
1042 GetWindowRect(hwnd, &win_rect);
1044 if (TRACE_ON(comboex)) {
1045 RECT client, rect;
1046 GetClientRect(hwnd, &client);
1047 GetWindowRect(infoPtr->hwndCombo, &rect);
1048 TRACE("EX window=(%s) client=(%s) CB wnd=(%s)\n",
1049 wine_dbgstr_rect(&win_rect), wine_dbgstr_rect(&client),
1050 wine_dbgstr_rect(&rect));
1052 SetWindowPos(infoPtr->hwndCombo, HWND_TOP, 0, 0,
1053 win_rect.right - win_rect.left, win_rect.bottom - win_rect.top,
1054 SWP_NOACTIVATE | SWP_NOREDRAW);
1056 GetWindowRect(infoPtr->hwndCombo, &win_rect);
1057 TRACE("CB window=(%s)\n", wine_dbgstr_rect(&win_rect));
1058 SetWindowPos(hwnd, HWND_TOP, 0, 0,
1059 win_rect.right - win_rect.left, win_rect.bottom - win_rect.top,
1060 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
1062 COMBOEX_AdjustEditPos (infoPtr);
1064 return 0;
1068 static LRESULT COMBOEX_Command (COMBOEX_INFO *infoPtr, WPARAM wParam)
1070 LRESULT lret;
1071 INT command = HIWORD(wParam);
1072 CBE_ITEMDATA *item = 0;
1073 WCHAR wintext[520];
1074 INT cursel, n;
1075 INT_PTR oldItem;
1076 NMCBEENDEDITW cbeend;
1077 DWORD oldflags;
1078 HWND parent = infoPtr->hwndNotify;
1080 TRACE("for command %d\n", command);
1082 switch (command)
1084 case CBN_DROPDOWN:
1085 SetFocus (infoPtr->hwndCombo);
1086 ShowWindow (infoPtr->hwndEdit, SW_HIDE);
1087 infoPtr->flags |= WCBE_ACTEDIT;
1088 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1089 case CBN_CLOSEUP:
1090 SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1091 ShowWindow (infoPtr->hwndEdit, SW_SHOW);
1092 InvalidateRect (infoPtr->hwndCombo, 0, TRUE);
1093 if (infoPtr->hwndEdit) InvalidateRect (infoPtr->hwndEdit, 0, TRUE);
1094 cursel = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1095 if (cursel == -1) {
1096 cmp_func_t cmptext = get_cmp_func(infoPtr);
1097 /* find match from edit against those in Combobox */
1098 GetWindowTextW (infoPtr->hwndEdit, wintext, 520);
1099 n = SendMessageW (infoPtr->hwndCombo, CB_GETCOUNT, 0, 0);
1100 for (cursel = 0; cursel < n; cursel++){
1101 item = get_item_data(infoPtr, cursel);
1102 if ((INT_PTR)item == CB_ERR) break;
1103 if (!cmptext(COMBOEX_GetText(infoPtr, item), wintext)) break;
1105 if ((cursel == n) || ((INT_PTR)item == CB_ERR)) {
1106 TRACE("failed to find match??? item=%p cursel=%d\n",
1107 item, cursel);
1108 if (infoPtr->hwndEdit) SetFocus(infoPtr->hwndEdit);
1109 return 0;
1112 else {
1113 item = get_item_data(infoPtr, cursel);
1114 if ((INT_PTR)item == CB_ERR) {
1115 TRACE("failed to find match??? item=%p cursel=%d\n",
1116 item, cursel);
1117 if (infoPtr->hwndEdit) SetFocus(infoPtr->hwndEdit);
1118 return 0;
1122 /* Save flags for testing and reset them */
1123 oldflags = infoPtr->flags;
1124 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1126 if (oldflags & WCBE_ACTEDIT) {
1127 cbeend.fChanged = (oldflags & WCBE_EDITCHG);
1128 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1129 CB_GETCURSEL, 0, 0);
1130 cbeend.iWhy = CBENF_DROPDOWN;
1132 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, COMBOEX_GetText(infoPtr, item))) return 0;
1135 /* if selection has changed the set the new current selection */
1136 cursel = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1137 if ((oldflags & WCBE_EDITCHG) || (cursel != infoPtr->selected)) {
1138 infoPtr->selected = cursel;
1139 SendMessageW (infoPtr->hwndSelf, CB_SETCURSEL, cursel, 0);
1140 SetFocus(infoPtr->hwndCombo);
1142 return 0;
1144 case CBN_SELCHANGE:
1146 * CB_GETCURSEL(Combo)
1147 * CB_GETITEMDATA(Combo) < simulated by COMBOEX_FindItem
1148 * lstrlenA
1149 * WM_SETTEXT(Edit)
1150 * WM_GETTEXTLENGTH(Edit)
1151 * WM_GETTEXT(Edit)
1152 * EM_SETSEL(Edit, 0,0)
1153 * WM_GETTEXTLENGTH(Edit)
1154 * WM_GETTEXT(Edit)
1155 * EM_SETSEL(Edit, 0,len)
1156 * return WM_COMMAND to parent
1158 oldItem = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1159 if (!(item = COMBOEX_FindItem(infoPtr, oldItem))) {
1160 ERR("item %ld not found. Problem!\n", oldItem);
1161 break;
1163 infoPtr->selected = oldItem;
1164 COMBOEX_SetEditText (infoPtr, item);
1165 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1167 case CBN_SELENDOK:
1168 case CBN_SELENDCANCEL:
1170 * We have to change the handle since we are the control
1171 * issuing the message. IE4 depends on this.
1173 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1175 case CBN_KILLFOCUS:
1176 SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1177 if (infoPtr->flags & WCBE_ACTEDIT) {
1178 GetWindowTextW (infoPtr->hwndEdit, wintext, 260);
1179 cbeend.fChanged = (infoPtr->flags & WCBE_EDITCHG);
1180 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1181 CB_GETCURSEL, 0, 0);
1182 cbeend.iWhy = CBENF_KILLFOCUS;
1184 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1185 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, wintext)) return 0;
1187 /* possible CB_GETCURSEL */
1188 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1189 return 0;
1191 case CBN_SETFOCUS:
1192 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1194 default:
1196 * We have to change the handle since we are the control
1197 * issuing the message. IE4 depends on this.
1198 * We also need to set the focus back to the Edit control
1199 * after passing the command to the parent of the ComboEx.
1201 lret = SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1202 if (infoPtr->hwndEdit) SetFocus(infoPtr->hwndEdit);
1203 return lret;
1205 return 0;
1209 static BOOL COMBOEX_WM_DeleteItem (COMBOEX_INFO *infoPtr, DELETEITEMSTRUCT const *dis)
1211 CBE_ITEMDATA *item, *olditem;
1212 NMCOMBOBOXEXW nmcit;
1213 UINT i;
1215 TRACE("CtlType=%08x, CtlID=%08x, itemID=%08x, hwnd=%p, data=%08lx\n",
1216 dis->CtlType, dis->CtlID, dis->itemID, dis->hwndItem, dis->itemData);
1218 if (dis->itemID >= infoPtr->nb_items) return FALSE;
1220 olditem = infoPtr->items;
1221 i = infoPtr->nb_items - 1;
1223 if (i == dis->itemID) {
1224 infoPtr->items = infoPtr->items->next;
1226 else {
1227 item = olditem;
1228 i--;
1230 /* find the prior item in the list */
1231 while (item->next && (i > dis->itemID)) {
1232 item = item->next;
1233 i--;
1235 if (!item->next || (i != dis->itemID)) {
1236 ERR("COMBOBOXEX item structures broken. Please report!\n");
1237 return FALSE;
1239 olditem = item->next;
1240 item->next = item->next->next;
1242 infoPtr->nb_items--;
1244 memset (&nmcit.ceItem, 0, sizeof(nmcit.ceItem));
1245 nmcit.ceItem.mask=~0;
1246 COMBOEX_CopyItem (olditem, &nmcit.ceItem);
1247 COMBOEX_NotifyItem (infoPtr, CBEN_DELETEITEM, &nmcit);
1249 COMBOEX_FreeText(olditem);
1250 Free(olditem);
1252 return TRUE;
1256 static LRESULT COMBOEX_DrawItem (COMBOEX_INFO *infoPtr, DRAWITEMSTRUCT const *dis)
1258 CBE_ITEMDATA *item = NULL;
1259 SIZE txtsize;
1260 RECT rect;
1261 LPCWSTR str = L"";
1262 UINT xbase, x, y;
1263 INT len;
1264 COLORREF nbkc, ntxc, bkc, txc;
1265 int drawimage, drawstate, xioff, selected;
1267 TRACE("DRAWITEMSTRUCT: CtlType=0x%08x CtlID=0x%08x\n",
1268 dis->CtlType, dis->CtlID);
1269 TRACE("itemID=0x%08x itemAction=0x%08x itemState=0x%08x\n",
1270 dis->itemID, dis->itemAction, dis->itemState);
1271 TRACE("hWnd=%p hDC=%p (%s) itemData=0x%08lx\n",
1272 dis->hwndItem, dis->hDC, wine_dbgstr_rect(&dis->rcItem), dis->itemData);
1274 /* MSDN says: */
1275 /* "itemID - Specifies the menu item identifier for a menu */
1276 /* item or the index of the item in a list box or combo box. */
1277 /* For an empty list box or combo box, this member can be -1. */
1278 /* This allows the application to draw only the focus */
1279 /* rectangle at the coordinates specified by the rcItem */
1280 /* member even though there are no items in the control. */
1281 /* This indicates to the user whether the list box or combo */
1282 /* box has the focus. How the bits are set in the itemAction */
1283 /* member determines whether the rectangle is to be drawn as */
1284 /* though the list box or combo box has the focus. */
1285 if (dis->itemID == 0xffffffff) {
1286 if ( ( (dis->itemAction & ODA_FOCUS) && (dis->itemState & ODS_SELECTED)) ||
1287 ( (dis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)) && (dis->itemState & ODS_FOCUS) ) ) {
1289 TRACE("drawing item -1 special focus, rect=(%s)\n",
1290 wine_dbgstr_rect(&dis->rcItem));
1292 else if ((dis->CtlType == ODT_COMBOBOX) &&
1293 (dis->itemAction == ODA_DRAWENTIRE)) {
1294 /* draw of edit control data */
1296 if (TRACE_ON(comboex)) {
1297 RECT exrc, cbrc, edrc;
1298 GetWindowRect (infoPtr->hwndSelf, &exrc);
1299 GetWindowRect (infoPtr->hwndCombo, &cbrc);
1300 SetRect(&edrc, -1, -1, -1, -1);
1301 if (infoPtr->hwndEdit) GetWindowRect (infoPtr->hwndEdit, &edrc);
1302 TRACE("window rects ex=(%s), cb=(%s), ed=(%s)\n",
1303 wine_dbgstr_rect(&exrc), wine_dbgstr_rect(&cbrc),
1304 wine_dbgstr_rect(&edrc));
1307 else {
1308 ERR("NOT drawing item -1 special focus, rect=(%s), action=%08x, state=%08x\n",
1309 wine_dbgstr_rect(&dis->rcItem),
1310 dis->itemAction, dis->itemState);
1311 return 0;
1315 /* If draw item is -1 (edit control) setup the item pointer */
1316 if (dis->itemID == 0xffffffff) {
1317 item = &infoPtr->edit;
1319 if (infoPtr->hwndEdit) {
1320 /* free previous text of edit item */
1321 COMBOEX_FreeText(item);
1322 item->mask &= ~CBEIF_TEXT;
1323 if( (len = GetWindowTextLengthW(infoPtr->hwndEdit)) ) {
1324 item->mask |= CBEIF_TEXT;
1325 item->pszText = Alloc ((len + 1)*sizeof(WCHAR));
1326 if (item->pszText)
1327 GetWindowTextW(infoPtr->hwndEdit, item->pszText, len+1);
1329 TRACE("edit control hwndEdit=%p, text len=%d str=%s\n",
1330 infoPtr->hwndEdit, len, debugstr_txt(item->pszText));
1336 /* if the item pointer is not set, then get the data and locate it */
1337 if (!item) {
1338 item = get_item_data(infoPtr, dis->itemID);
1339 if (item == (CBE_ITEMDATA *)CB_ERR) {
1340 ERR("invalid item for id %d\n", dis->itemID);
1341 return 0;
1345 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
1347 xbase = CBE_STARTOFFSET;
1348 if ((item->mask & CBEIF_INDENT) && (dis->itemState & ODS_COMBOEXLBOX)) {
1349 INT indent = item->iIndent;
1350 if (indent == I_INDENTCALLBACK) {
1351 NMCOMBOBOXEXW nmce;
1352 ZeroMemory(&nmce, sizeof(nmce));
1353 nmce.ceItem.mask = CBEIF_INDENT;
1354 nmce.ceItem.lParam = item->lParam;
1355 nmce.ceItem.iItem = dis->itemID;
1356 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1357 if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
1358 item->iIndent = nmce.ceItem.iIndent;
1359 indent = nmce.ceItem.iIndent;
1361 xbase += (indent * CBE_INDENT);
1364 drawimage = -2;
1365 drawstate = ILD_NORMAL;
1366 selected = infoPtr->selected == dis->itemID;
1368 if (item->mask & CBEIF_IMAGE)
1369 drawimage = item->iImage;
1370 if (item->mask & CBEIF_SELECTEDIMAGE && selected)
1371 drawimage = item->iSelectedImage;
1372 if (dis->itemState & ODS_COMBOEXLBOX) {
1373 /* drawing listbox entry */
1374 if (dis->itemState & ODS_SELECTED)
1375 drawstate = ILD_SELECTED;
1376 } else {
1377 /* drawing combo/edit entry */
1378 if (IsWindowVisible(infoPtr->hwndEdit)) {
1379 /* if we have an edit control, set the selection state from the edit focus state */
1380 if (infoPtr->flags & WCBE_EDITFOCUSED)
1381 drawstate = ILD_SELECTED;
1382 } else
1383 /* if we don't have an edit control, use
1384 * the requested state.
1386 if (dis->itemState & ODS_SELECTED)
1387 drawstate = ILD_SELECTED;
1390 if (infoPtr->himl && !(infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGEINDENT)) {
1391 IMAGEINFO iinfo;
1392 iinfo.rcImage.left = iinfo.rcImage.right = 0;
1393 ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
1394 xioff = iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP;
1395 } else xioff = 0;
1397 /* setup pointer to text to be drawn */
1398 str = COMBOEX_GetText(infoPtr, item);
1399 if (!str) str = L"";
1401 len = lstrlenW (str);
1402 GetTextExtentPoint32W (dis->hDC, str, len, &txtsize);
1404 if (dis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)) {
1405 int overlay = item->iOverlay;
1407 if (drawimage == I_IMAGECALLBACK) {
1408 NMCOMBOBOXEXW nmce;
1409 ZeroMemory(&nmce, sizeof(nmce));
1410 nmce.ceItem.mask = selected ? CBEIF_SELECTEDIMAGE : CBEIF_IMAGE;
1411 nmce.ceItem.lParam = item->lParam;
1412 nmce.ceItem.iItem = dis->itemID;
1413 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1414 if (!selected) {
1415 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) item->iImage = nmce.ceItem.iImage;
1416 drawimage = nmce.ceItem.iImage;
1417 } else {
1418 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) item->iSelectedImage = nmce.ceItem.iSelectedImage;
1419 drawimage = nmce.ceItem.iSelectedImage;
1423 if (overlay == I_IMAGECALLBACK) {
1424 NMCOMBOBOXEXW nmce;
1425 ZeroMemory(&nmce, sizeof(nmce));
1426 nmce.ceItem.mask = CBEIF_OVERLAY;
1427 nmce.ceItem.lParam = item->lParam;
1428 nmce.ceItem.iItem = dis->itemID;
1429 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1430 if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
1431 item->iOverlay = nmce.ceItem.iOverlay;
1432 overlay = nmce.ceItem.iOverlay;
1435 if (drawimage >= 0 &&
1436 !(infoPtr->dwExtStyle & (CBES_EX_NOEDITIMAGE | CBES_EX_NOEDITIMAGEINDENT))) {
1437 if (overlay > 0) ImageList_SetOverlayImage (infoPtr->himl, overlay, 1);
1438 ImageList_Draw (infoPtr->himl, drawimage, dis->hDC, xbase, dis->rcItem.top,
1439 drawstate | (overlay > 0 ? INDEXTOOVERLAYMASK(1) : 0));
1442 /* now draw the text */
1443 if (!IsWindowVisible (infoPtr->hwndEdit)) {
1444 nbkc = (dis->itemState & ODS_SELECTED) ?
1445 comctl32_color.clrHighlight : comctl32_color.clrWindow;
1446 bkc = SetBkColor (dis->hDC, nbkc);
1447 ntxc = (dis->itemState & ODS_SELECTED) ?
1448 comctl32_color.clrHighlightText : comctl32_color.clrWindowText;
1449 txc = SetTextColor (dis->hDC, ntxc);
1450 x = xbase + xioff;
1451 y = dis->rcItem.top +
1452 (dis->rcItem.bottom - dis->rcItem.top - txtsize.cy) / 2;
1453 SetRect(&rect, x, dis->rcItem.top + 1, x + txtsize.cx, dis->rcItem.bottom - 1);
1454 TRACE("drawing item %d text, rect=(%s)\n",
1455 dis->itemID, wine_dbgstr_rect(&rect));
1456 ExtTextOutW (dis->hDC, x, y, ETO_OPAQUE | ETO_CLIPPED,
1457 &rect, str, len, 0);
1458 SetBkColor (dis->hDC, bkc);
1459 SetTextColor (dis->hDC, txc);
1463 if (dis->itemAction & ODA_FOCUS) {
1464 rect.left = xbase + xioff - 1;
1465 rect.right = rect.left + txtsize.cx + 2;
1466 rect.top = dis->rcItem.top;
1467 rect.bottom = dis->rcItem.bottom;
1468 DrawFocusRect(dis->hDC, &rect);
1471 return 0;
1475 static void COMBOEX_ResetContent (COMBOEX_INFO *infoPtr)
1477 if (infoPtr->items)
1479 CBE_ITEMDATA *item, *next;
1481 item = infoPtr->items;
1482 while (item) {
1483 next = item->next;
1484 COMBOEX_FreeText (item);
1485 Free (item);
1486 item = next;
1488 infoPtr->items = 0;
1491 infoPtr->selected = -1;
1492 infoPtr->nb_items = 0;
1496 static LRESULT COMBOEX_Destroy (COMBOEX_INFO *infoPtr)
1498 if (infoPtr->hwndCombo)
1499 SetWindowSubclass(infoPtr->hwndCombo, COMBOEX_ComboWndProc, COMBO_SUBCLASSID, 0);
1501 if (infoPtr->hwndEdit)
1502 SetWindowSubclass(infoPtr->hwndEdit, COMBOEX_EditWndProc, EDIT_SUBCLASSID, 0);
1504 COMBOEX_FreeText (&infoPtr->edit);
1505 COMBOEX_ResetContent (infoPtr);
1507 if (infoPtr->defaultFont)
1508 DeleteObject (infoPtr->defaultFont);
1510 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1512 /* free comboex info data */
1513 Free (infoPtr);
1515 return 0;
1519 static LRESULT COMBOEX_Enable (COMBOEX_INFO *infoPtr, BOOL enable)
1521 TRACE("hwnd=%p, enable=%s\n", infoPtr->hwndSelf, enable ? "TRUE":"FALSE");
1523 if (infoPtr->hwndEdit)
1524 EnableWindow(infoPtr->hwndEdit, enable);
1526 EnableWindow(infoPtr->hwndCombo, enable);
1528 /* Force the control to repaint when the enabled state changes. */
1529 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1531 return 1;
1535 static LRESULT COMBOEX_MeasureItem (COMBOEX_INFO const *infoPtr, MEASUREITEMSTRUCT *mis)
1537 SIZE mysize;
1538 HDC hdc;
1540 hdc = GetDC (0);
1541 GetTextExtentPointW (hdc, L"W", 1, &mysize);
1542 ReleaseDC (0, hdc);
1543 mis->itemHeight = mysize.cy + CBE_EXTRA;
1545 TRACE("adjusted height hwnd=%p, height=%d\n",
1546 infoPtr->hwndSelf, mis->itemHeight);
1548 return 0;
1552 static LRESULT COMBOEX_NCCreate (HWND hwnd)
1554 /* WARNING: The COMBOEX_INFO structure is not yet created */
1555 DWORD oldstyle, newstyle;
1557 oldstyle = (DWORD)GetWindowLongW (hwnd, GWL_STYLE);
1558 newstyle = oldstyle & ~(WS_VSCROLL | WS_HSCROLL | WS_BORDER);
1559 if (newstyle != oldstyle) {
1560 TRACE("req style %08x, resetting style %08x\n",
1561 oldstyle, newstyle);
1562 SetWindowLongW (hwnd, GWL_STYLE, newstyle);
1564 return 1;
1568 static LRESULT COMBOEX_NotifyFormat (COMBOEX_INFO *infoPtr, LPARAM lParam)
1570 if (lParam == NF_REQUERY) {
1571 INT i = SendMessageW(infoPtr->hwndNotify,
1572 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1573 infoPtr->NtfUnicode = (i == NFR_UNICODE);
1575 return infoPtr->NtfUnicode ? NFR_UNICODE : NFR_ANSI;
1579 static LRESULT COMBOEX_Size (COMBOEX_INFO *infoPtr, INT width, INT height)
1581 TRACE("(width=%d, height=%d)\n", width, height);
1583 MoveWindow (infoPtr->hwndCombo, 0, 0, width, height, TRUE);
1585 COMBOEX_AdjustEditPos (infoPtr);
1587 return 0;
1590 static LRESULT COMBOEX_SetFont( COMBOEX_INFO *infoPtr, HFONT font, BOOL redraw )
1592 infoPtr->font = font;
1593 SendMessageW( infoPtr->hwndCombo, WM_SETFONT, (WPARAM)font, 0 );
1594 if (infoPtr->hwndEdit) SendMessageW( infoPtr->hwndEdit, WM_SETFONT, (WPARAM)font, 0 );
1595 COMBOEX_ReSize( infoPtr );
1596 if (redraw) InvalidateRect( infoPtr->hwndCombo, NULL, TRUE );
1597 return 0;
1600 static LRESULT COMBOEX_SetRedraw(const COMBOEX_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1602 LRESULT ret = DefWindowProcW( infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam );
1603 if (wParam) RedrawWindow( infoPtr->hwndSelf, NULL, 0, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN );
1604 return ret;
1608 static LRESULT COMBOEX_WindowPosChanging (const COMBOEX_INFO *infoPtr, WINDOWPOS *wp)
1610 RECT cbx_wrect, cbx_crect, cb_wrect;
1611 INT width, height;
1613 GetWindowRect (infoPtr->hwndSelf, &cbx_wrect);
1614 GetClientRect (infoPtr->hwndSelf, &cbx_crect);
1615 GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
1617 /* width is winpos value + border width of comboex */
1618 width = wp->cx
1619 + (cbx_wrect.right-cbx_wrect.left)
1620 - (cbx_crect.right-cbx_crect.left);
1622 TRACE("winpos=(%d,%d %dx%d) flags=0x%08x\n",
1623 wp->x, wp->y, wp->cx, wp->cy, wp->flags);
1624 TRACE("EX window=(%s), client=(%s)\n",
1625 wine_dbgstr_rect(&cbx_wrect), wine_dbgstr_rect(&cbx_crect));
1626 TRACE("CB window=(%s), EX setting=(0,0)-(%d,%d)\n",
1627 wine_dbgstr_rect(&cbx_wrect), width, cb_wrect.bottom-cb_wrect.top);
1629 if (width) SetWindowPos (infoPtr->hwndCombo, HWND_TOP, 0, 0,
1630 width,
1631 cb_wrect.bottom-cb_wrect.top,
1632 SWP_NOACTIVATE);
1634 GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
1636 /* height is combo window height plus border width of comboex */
1637 height = (cb_wrect.bottom-cb_wrect.top)
1638 + (cbx_wrect.bottom-cbx_wrect.top)
1639 - (cbx_crect.bottom-cbx_crect.top);
1640 wp->cy = height;
1641 if (infoPtr->hwndEdit) {
1642 COMBOEX_AdjustEditPos (infoPtr);
1643 InvalidateRect (infoPtr->hwndCombo, 0, TRUE);
1646 return 0;
1649 static LRESULT CALLBACK
1650 COMBOEX_EditWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
1651 UINT_PTR uId, DWORD_PTR ref_data)
1653 COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr ((HWND)ref_data);
1654 NMCBEENDEDITW cbeend;
1655 WCHAR edit_text[260];
1656 COLORREF obkc;
1657 HDC hDC;
1658 RECT rect;
1659 LRESULT lret;
1661 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx, info_ptr=%p\n",
1662 hwnd, uMsg, wParam, lParam, infoPtr);
1664 if (uMsg == WM_NCDESTROY)
1665 RemoveWindowSubclass(hwnd, COMBOEX_EditWndProc, EDIT_SUBCLASSID);
1667 if (!infoPtr)
1668 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1670 switch (uMsg)
1673 case WM_CHAR:
1674 /* handle (ignore) the return character */
1675 if (wParam == VK_RETURN) return 0;
1676 /* all other characters pass into the real Edit */
1677 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1679 case WM_ERASEBKGND:
1680 hDC = (HDC) wParam;
1681 obkc = SetBkColor (hDC, comctl32_color.clrWindow);
1682 GetClientRect (hwnd, &rect);
1683 TRACE("erasing (%s)\n", wine_dbgstr_rect(&rect));
1684 ExtTextOutW (hDC, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
1685 SetBkColor (hDC, obkc);
1686 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1688 case WM_KEYDOWN: {
1689 INT_PTR oldItem, selected;
1690 CBE_ITEMDATA *item;
1692 switch ((INT)wParam)
1694 case VK_ESCAPE:
1695 TRACE("special code for VK_ESCAPE\n");
1697 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1699 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1700 cbeend.fChanged = FALSE;
1701 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1702 CB_GETCURSEL, 0, 0);
1703 cbeend.iWhy = CBENF_ESCAPE;
1705 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) return 0;
1706 oldItem = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1707 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1708 if (!(item = COMBOEX_FindItem(infoPtr, oldItem))) {
1709 ERR("item %ld not found. Problem!\n", oldItem);
1710 break;
1712 infoPtr->selected = oldItem;
1713 COMBOEX_SetEditText (infoPtr, item);
1714 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE |
1715 RDW_INVALIDATE);
1716 break;
1718 case VK_RETURN:
1719 TRACE("special code for VK_RETURN\n");
1721 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1723 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1724 selected = SendMessageW (infoPtr->hwndCombo,
1725 CB_GETCURSEL, 0, 0);
1727 if (selected != -1) {
1728 cmp_func_t cmptext = get_cmp_func(infoPtr);
1729 item = COMBOEX_FindItem (infoPtr, selected);
1730 TRACE("handling VK_RETURN, selected = %ld, selected_text=%s\n",
1731 selected, debugstr_txt(item->pszText));
1732 TRACE("handling VK_RETURN, edittext=%s\n",
1733 debugstr_w(edit_text));
1734 if (cmptext (COMBOEX_GetText(infoPtr, item), edit_text)) {
1735 /* strings not equal -- indicate edit has changed */
1736 selected = -1;
1740 cbeend.iNewSelection = selected;
1741 cbeend.fChanged = TRUE;
1742 cbeend.iWhy = CBENF_RETURN;
1743 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) {
1744 /* abort the change, restore previous */
1745 TRACE("Notify requested abort of change\n");
1746 COMBOEX_SetEditText (infoPtr, &infoPtr->edit);
1747 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE |
1748 RDW_INVALIDATE);
1749 return 0;
1751 oldItem = SendMessageW (infoPtr->hwndCombo,CB_GETCURSEL, 0, 0);
1752 if (oldItem != -1) {
1753 /* if something is selected, then deselect it */
1754 SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, -1, 0);
1756 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1757 SetFocus(infoPtr->hwndEdit);
1758 break;
1760 case VK_UP:
1761 case VK_DOWN:
1763 INT step = wParam == VK_DOWN ? 1 : -1;
1765 oldItem = SendMessageW (infoPtr->hwndSelf, CB_GETCURSEL, 0, 0);
1766 if (oldItem >= 0 && oldItem + step >= 0)
1767 SendMessageW (infoPtr->hwndSelf, CB_SETCURSEL, oldItem + step, 0);
1768 return 0;
1770 default:
1771 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1773 return 0;
1776 case WM_SETFOCUS:
1777 /* remember the focus to set state of icon */
1778 lret = DefSubclassProc(hwnd, uMsg, wParam, lParam);
1779 infoPtr->flags |= WCBE_EDITFOCUSED;
1780 return lret;
1782 case WM_KILLFOCUS:
1784 * do NOTIFY CBEN_ENDEDIT with CBENF_KILLFOCUS
1786 infoPtr->flags &= ~WCBE_EDITFOCUSED;
1787 if (infoPtr->flags & WCBE_ACTEDIT) {
1788 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1790 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1791 cbeend.fChanged = FALSE;
1792 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1793 CB_GETCURSEL, 0, 0);
1794 cbeend.iWhy = CBENF_KILLFOCUS;
1796 COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text);
1798 /* fall through */
1800 default:
1801 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1806 static LRESULT CALLBACK
1807 COMBOEX_ComboWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
1808 UINT_PTR uId, DWORD_PTR ref_data)
1810 COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr ((HWND)ref_data);
1811 NMCBEENDEDITW cbeend;
1812 NMMOUSE nmmse;
1813 COLORREF obkc;
1814 HDC hDC;
1815 HWND focusedhwnd;
1816 RECT rect;
1817 POINT pt;
1818 WCHAR edit_text[260];
1820 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx, info_ptr=%p\n",
1821 hwnd, uMsg, wParam, lParam, infoPtr);
1823 if (uMsg == WM_NCDESTROY)
1824 RemoveWindowSubclass(hwnd, COMBOEX_ComboWndProc, COMBO_SUBCLASSID);
1826 if (!infoPtr)
1827 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1829 switch (uMsg)
1831 case WM_DRAWITEM:
1833 * The only way this message should come is from the
1834 * child Listbox issuing the message. Flag this so
1835 * that ComboEx knows this is listbox.
1837 ((DRAWITEMSTRUCT *)lParam)->itemState |= ODS_COMBOEXLBOX;
1838 break;
1840 case WM_ERASEBKGND:
1841 hDC = (HDC) wParam;
1842 obkc = SetBkColor (hDC, comctl32_color.clrWindow);
1843 GetClientRect (hwnd, &rect);
1844 TRACE("erasing (%s)\n", wine_dbgstr_rect(&rect));
1845 ExtTextOutW (hDC, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
1846 SetBkColor (hDC, obkc);
1847 break;
1849 case WM_SETCURSOR:
1851 * WM_NOTIFY to comboex parent (rebar)
1852 * with NM_SETCURSOR with extra words of 0,0,0,0,0x02010001
1853 * CallWindowProc (previous)
1855 nmmse.dwItemSpec = 0;
1856 nmmse.dwItemData = 0;
1857 nmmse.pt.x = 0;
1858 nmmse.pt.y = 0;
1859 nmmse.dwHitInfo = lParam;
1860 COMBOEX_Notify (infoPtr, NM_SETCURSOR, (NMHDR *)&nmmse);
1861 break;
1863 case WM_LBUTTONDOWN:
1864 GetClientRect (hwnd, &rect);
1865 rect.bottom = rect.top + SendMessageW(infoPtr->hwndSelf,
1866 CB_GETITEMHEIGHT, -1, 0);
1867 rect.left = rect.right - GetSystemMetrics(SM_CXVSCROLL);
1868 pt.x = (short)LOWORD(lParam);
1869 pt.y = (short)HIWORD(lParam);
1870 if (PtInRect(&rect, pt))
1871 break;
1873 infoPtr->flags |= WCBE_MOUSECAPTURED;
1874 SetCapture(hwnd);
1875 return 0;
1877 case WM_LBUTTONUP:
1878 if (!(infoPtr->flags & WCBE_MOUSECAPTURED))
1879 break;
1881 ReleaseCapture();
1882 infoPtr->flags &= ~WCBE_MOUSECAPTURED;
1883 if (infoPtr->flags & WCBE_MOUSEDRAGGED) {
1884 infoPtr->flags &= ~WCBE_MOUSEDRAGGED;
1885 } else {
1886 SendMessageW(hwnd, CB_SHOWDROPDOWN, TRUE, 0);
1888 return 0;
1890 case WM_MOUSEMOVE:
1891 if ( (infoPtr->flags & WCBE_MOUSECAPTURED) &&
1892 !(infoPtr->flags & WCBE_MOUSEDRAGGED)) {
1893 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1894 COMBOEX_NotifyDragBegin(infoPtr, edit_text);
1895 infoPtr->flags |= WCBE_MOUSEDRAGGED;
1897 break;
1899 case WM_COMMAND:
1900 switch (HIWORD(wParam)) {
1902 case EN_UPDATE:
1903 /* traces show that COMBOEX does not issue CBN_EDITUPDATE
1904 * on the EN_UPDATE
1906 return 0;
1908 case EN_KILLFOCUS:
1909 focusedhwnd = GetFocus();
1910 if (infoPtr->flags & WCBE_ACTEDIT) {
1911 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1912 cbeend.fChanged = (infoPtr->flags & WCBE_EDITCHG);
1913 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1914 CB_GETCURSEL, 0, 0);
1915 cbeend.iWhy = CBENF_KILLFOCUS;
1917 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1918 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) return 0;
1920 /* possible CB_GETCURSEL */
1921 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1922 if (focusedhwnd)
1923 SendMessageW (infoPtr->hwndCombo, WM_KILLFOCUS,
1924 (WPARAM)focusedhwnd, 0);
1925 return 0;
1927 case EN_SETFOCUS: {
1928 NMHDR hdr;
1930 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, 0);
1931 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, -1);
1932 COMBOEX_Notify (infoPtr, CBEN_BEGINEDIT, &hdr);
1933 infoPtr->flags |= WCBE_ACTEDIT;
1934 infoPtr->flags &= ~WCBE_EDITCHG; /* no change yet */
1935 return 0;
1938 case EN_CHANGE: {
1939 LPCWSTR lastwrk;
1940 cmp_func_t cmptext = get_cmp_func(infoPtr);
1942 INT_PTR selected = SendMessageW (infoPtr->hwndCombo,
1943 CB_GETCURSEL, 0, 0);
1945 /* lstrlenW( lastworkingURL ) */
1947 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1948 if (selected == -1) {
1949 lastwrk = infoPtr->edit.pszText;
1951 else {
1952 CBE_ITEMDATA *item = COMBOEX_FindItem (infoPtr, selected);
1953 lastwrk = COMBOEX_GetText(infoPtr, item);
1956 TRACE("handling EN_CHANGE, selected = %ld, selected_text=%s\n",
1957 selected, debugstr_w(lastwrk));
1958 TRACE("handling EN_CHANGE, edittext=%s\n",
1959 debugstr_w(edit_text));
1961 /* cmptext is between lastworkingURL and GetWindowText */
1962 if (cmptext (lastwrk, edit_text)) {
1963 /* strings not equal -- indicate edit has changed */
1964 infoPtr->flags |= WCBE_EDITCHG;
1966 SendMessageW ( infoPtr->hwndNotify, WM_COMMAND,
1967 MAKEWPARAM(GetDlgCtrlID (infoPtr->hwndSelf),
1968 CBN_EDITCHANGE),
1969 (LPARAM)infoPtr->hwndSelf);
1970 return 0;
1973 case LBN_SELCHANGE:
1974 default:
1975 break;
1976 }/* fall through */
1977 default:
1981 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1985 static LRESULT WINAPI
1986 COMBOEX_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1988 COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
1990 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
1992 if (!infoPtr) {
1993 if (uMsg == WM_CREATE)
1994 return COMBOEX_Create (hwnd, (LPCREATESTRUCTA)lParam);
1995 if (uMsg == WM_NCCREATE)
1996 COMBOEX_NCCreate (hwnd);
1997 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2000 switch (uMsg)
2002 case CBEM_DELETEITEM:
2003 return COMBOEX_DeleteItem (infoPtr, wParam);
2005 case CBEM_GETCOMBOCONTROL:
2006 return (LRESULT)infoPtr->hwndCombo;
2008 case CBEM_GETEDITCONTROL:
2009 return (LRESULT)infoPtr->hwndEdit;
2011 case CBEM_GETEXTENDEDSTYLE:
2012 return infoPtr->dwExtStyle;
2014 case CBEM_GETIMAGELIST:
2015 return (LRESULT)infoPtr->himl;
2017 case CBEM_GETITEMA:
2018 return (LRESULT)COMBOEX_GetItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2020 case CBEM_GETITEMW:
2021 return (LRESULT)COMBOEX_GetItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2023 case CBEM_GETUNICODEFORMAT:
2024 return infoPtr->unicode;
2026 case CBEM_HASEDITCHANGED:
2027 return COMBOEX_HasEditChanged (infoPtr);
2029 case CBEM_INSERTITEMA:
2030 return COMBOEX_InsertItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2032 case CBEM_INSERTITEMW:
2033 return COMBOEX_InsertItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2035 case CBEM_SETEXSTYLE:
2036 case CBEM_SETEXTENDEDSTYLE:
2037 return COMBOEX_SetExtendedStyle (infoPtr, (DWORD)wParam, (DWORD)lParam);
2039 case CBEM_SETIMAGELIST:
2040 return (LRESULT)COMBOEX_SetImageList (infoPtr, (HIMAGELIST)lParam);
2042 case CBEM_SETITEMA:
2043 return COMBOEX_SetItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2045 case CBEM_SETITEMW:
2046 return COMBOEX_SetItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2048 case CBEM_SETUNICODEFORMAT:
2049 return COMBOEX_SetUnicodeFormat (infoPtr, wParam);
2051 /*case CBEM_SETWINDOWTHEME:
2052 FIXME("CBEM_SETWINDOWTHEME: stub\n");*/
2054 case WM_SETTEXT:
2055 case WM_GETTEXT:
2056 case WM_GETTEXTLENGTH:
2057 return SendMessageW(infoPtr->hwndEdit, uMsg, wParam, lParam);
2059 case CB_GETLBTEXT:
2060 return COMBOEX_GetListboxText(infoPtr, wParam, (LPWSTR)lParam);
2062 case CB_GETLBTEXTLEN:
2063 return COMBOEX_GetListboxText(infoPtr, wParam, NULL);
2065 case CB_RESETCONTENT:
2066 COMBOEX_ResetContent(infoPtr);
2067 /* fall through */
2069 /* Combo messages we are not sure if we need to process or just forward */
2070 case CB_GETDROPPEDCONTROLRECT:
2071 case CB_GETITEMHEIGHT:
2072 case CB_GETEXTENDEDUI:
2073 case CB_LIMITTEXT:
2074 case CB_SELECTSTRING:
2076 /* Combo messages OK to just forward to the regular COMBO */
2077 case CB_GETCOUNT:
2078 case CB_GETCURSEL:
2079 case CB_GETDROPPEDSTATE:
2080 case CB_SETDROPPEDWIDTH:
2081 case CB_SETEXTENDEDUI:
2082 case CB_SHOWDROPDOWN:
2083 return SendMessageW (infoPtr->hwndCombo, uMsg, wParam, lParam);
2085 /* Combo messages we need to process specially */
2086 case CB_FINDSTRINGEXACT:
2087 return COMBOEX_FindStringExact (infoPtr, (INT)wParam, (LPCWSTR)lParam);
2089 case CB_GETITEMDATA:
2090 return COMBOEX_GetItemData (infoPtr, (INT)wParam);
2092 case CB_SETCURSEL:
2093 return COMBOEX_SetCursel (infoPtr, (INT)wParam);
2095 case CB_SETITEMDATA:
2096 return COMBOEX_SetItemData (infoPtr, (INT)wParam, (DWORD_PTR)lParam);
2098 case CB_SETITEMHEIGHT:
2099 return COMBOEX_SetItemHeight (infoPtr, (INT)wParam, (UINT)lParam);
2103 /* Window messages passed to parent */
2104 case WM_COMMAND:
2105 return COMBOEX_Command (infoPtr, wParam);
2107 case WM_NOTIFY:
2108 if (infoPtr->NtfUnicode)
2109 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
2110 else
2111 return SendMessageA (infoPtr->hwndNotify, uMsg, wParam, lParam);
2114 /* Window messages we need to process */
2115 case WM_DELETEITEM:
2116 return COMBOEX_WM_DeleteItem (infoPtr, (DELETEITEMSTRUCT *)lParam);
2118 case WM_DRAWITEM:
2119 return COMBOEX_DrawItem (infoPtr, (DRAWITEMSTRUCT *)lParam);
2121 case WM_DESTROY:
2122 return COMBOEX_Destroy (infoPtr);
2124 case WM_ENABLE:
2125 return COMBOEX_Enable (infoPtr, (BOOL)wParam);
2127 case WM_MEASUREITEM:
2128 return COMBOEX_MeasureItem (infoPtr, (MEASUREITEMSTRUCT *)lParam);
2130 case WM_NOTIFYFORMAT:
2131 return COMBOEX_NotifyFormat (infoPtr, lParam);
2133 case WM_SIZE:
2134 return COMBOEX_Size (infoPtr, LOWORD(lParam), HIWORD(lParam));
2136 case WM_GETFONT:
2137 return (LRESULT)infoPtr->font;
2139 case WM_SETFONT:
2140 return COMBOEX_SetFont( infoPtr, (HFONT)wParam, LOWORD(lParam) != 0 );
2142 case WM_SETREDRAW:
2143 return COMBOEX_SetRedraw(infoPtr, wParam, lParam);
2145 case WM_WINDOWPOSCHANGING:
2146 return COMBOEX_WindowPosChanging (infoPtr, (WINDOWPOS *)lParam);
2148 case WM_SETFOCUS:
2149 if (infoPtr->hwndEdit) SetFocus( infoPtr->hwndEdit );
2150 else SetFocus( infoPtr->hwndCombo );
2151 return 0;
2153 case WM_SYSCOLORCHANGE:
2154 COMCTL32_RefreshSysColors();
2155 return 0;
2157 default:
2158 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2159 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",uMsg,wParam,lParam);
2160 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2165 void COMBOEX_Register (void)
2167 WNDCLASSW wndClass;
2169 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2170 wndClass.style = CS_GLOBALCLASS;
2171 wndClass.lpfnWndProc = COMBOEX_WindowProc;
2172 wndClass.cbClsExtra = 0;
2173 wndClass.cbWndExtra = sizeof(COMBOEX_INFO *);
2174 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2175 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2176 wndClass.lpszClassName = WC_COMBOBOXEXW;
2178 RegisterClassW (&wndClass);
2182 void COMBOEX_Unregister (void)
2184 UnregisterClassW (WC_COMBOBOXEXW, NULL);