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