user32: Let ComboBox edit control handle the redraw even if CBF_NOREDRAW is set.
[wine.git] / dlls / user32 / combo.c
blob956385f0ea0201dd0e797f8459d3d3542ebcad66
1 /*
2 * Combo controls
4 * Copyright 1997 Alex Korobka
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTES
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.
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.
29 * TODO:
30 * - ComboBox_[GS]etMinVisible()
31 * - CB_GETMINVISIBLE, CB_SETMINVISIBLE
32 * - CB_SETTOPINDEX
35 #include <stdarg.h>
36 #include <string.h>
38 #define OEMRESOURCE
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "wine/unicode.h"
45 #include "user_private.h"
46 #include "win.h"
47 #include "controls.h"
48 #include "winternl.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(combo);
53 /* bits in the dwKeyData */
54 #define KEYDATA_ALT 0x2000
55 #define KEYDATA_PREVSTATE 0x4000
58 * Additional combo box definitions
61 #define CB_NOTIFY( lphc, code ) \
62 (SendMessageW((lphc)->owner, WM_COMMAND, \
63 MAKEWPARAM(GetWindowLongPtrW((lphc)->self,GWLP_ID), (code)), (LPARAM)(lphc)->self))
65 #define CB_DISABLED( lphc ) (!IsWindowEnabled((lphc)->self))
66 #define CB_OWNERDRAWN( lphc ) ((lphc)->dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
67 #define CB_HASSTRINGS( lphc ) ((lphc)->dwStyle & CBS_HASSTRINGS)
68 #define CB_HWND( lphc ) ((lphc)->self)
69 #define CB_GETTYPE( lphc ) ((lphc)->dwStyle & (CBS_DROPDOWNLIST))
71 #define ISWIN31 (LOWORD(GetVersion()) == 0x0a03)
74 * Drawing globals
76 static HBITMAP hComboBmp = 0;
77 static UINT CBitHeight, CBitWidth;
80 * Look and feel dependent "constants"
83 #define COMBO_YBORDERGAP 5
84 #define COMBO_XBORDERSIZE() 2
85 #define COMBO_YBORDERSIZE() 2
86 #define COMBO_EDITBUTTONSPACE() 0
87 #define EDIT_CONTROL_PADDING() 1
89 /*********************************************************************
90 * combo class descriptor
92 static const WCHAR comboboxW[] = {'C','o','m','b','o','B','o','x',0};
93 const struct builtin_class_descr COMBO_builtin_class =
95 comboboxW, /* name */
96 CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, /* style */
97 WINPROC_COMBO, /* proc */
98 sizeof(HEADCOMBO *), /* extra */
99 IDC_ARROW, /* cursor */
100 0 /* brush */
104 /***********************************************************************
105 * COMBO_Init
107 * Load combo button bitmap.
109 static BOOL COMBO_Init(void)
111 HDC hDC;
113 if( hComboBmp ) return TRUE;
114 if( (hDC = CreateCompatibleDC(0)) )
116 BOOL bRet = FALSE;
117 if( (hComboBmp = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_COMBO))) )
119 BITMAP bm;
120 HBITMAP hPrevB;
121 RECT r;
123 GetObjectW( hComboBmp, sizeof(bm), &bm );
124 CBitHeight = bm.bmHeight;
125 CBitWidth = bm.bmWidth;
127 TRACE("combo bitmap [%i,%i]\n", CBitWidth, CBitHeight );
129 hPrevB = SelectObject( hDC, hComboBmp);
130 SetRect( &r, 0, 0, CBitWidth, CBitHeight );
131 InvertRect( hDC, &r );
132 SelectObject( hDC, hPrevB );
133 bRet = TRUE;
135 DeleteDC( hDC );
136 return bRet;
138 return FALSE;
141 /***********************************************************************
142 * COMBO_NCCreate
144 static LRESULT COMBO_NCCreate(HWND hwnd, LONG style)
146 LPHEADCOMBO lphc;
148 if (COMBO_Init() && (lphc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HEADCOMBO))) )
150 lphc->self = hwnd;
151 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)lphc );
153 /* some braindead apps do try to use scrollbar/border flags */
155 lphc->dwStyle = style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL);
156 SetWindowLongW( hwnd, GWL_STYLE, style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL) );
159 * We also have to remove the client edge style to make sure
160 * we don't end-up with a non client area.
162 SetWindowLongW( hwnd, GWL_EXSTYLE,
163 GetWindowLongW( hwnd, GWL_EXSTYLE ) & ~WS_EX_CLIENTEDGE );
165 if( !(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) )
166 lphc->dwStyle |= CBS_HASSTRINGS;
167 if( !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) )
168 lphc->wState |= CBF_NOTIFY;
170 TRACE("[%p], style = %08x\n", lphc, lphc->dwStyle );
171 return TRUE;
173 return FALSE;
176 /***********************************************************************
177 * COMBO_NCDestroy
179 static LRESULT COMBO_NCDestroy( LPHEADCOMBO lphc )
182 if( lphc )
184 TRACE("[%p]: freeing storage\n", lphc->self);
186 if( (CB_GETTYPE(lphc) != CBS_SIMPLE) && lphc->hWndLBox )
187 DestroyWindow( lphc->hWndLBox );
189 SetWindowLongPtrW( lphc->self, 0, 0 );
190 HeapFree( GetProcessHeap(), 0, lphc );
192 return 0;
195 /***********************************************************************
196 * CBGetTextAreaHeight
198 * This method will calculate the height of the text area of the
199 * combobox.
200 * The height of the text area is set in two ways.
201 * It can be set explicitly through a combobox message or through a
202 * WM_MEASUREITEM callback.
203 * If this is not the case, the height is set to font height + 4px
204 * This height was determined through experimentation.
205 * CBCalcPlacement will add 2*COMBO_YBORDERSIZE pixels for the border
207 static INT CBGetTextAreaHeight(
208 HWND hwnd,
209 LPHEADCOMBO lphc)
211 INT iTextItemHeight;
213 if( lphc->editHeight ) /* explicitly set height */
215 iTextItemHeight = lphc->editHeight;
217 else
219 TEXTMETRICW tm;
220 HDC hDC = GetDC(hwnd);
221 HFONT hPrevFont = 0;
222 INT baseUnitY;
224 if (lphc->hFont)
225 hPrevFont = SelectObject( hDC, lphc->hFont );
227 GetTextMetricsW(hDC, &tm);
229 baseUnitY = tm.tmHeight;
231 if( hPrevFont )
232 SelectObject( hDC, hPrevFont );
234 ReleaseDC(hwnd, hDC);
236 iTextItemHeight = baseUnitY + 4;
240 * Check the ownerdraw case if we haven't asked the parent the size
241 * of the item yet.
243 if ( CB_OWNERDRAWN(lphc) &&
244 (lphc->wState & CBF_MEASUREITEM) )
246 MEASUREITEMSTRUCT measureItem;
247 RECT clientRect;
248 INT originalItemHeight = iTextItemHeight;
249 UINT id = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
252 * We use the client rect for the width of the item.
254 GetClientRect(hwnd, &clientRect);
256 lphc->wState &= ~CBF_MEASUREITEM;
259 * Send a first one to measure the size of the text area
261 measureItem.CtlType = ODT_COMBOBOX;
262 measureItem.CtlID = id;
263 measureItem.itemID = -1;
264 measureItem.itemWidth = clientRect.right;
265 measureItem.itemHeight = iTextItemHeight - 6; /* ownerdrawn cb is taller */
266 measureItem.itemData = 0;
267 SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
268 iTextItemHeight = 6 + measureItem.itemHeight;
271 * Send a second one in the case of a fixed ownerdraw list to calculate the
272 * size of the list items. (we basically do this on behalf of the listbox)
274 if (lphc->dwStyle & CBS_OWNERDRAWFIXED)
276 measureItem.CtlType = ODT_COMBOBOX;
277 measureItem.CtlID = id;
278 measureItem.itemID = 0;
279 measureItem.itemWidth = clientRect.right;
280 measureItem.itemHeight = originalItemHeight;
281 measureItem.itemData = 0;
282 SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
283 lphc->fixedOwnerDrawHeight = measureItem.itemHeight;
287 * Keep the size for the next time
289 lphc->editHeight = iTextItemHeight;
292 return iTextItemHeight;
295 /***********************************************************************
296 * CBForceDummyResize
298 * The dummy resize is used for listboxes that have a popup to trigger
299 * a re-arranging of the contents of the combobox and the recalculation
300 * of the size of the "real" control window.
302 static void CBForceDummyResize(
303 LPHEADCOMBO lphc)
305 RECT windowRect;
306 int newComboHeight;
308 newComboHeight = CBGetTextAreaHeight(lphc->self,lphc) + 2*COMBO_YBORDERSIZE();
310 GetWindowRect(lphc->self, &windowRect);
313 * We have to be careful, resizing a combobox also has the meaning that the
314 * dropped rect will be resized. In this case, we want to trigger a resize
315 * to recalculate layout but we don't want to change the dropped rectangle
316 * So, we pass the height of text area of control as the height.
317 * this will cancel-out in the processing of the WM_WINDOWPOSCHANGING
318 * message.
320 SetWindowPos( lphc->self,
321 NULL,
322 0, 0,
323 windowRect.right - windowRect.left,
324 newComboHeight,
325 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
328 /***********************************************************************
329 * CBCalcPlacement
331 * Set up component coordinates given valid lphc->RectCombo.
333 static void CBCalcPlacement(
334 HWND hwnd,
335 LPHEADCOMBO lphc,
336 LPRECT lprEdit,
337 LPRECT lprButton,
338 LPRECT lprLB)
341 * Again, start with the client rectangle.
343 GetClientRect(hwnd, lprEdit);
346 * Remove the borders
348 InflateRect(lprEdit, -COMBO_XBORDERSIZE(), -COMBO_YBORDERSIZE());
351 * Chop off the bottom part to fit with the height of the text area.
353 lprEdit->bottom = lprEdit->top + CBGetTextAreaHeight(hwnd, lphc);
356 * The button starts the same vertical position as the text area.
358 CopyRect(lprButton, lprEdit);
361 * If the combobox is "simple" there is no button.
363 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
364 lprButton->left = lprButton->right = lprButton->bottom = 0;
365 else
368 * Let's assume the combobox button is the same width as the
369 * scrollbar button.
370 * size the button horizontally and cut-off the text area.
372 lprButton->left = lprButton->right - GetSystemMetrics(SM_CXVSCROLL);
373 lprEdit->right = lprButton->left;
377 * In the case of a dropdown, there is an additional spacing between the
378 * text area and the button.
380 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
382 lprEdit->right -= COMBO_EDITBUTTONSPACE();
386 * If we have an edit control, we space it away from the borders slightly.
388 if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
390 InflateRect(lprEdit, -EDIT_CONTROL_PADDING(), -EDIT_CONTROL_PADDING());
394 * Adjust the size of the listbox popup.
396 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
399 * Use the client rectangle to initialize the listbox rectangle
401 GetClientRect(hwnd, lprLB);
404 * Then, chop-off the top part.
406 lprLB->top = lprEdit->bottom + COMBO_YBORDERSIZE();
408 else
411 * Make sure the dropped width is as large as the combobox itself.
413 if (lphc->droppedWidth < (lprButton->right + COMBO_XBORDERSIZE()))
415 lprLB->right = lprLB->left + (lprButton->right + COMBO_XBORDERSIZE());
418 * In the case of a dropdown, the popup listbox is offset to the right.
419 * so, we want to make sure it's flush with the right side of the
420 * combobox
422 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
423 lprLB->right -= COMBO_EDITBUTTONSPACE();
425 else
426 lprLB->right = lprLB->left + lphc->droppedWidth;
429 /* don't allow negative window width */
430 if (lprEdit->right < lprEdit->left)
431 lprEdit->right = lprEdit->left;
433 TRACE("\ttext\t= (%s)\n", wine_dbgstr_rect(lprEdit));
435 TRACE("\tbutton\t= (%s)\n", wine_dbgstr_rect(lprButton));
437 TRACE("\tlbox\t= (%s)\n", wine_dbgstr_rect(lprLB));
440 /***********************************************************************
441 * CBGetDroppedControlRect
443 static void CBGetDroppedControlRect( LPHEADCOMBO lphc, LPRECT lpRect)
445 /* In windows, CB_GETDROPPEDCONTROLRECT returns the upper left corner
446 of the combo box and the lower right corner of the listbox */
448 GetWindowRect(lphc->self, lpRect);
450 lpRect->right = lpRect->left + lphc->droppedRect.right - lphc->droppedRect.left;
451 lpRect->bottom = lpRect->top + lphc->droppedRect.bottom - lphc->droppedRect.top;
455 /***********************************************************************
456 * COMBO_Create
458 static LRESULT COMBO_Create( HWND hwnd, LPHEADCOMBO lphc, HWND hwndParent, LONG style,
459 BOOL unicode )
461 static const WCHAR clbName[] = {'C','o','m','b','o','L','B','o','x',0};
462 static const WCHAR editName[] = {'E','d','i','t',0};
464 if( !CB_GETTYPE(lphc) ) lphc->dwStyle |= CBS_SIMPLE;
465 if( CB_GETTYPE(lphc) != CBS_DROPDOWNLIST ) lphc->wState |= CBF_EDIT;
467 lphc->owner = hwndParent;
470 * The item height and dropped width are not set when the control
471 * is created.
473 lphc->droppedWidth = lphc->editHeight = 0;
476 * The first time we go through, we want to measure the ownerdraw item
478 lphc->wState |= CBF_MEASUREITEM;
480 /* M$ IE 3.01 actually creates (and rapidly destroys) an ownerless combobox */
482 if( lphc->owner || !(style & WS_VISIBLE) )
484 UINT lbeStyle = 0;
485 UINT lbeExStyle = 0;
488 * Initialize the dropped rect to the size of the client area of the
489 * control and then, force all the areas of the combobox to be
490 * recalculated.
492 GetClientRect( hwnd, &lphc->droppedRect );
493 CBCalcPlacement(hwnd, lphc, &lphc->textRect, &lphc->buttonRect, &lphc->droppedRect );
496 * Adjust the position of the popup listbox if it's necessary
498 if ( CB_GETTYPE(lphc) != CBS_SIMPLE )
500 lphc->droppedRect.top = lphc->textRect.bottom + COMBO_YBORDERSIZE();
503 * If it's a dropdown, the listbox is offset
505 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
506 lphc->droppedRect.left += COMBO_EDITBUTTONSPACE();
508 if (lphc->droppedRect.bottom < lphc->droppedRect.top)
509 lphc->droppedRect.bottom = lphc->droppedRect.top;
510 if (lphc->droppedRect.right < lphc->droppedRect.left)
511 lphc->droppedRect.right = lphc->droppedRect.left;
512 MapWindowPoints( hwnd, 0, (LPPOINT)&lphc->droppedRect, 2 );
515 /* create listbox popup */
517 lbeStyle = (LBS_NOTIFY | LBS_COMBOBOX | WS_BORDER | WS_CLIPSIBLINGS | WS_CHILD) |
518 (style & (WS_VSCROLL | CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE));
520 if( lphc->dwStyle & CBS_SORT )
521 lbeStyle |= LBS_SORT;
522 if( lphc->dwStyle & CBS_HASSTRINGS )
523 lbeStyle |= LBS_HASSTRINGS;
524 if( lphc->dwStyle & CBS_NOINTEGRALHEIGHT )
525 lbeStyle |= LBS_NOINTEGRALHEIGHT;
526 if( lphc->dwStyle & CBS_DISABLENOSCROLL )
527 lbeStyle |= LBS_DISABLENOSCROLL;
529 if( CB_GETTYPE(lphc) == CBS_SIMPLE ) /* child listbox */
531 lbeStyle |= WS_VISIBLE;
534 * In win 95 look n feel, the listbox in the simple combobox has
535 * the WS_EXCLIENTEDGE style instead of the WS_BORDER style.
537 lbeStyle &= ~WS_BORDER;
538 lbeExStyle |= WS_EX_CLIENTEDGE;
540 else
542 lbeExStyle |= (WS_EX_TOPMOST | WS_EX_TOOLWINDOW);
545 if (unicode)
546 lphc->hWndLBox = CreateWindowExW(lbeExStyle, clbName, NULL, lbeStyle,
547 lphc->droppedRect.left,
548 lphc->droppedRect.top,
549 lphc->droppedRect.right - lphc->droppedRect.left,
550 lphc->droppedRect.bottom - lphc->droppedRect.top,
551 hwnd, (HMENU)ID_CB_LISTBOX,
552 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
553 else
554 lphc->hWndLBox = CreateWindowExA(lbeExStyle, "ComboLBox", NULL, lbeStyle,
555 lphc->droppedRect.left,
556 lphc->droppedRect.top,
557 lphc->droppedRect.right - lphc->droppedRect.left,
558 lphc->droppedRect.bottom - lphc->droppedRect.top,
559 hwnd, (HMENU)ID_CB_LISTBOX,
560 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
562 if( lphc->hWndLBox )
564 BOOL bEdit = TRUE;
565 lbeStyle = WS_CHILD | WS_VISIBLE | ES_NOHIDESEL | ES_LEFT | ES_COMBO;
567 if( lphc->wState & CBF_EDIT )
569 if( lphc->dwStyle & CBS_OEMCONVERT )
570 lbeStyle |= ES_OEMCONVERT;
571 if( lphc->dwStyle & CBS_AUTOHSCROLL )
572 lbeStyle |= ES_AUTOHSCROLL;
573 if( lphc->dwStyle & CBS_LOWERCASE )
574 lbeStyle |= ES_LOWERCASE;
575 else if( lphc->dwStyle & CBS_UPPERCASE )
576 lbeStyle |= ES_UPPERCASE;
578 if (!IsWindowEnabled(hwnd)) lbeStyle |= WS_DISABLED;
580 if (unicode)
581 lphc->hWndEdit = CreateWindowExW(0, editName, NULL, lbeStyle,
582 lphc->textRect.left, lphc->textRect.top,
583 lphc->textRect.right - lphc->textRect.left,
584 lphc->textRect.bottom - lphc->textRect.top,
585 hwnd, (HMENU)ID_CB_EDIT,
586 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
587 else
588 lphc->hWndEdit = CreateWindowExA(0, "Edit", NULL, lbeStyle,
589 lphc->textRect.left, lphc->textRect.top,
590 lphc->textRect.right - lphc->textRect.left,
591 lphc->textRect.bottom - lphc->textRect.top,
592 hwnd, (HMENU)ID_CB_EDIT,
593 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
595 if( !lphc->hWndEdit )
596 bEdit = FALSE;
599 if( bEdit )
601 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
603 /* Now do the trick with parent */
604 SetParent(lphc->hWndLBox, HWND_DESKTOP);
606 * If the combo is a dropdown, we must resize the control
607 * to fit only the text area and button. To do this,
608 * we send a dummy resize and the WM_WINDOWPOSCHANGING message
609 * will take care of setting the height for us.
611 CBForceDummyResize(lphc);
614 TRACE("init done\n");
615 return 0;
617 ERR("edit control failure.\n");
618 } else ERR("listbox failure.\n");
619 } else ERR("no owner for visible combo.\n");
621 /* CreateWindow() will send WM_NCDESTROY to cleanup */
623 return -1;
626 /***********************************************************************
627 * CBPaintButton
629 * Paint combo button (normal, pressed, and disabled states).
631 static void CBPaintButton( LPHEADCOMBO lphc, HDC hdc, RECT rectButton)
633 UINT buttonState = DFCS_SCROLLCOMBOBOX;
635 if( lphc->wState & CBF_NOREDRAW )
636 return;
639 if (lphc->wState & CBF_BUTTONDOWN)
640 buttonState |= DFCS_PUSHED;
642 if (CB_DISABLED(lphc))
643 buttonState |= DFCS_INACTIVE;
645 DrawFrameControl(hdc, &rectButton, DFC_SCROLL, buttonState);
648 /***********************************************************************
649 * COMBO_PrepareColors
651 * This method will sent the appropriate WM_CTLCOLOR message to
652 * prepare and setup the colors for the combo's DC.
654 * It also returns the brush to use for the background.
656 static HBRUSH COMBO_PrepareColors(
657 LPHEADCOMBO lphc,
658 HDC hDC)
660 HBRUSH hBkgBrush;
663 * Get the background brush for this control.
665 if (CB_DISABLED(lphc))
667 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORSTATIC,
668 (WPARAM)hDC, (LPARAM)lphc->self );
671 * We have to change the text color since WM_CTLCOLORSTATIC will
672 * set it to the "enabled" color. This is the same behavior as the
673 * edit control
675 SetTextColor(hDC, GetSysColor(COLOR_GRAYTEXT));
677 else
679 /* FIXME: In which cases WM_CTLCOLORLISTBOX should be sent? */
680 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLOREDIT,
681 (WPARAM)hDC, (LPARAM)lphc->self );
685 * Catch errors.
687 if( !hBkgBrush )
688 hBkgBrush = GetSysColorBrush(COLOR_WINDOW);
690 return hBkgBrush;
693 /***********************************************************************
694 * CBPaintText
696 * Paint CBS_DROPDOWNLIST text field / update edit control contents.
698 static void CBPaintText(
699 LPHEADCOMBO lphc,
700 HDC hdc_paint)
702 RECT rectEdit = lphc->textRect;
703 INT id, size = 0;
704 LPWSTR pText = NULL;
706 TRACE("\n");
708 /* follow Windows combobox that sends a bunch of text
709 * inquiries to its listbox while processing WM_PAINT. */
711 if( (id = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0) ) != LB_ERR )
713 size = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, id, 0);
714 if (size == LB_ERR)
715 FIXME("LB_ERR probably not handled yet\n");
716 if( (pText = HeapAlloc( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))) )
718 /* size from LB_GETTEXTLEN may be too large, from LB_GETTEXT is accurate */
719 size=SendMessageW(lphc->hWndLBox, LB_GETTEXT, id, (LPARAM)pText);
720 pText[size] = '\0'; /* just in case */
721 } else return;
723 else
724 if( !CB_OWNERDRAWN(lphc) )
725 return;
727 if( lphc->wState & CBF_EDIT )
729 static const WCHAR empty_stringW[] = { 0 };
730 if( CB_HASSTRINGS(lphc) ) SetWindowTextW( lphc->hWndEdit, pText ? pText : empty_stringW );
731 if( lphc->wState & CBF_FOCUSED )
732 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, -1);
734 else if(!(lphc->wState & CBF_NOREDRAW) && IsWindowVisible( lphc->self ))
736 /* paint text field ourselves */
737 HDC hdc = hdc_paint ? hdc_paint : GetDC(lphc->self);
738 UINT itemState = ODS_COMBOBOXEDIT;
739 HFONT hPrevFont = (lphc->hFont) ? SelectObject(hdc, lphc->hFont) : 0;
740 HBRUSH hPrevBrush, hBkgBrush;
743 * Give ourselves some space.
745 InflateRect( &rectEdit, -1, -1 );
747 hBkgBrush = COMBO_PrepareColors( lphc, hdc );
748 hPrevBrush = SelectObject( hdc, hBkgBrush );
749 FillRect( hdc, &rectEdit, hBkgBrush );
751 if( CB_OWNERDRAWN(lphc) )
753 DRAWITEMSTRUCT dis;
754 HRGN clipRegion;
755 UINT ctlid = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
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;
762 if (!IsWindowEnabled(lphc->self)) itemState |= ODS_DISABLED;
764 dis.CtlType = ODT_COMBOBOX;
765 dis.CtlID = ctlid;
766 dis.hwndItem = lphc->self;
767 dis.itemAction = ODA_DRAWENTIRE;
768 dis.itemID = id;
769 dis.itemState = itemState;
770 dis.hDC = hdc;
771 dis.rcItem = rectEdit;
772 dis.itemData = SendMessageW(lphc->hWndLBox, LB_GETITEMDATA, id, 0);
775 * Clip the DC and have the parent draw the item.
777 clipRegion = set_control_clipping( hdc, &rectEdit );
779 SendMessageW(lphc->owner, WM_DRAWITEM, ctlid, (LPARAM)&dis );
781 SelectClipRgn( hdc, clipRegion );
782 if (clipRegion) DeleteObject( clipRegion );
784 else
786 static const WCHAR empty_stringW[] = { 0 };
788 if ( (lphc->wState & CBF_FOCUSED) &&
789 !(lphc->wState & CBF_DROPPED) ) {
791 /* highlight */
792 FillRect( hdc, &rectEdit, GetSysColorBrush(COLOR_HIGHLIGHT) );
793 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
794 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
797 ExtTextOutW( hdc,
798 rectEdit.left + 1,
799 rectEdit.top + 1,
800 ETO_OPAQUE | ETO_CLIPPED,
801 &rectEdit,
802 pText ? pText : empty_stringW , size, NULL );
804 if(lphc->wState & CBF_FOCUSED && !(lphc->wState & CBF_DROPPED))
805 DrawFocusRect( hdc, &rectEdit );
808 if( hPrevFont )
809 SelectObject(hdc, hPrevFont );
811 if( hPrevBrush )
812 SelectObject( hdc, hPrevBrush );
814 if( !hdc_paint )
815 ReleaseDC( lphc->self, hdc );
817 HeapFree( GetProcessHeap(), 0, pText );
820 /***********************************************************************
821 * CBPaintBorder
823 static void CBPaintBorder(
824 HWND hwnd,
825 const HEADCOMBO *lphc,
826 HDC hdc)
828 RECT clientRect;
830 if (CB_GETTYPE(lphc) != CBS_SIMPLE)
832 GetClientRect(hwnd, &clientRect);
834 else
836 clientRect = lphc->textRect;
838 InflateRect(&clientRect, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
839 InflateRect(&clientRect, COMBO_XBORDERSIZE(), COMBO_YBORDERSIZE());
842 DrawEdge(hdc, &clientRect, EDGE_SUNKEN, BF_RECT);
845 /***********************************************************************
846 * COMBO_Paint
848 static LRESULT COMBO_Paint(LPHEADCOMBO lphc, HDC hParamDC)
850 PAINTSTRUCT ps;
851 HDC hDC;
853 hDC = (hParamDC) ? hParamDC
854 : BeginPaint( lphc->self, &ps);
856 TRACE("hdc=%p\n", hDC);
858 if( hDC && !(lphc->wState & CBF_NOREDRAW) )
860 HBRUSH hPrevBrush, hBkgBrush;
863 * Retrieve the background brush and select it in the
864 * DC.
866 hBkgBrush = COMBO_PrepareColors(lphc, hDC);
868 hPrevBrush = SelectObject( hDC, hBkgBrush );
869 if (!(lphc->wState & CBF_EDIT))
870 FillRect(hDC, &lphc->textRect, hBkgBrush);
873 * In non 3.1 look, there is a sunken border on the combobox
875 CBPaintBorder(lphc->self, lphc, hDC);
877 if( !IsRectEmpty(&lphc->buttonRect) )
879 CBPaintButton(lphc, hDC, lphc->buttonRect);
882 /* paint the edit control padding area */
883 if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
885 RECT rPadEdit = lphc->textRect;
887 InflateRect(&rPadEdit, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
889 FrameRect( hDC, &rPadEdit, GetSysColorBrush(COLOR_WINDOW) );
892 if( !(lphc->wState & CBF_EDIT) )
893 CBPaintText( lphc, hDC );
895 if( hPrevBrush )
896 SelectObject( hDC, hPrevBrush );
899 if( !hParamDC )
900 EndPaint(lphc->self, &ps);
902 return 0;
905 /***********************************************************************
906 * CBUpdateLBox
908 * Select listbox entry according to the contents of the edit control.
910 static INT CBUpdateLBox( LPHEADCOMBO lphc, BOOL bSelect )
912 INT length, idx;
913 LPWSTR pText = NULL;
915 idx = LB_ERR;
916 length = SendMessageW( lphc->hWndEdit, WM_GETTEXTLENGTH, 0, 0 );
918 if( length > 0 )
919 pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
921 TRACE("\t edit text length %i\n", length );
923 if( pText )
925 GetWindowTextW( lphc->hWndEdit, pText, length + 1);
926 idx = SendMessageW(lphc->hWndLBox, LB_FINDSTRING, -1, (LPARAM)pText);
927 HeapFree( GetProcessHeap(), 0, pText );
930 SendMessageW(lphc->hWndLBox, LB_SETCURSEL, bSelect ? idx : -1, 0);
932 /* probably superfluous but Windows sends this too */
933 SendMessageW(lphc->hWndLBox, LB_SETCARETINDEX, idx < 0 ? 0 : idx, 0);
934 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, idx < 0 ? 0 : idx, 0);
936 return idx;
939 /***********************************************************************
940 * CBUpdateEdit
942 * Copy a listbox entry to the edit control.
944 static void CBUpdateEdit( LPHEADCOMBO lphc , INT index )
946 INT length;
947 LPWSTR pText = NULL;
948 static const WCHAR empty_stringW[] = { 0 };
950 TRACE("\t %i\n", index );
952 if( index >= 0 ) /* got an entry */
954 length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, index, 0);
955 if( length != LB_ERR)
957 if( (pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR))) )
959 SendMessageW(lphc->hWndLBox, LB_GETTEXT, index, (LPARAM)pText);
964 if( CB_HASSTRINGS(lphc) )
966 lphc->wState |= (CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
967 SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, pText ? (LPARAM)pText : (LPARAM)empty_stringW);
968 lphc->wState &= ~(CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
971 if( lphc->wState & CBF_FOCUSED )
972 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, -1);
974 HeapFree( GetProcessHeap(), 0, pText );
977 /***********************************************************************
978 * CBDropDown
980 * Show listbox popup.
982 static void CBDropDown( LPHEADCOMBO lphc )
984 HMONITOR monitor;
985 MONITORINFO mon_info;
986 RECT rect,r;
987 int nItems;
988 int nDroppedHeight;
990 TRACE("[%p]: drop down\n", lphc->self);
992 CB_NOTIFY( lphc, CBN_DROPDOWN );
994 /* set selection */
996 lphc->wState |= CBF_DROPPED;
997 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
999 lphc->droppedIndex = CBUpdateLBox( lphc, TRUE );
1001 /* Update edit only if item is in the list */
1002 if( !(lphc->wState & CBF_CAPTURE) && lphc->droppedIndex >= 0)
1003 CBUpdateEdit( lphc, lphc->droppedIndex );
1005 else
1007 lphc->droppedIndex = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1009 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX,
1010 lphc->droppedIndex == LB_ERR ? 0 : lphc->droppedIndex, 0);
1011 SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1014 /* now set popup position */
1015 GetWindowRect( lphc->self, &rect );
1018 * If it's a dropdown, the listbox is offset
1020 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1021 rect.left += COMBO_EDITBUTTONSPACE();
1023 /* if the dropped height is greater than the total height of the dropped
1024 items list, then force the drop down list height to be the total height
1025 of the items in the dropped list */
1027 /* And Remove any extra space (Best Fit) */
1028 nDroppedHeight = lphc->droppedRect.bottom - lphc->droppedRect.top;
1029 /* if listbox length has been set directly by its handle */
1030 GetWindowRect(lphc->hWndLBox, &r);
1031 if (nDroppedHeight < r.bottom - r.top)
1032 nDroppedHeight = r.bottom - r.top;
1033 nItems = (int)SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
1035 if (nItems > 0)
1037 int nHeight;
1038 int nIHeight;
1040 nIHeight = (int)SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, 0, 0);
1042 nHeight = nIHeight*nItems;
1044 if (nHeight < nDroppedHeight - COMBO_YBORDERSIZE())
1045 nDroppedHeight = nHeight + COMBO_YBORDERSIZE();
1047 if (nDroppedHeight < nHeight)
1049 if (nItems < 5)
1050 nDroppedHeight = (nItems+1)*nIHeight;
1051 else if (nDroppedHeight < 6*nIHeight)
1052 nDroppedHeight = 6*nIHeight;
1056 r.left = rect.left;
1057 r.top = rect.bottom;
1058 r.right = r.left + lphc->droppedRect.right - lphc->droppedRect.left;
1059 r.bottom = r.top + nDroppedHeight;
1061 /*If height of dropped rectangle gets beyond a screen size it should go up, otherwise down.*/
1062 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
1063 mon_info.cbSize = sizeof(mon_info);
1064 GetMonitorInfoW( monitor, &mon_info );
1066 if (r.bottom > mon_info.rcWork.bottom)
1068 r.top = max( rect.top - nDroppedHeight, mon_info.rcWork.top );
1069 r.bottom = min( r.top + nDroppedHeight, mon_info.rcWork.bottom );
1072 SetWindowPos( lphc->hWndLBox, HWND_TOPMOST, r.left, r.top, r.right - r.left, r.bottom - r.top,
1073 SWP_NOACTIVATE | SWP_SHOWWINDOW );
1076 if( !(lphc->wState & CBF_NOREDRAW) )
1077 RedrawWindow( lphc->self, NULL, 0, RDW_INVALIDATE |
1078 RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1080 EnableWindow( lphc->hWndLBox, TRUE );
1081 if (GetCapture() != lphc->self)
1082 SetCapture(lphc->hWndLBox);
1085 /***********************************************************************
1086 * CBRollUp
1088 * Hide listbox popup.
1090 static void CBRollUp( LPHEADCOMBO lphc, BOOL ok, BOOL bButton )
1092 HWND hWnd = lphc->self;
1094 TRACE("[%p]: sel ok? [%i] dropped? [%i]\n",
1095 lphc->self, ok, (INT)(lphc->wState & CBF_DROPPED));
1097 CB_NOTIFY( lphc, (ok) ? CBN_SELENDOK : CBN_SELENDCANCEL );
1099 if( IsWindow( hWnd ) && CB_GETTYPE(lphc) != CBS_SIMPLE )
1102 if( lphc->wState & CBF_DROPPED )
1104 RECT rect;
1106 lphc->wState &= ~CBF_DROPPED;
1107 ShowWindow( lphc->hWndLBox, SW_HIDE );
1109 if(GetCapture() == lphc->hWndLBox)
1111 ReleaseCapture();
1114 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1116 rect = lphc->buttonRect;
1118 else
1120 if( bButton )
1122 UnionRect( &rect,
1123 &lphc->buttonRect,
1124 &lphc->textRect);
1126 else
1127 rect = lphc->textRect;
1129 bButton = TRUE;
1132 if( bButton && !(lphc->wState & CBF_NOREDRAW) )
1133 RedrawWindow( hWnd, &rect, 0, RDW_INVALIDATE |
1134 RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1135 CB_NOTIFY( lphc, CBN_CLOSEUP );
1140 /***********************************************************************
1141 * COMBO_FlipListbox
1143 * Used by the ComboLBox to show/hide itself in response to VK_F4, etc...
1145 BOOL COMBO_FlipListbox( LPHEADCOMBO lphc, BOOL ok, BOOL bRedrawButton )
1147 if( lphc->wState & CBF_DROPPED )
1149 CBRollUp( lphc, ok, bRedrawButton );
1150 return FALSE;
1153 CBDropDown( lphc );
1154 return TRUE;
1157 /***********************************************************************
1158 * CBRepaintButton
1160 static void CBRepaintButton( LPHEADCOMBO lphc )
1162 InvalidateRect(lphc->self, &lphc->buttonRect, TRUE);
1163 UpdateWindow(lphc->self);
1166 /***********************************************************************
1167 * COMBO_SetFocus
1169 static void COMBO_SetFocus( LPHEADCOMBO lphc )
1171 if( !(lphc->wState & CBF_FOCUSED) )
1173 if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1174 SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1176 /* This is wrong. Message sequences seem to indicate that this
1177 is set *after* the notify. */
1178 /* lphc->wState |= CBF_FOCUSED; */
1180 if( !(lphc->wState & CBF_EDIT) )
1181 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1183 CB_NOTIFY( lphc, CBN_SETFOCUS );
1184 lphc->wState |= CBF_FOCUSED;
1188 /***********************************************************************
1189 * COMBO_KillFocus
1191 static void COMBO_KillFocus( LPHEADCOMBO lphc )
1193 HWND hWnd = lphc->self;
1195 if( lphc->wState & CBF_FOCUSED )
1197 CBRollUp( lphc, FALSE, TRUE );
1198 if( IsWindow( hWnd ) )
1200 if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1201 SendMessageW(lphc->hWndLBox, LB_CARETOFF, 0, 0);
1203 lphc->wState &= ~CBF_FOCUSED;
1205 /* redraw text */
1206 if( !(lphc->wState & CBF_EDIT) )
1207 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1209 CB_NOTIFY( lphc, CBN_KILLFOCUS );
1214 /***********************************************************************
1215 * COMBO_Command
1217 static LRESULT COMBO_Command( LPHEADCOMBO lphc, WPARAM wParam, HWND hWnd )
1219 if ( lphc->wState & CBF_EDIT && lphc->hWndEdit == hWnd )
1221 /* ">> 8" makes gcc generate jump-table instead of cmp ladder */
1223 switch( HIWORD(wParam) >> 8 )
1225 case (EN_SETFOCUS >> 8):
1227 TRACE("[%p]: edit [%p] got focus\n", lphc->self, lphc->hWndEdit );
1229 COMBO_SetFocus( lphc );
1230 break;
1232 case (EN_KILLFOCUS >> 8):
1234 TRACE("[%p]: edit [%p] lost focus\n", lphc->self, lphc->hWndEdit );
1236 /* NOTE: it seems that Windows' edit control sends an
1237 * undocumented message WM_USER + 0x1B instead of this
1238 * notification (only when it happens to be a part of
1239 * the combo). ?? - AK.
1242 COMBO_KillFocus( lphc );
1243 break;
1246 case (EN_CHANGE >> 8):
1248 * In some circumstances (when the selection of the combobox
1249 * is changed for example) we don't want the EN_CHANGE notification
1250 * to be forwarded to the parent of the combobox. This code
1251 * checks a flag that is set in these occasions and ignores the
1252 * notification.
1254 if (lphc->wState & CBF_NOLBSELECT)
1256 lphc->wState &= ~CBF_NOLBSELECT;
1258 else
1260 CBUpdateLBox( lphc, lphc->wState & CBF_DROPPED );
1263 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1264 CB_NOTIFY( lphc, CBN_EDITCHANGE );
1265 break;
1267 case (EN_UPDATE >> 8):
1268 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1269 CB_NOTIFY( lphc, CBN_EDITUPDATE );
1270 break;
1272 case (EN_ERRSPACE >> 8):
1273 CB_NOTIFY( lphc, CBN_ERRSPACE );
1276 else if( lphc->hWndLBox == hWnd )
1278 switch( (short)HIWORD(wParam) )
1280 case LBN_ERRSPACE:
1281 CB_NOTIFY( lphc, CBN_ERRSPACE );
1282 break;
1284 case LBN_DBLCLK:
1285 CB_NOTIFY( lphc, CBN_DBLCLK );
1286 break;
1288 case LBN_SELCHANGE:
1289 case LBN_SELCANCEL:
1291 TRACE("[%p]: lbox selection change [%x]\n", lphc->self, lphc->wState );
1293 /* do not roll up if selection is being tracked
1294 * by arrow keys in the dropdown listbox */
1295 if (!(lphc->wState & CBF_NOROLLUP))
1297 CBRollUp( lphc, (HIWORD(wParam) == LBN_SELCHANGE), TRUE );
1299 else lphc->wState &= ~CBF_NOROLLUP;
1301 CB_NOTIFY( lphc, CBN_SELCHANGE );
1303 if( HIWORD(wParam) == LBN_SELCHANGE)
1305 if( lphc->wState & CBF_EDIT )
1306 lphc->wState |= CBF_NOLBSELECT;
1307 CBPaintText( lphc, NULL );
1309 break;
1311 case LBN_SETFOCUS:
1312 case LBN_KILLFOCUS:
1313 /* nothing to do here since ComboLBox always resets the focus to its
1314 * combo/edit counterpart */
1315 break;
1318 return 0;
1321 /***********************************************************************
1322 * COMBO_ItemOp
1324 * Fixup an ownerdrawn item operation and pass it up to the combobox owner.
1326 static LRESULT COMBO_ItemOp( LPHEADCOMBO lphc, UINT msg, LPARAM lParam )
1328 HWND hWnd = lphc->self;
1329 UINT id = (UINT)GetWindowLongPtrW( hWnd, GWLP_ID );
1331 TRACE("[%p]: ownerdraw op %04x\n", lphc->self, msg );
1333 switch( msg )
1335 case WM_DELETEITEM:
1337 DELETEITEMSTRUCT *lpIS = (DELETEITEMSTRUCT *)lParam;
1338 lpIS->CtlType = ODT_COMBOBOX;
1339 lpIS->CtlID = id;
1340 lpIS->hwndItem = hWnd;
1341 break;
1343 case WM_DRAWITEM:
1345 DRAWITEMSTRUCT *lpIS = (DRAWITEMSTRUCT *)lParam;
1346 lpIS->CtlType = ODT_COMBOBOX;
1347 lpIS->CtlID = id;
1348 lpIS->hwndItem = hWnd;
1349 break;
1351 case WM_COMPAREITEM:
1353 COMPAREITEMSTRUCT *lpIS = (COMPAREITEMSTRUCT *)lParam;
1354 lpIS->CtlType = ODT_COMBOBOX;
1355 lpIS->CtlID = id;
1356 lpIS->hwndItem = hWnd;
1357 break;
1359 case WM_MEASUREITEM:
1361 MEASUREITEMSTRUCT *lpIS = (MEASUREITEMSTRUCT *)lParam;
1362 lpIS->CtlType = ODT_COMBOBOX;
1363 lpIS->CtlID = id;
1364 break;
1367 return SendMessageW(lphc->owner, msg, id, lParam);
1371 /***********************************************************************
1372 * COMBO_GetTextW
1374 static LRESULT COMBO_GetTextW( LPHEADCOMBO lphc, INT count, LPWSTR buf )
1376 INT length;
1378 if( lphc->wState & CBF_EDIT )
1379 return SendMessageW( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1381 /* get it from the listbox */
1383 if (!count || !buf) return 0;
1384 if( lphc->hWndLBox )
1386 INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1387 if (idx == LB_ERR) goto error;
1388 length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1389 if (length == LB_ERR) goto error;
1391 /* 'length' is without the terminating character */
1392 if (length >= count)
1394 LPWSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
1395 if (!lpBuffer) goto error;
1396 length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1398 /* truncate if buffer is too short */
1399 if (length != LB_ERR)
1401 lstrcpynW( buf, lpBuffer, count );
1402 length = count;
1404 HeapFree( GetProcessHeap(), 0, lpBuffer );
1406 else length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1408 if (length == LB_ERR) return 0;
1409 return length;
1412 error: /* error - truncate string, return zero */
1413 buf[0] = 0;
1414 return 0;
1418 /***********************************************************************
1419 * COMBO_GetTextA
1421 * NOTE! LB_GETTEXT does not count terminating \0, WM_GETTEXT does.
1422 * also LB_GETTEXT might return values < 0, WM_GETTEXT doesn't.
1424 static LRESULT COMBO_GetTextA( LPHEADCOMBO lphc, INT count, LPSTR buf )
1426 INT length;
1428 if( lphc->wState & CBF_EDIT )
1429 return SendMessageA( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1431 /* get it from the listbox */
1433 if (!count || !buf) return 0;
1434 if( lphc->hWndLBox )
1436 INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1437 if (idx == LB_ERR) goto error;
1438 length = SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1439 if (length == LB_ERR) goto error;
1441 /* 'length' is without the terminating character */
1442 if (length >= count)
1444 LPSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) );
1445 if (!lpBuffer) goto error;
1446 length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1448 /* truncate if buffer is too short */
1449 if (length != LB_ERR)
1451 lstrcpynA( buf, lpBuffer, count );
1452 length = count;
1454 HeapFree( GetProcessHeap(), 0, lpBuffer );
1456 else length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1458 if (length == LB_ERR) return 0;
1459 return length;
1462 error: /* error - truncate string, return zero */
1463 buf[0] = 0;
1464 return 0;
1468 /***********************************************************************
1469 * CBResetPos
1471 * This function sets window positions according to the updated
1472 * component placement struct.
1474 static void CBResetPos(
1475 LPHEADCOMBO lphc,
1476 const RECT *rectEdit,
1477 const RECT *rectLB,
1478 BOOL bRedraw)
1480 BOOL bDrop = (CB_GETTYPE(lphc) != CBS_SIMPLE);
1482 /* NOTE: logs sometimes have WM_LBUTTONUP before a cascade of
1483 * sizing messages */
1485 if( lphc->wState & CBF_EDIT )
1486 SetWindowPos( lphc->hWndEdit, 0,
1487 rectEdit->left, rectEdit->top,
1488 rectEdit->right - rectEdit->left,
1489 rectEdit->bottom - rectEdit->top,
1490 SWP_NOZORDER | SWP_NOACTIVATE | ((bDrop) ? SWP_NOREDRAW : 0) );
1492 SetWindowPos( lphc->hWndLBox, 0,
1493 rectLB->left, rectLB->top,
1494 rectLB->right - rectLB->left,
1495 rectLB->bottom - rectLB->top,
1496 SWP_NOACTIVATE | SWP_NOZORDER | ((bDrop) ? SWP_NOREDRAW : 0) );
1498 if( bDrop )
1500 if( lphc->wState & CBF_DROPPED )
1502 lphc->wState &= ~CBF_DROPPED;
1503 ShowWindow( lphc->hWndLBox, SW_HIDE );
1506 if( bRedraw && !(lphc->wState & CBF_NOREDRAW) )
1507 RedrawWindow( lphc->self, NULL, 0,
1508 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );
1513 /***********************************************************************
1514 * COMBO_Size
1516 static void COMBO_Size( LPHEADCOMBO lphc )
1519 * Those controls are always the same height. So we have to make sure
1520 * they are not resized to another value.
1522 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
1524 int newComboHeight, curComboHeight, curComboWidth;
1525 RECT rc;
1527 GetWindowRect(lphc->self, &rc);
1528 curComboHeight = rc.bottom - rc.top;
1529 curComboWidth = rc.right - rc.left;
1530 newComboHeight = CBGetTextAreaHeight(lphc->self, lphc) + 2*COMBO_YBORDERSIZE();
1533 * Resizing a combobox has another side effect, it resizes the dropped
1534 * rectangle as well. However, it does it only if the new height for the
1535 * combobox is more than the height it should have. In other words,
1536 * if the application resizing the combobox only had the intention to resize
1537 * the actual control, for example, to do the layout of a dialog that is
1538 * resized, the height of the dropdown is not changed.
1540 if( curComboHeight > newComboHeight )
1542 TRACE("oldComboHeight=%d, newComboHeight=%d, oldDropBottom=%d, oldDropTop=%d\n",
1543 curComboHeight, newComboHeight, lphc->droppedRect.bottom,
1544 lphc->droppedRect.top);
1545 lphc->droppedRect.bottom = lphc->droppedRect.top + curComboHeight - newComboHeight;
1548 * Restore original height
1550 if( curComboHeight != newComboHeight )
1551 SetWindowPos(lphc->self, 0, 0, 0, curComboWidth, newComboHeight,
1552 SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOREDRAW);
1555 CBCalcPlacement(lphc->self,
1556 lphc,
1557 &lphc->textRect,
1558 &lphc->buttonRect,
1559 &lphc->droppedRect);
1561 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1565 /***********************************************************************
1566 * COMBO_Font
1568 static void COMBO_Font( LPHEADCOMBO lphc, HFONT hFont, BOOL bRedraw )
1571 * Set the font
1573 lphc->hFont = hFont;
1576 * Propagate to owned windows.
1578 if( lphc->wState & CBF_EDIT )
1579 SendMessageW(lphc->hWndEdit, WM_SETFONT, (WPARAM)hFont, bRedraw);
1580 SendMessageW(lphc->hWndLBox, WM_SETFONT, (WPARAM)hFont, bRedraw);
1583 * Redo the layout of the control.
1585 if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1587 CBCalcPlacement(lphc->self,
1588 lphc,
1589 &lphc->textRect,
1590 &lphc->buttonRect,
1591 &lphc->droppedRect);
1593 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1595 else
1597 CBForceDummyResize(lphc);
1602 /***********************************************************************
1603 * COMBO_SetItemHeight
1605 static LRESULT COMBO_SetItemHeight( LPHEADCOMBO lphc, INT index, INT height )
1607 LRESULT lRet = CB_ERR;
1609 if( index == -1 ) /* set text field height */
1611 if( height < 32768 )
1613 lphc->editHeight = height + 2; /* Is the 2 for 2*EDIT_CONTROL_PADDING? */
1616 * Redo the layout of the control.
1618 if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1620 CBCalcPlacement(lphc->self,
1621 lphc,
1622 &lphc->textRect,
1623 &lphc->buttonRect,
1624 &lphc->droppedRect);
1626 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1628 else
1630 CBForceDummyResize(lphc);
1633 lRet = height;
1636 else if ( CB_OWNERDRAWN(lphc) ) /* set listbox item height */
1637 lRet = SendMessageW(lphc->hWndLBox, LB_SETITEMHEIGHT, index, height);
1638 return lRet;
1641 /***********************************************************************
1642 * COMBO_SelectString
1644 static LRESULT COMBO_SelectString( LPHEADCOMBO lphc, INT start, LPARAM pText, BOOL unicode )
1646 INT index = unicode ? SendMessageW(lphc->hWndLBox, LB_SELECTSTRING, start, pText) :
1647 SendMessageA(lphc->hWndLBox, LB_SELECTSTRING, start, pText);
1648 if( index >= 0 )
1650 if( lphc->wState & CBF_EDIT )
1651 CBUpdateEdit( lphc, index );
1652 else
1654 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1657 return (LRESULT)index;
1660 /***********************************************************************
1661 * COMBO_LButtonDown
1663 static void COMBO_LButtonDown( LPHEADCOMBO lphc, LPARAM lParam )
1665 POINT pt;
1666 BOOL bButton;
1667 HWND hWnd = lphc->self;
1669 pt.x = (short)LOWORD(lParam);
1670 pt.y = (short)HIWORD(lParam);
1671 bButton = PtInRect(&lphc->buttonRect, pt);
1673 if( (CB_GETTYPE(lphc) == CBS_DROPDOWNLIST) ||
1674 (bButton && (CB_GETTYPE(lphc) == CBS_DROPDOWN)) )
1676 lphc->wState |= CBF_BUTTONDOWN;
1677 if( lphc->wState & CBF_DROPPED )
1679 /* got a click to cancel selection */
1681 lphc->wState &= ~CBF_BUTTONDOWN;
1682 CBRollUp( lphc, TRUE, FALSE );
1683 if( !IsWindow( hWnd ) ) return;
1685 if( lphc->wState & CBF_CAPTURE )
1687 lphc->wState &= ~CBF_CAPTURE;
1688 ReleaseCapture();
1691 else
1693 /* drop down the listbox and start tracking */
1695 lphc->wState |= CBF_CAPTURE;
1696 SetCapture( hWnd );
1697 CBDropDown( lphc );
1699 if( bButton ) CBRepaintButton( lphc );
1703 /***********************************************************************
1704 * COMBO_LButtonUp
1706 * Release capture and stop tracking if needed.
1708 static void COMBO_LButtonUp( LPHEADCOMBO lphc )
1710 if( lphc->wState & CBF_CAPTURE )
1712 lphc->wState &= ~CBF_CAPTURE;
1713 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1715 INT index = CBUpdateLBox( lphc, TRUE );
1716 /* Update edit only if item is in the list */
1717 if(index >= 0)
1719 lphc->wState |= CBF_NOLBSELECT;
1720 CBUpdateEdit( lphc, index );
1721 lphc->wState &= ~CBF_NOLBSELECT;
1724 ReleaseCapture();
1725 SetCapture(lphc->hWndLBox);
1728 if( lphc->wState & CBF_BUTTONDOWN )
1730 lphc->wState &= ~CBF_BUTTONDOWN;
1731 CBRepaintButton( lphc );
1735 /***********************************************************************
1736 * COMBO_MouseMove
1738 * Two things to do - track combo button and release capture when
1739 * pointer goes into the listbox.
1741 static void COMBO_MouseMove( LPHEADCOMBO lphc, WPARAM wParam, LPARAM lParam )
1743 POINT pt;
1744 RECT lbRect;
1746 pt.x = (short)LOWORD(lParam);
1747 pt.y = (short)HIWORD(lParam);
1749 if( lphc->wState & CBF_BUTTONDOWN )
1751 BOOL bButton;
1753 bButton = PtInRect(&lphc->buttonRect, pt);
1755 if( !bButton )
1757 lphc->wState &= ~CBF_BUTTONDOWN;
1758 CBRepaintButton( lphc );
1762 GetClientRect( lphc->hWndLBox, &lbRect );
1763 MapWindowPoints( lphc->self, lphc->hWndLBox, &pt, 1 );
1764 if( PtInRect(&lbRect, pt) )
1766 lphc->wState &= ~CBF_CAPTURE;
1767 ReleaseCapture();
1768 if( CB_GETTYPE(lphc) == CBS_DROPDOWN ) CBUpdateLBox( lphc, TRUE );
1770 /* hand over pointer tracking */
1771 SendMessageW(lphc->hWndLBox, WM_LBUTTONDOWN, wParam, lParam);
1775 static LRESULT COMBO_GetComboBoxInfo(const HEADCOMBO *lphc, COMBOBOXINFO *pcbi)
1777 if (!pcbi || (pcbi->cbSize < sizeof(COMBOBOXINFO)))
1778 return FALSE;
1780 pcbi->rcItem = lphc->textRect;
1781 pcbi->rcButton = lphc->buttonRect;
1782 pcbi->stateButton = 0;
1783 if (lphc->wState & CBF_BUTTONDOWN)
1784 pcbi->stateButton |= STATE_SYSTEM_PRESSED;
1785 if (IsRectEmpty(&lphc->buttonRect))
1786 pcbi->stateButton |= STATE_SYSTEM_INVISIBLE;
1787 pcbi->hwndCombo = lphc->self;
1788 pcbi->hwndItem = lphc->hWndEdit;
1789 pcbi->hwndList = lphc->hWndLBox;
1790 return TRUE;
1793 static char *strdupA(LPCSTR str)
1795 char *ret;
1796 DWORD len;
1798 if(!str) return NULL;
1800 len = strlen(str);
1801 ret = HeapAlloc(GetProcessHeap(), 0, len + 1);
1802 memcpy(ret, str, len + 1);
1803 return ret;
1806 /***********************************************************************
1807 * ComboWndProc_common
1809 LRESULT ComboWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1811 LPHEADCOMBO lphc = (LPHEADCOMBO)GetWindowLongPtrW( hwnd, 0 );
1813 TRACE("[%p]: msg %s wp %08lx lp %08lx\n",
1814 hwnd, SPY_GetMsgName(message, hwnd), wParam, lParam );
1816 if (!IsWindow(hwnd)) return 0;
1818 if( lphc || message == WM_NCCREATE )
1819 switch(message)
1822 /* System messages */
1824 case WM_NCCREATE:
1826 LONG style = unicode ? ((LPCREATESTRUCTW)lParam)->style :
1827 ((LPCREATESTRUCTA)lParam)->style;
1828 return COMBO_NCCreate(hwnd, style);
1830 case WM_NCDESTROY:
1831 COMBO_NCDestroy(lphc);
1832 break;/* -> DefWindowProc */
1834 case WM_CREATE:
1836 HWND hwndParent;
1837 LONG style;
1838 if(unicode)
1840 hwndParent = ((LPCREATESTRUCTW)lParam)->hwndParent;
1841 style = ((LPCREATESTRUCTW)lParam)->style;
1843 else
1845 hwndParent = ((LPCREATESTRUCTA)lParam)->hwndParent;
1846 style = ((LPCREATESTRUCTA)lParam)->style;
1848 return COMBO_Create(hwnd, lphc, hwndParent, style, unicode);
1851 case WM_PRINTCLIENT:
1852 /* Fallthrough */
1853 case WM_PAINT:
1854 /* wParam may contain a valid HDC! */
1855 return COMBO_Paint(lphc, (HDC)wParam);
1857 case WM_ERASEBKGND:
1858 /* do all painting in WM_PAINT like Windows does */
1859 return 1;
1861 case WM_GETDLGCODE:
1863 LRESULT result = DLGC_WANTARROWS | DLGC_WANTCHARS;
1864 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1866 int vk = (int)((LPMSG)lParam)->wParam;
1868 if ((vk == VK_RETURN || vk == VK_ESCAPE) && (lphc->wState & CBF_DROPPED))
1869 result |= DLGC_WANTMESSAGE;
1871 return result;
1873 case WM_SIZE:
1874 if( lphc->hWndLBox &&
1875 !(lphc->wState & CBF_NORESIZE) ) COMBO_Size( lphc );
1876 return TRUE;
1877 case WM_SETFONT:
1878 COMBO_Font( lphc, (HFONT)wParam, (BOOL)lParam );
1879 return TRUE;
1880 case WM_GETFONT:
1881 return (LRESULT)lphc->hFont;
1882 case WM_SETFOCUS:
1883 if( lphc->wState & CBF_EDIT ) {
1884 SetFocus( lphc->hWndEdit );
1885 /* The first time focus is received, select all the text */
1886 if( !(lphc->wState & CBF_BEENFOCUSED) ) {
1887 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, -1);
1888 lphc->wState |= CBF_BEENFOCUSED;
1891 else
1892 COMBO_SetFocus( lphc );
1893 return TRUE;
1894 case WM_KILLFOCUS:
1896 HWND hwndFocus = WIN_GetFullHandle( (HWND)wParam );
1897 if( !hwndFocus ||
1898 (hwndFocus != lphc->hWndEdit && hwndFocus != lphc->hWndLBox ))
1899 COMBO_KillFocus( lphc );
1900 return TRUE;
1902 case WM_COMMAND:
1903 return COMBO_Command( lphc, wParam, WIN_GetFullHandle( (HWND)lParam ) );
1904 case WM_GETTEXT:
1905 return unicode ? COMBO_GetTextW( lphc, wParam, (LPWSTR)lParam )
1906 : COMBO_GetTextA( lphc, wParam, (LPSTR)lParam );
1907 case WM_SETTEXT:
1908 case WM_GETTEXTLENGTH:
1909 case WM_CLEAR:
1910 if ((message == WM_GETTEXTLENGTH) && !ISWIN31 && !(lphc->wState & CBF_EDIT))
1912 int j = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1913 if (j == -1) return 0;
1914 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, j, 0) :
1915 SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, j, 0);
1917 else if( lphc->wState & CBF_EDIT )
1919 LRESULT ret;
1920 lphc->wState |= CBF_NOEDITNOTIFY;
1921 ret = unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1922 SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1923 lphc->wState &= ~CBF_NOEDITNOTIFY;
1924 return ret;
1926 else return CB_ERR;
1927 case WM_CUT:
1928 case WM_PASTE:
1929 case WM_COPY:
1930 if( lphc->wState & CBF_EDIT )
1932 return unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1933 SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1935 else return CB_ERR;
1937 case WM_DRAWITEM:
1938 case WM_DELETEITEM:
1939 case WM_COMPAREITEM:
1940 case WM_MEASUREITEM:
1941 return COMBO_ItemOp(lphc, message, lParam);
1942 case WM_ENABLE:
1943 if( lphc->wState & CBF_EDIT )
1944 EnableWindow( lphc->hWndEdit, (BOOL)wParam );
1945 EnableWindow( lphc->hWndLBox, (BOOL)wParam );
1947 /* Force the control to repaint when the enabled state changes. */
1948 InvalidateRect(lphc->self, NULL, TRUE);
1949 return TRUE;
1950 case WM_SETREDRAW:
1951 if( wParam )
1952 lphc->wState &= ~CBF_NOREDRAW;
1953 else
1954 lphc->wState |= CBF_NOREDRAW;
1956 if( lphc->wState & CBF_EDIT )
1957 SendMessageW(lphc->hWndEdit, message, wParam, lParam);
1958 SendMessageW(lphc->hWndLBox, message, wParam, lParam);
1959 return 0;
1960 case WM_SYSKEYDOWN:
1961 if( KEYDATA_ALT & HIWORD(lParam) )
1962 if( wParam == VK_UP || wParam == VK_DOWN )
1963 COMBO_FlipListbox( lphc, FALSE, FALSE );
1964 return 0;
1966 case WM_KEYDOWN:
1967 if ((wParam == VK_RETURN || wParam == VK_ESCAPE) &&
1968 (lphc->wState & CBF_DROPPED))
1970 CBRollUp( lphc, wParam == VK_RETURN, FALSE );
1971 return TRUE;
1973 else if ((wParam == VK_F4) && !(lphc->wState & CBF_EUI))
1975 COMBO_FlipListbox( lphc, FALSE, FALSE );
1976 return TRUE;
1978 /* fall through */
1979 case WM_CHAR:
1980 case WM_IME_CHAR:
1982 HWND hwndTarget;
1984 if( lphc->wState & CBF_EDIT )
1985 hwndTarget = lphc->hWndEdit;
1986 else
1987 hwndTarget = lphc->hWndLBox;
1989 return unicode ? SendMessageW(hwndTarget, message, wParam, lParam) :
1990 SendMessageA(hwndTarget, message, wParam, lParam);
1992 case WM_LBUTTONDOWN:
1993 if( !(lphc->wState & CBF_FOCUSED) ) SetFocus( lphc->self );
1994 if( lphc->wState & CBF_FOCUSED ) COMBO_LButtonDown( lphc, lParam );
1995 return TRUE;
1996 case WM_LBUTTONUP:
1997 COMBO_LButtonUp( lphc );
1998 return TRUE;
1999 case WM_MOUSEMOVE:
2000 if( lphc->wState & CBF_CAPTURE )
2001 COMBO_MouseMove( lphc, wParam, lParam );
2002 return TRUE;
2004 case WM_MOUSEWHEEL:
2005 if (wParam & (MK_SHIFT | MK_CONTROL))
2006 return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2007 DefWindowProcA(hwnd, message, wParam, lParam);
2009 if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_UP, 0);
2010 if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_DOWN, 0);
2011 return TRUE;
2013 /* Combo messages */
2015 case CB_ADDSTRING:
2016 if( unicode )
2018 if( lphc->dwStyle & CBS_LOWERCASE )
2019 CharLowerW((LPWSTR)lParam);
2020 else if( lphc->dwStyle & CBS_UPPERCASE )
2021 CharUpperW((LPWSTR)lParam);
2022 return SendMessageW(lphc->hWndLBox, LB_ADDSTRING, 0, lParam);
2024 else /* unlike the unicode version, the ansi version does not overwrite
2025 the string if converting case */
2027 char *string = NULL;
2028 LRESULT ret;
2029 if( lphc->dwStyle & CBS_LOWERCASE )
2031 string = strdupA((LPSTR)lParam);
2032 CharLowerA(string);
2035 else if( lphc->dwStyle & CBS_UPPERCASE )
2037 string = strdupA((LPSTR)lParam);
2038 CharUpperA(string);
2041 ret = SendMessageA(lphc->hWndLBox, LB_ADDSTRING, 0, string ? (LPARAM)string : lParam);
2042 HeapFree(GetProcessHeap(), 0, string);
2043 return ret;
2045 case CB_INSERTSTRING:
2046 if( unicode )
2048 if( lphc->dwStyle & CBS_LOWERCASE )
2049 CharLowerW((LPWSTR)lParam);
2050 else if( lphc->dwStyle & CBS_UPPERCASE )
2051 CharUpperW((LPWSTR)lParam);
2052 return SendMessageW(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2054 else
2056 if( lphc->dwStyle & CBS_LOWERCASE )
2057 CharLowerA((LPSTR)lParam);
2058 else if( lphc->dwStyle & CBS_UPPERCASE )
2059 CharUpperA((LPSTR)lParam);
2061 return SendMessageA(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2063 case CB_DELETESTRING:
2064 return unicode ? SendMessageW(lphc->hWndLBox, LB_DELETESTRING, wParam, 0) :
2065 SendMessageA(lphc->hWndLBox, LB_DELETESTRING, wParam, 0);
2066 case CB_SELECTSTRING:
2067 return COMBO_SelectString(lphc, (INT)wParam, lParam, unicode);
2068 case CB_FINDSTRING:
2069 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam) :
2070 SendMessageA(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam);
2071 case CB_FINDSTRINGEXACT:
2072 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam) :
2073 SendMessageA(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam);
2074 case CB_SETITEMHEIGHT:
2075 return COMBO_SetItemHeight( lphc, (INT)wParam, (INT)lParam);
2076 case CB_GETITEMHEIGHT:
2077 if( (INT)wParam >= 0 ) /* listbox item */
2078 return SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, wParam, 0);
2079 return CBGetTextAreaHeight(hwnd, lphc);
2080 case CB_RESETCONTENT:
2081 SendMessageW(lphc->hWndLBox, LB_RESETCONTENT, 0, 0);
2082 if( (lphc->wState & CBF_EDIT) && CB_HASSTRINGS(lphc) )
2084 static const WCHAR empty_stringW[] = { 0 };
2085 SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, (LPARAM)empty_stringW);
2087 else
2088 InvalidateRect(lphc->self, NULL, TRUE);
2089 return TRUE;
2090 case CB_INITSTORAGE:
2091 return SendMessageW(lphc->hWndLBox, LB_INITSTORAGE, wParam, lParam);
2092 case CB_GETHORIZONTALEXTENT:
2093 return SendMessageW(lphc->hWndLBox, LB_GETHORIZONTALEXTENT, 0, 0);
2094 case CB_SETHORIZONTALEXTENT:
2095 return SendMessageW(lphc->hWndLBox, LB_SETHORIZONTALEXTENT, wParam, 0);
2096 case CB_GETTOPINDEX:
2097 return SendMessageW(lphc->hWndLBox, LB_GETTOPINDEX, 0, 0);
2098 case CB_GETLOCALE:
2099 return SendMessageW(lphc->hWndLBox, LB_GETLOCALE, 0, 0);
2100 case CB_SETLOCALE:
2101 return SendMessageW(lphc->hWndLBox, LB_SETLOCALE, wParam, 0);
2102 case CB_SETDROPPEDWIDTH:
2103 if( (CB_GETTYPE(lphc) == CBS_SIMPLE) ||
2104 (INT)wParam >= 32768 )
2105 return CB_ERR;
2106 /* new value must be higher than combobox width */
2107 if((INT)wParam >= lphc->droppedRect.right - lphc->droppedRect.left)
2108 lphc->droppedWidth = wParam;
2109 else if(wParam)
2110 lphc->droppedWidth = 0;
2112 /* recalculate the combobox area */
2113 CBCalcPlacement(hwnd, lphc, &lphc->textRect, &lphc->buttonRect, &lphc->droppedRect );
2115 /* fall through */
2116 case CB_GETDROPPEDWIDTH:
2117 if( lphc->droppedWidth )
2118 return lphc->droppedWidth;
2119 return lphc->droppedRect.right - lphc->droppedRect.left;
2120 case CB_GETDROPPEDCONTROLRECT:
2121 if( lParam ) CBGetDroppedControlRect(lphc, (LPRECT)lParam );
2122 return CB_OKAY;
2123 case CB_GETDROPPEDSTATE:
2124 return (lphc->wState & CBF_DROPPED) != 0;
2125 case CB_DIR:
2126 return unicode ? SendMessageW(lphc->hWndLBox, LB_DIR, wParam, lParam) :
2127 SendMessageA(lphc->hWndLBox, LB_DIR, wParam, lParam);
2129 case CB_SHOWDROPDOWN:
2130 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
2132 if( wParam )
2134 if( !(lphc->wState & CBF_DROPPED) )
2135 CBDropDown( lphc );
2137 else
2138 if( lphc->wState & CBF_DROPPED )
2139 CBRollUp( lphc, FALSE, TRUE );
2141 return TRUE;
2142 case CB_GETCOUNT:
2143 return SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
2144 case CB_GETCURSEL:
2145 return SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
2146 case CB_SETCURSEL:
2147 lParam = SendMessageW(lphc->hWndLBox, LB_SETCURSEL, wParam, 0);
2148 if( lParam >= 0 )
2149 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, wParam, 0);
2151 /* no LBN_SELCHANGE in this case, update manually */
2152 CBPaintText( lphc, NULL );
2153 lphc->wState &= ~CBF_SELCHANGE;
2154 return lParam;
2155 case CB_GETLBTEXT:
2156 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXT, wParam, lParam) :
2157 SendMessageA(lphc->hWndLBox, LB_GETTEXT, wParam, lParam);
2158 case CB_GETLBTEXTLEN:
2159 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0) :
2160 SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0);
2161 case CB_GETITEMDATA:
2162 return SendMessageW(lphc->hWndLBox, LB_GETITEMDATA, wParam, 0);
2163 case CB_SETITEMDATA:
2164 return SendMessageW(lphc->hWndLBox, LB_SETITEMDATA, wParam, lParam);
2165 case CB_GETEDITSEL:
2166 /* Edit checks passed parameters itself */
2167 if( lphc->wState & CBF_EDIT )
2168 return SendMessageW(lphc->hWndEdit, EM_GETSEL, wParam, lParam);
2169 return CB_ERR;
2170 case CB_SETEDITSEL:
2171 if( lphc->wState & CBF_EDIT )
2172 return SendMessageW(lphc->hWndEdit, EM_SETSEL,
2173 (INT)(SHORT)LOWORD(lParam), (INT)(SHORT)HIWORD(lParam) );
2174 return CB_ERR;
2175 case CB_SETEXTENDEDUI:
2176 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
2177 return CB_ERR;
2178 if( wParam )
2179 lphc->wState |= CBF_EUI;
2180 else lphc->wState &= ~CBF_EUI;
2181 return CB_OKAY;
2182 case CB_GETEXTENDEDUI:
2183 return (lphc->wState & CBF_EUI) != 0;
2184 case CB_GETCOMBOBOXINFO:
2185 return COMBO_GetComboBoxInfo(lphc, (COMBOBOXINFO *)lParam);
2186 case CB_LIMITTEXT:
2187 if( lphc->wState & CBF_EDIT )
2188 return SendMessageW(lphc->hWndEdit, EM_LIMITTEXT, wParam, lParam);
2189 return TRUE;
2190 default:
2191 if (message >= WM_USER)
2192 WARN("unknown msg WM_USER+%04x wp=%04lx lp=%08lx\n",
2193 message - WM_USER, wParam, lParam );
2194 break;
2196 return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2197 DefWindowProcA(hwnd, message, wParam, lParam);
2200 /*************************************************************************
2201 * GetComboBoxInfo (USER32.@)
2203 BOOL WINAPI GetComboBoxInfo(HWND hwndCombo, /* [in] handle to combo box */
2204 PCOMBOBOXINFO pcbi /* [in/out] combo box information */)
2206 TRACE("(%p, %p)\n", hwndCombo, pcbi);
2207 return SendMessageW(hwndCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)pcbi);