gimpui.c 8.38 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * 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,
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 15 16 17 18 19
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

22 23
#include <stdlib.h>

24 25 26 27
#include <gtk/gtk.h>

#include "libgimpmodule/gimpmodule.h"

28 29 30
#include "gimp.h"
#include "gimpui.h"

31
#include "libgimpwidgets/gimpwidgets.h"
32
#include "libgimpwidgets/gimpwidgets-private.h"
33

34 35 36

/*  local function prototypes  */

Sven Neumann's avatar
Sven Neumann committed
37 38 39 40 41
static void  gimp_ui_help_func              (const gchar *help_id,
                                             gpointer     help_data);
static void  gimp_ensure_modules            (void);
static void  gimp_window_transient_realized (GtkWidget   *window,
                                             GdkWindow   *parent);
42 43
static void  gimp_window_set_transient_for  (GtkWindow   *window,
                                             GdkWindow   *parent);
Sven Neumann's avatar
Sven Neumann committed
44 45 46


static gboolean gimp_ui_initialized = FALSE;
47 48 49 50


/*  public functions  */

51 52 53 54 55
/**
 * 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
 *             executable and _not_ the PDB procedure name or something.
56 57
 * @preview:   This parameter is unused and exists for historical
 *             reasons only.
58
 *
59 60
 * This function initializes GTK+ with gtk_init() and initializes GDK's
 * image rendering subsystem (GdkRGB) to follow the GIMP main program's
61
 * colormap allocation/installation policy.
62 63 64
 *
 * The GIMP's colormap policy can be determinded by the user with the
 * gimprc variables @min_colors and @install_cmap.
65
 **/
66
void
67 68
gimp_ui_init (const gchar *prog_name,
	      gboolean     preview)
69
{
70 71 72
  const gchar  *display_name;
  gint          argc;
  gchar       **argv;
73
  gchar        *themerc;
74
  GdkScreen    *screen;
75

76 77
  g_return_if_fail (prog_name != NULL);

Sven Neumann's avatar
Sven Neumann committed
78
  if (gimp_ui_initialized)
79 80
    return;

81 82 83 84
  display_name = gimp_display_name ();

  if (display_name)
    {
85
      const gchar *var_name = NULL;
86 87 88 89 90 91 92 93 94 95 96

#if defined (GDK_WINDOWING_X11)
      var_name = "DISPLAY";
#elif defined (GDK_WINDOWING_DIRECTFB) || defined (GDK_WINDOWING_FB)
      var_name = "GDK_DISPLAY";
#endif

      if (var_name)
        putenv (g_strdup_printf ("%s=%s", var_name, display_name));
    }

97 98
  argc    = 2;
  argv    = g_new (gchar *, 2);
99
  argv[0] = g_strdup (prog_name);
100
  argv[1] = g_strdup_printf ("--class=%s", gimp_wm_class ());
101 102 103

  gtk_init (&argc, &argv);

104 105 106
  themerc = gimp_personal_rc_file ("themerc");
  gtk_rc_add_default_file (themerc);
  g_free (themerc);
107

108 109
  gdk_rgb_set_min_colors (gimp_min_colors ());
  gdk_rgb_set_install (gimp_install_cmap ());
110

111 112
  screen = gdk_screen_get_default ();
  gtk_widget_set_default_colormap (gdk_screen_get_rgb_colormap (screen));
113

114
  gimp_widgets_init (gimp_ui_help_func,
115 116
                     gimp_context_get_foreground,
                     gimp_context_get_background,
117
                     gimp_ensure_modules);
118

119 120 121
  if (! gimp_show_tool_tips ())
    gimp_help_disable_tooltips ();

122 123
  gimp_dialogs_show_help_button (gimp_show_help_button ());

Sven Neumann's avatar
Sven Neumann committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  gimp_ui_initialized = TRUE;
}

/**
 * gimp_ui_get_display_window:
 *
 * 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.
 *
 * Since: GIMP 2.4
 */
