gimpprocbrowserdialog.c 17.3 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
18
 */

19
/*
20
21
22
23
24
 * dbbrowser_utils.c
 * 0.08  26th sept 97  by Thomas NOEL <thomas@minet.net>
 *
 * 98/12/13  Sven Neumann <sven@gimp.org> : added help display
 */
Elliot Lee's avatar
Elliot Lee committed
25

Sven Neumann's avatar
Sven Neumann committed
26
#include "config.h"
27

Daniel Egger's avatar
Daniel Egger committed
28
#include <string.h>
29

30
31
32
33
34
#include <gtk/gtk.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

35
#include "gimpprocbrowser.h"
36
#include "gimpprocview.h"
37

Sven Neumann's avatar
Sven Neumann committed
38
#include "libgimp/stdplugins-intl.h"
Elliot Lee's avatar
Elliot Lee committed
39

40

41
#define DBL_LIST_WIDTH 250
42
#define DBL_WIDTH      (DBL_LIST_WIDTH + 400)
43
#define DBL_HEIGHT     250
44

45
46
47
48
49

typedef enum
{
  SEARCH_TYPE_ALL,
  SEARCH_TYPE_NAME,
50
51
52
53
54
55
  SEARCH_TYPE_BLURB,
  SEARCH_TYPE_HELP,
  SEARCH_TYPE_AUTHOR,
  SEARCH_TYPE_COPYRIGHT,
  SEARCH_TYPE_DATE,
  SEARCH_TYPE_PROC_TYPE
56
57
} SearchType;

58
59
60
61
62
63
enum
{
  COLUMN_LABEL,
  COLUMN_PROC_NAME,
  N_COLUMNS
};
64
65
66

typedef struct
{
67
  GtkWidget        *dialog;
68

69
  GtkWidget        *browser;
70

71
  GtkListStore     *store;
72
  GtkTreeView      *tree_view;
73

74
  /* the currently selected procedure */
75
76
77
78
79
80
81
82
83
84
85
86
  gchar            *proc_name;
  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;

87
  gboolean                     scheme_names;
88
  GimpProcBrowserApplyCallback apply_callback;
89
  gpointer                     user_data;
90
91
92
93
94
} GimpDBBrowser;


/*  local function prototypes  */

95
96
97
98
99
100
101
102
static void       browser_selection_changed (GtkTreeSelection  *sel,
                                             GimpDBBrowser     *browser);
static void       browser_row_activated     (GtkTreeView       *treeview,
                                             GtkTreePath       *path,
                                             GtkTreeViewColumn *column,
                                             GimpDBBrowser     *browser);
static void       browser_show_procedure    (GimpDBBrowser     *browser,
                                             gchar             *proc_name);
103
static void       browser_search            (GimpBrowser       *browser,
104
                                             const gchar       *query_text,
105
106
                                             gint               search_type,
                                             GimpDBBrowser     *db_browser);
107
108
109
110
static void       browser_response          (GtkWidget         *widget,
                                             gint               response_id,
                                             GimpDBBrowser     *browser);
static void       browser_convert_string    (gchar             *str);
111
112
113
114


/*  public functions  */

115
GtkWidget *
116
gimp_proc_browser_dialog_new (gboolean                     scheme_names,
117
118
                              GimpProcBrowserApplyCallback apply_callback,
                              gpointer                     user_data)
