gimpitemfactory.c 26 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20
#include "config.h"

21
#include "string.h"
22

23 24 25 26 27
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED

Sven Neumann's avatar
Sven Neumann committed
28
#include <gtk/gtk.h>
29
#include <gdk/gdkkeysyms.h>
30

31
#include "libgimpbase/gimpbase.h"
32 33
#include "libgimpwidgets/gimpwidgets.h"

34 35
#include "widgets-types.h"

36 37
#include "core/gimp.h"

38
#include "gimpitemfactory.h"
39
#include "gimpwidgets-utils.h"
40

41
#include "gimphelp.h"
Elliot Lee's avatar
Elliot Lee committed
42
#include "gimprc.h"
43

44 45 46
#include "libgimp/gimpintl.h"


47 48
/*  local function prototypes  */

49 50 51 52 53 54
static void     gimp_item_factory_class_init      (GimpItemFactoryClass *klass);
static void     gimp_item_factory_init            (GimpItemFactory      *factory);

static void     gimp_item_factory_finalize        (GObject              *object);

static void     gimp_item_factory_create_branches (GimpItemFactory      *factory,
55 56 57 58 59 60 61 62 63 64 65 66
                                                   GimpItemFactoryEntry *entry);
static void     gimp_item_factory_item_realize    (GtkWidget            *widget,
                                                   gpointer              data);
static gboolean gimp_item_factory_item_key_press  (GtkWidget            *widget,
                                                   GdkEventKey          *kevent,
                                                   gpointer              data);
#ifdef ENABLE_NLS
static gchar *  gimp_item_factory_translate_func  (const gchar          *path,
                                                   gpointer              data);
#else
#define         gimp_item_factory_translate_func  (NULL)
#endif
67

68

69
static GtkItemFactoryClass *parent_class = NULL;
70

71

72 73 74 75
GType
gimp_item_factory_get_type (void)
{
  static GType factory_type = 0;
76

77
  if (! factory_type)
78
    {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      static const GTypeInfo factory_info =
      {
        sizeof (GimpItemFactoryClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) gimp_item_factory_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GimpItemFactory),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_item_factory_init,
      };

      factory_type = g_type_register_static (GTK_TYPE_ITEM_FACTORY,
					     "GimpItemFactory",
					     &factory_info, 0);
95
    }
96

97
  return factory_type;
98
}
99

100 101
static void
gimp_item_factory_class_init (GimpItemFactoryClass *klass)
102
{
103
  GObjectClass *object_class;
104

105
  object_class = G_OBJECT_CLASS (klass);
106

107
  parent_class = g_type_class_peek_parent (klass);
108

109
  object_class->finalize = gimp_item_factory_finalize;
110

111
  klass->factories = g_hash_table_new (g_str_hash, g_str_equal);
Elliot Lee's avatar
Elliot Lee committed
112 113
}

114 115
static void
gimp_item_factory_init (GimpItemFactory *factory)
Elliot Lee's avatar
Elliot Lee committed
116
{
117
  factory->factory_path = NULL;
118 119
}

120 121
static void
gimp_item_factory_finalize (GObject *object)
122
{
123
  GimpItemFactory *factory;
Elliot Lee's avatar
Elliot Lee committed
124

125
  factory = GIMP_ITEM_FACTORY (object);
126

127
  if (factory->factory_path)
128
    {
129 130
      g_hash_table_remove (GIMP_ITEM_FACTORY_GET_CLASS (object)->factories,
                           factory->factory_path);
131

132 133
      g_free (factory->factory_path);
      factory->factory_path = NULL;
134
    }
135

136
  G_OBJECT_CLASS (parent_class)->finalize (object);
137 138 139
}


140 141 142
/*  public functions  */

GimpItemFactory *
143 144
gimp_item_factory_new (Gimp                      *gimp,
                       GType                      container_type,
145 146 147 148 149 150 151
                       const gchar               *path,
                       const gchar               *factory_path,
                       GimpItemFactoryUpdateFunc  update_func,
                       guint                      n_entries,
                       GimpItemFactoryEntry      *entries,
                       gpointer                   callback_data,
                       gboolean                   create_tearoff)
