gimpitemfactory.c 25.4 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

Sven Neumann's avatar
Sven Neumann committed
23
#include <gtk/gtk.h>
24
#include <gdk/gdkkeysyms.h>
25

26
#include "libgimpbase/gimpbase.h"
27 28
#include "libgimpwidgets/gimpwidgets.h"

29 30 31
#include "widgets-types.h"

#include "gimpitemfactory.h"
32
#include "gimpwidgets-utils.h"
33

34
#include "gimphelp.h"
Elliot Lee's avatar
Elliot Lee committed
35
#include "gimprc.h"
36

37 38 39
#include "libgimp/gimpintl.h"


40 41
/*  local function prototypes  */

42 43 44 45 46 47 48 49 50 51 52 53 54
static void     gimp_item_factory_create_branches (GtkItemFactory       *factory,
                                                   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
55

56

57
/*  public functions  */
58

59 60 61 62
void
gimp_menu_item_create (GimpItemFactoryEntry *entry,
                       gchar                *domain_name,
                       gpointer              callback_data)
63
{
64 65
  GtkItemFactory *item_factory;
  gchar          *path;
66

67
  g_return_if_fail (entry != NULL);
68

69
  path = entry->entry.path;
70

71 72
  if (!path)
    return;
73

74
  item_factory = gtk_item_factory_from_path (path);
75

76 77
  if (!item_factory)
    {
78 79
      g_warning ("%s: Could not find item factory for path \"%s\"",
                 G_STRLOC, path);
80 81
      return;
    }
82

83
  g_object_set_data (G_OBJECT (item_factory), "textdomain", domain_name);
84

85 86 87
  while (*path != '>')
    path++;
  path++;
Elliot Lee's avatar
Elliot Lee committed
88

89
  entry->entry.path = path;
90

91 92 93 94 95
  gimp_item_factory_create_item (item_factory,
                                 entry,
                                 callback_data, 2,
                                 TRUE, FALSE);
}
96

97 98
void
gimp_menu_item_destroy (gchar *path)
99
{
100 101
  GtkItemFactory *factory;

102
  g_return_if_fail (path != NULL);
103

104 105 106 107 108 109 110 111 112 113 114
  factory = gtk_item_factory_from_path (path);

  if (factory)
    {
      gtk_item_factory_delete_item (factory, path);
    }
  else
    {
      g_warning ("%s: Could not find item factory for path \"%s\"",
                 G_STRLOC, path);
    }
115
}
116 117

void
118 119
gimp_menu_item_set_active (gchar    *path,
                           gboolean  active)
Elliot Lee's avatar
Elliot Lee committed
120
{
121 122 123 124 125 126 127
  GtkItemFactory *factory;

  g_return_if_fail (path != NULL);

  factory = gtk_item_factory_from_path (path);

  if (factory)
128
    {
129 130 131 132 133 134
      gimp_item_factory_set_active (factory, path, active);
    }
  else if (! strstr (path, "Script-Fu"))
    {
      g_warning ("%s: Could not find item factory for path \"%s\"",
                 G_STRLOC, path);
135
    }
Elliot Lee's avatar
Elliot Lee committed
136 137
}

138
void
139 140 141
gimp_menu_item_set_color (gchar         *path,
                          const GimpRGB *color,
                          gboolean       set_label)
Elliot Lee's avatar
Elliot Lee committed
142
{
143
  GtkItemFactory *factory;
144

145 146
  g_return_if_fail (path != NULL);
  g_return_if_fail (color != NULL);
147

148
  factory = gtk_item_factory_from_path (path);
149

150
  if (factory)
151
    {
152
      gimp_item_factory_set_color (factory, path, color, set_label);
153
    }
154
  else
155
    {
156 157
      g_warning ("%s: Could not find item factory for path \"%s\"",
                 G_STRLOC, path);
158
    }
159 160 161 162 163 164 165 166 167 168
}

void
gimp_menu_item_set_label (gchar       *path,
                          const gchar *label)
{
  GtkItemFactory *factory;

  g_return_if_fail (path != NULL);
  g_return_if_fail (label != NULL);
Elliot Lee's avatar
Elliot Lee committed
169

170 171 172
  factory = gtk_item_factory_from_path (path);

  if (factory)
173
    {
174
      gimp_item_factory_set_label (factory, path, label);
175
    }
176
  else
177
    {
178 179
      g_warning ("%s: Could not find item factory for path \"%s\"",
                 G_STRLOC, path);
180
    }
181
}
Elliot Lee's avatar
Elliot Lee committed
182

183 184 185 186 187 188 189 190 191 192 193
void
gimp_menu_item_set_sensitive (gchar    *path,
                              gboolean  sensitive)
{
  GtkItemFactory *factory;

  g_return_if_fail (path != NULL);

  factory = gtk_item_factory_from_path (path);

  if (factory)
194
    {
195
      gimp_item_factory_set_sensitive (factory, path, sensitive);
196
    }
197
  else if (! strstr (path, "Script-Fu"))
198
    {
199 200
      g_warning ("%s: Could not find item factory for path \"%s\"",
                 G_STRLOC, path);
201
    }
202 203 204 205 206 207 208 209 210
}

void
gimp_menu_item_set_visible (gchar    *path,
                            gboolean  visible)
{
  GtkItemFactory *factory;

  g_return_if_fail (path != NULL);
Elliot Lee's avatar
Elliot Lee committed
211

212 213 214
  factory = gtk_item_factory_from_path (path);

  if (factory)
215
    {
216
      gimp_item_factory_set_visible (factory, path, visible);
217
    }
218
  else
219
    {
220 221
      g_warning ("%s: Could not find item factory for path \"%s\"",
                 G_STRLOC, path);
222
    }
223 224 225 226
}


GtkItemFactory *
227 228 229 230 231 232 233 234
gimp_item_factory_new (GType                      container_type,
                       const gchar               *path,
                       const gchar               *factory_path,
                       GimpItemFactoryUpdateFunc  update_func,
                       guint                      n_entries,
                       GimpItemFactoryEntry      *entries,
                       gpointer                   callback_data,
                       gboolean                   create_tearoff)
235 236 237 238 239 240 241 242 243 244 245 246 247
{
  GtkItemFactory *factory;

  factory = gtk_item_factory_new (container_type, path, NULL);

  gtk_item_factory_set_translate_func (factory,
				       gimp_item_factory_translate_func,
				       (gpointer) path,
				       NULL);

  g_object_set_data (G_OBJECT (factory), "factory_path",
                     (gpointer) factory_path);

248 249 250
  g_object_set_data (G_OBJECT (factory), "gimp-item-factory-update-func",
                     update_func);

251 252 253 254 255 256 257
  gimp_item_factory_create_items (factory,
                                  n_entries,
                                  entries,
                                  callback_data,
                                  2,
                                  create_tearoff,
                                  TRUE);
258

259 260 261
  return factory;
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
void
gimp_item_factory_popup_with_data (GtkItemFactory   *item_factory,
				   gpointer          data,
                                   GtkDestroyNotify  popdown_func)
{
  GimpItemFactoryUpdateFunc  update_func;
  gint                       x, y;
  guint                      button;
  guint32                    activate_time;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (item_factory));

  update_func = g_object_get_data (G_OBJECT (item_factory),
                                   "gimp-item-factory-update-func");

  if (update_func)
    (* update_func) (item_factory, data);

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

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

293 294 295 296 297 298 299 300 301 302 303 304 305 306
void
gimp_item_factory_create_item (GtkItemFactory        *item_factory,
                               GimpItemFactoryEntry  *entry,
                               gpointer               callback_data,
                               guint                  callback_type,
                               gboolean               create_tearoff,
                               gboolean               static_entry)
{
  GtkWidget *menu_item;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (item_factory));
  g_return_if_fail (entry != NULL);

  if (! (strstr (entry->entry.path, "tearoff1")))
307
    {
308 309 310 311
      if (! gimprc.disable_tearoff_menus && create_tearoff)
	{
	  gimp_item_factory_create_branches (item_factory, entry);
	}
312
    }
313
  else if (gimprc.disable_tearoff_menus || ! create_tearoff)
314
    {
315
      return;
316
    }
317

318
  if (entry->quark_string)
319
    {
320 321 322 323 324 325 326 327
      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;
328 329
    }

330 331 332 333 334 335 336 337 338
  gtk_item_factory_create_item (item_factory,
				(GtkItemFactoryEntry *) entry,
				callback_data,
				callback_type);

  menu_item = gtk_item_factory_get_item (item_factory,
					 ((GtkItemFactoryEntry *) entry)->path);

  if (menu_item)
339
    {
340 341 342 343 344 345
      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);
346
    }
347
}
348

