gimpactionview.c 22 KB
Newer Older
1 2 3 4
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpactionview.c
5
 * Copyright (C) 2004-2005  Michael Natterer <mitch@gimp.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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
#include "gimpuimanager.h"
#include "gimpwidgets-utils.h"

#include "gimp-intl.h"


/*  local function prototypes  */

47 48
static void     gimp_action_view_dispose         (GObject         *object);

49 50 51 52 53 54 55 56
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);
57 58
static void     gimp_action_view_accel_edited    (GimpCellRendererAccel *accel,
                                                  const char      *path_string,
59
                                                  gboolean         delete,
60 61 62 63
                                                  guint            accel_key,
                                                  GdkModifierType  accel_mask,
                                                  GimpActionView  *view);

64

65
G_DEFINE_TYPE (GimpActionView, gimp_action_view, GTK_TYPE_TREE_VIEW);
66

67
#define parent_class gimp_action_view_parent_class
68 69 70 71 72


static void
gimp_action_view_class_init (GimpActionViewClass *klass)
{
73 74 75
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = gimp_action_view_dispose;
76 77 78 79 80 81 82
}

static void
gimp_action_view_init (GimpActionView *view)
{
}

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
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);
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 152 153 154 155 156
static gboolean
idle_start_editing (GtkTreeView *tree_view)
{
  GtkTreePath *path;

  path = g_object_get_data (G_OBJECT (tree_view), "start_editing_path");

  if (path)
    {
      gtk_widget_grab_focus (GTK_WIDGET (tree_view));

      gtk_tree_view_set_cursor (tree_view, path,
                                gtk_tree_view_get_column (tree_view, 1),
                                TRUE);

      g_object_set_data (G_OBJECT (tree_view), "start_editing_path", NULL);
    }

  return FALSE;
}

static gboolean
gimp_action_view_button_press (GtkWidget      *widget,
                               GdkEventButton *event)
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
  GtkTreePath *path;

  if (event->window != gtk_tree_view_get_bin_window (tree_view))
    return FALSE;

  if (gtk_tree_view_get_path_at_pos (tree_view,
                                     (gint) event->x,
				     (gint) event->y,
				     &path, NULL,
                                     NULL, NULL))
    {
      GClosure *closure;
      GSource  *source;

      if (gtk_tree_path_get_depth (path) == 1)
        {
	  gtk_tree_path_free (path);
	  return FALSE;
	}

      g_object_set_data_full (G_OBJECT (tree_view), "start_editing_path",
                              path, (GDestroyNotify) gtk_tree_path_free);

157
      g_signal_stop_emission_by_name (tree_view, "button-press-event");
158 159 160 161 162 163 164

      closure = g_cclosure_new_object (G_CALLBACK (idle_start_editing),
                                       G_OBJECT (tree_view));

      source = g_idle_source_new ();
      g_source_set_closure (source, closure);
      g_source_attach (source, NULL);
165
      g_source_unref (source);
166 167 168 169 170
    }

  return TRUE;
}

171 172
GtkWidget *
gimp_action_view_new (GimpUIManager *manager,
173
                      const gchar   *select_action,
174 175 176 177 178 179 180 181
                      gboolean       show_shortcuts)
{
  GtkTreeView       *view;
  GtkTreeViewColumn *column;
  GtkCellRenderer   *cell;
  GtkTreeStore      *store;
  GtkAccelGroup     *accel_group;
  GList             *list;
182
  GtkTreePath       *select_path = NULL;
183 184 185

  g_return_val_if_fail (GIMP_IS_UI_MANAGER (manager), NULL);

186
  store = gtk_tree_store_new (GIMP_ACTION_VIEW_NUM_COLUMNS,
187 188 189 190 191 192 193
                              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    */
                              G_TYPE_CLOSURE);        /* COLUMN_ACCEL_CLOSURE */
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

  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,
209 210
                          GIMP_ACTION_VIEW_COLUMN_STOCK_ID, group->stock_id,
                          GIMP_ACTION_VIEW_COLUMN_LABEL,    group->label,
211 212 213 214 215 216 217 218
                          -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))
        {
219 220 221 222 223 224 225 226 227 228
          GtkAction       *action        = list2->data;
          const gchar     *name          = gtk_action_get_name (action);
          gchar           *stock_id;
          gchar           *label;
          gchar           *tmp;
          guint            accel_key     = 0;
          GdkModifierType  accel_mask    = 0;
          GClosure        *accel_closure = NULL;
          GtkTreeIter      action_iter;

229 230 231
          if (strstr (name, "-menu")  ||
              strstr (name, "-popup") ||
              name[0] == '<')
232 233 234 235 236 237
            continue;

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

239 240
          label = gimp_strip_uline (tmp);
          g_free (tmp);
241

242 243
          if (show_shortcuts)
            {
244
              accel_closure = gtk_action_get_accel_closure (action);
245

246
              if (accel_closure)
247
                {
248
                  GtkAccelKey *key;
249

250 251 252 253 254 255 256
                  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)
257
                    {
258 259
                      accel_key  = key->accel_key;
                      accel_mask = key->accel_mods;
260 261
                    }
                }
262
            }