152
{
153 154 155
  GimpItemFactoryClass *factory_class;
  GimpItemFactory      *factory;

156
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
157 158
  g_return_val_if_fail (path != NULL, NULL);
  g_return_val_if_fail (factory_path != NULL, NULL);
159

160 161 162 163 164 165 166 167 168 169
  factory_class = g_type_class_ref (GIMP_TYPE_ITEM_FACTORY);

  if (g_hash_table_lookup (factory_class->factories, path))
    {
      g_warning ("%s: item factory with path \"%s\" already exists",
                 G_STRLOC, path);

      g_type_class_unref (factory_class);
      return NULL;
    }
170

171 172 173 174 175 176 177 178
  factory = g_object_new (GIMP_TYPE_ITEM_FACTORY, NULL);

  gtk_item_factory_construct (GTK_ITEM_FACTORY (factory),
                              container_type,
                              path,
                              NULL);

  gtk_item_factory_set_translate_func (GTK_ITEM_FACTORY (factory),
179 180 181 182
				       gimp_item_factory_translate_func,
				       (gpointer) path,
				       NULL);

183 184
  factory->gimp = gimp;

185 186 187 188 189 190 191 192 193
  /*  this is correct!  <mitch>  */
  factory->factory_path = g_strdup (path);
  factory->update_func  = update_func;

  g_hash_table_insert (factory_class->factories,
                       factory->factory_path,
                       factory);

  /*  this will go away  <mitch>  */
194 195 196 197 198 199 200 201 202 203
  g_object_set_data (G_OBJECT (factory), "factory_path",
                     (gpointer) factory_path);

  gimp_item_factory_create_items (factory,
                                  n_entries,
                                  entries,
                                  callback_data,
                                  2,
                                  create_tearoff,
                                  TRUE);
204

205 206
  g_type_class_unref (factory_class);

207 208 209
  return factory;
}

210 211
GimpItemFactory *
gimp_item_factory_from_path (const gchar *path)
212
{
213 214 215 216
  GimpItemFactoryClass *factory_class;
  GimpItemFactory      *factory;
  gchar                *base_path;
  gchar                *p;
217

218
  g_return_val_if_fail (path != NULL, NULL);
219

220
  base_path = g_strdup (path);
221

222
  p = strchr (base_path, '>');
223

224 225
  if (p)
    p[1] = '\0';
226

227 228 229 230 231 232 233 234 235
  factory_class = g_type_class_ref (GIMP_TYPE_ITEM_FACTORY);

  factory = g_hash_table_lookup (factory_class->factories, base_path);

  g_type_class_unref (factory_class);

  g_free (base_path);

  return factory;
236 237
}

238
void
239
gimp_item_factory_create_item (GimpItemFactory       *item_factory,
240 241 242 243 244 245 246 247
                               GimpItemFactoryEntry  *entry,
                               gpointer               callback_data,
                               guint                  callback_type,
                               gboolean               create_tearoff,
                               gboolean               static_entry)
{
  GtkWidget *menu_item;

248
  g_return_if_fail (GIMP_IS_ITEM_FACTORY (item_factory));
249 250 251
  g_return_if_fail (entry != NULL);

  if (! (strstr (entry->entry.path, "tearoff1")))
252
    {
253 254 255 256
      if (! gimprc.disable_tearoff_menus && create_tearoff)
	{
	  gimp_item_factory_create_branches (item_factory, entry);
	}
257
    }
258
  else if (gimprc.disable_tearoff_menus || ! create_tearoff)
259
    {
260
      return;
261
    }
262

263
  if (entry->quark_string)
264
    {
265 266 267 268 269 270 271 272
      GQuark quark;

      if (static_entry)
	quark = g_quark_from_static_string (entry->quark_string);
      else
	quark = g_quark_from_string (entry->quark_string);

      entry->entry.callback_action = (guint) quark;
273 274
    }

275
  gtk_item_factory_create_item (GTK_ITEM_FACTORY (item_factory),
276 277 278 279
				(GtkItemFactoryEntry *) entry,
				callback_data,
				callback_type);

280
  menu_item = gtk_item_factory_get_item (GTK_ITEM_FACTORY (item_factory),
281 282 283
					 ((GtkItemFactoryEntry *) entry)->path);

  if (menu_item)
284
    {
285 286 287 288
      g_signal_connect_after (G_OBJECT (menu_item), "realize",
			      G_CALLBACK (gimp_item_factory_item_realize),
			      item_factory);

289 290 291 292 293 294 295 296 297 298
      if (entry->help_page)
        {
          if (static_entry)
            g_object_set_data (G_OBJECT (menu_item), "help_page",
                               (gpointer) entry->help_page);
          else
            g_object_set_data_full (G_OBJECT (menu_item), "help_page",
                                    g_strdup (entry->help_page),
                                    g_free);
        }
299
    }
300
}
301

