gui-vtable.c 19.4 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

21 22
#include <string.h>

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

#include "libgimpwidgets/gimpwidgets.h"

#include "gui-types.h"

29 30
#include "config/gimpguiconfig.h"

31
#include "core/gimp.h"
32
#include "core/gimp-utils.h"
33 34
#include "core/gimpbrush.h"
#include "core/gimpcontainer.h"
35
#include "core/gimpcontext.h"
36
#include "core/gimpgradient.h"
37
#include "core/gimpimage.h"
38 39
#include "core/gimppalette.h"
#include "core/gimppattern.h"
40
#include "core/gimpprogress.h"
41 42

#include "text/gimpfont.h"
43

44
#include "widgets/gimpactiongroup.h"
45
#include "widgets/gimpbrushselect.h"
46
#include "widgets/gimpdialogfactory.h"
Sven Neumann's avatar
Sven Neumann committed
47
#include "widgets/gimpdocked.h"
48 49
#include "widgets/gimpfontselect.h"
#include "widgets/gimpgradientselect.h"
50
#include "widgets/gimphelp.h"
51 52 53 54
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpmenufactory.h"
#include "widgets/gimppaletteselect.h"
#include "widgets/gimppatternselect.h"
55
#include "widgets/gimpprogressdialog.h"
56
#include "widgets/gimpuimanager.h"
57
#include "widgets/gimpwidgets-utils.h"
58 59 60 61

#include "display/gimpdisplay.h"
#include "display/gimpdisplay-foreach.h"
#include "display/gimpdisplayshell.h"
62
#include "display/gimpdisplayshell-appearance.h"
63
#include "display/gimpdisplayshell-scale.h"
64

65 66
#include "actions/plug-in-actions.h"

67 68
#include "menus/menus.h"

69
#include "gui-message.h"
70
#include "gui-vtable.h"
Michael Natterer's avatar
Michael Natterer committed
71
#include "themes.h"
72 73 74 75


/*  local function prototypes  */

76 77
static void           gui_ungrab               (Gimp                *gimp);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static void           gui_threads_enter        (Gimp                *gimp);
static void           gui_threads_leave        (Gimp                *gimp);
static void           gui_set_busy             (Gimp                *gimp);
static void           gui_unset_busy           (Gimp                *gimp);
static void           gui_help                 (Gimp                *gimp,
                                                const gchar         *help_domain,
                                                const gchar         *help_id);
static const gchar  * gui_get_program_class    (Gimp                *gimp);
static gchar        * gui_get_display_name     (Gimp                *gimp,
                                                gint                 display_ID,
                                                gint                *monitor_number);
static const gchar  * gui_get_theme_dir        (Gimp                *gimp);
static GimpObject   * gui_display_get_by_ID    (Gimp                *gimp,
                                                gint                 ID);
static gint           gui_display_get_ID       (GimpObject          *display);
static guint32        gui_display_get_window   (GimpObject          *display);
94 95
static GimpObject   * gui_display_create       (Gimp                *gimp,
                                                GimpImage           *image,
96 97 98 99 100 101 102 103 104 105 106 107
                                                GimpUnit             unit,
                                                gdouble              scale);
static void           gui_display_delete       (GimpObject          *display);
static void           gui_displays_reconnect   (Gimp                *gimp,
                                                GimpImage           *old_image,
                                                GimpImage           *new_image);
static GimpProgress * gui_new_progress         (Gimp                *gimp,
                                                GimpObject          *display);
static void           gui_free_progress        (Gimp                *gimp,
                                                GimpProgress        *progress);
static gboolean       gui_pdb_dialog_new       (Gimp                *gimp,
                                                GimpContext         *context,
108
                                                GimpProgress        *progress,
109 110 111 112 113 114 115 116 117 118 119 120 121
                                                GimpContainer       *container,
                                                const gchar         *title,
                                                const gchar         *callback_name,
                                                const gchar         *object_name,
                                                va_list              args);
static gboolean       gui_pdb_dialog_set       (Gimp                *gimp,
                                                GimpContainer       *container,
                                                const gchar         *callback_name,
                                                const gchar         *object_name,
                                                va_list              args);
