Added support for loading 16-bit dlls.
[wine] / windows / syscolor.c
1 /*
2  * Support for system colors
3  *
4  * Copyright  David W. Metcalfe, 1993
5  * Copyright  Alexandre Julliard, 1994
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
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"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(syscolor);
41
42 static const char * const DefSysColors[] =
43 {
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 */
73 };
74
75 static const char * const DefSysColors95[] =
76 {
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 */
106 };
107
108
109 #define NUM_SYS_COLORS     (COLOR_GRADIENTINACTIVECAPTION+1)
110
111 static COLORREF SysColors[NUM_SYS_COLORS];
112 static HBRUSH SysColorBrushes[NUM_SYS_COLORS];
113 static HPEN   SysColorPens[NUM_SYS_COLORS];
114
115
116 /*************************************************************************
117  * SYSCOLOR_MakeObjectSystem
118  *
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).
126  */
127 static void SYSCOLOR_MakeObjectSystem( HGDIOBJ16 handle, BOOL set)
128 {
129     static WORD heap_sel = 0;
130     LPWORD ptr;
131
132     if (!heap_sel) heap_sel = LoadLibrary16( "gdi" );
133     if (heap_sel >= 32)
134     {
135         ptr = (LPWORD)LOCAL_Lock(heap_sel, handle);
136
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 );
143     }
144 }
145
146 /*************************************************************************
147  *             SYSCOLOR_SetColor
148  */
149 static void SYSCOLOR_SetColor( int index, COLORREF color )
150 {
151     if (index < 0 || index >= NUM_SYS_COLORS) return;
152     SysColors[index] = color;
153     if (SysColorBrushes[index])
154     {
155         SYSCOLOR_MakeObjectSystem( HBRUSH_16(SysColorBrushes[index]), FALSE);
156         DeleteObject( SysColorBrushes[index] );
157     }
158     SysColorBrushes[index] = CreateSolidBrush( color );
159     SYSCOLOR_MakeObjectSystem( HBRUSH_16(SysColorBrushes[index]), TRUE);
160
161     if (SysColorPens[index])
162     {
163         SYSCOLOR_MakeObjectSystem( HPEN_16(SysColorPens[index]), FALSE);
164         DeleteObject( SysColorPens[index] );
165     }
166     SysColorPens[index] = CreatePen( PS_SOLID, 1, color );
167     SYSCOLOR_MakeObjectSystem( HPEN_16(SysColorPens[index]), TRUE);
168 }
169
170
171 /*************************************************************************
172  *             SYSCOLOR_Init
173  */
174 void SYSCOLOR_Init(void)
175 {
176     int i, r, g, b;
177     const char * const *p;
178     char buffer[100];
179     BOOL bOk = FALSE, bNoReg = FALSE;
180     HKEY  hKey;
181
182     p = (TWEAK_WineLook == WIN31_LOOK) ? DefSysColors : DefSysColors95;
183
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;
189
190       /* first try, registry */
191       if (!bNoReg)
192       {
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;
197       }
198
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;
204       }
205
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);
210       }
211
212       SYSCOLOR_SetColor( i, RGB(r,g,b) );
213     }
214     if (!bNoReg)
215       RegCloseKey(hKey);
216 }
217
218
219 /*************************************************************************
220  *              GetSysColor (USER.180)
221  */
222 COLORREF WINAPI GetSysColor16( INT16 nIndex )
223 {
224     return GetSysColor (nIndex);
225 }
226
227
228 /*************************************************************************
229  *              GetSysColor (USER32.@)
230  */
231 COLORREF WINAPI GetSysColor( INT nIndex )
232 {
233     if (nIndex >= 0 && nIndex < NUM_SYS_COLORS)
234         return SysColors[nIndex];
235     else
236         return 0;
237 }
238
239
240 /*************************************************************************
241  *              SetSysColors (USER.181)
242  */
243 VOID WINAPI SetSysColors16( INT16 nChanges, const INT16 *lpSysColor,
244                             const COLORREF *lpColorValues )
245 {
246     int i;
247
248     for (i = 0; i < nChanges; i++)
249     {
250         SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
251     }
252
253     /* Send WM_SYSCOLORCHANGE message to all windows */
254
255     SendMessageTimeoutW( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0,
256                          SMTO_ABORTIFHUNG, 2000, NULL );
257
258     /* Repaint affected portions of all visible windows */
259
260     RedrawWindow( GetDesktopWindow(), NULL, 0,
261                 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
262 }
263
264
265 /*************************************************************************
266  *              SetSysColors (USER32.@)
267  */
268 BOOL WINAPI SetSysColors( INT nChanges, const INT *lpSysColor,
269                               const COLORREF *lpColorValues )
270 {
271     int i;
272
273     for (i = 0; i < nChanges; i++)
274     {
275         SYSCOLOR_SetColor( lpSysColor[i], lpColorValues[i] );
276     }
277
278     /* Send WM_SYSCOLORCHANGE message to all windows */
279
280     SendMessageTimeoutW( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0,
281                          SMTO_ABORTIFHUNG, 2000, NULL );
282
283     /* Repaint affected portions of all visible windows */
284
285     RedrawWindow( GetDesktopWindow(), NULL, 0,
286                 RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
287     return TRUE;
288 }
289
290 /*************************************************************************
291  *              SetSysColorsTemp (USER32.@)
292  *
293  * UNDOCUMENTED !!
294  *
295  * Called by W98SE desk.cpl Control Panel Applet:
296  * handle = SetSysColorsTemp(ptr, ptr, nCount);     ("set" call)
297  * result = SetSysColorsTemp(NULL, NULL, handle);   ("restore" call)
298  *
299  * pPens is an array of COLORREF values, which seems to be used
300  * to indicate the color values to create new pens with.
301  *
302  * pBrushes is an array of solid brush handles (returned by a previous
303  * CreateSolidBrush), which seems to contain the brush handles to set
304  * for the system colors.
305  *
306  * n seems to be used for
307  *   a) indicating the number of entries to operate on (length of pPens,
308  *      pBrushes)
309  *   b) passing the handle that points to the previously used color settings.
310  *      I couldn't figure out in hell what kind of handle this is on
311  *      Windows. I just use a heap handle instead. Shouldn't matter anyway.
312  *
313  * RETURNS
314  *     heap handle of our own copy of the current syscolors in case of
315  *                 "set" call, i.e. pPens, pBrushes != NULL.
316  *     TRUE (unconditionally !) in case of "restore" call,
317  *          i.e. pPens, pBrushes == NULL.
318  *     FALSE in case of either pPens != NULL and pBrushes == NULL
319  *          or pPens == NULL and pBrushes != NULL.
320  *
321  * I'm not sure whether this implementation is 100% correct. [AM]
322  */
323 DWORD WINAPI SetSysColorsTemp( const COLORREF *pPens, const HBRUSH *pBrushes, DWORD n)
324 {
325         int i;
326
327         if (pPens && pBrushes) /* "set" call */
328         {
329             /* allocate our structure to remember old colors */
330             LPVOID pOldCol = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD)+n*sizeof(HPEN)+n*sizeof(HBRUSH));
331             LPVOID p = pOldCol;
332            *(DWORD *)p = n; p = (char*)p + sizeof(DWORD);
333            memcpy(p, SysColorPens, n*sizeof(HPEN)); p = (char*)p + n*sizeof(HPEN);
334            memcpy(p, SysColorBrushes, n*sizeof(HBRUSH)); p = (char*)p + n*sizeof(HBRUSH);
335
336             for (i=0; i < n; i++)
337             {
338                 SysColorPens[i] = CreatePen( PS_SOLID, 1, pPens[i] );
339                 SysColorBrushes[i] = pBrushes[i];
340             }
341
342             return (DWORD)pOldCol;
343         }
344         if ((!pPens) && (!pBrushes)) /* "restore" call */
345         {
346             LPVOID pOldCol = (LPVOID)n;
347             LPVOID p = pOldCol;
348             DWORD nCount = *(DWORD *)p;
349            p = (char*)p + sizeof(DWORD);
350
351             for (i=0; i < nCount; i++)
352             {
353                 DeleteObject(SysColorPens[i]);
354                SysColorPens[i] = *(HPEN *)p; p = (char*)p + sizeof(HPEN);
355             }
356             for (i=0; i < nCount; i++)
357             {
358                SysColorBrushes[i] = *(HBRUSH *)p; p = (char*)p + sizeof(HBRUSH);
359             }
360             /* get rid of storage structure */
361             HeapFree(GetProcessHeap(), 0, pOldCol);
362
363             return TRUE;
364         }
365         return FALSE;
366 }
367
368 /***********************************************************************
369  *              GetSysColorBrush (USER32.@)
370  */
371 HBRUSH WINAPI GetSysColorBrush( INT index )
372 {
373     if (0 <= index && index < NUM_SYS_COLORS)
374         return SysColorBrushes[index];
375     WARN("Unknown index(%d)\n", index );
376     return GetStockObject(LTGRAY_BRUSH);
377 }
378
379
380 /***********************************************************************
381  *              SYSCOLOR_GetPen
382  */
383 HPEN SYSCOLOR_GetPen( INT index )
384 {
385     /* We can assert here, because this function is internal to Wine */
386     assert (0 <= index && index < NUM_SYS_COLORS);
387     return SysColorPens[index];
388
389 }