gimpui.c 11.6 KB
Newer Older
1 2 3
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
4
 * This library is free software: you can redistribute it and/or
5 6
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
7
 * version 3 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 11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 13 14
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library.  If not, see
16
 * <https://www.gnu.org/licenses/>.
17 18
 */

Sven Neumann's avatar
Sven Neumann committed
19 20
#include "config.h"

21 22
#include <gtk/gtk.h>

23 24 25 26 27 28 29 30
#ifdef GDK_WINDOWING_WIN32
#include <gdk/gdkwin32.h>
#endif

#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif

31 32 33 34
#ifdef GDK_WINDOWING_QUARTZ
#include <Cocoa/Cocoa.h>
#endif

35 36 37
#include "gimp.h"
#include "gimpui.h"

38 39
#include "libgimpmodule/gimpmodule.h"

40
#include "libgimpwidgets/gimpwidgets.h"
41
#include "libgimpwidgets/gimpwidgets-private.h"
42

43

44 45 46 47 48
/**
 * SECTION: gimpui
 * @title: gimpui
 * @short_description: Common user interface functions. This header includes
 *                     all other GIMP User Interface Library headers.
49
 * @see_also: gtk_init(), gdk_set_use_xshm(), gtk_widget_set_default_visual().
50 51 52 53 54 55
 *
 * Common user interface functions. This header includes all other
 * GIMP User Interface Library headers.
 **/


56 57
/*  local function prototypes  */

58 59
static void      gimp_ui_help_func              (const gchar   *help_id,
                                                 gpointer       help_data);
60
static void      gimp_ensure_modules            (void);
61 62 63 64 65
static void      gimp_window_transient_realized (GtkWidget     *window,
                                                 GdkWindow     *parent);
static gboolean  gimp_window_set_transient_for  (GtkWindow     *window,
                                                 GdkWindow     *parent);

66 67 68 69
#ifdef GDK_WINDOWING_QUARTZ
static void      gimp_osx_focus_window          (void);
#endif

Sven Neumann's avatar
Sven Neumann committed
70 71

static gboolean gimp_ui_initialized = FALSE;
72 73 74 75


/*  public functions  */

76 77 78 79
/**
 * gimp_ui_init:
 * @prog_name: The name of the plug-in which will be passed as argv[0] to
 *             gtk_init(). It's a convention to use the name of the
80
 *             executable and _not_ the PDB procedure name.
81 82
 * @preview:   This parameter is unused and exists for historical
 *             reasons only.
83
 *
84 85
 * This function initializes GTK+ with gtk_init() and initializes GDK's
 * image rendering subsystem (GdkRGB) to follow the GIMP main program's
86
 * colormap allocation/installation policy.
87
 *
88 89 90 91 92
 * It also sets up various other things so that the plug-in user looks
 * and behaves like the GIMP core. This includes selecting the GTK+
 * theme and setting up the help system as chosen in the GIMP
 * preferences. Any plug-in that provides a user interface should call
 * this function.
93
 **/
94
void
95
gimp_ui_init (const gchar *prog_name,
Sven Neumann's avatar
Sven Neumann committed
96
              gboolean     preview)
