gimpprocbrowserdialog.c 16.7 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
 * License along with this library.  If not, see
18
 * <https://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
#include <gegl.h>
26 27
#include <gtk/gtk.h>

28
#include "libgimpwidgets/gimpwidgets.h"
29

30 31 32 33
#include "gimp.h"

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

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

38

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


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

52

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

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

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


79 80 81 82 83 84 85 86 87 88 89
struct _GimpProcBrowserDialogPrivate
{
  GtkWidget    *browser;

  GtkListStore *store;
  GtkWidget    *tree_view;
};

#define GET_PRIVATE(obj) (((GimpProcBrowserDialog *) (obj))->priv)


90 91 92 93 94 95 96 97 98 99
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_search            (GimpBrowser           *browser,
                                             const gchar           *query_text,
                                             gint                   search_type,
                                             GimpProcBrowserDialog *dialog);
100

101

102 103
G_DEFINE_TYPE_WITH_PRIVATE (GimpProcBrowserDialog, gimp_proc_browser_dialog,
                            GIMP_TYPE_DIALOG)
104

105 106 107
#define parent_class gimp_proc_browser_dialog_parent_class

static guint dialog_signals[LAST_SIGNAL] = { 0, };
108 109


110 111 112
static void
gimp_proc_browser_dialog_class_init (GimpProcBrowserDialogClass *klass)
{
Sven Neumann's avatar
Sven Neumann committed
113 114 115 116 117 118
  /**
   * GimpProcBrowserDialog::selection-changed:
   * @dialog: the object that received the signal
   *
   * Emitted when the selection in the contained #GtkTreeView changes.
   */
119 120 121 122 123 124 125 126 127 128
  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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  /**
   * 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);

145
  klass->selection_changed = NULL;
Sven Neumann's avatar
Sven Neumann committed
146
  klass->row_activated     = NULL;
147
}
148

149 150
static void
gimp_proc_browser_dialog_init (GimpProcBrowserDialog *dialog)
Elliot Lee's avatar
Elliot Lee committed
151
{
152 153 154 155 156 157
  GimpProcBrowserDialogPrivate *priv;
  GtkWidget                    *scrolled_window;
  GtkCellRenderer              *renderer;
  GtkTreeSelection             *selection;
  GtkWidget                    *parent;

158
  dialog->priv = gimp_proc_browser_dialog_get_instance_private (dialog);
159 160

  priv = GET_PRIVATE (dialog);
161

162 163
  priv->browser = gimp_browser_new ();
  gimp_browser_add_search_types (GIMP_BROWSER (priv->browser),
164 165 166
                                 _("by name"),        SEARCH_TYPE_NAME,
                                 _("by description"), SEARCH_TYPE_BLURB,
                                 _("by help"),        SEARCH_TYPE_HELP,
167
                                 _("by authors"),     SEARCH_TYPE_AUTHORS,
168 169 170 171
                                 _("by copyright"),   SEARCH_TYPE_COPYRIGHT,
                                 _("by date"),        SEARCH_TYPE_DATE,
                                 _("by type"),        SEARCH_TYPE_PROC_TYPE,
                                 NULL);
172
  gtk_container_set_border_width (GTK_CONTAINER (priv->browser), 12);
173
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
174 175
                      priv->browser, TRUE, TRUE, 0);
  gtk_widget_show (priv->browser);
176

177
  g_signal_connect (priv->browser, "search",
178
                    G_CALLBACK (browser_search),
179
                    dialog);
180

Elliot Lee's avatar
Elliot Lee committed
181
  /* list : list in a scrolled_win */
182

183
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
184
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
185
                                       GTK_SHADOW_IN);
186
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
187 188
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_ALWAYS);
189
  gtk_box_pack_start (GTK_BOX (gimp_browser_get_left_vbox (GIMP_BROWSER (priv->browser))),
190
                      scrolled_window, TRUE, TRUE, 0);
191
  gtk_widget_show (scrolled_window);
192

193
  priv->tree_view = gtk_tree_view_new ();
194

195 196 197 198
  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_renderer_text_set_fixed_height_from_font
    (GTK_CELL_RENDERER_TEXT (renderer), 1);

199
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view),
200 201 202 203
                                               -1, NULL,
                                               renderer,
                                               "text", 0,
                                               NULL);
204
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
205

206
  g_signal_connect (priv->tree_view, "row_activated",
207
                    G_CALLBACK (browser_row_activated),
208
                    dialog);
209

210 211 212
  gtk_widget_set_size_request (priv->tree_view, DBL_LIST_WIDTH, DBL_HEIGHT);
  gtk_container_add (GTK_CONTAINER (scrolled_window), priv->tree_view);
  gtk_widget_show (priv->tree_view);
213

214
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
215

216
  g_signal_connect (selection, "changed",
217
                    G_CALLBACK (browser_selection_changed),
218
                    dialog);
Elliot Lee's avatar
Elliot Lee committed
219

220
  parent = gtk_widget_get_parent (gimp_browser_get_right_vbox (GIMP_BROWSER (priv->browser)));
