Update the address of the Free Software Foundation.
[wine] / dlls / comctl32 / theming.c
1 /*
2  * Theming - Initialization
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
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "comctl32.h"
29 #include "uxtheme.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(theming);
33
34 typedef LRESULT (CALLBACK* THEMING_SUBCLASSPROC)(HWND, UINT, WPARAM, LPARAM,
35     ULONG_PTR);
36
37 extern LRESULT CALLBACK THEMING_ComboSubclassProc (HWND, UINT, WPARAM, LPARAM,
38                                                    ULONG_PTR);
39 extern LRESULT CALLBACK THEMING_DialogSubclassProc (HWND, UINT, WPARAM, LPARAM,
40                                                     ULONG_PTR);
41 extern LRESULT CALLBACK THEMING_EditSubclassProc (HWND, UINT, WPARAM, LPARAM,
42                                                   ULONG_PTR);
43 extern LRESULT CALLBACK THEMING_ListBoxSubclassProc (HWND, UINT, WPARAM, LPARAM,
44                                                      ULONG_PTR);
45
46 static const WCHAR dialogClass[] = {'#','3','2','7','7','0',0};
47 static const WCHAR comboLboxClass[] = {'C','o','m','b','o','L','b','o','x',0};
48
49 static const struct ThemingSubclass
50 {
51     const WCHAR* className;
52     THEMING_SUBCLASSPROC subclassProc;
53 } subclasses[] = {
54     /* Note: list must be sorted by class name */
55     {dialogClass,          THEMING_DialogSubclassProc},
56     {WC_COMBOBOXW,         THEMING_ComboSubclassProc},
57     {comboLboxClass,       THEMING_ListBoxSubclassProc},
58     {WC_EDITW,             THEMING_EditSubclassProc},
59     {WC_LISTBOXW,          THEMING_ListBoxSubclassProc}
60 };
61
62 #define NUM_SUBCLASSES        (sizeof(subclasses)/sizeof(subclasses[0]))
63
64 static WNDPROC originalProcs[NUM_SUBCLASSES];
65 static ATOM atRefDataProp;
66 static ATOM atSubclassProp;
67
68 /* Generate a number of subclass window procs.
69  * With a single proc alone, we can't really reliably find out the superclass,
70  * so have one for each subclass. The subclass number is also stored in a prop
71  * since it's needed by THEMING_CallOriginalClass(). Then, the the subclass 
72  * proc and ref data are fetched and the proc called.
73  */
74 #define MAKE_SUBCLASS_PROC(N)                                               \
75 static LRESULT CALLBACK subclass_proc ## N (HWND wnd, UINT msg,             \
76                                             WPARAM wParam, LPARAM lParam)   \
77 {                                                                           \
78     LRESULT result;                                                         \
79     ULONG_PTR refData;                                                      \
80     SetPropW (wnd, MAKEINTATOMW (atSubclassProp), (HANDLE)N);               \
81     refData = (ULONG_PTR)GetPropW (wnd, MAKEINTATOMW (atRefDataProp));      \
82     TRACE ("%d; (%p, %x, %x, %lx, %lx)\n", N, wnd, msg, wParam, lParam,     \
83         refData);                                                           \
84     result = subclasses[N].subclassProc (wnd, msg, wParam, lParam, refData);\
85     TRACE ("result = %lx\n", result);                                       \
86     return result;                                                          \
87 }
88
89 MAKE_SUBCLASS_PROC(0)
90 MAKE_SUBCLASS_PROC(1)
91 MAKE_SUBCLASS_PROC(2)
92 MAKE_SUBCLASS_PROC(3)
93 MAKE_SUBCLASS_PROC(4)
94
95 static const WNDPROC subclassProcs[NUM_SUBCLASSES] = {
96     subclass_proc0,
97     subclass_proc1,
98     subclass_proc2,
99     subclass_proc3,
100     subclass_proc4
101 };
102
103 /***********************************************************************
104  * THEMING_Initialize
105  *
106  * Register classes for standard controls that will shadow the system
107  * classes.
108  */
109 void THEMING_Initialize (void)
110 {
111     int i;
112     static const WCHAR subclassPropName[] = 
113         { 'C','C','3','2','T','h','e','m','i','n','g','S','u','b','C','l',0 };
114     static const WCHAR refDataPropName[] = 
115         { 'C','C','3','2','T','h','e','m','i','n','g','D','a','t','a',0 };
116
117     if (!IsThemeActive()) return;
118
119     atSubclassProp = GlobalAddAtomW (subclassPropName);
120     atRefDataProp = GlobalAddAtomW (refDataPropName);
121
122     for (i = 0; i < NUM_SUBCLASSES; i++)
123     {
124         WNDCLASSEXW class;
125
126         class.cbSize = sizeof(class);
127         class.style |= CS_GLOBALCLASS;
128         GetClassInfoExW (NULL, subclasses[i].className, &class);
129         originalProcs[i] = class.lpfnWndProc;
130         class.lpfnWndProc = subclassProcs[i];
131         
132         if (!class.lpfnWndProc)
133         {
134             ERR("Missing proc for class %s\n", 
135                 debugstr_w (subclasses[i].className));
136             continue;
137         }
138
139         if (!RegisterClassExW (&class))
140         {
141             ERR("Could not re-register class %s: %lx\n", 
142                 debugstr_w (subclasses[i].className), GetLastError ());
143         }
144         else
145         {
146             TRACE("Re-registered class %s\n", 
147                 debugstr_w (subclasses[i].className));
148         }
149     }
150 }
151
152 /***********************************************************************
153  * THEMING_Uninitialize
154  *
155  * Unregister shadow classes for standard controls.
156  */
157 void THEMING_Uninitialize (void)
158 {
159     int i;
160
161     if (!atSubclassProp) return;  /* not initialized */
162
163     for (i = 0; i < NUM_SUBCLASSES; i++)
164     {
165         UnregisterClassW (subclasses[i].className, NULL);
166     }
167 }
168
169 /***********************************************************************
170  * THEMING_CallOriginalClass
171  *
172  * Determines the original window proc and calls it.
173  */
174 LRESULT THEMING_CallOriginalClass (HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
175 {
176     INT_PTR subclass = (INT_PTR)GetPropW (wnd, MAKEINTATOMW (atSubclassProp));
177     WNDPROC oldProc = originalProcs[subclass];
178     return CallWindowProcW (oldProc, wnd, msg, wParam, lParam);
179 }
180
181 /***********************************************************************
182  * THEMING_SetSubclassData
183  *
184  * Update the "refData" value of the subclassed window.
185  */
186 void THEMING_SetSubclassData (HWND wnd, ULONG_PTR refData)
187 {
188     SetPropW (wnd, MAKEINTATOMW (atRefDataProp), (HANDLE)refData);
189 }