gimpprocbrowserdialog.c 17.3 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
Elliot Lee's avatar
Elliot Lee committed
3
 *
4
 * gimpprocbrowserdialog.c
Elliot Lee's avatar
Elliot Lee committed
5
 *
6
 * This library is free software: you can redistribute it and/or
7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 3 of the License, or (at your option) any later version.
10 11
 *
 * This library is distributed in the hope that it will be useful,
Elliot Lee's avatar
Elliot Lee committed
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 18
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
19 20
 */

Sven Neumann's avatar
Sven Neumann committed
21
#include "config.h"
22

Daniel Egger's avatar
Daniel Egger committed
23
#include <string.h>
24

25 26
#include <gtk/gtk.h>

27
#include "libgimpwidgets/gimpwidgets.h"
28

29 30 31 32
#include "gimp.h"

#include "gimpuitypes.h"
#include "gimpprocbrowserdialog.h"
33
#include "gimpprocview.h"
34

35
#include "libgimp-intl.h"
Elliot Lee's avatar
Elliot Lee committed
36

37

38 39 40 41 42 43 44 45 46
/**
 * SECTION: gimpprocbrowserdialog
 * @title: GimpProcBrowserDialog
 * @short_description: The dialog for the procedure and plugin browsers.
 *
 * The dialog for the procedure and plugin browsers.
 **/


47
#define DBL_LIST_WIDTH 250
48
#define DBL_WIDTH      (DBL_LIST_WIDTH + 400)
49
#define DBL_HEIGHT     250
50

51

52 53 54
enum
{
  SELECTION_CHANGED,
Sven Neumann's avatar
Sven Neumann committed
55
  ROW_ACTIVATED,
56 57 58
  LAST_SIGNAL
};

59 60 61 62
typedef enum
{
  SEARCH_TYPE_ALL,
  SEARCH_TYPE_NAME,
63 64 65 66 67 68
  SEARCH_TYPE_BLURB,
  SEARCH_TYPE_HELP,
  SEARCH_TYPE_AUTHOR,
  SEARCH_TYPE_COPYRIGHT,
  SEARCH_TYPE_DATE,
  SEARCH_TYPE_PROC_TYPE
69 70
} SearchType;

71 72 73 74 75
enum
{
  COLUMN_PROC_NAME,
  N_COLUMNS
};
76 77


78 79 80 81 82 83 84 85 86 87 88 89
static void       browser_selection_changed (GtkTreeSelection      *sel,
                                             GimpProcBrowserDialog *dialog);
static void       browser_row_activated     (GtkTreeView           *treeview,
                                             GtkTreePath           *path,
                                             GtkTreeViewColumn     *column,
                                             GimpProcBrowserDialog *dialog);
static void       browser_show_procedure    (GimpProcBrowserDialog *dialog,
                                             const gchar           *proc_name);
static void       browser_search            (GimpBrowser           *browser,
                                             const gchar           *query_text,
                                             gint                   search_type,
                                             GimpProcBrowserDialog *dialog);
90

91

92
G_DEFINE_TYPE (GimpProcBrowserDialog, gimp_proc_browser_dialog,
93
               GIMP_TYPE_DIALOG)
94

95 96 97
#define parent_class gimp_proc_browser_dialog_parent_class

static guint dialog_signals[LAST_SIGNAL] = { 0, };
98 99


100 101 102
static void
gimp_proc_browser_dialog_class_init (GimpProcBrowserDialogClass *klass)
{
Sven Neumann's avatar
Sven Neumann committed
103 104 105 106 107 108
  /**
   * GimpProcBrowserDialog::selection-changed:
   * @dialog: the object that received the signal
   *
   * Emitted when the selection in the contained #GtkTreeView changes.
   */
109 110 111 112 113 114 115 116 117 118
  dialog_signals[SELECTION_CHANGED] =
    g_signal_new ("selection-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GimpProcBrowserDialogClass,
                                   selection_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

Sven Neumann's avatar
Sven Neumann committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  /**
   * GimpProcBrowserDialog::row-activated:
   * @dialog: the object that received the signal
   *
   * Emitted when one of the rows in the contained #GtkTreeView is activated.
   */
  dialog_signals[ROW_ACTIVATED] =
    g_signal_new ("row-activated",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GimpProcBrowserDialogClass,
                                   row_activated),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

135
  klass->selection_changed = NULL;
Sven Neumann's avatar
Sven Neumann committed
136
  klass->row_activated     = NULL;
137
}
138

139 140
static void
gimp_proc_browser_dialog_init (GimpProcBrowserDialog *dialog)
Elliot Lee's avatar
Elliot Lee committed
141
{
142 143 144
  GtkWidget        *scrolled_window;
  GtkCellRenderer  *renderer;
  GtkTreeSelection *selection;
145
  GtkWidget        *parent;
146

147 148
  dialog->browser = gimp_browser_new ();
  gimp_browser_add_search_types (GIMP_BROWSER (dialog->browser),
149 150 151 152 153 154 155 156
                                 _("by name"),        SEARCH_TYPE_NAME,
                                 _("by description"), SEARCH_TYPE_BLURB,
                                 _("by help"),        SEARCH_TYPE_HELP,
                                 _("by author"),      SEARCH_TYPE_AUTHOR,
                                 _("by copyright"),   SEARCH_TYPE_COPYRIGHT,
                                 _("by date"),        SEARCH_TYPE_DATE,
                                 _("by type"),        SEARCH_TYPE_PROC_TYPE,
                                 NULL);
157
  gtk_container_set_border_width (GTK_CONTAINER (dialog->browser), 12);
158 159
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      dialog->browser, TRUE, TRUE, 0);
160
  gtk_widget_show (dialog->browser);
161

162
  g_signal_connect (dialog->browser, "search",
163
                    G_CALLBACK (browser_search),
164
                    dialog);
165

Elliot Lee's avatar
Elliot Lee committed
166
  /* list : list in a scrolled_win */
167

168
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
169
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
170
                                       GTK_SHADOW_IN);