221 222
  parent = gtk_widget_get_parent (parent);

223
  gtk_widget_set_size_request (parent, DBL_WIDTH - DBL_LIST_WIDTH, -1);
224 225 226 227

  /* first search (all procedures) */
  browser_search (GIMP_BROWSER (dialog->priv->browser),
                  "", SEARCH_TYPE_ALL, dialog);
228 229 230 231 232
}


/*  public functions  */

233 234 235 236
/**
 * gimp_proc_browser_dialog_new:
 * @title:     The dialog's title.
 * @role:      The dialog's role, see gtk_window_set_role().
237 238
 * @help_func: (scope async): The function which will be called if
 *             the user presses "F1".
239 240 241 242 243
 * @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.
 *
244
 * Returns: a newly created #GimpProcBrowserDialog.
245
 *
246
 * Since: 2.4
247
 **/
248
GtkWidget *
249 250 251 252 253
gimp_proc_browser_dialog_new (const gchar  *title,
                              const gchar  *role,
                              GimpHelpFunc  help_func,
                              const gchar  *help_id,
                              ...)
254 255
{
  GimpProcBrowserDialog *dialog;
256
  va_list                args;
257
  gboolean               use_header_bar;
Elliot Lee's avatar
Elliot Lee committed
258

259 260
  va_start (args, help_id);

261 262 263 264
  g_object_get (gtk_settings_get_default (),
                "gtk-dialogs-use-header", &use_header_bar,
                NULL);

265
  dialog = g_object_new (GIMP_TYPE_PROC_BROWSER_DIALOG,
266 267 268 269 270
                         "title",          title,
                         "role",           role,
                         "help-func",      help_func,
                         "help-id",        help_id,
                         "use-header-bar", use_header_bar,
271 272 273 274 275
                         NULL);

  gimp_dialog_add_buttons_valist (GIMP_DIALOG (dialog), args);

  va_end (args);
Elliot Lee's avatar
Elliot Lee committed
276

277 278 279
  return GTK_WIDGET (dialog);
}

280 281 282 283 284 285
/**
 * gimp_proc_browser_dialog_get_selected:
 * @dialog: a #GimpProcBrowserDialog
 *
 * Retrieves the name of the currently selected procedure.
 *
286
 * Returns: (nullable): The name of the selected procedure of %NULL if no
287 288
 *               procedure is selected.
 *
289
 * Since: 2.4
290
 **/
291 292 293 294 295 296 297 298
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);

299
  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->priv->tree_view));
300 301 302 303 304

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

305
      gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->store), &iter,
306 307 308 309 310
                          COLUMN_PROC_NAME, &proc_name,
                          -1);

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

312
  return NULL;
313 314
}

315 316 317

/*  private functions  */

318
static void
319 320
browser_selection_changed (GtkTreeSelection      *sel,
                           GimpProcBrowserDialog *dialog)
Elliot Lee's avatar
Elliot Lee committed
321
{
322
  GtkTreeIter iter;
323

324 325
  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
    {
326 327
      gchar *proc_name;

328
      gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->store), &iter,
329
                          COLUMN_PROC_NAME, &proc_name,
330
                          -1);
331 332

      gimp_browser_set_widget (GIMP_BROWSER (dialog->priv->browser),
333
                               gimp_proc_view_new (proc_name));
334

335
      g_free (proc_name);
336
    }
337 338

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

341
static void
342 343 344 345
browser_row_activated (GtkTreeView           *treeview,
                       GtkTreePath           *path,
                       GtkTreeViewColumn     *column,
                       GimpProcBrowserDialog *dialog)
Elliot Lee's avatar
Elliot Lee committed
346
{
Sven Neumann's avatar
Sven Neumann committed
347
  g_signal_emit (dialog, dialog_signals[ROW_ACTIVATED], 0);
348
}
Elliot Lee's avatar
Elliot Lee committed
349

350
static void
351 352 353 354
browser_search (GimpBrowser           *browser,
                const gchar           *query_text,
                gint                   search_type,
                GimpProcBrowserDialog *dialog)
