gimpprocbrowser.c 16.4 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 "gimpprocbox.h"
36
#include "gimpprocbrowser.h"
37
#include "gimpprocview.h"
38

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

41

42
43
44
45
#define RESPONSE_SEARCH       1
#define RESPONSE_SEARCH_NAME  2
#define RESPONSE_SEARCH_BLURB 3

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

50
51
52
53
54
55
enum
{
  COLUMN_LABEL,
  COLUMN_PROC_NAME,
  N_COLUMNS
};
56
57
58

typedef struct
{
59
  GtkWidget        *dialog;
60

61
  GtkWidget        *count_label;
62
  GtkWidget        *search_entry;
63
  GtkWidget        *proc_box;
64

65
66
67
  GtkListStore     *store;
  GtkWidget        *tv;
  GtkTreeSelection *sel;
68
69

  /* the currently selected procedure */
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  gchar            *proc_name;
  gchar            *scheme_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;

  GimpProcBrowserApplyCallback apply_callback;
} GimpDBBrowser;


/*  local function prototypes  */

static void   browser_response          (GtkWidget         *widget,
                                         gint               response_id,
                                         GimpDBBrowser     *browser);
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);
static void   browser_convert_string    (gchar             *str);


/*  public functions  */

105
GtkWidget *
106
gimp_proc_browser_dialog_new (GimpProcBrowserApplyCallback apply_callback)
Elliot Lee's avatar
Elliot Lee committed
107
{
108
109
110
  GimpDBBrowser   *browser;
  GtkWidget       *paned;
  GtkWidget       *hbox;
111
112
113
114
  GtkWidget       *vbox;
  GtkWidget       *label;
  GtkWidget       *scrolled_window;
  GtkCellRenderer *renderer;
115

116
  browser = g_new0 (GimpDBBrowser, 1);
117

118
  browser->apply_callback = apply_callback;
119

120
121
  if (apply_callback)
    {
122
123
      browser->dialog =
        gimp_dialog_new (_("Procedure Browser"), "dbbrowser",
124
                         NULL, 0,
125
                         gimp_standard_help_func, "plug-in-db-browser",
126

127
128
129
130
                         _("Search by _Name"),  RESPONSE_SEARCH_NAME,
                         _("Search by _Blurb"), RESPONSE_SEARCH_BLURB,
                         GTK_STOCK_APPLY,       GTK_RESPONSE_APPLY,
                         GTK_STOCK_CLOSE,       GTK_RESPONSE_CLOSE,
131
132

                         NULL);
133
134
135
    }
  else
    {
136
137
      browser->dialog =
        gimp_dialog_new (_("Procedure Browser"), "dbbrowser",
138
                         NULL, 0,
139
                         gimp_standard_help_func, "plug-in-db-browser",
140

141
142
143
                         _("Search by _Name"),  RESPONSE_SEARCH_NAME,
                         _("Search by _Blurb"), RESPONSE_SEARCH_BLURB,
                         GTK_STOCK_CLOSE,       GTK_RESPONSE_CLOSE,
144

145
                         NULL);
146
147
    }

148
  gtk_dialog_set_default_response (GTK_DIALOG (browser->dialog),
149
150
                                   RESPONSE_SEARCH_NAME);

151
152
153
  g_signal_connect (browser->dialog, "response",
                    G_CALLBACK (browser_response),
                    browser);
154

155
  /* paned : left=list ; right=description */
Michael Natterer's avatar
Michael Natterer committed
156

157
158
159
160
161
  paned = gtk_hpaned_new ();
  gtk_container_set_border_width (GTK_CONTAINER (paned), 12);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (browser->dialog)->vbox),
                     paned);
  gtk_widget_show (paned);
162

Elliot Lee's avatar
Elliot Lee committed
163
  /* left = vbox : the list and the search entry */
164

165
  vbox = gtk_vbox_new (FALSE, 6);
166
  gtk_paned_pack1 (GTK_PANED (paned), vbox, FALSE, TRUE);
Michael Natterer's avatar
Michael Natterer committed
167
168
  gtk_widget_show (vbox);

169
170
171
172
173
  browser->count_label = gtk_label_new ("0 Procedures");
  gtk_misc_set_alignment (GTK_MISC (browser->count_label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (vbox), browser->count_label, FALSE, FALSE, 0);
  gtk_widget_show (browser->count_label);

Elliot Lee's avatar
Elliot Lee committed
174
  /* list : list in a scrolled_win */
175

176
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
177
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
178
                                       GTK_SHADOW_IN);