349 350 351 352 353 354 355 356 357 358 359 360
void
gimp_item_factory_create_items (GtkItemFactory       *item_factory,
                                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++)
361
    {
362 363 364 365 366 367
      gimp_item_factory_create_item (item_factory,
                                     entries + i,
                                     callback_data,
                                     callback_type,
                                     create_tearoff,
                                     static_entries);
368
    }
369
}
370

371 372 373 374 375 376 377 378 379 380 381 382 383
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)
384
    {
385 386 387 388 389 390 391
      if (GTK_IS_CHECK_MENU_ITEM (widget))
        {
          gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), active);
        }
      else
        {
        }
392
    }
393
  else if (! strstr (path, "Script-Fu"))
394
    {
395 396 397
      g_warning ("%s: Unable to set \"active\" for menu item "
                 "which doesn't exist:\n%s",
                 G_STRLOC, path);
398
    }
399 400
}

401
void
402 403 404 405
gimp_item_factory_set_color (GtkItemFactory *factory,
                             gchar          *path,
                             const GimpRGB  *color,
                             gboolean        set_label)
406
{
407 408 409
  GtkWidget *widget;
  GtkWidget *preview = NULL;
  GtkWidget *label   = NULL;
410

411 412 413
  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);
  g_return_if_fail (color != NULL);
