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