gimpdockbook.c 27.8 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpdockbook.c
5
 * Copyright (C) 2001-2007 Michael Natterer <mitch@gimp.org>
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23 24
#include <string.h>

25
#include <gegl.h>
26
#undef GTK_DISABLE_DEPRECATED
27 28 29 30
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

31
#include "widgets-types.h"
32

33 34 35
#include "config/gimpguiconfig.h"

#include "core/gimp.h"
36
#include "core/gimpcontext.h"
37
#include "core/gimpmarshal.h"
38

39
#include "gimpdialogfactory.h"
40
#include "gimpdnd.h"
41
#include "gimpdock.h"
42 43
#include "gimpdockable.h"
#include "gimpdockbook.h"
44
#include "gimpdocked.h"
45
#include "gimpdockcontainer.h"
46
#include "gimpdockwindow.h"
47
#include "gimphelp-ids.h"
48
#include "gimpmenufactory.h"
Martin Nordholts's avatar
Martin Nordholts committed
49
#include "gimppanedbox.h"
Michael Natterer's avatar
Michael Natterer committed
50 51
#include "gimpstringaction.h"
#include "gimpuimanager.h"
52
#include "gimpview.h"
53
#include "gimpwidgets-utils.h"
54

55
#include "gimp-log.h"
56
#include "gimp-intl.h"
57

58

59 60 61 62 63
#define DEFAULT_TAB_ICON_SIZE        GTK_ICON_SIZE_BUTTON
#define DND_WIDGET_ICON_SIZE         GTK_ICON_SIZE_BUTTON
#define MENU_WIDGET_SPACING          4
#define TAB_HOVER_TIMEOUT            500
#define GIMP_DOCKABLE_DETACH_REF_KEY "gimp-dockable-detach-ref"
64 65


66 67 68 69 70 71 72 73
enum
{
  DOCKABLE_ADDED,
  DOCKABLE_REMOVED,
  DOCKABLE_REORDERED,
  LAST_SIGNAL
};

74

75 76
struct _GimpDockbookPrivate
{
77 78
  GimpDock       *dock;
  GimpUIManager  *ui_manager;
79

80 81
  guint           tab_hover_timeout;
  GimpDockable   *tab_hover_dockable;
82

83 84 85
  GimpPanedBox   *drag_handler;

  GtkWidget      *menu_button;
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
static void         gimp_dockbook_finalize          (GObject        *object);

static gboolean     gimp_dockbook_drag_motion       (GtkWidget      *widget,
                                                     GdkDragContext *context,
                                                     gint            x,
                                                     gint            y,
                                                     guint           time);
static gboolean     gimp_dockbook_drag_drop         (GtkWidget      *widget,
                                                     GdkDragContext *context,
                                                     gint            x,
                                                     gint            y,
                                                     guint           time);
static gboolean     gimp_dockbook_popup_menu        (GtkWidget      *widget);

static GtkNotebook *gimp_dockbook_create_window     (GtkNotebook    *notebook,
                                                     GtkWidget      *page,
                                                     gint            x,
                                                     gint            y);
static void         gimp_dockbook_page_added        (GtkNotebook    *notebook,
                                                     GtkWidget      *child,
                                                     guint           page_num);
static void         gimp_dockbook_page_removed      (GtkNotebook    *notebook,
                                                     GtkWidget      *child,
                                                     guint           page_num);
static void         gimp_dockbook_page_reordered    (GtkNotebook    *notebook,
                                                     GtkWidget      *child,
                                                     guint           page_num);

static gboolean     gimp_dockbook_menu_button_press (GimpDockbook   *dockbook,
                                                     GdkEventButton *bevent,
                                                     GtkWidget      *button);
static gboolean     gimp_dockbook_show_menu         (GimpDockbook   *dockbook);
static void         gimp_dockbook_menu_end          (GimpDockable   *dockable);
static void         gimp_dockbook_tab_locked_notify (GimpDockable   *dockable,
                                                     GParamSpec     *pspec,
                                                     GimpDockbook   *dockbook);
static void         gimp_dockbook_help_func         (const gchar    *help_id,
                                                     gpointer        help_data);
127

128

129
G_DEFINE_TYPE_WITH_PRIVATE (GimpDockbook, gimp_dockbook, GTK_TYPE_NOTEBOOK)
130 131

#define parent_class gimp_dockbook_parent_class
132

133
static guint dockbook_signals[LAST_SIGNAL] = { 0 };
134

135
static const GtkTargetEntry dialog_target_table[] = { GIMP_TARGET_NOTEBOOK_TAB };
136 137 138 139 140


static void
gimp_dockbook_class_init (GimpDockbookClass *klass)
{
141 142 143
  GObjectClass     *object_class   = G_OBJECT_CLASS (klass);
  GtkWidgetClass   *widget_class   = GTK_WIDGET_CLASS (klass);
  GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
144

145
  dockbook_signals[DOCKABLE_ADDED] =
146
    g_signal_new ("dockable-added",
147 148 149 150 151 152 153
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpDockbookClass, dockable_added),
                  NULL, NULL,
                  gimp_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  GIMP_TYPE_DOCKABLE);
154 155