414

415
  widget = gtk_item_factory_get_widget (factory, path);
416

417
  if (! widget)
418
    {
419 420 421 422 423
      g_warning ("%s: Unable to set color of menu item "
                 "which doesn't exist:\n%s",
                 G_STRLOC, path);
      return;
    }
424

425 426
#define COLOR_BOX_WIDTH  16
#define COLOR_BOX_HEIGHT 16
427

428 429 430 431 432 433
  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");
434
    }
435
  else if (GTK_IS_LABEL (GTK_BIN (widget)->child))
436
    {
437
      GtkWidget *hbox;
438

439
      label = GTK_BIN (widget)->child;
440

441
      g_object_ref (G_OBJECT (label));
442

443
      gtk_container_remove (GTK_CONTAINER (widget), label);
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
      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);
462 463
    }

464
  if (preview)
465
    {
466 467 468 469 470 471 472
      guchar  rows[3][COLOR_BOX_WIDTH * 3];
      gint    x, y;
      gint    r0, g0, b0;
      gint    r1, g1, b1;
      guchar *p0, *p1, *p2;

      /* Fill rows */
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487
      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++)
488
        {
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
          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;
523
        }
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

      /* 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);
542 543
    }

544
  if (label && set_label)
545
    {
546 547 548 549
      gchar *str;

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

551 552 553
      gtk_label_set_text (GTK_LABEL (label), str);

      g_free (str);
554 555
    }

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
#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)
574
    {
575 576 577 578
      if (GTK_IS_MENU (widget))
        {
          widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
        }
579

580 581 582 583
      if (GTK_IS_LABEL (GTK_BIN (widget)->child))
        {
          gtk_label_set_text (GTK_LABEL (GTK_BIN (widget)->child), label);
        }
584
    }
585
  else
586
    {
587 588 589 590 591
      g_warning ("%s: Unable to set label of menu item "
                 "which doesn't exist:\n%s",
                 G_STRLOC, path);
    }
}
592

593 594 595 596 597 598
void
gimp_item_factory_set_sensitive (GtkItemFactory *factory,
                                 gchar          *path,
                                 gboolean        sensitive)
{
  GtkWidget *widget;
599

600 601
  g_return_if_fail (GTK_IS_ITEM_FACTORY (factory));
  g_return_if_fail (path != NULL);
602

603 604 605 606 607 608 609 610 611 612 613
  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"
                 "which doesn't exist:\n%s",
                 G_STRLOC, path);
614
    }
615
}
616

617 618 619 620 621 622 623 624 625 626 627
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);
628

629
  if (widget)
630
    {
631 632 633 634 635 636 637 638 639 640 641 642
      if (visible)
        gtk_widget_show (widget);
      else
        gtk_widget_hide (widget);
    }
  else
    {
      g_warning ("%s: Unable to set visibility of menu item"
                 "which doesn't exist:\n%s",
                 G_STRLOC, path);
    }
}
643 644


645 646 647 648 649 650 651 652 653 654
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)
655
	{
656 657 658
	  GtkWidget *toplevel;

	  toplevel = gtk_widget_get_toplevel (widget);
659

660
	  if (! GTK_IS_WINDOW (toplevel))
661
	    {
662 663 664 665 666 667 668 669 670 671
	      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);
672
	    }
673 674 675 676
	}
      else
	{
	  GtkWidget *toplevel;
677

678 679
	  toplevel = (GtkWidget *) g_object_get_data (G_OBJECT (widget),
                                                      "tearoff-menu-toplevel");
680

681 682 683 684 685 686 687 688 689 690
	  if (! toplevel)
	    {
	      g_warning ("%s: can't unregister tearoff menu top level window",
                         G_STRLOC);
	    }
	  else
	    {
#ifdef __GNUC__
#warning FIXME: unregister tearoffs
#endif
691 692 693 694 695 696
	    }
	}
    }
}


697
/*  private functions  */
698