179
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
180
181
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_ALWAYS);
182
183
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
  gtk_widget_show (scrolled_window);
184

185
  browser->tv = gtk_tree_view_new ();
186

187
188
189
190
  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_renderer_text_set_fixed_height_from_font
    (GTK_CELL_RENDERER_TEXT (renderer), 1);

191
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (browser->tv),
192
193
194
195
                                               -1, NULL,
                                               renderer,
                                               "text", 0,
                                               NULL);
196
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (browser->tv), FALSE);
197

198
  if (apply_callback)
199
200
201
202
203
204
205
    g_signal_connect (browser->tv, "row_activated",
                      G_CALLBACK (browser_row_activated),
                      browser);

  gtk_widget_set_size_request (browser->tv, DBL_LIST_WIDTH, DBL_HEIGHT);
  gtk_container_add (GTK_CONTAINER (scrolled_window), browser->tv);
  gtk_widget_show (browser->tv);
206

207
  browser->sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (browser->tv));
208

209
210
211
  g_signal_connect (browser->sel, "changed",
                    G_CALLBACK (browser_selection_changed),
                    browser);
Elliot Lee's avatar
Elliot Lee committed
212
213
214

  /* search entry */

215
216
217
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
Elliot Lee's avatar
Elliot Lee committed
218

219
  label = gtk_label_new_with_mnemonic (_("_Search:"));
220
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
221
  gtk_widget_show (label);
Elliot Lee's avatar
Elliot Lee committed
222

223
224
225
226
  browser->search_entry = gtk_entry_new ();
  gtk_entry_set_activates_default (GTK_ENTRY (browser->search_entry), TRUE);
  gtk_box_pack_start (GTK_BOX (hbox), browser->search_entry, TRUE, TRUE, 0);
  gtk_widget_show (browser->search_entry);
Elliot Lee's avatar
Elliot Lee committed
227

228
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), browser->search_entry);
229

Elliot Lee's avatar
Elliot Lee committed
230
231
  /* right = description */

232
233
  browser->proc_box = gimp_proc_box_new ();
  gtk_widget_set_size_request (browser->proc_box,
234
                               DBL_WIDTH - DBL_LIST_WIDTH, -1);
235
236
  gtk_paned_pack2 (GTK_PANED (paned), browser->proc_box, TRUE, TRUE);
  gtk_widget_show (browser->proc_box);
Elliot Lee's avatar
Elliot Lee committed
237
238
239

  /* now build the list */

240
  gtk_widget_show (browser->dialog);
Elliot Lee's avatar
Elliot Lee committed
241
242
243

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

244
245
246
247
248
249
250
251
252
253
254
255
  browser->proc_name        = NULL;
  browser->scheme_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
256
257

  /* first search (all procedures) */
258
  browser_response (browser->dialog, RESPONSE_SEARCH, browser);
259

260
  gtk_widget_grab_focus (browser->search_entry);
Elliot Lee's avatar
Elliot Lee committed
261

262
  return browser->dialog;
263
264
}

265
266
267

/*  private functions  */

268
static void
269
270
browser_selection_changed (GtkTreeSelection *sel,
                           GimpDBBrowser    *browser)
Elliot Lee's avatar
Elliot Lee committed
271
{
272
  GtkTreeIter iter;
273

274
275
  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
    {
276
277
278
279
      gchar *proc_name;

      gtk_tree_model_get (GTK_TREE_MODEL (browser->store), &iter,
                          COLUMN_PROC_NAME, &proc_name,
280
                          -1);
281
282
      browser_show_procedure (browser, proc_name);
      g_free (proc_name);
283
    }
Elliot Lee's avatar
Elliot Lee committed
284
285
}

286
static void
287
288
289
290
browser_row_activated (GtkTreeView       *treeview,
                       GtkTreePath       *path,
                       GtkTreeViewColumn *column,
                       GimpDBBrowser     *browser)
Elliot Lee's avatar
Elliot Lee committed
291
{
292
293
  browser_response (browser->dialog, GTK_RESPONSE_APPLY, browser);
}
Elliot Lee's avatar
Elliot Lee committed
294

295
296
297
298
299
300
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
301

302
303
304
  g_free (browser->scheme_proc_name);
  browser->scheme_proc_name = g_strdup (proc_name);
  browser_convert_string (browser->scheme_proc_name);
Elliot Lee's avatar
Elliot Lee committed
305

306
307
308
309
310
  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);
311
312
313

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

315
316
317
318
319
320
321
322
323
324
325
326
  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);

