gimpdialogfactory.c 39.4 KB
Newer Older
1 2 3
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
Michael Natterer's avatar
Michael Natterer committed
4
 * gimpdialogfactory.c
5
 * Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
Michael Natterer's avatar
Michael Natterer committed
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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"

24 25
#include <stdlib.h>
#include <string.h>
Hans Breuer's avatar
Hans Breuer committed
26

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

#include "libgimpwidgets/gimpwidgets.h"

31
#include "widgets-types.h"
32

33 34
#include "core/gimpcontext.h"

Michael Natterer's avatar
Michael Natterer committed
35
#include "gimpcursor.h"
36
#include "gimpdialogfactory.h"
37
#include "gimpdock.h"
38 39
#include "gimpdockbook.h"
#include "gimpdockable.h"
40
#include "gimpmenufactory.h"
41
#include "gimpsessioninfo.h"
42

43

Michael Natterer's avatar
Michael Natterer committed
44 45 46
/* #define DEBUG_FACTORY */

#ifdef DEBUG_FACTORY
47
#define D(stmnt) stmnt
Michael Natterer's avatar
Michael Natterer committed
48
#else
49
#define D(stmnt)
Michael Natterer's avatar
Michael Natterer committed
50 51 52
#endif


Michael Natterer's avatar
Michael Natterer committed
53 54 55 56 57 58 59 60
typedef enum
{
  GIMP_DIALOG_SHOW_ALL,
  GIMP_DIALOG_HIDE_ALL,
  GIMP_DIALOG_SHOW_TOOLBOX
} GimpDialogShowState;


61 62 63
static void   gimp_dialog_factory_class_init (GimpDialogFactoryClass *klass);
static void   gimp_dialog_factory_init       (GimpDialogFactory      *factory);

64 65
static void   gimp_dialog_factory_dispose             (GObject           *object);
static void   gimp_dialog_factory_finalize            (GObject           *object);
Michael Natterer's avatar
Michael Natterer committed
66

67 68 69
static void     gimp_dialog_factory_set_widget_data   (GtkWidget         *dialog,
                                                       GimpDialogFactory *factory,
                                                       GimpDialogFactoryEntry *entry);
70 71 72
static gboolean gimp_dialog_factory_set_user_pos      (GtkWidget         *dialog,
                                                       GdkEventConfigure *cevent,
                                                       gpointer           data);
73 74 75
static gboolean gimp_dialog_factory_dialog_configure  (GtkWidget         *dialog,
                                                       GdkEventConfigure *cevent,
                                                       GimpDialogFactory *factory);
76
static void   gimp_dialog_factories_save_foreach      (gconstpointer      key,
Michael Natterer's avatar
Michael Natterer committed
77
						       GimpDialogFactory *factory,
78
						       GimpConfigWriter  *writer);
79
static void   gimp_dialog_factories_restore_foreach   (gconstpointer      key,
Michael Natterer's avatar
Michael Natterer committed
80 81
						       GimpDialogFactory *factory,
						       gpointer           data);
82
static void   gimp_dialog_factories_clear_foreach     (gconstpointer      key,
83 84
						       GimpDialogFactory *factory,
						       gpointer           data);
85
static void   gimp_dialog_factories_hide_foreach      (gconstpointer      key,
Michael Natterer's avatar
Michael Natterer committed
86 87
						       GimpDialogFactory *factory,
						       gpointer           data);
88
static void   gimp_dialog_factories_show_foreach      (gconstpointer      key,
Michael Natterer's avatar
Michael Natterer committed
89 90
						       GimpDialogFactory *factory,
						       gpointer           data);
91
static void   gimp_dialog_factories_set_busy_foreach  (gconstpointer      key,
Michael Natterer's avatar
Michael Natterer committed
92 93
						       GimpDialogFactory *factory,
						       gpointer           data);
94
static void   gimp_dialog_factories_unset_busy_foreach(gconstpointer      key,
Michael Natterer's avatar
Michael Natterer committed
95 96
						       GimpDialogFactory *factory,
						       gpointer           data);
97

98 99 100 101

static GimpObjectClass *parent_class = NULL;


102
GType
103 104
gimp_dialog_factory_get_type (void)
{
105
  static GType factory_type = 0;
106 107 108

  if (! factory_type)
    {
109
      static const GTypeInfo factory_info =
110
      {
111 112 113 114 115 116 117 118 119
        sizeof (GimpDialogFactoryClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) gimp_dialog_factory_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GimpDialogFactory),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_dialog_factory_init,
120 121
      };

122 123 124
      factory_type = g_type_register_static (GIMP_TYPE_OBJECT,
					     "GimpDialogFactory",
					     &factory_info, 0);
125 126 127 128 129 130 131 132
    }

  return factory_type;
}

static void
gimp_dialog_factory_class_init (GimpDialogFactoryClass *klass)
{
133
  GObjectClass *object_class;
134

135
  object_class = G_OBJECT_CLASS (klass);
136

137
  parent_class = g_type_class_peek_parent (klass);
138

139
  object_class->dispose  = gimp_dialog_factory_dispose;
140
  object_class->finalize = gimp_dialog_factory_finalize;
141 142

  klass->factories = g_hash_table_new (g_str_hash, g_str_equal);
143 144 145 146 147
}