171
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
172 173
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_ALWAYS);
174
  gtk_box_pack_start (GTK_BOX (GIMP_BROWSER (dialog->browser)->left_vbox),
175
                      scrolled_window, TRUE, TRUE, 0);
176
  gtk_widget_show (scrolled_window);
177

178
  dialog->tree_view = gtk_tree_view_new ();
179

180 181 182 183
  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_renderer_text_set_fixed_height_from_font
    (GTK_CELL_RENDERER_TEXT (renderer), 1);

184
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (dialog->tree_view),
185 186 187 188
                                               -1, NULL,
                                               renderer,
                                               "text", 0,
                                               NULL);
189
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->tree_view), FALSE);
190

191
  g_signal_connect (dialog->tree_view, "row_activated",
192
                    G_CALLBACK (browser_row_activated),
193
                    dialog);
194

195 196 197
  gtk_widget_set_size_request (dialog->tree_view, DBL_LIST_WIDTH, DBL_HEIGHT);
  gtk_container_add (GTK_CONTAINER (scrolled_window), dialog->tree_view);
  gtk_widget_show (dialog->tree_view);
198

199
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view));
200

201
  g_signal_connect (selection, "changed",
202
                    G_CALLBACK (browser_selection_changed),
203
                    dialog);
Elliot Lee's avatar
Elliot Lee committed
204

205 206 207 208
  parent = gtk_widget_get_parent (GIMP_BROWSER (dialog->browser)->right_vbox);
  parent = gtk_widget_get_parent (parent);

    gtk_widget_set_size_request (parent, DBL_WIDTH - DBL_LIST_WIDTH, -1);
209 210 211 212 213
}


/*  public functions  */

214 215 216 217 218 219 220 221 222 223 224 225 226 227
/**
 * gimp_proc_browser_dialog_new:
 * @title:     The dialog's title.
 * @role:      The dialog's role, see gtk_window_set_role().
 * @help_func: The function which will be called if the user presses "F1".
 * @help_id:   The help_id which will be passed to @help_func.
 * @...:       A %NULL-terminated list destribing the action_area buttons.
 *
 * Create a new #GimpProcBrowserDialog.
 *
 * Return Value: a newly created #GimpProcBrowserDialog.
 *
 * Since: GIMP 2.4
 **/
228
GtkWidget *
229 230 231 232 233
gimp_proc_browser_dialog_new (const gchar  *title,
                              const gchar  *role,
                              GimpHelpFunc  help_func,
                              const gchar  *help_id,
                              ...)
234 235
{
  GimpProcBrowserDialog *dialog;
236
  va_list                args;
Elliot Lee's avatar
Elliot Lee committed
237

238 239 240 241 242 243 244 245 246 247 248 249
  va_start (args, help_id);

  dialog = g_object_new (GIMP_TYPE_PROC_BROWSER_DIALOG,
                         "title",     title,
                         "role",      role,
                         "help-func", help_func,
                         "help-id",   help_id,
                         NULL);

  gimp_dialog_add_buttons_valist (GIMP_DIALOG (dialog), args);

  va_end (args);
Elliot Lee's avatar
Elliot Lee committed
250 251

  /* first search (all procedures) */
252
  browser_search (GIMP_BROWSER (dialog->browser), "", SEARCH_TYPE_ALL, dialog);
253 254 255 256

  return GTK_WIDGET (dialog);
}

