gimpactionview.c 21.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpactionview.c
 * Copyright (C) 2004  Michael Natterer <mitch@gimp.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 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"

#include <string.h>

#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
29
#include "libgimpwidgets/gimpwidgets.h"
30 31 32 33 34 35

#include "widgets-types.h"

#include "gimpaction.h"
#include "gimpactiongroup.h"
#include "gimpactionview.h"
36
#include "gimpcellrendereraccel.h"
37 38
#include "gimpmessagebox.h"
#include "gimpmessagedialog.h"
39 40 41 42 43 44 45 46 47 48 49 50
#include "gimpuimanager.h"
#include "gimpwidgets-utils.h"

#include "gimp-intl.h"


enum
{
  COLUMN_ACTION,
  COLUMN_STOCK_ID,
  COLUMN_LABEL,
  COLUMN_NAME,
51 52
  COLUMN_ACCEL_KEY,
  COLUMN_ACCEL_MASK,
53 54 55 56 57 58 59 60 61 62
  COLUMN_MENU_ITEM,
  NUM_COLUMNS
};


/*  local function prototypes  */

static void     gimp_action_view_class_init  (GimpActionViewClass *klass);
static void     gimp_action_view_init        (GimpActionView      *view);

63 64
static void     gimp_action_view_dispose         (GObject         *object);

65 66 67 68 69 70 71 72
static gboolean gimp_action_view_accel_find_func (GtkAccelKey     *key,
                                                  GClosure        *closure,
                                                  gpointer         data);
static void     gimp_action_view_accel_changed   (GtkAccelGroup   *accel_group,
                                                  guint            unused1,
                                                  GdkModifierType  unused2,
                                                  GClosure        *accel_closure,
                                                  GimpActionView  *view);
73 74
static void     gimp_action_view_accel_edited    (GimpCellRendererAccel *accel,
                                                  const char      *path_string,
75
                                                  gboolean         delete,
76 77 78 79
                                                  guint            accel_key,
                                                  GdkModifierType  accel_mask,
                                                  GimpActionView  *view);

80

81 82
static GtkTreeViewClass *parent_class = NULL;

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

GType
gimp_action_view_get_type (void)
{
  static GType view_type = 0;

  if (! view_type)
    {
      static const GTypeInfo view_info =
      {
        sizeof (GimpActionViewClass),
        NULL,           /* base_init      */
        NULL,           /* base_finalize  */
        (GClassInitFunc) gimp_action_view_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpActionView),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_action_view_init
      };

      view_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
                                          "GimpActionView",
                                          &view_info, 0);
    }

  return view_type;
}

static void
gimp_action_view_class_init (GimpActionViewClass *klass)
{
115 116 117 118 119
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  object_class->dispose = gimp_action_view_dispose;
120 121 122 123 124 125 126
}

