summaryrefslogtreecommitdiff
path: root/gtk/gtkwin32.c
blob: c316824f5c780b14f2a2380050f0445832954f3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include "config.h"

#include "gdk/gdk.h"

#include "gtkprivate.h"

#define STRICT
#include <windows.h>
#include <commctrl.h>
#undef STRICT

/* In practice, resulting DLL will have manifest resource under index 2.
 * Fall back to that value if we can't find resource index programmatically.
 */
#define EMPIRIC_MANIFEST_RESOURCE_INDEX 2


static HMODULE   gtk_dll;
extern HINSTANCE _gdk_dll_hinstance;

BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
         DWORD     fdwReason,
         LPVOID    lpvReserved);

BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
         DWORD     fdwReason,
         LPVOID    lpvReserved)
{
  switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
      gtk_dll = (HMODULE) hinstDLL;
      _gdk_dll_hinstance = hinstDLL;
      break;
    default:
      break;
    }

  return TRUE;
}

static BOOL CALLBACK
find_first_manifest (HMODULE  module_handle,
                     LPCSTR   resource_type,
                     LPSTR    resource_name,
                     LONG_PTR user_data)
{
  LPSTR *result_name = (LPSTR *) user_data;

  if (resource_type == RT_MANIFEST)
    {
      if (IS_INTRESOURCE (resource_name))
        *result_name = resource_name;
      else
        *result_name = g_strdup (resource_name);
      return FALSE;
    }
  return TRUE;
}

/*
 * Grabs the first manifest it finds in libgtk3 (which is expected to be the
 * common-controls-6.0.0.0 manifest we embedded to enable visual styles),
 * uses it to create a process-default activation context, activates that
 * context, loads up the library passed in @dllname, then deactivates and
 * releases the context.
 *
 * In practice this is used to force system DLLs (like comdlg32) to be
 * loaded as if the application had the same manifest as libgtk3
 * (otherwise libgtk3 manifest only affests libgtk3 itself).
 * This way application does not need to have a manifest or to link
 * against comctl32.
 *
 * Note that loaded library handle leaks, so only use this function in
 * g_once_init_enter (leaking once is OK, Windows will clean up after us).
 */
void
_gtk_load_dll_with_libgtk3_manifest (const gchar *dll_name)
{
  HANDLE activation_ctx_handle;
  ACTCTXA activation_ctx_descriptor;
  ULONG_PTR activation_cookie;
  LPSTR resource_name;
  BOOL activated;
  DWORD error_code;

  resource_name = NULL;
  EnumResourceNames (gtk_dll, RT_MANIFEST, find_first_manifest,
                     (LONG_PTR) &resource_name);

  if (resource_name == NULL)
    resource_name = MAKEINTRESOURCEA (EMPIRIC_MANIFEST_RESOURCE_INDEX);

  memset (&activation_ctx_descriptor, 0, sizeof (activation_ctx_descriptor));
  activation_ctx_descriptor.cbSize = sizeof (activation_ctx_descriptor);
  activation_ctx_descriptor.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID |
                                      ACTCTX_FLAG_HMODULE_VALID |
                                      ACTCTX_FLAG_SET_PROCESS_DEFAULT;
  activation_ctx_descriptor.hModule = gtk_dll;
  activation_ctx_descriptor.lpResourceName = resource_name;
  activation_ctx_handle = CreateActCtx (&activation_ctx_descriptor);
  error_code = GetLastError ();

  if (activation_ctx_handle == INVALID_HANDLE_VALUE &&
      error_code != ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET)
    g_warning ("Failed to CreateActCtx for module %p, resource %p: %lu",
               gtk_dll, resource_name, GetLastError ());
  else if (error_code != ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET)
    {
      activation_cookie = 0;
      activated = ActivateActCtx (activation_ctx_handle, &activation_cookie);

      if (!activated)
        g_warning ("Failed to ActivateActCtx: %lu", GetLastError ());

      LoadLibraryA (dll_name);

      if (activated && !DeactivateActCtx (0, activation_cookie))
        g_warning ("Failed to DeactivateActCtx: %lu", GetLastError ());

      ReleaseActCtx (activation_ctx_handle);
    }

  if (!IS_INTRESOURCE (resource_name))
    g_free (resource_name);
}

const gchar *
_gtk_get_libdir (void)
{
  static char *gtk_libdir = NULL;
  if (gtk_libdir == NULL)
    {
      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      gchar *slash = strrchr (root, '\\');
      if (slash != NULL &&
          g_ascii_strcasecmp (slash + 1, ".libs") == 0)
        gtk_libdir = GTK_LIBDIR;
      else
        gtk_libdir = g_build_filename (root, "lib", NULL);
      g_free (root);
    }

  return gtk_libdir;
}

const gchar *
_gtk_get_localedir (void)
{
  static char *gtk_localedir = NULL;
  if (gtk_localedir == NULL)
    {
      const gchar *p;
      gchar *root, *temp;

      /* GTK_LOCALEDIR ends in either /lib/locale or
       * /share/locale. Scan for that slash.
       */
      p = GTK_LOCALEDIR + strlen (GTK_LOCALEDIR);
      while (*--p != '/')
        ;
      while (*--p != '/')
        ;

      root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      temp = g_build_filename (root, p, NULL);
      g_free (root);

      /* gtk_localedir is passed to bindtextdomain() which isn't
       * UTF-8-aware.
       */
      gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
      g_free (temp);
    }
  return gtk_localedir;
}

const gchar *
_gtk_get_datadir (void)
{
  static char *gtk_datadir = NULL;
  if (gtk_datadir == NULL)
    {
      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      gtk_datadir = g_build_filename (root, "share", NULL);
      g_free (root);
    }

  return gtk_datadir;
}

const gchar *
_gtk_get_sysconfdir (void)
{
  static char *gtk_sysconfdir = NULL;
  if (gtk_sysconfdir == NULL)
    {
      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      gtk_sysconfdir = g_build_filename (root, "etc", NULL);
      g_free (root);
    }

  return gtk_sysconfdir;
}

const gchar *
_gtk_get_data_prefix (void)
{
  static char *gtk_data_prefix = NULL;
  if (gtk_data_prefix == NULL)
    gtk_data_prefix = g_win32_get_package_installation_directory_of_module (gtk_dll);

  return gtk_data_prefix;
}