Elliot Lee's avatar
Elliot Lee committed
119
{
120
121
122
123
  GimpDBBrowser    *browser;
  GtkWidget        *scrolled_window;
  GtkCellRenderer  *renderer;
  GtkTreeSelection *selection;
124

125
  browser = g_new0 (GimpDBBrowser, 1);
126

127
  browser->scheme_names   = scheme_names ? TRUE : FALSE;
128
  browser->apply_callback = apply_callback;
129
  browser->user_data      = user_data;
130

131
132
133
134
135
  browser->dialog = gimp_dialog_new (_("Procedure Browser"), "dbbrowser",
                                     NULL, 0,
                                     gimp_standard_help_func,
                                     "plug-in-db-browser",
                                     NULL);
Sven Neumann's avatar
Sven Neumann committed
136

137
  if (apply_callback)
138
139
140
141
142
143
    {
      gtk_dialog_add_button (GTK_DIALOG (browser->dialog),
                             GTK_STOCK_APPLY, GTK_RESPONSE_APPLY);
      gtk_dialog_set_default_response (GTK_DIALOG (browser->dialog),
                                       GTK_RESPONSE_APPLY);
    }
Sven Neumann's avatar
Sven Neumann committed
144
145
146

  gtk_dialog_add_button (GTK_DIALOG (browser->dialog),
                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
147

148
149
150
  g_signal_connect (browser->dialog, "response",
                    G_CALLBACK (browser_response),
                    browser);
151

152
153
154
155
156
157
158
159
160
161
162
  browser->browser = gimp_browser_new ();
  gimp_browser_add_search_types (GIMP_BROWSER (browser->browser),
                                 _("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);
  gtk_container_set_border_width (GTK_CONTAINER (browser->browser), 12);
163
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (browser->dialog)->vbox),
164
165
                     browser->browser);
  gtk_widget_show (browser->browser);
Michael Natterer's avatar
Michael Natterer committed
166

167
168
  g_signal_connect (browser->browser, "search",
                    G_CALLBACK (browser_search),
169
                    browser);
170

Elliot Lee's avatar
Elliot Lee committed
171
  /* list : list in a scrolled_win */
172

173
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
174
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
175
                                       GTK_SHADOW_IN);
176
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
177
178
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_ALWAYS);
179
180
  gtk_box_pack_start (GTK_BOX (GIMP_BROWSER (browser->browser)->left_vbox),
                      scrolled_window, TRUE, TRUE, 0);
181
  gtk_widget_show (scrolled_window);
182

183
  browser->tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
184

185
186
187
188
  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_renderer_text_set_fixed_height_from_font
    (GTK_CELL_RENDERER_TEXT (renderer), 1);

189
  gtk_tree_view_insert_column_with_attributes (browser->tree_view,
190
191
192
193
                                               -1, NULL,
                                               renderer,
                                               "text", 0,
                                               NULL);
194
  gtk_tree_view_set_headers_visible (browser->tree_view, FALSE);
195

196
  g_signal_connect (browser->tree_view, "row_activated",
197
198
                    G_CALLBACK (browser_row_activated),
                    browser);
199

200
201
202
203
204
  gtk_widget_set_size_request (GTK_WIDGET (browser->tree_view),
                               DBL_LIST_WIDTH, DBL_HEIGHT);
  gtk_container_add (GTK_CONTAINER (scrolled_window),
                     GTK_WIDGET (browser->tree_view));
  gtk_widget_show (GTK_WIDGET (browser->tree_view));
205

206
  selection = gtk_tree_view_get_selection (browser->tree_view);
207

208
  g_signal_connect (selection, "changed",
209
210
                    G_CALLBACK (browser_selection_changed),
                    browser);
Elliot Lee's avatar
Elliot Lee committed
211

212
  gtk_widget_set_size_request (GIMP_BROWSER (browser->browser)->right_vbox->parent->parent,
213
                               DBL_WIDTH - DBL_LIST_WIDTH, -1);
Elliot Lee's avatar
Elliot Lee committed
214
215
216

  /* now build the list */

217
  gtk_widget_show (browser->dialog);
Elliot Lee's avatar
Elliot Lee committed
218
219
220

  /* initialize the "return" value (for "apply") */

221
222
223
224
225
226
227
228
229
230
231
  browser->proc_name      = NULL;
  browser->proc_blurb     = NULL;
  browser->proc_help      = NULL;
  browser->proc_author    = NULL;
  browser->proc_copyright = NULL;
  browser->proc_date      = NULL;
  browser->proc_type      = 0;
  browser->n_params       = 0;
  browser->n_return_vals  = 0;
  browser->params         = NULL;
  browser->return_vals    = NULL;
Elliot Lee's avatar
Elliot Lee committed
232
233

  /* first search (all procedures) */
234
235
  browser_search (GIMP_BROWSER (browser->browser), "", SEARCH_TYPE_ALL,
                  browser);
Elliot Lee's avatar
Elliot Lee committed
236

237
  return browser->dialog;
238
239
}

240
241
242

/*  private functions  */

243
static void
244
245
browser_selection_changed (GtkTreeSelection *sel,
                           GimpDBBrowser    *browser)
Elliot Lee's avatar
Elliot Lee committed
246
{
247
  GtkTreeIter iter;
248

249
250
  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
    {
251
252
253
254
      gchar *proc_name;

      gtk_tree_model_get (GTK_TREE_MODEL (browser->store), &iter,
                          COLUMN_PROC_NAME, &proc_name,
255
                          -1);
256
257
      browser_show_procedure (browser, proc_name);
      g_free (proc_name);
258
    }
Elliot Lee's avatar
Elliot Lee committed
259
260
}