static void
gimp_action_view_init (GimpActionView *view)
{
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static void
gimp_action_view_dispose (GObject *object)
{
  GimpActionView *view = GIMP_ACTION_VIEW (object);

  if (view->manager)
    {
      if (view->show_shortcuts)
        {
          GtkAccelGroup *group;

          group = gtk_ui_manager_get_accel_group (GTK_UI_MANAGER (view->manager));

          g_signal_handlers_disconnect_by_func (group,
                                                gimp_action_view_accel_changed,
                                                view);
        }

      g_object_unref (view->manager);
      view->manager = NULL;
    }

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

152 153 154 155 156 157 158 159 160 161 162 163 164 165
GtkWidget *
gimp_action_view_new (GimpUIManager *manager,
                      gboolean       show_shortcuts)
{
  GtkTreeView       *view;
  GtkTreeViewColumn *column;
  GtkCellRenderer   *cell;
  GtkTreeStore      *store;
  GtkAccelGroup     *accel_group;
  GList             *list;

  g_return_val_if_fail (GIMP_IS_UI_MANAGER (manager), NULL);

  store = gtk_tree_store_new (NUM_COLUMNS,
166 167 168 169 170 171 172
                              GTK_TYPE_ACTION,        /* COLUMN_ACTION     */
                              G_TYPE_STRING,          /* COLUMN_STOCK_ID   */
                              G_TYPE_STRING,          /* COLUMN_LABEL      */
                              G_TYPE_STRING,          /* COLUMN_NAME       */
                              G_TYPE_UINT,            /* COLUMN_ACCEL_KEY  */
                              GDK_TYPE_MODIFIER_TYPE, /* COLUMN_ACCEL_MASK */
                              GTK_TYPE_MENU_ITEM);    /* COLUMN_MENU_ITEM  */
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  accel_group = gtk_ui_manager_get_accel_group (GTK_UI_MANAGER (manager));

  for (list = gtk_ui_manager_get_action_groups (GTK_UI_MANAGER (manager));
       list;
       list = g_list_next (list))
    {
      GimpActionGroup *group = list->data;
      GList           *actions;
      GList           *list2;
      GtkTreeIter      group_iter;

      gtk_tree_store_append (store, &group_iter, NULL);

      gtk_tree_store_set (store, &group_iter,
188 189
                          COLUMN_STOCK_ID, group->stock_id,
                          COLUMN_LABEL,    group->label,
190 191 192 193 194 195 196 197 198 199 200 201 202 203
                          -1);

      actions = gtk_action_group_list_actions (GTK_ACTION_GROUP (group));

      actions = g_list_sort (actions, (GCompareFunc) gimp_action_name_compare);

      for (list2 = actions; list2; list2 = g_list_next (list2))
        {
          GtkAction   *action = list2->data;
          const gchar *name   = gtk_action_get_name (action);

          if (! strstr (name, "-menu") &&
              ! strstr (name, "-popup"))
            {
204 205 206 207 208 209 210
              GtkTreeIter      action_iter;
              gchar           *stock_id;
              gchar           *label;
              gchar           *stripped;
              guint            accel_key  = 0;
              GdkModifierType  accel_mask = 0;
              GtkWidget       *menu_item  = NULL;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

              g_object_get (action,
                            "stock-id", &stock_id,
                            "label",    &label,
                            NULL);

              stripped = gimp_strip_uline (label);

              if (show_shortcuts)
                {
                  menu_item = gtk_action_create_menu_item (action);

                  if (GTK_IS_MENU_ITEM (menu_item) &&
                      GTK_IS_ACCEL_LABEL (GTK_BIN (menu_item)->child))
                    {
                      GtkWidget *accel_label = GTK_BIN (menu_item)->child;
                      GClosure  *accel_closure;

                      g_object_get (accel_label,
                                    "accel-closure", &accel_closure,
                                    NULL);

                      if (accel_closure)
234 235 236 237 238 239 240 241 242 243 244 245 246 247
                        {
                          GtkAccelKey *key;

                          key = gtk_accel_group_find (accel_group,
                                                      gimp_action_view_accel_find_func,
                                                      accel_closure);

                          if (key            &&
                              key->accel_key &&
                              key->accel_flags & GTK_ACCEL_VISIBLE)
                            {
                              accel_key  = key->accel_key;
                              accel_mask = key->accel_mods;
                            }
248 249

                          g_closure_unref (accel_closure);
250
                        }
251 252 253 254 255 256 257 258 259 260 261 262 263 264

                      g_object_ref (menu_item);
                      gtk_object_sink (GTK_OBJECT (menu_item));
                    }
                  else if (menu_item)
                    {
                      gtk_object_sink (GTK_OBJECT (menu_item));
                      menu_item = NULL;
                    }
                }

              gtk_tree_store_append (store, &action_iter, &group_iter);

              gtk_tree_store_set (store, &action_iter,
265 266 267 268 269 270 271
                                  COLUMN_ACTION,     action,
                                  COLUMN_STOCK_ID,   stock_id,
                                  COLUMN_LABEL,      stripped,
                                  COLUMN_NAME,       name,
                                  COLUMN_ACCEL_KEY,  accel_key,
                                  COLUMN_ACCEL_MASK, accel_mask,
                                  COLUMN_MENU_ITEM,  menu_item,
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                                  -1);

              g_free (stock_id);
              g_free (label);
              g_free (stripped);

              if (menu_item)
                g_object_unref (menu_item);
            }
        }

      g_list_free (actions);
    }

  view = g_object_new (GIMP_TYPE_ACTION_VIEW,
                       "model",      store,
                       "rules-hint", TRUE,
                       NULL);

  g_object_unref (store);

293 294 295
  GIMP_ACTION_VIEW (view)->manager        = g_object_ref (manager);
  GIMP_ACTION_VIEW (view)->show_shortcuts = show_shortcuts;

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, _("Action"));

  cell = gtk_cell_renderer_pixbuf_new ();
  gtk_tree_view_column_pack_start (column, cell, FALSE);
  gtk_tree_view_column_set_attributes (column, cell,
                                       "stock-id", COLUMN_STOCK_ID,
                                       NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, cell, TRUE);
  gtk_tree_view_column_set_attributes (column, cell,
                                       "text", COLUMN_LABEL,
                                       NULL);

  gtk_tree_view_append_column (view, column);

  if (show_shortcuts)
    {
315 316 317
      g_signal_connect (accel_group, "accel-changed",
                        G_CALLBACK (gimp_action_view_accel_changed),
                        view);
318 319 320 321

      column = gtk_tree_view_column_new ();
      gtk_tree_view_column_set_title (column, _("Shortcut"));

322 323 324
      cell = gimp_cell_renderer_accel_new ();
      cell->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
      GTK_CELL_RENDERER_TEXT (cell)->editable = TRUE;
325 326
      gtk_tree_view_column_pack_start (column, cell, TRUE);
      gtk_tree_view_column_set_attributes (column, cell,
327 328
                                           "accel-key",  COLUMN_ACCEL_KEY,
                                           "accel-mask", COLUMN_ACCEL_MASK,
329 330
                                           NULL);

331 332 333 334
      g_signal_connect (cell, "accel-edited",
                        G_CALLBACK (gimp_action_view_accel_edited),
                        view);

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
      gtk_tree_view_append_column (view, column);
    }
  else
    {
      column = gtk_tree_view_column_new ();
      gtk_tree_view_column_set_title (column, _("Name"));

      cell = gtk_cell_renderer_text_new ();
      gtk_tree_view_column_pack_start (column, cell, TRUE);
      gtk_tree_view_column_set_attributes (column, cell,
                                           "text", COLUMN_NAME,
                                           NULL);

      gtk_tree_view_append_column (view, column);
    }

  return GTK_WIDGET (view);
}


/*  private functions  */

static gboolean
gimp_action_view_accel_find_func (GtkAccelKey *key,
                                  GClosure    *closure,
                                  gpointer     data)
{
  return (GClosure *) data == closure;
}

static void
gimp_action_view_accel_changed (GtkAccelGroup   *accel_group,
                                guint            unused1,
                                GdkModifierType  unused2,
                                GClosure        *accel_closure,
                                GimpActionView  *view)
{
  GtkTreeModel *model;
  GtkTreeIter   iter;
  gboolean      iter_valid;

  model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
  if (! model)
    return;

  for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (model, &iter))
    {
      GtkTreeIter child_iter;
      gboolean    child_valid;

      for (child_valid = gtk_tree_model_iter_children (model, &child_iter,
                                                       &iter);
           child_valid;
           child_valid = gtk_tree_model_iter_next (model, &child_iter))
        {
          GtkWidget *menu_item;

          gtk_tree_model_get (model, &child_iter,
                              COLUMN_MENU_ITEM, &menu_item,
                              -1);

          if (menu_item)
            {
              GClosure *item_closure;

              g_object_get (GTK_BIN (menu_item)->child,
                            "accel-closure", &item_closure,
                            NULL);

              g_object_unref (menu_item);

              if (accel_closure == item_closure)
                {
410 411 412 413 414 415 416
                  GtkAccelKey     *key;
                  guint            accel_key  = 0;
                  GdkModifierType  accel_mask = 0;

                  key = gtk_accel_group_find (accel_group,
                                              gimp_action_view_accel_find_func,
                                              accel_closure);
417

418 419 420 421 422 423 424
                  if (key            &&
                      key->accel_key &&
                      key->accel_flags & GTK_ACCEL_VISIBLE)
                    {
                      accel_key  = key->accel_key;
                      accel_mask = key->accel_mods;
                    }
425 426

                  gtk_tree_store_set (GTK_TREE_STORE (model), &child_iter,
427 428
                                      COLUMN_ACCEL_KEY,  accel_key,
                                      COLUMN_ACCEL_MASK, accel_mask,
429 430 431 432 433 434 435 436
                                      -1);

                  return;
                }
            }
        }
    }
}
437 438 439 440 441 442 443 444 445

