gimpitemfactory.c 25.7 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 289 290
      g_signal_connect_after (G_OBJECT (menu_item), "realize",
			      G_CALLBACK (gimp_item_factory_item_realize),
			      item_factory);

      g_object_set_data (G_OBJECT (menu_item), "help_page",
			 (gpointer) entry->help_page);
291
    }
292
}
293

294
void
295
gimp_item_factory_create_items (GimpItemFactory      *item_factory,
296 297 298 299 300 301 302 303 304 305
                                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++)
306
    {
307 308 309 310 311 312
      gimp_item_factory_create_item (item_factory,
                                     entries + i,
                                     callback_data,
                                     callback_type,
                                     create_tearoff,
                                     static_entries);
313
    }
314
}
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
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);
}

343 344 345 346 347 348 349 350 351 352 353 354 355
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)
356
    {
357 358 359 360 361 362
      if (GTK_IS_CHECK_MENU_ITEM (widget))
        {
          gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), active);
        }
      else
        {
363 364 365 366 367
          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);
368
        }
369
    }
370
  else if (! strstr (path, "Script-Fu"))
371
    {
372
      g_warning ("%s: Unable to set \"active\" for menu item "
373
                 "which doesn't exist: %s",
374
                 G_STRLOC, path);
375
    }
376 377
}

378
void
379 380 381 382
gimp_item_factory_set_color (GtkItemFactory *factory,
                             gchar          *path,
                             const GimpRGB  *color,
                             gboolean        set_label)
383
{
384 385 386
  GtkWidget *widget;
  GtkWidget *preview = NULL;
  GtkWidget *label   = NULL;
387

388 389 390
  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);
  g_return_if_fail (color != NULL);
391

392
  widget = gtk_item_factory_get_widget (factory, path);
393

394
  if (! widget)
395
    {
396
      g_warning ("%s: Unable to set color of menu item "
397
                 "which doesn't exist: %s",
398 399 400
                 G_STRLOC, path);
      return;
    }
401

402 403
#define COLOR_BOX_WIDTH  16
#define COLOR_BOX_HEIGHT 16
404

405 406 407 408 409 410
  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");
411
    }
412
  else if (GTK_IS_LABEL (GTK_BIN (widget)->child))
413
    {
414
      GtkWidget *hbox;
415

416
      label = GTK_BIN (widget)->child;
417

418
      g_object_ref (G_OBJECT (label));
419

420
      gtk_container_remove (GTK_CONTAINER (widget), label);
421

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
      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);

      gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
      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);
439 440
    }

441
  if (preview)
442
    {
443 444 445 446 447 448 449
      guchar  rows[3][COLOR_BOX_WIDTH * 3];
      gint    x, y;
      gint    r0, g0, b0;
      gint    r1, g1, b1;
      guchar *p0, *p1, *p2;

      /* Fill rows */
450

451 452 453 454 455 456 457 458 459 460 461 462 463 464
      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++)
465
        {
466 467 468 469 470 471 472 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
          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;
500
        }
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

      /* 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);
519 520
    }

521
  if (label && set_label)
522
    {
523 524 525 526
      gchar *str;

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

528 529 530
      gtk_label_set_text (GTK_LABEL (label), str);

      g_free (str);
531 532
    }

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
#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)
551
    {
552 553 554 555
      if (GTK_IS_MENU (widget))
        {
          widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
        }
556

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

570 571 572 573 574 575
void
gimp_item_factory_set_sensitive (GtkItemFactory *factory,
                                 gchar          *path,
                                 gboolean        sensitive)
{
  GtkWidget *widget;
576

577 578
  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);
579

580 581 582 583 584 585 586 587 588
  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"
589
                 "which doesn't exist: %s",
590
                 G_STRLOC, path);
591
    }
592
}
593

594 595 596 597 598 599 600 601 602 603 604
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);
605

606
  if (widget)
607
    {
608 609 610 611 612 613 614 615
      if (visible)
        gtk_widget_show (widget);
      else
        gtk_widget_hide (widget);
    }
  else
    {
      g_warning ("%s: Unable to set visibility of menu item"
616 617 618 619 620
                 "which doesn't exist: %s",
                 G_STRLOC, path);
    }
}

621 622 623 624 625 626 627 628 629 630
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)
631
	{
632 633 634
	  GtkWidget *toplevel;

	  toplevel = gtk_widget_get_toplevel (widget);
635

636
	  if (! GTK_IS_WINDOW (toplevel))
637
	    {
638 639 640 641 642 643 644 645 646 647
	      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);
648
	    }
649 650 651 652
	}
      else
	{
	  GtkWidget *toplevel;
653

654 655
	  toplevel = (GtkWidget *) g_object_get_data (G_OBJECT (widget),
                                                      "tearoff-menu-toplevel");
656

657 658 659 660 661 662 663 664 665 666
	  if (! toplevel)
	    {
	      g_warning ("%s: can't unregister tearoff menu top level window",
                         G_STRLOC);
	    }
	  else
	    {
#ifdef __GNUC__
#warning FIXME: unregister tearoffs
#endif
667 668 669 670 671 672
	    }
	}
    }
}


673
/*  private functions  */
674