257 258 259 260 261 262 263 264 265 266 267
/**
 * gimp_proc_browser_dialog_get_selected:
 * @dialog: a #GimpProcBrowserDialog
 *
 * Retrieves the name of the currently selected procedure.
 *
 * Return Value: The name of the selected procedure of %NULL if no
 *               procedure is selected.
 *
 * Since: GIMP 2.4
 **/
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
gchar *
gimp_proc_browser_dialog_get_selected (GimpProcBrowserDialog *dialog)
{
  GtkTreeSelection *sel;
  GtkTreeIter       iter;

  g_return_val_if_fail (GIMP_IS_PROC_BROWSER_DIALOG (dialog), NULL);

  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view));

  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
    {
      gchar *proc_name;

      gtk_tree_model_get (GTK_TREE_MODEL (dialog->store), &iter,
                          COLUMN_PROC_NAME, &proc_name,
                          -1);

      return proc_name;
    }
Elliot Lee's avatar
Elliot Lee committed
288

289
  return NULL;
290 291
}

292 293 294

/*  private functions  */

295
static void
296 297
browser_selection_changed (GtkTreeSelection      *sel,
                           GimpProcBrowserDialog *dialog)
Elliot Lee's avatar
Elliot Lee committed
298
{
299
  GtkTreeIter iter;
300

301 302
  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
    {
303 304
      gchar *proc_name;

305
      gtk_tree_model_get (GTK_TREE_MODEL (dialog->store), &iter,
306
                          COLUMN_PROC_NAME, &proc_name,
307
                          -1);
308
      browser_show_procedure (dialog, proc_name);
309
      g_free (proc_name);
310
    }
311 312

  g_signal_emit (dialog, dialog_signals[SELECTION_CHANGED], 0);
Elliot Lee's avatar
Elliot Lee committed
313 314
}

315
static void
316 317 318 319
browser_row_activated (GtkTreeView           *treeview,
                       GtkTreePath           *path,
                       GtkTreeViewColumn     *column,
                       GimpProcBrowserDialog *dialog)
Elliot Lee's avatar
Elliot Lee committed
320
{
Sven Neumann's avatar
Sven Neumann committed
321
  g_signal_emit (dialog, dialog_signals[ROW_ACTIVATED], 0);
322
}
Elliot Lee's avatar
Elliot Lee committed
323

324
static void
325 326
browser_show_procedure (GimpProcBrowserDialog *dialog,
                        const gchar           *proc_name)
327
{
328 329 330 331 332 333 334 335 336 337 338
  gchar           *proc_blurb;
  gchar           *proc_help;
  gchar           *proc_author;
  gchar           *proc_copyright;
  gchar           *proc_date;
  GimpPDBProcType  proc_type;
  gint             n_params;
  gint             n_return_vals;
  GimpParamDef    *params;
  GimpParamDef    *return_vals;

339
  gimp_procedural_db_proc_info (proc_name,
340 341 342 343 344 345 346 347 348 349 350 351
                                &proc_blurb,
                                &proc_help,
                                &proc_author,
                                &proc_copyright,
                                &proc_date,
                                &proc_type,
                                &n_params,
                                &n_return_vals,
                                &params,
                                &return_vals);

  gimp_browser_set_widget (GIMP_BROWSER (dialog->browser),
352
                           gimp_proc_view_new (proc_name,
353
                                               NULL,
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
                                               proc_blurb,
                                               proc_help,
                                               proc_author,
                                               proc_copyright,
                                               proc_date,
                                               proc_type,
                                               n_params,
                                               n_return_vals,
                                               params,
                                               return_vals));

  g_free (proc_blurb);
  g_free (proc_help);
  g_free (proc_author);
  g_free (proc_copyright);
  g_free (proc_date);

  gimp_destroy_paramdefs (params,      n_params);
  gimp_destroy_paramdefs (return_vals, n_return_vals);
373 374 375
}

static void
376 377 378 379
browser_search (GimpBrowser           *browser,
                const gchar           *query_text,
                gint                   search_type,
                GimpProcBrowserDialog *dialog)
380
{
381 382 383 384 385 386 387 388 389 390 391 392 393
  gchar  **proc_list;
  gint     num_procs;
  gchar   *str;
  GRegex  *regex;

  /*  first check if the query is a valid regex  */
  regex = g_regex_new (query_text, 0, 0, NULL);

  if (! regex)
    {
      gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->tree_view), NULL);
      dialog->store = NULL;

394 395 396 397
      gimp_browser_show_message (browser, _("No matches"));

      gtk_label_set_text (GTK_LABEL (browser->count_label),
                          _("Search term invalid or incomplete"));
398 399 400 401
      return;
    }

  g_regex_unref (regex);
402

403
  switch (search_type)