  dockbook_signals[DOCKABLE_REMOVED] =
156
    g_signal_new ("dockable-removed",
157 158 159 160 161 162 163
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpDockbookClass, dockable_removed),
                  NULL, NULL,
                  gimp_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  GIMP_TYPE_DOCKABLE);
164 165

  dockbook_signals[DOCKABLE_REORDERED] =
166
    g_signal_new ("dockable-reordered",
167 168 169 170 171 172 173
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpDockbookClass, dockable_reordered),
                  NULL, NULL,
                  gimp_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  GIMP_TYPE_DOCKABLE);
174

175
  object_class->finalize         = gimp_dockbook_finalize;
176

177 178 179
  widget_class->drag_motion      = gimp_dockbook_drag_motion;
  widget_class->drag_drop        = gimp_dockbook_drag_drop;
  widget_class->popup_menu       = gimp_dockbook_popup_menu;
180

181 182 183 184 185 186 187 188
  notebook_class->create_window  = gimp_dockbook_create_window;
  notebook_class->page_added     = gimp_dockbook_page_added;
  notebook_class->page_removed   = gimp_dockbook_page_removed;
  notebook_class->page_reordered = gimp_dockbook_page_reordered;

  klass->dockable_added          = NULL;
  klass->dockable_removed        = NULL;
  klass->dockable_reordered      = NULL;
189

190
  gtk_widget_class_install_style_property (widget_class,
191
                                           g_param_spec_enum ("tab-icon-size",
192 193
                                                              NULL, NULL,
                                                              GTK_TYPE_ICON_SIZE,
194
                                                              DEFAULT_TAB_ICON_SIZE,
195
                                                              GIMP_PARAM_READABLE));
196 197 198 199 200
}