302
void
303
gimp_item_factory_create_items (GimpItemFactory      *item_factory,
304 305 306 307 308 309 310 311 312 313
                                guint                 n_entries,
                                GimpItemFactoryEntry *entries,
                                gpointer              callback_data,
                                guint                 callback_type,
                                gboolean              create_tearoff,
                                gboolean              static_entries)
{
  gint i;

  for (i = 0; i < n_entries; i++)
314
    {
315 316 317 318 319 320
      gimp_item_factory_create_item (item_factory,
                                     entries + i,
                                     callback_data,
                                     callback_type,
                                     create_tearoff,
                                     static_entries);
321
    }
322
}
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
void
gimp_item_factory_popup_with_data (GimpItemFactory  *item_factory,
				   gpointer          data,
                                   GtkDestroyNotify  popdown_func)
{
  gint    x, y;
  guint   button;
  guint32 activate_time;

  g_return_if_fail (GIMP_IS_ITEM_FACTORY (item_factory));

  if (item_factory->update_func)
    item_factory->update_func (GTK_ITEM_FACTORY (item_factory), data);

  gimp_menu_position (GTK_MENU (GTK_ITEM_FACTORY (item_factory)->widget),
		      &x, &y,
		      &button,
		      &activate_time);

  gtk_item_factory_popup_with_data (GTK_ITEM_FACTORY (item_factory),
				    data,
				    popdown_func,
				    x, y,
				    button,
				    activate_time);
}

351 352 353 354 355 356 357 358 359 360 361 362 363
void
gimp_item_factory_set_active (GtkItemFactory *factory,
                              gchar          *path,
                              gboolean        active)
{
  GtkWidget *widget;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);

  widget = gtk_item_factory_get_widget (factory, path);

  if (widget)
364
    {
365 366 367 368 369 370
      if (GTK_IS_CHECK_MENU_ITEM (widget))
        {
          gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), active);
        }
      else
        {
371 372 373 374 375
          g_warning ("%s: Unable to set \"active\" for menu item "
                     "of type \"%s\": %s",
                     G_STRLOC,
                     g_type_name (G_TYPE_FROM_INSTANCE (widget)),
                     path);
376
        }
377
    }
378
  else if (! strstr (path, "Script-Fu"))
379
    {
380
      g_warning ("%s: Unable to set \"active\" for menu item "
381
                 "which doesn't exist: %s",
382
                 G_STRLOC, path);
383
    }
384 385
}

386
void
387 388 389 390
gimp_item_factory_set_color (GtkItemFactory *factory,
                             gchar          *path,
                             const GimpRGB  *color,
                             gboolean        set_label)
391
{
392 393 394
  GtkWidget *widget;
  GtkWidget *preview = NULL;
  GtkWidget *label   = NULL;
395

396 397 398
  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);
  g_return_if_fail (color != NULL);
399

400
  widget = gtk_item_factory_get_widget (factory, path);
401

402
  if (! widget)
403
    {
404
      g_warning ("%s: Unable to set color of menu item "
405
                 "which doesn't exist: %s",
406 407 408
                 G_STRLOC, path);
      return;
    }
409

410 411
#define COLOR_BOX_WIDTH  16
#define COLOR_BOX_HEIGHT 16
412

413 414 415 416 417 418
  if (GTK_IS_HBOX (GTK_BIN (widget)->child))
    {
      preview = g_object_get_data (G_OBJECT (GTK_BIN (widget)->child),
                                   "preview");
      label = g_object_get_data (G_OBJECT (GTK_BIN (widget)->child),
                                 "label");
419
    }
420
  else if (GTK_IS_LABEL (GTK_BIN (widget)->child))