97
{
98 99 100 101 102 103
  const gchar    *display_name;
  GtkCssProvider *css_provider;
  gchar          *theme_css;
  GFileMonitor   *css_monitor;
  GFile          *file;
  GError         *error = NULL;
104

105 106
  g_return_if_fail (prog_name != NULL);

Sven Neumann's avatar
Sven Neumann committed
107
  if (gimp_ui_initialized)
108 109
    return;

110 111
  g_set_prgname (prog_name);

112 113 114 115 116
  display_name = gimp_display_name ();

  if (display_name)
    {
#if defined (GDK_WINDOWING_X11)
117
      g_setenv ("DISPLAY", display_name, TRUE);
118
#else
119
      g_setenv ("GDK_DISPLAY", display_name, TRUE);
120 121 122
#endif
    }

123 124 125 126 127 128 129 130 131 132 133
  if (gimp_user_time ())
    {
      /* Construct a fake startup ID as we only want to pass the
       * interaction timestamp, see _gdk_windowing_set_default_display().
       */
      gchar *startup_id = g_strdup_printf ("_TIME%u", gimp_user_time ());

      g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
      g_free (startup_id);
    }

134
  gtk_init (NULL, NULL);
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  css_provider = gtk_css_provider_new ();

  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                             GTK_STYLE_PROVIDER (css_provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  g_object_unref (css_provider);

  theme_css = gimp_personal_rc_file ("theme.css");

  if (! gtk_css_provider_load_from_path (css_provider,
                                         theme_css, &error))
    {
      g_printerr ("%s: error parsing %s: %s\n", G_STRFUNC,
                  gimp_filename_to_utf8 (theme_css), error->message);
      g_clear_error (&error);
    }
153

154 155
  file = g_file_new_for_path (theme_css);
  g_free (theme_css);
156

157
  css_monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, NULL);
158 159
  g_object_unref (file);

160 161 162 163
#if 0
  /* FIXME CSS: do we still need such code on gtk3? */
  g_signal_connect (css_monitor, "changed",
                    G_CALLBACK (gtk_rc_reparse_all),
164
                    NULL);
165
#endif
166

167 168
  gdk_set_program_class (gimp_wm_class ());

169 170 171 172 173 174
  if (gimp_icon_theme_dir ())
    {
      file = g_file_new_for_path (gimp_icon_theme_dir ());
      gimp_icons_set_icon_theme (file);
      g_object_unref (file);
    }
175

176
  gimp_widgets_init (gimp_ui_help_func,
177 178
                     gimp_context_get_foreground,
                     gimp_context_get_background,
179
                     gimp_ensure_modules);
180

181 182
  gimp_dialogs_show_help_button (gimp_show_help_button ());

183
#ifdef GDK_WINDOWING_QUARTZ
184
  g_idle_add ((GSourceFunc) gimp_osx_focus_window, NULL);
185 186
#endif

Sven Neumann's avatar
Sven Neumann committed
187 188 189
  gimp_ui_initialized = TRUE;
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static GdkWindow *
gimp_ui_get_foreign_window (guint32 window)
{
#ifdef GDK_WINDOWING_X11
  return gdk_x11_window_foreign_new_for_display (gdk_display_get_default (),
                                                 window);
#endif

#ifdef GDK_WINDOWING_WIN32
  return gdk_win32_window_foreign_new_for_display (gdk_display_get_default (),
                                                   window);
#endif

  return NULL;
}

Sven Neumann's avatar
Sven Neumann committed
206 207
/**
 * gimp_ui_get_display_window:
208
 * @gdisp_ID: a #GimpDisplay ID.
Sven Neumann's avatar
Sven Neumann committed
209 210 211 212 213 214 215 216 217 218 219 220
 *
 * Returns the #GdkWindow of a display window. The purpose is to allow
 * to make plug-in dialogs transient to the image display as explained
 * with gdk_window_set_transient_for().
 *
 * You shouldn't have to call this function directly. Use
 * gimp_window_set_transient_for_display() instead.
 *
 * Return value: A reference to a #GdkWindow or %NULL. You should
 *               unref the window using g_object_unref() as soon as
 *               you don't need it any longer.
 *
221
 * Since: 2.4
Sven Neumann's avatar
Sven Neumann committed
222 223 224 225
 */
GdkWindow *
gimp_ui_get_display_window (guint32 gdisp_ID)
{
226
  guint32 window;
Sven Neumann's avatar
Sven Neumann committed
227 228 229 230 231

  g_return_val_if_fail (gimp_ui_initialized, NULL);

  window = gimp_display_get_window_handle (gdisp_ID);
  if (window)
232
    return gimp_ui_get_foreign_window (window);
Sven Neumann's avatar
Sven Neumann committed
233 234 235 236

  return NULL;
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250
/**
 * gimp_ui_get_progress_window:
 *
 * Returns the #GdkWindow of the window this plug-in's progress bar is
 * shown in. Use it to make plug-in dialogs transient to this window
 * as explained with gdk_window_set_transient_for().
 *
 * You shouldn't have to call this function directly. Use
 * gimp_window_set_transient() instead.
 *
 * Return value: A reference to a #GdkWindow or %NULL. You should
 *               unref the window using g_object_unref() as soon as
 *               you don't need it any longer.
 *
251
 * Since: 2.4
252 253 254 255
 */
GdkWindow *
gimp_ui_get_progress_window (void)
{
256
  guint32  window;
257 258 259 260 261

  g_return_val_if_fail (gimp_ui_initialized, NULL);

  window = gimp_progress_get_window_handle ();
  if (window)
262
     return gimp_ui_get_foreign_window (window);
263 264 265 266

  return NULL;
}

267 268 269 270 271 272 273 274 275 276 277
#ifdef GDK_WINDOWING_QUARTZ
static void
gimp_window_transient_show (GtkWidget *window)
{
  g_signal_handlers_disconnect_by_func (window,
                                        gimp_window_transient_show,
                                        NULL);
  [NSApp arrangeInFront: nil];
}
#endif

Sven Neumann's avatar
Sven Neumann committed
278 279 280 281 282 283 284 285 286 287
/**
 * gimp_window_set_transient_for_display:
 * @window:   the #GtkWindow that should become transient
 * @gdisp_ID: display ID of the image window that should become the parent
 *
 * Indicates to the window manager that @window is a transient dialog
 * associated with the GIMP image window that is identified by it's
 * display ID.  See gdk_window_set_transient_for () for more information.
 *
 * Most of the time you will want to use the convenience function
288
 * gimp_window_set_transient().
Sven Neumann's avatar
Sven Neumann committed
289
 *
290
 * Since: 2.4
Sven Neumann's avatar
Sven Neumann committed
291 292 293 294 295 296 297 298
 */
void
gimp_window_set_transient_for_display (GtkWindow *window,
                                       guint32    gdisp_ID)
{
  g_return_if_fail (gimp_ui_initialized);
  g_return_if_fail (GTK_IS_WINDOW (window));

299 300 301 302 303 304 305 306
  if (! gimp_window_set_transient_for (window,
                                       gimp_ui_get_display_window (gdisp_ID)))
    {
      /*  if setting the window transient failed, at least set
       *  WIN_POS_CENTER, which will center the window on the screen
       *  where the mouse is (see bug #684003).
       */
      gtk_window_set_position (window, GTK_WIN_POS_CENTER);
307 308

#ifdef GDK_WINDOWING_QUARTZ
309 310 311
      g_signal_connect (window, "show",
                        G_CALLBACK (gimp_window_transient_show),
                        NULL);
312
#endif
313
    }
Sven Neumann's avatar
Sven Neumann committed
314 315 316
}

/**
317
 * gimp_window_set_transient:
Sven Neumann's avatar
Sven Neumann committed
318 319 320
 * @window: the #GtkWindow that should become transient
 *
 * Indicates to the window manager that @window is a transient dialog
321
 * associated with the GIMP window that the plug-in has been
Sven Neumann's avatar
Sven Neumann committed
322 323
 * started from. See also gimp_window_set_transient_for_display().
 *
324
 * Since: 2.4
Sven Neumann's avatar
Sven Neumann committed
325 326
 */
void
327
gimp_window_set_transient (GtkWindow *window)
Sven Neumann's avatar
Sven Neumann committed
328 329 330 331
{
  g_return_if_fail (gimp_ui_initialized);
  g_return_if_fail (GTK_IS_WINDOW (window));

332 333 334 335
  if (! gimp_window_set_transient_for (window, gimp_ui_get_progress_window ()))
    {
      /*  see above  */
      gtk_window_set_position (window, GTK_WIN_POS_CENTER);
336 337

#ifdef GDK_WINDOWING_QUARTZ
338 339 340
      g_signal_connect (window, "show",
                        G_CALLBACK (gimp_window_transient_show),
                        NULL);
341
#endif
342
    }
343
}
344 345 346 347 348


/*  private functions  */

static void
349 350
gimp_ui_help_func (const gchar *help_id,
                   gpointer     help_data)
351
{
352
  gimp_help (NULL, help_id);
353
}
354 355 356 357 358 359 360 361

static void
gimp_ensure_modules (void)
{
  static GimpModuleDB *module_db = NULL;

  if (! module_db)
    {
Sven Neumann's avatar
Sven Neumann committed
362 363
      gchar *load_inhibit = gimp_get_module_load_inhibit ();
      gchar *module_path  = gimp_gimprc_query ("module-path");
364 365

      module_db = gimp_module_db_new (FALSE);
Sven Neumann's avatar
Sven Neumann committed
366

367 368 369 370
      gimp_module_db_set_load_inhibit (module_db, load_inhibit);
      gimp_module_db_load (module_db, module_path);

      g_free (module_path);
Sven Neumann's avatar
Sven Neumann committed
371
      g_free (load_inhibit);
372 373
    }
}
Sven Neumann's avatar
Sven Neumann committed
374 375 376 377 378

static void
gimp_window_transient_realized (GtkWidget *window,
                                GdkWindow *parent)
{
379
  if (gtk_widget_get_realized (window))
380
    gdk_window_set_transient_for (gtk_widget_get_window (window), parent);
Sven Neumann's avatar
Sven Neumann committed
381
}
382

383
static gboolean
384 385 386 387 388
gimp_window_set_transient_for (GtkWindow *window,
                               GdkWindow *parent)
{
  gtk_window_set_transient_for (window, NULL);

389
#ifndef GDK_WINDOWING_WIN32
390 391 392 393 394 395
  g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
                                        0, 0, NULL,
                                        gimp_window_transient_realized,
                                        NULL);

  if (! parent)
396
    return FALSE;
397

398
  if (gtk_widget_get_realized (GTK_WIDGET (window)))
399 400
    gdk_window_set_transient_for (gtk_widget_get_window (GTK_WIDGET (window)),
                                  parent);
401 402 403 404 405

  g_signal_connect_object (window, "realize",
                           G_CALLBACK (gimp_window_transient_realized),
                           parent, 0);
  g_object_unref (parent);
406 407

  return TRUE;
408
#endif
409 410

  return FALSE;
411
}
412 413 414 415 416 417 418 419

#ifdef GDK_WINDOWING_QUARTZ
static void
gimp_osx_focus_window (void)
{
  [NSApp activateIgnoringOtherApps:YES];
}
#endif