typedef struct
{
  gchar           *accel_path;
  guint            accel_key;
  GdkModifierType  accel_mask;
} ConfirmData;

static void
446 447 448
gimp_action_view_conflict_response (GtkWidget   *dialog,
                                    gint         response_id,
                                    ConfirmData *confirm_data)
449
{
450
  gtk_widget_destroy (dialog);
451

452 453
  if (response_id == GTK_RESPONSE_OK)
    {
454 455 456 457 458 459 460 461
      if (! gtk_accel_map_change_entry (confirm_data->accel_path,
                                        confirm_data->accel_key,
                                        confirm_data->accel_mask,
                                        TRUE))
        {
          g_message (_("Changing shortcut failed."));
        }
    }
462 463 464 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 500 501 502 503 504

  g_free (confirm_data->accel_path);
  g_free (confirm_data);
}

static void
gimp_action_view_conflict_confirm (GimpActionView  *view,
                                   GtkAction       *action,
                                   guint            accel_key,
                                   GdkModifierType  accel_mask,
                                   const gchar     *accel_path)
{
  GimpActionGroup *group;
  GimpMessageBox  *box;
  gchar           *label;
  gchar           *stripped;
  gchar           *accel_string;
  ConfirmData     *confirm_data;
  GtkWidget       *dialog;

  g_object_get (action,
                "action-group", &group,
                "label",        &label,
                NULL);

  stripped = gimp_strip_uline (label);
  g_free (label);

  accel_string = gimp_get_accel_string (accel_key, accel_mask);

  confirm_data = g_new0 (ConfirmData, 1);

  confirm_data->accel_path = g_strdup (accel_path);
  confirm_data->accel_key  = accel_key;
  confirm_data->accel_mask = accel_mask;

  dialog =
    gimp_message_dialog_new (_("Conflicting Shortcuts"),
                             GIMP_STOCK_WARNING,
                             gtk_widget_get_toplevel (GTK_WIDGET (view)), 0,
                             gimp_standard_help_func, NULL,

                             GTK_STOCK_CANCEL,         GTK_RESPONSE_CANCEL,
Kevin Cozens's avatar
Kevin Cozens committed
505
                             _("_Reassign shortcut"),  GTK_RESPONSE_OK,
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

                             NULL);

  g_signal_connect (dialog, "response",
                    G_CALLBACK (gimp_action_view_conflict_response),
                    confirm_data);

  box = GIMP_MESSAGE_DIALOG (dialog)->box;

  gimp_message_box_set_primary_text (box,
                                     _("Shortcut \"%s\" is already taken "
                                       "by \"%s\" from the \"%s\" group."),
                                     accel_string, stripped, group->label);
  gimp_message_box_set_text (box,
                             _("Reassigning the shortcut will cause it "
                               "to be removed from \"%s\"."),
                             stripped);

  g_free (stripped);
  g_free (accel_string);

  g_object_unref (group);

  gtk_widget_show (dialog);
530 531 532 533 534
}