421
    {
422
      GtkWidget *hbox;
423

424
      label = GTK_BIN (widget)->child;
425

426
      g_object_ref (G_OBJECT (label));
427

428
      gtk_container_remove (GTK_CONTAINER (widget), label);
429

430 431 432 433 434 435 436 437 438 439
      hbox = gtk_hbox_new (FALSE, 4);
      gtk_container_add (GTK_CONTAINER (widget), hbox);
      gtk_widget_show (hbox);

      preview = gtk_preview_new (GTK_PREVIEW_COLOR);
      gtk_preview_size (GTK_PREVIEW (preview),
                        COLOR_BOX_WIDTH, COLOR_BOX_HEIGHT);
      gtk_box_pack_start (GTK_BOX (hbox), preview, FALSE, FALSE, 0);
      gtk_widget_show (preview);

440
      gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
441 442 443 444 445 446
      gtk_widget_show (label);

      g_object_unref (G_OBJECT (label));

      g_object_set_data (G_OBJECT (hbox), "preview", preview);
      g_object_set_data (G_OBJECT (hbox), "label",   label);
447 448
    }

449
  if (preview)
450
    {
451 452 453 454 455 456 457
      guchar  rows[3][COLOR_BOX_WIDTH * 3];
      gint    x, y;
      gint    r0, g0, b0;
      gint    r1, g1, b1;
      guchar *p0, *p1, *p2;

      /* Fill rows */
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472
      r0 = (GIMP_CHECK_DARK + (color->r - GIMP_CHECK_DARK) * color->a) * 255.0;
      r1 = (GIMP_CHECK_LIGHT + (color->r - GIMP_CHECK_LIGHT) * color->a) * 255.0;

      g0 = (GIMP_CHECK_DARK + (color->g - GIMP_CHECK_DARK) * color->a) * 255.0;
      g1 = (GIMP_CHECK_LIGHT + (color->g - GIMP_CHECK_LIGHT) * color->a) * 255.0;

      b0 = (GIMP_CHECK_DARK + (color->b - GIMP_CHECK_DARK) * color->a) * 255.0;
      b1 = (GIMP_CHECK_LIGHT + (color->b - GIMP_CHECK_LIGHT) * color->a) * 255.0;

      p0 = rows[0];
      p1 = rows[1];
      p2 = rows[2];

      for (x = 0; x < COLOR_BOX_WIDTH; x++)
473
        {
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
          if ((x == 0) || (x == (COLOR_BOX_WIDTH - 1)))
            {
              *p0++ = 0;
              *p0++ = 0;
              *p0++ = 0;

              *p1++ = 0;
              *p1++ = 0;
              *p1++ = 0;
            }
          else if ((x / GIMP_CHECK_SIZE) & 1)
            {
              *p0++ = r1;
              *p0++ = g1;
              *p0++ = b1;

              *p1++ = r0;
              *p1++ = g0;
              *p1++ = b0;
            }
          else
            {
              *p0++ = r0;
              *p0++ = g0;
              *p0++ = b0;

              *p1++ = r1;
              *p1++ = g1;
              *p1++ = b1;
            }

          *p2++ = 0;
          *p2++ = 0;
          *p2++ = 0;
508
        }
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

      /* Fill preview */

      gtk_preview_draw_row (GTK_PREVIEW (preview), rows[2],
                            0, 0, COLOR_BOX_WIDTH);

      for (y = 1; y < (COLOR_BOX_HEIGHT - 1); y++)
        if ((y / GIMP_CHECK_SIZE) & 1)
          gtk_preview_draw_row (GTK_PREVIEW (preview), rows[1],
                                0, y, COLOR_BOX_WIDTH);
        else
          gtk_preview_draw_row (GTK_PREVIEW (preview), rows[0],
                                0, y, COLOR_BOX_WIDTH);

      gtk_preview_draw_row (GTK_PREVIEW (preview), rows[2],
                            0, y, COLOR_BOX_WIDTH);

      gtk_widget_queue_draw (preview);
527 528
    }

529
  if (label && set_label)
530
    {
531 532 533 534
      gchar *str;

      str = g_strdup_printf (_("RGBA (%0.3f, %0.3f, %0.3f, %0.3f)"),
                             color->r, color->g, color->b, color->a);
535

536 537 538
      gtk_label_set_text (GTK_LABEL (label), str);

      g_free (str);
539 540
    }

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
#undef COLOR_BOX_WIDTH
#undef COLOR_BOX_HEIGHT
}