static void
gimp_dialog_factory_init (GimpDialogFactory *factory)
{
148
  factory->menu_factory       = NULL;
Michael Natterer's avatar
Michael Natterer committed
149
  factory->new_dock_func      = NULL;
150
  factory->registered_dialogs = NULL;
Michael Natterer's avatar
Michael Natterer committed
151
  factory->session_infos      = NULL;
152
  factory->open_dialogs       = NULL;
153 154
}

155 156 157 158 159 160 161 162
static void
gimp_dialog_factory_dispose (GObject *object)
{
  GimpDialogFactory *factory;
  GList             *list;

  factory = GIMP_DIALOG_FACTORY (object);

163 164 165 166
  /*  start iterating from the beginning each time we destroyed a
   *  toplevel because destroying a dock may cause lots of items
   *  to be removed from factory->open_dialogs
   */
167 168 169 170 171 172 173 174 175 176 177
  while (factory->open_dialogs)
    {
      for (list = factory->open_dialogs; list; list = g_list_next (list))
        {
          if (GTK_WIDGET_TOPLEVEL (list->data))
            {
              gtk_widget_destroy (GTK_WIDGET (list->data));
              break;
            }
        }

178 179 180
      /*  the list being non-empty without any toplevel is an error,
       *  so eek and chain up
       */
181 182
      if (! list)
        {
183
          g_warning ("%s: stale non-toplevel entries in factory->open_dialogs",
184
                     G_STRFUNC);
185 186 187 188 189 190 191
          break;
        }
    }

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

192
static void
193
gimp_dialog_factory_finalize (GObject *object)
194 195 196 197 198 199
{
  GimpDialogFactory *factory;
  GList             *list;

  factory = GIMP_DIALOG_FACTORY (object);

200
  g_hash_table_remove (GIMP_DIALOG_FACTORY_GET_CLASS (object)->factories,
201 202
		       GIMP_OBJECT (factory)->name);

203 204 205 206 207 208 209 210 211 212
  for (list = factory->registered_dialogs; list; list = g_list_next (list))
    {
      GimpDialogFactoryEntry *entry;

      entry = (GimpDialogFactoryEntry *) list->data;

      g_free (entry->identifier);
      g_free (entry);
    }

213 214 215 216 217 218 219 220 221 222
  if (factory->registered_dialogs)
    {
      g_list_free (factory->registered_dialogs);
      factory->registered_dialogs = NULL;
    }
  if (factory->open_dialogs)
    {
      g_list_free (factory->open_dialogs);
      factory->open_dialogs = NULL;
    }
223

224
  G_OBJECT_CLASS (parent_class)->finalize (object);
225 226 227
}

GimpDialogFactory *
Michael Natterer's avatar
Michael Natterer committed
228 229
gimp_dialog_factory_new (const gchar       *name,
			 GimpContext       *context,
230
			 GimpMenuFactory   *menu_factory,
Michael Natterer's avatar
Michael Natterer committed
231
			 GimpDialogNewFunc  new_dock_func)
232
{
233 234
  GimpDialogFactory *factory;
  gpointer           key;
235 236

  g_return_val_if_fail (name != NULL, NULL);
237
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
238
  g_return_val_if_fail (! menu_factory || GIMP_IS_MENU_FACTORY (menu_factory),
239 240
			NULL);

241
  if (gimp_dialog_factory_from_name (name))
242
    {
243
      g_warning ("%s: dialog factory \"%s\" already exists",
244
		 G_STRFUNC, name);
245 246
      return NULL;
    }
247

Michael Natterer's avatar
Michael Natterer committed
248
  factory = g_object_new (GIMP_TYPE_DIALOG_FACTORY, NULL);
249

250 251
  gimp_object_set_name (GIMP_OBJECT (factory), name);

252 253 254 255 256 257 258 259
  /*  hack to keep the toolbox on the pool position  */
  if (strcmp (name, "toolbox") == 0)
    key = "";
  else
    key = GIMP_OBJECT (factory)->name;

  g_hash_table_insert (GIMP_DIALOG_FACTORY_GET_CLASS (factory)->factories,
		       key, factory);
260

Michael Natterer's avatar
Michael Natterer committed
261
  factory->context       = context;
262
  factory->menu_factory  = menu_factory;
Michael Natterer's avatar
Michael Natterer committed
263
  factory->new_dock_func = new_dock_func;
264 265 266 267

  return factory;
}

268 269 270 271 272 273 274 275
GimpDialogFactory *
gimp_dialog_factory_from_name (const gchar *name)
{
  GimpDialogFactoryClass *factory_class;
  GimpDialogFactory      *factory;

  g_return_val_if_fail (name != NULL, NULL);

276
  factory_class = g_type_class_peek (GIMP_TYPE_DIALOG_FACTORY);
277 278
  if (! factory_class)
    return NULL;
279

280 281 282
  /*  hack to keep the toolbox on the pool position  */
  if (strcmp (name, "toolbox") == 0)
    name = "";
283

284 285
  factory =
    (GimpDialogFactory *) g_hash_table_lookup (factory_class->factories, name);
286 287 288 289

  return factory;
}

290
void
291 292 293
gimp_dialog_factory_register_entry (GimpDialogFactory *factory,
				    const gchar       *identifier,
				    GimpDialogNewFunc  new_func,
294
                                    gint               preview_size,
295 296
				    gboolean           singleton,
				    gboolean           session_managed,
297 298
				    gboolean           remember_size,
				    gboolean           remember_if_open)
299 300 301 302 303 304 305 306
{
  GimpDialogFactoryEntry *entry;

  g_return_if_fail (GIMP_IS_DIALOG_FACTORY (factory));
  g_return_if_fail (identifier != NULL);

  entry = g_new0 (GimpDialogFactoryEntry, 1);

307 308
  entry->identifier       = g_strdup (identifier);
  entry->new_func         = new_func;
309
  entry->preview_size     = preview_size;
310 311 312 313
  entry->singleton        = singleton ? TRUE : FALSE;
  entry->session_managed  = session_managed ? TRUE : FALSE;
  entry->remember_size    = remember_size ? TRUE : FALSE;
  entry->remember_if_open = remember_if_open ? TRUE : FALSE;
314 315 316 317 318

  factory->registered_dialogs = g_list_prepend (factory->registered_dialogs,
						entry);
}

319 320
GimpDialogFactoryEntry *
gimp_dialog_factory_find_entry (GimpDialogFactory *factory,
321 322 323 324 325 326 327 328 329 330 331 332 333 334
				const gchar       *identifier)
{
  GList *list;

  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
  g_return_val_if_fail (identifier != NULL, NULL);

  for (list = factory->registered_dialogs; list; list = g_list_next (list))
    {
      GimpDialogFactoryEntry *entry;

      entry = (GimpDialogFactoryEntry *) list->data;

      if (! strcmp (identifier, entry->identifier))
335
        return entry;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    }

  return NULL;
}

GimpSessionInfo *
gimp_dialog_factory_find_session_info (GimpDialogFactory *factory,
				       const gchar       *identifier)
{
  GList *list;

  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
  g_return_val_if_fail (identifier != NULL, NULL);

  for (list = factory->session_infos; list; list = g_list_next (list))
    {
      GimpSessionInfo *info;

      info = (GimpSessionInfo *) list->data;

Michael Natterer's avatar
Michael Natterer committed
356 357 358 359
      if ((info->toplevel_entry &&
	   ! strcmp (identifier, info->toplevel_entry->identifier)) ||
	  (info->dockable_entry &&
	   ! strcmp (identifier, info->dockable_entry->identifier)))
360 361 362 363 364 365 366 367
	{
	  return info;
	}
    }

  return NULL;
}

368 369
static GtkWidget *
gimp_dialog_factory_dialog_new_internal (GimpDialogFactory *factory,
370
                                         GdkScreen         *screen,
371
					 GimpContext       *context,
Michael Natterer's avatar
Michael Natterer committed
372
					 const gchar       *identifier,
373
                                         gint               preview_size,
Michael Natterer's avatar
Michael Natterer committed
374
					 gboolean           raise_if_found)
375 376 377 378 379 380 381 382 383 384 385
{
  GimpDialogFactoryEntry *entry;
  GtkWidget              *dialog = NULL;

  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
  g_return_val_if_fail (identifier != NULL, NULL);

  entry = gimp_dialog_factory_find_entry (factory, identifier);

  if (! entry)
    {
386
      g_warning ("%s: no entry registered for \"%s\"",
387
		 G_STRFUNC, identifier);
388 389 390 391 392
      return NULL;
    }

  if (! entry->new_func)
    {
393
      g_warning ("%s: entry for \"%s\" has no constructor",
394
		 G_STRFUNC, identifier);
395 396 397
      return NULL;
    }

398
  /*  a singleton dialog is always only raised if it already exisits  */
Michael Natterer's avatar
Michael Natterer committed
399
  if (raise_if_found || entry->singleton)
400 401 402 403 404 405 406 407
    {
      GimpSessionInfo *info;

      info = gimp_dialog_factory_find_session_info (factory, identifier);

      if (info)
	dialog = info->widget;
    }
408

409
  /*  create the dialog if it was not found  */
410 411
  if (! dialog)
    {
Michael Natterer's avatar
Michael Natterer committed
412 413
      GtkWidget *dock = NULL;

414 415 416 417 418 419
      /*  If the dialog will be a dockable (factory->new_dock_func) and
       *  we are called from gimp_dialog_factory_dialog_raise() (! context),
       *  create a new dock _before_ creating the dialog.
       *  We do this because the new dockable needs to be created in it's
       *  dock's context.
       */
Michael Natterer's avatar
Michael Natterer committed
420 421
      if (factory->new_dock_func && ! context)
	{
422 423
          GtkWidget *dockbook;

424
	  dock     = gimp_dialog_factory_dock_new (factory, screen);
425
          dockbook = gimp_dockbook_new (factory->menu_factory);
Michael Natterer's avatar
Michael Natterer committed
426 427

	  gimp_dock_add_book (GIMP_DOCK (dock),
428 429
			      GIMP_DOCKBOOK (dockbook),
                              0);
Michael Natterer's avatar
Michael Natterer committed
430 431
	}

432 433 434 435 436 437
      /*  Create the new dialog in the appropriate context which is
       *  - the passed context if not NULL
       *  - the newly created dock's context if we just created it
       *  - the factory's context, which happens when raising a toplevel
       *    dialog was the original request.
       */
438
      if (preview_size < GIMP_PREVIEW_SIZE_TINY)
439 440
        preview_size = entry->preview_size;

Michael Natterer's avatar
Michael Natterer committed
441
      if (context)
442 443 444
	dialog = entry->new_func (factory,
                                  context,
                                  preview_size);
Michael Natterer's avatar
Michael Natterer committed
445
      else if (dock)
446 447 448
	dialog = entry->new_func (factory,
                                  GIMP_DOCK (dock)->context,
                                  preview_size);
Michael Natterer's avatar
Michael Natterer committed
449
      else
450 451 452
	dialog = entry->new_func (factory,
                                  factory->context,
                                  preview_size);
453

454 455
      if (dialog)
	{
456
          gimp_dialog_factory_set_widget_data (dialog, factory, entry);
457

458 459 460
	  /*  If we created a dock before, the newly created dialog is
	   *  supposed to be a GimpDockable.
	   */
Michael Natterer's avatar
Michael Natterer committed
461
	  if (dock)
462
	    {
463 464 465 466 467 468 469 470 471
	      if (GIMP_IS_DOCKABLE (dialog))
		{
		  gimp_dock_add (GIMP_DOCK (dock), GIMP_DOCKABLE (dialog),
				 0, 0);

		  gtk_widget_show (dock);
		}
	      else
		{
472
		  g_warning ("%s: GimpDialogFactory is a dockable factory "
473 474
			     "but constructor for \"%s\" did not return a "
			     "GimpDockable",
475
			     G_STRFUNC, identifier);
476 477

		  gtk_widget_destroy (dialog);
478
                  gtk_widget_destroy (dock);
Michael Natterer's avatar
Michael Natterer committed
479

480
		  dialog = NULL;
481
                  dock   = NULL;
482
		}
483
	    }
484
	}
Michael Natterer's avatar
Michael Natterer committed
485 486
      else if (dock)
	{
487
	  g_warning ("%s: constructor for \"%s\" returned NULL",
488
		     G_STRFUNC, identifier);
489

Michael Natterer's avatar
Michael Natterer committed
490
	  gtk_widget_destroy (dock);
491 492

	  dock = NULL;
Michael Natterer's avatar
Michael Natterer committed
493
	}
494 495 496

      if (dialog)
	gimp_dialog_factory_add_dialog (factory, dialog);
497 498
    }

499 500 501
  /*  finally, if we found an existing dialog or created a new one,
   *  raise it
   */
Michael Natterer's avatar
Michael Natterer committed
502
  if (dialog)
503
    {
Michael Natterer's avatar
Michael Natterer committed
504 505
      if (GTK_WIDGET_TOPLEVEL (dialog))
	{
506
          gtk_window_set_screen (GTK_WINDOW (dialog), screen);
507
          gtk_window_present (GTK_WINDOW (dialog));
Michael Natterer's avatar
Michael Natterer committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
	}
      else if (GIMP_IS_DOCKABLE (dialog))
	{
	  GimpDockable *dockable;
	  gint          page_num;

	  dockable = GIMP_DOCKABLE (dialog);

	  if (dockable->dockbook && dockable->dockbook->dock)
	    {
	      page_num =
		gtk_notebook_page_num (GTK_NOTEBOOK (dockable->dockbook),
				       dialog);

	      if (page_num != -1)
		{
		  GtkWidget *toplevel;

526 527
		  gtk_notebook_set_current_page (GTK_NOTEBOOK (dockable->dockbook),
                                                 page_num);
Michael Natterer's avatar
Michael Natterer committed
528 529 530

		  toplevel = gtk_widget_get_toplevel (dialog);

531
                  gtk_window_present (GTK_WINDOW (toplevel));
Michael Natterer's avatar
Michael Natterer committed
532 533 534
		}
	    }
	}
535 536 537
    }

  return dialog;
538
}
539

540 541
/**
 * gimp_dialog_factory_dialog_new:
542 543 544 545
 * @factory:      a #GimpDialogFactory
 * @screen:       the #GdkScreen the dialog should appear on
 * @identifier:   the identifier of the dialog as registered with
 *                gimp_dialog_factory_register_entry()
546 547
 * @preview_size:
 *
548 549 550 551
 * Creates a new toplevel dialog or a #GimpDockable, depending on whether
 * %factory is a toplevel of dockable factory.
 *
 * Implicitly raises singleton dialogs.
552
 *
553 554 555
 * Return value: the newly created dialog or an already existing singleton
 *               dialog.
 **/
556 557
GtkWidget *
gimp_dialog_factory_dialog_new (GimpDialogFactory *factory,
558
                                GdkScreen         *screen,
559 560
				const gchar       *identifier,
                                gint               preview_size)
561 562
{
  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
563
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
564 565 566
  g_return_val_if_fail (identifier != NULL, NULL);

  return gimp_dialog_factory_dialog_new_internal (factory,
567
                                                  screen,
568
						  factory->context,
Michael Natterer's avatar
Michael Natterer committed
569
						  identifier,
570
                                                  preview_size,
Michael Natterer's avatar
Michael Natterer committed
571 572 573
						  FALSE);
}

574 575
/**
 * gimp_dialog_factory_dialog_raise:
576 577 578 579
 * @factory:      a #GimpDialogFactory
 * @screen:       the #GdkScreen the dialog should appear on
 * @identifiers:  a '|' separated list of identifiers of dialogs as
 *                registered with gimp_dialog_factory_register_entry()
580
 * @preview_size:
581
 *
582 583
 * Raises any of a list of already existing toplevel dialog or
 * #GimpDockable if it was already created by this %facory.
584
 *
585 586
 * Implicitly creates the first dialog in the list if none of the dialogs
 * were found.
587
 *
588 589
 * Return value: the raised or newly created dialog.
 **/
Michael Natterer's avatar
Michael Natterer committed
590 591
GtkWidget *
gimp_dialog_factory_dialog_raise (GimpDialogFactory *factory,
592
                                  GdkScreen         *screen,
593
				  const gchar       *identifiers,
594
                                  gint               preview_size)
Michael Natterer's avatar
Michael Natterer committed
595
{
596 597
  GtkWidget *dialog;

Michael Natterer's avatar
Michael Natterer committed
598
  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
599
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
600
  g_return_val_if_fail (identifiers != NULL, NULL);
Michael Natterer's avatar
Michael Natterer committed
601

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
  /*  If the identifier is a list, try to find a matching dialog and
   *  raise it. If there's no match, use the first list item.
   */
  if (strchr (identifiers, '|'))
    {
      gchar **ids = g_strsplit (identifiers, "|", 0);
      gint    i;

      for (i = 0; ids[i]; i++)
        {
          GimpSessionInfo *info;

          info = gimp_dialog_factory_find_session_info (factory, ids[i]);
          if (info && info->widget)
            break;
        }

      dialog = gimp_dialog_factory_dialog_new_internal (factory,
                                                        screen,
                                                        NULL,
                                                        ids[i] ? ids[i] : ids[0],
                                                        preview_size,
                                                        TRUE);
      g_strfreev (ids);
    }
  else
    {
      dialog = gimp_dialog_factory_dialog_new_internal (factory,
                                                        screen,
                                                        NULL,
                                                        identifiers,
                                                        preview_size,
                                                        TRUE);
    }

  return dialog;
638 639
}

640 641
/**
 * gimp_dialog_factory_dockable_new:
642 643 644 645
 * @factory:      a #GimpDialogFactory
 * @dock:         a #GimpDock crated by this %factory.
 * @identifier:   the identifier of the dialog as registered with
 *                gimp_dialog_factory_register_entry()
Sven Neumann's avatar
Sven Neumann committed
646
 * @preview_size:
647
 *
648 649 650 651 652 653
 * Creates a new #GimpDockable in the context of the #GimpDock it will be
 * added to.
 *
 * Implicitly raises & returns an already existing singleton dockable,
 * so callers should check that dockable->dockbook is NULL before trying
 * to add it to it's #GimpDockbook.
654
 *
655 656 657
 * Return value: the newly created #GimpDockable or an already existing
 *               singleton dockable.
 **/
658 659 660
GtkWidget *
gimp_dialog_factory_dockable_new (GimpDialogFactory *factory,
				  GimpDock          *dock,
661 662
				  const gchar       *identifier,
                                  gint               preview_size)
663 664 665 666 667 668
{
  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
  g_return_val_if_fail (GIMP_IS_DOCK (dock), NULL);
  g_return_val_if_fail (identifier != NULL, NULL);

  return gimp_dialog_factory_dialog_new_internal (factory,
669
                                                  gtk_widget_get_screen (GTK_WIDGET (dock)),
670
						  dock->context,
Michael Natterer's avatar
Michael Natterer committed
671
						  identifier,
672
                                                  preview_size,
Michael Natterer's avatar
Michael Natterer committed
673
						  FALSE);
674 675
}

676 677 678
/**
 * gimp_dialog_factory_dock_new:
 * @factory: a #GimpDialogFacotry
679
 * @screen:  the #GdkScreen the dock should appear on
680
 *
681 682 683 684
 * Returns a new #GimpDock in this %factory's context. We use a function
 * pointer passed to this %factory's constructor instead of simply
 * gimp_dock_new() because we may want different instances of
 * #GimpDialogFactory create different subclasses of #GimpDock.
685
 *
686 687
 * Return value: the newly created #GimpDock.
 **/
Michael Natterer's avatar
Michael Natterer committed
688
GtkWidget *
689 690
gimp_dialog_factory_dock_new (GimpDialogFactory *factory,
                              GdkScreen         *screen)
Michael Natterer's avatar
Michael Natterer committed
691 692 693 694
{
  GtkWidget *dock;

  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
695
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
Michael Natterer's avatar
Michael Natterer committed
696 697
  g_return_val_if_fail (factory->new_dock_func != NULL, NULL);

698
  dock = factory->new_dock_func (factory, factory->context, 0);
Michael Natterer's avatar
Michael Natterer committed
699 700

  if (dock)
701
    {
702 703
      gtk_window_set_screen (GTK_WINDOW (dock), screen);

704
      gimp_dialog_factory_set_widget_data (dock, factory, NULL);
705 706 707

      gimp_dialog_factory_add_dialog (factory, dock);
    }
Michael Natterer's avatar
Michael Natterer committed
708 709 710 711

  return dock;
}

712
void
Michael Natterer's avatar
Michael Natterer committed
713 714
gimp_dialog_factory_add_dialog (GimpDialogFactory *factory,
				GtkWidget         *dialog)
715
{
Michael Natterer's avatar
Michael Natterer committed
716
  GimpDialogFactory      *dialog_factory;
717 718 719
  GimpDialogFactoryEntry *entry;
  GimpSessionInfo        *info;
  GList                  *list;
720
  gboolean                toplevel;
721

722
  g_return_if_fail (GIMP_IS_DIALOG_FACTORY (factory));
Michael Natterer's avatar
Michael Natterer committed
723
  g_return_if_fail (GTK_IS_WIDGET (dialog));
724

Michael Natterer's avatar
Michael Natterer committed
725
  if (g_list_find (factory->open_dialogs, dialog))
726
    {
727
      g_warning ("%s: dialog already registered", G_STRFUNC);
728 729 730
      return;
    }

731
  dialog_factory = gimp_dialog_factory_from_widget (dialog, &entry);
732

733
  if (! (dialog_factory && (entry || GIMP_IS_DOCK (dialog))))
734
    {
735
      g_warning ("%s: dialog was not created by a GimpDialogFactory",
736
		 G_STRFUNC);
737 738 739 740
      return;
    }

  if (dialog_factory != factory)
741
    {
742
      g_warning ("%s: dialog was created by a different GimpDialogFactory",
743
		 G_STRFUNC);
744 745 746
      return;
    }

747 748
  toplevel = GTK_WIDGET_TOPLEVEL (dialog);

749
  if (entry) /* dialog is a toplevel (but not a GimpDock) or a GimpDockable */
750
    {
751
      D (g_print ("%s: adding %s \"%s\"\n",
752
                  G_STRFUNC,
753 754
                  toplevel ? "toplevel" : "dockable",
                  entry->identifier));
755

756 757 758 759
      for (list = factory->session_infos; list; list = g_list_next (list))
	{
	  info = (GimpSessionInfo *) list->data;

760
	  if ((info->toplevel_entry == entry) ||
Michael Natterer's avatar
Michael Natterer committed
761
	      (info->dockable_entry == entry))
762
	    {
763
	      if (info->widget)
764
		{
765
		  if (entry->singleton)
766
		    {
767
		      g_warning ("%s: singleton dialog \"%s\" created twice",
768
				 G_STRFUNC, entry->identifier);
769

770
                      D (g_print ("%s: corrupt session info: %p (widget %p)\n",
771
                                  G_STRFUNC,
772
                                  info, info->widget));
773

774 775
		      return;
		    }
776

777 778 779
		  continue;
		}

780 781
              info->widget = dialog;

782
              D (g_print ("%s: updating session info %p (widget %p) for %s \"%s\"\n",
783
                          G_STRFUNC,
784 785 786
                          info, info->widget,
                          toplevel ? "toplevel" : "dockable",
                          entry->identifier));
787

788
	      if (toplevel && entry->session_managed)
789
                gimp_session_info_set_geometry (info);
790

791 792 793 794 795 796 797 798
	      break;
	    }
	}

      if (! list) /*  didn't find a session info  */
	{
	  info = g_new0 (GimpSessionInfo, 1);

Michael Natterer's avatar
Michael Natterer committed
799 800
	  info->widget = dialog;

801
          D (g_print  ("%s: creating session info %p (widget %p) for %s \"%s\"\n",
802
                       G_STRFUNC,
803 804 805
                       info, info->widget,
                       toplevel ? "toplevel" : "dockable",
                       entry->identifier));
806

Michael Natterer's avatar
Michael Natterer committed
807
	  if (toplevel)
808 809 810 811
            {
              info->toplevel_entry = entry;

              /*  if we create a new session info, we never call
812 813
               *  gimp_session_info_set_geometry(), but still the
               *  dialog needs GDK_HINT_USER_POS so it keeps its
814 815 816 817 818 819 820
               *  position when hidden/shown within this(!) session.
               */
              if (entry->session_managed)
                g_signal_connect (dialog, "configure_event",
                                  G_CALLBACK (gimp_dialog_factory_set_user_pos),
                                  NULL);
            }
Michael Natterer's avatar
Michael Natterer committed
821
	  else
822 823 824
            {
              info->dockable_entry = entry;
            }
825 826 827 828

	  factory->session_infos = g_list_append (factory->session_infos, info);
	}
    }
829
  else /*  dialog is a GimpDock  */
830
    {
831
      D (g_print ("%s: adding dock\n", G_STRFUNC));
832

833 834 835 836
      for (list = factory->session_infos; list; list = g_list_next (list))
	{
	  info = (GimpSessionInfo *) list->data;

837 838 839 840
          /*  take the first empty slot  */
          if (! info->toplevel_entry &&
              ! info->dockable_entry &&
              ! info->widget)
841
	    {
Michael Natterer's avatar
Michael Natterer committed
842
	      info->widget = dialog;
843

844
              D (g_print ("%s: updating session info %p (widget %p) for dock\n",
845
                          G_STRFUNC,
846
                          info, info->widget));
847

848
	      gimp_session_info_set_geometry (info);
849 850 851 852 853 854 855 856 857

	      break;
	    }
	}

      if (! list) /*  didn't find a session info  */
	{
	  info = g_new0 (GimpSessionInfo, 1);

Michael Natterer's avatar
Michael Natterer committed
858
	  info->widget = dialog;
859

860
          D (g_print ("%s: creating session info %p (widget %p) for dock\n",
861
                      G_STRFUNC,
862
                      info, info->widget));
863

864 865 866 867 868 869 870 871 872
          /*  if we create a new session info, we never call
           *  gimp_session_info_set_geometry(), but still the
           *  dialog needs GDK_HINT_USER_POS so it keeps its
           *  position when hidden/shown within this(!) session.
           */
          g_signal_connect (dialog, "configure_event",
                            G_CALLBACK (gimp_dialog_factory_set_user_pos),
                            NULL);

873 874 875 876
	  factory->session_infos = g_list_append (factory->session_infos, info);
	}
    }

Michael Natterer's avatar
Michael Natterer committed
877
  factory->open_dialogs = g_list_prepend (factory->open_dialogs, dialog);
878

879
  g_signal_connect_object (dialog, "destroy",
880
                           G_CALLBACK (gimp_dialog_factory_remove_dialog),
881 882
                           factory,
                           G_CONNECT_SWAPPED);
883

884 885 886 887 888
  if (entry && entry->session_managed && toplevel)
    g_signal_connect_object (dialog, "configure_event",
                             G_CALLBACK (gimp_dialog_factory_dialog_configure),
                             factory,
                             0);
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
}

void
gimp_dialog_factory_add_foreign (GimpDialogFactory *factory,
                                 const gchar       *identifier,
                                 GtkWidget         *dialog)
{
  GimpDialogFactory      *dialog_factory;
  GimpDialogFactoryEntry *entry;

  g_return_if_fail (GIMP_IS_DIALOG_FACTORY (factory));
  g_return_if_fail (identifier != NULL);
  g_return_if_fail (GTK_IS_WIDGET (dialog));
  g_return_if_fail (GTK_WIDGET_TOPLEVEL (dialog));

904
  dialog_factory = gimp_dialog_factory_from_widget (dialog, &entry);
905 906 907 908

  if (dialog_factory || entry)
    {
      g_warning ("%s: dialog was created by a GimpDialogFactory",
909
		 G_STRFUNC);
910 911 912 913 914 915 916 917
      return;
    }

  entry = gimp_dialog_factory_find_entry (factory, identifier);

  if (! entry)
    {
      g_warning ("%s: no entry registered for \"%s\"",
918
		 G_STRFUNC, identifier);
919 920 921 922 923 924
      return;
    }

  if (entry->new_func)
    {
      g_warning ("%s: entry for \"%s\" has a constructor (is not foreign)",
925
		 G_STRFUNC, identifier);
926 927 928
      return;
    }

929
  gimp_dialog_factory_set_widget_data (dialog, factory, entry);
930 931

  gimp_dialog_factory_add_dialog (factory, dialog);
932 933 934
}

void
Michael Natterer's avatar
Michael Natterer committed
935 936
gimp_dialog_factory_remove_dialog (GimpDialogFactory *factory,
				   GtkWidget         *dialog)
937
{
938 939 940 941
  GimpDialogFactory      *dialog_factory;
  GimpDialogFactoryEntry *entry;
  GimpSessionInfo        *session_info;
  GList                  *list;
942

943
  g_return_if_fail (GIMP_IS_DIALOG_FACTORY (factory));
Michael Natterer's avatar
Michael Natterer committed
944
  g_return_if_fail (GTK_IS_WIDGET (dialog));
945

Michael Natterer's avatar
Michael Natterer committed
946
  if (! g_list_find (factory->open_dialogs, dialog))
947
    {
948
      g_warning ("%s: dialog not registered", G_STRFUNC);
949 950 951
      return;
    }

Michael Natterer's avatar
Michael Natterer committed
952
  factory->open_dialogs = g_list_remove (factory->open_dialogs, dialog);
953

954
  dialog_factory = gimp_dialog_factory_from_widget (dialog, &entry);
955

956
  if (! (dialog_factory && (entry || GIMP_IS_DOCK (dialog))))
957
    {
958
      g_warning ("%s: dialog was not created by a GimpDialogFactory",
959
		 G_STRFUNC);
960 961 962 963
      return;
    }

  if (dialog_factory != factory)
964
    {
965
      g_warning ("%s: dialog was created by a different GimpDialogFactory",
966
		 G_STRFUNC);
967 968 969
      return;
    }

970
  D (g_print ("%s: removing \"%s\"\n",
971
              G_STRFUNC,
972
              entry ? entry->identifier : "dock"));
973

974 975 976 977
  for (list = factory->session_infos; list; list = g_list_next (list))
    {
      session_info = (GimpSessionInfo *) list->data;

Michael Natterer's avatar
Michael Natterer committed
978
      if (session_info->widget == dialog)
979
	{
980
          D (g_print ("%s: clearing session info %p (widget %p) for \"%s\"\n",
981
                      G_STRFUNC,
982 983
                      session_info, session_info->widget,
                      entry ? entry->identifier : "dock"));
984

985 986
	  session_info->widget = NULL;

Michael Natterer's avatar
Michael Natterer committed
987 988 989 990 991 992 993 994 995
	  /*  don't save session info for empty docks  */
	  if (GIMP_IS_DOCK (dialog))
	    {
	      factory->session_infos = g_list_remove (factory->session_infos,
						      session_info);

	      g_free (session_info);
	    }

996 997 998 999 1000
	  break;
	}
    }
}

Michael Natterer's avatar
Michael Natterer committed
1001
void
1002
gimp_dialog_factories_session_save (GimpConfigWriter *writer)
Michael Natterer's avatar
Michael Natterer committed
1003 1004 1005
{
  GimpDialogFactoryClass *factory_class;

1006
  g_return_if_fail (writer != NULL);
Michael Natterer's avatar
Michael Natterer committed
1007

1008
  factory_class = g_type_class_peek (GIMP_TYPE_DIALOG_FACTORY);
Michael Natterer's avatar
Michael Natterer committed
1009 1010 1011

  g_hash_table_foreach (factory_class->factories,
			(GHFunc) gimp_dialog_factories_save_foreach,
1012
			writer);
Michael Natterer's avatar
Michael Natterer committed
1013 1014 1015 1016 1017 1018 1019
}

void
gimp_dialog_factories_session_restore (void)
{
  GimpDialogFactoryClass *factory_class;

1020
  factory_class = g_type_class_peek (GIMP_TYPE_DIALOG_FACTORY);
Michael Natterer's avatar
Michael Natterer committed
1021 1022 1023 1024 1025 1026

  g_hash_table_foreach (factory_class->factories,
			(GHFunc) gimp_dialog_factories_restore_foreach,
			NULL);
}

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
void
gimp_dialog_factories_session_clear (void)
{
  GimpDialogFactoryClass *factory_class;

  factory_class = g_type_class_peek (GIMP_TYPE_DIALOG_FACTORY);

  g_hash_table_foreach (factory_class->factories,
			(GHFunc) gimp_dialog_factories_clear_foreach,
			NULL);
}

Michael Natterer's avatar
Michael Natterer committed
1039
void
1040
gimp_dialog_factories_toggle (GimpDialogFactory *toolbox_factory)
Michael Natterer's avatar
Michael Natterer committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
{
  static GimpDialogShowState toggle_state = GIMP_DIALOG_SHOW_ALL;
  static gboolean            doing_update = FALSE;

  GimpDialogFactoryClass *factory_class;

  if (doing_update)
    return;

  doing_update = TRUE;

1052
  factory_class = g_type_class_peek (GIMP_TYPE_DIALOG_FACTORY);
Michael Natterer's avatar
Michael Natterer committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

  switch (toggle_state)
    {
    case GIMP_DIALOG_SHOW_ALL:
      toggle_state = GIMP_DIALOG_HIDE_ALL;

      g_hash_table_foreach (factory_class->factories,
			    (GHFunc) gimp_dialog_factories_hide_foreach,
			    NULL);
      break;

    case GIMP_DIALOG_HIDE_ALL:
      toggle_state = GIMP_DIALOG_SHOW_TOOLBOX;

1067 1068 1069
      gimp_dialog_factories_show_foreach (GIMP_OBJECT (toolbox_factory)->name,
                                          toolbox_factory,
                                          NULL);
Michael Natterer's avatar
Michael Natterer committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
      break;

    case GIMP_DIALOG_SHOW_TOOLBOX:
      toggle_state = GIMP_DIALOG_SHOW_ALL;

      g_hash_table_foreach (factory_class->factories,
			    (GHFunc) gimp_dialog_factories_show_foreach,
			    NULL);
    default:
      break;
    }

  doing_update = FALSE;
}

void
1086
gimp_dialog_factories_set_busy (void)
Michael Natterer's avatar
Michael Natterer committed
1087 1088 1089
{
  GimpDialogFactoryClass *factory_class;

1090
  factory_class = g_type_class_peek (GIMP_TYPE_DIALOG_FACTORY);
Michael Natterer's avatar
Michael Natterer committed
1091 1092

  g_hash_table_foreach (factory_class->factories,
1093
			(GHFunc) gimp_dialog_factories_set_busy_foreach,
Michael Natterer's avatar
Michael Natterer committed
1094 1095 1096 1097
			NULL);
}

void
1098
gimp_dialog_factories_unset_busy (void)
Michael Natterer's avatar
Michael Natterer committed
1099 1100 1101
{
  GimpDialogFactoryClass *factory_class;