263

264
          gtk_tree_store_append (store, &action_iter, &group_iter);
265

266 267 268 269 270 271 272 273 274
          gtk_tree_store_set (store, &action_iter,
                              GIMP_ACTION_VIEW_COLUMN_ACTION,        action,
                              GIMP_ACTION_VIEW_COLUMN_STOCK_ID,      stock_id,
                              GIMP_ACTION_VIEW_COLUMN_LABEL,         label,
                              GIMP_ACTION_VIEW_COLUMN_NAME,          name,
                              GIMP_ACTION_VIEW_COLUMN_ACCEL_KEY,     accel_key,
                              GIMP_ACTION_VIEW_COLUMN_ACCEL_MASK,    accel_mask,
                              GIMP_ACTION_VIEW_COLUMN_ACCEL_CLOSURE, accel_closure,
                              -1);
275

276 277
          g_free (stock_id);
          g_free (label);
278

279 280 281 282
          if (select_action && ! strcmp (select_action, name))
            {
              select_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store),
                                                     &action_iter);
283 284 285 286 287 288 289 290 291 292 293 294 295
            }
        }

      g_list_free (actions);
    }

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

  g_object_unref (store);

296 297 298
  GIMP_ACTION_VIEW (view)->manager        = g_object_ref (manager);
  GIMP_ACTION_VIEW (view)->show_shortcuts = show_shortcuts;

Michael Natterer's avatar
Michael Natterer committed
299 300 301
  gtk_tree_view_set_search_column (GTK_TREE_VIEW (view),
                                   GIMP_ACTION_VIEW_COLUMN_LABEL);

302 303 304 305 306 307
  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,
308 309
                                       "stock-id",
                                       GIMP_ACTION_VIEW_COLUMN_STOCK_ID,
310 311 312 313 314
                                       NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, cell, TRUE);
  gtk_tree_view_column_set_attributes (column, cell,
315 316
                                       "text",
                                       GIMP_ACTION_VIEW_COLUMN_LABEL,
317 318 319 320 321 322
                                       NULL);

  gtk_tree_view_append_column (view, column);

  if (show_shortcuts)
    {
323
      g_signal_connect (view, "button-press-event",
324 325 326
                        G_CALLBACK (gimp_action_view_button_press),
                        NULL);

327 328 329
      g_signal_connect (accel_group, "accel-changed",
                        G_CALLBACK (gimp_action_view_accel_changed),
                        view);
330 331 332 333

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

334 335 336
      cell = gimp_cell_renderer_accel_new ();
      cell->mode = GTK_CELL_RENDERER_MODE_EDITABLE;
      GTK_CELL_RENDERER_TEXT (cell)->editable = TRUE;
337 338
      gtk_tree_view_column_pack_start (column, cell, TRUE);
      gtk_tree_view_column_set_attributes (column, cell,
339 340 341 342
                                           "accel-key",
                                           GIMP_ACTION_VIEW_COLUMN_ACCEL_KEY,
                                           "accel-mask",
                                           GIMP_ACTION_VIEW_COLUMN_ACCEL_MASK,
343 344
                                           NULL);

345 346 347 348
      g_signal_connect (cell, "accel-edited",
                        G_CALLBACK (gimp_action_view_accel_edited),
                        view);

349 350 351 352 353 354 355 356 357 358
      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,
359 360
                                           "text",
                                           GIMP_ACTION_VIEW_COLUMN_NAME,
361 362 363 364 365
                                           NULL);

      gtk_tree_view_append_column (view, column);
    }

366 367 368 369 370 371 372 373 374 375 376 377
  if (select_path)
    {
      GtkTreeSelection *sel = gtk_tree_view_get_selection (view);
      GtkTreePath      *expand;

      expand = gtk_tree_path_copy (select_path);
      gtk_tree_path_up (expand);
      gtk_tree_view_expand_row (view, expand, FALSE);

      gtk_tree_selection_select_path (sel, select_path);

      gtk_tree_view_scroll_to_cell (view, select_path, NULL,
378
                                    TRUE, 0.5, 0.0);
379 380 381 382 383

      gtk_tree_path_free (select_path);
      gtk_tree_path_free (expand);
    }

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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  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))
        {
425
          GClosure *closure;
426 427

          gtk_tree_model_get (model, &child_iter,
428
                              GIMP_ACTION_VIEW_COLUMN_ACCEL_CLOSURE, &closure,
429 430
                              -1);

431 432
          if (closure)
            g_closure_unref (closure);
433

434 435 436 437 438
          if (accel_closure == closure)
            {
              GtkAccelKey     *key;
              guint            accel_key  = 0;
              GdkModifierType  accel_mask = 0;
439

440 441 442
              key = gtk_accel_group_find (accel_group,
                                          gimp_action_view_accel_find_func,
                                          accel_closure);
443

444 445 446
              if (key            &&
                  key->accel_key &&
                  key->accel_flags & GTK_ACCEL_VISIBLE)
447
                {
448 449 450
                  accel_key  = key->accel_key;
                  accel_mask = key->accel_mods;
                }
451

452 453 454 455
              gtk_tree_store_set (GTK_TREE_STORE (model), &child_iter,
                                  GIMP_ACTION_VIEW_COLUMN_ACCEL_KEY,  accel_key,
                                  GIMP_ACTION_VIEW_COLUMN_ACCEL_MASK, accel_mask,
                                  -1);
456

457
              return;
458 459 460 461
            }
        }
    }
}
462 463 464 465 466 467 468 469 470

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