355
{
356 357 358
  GimpProcBrowserDialogPrivate  *priv      = GET_PRIVATE (dialog);
  GimpPDB                       *pdb       = gimp_get_pdb ();
  gchar                        **proc_list = NULL;
359 360 361
  gint                           num_procs;
  gchar                         *str;
  GRegex                        *regex;
362 363 364 365 366 367

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

  if (! regex)
    {
368 369
      gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
      priv->store = NULL;
370

371 372
      gimp_browser_show_message (browser, _("No matches"));

373 374
      gimp_browser_set_search_summary (browser,
                                       _("Search term invalid or incomplete"));
375 376 377 378
      return;
    }

  g_regex_unref (regex);
379

380
  switch (search_type)
381
    {
382 383
    case SEARCH_TYPE_ALL:
      gimp_browser_show_message (browser, _("Searching"));
384

385 386 387
      proc_list = gimp_pdb_query_procedures (pdb,
                                             ".*", ".*", ".*", ".*", ".*",
                                             ".*", ".*", ".*", &num_procs);
388
      break;
389

390 391 392 393
    case SEARCH_TYPE_NAME:
      {
        GString     *query = g_string_new ("");
        const gchar *q     = query_text;
394

395
        gimp_browser_show_message (browser, _("Searching by name"));
396

397 398 399 400 401 402
        while (*q)
          {
            if ((*q == '_') || (*q == '-'))
              g_string_append (query, "-");
            else
              g_string_append_c (query, *q);
403

404 405 406
            q++;
          }

407 408 409
        proc_list = gimp_pdb_query_procedures (pdb,
                                               query->str, ".*", ".*", ".*", ".*",
                                               ".*", ".*", ".*", &num_procs);
410 411 412 413 414 415 416

        g_string_free (query, TRUE);
      }
      break;

    case SEARCH_TYPE_BLURB:
      gimp_browser_show_message (browser, _("Searching by description"));
417

418 419 420
      proc_list = gimp_pdb_query_procedures (pdb,
                                             ".*", query_text, ".*", ".*", ".*",
                                             ".*", ".*", ".*", &num_procs);
421 422 423 424
      break;

    case SEARCH_TYPE_HELP:
      gimp_browser_show_message (browser, _("Searching by help"));
425

426 427 428
      proc_list = gimp_pdb_query_procedures (pdb,
                                             ".*", ".*", query_text, ".*", ".*",
                                             ".*", ".*", ".*", &num_procs);
429 430
      break;

431 432
    case SEARCH_TYPE_AUTHORS:
      gimp_browser_show_message (browser, _("Searching by authors"));
433

434 435 436
      proc_list = gimp_pdb_query_procedures (pdb,
                                             ".*", ".*", ".*", ".*", query_text,
                                             ".*", ".*", ".*", &num_procs);
437 438 439 440
      break;

    case SEARCH_TYPE_COPYRIGHT:
      gimp_browser_show_message (browser, _("Searching by copyright"));
441

442 443 444
      proc_list = gimp_pdb_query_procedures (pdb,
                                             ".*", ".*", ".*", ".*", ".*",
                                             query_text, ".*", ".*", &num_procs);
445 446 447 448
      break;

    case SEARCH_TYPE_DATE:
      gimp_browser_show_message (browser, _("Searching by date"));
449

450 451 452
      proc_list = gimp_pdb_query_procedures (pdb,
                                             ".*", ".*", ".*", ".*", ".*",
                                             ".*", query_text, ".*", &num_procs);
453
      break;
454

455 456
    case SEARCH_TYPE_PROC_TYPE:
      gimp_browser_show_message (browser, _("Searching by type"));
457

458 459 460
      proc_list = gimp_pdb_query_procedures (pdb,
                                             ".*", ".*", ".*", ".*", ".*",
                                             ".*", ".*", query_text, &num_procs);
461
      break;
462 463
    }

464 465
  if (! query_text || strlen (query_text) == 0)
    {
466 467 468 469
      str = g_strdup_printf (dngettext (GETTEXT_PACKAGE "-libgimp",
                                        "%d procedure",
                                        "%d procedures",
                                        num_procs), num_procs);
470
    }
471
  else
472 473 474 475 476 477 478
    {
      switch (num_procs)
        {
        case 0:
          str = g_strdup (_("No matches for your query"));
          break;
        default:
479 480 481 482
          str = g_strdup_printf (dngettext (GETTEXT_PACKAGE "-libgimp",
                                            "%d procedure matches your query",
                                            "%d procedures match your query",
                                            num_procs), num_procs);
483 484 485
          break;
        }
    }
486

487
  gimp_browser_set_search_summary (browser, str);
488 489 490 491
  g_free (str);

  if (num_procs > 0)
    {
492 493 494
      GtkTreeSelection *selection;
      GtkTreeIter       iter;
      gint              i;
495

496
      priv->store = gtk_list_store_new (N_COLUMNS,
497
                                          G_TYPE_STRING);
498 499 500
      gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view),
                               GTK_TREE_MODEL (priv->store));
      g_object_unref (priv->store);
501 502 503

      for (i = 0; i < num_procs; i++)
        {
504 505
          gtk_list_store_append (priv->store, &iter);
          gtk_list_store_set (priv->store, &iter,
506 507 508 509
                              COLUMN_PROC_NAME, proc_list[i],
                              -1);
        }

510
      g_strfreev (proc_list);
511

512
      gtk_tree_view_columns_autosize (GTK_TREE_VIEW (priv->tree_view));
513

514
      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
515 516
                                            COLUMN_PROC_NAME,
                                            GTK_SORT_ASCENDING);
517

518 519
      gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store), &iter);
      selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
520
      gtk_tree_selection_select_iter (selection, &iter);
521 522 523
    }
  else
    {
524 525
      gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
      priv->store = NULL;
526

527
      gimp_browser_show_message (browser, _("No matches"));
528
    }
Elliot Lee's avatar
Elliot Lee committed
529
}