static void
gimp_dockbook_init (GimpDockbook *dockbook)
{
201
  GtkNotebook *notebook = GTK_NOTEBOOK (dockbook);
202
  GtkWidget   *image;
203

204
  dockbook->p = gimp_dockbook_get_instance_private (dockbook);
205

206
  /* Various init */
207 208 209
  gtk_notebook_popup_enable (notebook);
  gtk_notebook_set_scrollable (notebook, TRUE);
  gtk_notebook_set_show_border (notebook, FALSE);
210
  gtk_notebook_set_show_tabs (notebook, TRUE);
211
  gtk_notebook_set_group_name (notebook, "gimp-dockbook");
212 213

  gtk_drag_dest_set (GTK_WIDGET (dockbook),
214
                     0,
215
                     dialog_target_table, G_N_ELEMENTS (dialog_target_table),
216
                     GDK_ACTION_MOVE);
217 218 219 220 221 222

  /* Menu button */
  dockbook->p->menu_button = gtk_button_new ();
  gtk_widget_set_can_focus (dockbook->p->menu_button, FALSE);
  gtk_button_set_relief (GTK_BUTTON (dockbook->p->menu_button),
                         GTK_RELIEF_NONE);
223 224 225
  gtk_notebook_set_action_widget (notebook,
                                  dockbook->p->menu_button,
                                  GTK_PACK_END);
226 227
  gtk_widget_show (dockbook->p->menu_button);

228 229
  image = gtk_image_new_from_icon_name (GIMP_ICON_MENU_LEFT,
                                        GTK_ICON_SIZE_MENU);
230
  gtk_image_set_pixel_size (GTK_IMAGE (image), 12);
231 232
  gtk_image_set_from_icon_name (GTK_IMAGE (image), GIMP_ICON_MENU_LEFT,
                                GTK_ICON_SIZE_MENU);
233 234 235 236 237 238 239 240 241
  gtk_container_add (GTK_CONTAINER (dockbook->p->menu_button), image);
  gtk_widget_show (image);

  gimp_help_set_help_data (dockbook->p->menu_button, _("Configure this tab"),
                           GIMP_HELP_DOCK_TAB_MENU);

  g_signal_connect_swapped (dockbook->p->menu_button, "button-press-event",
                            G_CALLBACK (gimp_dockbook_menu_button_press),
                            dockbook);
242 243
}

