gimpdockwindow.c 45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpdockwindow.c
 * Copyright (C) 2001-2005 Michael Natterer <mitch@gimp.org>
 * Copyright (C)      2009 Martin Nordholts <martinn@src.gnome.org>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

24 25
#include <gegl.h>

26 27 28 29 30 31
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

32 33
#include "dialogs/dialogs.h" /* FIXME, we are in the widget layer */

34 35
#include "menus/menus.h"

36 37 38
#include "config/gimpguiconfig.h"

#include "core/gimp.h"
39
#include "core/gimpcontext.h"
40
#include "core/gimpcontainer.h"
41
#include "core/gimpcontainer.h"
42 43
#include "core/gimplist.h"
#include "core/gimpimage.h"
44

45 46
#include "gimpcontainercombobox.h"
#include "gimpcontainerview.h"
47
#include "gimpdialogfactory.h"
48 49
#include "gimpdock.h"
#include "gimpdockbook.h"
50
#include "gimpdockcolumns.h"
51
#include "gimpdockcontainer.h"
52
#include "gimpdockwindow.h"
53
#include "gimphelp-ids.h"
54
#include "gimpmenufactory.h"
55
#include "gimpsessioninfo-aux.h"
56
#include "gimpsessioninfo.h"
57
#include "gimpsessionmanaged.h"
58
#include "gimptoolbox.h"
59
#include "gimpuimanager.h"
60
#include "gimpwidgets-utils.h"
61 62 63 64 65
#include "gimpwindow.h"

#include "gimp-intl.h"


66 67 68 69 70 71
#define DEFAULT_DOCK_HEIGHT          300
#define DEFAULT_MENU_VIEW_SIZE       GTK_ICON_SIZE_SMALL_TOOLBAR
#define AUX_INFO_SHOW_IMAGE_MENU     "show-image-menu"
#define AUX_INFO_FOLLOW_ACTIVE_IMAGE "follow-active-image"


72 73 74 75
enum
{
  PROP_0,
  PROP_CONTEXT,
76
  PROP_DIALOG_FACTORY,
77
  PROP_UI_MANAGER_NAME,
78 79
  PROP_IMAGE_CONTAINER,
  PROP_DISPLAY_CONTAINER,
80
  PROP_ALLOW_DOCKBOOK_ABSENCE
81 82 83
};


84 85
struct _GimpDockWindowPrivate
{
86
  GimpContext       *context;
87

88 89 90 91 92
  GimpDialogFactory *dialog_factory;

  gchar             *ui_manager_name;
  GimpUIManager     *ui_manager;
  GQuark             image_flush_handler_id;
93

94 95
  GimpDockColumns   *dock_columns;

96 97
  gboolean           allow_dockbook_absence;

98 99
  guint              update_title_idle_id;

100
  gint               ID;
101 102 103 104 105 106 107 108 109

  GimpContainer     *image_container;
  GimpContainer     *display_container;

  gboolean           show_image_menu;
  gboolean           auto_follow_active;

  GtkWidget         *image_combo;
  GtkWidget         *auto_button;
110 111
};

112

113
static void            gimp_dock_window_dock_container_iface_init (GimpDockContainerInterface *iface);
114
static void            gimp_dock_window_session_managed_iface_init(GimpSessionManagedInterface*iface);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
static void            gimp_dock_window_constructed               (GObject                    *object);
static void            gimp_dock_window_dispose                   (GObject                    *object);
static void            gimp_dock_window_finalize                  (GObject                    *object);
static void            gimp_dock_window_set_property              (GObject                    *object,
                                                                   guint                       property_id,
                                                                   const GValue               *value,
                                                                   GParamSpec                 *pspec);
static void            gimp_dock_window_get_property              (GObject                    *object,
                                                                   guint                       property_id,
                                                                   GValue                     *value,
                                                                   GParamSpec                 *pspec);
static void            gimp_dock_window_style_set                 (GtkWidget                  *widget,
                                                                   GtkStyle                   *prev_style);
static gboolean        gimp_dock_window_delete_event              (GtkWidget                  *widget,
                                                                   GdkEventAny                *event);
static GList         * gimp_dock_window_get_docks                 (GimpDockContainer          *dock_container);
static GimpUIManager * gimp_dock_window_get_ui_manager            (GimpDockContainer          *dock_container);
static void            gimp_dock_window_add_dock_from_session     (GimpDockContainer          *dock_container,
                                                                   GimpDock                   *dock,
                                                                   GimpSessionInfoDock        *dock_info);
135 136 137
static GList         * gimp_dock_window_get_aux_info              (GimpSessionManaged         *session_managed);
static void            gimp_dock_window_set_aux_info              (GimpSessionManaged         *session_managed,
                                                                   GList                      *aux_info);
138 139 140
static GimpAlignmentType
                       gimp_dock_window_get_dock_side             (GimpDockContainer          *dock_container,
                                                                   GimpDock                   *dock);
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
static gboolean        gimp_dock_window_should_add_to_recent      (GimpDockWindow             *dock_window);
static void            gimp_dock_window_display_changed           (GimpDockWindow             *dock_window,
                                                                   GimpObject                 *display,
                                                                   GimpContext                *context);
static void            gimp_dock_window_image_changed             (GimpDockWindow             *dock_window,
                                                                   GimpImage                  *image,
                                                                   GimpContext                *context);
