Code cleanup. Added ChangeLog entry.
[emacs.git] / src / w32reg.c
blob77667eb7dc9eabbfb91c1bbd9aff351b729f9d53
1 /* Emulate the X Resource Manager through the registry.
2 Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Written by Kevin Gallo */
22 #include <config.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "w32term.h"
26 #include "blockinput.h"
28 #include <stdio.h>
29 #include <string.h>
31 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
33 /* Default system colors from the Display Control Panel settings. */
34 #define SYSTEM_DEFAULT_RESOURCES \
35 "emacs.foreground:SystemWindowText\0" \
36 "emacs.background:SystemWindow\0" \
37 "emacs.tooltip.attributeForeground:SystemInfoText\0" \
38 "emacs.tooltip.attributeBackground:SystemInfoWindow\0" \
39 "emacs.tool-bar.attributeForeground:SystemButtonText\0" \
40 "emacs.tool-bar.attributeBackground:SystemButtonFace\0" \
41 "emacs.menu.attributeForeground:SystemMenuText\0" \
42 "emacs.menu.attributeBackground:SystemMenu\0" \
43 "emacs.scroll-bar.attributeForeground:SystemScrollbar\0"
45 /* Other possibilities for default faces:
47 region: Could use SystemHilight, but interferes with our ability to
48 see most syntax highlighting through the region face.
50 modeline: Could use System(In)ActiveTitle, gradient versions (not
51 supported on 95 and NT), but modeline is more like a status bar
52 really (which don't appear to be configurable in Windows).
54 highlight: Could use SystemHotTrackingColor, but it is not supported
55 on Windows 95 or NT, and other apps only seem to use it for menus
56 anyway.
60 static char *
61 w32_get_rdb_resource (char *rdb, char *resource)
63 char *value = rdb;
64 int len = strlen (resource);
66 while (*value)
68 /* Comparison is case-insensitive because registry searches are too. */
69 if ((strnicmp (value, resource, len) == 0) && (value[len] == ':'))
70 return xstrdup (&value[len + 1]);
72 value = strchr (value, '\0') + 1;
75 return NULL;
78 static LPBYTE
79 w32_get_string_resource (char *name, char *class, DWORD dwexptype)
81 LPBYTE lpvalue = NULL;
82 HKEY hrootkey = NULL;
83 DWORD dwType;
84 DWORD cbData;
85 BOOL ok = FALSE;
86 HKEY hive = HKEY_CURRENT_USER;
88 trykey:
90 BLOCK_INPUT;
92 /* Check both the current user and the local machine to see if we have
93 any resources */
95 if (RegOpenKeyEx (hive, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
97 char *keyname;
99 if (RegQueryValueEx (hrootkey, name, NULL, &dwType, NULL, &cbData) == ERROR_SUCCESS
100 && dwType == dwexptype)
102 keyname = name;
104 else if (RegQueryValueEx (hrootkey, class, NULL, &dwType, NULL, &cbData) == ERROR_SUCCESS
105 && dwType == dwexptype)
107 keyname = class;
109 else
111 keyname = NULL;
114 ok = (keyname
115 && (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL
116 && RegQueryValueEx (hrootkey, keyname, NULL, NULL, lpvalue, &cbData) == ERROR_SUCCESS);
118 RegCloseKey (hrootkey);
121 UNBLOCK_INPUT;
123 if (!ok)
125 if (lpvalue)
127 xfree (lpvalue);
128 lpvalue = NULL;
130 if (hive == HKEY_CURRENT_USER)
132 hive = HKEY_LOCAL_MACHINE;
133 goto trykey;
136 /* Check if there are Windows specific defaults defined. */
137 return w32_get_rdb_resource (SYSTEM_DEFAULT_RESOURCES, name);
139 return (lpvalue);
142 /* Retrieve the string resource specified by NAME with CLASS from
143 database RDB. */
145 char *
146 x_get_string_resource (XrmDatabase rdb, char *name, char *class)
148 if (rdb)
150 char *resource;
152 if (resource = w32_get_rdb_resource (rdb, name))
153 return resource;
154 if (resource = w32_get_rdb_resource (rdb, class))
155 return resource;
158 if (inhibit_x_resources)
159 /* --quick was passed, so this is a no-op. */
160 return NULL;
162 return (w32_get_string_resource (name, class, REG_SZ));
165 /* arch-tag: 755fce25-42d7-4acb-874f-2fb42336823d
166 (do not change this comment) */