699
static void
700 701
gimp_item_factory_create_branches (GtkItemFactory       *factory,
                                   GimpItemFactoryEntry *entry)
702
{
703 704 705 706 707 708 709
  GString *tearoff_path;
  gint     factory_length;
  gchar   *p;
  gchar   *path;

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

711
  tearoff_path = g_string_new ("");
712

713 714 715 716 717 718 719 720 721
  path = entry->entry.path;
  p = strchr (path, '/');
  factory_length = p - path;

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

  while (p)
722
    {
723 724 725 726
      g_string_assign (tearoff_path, path + factory_length);
      g_string_truncate (tearoff_path, p - path - factory_length);

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

	  branch_entry.entry.path = tearoff_path->str;
	  g_object_set_data (G_OBJECT (factory), "complete", path);

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

	  g_object_set_data (G_OBJECT (factory), "complete", NULL);
743
	}
744 745 746 747

      g_string_append (tearoff_path, "/tearoff1");

      if (! gtk_item_factory_get_widget (factory, tearoff_path->str))
748
	{
749 750 751 752 753 754 755 756 757 758 759 760
	  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);
761
	}
762 763

      p = strchr (p + 1, '/');
764
    }
765

766 767 768 769 770 771 772 773
  g_string_free (tearoff_path, TRUE);
}

static void
gimp_item_factory_item_realize (GtkWidget *widget,
                                gpointer   data)
{
  if (GTK_IS_MENU_SHELL (widget->parent))
774
    {
775 776 777 778 779 780
      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);
781

782 783 784 785
	  g_object_set_data (G_OBJECT (widget->parent),
                             "menus_key_press_connected",
                             (gpointer) TRUE);
	}
786
    }
787 788
}

789 790 791 792
static gboolean
gimp_item_factory_item_key_press (GtkWidget   *widget,
                                  GdkEventKey *kevent,
                                  gpointer     data)
