Fix memory leaks on error path.
[wine.git] / windows / syscolor.c
blobe98e1f8f2602393d6d684cd29a9bf1c28fda4d6e
1 /*
2 * Support for system colors
4 * Copyright David W. Metcalfe, 1993
5 * Copyright Alexandre Julliard, 1994
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "wine/winbase16.h"
31 #include "wine/winuser16.h"
32 #include "winuser.h"
33 #include "wownt32.h"
34 #include "winreg.h"
35 #include "local.h"
36 #include "user.h"
37 #include "gdi.h" /* sic */
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(syscolor);
42 static const char * const DefSysColors[] =
44 "Scrollbar", "224 224 224", /* COLOR_SCROLLBAR */
45 "Background", "192 192 192", /* COLOR_BACKGROUND */
46 "ActiveTitle", "0 64 128", /* COLOR_ACTIVECAPTION */
47 "InactiveTitle", "255 255 255", /* COLOR_INACTIVECAPTION */
48 "Menu", "255 255 255", /* COLOR_MENU */
49 "Window", "255 255 255", /* COLOR_WINDOW */
50 "WindowFrame", "0 0 0", /* COLOR_WINDOWFRAME */
51 "MenuText", "0 0 0", /* COLOR_MENUTEXT */
52 "WindowText", "0 0 0", /* COLOR_WINDOWTEXT */
53 "TitleText", "255 255 255", /* COLOR_CAPTIONTEXT */
54 "ActiveBorder", "128 128 128", /* COLOR_ACTIVEBORDER */
55 "InactiveBorder", "255 255 255", /* COLOR_INACTIVEBORDER */
56 "AppWorkspace", "255 255 232", /* COLOR_APPWORKSPACE */
57 "Hilight", "224 224 224", /* COLOR_HIGHLIGHT */
58 "HilightText", "0 0 0", /* COLOR_HIGHLIGHTTEXT */
59 "ButtonFace", "192 192 192", /* COLOR_BTNFACE */
60 "ButtonShadow", "128 128 128", /* COLOR_BTNSHADOW */
61 "GrayText", "192 192 192", /* COLOR_GRAYTEXT */
62 "ButtonText", "0 0 0", /* COLOR_BTNTEXT */
63 "InactiveTitleText", "0 0 0", /* COLOR_INACTIVECAPTIONTEXT */
64 "ButtonHilight", "255 255 255", /* COLOR_BTNHIGHLIGHT */
65 "3DDarkShadow", "32 32 32", /* COLOR_3DDKSHADOW */
66 "3DLight", "192 192 192", /* COLOR_3DLIGHT */
67 "InfoText", "0 0 0", /* COLOR_INFOTEXT */
68 "InfoBackground", "255 255 192", /* COLOR_INFOBK */
69 "AlternateButtonFace", "184 180 184", /* COLOR_ALTERNATEBTNFACE */
70 "HotTrackingColor", "0 0 255", /* COLOR_HOTLIGHT */
71 "GradientActiveTitle", "16 132 208", /* COLOR_GRADIENTACTIVECAPTION */
72 "GradientInactiveTitle", "181 181 181" /* COLOR_GRADIENTINACTIVECAPTION */
75 static const char * const DefSysColors95[] =
77 "Scrollbar", "192 192 192", /* COLOR_SCROLLBAR */
78 "Background", "0 128 128", /* COLOR_BACKGROUND */
79 "ActiveTitle", "0 0 128", /* COLOR_ACTIVECAPTION */
80 "InactiveTitle", "128 128 128", /* COLOR_INACTIVECAPTION */
81 "Menu", "192 192 192", /* COLOR_MENU */
82 "Window", "255 255 255", /* COLOR_WINDOW */
83 "WindowFrame", "0 0 0", /* COLOR_WINDOWFRAME */
84 "MenuText", "0 0 0", /* COLOR_MENUTEXT */
85 "WindowText", "0 0 0", /* COLOR_WINDOWTEXT */
86 "TitleText", "255 255 255", /* COLOR_CAPTIONTEXT */
87 "ActiveBorder", "192 192 192", /* COLOR_ACTIVEBORDER */
88 "InactiveBorder", "192 192 192", /* COLOR_INACTIVEBORDER */
89 "AppWorkspace", "128 128 128", /* COLOR_APPWORKSPACE */
90 "Hilight", "0 0 128", /* COLOR_HIGHLIGHT */
91 "HilightText", "255 255 255", /* COLOR_HIGHLIGHTTEXT */
92 "ButtonFace", "192 192 192", /* COLOR_BTNFACE */
93 "ButtonShadow", "128 128 128", /* COLOR_BTNSHADOW */
94 "GrayText", "128 128 128", /* COLOR_GRAYTEXT */
95 "ButtonText", "0 0 0", /* COLOR_BTNTEXT */
96 "InactiveTitleText", "192 192 192",/* COLOR_INACTIVECAPTIONTEXT */
97 "ButtonHilight", "255 255 255", /* COLOR_BTNHIGHLIGHT */
98 "3DDarkShadow", "0 0 0", /* COLOR_3DDKSHADOW */
99 "3DLight", "224 224 224", /* COLOR_3DLIGHT */
100 "InfoText", "0 0 0", /* COLOR_INFOTEXT */
101 "InfoBackground", "255 255 225", /* COLOR_INFOBK */
102 "AlternateButtonFace", "180 180 180", /* COLOR_ALTERNATEBTNFACE */
103 "HotTrackingColor", "0 0 255", /* COLOR_HOTLIGHT */
104 "GradientActiveTitle", "16 132 208", /* COLOR_GRADIENTACTIVECAPTION */
105 "GradientInactiveTitle", "181 181 181" /* COLOR_GRADIENTINACTIVECAPTION */
109 #define NUM_SYS_COLORS (COLOR_GRADIENTINACTIVECAPTION+1)
111 static COLORREF SysColors[NUM_SYS_COLORS];
112 static HBRUSH SysColorBrushes[NUM_SYS_COLORS];
113 static HPEN SysColorPens[NUM_SYS_COLORS];
116 /*************************************************************************
117 * SYSCOLOR_MakeObjectSystem
119 * OK, now for a very ugly hack.
120 * USER somehow has to tell GDI that its system brushes and pens are
121 * non-deletable.
122 * We don't want to export a function from GDI doing this for us,
123 * so we just do that ourselves by "wildly flipping some bits in memory".
124 * For a description of the GDI object magics and their flags,
125 * see "Undocumented Windows" (wrong about the OBJECT_NOSYSTEM flag, though).
127 static void SYSCOLOR_MakeObjectSystem( HGDIOBJ16 handle, BOOL set)
129 static WORD heap_sel = 0;
130 LPWORD ptr;
132 if (!heap_sel) heap_sel = LoadLibrary16( "gdi" );
133 if (heap_sel >= 32)
135 ptr = (LPWORD)LOCAL_Lock(heap_sel, handle);
137 /* touch the "system" bit of the wMagic field of a GDIOBJHDR */
138 if (set)
139 *(ptr+1) &= ~OBJECT_NOSYSTEM;
140 else
141 *(ptr+1) |= OBJECT_NOSYSTEM;
142 LOCAL_Unlock( heap_sel, handle );
146 /*************************************************************************
147 * SYSCOLOR_SetColor
149 static void SYSCOLOR_SetColor( int index, COLORREF color )
151 if (index < 0 || index >= NUM_SYS_COLORS) return;
152 SysColors[index] = color;
153 if (SysColorBrushes[index])
155 SYSCOLOR_MakeObjectSystem( HBRUSH_16(SysColorBrushes[index]), FALSE);
156 DeleteObject( SysColorBrushes[index] );
158 SysColorBrushes[index] = CreateSolidBrush( color );
159 SYSCOLOR_MakeObjectSystem( HBRUSH_16(SysColorBrushes[index]), TRUE);
161 if (SysColorPens[index])
163 SYSCOLOR_MakeObjectSystem( HPEN_16(SysColorPens[index]), FALSE);
164 DeleteObject( SysColorPens[index] );
166 SysColorPens[index] = CreatePen( PS_SOLID, 1, color );
167 SYSCOLOR_MakeObjectSystem( HPEN_16(SysColorPens[index]), TRUE);
171 /*************************************************************************
172 * SYSCOLOR_Init
174 void SYSCOLOR_Init(void)
176 int i, r, g, b;
177 const char * const *p;
178 char buffer[100];
179 BOOL bOk = FALSE, bNoReg = FALSE;
180 HKEY hKey;
182 p = (TWEAK_WineLook == WIN31_LOOK) ? DefSysColors : DefSysColors95;
184 /* first, try to read the values from the registry */
185 if (RegCreateKeyExA(HKEY_CURRENT_USER, "Control Panel\\Colors", 0, 0, 0, KEY_ALL_ACCESS, 0, &hKey, 0))
186 bNoReg = TRUE;
187 for (i = 0; i < NUM_SYS_COLORS; i++)
188 { bOk = FALSE;
190 /* first try, registry */
191 if (!bNoReg)
193 DWORD dwDataSize = sizeof(buffer);
194 if (!(RegQueryValueExA(hKey,(LPSTR)p[i*2], 0, 0, buffer, &dwDataSize)))
195 if (sscanf( buffer, "%d %d %d", &r, &g, &b ) == 3)
196 bOk = TRUE;
199 /* second try, win.ini */
200 if (!bOk)
201 { GetProfileStringA( "colors", p[i*2], p[i*2+1], buffer, 100 );
202 if (sscanf( buffer, " %d %d %d", &r, &g, &b ) == 3)
203 bOk = TRUE;
206 /* last chance, take the default */
207 if (!bOk)
208 { int iNumColors = sscanf( p[i*2+1], " %d %d %d", &r, &g, &b );
209 assert (iNumColors==3);
212 SYSCOLOR_SetColor( i, RGB(r,g,b) );
214 if (!bNoReg)
215 RegCloseKey(hKey);
219 /*************************************************************************
220 * GetSysColor (USER.180)
222 COLORREF WINAPI GetSysColor16( INT16 nIndex )
224 return GetSysColor (nIndex);
228 /*************************************************************************
229 * GetSysColor (USER32.@)
231 COLORREF WINAPI GetSysColor( INT nIndex )
233 if (nIndex >= 0 && nIndex < NUM_SYS_COLORS)
234 return SysColors[nIndex];
235 else
236 return 0;
240 /*************************************************************************
241 * SetSysColors (USER.181)
243 VOID WINAPI SetSysColors16( INT16 nChanges, const INT16 *lpSysColor,
244 const COLORREF *lpColorValues )
246 int i;
248 for (i = 0; i < nChanges; i++)
250 SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
253 /* Send WM_SYSCOLORCHANGE message to all windows */
255 SendMessageA( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0 );
257 /* Repaint affected portions of all visible windows */
259 RedrawWindow( GetDesktopWindow(), NULL, 0,
260 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
264 /*************************************************************************
265 * SetSysColors (USER32.@)
267 BOOL WINAPI SetSysColors( INT nChanges, const INT *lpSysColor,
268 const COLORREF *lpColorValues )
270 int i;
272 for (i = 0; i < nChanges; i++)
274 SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
277 /* Send WM_SYSCOLORCHANGE message to all windows */
279 SendMessageA( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0 );
281 /* Repaint affected portions of all visible windows */
283 RedrawWindow( GetDesktopWindow(), NULL, 0,
284 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
285 return TRUE;
288 /*************************************************************************
289 * SetSysColorsTemp (USER32.@)
291 * UNDOCUMENTED !!
293 * Called by W98SE desk.cpl Control Panel Applet:
294 * handle = SetSysColorsTemp(ptr, ptr, nCount); ("set" call)
295 * result = SetSysColorsTemp(NULL, NULL, handle); ("restore" call)
297 * pPens is an array of COLORREF values, which seems to be used
298 * to indicate the color values to create new pens with.
300 * pBrushes is an array of solid brush handles (returned by a previous
301 * CreateSolidBrush), which seems to contain the brush handles to set
302 * for the system colors.
304 * n seems to be used for
305 * a) indicating the number of entries to operate on (length of pPens,
306 * pBrushes)
307 * b) passing the handle that points to the previously used color settings.
308 * I couldn't figure out in hell what kind of handle this is on
309 * Windows. I just use a heap handle instead. Shouldn't matter anyway.
311 * RETURNS
312 * heap handle of our own copy of the current syscolors in case of
313 * "set" call, i.e. pPens, pBrushes != NULL.
314 * TRUE (unconditionally !) in case of "restore" call,
315 * i.e. pPens, pBrushes == NULL.
316 * FALSE in case of either pPens != NULL and pBrushes == NULL
317 * or pPens == NULL and pBrushes != NULL.
319 * I'm not sure whether this implementation is 100% correct. [AM]
321 DWORD WINAPI SetSysColorsTemp( const COLORREF *pPens, const HBRUSH *pBrushes, DWORD n)
323 int i;
325 if (pPens && pBrushes) /* "set" call */
327 /* allocate our structure to remember old colors */
328 LPVOID pOldCol = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD)+n*sizeof(HPEN)+n*sizeof(HBRUSH));
329 LPVOID p = pOldCol;
330 *(DWORD *)p = n; p = (char*)p + sizeof(DWORD);
331 memcpy(p, SysColorPens, n*sizeof(HPEN)); p = (char*)p + n*sizeof(HPEN);
332 memcpy(p, SysColorBrushes, n*sizeof(HBRUSH)); p = (char*)p + n*sizeof(HBRUSH);
334 for (i=0; i < n; i++)
336 SysColorPens[i] = CreatePen( PS_SOLID, 1, pPens[i] );
337 SysColorBrushes[i] = pBrushes[i];
340 return (DWORD)pOldCol;
342 if ((!pPens) && (!pBrushes)) /* "restore" call */
344 LPVOID pOldCol = (LPVOID)n;
345 LPVOID p = pOldCol;
346 DWORD nCount = *(DWORD *)p;
347 p = (char*)p + sizeof(DWORD);
349 for (i=0; i < nCount; i++)
351 DeleteObject(SysColorPens[i]);
352 SysColorPens[i] = *(HPEN *)p; p = (char*)p + sizeof(HPEN);
354 for (i=0; i < nCount; i++)
356 SysColorBrushes[i] = *(HBRUSH *)p; p = (char*)p + sizeof(HBRUSH);
358 /* get rid of storage structure */
359 HeapFree(GetProcessHeap(), 0, pOldCol);
361 return TRUE;
363 return FALSE;
366 /***********************************************************************
367 * GetSysColorBrush (USER32.@)
369 HBRUSH WINAPI GetSysColorBrush( INT index )
371 if (0 <= index && index < NUM_SYS_COLORS)
372 return SysColorBrushes[index];
373 WARN("Unknown index(%d)\n", index );
374 return GetStockObject(LTGRAY_BRUSH);
378 /***********************************************************************
379 * SYSCOLOR_GetPen
381 HPEN SYSCOLOR_GetPen( INT index )
383 /* We can assert here, because this function is internal to Wine */
384 assert (0 <= index && index < NUM_SYS_COLORS);
385 return SysColorPens[index];