static void            gimp_dock_window_image_flush               (GimpImage                  *image,
                                                                   gboolean                    invalidate_preview,
                                                                   GimpDockWindow             *dock_window);
static void            gimp_dock_window_update_title              (GimpDockWindow             *dock_window);
static gboolean        gimp_dock_window_update_title_idle         (GimpDockWindow             *dock_window);
static gchar         * gimp_dock_window_get_description           (GimpDockWindow             *dock_window,
                                                                   gboolean                    complete);
static void            gimp_dock_window_dock_removed              (GimpDockWindow             *dock_window,
                                                                   GimpDock                   *dock,
                                                                   GimpDockColumns            *dock_columns);
static void            gimp_dock_window_factory_display_changed   (GimpContext                *context,
                                                                   GimpObject                 *display,
                                                                   GimpDock                   *dock);
static void            gimp_dock_window_factory_image_changed     (GimpContext                *context,
                                                                   GimpImage                  *image,
                                                                   GimpDock                   *dock);
static void            gimp_dock_window_auto_clicked              (GtkWidget                  *widget,
                                                                   GimpDock                   *dock);
166

167

168 169
G_DEFINE_TYPE_WITH_CODE (GimpDockWindow, gimp_dock_window, GIMP_TYPE_WINDOW,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCK_CONTAINER,
170 171 172
                                                gimp_dock_window_dock_container_iface_init)
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_SESSION_MANAGED,
                                                gimp_dock_window_session_managed_iface_init))
173 174 175 176 177 178

#define parent_class gimp_dock_window_parent_class