793
{
794 795 796 797 798
  GtkItemFactory *item_factory     = NULL;
  GtkWidget      *active_menu_item = NULL;
  gchar          *factory_path     = NULL;
  gchar          *help_path        = NULL;
  gchar          *help_page        = NULL;
799

800 801
  item_factory     = (GtkItemFactory *) data;
  active_menu_item = GTK_MENU_SHELL (widget)->active_menu_item;
802

803 804 805 806 807 808 809
  /*  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");
    }
810

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  /*  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 &&
	  item_factory == gtk_item_factory_from_path ("<Toolbox>") &&
	  (strcmp (help_page, "help/dialogs/help.html") == 0 ||
	   strcmp (help_page, "help/context_help.html") == 0))
	{
	  return TRUE;
	}
      else
	{
	  return FALSE;
	}
    }
830

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

833 834
  factory_path = (gchar *) g_object_get_data (G_OBJECT (item_factory),
                                              "factory_path");
835

836 837 838
  if (! help_page ||
      ! *help_page)
    help_page = "index.html";
Sven Neumann's avatar
Sven Neumann committed
839

840
  if (factory_path && help_page)
Michael Natterer's avatar
Michael Natterer committed
841
    {
842 843
      gchar *help_string;
      gchar *at;
844

845
      help_page = g_strdup (help_page);
846

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

849 850 851 852 853 854 855 856 857 858
      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);
	}
859

860
      gimp_help (help_path, help_string);
861

862 863 864 865 866 867 868
      g_free (help_string);
      g_free (help_page);
    }
  else
    {
      gimp_standard_help_func (NULL);
    }
869

870 871
  return TRUE;
}
872

873
#ifdef ENABLE_NLS
874

875 876 877 878 879
static gchar *
gimp_item_factory_translate_func (const gchar *path,
                                  gpointer     data)
{
  static gchar   *menupath = NULL;
880

881 882 883 884 885 886 887
  GtkItemFactory *item_factory = NULL;
  gchar          *retval;
  gchar          *factory;
  gchar          *translation;
  gchar          *domain = NULL;
  gchar          *complete = NULL;
  gchar          *p, *t;
888

889
  factory = (gchar *) data;
890

891 892
  if (menupath)
    g_free (menupath);
893

894
  retval = menupath = g_strdup (path);
895

896 897 898 899
  if ((strstr (path, "/tearoff1") != NULL) ||
      (strstr (path, "/---") != NULL) ||
      (strstr (path, "/MRU") != NULL))
    return retval;
900

901 902 903
  if (factory)
    item_factory = gtk_item_factory_from_path (factory);
  if (item_factory)
904
    {
905 906 907 908 909 910 911 912 913 914
      domain   = g_object_get_data (G_OBJECT (item_factory), "textdomain");
      complete = g_object_get_data (G_OBJECT (item_factory), "complete");
    }
  
  if (domain)   /*  use the plugin textdomain  */
    {
      g_free (menupath);
      menupath = g_strconcat (factory, path, NULL);

      if (complete)
915
	{
916 917 918 919 920 921 922 923 924 925
	  /*  
           *  This is a branch, use the complete path for translation, 
	   *  then strip off entries from the end until it matches. 
	   */
	  complete = g_strconcat (factory, complete, NULL);
	  translation = g_strdup (dgettext (domain, complete));

	  while (complete && *complete && 
		 translation && *translation && 
		 strcmp (complete, menupath))
926
	    {
927 928 929 930 931 932 933 934 935
	      p = strrchr (complete, '/');
	      t = strrchr (translation, '/');
	      if (p && t)
		{
		  *p = '\0';
		  *t = '\0';
		}
	      else
		break;
936 937
	    }

938 939 940 941 942 943
	  g_free (complete);
	}
      else
	{
	  translation = dgettext (domain, menupath);
	}
944

945 946 947 948 949 950 951
      /* 
       * 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)
952
	    {
953 954
	      g_free (menupath);
	      menupath = translation;
955
	    }
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	}
      else
	{
	  g_warning ("bad translation for menupath: %s", menupath);
	  retval = menupath + strlen (factory);
	  if (complete)
	    g_free (translation);
	}
    }
  else   /*  use the gimp textdomain  */
    {
      if (complete)
	{
	  /*  
           *  This is a branch, use the complete path for translation, 
	   *  then strip off entries from the end until it matches. 
	   */
	  complete = g_strdup (complete);
	  translation = g_strdup (gettext (complete));
	  
	  while (*complete && *translation && strcmp (complete, menupath))
977
	    {
978 979 980
	      p = strrchr (complete, '/');
	      t = strrchr (translation, '/');
	      if (p && t)
981
		{
982 983
		  *p = '\0';
		  *t = '\0';
984 985
		}
	      else
986
		break;
987
	    }
988 989 990 991
	  g_free (complete);
	}
      else
	translation = gettext (menupath);
992

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
      if (*translation == '/')
	{
	  retval = translation;
	  if (complete)
	    {
	      g_free (menupath);
	      menupath = translation;
	    }
	}
      else
	{
	  g_warning ("bad translation for menupath: %s", menupath);
	  if (complete)
	    g_free (translation);
	}
1008 1009
    }
  
1010
  return retval;
1011
}
Michael Natterer's avatar
Michael Natterer committed
1012

1013
#endif  /*  ENABLE_NLS  */