244 245 246
static void
gimp_dockbook_finalize (GObject *object)
{
247
  GimpDockbook *dockbook = GIMP_DOCKBOOK (object);
248

249 250
  g_clear_object (&dockbook->p->ui_manager);

251 252 253
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

254 255 256 257 258 259 260
static gboolean
gimp_dockbook_drag_motion (GtkWidget      *widget,
                           GdkDragContext *context,
                           gint            x,
                           gint            y,
                           guint           time)
{
261 262 263 264 265 266 267 268 269 270 271 272 273
  GimpDockbook *dockbook = GIMP_DOCKBOOK (widget);

  if (gimp_paned_box_will_handle_drag (dockbook->p->drag_handler,
                                       widget,
                                       context,
                                       x, y,
                                       time))
    {
      gdk_drag_status (context, 0, time);

      return FALSE;
    }

274 275
  return GTK_WIDGET_CLASS (parent_class)->drag_motion (widget, context,
                                                       x, y, time);
276 277
}

278 279
static gboolean
gimp_dockbook_drag_drop (GtkWidget      *widget,
280 281 282 283
                         GdkDragContext *context,
                         gint            x,
                         gint            y,
                         guint           time)
284
{
285 286 287 288 289 290 291 292
  GimpDockbook *dockbook = GIMP_DOCKBOOK (widget);

  if (gimp_paned_box_will_handle_drag (dockbook->p->drag_handler,
                                       widget,
                                       context,
                                       x, y,
                                       time))
    {
293
      return FALSE;
294 295
    }

296 297 298 299 300 301
  if (widget == gtk_drag_get_source_widget (context))
    {
      GList *children   = gtk_container_get_children (GTK_CONTAINER (widget));
      gint   n_children = g_list_length (children);

      g_list_free (children);
302

303 304 305 306 307 308 309 310
      /* we dragged the only tab, and want to drop it back on the same
       * notebook, this would remove and add the page, causing the
       * dockbook to be destroyed in the process because it becomes
       * empty
       */
      if (n_children == 1)
        return FALSE;
    }
311

312 313
  return GTK_WIDGET_CLASS (parent_class)->drag_drop (widget, context,
                                                     x, y, time);
314
}
315

316 317 318 319 320 321
static gboolean
gimp_dockbook_popup_menu (GtkWidget *widget)
{
  return gimp_dockbook_show_menu (GIMP_DOCKBOOK (widget));
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
static GtkNotebook *
gimp_dockbook_create_window (GtkNotebook *notebook,
                             GtkWidget   *page,
                             gint         x,
                             gint         y)
{
  GimpDockbook      *dockbook = GIMP_DOCKBOOK (notebook);
  GimpDockable      *dockable = GIMP_DOCKABLE (page);
  GimpDialogFactory *dialog_factory;
  GimpMenuFactory   *menu_factory;
  GimpDockWindow    *src_dock_window;
  GimpDock          *src_dock;
  GtkWidget         *new_dock;
  GimpDockWindow    *new_dock_window;
  GtkWidget         *new_dockbook;

  src_dock        = gimp_dockbook_get_dock (dockbook);
  src_dock_window = gimp_dock_window_from_dock (src_dock);

  dialog_factory = gimp_dock_get_dialog_factory (src_dock);
  menu_factory   = gimp_dialog_factory_get_menu_factory (dialog_factory);

  new_dock = gimp_dock_with_window_new (dialog_factory,
                                        gimp_widget_get_monitor (page),
                                        FALSE);

  new_dock_window = gimp_dock_window_from_dock (GIMP_DOCK (new_dock));
  gtk_window_set_position (GTK_WINDOW (new_dock_window), GTK_WIN_POS_MOUSE);
  if (src_dock_window)
    gimp_dock_window_setup (new_dock_window, src_dock_window);

  new_dockbook = gimp_dockbook_new (menu_factory);

  gimp_dock_add_book (GIMP_DOCK (new_dock), GIMP_DOCKBOOK (new_dockbook), 0);

  gtk_widget_show (GTK_WIDGET (new_dock_window));
  gtk_widget_show (new_dock);

  return GTK_NOTEBOOK (new_dockbook);
}

static void
gimp_dockbook_page_added (GtkNotebook *notebook,
                          GtkWidget   *child,
                          guint        page_num)
{
  GimpDockbook *dockbook = GIMP_DOCKBOOK (notebook);
  GimpDockable *dockable = GIMP_DOCKABLE (child);
  GtkWidget    *tab_widget;
  GtkWidget    *menu_widget;

  GIMP_LOG (DND, "GimpDockable %p added to GimpDockbook %p",
            dockable, dockbook);

  tab_widget = gimp_dockbook_create_tab_widget (dockbook, dockable);

  /* For the notebook right-click menu, always use the icon style */
  menu_widget =
    gimp_dockable_create_tab_widget (dockable,
                                     gimp_dock_get_context (dockbook->p->dock),
                                     GIMP_TAB_STYLE_ICON_BLURB,
                                     GTK_ICON_SIZE_MENU);

  gtk_notebook_set_tab_label (notebook, child, tab_widget);
  gtk_notebook_set_menu_label (notebook, child, menu_widget);

388
  if (! gimp_dockable_get_locked (dockable))
389 390 391 392 393 394 395 396 397 398 399 400 401 402
    {
      gtk_notebook_set_tab_reorderable (notebook, child, TRUE);
      gtk_notebook_set_tab_detachable (notebook, child, TRUE);
    }

  gimp_dockable_set_dockbook (dockable, dockbook);

  gimp_dockable_set_context (dockable,
                             gimp_dock_get_context (dockbook->p->dock));

  g_signal_connect (dockable, "notify::locked",
                    G_CALLBACK (gimp_dockbook_tab_locked_notify),
                    dockbook);

403
  gtk_widget_show (child);
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 437 438 439 440 441 442 443 444 445 446 447
  gtk_notebook_set_current_page (notebook, page_num);

  g_signal_emit (dockbook, dockbook_signals[DOCKABLE_ADDED], 0, dockable);
}

static void
gimp_dockbook_page_removed (GtkNotebook *notebook,
                            GtkWidget   *child,
                            guint        page_num)
{
  GimpDockbook *dockbook = GIMP_DOCKBOOK (notebook);
  GimpDockable *dockable = GIMP_DOCKABLE (child);

  GIMP_LOG (DND, "GimpDockable removed %p from GimpDockbook %p",
            dockable, dockbook);

  g_signal_handlers_disconnect_by_func (dockable,
                                        G_CALLBACK (gimp_dockbook_tab_locked_notify),
                                        dockbook);

  gimp_dockable_set_dockbook (dockable, NULL);

  gimp_dockable_set_context (dockable, NULL);

  g_signal_emit (dockbook, dockbook_signals[DOCKABLE_REMOVED], 0, dockable);

  if (dockbook->p->dock)
    {
      GList *children = gtk_container_get_children (GTK_CONTAINER (dockbook));

      if (! children)
        gimp_dock_remove_book (dockbook->p->dock, dockbook);

      g_list_free (children);
    }
}

static void
gimp_dockbook_page_reordered (GtkNotebook *notebook,
                              GtkWidget   *child,
                              guint        page_num)
{
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
static gboolean
gimp_dockbook_menu_button_press (GimpDockbook   *dockbook,
                                 GdkEventButton *bevent,
                                 GtkWidget      *button)
{
  gboolean handled = FALSE;

  if (bevent->button == 1 && bevent->type == GDK_BUTTON_PRESS)
    handled = gimp_dockbook_show_menu (dockbook);

  return handled;
}

static gboolean
gimp_dockbook_show_menu (GimpDockbook *dockbook)
{
464 465 466 467 468 469 470 471
  GimpUIManager *dockbook_ui_manager;
  GimpUIManager *dialog_ui_manager;
  const gchar   *dialog_ui_path;
  gpointer       dialog_popup_data;
  GtkWidget     *parent_menu_widget;
  GtkAction     *parent_menu_action;
  GimpDockable  *dockable;
  gint           page_num;
472

473
  dockbook_ui_manager = gimp_dockbook_get_ui_manager (dockbook);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

  if (! dockbook_ui_manager)
    return FALSE;

  parent_menu_widget =
    gtk_ui_manager_get_widget (GTK_UI_MANAGER (dockbook_ui_manager),
                               "/dockable-popup/dockable-menu");
  parent_menu_action =
    gtk_ui_manager_get_action (GTK_UI_MANAGER (dockbook_ui_manager),
                               "/dockable-popup/dockable-menu");

  if (! parent_menu_widget || ! parent_menu_action)
    return FALSE;

  page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (dockbook));
  dockable = GIMP_DOCKABLE (gtk_notebook_get_nth_page (GTK_NOTEBOOK (dockbook),
                                                       page_num));

492
  if (! dockable)
493 494 495 496 497 498 499 500
    return FALSE;

  dialog_ui_manager = gimp_dockable_get_menu (dockable,
                                              &dialog_ui_path,
                                              &dialog_popup_data);

  if (dialog_ui_manager && dialog_ui_path)
    {
501 502 503
      GtkWidget *child_menu_widget;
      GtkAction *child_menu_action;
      gchar     *label;
504 505 506 507 508 509 510

      child_menu_widget =
        gtk_ui_manager_get_widget (GTK_UI_MANAGER (dialog_ui_manager),
                                   dialog_ui_path);

      if (! child_menu_widget)
        {
511
          g_warning ("%s: UI manager '%s' has no widget at path '%s'",
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
                     G_STRFUNC, dialog_ui_manager->name, dialog_ui_path);
          return FALSE;
        }

      child_menu_action =
        gtk_ui_manager_get_action (GTK_UI_MANAGER (dialog_ui_manager),
                                   dialog_ui_path);

      if (! child_menu_action)
        {
          g_warning ("%s: UI manager '%s' has no action at path '%s'",
                     G_STRFUNC, dialog_ui_manager->name, dialog_ui_path);
          return FALSE;
        }

      g_object_get (child_menu_action,
                    "label", &label,
                    NULL);

      g_object_set (parent_menu_action,
532 533 534
                    "label",     label,
                    "icon-name", gimp_dockable_get_icon_name (dockable),
                    "visible",   TRUE,
535 536
                    NULL);

537 538
      g_free (label);

539 540 541 542 543 544 545 546 547 548
      if (! GTK_IS_MENU (child_menu_widget))
        {
          g_warning ("%s: child_menu_widget (%p) is not a GtkMenu",
                     G_STRFUNC, child_menu_widget);
          return FALSE;
        }

      {
        GtkWidget *image = gimp_dockable_get_icon (dockable,
                                                   GTK_ICON_SIZE_MENU);
549
        gimp_menu_item_set_image (GTK_MENU_ITEM (parent_menu_widget), image);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
        gtk_widget_show (image);
      }

      gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent_menu_widget),
                                 child_menu_widget);

      gimp_ui_manager_update (dialog_ui_manager, dialog_popup_data);
    }
  else
    {
      g_object_set (parent_menu_action, "visible", FALSE, NULL);
    }

  /*  an action callback may destroy both dockable and dockbook, so
   *  reference them for gimp_dockbook_menu_end()
   */
  g_object_ref (dockable);
  g_object_set_data_full (G_OBJECT (dockable), GIMP_DOCKABLE_DETACH_REF_KEY,
                          g_object_ref (dockbook),
                          g_object_unref);

  gimp_ui_manager_update (dockbook_ui_manager, dockable);
572 573 574 575 576 577 578 579 580

  gimp_ui_manager_ui_popup_at_widget (dockbook_ui_manager,
                                      "/dockable-popup",
                                      dockbook->p->menu_button,
                                      GDK_GRAVITY_WEST,
                                      GDK_GRAVITY_NORTH_EAST,
                                      NULL,
                                      (GDestroyNotify) gimp_dockbook_menu_end,
                                      dockable);
581 582 583 584 585 586 587

  return TRUE;
}

