dwDataSize needs to be reset before each RegQueryValueEx() call.
[wine.git] / windows / syscolor.c
blobf3f389661e65096e1f8ef0e7af3ba7b499124f2d
1 /*
2 * Support for system colors
4 * Copyright David W. Metcalfe, 1993
5 * Copyright Alexandre Julliard, 1994
7 */
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "winbase.h"
13 #include "winuser.h"
14 #include "debug.h"
15 #include "tweak.h"
16 #include "winreg.h"
17 #include "winversion.h"
19 DEFAULT_DEBUG_CHANNEL(syscolor)
21 static const char * const DefSysColors[] =
23 "Scrollbar", "224 224 224", /* COLOR_SCROLLBAR */
24 "Background", "192 192 192", /* COLOR_BACKGROUND */
25 "ActiveTitle", "0 64 128", /* COLOR_ACTIVECAPTION */
26 "InactiveTitle", "255 255 255", /* COLOR_INACTIVECAPTION */
27 "Menu", "255 255 255", /* COLOR_MENU */
28 "Window", "255 255 255", /* COLOR_WINDOW */
29 "WindowFrame", "0 0 0", /* COLOR_WINDOWFRAME */
30 "MenuText", "0 0 0", /* COLOR_MENUTEXT */
31 "WindowText", "0 0 0", /* COLOR_WINDOWTEXT */
32 "TitleText", "255 255 255", /* COLOR_CAPTIONTEXT */
33 "ActiveBorder", "128 128 128", /* COLOR_ACTIVEBORDER */
34 "InactiveBorder", "255 255 255", /* COLOR_INACTIVEBORDER */
35 "AppWorkspace", "255 255 232", /* COLOR_APPWORKSPACE */
36 "Hilight", "224 224 224", /* COLOR_HIGHLIGHT */
37 "HilightText", "0 0 0", /* COLOR_HIGHLIGHTTEXT */
38 "ButtonFace", "192 192 192", /* COLOR_BTNFACE */
39 "ButtonShadow", "128 128 128", /* COLOR_BTNSHADOW */
40 "GrayText", "192 192 192", /* COLOR_GRAYTEXT */
41 "ButtonText", "0 0 0", /* COLOR_BTNTEXT */
42 "InactiveTitleText", "0 0 0", /* COLOR_INACTIVECAPTIONTEXT */
43 "ButtonHilight", "255 255 255", /* COLOR_BTNHIGHLIGHT */
44 "3DDarkShadow", "32 32 32", /* COLOR_3DDKSHADOW */
45 "3DLight", "192 192 192", /* COLOR_3DLIGHT */
46 "InfoText", "0 0 0", /* COLOR_INFOTEXT */
47 "InfoBackground", "255 255 192", /* COLOR_INFOBK */
48 "AlternateButtonFace", "184 180 184", /* COLOR_ALTERNATEBTNFACE */
49 "HotTrackingColor", "0 0 255", /* COLOR_HOTLIGHT */
50 "GradientActiveTitle", "16 132 208", /* COLOR_GRADIENTACTIVECAPTION */
51 "GradientInactiveTitle", "184 180 184" /* COLOR_GRADIENTINACTIVECAPTION */
54 static const char * const DefSysColors95[] =
56 "Scrollbar", "223 223 223", /* COLOR_SCROLLBAR */
57 "Background", "192 192 192", /* COLOR_BACKGROUND */
58 "ActiveTitle", "0 0 128", /* COLOR_ACTIVECAPTION */
59 "InactiveTitle", "128 128 128", /* COLOR_INACTIVECAPTION */
60 "Menu", "192 192 192", /* COLOR_MENU */
61 "Window", "255 255 255", /* COLOR_WINDOW */
62 "WindowFrame", "0 0 0", /* COLOR_WINDOWFRAME */
63 "MenuText", "0 0 0", /* COLOR_MENUTEXT */
64 "WindowText", "0 0 0", /* COLOR_WINDOWTEXT */
65 "TitleText", "255 255 255", /* COLOR_CAPTIONTEXT */
66 "ActiveBorder", "192 192 192", /* COLOR_ACTIVEBORDER */
67 "InactiveBorder", "192 192 192", /* COLOR_INACTIVEBORDER */
68 "AppWorkspace", "128 128 128", /* COLOR_APPWORKSPACE */
69 "Hilight", "0 0 128", /* COLOR_HIGHLIGHT */
70 "HilightText", "255 255 255", /* COLOR_HIGHLIGHTTEXT */
71 "ButtonFace", "192 192 192", /* COLOR_BTNFACE */
72 "ButtonShadow", "128 128 128", /* COLOR_BTNSHADOW */
73 "GrayText", "192 192 192", /* COLOR_GRAYTEXT */
74 "ButtonText", "0 0 0", /* COLOR_BTNTEXT */
75 "InactiveTitleText", "0 0 0", /* COLOR_INACTIVECAPTIONTEXT */
76 "ButtonHilight", "255 255 255", /* COLOR_BTNHIGHLIGHT */
77 "3DDarkShadow", "0 0 0", /* COLOR_3DDKSHADOW */
78 "3DLight", "223 223 223", /* COLOR_3DLIGHT */
79 "InfoText", "0 0 0", /* COLOR_INFOTEXT */
80 "InfoBackground", "255 255 192", /* COLOR_INFOBK */
81 "AlternateButtonFace", "184 180 184", /* COLOR_ALTERNATEBTNFACE */
82 "HotTrackingColor", "0 0 255", /* COLOR_HOTLIGHT */
83 "GradientActiveTitle", "16 132 208", /* COLOR_GRADIENTACTIVECAPTION */
84 "GradientInactiveTitle", "184 180 184" /* COLOR_GRADIENTINACTIVECAPTION */
88 #define NUM_SYS_COLORS (COLOR_GRADIENTINACTIVECAPTION+1)
90 static COLORREF SysColors[NUM_SYS_COLORS];
91 static HBRUSH SysColorBrushes[NUM_SYS_COLORS];
92 static HPEN SysColorPens[NUM_SYS_COLORS];
94 #define MAKE_SOLID(color) \
95 (PALETTEINDEX(GetNearestPaletteIndex(STOCK_DEFAULT_PALETTE,(color))))
97 /*************************************************************************
98 * SYSCOLOR_SetColor
100 static void SYSCOLOR_SetColor( int index, COLORREF color )
102 if (index < 0 || index >= NUM_SYS_COLORS) return;
103 SysColors[index] = color;
104 if (SysColorBrushes[index]) DeleteObject( SysColorBrushes[index] );
105 SysColorBrushes[index] = CreateSolidBrush( color );
106 if (SysColorPens[index]) DeleteObject( SysColorPens[index] );
107 SysColorPens[index] = CreatePen( PS_SOLID, 1, color );
111 /*************************************************************************
112 * SYSCOLOR_Init
114 void SYSCOLOR_Init(void)
116 int i, r, g, b;
117 const char * const *p;
118 char buffer[100];
119 BOOL bOk = FALSE, bNoReg = FALSE;
120 HKEY hKey;
122 p = (TWEAK_WineLook == WIN31_LOOK) ? DefSysColors : DefSysColors95;
124 /* first, try to read the values from the registry */
125 if (VERSION_GetVersion() != WIN31)
127 if (RegCreateKeyExA(HKEY_CURRENT_USER, "Control Panel\\Colors", 0, 0, 0, KEY_ALL_ACCESS, 0, &hKey, 0))
128 bNoReg = TRUE;
131 for (i = 0; i < NUM_SYS_COLORS; i++)
132 { bOk = FALSE;
134 /* first try, registry */
135 if (!bNoReg)
137 DWORD dwDataSize = sizeof(buffer);
138 if (!(RegQueryValueExA(hKey,(LPSTR)p[i*2], 0, 0, buffer, &dwDataSize)))
139 if (sscanf( buffer, "%d %d %d", &r, &g, &b ) == 3)
140 bOk = TRUE;
143 /* second try, win.ini */
144 if (!bOk)
145 { GetProfileStringA( "colors", p[i*2], p[i*2+1], buffer, 100 );
146 if (sscanf( buffer, " %d %d %d", &r, &g, &b ) == 3)
147 bOk = TRUE;
150 /* last chance, take the default */
151 if (!bOk)
152 { int iNumColors = sscanf( p[i*2+1], " %d %d %d", &r, &g, &b );
153 assert (iNumColors==3);
156 SYSCOLOR_SetColor( i, RGB(r,g,b) );
158 if (!bNoReg)
159 RegCloseKey(hKey);
164 /*************************************************************************
165 * GetSysColor16 (USER.180)
167 COLORREF WINAPI GetSysColor16( INT16 nIndex )
169 return GetSysColor (nIndex);
173 /*************************************************************************
174 * GetSysColor32 (USER32.289)
176 COLORREF WINAPI GetSysColor( INT nIndex )
178 if (nIndex >= 0 && nIndex < NUM_SYS_COLORS)
179 return SysColors[nIndex];
180 else
181 return 0;
185 /*************************************************************************
186 * SetSysColors16 (USER.181)
188 VOID WINAPI SetSysColors16( INT16 nChanges, const INT16 *lpSysColor,
189 const COLORREF *lpColorValues )
191 int i;
193 for (i = 0; i < nChanges; i++)
195 SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
198 /* Send WM_SYSCOLORCHANGE message to all windows */
200 SendMessageA( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0 );
202 /* Repaint affected portions of all visible windows */
204 RedrawWindow( GetDesktopWindow(), NULL, 0,
205 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
209 /*************************************************************************
210 * SetSysColors32 (USER32.505)
212 BOOL WINAPI SetSysColors( INT nChanges, const INT *lpSysColor,
213 const COLORREF *lpColorValues )
215 int i;
217 for (i = 0; i < nChanges; i++)
219 SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
222 /* Send WM_SYSCOLORCHANGE message to all windows */
224 SendMessageA( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0 );
226 /* Repaint affected portions of all visible windows */
228 RedrawWindow( GetDesktopWindow(), NULL, 0,
229 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
230 return TRUE;
234 /***********************************************************************
235 * GetSysColorBrush16 (USER.281)
237 HBRUSH16 WINAPI GetSysColorBrush16( INT16 index )
239 return (HBRUSH16)GetSysColorBrush(index);
243 /***********************************************************************
244 * GetSysColorBrush32 (USER32.290)
246 HBRUSH WINAPI GetSysColorBrush( INT index )
248 if (0 <= index && index < NUM_SYS_COLORS)
249 return SysColorBrushes[index];
250 WARN(syscolor, "Unknown index(%d)\n", index );
251 return GetStockObject(LTGRAY_BRUSH);
255 /***********************************************************************
256 * GetSysColorPen16 (Not a Windows API)
258 HPEN16 WINAPI GetSysColorPen16( INT16 index )
260 return (HPEN16)GetSysColorPen(index);
264 /***********************************************************************
265 * GetSysColorPen32 (Not a Windows API)
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.
271 HPEN WINAPI GetSysColorPen( INT index )
273 /* We can assert here, because this function is internal to Wine */
274 assert (0 <= index && index < NUM_SYS_COLORS);
275 return SysColorPens[index];