Merge from trunk
[bpt/emacs.git] / src / w32reg.c
1 /* Emulate the X Resource Manager through the registry.
2 Copyright (C) 1990, 1993-1994, 2001-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Written by Kevin Gallo */
20
21 #include <config.h>
22 #include "lisp.h"
23 #include "w32term.h"
24 #include "blockinput.h"
25
26 #include <stdio.h>
27
28 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
29
30 /* Default system colors from the Display Control Panel settings. */
31 #define SYSTEM_DEFAULT_RESOURCES \
32 "emacs.foreground:SystemWindowText\0" \
33 "emacs.background:SystemWindow\0" \
34 "emacs.tooltip.attributeForeground:SystemInfoText\0" \
35 "emacs.tooltip.attributeBackground:SystemInfoWindow\0" \
36 "emacs.tool-bar.attributeForeground:SystemButtonText\0" \
37 "emacs.tool-bar.attributeBackground:SystemButtonFace\0" \
38 "emacs.menu.attributeForeground:SystemMenuText\0" \
39 "emacs.menu.attributeBackground:SystemMenu\0" \
40 "emacs.scroll-bar.attributeForeground:SystemScrollbar\0"
41
42 /* Other possibilities for default faces:
43
44 region: Could use SystemHilight, but interferes with our ability to
45 see most syntax highlighting through the region face.
46
47 modeline: Could use System(In)ActiveTitle, gradient versions (not
48 supported on 95 and NT), but modeline is more like a status bar
49 really (which don't appear to be configurable in Windows).
50
51 highlight: Could use SystemHotTrackingColor, but it is not supported
52 on Windows 95 or NT, and other apps only seem to use it for menus
53 anyway.
54
55 */
56
57 static char *
58 w32_get_rdb_resource (char *rdb, char *resource)
59 {
60 char *value = rdb;
61 int len = strlen (resource);
62
63 while (*value)
64 {
65 /* Comparison is case-insensitive because registry searches are too. */
66 if ((strnicmp (value, resource, len) == 0) && (value[len] == ':'))
67 return xstrdup (&value[len + 1]);
68
69 value = strchr (value, '\0') + 1;
70 }
71
72 return NULL;
73 }
74
75 static LPBYTE
76 w32_get_string_resource (char *name, char *class, DWORD dwexptype)
77 {
78 LPBYTE lpvalue = NULL;
79 HKEY hrootkey = NULL;
80 DWORD dwType;
81 DWORD cbData;
82 BOOL ok = FALSE;
83 HKEY hive = HKEY_CURRENT_USER;
84
85 trykey:
86
87 block_input ();
88
89 /* Check both the current user and the local machine to see if we have
90 any resources */
91
92 if (RegOpenKeyEx (hive, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
93 {
94 char *keyname;
95
96 if (RegQueryValueEx (hrootkey, name, NULL, &dwType, NULL, &cbData) == ERROR_SUCCESS
97 && dwType == dwexptype)
98 {
99 keyname = name;
100 }
101 else if (RegQueryValueEx (hrootkey, class, NULL, &dwType, NULL, &cbData) == ERROR_SUCCESS
102 && dwType == dwexptype)
103 {
104 keyname = class;
105 }
106 else
107 {
108 keyname = NULL;
109 }
110
111 ok = (keyname
112 && (lpvalue = xmalloc (cbData)) != NULL
113 && RegQueryValueEx (hrootkey, keyname, NULL, NULL, lpvalue, &cbData) == ERROR_SUCCESS);
114
115 RegCloseKey (hrootkey);
116 }
117
118 unblock_input ();
119
120 if (!ok)
121 {
122 if (lpvalue)
123 {
124 xfree (lpvalue);
125 lpvalue = NULL;
126 }
127 if (hive == HKEY_CURRENT_USER)
128 {
129 hive = HKEY_LOCAL_MACHINE;
130 goto trykey;
131 }
132
133 /* Check if there are Windows specific defaults defined. */
134 return w32_get_rdb_resource (SYSTEM_DEFAULT_RESOURCES, name);
135 }
136 return (lpvalue);
137 }
138
139 /* Retrieve the string resource specified by NAME with CLASS from
140 database RDB. */
141
142 char *
143 x_get_string_resource (XrmDatabase rdb, char *name, char *class)
144 {
145 if (rdb)
146 {
147 char *resource;
148
149 if ((resource = w32_get_rdb_resource (rdb, name)))
150 return resource;
151 if ((resource = w32_get_rdb_resource (rdb, class)))
152 return resource;
153 }
154
155 if (inhibit_x_resources)
156 /* --quick was passed, so this is a no-op. */
157 return NULL;
158
159 return w32_get_string_resource (name, class, REG_SZ);
160 }