void
gimp_item_factory_set_label (GtkItemFactory *factory,
                             gchar          *path,
                             const gchar    *label)
{
  GtkWidget *widget;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);
  g_return_if_fail (label != NULL);

  widget = gtk_item_factory_get_widget (factory, path);

  if (widget)
559
    {
560
      if (GTK_IS_MENU (widget))
561
        widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
562

563
      if (GTK_IS_LABEL (GTK_BIN (widget)->child))
564
        gtk_label_set_text (GTK_LABEL (GTK_BIN (widget)->child), label);
565
    }
566
  else
567
    {
568
      g_warning ("%s: Unable to set label of menu item "
569
                 "which doesn't exist: %s",
570 571 572
                 G_STRLOC, path);
    }
}
573

574 575 576 577 578 579
void
gimp_item_factory_set_sensitive (GtkItemFactory *factory,
                                 gchar          *path,
                                 gboolean        sensitive)
{
  GtkWidget *widget;
580

581 582
  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);
583

584 585 586 587 588 589 590 591 592
  widget = gtk_item_factory_get_widget (factory, path);

  if (widget)
    {
      gtk_widget_set_sensitive (widget, sensitive);
    }
  else if (! strstr (path, "Script-Fu"))
    {
      g_warning ("%s: Unable to set sensitivity of menu item"
593
                 "which doesn't exist: %s",
594
                 G_STRLOC, path);
595
    }
596
}
597

598 599 600 601 602 603 604 605 606 607 608
void
gimp_item_factory_set_visible (GtkItemFactory *factory,
                               gchar          *path,
                               gboolean        visible)
{
  GtkWidget *widget;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);

  widget = gtk_item_factory_get_widget (factory, path);
609

610
  if (widget)
611
    {
612 613 614
      if (GTK_IS_MENU (widget))
        widget = gtk_menu_get_attach_widget (GTK_MENU (widget));

615 616 617 618 619 620 621 622
      if (visible)
        gtk_widget_show (widget);
      else
        gtk_widget_hide (widget);
    }
  else
    {
      g_warning ("%s: Unable to set visibility of menu item"
623 624 625 626 627
                 "which doesn't exist: %s",
                 G_STRLOC, path);
    }
}

628 629 630 631 632 633 634 635 636 637
void
gimp_item_factory_tearoff_callback (GtkWidget *widget,
                                    gpointer   data,
                                    guint      action)
{
  if (GTK_IS_TEAROFF_MENU_ITEM (widget))
    {
      GtkTearoffMenuItem *tomi = (GtkTearoffMenuItem *) widget;

      if (tomi->torn_off)
638
	{
639 640 641
	  GtkWidget *toplevel;

	  toplevel = gtk_widget_get_toplevel (widget);
642

643
	  if (! GTK_IS_WINDOW (toplevel))
644
	    {
645 646 647 648 649 650 651 652 653 654
	      g_warning ("%s: tearoff menu not in top level window",
                         G_STRLOC);
	    }
	  else
	    {
#ifdef __GNUC__
#warning FIXME: register tearoffs
#endif
	      g_object_set_data (G_OBJECT (widget), "tearoff-menu-toplevel",
                                 toplevel);
655
	    }
656 657 658 659
	}
      else
	{
	  GtkWidget *toplevel;
660

661 662
	  toplevel = (GtkWidget *) g_object_get_data (G_OBJECT (widget),
                                                      "tearoff-menu-toplevel");
663

664 665 666 667 668 669 670 671 672 673
	  if (! toplevel)
	    {
	      g_warning ("%s: can't unregister tearoff menu top level window",
                         G_STRLOC);
	    }
	  else
	    {
#ifdef __GNUC__
#warning FIXME: unregister tearoffs
#endif
674 675 676 677 678 679
	    }
	}
    }
}


680
/*  private functions  */
681

682
static void
683
gimp_item_factory_create_branches (GimpItemFactory      *factory,
684
                                   GimpItemFactoryEntry *entry)