static gboolean       gui_pdb_dialog_close     (Gimp                *gimp,
                                                GimpContainer       *container,
                                                const gchar         *callback_name);
122 123 124
static gboolean       gui_recent_list_add_uri  (Gimp                *gimp,
                                                const gchar         *uri,
                                                const gchar         *mime_type);
125 126 127 128 129 130 131 132

/*  public functions  */

void
gui_vtable_init (Gimp *gimp)
{
  g_return_if_fail (GIMP_IS_GIMP (gimp));

133
  gimp->gui.ungrab              = gui_ungrab;
134 135 136 137
  gimp->gui.threads_enter       = gui_threads_enter;
  gimp->gui.threads_leave       = gui_threads_leave;
  gimp->gui.set_busy            = gui_set_busy;
  gimp->gui.unset_busy          = gui_unset_busy;
138
  gimp->gui.show_message        = gui_message;
139 140 141 142 143 144
  gimp->gui.help                = gui_help;
  gimp->gui.get_program_class   = gui_get_program_class;
  gimp->gui.get_display_name    = gui_get_display_name;
  gimp->gui.get_theme_dir       = gui_get_theme_dir;
  gimp->gui.display_get_by_id   = gui_display_get_by_ID;
  gimp->gui.display_get_id      = gui_display_get_ID;
Sven Neumann's avatar
Sven Neumann committed
145
  gimp->gui.display_get_window  = gui_display_get_window;
146 147 148 149 150 151 152 153
  gimp->gui.display_create      = gui_display_create;
  gimp->gui.display_delete      = gui_display_delete;
  gimp->gui.displays_reconnect  = gui_displays_reconnect;
  gimp->gui.progress_new        = gui_new_progress;
  gimp->gui.progress_free       = gui_free_progress;
  gimp->gui.pdb_dialog_new      = gui_pdb_dialog_new;
  gimp->gui.pdb_dialog_set      = gui_pdb_dialog_set;
  gimp->gui.pdb_dialog_close    = gui_pdb_dialog_close;
154
  gimp->gui.recent_list_add_uri = gui_recent_list_add_uri;
155 156 157 158 159
}


/*  private functions  */