static void
gimp_dock_window_class_init (GimpDockWindowClass *klass)
{
179 180
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
181

182
  object_class->constructed  = gimp_dock_window_constructed;
183
  object_class->dispose      = gimp_dock_window_dispose;
184
  object_class->finalize     = gimp_dock_window_finalize;
185 186
  object_class->set_property = gimp_dock_window_set_property;
  object_class->get_property = gimp_dock_window_get_property;
187

188 189
  widget_class->style_set    = gimp_dock_window_style_set;
  widget_class->delete_event = gimp_dock_window_delete_event;
190

191
  g_object_class_install_property (object_class, PROP_CONTEXT,
192
                                   g_param_spec_object ("context", NULL, NULL,
193 194 195 196
                                                        GIMP_TYPE_CONTEXT,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

197
  g_object_class_install_property (object_class, PROP_DIALOG_FACTORY,
198
                                   g_param_spec_object ("dialog-factory",
199 200 201 202 203
                                                        NULL, NULL,
                                                        GIMP_TYPE_DIALOG_FACTORY,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

204 205 206 207 208 209 210
  g_object_class_install_property (object_class, PROP_UI_MANAGER_NAME,
                                   g_param_spec_string ("ui-manager-name",
                                                        NULL, NULL,
                                                        NULL,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

211 212 213 214 215 216 217 218 219 220 221 222 223 224
  g_object_class_install_property (object_class, PROP_IMAGE_CONTAINER,
                                   g_param_spec_object ("image-container",
                                                        NULL, NULL,
                                                        GIMP_TYPE_CONTAINER,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_DISPLAY_CONTAINER,
                                   g_param_spec_object ("display-container",
                                                        NULL, NULL,
                                                        GIMP_TYPE_CONTAINER,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

225 226 227 228 229 230 231
  g_object_class_install_property (object_class, PROP_ALLOW_DOCKBOOK_ABSENCE,
                                   g_param_spec_boolean ("allow-dockbook-absence",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));

232

233 234 235 236 237 238 239
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("default-height",
                                                             NULL, NULL,
                                                             -1, G_MAXINT,
                                                             DEFAULT_DOCK_HEIGHT,
                                                             GIMP_PARAM_READABLE));

240 241 242 243 244 245 246
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_enum ("menu-preview-size",
                                                              NULL, NULL,
                                                              GTK_TYPE_ICON_SIZE,
                                                              DEFAULT_MENU_VIEW_SIZE,
                                                              GIMP_PARAM_READABLE));

247 248 249 250 251 252
  g_type_class_add_private (klass, sizeof (GimpDockWindowPrivate));
}

static void
gimp_dock_window_init (GimpDockWindow *dock_window)
{
253
  static gint  dock_window_ID = 1;
254
  gchar       *name           = NULL;
255 256

  /* Initialize members */
257 258 259
  dock_window->p = G_TYPE_INSTANCE_GET_PRIVATE (dock_window,
                                                GIMP_TYPE_DOCK_WINDOW,
                                                GimpDockWindowPrivate);
260 261
  dock_window->p->ID                 = dock_window_ID++;
  dock_window->p->auto_follow_active = TRUE;
262 263 264 265 266

  /* Initialize theming and style-setting stuff */
  name = g_strdup_printf ("gimp-dock-%d", dock_window->p->ID);
  gtk_widget_set_name (GTK_WIDGET (dock_window), name);
  g_free (name);
267

268 269 270
  /* Misc */
  gtk_window_set_resizable (GTK_WINDOW (dock_window), TRUE);
  gtk_window_set_focus_on_map (GTK_WINDOW (dock_window), FALSE);
271 272
}

273 274 275
static void
gimp_dock_window_dock_container_iface_init (GimpDockContainerInterface *iface)
{
276 277
  iface->get_docks      = gimp_dock_window_get_docks;
  iface->get_ui_manager = gimp_dock_window_get_ui_manager;
278
  iface->add_dock       = gimp_dock_window_add_dock_from_session;
279
  iface->get_dock_side  = gimp_dock_window_get_dock_side;
280 281
}

282 283 284 285 286 287 288
static void
gimp_dock_window_session_managed_iface_init (GimpSessionManagedInterface *iface)
{
  iface->get_aux_info = gimp_dock_window_get_aux_info;
  iface->set_aux_info = gimp_dock_window_set_aux_info;
}

289 290
static void
gimp_dock_window_constructed (GObject *object)
291
{
292 293
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (object);
  GimpGuiConfig  *config;
294
  GimpContext    *factory_context;
295 296 297
  GtkAccelGroup  *accel_group;
  Gimp           *gimp;
  GtkSettings    *settings;
298 299
  gint            menu_view_width  = -1;
  gint            menu_view_height = -1;
300

301
  G_OBJECT_CLASS (parent_class)->constructed (object);
302 303 304

  gimp   = GIMP (dock_window->p->context->gimp);
  config = GIMP_GUI_CONFIG (gimp->config);
305 306 307 308 309 310 311 312 313

  /* Create a separate context per dock so that docks can be bound to
   * a specific image and does not necessarily have to follow the
   * active image in the user context
   */
  g_object_unref (dock_window->p->context);
  dock_window->p->context           = gimp_context_new (gimp, "Dock Context", NULL);
  dock_window->p->image_container   = gimp->images;
  dock_window->p->display_container = gimp->displays;
314

315 316 317
  factory_context =
    gimp_dialog_factory_get_context (dock_window->p->dialog_factory);

318
  /* Setup hints */
319 320
  gimp_window_set_hint (GTK_WINDOW (dock_window), config->dock_window_hint);

321 322 323 324
  /* Make image window related keyboard shortcuts work also when a
   * dock window is the focused window
   */
  dock_window->p->ui_manager =
325
    gimp_menu_factory_manager_new (global_menu_factory,
326 327 328
                                   dock_window->p->ui_manager_name,
                                   dock_window,
                                   config->tearoff_menus);
329
  accel_group =
330 331 332 333 334 335 336 337 338 339 340 341 342
    gtk_ui_manager_get_accel_group (GTK_UI_MANAGER (dock_window->p->ui_manager));
  gtk_window_add_accel_group (GTK_WINDOW (dock_window), accel_group);

  g_signal_connect_object (dock_window->p->context, "display-changed",
                           G_CALLBACK (gimp_dock_window_display_changed),
                           dock_window,
                           G_CONNECT_SWAPPED);
  g_signal_connect_object (dock_window->p->context, "image-changed",
                           G_CALLBACK (gimp_dock_window_image_changed),
                           dock_window,
                           G_CONNECT_SWAPPED);

  dock_window->p->image_flush_handler_id =
343
    gimp_container_add_handler (gimp->images, "flush",
344 345 346
                                G_CALLBACK (gimp_dock_window_image_flush),
                                dock_window);

347 348 349 350 351
  gimp_context_define_properties (dock_window->p->context,
                                  GIMP_CONTEXT_ALL_PROPS_MASK &
                                  ~(GIMP_CONTEXT_IMAGE_MASK |
                                    GIMP_CONTEXT_DISPLAY_MASK),
                                  FALSE);
352
  gimp_context_set_parent (dock_window->p->context,
353
                           factory_context);
354

355 356 357 358 359
  /* Setup widget hierarchy */
  {
    GtkWidget *vbox = NULL;

    /* Top-level GtkVBox */
360
    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
361 362 363 364
    gtk_container_add (GTK_CONTAINER (dock_window), vbox);
    gtk_widget_show (vbox);

    /* Image selection menu */
365
    {
366 367 368
      GtkWidget *hbox = NULL;

      /* GtkHBox */
369
      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
      gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
      if (dock_window->p->show_image_menu)
        gtk_widget_show (hbox);

      /* Image combo */
      dock_window->p->image_combo = gimp_container_combo_box_new (NULL, NULL, 16, 1);
      gtk_box_pack_start (GTK_BOX (hbox), dock_window->p->image_combo, TRUE, TRUE, 0);
      g_signal_connect (dock_window->p->image_combo, "destroy",
                        G_CALLBACK (gtk_widget_destroyed),
                        &dock_window->p->image_combo);
      gimp_help_set_help_data (dock_window->p->image_combo,
                               NULL, GIMP_HELP_DOCK_IMAGE_MENU);
      gtk_widget_show (dock_window->p->image_combo);

      /* Auto button */
      dock_window->p->auto_button = gtk_toggle_button_new_with_label (_("Auto"));
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dock_window->p->auto_button),
                                    dock_window->p->auto_follow_active);
      gtk_box_pack_start (GTK_BOX (hbox), dock_window->p->auto_button, FALSE, FALSE, 0);
      gtk_widget_show (dock_window->p->auto_button);

      g_signal_connect (dock_window->p->auto_button, "clicked",
                        G_CALLBACK (gimp_dock_window_auto_clicked),
                        dock_window);

      gimp_help_set_help_data (dock_window->p->auto_button,
                               _("When enabled the dialog automatically "
                                 "follows the image you are working on."),
                               GIMP_HELP_DOCK_AUTO_BUTTON);
399 400
    }

401 402 403 404 405 406
    /* GimpDockColumns */
    /* Let the GimpDockColumns mirror the context so that a GimpDock can
     * get it when inside a dock window. We do the same thing in the
     * GimpImageWindow so docks can get the GimpContext there as well
     */
    dock_window->p->dock_columns =
407 408 409
      GIMP_DOCK_COLUMNS (gimp_dock_columns_new (dock_window->p->context,
                                                dock_window->p->dialog_factory,
                                                dock_window->p->ui_manager));
410 411 412 413 414 415 416
    gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (dock_window->p->dock_columns),
                        TRUE, TRUE, 0);
    gtk_widget_show (GTK_WIDGET (dock_window->p->dock_columns));
    g_signal_connect_object (dock_window->p->dock_columns, "dock-removed",
                             G_CALLBACK (gimp_dock_window_dock_removed),
                             dock_window,
                             G_CONNECT_SWAPPED);
417 418 419 420 421 422 423 424 425

    g_signal_connect_object (dock_window->p->dock_columns, "dock-added",
                             G_CALLBACK (gimp_dock_window_update_title),
                             dock_window,
                             G_CONNECT_SWAPPED);
    g_signal_connect_object (dock_window->p->dock_columns, "dock-removed",
                             G_CALLBACK (gimp_dock_window_update_title),
                             dock_window,
                             G_CONNECT_SWAPPED);
426 427
  }

428 429
  if (dock_window->p->auto_follow_active)
    {
430 431
      if (gimp_context_get_display (factory_context))
        gimp_context_copy_property (factory_context,
432 433 434
                                    dock_window->p->context,
                                    GIMP_CONTEXT_PROP_DISPLAY);
      else
435
        gimp_context_copy_property (factory_context,
436 437 438 439
                                    dock_window->p->context,
                                    GIMP_CONTEXT_PROP_IMAGE);
    }

440
  g_signal_connect_object (factory_context, "display-changed",
441 442 443
                           G_CALLBACK (gimp_dock_window_factory_display_changed),
                           dock_window,
                           0);
444
  g_signal_connect_object (factory_context, "image-changed",
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
                           G_CALLBACK (gimp_dock_window_factory_image_changed),
                           dock_window,
                           0);

  settings = gtk_widget_get_settings (GTK_WIDGET (dock_window));
  gtk_icon_size_lookup_for_settings (settings,
                                     DEFAULT_MENU_VIEW_SIZE,
                                     &menu_view_width,
                                     &menu_view_height);

  g_object_set (dock_window->p->image_combo,
                "container", dock_window->p->image_container,
                "context",   dock_window->p->context,
                NULL);

  gimp_help_connect (GTK_WIDGET (dock_window), gimp_standard_help_func,
                     GIMP_HELP_DOCK, NULL);

463 464
  if (dock_window->p->auto_follow_active)
    {
465 466
      if (gimp_context_get_display (factory_context))
        gimp_context_copy_property (factory_context,
467 468 469
                                    dock_window->p->context,
                                    GIMP_CONTEXT_PROP_DISPLAY);
      else
470
        gimp_context_copy_property (factory_context,
471 472 473
                                    dock_window->p->context,
                                    GIMP_CONTEXT_PROP_IMAGE);
    }
474 475
}

476 477 478 479 480
static void
gimp_dock_window_dispose (GObject *object)
{
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (object);

481 482 483 484 485 486
  if (dock_window->p->update_title_idle_id)
    {
      g_source_remove (dock_window->p->update_title_idle_id);
      dock_window->p->update_title_idle_id = 0;
    }

487 488 489 490 491 492 493 494 495 496 497 498 499
  if (dock_window->p->image_flush_handler_id)
    {
      gimp_container_remove_handler (dock_window->p->context->gimp->images,
                                     dock_window->p->image_flush_handler_id);
      dock_window->p->image_flush_handler_id = 0;
    }

  if (dock_window->p->ui_manager)
    {
      g_object_unref (dock_window->p->ui_manager);
      dock_window->p->ui_manager = NULL;
    }

500 501 502 503 504 505
  if (dock_window->p->dialog_factory)
    {
      g_object_unref (dock_window->p->dialog_factory);
      dock_window->p->dialog_factory = NULL;
    }

506 507 508 509 510 511 512 513 514
  if (dock_window->p->context)
    {
      g_object_unref (dock_window->p->context);
      dock_window->p->context = NULL;
    }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528
static void
gimp_dock_window_finalize (GObject *object)
{
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (object);

  if (dock_window->p->ui_manager_name)
    {
      g_free (dock_window->p->ui_manager_name);
      dock_window->p->ui_manager_name = NULL;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

529 530 531 532 533 534 535 536 537 538 539 540 541 542
static void
gimp_dock_window_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (object);

  switch (property_id)
    {
    case PROP_CONTEXT:
      dock_window->p->context = g_value_dup_object (value);
      break;

543 544 545 546
    case PROP_DIALOG_FACTORY:
      dock_window->p->dialog_factory = g_value_dup_object (value);
      break;

547 548 549 550 551
    case PROP_UI_MANAGER_NAME:
      g_free (dock_window->p->ui_manager_name);
      dock_window->p->ui_manager_name = g_value_dup_string (value);
      break;

552 553 554 555 556 557 558 559
    case PROP_IMAGE_CONTAINER:
      dock_window->p->image_container = g_value_dup_object (value);
      break;

    case PROP_DISPLAY_CONTAINER:
      dock_window->p->display_container = g_value_dup_object (value);
      break;

560 561 562 563
    case PROP_ALLOW_DOCKBOOK_ABSENCE:
      dock_window->p->allow_dockbook_absence = g_value_get_boolean (value);
      break;

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_dock_window_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (object);

  switch (property_id)
    {
    case PROP_CONTEXT:
      g_value_set_object (value, dock_window->p->context);
      break;

584 585 586 587
    case PROP_DIALOG_FACTORY:
      g_value_set_object (value, dock_window->p->dialog_factory);
      break;

588 589 590 591
    case PROP_UI_MANAGER_NAME:
      g_value_set_string (value, dock_window->p->ui_manager_name);
      break;

592 593 594 595 596 597 598 599
    case PROP_IMAGE_CONTAINER:
      g_value_set_object (value, dock_window->p->image_container);
      break;

    case PROP_DISPLAY_CONTAINER:
      g_value_set_object (value, dock_window->p->display_container);
      break;

600 601 602 603
    case PROP_ALLOW_DOCKBOOK_ABSENCE:
      g_value_set_boolean (value, dock_window->p->allow_dockbook_absence);
      break;

604 605 606 607
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
608
}
609

610 611 612 613
static void
gimp_dock_window_style_set (GtkWidget *widget,
                            GtkStyle  *prev_style)
{
614 615 616 617 618 619 620 621 622 623
  GimpDockWindow *dock_window      = GIMP_DOCK_WINDOW (widget);
  GtkStyle       *button_style;
  GtkIconSize     menu_view_size;
  GtkSettings    *settings;
  gint            menu_view_width  = 18;
  gint            menu_view_height = 18;
  gint            focus_line_width;
  gint            focus_padding;
  gint            ythickness;

624
  gint default_height = DEFAULT_DOCK_HEIGHT;
625 626 627 628 629

  GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);

  gtk_widget_style_get (widget,
                        "default-height", &default_height,
630
                        "menu-preview-size", &menu_view_size,
631 632 633
                        NULL);

  gtk_window_set_default_size (GTK_WINDOW (widget), -1, default_height);
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

  settings = gtk_widget_get_settings (dock_window->p->image_combo);
  gtk_icon_size_lookup_for_settings (settings,
                                     menu_view_size,
                                     &menu_view_width,
                                     &menu_view_height);

  gtk_widget_style_get (dock_window->p->auto_button,
                        "focus-line-width", &focus_line_width,
                        "focus-padding",    &focus_padding,
                        NULL);

  button_style = gtk_widget_get_style (widget);
  ythickness = button_style->ythickness;

  gimp_container_view_set_view_size (GIMP_CONTAINER_VIEW (dock_window->p->image_combo),
                                     menu_view_height, 1);

  gtk_widget_set_size_request (dock_window->p->auto_button, -1,
                               menu_view_height +
                               2 * (1 /* CHILD_SPACING */ +
                                    ythickness            +
                                    focus_padding         +
                                    focus_line_width));
658 659
}

660 661 662 663 664 665 666 667 668 669 670 671
/**
 * gimp_dock_window_delete_event:
 * @widget:
 * @event:
 *
 * Makes sure that when dock windows are closed they are added to the
 * list of recently closed docks so that they are easy to bring back.
 **/
static gboolean
gimp_dock_window_delete_event (GtkWidget   *widget,
                               GdkEventAny *event)
{
672 673 674 675
  GimpDockWindow         *dock_window = GIMP_DOCK_WINDOW (widget);
  GimpSessionInfo        *info        = NULL;
  const gchar            *entry_name  = NULL;
  GimpDialogFactoryEntry *entry       = NULL;
676
  gchar                  *name        = NULL;
677 678 679 680

  /* Don't add docks with just a singe dockable to the list of
   * recently closed dock since those can be brought back through the
   * normal Windows->Dockable Dialogs menu
681
   */
682
  if (! gimp_dock_window_should_add_to_recent (dock_window))
683 684 685 686
    return FALSE;

  info = gimp_session_info_new ();

687 688 689
  name = gimp_dock_window_get_description (dock_window, TRUE /*complete*/);
  gimp_object_set_name (GIMP_OBJECT (info), name);
  g_free (name);
690

691
  gimp_session_info_get_info_with_widget (info, GTK_WIDGET (dock_window));
692

693 694 695
  entry_name = (gimp_dock_window_has_toolbox (dock_window) ?
                "gimp-toolbox-window" :
                "gimp-dock-window");
696
  entry = gimp_dialog_factory_find_entry (gimp_dialog_factory_get_singleton (), entry_name);
697 698
  gimp_session_info_set_factory_entry (info, entry);

699 700 701 702 703 704
  gimp_container_add (global_recent_docks, GIMP_OBJECT (info));
  g_object_unref (info);

  return FALSE;
}

705 706 707 708 709 710 711 712 713
static GList *
gimp_dock_window_get_docks (GimpDockContainer *dock_container)
{
  GimpDockWindow *dock_window;

  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_container), NULL);

  dock_window = GIMP_DOCK_WINDOW (dock_container);

714
  return g_list_copy (gimp_dock_columns_get_docks (dock_window->p->dock_columns));
715 716
}

717 718 719 720 721 722 723 724 725 726 727 728
static GimpUIManager *
gimp_dock_window_get_ui_manager (GimpDockContainer *dock_container)
{
  GimpDockWindow *dock_window;

  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_container), NULL);

  dock_window = GIMP_DOCK_WINDOW (dock_container);

  return dock_window->p->ui_manager;
}

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
static void
gimp_dock_window_add_dock_from_session (GimpDockContainer   *dock_container,
                                        GimpDock            *dock,
                                        GimpSessionInfoDock *dock_info)
{
  GimpDockWindow *dock_window;

  g_return_if_fail (GIMP_IS_DOCK_WINDOW (dock_container));

  dock_window = GIMP_DOCK_WINDOW (dock_container);

  gimp_dock_window_add_dock (dock_window,
                             dock,
                             -1 /*index*/);
}

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 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 804 805 806 807 808 809 810 811 812
static GList *
gimp_dock_window_get_aux_info (GimpSessionManaged *session_managed)
{
  GimpDockWindow     *dock_window;
  GList              *aux_info = NULL;
  GimpSessionInfoAux *aux;

  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (session_managed), NULL);

  dock_window = GIMP_DOCK_WINDOW (session_managed);

  if (dock_window->p->allow_dockbook_absence)
    {
      /* Assume it is the toolbox; it does not have aux info */
      return NULL;
    }

  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_window), NULL);

  aux = gimp_session_info_aux_new (AUX_INFO_SHOW_IMAGE_MENU,
                                   dock_window->p->show_image_menu ?
                                   "true" : "false");
  aux_info = g_list_append (aux_info, aux);

  aux = gimp_session_info_aux_new (AUX_INFO_FOLLOW_ACTIVE_IMAGE,
                                   dock_window->p->auto_follow_active ?
                                   "true" : "false");
  aux_info = g_list_append (aux_info, aux);

  return aux_info;
}