static void
471 472 473
gimp_action_view_conflict_response (GtkWidget   *dialog,
                                    gint         response_id,
                                    ConfirmData *confirm_data)
474
{
475
  gtk_widget_destroy (dialog);
476

477 478
  if (response_id == GTK_RESPONSE_OK)
    {
479 480 481 482 483 484 485 486
      if (! gtk_accel_map_change_entry (confirm_data->accel_path,
                                        confirm_data->accel_key,
                                        confirm_data->accel_mask,
                                        TRUE))
        {
          g_message (_("Changing shortcut failed."));
        }
    }
487 488 489 490 491 492 493 494 495 496 497 498 499 500

  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;
  gchar           *label;
501
  gchar           *tmp;
502 503 504
  gchar           *accel_string;
  ConfirmData     *confirm_data;
  GtkWidget       *dialog;
505
  GimpMessageBox  *box;
506 507 508

  g_object_get (action,
                "action-group", &group,
509
                "label",        &tmp,
510 511
                NULL);

512 513
  label = gimp_strip_uline (tmp);
  g_free (tmp);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

  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
530
                             _("_Reassign shortcut"),  GTK_RESPONSE_OK,
531 532 533 534 535 536 537 538 539 540 541 542

                             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."),
543
                                     accel_string, label, group->label);
544 545 546
  gimp_message_box_set_text (box,
                             _("Reassigning the shortcut will cause it "
                               "to be removed from \"%s\"."),
547
                             label);
548

549
  g_free (label);
550 551 552 553 554
  g_free (accel_string);

  g_object_unref (group);

  gtk_widget_show (dialog);
555 556 557 558 559
}

static void
gimp_action_view_accel_edited (GimpCellRendererAccel *accel,
                               const char            *path_string,
560
                               gboolean               delete,
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
                               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;
579
      const gchar    *accel_path;
580 581

      gtk_tree_model_get (model, &iter,
582
                          GIMP_ACTION_VIEW_COLUMN_ACTION, &action,
583 584 585 586 587 588 589 590 591 592 593 594 595
                          -1);

      if (! action)
        goto done;

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

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

596
      accel_path = gtk_action_get_accel_path (action);
597

598 599 600 601 602 603 604 605 606 607 608
      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."));
        }
609 610
      else if (! gtk_accel_map_change_entry (accel_path,
                                             accel_key, accel_mask, FALSE))
611
        {
612 613 614
          GtkAction   *conflict_action = NULL;
          GtkTreeIter  iter;
          gboolean     iter_valid;
615

616 617 618 619 620 621 622 623 624 625 626 627
          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))
628
                {
629 630
                  guint           child_accel_key;
                  GdkModifierType child_accel_mask;
631

632 633 634 635 636 637 638 639 640 641
                  gtk_tree_model_get (model, &child_iter,
                                      GIMP_ACTION_VIEW_COLUMN_ACCEL_KEY,
                                      &child_accel_key,
                                      GIMP_ACTION_VIEW_COLUMN_ACCEL_MASK,
                                      &child_accel_mask,
                                      -1);

                  if (accel_key  == child_accel_key &&
                      accel_mask == child_accel_mask)
                    {
642
                      gtk_tree_model_get (model, &child_iter,
643 644
                                          GIMP_ACTION_VIEW_COLUMN_ACTION,
                                          &conflict_action,
645
                                          -1);
646
                      break;
647 648 649
                    }
                }

650 651 652 653 654 655 656
              if (conflict_action)
                break;
            }

          if (conflict_action != action)
            {
              if (conflict_action)
657
                {
658 659 660 661
                  gimp_action_view_conflict_confirm (view, conflict_action,
                                                     accel_key,
                                                     accel_mask,
                                                     accel_path);
662 663
                  g_object_unref (conflict_action);
                }
664
              else
665 666 667 668 669 670 671 672 673 674 675 676 677 678
                {
                  g_message (_("Changing shortcut failed."));
                }
            }
        }

      g_object_unref (group);
      g_object_unref (action);
    }

 done:

  gtk_tree_path_free (path);
}