685
{
686 687 688 689 690 691
  GString *tearoff_path;
  gint     factory_length;
  gchar   *p;

  if (! entry->entry.path)
    return;
692

693
  tearoff_path = g_string_new ("");
694

695 696
  p              = strchr (entry->entry.path, '/');
  factory_length = p - entry->entry.path;
697 698 699 700 701 702

  /*  skip the first slash  */
  if (p)
    p = strchr (p + 1, '/');

  while (p)
703
    {
704 705
      g_string_assign (tearoff_path, entry->entry.path + factory_length);
      g_string_truncate (tearoff_path, p - entry->entry.path - factory_length);
706

707 708
      if (! gtk_item_factory_get_widget (GTK_ITEM_FACTORY (factory),
                                         tearoff_path->str))
709
	{
710 711
	  GimpItemFactoryEntry branch_entry =
	  {
712
	    { tearoff_path->str, NULL, NULL, 0, "<Branch>" },
713 714 715 716
	    NULL,
	    NULL
	  };

717
	  g_object_set_data (G_OBJECT (factory), "complete", entry->entry.path);
718 719 720 721 722 723

	  gimp_item_factory_create_item (factory,
                                         &branch_entry,
                                         NULL, 2, TRUE, FALSE);

	  g_object_set_data (G_OBJECT (factory), "complete", NULL);
724
	}
725 726 727

      g_string_append (tearoff_path, "/tearoff1");

728 729
      if (! gtk_item_factory_get_widget (GTK_ITEM_FACTORY (factory),
                                         tearoff_path->str))
730
	{
731 732
	  GimpItemFactoryEntry tearoff_entry =
	  {
733 734
	    { tearoff_path->str, NULL,
              gimp_item_factory_tearoff_callback, 0, "<Tearoff>" },
735 736 737 738 739 740 741
	    NULL,
	    NULL, NULL
	  };

	  gimp_item_factory_create_item (factory,
                                         &tearoff_entry,
                                         NULL, 2, TRUE, FALSE);
742
	}
743 744

      p = strchr (p + 1, '/');
745
    }
746

747 748 749 750 751 752 753 754
  g_string_free (tearoff_path, TRUE);
}

static void
gimp_item_factory_item_realize (GtkWidget *widget,
                                gpointer   data)
{
  if (GTK_IS_MENU_SHELL (widget->parent))
755
    {
756 757 758 759 760 761
      if (! g_object_get_data (G_OBJECT (widget->parent),
                               "menus_key_press_connected"))
	{
	  g_signal_connect (G_OBJECT (widget->parent), "key_press_event",
                            G_CALLBACK (gimp_item_factory_item_key_press),
                            data);
762

763 764 765 766
	  g_object_set_data (G_OBJECT (widget->parent),
                             "menus_key_press_connected",
                             (gpointer) TRUE);
	}
767
    }
768 769
}

770 771 772 773
static gboolean
gimp_item_factory_item_key_press (GtkWidget   *widget,
                                  GdkEventKey *kevent,
                                  gpointer     data)
774
{
775 776 777 778 779
  GtkItemFactory *item_factory     = NULL;
  GtkWidget      *active_menu_item = NULL;
  gchar          *factory_path     = NULL;
  gchar          *help_path        = NULL;
  gchar          *help_page        = NULL;
780

781 782
  item_factory     = (GtkItemFactory *) data;
  active_menu_item = GTK_MENU_SHELL (widget)->active_menu_item;
783

784 785 786 787 788 789 790
  /*  first, get the help page from the item
   */
  if (active_menu_item)
    {
      help_page = (gchar *) g_object_get_data (G_OBJECT (active_menu_item),
                                               "help_page");
    }
791

792 793 794 795 796 797 798 799
  /*  For any key except F1, continue with the standard
   *  GtkItemFactory callback and assign a new shortcut, but don't
   *  assign a shortcut to the help menu entries...
   */
  if (kevent->keyval != GDK_F1)
    {
      if (help_page &&
	  *help_page &&
800
	  item_factory == (GtkItemFactory *) gimp_item_factory_from_path ("<Toolbox>") &&
801 802 803 804 805 806 807 808 809 810
	  (strcmp (help_page, "help/dialogs/help.html") == 0 ||
	   strcmp (help_page, "help/context_help.html") == 0))
	{
	  return TRUE;
	}
      else
	{
	  return FALSE;
	}
    }
811

812
  /*  ...finally, if F1 was pressed over any menu, show it's help page...  */
813

814 815
  factory_path = (gchar *) g_object_get_data (G_OBJECT (item_factory),
                                              "factory_path");
816

817 818 819
  if (! help_page ||
      ! *help_page)
    help_page = "index.html";
Sven Neumann's avatar
Sven Neumann committed
820

821
  if (factory_path && help_page)
Michael Natterer's avatar
Michael Natterer committed
822
    {
823 824
      gchar *help_string;
      gchar *at;
825

826
      help_page = g_strdup (help_page);
827

828
      at = strchr (help_page, '@');  /* HACK: locale subdir */
829

830 831 832 833 834 835 836 837 838 839
      if (at)
	{
	  *at = '\0';
	  help_path   = g_strdup (help_page);
	  help_string = g_strdup (at + 1);
	}
      else
	{
	  help_string = g_strdup_printf ("%s/%s", factory_path, help_page);
	}
840

841 842
      gimp_help (GIMP_ITEM_FACTORY (item_factory)->gimp,
                 help_path, help_string);
843

844 845 846 847 848 849 850
      g_free (help_string);
      g_free (help_page);
    }
  else
    {
      gimp_standard_help_func (NULL);
    }
851

852 853
  return TRUE;
}
854