static void
gimp_dock_window_set_aux_info (GimpSessionManaged *session_managed,
                               GList              *aux_info)
{
  GimpDockWindow *dock_window;
  GList          *list;
  gboolean        menu_shown;
  gboolean        auto_follow;

  g_return_if_fail (GIMP_IS_DOCK_WINDOW (session_managed));

  dock_window = GIMP_DOCK_WINDOW (session_managed);
  menu_shown  = dock_window->p->show_image_menu;
  auto_follow = dock_window->p->auto_follow_active;

  for (list = aux_info; list; list = g_list_next (list))
    {
      GimpSessionInfoAux *aux = list->data;

      if (! strcmp (aux->name, AUX_INFO_SHOW_IMAGE_MENU))
        {
          menu_shown = ! g_ascii_strcasecmp (aux->value, "true");
        }
      else if (! strcmp (aux->name, AUX_INFO_FOLLOW_ACTIVE_IMAGE))
        {
          auto_follow = ! g_ascii_strcasecmp (aux->value, "true");
        }
    }

  if (menu_shown != dock_window->p->show_image_menu)
    gimp_dock_window_set_show_image_menu (dock_window, menu_shown);

  if (auto_follow != dock_window->p->auto_follow_active)
    gimp_dock_window_set_auto_follow_active (dock_window, auto_follow);
}