static void
gimp_dockbook_menu_end (GimpDockable *dockable)
{
588 589 590
  GimpUIManager *dialog_ui_manager;
  const gchar   *dialog_ui_path;
  gpointer       dialog_popup_data;
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

  dialog_ui_manager = gimp_dockable_get_menu (dockable,
                                              &dialog_ui_path,
                                              &dialog_popup_data);

  if (dialog_ui_manager && dialog_ui_path)
    {
      GtkWidget *child_menu_widget =
        gtk_ui_manager_get_widget (GTK_UI_MANAGER (dialog_ui_manager),
                                   dialog_ui_path);

      if (child_menu_widget)
        gtk_menu_detach (GTK_MENU (child_menu_widget));
    }

606
  /*  release gimp_dockbook_show_menu()'s references  */
607 608 609 610
  g_object_set_data (G_OBJECT (dockable), GIMP_DOCKABLE_DETACH_REF_KEY, NULL);
  g_object_unref (dockable);
}

611

612
/*  public functions  */
613

614 615 616 617
GtkWidget *
gimp_dockbook_new (GimpMenuFactory *menu_factory)
{
  GimpDockbook *dockbook;
618

619
  g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);
620

621
  dockbook = g_object_new (GIMP_TYPE_DOCKBOOK, NULL);