855
#ifdef ENABLE_NLS
856

857 858 859 860 861
static gchar *
gimp_item_factory_translate_func (const gchar *path,
                                  gpointer     data)
{
  static gchar   *menupath = NULL;
862

863 864 865 866
  GtkItemFactory *item_factory = NULL;
  gchar          *retval;
  gchar          *factory;
  gchar          *translation;
867
  gchar          *domain   = NULL;
868 869
  gchar          *complete = NULL;
  gchar          *p, *t;
870

871
  factory = (gchar *) data;
872

873 874
  if (menupath)
    g_free (menupath);
875

876
  retval = menupath = g_strdup (path);
877

878
  if ((strstr (path, "/tearoff1") != NULL) ||
879 880
      (strstr (path, "/---")      != NULL) ||
      (strstr (path, "/MRU")      != NULL))
881
    return retval;
882

883
  if (factory)
884 885
    item_factory = (GtkItemFactory *) gimp_item_factory_from_path (factory);

886
  if (item_factory)
887
    {
888 889 890
      domain   = g_object_get_data (G_OBJECT (item_factory), "textdomain");
      complete = g_object_get_data (G_OBJECT (item_factory), "complete");
    }
891 892

  if (domain)  /*  use the plugin textdomain  */
893 894 895 896 897
    {
      g_free (menupath);
      menupath = g_strconcat (factory, path, NULL);

      if (complete)
898
	{
899
          /*  This is a branch, use the complete path for translation, 
900 901
	   *  then strip off entries from the end until it matches. 
	   */
902
	  complete    = g_strconcat (factory, complete, NULL);
903 904
	  translation = g_strdup (dgettext (domain, complete));

905
	  while (*complete && *translation && strcmp (complete, menupath))
906
	    {
907 908 909 910 911 912 913 914 915
	      p = strrchr (complete, '/');
	      t = strrchr (translation, '/');
	      if (p && t)
		{
		  *p = '\0';
		  *t = '\0';
		}
	      else
		break;
916 917
	    }

918 919 920 921 922 923
	  g_free (complete);
	}
      else
	{
	  translation = dgettext (domain, menupath);
	}
924

925 926 927 928 929 930 931
      /* 
       * Work around a bug in GTK+ prior to 1.2.7 (similar workaround below)
       */
      if (strncmp (factory, translation, strlen (factory)) == 0)
	{
	  retval = translation + strlen (factory);
	  if (complete)
932
	    {
933 934
	      g_free (menupath);
	      menupath = translation;
935
	    }
936 937 938
	}
      else
	{
939 940 941
	  g_warning ("%s: bad translation for menupath: %s",
                     G_STRLOC, menupath);

942 943 944 945 946
	  retval = menupath + strlen (factory);
	  if (complete)
	    g_free (translation);
	}
    }
947
  else  /*  use the gimp textdomain  */
948 949 950
    {
      if (complete)
	{
951
          /*  This is a branch, use the complete path for translation, 
952 953
	   *  then strip off entries from the end until it matches. 
	   */
954
	  complete    = g_strdup (complete);
955 956 957
	  translation = g_strdup (gettext (complete));