1 /*
2 * ComboBoxEx control
3 *
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 2000, 2001, 2002 Guy Albertelli <galberte@neo.lrun.com>
6 * Copyright 2002 Dimitrie O. Paun
7 *
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.
12 *
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.
17 *
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
21 *
22 * NOTE
23 *
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.
26 *
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.
30 *
31 */
32
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"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(comboex);
46
47 /* Item structure */
48 typedef struct _CBE_ITEMDATA
49 {
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;
61
62 /* ComboBoxEx structure */
63 typedef struct
64 {
65 HIMAGELIST himl;
66 HWND hwndSelf; /* my own hwnd */
67 HWND hwndNotify; /* my parent hwnd */
68 HWND hwndCombo;
69 HWND hwndEdit;
70 WNDPROC prevEditWndProc; /* previous Edit WNDPROC value */
71 WNDPROC prevComboWndProc; /* previous Combo WNDPROC value */
72 DWORD dwExtStyle;
73 INT selected; /* index of selected item */
74 DWORD flags; /* WINE internal flags */
75 HFONT defaultFont;
76 HFONT font;
77 INT nb_items; /* Number of items */
78 BOOL unicode; /* TRUE if this window is Unicode */
79 BOOL NtfUnicode; /* TRUE if parent wants notify in Unicode */
80 CBE_ITEMDATA *edit; /* item data for edit item */
81 CBE_ITEMDATA *items; /* Array of items */
82 } COMBOEX_INFO;
83
84 /* internal flags in the COMBOEX_INFO structure */
85 #define WCBE_ACTEDIT 0x00000001 /* Edit active i.e.
86 * CBEN_BEGINEDIT issued
87 * but CBEN_ENDEDIT{A|W}
88 * not yet issued. */
89 #define WCBE_EDITCHG 0x00000002 /* Edit issued EN_CHANGE */
90 #define WCBE_EDITHASCHANGED (WCBE_ACTEDIT | WCBE_EDITCHG)
91 #define WCBE_EDITFOCUSED 0x00000004 /* Edit control has focus */
92 #define WCBE_MOUSECAPTURED 0x00000008 /* Combo has captured mouse */
93 #define WCBE_MOUSEDRAGGED 0x00000010 /* User has dragged in combo */
94
95 #define ID_CB_EDIT 1001
96
97
98 /*
99 * Special flag set in DRAWITEMSTRUCT itemState field. It is set by
100 * the ComboEx version of the Combo Window Proc so that when the
101 * WM_DRAWITEM message is then passed to ComboEx, we know that this
102 * particular WM_DRAWITEM message is for listbox only items. Any messasges
103 * without this flag is then for the Edit control field.
104 *
105 * We really cannot use the ODS_COMBOBOXEDIT flag because MSDN states that
106 * only version 4.0 applications will have ODS_COMBOBOXEDIT set.
107 */
108 #define ODS_COMBOEXLBOX 0x4000
109
110
111
112 /* Height in pixels of control over the amount of the selected font */
113 #define CBE_EXTRA 3
114
115 /* Indent amount per MS documentation */
116 #define CBE_INDENT 10
117
118 /* Offset in pixels from left side for start of image or text */
119 #define CBE_STARTOFFSET 6
120
121 /* Offset between image and text */
122 #define CBE_SEP 4
123
124 static const WCHAR COMBOEX_SUBCLASS_PROP[] = {
125 'C','C','C','o','m','b','o','E','x','3','2',
126 'S','u','b','c','l','a','s','s','I','n','f','o',0
127 };
128
129 #define COMBOEX_GetInfoPtr(hwnd) ((COMBOEX_INFO *)GetWindowLongPtrW (hwnd, 0))
130
131
132 /* Things common to the entire DLL */
133 static LRESULT WINAPI COMBOEX_EditWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
134 static LRESULT WINAPI COMBOEX_ComboWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
135 static LRESULT COMBOEX_Destroy (COMBOEX_INFO *infoPtr);
136 typedef INT (WINAPI *cmp_func_t)(LPCWSTR, LPCWSTR);
137
138 static inline BOOL is_textW(LPCWSTR str)
139 {
140 return str && str != LPSTR_TEXTCALLBACKW;
141 }
142
143 static inline BOOL is_textA(LPCSTR str)
144 {
145 return str && str != LPSTR_TEXTCALLBACKA;
146 }
147
148 static inline LPCSTR debugstr_txt(LPCWSTR str)
149 {
150 if (str == LPSTR_TEXTCALLBACKW) return "(callback)";
151 return debugstr_w(str);
152 }
153
154 static void COMBOEX_DumpItem (CBE_ITEMDATA const *item)
155 {
156 TRACE("item %p - mask=%08x, pszText=%p, cchTM=%d, iImage=%d\n",
157 item, item->mask, item->pszText, item->cchTextMax, item->iImage);
158 TRACE("item %p - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
159 item, item->iSelectedImage, item->iOverlay, item->iIndent, item->lParam);
160 if (item->mask & CBEIF_TEXT)
161 TRACE("item %p - pszText=%s\n", item, debugstr_txt(item->pszText));
162 }
163
164
165 static void COMBOEX_DumpInput (COMBOBOXEXITEMW const *input)
166 {
167 TRACE("input - mask=%08x, iItem=%d, pszText=%p, cchTM=%d, iImage=%d\n",
168 input->mask, input->iItem, input->pszText, input->cchTextMax,
169 input->iImage);
170 if (input->mask & CBEIF_TEXT)
171 TRACE("input - pszText=<%s>\n", debugstr_txt(input->pszText));
172 TRACE("input - iSelectedImage=%d, iOverlay=%d, iIndent=%d, lParam=%08lx\n",
173 input->iSelectedImage, input->iOverlay, input->iIndent, input->lParam);
174 }
175
176
177 static inline CBE_ITEMDATA *get_item_data(COMBOEX_INFO *infoPtr, INT index)
178 {
179 return (CBE_ITEMDATA *)SendMessageW (infoPtr->hwndCombo, CB_GETITEMDATA,
180 (WPARAM)index, 0);
181 }
182
183 static inline cmp_func_t get_cmp_func(COMBOEX_INFO const *infoPtr)
184 {
185 return infoPtr->dwExtStyle & CBES_EX_CASESENSITIVE ? lstrcmpW : lstrcmpiW;
186 }
187
188 static INT COMBOEX_Notify (COMBOEX_INFO *infoPtr, INT code, NMHDR *hdr)
189 {
190 hdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
191 hdr->hwndFrom = infoPtr->hwndSelf;
192 hdr->code = code;
193 if (infoPtr->NtfUnicode)
194 return SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, 0, (LPARAM)hdr);
195 else
196 return SendMessageA (infoPtr->hwndNotify, WM_NOTIFY, 0, (LPARAM)hdr);
197 }
198
199
200 static INT
201 COMBOEX_NotifyItem (COMBOEX_INFO *infoPtr, INT code, NMCOMBOBOXEXW *hdr)
202 {
203 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
204 if (infoPtr->NtfUnicode)
205 return COMBOEX_Notify (infoPtr, code, &hdr->hdr);
206 else {
207 LPWSTR wstr = hdr->ceItem.pszText;
208 LPSTR astr = 0;
209 INT ret, len = 0;
210
211 if ((hdr->ceItem.mask & CBEIF_TEXT) && is_textW(wstr)) {
212 len = WideCharToMultiByte (CP_ACP, 0, wstr, -1, 0, 0, NULL, NULL);
213 if (len > 0) {
214 astr = (LPSTR)Alloc ((len + 1)*sizeof(CHAR));
215 if (!astr) return 0;
216 WideCharToMultiByte (CP_ACP, 0, wstr, -1, astr, len, 0, 0);
217 hdr->ceItem.pszText = (LPWSTR)astr;
218 }
219 }
220
221 if (code == CBEN_ENDEDITW) code = CBEN_ENDEDITA;
222 else if (code == CBEN_GETDISPINFOW) code = CBEN_GETDISPINFOA;
223 else if (code == CBEN_DRAGBEGINW) code = CBEN_DRAGBEGINA;
224
225 ret = COMBOEX_Notify (infoPtr, code, (NMHDR *)hdr);
226
227 if (astr && hdr->ceItem.pszText == (LPWSTR)astr)
228 hdr->ceItem.pszText = wstr;
229
230 Free(astr);
231
232 return ret;
233 }
234 }
235
236
237 static INT COMBOEX_NotifyEndEdit (COMBOEX_INFO *infoPtr, NMCBEENDEDITW *neew, LPCWSTR wstr)
238 {
239 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
240 if (infoPtr->NtfUnicode) {
241 lstrcpynW(neew->szText, wstr, CBEMAXSTRLEN);
242 return COMBOEX_Notify (infoPtr, CBEN_ENDEDITW, &neew->hdr);
243 } else {
244 NMCBEENDEDITA neea;
245
246 neea.hdr = neew->hdr;
247 neea.fChanged = neew->fChanged;
248 neea.iNewSelection = neew->iNewSelection;
249 WideCharToMultiByte (CP_ACP, 0, wstr, -1, neea.szText, CBEMAXSTRLEN, 0, 0);
250 neea.iWhy = neew->iWhy;
251
252 return COMBOEX_Notify (infoPtr, CBEN_ENDEDITA, &neea.hdr);
253 }
254 }
255
256
257 static void COMBOEX_NotifyDragBegin(COMBOEX_INFO *infoPtr, LPCWSTR wstr)
258 {
259 /* Change the Text item from Unicode to ANSI if necessary for NOTIFY */
260 if (infoPtr->NtfUnicode) {
261 NMCBEDRAGBEGINW ndbw;
262
263 ndbw.iItemid = -1;
264 lstrcpynW(ndbw.szText, wstr, CBEMAXSTRLEN);
265 COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINW, &ndbw.hdr);
266 } else {
267 NMCBEDRAGBEGINA ndba;
268
269 ndba.iItemid = -1;
270 WideCharToMultiByte (CP_ACP, 0, wstr, -1, ndba.szText, CBEMAXSTRLEN, 0, 0);
271
272 COMBOEX_Notify (infoPtr, CBEN_DRAGBEGINA, &ndba.hdr);
273 }
274 }
275
276
277 static void COMBOEX_FreeText (CBE_ITEMDATA *item)
278 {
279 if (is_textW(item->pszText)) Free(item->pszText);
280 item->pszText = 0;
281 Free(item->pszTemp);
282 item->pszTemp = 0;
283 }
284
285
286 static INT COMBOEX_GetIndex(COMBOEX_INFO const *infoPtr, CBE_ITEMDATA const *item)
287 {
288 CBE_ITEMDATA const *moving;
289 INT index;
290
291 moving = infoPtr->items;
292 index = infoPtr->nb_items - 1;
293
294 while (moving && (moving != item)) {
295 moving = moving->next;
296 index--;
297 }
298 if (!moving || (index < 0)) {
299 ERR("COMBOBOXEX item structures broken. Please report!\n");
300 return -1;
301 }
302 return index;
303 }
304
305
306 static LPCWSTR COMBOEX_GetText(COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
307 {
308 NMCOMBOBOXEXW nmce;
309 LPWSTR text, buf;
310 INT len;
311
312 if (item->pszText != LPSTR_TEXTCALLBACKW)
313 return item->pszText;
314
315 ZeroMemory(&nmce, sizeof(nmce));
316 nmce.ceItem.mask = CBEIF_TEXT;
317 nmce.ceItem.lParam = item->lParam;
318 nmce.ceItem.iItem = COMBOEX_GetIndex(infoPtr, item);
319 COMBOEX_NotifyItem(infoPtr, CBEN_GETDISPINFOW, &nmce);
320
321 if (is_textW(nmce.ceItem.pszText)) {
322 len = MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, NULL, 0);
323 buf = (LPWSTR)Alloc ((len + 1)*sizeof(WCHAR));
324 if (buf)
325 MultiByteToWideChar (CP_ACP, 0, (LPSTR)nmce.ceItem.pszText, -1, buf, len);
326 if (nmce.ceItem.mask & CBEIF_DI_SETITEM) {
327 COMBOEX_FreeText(item);
328 item->pszText = buf;
329 } else {
330 Free(item->pszTemp);
331 item->pszTemp = buf;
332 }
333 text = buf;
334 } else
335 text = nmce.ceItem.pszText;
336
337 if (nmce.ceItem.mask & CBEIF_DI_SETITEM)
338 item->pszText = text;
339 return text;
340 }
341
342
343 static void COMBOEX_GetComboFontSize (COMBOEX_INFO *infoPtr, SIZE *size)
344 {
345 static const WCHAR strA[] = { 'A', 0 };
346 HFONT nfont, ofont;
347 HDC mydc;
348
349 mydc = GetDC (0); /* why the entire screen???? */
350 nfont = (HFONT)SendMessageW (infoPtr->hwndCombo, WM_GETFONT, 0, 0);
351 ofont = (HFONT) SelectObject (mydc, nfont);
352 GetTextExtentPointW (mydc, strA, 1, size);
353 SelectObject (mydc, ofont);
354 ReleaseDC (0, mydc);
355 TRACE("selected font hwnd=%p, height=%d\n", nfont, size->cy);
356 }
357
358
359 static void COMBOEX_CopyItem (CBE_ITEMDATA *item, COMBOBOXEXITEMW *cit)
360 {
361 if (cit->mask & CBEIF_TEXT) {
362 /*
363 * when given a text buffer actually use that buffer
364 */
365 if (cit->pszText) {
366 if (is_textW(item->pszText))
367 lstrcpynW(cit->pszText, item->pszText, cit->cchTextMax);
368 else
369 cit->pszText[0] = 0;
370 } else {
371 cit->pszText = item->pszText;
372 cit->cchTextMax = item->cchTextMax;
373 }
374 }
375 if (cit->mask & CBEIF_IMAGE)
376 cit->iImage = item->iImage;
377 if (cit->mask & CBEIF_SELECTEDIMAGE)
378 cit->iSelectedImage = item->iSelectedImage;
379 if (cit->mask & CBEIF_OVERLAY)
380 cit->iOverlay = item->iOverlay;
381 if (cit->mask & CBEIF_INDENT)
382 cit->iIndent = item->iIndent;
383 if (cit->mask & CBEIF_LPARAM)
384 cit->lParam = item->lParam;
385 }
386
387
388 static void COMBOEX_AdjustEditPos (COMBOEX_INFO *infoPtr)
389 {
390 SIZE mysize;
391 INT x, y, w, h, xioff;
392 RECT rect;
393
394 if (!infoPtr->hwndEdit) return;
395
396 if (infoPtr->himl && !(infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGEINDENT)) {
397 IMAGEINFO iinfo;
398 iinfo.rcImage.left = iinfo.rcImage.right = 0;
399 ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo);
400 xioff = iinfo.rcImage.right - iinfo.rcImage.left + CBE_SEP;
401 } else xioff = 0;
402
403 GetClientRect (infoPtr->hwndCombo, &rect);
404 InflateRect (&rect, -2, -2);
405 InvalidateRect (infoPtr->hwndCombo, &rect, TRUE);
406
407 /* reposition the Edit control based on whether icon exists */
408 COMBOEX_GetComboFontSize (infoPtr, &mysize);
409 TRACE("Combo font x=%d, y=%d\n", mysize.cx, mysize.cy);
410 x = xioff + CBE_STARTOFFSET + 1;
411 w = rect.right-rect.left - x - GetSystemMetrics(SM_CXVSCROLL) - 1;
412 h = mysize.cy + 1;
413 y = rect.bottom - h - 1;
414
415 TRACE("Combo client (%s), setting Edit to (%d,%d)-(%d,%d)\n",
416 wine_dbgstr_rect(&rect), x, y, x + w, y + h);
417 SetWindowPos(infoPtr->hwndEdit, HWND_TOP, x, y, w, h,
418 SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER);
419 }
420
421
422 static void COMBOEX_ReSize (COMBOEX_INFO *infoPtr)
423 {
424 SIZE mysize;
425 LONG cy;
426 IMAGEINFO iinfo;
427
428 COMBOEX_GetComboFontSize (infoPtr, &mysize);
429 cy = mysize.cy + CBE_EXTRA;
430 if (infoPtr->himl && ImageList_GetImageInfo(infoPtr->himl, 0, &iinfo)) {
431 cy = max (iinfo.rcImage.bottom - iinfo.rcImage.top, cy);
432 TRACE("upgraded height due to image: height=%d\n", cy);
433 }
434 SendMessageW (infoPtr->hwndSelf, CB_SETITEMHEIGHT, (WPARAM)-1, (LPARAM)cy);
435 if (infoPtr->hwndCombo) {
436 SendMessageW (infoPtr->hwndCombo, CB_SETITEMHEIGHT,
437 (WPARAM) 0, (LPARAM) cy);
438 if ( !(infoPtr->flags & CBES_EX_NOSIZELIMIT)) {
439 RECT comboRect;
440 if (GetWindowRect(infoPtr->hwndCombo, &comboRect)) {
441 RECT ourRect;
442 if (GetWindowRect(infoPtr->hwndSelf, &ourRect)) {
443 if (comboRect.bottom > ourRect.bottom) {
444 POINT pt = { ourRect.left, ourRect.top };
445 if (ScreenToClient(infoPtr->hwndSelf, &pt))
446 MoveWindow( infoPtr->hwndSelf, pt.x, pt.y, ourRect.right - ourRect.left,
447 comboRect.bottom - comboRect.top, FALSE);
448 }
449 }
450 }
451 }
452 }
453 }
454
455
456 static void COMBOEX_SetEditText (COMBOEX_INFO *infoPtr, CBE_ITEMDATA *item)
457 {
458 if (!infoPtr->hwndEdit) return;
459 /* native issues the following messages to the {Edit} control */
460 /* WM_SETTEXT (0,addr) */
461 /* EM_SETSEL32 (0,0) */
462 /* EM_SETSEL32 (0,-1) */
463 if (item->mask & CBEIF_TEXT) {
464 SendMessageW (infoPtr->hwndEdit, WM_SETTEXT, 0, (LPARAM)COMBOEX_GetText(infoPtr, item));
465 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, 0);
466 SendMessageW (infoPtr->hwndEdit, EM_SETSEL, 0, -1);
467 }
468 }
469
470
471 static CBE_ITEMDATA * COMBOEX_FindItem(COMBOEX_INFO *infoPtr, INT index)
472 {
473 CBE_ITEMDATA *item;
474 INT i;
475
476 if ((index >= infoPtr->nb_items) || (index < -1))
477 return 0;
478 if (index == -1)
479 return infoPtr->edit;
480 item = infoPtr->items;
481 i = infoPtr->nb_items - 1;
482
483 /* find the item in the list */
484 while (item && (i > index)) {
485 item = item->next;
486 i--;
487 }
488 if (!item || (i != index)) {
489 ERR("COMBOBOXEX item structures broken. Please report!\n");
490 return 0;
491 }
492 return item;
493 }
494
495
496 static inline BOOL COMBOEX_HasEdit(COMBOEX_INFO const *infoPtr)
497 {
498 return infoPtr->hwndEdit ? TRUE : FALSE;
499 }
500
501
502 /* *** CBEM_xxx message support *** */
503
504 static UINT COMBOEX_GetListboxText(COMBOEX_INFO *infoPtr, int n, LPWSTR buf)
505 {
506 CBE_ITEMDATA *item;
507 LPCWSTR str;
508
509 item = COMBOEX_FindItem(infoPtr, n);
510 if (!item)
511 return 0;
512
513 str = COMBOEX_GetText(infoPtr, item);
514
515 if (infoPtr->unicode)
516 {
517 if (buf)
518 lstrcpyW(buf, str);
519 return lstrlenW(str);
520 }
521 else
522 {
523 UINT r;
524 r = WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)buf, 0x40000000, NULL, NULL);
525 if (r) r--;
526 return r;
527 }
528 }
529
530
531 static INT COMBOEX_DeleteItem (COMBOEX_INFO *infoPtr, INT index)
532 {
533 TRACE("(index=%d)\n", index);
534
535 /* if item number requested does not exist then return failure */
536 if ((index >= infoPtr->nb_items) || (index < 0)) return CB_ERR;
537 if (!COMBOEX_FindItem(infoPtr, index)) return CB_ERR;
538
539 /* doing this will result in WM_DELETEITEM being issued */
540 SendMessageW (infoPtr->hwndCombo, CB_DELETESTRING, (WPARAM)index, 0);
541
542 return infoPtr->nb_items;
543 }
544
545
546 static BOOL COMBOEX_GetItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW *cit)
547 {
548 INT index = cit->iItem;
549 CBE_ITEMDATA *item;
550
551 TRACE("(...)\n");
552
553 /* if item number requested does not exist then return failure */
554 if ((index >= infoPtr->nb_items) || (index < -1)) return FALSE;
555
556 /* if the item is the edit control and there is no edit control, skip */
557 if ((index == -1) && !COMBOEX_HasEdit(infoPtr)) return FALSE;
558
559 if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
560
561 COMBOEX_CopyItem (item, cit);
562
563 return TRUE;
564 }
565
566
567 static BOOL COMBOEX_GetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA *cit)
568 {
569 COMBOBOXEXITEMW tmpcit;
570
571 TRACE("(...)\n");
572
573 tmpcit.mask = cit->mask;
574 tmpcit.iItem = cit->iItem;
575 tmpcit.pszText = 0;
576 if(!COMBOEX_GetItemW (infoPtr, &tmpcit)) return FALSE;
577
578 if (cit->mask & CBEIF_TEXT)
579 {
580 if (is_textW(tmpcit.pszText) && cit->pszText)
581 WideCharToMultiByte(CP_ACP, 0, tmpcit.pszText, -1,
582 cit->pszText, cit->cchTextMax, NULL, NULL);
583 else if (cit->pszText) cit->pszText[0] = 0;
584 else cit->pszText = (LPSTR)tmpcit.pszText;
585 }
586
587 if (cit->mask & CBEIF_IMAGE)
588 cit->iImage = tmpcit.iImage;
589 if (cit->mask & CBEIF_SELECTEDIMAGE)
590 cit->iSelectedImage = tmpcit.iSelectedImage;
591 if (cit->mask & CBEIF_OVERLAY)
592 cit->iOverlay = tmpcit.iOverlay;
593 if (cit->mask & CBEIF_INDENT)
594 cit->iIndent = tmpcit.iIndent;
595 if (cit->mask & CBEIF_LPARAM)
596 cit->lParam = tmpcit.lParam;
597
598 return TRUE;
599 }
600
601
602 static inline BOOL COMBOEX_HasEditChanged (COMBOEX_INFO const *infoPtr)
603 {
604 return COMBOEX_HasEdit(infoPtr) &&
605 (infoPtr->flags & WCBE_EDITHASCHANGED) == WCBE_EDITHASCHANGED;
606 }
607
608
609 static INT COMBOEX_InsertItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW const *cit)
610 {
611 INT index;
612 CBE_ITEMDATA *item;
613 NMCOMBOBOXEXW nmcit;
614
615 TRACE("\n");
616
617 if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
618
619 /* get real index of item to insert */
620 index = cit->iItem;
621 if (index == -1) index = infoPtr->nb_items;
622 if (index > infoPtr->nb_items) return -1;
623
624 /* get zero-filled space and chain it in */
625 if(!(item = (CBE_ITEMDATA *)Alloc (sizeof(*item)))) return -1;
626
627 /* locate position to insert new item in */
628 if (index == infoPtr->nb_items) {
629 /* fast path for iItem = -1 */
630 item->next = infoPtr->items;
631 infoPtr->items = item;
632 }
633 else {
634 INT i = infoPtr->nb_items-1;
635 CBE_ITEMDATA *moving = infoPtr->items;
636
637 while ((i > index) && moving) {
638 moving = moving->next;
639 i--;
640 }
641 if (!moving) {
642 ERR("COMBOBOXEX item structures broken. Please report!\n");
643 Free(item);
644 return -1;
645 }
646 item->next = moving->next;
647 moving->next = item;
648 }
649
650 /* fill in our hidden item structure */
651 item->mask = cit->mask;
652 if (item->mask & CBEIF_TEXT) {
653 INT len = 0;
654
655 if (is_textW(cit->pszText)) len = strlenW (cit->pszText);
656 if (len > 0) {
657 item->pszText = (LPWSTR)Alloc ((len + 1)*sizeof(WCHAR));
658 if (!item->pszText) {
659 Free(item);
660 return -1;
661 }
662 strcpyW (item->pszText, cit->pszText);
663 }
664 else if (cit->pszText == LPSTR_TEXTCALLBACKW)
665 item->pszText = LPSTR_TEXTCALLBACKW;
666 item->cchTextMax = cit->cchTextMax;
667 }
668 if (item->mask & CBEIF_IMAGE)
669 item->iImage = cit->iImage;
670 if (item->mask & CBEIF_SELECTEDIMAGE)
671 item->iSelectedImage = cit->iSelectedImage;
672 if (item->mask & CBEIF_OVERLAY)
673 item->iOverlay = cit->iOverlay;
674 if (item->mask & CBEIF_INDENT)
675 item->iIndent = cit->iIndent;
676 if (item->mask & CBEIF_LPARAM)
677 item->lParam = cit->lParam;
678 infoPtr->nb_items++;
679
680 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
681
682 SendMessageW (infoPtr->hwndCombo, CB_INSERTSTRING,
683 (WPARAM)cit->iItem, (LPARAM)item);
684
685 memset (&nmcit.ceItem, 0, sizeof(nmcit.ceItem));
686 COMBOEX_CopyItem (item, &nmcit.ceItem);
687 COMBOEX_NotifyItem (infoPtr, CBEN_INSERTITEM, &nmcit);
688
689 return index;
690
691 }
692
693
694 static INT COMBOEX_InsertItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA const *cit)
695 {
696 COMBOBOXEXITEMW citW;
697 LPWSTR wstr = NULL;
698 INT ret;
699
700 memcpy(&citW,cit,sizeof(COMBOBOXEXITEMA));
701 if (cit->mask & CBEIF_TEXT && is_textA(cit->pszText)) {
702 INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
703 wstr = (LPWSTR)Alloc ((len + 1)*sizeof(WCHAR));
704 if (!wstr) return -1;
705 MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
706 citW.pszText = wstr;
707 }
708 ret = COMBOEX_InsertItemW(infoPtr, &citW);
709
710 Free(wstr);
711
712 return ret;
713 }
714
715
716 static DWORD
717 COMBOEX_SetExtendedStyle (COMBOEX_INFO *infoPtr, DWORD mask, DWORD style)
718 {
719 DWORD dwTemp;
720
721 TRACE("(mask=x%08x, style=0x%08x)\n", mask, style);
722
723 dwTemp = infoPtr->dwExtStyle;
724
725 if (mask)
726 infoPtr->dwExtStyle = (infoPtr->dwExtStyle & ~mask) | style;
727 else
728 infoPtr->dwExtStyle = style;
729
730 /* see if we need to change the word break proc on the edit */
731 if ((infoPtr->dwExtStyle ^ dwTemp) & CBES_EX_PATHWORDBREAKPROC)
732 SetPathWordBreakProc(infoPtr->hwndEdit,
733 (infoPtr->dwExtStyle & CBES_EX_PATHWORDBREAKPROC) ? TRUE : FALSE);
734
735 /* test if the control's appearance has changed */
736 mask = CBES_EX_NOEDITIMAGE | CBES_EX_NOEDITIMAGEINDENT;
737 if ((infoPtr->dwExtStyle & mask) != (dwTemp & mask)) {
738 /* if state of EX_NOEDITIMAGE changes, invalidate all */
739 TRACE("EX_NOEDITIMAGE state changed to %d\n",
740 infoPtr->dwExtStyle & CBES_EX_NOEDITIMAGE);
741 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
742 COMBOEX_AdjustEditPos (infoPtr);
743 if (infoPtr->hwndEdit)
744 InvalidateRect (infoPtr->hwndEdit, NULL, TRUE);
745 }
746
747 return dwTemp;
748 }
749
750
751 static HIMAGELIST COMBOEX_SetImageList (COMBOEX_INFO *infoPtr, HIMAGELIST himl)
752 {
753 HIMAGELIST himlTemp = infoPtr->himl;
754
755 TRACE("(...)\n");
756
757 infoPtr->himl = himl;
758
759 COMBOEX_ReSize (infoPtr);
760 InvalidateRect (infoPtr->hwndCombo, NULL, TRUE);
761
762 /* reposition the Edit control based on whether icon exists */
763 COMBOEX_AdjustEditPos (infoPtr);
764 return himlTemp;
765 }
766
767 static BOOL COMBOEX_SetItemW (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMW *cit)
768 {
769 INT index = cit->iItem;
770 CBE_ITEMDATA *item;
771
772 if (TRACE_ON(comboex)) COMBOEX_DumpInput (cit);
773
774 /* if item number requested does not exist then return failure */
775 if ((index >= infoPtr->nb_items) || (index < -1)) return FALSE;
776
777 /* if the item is the edit control and there is no edit control, skip */
778 if ((index == -1) && !COMBOEX_HasEdit(infoPtr)) return FALSE;
779
780 if (!(item = COMBOEX_FindItem(infoPtr, index))) return FALSE;
781
782 /* add/change stuff to the internal item structure */
783 item->mask |= cit->mask;
784 if (cit->mask & CBEIF_TEXT) {
785 INT len = 0;
786
787 COMBOEX_FreeText(item);
788 if (is_textW(cit->pszText)) len = strlenW(cit->pszText);
789 if (len > 0) {
790 item->pszText = (LPWSTR)Alloc ((len + 1)*sizeof(WCHAR));
791 if (!item->pszText) return FALSE;
792 strcpyW(item->pszText, cit->pszText);
793 } else if (cit->pszText == LPSTR_TEXTCALLBACKW)
794 item->pszText = LPSTR_TEXTCALLBACKW;
795 item->cchTextMax = cit->cchTextMax;
796 }
797 if (cit->mask & CBEIF_IMAGE)
798 item->iImage = cit->iImage;
799 if (cit->mask & CBEIF_SELECTEDIMAGE)
800 item->iSelectedImage = cit->iSelectedImage;
801 if (cit->mask & CBEIF_OVERLAY)
802 item->iOverlay = cit->iOverlay;
803 if (cit->mask & CBEIF_INDENT)
804 item->iIndent = cit->iIndent;
805 if (cit->mask & CBEIF_LPARAM)
806 cit->lParam = cit->lParam;
807
808 if (TRACE_ON(comboex)) COMBOEX_DumpItem (item);
809
810 /* if original request was to update edit control, do some fast foot work */
811 if (cit->iItem == -1) {
812 COMBOEX_SetEditText (infoPtr, item);
813 RedrawWindow (infoPtr->hwndCombo, 0, 0, RDW_ERASE | RDW_INVALIDATE);
814 }
815 return TRUE;
816 }
817
818 static BOOL COMBOEX_SetItemA (COMBOEX_INFO *infoPtr, COMBOBOXEXITEMA const *cit)
819 {
820 COMBOBOXEXITEMW citW;
821 LPWSTR wstr = NULL;
822 BOOL ret;
823
824 memcpy(&citW, cit, sizeof(COMBOBOXEXITEMA));
825 if ((cit->mask & CBEIF_TEXT) && is_textA(cit->pszText)) {
826 INT len = MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, NULL, 0);
827 wstr = (LPWSTR)Alloc ((len + 1)*sizeof(WCHAR));
828 if (!wstr) return FALSE;
829 MultiByteToWideChar (CP_ACP, 0, cit->pszText, -1, wstr, len);
830 citW.pszText = wstr;
831 }
832 ret = COMBOEX_SetItemW(infoPtr, &citW);
833
834 Free(wstr);
835
836 return ret;
837 }
838
839
840 static BOOL COMBOEX_SetUnicodeFormat (COMBOEX_INFO *infoPtr, BOOL value)
841 {
842 BOOL bTemp = infoPtr->unicode;
843
844 TRACE("to %s, was %s\n", value ? "TRUE":"FALSE", bTemp ? "TRUE":"FALSE");
845
846 infoPtr->unicode = value;
847
848 return bTemp;
849 }
850
851
852 /* *** CB_xxx message support *** */
853
854 static INT
855 COMBOEX_FindStringExact (COMBOEX_INFO *infoPtr, INT start, LPCWSTR str)
856 {
857 INT i;
858 cmp_func_t cmptext = get_cmp_func(infoPtr);
859 INT count = SendMessageW (infoPtr->hwndCombo, CB_GETCOUNT, 0, 0);
860
861 /* now search from after starting loc and wrapping back to start */
862 for(i=start+1; i<count; i++) {
863 CBE_ITEMDATA *item = get_item_data(infoPtr, i);
864 if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
865 }
866 for(i=0; i<=start; i++) {
867 CBE_ITEMDATA *item = get_item_data(infoPtr, i);
868 if (cmptext(COMBOEX_GetText(infoPtr, item), str) == 0) return i;
869 }
870 return CB_ERR;
871 }
872
873
874 static DWORD_PTR COMBOEX_GetItemData (COMBOEX_INFO *infoPtr, INT index)
875 {
876 CBE_ITEMDATA const *item1;
877 CBE_ITEMDATA const *item2;
878 DWORD_PTR ret = 0;
879
880 item1 = get_item_data(infoPtr, index);
881 if ((item1 != NULL) && ((LRESULT)item1 != CB_ERR)) {
882 item2 = COMBOEX_FindItem (infoPtr, index);
883 if (item2 != item1) {
884 ERR("data structures damaged!\n");
885 return CB_ERR;
886 }
887 if (item1->mask & CBEIF_LPARAM) ret = item1->lParam;
888 TRACE("returning 0x%08lx\n", ret);
889 } else {
890 ret = (DWORD_PTR)item1;
891 TRACE("non-valid result from combo, returning 0x%08lx\n", ret);
892 }
893 return ret;
894 }
895
896
897 static INT COMBOEX_SetCursel (COMBOEX_INFO *infoPtr, INT index)
898 {
899 CBE_ITEMDATA *item;
900 INT sel;
901
902 if (!(item = COMBOEX_FindItem(infoPtr, index)))
903 return SendMessageW (infoPtr->hwndCombo, CB_SETCURSEL, index, 0);
904
905 TRA