622

623
  dockbook->p->ui_manager = gimp_menu_factory_manager_new (menu_factory,
Jehan's avatar
Jehan committed
624
                                                           "<Dockable>",
625
                                                           dockbook);
626

627 628 629 630
  gimp_help_connect (GTK_WIDGET (dockbook), gimp_dockbook_help_func,
                     GIMP_HELP_DOCK, dockbook);

  return GTK_WIDGET (dockbook);
631 632
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
GimpDock *
gimp_dockbook_get_dock (GimpDockbook *dockbook)
{
  g_return_val_if_fail (GIMP_IS_DOCKBOOK (dockbook), NULL);

  return dockbook->p->dock;
}

void
gimp_dockbook_set_dock (GimpDockbook *dockbook,
                        GimpDock     *dock)
{
  g_return_if_fail (GIMP_IS_DOCKBOOK (dockbook));
  g_return_if_fail (dock == NULL || GIMP_IS_DOCK (dock));

  dockbook->p->dock = dock;
}

GimpUIManager *
gimp_dockbook_get_ui_manager (GimpDockbook *dockbook)
{
  g_return_val_if_fail (GIMP_IS_DOCKBOOK (dockbook), NULL);

  return dockbook->p->ui_manager;
}

659 660 661 662 663
/**
 * gimp_dockbook_add_from_dialog_factory:
 * @dockbook:    The #DockBook
 * @identifiers: The dockable identifier(s)
 *
664
 * Add a dockable from the dialog factory associated with the dockbook.
665 666 667
 **/
