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