404
    {
405 406
    case SEARCH_TYPE_ALL:
      gimp_browser_show_message (browser, _("Searching"));
407

408 409 410
      gimp_procedural_db_query (".*", ".*", ".*", ".*", ".*", ".*", ".*",
                                &num_procs, &proc_list);
      break;
411

412 413 414 415
    case SEARCH_TYPE_NAME:
      {
        GString     *query = g_string_new ("");
        const gchar *q     = query_text;
416

417
        gimp_browser_show_message (browser, _("Searching by name"));
418

419 420 421 422 423 424
        while (*q)
          {
            if ((*q == '_') || (*q == '-'))
              g_string_append (query, "-");
            else
              g_string_append_c (query, *q);
425

426 427 428 429 430 431 432 433 434 435 436 437 438
            q++;
          }

        gimp_procedural_db_query (query->str,
                                  ".*", ".*", ".*", ".*", ".*", ".*",
                                  &num_procs, &proc_list);

        g_string_free (query, TRUE);
      }
      break;

    case SEARCH_TYPE_BLURB:
      gimp_browser_show_message (browser, _("Searching by description"));
439 440 441

      gimp_procedural_db_query (".*", query_text, ".*", ".*", ".*", ".*", ".*",
                                &num_procs, &proc_list);
442 443 444 445
      break;

    case SEARCH_TYPE_HELP:
      gimp_browser_show_message (browser, _("Searching by help"));
446 447 448

      gimp_procedural_db_query (".*", ".*", query_text, ".*", ".*", ".*", ".*",
                                &num_procs, &proc_list);
449 450 451 452
      break;

    case SEARCH_TYPE_AUTHOR:
      gimp_browser_show_message (browser, _("Searching by author"));
453 454 455

      gimp_procedural_db_query (".*", ".*", ".*", query_text, ".*", ".*", ".*",
                                &num_procs, &proc_list);
456 457 458 459
      break;

    case SEARCH_TYPE_COPYRIGHT:
      gimp_browser_show_message (browser, _("Searching by copyright"));
460 461 462

      gimp_procedural_db_query (".*", ".*", ".*", ".*", query_text, ".*", ".*",
                                &num_procs, &proc_list);
463 464 465 466
      break;

    case SEARCH_TYPE_DATE:
      gimp_browser_show_message (browser, _("Searching by date"));
467 468 469

      gimp_procedural_db_query (".*", ".*", ".*", ".*", ".*", query_text, ".*",
                                &num_procs, &proc_list);
470
      break;
471

472 473
    case SEARCH_TYPE_PROC_TYPE:
      gimp_browser_show_message (browser, _("Searching by type"));
474

475
      gimp_procedural_db_query (".*", ".*", ".*", ".*", ".*", ".*", query_text,
476
                                &num_procs, &proc_list);
477
      break;
478 479
    }

480 481
  if (! query_text || strlen (query_text) == 0)
    {
482 483 484 485
      str = g_strdup_printf (dngettext (GETTEXT_PACKAGE "-libgimp",
                                        "%d procedure",
                                        "%d procedures",
                                        num_procs), num_procs);
486
    }
487
  else
488 489 490 491 492 493 494
    {
      switch (num_procs)
        {
        case 0:
          str = g_strdup (_("No matches for your query"));
          break;
        default:
495 496 497 498
          str = g_strdup_printf (dngettext (GETTEXT_PACKAGE "-libgimp",
                                            "%d procedure matches your query",
                                            "%d procedures match your query",
                                            num_procs), num_procs);
499 500 501
          break;
        }
    }
502

503
  gtk_label_set_text (GTK_LABEL (browser->count_label), str);
504 505 506 507
  g_free (str);

  if (num_procs > 0)
    {
508 509 510
      GtkTreeSelection *selection;
      GtkTreeIter       iter;
      gint              i;
511

512 513 514 515 516
      dialog->store = gtk_list_store_new (N_COLUMNS,
                                          G_TYPE_STRING);
      gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->tree_view),
                               GTK_TREE_MODEL (dialog->store));
      g_object_unref (dialog->store);
517 518 519

      for (i = 0; i < num_procs; i++)
        {
520 521
          gtk_list_store_append (dialog->store, &iter);
          gtk_list_store_set (dialog->store, &iter,
522 523 524 525 526 527 528 529
                              COLUMN_PROC_NAME, proc_list[i],
                              -1);

          g_free (proc_list[i]);
        }

      g_free (proc_list);

530
      gtk_tree_view_columns_autosize (GTK_TREE_VIEW (dialog->tree_view));
531

532
      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (dialog->store),
533 534
                                            COLUMN_PROC_NAME,
                                            GTK_SORT_ASCENDING);
535

536 537
      gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dialog->store), &iter);
      selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->tree_view));
538
      gtk_tree_selection_select_iter (selection, &iter);
539 540 541
    }
  else
    {
542 543
      gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->tree_view), NULL);
      dialog->store = NULL;
544

545
      gimp_browser_show_message (browser, _("No matches"));
546
    }
Elliot Lee's avatar
Elliot Lee committed
547
}