Removed some HEAP_xalloc calls.
[wine] / windows / syscolor.c
1 /*
2  * Support for system colors
3  *
4  * Copyright  David W. Metcalfe, 1993
5  * Copyright  Alexandre Julliard, 1994
6  *
7  */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "windef.h"
14 #include "wingdi.h"
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "sysmetrics.h"
18 #include "winbase.h"
19 #include "winuser.h"
20 #include "debugtools.h"
21 #include "tweak.h"
22 #include "winreg.h"
23
24 DEFAULT_DEBUG_CHANNEL(syscolor)
25
26 static const char * const DefSysColors[] =
27 {
28     "Scrollbar", "224 224 224",      /* COLOR_SCROLLBAR           */
29     "Background", "192 192 192",     /* COLOR_BACKGROUND          */
30     "ActiveTitle", "0 64 128",       /* COLOR_ACTIVECAPTION       */
31     "InactiveTitle", "255 255 255",  /* COLOR_INACTIVECAPTION     */
32     "Menu", "255 255 255",           /* COLOR_MENU                */
33     "Window", "255 255 255",         /* COLOR_WINDOW              */
34     "WindowFrame", "0 0 0",          /* COLOR_WINDOWFRAME         */
35     "MenuText", "0 0 0",             /* COLOR_MENUTEXT            */
36     "WindowText", "0 0 0",           /* COLOR_WINDOWTEXT          */
37     "TitleText", "255 255 255",      /* COLOR_CAPTIONTEXT         */
38     "ActiveBorder", "128 128 128",   /* COLOR_ACTIVEBORDER        */
39     "InactiveBorder", "255 255 255", /* COLOR_INACTIVEBORDER      */
40     "AppWorkspace", "255 255 232",   /* COLOR_APPWORKSPACE        */
41     "Hilight", "224 224 224",        /* COLOR_HIGHLIGHT           */
42     "HilightText", "0 0 0",          /* COLOR_HIGHLIGHTTEXT       */
43     "ButtonFace", "192 192 192",     /* COLOR_BTNFACE             */
44     "ButtonShadow", "128 128 128",   /* COLOR_BTNSHADOW           */
45     "GrayText", "192 192 192",       /* COLOR_GRAYTEXT            */
46     "ButtonText", "0 0 0",           /* COLOR_BTNTEXT             */
47     "InactiveTitleText", "0 0 0",    /* COLOR_INACTIVECAPTIONTEXT */
48     "ButtonHilight", "255 255 255",  /* COLOR_BTNHIGHLIGHT        */
49     "3DDarkShadow", "32 32 32",      /* COLOR_3DDKSHADOW          */
50     "3DLight", "192 192 192",        /* COLOR_3DLIGHT             */
51     "InfoText", "0 0 0",             /* COLOR_INFOTEXT            */
52     "InfoBackground", "255 255 192", /* COLOR_INFOBK              */
53     "AlternateButtonFace", "184 180 184",  /* COLOR_ALTERNATEBTNFACE */
54     "HotTrackingColor", "0 0 255",         /* COLOR_HOTLIGHT */
55     "GradientActiveTitle", "16 132 208",   /* COLOR_GRADIENTACTIVECAPTION */
56     "GradientInactiveTitle", "184 180 184" /* COLOR_GRADIENTINACTIVECAPTION */
57 };
58
59 static const char * const DefSysColors95[] =
60 {
61     "Scrollbar", "223 223 223",      /* COLOR_SCROLLBAR           */
62     "Background", "192 192 192",     /* COLOR_BACKGROUND          */
63     "ActiveTitle", "0 0 128",        /* COLOR_ACTIVECAPTION       */
64     "InactiveTitle", "128 128 128",  /* COLOR_INACTIVECAPTION     */
65     "Menu", "192 192 192",           /* COLOR_MENU                */
66     "Window", "255 255 255",         /* COLOR_WINDOW              */
67     "WindowFrame", "0 0 0",          /* COLOR_WINDOWFRAME         */
68     "MenuText", "0 0 0",             /* COLOR_MENUTEXT            */
69     "WindowText", "0 0 0",           /* COLOR_WINDOWTEXT          */
70     "TitleText", "255 255 255",      /* COLOR_CAPTIONTEXT         */
71     "ActiveBorder", "192 192 192",   /* COLOR_ACTIVEBORDER        */
72     "InactiveBorder", "192 192 192", /* COLOR_INACTIVEBORDER      */
73     "AppWorkspace", "128 128 128",   /* COLOR_APPWORKSPACE        */
74     "Hilight", "0 0 128",            /* COLOR_HIGHLIGHT           */
75     "HilightText", "255 255 255",    /* COLOR_HIGHLIGHTTEXT       */
76     "ButtonFace", "192 192 192",     /* COLOR_BTNFACE             */
77     "ButtonShadow", "128 128 128",   /* COLOR_BTNSHADOW           */
78     "GrayText", "128 128 128",       /* COLOR_GRAYTEXT            */
79     "ButtonText", "0 0 0",           /* COLOR_BTNTEXT             */
80     "InactiveTitleText", "0 0 0",    /* COLOR_INACTIVECAPTIONTEXT */
81     "ButtonHilight", "255 255 255",  /* COLOR_BTNHIGHLIGHT        */
82     "3DDarkShadow", "0 0 0",         /* COLOR_3DDKSHADOW          */
83     "3DLight", "192 192 192",        /* COLOR_3DLIGHT             */
84     "InfoText", "0 0 0",             /* COLOR_INFOTEXT            */
85     "InfoBackground", "255 255 192", /* COLOR_INFOBK              */
86     "AlternateButtonFace", "184 180 184",  /* COLOR_ALTERNATEBTNFACE */
87     "HotTrackingColor", "0 0 255",         /* COLOR_HOTLIGHT */
88     "GradientActiveTitle", "16 132 208",   /* COLOR_GRADIENTACTIVECAPTION */
89     "GradientInactiveTitle", "184 180 184" /* COLOR_GRADIENTINACTIVECAPTION */
90 };
91
92
93 #define NUM_SYS_COLORS     (COLOR_GRADIENTINACTIVECAPTION+1)
94
95 static COLORREF SysColors[NUM_SYS_COLORS];
96 static HBRUSH SysColorBrushes[NUM_SYS_COLORS];
97 static HPEN   SysColorPens[NUM_SYS_COLORS];
98
99 #define MAKE_SOLID(color) \
100        (PALETTEINDEX(GetNearestPaletteIndex(STOCK_DEFAULT_PALETTE,(color))))
101
102 /*************************************************************************
103  *             SYSCOLOR_SetColor
104  */
105 static void SYSCOLOR_SetColor( int index, COLORREF color )
106 {
107     if (index < 0 || index >= NUM_SYS_COLORS) return;
108     SysColors[index] = color;
109     if (SysColorBrushes[index]) DeleteObject( SysColorBrushes[index] );
110     SysColorBrushes[index] = CreateSolidBrush( color );
111     if (SysColorPens[index]) DeleteObject( SysColorPens[index] ); 
112     SysColorPens[index] = CreatePen( PS_SOLID, 1, color );
113 }
114
115
116 /*************************************************************************
117  *             SYSCOLOR_Init
118  */
119 void SYSCOLOR_Init(void)
120 {
121     int i, r, g, b;
122     const char * const *p;
123     char buffer[100];
124     BOOL bOk = FALSE, bNoReg = FALSE;
125     HKEY  hKey;
126
127     p = (TWEAK_WineLook == WIN31_LOOK) ? DefSysColors : DefSysColors95;
128
129     /* first, try to read the values from the registry */
130     if (RegCreateKeyExA(HKEY_CURRENT_USER, "Control Panel\\Colors", 0, 0, 0, KEY_ALL_ACCESS, 0, &hKey, 0))
131       bNoReg = TRUE;
132     for (i = 0; i < NUM_SYS_COLORS; i++)
133     { bOk = FALSE;
134
135       /* first try, registry */
136       if (!bNoReg)
137       {
138         DWORD dwDataSize = sizeof(buffer);
139         if (!(RegQueryValueExA(hKey,(LPSTR)p[i*2], 0, 0, buffer, &dwDataSize)))
140           if (sscanf( buffer, "%d %d %d", &r, &g, &b ) == 3) 
141             bOk = TRUE;
142       }
143
144       /* second try, win.ini */
145       if (!bOk)
146       { GetProfileStringA( "colors", p[i*2], p[i*2+1], buffer, 100 );
147         if (sscanf( buffer, " %d %d %d", &r, &g, &b ) == 3)
148           bOk = TRUE;
149       }
150       
151       /* last chance, take the default */
152       if (!bOk)
153       { int iNumColors = sscanf( p[i*2+1], " %d %d %d", &r, &g, &b );
154         assert (iNumColors==3);
155       }
156       
157       SYSCOLOR_SetColor( i, RGB(r,g,b) );
158     }
159     if (!bNoReg)
160       RegCloseKey(hKey);
161 }
162
163
164 /*************************************************************************
165  *             GetSysColor16   (USER.180)
166  */
167 COLORREF WINAPI GetSysColor16( INT16 nIndex )
168 {
169     return GetSysColor (nIndex);
170 }
171
172
173 /*************************************************************************
174  *             GetSysColor   (USER32.289)
175  */
176 COLORREF WINAPI GetSysColor( INT nIndex )
177 {
178     if (nIndex >= 0 && nIndex < NUM_SYS_COLORS)
179         return SysColors[nIndex];
180     else
181         return 0;
182 }
183
184
185 /*************************************************************************
186  *             SetSysColors16   (USER.181)
187  */
188 VOID WINAPI SetSysColors16( INT16 nChanges, const INT16 *lpSysColor,
189                             const COLORREF *lpColorValues )
190 {
191     int i;
192
193     for (i = 0; i < nChanges; i++)
194     {
195         SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
196     }
197
198     /* Send WM_SYSCOLORCHANGE message to all windows */
199
200     SendMessageA( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0 );
201
202     /* Repaint affected portions of all visible windows */
203
204     RedrawWindow( GetDesktopWindow(), NULL, 0,
205                 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
206 }
207
208
209 /*************************************************************************
210  *             SetSysColors   (USER32.505)
211  */
212 BOOL WINAPI SetSysColors( INT nChanges, const INT *lpSysColor,
213                               const COLORREF *lpColorValues )
214 {
215     int i;
216
217     for (i = 0; i < nChanges; i++)
218     {
219         SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
220     }
221
222     /* Send WM_SYSCOLORCHANGE message to all windows */
223
224     SendMessageA( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0 );
225
226     /* Repaint affected portions of all visible windows */
227
228     RedrawWindow( GetDesktopWindow(), NULL, 0,
229                 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
230     return TRUE;
231 }
232
233
234 /***********************************************************************
235  *           GetSysColorBrush16    (USER.281)
236  */
237 HBRUSH16 WINAPI GetSysColorBrush16( INT16 index )
238 {
239     return (HBRUSH16)GetSysColorBrush(index);
240 }
241
242
243 /***********************************************************************
244  *           GetSysColorBrush    (USER32.290)
245  */
246 HBRUSH WINAPI GetSysColorBrush( INT index )
247 {
248     if (0 <= index && index < NUM_SYS_COLORS)
249         return SysColorBrushes[index];
250     WARN("Unknown index(%d)\n", index );
251     return GetStockObject(LTGRAY_BRUSH);
252 }
253
254
255 /***********************************************************************
256  *           GetSysColorPen16    (Not a Windows API)
257  */
258 HPEN16 WINAPI GetSysColorPen16( INT16 index )
259 {
260     return (HPEN16)GetSysColorPen(index);
261 }
262
263
264 /***********************************************************************
265  *           GetSysColorPen    (Not a Windows API)
266  *
267  * This function is new to the Wine lib -- it does not exist in 
268  * Windows. However, it is a natural complement for GetSysColorBrush
269  * in the Win32 API and is needed quite a bit inside Wine.
270  */
271 HPEN WINAPI GetSysColorPen( INT index )
272 {
273     /* We can assert here, because this function is internal to Wine */
274     assert (0 <= index && index < NUM_SYS_COLORS);
275     return SysColorPens[index];
276
277 }