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