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