comctl32/listview: Convert header notifications to ANSI in place.
[wine] / dlls / comctl32 / theme_combo.c
1 /*
2  * Theming - Combo box control
3  *
4  * Copyright (c) 2005 by Frank Richter
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdlib.h>
25
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"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(themingcombo);
36
37 /* Subclass-private state flags */
38 #define STATE_NOREDRAW         1
39 #define STATE_HOT              2
40
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
46
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)
49 {
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;
58
59     TRACE("\n");
60
61     /* follow Windows combobox that sends a bunch of text
62      * inquiries to its listbox while processing WM_PAINT. */
63
64     if( (id = SendMessageW (cbi->hwndList, LB_GETCURSEL, 0, 0) ) != LB_ERR )
65     {
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))) )
70         {
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;
75     }
76     else
77        if( !(dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) )
78            return;
79
80     /*
81      * Give ourselves some space.
82      */
83     CopyRect (&rectEdit, &cbi->rcItem);
84     InflateRect( &rectEdit, -1, -1 );
85      
86     if(dwStyle & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
87     {
88        DRAWITEMSTRUCT dis;
89        HRGN           clipRegion;
90        UINT ctlid = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
91
92        /* setup state for DRAWITEM message. Owner will highlight */
93        if ( focused && !dropped )
94            itemState |= ODS_SELECTED | ODS_FOCUS;
95
96        /*
97         * Save the current clip region.
98         * To retrieve the clip region, we need to create one "dummy"
99         * clip region.
100         */
101        clipRegion = CreateRectRgnIndirect(&rectEdit);
102
103        if (GetClipRgn(hdc, clipRegion)!=1)
104        {
105          DeleteObject(clipRegion);
106          clipRegion=NULL;
107        }
108
109        if (!IsWindowEnabled(hwnd)) itemState |= ODS_DISABLED;
110
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);
120
121        /*
122         * Clip the DC and have the parent draw the item.
123         */
124        IntersectClipRect(hdc,
125                          rectEdit.left,  rectEdit.top,
126                          rectEdit.right, rectEdit.bottom);
127
128        SendMessageW(GetParent (hwnd), WM_DRAWITEM, ctlid, (LPARAM)&dis );
129
130        /*
131         * Reset the clipping region.
132         */
133        SelectClipRgn(hdc, clipRegion);
134     }
135     else
136     {
137        static const WCHAR empty_stringW[] = { 0 };
138
139        if ( focused && !dropped ) {
140
141            /* highlight */
142            FillRect( hdc, &rectEdit, GetSysColorBrush(COLOR_HIGHLIGHT) );
143            SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
144            SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
145       }
146
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 );
153
154       if ( focused && !dropped )
155         DrawFocusRect( hdc, &rectEdit );
156     }
157
158     if( hPrevFont )
159       SelectObject(hdc, hPrevFont );
160    
161     HeapFree( GetProcessHeap(), 0, pText );
162 }
163
164 /* paint the combobox */
165 static LRESULT paint (HTHEME theme, HWND hwnd, HDC hParamDC, ULONG state)
166 {
167   PAINTSTRUCT ps;
168   HDC   hDC;
169   COMBOBOXINFO cbi;
170   DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
171
172   hDC = (hParamDC) ? hParamDC
173                    : BeginPaint( hwnd, &ps);
174
175   TRACE("hdc=%p\n", hDC);
176
177   if( hDC && !(state & STATE_NOREDRAW) )
178   {
179       RECT frameRect;
180       int buttonState;
181   
182       cbi.cbSize = sizeof (cbi);
183       SendMessageW (hwnd, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbi);
184       
185       /* paint border */
186       if ((dwStyle & CBS_DROPDOWNLIST) != CBS_SIMPLE)
187           GetClientRect (hwnd, &frameRect);
188       else
189       {
190           CopyRect (&frameRect, &cbi.rcItem);
191
192           InflateRect(&frameRect, 
193               EDIT_CONTROL_PADDING + COMBO_XBORDERSIZE, 
194               EDIT_CONTROL_PADDING + COMBO_YBORDERSIZE);
195       }
196       
197       DrawThemeBackground (theme, hDC, 0, 
198           IsWindowEnabled (hwnd) ? CBXS_NORMAL : CBXS_DISABLED, &frameRect, NULL);
199       
200       /* paint button */
201       if (cbi.stateButton != STATE_SYSTEM_INVISIBLE)
202       {
203           if (!IsWindowEnabled (hwnd))
204               buttonState = CBXS_DISABLED;
205           else if (cbi.stateButton == STATE_SYSTEM_PRESSED)
206               buttonState = CBXS_PRESSED;
207           else if (state & STATE_HOT)
208               buttonState = CBXS_HOT;
209           else
210               buttonState = CBXS_NORMAL;
211           DrawThemeBackground (theme, hDC, CP_DROPDOWNBUTTON, buttonState, 
212               &cbi.rcButton, NULL);
213       }
214
215       /* paint text, if we need to */
216       if ((dwStyle & CBS_DROPDOWNLIST) == CBS_DROPDOWNLIST)
217           paint_text (hwnd, hDC, dwStyle, &cbi);
218   }
219
220   if( !hParamDC )
221     EndPaint(hwnd, &ps);
222
223   return 0;
224 }
225
226
227 /**********************************************************************
228  * The combo control subclass window proc.
229  */
230 LRESULT CALLBACK THEMING_ComboSubclassProc (HWND hwnd, UINT msg, 
231                                             WPARAM wParam, LPARAM lParam, 
232                                             ULONG_PTR dwRefData)
233 {
234     const WCHAR* themeClass = WC_COMBOBOXW;
235     HTHEME theme;
236     LRESULT result;
237       
238     switch (msg)
239     {
240     case WM_CREATE:
241         result = THEMING_CallOriginalClass  (hwnd, msg, wParam, lParam);
242         OpenThemeData( hwnd, themeClass );
243         return result;
244     
245     case WM_DESTROY:
246         theme = GetWindowTheme( hwnd );
247         CloseThemeData ( theme );
248         return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
249
250     case WM_THEMECHANGED:
251         theme = GetWindowTheme( hwnd );
252         CloseThemeData ( theme );
253         OpenThemeData( hwnd, themeClass );
254         break;
255         
256     case WM_SYSCOLORCHANGE:
257         theme = GetWindowTheme( hwnd );
258         if (!theme) return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
259         /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
260          * which will do the repaint. */
261         break;
262         
263     case WM_PAINT:
264         theme = GetWindowTheme( hwnd );
265         if (!theme) return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
266         return paint (theme, hwnd, (HDC)wParam, dwRefData);
267
268     case WM_SETREDRAW:
269         /* Since there doesn't seem to be WM_GETREDRAW, do redraw tracking in 
270          * the subclass as well. */
271         if( wParam )
272             dwRefData &= ~STATE_NOREDRAW;
273         else
274             dwRefData |= STATE_NOREDRAW;
275         THEMING_SetSubclassData (hwnd, dwRefData);
276         return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
277         
278     case WM_MOUSEMOVE:
279         {
280             /* Dropdown button hot-tracking */
281             COMBOBOXINFO cbi;
282             POINT pt;
283
284             pt.x = (short)LOWORD(lParam);
285             pt.y = (short)HIWORD(lParam);
286             cbi.cbSize = sizeof (cbi);
287             SendMessageW (hwnd, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbi);
288             
289             if (cbi.stateButton != STATE_SYSTEM_INVISIBLE)
290             {
291                 if (PtInRect (&cbi.rcButton, pt))
292                 {
293                     if (!(dwRefData & STATE_HOT))
294                     {
295                         dwRefData |= STATE_HOT;
296                         THEMING_SetSubclassData (hwnd, dwRefData);
297                         RedrawWindow (hwnd, &cbi.rcButton, 0, 
298                             RDW_INVALIDATE | RDW_UPDATENOW);
299                     }
300                 }
301                 else
302                 {
303                     if (dwRefData & STATE_HOT)
304                     {
305                         dwRefData &= ~STATE_HOT;
306                         THEMING_SetSubclassData (hwnd, dwRefData);
307                         RedrawWindow (hwnd, &cbi.rcButton, 0, 
308                             RDW_INVALIDATE | RDW_UPDATENOW);
309                     }
310                 }
311             }
312         }
313         return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
314     
315     default: 
316         /* Call old proc */
317         return THEMING_CallOriginalClass (hwnd, msg, wParam, lParam);
318     }
319     return 0;
320 }