675
static void
676
gimp_item_factory_create_branches (GimpItemFactory      *factory,
677
                                   GimpItemFactoryEntry *entry)
678
{
679 680 681 682 683 684
  GString *tearoff_path;
  gint     factory_length;
  gchar   *p;

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

686
  tearoff_path = g_string_new ("");
687

688 689
  p              = strchr (entry->entry.path, '/');
  factory_length = p - entry->entry.path;
690 691 692 693 694 695

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

  while (p)
696
    {
697 698
      g_string_assign (tearoff_path, entry->entry.path + factory_length);
      g_string_truncate (tearoff_path, p - entry->entry.path - factory_length);
699

700 701
      if (! gtk_item_factory_get_widget (GTK_ITEM_FACTORY (factory),
                                         tearoff_path->str))
702
	{
703 704 705 706 707 708 709 710
	  GimpItemFactoryEntry branch_entry =
	  {
	    { NULL, NULL, NULL, 0, "<Branch>" },
	    NULL,
	    NULL
	  };

	  branch_entry.entry.path = tearoff_path->str;
711
	  g_object_set_data (G_OBJECT (factory), "complete", entry->entry.path);
712 713 714 715 716 717

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

	  g_object_set_data (G_OBJECT (factory), "complete", NULL);
718
	}
719 720 721

      g_string_append (tearoff_path, "/tearoff1");

722 723
      if (! gtk_item_factory_get_widget (GTK_ITEM_FACTORY (factory),
                                         tearoff_path->str))
724
	{
725 726 727 728 729 730 731 732 733 734 735 736
	  GimpItemFactoryEntry tearoff_entry =
	  {
	    { NULL, NULL, gimp_item_factory_tearoff_callback, 0, "<Tearoff>" },
	    NULL,
	    NULL, NULL
	  };

	  tearoff_entry.entry.path = tearoff_path->str;

	  gimp_item_factory_create_item (factory,
                                         &tearoff_entry,
                                         NULL, 2, TRUE, FALSE);
737
	}
738 739

      p = strchr (p + 1, '/');
740
    }
741

742 743 744 745 746 747 748 749
  g_string_free (tearoff_path, TRUE);
}

static void
gimp_item_factory_item_realize (GtkWidget *widget,
                                gpointer   data)
{
  if (GTK_IS_MENU_SHELL (widget->parent))
750
    {
751 752 753 754 755 756
      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);
757

758 759 760 761
	  g_object_set_data (G_OBJECT (widget->parent),
                             "menus_key_press_connected",
                             (gpointer) TRUE);
	}
762
    }
763 764
}

765 766 767 768
static gboolean
gimp_item_factory_item_key_press (GtkWidget   *widget,
                                  GdkEventKey *kevent,
                                  gpointer     data)