327
328
329
330
331
332
333
334
335
336
337
338
339
  gimp_proc_box_set_widget (browser->proc_box,
                            gimp_proc_view_new (browser->scheme_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));
Elliot Lee's avatar
Elliot Lee committed
340
341
342
}

static void
343
344
345
browser_response (GtkWidget     *widget,
                  gint           response_id,
                  GimpDBBrowser *browser)
Elliot Lee's avatar
Elliot Lee committed
346
{
347
  switch (response_id)
348
    {
349
    case GTK_RESPONSE_APPLY:
350
351
352
353
354
355
356
357
358
359
360
361
      browser->apply_callback (browser->proc_name,
                               browser->scheme_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);
362
363
364
365
366
367
368
369
      break;

    case RESPONSE_SEARCH:
    case RESPONSE_SEARCH_NAME:
    case RESPONSE_SEARCH_BLURB:
      {
        gchar       **proc_list;
        gint          num_procs;
370
        gchar        *str;
371
372
373
374
375
        gint          i;
        const gchar  *query_text;
        GString      *query;
        GtkTreeIter   iter;

376
        gtk_tree_view_set_model (GTK_TREE_VIEW (browser->tv), NULL);
377
378
379
380
381

        /* search */

        if (response_id == RESPONSE_SEARCH_NAME)
          {
382
383
            gimp_proc_box_show_message (browser->proc_box,
                                        _("Searching by name - please wait"));
384
385

            query = g_string_new ("");
386
            query_text = gtk_entry_get_text (GTK_ENTRY (browser->search_entry));
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

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

                query_text++;
              }

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

            g_string_free (query, TRUE);
          }
        else if (response_id == RESPONSE_SEARCH_BLURB)
          {
406
407
            gimp_proc_box_show_message (browser->proc_box,
                                        _("Searching by blurb - please wait"));
408
409
410

            gimp_procedural_db_query (".*",
                                      (gchar *) gtk_entry_get_text
411
                                      (GTK_ENTRY (browser->search_entry)),
412
413
414
415
416
                                      ".*", ".*", ".*", ".*", ".*",
                                      &num_procs, &proc_list);
          }
        else
          {
417
418
            gimp_proc_box_show_message (browser->proc_box,
                                        _("Searching - please wait"));
419
420
421
422
423

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

424
425
426
427
428
        if (num_procs == 1)
          str = g_strdup (_("1 Procedure"));
        else
          str = g_strdup_printf (_("%d Procedures"), num_procs);

429
430
431
432
433
434
435
436
437
        gtk_label_set_text (GTK_LABEL (browser->count_label), str);
        g_free (str);

        browser->store = gtk_list_store_new (N_COLUMNS,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING);
        gtk_tree_view_set_model (GTK_TREE_VIEW (browser->tv),
                                 GTK_TREE_MODEL (browser->store));
        g_object_unref (browser->store);
438
439
440

        for (i = 0; i < num_procs; i++)
          {
441
442
            gchar *label;

443
            label = g_strdup (proc_list[i]);
444
445
446
447
448
449
            browser_convert_string (label);

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

452
453
454
455
456
457
            g_free (label);
            g_free (proc_list[i]);
          }

        g_free (proc_list);

458
459
        gtk_tree_view_columns_autosize (GTK_TREE_VIEW (browser->tv));

460
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (browser->store),
461
                                              COLUMN_LABEL, GTK_SORT_ASCENDING);
462
463
464

        if (num_procs > 0)
          {
465
466
            gtk_tree_model_get_iter_first (GTK_TREE_MODEL (browser->store),
                                           &iter);
467
            gtk_tree_selection_select_iter (browser->sel, &iter);
468
469
470
          }
        else
          {
471
            gimp_proc_box_show_message (browser->proc_box, _("No matches"));
472
473
474
475
476
          }
      }
      break;

    default:
477
      if (browser->apply_callback)
478
        {
479
480
481
482
          /* we are called by another application:
           * just destroy the dialog box
           */
          gtk_widget_destroy (browser->dialog);
483
484
485
        }
      else
        {
486
487
488
          /* we are in the plug_in:
           * quit the gtk application
           */
489
490
491
          gtk_main_quit ();
        }
      break;
492
    }
Elliot Lee's avatar
Elliot Lee committed
493
494
}

495
static void
496
browser_convert_string (gchar *str)
Elliot Lee's avatar
Elliot Lee committed
497
498
499
{
  while (*str)
    {
500
      if (*str == '_')
501
        *str = '-';
502

Elliot Lee's avatar
Elliot Lee committed
503
504
505
      str++;
    }
}