GtkWidget *
gimp_dockbook_add_from_dialog_factory (GimpDockbook *dockbook,
668
                                       const gchar  *identifiers)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
{
  GtkWidget *dockable;
  GimpDock  *dock;
  gchar     *identifier;
  gchar     *p;

  g_return_val_if_fail (GIMP_IS_DOCKBOOK (dockbook), NULL);
  g_return_val_if_fail (identifiers != NULL, NULL);

  identifier = g_strdup (identifiers);

  p = strchr (identifier, '|');

  if (p)
    *p = '\0';

  dock     = gimp_dockbook_get_dock (dockbook);
  dockable = gimp_dialog_factory_dockable_new (gimp_dock_get_dialog_factory (dock),
                                               dock,
                                               identifier, -1);

  g_free (identifier);

  /*  Maybe gimp_dialog_factory_dockable_new() returned an already
   *  existing singleton dockable, so check if it already is
   *  attached to a dockbook.
   */
  if (dockable && ! gimp_dockable_get_dockbook (GIMP_DOCKABLE (dockable)))
697 698
    gtk_notebook_append_page (GTK_NOTEBOOK (dockbook),
                              dockable, NULL);
699 700 701 702

  return dockable;
}

703 704 705 706 707 708 709 710 711 712 713 714
/**
 * gimp_dockbook_update_with_context:
 * @dockbook:
 * @context:
 *
 * Set @context on all dockables in @dockbook.
 **/
void
gimp_dockbook_update_with_context (GimpDockbook *dockbook,
                                   GimpContext  *context)
{
  GList *children = gtk_container_get_children (GTK_CONTAINER (dockbook));
715
  GList *iter;
716

717
  for (iter = children; iter; iter = g_list_next (iter))
718 719 720 721 722 723 724 725 726
    {
      GimpDockable *dockable = GIMP_DOCKABLE (iter->data);

      gimp_dockable_set_context (dockable, context);
    }

  g_list_free (children);
}

727
GtkWidget *
728 729
gimp_dockbook_create_tab_widget (GimpDockbook *dockbook,
                                 GimpDockable *dockable)