769
{
770 771 772 773 774
  GtkItemFactory *item_factory     = NULL;
  GtkWidget      *active_menu_item = NULL;
  gchar          *factory_path     = NULL;
  gchar          *help_path        = NULL;
  gchar          *help_page        = NULL;
775

776 777
  item_factory     = (GtkItemFactory *) data;
  active_menu_item = GTK_MENU_SHELL (widget)->active_menu_item;
778

779 780 781 782 783 784 785
  /*  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");
    }
786

787 788 789 790 791 792 793 794
  /*  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 &&
795
	  item_factory == (GtkItemFactory *) gimp_item_factory_from_path ("<Toolbox>") &&
796 797 798 799 800 801 802 803 804 805
	  (strcmp (help_page, "help/dialogs/help.html") == 0 ||
	   strcmp (help_page, "help/context_help.html") == 0))
	{
	  return TRUE;
	}
      else
	{
	  return FALSE;
	}
    }
806

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

809 810
  factory_path = (gchar *) g_object_get_data (G_OBJECT (item_factory),
                                              "factory_path");
811

812 813 814
  if (! help_page ||
      ! *help_page)
    help_page = "index.html";
Sven Neumann's avatar
Sven Neumann committed
815

816
  if (factory_path && help_page)
Michael Natterer's avatar
Michael Natterer committed
817
    {
818 819
      gchar *help_string;
      gchar *at;
820

821
      help_page = g_strdup (help_page);
822

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

825 826 827 828 829 830 831 832 833 834
      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);
	}
835

836 837
      gimp_help (GIMP_ITEM_FACTORY (item_factory)->gimp,
                 help_path, help_string);
838

839 840 841 842 843 844 845
      g_free (help_string);
      g_free (help_page);
    }
  else
    {
      gimp_standard_help_func (NULL);
    }
846

847 848
  return TRUE;
}
849

850
#ifdef ENABLE_NLS
851

852 853 854 855 856
static gchar *
gimp_item_factory_translate_func (const gchar *path,
                                  gpointer     data)
{
  static gchar   *menupath = NULL;
857

858 859 860 861
  GtkItemFactory *item_factory = NULL;
  gchar          *retval;
  gchar          *factory;
  gchar          *translation;
862
  gchar          *domain   = NULL;
863 864
  gchar          *complete = NULL;
  gchar          *p, *t;
865

866
  factory = (gchar *) data;
867

868 869
  if (menupath)
    g_free (menupath);
870

871
  retval = menupath = g_strdup (path);
872

873
  if ((strstr (path, "/tearoff1") != NULL) ||
874 875
      (strstr (path, "/---")      != NULL) ||
      (strstr (path, "/MRU")      != NULL))
876
    return retval;
877

878
  if (factory)
879 880
    item_factory = (GtkItemFactory *) gimp_item_factory_from_path (factory);

881
  if (item_factory)
882
    {
883 884 885
      domain   = g_object_get_data (G_OBJECT (item_factory), "textdomain");
      complete = g_object_get_data (G_OBJECT (item_factory), "complete");
    }
886 887

  if (domain)  /*  use the plugin textdomain  */
888 889 890 891 892
    {
      g_free (menupath);
      menupath = g_strconcat (factory, path, NULL);

      if (complete)
893
	{
894
          /*  This is a branch, use the complete path for translation, 
895 896
	   *  then strip off entries from the end until it matches. 
	   */
897
	  complete    = g_strconcat (factory, complete, NULL);
898 899
	  translation = g_strdup (dgettext (domain, complete));

900
	  while (*complete && *translation && strcmp (complete, menupath))
901
	    {
902 903 904 905 906 907 908 909 910
	      p = strrchr (complete, '/');
	      t = strrchr (translation, '/');
	      if (p && t)
		{
		  *p = '\0';
		  *t = '\0';
		}
	      else
		break;
911 912
	    }

913 914 915 916 917 918
	  g_free (complete);
	}
      else
	{
	  translation = dgettext (domain, menupath);
	}
919

920 921 922 923 924 925 926
      /* 
       * 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)
927
	    {
928 929
	      g_free (menupath);
	      menupath = translation;
930
	    }
931 932 933
	}
      else
	{
934 935 936
	  g_warning ("%s: bad translation for menupath: %s",
                     G_STRLOC, menupath);

937 938 939 940 941
	  retval = menupath + strlen (factory);
	  if (complete)
	    g_free (translation);
	}
    }
942
  else  /*  use the gimp textdomain  */
943 944 945
    {
      if (complete)
	{
946
          /*  This is a branch, use the complete path for translation, 
947 948
	   *  then strip off entries from the end until it matches. 
	   */
949
	  complete    = g_strdup (complete);
950 951 952
	  translation = g_strdup (gettext (complete));
	  
	  while (*complete && *translation && strcmp (complete, menupath))
953
	    {
954 955 956
	      p = strrchr (complete, '/');
	      t = strrchr (translation, '/');
	      if (p && t)
957
		{
958 959
		  *p = '\0';
		  *t = '\0';
960 961
		}
	      else
962
		break;
963
	    }
964