user32 revert lite ;-P
[reactos.git] / reactos / dll / win32 / user32 / controls / combo.c
1 /*
2 * Combo controls
3 *
4 * Copyright 1997 Alex Korobka
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * NOTES
21 *
22 * This code was audited for completeness against the documented features
23 * of Comctl32.dll version 6.0 on Oct. 4, 2004, by Dimitrie O. Paun.
24 *
25 * Unless otherwise noted, we believe this code to be complete, as per
26 * the specification mentioned above.
27 * If you discover missing features, or bugs, please note them below.
28 *
29 * TODO:
30 * - ComboBox_[GS]etMinVisible()
31 * - CB_GETMINVISIBLE, CB_SETMINVISIBLE
32 * - CB_SETTOPINDEX
33 */
34
35 #include <user32.h>
36
37 #include <wine/debug.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(combo);
39
40 /* bits in the dwKeyData */
41 #define KEYDATA_ALT 0x2000
42 #define KEYDATA_PREVSTATE 0x4000
43
44 /*
45 * Additional combo box definitions
46 */
47
48 #define CB_NOTIFY( lphc, code ) \
49 (SendMessageW((lphc)->owner, WM_COMMAND, \
50 MAKEWPARAM(GetWindowLongPtrW((lphc)->self,GWLP_ID), (code)), (LPARAM)(lphc)->self))
51
52 #define CB_DISABLED( lphc ) (!IsWindowEnabled((lphc)->self))
53 #define CB_OWNERDRAWN( lphc ) ((lphc)->dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
54 #define CB_HASSTRINGS( lphc ) ((lphc)->dwStyle & CBS_HASSTRINGS)
55 #define CB_HWND( lphc ) ((lphc)->self)
56 // ReactOS already define in include/controls.h We have it here as a sync note.
57 //#define CB_GETTYPE( lphc ) ((lphc)->dwStyle & (CBS_DROPDOWNLIST))
58
59 #define ISWIN31 (LOWORD(GetVersion()) == 0x0a03)
60
61 /*
62 * Drawing globals
63 */
64 static HBITMAP hComboBmp = 0;
65 static UINT CBitHeight, CBitWidth;
66
67 /*
68 * Look and feel dependent "constants"
69 */
70
71 #define COMBO_YBORDERGAP 5
72 #define COMBO_XBORDERSIZE() 2
73 #define COMBO_YBORDERSIZE() 2
74 #define COMBO_EDITBUTTONSPACE() 0
75 #define EDIT_CONTROL_PADDING() 1
76
77 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
78 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
79
80 /*********************************************************************
81 * combo class descriptor
82 */
83 const struct builtin_class_descr COMBO_builtin_class =
84 {
85 #ifdef __REACTOS__
86 L"ComboBox", /* name */
87 CS_PARENTDC | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS, /* style */
88 (WNDPROC) ComboWndProcW, /* procW */
89 (WNDPROC) ComboWndProcA, /* procA */
90 sizeof(HEADCOMBO *), /* extra */
91 (LPCWSTR) IDC_ARROW, /* cursor */
92 0 /* brush */
93 #else
94 "ComboBox", /* name */
95 CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, /* style */
96 ComboWndProcA, /* procA */
97 ComboWndProcW, /* procW */
98 sizeof(HEADCOMBO *), /* extra */
99 IDC_ARROW, /* cursor */
100 0 /* brush */
101 #endif
102 };
103
104
105 /***********************************************************************
106 * COMBO_Init
107 *
108 * Load combo button bitmap.
109 */
110 static BOOL COMBO_Init(void)
111 {
112 HDC hDC;
113
114 if( hComboBmp ) return TRUE;
115 if( (hDC = CreateCompatibleDC(0)) )
116 {
117 BOOL bRet = FALSE;
118 if( (hComboBmp = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_COMBO))) )
119 {
120 BITMAP bm;
121 HBITMAP hPrevB;
122 RECT r;
123
124 GetObjectW( hComboBmp, sizeof(bm), &bm );
125 CBitHeight = bm.bmHeight;
126 CBitWidth = bm.bmWidth;
127
128 TRACE("combo bitmap [%i,%i]\n", CBitWidth, CBitHeight );
129
130 hPrevB = SelectObject( hDC, hComboBmp);
131 SetRect( &r, 0, 0, CBitWidth, CBitHeight );
132 InvertRect( hDC, &r );
133 SelectObject( hDC, hPrevB );
134 bRet = TRUE;
135 }
136 DeleteDC( hDC );
137 return bRet;
138 }
139 return FALSE;
140 }
141
142
143 /* Retrieve the UI state for the control */
144 static BOOL COMBO_update_uistate(LPHEADCOMBO lphc)
145 {
146 LONG prev_flags;
147
148 prev_flags = lphc->UIState;
149 lphc->UIState = DefWindowProcW(lphc->self, WM_QUERYUISTATE, 0, 0);
150 return prev_flags != lphc->UIState;
151 }
152
153 /***********************************************************************
154 * COMBO_NCCreate
155 */
156 static LRESULT COMBO_NCCreate(HWND hwnd, LONG style)
157 {
158 LPHEADCOMBO lphc;
159
160 if (COMBO_Init() && (lphc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HEADCOMBO))) )
161 {
162 lphc->self = hwnd;
163 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)lphc );
164
165 COMBO_update_uistate(lphc);
166
167 /* some braindead apps do try to use scrollbar/border flags */
168
169 lphc->dwStyle = style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL);
170 SetWindowLongW( hwnd, GWL_STYLE, style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL) );
171
172 /*
173 * We also have to remove the client edge style to make sure
174 * we don't end-up with a non client area.
175 */
176 SetWindowLongW( hwnd, GWL_EXSTYLE,
177 GetWindowLongW( hwnd, GWL_EXSTYLE ) & ~WS_EX_CLIENTEDGE );
178
179 if( !(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) )
180 lphc->dwStyle |= CBS_HASSTRINGS;
181 if( !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) )
182 lphc->wState |= CBF_NOTIFY;
183
184 TRACE("[%p], style = %08x\n", lphc, lphc->dwStyle );
185 return TRUE;
186 }
187 return FALSE;
188 }
189
190 /***********************************************************************
191 * COMBO_NCDestroy
192 */
193 static LRESULT COMBO_NCDestroy( LPHEADCOMBO lphc )
194 {
195
196 if( lphc )
197 {
198 TRACE("[%p]: freeing storage\n", lphc->self);
199
200 if( (CB_GETTYPE(lphc) != CBS_SIMPLE) && lphc->hWndLBox )
201 DestroyWindow( lphc->hWndLBox );
202
203 SetWindowLongPtrW( lphc->self, 0, 0 );
204 HeapFree( GetProcessHeap(), 0, lphc );
205 }
206 return 0;
207 }
208
209 /***********************************************************************
210 * CBGetTextAreaHeight
211 *
212 * This method will calculate the height of the text area of the
213 * combobox.
214 * The height of the text area is set in two ways.
215 * It can be set explicitly through a combobox message or through a
216 * WM_MEASUREITEM callback.
217 * If this is not the case, the height is set to font height + 4px
218 * This height was determined through experimentation.
219 * CBCalcPlacement will add 2*COMBO_YBORDERSIZE pixels for the border
220 */
221 static INT CBGetTextAreaHeight(
222 HWND hwnd,
223 LPHEADCOMBO lphc)
224 {
225 INT iTextItemHeight;
226
227 if( lphc->editHeight ) /* explicitly set height */
228 {
229 iTextItemHeight = lphc->editHeight;
230 }
231 else
232 {
233 TEXTMETRICW tm;
234 HDC hDC = GetDC(hwnd);
235 HFONT hPrevFont = 0;
236 INT baseUnitY;
237
238 if (lphc->hFont)
239 hPrevFont = SelectObject( hDC, lphc->hFont );
240
241 GetTextMetricsW(hDC, &tm);
242
243 baseUnitY = tm.tmHeight;
244
245 if( hPrevFont )
246 SelectObject( hDC, hPrevFont );
247
248 ReleaseDC(hwnd, hDC);
249
250 iTextItemHeight = baseUnitY + 4;
251 }
252
253 /*
254 * Check the ownerdraw case if we haven't asked the parent the size
255 * of the item yet.
256 */
257 if ( CB_OWNERDRAWN(lphc) &&
258 (lphc->wState & CBF_MEASUREITEM) )
259 {
260 MEASUREITEMSTRUCT measureItem;
261 RECT clientRect;
262 INT originalItemHeight = iTextItemHeight;
263 UINT id = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
264
265 /*
266 * We use the client rect for the width of the item.
267 */
268 GetClientRect(hwnd, &clientRect);
269
270 lphc->wState &= ~CBF_MEASUREITEM;
271
272 /*
273 * Send a first one to measure the size of the text area
274 */
275 measureItem.CtlType = ODT_COMBOBOX;
276 measureItem.CtlID = id;
277 measureItem.itemID = -1;
278 measureItem.itemWidth = clientRect.right;
279 measureItem.itemHeight = iTextItemHeight - 6; /* ownerdrawn cb is taller */
280 measureItem.itemData = 0;
281 SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
282 iTextItemHeight = 6 + measureItem.itemHeight;
283
284 /*
285 * Send a second one in the case of a fixed ownerdraw list to calculate the
286 * size of the list items. (we basically do this on behalf of the listbox)
287 */
288 if (lphc->dwStyle & CBS_OWNERDRAWFIXED)
289 {
290 measureItem.CtlType = ODT_COMBOBOX;
291 measureItem.CtlID = id;
292 measureItem.itemID = 0;
293 measureItem.itemWidth = clientRect.right;
294 measureItem.itemHeight = originalItemHeight;
295 measureItem.itemData = 0;
296 SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
297 lphc->fixedOwnerDrawHeight = measureItem.itemHeight;
298 }
299
300 /*
301 * Keep the size for the next time
302 */
303 lphc->editHeight = iTextItemHeight;
304 }
305
306 return iTextItemHeight;
307 }
308
309 /***********************************************************************
310 * CBForceDummyResize
311 *
312 * The dummy resize is used for listboxes that have a popup to trigger
313 * a re-arranging of the contents of the combobox and the recalculation
314 * of the size of the "real" control window.
315 */
316 static void CBForceDummyResize(
317 LPHEADCOMBO lphc)
318 {
319 RECT windowRect;
320 int newComboHeight;
321
322 newComboHeight = CBGetTextAreaHeight(lphc->self,lphc) + 2*COMBO_YBORDERSIZE();
323
324 GetWindowRect(lphc->self, &windowRect);
325
326 /*
327 * We have to be careful, resizing a combobox also has the meaning that the
328 * dropped rect will be resized. In this case, we want to trigger a resize
329 * to recalculate layout but we don't want to change the dropped rectangle
330 * So, we pass the height of text area of control as the height.
331 * this will cancel-out in the processing of the WM_WINDOWPOSCHANGING
332 * message.
333 */
334 SetWindowPos( lphc->self,
335 NULL,
336 0, 0,
337 windowRect.right - windowRect.left,
338 newComboHeight,
339 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
340 }
341
342 /***********************************************************************
343 * CBCalcPlacement
344 *
345 * Set up component coordinates given valid lphc->RectCombo.
346 */
347 static void CBCalcPlacement(
348 HWND hwnd,
349 LPHEADCOMBO lphc,
350 LPRECT lprEdit,
351 LPRECT lprButton,
352 LPRECT lprLB)
353 {
354 /*
355 * Again, start with the client rectangle.
356 */
357 GetClientRect(hwnd, lprEdit);
358
359 /*
360 * Remove the borders
361 */
362 InflateRect(lprEdit, -COMBO_XBORDERSIZE(), -COMBO_YBORDERSIZE());
363
364 /*
365 * Chop off the bottom part to fit with the height of the text area.
366 */
367 lprEdit->bottom = lprEdit->top + CBGetTextAreaHeight(hwnd, lphc);
368
369 /*
370 * The button starts the same vertical position as the text area.
371 */
372 CopyRect(lprButton, lprEdit);
373
374 /*
375 * If the combobox is "simple" there is no button.
376 */
377 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
378 lprButton->left = lprButton->right = lprButton->bottom = 0;
379 else
380 {
381 /*
382 * Let's assume the combobox button is the same width as the
383 * scrollbar button.
384 * size the button horizontally and cut-off the text area.
385 */
386 lprButton->left = lprButton->right - GetSystemMetrics(SM_CXVSCROLL);
387 lprEdit->right = lprButton->left;
388 }
389
390 /*
391 * In the case of a dropdown, there is an additional spacing between the
392 * text area and the button.
393 */
394 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
395 {
396 lprEdit->right -= COMBO_EDITBUTTONSPACE();
397 }
398
399 /*
400 * If we have an edit control, we space it away from the borders slightly.
401 */
402 if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
403 {
404 InflateRect(lprEdit, -EDIT_CONTROL_PADDING(), -EDIT_CONTROL_PADDING());
405 }
406
407 /*
408 * Adjust the size of the listbox popup.
409 */
410 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
411 {
412 /*
413 * Use the client rectangle to initialize the listbox rectangle
414 */
415 GetClientRect(hwnd, lprLB);
416
417 /*
418 * Then, chop-off the top part.
419 */
420 lprLB->top = lprEdit->bottom + COMBO_YBORDERSIZE();
421 }
422 else
423 {
424 /*
425 * Make sure the dropped width is as large as the combobox itself.
426 */
427 if (lphc->droppedWidth < (lprButton->right + COMBO_XBORDERSIZE()))
428 {
429 lprLB->right = lprLB->left + (lprButton->right + COMBO_XBORDERSIZE());
430
431 /*
432 * In the case of a dropdown, the popup listbox is offset to the right.
433 * so, we want to make sure it's flush with the right side of the
434 * combobox
435 */
436 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
437 lprLB->right -= COMBO_EDITBUTTONSPACE();
438 }
439 else
440 lprLB->right = lprLB->left + lphc->droppedWidth;
441 }
442
443 /* don't allow negative window width */
444 if (lprEdit->right < lprEdit->left)
445 lprEdit->right = lprEdit->left;
446
447 TRACE("\ttext\t= (%ld,%ld-%ld,%ld)\n",
448 lprEdit->left, lprEdit->top, lprEdit->right, lprEdit->bottom);
449
450 TRACE("\tbutton\t= (%ld,%ld-%ld,%ld)\n",
451 lprButton->left, lprButton->top, lprButton->right, lprButton->bottom);
452
453 TRACE("\tlbox\t= (%ld,%ld-%ld,%ld)\n",
454 lprLB->left, lprLB->top, lprLB->right, lprLB->bottom );
455 }
456
457 /***********************************************************************
458 * CBGetDroppedControlRect
459 */
460 static void CBGetDroppedControlRect( LPHEADCOMBO lphc, LPRECT lpRect)
461 {
462 /* In windows, CB_GETDROPPEDCONTROLRECT returns the upper left corner
463 of the combo box and the lower right corner of the listbox */
464
465 GetWindowRect(lphc->self, lpRect);
466
467 lpRect->right = lpRect->left + lphc->droppedRect.right - lphc->droppedRect.left;
468 lpRect->bottom = lpRect->top + lphc->droppedRect.bottom - lphc->droppedRect.top;
469
470 }
471
472 /***********************************************************************
473 * COMBO_WindowPosChanging
474 */
475 static LRESULT COMBO_WindowPosChanging(
476 HWND hwnd,
477 LPHEADCOMBO lphc,
478 WINDOWPOS* posChanging)
479 {
480 /*
481 * We need to override the WM_WINDOWPOSCHANGING method to handle all
482 * the non-simple comboboxes. The problem is that those controls are
483 * always the same height. We have to make sure they are not resized
484 * to another value.
485 */
486 if ( ( CB_GETTYPE(lphc) != CBS_SIMPLE ) &&
487 ((posChanging->flags & SWP_NOSIZE) == 0) )
488 {
489 int newComboHeight;
490
491 newComboHeight = CBGetTextAreaHeight(hwnd,lphc) +
492 2*COMBO_YBORDERSIZE();
493
494 /*
495 * Resizing a combobox has another side effect, it resizes the dropped
496 * rectangle as well. However, it does it only if the new height for the
497 * combobox is different from the height it should have. In other words,
498 * if the application resizing the combobox only had the intention to resize
499 * the actual control, for example, to do the layout of a dialog that is
500 * resized, the height of the dropdown is not changed.
501 */
502 if (posChanging->cy != newComboHeight)
503 {
504 TRACE("posChanging->cy=%d, newComboHeight=%d, oldbot=%ld, oldtop=%ld\n",
505 posChanging->cy, newComboHeight, lphc->droppedRect.bottom,
506 lphc->droppedRect.top);
507 lphc->droppedRect.bottom = lphc->droppedRect.top + posChanging->cy - newComboHeight;
508
509 posChanging->cy = newComboHeight;
510 }
511 }
512
513 return 0;
514 }
515
516 /***********************************************************************
517 * COMBO_Create
518 */
519 static LRESULT COMBO_Create( HWND hwnd, LPHEADCOMBO lphc, HWND hwndParent, LONG style,
520 BOOL unicode )
521 {
522 static const WCHAR clbName[] = {'C','o','m','b','o','L','B','o','x',0};
523 static const WCHAR editName[] = {'E','d','i','t',0};
524
525 if( !CB_GETTYPE(lphc) ) lphc->dwStyle |= CBS_SIMPLE;
526 if( CB_GETTYPE(lphc) != CBS_DROPDOWNLIST ) lphc->wState |= CBF_EDIT;
527
528 lphc->owner = hwndParent;
529
530 /*
531 * The item height and dropped width are not set when the control
532 * is created.
533 */
534 lphc->droppedWidth = lphc->editHeight = 0;
535
536 /*
537 * The first time we go through, we want to measure the ownerdraw item
538 */
539 lphc->wState |= CBF_MEASUREITEM;
540
541 /* M$ IE 3.01 actually creates (and rapidly destroys) an ownerless combobox */
542
543 if( lphc->owner || !(style & WS_VISIBLE) )
544 {
545 UINT lbeStyle = 0;
546 UINT lbeExStyle = 0;
547
548 /*
549 * Initialize the dropped rect to the size of the client area of the
550 * control and then, force all the areas of the combobox to be
551 * recalculated.
552 */
553 GetClientRect( hwnd, &lphc->droppedRect );
554 CBCalcPlacement(hwnd, lphc, &lphc->textRect, &lphc->buttonRect, &lphc->droppedRect );
555
556 /*
557 * Adjust the position of the popup listbox if it's necessary
558 */
559 if ( CB_GETTYPE(lphc) != CBS_SIMPLE )
560 {
561 lphc->droppedRect.top = lphc->textRect.bottom + COMBO_YBORDERSIZE();
562
563 /*
564 * If it's a dropdown, the listbox is offset
565 */
566 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
567 lphc->droppedRect.left += COMBO_EDITBUTTONSPACE();
568
569 if (lphc->droppedRect.bottom < lphc->droppedRect.top)
570 lphc->droppedRect.bottom = lphc->droppedRect.top;
571 if (lphc->droppedRect.right < lphc->droppedRect.left)
572 lphc->droppedRect.right = lphc->droppedRect.left;
573 MapWindowPoints( hwnd, 0, (LPPOINT)&lphc->droppedRect, 2 );
574 }
575
576 /* create listbox popup */
577
578 lbeStyle = (LBS_NOTIFY | LBS_COMBOBOX | WS_BORDER | WS_CLIPSIBLINGS | WS_CHILD) |
579 (style & (WS_VSCROLL | CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE));
580
581 if( lphc->dwStyle & CBS_SORT )
582 lbeStyle |= LBS_SORT;
583 if( lphc->dwStyle & CBS_HASSTRINGS )
584 lbeStyle |= LBS_HASSTRINGS;
585 if( lphc->dwStyle & CBS_NOINTEGRALHEIGHT )
586 lbeStyle |= LBS_NOINTEGRALHEIGHT;
587 if( lphc->dwStyle & CBS_DISABLENOSCROLL )
588 lbeStyle |= LBS_DISABLENOSCROLL;
589
590 if( CB_GETTYPE(lphc) == CBS_SIMPLE ) /* child listbox */
591 {
592 lbeStyle |= WS_VISIBLE;
593
594 /*
595 * In win 95 look n feel, the listbox in the simple combobox has
596 * the WS_EXCLIENTEDGE style instead of the WS_BORDER style.
597 */
598 lbeStyle &= ~WS_BORDER;
599 lbeExStyle |= WS_EX_CLIENTEDGE;
600 }
601 else
602 {
603 lbeExStyle |= (WS_EX_TOPMOST | WS_EX_TOOLWINDOW);
604 }
605
606 if (unicode)
607 lphc->hWndLBox = CreateWindowExW(lbeExStyle, clbName, NULL, lbeStyle,
608 lphc->droppedRect.left,
609 lphc->droppedRect.top,
610 lphc->droppedRect.right - lphc->droppedRect.left,
611 lphc->droppedRect.bottom - lphc->droppedRect.top,
612 hwnd, (HMENU)ID_CB_LISTBOX,
613 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
614 else
615 lphc->hWndLBox = CreateWindowExA(lbeExStyle, "ComboLBox", NULL, lbeStyle,
616 lphc->droppedRect.left,
617 lphc->droppedRect.top,
618 lphc->droppedRect.right - lphc->droppedRect.left,
619 lphc->droppedRect.bottom - lphc->droppedRect.top,
620 hwnd, (HMENU)ID_CB_LISTBOX,
621 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
622
623 if( lphc->hWndLBox )
624 {
625 BOOL bEdit = TRUE;
626 lbeStyle = WS_CHILD | WS_VISIBLE | ES_NOHIDESEL | ES_LEFT | ES_COMBO;
627
628 if( lphc->wState & CBF_EDIT )
629 {
630 if( lphc->dwStyle & CBS_OEMCONVERT )
631 lbeStyle |= ES_OEMCONVERT;
632 if( lphc->dwStyle & CBS_AUTOHSCROLL )
633 lbeStyle |= ES_AUTOHSCROLL;
634 if( lphc->dwStyle & CBS_LOWERCASE )
635 lbeStyle |= ES_LOWERCASE;
636 else if( lphc->dwStyle & CBS_UPPERCASE )
637 lbeStyle |= ES_UPPERCASE;
638
639 if (!IsWindowEnabled(hwnd)) lbeStyle |= WS_DISABLED;
640
641 if (unicode)
642 lphc->hWndEdit = CreateWindowExW(0, editName, NULL, lbeStyle,
643 lphc->textRect.left, lphc->textRect.top,
644 lphc->textRect.right - lphc->textRect.left,
645 lphc->textRect.bottom - lphc->textRect.top,
646 hwnd, (HMENU)ID_CB_EDIT,
647 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
648 else
649 lphc->hWndEdit = CreateWindowExA(0, "Edit", NULL, lbeStyle,
650 lphc->textRect.left, lphc->textRect.top,
651 lphc->textRect.right - lphc->textRect.left,
652 lphc->textRect.bottom - lphc->textRect.top,
653 hwnd, (HMENU)ID_CB_EDIT,
654 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
655
656 if( !lphc->hWndEdit )
657 bEdit = FALSE;
658 }
659
660 if( bEdit )
661 {
662 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
663 {
664 /* Now do the trick with parent */
665 SetParent(lphc->hWndLBox, HWND_DESKTOP);
666 /*
667 * If the combo is a dropdown, we must resize the control
668 * to fit only the text area and button. To do this,
669 * we send a dummy resize and the WM_WINDOWPOSCHANGING message
670 * will take care of setting the height for us.
671 */
672 CBForceDummyResize(lphc);
673 }
674
675 TRACE("init done\n");
676 return 0;
677 }
678 ERR("edit control failure.\n");
679 } else ERR("listbox failure.\n");
680 } else ERR("no owner for visible combo.\n");
681
682 /* CreateWindow() will send WM_NCDESTROY to cleanup */
683
684 return -1;
685 }
686
687 /***********************************************************************
688 * CBPaintButton
689 *
690 * Paint combo button (normal, pressed, and disabled states).
691 */
692 static void CBPaintButton( LPHEADCOMBO lphc, HDC hdc, RECT rectButton)
693 {
694 UINT buttonState = DFCS_SCROLLCOMBOBOX;
695
696 if( lphc->wState & CBF_NOREDRAW )
697 return;
698
699
700 if (lphc->wState & CBF_BUTTONDOWN)
701 buttonState |= DFCS_PUSHED;
702
703 if (CB_DISABLED(lphc))
704 buttonState |= DFCS_INACTIVE;
705
706 DrawFrameControl(hdc, &rectButton, DFC_SCROLL, buttonState);
707 }
708
709 /***********************************************************************
710 * CBPaintText
711 *
712 * Paint CBS_DROPDOWNLIST text field / update edit control contents.
713 */
714 static void CBPaintText(
715 LPHEADCOMBO lphc,
716 HDC hdc,
717 RECT rectEdit)
718 {
719 INT id, size = 0;
720 LPWSTR pText = NULL;
721
722 if( lphc->wState & CBF_NOREDRAW ) return;
723
724 TRACE("\n");
725
726 /* follow Windows combobox that sends a bunch of text
727 * inquiries to its listbox while processing WM_PAINT. */
728
729 if( (id = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0) ) != LB_ERR )
730 {
731 size = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, id, 0);
732 if (size == LB_ERR)
733 FIXME("LB_ERR probably not handled yet\n");
734 if( (pText = HeapAlloc( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))) )
735 {
736 /* size from LB_GETTEXTLEN may be too large, from LB_GETTEXT is accurate */
737 size=SendMessageW(lphc->hWndLBox, LB_GETTEXT, (WPARAM)id, (LPARAM)pText);
738 pText[size] = '\0'; /* just in case */
739 } else return;
740 }
741 else
742 if( !CB_OWNERDRAWN(lphc) )
743 return;
744
745 if( lphc->wState & CBF_EDIT )
746 {
747 static const WCHAR empty_stringW[] = { 0 };
748 if( CB_HASSTRINGS(lphc) ) SetWindowTextW( lphc->hWndEdit, pText ? pText : empty_stringW );
749 if( lphc->wState & CBF_FOCUSED )
750 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
751 }
752 else /* paint text field ourselves */
753 {
754 UINT itemState = ODS_COMBOBOXEDIT;
755 HFONT hPrevFont = (lphc->hFont) ? SelectObject(hdc, lphc->hFont) : 0;
756
757 /*
758 * Give ourselves some space.
759 */
760 InflateRect( &rectEdit, -1, -1 );
761
762 if( CB_OWNERDRAWN(lphc) )
763 {
764 DRAWITEMSTRUCT dis;
765 HRGN clipRegion;
766 UINT ctlid = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
767
768 /* setup state for DRAWITEM message. Owner will highlight */
769 if ( (lphc->wState & CBF_FOCUSED) &&
770 !(lphc->wState & CBF_DROPPED) )
771 itemState |= ODS_SELECTED | ODS_FOCUS;
772
773 /*
774 * Save the current clip region.
775 * To retrieve the clip region, we need to create one "dummy"
776 * clip region.
777 */
778 clipRegion = CreateRectRgnIndirect(&rectEdit);
779
780 if (GetClipRgn(hdc, clipRegion)!=1)
781 {
782 DeleteObject(clipRegion);
783 clipRegion=NULL;
784 }
785
786 if (!(IsWindowEnabled(lphc->self) & WS_DISABLED)) itemState |= ODS_DISABLED;
787
788 dis.CtlType = ODT_COMBOBOX;
789 dis.CtlID = ctlid;
790 dis.hwndItem = lphc->self;
791 dis.itemAction = ODA_DRAWENTIRE;
792 dis.itemID = id;
793 dis.itemState = itemState;
794 dis.hDC = hdc;
795 dis.rcItem = rectEdit;
796 dis.itemData = SendMessageW(lphc->hWndLBox, LB_GETITEMDATA,
797 (WPARAM)id, 0 );
798
799 /*
800 * Clip the DC and have the parent draw the item.
801 */
802 IntersectClipRect(hdc,
803 rectEdit.left, rectEdit.top,
804 rectEdit.right, rectEdit.bottom);
805
806 SendMessageW(lphc->owner, WM_DRAWITEM, ctlid, (LPARAM)&dis );
807
808 /*
809 * Reset the clipping region.
810 */
811 SelectClipRgn(hdc, clipRegion);
812 }
813 else
814 {
815 static const WCHAR empty_stringW[] = { 0 };
816
817 if ( (lphc->wState & CBF_FOCUSED) &&
818 !(lphc->wState & CBF_DROPPED) ) {
819
820 /* highlight */
821 FillRect( hdc, &rectEdit, GetSysColorBrush(COLOR_HIGHLIGHT) );
822 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
823 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
824 }
825
826 ExtTextOutW( hdc,
827 rectEdit.left + 1,
828 rectEdit.top + 1,
829 ETO_OPAQUE | ETO_CLIPPED,
830 &rectEdit,
831 pText ? pText : empty_stringW , size, NULL );
832
833 if(lphc->wState & CBF_FOCUSED && !(lphc->wState & CBF_DROPPED) &&
834 !(lphc->UIState & UISF_HIDEFOCUS))
835 DrawFocusRect( hdc, &rectEdit );
836 }
837
838 if( hPrevFont )
839 SelectObject(hdc, hPrevFont );
840 }
841 if (pText)
842 HeapFree( GetProcessHeap(), 0, pText );
843 }
844
845 /***********************************************************************
846 * CBPaintBorder
847 */
848 static void CBPaintBorder(
849 HWND hwnd,
850 const HEADCOMBO *lphc,
851 HDC hdc)
852 {
853 RECT clientRect;
854
855 if (CB_GETTYPE(lphc) != CBS_SIMPLE)
856 {
857 GetClientRect(hwnd, &clientRect);
858 }
859 else
860 {
861 CopyRect(&clientRect, &lphc->textRect);
862
863 InflateRect(&clientRect, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
864 InflateRect(&clientRect, COMBO_XBORDERSIZE(), COMBO_YBORDERSIZE());
865 }
866
867 DrawEdge(hdc, &clientRect, EDGE_SUNKEN, BF_RECT);
868 }
869
870 /***********************************************************************
871 * COMBO_PrepareColors
872 *
873 * This method will sent the appropriate WM_CTLCOLOR message to
874 * prepare and setup the colors for the combo's DC.
875 *
876 * It also returns the brush to use for the background.
877 */
878 static HBRUSH COMBO_PrepareColors(
879 LPHEADCOMBO lphc,
880 HDC hDC)
881 {
882 HBRUSH hBkgBrush;
883
884 /*
885 * Get the background brush for this control.
886 */
887 if (CB_DISABLED(lphc))
888 {
889 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORSTATIC,
890 (WPARAM)hDC, (LPARAM)lphc->self );
891
892 /*
893 * We have to change the text color since WM_CTLCOLORSTATIC will
894 * set it to the "enabled" color. This is the same behavior as the
895 * edit control
896 */
897 SetTextColor(hDC, GetSysColor(COLOR_GRAYTEXT));
898 }
899 else
900 {
901 if (lphc->wState & CBF_EDIT)
902 {
903 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLOREDIT,
904 (WPARAM)hDC, (LPARAM)lphc->self );
905 }
906 else
907 {
908 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORLISTBOX,
909 (WPARAM)hDC, (LPARAM)lphc->self );
910 }
911 }
912
913 /*
914 * Catch errors.
915 */
916 if( !hBkgBrush )
917 hBkgBrush = GetSysColorBrush(COLOR_WINDOW);
918
919 return hBkgBrush;
920 }
921
922
923 /***********************************************************************
924 * COMBO_Paint
925 */
926 static LRESULT COMBO_Paint(LPHEADCOMBO lphc, HDC hParamDC)
927 {
928 PAINTSTRUCT ps;
929 HDC hDC;
930
931 hDC = (hParamDC) ? hParamDC
932 : BeginPaint( lphc->self, &ps);
933
934 TRACE("hdc=%p\n", hDC);
935
936 if( hDC && !(lphc->wState & CBF_NOREDRAW) )
937 {
938 HBRUSH hPrevBrush, hBkgBrush;
939
940 /*
941 * Retrieve the background brush and select it in the
942 * DC.
943 */
944 hBkgBrush = COMBO_PrepareColors(lphc, hDC);
945
946 hPrevBrush = SelectObject( hDC, hBkgBrush );
947 if (!(lphc->wState & CBF_EDIT))
948 FillRect(hDC, &lphc->textRect, hBkgBrush);
949
950 /*
951 * In non 3.1 look, there is a sunken border on the combobox
952 */
953 CBPaintBorder(lphc->self, lphc, hDC);
954
955 if( !IsRectEmpty(&lphc->buttonRect) )
956 {
957 CBPaintButton(lphc, hDC, lphc->buttonRect);
958 }
959
960 /* paint the edit control padding area */
961 if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
962 {
963 RECT rPadEdit = lphc->textRect;
964
965 InflateRect(&rPadEdit, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
966
967 FrameRect( hDC, &rPadEdit, GetSysColorBrush(COLOR_WINDOW) );
968 }
969
970 if( !(lphc->wState & CBF_EDIT) )
971 CBPaintText( lphc, hDC, lphc->textRect);
972
973 if( hPrevBrush )
974 SelectObject( hDC, hPrevBrush );
975 }
976
977 if( !hParamDC )
978 EndPaint(lphc->self, &ps);
979
980 return 0;
981 }
982
983 /***********************************************************************
984 * CBUpdateLBox
985 *
986 * Select listbox entry according to the contents of the edit control.
987 */
988 static INT CBUpdateLBox( LPHEADCOMBO lphc, BOOL bSelect )
989 {
990 INT length, idx;
991 LPWSTR pText = NULL;
992
993 idx = LB_ERR;
994 length = SendMessageW( lphc->hWndEdit, WM_GETTEXTLENGTH, 0, 0 );
995
996 if( length > 0 )
997 pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
998
999 TRACE("\t edit text length %i\n", length );
1000
1001 if( pText )
1002 {
1003 GetWindowTextW( lphc->hWndEdit, pText, length + 1);
1004 idx = SendMessageW(lphc->hWndLBox, LB_FINDSTRING,
1005 (WPARAM)(-1), (LPARAM)pText );
1006 HeapFree( GetProcessHeap(), 0, pText );
1007 }
1008
1009 SendMessageW(lphc->hWndLBox, LB_SETCURSEL, (WPARAM)(bSelect ? idx : -1), 0);
1010
1011 /* probably superfluous but Windows sends this too */
1012 SendMessageW(lphc->hWndLBox, LB_SETCARETINDEX, (WPARAM)(idx < 0 ? 0 : idx), 0);
1013 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, (WPARAM)(idx < 0 ? 0 : idx), 0);
1014
1015 return idx;
1016 }
1017
1018 /***********************************************************************
1019 * CBUpdateEdit
1020 *
1021 * Copy a listbox entry to the edit control.
1022 */
1023 static void CBUpdateEdit( LPHEADCOMBO lphc , INT index )
1024 {
1025 INT length;
1026 LPWSTR pText = NULL;
1027 static const WCHAR empty_stringW[] = { 0 };
1028
1029 TRACE("\t %i\n", index );
1030
1031 if( index >= 0 ) /* got an entry */
1032 {
1033 length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, (WPARAM)index, 0);
1034 if( length != LB_ERR)
1035 {
1036 if( (pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR))) )
1037 {
1038 SendMessageW(lphc->hWndLBox, LB_GETTEXT,
1039 (WPARAM)index, (LPARAM)pText );
1040 }
1041 }
1042 }
1043
1044 if( CB_HASSTRINGS(lphc) )
1045 {
1046 lphc->wState |= (CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
1047 SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, pText ? (LPARAM)pText : (LPARAM)empty_stringW);
1048 lphc->wState &= ~(CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
1049 }
1050
1051 if( lphc->wState & CBF_FOCUSED )
1052 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
1053
1054 HeapFree( GetProcessHeap(), 0, pText );
1055 }
1056
1057 /***********************************************************************
1058 * CBDropDown
1059 *
1060 * Show listbox popup.
1061 */
1062 static void CBDropDown( LPHEADCOMBO lphc )
1063 {
1064 HMONITOR monitor;
1065 MONITORINFO mon_info;
1066 RECT rect,r;
1067 int nItems = 0;
1068 int nDroppedHeight;
1069
1070 TRACE("[%p]: drop down\n", lphc->self);
1071
1072 CB_NOTIFY( lphc, CBN_DROPDOWN );
1073
1074 /* set selection */
1075
1076 lphc->wState |= CBF_DROPPED;
1077 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1078 {
1079 lphc->droppedIndex = CBUpdateLBox( lphc, TRUE );
1080
1081 /* Update edit only if item is in the list */
1082 if( !(lphc->wState & CBF_CAPTURE) && lphc->droppedIndex >= 0)
1083 CBUpdateEdit( lphc, lphc->droppedIndex );
1084 }
1085 else
1086 {
1087 lphc->droppedIndex = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1088
1089 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX,
1090 (WPARAM)(lphc->droppedIndex == LB_ERR ? 0 : lphc->droppedIndex), 0 );
1091 SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1092 }
1093
1094 /* now set popup position */
1095 GetWindowRect( lphc->self, &rect );
1096
1097 /*
1098 * If it's a dropdown, the listbox is offset
1099 */
1100 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1101 rect.left += COMBO_EDITBUTTONSPACE();
1102
1103 /* if the dropped height is greater than the total height of the dropped
1104 items list, then force the drop down list height to be the total height
1105 of the items in the dropped list */
1106
1107 /* And Remove any extra space (Best Fit) */
1108 nDroppedHeight = lphc->droppedRect.bottom - lphc->droppedRect.top;
1109 /* if listbox length has been set directly by its handle */
1110 GetWindowRect(lphc->hWndLBox, &r);
1111 if (nDroppedHeight < r.bottom - r.top)
1112 nDroppedHeight = r.bottom - r.top;
1113 nItems = (int)SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
1114
1115 if (nItems > 0)
1116 {
1117 int nHeight;
1118 int nIHeight;
1119
1120 nIHeight = (int)SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, 0, 0);
1121
1122 nHeight = nIHeight*nItems;
1123
1124 if (nHeight < nDroppedHeight - COMBO_YBORDERSIZE())
1125 nDroppedHeight = nHeight + COMBO_YBORDERSIZE();
1126
1127 if (nDroppedHeight < nHeight)
1128 {
1129 if (nItems < 5)
1130 nDroppedHeight = (nItems+1)*nIHeight;
1131 else
1132 nDroppedHeight = 6*nIHeight;
1133 }
1134 }
1135
1136 /*If height of dropped rectangle gets beyond a screen size it should go up, otherwise down.*/
1137 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
1138 mon_info.cbSize = sizeof(mon_info);
1139 GetMonitorInfoW( monitor, &mon_info );
1140
1141 if( (rect.bottom + nDroppedHeight) >= mon_info.rcWork.bottom )
1142 rect.bottom = rect.top - nDroppedHeight;
1143
1144 SetWindowPos( lphc->hWndLBox, HWND_TOP, rect.left, rect.bottom,
1145 lphc->droppedRect.right - lphc->droppedRect.left,
1146 nDroppedHeight,
1147 SWP_NOACTIVATE | SWP_SHOWWINDOW);
1148
1149
1150 if( !(lphc->wState & CBF_NOREDRAW) )
1151 RedrawWindow( lphc->self, NULL, 0, RDW_INVALIDATE |
1152 RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1153
1154 EnableWindow( lphc->hWndLBox, TRUE );
1155 if (GetCapture() != lphc->self)
1156 SetCapture(lphc->hWndLBox);
1157 }
1158
1159 /***********************************************************************
1160 * CBRollUp
1161 *
1162 * Hide listbox popup.
1163 */
1164 static void CBRollUp( LPHEADCOMBO lphc, BOOL ok, BOOL bButton )
1165 {
1166 HWND hWnd = lphc->self;
1167
1168 TRACE("[%p]: sel ok? [%i] dropped? [%i]\n",
1169 lphc->self, ok, (INT)(lphc->wState & CBF_DROPPED));
1170
1171 CB_NOTIFY( lphc, (ok) ? CBN_SELENDOK : CBN_SELENDCANCEL );
1172
1173 if( IsWindow( hWnd ) && CB_GETTYPE(lphc) != CBS_SIMPLE )
1174 {
1175
1176 if( lphc->wState & CBF_DROPPED )
1177 {
1178 RECT rect;
1179
1180 lphc->wState &= ~CBF_DROPPED;
1181 ShowWindow( lphc->hWndLBox, SW_HIDE );
1182
1183 if(GetCapture() == lphc->hWndLBox)
1184 {
1185 ReleaseCapture();
1186 }
1187
1188 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1189 {
1190 rect = lphc->buttonRect;
1191 }
1192 else
1193 {
1194 if( bButton )
1195 {
1196 UnionRect( &rect,
1197 &lphc->buttonRect,
1198 &lphc->textRect);
1199 }
1200 else
1201 rect = lphc->textRect;
1202
1203 bButton = TRUE;
1204 }
1205
1206 if( bButton && !(lphc->wState & CBF_NOREDRAW) )
1207 RedrawWindow( hWnd, &rect, 0, RDW_INVALIDATE |
1208 RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1209 CB_NOTIFY( lphc, CBN_CLOSEUP );
1210 }
1211 }
1212 }
1213
1214 /***********************************************************************
1215 * COMBO_FlipListbox
1216 *
1217 * Used by the ComboLBox to show/hide itself in response to VK_F4, etc...
1218 */
1219 BOOL COMBO_FlipListbox( LPHEADCOMBO lphc, BOOL ok, BOOL bRedrawButton )
1220 {
1221 if( lphc->wState & CBF_DROPPED )
1222 {
1223 CBRollUp( lphc, ok, bRedrawButton );
1224 return FALSE;
1225 }
1226
1227 CBDropDown( lphc );
1228 return TRUE;
1229 }
1230
1231 /***********************************************************************
1232 * CBRepaintButton
1233 */
1234 static void CBRepaintButton( LPHEADCOMBO lphc )
1235 {
1236 InvalidateRect(lphc->self, &lphc->buttonRect, TRUE);
1237 UpdateWindow(lphc->self);
1238 }
1239
1240 /***********************************************************************
1241 * COMBO_SetFocus
1242 */
1243 static void COMBO_SetFocus( LPHEADCOMBO lphc )
1244 {
1245 if( !(lphc->wState & CBF_FOCUSED) )
1246 {
1247 if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1248 SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1249
1250 /* This is wrong. Message sequences seem to indicate that this
1251 is set *after* the notify. */
1252 /* lphc->wState |= CBF_FOCUSED; */
1253
1254 if( !(lphc->wState & CBF_EDIT) )
1255 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1256
1257 CB_NOTIFY( lphc, CBN_SETFOCUS );
1258 lphc->wState |= CBF_FOCUSED;
1259 }
1260 }
1261
1262 /***********************************************************************
1263 * COMBO_KillFocus
1264 */
1265 static void COMBO_KillFocus( LPHEADCOMBO lphc )
1266 {
1267 HWND hWnd = lphc->self;
1268
1269 if( lphc->wState & CBF_FOCUSED )
1270 {
1271 CBRollUp( lphc, FALSE, TRUE );
1272 if( IsWindow( hWnd ) )
1273 {
1274 if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1275 SendMessageW(lphc->hWndLBox, LB_CARETOFF, 0, 0);
1276
1277 lphc->wState &= ~CBF_FOCUSED;
1278
1279 /* redraw text */
1280 if( !(lphc->wState & CBF_EDIT) )
1281 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1282
1283 CB_NOTIFY( lphc, CBN_KILLFOCUS );
1284 }
1285 }
1286 }
1287
1288 /***********************************************************************
1289 * COMBO_Command
1290 */
1291 static LRESULT COMBO_Command( LPHEADCOMBO lphc, WPARAM wParam, HWND hWnd )
1292 {
1293 if ( lphc->wState & CBF_EDIT && lphc->hWndEdit == hWnd )
1294 {
1295 /* ">> 8" makes gcc generate jump-table instead of cmp ladder */
1296
1297 switch( HIWORD(wParam) >> 8 )
1298 {
1299 case (EN_SETFOCUS >> 8):
1300
1301 TRACE("[%p]: edit [%p] got focus\n", lphc->self, lphc->hWndEdit );
1302
1303 COMBO_SetFocus( lphc );
1304 break;
1305
1306 case (EN_KILLFOCUS >> 8):
1307
1308 TRACE("[%p]: edit [%p] lost focus\n", lphc->self, lphc->hWndEdit );
1309
1310 /* NOTE: it seems that Windows' edit control sends an
1311 * undocumented message WM_USER + 0x1B instead of this
1312 * notification (only when it happens to be a part of
1313 * the combo). ?? - AK.
1314 */
1315
1316 COMBO_KillFocus( lphc );
1317 break;
1318
1319
1320 case (EN_CHANGE >> 8):
1321 /*
1322 * In some circumstances (when the selection of the combobox
1323 * is changed for example) we don't want the EN_CHANGE notification
1324 * to be forwarded to the parent of the combobox. This code
1325 * checks a flag that is set in these occasions and ignores the
1326 * notification.
1327 */
1328 if (lphc->wState & CBF_NOLBSELECT)
1329 {
1330 lphc->wState &= ~CBF_NOLBSELECT;
1331 }
1332 else
1333 {
1334 CBUpdateLBox( lphc, lphc->wState & CBF_DROPPED );
1335 }
1336
1337 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1338 CB_NOTIFY( lphc, CBN_EDITCHANGE );
1339 break;
1340
1341 case (EN_UPDATE >> 8):
1342 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1343 CB_NOTIFY( lphc, CBN_EDITUPDATE );
1344 break;
1345
1346 case (EN_ERRSPACE >> 8):
1347 CB_NOTIFY( lphc, CBN_ERRSPACE );
1348 }
1349 }
1350 else if( lphc->hWndLBox == hWnd )
1351 {
1352 switch( (short)HIWORD(wParam) )
1353 {
1354 case LBN_ERRSPACE:
1355 CB_NOTIFY( lphc, CBN_ERRSPACE );
1356 break;
1357
1358 case LBN_DBLCLK:
1359 CB_NOTIFY( lphc, CBN_DBLCLK );
1360 break;
1361
1362 case LBN_SELCHANGE:
1363 case LBN_SELCANCEL:
1364
1365 TRACE("[%p]: lbox selection change [%x]\n", lphc->self, lphc->wState );
1366
1367 CB_NOTIFY( lphc, CBN_SELCHANGE );
1368
1369 if( HIWORD(wParam) == LBN_SELCHANGE)
1370 {
1371 if( lphc->wState & CBF_EDIT )
1372 {
1373 INT index = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1374 lphc->wState |= CBF_NOLBSELECT;
1375 CBUpdateEdit( lphc, index );
1376 /* select text in edit, as Windows does */
1377 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
1378 }
1379 else
1380 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1381 }
1382
1383 /* do not roll up if selection is being tracked
1384 * by arrowkeys in the dropdown listbox */
1385 if( ((lphc->wState & CBF_DROPPED) && !(lphc->wState & CBF_NOROLLUP)) )
1386 {
1387 CBRollUp( lphc, (HIWORD(wParam) == LBN_SELCHANGE), TRUE );
1388 }
1389 else lphc->wState &= ~CBF_NOROLLUP;
1390
1391 break;
1392
1393 case LBN_SETFOCUS:
1394 case LBN_KILLFOCUS:
1395 /* nothing to do here since ComboLBox always resets the focus to its
1396 * combo/edit counterpart */
1397 break;
1398 }
1399 }
1400 return 0;
1401 }
1402
1403 /***********************************************************************
1404 * COMBO_ItemOp
1405 *
1406 * Fixup an ownerdrawn item operation and pass it up to the combobox owner.
1407 */
1408 static LRESULT COMBO_ItemOp( LPHEADCOMBO lphc, UINT msg, LPARAM lParam )
1409 {
1410 HWND hWnd = lphc->self;
1411 UINT id = (UINT)GetWindowLongPtrW( hWnd, GWLP_ID );
1412
1413 TRACE("[%p]: ownerdraw op %04x\n", lphc->self, msg );
1414
1415 switch( msg )
1416 {
1417 case WM_DELETEITEM:
1418 {
1419 DELETEITEMSTRUCT *lpIS = (DELETEITEMSTRUCT *)lParam;
1420 lpIS->CtlType = ODT_COMBOBOX;
1421 lpIS->CtlID = id;
1422 lpIS->hwndItem = hWnd;
1423 break;
1424 }
1425 case WM_DRAWITEM:
1426 {
1427 DRAWITEMSTRUCT *lpIS = (DRAWITEMSTRUCT *)lParam;
1428 lpIS->CtlType = ODT_COMBOBOX;
1429 lpIS->CtlID = id;
1430 lpIS->hwndItem = hWnd;
1431 break;
1432 }
1433 case WM_COMPAREITEM:
1434 {
1435 COMPAREITEMSTRUCT *lpIS = (COMPAREITEMSTRUCT *)lParam;
1436 lpIS->CtlType = ODT_COMBOBOX;
1437 lpIS->CtlID = id;
1438 lpIS->hwndItem = hWnd;
1439 break;
1440 }
1441 case WM_MEASUREITEM:
1442 {
1443 MEASUREITEMSTRUCT *lpIS = (MEASUREITEMSTRUCT *)lParam;
1444 lpIS->CtlType = ODT_COMBOBOX;
1445 lpIS->CtlID = id;
1446 break;
1447 }
1448 }
1449 return SendMessageW(lphc->owner, msg, id, lParam);
1450 }
1451
1452
1453 /***********************************************************************
1454 * COMBO_GetTextW
1455 */
1456 static LRESULT COMBO_GetTextW( LPHEADCOMBO lphc, INT count, LPWSTR buf )
1457 {
1458 INT length;
1459
1460 if( lphc->wState & CBF_EDIT )
1461 return SendMessageW( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1462
1463 /* get it from the listbox */
1464
1465 if (!count || !buf) return 0;
1466 if( lphc->hWndLBox )
1467 {
1468 INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1469 if (idx == LB_ERR) goto error;
1470 length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1471 if (length == LB_ERR) goto error;
1472
1473 /* 'length' is without the terminating character */
1474 if (length >= count)
1475 {
1476 LPWSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
1477 if (!lpBuffer) goto error;
1478 length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1479
1480 /* truncate if buffer is too short */
1481 if (length != LB_ERR)
1482 {
1483 lstrcpynW( buf, lpBuffer, count );
1484 length = count;
1485 }
1486 HeapFree( GetProcessHeap(), 0, lpBuffer );
1487 }
1488 else length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1489
1490 if (length == LB_ERR) return 0;
1491 return length;
1492 }
1493
1494 error: /* error - truncate string, return zero */
1495 buf[0] = 0;
1496 return 0;
1497 }
1498
1499
1500 /***********************************************************************
1501 * COMBO_GetTextA
1502 *
1503 * NOTE! LB_GETTEXT does not count terminating \0, WM_GETTEXT does.
1504 * also LB_GETTEXT might return values < 0, WM_GETTEXT doesn't.
1505 */
1506 static LRESULT COMBO_GetTextA( LPHEADCOMBO lphc, INT count, LPSTR buf )
1507 {
1508 INT length;
1509
1510 if( lphc->wState & CBF_EDIT )
1511 return SendMessageA( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1512
1513 /* get it from the listbox */
1514
1515 if (!count || !buf) return 0;
1516 if( lphc->hWndLBox )
1517 {
1518 INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1519 if (idx == LB_ERR) goto error;
1520 length = SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1521 if (length == LB_ERR) goto error;
1522
1523 /* 'length' is without the terminating character */
1524 if (length >= count)
1525 {
1526 LPSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) );
1527 if (!lpBuffer) goto error;
1528 length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1529
1530 /* truncate if buffer is too short */
1531 if (length != LB_ERR)
1532 {
1533 lstrcpynA( buf, lpBuffer, count );
1534 length = count;
1535 }
1536 HeapFree( GetProcessHeap(), 0, lpBuffer );
1537 }
1538 else length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1539
1540 if (length == LB_ERR) return 0;
1541 return length;
1542 }
1543
1544 error: /* error - truncate string, return zero */
1545 buf[0] = 0;
1546 return 0;
1547 }
1548
1549
1550 /***********************************************************************
1551 * CBResetPos
1552 *
1553 * This function sets window positions according to the updated
1554 * component placement struct.
1555 */
1556 static void CBResetPos(
1557 LPHEADCOMBO lphc,
1558 const RECT *rectEdit,
1559 const RECT *rectLB,
1560 BOOL bRedraw)
1561 {
1562 BOOL bDrop = (CB_GETTYPE(lphc) != CBS_SIMPLE);
1563
1564 /* NOTE: logs sometimes have WM_LBUTTONUP before a cascade of
1565 * sizing messages */
1566
1567 if( lphc->wState & CBF_EDIT )
1568 SetWindowPos( lphc->hWndEdit, 0,
1569 rectEdit->left, rectEdit->top,
1570 rectEdit->right - rectEdit->left,
1571 rectEdit->bottom - rectEdit->top,
1572 SWP_NOZORDER | SWP_NOACTIVATE | ((bDrop) ? SWP_NOREDRAW : 0) );
1573
1574 SetWindowPos( lphc->hWndLBox, 0,
1575 rectLB->left, rectLB->top,
1576 rectLB->right - rectLB->left,
1577 rectLB->bottom - rectLB->top,
1578 SWP_NOACTIVATE | SWP_NOZORDER | ((bDrop) ? SWP_NOREDRAW : 0) );
1579
1580 if( bDrop )
1581 {
1582 if( lphc->wState & CBF_DROPPED )
1583 {
1584 lphc->wState &= ~CBF_DROPPED;
1585 ShowWindow( lphc->hWndLBox, SW_HIDE );
1586 }
1587
1588 if( bRedraw && !(lphc->wState & CBF_NOREDRAW) )
1589 RedrawWindow( lphc->self, NULL, 0,
1590 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );
1591 }
1592 }
1593
1594
1595 /***********************************************************************
1596 * COMBO_Size
1597 */
1598 static void COMBO_Size( LPHEADCOMBO lphc, BOOL bRedraw )
1599 {
1600 CBCalcPlacement(lphc->self,
1601 lphc,
1602 &lphc->textRect,
1603 &lphc->buttonRect,
1604 &lphc->droppedRect);
1605
1606 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, bRedraw );
1607 }
1608
1609
1610 /***********************************************************************
1611 * COMBO_Font
1612 */
1613 static void COMBO_Font( LPHEADCOMBO lphc, HFONT hFont, BOOL bRedraw )
1614 {
1615 /*
1616 * Set the font
1617 */
1618 lphc->hFont = hFont;
1619
1620 /*
1621 * Propagate to owned windows.
1622 */
1623 if( lphc->wState & CBF_EDIT )
1624 SendMessageW(lphc->hWndEdit, WM_SETFONT, (WPARAM)hFont, bRedraw);
1625 SendMessageW(lphc->hWndLBox, WM_SETFONT, (WPARAM)hFont, bRedraw);
1626
1627 /*
1628 * Redo the layout of the control.
1629 */
1630 if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1631 {
1632 CBCalcPlacement(lphc->self,
1633 lphc,
1634 &lphc->textRect,
1635 &lphc->buttonRect,
1636 &lphc->droppedRect);
1637
1638 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1639 }
1640 else
1641 {
1642 CBForceDummyResize(lphc);
1643 }
1644 }
1645
1646
1647 /***********************************************************************
1648 * COMBO_SetItemHeight
1649 */
1650 static LRESULT COMBO_SetItemHeight( LPHEADCOMBO lphc, INT index, INT height )
1651 {
1652 LRESULT lRet = CB_ERR;
1653
1654 if( index == -1 ) /* set text field height */
1655 {
1656 if( height < 32768 )
1657 {
1658 lphc->editHeight = height + 2; /* Is the 2 for 2*EDIT_CONTROL_PADDING? */
1659
1660 /*
1661 * Redo the layout of the control.
1662 */
1663 if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1664 {
1665 CBCalcPlacement(lphc->self,
1666 lphc,
1667 &lphc->textRect,
1668 &lphc->buttonRect,
1669 &lphc->droppedRect);
1670
1671 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1672 }
1673 else
1674 {
1675 CBForceDummyResize(lphc);
1676 }
1677
1678 lRet = height;
1679 }
1680 }
1681 else if ( CB_OWNERDRAWN(lphc) ) /* set listbox item height */
1682 lRet = SendMessageW(lphc->hWndLBox, LB_SETITEMHEIGHT,
1683 (WPARAM)index, (LPARAM)height );
1684 return lRet;
1685 }
1686
1687 /***********************************************************************
1688 * COMBO_SelectString
1689 */
1690 static LRESULT COMBO_SelectString( LPHEADCOMBO lphc, INT start, LPARAM pText, BOOL unicode )
1691 {
1692 INT index = unicode ? SendMessageW(lphc->hWndLBox, LB_SELECTSTRING, (WPARAM)start, pText) :
1693 SendMessageA(lphc->hWndLBox, LB_SELECTSTRING, (WPARAM)start, pText);
1694 if( index >= 0 )
1695 {
1696 if( lphc->wState & CBF_EDIT )
1697 CBUpdateEdit( lphc, index );
1698 else
1699 {
1700 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1701 }
1702 }
1703 return (LRESULT)index;
1704 }
1705
1706 /***********************************************************************
1707 * COMBO_LButtonDown
1708 */
1709 static void COMBO_LButtonDown( LPHEADCOMBO lphc, LPARAM lParam )
1710 {
1711 POINT pt;
1712 BOOL bButton;
1713 HWND hWnd = lphc->self;
1714
1715 pt.x = (short)LOWORD(lParam);
1716 pt.y = (short)HIWORD(lParam);
1717 bButton = PtInRect(&lphc->buttonRect, pt);
1718
1719 if( (CB_GETTYPE(lphc) == CBS_DROPDOWNLIST) ||
1720 (bButton && (CB_GETTYPE(lphc) == CBS_DROPDOWN)) )
1721 {
1722 lphc->wState |= CBF_BUTTONDOWN;
1723 if( lphc->wState & CBF_DROPPED )
1724 {
1725 /* got a click to cancel selection */
1726
1727 lphc->wState &= ~CBF_BUTTONDOWN;
1728 CBRollUp( lphc, TRUE, FALSE );
1729 if( !IsWindow( hWnd ) ) return;
1730
1731 if( lphc->wState & CBF_CAPTURE )
1732 {
1733 lphc->wState &= ~CBF_CAPTURE;
1734 ReleaseCapture();
1735 }
1736 }
1737 else
1738 {
1739 /* drop down the listbox and start tracking */
1740
1741 lphc->wState |= CBF_CAPTURE;
1742 SetCapture( hWnd );
1743 CBDropDown( lphc );
1744 }
1745 if( bButton ) CBRepaintButton( lphc );
1746 }
1747 }
1748
1749 /***********************************************************************
1750 * COMBO_LButtonUp
1751 *
1752 * Release capture and stop tracking if needed.
1753 */
1754 static void COMBO_LButtonUp( LPHEADCOMBO lphc )
1755 {
1756 if( lphc->wState & CBF_CAPTURE )
1757 {
1758 lphc->wState &= ~CBF_CAPTURE;
1759 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1760 {
1761 INT index = CBUpdateLBox( lphc, TRUE );
1762 /* Update edit only if item is in the list */
1763 if(index >= 0)
1764 {
1765 lphc->wState |= CBF_NOLBSELECT;
1766 CBUpdateEdit( lphc, index );
1767 lphc->wState &= ~CBF_NOLBSELECT;
1768 }
1769 }
1770 ReleaseCapture();
1771 SetCapture(lphc->hWndLBox);
1772 }
1773
1774 if( lphc->wState & CBF_BUTTONDOWN )
1775 {
1776 lphc->wState &= ~CBF_BUTTONDOWN;
1777 CBRepaintButton( lphc );
1778 }
1779 }
1780
1781 /***********************************************************************
1782 * COMBO_MouseMove
1783 *
1784 * Two things to do - track combo button and release capture when
1785 * pointer goes into the listbox.
1786 */
1787 static void COMBO_MouseMove( LPHEADCOMBO lphc, WPARAM wParam, LPARAM lParam )
1788 {
1789 POINT pt;
1790 RECT lbRect;
1791
1792 pt.x = (short)LOWORD(lParam);
1793 pt.y = (short)HIWORD(lParam);
1794
1795 if( lphc->wState & CBF_BUTTONDOWN )
1796 {
1797 BOOL bButton;
1798
1799 bButton = PtInRect(&lphc->buttonRect, pt);
1800
1801 if( !bButton )
1802 {
1803 lphc->wState &= ~CBF_BUTTONDOWN;
1804 CBRepaintButton( lphc );
1805 }
1806 }
1807
1808 GetClientRect( lphc->hWndLBox, &lbRect );
1809 MapWindowPoints( lphc->self, lphc->hWndLBox, &pt, 1 );
1810 if( PtInRect(&lbRect, pt) )
1811 {
1812 lphc->wState &= ~CBF_CAPTURE;
1813 ReleaseCapture();
1814 if( CB_GETTYPE(lphc) == CBS_DROPDOWN ) CBUpdateLBox( lphc, TRUE );
1815
1816 /* hand over pointer tracking */
1817 SendMessageW(lphc->hWndLBox, WM_LBUTTONDOWN, wParam, lParam);
1818 }
1819 }
1820
1821 static LRESULT COMBO_GetComboBoxInfo(const HEADCOMBO *lphc, COMBOBOXINFO *pcbi)
1822 {
1823 if (!pcbi || (pcbi->cbSize < sizeof(COMBOBOXINFO)))
1824 return FALSE;
1825
1826 pcbi->rcItem = lphc->textRect;
1827 pcbi->rcButton = lphc->buttonRect;
1828 pcbi->stateButton = 0;
1829 if (lphc->wState & CBF_BUTTONDOWN)
1830 pcbi->stateButton |= STATE_SYSTEM_PRESSED;
1831 if (IsRectEmpty(&lphc->buttonRect))
1832 pcbi->stateButton |= STATE_SYSTEM_INVISIBLE;
1833 pcbi->hwndCombo = lphc->self;
1834 pcbi->hwndItem = lphc->hWndEdit;
1835 pcbi->hwndList = lphc->hWndLBox;
1836 return TRUE;
1837 }
1838
1839 static char *strdupA(LPCSTR str)
1840 {
1841 char *ret;
1842 DWORD len;
1843
1844 if(!str) return NULL;
1845
1846 len = strlen(str);
1847 ret = HeapAlloc(GetProcessHeap(), 0, len + 1);
1848 if (ret != NULL)
1849 memcpy(ret, str, len + 1);
1850 return ret;
1851 }
1852
1853 /***********************************************************************
1854 * ComboWndProc_common
1855 *
1856 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/comboboxes/comboboxes.asp
1857 */
1858 static LRESULT ComboWndProc_common( HWND hwnd, UINT message,
1859 WPARAM wParam, LPARAM lParam, BOOL unicode )
1860 {
1861 LPHEADCOMBO lphc = (LPHEADCOMBO)GetWindowLongPtrW( hwnd, 0 );
1862
1863 TRACE("[%p]: msg %s wp %08lx lp %08lx\n",
1864 hwnd, SPY_GetMsgName(message, hwnd), wParam, lParam );
1865
1866 if( lphc || message == WM_NCCREATE )
1867 switch(message)
1868 {
1869
1870 /* System messages */
1871
1872 case WM_NCCREATE:
1873 {
1874 LONG style = unicode ? ((LPCREATESTRUCTW)lParam)->style :
1875 ((LPCREATESTRUCTA)lParam)->style;
1876 return COMBO_NCCreate(hwnd, style);
1877 }
1878 case WM_NCDESTROY:
1879 COMBO_NCDestroy(lphc);
1880 break;/* -> DefWindowProc */
1881
1882 case WM_CREATE:
1883 {
1884 HWND hwndParent;
1885 LONG style;
1886 if(unicode)
1887 {
1888 hwndParent = ((LPCREATESTRUCTW)lParam)->hwndParent;
1889 style = ((LPCREATESTRUCTW)lParam)->style;
1890 }
1891 else
1892 {
1893 hwndParent = ((LPCREATESTRUCTA)lParam)->hwndParent;
1894 style = ((LPCREATESTRUCTA)lParam)->style;
1895 }
1896 return COMBO_Create(hwnd, lphc, hwndParent, style, unicode);
1897 }
1898
1899 case WM_PRINTCLIENT:
1900 /* Fallthrough */
1901 case WM_PAINT:
1902 /* wParam may contain a valid HDC! */
1903 return COMBO_Paint(lphc, (HDC)wParam);
1904
1905 case WM_ERASEBKGND:
1906 /* do all painting in WM_PAINT like Windows does */
1907 return 1;
1908
1909 case WM_GETDLGCODE:
1910 {
1911 LRESULT result = DLGC_WANTARROWS | DLGC_WANTCHARS;
1912 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1913 {
1914 int vk = (int)((LPMSG)lParam)->wParam;
1915
1916 if ((vk == VK_RETURN || vk == VK_ESCAPE) && (lphc->wState & CBF_DROPPED))
1917 result |= DLGC_WANTMESSAGE;
1918 }
1919 return result;
1920 }
1921 case WM_WINDOWPOSCHANGING:
1922 return COMBO_WindowPosChanging(hwnd, lphc, (LPWINDOWPOS)lParam);
1923 case WM_WINDOWPOSCHANGED:
1924 /* SetWindowPos can be called on a Combobox to resize its Listbox.
1925 * In that case, the Combobox itself will not be resized, so we won't
1926 * get a WM_SIZE. Since we still want to update the Listbox, we have to
1927 * do it here.
1928 */
1929 /* we should not force repainting on WM_WINDOWPOSCHANGED, it breaks
1930 * Z-order based painting.
1931 */
1932 /* fall through */
1933 case WM_SIZE:
1934 if( lphc->hWndLBox &&
1935 !(lphc->wState & CBF_NORESIZE) ) COMBO_Size( lphc, message == WM_SIZE );
1936 return TRUE;
1937 case WM_SETFONT:
1938 COMBO_Font( lphc, (HFONT)wParam, (BOOL)lParam );
1939 return TRUE;
1940 case WM_GETFONT:
1941 return (LRESULT)lphc->hFont;
1942 case WM_SETFOCUS:
1943 if( lphc->wState & CBF_EDIT )
1944 SetFocus( lphc->hWndEdit );
1945 else
1946 COMBO_SetFocus( lphc );
1947 return TRUE;
1948 case WM_KILLFOCUS:
1949 {
1950 #ifdef __REACTOS__
1951 HWND hwndFocus = (HWND)wParam;
1952 #else
1953 HWND hwndFocus = WIN_GetFullHandle( (HWND)wParam );
1954 #endif
1955 if( !hwndFocus ||
1956 (hwndFocus != lphc->hWndEdit && hwndFocus != lphc->hWndLBox ))
1957 COMBO_KillFocus( lphc );
1958 return TRUE;
1959 }
1960 case WM_COMMAND:
1961 #ifdef __REACTOS__
1962 return COMBO_Command( lphc, wParam, (HWND)lParam);
1963 #else
1964 return COMBO_Command( lphc, wParam, WIN_GetFullHandle( (HWND)lParam ) );
1965 #endif
1966 case WM_GETTEXT:
1967 return unicode ? COMBO_GetTextW( lphc, wParam, (LPWSTR)lParam )
1968 : COMBO_GetTextA( lphc, wParam, (LPSTR)lParam );
1969 case WM_SETTEXT:
1970 case WM_GETTEXTLENGTH:
1971 case WM_CLEAR:
1972 if ((message == WM_GETTEXTLENGTH) && !ISWIN31 && !(lphc->wState & CBF_EDIT))
1973 {
1974 int j = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1975 if (j == -1) return 0;
1976 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, j, 0) :
1977 SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, j, 0);
1978 }
1979 else if( lphc->wState & CBF_EDIT )
1980 {
1981 LRESULT ret;
1982 lphc->wState |= CBF_NOEDITNOTIFY;
1983 ret = unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1984 SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1985 lphc->wState &= ~CBF_NOEDITNOTIFY;
1986 return ret;
1987 }
1988 else return CB_ERR;
1989 case WM_CUT:
1990 case WM_PASTE:
1991 case WM_COPY:
1992 if( lphc->wState & CBF_EDIT )
1993 {
1994 return unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1995 SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1996 }
1997 else return CB_ERR;
1998
1999 case WM_DRAWITEM:
2000 case WM_DELETEITEM:
2001 case WM_COMPAREITEM:
2002 case WM_MEASUREITEM:
2003 return COMBO_ItemOp(lphc, message, lParam);
2004 case WM_ENABLE:
2005 if( lphc->wState & CBF_EDIT )
2006 EnableWindow( lphc->hWndEdit, (BOOL)wParam );
2007 EnableWindow( lphc->hWndLBox, (BOOL)wParam );
2008
2009 /* Force the control to repaint when the enabled state changes. */
2010 InvalidateRect(lphc->self, NULL, TRUE);
2011 return TRUE;
2012 case WM_SETREDRAW:
2013 if( wParam )
2014 lphc->wState &= ~CBF_NOREDRAW;
2015 else
2016 lphc->wState |= CBF_NOREDRAW;
2017
2018 if( lphc->wState & CBF_EDIT )
2019 SendMessageW(lphc->hWndEdit, message, wParam, lParam);
2020 SendMessageW(lphc->hWndLBox, message, wParam, lParam);
2021 return 0;
2022 case WM_SYSKEYDOWN:
2023 if( KEYDATA_ALT & HIWORD(lParam) )
2024 if( wParam == VK_UP || wParam == VK_DOWN )
2025 COMBO_FlipListbox( lphc, FALSE, FALSE );
2026 return 0;
2027
2028 case WM_CHAR:
2029 case WM_IME_CHAR:
2030 case WM_KEYDOWN:
2031 {
2032 HWND hwndTarget;
2033
2034 if ((wParam == VK_RETURN || wParam == VK_ESCAPE) &&
2035 (lphc->wState & CBF_DROPPED))
2036 {
2037 CBRollUp( lphc, wParam == VK_RETURN, FALSE );
2038 return TRUE;
2039 }
2040 else if ((wParam == VK_F4) && !(lphc->wState & CBF_EUI))
2041 {
2042 COMBO_FlipListbox( lphc, FALSE, FALSE );
2043 return TRUE;
2044 }
2045
2046 if( lphc->wState & CBF_EDIT )
2047 hwndTarget = lphc->hWndEdit;
2048 else
2049 hwndTarget = lphc->hWndLBox;
2050
2051 return unicode ? SendMessageW(hwndTarget, message, wParam, lParam) :
2052 SendMessageA(hwndTarget, message, wParam, lParam);
2053 }
2054 case WM_LBUTTONDOWN:
2055 if( !(lphc->wState & CBF_FOCUSED) ) SetFocus( lphc->self );
2056 if( lphc->wState & CBF_FOCUSED ) COMBO_LButtonDown( lphc, lParam );
2057 return TRUE;
2058 case WM_LBUTTONUP:
2059 COMBO_LButtonUp( lphc );
2060 return TRUE;
2061 case WM_MOUSEMOVE:
2062 if( lphc->wState & CBF_CAPTURE )
2063 COMBO_MouseMove( lphc, wParam, lParam );
2064 return TRUE;
2065
2066 case WM_MOUSEWHEEL:
2067 if (wParam & (MK_SHIFT | MK_CONTROL))
2068 return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2069 DefWindowProcA(hwnd, message, wParam, lParam);
2070
2071 if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_UP, 0);
2072 if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_DOWN, 0);
2073 return TRUE;
2074
2075 /* Combo messages */
2076
2077 #ifndef __REACTOS__
2078 case CB_ADDSTRING16:
2079 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2080 /* fall through */
2081 #endif
2082 case CB_ADDSTRING:
2083 if( unicode )
2084 {
2085 if( lphc->dwStyle & CBS_LOWERCASE )
2086 CharLowerW((LPWSTR)lParam);
2087 else if( lphc->dwStyle & CBS_UPPERCASE )
2088 CharUpperW((LPWSTR)lParam);
2089 return SendMessageW(lphc->hWndLBox, LB_ADDSTRING, 0, lParam);
2090 }
2091 else /* unlike the unicode version, the ansi version does not overwrite
2092 the string if converting case */
2093 {
2094 char *string = NULL;
2095 LRESULT ret;
2096 if( lphc->dwStyle & CBS_LOWERCASE )
2097 {
2098 string = strdupA((LPSTR)lParam);
2099 CharLowerA(string);
2100 }
2101
2102 else if( lphc->dwStyle & CBS_UPPERCASE )
2103 {
2104 string = strdupA((LPSTR)lParam);
2105 CharUpperA(string);
2106 }
2107
2108 ret = SendMessageA(lphc->hWndLBox, LB_ADDSTRING, 0, string ? (LPARAM)string : lParam);
2109 HeapFree(GetProcessHeap(), 0, string);
2110 return ret;
2111 }
2112 #ifndef __REACTOS__
2113 case CB_INSERTSTRING16:
2114 wParam = (INT)(INT16)wParam;
2115 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2116 /* fall through */
2117 #endif
2118 case CB_INSERTSTRING:
2119 if( unicode )
2120 {
2121 if( lphc->dwStyle & CBS_LOWERCASE )
2122 CharLowerW((LPWSTR)lParam);
2123 else if( lphc->dwStyle & CBS_UPPERCASE )
2124 CharUpperW((LPWSTR)lParam);
2125 return SendMessageW(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2126 }
2127 else
2128 {
2129 if( lphc->dwStyle & CBS_LOWERCASE )
2130 CharLowerA((LPSTR)lParam);
2131 else if( lphc->dwStyle & CBS_UPPERCASE )
2132 CharUpperA((LPSTR)lParam);
2133
2134 return SendMessageA(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2135 }
2136 #ifndef __REACTOS__
2137 case CB_DELETESTRING16:
2138 #endif
2139 case CB_DELETESTRING:
2140 return unicode ? SendMessageW(lphc->hWndLBox, LB_DELETESTRING, wParam, 0) :
2141 SendMessageA(lphc->hWndLBox, LB_DELETESTRING, wParam, 0);
2142 #ifndef __REACTOS__
2143 case CB_SELECTSTRING16:
2144 wParam = (INT)(INT16)wParam;
2145 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2146 /* fall through */
2147 #endif
2148 case CB_SELECTSTRING:
2149 return COMBO_SelectString(lphc, (INT)wParam, lParam, unicode);
2150 #ifndef __REACTOS__
2151 case CB_FINDSTRING16:
2152 wParam = (INT)(INT16)wParam;
2153 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2154 /* fall through */
2155 #endif
2156 case CB_FINDSTRING:
2157 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam) :
2158 SendMessageA(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam);
2159 #ifndef __REACTOS__
2160 case CB_FINDSTRINGEXACT16:
2161 wParam = (INT)(INT16)wParam;
2162 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2163 /* fall through */
2164 #endif
2165 case CB_FINDSTRINGEXACT:
2166 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam) :
2167 SendMessageA(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam);
2168 #ifndef __REACTOS__
2169 case CB_SETITEMHEIGHT16:
2170 wParam = (INT)(INT16)wParam; /* signed integer */
2171 /* fall through */
2172 #endif
2173 case CB_SETITEMHEIGHT:
2174 return COMBO_SetItemHeight( lphc, (INT)wParam, (INT)lParam);
2175 #ifndef __REACTOS__
2176 case CB_GETITEMHEIGHT16:
2177 wParam = (INT)(INT16)wParam;
2178 /* fall through */
2179 #endif
2180 case CB_GETITEMHEIGHT:
2181 if( (INT)wParam >= 0 ) /* listbox item */
2182 return SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, wParam, 0);
2183 return CBGetTextAreaHeight(hwnd, lphc);
2184 #ifndef __REACTOS__
2185 case CB_RESETCONTENT16:
2186 #endif
2187 case CB_RESETCONTENT:
2188 SendMessageW(lphc->hWndLBox, LB_RESETCONTENT, 0, 0);
2189 if( (lphc->wState & CBF_EDIT) && CB_HASSTRINGS(lphc) )
2190 {
2191 static const WCHAR empty_stringW[] = { 0 };
2192 SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, (LPARAM)empty_stringW);
2193 }
2194 else
2195 InvalidateRect(lphc->self, NULL, TRUE);
2196 return TRUE;
2197 case CB_INITSTORAGE:
2198 return SendMessageW(lphc->hWndLBox, LB_INITSTORAGE, wParam, lParam);
2199 case CB_GETHORIZONTALEXTENT:
2200 return SendMessageW(lphc->hWndLBox, LB_GETHORIZONTALEXTENT, 0, 0);
2201 case CB_SETHORIZONTALEXTENT:
2202 return SendMessageW(lphc->hWndLBox, LB_SETHORIZONTALEXTENT, wParam, 0);
2203 case CB_GETTOPINDEX:
2204 return SendMessageW(lphc->hWndLBox, LB_GETTOPINDEX, 0, 0);
2205 case CB_GETLOCALE:
2206 return SendMessageW(lphc->hWndLBox, LB_GETLOCALE, 0, 0);
2207 case CB_SETLOCALE:
2208 return SendMessageW(lphc->hWndLBox, LB_SETLOCALE, wParam, 0);
2209 case CB_GETDROPPEDWIDTH:
2210 if( lphc->droppedWidth )
2211 return lphc->droppedWidth;
2212 return lphc->droppedRect.right - lphc->droppedRect.left;
2213 case CB_SETDROPPEDWIDTH:
2214 if( (CB_GETTYPE(lphc) != CBS_SIMPLE) &&
2215 (INT)wParam < 32768 ) lphc->droppedWidth = (INT)wParam;
2216 return CB_ERR;
2217 #ifndef __REACTOS__
2218 case CB_GETDROPPEDCONTROLRECT16:
2219 lParam = (LPARAM)MapSL(lParam);
2220 if( lParam )
2221 {
2222 RECT r;
2223 CBGetDroppedControlRect( lphc, &r );
2224 CONV_RECT32TO16( &r, (LPRECT16)lParam );
2225 }
2226 return CB_OKAY;
2227 #endif
2228 case CB_GETDROPPEDCONTROLRECT:
2229 if( lParam ) CBGetDroppedControlRect(lphc, (LPRECT)lParam );
2230 return CB_OKAY;
2231 #ifndef __REACTOS__
2232 case CB_GETDROPPEDSTATE16:
2233 #endif
2234 case CB_GETDROPPEDSTATE:
2235 return (lphc->wState & CBF_DROPPED) ? TRUE : FALSE;
2236 #ifndef __REACTOS__
2237 case CB_DIR16:
2238 return SendMessageA(lphc->hWndLBox, LB_DIR16, wParam, lParam);
2239 #endif
2240 case CB_DIR:
2241 return unicode ? SendMessageW(lphc->hWndLBox, LB_DIR, wParam, lParam) :
2242 SendMessageA(lphc->hWndLBox, LB_DIR, wParam, lParam);
2243
2244 #ifndef __REACTOS__
2245 case CB_SHOWDROPDOWN16:
2246 #endif
2247 case CB_SHOWDROPDOWN:
2248 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
2249 {
2250 if( wParam )
2251 {
2252 if( !(lphc->wState & CBF_DROPPED) )
2253 CBDropDown( lphc );
2254 }
2255 else
2256 if( lphc->wState & CBF_DROPPED )
2257 CBRollUp( lphc, FALSE, TRUE );
2258 }
2259 return TRUE;
2260 #ifndef __REACTOS__
2261 case CB_GETCOUNT16:
2262 #endif
2263 case CB_GETCOUNT:
2264 return SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
2265 #ifndef __REACTOS__
2266 case CB_GETCURSEL16:
2267 #endif
2268 case CB_GETCURSEL:
2269 return SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
2270 #ifndef __REACTOS__
2271 case CB_SETCURSEL16:
2272 wParam = (INT)(INT16)wParam;
2273 /* fall through */
2274 #endif
2275 case CB_SETCURSEL:
2276 lParam = SendMessageW(lphc->hWndLBox, LB_SETCURSEL, wParam, 0);
2277 if( lParam >= 0 )
2278 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, wParam, 0);
2279
2280 /* no LBN_SELCHANGE in this case, update manually */
2281 if( lphc->wState & CBF_EDIT )
2282 CBUpdateEdit( lphc, (INT)wParam );
2283 else
2284 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
2285 lphc->wState &= ~CBF_SELCHANGE;
2286 return lParam;
2287 #ifndef __REACTOS__
2288 case CB_GETLBTEXT16:
2289 wParam = (INT)(INT16)wParam;
2290 lParam = (LPARAM)MapSL(lParam);
2291 /* fall through */
2292 #endif
2293 case CB_GETLBTEXT:
2294 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXT, wParam, lParam) :
2295 SendMessageA(lphc->hWndLBox, LB_GETTEXT, wParam, lParam);
2296 #ifndef __REACTOS__
2297 case CB_GETLBTEXTLEN16:
2298 wParam = (INT)(INT16)wParam;
2299 /* fall through */
2300 #endif
2301 case CB_GETLBTEXTLEN:
2302 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0) :
2303 SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0);
2304 #ifndef __REACTOS__
2305 case CB_GETITEMDATA16:
2306 wParam = (INT)(INT16)wParam;
2307 /* fall through */
2308 #endif
2309 case CB_GETITEMDATA:
2310 return SendMessageW(lphc->hWndLBox, LB_GETITEMDATA, wParam, 0);
2311 #ifndef __REACTOS__
2312 case CB_SETITEMDATA16:
2313 wParam = (INT)(INT16)wParam;
2314 /* fall through */
2315 #endif
2316 case CB_SETITEMDATA:
2317 return SendMessageW(lphc->hWndLBox, LB_SETITEMDATA, wParam, lParam);
2318 #ifndef __REACTOS__
2319 case CB_GETEDITSEL16:
2320 wParam = lParam = 0; /* just in case */
2321 /* fall through */
2322 #endif
2323 case CB_GETEDITSEL:
2324 /* Edit checks passed parameters itself */
2325 if( lphc->wState & CBF_EDIT )
2326 return SendMessageW(lphc->hWndEdit, EM_GETSEL, wParam, lParam);
2327 return CB_ERR;
2328 #ifndef __REACTOS__
2329 case CB_SETEDITSEL16:
2330 #endif
2331 case CB_SETEDITSEL:
2332 if( lphc->wState & CBF_EDIT )
2333 return SendMessageW(lphc->hWndEdit, EM_SETSEL,
2334 (INT)(INT16)LOWORD(lParam), (INT)(INT16)HIWORD(lParam) );
2335 return CB_ERR;
2336 #ifndef __REACTOS__
2337 case CB_SETEXTENDEDUI16:
2338 #endif
2339 case CB_SETEXTENDEDUI:
2340 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
2341 return CB_ERR;
2342 if( wParam )
2343 lphc->wState |= CBF_EUI;
2344 else lphc->wState &= ~CBF_EUI;
2345 return CB_OKAY;
2346 #ifndef __REACTOS__
2347 case CB_GETEXTENDEDUI16:
2348 #endif
2349 case CB_GETEXTENDEDUI:
2350 return (lphc->wState & CBF_EUI) ? TRUE : FALSE;
2351 case CB_GETCOMBOBOXINFO:
2352 return COMBO_GetComboBoxInfo(lphc, (COMBOBOXINFO *)lParam);
2353 case CB_LIMITTEXT:
2354 if( lphc->wState & CBF_EDIT )
2355 return SendMessageW(lphc->hWndEdit, EM_LIMITTEXT, wParam, lParam);
2356 break;
2357
2358 case WM_UPDATEUISTATE:
2359 if (unicode)
2360 DefWindowProcW(lphc->self, message, wParam, lParam);
2361 else
2362 DefWindowProcA(lphc->self, message, wParam, lParam);
2363
2364 if (COMBO_update_uistate(lphc))
2365 {
2366 /* redraw text */
2367 if( !(lphc->wState & CBF_EDIT) )
2368 NtUserInvalidateRect(lphc->self, &lphc->textRect, TRUE);
2369 }
2370 break;
2371
2372 default:
2373 if (message >= WM_USER)
2374 WARN("unknown msg WM_USER+%04x wp=%04lx lp=%08lx\n",
2375 message - WM_USER, wParam, lParam );
2376 break;
2377 }
2378 return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2379 DefWindowProcA(hwnd, message, wParam, lParam);
2380 }
2381
2382 /***********************************************************************
2383 * ComboWndProcA
2384 *
2385 * This is just a wrapper for the real ComboWndProc which locks/unlocks
2386 * window structs.
2387 */
2388 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
2389 {
2390 if (!IsWindow(hwnd)) return 0;
2391 return ComboWndProc_common( hwnd, message, wParam, lParam, FALSE );
2392 }
2393
2394 /***********************************************************************
2395 * ComboWndProcW
2396 */
2397 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
2398 {
2399 if (!IsWindow(hwnd)) return 0;
2400 return ComboWndProc_common( hwnd, message, wParam, lParam, TRUE );
2401 }
2402
2403 /*************************************************************************
2404 * GetComboBoxInfo (USER32.@)
2405 */
2406 BOOL WINAPI GetComboBoxInfo(HWND hwndCombo, /* [in] handle to combo box */
2407 PCOMBOBOXINFO pcbi /* [in/out] combo box information */)
2408 {
2409 TRACE("(%p, %p)\n", hwndCombo, pcbi);
2410 #ifndef __REACTOS__
2411 return SendMessageW(hwndCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)pcbi);
2412 #else
2413 return NtUserGetComboBoxInfo(hwndCombo, pcbi);
2414 #endif
2415 }