730
{
731 732
  GtkWidget      *tab_widget;
  GimpDockWindow *dock_window;
733
  GtkAction      *action = NULL;
734
  GtkIconSize     tab_size = DEFAULT_TAB_ICON_SIZE;
735

736 737 738
  gtk_widget_style_get (GTK_WIDGET (dockbook),
                        "tab-icon-size", &tab_size,
                        NULL);
739
  tab_widget =
740 741 742
    gimp_dockable_create_tab_widget (dockable,
                                     gimp_dock_get_context (dockbook->p->dock),
                                     gimp_dockable_get_tab_style (dockable),
743
                                     tab_size);
744

745
  if (GIMP_IS_VIEW (tab_widget))
746 747 748 749 750 751 752 753 754 755 756
    {
      GtkWidget *event_box;

      event_box = gtk_event_box_new ();
      gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
      gtk_event_box_set_above_child (GTK_EVENT_BOX (event_box), TRUE);
      gtk_container_add (GTK_CONTAINER (event_box), tab_widget);
      gtk_widget_show (tab_widget);

      tab_widget = event_box;
    }
757

Michael Natterer's avatar
Michael Natterer committed
758
  /* EEK */
759
  dock_window = gimp_dock_window_from_dock (dockbook->p->dock);
760 761
  if (dock_window &&
      gimp_dock_container_get_ui_manager (GIMP_DOCK_CONTAINER (dock_window)))
Michael Natterer's avatar
Michael Natterer committed
762 763 764 765 766 767 768 769
    {
      const gchar *dialog_id;

      dialog_id = g_object_get_data (G_OBJECT (dockable),
                                     "gimp-dialog-identifier");

      if (dialog_id)
        {
770 771 772 773
          GimpDockContainer *dock_container;
          GimpActionGroup   *group;

          dock_container = GIMP_DOCK_CONTAINER (dock_window);
Michael Natterer's avatar
Michael Natterer committed
774 775

          group = gimp_ui_manager_get_action_group
776
            (gimp_dock_container_get_ui_manager (dock_container), "dialogs");
Michael Natterer's avatar
Michael Natterer committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

          if (group)
            {
              GList *actions;
              GList *list;

              actions = gtk_action_group_list_actions (GTK_ACTION_GROUP (group));

              for (list = actions; list; list = g_list_next (list))
                {
                  if (GIMP_IS_STRING_ACTION (list->data) &&
                      strstr (GIMP_STRING_ACTION (list->data)->value,
                              dialog_id))
                    {
                      action = list->data;
                      break;
                    }
                }

              g_list_free (actions);
            }
        }
    }

  if (action)
    gimp_widget_set_accel_help (tab_widget, action);
  else
804 805 806
    gimp_help_set_help_data (tab_widget,
                             gimp_dockable_get_blurb (dockable),
                             gimp_dockable_get_help_id (dockable));
807 808 809 810

  return tab_widget;
}

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
/**
 * gimp_dockable_set_drag_handler:
 * @dockable:
 * @handler:
 *
 * Set a drag handler that will be asked if it will handle drag events
 * before the dockbook handles the event itself.
 **/
void
gimp_dockbook_set_drag_handler (GimpDockbook *dockbook,
                                GimpPanedBox *drag_handler)
{
  g_return_if_fail (GIMP_IS_DOCKBOOK (dockbook));

  dockbook->p->drag_handler = drag_handler;
}

828

829
/*  private functions  */
830

831 832 833 834 835
static void
gimp_dockbook_tab_locked_notify (GimpDockable *dockable,
                                 GParamSpec   *pspec,
                                 GimpDockbook *dockbook)
{
836
  gboolean locked = gimp_dockable_get_locked (dockable);
837

838 839 840 841
  gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (dockbook),
                                    GTK_WIDGET (dockable), ! locked);
  gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (dockbook),
                                   GTK_WIDGET (dockable), ! locked);
842 843
}

844 845 846 847
static void
gimp_dockbook_help_func (const gchar *help_id,
                         gpointer     help_data)
{
848
  GimpDockbook *dockbook = GIMP_DOCKBOOK (help_data);
849 850 851 852 853 854 855 856
  GtkWidget    *dockable;
  gint          page_num;

  page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (dockbook));

  dockable = gtk_notebook_get_nth_page (GTK_NOTEBOOK (dockbook), page_num);

  if (GIMP_IS_DOCKABLE (dockable))
857 858
    gimp_standard_help_func (gimp_dockable_get_help_id (GIMP_DOCKABLE (dockable)),
                             NULL);
859 860 861
  else
    gimp_standard_help_func (GIMP_HELP_DOCK, NULL);
}