appwiz.cpl: Add __WINE_ALLOC_SIZE attributes to heap_xxx() functions.
[wine.git] / dlls / comctl32 / theme_combo.c
blob3ee1d922772848cebd9b0f95888544efc7457300
1 /*
2 * Theming - Combo box control
4 * Copyright (c) 2005 by Frank Richter
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
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "uxtheme.h"
31 #include "vssym32.h"
32 #include "comctl32.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(themingcombo);
37 /* Subclass-private state flags */
38 #define STATE_NOREDRAW 1
39 #define STATE_HOT 2
41 /* some constants for metrics, same as in user32 */
42 #define COMBO_XBORDERSIZE 2
43 #define COMBO_YBORDERSIZE 2
44 #define COMBO_EDITBUTTONSPACE 0
45 #define EDIT_CONTROL_PADDING 1
47 /* paint text of combobox, needed for read-only drop downs. */
48 static void paint_text (HWND hwnd, HDC hdc, DWORD dwStyle, const COMBOBOXINFO *cbi)
50 INT id, size = 0;
51 LPWSTR pText = NULL;
52 UINT itemState = ODS_COMBOBOXEDIT;
53 HFONT font = (HFONT)SendMessageW (hwnd, WM_GETFONT, 0, 0);
54 HFONT hPrevFont = (font) ? SelectObject(hdc, font) : 0;
55 RECT rectEdit;
56 BOOL focused = GetFocus () == hwnd;
57 BOOL dropped = cbi->stateButton == STATE_SYSTEM_PRESSED;
59 TRACE("\n");
61 /* follow Windows combobox that sends a bunch of text
62 * inquiries to its listbox while processing WM_PAINT. */
64 if( (id = SendMessageW (cbi->hwndList, LB_GETCURSEL, 0, 0) ) != LB_ERR )
66 size = SendMessageW (cbi->hwndList, LB_GETTEXTLEN, id, 0);
67 if (size == LB_ERR)
68 FIXME("LB_ERR probably not handled yet\n");
69 if( (pText = HeapAlloc( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))) )
71 /* size from LB_GETTEXTLEN may be too large, from LB_GETTEXT is accurate */
72 size = SendMessageW (cbi->hwndList, LB_GETTEXT, id, (LPARAM)pText);
73 pText[size] = '\0'; /* just in case */
74 } else return;
76 else
77 if( !(dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) )
78 return;
81 * Give ourselves some space.
83 rectEdit = cbi->rcItem;
84 InflateRect( &rectEdit, -1, -1 );
86 if(dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
88 DRAWITEMSTRUCT dis;
89 HRGN clipRegion;
90 UINT ctlid = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
92 /* setup state for DRAWITEM message. Owner will highlight */
93 if ( focused && !dropped )
94 itemState |= ODS_SELECTED | ODS_FOCUS;
97 * Save the current clip region.
98 * To retrieve the clip region, we need to create one "dummy"
99 * clip region.
101 clipRegion = CreateRectRgnIndirect(&rectEdit);
103 if (GetClipRgn(hdc, clipRegion)!=1)
105 DeleteObject(clipRegion);
106 clipRegion=NULL;
109 if (!IsWindowEnabled(hwnd)) itemState |= ODS_DISABLED;
111 dis.CtlType = ODT_COMBOBOX;
112 dis.CtlID = ctlid;
113 dis.hwndItem = hwnd;
114 dis.itemAction = ODA_DRAWENTIRE;
115 dis.itemID = id;
116 dis.itemState = itemState;
117 dis.hDC = hdc;
118 dis.rcItem = rectEdit;
119 dis.itemData = SendMessageW(cbi->hwndList, LB_GETITEMDATA, id, 0);
122 * Clip the DC and have the parent draw the item.
124 IntersectClipRect(hdc,
125 rectEdit.left, rectEdit.top,
126 rectEdit.right, rectEdit.bottom);
128 SendMessageW(GetParent (hwnd), WM_DRAWITEM, ctlid, (LPARAM)&dis );
131 * Reset the clipping region.
133 SelectClipRgn(hdc, clipRegion);
135 else
137 static const WCHAR empty_stringW[] = { 0 };
139 if ( focused && !dropped ) {
141 /* highlight */
142 FillRect( hdc, &rectEdit, GetSysColorBrush(COLOR_HIGHLIGHT) );
143 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
144 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
147 ExtTextOutW( hdc,
148 rectEdit.left + 1,
149 rectEdit.top + 1,
150 ETO_OPAQUE | ETO_CLIPPED,
151 &rectEdit,
152 pText ? pText : empty_stringW , size, NULL );
154 if ( focused && !dropped )
155 DrawFocusRect( hdc, &rectEdit );
158 if( hPrevFont )
159 SelectObject(hdc, hPrevFont );
161 HeapFree( GetProcessHeap(), 0, pText );
164 /* paint the combobox */
165 static LRESULT paint (HTHEME theme, HWND hwnd, HDC hParamDC, ULONG state)
167 PAINTSTRUCT ps;
168 HDC hDC;
169 COMBOBOXINFO cbi;
170 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
172 hDC = (hParamDC) ? hParamDC
173 : BeginPaint( hwnd, &ps);
175 TRACE("hdc=%p\n", hDC);
177 if( hDC && !(state & STATE_NOREDRAW) )
179 RECT frameRect;
180 int buttonState;
182 cbi.cbSize = sizeof (cbi);
183 SendMessageW (hwnd, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbi);
185 /* paint border */
186 if ((dwStyle & CBS_DROPDOWNLIST) != CBS_SIMPLE)
187 GetClientRect (hwnd, &frameRect);
188 else
190 frameRect = cbi.rcItem;
191 InflateRect(&frameRect,
192 EDIT_CONTROL_PADDING + COMBO_XBORDERSIZE,
193 EDIT_CONTROL_PADDING + COMBO_YBORDERSIZE);
196 DrawThemeBackground (theme, hDC, 0,
197 IsWindowEnabled (hwnd) ? CBXS_NORMAL : CBXS_DISABLED, &frameRect, NULL);
199 /* paint button */
200 if (cbi.stateButton != STATE_SYSTEM_INVISIBLE)
202 if (!IsWindowEnabled (hwnd))
203 buttonState = CBXS_DISABLED;
204 else if (cbi.stateButton == STATE_SYSTEM_PRESSED)
205 buttonState = CBXS_PRESSED;
206 else if (state & STATE_HOT)
207 buttonState = CBXS_HOT;
208 else
209 buttonState = CBXS_NORMAL;
210 DrawThemeBackground (theme, hDC, CP_DROPDOWNBUTTON, buttonState,
211 &cbi.rcButton, NULL);
214 /* paint text, if we need to */
215 if ((dwStyle & CBS_DROPDOWNLIST) == CBS_DROPDOWNLIST)
216 paint_text (hwnd, hDC, dwStyle, &cbi);
219 if( !hParamDC )
220 EndPaint(hwnd, &ps);
222 return 0;
226 /**********************************************************************
227 * The combo control subclass window proc.
229 LRESULT CALLBACK THEMING_ComboSubclassProc (HWND hwnd, UINT msg,
230 WPARAM wParam, LPARAM lParam,
231 ULONG_PTR dwRefData)
233 const WCHAR* themeClass = WC_COMBOBOXW;
234 HTHEME theme;
235 LRESULT result;
237 switch (msg)
239 case WM_CREATE:
240 result = THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
241 OpenThemeData( hwnd, themeClass );
242 return result;
244 case WM_DESTROY:
245 theme = GetWindowTheme( hwnd );
246 CloseThemeData ( theme );
247 return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
249 case WM_THEMECHANGED:
250 theme = GetWindowTheme( hwnd );
251 CloseThemeData ( theme );
252 OpenThemeData( hwnd, themeClass );
253 break;
255 case WM_SYSCOLORCHANGE:
256 theme = GetWindowTheme( hwnd );
257 if (!theme) return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
258 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
259 * which will do the repaint. */
260 break;
262 case WM_PAINT:
263 theme = GetWindowTheme( hwnd );
264 if (!theme) return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
265 return paint (theme, hwnd, (HDC)wParam, dwRefData);
267 case WM_SETREDRAW:
268 /* Since there doesn't seem to be WM_GETREDRAW, do redraw tracking in
269 * the subclass as well. */
270 if( wParam )
271 dwRefData &= ~STATE_NOREDRAW;
272 else
273 dwRefData |= STATE_NOREDRAW;
274 THEMING_SetSubclassData (hwnd, dwRefData);
275 return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
277 case WM_MOUSEMOVE:
279 /* Dropdown button hot-tracking */
280 COMBOBOXINFO cbi;
281 POINT pt;
283 pt.x = (short)LOWORD(lParam);
284 pt.y = (short)HIWORD(lParam);
285 cbi.cbSize = sizeof (cbi);
286 SendMessageW (hwnd, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbi);
288 if (cbi.stateButton != STATE_SYSTEM_INVISIBLE)
290 if (PtInRect (&cbi.rcButton, pt))
292 if (!(dwRefData & STATE_HOT))
294 dwRefData |= STATE_HOT;
295 THEMING_SetSubclassData (hwnd, dwRefData);
296 RedrawWindow (hwnd, &cbi.rcButton, 0,
297 RDW_INVALIDATE | RDW_UPDATENOW);
300 else
302 if (dwRefData & STATE_HOT)
304 dwRefData &= ~STATE_HOT;
305 THEMING_SetSubclassData (hwnd, dwRefData);
306 RedrawWindow (hwnd, &cbi.rcButton, 0,
307 RDW_INVALIDATE | RDW_UPDATENOW);
312 return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
314 default:
315 /* Call old proc */
316 return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
318 return 0;