813 814 815 816 817 818 819 820 821 822 823 824 825
static GimpAlignmentType
gimp_dock_window_get_dock_side (GimpDockContainer *dock_container,
                                GimpDock          *dock)
{
  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_container), -1);
  g_return_val_if_fail (GIMP_IS_DOCK (dock), -1);

  /* A GimpDockWindow don't have docks on different sides, it's just
   * one set of columns
   */
  return -1;
}

826 827 828 829 830 831 832 833 834 835 836 837
/**
 * gimp_dock_window_should_add_to_recent:
 * @dock_window:
 *
 * Returns: %FALSE if the dock window can be recreated with one
 *          Windows menu item such as Windows->Toolbox or
 *          Windows->Dockable Dialogs->Layers, %TRUE if not. It should
 *          then be added to the list of recently closed docks.
 **/
static gboolean
gimp_dock_window_should_add_to_recent (GimpDockWindow *dock_window)
{
838
  GList    *docks;
839 840
  gboolean  should_add = TRUE;

841 842
  docks = gimp_dock_container_get_docks (GIMP_DOCK_CONTAINER (dock_window));

843
  if (! docks)
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    {
      should_add = FALSE;
    }
  else if (g_list_length (docks) == 1)
    {
      GimpDock *dock = GIMP_DOCK (g_list_nth_data (docks, 0));

      if (GIMP_IS_TOOLBOX (dock) &&
          gimp_dock_get_n_dockables (dock) == 0)
        {
          should_add = FALSE;
        }
      else if (! GIMP_IS_TOOLBOX (dock) &&
               gimp_dock_get_n_dockables (dock) == 1)
        {
          should_add = FALSE;
        }
    }

863 864
  g_list_free (docks);

865 866 867
  return should_add;
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881
static void
gimp_dock_window_image_flush (GimpImage      *image,
                              gboolean        invalidate_preview,
                              GimpDockWindow *dock_window)
{
  if (image == gimp_context_get_image (dock_window->p->context))
    {
      GimpObject *display = gimp_context_get_display (dock_window->p->context);

      if (display)
        gimp_ui_manager_update (dock_window->p->ui_manager, display);
    }
}

882 883 884 885 886 887 888 889 890 891 892 893 894 895
static void
gimp_dock_window_update_title (GimpDockWindow *dock_window)
{
  if (dock_window->p->update_title_idle_id)
    g_source_remove (dock_window->p->update_title_idle_id);

  dock_window->p->update_title_idle_id =
    g_idle_add ((GSourceFunc) gimp_dock_window_update_title_idle,
                dock_window);
}

static gboolean
gimp_dock_window_update_title_idle (GimpDockWindow *dock_window)
{
896 897
  gchar *desc = gimp_dock_window_get_description (dock_window,
                                                  FALSE /*complete*/);
898 899 900 901 902
  if (desc)
    {
      gtk_window_set_title (GTK_WINDOW (dock_window), desc);
      g_free (desc);
    }
903 904 905 906 907 908 909 910 911 912 913

  dock_window->p->update_title_idle_id = 0;

  return FALSE;
}

static gchar *
gimp_dock_window_get_description (GimpDockWindow *dock_window,
                                  gboolean        complete)
{
  GString *complete_desc = g_string_new (NULL);
914
  GList   *docks         = NULL;
915
  GList   *iter          = NULL;
916

917 918 919
  docks = gimp_dock_container_get_docks (GIMP_DOCK_CONTAINER (dock_window));

  for (iter = docks;
920 921 922
       iter;
       iter = g_list_next (iter))
    {
923 924 925
      gchar *desc = gimp_dock_get_description (GIMP_DOCK (iter->data), complete);
      g_string_append (complete_desc, desc);
      g_free (desc);
926

927
      if (g_list_next (iter))
928
        g_string_append (complete_desc, GIMP_DOCK_COLUMN_SEPARATOR);
929
    }
930

931 932
  g_list_free (docks);

933
  return g_string_free (complete_desc, FALSE /*free_segment*/);
934 935
}

936
static void
937 938 939
gimp_dock_window_dock_removed (GimpDockWindow  *dock_window,
                               GimpDock        *dock,
                               GimpDockColumns *dock_columns)
940 941 942
{
  g_return_if_fail (GIMP_IS_DOCK (dock));

943
  if (gimp_dock_columns_get_docks (dock_columns) == NULL &&
944
      ! dock_window->p->allow_dockbook_absence)
945 946 947
    gtk_widget_destroy (GTK_WIDGET (dock_window));
}

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
static void
gimp_dock_window_factory_display_changed (GimpContext *context,
                                          GimpObject  *display,
                                          GimpDock    *dock)
{
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (dock);

  if (display && dock_window->p->auto_follow_active)
    gimp_context_set_display (dock_window->p->context, display);
}

static void
gimp_dock_window_factory_image_changed (GimpContext *context,
                                        GimpImage   *image,
                                        GimpDock    *dock)
{
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (dock);

  /*  won't do anything if we already set the display above  */
  if (image && dock_window->p->auto_follow_active)
    gimp_context_set_image (dock_window->p->context, image);
}

971 972 973 974 975 976 977 978 979
static void
gimp_dock_window_display_changed (GimpDockWindow *dock_window,
                                  GimpObject     *display,
                                  GimpContext    *context)
{
  gimp_ui_manager_update (dock_window->p->ui_manager,
                          display);
}

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
static void
gimp_dock_window_image_changed (GimpDockWindow *dock_window,
                                GimpImage      *image,
                                GimpContext    *context)
{
  GimpContainer *image_container   = dock_window->p->image_container;
  GimpContainer *display_container = dock_window->p->display_container;

  if (image == NULL && ! gimp_container_is_empty (image_container))
    {
      image = GIMP_IMAGE (gimp_container_get_first_child (image_container));

      /*  this invokes this function recursively but we don't enter
       *  the if() branch the second time
       */
      gimp_context_set_image (context, image);

      /*  stop the emission of the original signal (the emission of
       *  the recursive signal is finished)
       */
      g_signal_stop_emission_by_name (context, "image-changed");
    }
  else if (image != NULL && ! gimp_container_is_empty (display_container))
    {
      GimpObject *display;
      GimpImage  *display_image;
      gboolean    find_display = TRUE;

      display = gimp_context_get_display (context);

      if (display)
        {
          g_object_get (display, "image", &display_image, NULL);

          if (display_image)
            {
              g_object_unref (display_image);

              if (display_image == image)
                find_display = FALSE;
            }
        }

      if (find_display)
        {
          GList *list;

          for (list = GIMP_LIST (display_container)->list;
               list;
               list = g_list_next (list))
            {
              display = GIMP_OBJECT (list->data);

              g_object_get (display, "image", &display_image, NULL);

              if (display_image)
                {
                  g_object_unref (display_image);

                  if (display_image == image)
                    {
                      /*  this invokes this function recursively but we
                       *  don't enter the if(find_display) branch the
                       *  second time
                       */
                      gimp_context_set_display (context, display);

                      /*  don't stop signal emission here because the
                       *  context's image was not changed by the
                       *  recursive call
                       */
                      break;
                    }
                }
            }
        }
    }

  gimp_ui_manager_update (dock_window->p->ui_manager,
                          gimp_context_get_display (context));
}

static void
gimp_dock_window_auto_clicked (GtkWidget *widget,
                               GimpDock  *dock)
{
  GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (dock);

  gimp_toggle_button_update (widget, &dock_window->p->auto_follow_active);

  if (dock_window->p->auto_follow_active)
    {
1072
      gimp_context_copy_properties (gimp_dialog_factory_get_context (dock_window->p->dialog_factory),
1073 1074 1075 1076 1077 1078 1079
                                    dock_window->p->context,
                                    GIMP_CONTEXT_DISPLAY_MASK |
                                    GIMP_CONTEXT_IMAGE_MASK);
    }
}


1080
void
1081 1082 1083
gimp_dock_window_add_dock (GimpDockWindow *dock_window,
                           GimpDock       *dock,
                           gint            index)
1084 1085 1086 1087
{
  g_return_if_fail (GIMP_IS_DOCK_WINDOW (dock_window));
  g_return_if_fail (GIMP_IS_DOCK (dock));

1088 1089 1090
  gimp_dock_columns_add_dock (dock_window->p->dock_columns,
                              GIMP_DOCK (dock),
                              index);
1091

1092
  g_signal_connect_object (dock, "description-invalidated",
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
                           G_CALLBACK (gimp_dock_window_update_title),
                           dock_window,
                           G_CONNECT_SWAPPED);

  /* Some docks like the toolbox dock needs to maintain special hints
   * on its container GtkWindow, allow those to do so
   */
  gimp_dock_set_host_geometry_hints (dock, GTK_WINDOW (dock_window));
  g_signal_connect_object (dock, "geometry-invalidated",
                           G_CALLBACK (gimp_dock_set_host_geometry_hints),
                           dock_window, 0);
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
void
gimp_dock_window_remove_dock (GimpDockWindow *dock_window,
                              GimpDock       *dock)
{
  gimp_dock_columns_remove_dock (dock_window->p->dock_columns,
                                 GIMP_DOCK (dock));

  g_signal_handlers_disconnect_by_func (dock,
                                        gimp_dock_window_update_title,
                                        dock_window);
  g_signal_handlers_disconnect_by_func (dock,
                                        gimp_dock_set_host_geometry_hints,
                                        dock_window);
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
GtkWidget *
gimp_dock_window_new (const gchar       *role,
                      const gchar       *ui_manager_name,
                      gboolean           allow_dockbook_absence,
                      GimpDialogFactory *factory,
                      GimpContext       *context)
{
  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);

  return g_object_new (GIMP_TYPE_DOCK_WINDOW,
                       "role",                   role,
                       "ui-manager-name",        ui_manager_name,
                       "allow-dockbook-absence", allow_dockbook_absence,
                       "dialog-factory",         factory,
                       "context",                context,
                       NULL);
}

1140 1141 1142 1143 1144 1145 1146 1147
gint
gimp_dock_window_get_id (GimpDockWindow *dock_window)
{
  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_window), 0);

  return dock_window->p->ID;
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
GimpContext *
gimp_dock_window_get_context (GimpDockWindow *dock_window)
{
  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_window), NULL);

  return dock_window->p->context;
}