261
static void
262
263
264
265
browser_row_activated (GtkTreeView       *treeview,
                       GtkTreePath       *path,
                       GtkTreeViewColumn *column,
                       GimpDBBrowser     *browser)
Elliot Lee's avatar
Elliot Lee committed
266
{
267
268
  browser_response (browser->dialog, GTK_RESPONSE_APPLY, browser);
}
Elliot Lee's avatar
Elliot Lee committed
269

270
271
272
273
274
275
static void
browser_show_procedure (GimpDBBrowser *browser,
                        gchar         *proc_name)
{
  g_free (browser->proc_name);
  browser->proc_name = g_strdup (proc_name);
Elliot Lee's avatar
Elliot Lee committed
276

277
278
  if (browser->scheme_names)
    browser_convert_string (browser->proc_name);
Elliot Lee's avatar
Elliot Lee committed
279

280
281
282
283
284
  g_free (browser->proc_blurb);
  g_free (browser->proc_help);
  g_free (browser->proc_author);
  g_free (browser->proc_copyright);
  g_free (browser->proc_date);
285
286
287

  gimp_destroy_paramdefs (browser->params,      browser->n_params);
  gimp_destroy_paramdefs (browser->return_vals, browser->n_return_vals);
288

289
290
291
292
293
294
295
296
297
298
299
300
  gimp_procedural_db_proc_info (proc_name,
                                &browser->proc_blurb,
                                &browser->proc_help,
                                &browser->proc_author,
                                &browser->proc_copyright,
                                &browser->proc_date,
                                &browser->proc_type,
                                &browser->n_params,
                                &browser->n_return_vals,
                                &browser->params,
                                &browser->return_vals);

301
302
303
304
305
306
307
308
309
310
311
312
313
  gimp_browser_set_widget (GIMP_BROWSER (browser->browser),
                           gimp_proc_view_new (browser->proc_name,
                                               NULL,
                                               browser->proc_blurb,
                                               browser->proc_help,
                                               browser->proc_author,
                                               browser->proc_copyright,
                                               browser->proc_date,
                                               browser->proc_type,
                                               browser->n_params,
                                               browser->n_return_vals,
                                               browser->params,
                                               browser->return_vals));
314
315
316
}

static void
317
browser_search (GimpBrowser   *gimp_browser,
318
                const gchar   *query_text,
319
320
                gint           search_type,
                GimpDBBrowser *browser)
321
{
322
323
324
  gchar **proc_list;
  gint    num_procs;
  gchar  *str;
325
326
327
328
329

  if (search_type == SEARCH_TYPE_NAME)
    {
      GString *query;

330
331
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching by name - please wait"));
332
333
334
335
336
337
338
339
340
341
342
343
344

      query = g_string_new ("");

      while (*query_text)
        {
          if ((*query_text == '_') || (*query_text == '-'))
            g_string_append (query, "[-_]");
          else
            g_string_append_c (query, *query_text);

          query_text++;
        }

345
      gimp_procedural_db_query (query->str, ".*", ".*", ".*", ".*", ".*", ".*",
346
347
348
349
350
351
                                &num_procs, &proc_list);

      g_string_free (query, TRUE);
    }
  else if (search_type == SEARCH_TYPE_BLURB)
    {
352
353
354
355
356
357
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching by description - please wait"));

      gimp_procedural_db_query (".*", query_text, ".*", ".*", ".*", ".*", ".*",
                                &num_procs, &proc_list);
    }
358
359
360
361
362
363
364
365
  else if (search_type == SEARCH_TYPE_HELP)
    {
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching by help - please wait"));

      gimp_procedural_db_query (".*", ".*", query_text, ".*", ".*", ".*", ".*",
                                &num_procs, &proc_list);
    }
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
  else if (search_type == SEARCH_TYPE_AUTHOR)
    {
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching by author - please wait"));

      gimp_procedural_db_query (".*", ".*", ".*", query_text, ".*", ".*", ".*",
                                &num_procs, &proc_list);
    }
  else if (search_type == SEARCH_TYPE_COPYRIGHT)
    {
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching by copyright - please wait"));

      gimp_procedural_db_query (".*", ".*", ".*", ".*", query_text, ".*", ".*",
                                &num_procs, &proc_list);
    }
  else if (search_type == SEARCH_TYPE_DATE)
    {
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching by date - please wait"));

      gimp_procedural_db_query (".*", ".*", ".*", ".*", ".*", query_text, ".*",
                                &num_procs, &proc_list);
    }
  else if (search_type == SEARCH_TYPE_PROC_TYPE)
    {
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching by type - please wait"));
394

395
      gimp_procedural_db_query (".*", ".*", ".*", ".*", ".*", ".*", query_text,
396
397
398
399
                                &num_procs, &proc_list);
    }
  else
    {
400
401
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("Searching - please wait"));
402
403
404
405
406
407
408
409
410
411

      gimp_procedural_db_query (".*", ".*", ".*", ".*", ".*", ".*", ".*",
                                &num_procs, &proc_list);
    }

  if (num_procs == 1)
    str = g_strdup (_("1 Procedure"));
  else
    str = g_strdup_printf (_("%d Procedures"), num_procs);

