theming.c 5.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Theming - Initialization
 *
 * Copyright (c) 2005 by Frank Richter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23 24 25 26 27 28
 *
 */

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "comctl32.h"
29
#include "uxtheme.h"
30 31 32 33 34 35 36
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(theming);

typedef LRESULT (CALLBACK* THEMING_SUBCLASSPROC)(HWND, UINT, WPARAM, LPARAM,
    ULONG_PTR);

37
extern LRESULT CALLBACK THEMING_DialogSubclassProc (HWND, UINT, WPARAM, LPARAM,
38
                                                    ULONG_PTR) DECLSPEC_HIDDEN;
39 40
extern LRESULT CALLBACK THEMING_ScrollbarSubclassProc (HWND, UINT, WPARAM, LPARAM,
                                                       ULONG_PTR) DECLSPEC_HIDDEN;
41

42
static const WCHAR dialogClass[] = {'#','3','2','7','7','0',0};
43

44 45 46 47 48 49
static const struct ThemingSubclass
{
    const WCHAR* className;
    THEMING_SUBCLASSPROC subclassProc;
} subclasses[] = {
    /* Note: list must be sorted by class name */
50
    {dialogClass,          THEMING_DialogSubclassProc},
51
    {WC_SCROLLBARW,        THEMING_ScrollbarSubclassProc}
52 53
};

54
#define NUM_SUBCLASSES        (ARRAY_SIZE(subclasses))
55 56 57 58 59

static WNDPROC originalProcs[NUM_SUBCLASSES];
static ATOM atRefDataProp;
static ATOM atSubclassProp;

60 61
/* Generate a number of subclass window procs.
 * With a single proc alone, we can't really reliably find out the superclass,
62
 * so have one for each subclass. The subclass number is also stored in a prop
63
 * since it's needed by THEMING_CallOriginalClass(). Then, the subclass
64
 * proc and ref data are fetched and the proc called.
65
 */
66 67
#define MAKE_SUBCLASS_PROC(N)                                               \
static LRESULT CALLBACK subclass_proc ## N (HWND wnd, UINT msg,             \
68 69
                                            WPARAM wParam, LPARAM lParam)   \
{                                                                           \
70 71
    LRESULT result;                                                         \
    ULONG_PTR refData;                                                      \
72 73
    SetPropW (wnd, (LPCWSTR)MAKEINTATOM(atSubclassProp), (HANDLE)N);        \
    refData = (ULONG_PTR)GetPropW (wnd, (LPCWSTR)MAKEINTATOM(atRefDataProp)); \
74
    TRACE ("%d; (%p, %x, %lx, %lx, %lx)\n", N, wnd, msg, wParam, lParam,     \
75 76 77 78
        refData);                                                           \
    result = subclasses[N].subclassProc (wnd, msg, wParam, lParam, refData);\
    TRACE ("result = %lx\n", result);                                       \
    return result;                                                          \
79 80
}

81 82 83
MAKE_SUBCLASS_PROC(0)
MAKE_SUBCLASS_PROC(1)

84
static const WNDPROC subclassProcs[NUM_SUBCLASSES] = {
85 86
    subclass_proc0,
    subclass_proc1,
87 88
};

89 90 91 92 93 94 95 96
/***********************************************************************
 * THEMING_Initialize
 *
 * Register classes for standard controls that will shadow the system
 * classes.
 */
void THEMING_Initialize (void)
{
97
    unsigned int i;
98 99 100 101 102
    static const WCHAR subclassPropName[] = 
        { 'C','C','3','2','T','h','e','m','i','n','g','S','u','b','C','l',0 };
    static const WCHAR refDataPropName[] = 
        { 'C','C','3','2','T','h','e','m','i','n','g','D','a','t','a',0 };

103 104
    if (!IsThemeActive()) return;

105 106 107 108 109 110 111 112
    atSubclassProp = GlobalAddAtomW (subclassPropName);
    atRefDataProp = GlobalAddAtomW (refDataPropName);

    for (i = 0; i < NUM_SUBCLASSES; i++)
    {
        WNDCLASSEXW class;

        class.cbSize = sizeof(class);
113 114 115 116 117 118
        if (!GetClassInfoExW (NULL, subclasses[i].className, &class))
        {
            ERR("Could not retrieve information for class %s\n",
                debugstr_w (subclasses[i].className));
            continue;
        }
119
        originalProcs[i] = class.lpfnWndProc;
120
        class.lpfnWndProc = subclassProcs[i];
121 122 123
        
        if (!class.lpfnWndProc)
        {
124
            ERR("Missing proc for class %s\n", 
125 126 127
                debugstr_w (subclasses[i].className));
            continue;
        }
128 129 130

        if (!RegisterClassExW (&class))
        {
131
            ERR("Could not re-register class %s: %x\n",
132 133 134 135 136 137 138 139 140 141
                debugstr_w (subclasses[i].className), GetLastError ());
        }
        else
        {
            TRACE("Re-registered class %s\n", 
                debugstr_w (subclasses[i].className));
        }
    }
}

142 143 144 145 146 147 148
/***********************************************************************
 * THEMING_Uninitialize
 *
 * Unregister shadow classes for standard controls.
 */
void THEMING_Uninitialize (void)
{
149
    unsigned int i;
150 151 152

    if (!atSubclassProp) return;  /* not initialized */

153 154 155 156 157 158
    for (i = 0; i < NUM_SUBCLASSES; i++)
    {
        UnregisterClassW (subclasses[i].className, NULL);
    }
}

159 160 161 162 163 164 165
/***********************************************************************
 * THEMING_CallOriginalClass
 *
 * Determines the original window proc and calls it.
 */
LRESULT THEMING_CallOriginalClass (HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
166
    INT_PTR subclass = (INT_PTR)GetPropW (wnd, (LPCWSTR)MAKEINTATOM(atSubclassProp));
167 168 169
    WNDPROC oldProc = originalProcs[subclass];
    return CallWindowProcW (oldProc, wnd, msg, wParam, lParam);
}