GimpDialogFactory *
gimp_dock_window_get_dialog_factory (GimpDockWindow *dock_window)
{
  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_window), NULL);

  return dock_window->p->dialog_factory;
}

gboolean
gimp_dock_window_get_auto_follow_active (GimpDockWindow *dock_window)
{
  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_window), FALSE);

  return dock_window->p->auto_follow_active;
}

void
gimp_dock_window_set_auto_follow_active (GimpDockWindow *dock_window,
                                         gboolean        auto_follow_active)
{
  g_return_if_fail (GIMP_IS_DOCK_WINDOW (dock_window));

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dock_window->p->auto_button),
                                auto_follow_active ? TRUE : FALSE);
}

gboolean
gimp_dock_window_get_show_image_menu (GimpDockWindow *dock_window)
{
  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_window), FALSE);

  return dock_window->p->show_image_menu;
}

void
gimp_dock_window_set_show_image_menu (GimpDockWindow *dock_window,
                                      gboolean        show)
{
  GtkWidget *parent;

  g_return_if_fail (GIMP_IS_DOCK_WINDOW (dock_window));

  parent = gtk_widget_get_parent (dock_window->p->image_combo);

  gtk_widget_set_visible (parent, show);

  dock_window->p->show_image_menu = show ? TRUE : FALSE;
}

