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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
31 * - ComboBox_[GS]etMinVisible()
32 * - CB_GETCOMBOBOXINFO
33 * - CB_GETMINVISIBLE, CB_SETMINVISIBLE
45 #include "wine/winuser16.h"
46 #include "wine/unicode.h"
47 #include "user_private.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(combo);
56 /* bits in the dwKeyData */
57 #define KEYDATA_ALT 0x2000
58 #define KEYDATA_PREVSTATE 0x4000
61 * Additional combo box definitions
64 #define CB_NOTIFY( lphc, code ) \
65 (SendMessageW((lphc)->owner, WM_COMMAND, \
66 MAKEWPARAM(GetWindowLongPtrW((lphc)->self,GWLP_ID), (code)), (LPARAM)(lphc)->self))
68 #define CB_DISABLED( lphc ) (!IsWindowEnabled((lphc)->self))
69 #define CB_OWNERDRAWN( lphc ) ((lphc)->dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
70 #define CB_HASSTRINGS( lphc ) ((lphc)->dwStyle & CBS_HASSTRINGS)
71 #define CB_HWND( lphc ) ((lphc)->self)
72 #define CB_GETTYPE( lphc ) ((lphc)->dwStyle & (CBS_DROPDOWNLIST))
74 #define ISWIN31 (LOWORD(GetVersion()) == 0x0a03)
79 static HBITMAP hComboBmp = 0;
80 static UINT CBitHeight, CBitWidth;
83 * Look and feel dependent "constants"
86 #define COMBO_YBORDERGAP 5
87 #define COMBO_XBORDERSIZE() 2
88 #define COMBO_YBORDERSIZE() 2
89 #define COMBO_EDITBUTTONSPACE() 0
90 #define EDIT_CONTROL_PADDING() 1
92 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
93 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
95 /*********************************************************************
96 * combo class descriptor
98 const struct builtin_class_descr COMBO_builtin_class =
100 "ComboBox", /* name */
101 CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, /* style */
102 ComboWndProcA, /* procA */
103 ComboWndProcW, /* procW */
104 sizeof(HEADCOMBO *), /* extra */
105 IDC_ARROW, /* cursor */
110 /***********************************************************************
113 * Load combo button bitmap.
115 static BOOL COMBO_Init()
119 if( hComboBmp ) return TRUE;
120 if( (hDC = CreateCompatibleDC(0)) )
123 if( (hComboBmp = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_COMBO))) )
129 GetObjectW( hComboBmp, sizeof(bm), &bm );
130 CBitHeight = bm.bmHeight;
131 CBitWidth = bm.bmWidth;
133 TRACE("combo bitmap [%i,%i]\n", CBitWidth, CBitHeight );
135 hPrevB = SelectObject( hDC, hComboBmp);
136 SetRect( &r, 0, 0, CBitWidth, CBitHeight );
137 InvertRect( hDC, &r );
138 SelectObject( hDC, hPrevB );
147 /***********************************************************************
150 static LRESULT COMBO_NCCreate(HWND hwnd, LONG style)
154 if (COMBO_Init() && (lphc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HEADCOMBO))) )
157 SetWindowLongW( hwnd, 0, (LONG)lphc );
159 /* some braindead apps do try to use scrollbar/border flags */
161 lphc->dwStyle = style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL);
162 SetWindowLongW( hwnd, GWL_STYLE, style & ~(WS_BORDER | WS_HSCROLL | WS_VSCROLL) );
165 * We also have to remove the client edge style to make sure
166 * we don't end-up with a non client area.
168 SetWindowLongW( hwnd, GWL_EXSTYLE,
169 GetWindowLongW( hwnd, GWL_EXSTYLE ) & ~WS_EX_CLIENTEDGE );
171 if( !(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) )
172 lphc->dwStyle |= CBS_HASSTRINGS;
173 if( !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) )
174 lphc->wState |= CBF_NOTIFY;
176 TRACE("[%p], style = %08x\n", lphc, lphc->dwStyle );
182 /***********************************************************************
185 static LRESULT COMBO_NCDestroy( LPHEADCOMBO lphc )
190 TRACE("[%p]: freeing storage\n", lphc->self);
192 if( (CB_GETTYPE(lphc) != CBS_SIMPLE) && lphc->hWndLBox )
193 DestroyWindow( lphc->hWndLBox );
195 SetWindowLongW( lphc->self, 0, 0 );
196 HeapFree( GetProcessHeap(), 0, lphc );
201 /***********************************************************************
202 * CBGetTextAreaHeight
204 * This method will calculate the height of the text area of the
206 * The height of the text area is set in two ways.
207 * It can be set explicitly through a combobox message or through a
208 * WM_MEASUREITEM callback.
209 * If this is not the case, the height is set to 13 dialog units.
210 * This height was determined through experimentation.
212 static INT CBGetTextAreaHeight(
218 if( lphc->editHeight ) /* explicitly set height */
220 iTextItemHeight = lphc->editHeight;
225 HDC hDC = GetDC(hwnd);
230 hPrevFont = SelectObject( hDC, lphc->hFont );
232 GetTextMetricsW(hDC, &tm);
234 baseUnitY = tm.tmHeight;
237 SelectObject( hDC, hPrevFont );
239 ReleaseDC(hwnd, hDC);
241 iTextItemHeight = ((13 * baseUnitY) / 8);
244 * This "formula" calculates the height of the complete control.
245 * To calculate the height of the text area, we have to remove the
248 iTextItemHeight -= 2*COMBO_YBORDERSIZE();
252 * Check the ownerdraw case if we haven't asked the parent the size
255 if ( CB_OWNERDRAWN(lphc) &&
256 (lphc->wState & CBF_MEASUREITEM) )
258 MEASUREITEMSTRUCT measureItem;
260 INT originalItemHeight = iTextItemHeight;
261 UINT id = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
264 * We use the client rect for the width of the item.
266 GetClientRect(hwnd, &clientRect);
268 lphc->wState &= ~CBF_MEASUREITEM;
271 * Send a first one to measure the size of the text area
273 measureItem.CtlType = ODT_COMBOBOX;
274 measureItem.CtlID = id;
275 measureItem.itemID = -1;
276 measureItem.itemWidth = clientRect.right;
277 measureItem.itemHeight = iTextItemHeight - 6; /* ownerdrawn cb is taller */
278 measureItem.itemData = 0;
279 SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
280 iTextItemHeight = 6 + measureItem.itemHeight;
283 * Send a second one in the case of a fixed ownerdraw list to calculate the
284 * size of the list items. (we basically do this on behalf of the listbox)
286 if (lphc->dwStyle & CBS_OWNERDRAWFIXED)
288 measureItem.CtlType = ODT_COMBOBOX;
289 measureItem.CtlID = id;
290 measureItem.itemID = 0;
291 measureItem.itemWidth = clientRect.right;
292 measureItem.itemHeight = originalItemHeight;
293 measureItem.itemData = 0;
294 SendMessageW(lphc->owner, WM_MEASUREITEM, id, (LPARAM)&measureItem);
295 lphc->fixedOwnerDrawHeight = measureItem.itemHeight;
299 * Keep the size for the next time
301 lphc->editHeight = iTextItemHeight;
304 return iTextItemHeight;
307 /***********************************************************************
310 * The dummy resize is used for listboxes that have a popup to trigger
311 * a re-arranging of the contents of the combobox and the recalculation
312 * of the size of the "real" control window.
314 static void CBForceDummyResize(
320 newComboHeight = CBGetTextAreaHeight(lphc->self,lphc) + 2*COMBO_YBORDERSIZE();
322 GetWindowRect(lphc->self, &windowRect);
325 * We have to be careful, resizing a combobox also has the meaning that the
326 * dropped rect will be resized. In this case, we want to trigger a resize
327 * to recalculate layout but we don't want to change the dropped rectangle
328 * So, we pass the height of text area of control as the height.
329 * this will cancel-out in the processing of the WM_WINDOWPOSCHANGING
332 SetWindowPos( lphc->self,
335 windowRect.right - windowRect.left,
337 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
340 /***********************************************************************
343 * Set up component coordinates given valid lphc->RectCombo.
345 static void CBCalcPlacement(
353 * Again, start with the client rectangle.
355 GetClientRect(hwnd, lprEdit);
360 InflateRect(lprEdit, -COMBO_XBORDERSIZE(), -COMBO_YBORDERSIZE());
363 * Chop off the bottom part to fit with the height of the text area.
365 lprEdit->bottom = lprEdit->top + CBGetTextAreaHeight(hwnd, lphc);
368 * The button starts the same vertical position as the text area.
370 CopyRect(lprButton, lprEdit);
373 * If the combobox is "simple" there is no button.
375 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
376 lprButton->left = lprButton->right = lprButton->bottom = 0;
380 * Let's assume the combobox button is the same width as the
382 * size the button horizontally and cut-off the text area.
384 lprButton->left = lprButton->right - GetSystemMetrics(SM_CXVSCROLL);
385 lprEdit->right = lprButton->left;
389 * In the case of a dropdown, there is an additional spacing between the
390 * text area and the button.
392 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
394 lprEdit->right -= COMBO_EDITBUTTONSPACE();
398 * If we have an edit control, we space it away from the borders slightly.
400 if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
402 InflateRect(lprEdit, -EDIT_CONTROL_PADDING(), -EDIT_CONTROL_PADDING());
406 * Adjust the size of the listbox popup.
408 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
411 * Use the client rectangle to initialize the listbox rectangle
413 GetClientRect(hwnd, lprLB);
416 * Then, chop-off the top part.
418 lprLB->top = lprEdit->bottom + COMBO_YBORDERSIZE();
423 * Make sure the dropped width is as large as the combobox itself.
425 if (lphc->droppedWidth < (lprButton->right + COMBO_XBORDERSIZE()))
427 lprLB->right = lprLB->left + (lprButton->right + COMBO_XBORDERSIZE());
430 * In the case of a dropdown, the popup listbox is offset to the right.
431 * so, we want to make sure it's flush with the right side of the
434 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
435 lprLB->right -= COMBO_EDITBUTTONSPACE();
438 lprLB->right = lprLB->left + lphc->droppedWidth;
441 TRACE("\ttext\t= (%ld,%ld-%ld,%ld)\n",
442 lprEdit->left, lprEdit->top, lprEdit->right, lprEdit->bottom);
444 TRACE("\tbutton\t= (%ld,%ld-%ld,%ld)\n",
445 lprButton->left, lprButton->top, lprButton->right, lprButton->bottom);
447 TRACE("\tlbox\t= (%ld,%ld-%ld,%ld)\n",
448 lprLB->left, lprLB->top, lprLB->right, lprLB->bottom );
451 /***********************************************************************
452 * CBGetDroppedControlRect
454 static void CBGetDroppedControlRect( LPHEADCOMBO lphc, LPRECT lpRect)
456 /* In windows, CB_GETDROPPEDCONTROLRECT returns the upper left corner
457 of the combo box and the lower right corner of the listbox */
459 GetWindowRect(lphc->self, lpRect);
461 lpRect->right = lpRect->left + lphc->droppedRect.right - lphc->droppedRect.left;
462 lpRect->bottom = lpRect->top + lphc->droppedRect.bottom - lphc->droppedRect.top;
466 /***********************************************************************
467 * COMBO_WindowPosChanging
469 static LRESULT COMBO_WindowPosChanging(
472 WINDOWPOS* posChanging)
475 * We need to override the WM_WINDOWPOSCHANGING method to handle all
476 * the non-simple comboboxes. The problem is that those controls are
477 * always the same height. We have to make sure they are not resized
480 if ( ( CB_GETTYPE(lphc) != CBS_SIMPLE ) &&
481 ((posChanging->flags & SWP_NOSIZE) == 0) )
485 newComboHeight = CBGetTextAreaHeight(hwnd,lphc) +
486 2*COMBO_YBORDERSIZE();
489 * Resizing a combobox has another side effect, it resizes the dropped
490 * rectangle as well. However, it does it only if the new height for the
491 * combobox is different from the height it should have. In other words,
492 * if the application resizing the combobox only had the intention to resize
493 * the actual control, for example, to do the layout of a dialog that is
494 * resized, the height of the dropdown is not changed.
496 if (posChanging->cy != newComboHeight)
498 TRACE("posChanging->cy=%d, newComboHeight=%d, oldbot=%ld, oldtop=%ld\n",
499 posChanging->cy, newComboHeight, lphc->droppedRect.bottom,
500 lphc->droppedRect.top);
501 lphc->droppedRect.bottom = lphc->droppedRect.top + posChanging->cy - newComboHeight;
503 posChanging->cy = newComboHeight;
510 /***********************************************************************
513 static LRESULT COMBO_Create( HWND hwnd, LPHEADCOMBO lphc, HWND hwndParent, LONG style,
516 static const WCHAR clbName[] = {'C','o','m','b','o','L','B','o','x',0};
517 static const WCHAR editName[] = {'E','d','i','t',0};
519 if( !CB_GETTYPE(lphc) ) lphc->dwStyle |= CBS_SIMPLE;
520 if( CB_GETTYPE(lphc) != CBS_DROPDOWNLIST ) lphc->wState |= CBF_EDIT;
522 lphc->owner = hwndParent;
525 * The item height and dropped width are not set when the control
528 lphc->droppedWidth = lphc->editHeight = 0;
531 * The first time we go through, we want to measure the ownerdraw item
533 lphc->wState |= CBF_MEASUREITEM;
535 /* M$ IE 3.01 actually creates (and rapidly destroys) an ownerless combobox */
537 if( lphc->owner || !(style & WS_VISIBLE) )
543 * Initialize the dropped rect to the size of the client area of the
544 * control and then, force all the areas of the combobox to be
547 GetClientRect( hwnd, &lphc->droppedRect );
548 CBCalcPlacement(hwnd, lphc, &lphc->textRect, &lphc->buttonRect, &lphc->droppedRect );
551 * Adjust the position of the popup listbox if it's necessary
553 if ( CB_GETTYPE(lphc) != CBS_SIMPLE )
555 lphc->droppedRect.top = lphc->textRect.bottom + COMBO_YBORDERSIZE();
558 * If it's a dropdown, the listbox is offset
560 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
561 lphc->droppedRect.left += COMBO_EDITBUTTONSPACE();
563 if (lphc->droppedRect.bottom < lphc->droppedRect.top)
564 lphc->droppedRect.bottom = lphc->droppedRect.top;
565 if (lphc->droppedRect.right < lphc->droppedRect.left)
566 lphc->droppedRect.right = lphc->droppedRect.left;
567 MapWindowPoints( hwnd, 0, (LPPOINT)&lphc->droppedRect, 2 );
570 /* create listbox popup */
572 lbeStyle = (LBS_NOTIFY | LBS_COMBOBOX | WS_BORDER | WS_CLIPSIBLINGS | WS_CHILD) |
573 (style & (WS_VSCROLL | CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE));
575 if( lphc->dwStyle & CBS_SORT )
576 lbeStyle |= LBS_SORT;
577 if( lphc->dwStyle & CBS_HASSTRINGS )
578 lbeStyle |= LBS_HASSTRINGS;
579 if( lphc->dwStyle & CBS_NOINTEGRALHEIGHT )
580 lbeStyle |= LBS_NOINTEGRALHEIGHT;
581 if( lphc->dwStyle & CBS_DISABLENOSCROLL )
582 lbeStyle |= LBS_DISABLENOSCROLL;
584 if( CB_GETTYPE(lphc) == CBS_SIMPLE ) /* child listbox */
586 lbeStyle |= WS_VISIBLE;
589 * In win 95 look n feel, the listbox in the simple combobox has
590 * the WS_EXCLIENTEDGE style instead of the WS_BORDER style.
592 lbeStyle &= ~WS_BORDER;
593 lbeExStyle |= WS_EX_CLIENTEDGE;
597 lbeExStyle |= (WS_EX_TOPMOST | WS_EX_TOOLWINDOW);
601 lphc->hWndLBox = CreateWindowExW(lbeExStyle, clbName, NULL, lbeStyle,
602 lphc->droppedRect.left,
603 lphc->droppedRect.top,
604 lphc->droppedRect.right - lphc->droppedRect.left,
605 lphc->droppedRect.bottom - lphc->droppedRect.top,
606 hwnd, (HMENU)ID_CB_LISTBOX,
607 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
609 lphc->hWndLBox = CreateWindowExA(lbeExStyle, "ComboLBox", NULL, lbeStyle,
610 lphc->droppedRect.left,
611 lphc->droppedRect.top,
612 lphc->droppedRect.right - lphc->droppedRect.left,
613 lphc->droppedRect.bottom - lphc->droppedRect.top,
614 hwnd, (HMENU)ID_CB_LISTBOX,
615 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), lphc );
620 lbeStyle = WS_CHILD | WS_VISIBLE | ES_NOHIDESEL | ES_LEFT | ES_COMBO;
622 if( lphc->wState & CBF_EDIT )
624 if( lphc->dwStyle & CBS_OEMCONVERT )
625 lbeStyle |= ES_OEMCONVERT;
626 if( lphc->dwStyle & CBS_AUTOHSCROLL )
627 lbeStyle |= ES_AUTOHSCROLL;
628 if( lphc->dwStyle & CBS_LOWERCASE )
629 lbeStyle |= ES_LOWERCASE;
630 else if( lphc->dwStyle & CBS_UPPERCASE )
631 lbeStyle |= ES_UPPERCASE;
633 if (!IsWindowEnabled(hwnd)) lbeStyle |= WS_DISABLED;
636 lphc->hWndEdit = CreateWindowExW(0, editName, NULL, lbeStyle,
637 lphc->textRect.left, lphc->textRect.top,
638 lphc->textRect.right - lphc->textRect.left,
639 lphc->textRect.bottom - lphc->textRect.top,
640 hwnd, (HMENU)ID_CB_EDIT,
641 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
643 lphc->hWndEdit = CreateWindowExA(0, "Edit", NULL, lbeStyle,
644 lphc->textRect.left, lphc->textRect.top,
645 lphc->textRect.right - lphc->textRect.left,
646 lphc->textRect.bottom - lphc->textRect.top,
647 hwnd, (HMENU)ID_CB_EDIT,
648 (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE ), NULL );
650 if( !lphc->hWndEdit )
656 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
658 /* Now do the trick with parent */
659 SetParent(lphc->hWndLBox, HWND_DESKTOP);
661 * If the combo is a dropdown, we must resize the control
662 * to fit only the text area and button. To do this,
663 * we send a dummy resize and the WM_WINDOWPOSCHANGING message
664 * will take care of setting the height for us.
666 CBForceDummyResize(lphc);
669 TRACE("init done\n");
672 ERR("edit control failure.\n");
673 } else ERR("listbox failure.\n");
674 } else ERR("no owner for visible combo.\n");
676 /* CreateWindow() will send WM_NCDESTROY to cleanup */
681 /***********************************************************************
684 * Paint combo button (normal, pressed, and disabled states).
686 static void CBPaintButton( LPHEADCOMBO lphc, HDC hdc, RECT rectButton)
688 UINT buttonState = DFCS_SCROLLCOMBOBOX;
690 if( lphc->wState & CBF_NOREDRAW )
694 if (lphc->wState & CBF_BUTTONDOWN)
695 buttonState |= DFCS_PUSHED;
697 if (CB_DISABLED(lphc))
698 buttonState |= DFCS_INACTIVE;
700 DrawFrameControl(hdc, &rectButton, DFC_SCROLL, buttonState);
703 /***********************************************************************
706 * Paint CBS_DROPDOWNLIST text field / update edit control contents.
708 static void CBPaintText(
716 if( lphc->wState & CBF_NOREDRAW ) return;
720 /* follow Windows combobox that sends a bunch of text
721 * inquiries to its listbox while processing WM_PAINT. */
723 if( (id = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0) ) != LB_ERR )
725 size = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, id, 0);
727 FIXME("LB_ERR probably not handled yet\n");
728 if( (pText = HeapAlloc( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))) )
730 /* size from LB_GETTEXTLEN may be too large, from LB_GETTEXT is accurate */
731 size=SendMessageW(lphc->hWndLBox, LB_GETTEXT, (WPARAM)id, (LPARAM)pText);
732 pText[size] = '\0'; /* just in case */
736 if( !CB_OWNERDRAWN(lphc) )
739 if( lphc->wState & CBF_EDIT )
741 static const WCHAR empty_stringW[] = { 0 };
742 if( CB_HASSTRINGS(lphc) ) SetWindowTextW( lphc->hWndEdit, pText ? pText : empty_stringW );
743 if( lphc->wState & CBF_FOCUSED )
744 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
746 else /* paint text field ourselves */
748 UINT itemState = ODS_COMBOBOXEDIT;
749 HFONT hPrevFont = (lphc->hFont) ? SelectObject(hdc, lphc->hFont) : 0;
752 * Give ourselves some space.
754 InflateRect( &rectEdit, -1, -1 );
756 if( CB_OWNERDRAWN(lphc) )
760 UINT ctlid = (UINT)GetWindowLongPtrW( lphc->self, GWLP_ID );
762 /* setup state for DRAWITEM message. Owner will highlight */
763 if ( (lphc->wState & CBF_FOCUSED) &&
764 !(lphc->wState & CBF_DROPPED) )
765 itemState |= ODS_SELECTED | ODS_FOCUS;
768 * Save the current clip region.
769 * To retrieve the clip region, we need to create one "dummy"
772 clipRegion = CreateRectRgnIndirect(&rectEdit);
774 if (GetClipRgn(hdc, clipRegion)!=1)
776 DeleteObject(clipRegion);
780 if (!IsWindowEnabled(lphc->self) & WS_DISABLED) itemState |= ODS_DISABLED;
782 dis.CtlType = ODT_COMBOBOX;
784 dis.hwndItem = lphc->self;
785 dis.itemAction = ODA_DRAWENTIRE;
787 dis.itemState = itemState;
789 dis.rcItem = rectEdit;
790 dis.itemData = SendMessageW(lphc->hWndLBox, LB_GETITEMDATA,
794 * Clip the DC and have the parent draw the item.
796 IntersectClipRect(hdc,
797 rectEdit.left, rectEdit.top,
798 rectEdit.right, rectEdit.bottom);
800 SendMessageW(lphc->owner, WM_DRAWITEM, ctlid, (LPARAM)&dis );
803 * Reset the clipping region.
805 SelectClipRgn(hdc, clipRegion);
809 static const WCHAR empty_stringW[] = { 0 };
811 if ( (lphc->wState & CBF_FOCUSED) &&
812 !(lphc->wState & CBF_DROPPED) ) {
815 FillRect( hdc, &rectEdit, GetSysColorBrush(COLOR_HIGHLIGHT) );
816 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
817 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
823 ETO_OPAQUE | ETO_CLIPPED,
825 pText ? pText : empty_stringW , size, NULL );
827 if(lphc->wState & CBF_FOCUSED && !(lphc->wState & CBF_DROPPED))
828 DrawFocusRect( hdc, &rectEdit );
832 SelectObject(hdc, hPrevFont );
834 HeapFree( GetProcessHeap(), 0, pText );
837 /***********************************************************************
840 static void CBPaintBorder(
847 if (CB_GETTYPE(lphc) != CBS_SIMPLE)
849 GetClientRect(hwnd, &clientRect);
853 CopyRect(&clientRect, &lphc->textRect);
855 InflateRect(&clientRect, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
856 InflateRect(&clientRect, COMBO_XBORDERSIZE(), COMBO_YBORDERSIZE());
859 DrawEdge(hdc, &clientRect, EDGE_SUNKEN, BF_RECT);
862 /***********************************************************************
863 * COMBO_PrepareColors
865 * This method will sent the appropriate WM_CTLCOLOR message to
866 * prepare and setup the colors for the combo's DC.
868 * It also returns the brush to use for the background.
870 static HBRUSH COMBO_PrepareColors(
877 * Get the background brush for this control.
879 if (CB_DISABLED(lphc))
881 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORSTATIC,
882 (WPARAM)hDC, (LPARAM)lphc->self );
885 * We have to change the text color since WM_CTLCOLORSTATIC will
886 * set it to the "enabled" color. This is the same behavior as the
889 SetTextColor(hDC, GetSysColor(COLOR_GRAYTEXT));
893 if (lphc->wState & CBF_EDIT)
895 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLOREDIT,
896 (WPARAM)hDC, (LPARAM)lphc->self );
900 hBkgBrush = (HBRUSH)SendMessageW(lphc->owner, WM_CTLCOLORLISTBOX,
901 (WPARAM)hDC, (LPARAM)lphc->self );
909 hBkgBrush = GetSysColorBrush(COLOR_WINDOW);
914 /***********************************************************************
915 * COMBO_EraseBackground
917 static LRESULT COMBO_EraseBackground(
925 if(lphc->wState & CBF_EDIT)
928 hDC = (hParamDC) ? hParamDC
931 * Retrieve the background brush
933 hBkgBrush = COMBO_PrepareColors(lphc, hDC);
935 FillRect(hDC, &lphc->textRect, hBkgBrush);
938 ReleaseDC(hwnd, hDC);
943 /***********************************************************************
946 static LRESULT COMBO_Paint(LPHEADCOMBO lphc, HDC hParamDC)
951 hDC = (hParamDC) ? hParamDC
952 : BeginPaint( lphc->self, &ps);
954 TRACE("hdc=%p\n", hDC);
956 if( hDC && !(lphc->wState & CBF_NOREDRAW) )
958 HBRUSH hPrevBrush, hBkgBrush;
961 * Retrieve the background brush and select it in the
964 hBkgBrush = COMBO_PrepareColors(lphc, hDC);
966 hPrevBrush = SelectObject( hDC, hBkgBrush );
969 * In non 3.1 look, there is a sunken border on the combobox
971 CBPaintBorder(lphc->self, lphc, hDC);
973 if( !IsRectEmpty(&lphc->buttonRect) )
975 CBPaintButton(lphc, hDC, lphc->buttonRect);
978 /* paint the edit control padding area */
979 if (CB_GETTYPE(lphc) != CBS_DROPDOWNLIST)
981 RECT rPadEdit = lphc->textRect;
983 InflateRect(&rPadEdit, EDIT_CONTROL_PADDING(), EDIT_CONTROL_PADDING());
985 FrameRect( hDC, &rPadEdit, GetSysColorBrush(COLOR_WINDOW) );
988 if( !(lphc->wState & CBF_EDIT) )
989 CBPaintText( lphc, hDC, lphc->textRect);
992 SelectObject( hDC, hPrevBrush );
996 EndPaint(lphc->self, &ps);
1001 /***********************************************************************
1004 * Select listbox entry according to the contents of the edit control.
1006 static INT CBUpdateLBox( LPHEADCOMBO lphc, BOOL bSelect )
1009 LPWSTR pText = NULL;
1012 length = SendMessageW( lphc->hWndEdit, WM_GETTEXTLENGTH, 0, 0 );
1015 pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
1017 TRACE("\t edit text length %i\n", length );
1021 if( length ) GetWindowTextW( lphc->hWndEdit, pText, length + 1);
1022 else pText[0] = '\0';
1023 idx = SendMessageW(lphc->hWndLBox, LB_FINDSTRING,
1024 (WPARAM)(-1), (LPARAM)pText );
1025 HeapFree( GetProcessHeap(), 0, pText );
1028 SendMessageW(lphc->hWndLBox, LB_SETCURSEL, (WPARAM)(bSelect ? idx : -1), 0);
1030 /* probably superfluous but Windows sends this too */
1031 SendMessageW(lphc->hWndLBox, LB_SETCARETINDEX, (WPARAM)(idx < 0 ? 0 : idx), 0);
1032 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, (WPARAM)(idx < 0 ? 0 : idx), 0);
1037 /***********************************************************************
1040 * Copy a listbox entry to the edit control.
1042 static void CBUpdateEdit( LPHEADCOMBO lphc , INT index )
1045 LPWSTR pText = NULL;
1046 static const WCHAR empty_stringW[] = { 0 };
1048 TRACE("\t %i\n", index );
1050 if( index >= 0 ) /* got an entry */
1052 length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, (WPARAM)index, 0);
1053 if( length != LB_ERR)
1055 if( (pText = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR))) )
1057 SendMessageW(lphc->hWndLBox, LB_GETTEXT,
1058 (WPARAM)index, (LPARAM)pText );
1063 lphc->wState |= (CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
1064 SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, pText ? (LPARAM)pText : (LPARAM)empty_stringW);
1065 lphc->wState &= ~(CBF_NOEDITNOTIFY | CBF_NOLBSELECT);
1067 if( lphc->wState & CBF_FOCUSED )
1068 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
1070 HeapFree( GetProcessHeap(), 0, pText );
1073 /***********************************************************************
1076 * Show listbox popup.
1078 static void CBDropDown( LPHEADCOMBO lphc )
1084 TRACE("[%p]: drop down\n", lphc->self);
1086 CB_NOTIFY( lphc, CBN_DROPDOWN );
1090 lphc->wState |= CBF_DROPPED;
1091 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1093 lphc->droppedIndex = CBUpdateLBox( lphc, TRUE );
1095 /* Update edit only if item is in the list */
1096 if( !(lphc->wState & CBF_CAPTURE) && lphc->droppedIndex >= 0)
1097 CBUpdateEdit( lphc, lphc->droppedIndex );
1101 lphc->droppedIndex = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1103 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX,
1104 (WPARAM)(lphc->droppedIndex == LB_ERR ? 0 : lphc->droppedIndex), 0 );
1105 SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1108 /* now set popup position */
1109 GetWindowRect( lphc->self, &rect );
1112 * If it's a dropdown, the listbox is offset
1114 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1115 rect.left += COMBO_EDITBUTTONSPACE();
1117 /* if the dropped height is greater than the total height of the dropped
1118 items list, then force the drop down list height to be the total height
1119 of the items in the dropped list */
1121 /* And Remove any extra space (Best Fit) */
1122 nDroppedHeight = lphc->droppedRect.bottom - lphc->droppedRect.top;
1123 /* if listbox length has been set directly by its handle */
1124 GetWindowRect(lphc->hWndLBox, &r);
1125 if (nDroppedHeight < r.bottom - r.top)
1126 nDroppedHeight = r.bottom - r.top;
1127 nItems = (int)SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
1134 nIHeight = (int)SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, 0, 0);
1136 nHeight = nIHeight*nItems;
1138 if (nHeight < nDroppedHeight - COMBO_YBORDERSIZE())
1139 nDroppedHeight = nHeight + COMBO_YBORDERSIZE();
1141 if (nDroppedHeight < nIHeight)
1144 nDroppedHeight = (nItems+1)*nIHeight;
1146 nDroppedHeight = 6*nIHeight;
1150 /*If height of dropped rectangle gets beyond a screen size it should go up, otherwise down.*/
1151 if( (rect.bottom + nDroppedHeight) >= GetSystemMetrics( SM_CYSCREEN ) )
1152 rect.bottom = rect.top - nDroppedHeight;
1154 SetWindowPos( lphc->hWndLBox, HWND_TOP, rect.left, rect.bottom,
1155 lphc->droppedRect.right - lphc->droppedRect.left,
1157 SWP_NOACTIVATE | SWP_SHOWWINDOW);
1160 if( !(lphc->wState & CBF_NOREDRAW) )
1161 RedrawWindow( lphc->self, NULL, 0, RDW_INVALIDATE |
1162 RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1164 EnableWindow( lphc->hWndLBox, TRUE );
1165 if (GetCapture() != lphc->self)
1166 SetCapture(lphc->hWndLBox);
1169 /***********************************************************************
1172 * Hide listbox popup.
1174 static void CBRollUp( LPHEADCOMBO lphc, BOOL ok, BOOL bButton )
1176 HWND hWnd = lphc->self;
1178 TRACE("[%p]: sel ok? [%i] dropped? [%i]\n",
1179 lphc->self, (INT)ok, (INT)(lphc->wState & CBF_DROPPED));
1181 CB_NOTIFY( lphc, (ok) ? CBN_SELENDOK : CBN_SELENDCANCEL );
1183 if( IsWindow( hWnd ) && CB_GETTYPE(lphc) != CBS_SIMPLE )
1186 if( lphc->wState & CBF_DROPPED )
1190 lphc->wState &= ~CBF_DROPPED;
1191 ShowWindow( lphc->hWndLBox, SW_HIDE );
1193 if(GetCapture() == lphc->hWndLBox)
1198 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1200 rect = lphc->buttonRect;
1211 rect = lphc->textRect;
1216 if( bButton && !(lphc->wState & CBF_NOREDRAW) )
1217 RedrawWindow( hWnd, &rect, 0, RDW_INVALIDATE |
1218 RDW_ERASE | RDW_UPDATENOW | RDW_NOCHILDREN );
1219 CB_NOTIFY( lphc, CBN_CLOSEUP );
1224 /***********************************************************************
1227 * Used by the ComboLBox to show/hide itself in response to VK_F4, etc...
1229 BOOL COMBO_FlipListbox( LPHEADCOMBO lphc, BOOL ok, BOOL bRedrawButton )
1231 if( lphc->wState & CBF_DROPPED )
1233 CBRollUp( lphc, ok, bRedrawButton );
1241 /***********************************************************************
1244 static void CBRepaintButton( LPHEADCOMBO lphc )
1246 InvalidateRect(lphc->self, &lphc->buttonRect, TRUE);
1247 UpdateWindow(lphc->self);
1250 /***********************************************************************
1253 static void COMBO_SetFocus( LPHEADCOMBO lphc )
1255 if( !(lphc->wState & CBF_FOCUSED) )
1257 if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1258 SendMessageW(lphc->hWndLBox, LB_CARETON, 0, 0);
1260 /* This is wrong. Message sequences seem to indicate that this
1261 is set *after* the notify. */
1262 /* lphc->wState |= CBF_FOCUSED; */
1264 if( !(lphc->wState & CBF_EDIT) )
1265 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1267 CB_NOTIFY( lphc, CBN_SETFOCUS );
1268 lphc->wState |= CBF_FOCUSED;
1272 /***********************************************************************
1275 static void COMBO_KillFocus( LPHEADCOMBO lphc )
1277 HWND hWnd = lphc->self;
1279 if( lphc->wState & CBF_FOCUSED )
1281 CBRollUp( lphc, FALSE, TRUE );
1282 if( IsWindow( hWnd ) )
1284 if( CB_GETTYPE(lphc) == CBS_DROPDOWNLIST )
1285 SendMessageW(lphc->hWndLBox, LB_CARETOFF, 0, 0);
1287 lphc->wState &= ~CBF_FOCUSED;
1290 if( !(lphc->wState & CBF_EDIT) )
1291 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1293 CB_NOTIFY( lphc, CBN_KILLFOCUS );
1298 /***********************************************************************
1301 static LRESULT COMBO_Command( LPHEADCOMBO lphc, WPARAM wParam, HWND hWnd )
1303 if ( lphc->wState & CBF_EDIT && lphc->hWndEdit == hWnd )
1305 /* ">> 8" makes gcc generate jump-table instead of cmp ladder */
1307 switch( HIWORD(wParam) >> 8 )
1309 case (EN_SETFOCUS >> 8):
1311 TRACE("[%p]: edit [%p] got focus\n", lphc->self, lphc->hWndEdit );
1313 COMBO_SetFocus( lphc );
1316 case (EN_KILLFOCUS >> 8):
1318 TRACE("[%p]: edit [%p] lost focus\n", lphc->self, lphc->hWndEdit );
1320 /* NOTE: it seems that Windows' edit control sends an
1321 * undocumented message WM_USER + 0x1B instead of this
1322 * notification (only when it happens to be a part of
1323 * the combo). ?? - AK.
1326 COMBO_KillFocus( lphc );
1330 case (EN_CHANGE >> 8):
1332 * In some circumstances (when the selection of the combobox
1333 * is changed for example) we don't want the EN_CHANGE notification
1334 * to be forwarded to the parent of the combobox. This code
1335 * checks a flag that is set in these occasions and ignores the
1338 if (lphc->wState & CBF_NOLBSELECT)
1340 lphc->wState &= ~CBF_NOLBSELECT;
1344 CBUpdateLBox( lphc, lphc->wState & CBF_DROPPED );
1347 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1348 CB_NOTIFY( lphc, CBN_EDITCHANGE );
1351 case (EN_UPDATE >> 8):
1352 if (!(lphc->wState & CBF_NOEDITNOTIFY))
1353 CB_NOTIFY( lphc, CBN_EDITUPDATE );
1356 case (EN_ERRSPACE >> 8):
1357 CB_NOTIFY( lphc, CBN_ERRSPACE );
1360 else if( lphc->hWndLBox == hWnd )
1362 switch( (short)HIWORD(wParam) )
1365 CB_NOTIFY( lphc, CBN_ERRSPACE );
1369 CB_NOTIFY( lphc, CBN_DBLCLK );
1375 TRACE("[%p]: lbox selection change [%x]\n", lphc->self, lphc->wState );
1377 if( HIWORD(wParam) == LBN_SELCHANGE)
1379 if( lphc->wState & CBF_EDIT )
1381 INT index = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1382 lphc->wState |= CBF_NOLBSELECT;
1383 CBUpdateEdit( lphc, index );
1384 /* select text in edit, as Windows does */
1385 SendMessageW(lphc->hWndEdit, EM_SETSEL, 0, (LPARAM)(-1));
1388 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1391 /* do not roll up if selection is being tracked
1392 * by arrowkeys in the dropdown listbox */
1393 if( ((lphc->wState & CBF_DROPPED) && !(lphc->wState & CBF_NOROLLUP)) )
1395 CBRollUp( lphc, (HIWORD(wParam) == LBN_SELCHANGE), TRUE );
1397 else lphc->wState &= ~CBF_NOROLLUP;
1399 CB_NOTIFY( lphc, CBN_SELCHANGE );
1405 /* nothing to do here since ComboLBox always resets the focus to its
1406 * combo/edit counterpart */
1413 /***********************************************************************
1416 * Fixup an ownerdrawn item operation and pass it up to the combobox owner.
1418 static LRESULT COMBO_ItemOp( LPHEADCOMBO lphc, UINT msg, LPARAM lParam )
1420 HWND hWnd = lphc->self;
1421 UINT id = (UINT)GetWindowLongPtrW( hWnd, GWLP_ID );
1423 TRACE("[%p]: ownerdraw op %04x\n", lphc->self, msg );
1429 DELETEITEMSTRUCT *lpIS = (DELETEITEMSTRUCT *)lParam;
1430 lpIS->CtlType = ODT_COMBOBOX;
1432 lpIS->hwndItem = hWnd;
1437 DRAWITEMSTRUCT *lpIS = (DRAWITEMSTRUCT *)lParam;
1438 lpIS->CtlType = ODT_COMBOBOX;
1440 lpIS->hwndItem = hWnd;
1443 case WM_COMPAREITEM:
1445 COMPAREITEMSTRUCT *lpIS = (COMPAREITEMSTRUCT *)lParam;
1446 lpIS->CtlType = ODT_COMBOBOX;
1448 lpIS->hwndItem = hWnd;
1451 case WM_MEASUREITEM:
1453 MEASUREITEMSTRUCT *lpIS = (MEASUREITEMSTRUCT *)lParam;
1454 lpIS->CtlType = ODT_COMBOBOX;
1459 return SendMessageW(lphc->owner, msg, id, lParam);
1463 /***********************************************************************
1466 static LRESULT COMBO_GetTextW( LPHEADCOMBO lphc, INT count, LPWSTR buf )
1470 if( lphc->wState & CBF_EDIT )
1471 return SendMessageW( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1473 /* get it from the listbox */
1475 if (!count || !buf) return 0;
1476 if( lphc->hWndLBox )
1478 INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1479 if (idx == LB_ERR) goto error;
1480 length = SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1481 if (length == LB_ERR) goto error;
1483 /* 'length' is without the terminating character */
1484 if (length >= count)
1486 LPWSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
1487 if (!lpBuffer) goto error;
1488 length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1490 /* truncate if buffer is too short */
1491 if (length != LB_ERR)
1493 lstrcpynW( buf, lpBuffer, count );
1496 HeapFree( GetProcessHeap(), 0, lpBuffer );
1498 else length = SendMessageW(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1500 if (length == LB_ERR) return 0;
1504 error: /* error - truncate string, return zero */
1510 /***********************************************************************
1513 * NOTE! LB_GETTEXT does not count terminating \0, WM_GETTEXT does.
1514 * also LB_GETTEXT might return values < 0, WM_GETTEXT doesn't.
1516 static LRESULT COMBO_GetTextA( LPHEADCOMBO lphc, INT count, LPSTR buf )
1520 if( lphc->wState & CBF_EDIT )
1521 return SendMessageA( lphc->hWndEdit, WM_GETTEXT, count, (LPARAM)buf );
1523 /* get it from the listbox */
1525 if (!count || !buf) return 0;
1526 if( lphc->hWndLBox )
1528 INT idx = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1529 if (idx == LB_ERR) goto error;
1530 length = SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, idx, 0 );
1531 if (length == LB_ERR) goto error;
1533 /* 'length' is without the terminating character */
1534 if (length >= count)
1536 LPSTR lpBuffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) );
1537 if (!lpBuffer) goto error;
1538 length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)lpBuffer);
1540 /* truncate if buffer is too short */
1541 if (length != LB_ERR)
1543 lstrcpynA( buf, lpBuffer, count );
1546 HeapFree( GetProcessHeap(), 0, lpBuffer );
1548 else length = SendMessageA(lphc->hWndLBox, LB_GETTEXT, idx, (LPARAM)buf);
1550 if (length == LB_ERR) return 0;
1554 error: /* error - truncate string, return zero */
1560 /***********************************************************************
1563 * This function sets window positions according to the updated
1564 * component placement struct.
1566 static void CBResetPos(
1572 BOOL bDrop = (CB_GETTYPE(lphc) != CBS_SIMPLE);
1574 /* NOTE: logs sometimes have WM_LBUTTONUP before a cascade of
1575 * sizing messages */
1577 if( lphc->wState & CBF_EDIT )
1578 SetWindowPos( lphc->hWndEdit, 0,
1579 rectEdit->left, rectEdit->top,
1580 rectEdit->right - rectEdit->left,
1581 rectEdit->bottom - rectEdit->top,
1582 SWP_NOZORDER | SWP_NOACTIVATE | ((bDrop) ? SWP_NOREDRAW : 0) );
1584 SetWindowPos( lphc->hWndLBox, 0,
1585 rectLB->left, rectLB->top,
1586 rectLB->right - rectLB->left,
1587 rectLB->bottom - rectLB->top,
1588 SWP_NOACTIVATE | SWP_NOZORDER | ((bDrop) ? SWP_NOREDRAW : 0) );
1592 if( lphc->wState & CBF_DROPPED )
1594 lphc->wState &= ~CBF_DROPPED;
1595 ShowWindow( lphc->hWndLBox, SW_HIDE );
1598 if( bRedraw && !(lphc->wState & CBF_NOREDRAW) )
1599 RedrawWindow( lphc->self, NULL, 0,
1600 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );
1605 /***********************************************************************
1608 static void COMBO_Size( LPHEADCOMBO lphc )
1610 CBCalcPlacement(lphc->self,
1614 &lphc->droppedRect);
1616 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1620 /***********************************************************************
1623 static void COMBO_Font( LPHEADCOMBO lphc, HFONT hFont, BOOL bRedraw )
1628 lphc->hFont = hFont;
1631 * Propagate to owned windows.
1633 if( lphc->wState & CBF_EDIT )
1634 SendMessageW(lphc->hWndEdit, WM_SETFONT, (WPARAM)hFont, bRedraw);
1635 SendMessageW(lphc->hWndLBox, WM_SETFONT, (WPARAM)hFont, bRedraw);
1638 * Redo the layout of the control.
1640 if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1642 CBCalcPlacement(lphc->self,
1646 &lphc->droppedRect);
1648 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1652 CBForceDummyResize(lphc);
1657 /***********************************************************************
1658 * COMBO_SetItemHeight
1660 static LRESULT COMBO_SetItemHeight( LPHEADCOMBO lphc, INT index, INT height )
1662 LRESULT lRet = CB_ERR;
1664 if( index == -1 ) /* set text field height */
1666 if( height < 32768 )
1668 lphc->editHeight = height;
1671 * Redo the layout of the control.
1673 if ( CB_GETTYPE(lphc) == CBS_SIMPLE)
1675 CBCalcPlacement(lphc->self,
1679 &lphc->droppedRect);
1681 CBResetPos( lphc, &lphc->textRect, &lphc->droppedRect, TRUE );
1685 CBForceDummyResize(lphc);
1691 else if ( CB_OWNERDRAWN(lphc) ) /* set listbox item height */
1692 lRet = SendMessageW(lphc->hWndLBox, LB_SETITEMHEIGHT,
1693 (WPARAM)index, (LPARAM)height );
1697 /***********************************************************************
1698 * COMBO_SelectString
1700 static LRESULT COMBO_SelectString( LPHEADCOMBO lphc, INT start, LPARAM pText, BOOL unicode )
1702 INT index = unicode ? SendMessageW(lphc->hWndLBox, LB_SELECTSTRING, (WPARAM)start, pText) :
1703 SendMessageA(lphc->hWndLBox, LB_SELECTSTRING, (WPARAM)start, pText);
1706 if( lphc->wState & CBF_EDIT )
1707 CBUpdateEdit( lphc, index );
1710 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
1713 return (LRESULT)index;
1716 /***********************************************************************
1719 static void COMBO_LButtonDown( LPHEADCOMBO lphc, LPARAM lParam )
1723 HWND hWnd = lphc->self;
1725 pt.x = LOWORD(lParam);
1726 pt.y = HIWORD(lParam);
1727 bButton = PtInRect(&lphc->buttonRect, pt);
1729 if( (CB_GETTYPE(lphc) == CBS_DROPDOWNLIST) ||
1730 (bButton && (CB_GETTYPE(lphc) == CBS_DROPDOWN)) )
1732 lphc->wState |= CBF_BUTTONDOWN;
1733 if( lphc->wState & CBF_DROPPED )
1735 /* got a click to cancel selection */
1737 lphc->wState &= ~CBF_BUTTONDOWN;
1738 CBRollUp( lphc, TRUE, FALSE );
1739 if( !IsWindow( hWnd ) ) return;
1741 if( lphc->wState & CBF_CAPTURE )
1743 lphc->wState &= ~CBF_CAPTURE;
1749 /* drop down the listbox and start tracking */
1751 lphc->wState |= CBF_CAPTURE;
1755 if( bButton ) CBRepaintButton( lphc );
1759 /***********************************************************************
1762 * Release capture and stop tracking if needed.
1764 static void COMBO_LButtonUp( LPHEADCOMBO lphc )
1766 if( lphc->wState & CBF_CAPTURE )
1768 lphc->wState &= ~CBF_CAPTURE;
1769 if( CB_GETTYPE(lphc) == CBS_DROPDOWN )
1771 INT index = CBUpdateLBox( lphc, TRUE );
1772 /* Update edit only if item is in the list */
1775 lphc->wState |= CBF_NOLBSELECT;
1776 CBUpdateEdit( lphc, index );
1777 lphc->wState &= ~CBF_NOLBSELECT;
1781 SetCapture(lphc->hWndLBox);
1784 if( lphc->wState & CBF_BUTTONDOWN )
1786 lphc->wState &= ~CBF_BUTTONDOWN;
1787 CBRepaintButton( lphc );
1791 /***********************************************************************
1794 * Two things to do - track combo button and release capture when
1795 * pointer goes into the listbox.
1797 static void COMBO_MouseMove( LPHEADCOMBO lphc, WPARAM wParam, LPARAM lParam )
1802 pt.x = LOWORD(lParam);
1803 pt.y = HIWORD(lParam);
1805 if( lphc->wState & CBF_BUTTONDOWN )
1809 bButton = PtInRect(&lphc->buttonRect, pt);
1813 lphc->wState &= ~CBF_BUTTONDOWN;
1814 CBRepaintButton( lphc );
1818 GetClientRect( lphc->hWndLBox, &lbRect );
1819 MapWindowPoints( lphc->self, lphc->hWndLBox, &pt, 1 );
1820 if( PtInRect(&lbRect, pt) )
1822 lphc->wState &= ~CBF_CAPTURE;
1824 if( CB_GETTYPE(lphc) == CBS_DROPDOWN ) CBUpdateLBox( lphc, TRUE );
1826 /* hand over pointer tracking */
1827 SendMessageW(lphc->hWndLBox, WM_LBUTTONDOWN, wParam, lParam);
1831 static char *strdupA(LPCSTR str)
1836 if(!str) return NULL;
1839 ret = HeapAlloc(GetProcessHeap(), 0, len + 1);
1840 memcpy(ret, str, len + 1);
1844 /***********************************************************************
1845 * ComboWndProc_common
1847 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/comboboxes/comboboxes.asp
1849 static LRESULT ComboWndProc_common( HWND hwnd, UINT message,
1850 WPARAM wParam, LPARAM lParam, BOOL unicode )
1852 LPHEADCOMBO lphc = (LPHEADCOMBO)GetWindowLongW( hwnd, 0 );
1854 TRACE("[%p]: msg %s wp %08x lp %08lx\n",
1855 hwnd, SPY_GetMsgName(message, hwnd), wParam, lParam );
1857 if( lphc || message == WM_NCCREATE )
1861 /* System messages */
1865 LONG style = unicode ? ((LPCREATESTRUCTW)lParam)->style :
1866 ((LPCREATESTRUCTA)lParam)->style;
1867 return COMBO_NCCreate(hwnd, style);
1870 COMBO_NCDestroy(lphc);
1871 break;/* -> DefWindowProc */
1879 hwndParent = ((LPCREATESTRUCTW)lParam)->hwndParent;
1880 style = ((LPCREATESTRUCTW)lParam)->style;
1884 hwndParent = ((LPCREATESTRUCTA)lParam)->hwndParent;
1885 style = ((LPCREATESTRUCTA)lParam)->style;
1887 return COMBO_Create(hwnd, lphc, hwndParent, style, unicode);
1890 case WM_PRINTCLIENT:
1891 if (lParam & PRF_ERASEBKGND)
1892 COMBO_EraseBackground(hwnd, lphc, (HDC)wParam);
1896 /* wParam may contain a valid HDC! */
1897 return COMBO_Paint(lphc, (HDC)wParam);
1899 return COMBO_EraseBackground(hwnd, lphc, (HDC)wParam);
1902 LRESULT result = DLGC_WANTARROWS | DLGC_WANTCHARS;
1903 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1905 int vk = (int)((LPMSG)lParam)->wParam;
1907 if ((vk == VK_RETURN || vk == VK_ESCAPE) && (lphc->wState & CBF_DROPPED))
1908 result |= DLGC_WANTMESSAGE;
1912 case WM_WINDOWPOSCHANGING:
1913 return COMBO_WindowPosChanging(hwnd, lphc, (LPWINDOWPOS)lParam);
1914 case WM_WINDOWPOSCHANGED:
1915 /* SetWindowPos can be called on a Combobox to resize its Listbox.
1916 * In that case, the Combobox itself will not be resized, so we won't
1917 * get a WM_SIZE. Since we still want to update the Listbox, we have to
1922 if( lphc->hWndLBox &&
1923 !(lphc->wState & CBF_NORESIZE) ) COMBO_Size( lphc );
1926 COMBO_Font( lphc, (HFONT)wParam, (BOOL)lParam );
1929 return (LRESULT)lphc->hFont;
1931 if( lphc->wState & CBF_EDIT )
1932 SetFocus( lphc->hWndEdit );
1934 COMBO_SetFocus( lphc );
1938 HWND hwndFocus = WIN_GetFullHandle( (HWND)wParam );
1940 (hwndFocus != lphc->hWndEdit && hwndFocus != lphc->hWndLBox ))
1941 COMBO_KillFocus( lphc );
1945 return COMBO_Command( lphc, wParam, WIN_GetFullHandle( (HWND)lParam ) );
1947 return unicode ? COMBO_GetTextW( lphc, wParam, (LPWSTR)lParam )
1948 : COMBO_GetTextA( lphc, wParam, (LPSTR)lParam );
1950 case WM_GETTEXTLENGTH:
1952 if ((message == WM_GETTEXTLENGTH) && !ISWIN31 && !(lphc->wState & CBF_EDIT))
1954 int j = SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
1955 if (j == -1) return 0;
1956 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, j, 0) :
1957 SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, j, 0);
1959 else if( lphc->wState & CBF_EDIT )
1962 lphc->wState |= CBF_NOEDITNOTIFY;
1963 ret = unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1964 SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1965 lphc->wState &= ~CBF_NOEDITNOTIFY;
1972 if( lphc->wState & CBF_EDIT )
1974 return unicode ? SendMessageW(lphc->hWndEdit, message, wParam, lParam) :
1975 SendMessageA(lphc->hWndEdit, message, wParam, lParam);
1981 case WM_COMPAREITEM:
1982 case WM_MEASUREITEM:
1983 return COMBO_ItemOp(lphc, message, lParam);
1985 if( lphc->wState & CBF_EDIT )
1986 EnableWindow( lphc->hWndEdit, (BOOL)wParam );
1987 EnableWindow( lphc->hWndLBox, (BOOL)wParam );
1989 /* Force the control to repaint when the enabled state changes. */
1990 InvalidateRect(lphc->self, NULL, TRUE);
1994 lphc->wState &= ~CBF_NOREDRAW;
1996 lphc->wState |= CBF_NOREDRAW;
1998 if( lphc->wState & CBF_EDIT )
1999 SendMessageW(lphc->hWndEdit, message, wParam, lParam);
2000 SendMessageW(lphc->hWndLBox, message, wParam, lParam);
2003 if( KEYDATA_ALT & HIWORD(lParam) )
2004 if( wParam == VK_UP || wParam == VK_DOWN )
2005 COMBO_FlipListbox( lphc, FALSE, FALSE );
2014 if ((wParam == VK_RETURN || wParam == VK_ESCAPE) &&
2015 (lphc->wState & CBF_DROPPED))
2017 CBRollUp( lphc, wParam == VK_RETURN, FALSE );
2020 else if ((wParam == VK_F4) && !(lphc->wState & CBF_EUI))
2022 COMBO_FlipListbox( lphc, FALSE, FALSE );
2026 if( lphc->wState & CBF_EDIT )
2027 hwndTarget = lphc->hWndEdit;
2029 hwndTarget = lphc->hWndLBox;
2031 return unicode ? SendMessageW(hwndTarget, message, wParam, lParam) :
2032 SendMessageA(hwndTarget, message, wParam, lParam);
2034 case WM_LBUTTONDOWN:
2035 if( !(lphc->wState & CBF_FOCUSED) ) SetFocus( lphc->self );
2036 if( lphc->wState & CBF_FOCUSED ) COMBO_LButtonDown( lphc, lParam );
2039 COMBO_LButtonUp( lphc );
2042 if( lphc->wState & CBF_CAPTURE )
2043 COMBO_MouseMove( lphc, wParam, lParam );
2047 if (wParam & (MK_SHIFT | MK_CONTROL))
2048 return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2049 DefWindowProcA(hwnd, message, wParam, lParam);
2051 if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_UP, 0);
2052 if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) return SendMessageW(hwnd, WM_KEYDOWN, VK_DOWN, 0);
2055 /* Combo messages */
2057 case CB_ADDSTRING16:
2058 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2063 if( lphc->dwStyle & CBS_LOWERCASE )
2064 strlwrW((LPWSTR)lParam);
2065 else if( lphc->dwStyle & CBS_UPPERCASE )
2066 struprW((LPWSTR)lParam);
2067 return SendMessageW(lphc->hWndLBox, LB_ADDSTRING, 0, lParam);
2069 else /* unlike the unicode version, the ansi version does not overwrite
2070 the string if converting case */
2072 char *string = NULL;
2074 if( lphc->dwStyle & CBS_LOWERCASE )
2076 string = strdupA((LPSTR)lParam);
2079 else if( lphc->dwStyle & CBS_UPPERCASE )
2081 string = strdupA((LPSTR)lParam);
2084 ret = SendMessageA(lphc->hWndLBox, LB_ADDSTRING, 0, string ? (LPARAM)string : lParam);
2085 HeapFree(GetProcessHeap(), 0, string);
2088 case CB_INSERTSTRING16:
2089 wParam = (INT)(INT16)wParam;
2090 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2092 case CB_INSERTSTRING:
2095 if( lphc->dwStyle & CBS_LOWERCASE )
2096 strlwrW((LPWSTR)lParam);
2097 else if( lphc->dwStyle & CBS_UPPERCASE )
2098 struprW((LPWSTR)lParam);
2099 return SendMessageW(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2103 if( lphc->dwStyle & CBS_LOWERCASE )
2104 _strlwr((LPSTR)lParam);
2105 else if( lphc->dwStyle & CBS_UPPERCASE )
2106 _strupr((LPSTR)lParam);
2107 return SendMessageA(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam);
2109 case CB_DELETESTRING16:
2110 case CB_DELETESTRING:
2111 return unicode ? SendMessageW(lphc->hWndLBox, LB_DELETESTRING, wParam, 0) :
2112 SendMessageA(lphc->hWndLBox, LB_DELETESTRING, wParam, 0);
2113 case CB_SELECTSTRING16:
2114 wParam = (INT)(INT16)wParam;
2115 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2117 case CB_SELECTSTRING:
2118 return COMBO_SelectString(lphc, (INT)wParam, lParam, unicode);
2119 case CB_FINDSTRING16:
2120 wParam = (INT)(INT16)wParam;
2121 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2124 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam) :
2125 SendMessageA(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam);
2126 case CB_FINDSTRINGEXACT16:
2127 wParam = (INT)(INT16)wParam;
2128 if( CB_HASSTRINGS(lphc) ) lParam = (LPARAM)MapSL(lParam);
2130 case CB_FINDSTRINGEXACT:
2131 return unicode ? SendMessageW(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam) :
2132 SendMessageA(lphc->hWndLBox, LB_FINDSTRINGEXACT, wParam, lParam);
2133 case CB_SETITEMHEIGHT16:
2134 wParam = (INT)(INT16)wParam; /* signed integer */
2136 case CB_SETITEMHEIGHT:
2137 return COMBO_SetItemHeight( lphc, (INT)wParam, (INT)lParam);
2138 case CB_GETITEMHEIGHT16:
2139 wParam = (INT)(INT16)wParam;
2141 case CB_GETITEMHEIGHT:
2142 if( (INT)wParam >= 0 ) /* listbox item */
2143 return SendMessageW(lphc->hWndLBox, LB_GETITEMHEIGHT, wParam, 0);
2144 return CBGetTextAreaHeight(hwnd, lphc);
2145 case CB_RESETCONTENT16:
2146 case CB_RESETCONTENT:
2147 SendMessageW(lphc->hWndLBox, LB_RESETCONTENT, 0, 0);
2148 if( (lphc->wState & CBF_EDIT) && CB_HASSTRINGS(lphc) )
2150 static const WCHAR empty_stringW[] = { 0 };
2151 SendMessageW(lphc->hWndEdit, WM_SETTEXT, 0, (LPARAM)empty_stringW);
2154 InvalidateRect(lphc->self, NULL, TRUE);
2156 case CB_INITSTORAGE:
2157 return SendMessageW(lphc->hWndLBox, LB_INITSTORAGE, wParam, lParam);
2158 case CB_GETHORIZONTALEXTENT:
2159 return SendMessageW(lphc->hWndLBox, LB_GETHORIZONTALEXTENT, 0, 0);
2160 case CB_SETHORIZONTALEXTENT:
2161 return SendMessageW(lphc->hWndLBox, LB_SETHORIZONTALEXTENT, wParam, 0);
2162 case CB_GETTOPINDEX:
2163 return SendMessageW(lphc->hWndLBox, LB_GETTOPINDEX, 0, 0);
2165 return SendMessageW(lphc->hWndLBox, LB_GETLOCALE, 0, 0);
2167 return SendMessageW(lphc->hWndLBox, LB_SETLOCALE, wParam, 0);
2168 case CB_GETDROPPEDWIDTH:
2169 if( lphc->droppedWidth )
2170 return lphc->droppedWidth;
2171 return lphc->droppedRect.right - lphc->droppedRect.left;
2172 case CB_SETDROPPEDWIDTH:
2173 if( (CB_GETTYPE(lphc) != CBS_SIMPLE) &&
2174 (INT)wParam < 32768 ) lphc->droppedWidth = (INT)wParam;
2176 case CB_GETDROPPEDCONTROLRECT16:
2177 lParam = (LPARAM)MapSL(lParam);
2181 RECT16 *r16 = (RECT16 *)lParam;
2182 CBGetDroppedControlRect( lphc, &r );
2185 r16->right = r.right;
2186 r16->bottom = r.bottom;
2189 case CB_GETDROPPEDCONTROLRECT:
2190 if( lParam ) CBGetDroppedControlRect(lphc, (LPRECT)lParam );
2192 case CB_GETDROPPEDSTATE16:
2193 case CB_GETDROPPEDSTATE:
2194 return (lphc->wState & CBF_DROPPED) ? TRUE : FALSE;
2196 return SendMessageA(lphc->hWndLBox, LB_DIR16, wParam, lParam);
2198 return unicode ? SendMessageW(lphc->hWndLBox, LB_DIR, wParam, lParam) :
2199 SendMessageA(lphc->hWndLBox, LB_DIR, wParam, lParam);
2201 case CB_SHOWDROPDOWN16:
2202 case CB_SHOWDROPDOWN:
2203 if( CB_GETTYPE(lphc) != CBS_SIMPLE )
2207 if( !(lphc->wState & CBF_DROPPED) )
2211 if( lphc->wState & CBF_DROPPED )
2212 CBRollUp( lphc, FALSE, TRUE );
2217 return SendMessageW(lphc->hWndLBox, LB_GETCOUNT, 0, 0);
2218 case CB_GETCURSEL16:
2220 return SendMessageW(lphc->hWndLBox, LB_GETCURSEL, 0, 0);
2221 case CB_SETCURSEL16:
2222 wParam = (INT)(INT16)wParam;
2225 lParam = SendMessageW(lphc->hWndLBox, LB_SETCURSEL, wParam, 0);
2227 SendMessageW(lphc->hWndLBox, LB_SETTOPINDEX, wParam, 0);
2229 /* no LBN_SELCHANGE in this case, update manually */
2230 if( lphc->wState & CBF_EDIT )
2231 CBUpdateEdit( lphc, (INT)wParam );
2233 InvalidateRect(lphc->self, &lphc->textRect, TRUE);
2234 lphc->wState &= ~CBF_SELCHANGE;
2236 case CB_GETLBTEXT16:
2237 wParam = (INT)(INT16)wParam;
2238 lParam = (LPARAM)MapSL(lParam);
2241 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXT, wParam, lParam) :
2242 SendMessageA(lphc->hWndLBox, LB_GETTEXT, wParam, lParam);
2243 case CB_GETLBTEXTLEN16:
2244 wParam = (INT)(INT16)wParam;
2246 case CB_GETLBTEXTLEN:
2247 return unicode ? SendMessageW(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0) :
2248 SendMessageA(lphc->hWndLBox, LB_GETTEXTLEN, wParam, 0);
2249 case CB_GETITEMDATA16:
2250 wParam = (INT)(INT16)wParam;
2252 case CB_GETITEMDATA:
2253 return SendMessageW(lphc->hWndLBox, LB_GETITEMDATA, wParam, 0);
2254 case CB_SETITEMDATA16:
2255 wParam = (INT)(INT16)wParam;
2257 case CB_SETITEMDATA:
2258 return SendMessageW(lphc->hWndLBox, LB_SETITEMDATA, wParam, lParam);
2259 case CB_GETEDITSEL16:
2260 wParam = lParam = 0; /* just in case */
2263 /* Edit checks passed parameters itself */
2264 if( lphc->wState & CBF_EDIT )
2265 return SendMessageW(lphc->hWndEdit, EM_GETSEL, wParam, lParam);
2267 case CB_SETEDITSEL16:
2269 if( lphc->wState & CBF_EDIT )
2270 return SendMessageW(lphc->hWndEdit, EM_SETSEL,
2271 (INT)(INT16)LOWORD(lParam), (INT)(INT16)HIWORD(lParam) );
2273 case CB_SETEXTENDEDUI16:
2274 case CB_SETEXTENDEDUI:
2275 if( CB_GETTYPE(lphc) == CBS_SIMPLE )
2278 lphc->wState |= CBF_EUI;
2279 else lphc->wState &= ~CBF_EUI;
2281 case CB_GETEXTENDEDUI16:
2282 case CB_GETEXTENDEDUI:
2283 return (lphc->wState & CBF_EUI) ? TRUE : FALSE;
2286 if (message >= WM_USER)
2287 WARN("unknown msg WM_USER+%04x wp=%04x lp=%08lx\n",
2288 message - WM_USER, wParam, lParam );
2291 return unicode ? DefWindowProcW(hwnd, message, wParam, lParam) :
2292 DefWindowProcA(hwnd, message, wParam, lParam);
2295 /***********************************************************************
2298 * This is just a wrapper for the real ComboWndProc which locks/unlocks
2301 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
2303 if (!IsWindow(hwnd)) return 0;
2304 return ComboWndProc_common( hwnd, message, wParam, lParam, FALSE );
2307 /***********************************************************************
2310 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
2312 if (!IsWindow(hwnd)) return 0;
2313 return ComboWndProc_common( hwnd, message, wParam, lParam, TRUE );
2316 /*************************************************************************
2317 * GetComboBoxInfo (USER32.@)
2319 BOOL WINAPI GetComboBoxInfo(HWND hwndCombo, /* [in] handle to combo box */
2320 PCOMBOBOXINFO pcbi /* [in/out] combo box information */)