412
  gtk_label_set_text (GTK_LABEL (gimp_browser->count_label), str);
413
414
415
416
  g_free (str);

  if (num_procs > 0)
    {
417
418
419
      GtkTreeSelection *selection;
      GtkTreeIter       iter;
      gint              i;
420
421
422
423

      browser->store = gtk_list_store_new (N_COLUMNS,
                                           G_TYPE_STRING,
                                           G_TYPE_STRING);
424
      gtk_tree_view_set_model (browser->tree_view,
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                               GTK_TREE_MODEL (browser->store));
      g_object_unref (browser->store);

      for (i = 0; i < num_procs; i++)
        {
          str = g_strdup (proc_list[i]);

          if (browser->scheme_names)
            browser_convert_string (str);

          gtk_list_store_append (browser->store, &iter);
          gtk_list_store_set (browser->store, &iter,
                              COLUMN_LABEL,     str,
                              COLUMN_PROC_NAME, proc_list[i],
                              -1);

          g_free (str);
          g_free (proc_list[i]);
        }

      g_free (proc_list);

447
      gtk_tree_view_columns_autosize (browser->tree_view);
448
449
450
451

      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (browser->store),
                                            COLUMN_LABEL, GTK_SORT_ASCENDING);

452
453
454
      gtk_tree_model_get_iter_first (GTK_TREE_MODEL (browser->store), &iter);
      selection = gtk_tree_view_get_selection (browser->tree_view);
      gtk_tree_selection_select_iter (selection, &iter);
455
456
457
    }
  else
    {
458
      gtk_tree_view_set_model (browser->tree_view, NULL);
459
460
      browser->store = NULL;

461
462
      gimp_browser_show_message (GIMP_BROWSER (browser->browser),
                                 _("No matches"));
463
464
465
    }
}

Elliot Lee's avatar
Elliot Lee committed
466
static void
467
468
469
browser_response (GtkWidget     *widget,
                  gint           response_id,
                  GimpDBBrowser *browser)
Elliot Lee's avatar
Elliot Lee committed
470
{
471
  switch (response_id)
472
    {
473
    case GTK_RESPONSE_APPLY:
474
475
476
477
478
479
480
481
482
483
484
485
486
      if (browser->apply_callback)
        browser->apply_callback (browser->proc_name,
                                 browser->proc_blurb,
                                 browser->proc_help,
                                 browser->proc_author,
                                 browser->proc_copyright,
                                 browser->proc_date,
                                 browser->proc_type,
                                 browser->n_params,
                                 browser->n_return_vals,
                                 browser->params,
                                 browser->return_vals,
                                 browser->user_data);
487
488
489
      break;

    default:
490
      if (browser->apply_callback)
491
        {
492
493
494
495
          /* we are called by another application:
           * just destroy the dialog box
           */
          gtk_widget_destroy (browser->dialog);
496
497
498
        }
      else
        {
499
500
501
          /* we are in the plug_in:
           * quit the gtk application
           */
502
503
504
          gtk_main_quit ();
        }
      break;
505
    }
Elliot Lee's avatar
Elliot Lee committed
506
507
}

508
static void
509
browser_convert_string (gchar *str)
Elliot Lee's avatar
Elliot Lee committed
510
511
512
{
  while (*str)
    {
513
      if (*str == '_')
514
        *str = '-';
515

Elliot Lee's avatar
Elliot Lee committed
516
517
518
      str++;
    }
}