void
gimp_dock_window_setup (GimpDockWindow *dock_window,
                        GimpDockWindow *template)
{
  gimp_dock_window_set_auto_follow_active (GIMP_DOCK_WINDOW (dock_window),
                                           template->p->auto_follow_active);
  gimp_dock_window_set_show_image_menu (GIMP_DOCK_WINDOW (dock_window),
                                        template->p->show_image_menu);
}

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
/**
 * gimp_dock_window_has_toolbox:
 * @dock_window:
 *
 * Returns: %TRUE if the dock window has a GimpToolbox dock, %FALSE
 * otherwise.
 **/
gboolean
gimp_dock_window_has_toolbox (GimpDockWindow *dock_window)
{
  GList *iter = NULL;

  g_return_val_if_fail (GIMP_IS_DOCK_WINDOW (dock_window), FALSE);

  for (iter = gimp_dock_columns_get_docks (dock_window->p->dock_columns);
       iter;
       iter = g_list_next (iter))
    {
      if (GIMP_IS_TOOLBOX (iter->data))
        return TRUE;
    }

  return FALSE;
}

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263

/**
 * gimp_dock_window_from_dock:
 * @dock:
 *
 * For convenience.
 *
 * Returns: If the toplevel widget for the dock is a GimpDockWindow,
 * return that. Otherwise return %NULL.
 **/
GimpDockWindow *
gimp_dock_window_from_dock (GimpDock *dock)
{
  GtkWidget *toplevel = NULL;

  g_return_val_if_fail (GIMP_IS_DOCK (dock), NULL);

  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (dock));

  if (GIMP_IS_DOCK_WINDOW (toplevel))
    return GIMP_DOCK_WINDOW (toplevel);
  else
    return NULL;
}