160 161 162 163 164
static void
gui_ungrab (Gimp *gimp)
{
  GdkDisplay *display = gdk_display_get_default ();

Sven Neumann's avatar
Sven Neumann committed
165 166 167 168 169
  if (display)
    {
      gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
      gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
    }
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static void
gui_threads_enter (Gimp *gimp)
{
  GDK_THREADS_ENTER ();
}

static void
gui_threads_leave (Gimp *gimp)
{
  GDK_THREADS_LEAVE ();
}

static void
gui_set_busy (Gimp *gimp)
{
  gimp_displays_set_busy (gimp);
188
  gimp_dialog_factories_set_busy ();
189 190 191 192 193 194 195 196

  gdk_flush ();
}

static void
gui_unset_busy (Gimp *gimp)
{
  gimp_displays_unset_busy (gimp);
197
  gimp_dialog_factories_unset_busy ();
198 199 200 201 202

  gdk_flush ();
}

static void
203 204 205
gui_help (Gimp        *gimp,
          const gchar *help_domain,
          const gchar *help_id)
206
{
207
  gimp_help_show (gimp, help_domain, help_id);
208 209
}

210 211 212 213 214 215 216 217
static const gchar *
gui_get_program_class (Gimp *gimp)
{
  return gdk_get_program_class ();
}

static gchar *
gui_get_display_name (Gimp *gimp,
218
                      gint  display_ID,
219 220
                      gint *monitor_number)
{
221
  GimpDisplay *display = NULL;
222 223 224
  GdkScreen   *screen;
  gint         monitor;

225 226
  if (display_ID > 0)
    display = gimp_display_get_by_ID (gimp, display_ID);
227

228
  if (display)
229
    {
230
      screen  = gtk_widget_get_screen (display->shell);
231
      monitor = gdk_screen_get_monitor_at_window (screen,
232
                                                  display->shell->window);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    }
  else
    {
      gint x, y;

      gdk_display_get_pointer (gdk_display_get_default (),
                               &screen, &x, &y, NULL);
      monitor = gdk_screen_get_monitor_at_point (screen, x, y);
    }

  *monitor_number = monitor;

  if (screen)
    return gdk_screen_make_display_name (screen);

  return NULL;
}

static const gchar *
gui_get_theme_dir (Gimp *gimp)
{
  return themes_get_theme_dir (gimp, GIMP_GUI_CONFIG (gimp->config)->theme);
}

static GimpObject *
gui_display_get_by_ID (Gimp *gimp,
                       gint  ID)
{
  return (GimpObject *) gimp_display_get_by_ID (gimp, ID);
}

static gint
gui_display_get_ID (GimpObject *display)
{
  return gimp_display_get_ID (GIMP_DISPLAY (display));
}

Sven Neumann's avatar
Sven Neumann committed
270 271 272
static guint32
gui_display_get_window (GimpObject *display)
{
273 274 275 276 277
  GimpDisplay *disp = GIMP_DISPLAY (display);

  if (disp->shell)
    return (guint32) gimp_window_get_native (GTK_WINDOW (disp->shell));

Sven Neumann's avatar
Sven Neumann committed
278 279 280
  return 0;
}

281
static GimpObject *
282 283
gui_display_create (Gimp      *gimp,
                    GimpImage *image,
284 285 286
                    GimpUnit   unit,
                    gdouble    scale)
{
287 288 289 290 291 292 293 294 295 296 297
  GimpContext *context = gimp_get_user_context (gimp);
  GimpDisplay *display = NULL;

  if (gimp_container_num_children (gimp->displays) == 1)
    {
      display = (GimpDisplay *)
        gimp_container_get_child_by_index (gimp->displays, 0);

      if (display->image)
        display = NULL;
    }
298

299 300 301 302 303 304
  if (display)
    {
      gimp_display_set_image (display, image);
      gimp_display_shell_set_unit (GIMP_DISPLAY_SHELL (display->shell), unit);
      gimp_display_shell_scale (GIMP_DISPLAY_SHELL (display->shell),
                                GIMP_ZOOM_TO, scale);
305
      gimp_display_shell_appearance_update (GIMP_DISPLAY_SHELL (display->shell));
306 307 308 309 310 311 312 313 314 315 316 317 318 319

      if (gimp_context_get_display (context) == display)
        {
          gimp_context_set_image (context, image);
          gimp_context_display_changed (context);
        }
      else
        {
          gimp_context_set_display (context, display);
        }
    }
  else
    {
      GList *image_managers = gimp_ui_managers_from_name ("<Image>");
320

321
      g_return_val_if_fail (image_managers != NULL, NULL);
322

323 324 325
      display = gimp_display_new (gimp, image, unit, scale,
                                  global_menu_factory,
                                  image_managers->data);
326

327 328
      gimp_context_set_display (context, display);
   }
329

330 331
  gimp_ui_manager_update (GIMP_DISPLAY_SHELL (display->shell)->menubar_manager,
                          display);
332

333
  return GIMP_OBJECT (display);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
}

static void
gui_display_delete (GimpObject *display)
{
  gimp_display_delete (GIMP_DISPLAY (display));
}

static void
gui_displays_reconnect (Gimp      *gimp,
                        GimpImage *old_image,
                        GimpImage *new_image)
{
  gimp_displays_reconnect (gimp, old_image, new_image);
}

350
static GimpProgress *
351 352
gui_new_progress (Gimp       *gimp,
                  GimpObject *display)
353
{
354
  g_return_val_if_fail (display == NULL || GIMP_IS_DISPLAY (display), NULL);
355 356 357 358

  if (display)
    return GIMP_PROGRESS (display);

359
  return GIMP_PROGRESS (gimp_progress_dialog_new ());
360 361 362
}

static void
363 364
gui_free_progress (Gimp          *gimp,
                   GimpProgress  *progress)
365
{
366
  g_return_if_fail (GIMP_IS_PROGRESS_DIALOG (progress));
367

368 369
  if (GIMP_IS_PROGRESS_DIALOG (progress))
    gtk_widget_destroy (GTK_WIDGET (progress));
370 371
}

372 373 374 375 376 377 378 379
static gboolean
gui_pdb_dialog_present (GtkWindow *window)
{
  gtk_window_present (window);

  return FALSE;
}

380 381 382
static gboolean
gui_pdb_dialog_new (Gimp          *gimp,
                    GimpContext   *context,
383
                    GimpProgress  *progress,
384 385 386 387 388 389
                    GimpContainer *container,
                    const gchar   *title,
                    const gchar   *callback_name,
                    const gchar   *object_name,
                    va_list        args)
{
390 391 392
  GType        dialog_type = G_TYPE_NONE;
  const gchar *dialog_role = NULL;
  const gchar *help_id     = NULL;
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

  if (container->children_type == GIMP_TYPE_BRUSH)
    {
      dialog_type = GIMP_TYPE_BRUSH_SELECT;
      dialog_role = "gimp-brush-selection";
      help_id     = GIMP_HELP_BRUSH_DIALOG;
    }
  else if (container->children_type == GIMP_TYPE_FONT)
    {
      dialog_type = GIMP_TYPE_FONT_SELECT;
      dialog_role = "gimp-font-selection";
      help_id     = GIMP_HELP_FONT_DIALOG;
    }
  else if (container->children_type == GIMP_TYPE_GRADIENT)
    {
      dialog_type = GIMP_TYPE_GRADIENT_SELECT;
      dialog_role = "gimp-gradient-selection";
      help_id     = GIMP_HELP_GRADIENT_DIALOG;
    }
  else if (container->children_type == GIMP_TYPE_PALETTE)
    {
      dialog_type = GIMP_TYPE_PALETTE_SELECT;
      dialog_role = "gimp-palette-selection";
      help_id     = GIMP_HELP_PALETTE_DIALOG;
    }
  else if (container->children_type == GIMP_TYPE_PATTERN)
    {
      dialog_type = GIMP_TYPE_PATTERN_SELECT;
      dialog_role = "gimp-pattern-selection";
      help_id     = GIMP_HELP_PATTERN_DIALOG;
    }

  if (dialog_type != G_TYPE_NONE)
    {
      GimpObject *object = NULL;

      if (object_name && strlen (object_name))
        object = gimp_container_get_child_by_name (container, object_name);

      if (! object)
        object = gimp_context_get_by_type (context, container->children_type);

      if (object)
        {
437 438 439
          GParameter *params   = NULL;
          gint        n_params = 0;
          GtkWidget  *dialog;
Sven Neumann's avatar
Sven Neumann committed
440
          GtkWidget  *view;
441 442 443 444 445 446

          params = gimp_parameters_append (dialog_type, params, &n_params,
                                           "title",          title,
                                           "role",           dialog_role,
                                           "help-func",      gimp_standard_help_func,
                                           "help-id",        help_id,
447
                                           "pdb",            gimp->pdb,
448 449 450 451 452 453
                                           "context",        context,
                                           "select-type",    container->children_type,
                                           "initial-object", object,
                                           "callback-name",  callback_name,
                                           "menu-factory",   global_menu_factory,
                                           NULL);
454

455 456 457
          params = gimp_parameters_append_valist (dialog_type,
                                                  params, &n_params,
                                                  args);
458 459 460

          dialog = g_object_newv (dialog_type, n_params, params);

461
          gimp_parameters_free (params, n_params);
462

Sven Neumann's avatar
Sven Neumann committed
463 464 465 466
          view = GIMP_PDB_DIALOG (dialog)->view;
          if (view)
            gimp_docked_set_show_button_bar (GIMP_DOCKED (view), FALSE);

467 468 469 470 471 472 473 474
          if (progress)
            {
              guint32 window = gimp_progress_get_window (progress);

              if (window)
                gimp_window_set_transient_for (GTK_WINDOW (dialog), window);
            }

475 476
          gtk_widget_show (dialog);

477 478
          /*  workaround for bug #360106  */
          {
Sven Neumann's avatar
Sven Neumann committed
479 480 481 482 483
            GSource  *source = g_timeout_source_new (100);
            GClosure *closure;

            closure = g_cclosure_new_object (G_CALLBACK (gui_pdb_dialog_present),
                                             G_OBJECT (dialog));
484 485 486 487 488 489

            g_source_set_closure (source, closure);
            g_source_attach (source, NULL);
            g_source_unref (source);
          }

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
          return TRUE;
        }
    }

  return FALSE;
}

static gboolean
gui_pdb_dialog_set (Gimp          *gimp,
                    GimpContainer *container,
                    const gchar   *callback_name,
                    const gchar   *object_name,
                    va_list        args)
{
  GimpPdbDialogClass *klass = NULL;

  if (container->children_type == GIMP_TYPE_BRUSH)
    klass = g_type_class_peek (GIMP_TYPE_BRUSH_SELECT);
  else if (container->children_type == GIMP_TYPE_FONT)
    klass = g_type_class_peek (GIMP_TYPE_FONT_SELECT);
  else if (container->children_type == GIMP_TYPE_GRADIENT)
    klass = g_type_class_peek (GIMP_TYPE_GRADIENT_SELECT);
  else if (container->children_type == GIMP_TYPE_PALETTE)
    klass = g_type_class_peek (GIMP_TYPE_PALETTE_SELECT);
  else if (container->children_type == GIMP_TYPE_PATTERN)
    klass = g_type_class_peek (GIMP_TYPE_PATTERN_SELECT);

  if (klass)
    {
      GimpPdbDialog *dialog;

      dialog = gimp_pdb_dialog_get_by_callback (klass, callback_name);

      if (dialog && dialog->select_type == container->children_type)
        {
          GimpObject *object;

          object = gimp_container_get_child_by_name (container, object_name);

          if (object)
            {
531
              const gchar *prop_name = va_arg (args, const gchar *);
532 533 534 535 536 537 538 539

              gimp_context_set_by_type (dialog->context, dialog->select_type,
                                        object);

              if (prop_name)
                g_object_set_valist (G_OBJECT (dialog), prop_name, args);

              gtk_window_present (GTK_WINDOW (dialog));
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
              return TRUE;
            }
        }
    }

  return FALSE;
}

static gboolean
gui_pdb_dialog_close (Gimp          *gimp,
                      GimpContainer *container,
                      const gchar   *callback_name)
{
  GimpPdbDialogClass *klass = NULL;

  if (container->children_type == GIMP_TYPE_BRUSH)
    klass = g_type_class_peek (GIMP_TYPE_BRUSH_SELECT);
  else if (container->children_type == GIMP_TYPE_FONT)
    klass = g_type_class_peek (GIMP_TYPE_FONT_SELECT);
  else if (container->children_type == GIMP_TYPE_GRADIENT)
    klass = g_type_class_peek (GIMP_TYPE_GRADIENT_SELECT);
  else if (container->children_type == GIMP_TYPE_PALETTE)
    klass = g_type_class_peek (GIMP_TYPE_PALETTE_SELECT);
  else if (container->children_type == GIMP_TYPE_PATTERN)
    klass = g_type_class_peek (GIMP_TYPE_PATTERN_SELECT);

  if (klass)
    {
      GimpPdbDialog *dialog;

      dialog = gimp_pdb_dialog_get_by_callback (klass, callback_name);

      if (dialog && dialog->select_type == container->children_type)
        {
          gtk_widget_destroy (GTK_WIDGET (dialog));
          return TRUE;
        }
    }

  return FALSE;
}
582 583

static gboolean
584
gui_recent_list_add_uri (Gimp        *gimp,
585 586 587
                         const gchar *uri,
                         const gchar *mime_type)
{
588
  GtkRecentManager *manager;
589
  GtkRecentData     recent;
590
  const gchar      *groups[2] = { "Graphics", NULL };
591 592 593 594

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
  g_return_val_if_fail (uri != NULL, FALSE);

595
  manager = gtk_recent_manager_get_default ();
596

597
  g_return_val_if_fail (manager != NULL, FALSE);
598 599 600

  /* use last part of the URI */
  recent.display_name = NULL;
601

602
  /* no special description */
603
  recent.description  = NULL;
604 605
  recent.mime_type    = (mime_type ?
                         (gchar *) mime_type : "application/octet-stream");
606
  recent.app_name     = "GNU Image Manipulation Program";
607
  recent.app_exec     = GIMP_COMMAND " %u";
608
  recent.groups       = (gchar **) groups;
609
  recent.is_private   = FALSE;
610

611
  return gtk_recent_manager_add_full (manager, uri, &recent);
612
}