wined3d: Use raw blits in wined3d_device_copy_sub_resource_region().
[wine.git] / dlls / comctl32 / comboex.c
blobf86a519cdcc566fc024dc6c347aa48a4bb2200a1
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
22 * NOTE
24 * This code was audited for completeness against the documented features
25 * of Comctl32.dll version 6.0 on Sep. 9, 2002, by Dimitrie O. Paun.
27 * Unless otherwise noted, we believe this code to be complete, as per
28 * the specification mentioned above.
29 * If you discover missing features, or bugs, please note them below.
33 #include <stdarg.h>
34 #include <string.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "commctrl.h"
41 #include "comctl32.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(comboex);
47 /* Item structure */
48 typedef struct _CBE_ITEMDATA
50 struct _CBE_ITEMDATA *next;
51 UINT mask;
52 LPWSTR pszText;
53 LPWSTR pszTemp;
54 int cchTextMax;
55 int iImage;
56 int iSelectedImage;
57 int iOverlay;
58 int iIndent;
59 LPARAM lParam;
60 } CBE_ITEMDATA;
62 /* ComboBoxEx structure */
63 typedef struct
65 HIMAGELIST himl;
66 HWND hwndSelf; /* my own hwnd */
67 HWND hwndNotify; /* my parent hwnd */
68 HWND hwndCombo;
69 HWND hwndEdit;
70 DWORD dwExtStyle;
71 INT selected; /* index of selected item */
72 DWORD flags; /* WINE internal flags */
73 HFONT defaultFont;
74 HFONT font;
75 INT nb_items; /* Number of items */
76 BOOL unicode; /* TRUE if this window is Unicode */
77 BOOL NtfUnicode; /* TRUE if parent wants notify in Unicode */
78 CBE_ITEMDATA edit; /* item data for edit item */
79 CBE_ITEMDATA *items; /* Array of items */
80 } COMBOEX_INFO;
82 /* internal flags in the COMBOEX_INFO structure */
83 #define WCBE_ACTEDIT 0x00000001 /* Edit active i.e.
84 * CBEN_BEGINEDIT issued
85 * but CBEN_ENDEDIT{A|W}
86 * not yet issued. */
87 #define WCBE_EDITCHG 0x00000002 /* Edit issued EN_CHANGE */
88 #define WCBE_EDITHASCHANGED (WCBE_ACTEDIT | WCBE_EDITCHG)
89 #define WCBE_EDITFOCUSED 0x00000004 /* Edit control has focus */
90 #define WCBE_MOUSECAPTURED 0x00000008 /* Combo has captured mouse */
91 #define WCBE_MOUSEDRAGGED 0x00000010 /* User has dragged in combo */
93 #define ID_CB_EDIT 1001
97 * Special flag set in DRAWITEMSTRUCT itemState field. It is set by
98 * the ComboEx version of the Combo Window Proc so that when the
99 * WM_DRAWITEM message is then passed to ComboEx, we know that this
100 * particular WM_DRAWITEM message is for listbox only items. Any message
101 * without this flag is then for the Edit control field.
103 * We really cannot use the ODS_COMBOBOXEDIT flag because MSDN states that
104 * only version 4.0 applications will have ODS_COMBOBOXEDIT set.
106 #define ODS_COMBOEXLBOX 0x4000
110 /* Height in pixels of control over the amount of the selected font */
111 #define CBE_EXTRA 3
113 /* Indent amount per MS documentation */
114 #define CBE_INDENT 10
116 /* Offset in pixels from left side for start of image or text */
117 #define CBE_STARTOFFSET 6
119 /* Offset between image and text */
120 #define CBE_SEP 4
122 #define COMBO_SUBCLASSID 1
123 #define EDIT_SUBCLASSID 2
125 #define COMBOEX_GetInfoPtr(hwnd) ((COMBOEX_INFO *)GetWindowLongPtrW (hwnd, 0))
127 static LRESULT CALLBACK COMBOEX_EditWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
128 UINT_PTR uId, DWORD_PTR ref_data);
129 static LRESULT CALLBACK COMBOEX_ComboWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
130 UINT_PTR uId, DWORD_PTR ref_data);
131 static LRESULT COMBOEX_Destroy (COMBOEX_INFO *infoPtr);
132 typedef INT (WINAPI *cmp_func_t)(LPCWSTR, LPCWSTR);
134 static inline BOOL is_textW(LPCWSTR str)
136 return str && str != LPSTR_TEXTCALLBACKW;
139 static inline BOOL is_textA(LPCSTR str)
141 return str && str != LPSTR_TEXTCALLBACKA;
144 static inline LPCSTR debugstr_txt(LPCWSTR str)
146 if (str == LPSTR_TEXTCALLBACKW) return "(callback)";
147 return debugstr_w(str);
150 static void COMBOEX_DumpItem (CBE_ITEMDATA const *item)
152 TRACE("item %p - mask=%08x, pszText=%p, cchTM=%d, iImage=%d\n",
153 item, item->mask, item->pszText, item->cchTextMax, item->iImage);
154 TRACE("item %p - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
155 item, item->iSelectedImage, item->iOverlay, item->iIndent, item->lParam);
156 if (item->mask & CBEIF_TEXT)
157 TRACE("item %p - pszText=%s\n", item, debugstr_txt(item->pszText));
161 static void COMBOEX_DumpInput (COMBOBOXEXITEMW const *input)
163 TRACE("input - mask=%08x, iItem=%ld, pszText=%p, cchTM=%d, iImage=%d\n",
164 input->mask, input->iItem, input->pszText, input->cchTextMax,
165 input->iImage);
166 if (input->mask & CBEIF_TEXT)
167 TRACE("input - pszText=<%s>\n", debugstr_txt(input->pszText));
168 TRACE("input - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
169 input->iSelectedImage, input->iOverlay, input->iIndent, input->lParam);
173 static inline CBE_ITEMDATA *get_item_data(const COMBOEX_INFO *infoPtr, INT index)
175 return (CBE_ITEMDATA *)SendMessageW (infoPtr->hwndCombo, CB_GETITEMDATA,
176 index, 0);
179 static inline cmp_func_t get_cmp_func(COMBOEX_INFO const *infoPtr)
181 return infoPtr->dwExtStyle & CBES_EX_CASESENSITIVE ? lstrcmpW : lstrcmpiW;
184 static INT COMBOEX_Notify (const COMBOEX_INFO *infoPtr, INT code, NMHDR *hdr)
186 hdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
187 hdr->hwndFrom = infoPtr->hwndSelf;
188 hdr->code = code;
189 if (infoPtr->NtfUnicode)
190 return SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, 0, (LPARAM)hdr);
191 else
192 return SendMessageA (infoPtr->hwndNotify, WM_NOTIFY, 0, (LPARAM)hdr);
196 static INT
197 COMBOEX_NotifyItem (const COMBOEX_INFO *infoPtr, UINT code, NMCOMBOBOXEXW *hdr)
199 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
200 if (infoPtr->NtfUnicode)
201 return COMBOEX_Notify (infoPtr, code, &hdr->hdr);
202 else {
203 LPWSTR wstr = hdr->ceItem.pszText;
204 LPSTR astr = 0;
205 INT ret, len = 0;
207 if ((hdr->ceItem.mask & CBEIF_TEXT) && is_textW(wstr)) {
208 len = WideCharToMultiByte (CP_ACP, 0, wstr, -1, 0, 0, NULL, NULL);
209 if (len > 0) {
210 astr = Alloc ((len + 1)*sizeof(CHAR));
211 if (!astr) return 0;
212 WideCharToMultiByte (CP_ACP, 0, wstr, -1, astr, len, 0, 0);
213 hdr->ceItem.pszText = (LPWSTR)astr;
217 if (code == CBEN_ENDEDITW) code = CBEN_ENDEDITA;
218 else if (code == CBEN_GETDISPINFOW) code = CBEN_GETDISPINFOA;
219 else if (code == CBEN_DRAGBEGINW) code = CBEN_DRAGBEGINA;
221 ret = COMBOEX_Notify (infoPtr, code, (NMHDR *)hdr);
223 if (astr && hdr->ceItem.pszText == (LPWSTR)astr)
224 hdr->ceItem.pszText = wstr;
226 Free(astr);
228 return ret;
233 static INT COMBOEX_NotifyEndEdit (const COMBOEX_INFO *infoPtr, NMCBEENDEDITW *neew, LPCWSTR wstr)
235 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
236 if (infoPtr->NtfUnicode) {
237 lstrcpynW(neew->szText, wstr, CBEMAXSTRLEN);
238 return COMBOEX_Notify (infoPtr, CBEN_ENDEDITW, &neew->hdr);
239 } else {
240 NMCBEENDEDITA neea;
242 neea.hdr = neew->hdr;
243 neea.fChanged = neew->fChanged;
244 neea.iNewSelection = neew->iNewSelection;
245 WideCharToMultiByte (CP_ACP, 0, wstr, -1, neea.szText, CBEMAXSTRLEN, 0, 0);
246 neea.iWhy = neew->iWhy;
248 return COMBOEX_Notify (infoPtr, CBEN_ENDEDITA, &neea.hdr);
253 static void COMBOEX_NotifyDragBegin(const COMBOEX_INFO *infoPtr, LPCWSTR wstr)
255 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
256 if (infoPtr->NtfUnicode) {
257 NMCBEDRAGBEGINW ndbw;
259 ndbw.iItemid = -1;
260 lstrcpynW(ndbw.szText, wstr, CBEMAXSTRLEN);
261 COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINW, &ndbw.hdr);
262 } else {
263 NMCBEDRAGBEGINA ndba;
265 ndba.iItemid = -1;
266 WideCharToMultiByte (CP_ACP, 0, wstr, -1, ndba.szText, CBEMAXSTRLEN, 0, 0);
268 COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINA, &ndba.hdr);
273 static void COMBOEX_FreeText (CBE_ITEMDATA *item)
275 if (is_textW(item->pszText)) Free(item->pszText);
276 item->pszText = NULL;
277 Free(item->pszTemp);
278 item->pszTemp = NULL;
282 static INT COMBOEX_GetIndex(COMBOEX_INFO const *infoPtr, CBE_ITEMDATA const *item)
284 CBE_ITEMDATA const *moving;
285 INT index;
287 moving = infoPtr->items;
288 index = infoPtr->nb_items - 1;
290 while (moving && (moving != item)) {
291 moving = moving->next;
292 index--;
294 if (!moving || (index < 0)) {
295 ERR("COMBOBOXEX item structures broken. Please report!\n");
296 return -1;
298 return index;
302 static LPCWSTR COMBOEX_GetText(const COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
304 NMCOMBOBOXEXW nmce;
305 LPWSTR text, buf;
306 INT len;
308 if (item->pszText != LPSTR_TEXTCALLBACKW)
309 return item->pszText;
311 ZeroMemory(&nmce, sizeof(nmce));
312 nmce.ceItem.mask = CBEIF_TEXT;
313 nmce.ceItem.lParam = item->lParam;
314 nmce.ceItem.iItem = COMBOEX_GetIndex(infoPtr, item);
315 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
317 if (is_textW(nmce.ceItem.pszText)) {
318 len = MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, NULL, 0);
319 buf = Alloc ((len + 1)*sizeof(WCHAR));
320 if (buf)
321 MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, buf, len);
322 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) {
323 COMBOEX_FreeText(item);
324 item->pszText = buf;
325 } else {
326 Free(item->pszTemp);
327 item->pszTemp = buf;
329 text = buf;
330 } else
331 text = nmce.ceItem.pszText;
333 if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
334 item->pszText = text;
335 return text;
339 static void COMBOEX_GetComboFontSize (const COMBOEX_INFO *infoPtr, SIZE *size)
341 static const WCHAR strA[] = { 'A', 0 };
342 HFONT nfont, ofont;
343 HDC mydc;
345 mydc = GetDC (0); /* why the entire screen???? */
346 nfont = (HFONT)SendMessageW (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
347 ofont = SelectObject (mydc, nfont);
348 GetTextExtentPointW (mydc, strA, 1, size);
349 SelectObject (mydc, ofont);
350 ReleaseDC (0, mydc);
351 TRACE("selected font hwnd=%p, height=%d\n", nfont, size->cy);
355 static void COMBOEX_CopyItem (const CBE_ITEMDATA *item, COMBOBOXEXITEMW *cit)
357 if (cit->mask & CBEIF_TEXT) {
359 * when given a text buffer actually use that buffer
361 if (cit->pszText) {
362 if (is_textW(item->pszText))
363 lstrcpynW(cit->pszText, item->pszText, cit->cchTextMax);
364 else
365 cit->pszText[0] = 0;
366 } else {
367 cit->pszText = item->pszText;
368 cit->cchTextMax = item->cchTextMax;
371 if (cit->mask & CBEIF_IMAGE)
372 cit->iImage = item->iImage;
373 if (cit->mask & CBEIF_SELECTEDIMAGE)
374 cit->iSelectedImage = item->iSelectedImage;
375 if (cit->mask & CBEIF_OVERLAY)
376 cit->iOverlay = item->iOverlay;
377 if (cit->mask & CBEIF_INDENT)
378 cit->iIndent = item->iIndent;
379 if (cit->mask & CBEIF_LPARAM)
380 cit->lParam = item->lParam;
384 static void COMBOEX_AdjustEditPos (const COMBOEX_INFO *infoPtr)
386 SIZE mysize;
387 INT x, y, w, h, xioff;
388 RECT rect;
390 if (!infoPtr->hwndEdit) return;
392 if (infoPtr->himl && !(infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGEINDENT)) {
393 IMAGEINFO iinfo;
394 iinfo.rcImage.left = iinfo.rcImage.right = 0;
395 ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
396 xioff = iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP;
397 } else xioff = 0;
399 GetClientRect (infoPtr->hwndCombo, &rect);
400 InflateRect (&rect, -2, -2);
401 InvalidateRect (infoPtr->hwndCombo, &rect, TRUE);
403 /* reposition the Edit control based on whether icon exists */
404 COMBOEX_GetComboFontSize (infoPtr, &mysize);
405 TRACE("Combo font x=%d, y=%d\n", mysize.cx, mysize.cy);
406 x = xioff + CBE_STARTOFFSET + 1;
407 w = rect.right-rect.left - x - GetSystemMetrics(SM_CXVSCROLL) - 1;
408 h = mysize.cy + 1;
409 y = rect.bottom - h - 1;
411 TRACE("Combo client (%s), setting Edit to (%d,%d)-(%d,%d)\n",
412 wine_dbgstr_rect(&rect), x, y, x + w, y + h);
413 SetWindowPos(infoPtr->hwndEdit, HWND_TOP, x, y, w, h,
414 SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER);
418 static void COMBOEX_ReSize (const COMBOEX_INFO *infoPtr)
420 SIZE mysize;
421 LONG cy;
422 IMAGEINFO iinfo;
424 COMBOEX_GetComboFontSize (infoPtr, &mysize);
425 cy = mysize.cy + CBE_EXTRA;
426 if (infoPtr->himl && ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo)) {
427 cy = max (iinfo.rcImage.bottom - iinfo.rcImage.top, cy);
428 TRACE("upgraded height due to image: height=%d\n", cy);
430 SendMessageW (infoPtr->hwndSelf, CB_SETITEMHEIGHT, -1, cy);
431 if (infoPtr->hwndCombo) {
432 SendMessageW (infoPtr->hwndCombo, CB_SETITEMHEIGHT, 0, cy);
433 if ( !(infoPtr->flags & CBES_EX_NOSIZELIMIT)) {
434 RECT comboRect, ourRect;
435 GetWindowRect(infoPtr->hwndCombo, &comboRect);
436 GetWindowRect(infoPtr->hwndSelf, &ourRect);
437 if (comboRect.bottom > ourRect.bottom)
438 SetWindowPos( infoPtr->hwndSelf, 0, 0, 0, ourRect.right - ourRect.left,
439 comboRect.bottom - comboRect.top,
440 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
446 static void COMBOEX_SetEditText (const COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
448 if (!infoPtr->hwndEdit) return;
450 if (item->mask & CBEIF_TEXT) {
451 SendMessageW (infoPtr->hwndEdit, WM_SETTEXT, 0, (LPARAM)COMBOEX_GetText(infoPtr, item));
452 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, 0);
453 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, -1);
458 static CBE_ITEMDATA *COMBOEX_FindItem(COMBOEX_INFO *infoPtr, INT_PTR index)
460 CBE_ITEMDATA *item;
461 INT i;
463 if ((index >= infoPtr->nb_items) || (index < -1))
464 return NULL;
465 if (index == -1)
466 return &infoPtr->edit;
467 item = infoPtr->items;
468 i = infoPtr->nb_items - 1;
470 /* find the item in the list */
471 while (item && (i > index)) {
472 item = item->next;
473 i--;
475 if (!item || (i != index)) {
476 ERR("COMBOBOXEX item structures broken. Please report!\n");
477 return 0;
479 return item;
482 /* *** CBEM_xxx message support *** */
484 static UINT COMBOEX_GetListboxText(COMBOEX_INFO *infoPtr, INT_PTR n, LPWSTR buf)
486 CBE_ITEMDATA *item;
487 LPCWSTR str;
489 item = COMBOEX_FindItem(infoPtr, n);
490 if (!item)
491 return 0;
493 str = COMBOEX_GetText(infoPtr, item);
494 if (!str)
496 if (buf)
498 if (infoPtr->unicode)
499 buf[0] = 0;
500 else
501 *((LPSTR)buf) = 0;
503 return 0;
506 if (infoPtr->unicode)
508 if (buf)
509 lstrcpyW(buf, str);
510 return lstrlenW(str);
512 else
514 UINT r;
515 r = WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)buf, 0x40000000, NULL, NULL);
516 if (r) r--;
517 return r;
522 static INT COMBOEX_DeleteItem (COMBOEX_INFO *infoPtr, INT_PTR index)
524 TRACE("(index=%ld)\n", index);
526 /* if item number requested does not exist then return failure */
527 if ((index >= infoPtr->nb_items) || (index < 0)) return CB_ERR;
528 if (!COMBOEX_FindItem(infoPtr, index)) return CB_ERR;
530 /* doing this will result in WM_DELETEITEM being issued */
531 SendMessageW (infoPtr->hwndCombo, CB_DELETESTRING, index, 0);
533 return infoPtr->nb_items;
537 static BOOL COMBOEX_GetItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW *cit)
539 INT_PTR index = cit->iItem;
540 CBE_ITEMDATA *item;
542 TRACE("\n");
544 /* if item number requested does not exist then return failure */
545 if ((index >= infoPtr->nb_items) || (index < -1)) return FALSE;
547 /* if the item is the edit control and there is no edit control, skip */
548 if ((index == -1) && !infoPtr->hwndEdit) return FALSE;
550 if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
552 COMBOEX_CopyItem (item, cit);
554 return TRUE;
558 static BOOL COMBOEX_GetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA *cit)
560 COMBOBOXEXITEMW tmpcit;
562 TRACE("\n");
564 tmpcit.mask = cit->mask;
565 tmpcit.iItem = cit->iItem;
566 tmpcit.pszText = 0;
567 if(!COMBOEX_GetItemW (infoPtr, &tmpcit)) return FALSE;
569 if (cit->mask & CBEIF_TEXT)
571 if (is_textW(tmpcit.pszText) && cit->pszText)
572 WideCharToMultiByte(CP_ACP, 0, tmpcit.pszText, -1,
573 cit->pszText, cit->cchTextMax, NULL, NULL);
574 else if (cit->pszText) cit->pszText[0] = 0;
575 else cit->pszText = (LPSTR)tmpcit.pszText;
578 if (cit->mask & CBEIF_IMAGE)
579 cit->iImage = tmpcit.iImage;
580 if (cit->mask & CBEIF_SELECTEDIMAGE)
581 cit->iSelectedImage = tmpcit.iSelectedImage;
582 if (cit->mask & CBEIF_OVERLAY)
583 cit->iOverlay = tmpcit.iOverlay;
584 if (cit->mask & CBEIF_INDENT)
585 cit->iIndent = tmpcit.iIndent;
586 if (cit->mask & CBEIF_LPARAM)
587 cit->lParam = tmpcit.lParam;
589 return TRUE;
593 static inline BOOL COMBOEX_HasEditChanged (COMBOEX_INFO const *infoPtr)
595 return infoPtr->hwndEdit && (infoPtr->flags & WCBE_EDITHASCHANGED) == WCBE_EDITHASCHANGED;
599 static INT COMBOEX_InsertItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW const *cit)
601 INT_PTR index;
602 CBE_ITEMDATA *item;
603 NMCOMBOBOXEXW nmcit;
605 TRACE("\n");
607 if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
609 /* get real index of item to insert */
610 index = cit->iItem;
611 if (index == -1) index = infoPtr->nb_items;
612 if (index > infoPtr->nb_items) return -1;
614 /* get zero-filled space and chain it in */
615 if(!(item = Alloc (sizeof(*item)))) return -1;
617 /* locate position to insert new item in */
618 if (index == infoPtr->nb_items) {
619 /* fast path for iItem = -1 */
620 item->next = infoPtr->items;
621 infoPtr->items = item;
623 else {
624 INT i = infoPtr->nb_items-1;
625 CBE_ITEMDATA *moving = infoPtr->items;
627 while ((i > index) && moving) {
628 moving = moving->next;
629 i--;
631 if (!moving) {
632 ERR("COMBOBOXEX item structures broken. Please report!\n");
633 Free(item);
634 return -1;
636 item->next = moving->next;
637 moving->next = item;
640 /* fill in our hidden item structure */
641 item->mask = cit->mask;
642 if (item->mask & CBEIF_TEXT) {
643 INT len = 0;
645 if (is_textW(cit->pszText)) len = strlenW (cit->pszText);
646 if (len > 0) {
647 item->pszText = Alloc ((len + 1)*sizeof(WCHAR));
648 if (!item->pszText) {
649 Free(item);
650 return -1;
652 strcpyW (item->pszText, cit->pszText);
654 else if (cit->pszText == LPSTR_TEXTCALLBACKW)
655 item->pszText = LPSTR_TEXTCALLBACKW;
656 item->cchTextMax = cit->cchTextMax;
658 if (item->mask & CBEIF_IMAGE)
659 item->iImage = cit->iImage;
660 if (item->mask & CBEIF_SELECTEDIMAGE)
661 item->iSelectedImage = cit->iSelectedImage;
662 if (item->mask & CBEIF_OVERLAY)
663 item->iOverlay = cit->iOverlay;
664 if (item->mask & CBEIF_INDENT)
665 item->iIndent = cit->iIndent;
666 if (item->mask & CBEIF_LPARAM)
667 item->lParam = cit->lParam;
668 infoPtr->nb_items++;
670 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
672 SendMessageW (infoPtr->hwndCombo, CB_INSERTSTRING, cit->iItem, (LPARAM)item);
674 memset (&nmcit.ceItem, 0, sizeof(nmcit.ceItem));
675 nmcit.ceItem.mask=~0;
676 COMBOEX_CopyItem (item, &nmcit.ceItem);
677 COMBOEX_NotifyItem (infoPtr, CBEN_INSERTITEM, &nmcit);
679 return index;
684 static INT COMBOEX_InsertItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA const *cit)
686 COMBOBOXEXITEMW citW;
687 LPWSTR wstr = NULL;
688 INT ret;
690 memcpy(&citW,cit,sizeof(COMBOBOXEXITEMA));
691 if (cit->mask & CBEIF_TEXT && is_textA(cit->pszText)) {
692 INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
693 wstr = Alloc ((len + 1)*sizeof(WCHAR));
694 if (!wstr) return -1;
695 MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
696 citW.pszText = wstr;
698 ret = COMBOEX_InsertItemW(infoPtr, &citW);
700 Free(wstr);
702 return ret;
706 static DWORD
707 COMBOEX_SetExtendedStyle (COMBOEX_INFO *infoPtr, DWORD mask, DWORD style)
709 DWORD dwTemp;
711 TRACE("(mask=x%08x, style=0x%08x)\n", mask, style);
713 dwTemp = infoPtr->dwExtStyle;
715 if (mask)
716 infoPtr->dwExtStyle = (infoPtr->dwExtStyle & ~mask) | style;
717 else
718 infoPtr->dwExtStyle = style;
720 /* see if we need to change the word break proc on the edit */
721 if ((infoPtr->dwExtStyle ^ dwTemp) & CBES_EX_PATHWORDBREAKPROC)
722 SetPathWordBreakProc(infoPtr->hwndEdit,
723 (infoPtr->dwExtStyle & CBES_EX_PATHWORDBREAKPROC) != 0);
725 /* test if the control's appearance has changed */
726 mask = CBES_EX_NOEDITIMAGE | CBES_EX_NOEDITIMAGEINDENT;
727 if ((infoPtr->dwExtStyle & mask) != (dwTemp & mask)) {
728 /* if state of EX_NOEDITIMAGE changes, invalidate all */
729 TRACE("EX_NOEDITIMAGE state changed to %d\n",
730 infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGE);
731 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
732 COMBOEX_AdjustEditPos (infoPtr);
733 if (infoPtr->hwndEdit)
734 InvalidateRect (infoPtr->hwndEdit, NULL, TRUE);
737 return dwTemp;
741 static HIMAGELIST COMBOEX_SetImageList (COMBOEX_INFO *infoPtr, HIMAGELIST himl)
743 HIMAGELIST himlTemp = infoPtr->himl;
745 TRACE("\n");
747 infoPtr->himl = himl;
749 COMBOEX_ReSize (infoPtr);
750 InvalidateRect (infoPtr->hwndCombo, NULL, TRUE);
752 /* reposition the Edit control based on whether icon exists */
753 COMBOEX_AdjustEditPos (infoPtr);
754 return himlTemp;
757 static BOOL COMBOEX_SetItemW (COMBOEX_INFO *infoPtr, const COMBOBOXEXITEMW *cit)
759 INT_PTR index = cit->iItem;
760 CBE_ITEMDATA *item;
762 if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
764 /* if item number requested does not exist then return failure */
765 if ((index >= infoPtr->nb_items) || (index < -1)) return FALSE;
767 /* if the item is the edit control and there is no edit control, skip */
768 if ((index == -1) && !infoPtr->hwndEdit) return FALSE;
770 if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
772 /* add/change stuff to the internal item structure */
773 item->mask |= cit->mask;
774 if (cit->mask & CBEIF_TEXT) {
775 INT len = 0;
777 COMBOEX_FreeText(item);
778 if (is_textW(cit->pszText)) len = strlenW(cit->pszText);
779 if (len > 0) {
780 item->pszText = Alloc ((len + 1)*sizeof(WCHAR));
781 if (!item->pszText) return FALSE;
782 strcpyW(item->pszText, cit->pszText);
783 } else if (cit->pszText == LPSTR_TEXTCALLBACKW)
784 item->pszText = LPSTR_TEXTCALLBACKW;
785 item->cchTextMax = cit->cchTextMax;
787 if (cit->mask & CBEIF_IMAGE)
788 item->iImage = cit->iImage;
789 if (cit->mask & CBEIF_SELECTEDIMAGE)
790 item->iSelectedImage = cit->iSelectedImage;
791 if (cit->mask & CBEIF_OVERLAY)
792 item->iOverlay = cit->iOverlay;
793 if (cit->mask & CBEIF_INDENT)
794 item->iIndent = cit->iIndent;
795 if (cit->mask & CBEIF_LPARAM)
796 item->lParam = cit->lParam;
798 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
800 /* if original request was to update edit control, do some fast foot work */
801 if (cit->iItem == -1 && cit->mask & CBEIF_TEXT) {
802 COMBOEX_SetEditText (infoPtr, item);
803 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE | RDW_INVALIDATE);
805 return TRUE;
808 static BOOL COMBOEX_SetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA const *cit)
810 COMBOBOXEXITEMW citW;
811 LPWSTR wstr = NULL;
812 BOOL ret;
814 memcpy(&citW, cit, sizeof(COMBOBOXEXITEMA));
815 if ((cit->mask & CBEIF_TEXT) && is_textA(cit->pszText)) {
816 INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
817 wstr = Alloc ((len + 1)*sizeof(WCHAR));
818 if (!wstr) return FALSE;
819 MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
820 citW.pszText = wstr;
822 ret = COMBOEX_SetItemW(infoPtr, &citW);
824 Free(wstr);
826 return ret;
830 static BOOL COMBOEX_SetUnicodeFormat (COMBOEX_INFO *infoPtr, BOOL value)
832 BOOL bTemp = infoPtr->unicode;
834 TRACE("to %s, was %s\n", value ? "TRUE":"FALSE", bTemp ? "TRUE":"FALSE");
836 infoPtr->unicode = value;
838 return bTemp;
842 /* *** CB_xxx message support *** */
844 static INT
845 COMBOEX_FindStringExact (const COMBOEX_INFO *infoPtr, INT start, LPCWSTR str)
847 INT i;
848 cmp_func_t cmptext = get_cmp_func(infoPtr);
849 INT count = SendMessageW (infoPtr->hwndCombo, CB_GETCOUNT, 0, 0);
851 /* now search from after starting loc and wrapping back to start */
852 for(i=start+1; i<count; i++) {
853 CBE_ITEMDATA *item = get_item_data(infoPtr, i);
854 if ((LRESULT)item == CB_ERR) continue;
855 if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
857 for(i=0; i<=start; i++) {
858 CBE_ITEMDATA *item = get_item_data(infoPtr, i);
859 if ((LRESULT)item == CB_ERR) continue;
860 if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
862 return CB_ERR;
866 static DWORD_PTR COMBOEX_GetItemData (COMBOEX_INFO *infoPtr, INT_PTR index)
868 CBE_ITEMDATA const *item1;
869 CBE_ITEMDATA const *item2;
870 DWORD_PTR ret = 0;
872 item1 = get_item_data(infoPtr, index);
873 if ((item1 != NULL) && ((LRESULT)item1 != CB_ERR)) {
874 item2 = COMBOEX_FindItem (infoPtr, index);
875 if (item2 != item1) {
876 ERR("data structures damaged!\n");
877 return CB_ERR;
879 if (item1->mask & CBEIF_LPARAM) ret = item1->lParam;
880 TRACE("returning 0x%08lx\n", ret);
881 } else {
882 ret = (DWORD_PTR)item1;
883 TRACE("non-valid result from combo, returning 0x%08lx\n", ret);
885 return ret;
889 static INT COMBOEX_SetCursel (COMBOEX_INFO *infoPtr, INT_PTR index)
891 CBE_ITEMDATA *item;
892 INT sel;
894 if (!(item = COMBOEX_FindItem(infoPtr, index)))
895 return SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, index, 0);
897 TRACE("selecting item %ld text=%s\n", index, debugstr_txt(item->pszText));
898 infoPtr->selected = index;
900 sel = (INT)SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, index, 0);
901 COMBOEX_SetEditText (infoPtr, item);
902 return sel;
906 static DWORD_PTR COMBOEX_SetItemData (COMBOEX_INFO *infoPtr, INT_PTR index, DWORD_PTR data)
908 CBE_ITEMDATA *item1;
909 CBE_ITEMDATA const *item2;
911 item1 = get_item_data(infoPtr, index);
912 if ((item1 != NULL) && ((LRESULT)item1 != CB_ERR)) {
913 item2 = COMBOEX_FindItem (infoPtr, index);
914 if (item2 != item1) {
915 ERR("data structures damaged!\n");
916 return CB_ERR;
918 item1->mask |= CBEIF_LPARAM;
919 item1->lParam = data;
920 TRACE("setting lparam to 0x%08lx\n", data);
921 return 0;
923 TRACE("non-valid result from combo %p\n", item1);
924 return (DWORD_PTR)item1;
928 static INT COMBOEX_SetItemHeight (COMBOEX_INFO const *infoPtr, INT index, UINT height)
930 RECT cb_wrect, cbx_wrect, cbx_crect;
932 /* First, lets forward the message to the normal combo control
933 just like Windows. */
934 if (infoPtr->hwndCombo)
935 if (SendMessageW (infoPtr->hwndCombo, CB_SETITEMHEIGHT,
936 index, height) == CB_ERR) return CB_ERR;
938 GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
939 GetWindowRect (infoPtr->hwndSelf, &cbx_wrect);
940 GetClientRect (infoPtr->hwndSelf, &cbx_crect);
941 /* the height of comboex as height of the combo + comboex border */
942 height = cb_wrect.bottom-cb_wrect.top
943 + cbx_wrect.bottom-cbx_wrect.top
944 - (cbx_crect.bottom-cbx_crect.top);
945 TRACE("EX window=(%s), client=(%s)\n",
946 wine_dbgstr_rect(&cbx_wrect), wine_dbgstr_rect(&cbx_crect));
947 TRACE("CB window=(%s), EX setting=(0,0)-(%d,%d)\n",
948 wine_dbgstr_rect(&cbx_wrect), cbx_wrect.right-cbx_wrect.left, height);
949 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0,
950 cbx_wrect.right-cbx_wrect.left, height,
951 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
953 return 0;
957 /* *** WM_xxx message support *** */
960 static LRESULT COMBOEX_Create (HWND hwnd, CREATESTRUCTA const *cs)
962 static const WCHAR NIL[] = { 0 };
963 COMBOEX_INFO *infoPtr;
964 LOGFONTW mylogfont;
965 RECT win_rect;
966 INT i;
968 /* allocate memory for info structure */
969 infoPtr = Alloc (sizeof(COMBOEX_INFO));
970 if (!infoPtr) return -1;
972 /* initialize info structure */
973 /* note that infoPtr is allocated zero-filled */
975 infoPtr->hwndSelf = hwnd;
976 infoPtr->selected = -1;
978 infoPtr->unicode = IsWindowUnicode (hwnd);
979 infoPtr->hwndNotify = cs->hwndParent;
981 i = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
982 if ((i != NFR_ANSI) && (i != NFR_UNICODE)) {
983 WARN("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n", i);
984 i = NFR_ANSI;
986 infoPtr->NtfUnicode = (i == NFR_UNICODE);
988 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
990 if (TRACE_ON(comboex)) {
991 RECT client, rect;
992 GetWindowRect(hwnd, &rect);
993 GetClientRect(hwnd, &client);
994 TRACE("EX window=(%s), client=(%s)\n",
995 wine_dbgstr_rect(&rect), wine_dbgstr_rect(&client));
998 /* Native version of ComboEx creates the ComboBox with DROPDOWNLIST */
999 /* specified. It then creates its own version of the EDIT control */
1000 /* and makes the ComboBox the parent. This is because a normal */
1001 /* DROPDOWNLIST does not have an EDIT control, but we need one. */
1002 /* We also need to place the edit control at the proper location */
1003 /* (allow space for the icons). */
1005 infoPtr->hwndCombo = CreateWindowW (WC_COMBOBOXW, NIL,
1006 WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VSCROLL |
1007 CBS_NOINTEGRALHEIGHT | CBS_DROPDOWNLIST |
1008 WS_CHILD | WS_VISIBLE | CBS_OWNERDRAWFIXED |
1009 GetWindowLongW (hwnd, GWL_STYLE),
1010 cs->y, cs->x, cs->cx, cs->cy, hwnd,
1011 (HMENU) GetWindowLongPtrW (hwnd, GWLP_ID),
1012 (HINSTANCE)GetWindowLongPtrW (hwnd, GWLP_HINSTANCE), NULL);
1014 SetWindowSubclass(infoPtr->hwndCombo, COMBOEX_ComboWndProc, COMBO_SUBCLASSID,
1015 (DWORD_PTR)hwnd);
1016 infoPtr->font = (HFONT)SendMessageW (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
1019 * Now create our own EDIT control so we can position it.
1020 * It is created only for CBS_DROPDOWN style
1022 if ((cs->style & CBS_DROPDOWNLIST) == CBS_DROPDOWN) {
1023 infoPtr->hwndEdit = CreateWindowExW (0, WC_EDITW, NIL,
1024 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | ES_AUTOHSCROLL,
1025 0, 0, 0, 0, /* will set later */
1026 infoPtr->hwndCombo,
1027 (HMENU) GetWindowLongPtrW (hwnd, GWLP_ID),
1028 (HINSTANCE)GetWindowLongPtrW (hwnd, GWLP_HINSTANCE), NULL);
1030 SetWindowSubclass(infoPtr->hwndEdit, COMBOEX_EditWndProc, EDIT_SUBCLASSID,
1031 (DWORD_PTR)hwnd);
1033 infoPtr->font = (HFONT)SendMessageW(infoPtr->hwndCombo, WM_GETFONT, 0, 0);
1037 * Locate the default font if necessary and then set it in
1038 * all associated controls
1040 if (!infoPtr->font) {
1041 SystemParametersInfoW (SPI_GETICONTITLELOGFONT, sizeof(mylogfont),
1042 &mylogfont, 0);
1043 infoPtr->font = infoPtr->defaultFont = CreateFontIndirectW (&mylogfont);
1045 SendMessageW (infoPtr->hwndCombo, WM_SETFONT, (WPARAM)infoPtr->font, 0);
1046 if (infoPtr->hwndEdit) {
1047 SendMessageW (infoPtr->hwndEdit, WM_SETFONT, (WPARAM)infoPtr->font, 0);
1048 SendMessageW (infoPtr->hwndEdit, EM_SETMARGINS, EC_USEFONTINFO, 0);
1051 COMBOEX_ReSize (infoPtr);
1053 /* Above is fairly certain, below is much less certain. */
1055 GetWindowRect(hwnd, &win_rect);
1057 if (TRACE_ON(comboex)) {
1058 RECT client, rect;
1059 GetClientRect(hwnd, &client);
1060 GetWindowRect(infoPtr->hwndCombo, &rect);
1061 TRACE("EX window=(%s) client=(%s) CB wnd=(%s)\n",
1062 wine_dbgstr_rect(&win_rect), wine_dbgstr_rect(&client),
1063 wine_dbgstr_rect(&rect));
1065 SetWindowPos(infoPtr->hwndCombo, HWND_TOP, 0, 0,
1066 win_rect.right - win_rect.left, win_rect.bottom - win_rect.top,
1067 SWP_NOACTIVATE | SWP_NOREDRAW);
1069 GetWindowRect(infoPtr->hwndCombo, &win_rect);
1070 TRACE("CB window=(%s)\n", wine_dbgstr_rect(&win_rect));
1071 SetWindowPos(hwnd, HWND_TOP, 0, 0,
1072 win_rect.right - win_rect.left, win_rect.bottom - win_rect.top,
1073 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
1075 COMBOEX_AdjustEditPos (infoPtr);
1077 return 0;
1081 static LRESULT COMBOEX_Command (COMBOEX_INFO *infoPtr, WPARAM wParam)
1083 LRESULT lret;
1084 INT command = HIWORD(wParam);
1085 CBE_ITEMDATA *item = 0;
1086 WCHAR wintext[520];
1087 INT cursel, n;
1088 INT_PTR oldItem;
1089 NMCBEENDEDITW cbeend;
1090 DWORD oldflags;
1091 HWND parent = infoPtr->hwndNotify;
1093 TRACE("for command %d\n", command);
1095 switch (command)
1097 case CBN_DROPDOWN:
1098 SetFocus (infoPtr->hwndCombo);
1099 ShowWindow (infoPtr->hwndEdit, SW_HIDE);
1100 infoPtr->flags |= WCBE_ACTEDIT;
1101 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1102 case CBN_CLOSEUP:
1103 SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1104 ShowWindow (infoPtr->hwndEdit, SW_SHOW);
1105 InvalidateRect (infoPtr->hwndCombo, 0, TRUE);
1106 if (infoPtr->hwndEdit) InvalidateRect (infoPtr->hwndEdit, 0, TRUE);
1107 cursel = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1108 if (cursel == -1) {
1109 cmp_func_t cmptext = get_cmp_func(infoPtr);
1110 /* find match from edit against those in Combobox */
1111 GetWindowTextW (infoPtr->hwndEdit, wintext, 520);
1112 n = SendMessageW (infoPtr->hwndCombo, CB_GETCOUNT, 0, 0);
1113 for (cursel = 0; cursel < n; cursel++){
1114 item = get_item_data(infoPtr, cursel);
1115 if ((INT_PTR)item == CB_ERR) break;
1116 if (!cmptext(COMBOEX_GetText(infoPtr, item), wintext)) break;
1118 if ((cursel == n) || ((INT_PTR)item == CB_ERR)) {
1119 TRACE("failed to find match??? item=%p cursel=%d\n",
1120 item, cursel);
1121 if (infoPtr->hwndEdit) SetFocus(infoPtr->hwndEdit);
1122 return 0;
1125 else {
1126 item = get_item_data(infoPtr, cursel);
1127 if ((INT_PTR)item == CB_ERR) {
1128 TRACE("failed to find match??? item=%p cursel=%d\n",
1129 item, cursel);
1130 if (infoPtr->hwndEdit) SetFocus(infoPtr->hwndEdit);
1131 return 0;
1135 /* Save flags for testing and reset them */
1136 oldflags = infoPtr->flags;
1137 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1139 if (oldflags & WCBE_ACTEDIT) {
1140 cbeend.fChanged = (oldflags & WCBE_EDITCHG);
1141 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1142 CB_GETCURSEL, 0, 0);
1143 cbeend.iWhy = CBENF_DROPDOWN;
1145 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, COMBOEX_GetText(infoPtr, item))) return 0;
1148 /* if selection has changed the set the new current selection */
1149 cursel = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1150 if ((oldflags & WCBE_EDITCHG) || (cursel != infoPtr->selected)) {
1151 infoPtr->selected = cursel;
1152 SendMessageW (infoPtr->hwndSelf, CB_SETCURSEL, cursel, 0);
1153 SetFocus(infoPtr->hwndCombo);
1155 return 0;
1157 case CBN_SELCHANGE:
1159 * CB_GETCURSEL(Combo)
1160 * CB_GETITEMDATA(Combo) < simulated by COMBOEX_FindItem
1161 * lstrlenA
1162 * WM_SETTEXT(Edit)
1163 * WM_GETTEXTLENGTH(Edit)
1164 * WM_GETTEXT(Edit)
1165 * EM_SETSEL(Edit, 0,0)
1166 * WM_GETTEXTLENGTH(Edit)
1167 * WM_GETTEXT(Edit)
1168 * EM_SETSEL(Edit, 0,len)
1169 * return WM_COMMAND to parent
1171 oldItem = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1172 if (!(item = COMBOEX_FindItem(infoPtr, oldItem))) {
1173 ERR("item %ld not found. Problem!\n", oldItem);
1174 break;
1176 infoPtr->selected = oldItem;
1177 COMBOEX_SetEditText (infoPtr, item);
1178 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1180 case CBN_SELENDOK:
1181 case CBN_SELENDCANCEL:
1183 * We have to change the handle since we are the control
1184 * issuing the message. IE4 depends on this.
1186 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1188 case CBN_KILLFOCUS:
1189 SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1190 if (infoPtr->flags & WCBE_ACTEDIT) {
1191 GetWindowTextW (infoPtr->hwndEdit, wintext, 260);
1192 cbeend.fChanged = (infoPtr->flags & WCBE_EDITCHG);
1193 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1194 CB_GETCURSEL, 0, 0);
1195 cbeend.iWhy = CBENF_KILLFOCUS;
1197 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1198 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, wintext)) return 0;
1200 /* possible CB_GETCURSEL */
1201 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1202 return 0;
1204 case CBN_SETFOCUS:
1205 return SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1207 default:
1209 * We have to change the handle since we are the control
1210 * issuing the message. IE4 depends on this.
1211 * We also need to set the focus back to the Edit control
1212 * after passing the command to the parent of the ComboEx.
1214 lret = SendMessageW (parent, WM_COMMAND, wParam, (LPARAM)infoPtr->hwndSelf);
1215 if (infoPtr->hwndEdit) SetFocus(infoPtr->hwndEdit);
1216 return lret;
1218 return 0;
1222 static BOOL COMBOEX_WM_DeleteItem (COMBOEX_INFO *infoPtr, DELETEITEMSTRUCT const *dis)
1224 CBE_ITEMDATA *item, *olditem;
1225 NMCOMBOBOXEXW nmcit;
1226 UINT i;
1228 TRACE("CtlType=%08x, CtlID=%08x, itemID=%08x, hwnd=%p, data=%08lx\n",
1229 dis->CtlType, dis->CtlID, dis->itemID, dis->hwndItem, dis->itemData);
1231 if (dis->itemID >= infoPtr->nb_items) return FALSE;
1233 olditem = infoPtr->items;
1234 i = infoPtr->nb_items - 1;
1236 if (i == dis->itemID) {
1237 infoPtr->items = infoPtr->items->next;
1239 else {
1240 item = olditem;
1241 i--;
1243 /* find the prior item in the list */
1244 while (item->next && (i > dis->itemID)) {
1245 item = item->next;
1246 i--;
1248 if (!item->next || (i != dis->itemID)) {
1249 ERR("COMBOBOXEX item structures broken. Please report!\n");
1250 return FALSE;
1252 olditem = item->next;
1253 item->next = item->next->next;
1255 infoPtr->nb_items--;
1257 memset (&nmcit.ceItem, 0, sizeof(nmcit.ceItem));
1258 nmcit.ceItem.mask=~0;
1259 COMBOEX_CopyItem (olditem, &nmcit.ceItem);
1260 COMBOEX_NotifyItem (infoPtr, CBEN_DELETEITEM, &nmcit);
1262 COMBOEX_FreeText(olditem);
1263 Free(olditem);
1265 return TRUE;
1269 static LRESULT COMBOEX_DrawItem (COMBOEX_INFO *infoPtr, DRAWITEMSTRUCT const *dis)
1271 static const WCHAR nil[] = { 0 };
1272 CBE_ITEMDATA *item = NULL;
1273 SIZE txtsize;
1274 RECT rect;
1275 LPCWSTR str = nil;
1276 UINT xbase, x, y;
1277 INT len;
1278 COLORREF nbkc, ntxc, bkc, txc;
1279 int drawimage, drawstate, xioff, selected;
1281 TRACE("DRAWITEMSTRUCT: CtlType=0x%08x CtlID=0x%08x\n",
1282 dis->CtlType, dis->CtlID);
1283 TRACE("itemID=0x%08x itemAction=0x%08x itemState=0x%08x\n",
1284 dis->itemID, dis->itemAction, dis->itemState);
1285 TRACE("hWnd=%p hDC=%p (%s) itemData=0x%08lx\n",
1286 dis->hwndItem, dis->hDC, wine_dbgstr_rect(&dis->rcItem), dis->itemData);
1288 /* MSDN says: */
1289 /* "itemID - Specifies the menu item identifier for a menu */
1290 /* item or the index of the item in a list box or combo box. */
1291 /* For an empty list box or combo box, this member can be -1. */
1292 /* This allows the application to draw only the focus */
1293 /* rectangle at the coordinates specified by the rcItem */
1294 /* member even though there are no items in the control. */
1295 /* This indicates to the user whether the list box or combo */
1296 /* box has the focus. How the bits are set in the itemAction */
1297 /* member determines whether the rectangle is to be drawn as */
1298 /* though the list box or combo box has the focus. */
1299 if (dis->itemID == 0xffffffff) {
1300 if ( ( (dis->itemAction & ODA_FOCUS) && (dis->itemState & ODS_SELECTED)) ||
1301 ( (dis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)) && (dis->itemState & ODS_FOCUS) ) ) {
1303 TRACE("drawing item -1 special focus, rect=(%s)\n",
1304 wine_dbgstr_rect(&dis->rcItem));
1306 else if ((dis->CtlType == ODT_COMBOBOX) &&
1307 (dis->itemAction == ODA_DRAWENTIRE)) {
1308 /* draw of edit control data */
1310 if (TRACE_ON(comboex)) {
1311 RECT exrc, cbrc, edrc;
1312 GetWindowRect (infoPtr->hwndSelf, &exrc);
1313 GetWindowRect (infoPtr->hwndCombo, &cbrc);
1314 SetRect(&edrc, -1, -1, -1, -1);
1315 if (infoPtr->hwndEdit) GetWindowRect (infoPtr->hwndEdit, &edrc);
1316 TRACE("window rects ex=(%s), cb=(%s), ed=(%s)\n",
1317 wine_dbgstr_rect(&exrc), wine_dbgstr_rect(&cbrc),
1318 wine_dbgstr_rect(&edrc));
1321 else {
1322 ERR("NOT drawing item -1 special focus, rect=(%s), action=%08x, state=%08x\n",
1323 wine_dbgstr_rect(&dis->rcItem),
1324 dis->itemAction, dis->itemState);
1325 return 0;
1329 /* If draw item is -1 (edit control) setup the item pointer */
1330 if (dis->itemID == 0xffffffff) {
1331 item = &infoPtr->edit;
1333 if (infoPtr->hwndEdit) {
1334 /* free previous text of edit item */
1335 COMBOEX_FreeText(item);
1336 item->mask &= ~CBEIF_TEXT;
1337 if( (len = GetWindowTextLengthW(infoPtr->hwndEdit)) ) {
1338 item->mask |= CBEIF_TEXT;
1339 item->pszText = Alloc ((len + 1)*sizeof(WCHAR));
1340 if (item->pszText)
1341 GetWindowTextW(infoPtr->hwndEdit, item->pszText, len+1);
1343 TRACE("edit control hwndEdit=%p, text len=%d str=%s\n",
1344 infoPtr->hwndEdit, len, debugstr_txt(item->pszText));
1350 /* if the item pointer is not set, then get the data and locate it */
1351 if (!item) {
1352 item = get_item_data(infoPtr, dis->itemID);
1353 if (item == (CBE_ITEMDATA *)CB_ERR) {
1354 ERR("invalid item for id %d\n", dis->itemID);
1355 return 0;
1359 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
1361 xbase = CBE_STARTOFFSET;
1362 if ((item->mask & CBEIF_INDENT) && (dis->itemState & ODS_COMBOEXLBOX)) {
1363 INT indent = item->iIndent;
1364 if (indent == I_INDENTCALLBACK) {
1365 NMCOMBOBOXEXW nmce;
1366 ZeroMemory(&nmce, sizeof(nmce));
1367 nmce.ceItem.mask = CBEIF_INDENT;
1368 nmce.ceItem.lParam = item->lParam;
1369 nmce.ceItem.iItem = dis->itemID;
1370 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1371 if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
1372 item->iIndent = nmce.ceItem.iIndent;
1373 indent = nmce.ceItem.iIndent;
1375 xbase += (indent * CBE_INDENT);
1378 drawimage = -2;
1379 drawstate = ILD_NORMAL;
1380 selected = infoPtr->selected == dis->itemID;
1382 if (item->mask & CBEIF_IMAGE)
1383 drawimage = item->iImage;
1384 if (item->mask & CBEIF_SELECTEDIMAGE && selected)
1385 drawimage = item->iSelectedImage;
1386 if (dis->itemState & ODS_COMBOEXLBOX) {
1387 /* drawing listbox entry */
1388 if (dis->itemState & ODS_SELECTED)
1389 drawstate = ILD_SELECTED;
1390 } else {
1391 /* drawing combo/edit entry */
1392 if (IsWindowVisible(infoPtr->hwndEdit)) {
1393 /* if we have an edit control, the slave the
1394 * selection state to the Edit focus state
1396 if (infoPtr->flags & WCBE_EDITFOCUSED)
1397 drawstate = ILD_SELECTED;
1398 } else
1399 /* if we don't have an edit control, use
1400 * the requested state.
1402 if (dis->itemState & ODS_SELECTED)
1403 drawstate = ILD_SELECTED;
1406 if (infoPtr->himl && !(infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGEINDENT)) {
1407 IMAGEINFO iinfo;
1408 iinfo.rcImage.left = iinfo.rcImage.right = 0;
1409 ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
1410 xioff = iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP;
1411 } else xioff = 0;
1413 /* setup pointer to text to be drawn */
1414 str = COMBOEX_GetText(infoPtr, item);
1415 if (!str) str = nil;
1417 len = strlenW (str);
1418 GetTextExtentPoint32W (dis->hDC, str, len, &txtsize);
1420 if (dis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)) {
1421 int overlay = item->iOverlay;
1423 if (drawimage == I_IMAGECALLBACK) {
1424 NMCOMBOBOXEXW nmce;
1425 ZeroMemory(&nmce, sizeof(nmce));
1426 nmce.ceItem.mask = selected ? CBEIF_SELECTEDIMAGE : CBEIF_IMAGE;
1427 nmce.ceItem.lParam = item->lParam;
1428 nmce.ceItem.iItem = dis->itemID;
1429 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1430 if (!selected) {
1431 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) item->iImage = nmce.ceItem.iImage;
1432 drawimage = nmce.ceItem.iImage;
1433 } else {
1434 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) item->iSelectedImage = nmce.ceItem.iSelectedImage;
1435 drawimage = nmce.ceItem.iSelectedImage;
1439 if (overlay == I_IMAGECALLBACK) {
1440 NMCOMBOBOXEXW nmce;
1441 ZeroMemory(&nmce, sizeof(nmce));
1442 nmce.ceItem.mask = CBEIF_OVERLAY;
1443 nmce.ceItem.lParam = item->lParam;
1444 nmce.ceItem.iItem = dis->itemID;
1445 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
1446 if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
1447 item->iOverlay = nmce.ceItem.iOverlay;
1448 overlay = nmce.ceItem.iOverlay;
1451 if (drawimage >= 0 &&
1452 !(infoPtr->dwExtStyle & (CBES_EX_NOEDITIMAGE | CBES_EX_NOEDITIMAGEINDENT))) {
1453 if (overlay > 0) ImageList_SetOverlayImage (infoPtr->himl, overlay, 1);
1454 ImageList_Draw (infoPtr->himl, drawimage, dis->hDC, xbase, dis->rcItem.top,
1455 drawstate | (overlay > 0 ? INDEXTOOVERLAYMASK(1) : 0));
1458 /* now draw the text */
1459 if (!IsWindowVisible (infoPtr->hwndEdit)) {
1460 nbkc = (dis->itemState & ODS_SELECTED) ?
1461 comctl32_color.clrHighlight : comctl32_color.clrWindow;
1462 bkc = SetBkColor (dis->hDC, nbkc);
1463 ntxc = (dis->itemState & ODS_SELECTED) ?
1464 comctl32_color.clrHighlightText : comctl32_color.clrWindowText;
1465 txc = SetTextColor (dis->hDC, ntxc);
1466 x = xbase + xioff;
1467 y = dis->rcItem.top +
1468 (dis->rcItem.bottom - dis->rcItem.top - txtsize.cy) / 2;
1469 SetRect(&rect, x, dis->rcItem.top + 1, x + txtsize.cx, dis->rcItem.bottom - 1);
1470 TRACE("drawing item %d text, rect=(%s)\n",
1471 dis->itemID, wine_dbgstr_rect(&rect));
1472 ExtTextOutW (dis->hDC, x, y, ETO_OPAQUE | ETO_CLIPPED,
1473 &rect, str, len, 0);
1474 SetBkColor (dis->hDC, bkc);
1475 SetTextColor (dis->hDC, txc);
1479 if (dis->itemAction & ODA_FOCUS) {
1480 rect.left = xbase + xioff - 1;
1481 rect.right = rect.left + txtsize.cx + 2;
1482 rect.top = dis->rcItem.top;
1483 rect.bottom = dis->rcItem.bottom;
1484 DrawFocusRect(dis->hDC, &rect);
1487 return 0;
1491 static void COMBOEX_ResetContent (COMBOEX_INFO *infoPtr)
1493 if (infoPtr->items)
1495 CBE_ITEMDATA *item, *next;
1497 item = infoPtr->items;
1498 while (item) {
1499 next = item->next;
1500 COMBOEX_FreeText (item);
1501 Free (item);
1502 item = next;
1504 infoPtr->items = 0;
1507 infoPtr->selected = -1;
1508 infoPtr->nb_items = 0;
1512 static LRESULT COMBOEX_Destroy (COMBOEX_INFO *infoPtr)
1514 if (infoPtr->hwndCombo)
1515 RemoveWindowSubclass(infoPtr->hwndCombo, COMBOEX_ComboWndProc, COMBO_SUBCLASSID);
1517 if (infoPtr->hwndEdit)
1518 RemoveWindowSubclass(infoPtr->hwndEdit, COMBOEX_EditWndProc, EDIT_SUBCLASSID);
1520 COMBOEX_FreeText (&infoPtr->edit);
1521 COMBOEX_ResetContent (infoPtr);
1523 if (infoPtr->defaultFont)
1524 DeleteObject (infoPtr->defaultFont);
1526 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1528 /* free comboex info data */
1529 Free (infoPtr);
1531 return 0;
1535 static LRESULT COMBOEX_Enable (COMBOEX_INFO *infoPtr, BOOL enable)
1537 TRACE("hwnd=%p, enable=%s\n", infoPtr->hwndSelf, enable ? "TRUE":"FALSE");
1539 if (infoPtr->hwndEdit)
1540 EnableWindow(infoPtr->hwndEdit, enable);
1542 EnableWindow(infoPtr->hwndCombo, enable);
1544 /* Force the control to repaint when the enabled state changes. */
1545 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1547 return 1;
1551 static LRESULT COMBOEX_MeasureItem (COMBOEX_INFO const *infoPtr, MEASUREITEMSTRUCT *mis)
1553 static const WCHAR strW[] = { 'W', 0 };
1554 SIZE mysize;
1555 HDC hdc;
1557 hdc = GetDC (0);
1558 GetTextExtentPointW (hdc, strW, 1, &mysize);
1559 ReleaseDC (0, hdc);
1560 mis->itemHeight = mysize.cy + CBE_EXTRA;
1562 TRACE("adjusted height hwnd=%p, height=%d\n",
1563 infoPtr->hwndSelf, mis->itemHeight);
1565 return 0;
1569 static LRESULT COMBOEX_NCCreate (HWND hwnd)
1571 /* WARNING: The COMBOEX_INFO structure is not yet created */
1572 DWORD oldstyle, newstyle;
1574 oldstyle = (DWORD)GetWindowLongW (hwnd, GWL_STYLE);
1575 newstyle = oldstyle & ~(WS_VSCROLL | WS_HSCROLL | WS_BORDER);
1576 if (newstyle != oldstyle) {
1577 TRACE("req style %08x, resetting style %08x\n",
1578 oldstyle, newstyle);
1579 SetWindowLongW (hwnd, GWL_STYLE, newstyle);
1581 return 1;
1585 static LRESULT COMBOEX_NotifyFormat (COMBOEX_INFO *infoPtr, LPARAM lParam)
1587 if (lParam == NF_REQUERY) {
1588 INT i = SendMessageW(infoPtr->hwndNotify,
1589 WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1590 infoPtr->NtfUnicode = (i == NFR_UNICODE);
1592 return infoPtr->NtfUnicode ? NFR_UNICODE : NFR_ANSI;
1596 static LRESULT COMBOEX_Size (COMBOEX_INFO *infoPtr, INT width, INT height)
1598 TRACE("(width=%d, height=%d)\n", width, height);
1600 MoveWindow (infoPtr->hwndCombo, 0, 0, width, height, TRUE);
1602 COMBOEX_AdjustEditPos (infoPtr);
1604 return 0;
1607 static LRESULT COMBOEX_SetFont( COMBOEX_INFO *infoPtr, HFONT font, BOOL redraw )
1609 infoPtr->font = font;
1610 SendMessageW( infoPtr->hwndCombo, WM_SETFONT, (WPARAM)font, 0 );
1611 if (infoPtr->hwndEdit) SendMessageW( infoPtr->hwndEdit, WM_SETFONT, (WPARAM)font, 0 );
1612 COMBOEX_ReSize( infoPtr );
1613 if (redraw) InvalidateRect( infoPtr->hwndCombo, NULL, TRUE );
1614 return 0;
1617 static LRESULT COMBOEX_SetRedraw(const COMBOEX_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1619 LRESULT ret = DefWindowProcW( infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam );
1620 if (wParam) RedrawWindow( infoPtr->hwndSelf, NULL, 0, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN );
1621 return ret;
1625 static LRESULT COMBOEX_WindowPosChanging (const COMBOEX_INFO *infoPtr, WINDOWPOS *wp)
1627 RECT cbx_wrect, cbx_crect, cb_wrect;
1628 INT width, height;
1630 GetWindowRect (infoPtr->hwndSelf, &cbx_wrect);
1631 GetClientRect (infoPtr->hwndSelf, &cbx_crect);
1632 GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
1634 /* width is winpos value + border width of comboex */
1635 width = wp->cx
1636 + (cbx_wrect.right-cbx_wrect.left)
1637 - (cbx_crect.right-cbx_crect.left);
1639 TRACE("winpos=(%d,%d %dx%d) flags=0x%08x\n",
1640 wp->x, wp->y, wp->cx, wp->cy, wp->flags);
1641 TRACE("EX window=(%s), client=(%s)\n",
1642 wine_dbgstr_rect(&cbx_wrect), wine_dbgstr_rect(&cbx_crect));
1643 TRACE("CB window=(%s), EX setting=(0,0)-(%d,%d)\n",
1644 wine_dbgstr_rect(&cbx_wrect), width, cb_wrect.bottom-cb_wrect.top);
1646 if (width) SetWindowPos (infoPtr->hwndCombo, HWND_TOP, 0, 0,
1647 width,
1648 cb_wrect.bottom-cb_wrect.top,
1649 SWP_NOACTIVATE);
1651 GetWindowRect (infoPtr->hwndCombo, &cb_wrect);
1653 /* height is combo window height plus border width of comboex */
1654 height = (cb_wrect.bottom-cb_wrect.top)
1655 + (cbx_wrect.bottom-cbx_wrect.top)
1656 - (cbx_crect.bottom-cbx_crect.top);
1657 wp->cy = height;
1658 if (infoPtr->hwndEdit) {
1659 COMBOEX_AdjustEditPos (infoPtr);
1660 InvalidateRect (infoPtr->hwndCombo, 0, TRUE);
1663 return 0;
1666 static LRESULT CALLBACK
1667 COMBOEX_EditWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
1668 UINT_PTR uId, DWORD_PTR ref_data)
1670 COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr ((HWND)ref_data);
1671 NMCBEENDEDITW cbeend;
1672 WCHAR edit_text[260];
1673 COLORREF obkc;
1674 HDC hDC;
1675 RECT rect;
1676 LRESULT lret;
1678 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx, info_ptr=%p\n",
1679 hwnd, uMsg, wParam, lParam, infoPtr);
1681 if (!infoPtr) return 0;
1683 switch (uMsg)
1686 case WM_CHAR:
1687 /* handle (ignore) the return character */
1688 if (wParam == VK_RETURN) return 0;
1689 /* all other characters pass into the real Edit */
1690 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1692 case WM_ERASEBKGND:
1693 hDC = (HDC) wParam;
1694 obkc = SetBkColor (hDC, comctl32_color.clrWindow);
1695 GetClientRect (hwnd, &rect);
1696 TRACE("erasing (%s)\n", wine_dbgstr_rect(&rect));
1697 ExtTextOutW (hDC, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
1698 SetBkColor (hDC, obkc);
1699 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1701 case WM_KEYDOWN: {
1702 INT_PTR oldItem, selected;
1703 CBE_ITEMDATA *item;
1705 switch ((INT)wParam)
1707 case VK_ESCAPE:
1708 TRACE("special code for VK_ESCAPE\n");
1710 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1712 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1713 cbeend.fChanged = FALSE;
1714 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1715 CB_GETCURSEL, 0, 0);
1716 cbeend.iWhy = CBENF_ESCAPE;
1718 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) return 0;
1719 oldItem = SendMessageW (infoPtr->hwndCombo, CB_GETCURSEL, 0, 0);
1720 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1721 if (!(item = COMBOEX_FindItem(infoPtr, oldItem))) {
1722 ERR("item %ld not found. Problem!\n", oldItem);
1723 break;
1725 infoPtr->selected = oldItem;
1726 COMBOEX_SetEditText (infoPtr, item);
1727 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE |
1728 RDW_INVALIDATE);
1729 break;
1731 case VK_RETURN:
1732 TRACE("special code for VK_RETURN\n");
1734 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1736 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1737 selected = SendMessageW (infoPtr->hwndCombo,
1738 CB_GETCURSEL, 0, 0);
1740 if (selected != -1) {
1741 cmp_func_t cmptext = get_cmp_func(infoPtr);
1742 item = COMBOEX_FindItem (infoPtr, selected);
1743 TRACE("handling VK_RETURN, selected = %ld, selected_text=%s\n",
1744 selected, debugstr_txt(item->pszText));
1745 TRACE("handling VK_RETURN, edittext=%s\n",
1746 debugstr_w(edit_text));
1747 if (cmptext (COMBOEX_GetText(infoPtr, item), edit_text)) {
1748 /* strings not equal -- indicate edit has changed */
1749 selected = -1;
1753 cbeend.iNewSelection = selected;
1754 cbeend.fChanged = TRUE;
1755 cbeend.iWhy = CBENF_RETURN;
1756 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) {
1757 /* abort the change, restore previous */
1758 TRACE("Notify requested abort of change\n");
1759 COMBOEX_SetEditText (infoPtr, &infoPtr->edit);
1760 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE |
1761 RDW_INVALIDATE);
1762 return 0;
1764 oldItem = SendMessageW (infoPtr->hwndCombo,CB_GETCURSEL, 0, 0);
1765 if (oldItem != -1) {
1766 /* if something is selected, then deselect it */
1767 SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, -1, 0);
1769 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1770 SetFocus(infoPtr->hwndEdit);
1771 break;
1773 case VK_UP:
1774 case VK_DOWN:
1776 INT step = wParam == VK_DOWN ? 1 : -1;
1778 oldItem = SendMessageW (infoPtr->hwndSelf, CB_GETCURSEL, 0, 0);
1779 if (oldItem >= 0 && oldItem + step >= 0)
1780 SendMessageW (infoPtr->hwndSelf, CB_SETCURSEL, oldItem + step, 0);
1781 return 0;
1783 default:
1784 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1786 return 0;
1789 case WM_SETFOCUS:
1790 /* remember the focus to set state of icon */
1791 lret = DefSubclassProc(hwnd, uMsg, wParam, lParam);
1792 infoPtr->flags |= WCBE_EDITFOCUSED;
1793 return lret;
1795 case WM_KILLFOCUS:
1797 * do NOTIFY CBEN_ENDEDIT with CBENF_KILLFOCUS
1799 infoPtr->flags &= ~WCBE_EDITFOCUSED;
1800 if (infoPtr->flags & WCBE_ACTEDIT) {
1801 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1803 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1804 cbeend.fChanged = FALSE;
1805 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1806 CB_GETCURSEL, 0, 0);
1807 cbeend.iWhy = CBENF_KILLFOCUS;
1809 COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text);
1811 /* fall through */
1813 default:
1814 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1819 static LRESULT CALLBACK
1820 COMBOEX_ComboWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
1821 UINT_PTR uId, DWORD_PTR ref_data)
1823 COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr ((HWND)ref_data);
1824 NMCBEENDEDITW cbeend;
1825 NMMOUSE nmmse;
1826 COLORREF obkc;
1827 HDC hDC;
1828 HWND focusedhwnd;
1829 RECT rect;
1830 POINT pt;
1831 WCHAR edit_text[260];
1833 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx, info_ptr=%p\n",
1834 hwnd, uMsg, wParam, lParam, infoPtr);
1836 if (!infoPtr) return 0;
1838 switch (uMsg)
1841 case WM_DRAWITEM:
1843 * The only way this message should come is from the
1844 * child Listbox issuing the message. Flag this so
1845 * that ComboEx knows this is listbox.
1847 ((DRAWITEMSTRUCT *)lParam)->itemState |= ODS_COMBOEXLBOX;
1848 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1850 case WM_ERASEBKGND:
1851 hDC = (HDC) wParam;
1852 obkc = SetBkColor (hDC, comctl32_color.clrWindow);
1853 GetClientRect (hwnd, &rect);
1854 TRACE("erasing (%s)\n", wine_dbgstr_rect(&rect));
1855 ExtTextOutW (hDC, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
1856 SetBkColor (hDC, obkc);
1857 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1859 case WM_SETCURSOR:
1861 * WM_NOTIFY to comboex parent (rebar)
1862 * with NM_SETCURSOR with extra words of 0,0,0,0,0x02010001
1863 * CallWindowProc (previous)
1865 nmmse.dwItemSpec = 0;
1866 nmmse.dwItemData = 0;
1867 nmmse.pt.x = 0;
1868 nmmse.pt.y = 0;
1869 nmmse.dwHitInfo = lParam;
1870 COMBOEX_Notify (infoPtr, NM_SETCURSOR, (NMHDR *)&nmmse);
1871 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1873 case WM_LBUTTONDOWN:
1874 GetClientRect (hwnd, &rect);
1875 rect.bottom = rect.top + SendMessageW(infoPtr->hwndSelf,
1876 CB_GETITEMHEIGHT, -1, 0);
1877 rect.left = rect.right - GetSystemMetrics(SM_CXVSCROLL);
1878 pt.x = (short)LOWORD(lParam);
1879 pt.y = (short)HIWORD(lParam);
1880 if (PtInRect(&rect, pt))
1881 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1883 infoPtr->flags |= WCBE_MOUSECAPTURED;
1884 SetCapture(hwnd);
1885 break;
1887 case WM_LBUTTONUP:
1888 if (!(infoPtr->flags & WCBE_MOUSECAPTURED))
1889 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1891 ReleaseCapture();
1892 infoPtr->flags &= ~WCBE_MOUSECAPTURED;
1893 if (infoPtr->flags & WCBE_MOUSEDRAGGED) {
1894 infoPtr->flags &= ~WCBE_MOUSEDRAGGED;
1895 } else {
1896 SendMessageW(hwnd, CB_SHOWDROPDOWN, TRUE, 0);
1898 break;
1900 case WM_MOUSEMOVE:
1901 if ( (infoPtr->flags & WCBE_MOUSECAPTURED) &&
1902 !(infoPtr->flags & WCBE_MOUSEDRAGGED)) {
1903 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1904 COMBOEX_NotifyDragBegin(infoPtr, edit_text);
1905 infoPtr->flags |= WCBE_MOUSEDRAGGED;
1907 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1909 case WM_COMMAND:
1910 switch (HIWORD(wParam)) {
1912 case EN_UPDATE:
1913 /* traces show that COMBOEX does not issue CBN_EDITUPDATE
1914 * on the EN_UPDATE
1916 return 0;
1918 case EN_KILLFOCUS:
1919 focusedhwnd = GetFocus();
1920 if (infoPtr->flags & WCBE_ACTEDIT) {
1921 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1922 cbeend.fChanged = (infoPtr->flags & WCBE_EDITCHG);
1923 cbeend.iNewSelection = SendMessageW (infoPtr->hwndCombo,
1924 CB_GETCURSEL, 0, 0);
1925 cbeend.iWhy = CBENF_KILLFOCUS;
1927 infoPtr->flags &= ~(WCBE_ACTEDIT | WCBE_EDITCHG);
1928 if (COMBOEX_NotifyEndEdit (infoPtr, &cbeend, edit_text)) return 0;
1930 /* possible CB_GETCURSEL */
1931 InvalidateRect (infoPtr->hwndCombo, 0, 0);
1932 if (focusedhwnd)
1933 SendMessageW (infoPtr->hwndCombo, WM_KILLFOCUS,
1934 (WPARAM)focusedhwnd, 0);
1935 return 0;
1937 case EN_SETFOCUS: {
1938 NMHDR hdr;
1940 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, 0);
1941 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, -1);
1942 COMBOEX_Notify (infoPtr, CBEN_BEGINEDIT, &hdr);
1943 infoPtr->flags |= WCBE_ACTEDIT;
1944 infoPtr->flags &= ~WCBE_EDITCHG; /* no change yet */
1945 return 0;
1948 case EN_CHANGE: {
1949 LPCWSTR lastwrk;
1950 cmp_func_t cmptext = get_cmp_func(infoPtr);
1952 INT_PTR selected = SendMessageW (infoPtr->hwndCombo,
1953 CB_GETCURSEL, 0, 0);
1955 /* lstrlenW( lastworkingURL ) */
1957 GetWindowTextW (infoPtr->hwndEdit, edit_text, 260);
1958 if (selected == -1) {
1959 lastwrk = infoPtr->edit.pszText;
1961 else {
1962 CBE_ITEMDATA *item = COMBOEX_FindItem (infoPtr, selected);
1963 lastwrk = COMBOEX_GetText(infoPtr, item);
1966 TRACE("handling EN_CHANGE, selected = %ld, selected_text=%s\n",
1967 selected, debugstr_w(lastwrk));
1968 TRACE("handling EN_CHANGE, edittext=%s\n",
1969 debugstr_w(edit_text));
1971 /* cmptext is between lastworkingURL and GetWindowText */
1972 if (cmptext (lastwrk, edit_text)) {
1973 /* strings not equal -- indicate edit has changed */
1974 infoPtr->flags |= WCBE_EDITCHG;
1976 SendMessageW ( infoPtr->hwndNotify, WM_COMMAND,
1977 MAKEWPARAM(GetDlgCtrlID (infoPtr->hwndSelf),
1978 CBN_EDITCHANGE),
1979 (LPARAM)infoPtr->hwndSelf);
1980 return 0;
1983 case LBN_SELCHANGE:
1984 default:
1985 break;
1986 }/* fall through */
1987 default:
1988 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1990 return 0;
1994 static LRESULT WINAPI
1995 COMBOEX_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1997 COMBOEX_INFO *infoPtr = COMBOEX_GetInfoPtr (hwnd);
1999 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2001 if (!infoPtr) {
2002 if (uMsg == WM_CREATE)
2003 return COMBOEX_Create (hwnd, (LPCREATESTRUCTA)lParam);
2004 if (uMsg == WM_NCCREATE)
2005 COMBOEX_NCCreate (hwnd);
2006 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2009 switch (uMsg)
2011 case CBEM_DELETEITEM:
2012 return COMBOEX_DeleteItem (infoPtr, wParam);
2014 case CBEM_GETCOMBOCONTROL:
2015 return (LRESULT)infoPtr->hwndCombo;
2017 case CBEM_GETEDITCONTROL:
2018 return (LRESULT)infoPtr->hwndEdit;
2020 case CBEM_GETEXTENDEDSTYLE:
2021 return infoPtr->dwExtStyle;
2023 case CBEM_GETIMAGELIST:
2024 return (LRESULT)infoPtr->himl;
2026 case CBEM_GETITEMA:
2027 return (LRESULT)COMBOEX_GetItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2029 case CBEM_GETITEMW:
2030 return (LRESULT)COMBOEX_GetItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2032 case CBEM_GETUNICODEFORMAT:
2033 return infoPtr->unicode;
2035 case CBEM_HASEDITCHANGED:
2036 return COMBOEX_HasEditChanged (infoPtr);
2038 case CBEM_INSERTITEMA:
2039 return COMBOEX_InsertItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2041 case CBEM_INSERTITEMW:
2042 return COMBOEX_InsertItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2044 case CBEM_SETEXSTYLE:
2045 case CBEM_SETEXTENDEDSTYLE:
2046 return COMBOEX_SetExtendedStyle (infoPtr, (DWORD)wParam, (DWORD)lParam);
2048 case CBEM_SETIMAGELIST:
2049 return (LRESULT)COMBOEX_SetImageList (infoPtr, (HIMAGELIST)lParam);
2051 case CBEM_SETITEMA:
2052 return COMBOEX_SetItemA (infoPtr, (COMBOBOXEXITEMA *)lParam);
2054 case CBEM_SETITEMW:
2055 return COMBOEX_SetItemW (infoPtr, (COMBOBOXEXITEMW *)lParam);
2057 case CBEM_SETUNICODEFORMAT:
2058 return COMBOEX_SetUnicodeFormat (infoPtr, wParam);
2060 /*case CBEM_SETWINDOWTHEME:
2061 FIXME("CBEM_SETWINDOWTHEME: stub\n");*/
2063 case WM_SETTEXT:
2064 case WM_GETTEXT:
2065 case WM_GETTEXTLENGTH:
2066 return SendMessageW(infoPtr->hwndEdit, uMsg, wParam, lParam);
2068 case CB_GETLBTEXT:
2069 return COMBOEX_GetListboxText(infoPtr, wParam, (LPWSTR)lParam);
2071 case CB_GETLBTEXTLEN:
2072 return COMBOEX_GetListboxText(infoPtr, wParam, NULL);
2074 case CB_RESETCONTENT:
2075 COMBOEX_ResetContent(infoPtr);
2076 /* fall through */
2078 /* Combo messages we are not sure if we need to process or just forward */
2079 case CB_GETDROPPEDCONTROLRECT:
2080 case CB_GETITEMHEIGHT:
2081 case CB_GETEXTENDEDUI:
2082 case CB_LIMITTEXT:
2083 case CB_SELECTSTRING:
2085 /* Combo messages OK to just forward to the regular COMBO */
2086 case CB_GETCOUNT:
2087 case CB_GETCURSEL:
2088 case CB_GETDROPPEDSTATE:
2089 case CB_SETDROPPEDWIDTH:
2090 case CB_SETEXTENDEDUI:
2091 case CB_SHOWDROPDOWN:
2092 return SendMessageW (infoPtr->hwndCombo, uMsg, wParam, lParam);
2094 /* Combo messages we need to process specially */
2095 case CB_FINDSTRINGEXACT:
2096 return COMBOEX_FindStringExact (infoPtr, (INT)wParam, (LPCWSTR)lParam);
2098 case CB_GETITEMDATA:
2099 return COMBOEX_GetItemData (infoPtr, (INT)wParam);
2101 case CB_SETCURSEL:
2102 return COMBOEX_SetCursel (infoPtr, (INT)wParam);
2104 case CB_SETITEMDATA:
2105 return COMBOEX_SetItemData (infoPtr, (INT)wParam, (DWORD_PTR)lParam);
2107 case CB_SETITEMHEIGHT:
2108 return COMBOEX_SetItemHeight (infoPtr, (INT)wParam, (UINT)lParam);
2112 /* Window messages passed to parent */
2113 case WM_COMMAND:
2114 return COMBOEX_Command (infoPtr, wParam);
2116 case WM_NOTIFY:
2117 if (infoPtr->NtfUnicode)
2118 return SendMessageW (infoPtr->hwndNotify, uMsg, wParam, lParam);
2119 else
2120 return SendMessageA (infoPtr->hwndNotify, uMsg, wParam, lParam);
2123 /* Window messages we need to process */
2124 case WM_DELETEITEM:
2125 return COMBOEX_WM_DeleteItem (infoPtr, (DELETEITEMSTRUCT *)lParam);
2127 case WM_DRAWITEM:
2128 return COMBOEX_DrawItem (infoPtr, (DRAWITEMSTRUCT *)lParam);
2130 case WM_DESTROY:
2131 return COMBOEX_Destroy (infoPtr);
2133 case WM_ENABLE:
2134 return COMBOEX_Enable (infoPtr, (BOOL)wParam);
2136 case WM_MEASUREITEM:
2137 return COMBOEX_MeasureItem (infoPtr, (MEASUREITEMSTRUCT *)lParam);
2139 case WM_NOTIFYFORMAT:
2140 return COMBOEX_NotifyFormat (infoPtr, lParam);
2142 case WM_SIZE:
2143 return COMBOEX_Size (infoPtr, LOWORD(lParam), HIWORD(lParam));
2145 case WM_GETFONT:
2146 return (LRESULT)infoPtr->font;
2148 case WM_SETFONT:
2149 return COMBOEX_SetFont( infoPtr, (HFONT)wParam, LOWORD(lParam) != 0 );
2151 case WM_SETREDRAW:
2152 return COMBOEX_SetRedraw(infoPtr, wParam, lParam);
2154 case WM_WINDOWPOSCHANGING:
2155 return COMBOEX_WindowPosChanging (infoPtr, (WINDOWPOS *)lParam);
2157 case WM_SETFOCUS:
2158 if (infoPtr->hwndEdit) SetFocus( infoPtr->hwndEdit );
2159 else SetFocus( infoPtr->hwndCombo );
2160 return 0;
2162 case WM_SYSCOLORCHANGE:
2163 COMCTL32_RefreshSysColors();
2164 return 0;
2166 default:
2167 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2168 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",uMsg,wParam,lParam);
2169 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2174 void COMBOEX_Register (void)
2176 WNDCLASSW wndClass;
2178 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2179 wndClass.style = CS_GLOBALCLASS;
2180 wndClass.lpfnWndProc = COMBOEX_WindowProc;
2181 wndClass.cbClsExtra = 0;
2182 wndClass.cbWndExtra = sizeof(COMBOEX_INFO *);
2183 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2184 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2185 wndClass.lpszClassName = WC_COMBOBOXEXW;
2187 RegisterClassW (&wndClass);
2191 void COMBOEX_Unregister (void)
2193 UnregisterClassW (WC_COMBOBOXEXW, NULL);