Debian Codegraph @

gtk/gtkwin32.c

raw symbols SCIP
1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #include "config.h"
26
27 #include "gdk/gdk.h"
28
29 #include "gtkprivate.h"
30
31 #include <windows.h>
32 #include <commctrl.h>
33
34 extern IMAGE_DOS_HEADER __ImageBase;
35
36 static inline HMODULE
37 this_module (void)
38 {
39 return (HMODULE) &__ImageBase;
40 }
41
42 /* In practice, resulting DLL will have manifest resource under index 2.
43 * Fall back to that value if we can't find resource index programmatically.
44 */
45 #define EMPIRIC_MANIFEST_RESOURCE_INDEX 2
46
47 static wchar_t *
48 g_wcsdup (const wchar_t *wcs)
49 {
50 wchar_t *new_wcs = NULL;
51 gsize length;
52
53 if G_LIKELY (wcs)
54 {
55 length = wcslen (wcs) + 1;
56 new_wcs = g_new (wchar_t, length);
57 wcscpy (new_wcs, wcs);
58 new_wcs[length - 1] = L'\0';
59 }
60
61 return new_wcs;
62 }
63
64 static BOOL CALLBACK
65 find_first_manifest (HMODULE module_handle,
66 LPCWSTR resource_type,
67 LPWSTR resource_name,
68 LONG_PTR user_data)
69 {
70 LPWSTR *result_name = (LPWSTR *) user_data;
71
72 if (resource_type == RT_MANIFEST)
73 {
74 if (IS_INTRESOURCE (resource_name))
75 *result_name = resource_name;
76 else
77 *result_name = g_wcsdup (resource_name);
78 return FALSE;
79 }
80 return TRUE;
81 }
82
83 /*
84 * Grabs the first manifest it finds in libgtk (which is expected to be the
85 * common-controls-6.0.0.0 manifest we embedded to enable visual styles),
86 * uses it to create a process-default activation context, activates that
87 * context, loads up the library passed in @dllname, then deactivates and
88 * releases the context.
89 *
90 * In practice this is used to force system DLLs (like comdlg32) to be
91 * loaded as if the application had the same manifest as libgtk
92 * (otherwise libgtk3 manifest only affests libgtk itself).
93 * This way application does not need to have a manifest or to link
94 * against comctl32.
95 *
96 * Note that loaded library handle leaks, so only use this function in
97 * g_once_init_enter (leaking once is OK, Windows will clean up after us).
98 */
99 void
100 _gtk_load_dll_with_libgtk3_manifest (const wchar_t *dll_name)
101 {
102 HANDLE activation_ctx_handle;
103 ACTCTX activation_ctx_descriptor;
104 ULONG_PTR activation_cookie;
105 LPWSTR resource_name;
106 BOOL activated;
107 DWORD error_code;
108
109 resource_name = NULL;
110 EnumResourceNames (this_module (), RT_MANIFEST, find_first_manifest,
111 (LONG_PTR) &resource_name);
112
113 if (resource_name == NULL)
114 resource_name = MAKEINTRESOURCE (EMPIRIC_MANIFEST_RESOURCE_INDEX);
115
116 memset (&activation_ctx_descriptor, 0, sizeof (activation_ctx_descriptor));
117 activation_ctx_descriptor.cbSize = sizeof (activation_ctx_descriptor);
118 activation_ctx_descriptor.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID |
119 ACTCTX_FLAG_HMODULE_VALID |
120 ACTCTX_FLAG_SET_PROCESS_DEFAULT;
121 activation_ctx_descriptor.hModule = this_module ();
122 activation_ctx_descriptor.lpResourceName = resource_name;
123 activation_ctx_handle = CreateActCtx (&activation_ctx_descriptor);
124 error_code = GetLastError ();
125
126 if (activation_ctx_handle == INVALID_HANDLE_VALUE &&
127 error_code != ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET)
128 g_warning ("Failed to CreateActCtx for module %p, resource %p: %lu",
129 this_module (), resource_name, GetLastError ());
130 else if (error_code != ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET)
131 {
132 activation_cookie = 0;
133 activated = ActivateActCtx (activation_ctx_handle, &activation_cookie);
134
135 if (!activated)
136 g_warning ("Failed to ActivateActCtx: %lu", GetLastError ());
137
138 LoadLibrary (dll_name);
139
140 if (activated && !DeactivateActCtx (0, activation_cookie))
141 g_warning ("Failed to DeactivateActCtx: %lu", GetLastError ());
142
143 ReleaseActCtx (activation_ctx_handle);
144 }
145
146 if (!IS_INTRESOURCE (resource_name))
147 g_free (resource_name);
148 }
149
150 const char *
151 _gtk_get_libdir (void)
152 {
153 static char *gtk_libdir = NULL;
154 if (gtk_libdir == NULL)
155 {
156 char *root = g_win32_get_package_installation_directory_of_module (this_module ());
157 char *slash = strrchr (root, '\\');
158 if (slash != NULL &&
159 g_ascii_strcasecmp (slash + 1, ".libs") == 0)
160 gtk_libdir = g_strdup (GTK_LIBDIR);
161 else
162 gtk_libdir = g_build_filename (root, "lib", NULL);
163 g_free (root);
164 }
165
166 return gtk_libdir;
167 }
168
169 const char *
170 _gtk_get_localedir (void)
171 {
172 static char *gtk_localedir = NULL;
173 if (gtk_localedir == NULL)
174 {
175 const char *p;
176 char *root, *temp;
177
178 /* GTK_LOCALEDIR ends in either /lib/locale or
179 * /share/locale. Scan for that slash.
180 */
181 p = GTK_LOCALEDIR;
182 p += strlen (GTK_LOCALEDIR);
183 while (*--p != '/')
184 ;
185 while (*--p != '/')
186 ;
187
188 root = g_win32_get_package_installation_directory_of_module (this_module ());
189 temp = g_build_filename (root, p, NULL);
190 g_free (root);
191
192 /* gtk_localedir is passed to bindtextdomain() which isn't
193 * UTF-8-aware.
194 */
195 gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
196 g_free (temp);
197 }
198 return gtk_localedir;
199 }
200
201 const char *
202 _gtk_get_datadir (void)
203 {
204 static char *gtk_datadir = NULL;
205 if (gtk_datadir == NULL)
206 {
207 char *root = g_win32_get_package_installation_directory_of_module (this_module ());
208 gtk_datadir = g_build_filename (root, "share", NULL);
209 g_free (root);
210 }
211
212 return gtk_datadir;
213 }
214
215 const char *
216 _gtk_get_sysconfdir (void)
217 {
218 static char *gtk_sysconfdir = NULL;
219 if (gtk_sysconfdir == NULL)
220 {
221 char *root = g_win32_get_package_installation_directory_of_module (this_module ());
222 gtk_sysconfdir = g_build_filename (root, "etc", NULL);
223 g_free (root);
224 }
225
226 return gtk_sysconfdir;
227 }
228
229 const char *
230 _gtk_get_data_prefix (void)
231 {
232 static char *gtk_data_prefix = NULL;
233 if (gtk_data_prefix == NULL)
234 gtk_data_prefix = g_win32_get_package_installation_directory_of_module (this_module ());
235
236 return gtk_data_prefix;
237 }