static void
gimp_action_view_accel_edited (GimpCellRendererAccel *accel,
                               const char            *path_string,
535
                               gboolean               delete,
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
                               guint                  accel_key,
                               GdkModifierType        accel_mask,
                               GimpActionView        *view)
{
  GtkTreeModel *model;
  GtkTreePath  *path;
  GtkTreeIter   iter;

  model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
  if (! model)
    return;

  path = gtk_tree_path_new_from_string (path_string);

  if (gtk_tree_model_get_iter (model, &iter, path))
    {
      GtkAction      *action;
      GtkActionGroup *group;
      gchar          *accel_path;

      gtk_tree_model_get (model, &iter,
                          COLUMN_ACTION, &action,
                          -1);

      if (! action)
        goto done;

      g_object_get (action, "action-group", &group, NULL);

      if (! group)
        {
          g_object_unref (action);
          goto done;
        }

#ifdef __GNUC__
#warning FIXME: remove accel_path hack
#endif
      accel_path = g_object_get_data (G_OBJECT (action), "gimp-accel-path");

      if (accel_path)
        accel_path = g_strdup (accel_path);
      else
        accel_path = g_strdup_printf ("<Actions>/%s/%s",
                                      gtk_action_group_get_name (group),
                                      gtk_action_get_name (action));

583 584 585 586 587 588 589 590 591 592 593 594
      if (delete)
        {
          if (! gtk_accel_map_change_entry (accel_path, 0, 0, FALSE))
            {
              g_message (_("Removing shortcut failed."));
            }
        }
      else if (! accel_key)
        {
          g_message (_("Invalid shortcut."));
        }
      else
595 596 597 598 599 600 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 638
        {
          if (! gtk_accel_map_change_entry (accel_path,
                                            accel_key, accel_mask, FALSE))
            {
              GtkAction   *conflict_action = NULL;
              GtkTreeIter  iter;
              gboolean     iter_valid;

              for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
                   iter_valid;
                   iter_valid = gtk_tree_model_iter_next (model, &iter))
                {
                  GtkTreeIter child_iter;
                  gboolean    child_valid;

                  for (child_valid = gtk_tree_model_iter_children (model,
                                                                   &child_iter,
                                                                   &iter);
                       child_valid;
                       child_valid = gtk_tree_model_iter_next (model,
                                                               &child_iter))
                    {
                      guint           child_accel_key;
                      GdkModifierType child_accel_mask;

                      gtk_tree_model_get (model, &child_iter,
                                          COLUMN_ACCEL_KEY,  &child_accel_key,
                                          COLUMN_ACCEL_MASK, &child_accel_mask,
                                          -1);

                      if (accel_key  == child_accel_key &&
                          accel_mask == child_accel_mask)
                        {
                          gtk_tree_model_get (model, &child_iter,
                                              COLUMN_ACTION, &conflict_action,
                                              -1);
                          break;
                        }
                    }

                  if (conflict_action)
                    break;
                }

639
              if (conflict_action && conflict_action != action)
640
                {
641 642 643 644
                  gimp_action_view_conflict_confirm (view, conflict_action,
                                                     accel_key,
                                                     accel_mask,
                                                     accel_path);
645 646
                  g_object_unref (conflict_action);
                }
647
              else if (conflict_action != action)
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
                {
                  g_message (_("Changing shortcut failed."));
                }
            }
        }

      g_free (accel_path);
      g_object_unref (group);
      g_object_unref (action);
    }

 done:

  gtk_tree_path_free (path);
}