GdkWindow *
gimp_ui_get_display_window (guint32 gdisp_ID)
{
146
#ifndef GDK_NATIVE_WINDOW_POINTER
Sven Neumann's avatar
Sven Neumann committed
147 148 149 150 151 152 153 154 155 156 157 158 159
  GdkNativeWindow  window;

  g_return_val_if_fail (gimp_ui_initialized, NULL);

  window = gimp_display_get_window_handle (gdisp_ID);
  if (window)
    return gdk_window_foreign_new_for_display (gdk_display_get_default (),
                                               window);
#endif

  return NULL;
}

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
/**
 * 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.
 *
 * Since: GIMP 2.4
 */
GdkWindow *
gimp_ui_get_progress_window (void)
{
#ifndef GDK_NATIVE_WINDOW_POINTER
  GdkNativeWindow  window;

  g_return_val_if_fail (gimp_ui_initialized, NULL);

  window = gimp_progress_get_window_handle ();
  if (window)
    return gdk_window_foreign_new_for_display (gdk_display_get_default (),
                                               window);
#endif

  return NULL;
}

Sven Neumann's avatar
Sven Neumann committed
193 194 195 196 197 198 199 200 201 202
/**
 * 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
203
 * gimp_window_set_transient().
Sven Neumann's avatar
Sven Neumann committed
204 205 206 207 208 209 210 211 212 213
 *
 * Since: GIMP 2.4
 */
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));

214 215
  gimp_window_set_transient_for (window,
                                 gimp_ui_get_display_window (gdisp_ID));
Sven Neumann's avatar
Sven Neumann committed
216 217 218
}

/**
219
 * gimp_window_set_transient:
Sven Neumann's avatar
Sven Neumann committed
220 221 222
 * @window: the #GtkWindow that should become transient
 *
 * Indicates to the window manager that @window is a transient dialog
223
 * associated with the GIMP window that the plug-in has been
Sven Neumann's avatar
Sven Neumann committed
224 225 226 227 228
 * started from. See also gimp_window_set_transient_for_display().
 *
 * Since: GIMP 2.4
 */
void
229
gimp_window_set_transient (GtkWindow *window)
Sven Neumann's avatar
Sven Neumann committed
230 231 232 233
{
  g_return_if_fail (gimp_ui_initialized);
  g_return_if_fail (GTK_IS_WINDOW (window));

234
  gimp_window_set_transient_for (window, gimp_ui_get_progress_window ());
235
}
236 237 238 239 240


/*  private functions  */

static void
241 242
gimp_ui_help_func (const gchar *help_id,
                   gpointer     help_data)
243
{
244
  gimp_help (NULL, help_id);
245
}
246 247 248 249 250 251 252 253

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

  if (! module_db)
    {
Sven Neumann's avatar
Sven Neumann committed
254 255
      gchar *load_inhibit = gimp_get_module_load_inhibit ();
      gchar *module_path  = gimp_gimprc_query ("module-path");
256 257

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

259 260 261 262
      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
263
      g_free (load_inhibit);
264 265
    }
}
Sven Neumann's avatar
Sven Neumann committed
266 267 268 269 270 271 272 273

static void
gimp_window_transient_realized (GtkWidget *window,
                                GdkWindow *parent)
{
  if (GTK_WIDGET_REALIZED (window))
    gdk_window_set_transient_for (window->window, parent);
}
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

static void
gimp_window_set_transient_for (GtkWindow *window,
                               GdkWindow *parent)
{
  gtk_window_set_transient_for (window, NULL);

  g_signal_handlers_disconnect_matched (window, G_SIGNAL_MATCH_FUNC,
                                        0, 0, NULL,
                                        gimp_window_transient_realized,
                                        NULL);

  if (! parent)
    return;

  if (GTK_WIDGET_REALIZED (window))
    gdk_window_set_transient_for (GTK_WIDGET (window)->window, parent);

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