1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
105
106
107
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpfileprocview.c
* Copyright (C) 2004 Sven Neumann <sven@gimp.org>
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gtk/gtk.h>
#include <gegl.h>
#include "widgets-types.h"
#include "core/gimp.h"
#include "core/gimpmarshal.h"
#include "plug-in/gimppluginprocedure.h"
#include "gimpfileprocview.h"
#include "gimp-intl.h"
/* an arbitrary limit to keep the file dialog from becoming too wide */
#define MAX_EXTENSIONS 4
enum
{
COLUMN_PROC,
COLUMN_LABEL,
COLUMN_EXTENSIONS,
COLUMN_HELP_ID,
COLUMN_FILTER,
N_COLUMNS
};
enum
{
CHANGED,
LAST_SIGNAL
};
static void gimp_file_proc_view_finalize (GObject *object);
static void gimp_file_proc_view_selection_changed (GtkTreeSelection *selection,
GimpFileProcView *view);
static GtkFileFilter * gimp_file_proc_view_process_procedure (GimpPlugInProcedure *file_proc,
GtkFileFilter *all);
static gchar * gimp_file_proc_view_pattern_from_extension (const gchar *extension);
G_DEFINE_TYPE (GimpFileProcView, gimp_file_proc_view, GTK_TYPE_TREE_VIEW)
#define parent_class gimp_file_proc_view_parent_class
static guint view_signals[LAST_SIGNAL] = { 0 };
static void
gimp_file_proc_view_class_init (GimpFileProcViewClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gimp_file_proc_view_finalize;
klass->changed = NULL;
view_signals[CHANGED] = g_signal_new ("changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GimpFileProcViewClass,
changed),
NULL, NULL,
gimp_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
gimp_file_proc_view_init (GimpFileProcView *view)
{
}
static void
gimp_file_proc_view_finalize (GObject *object)
{
GimpFileProcView *view = GIMP_FILE_PROC_VIEW (object);
if (view->meta_extensions)
{
g_list_free_full (view->meta_extensions, (GDestroyNotify) g_free);
view->meta_extensions = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GtkWidget *
gimp_file_proc_view_new (Gimp *gimp,
GSList *procedures,
const gchar *automatic,
const gchar *automatic_help_id)
{
GtkFileFilter *all_filter;
GtkTreeView *view;
GtkTreeViewColumn *column;
GtkCellRenderer *cell;
GtkListStore *store;
GSList *list;
GtkTreeIter iter;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
store = gtk_list_store_new (N_COLUMNS,
GIMP_TYPE_PLUG_IN_PROCEDURE, /* COLUMN_PROC */
G_TYPE_STRING, /* COLUMN_LABEL */
G_TYPE_STRING, /* COLUMN_EXTENSIONS */
G_TYPE_STRING, /* COLUMN_HELP_ID */
GTK_TYPE_FILE_FILTER); /* COLUMN_FILTER */
view = g_object_new (GIMP_TYPE_FILE_PROC_VIEW,
"model", store,
"rules-hint", TRUE,
NULL);
g_object_unref (store);
all_filter = gtk_file_filter_new ();
for (list = procedures; list; list = g_slist_next (list))
{
GimpPlugInProcedure *proc = list->data;
if (! proc->prefixes_list) /* skip URL loaders */
{
const gchar *label = gimp_procedure_get_label (GIMP_PROCEDURE (proc));
const gchar *help_id = gimp_procedure_get_help_id (GIMP_PROCEDURE (proc));
GSList *list2;
if (label)
{
GtkFileFilter *filter;
filter = gimp_file_proc_view_process_procedure (proc, all_filter);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_PROC, proc,
COLUMN_LABEL, label,
COLUMN_EXTENSIONS, proc->extensions,
COLUMN_HELP_ID, help_id,
COLUMN_FILTER, filter,
-1);
g_object_unref (filter);
}
for (list2 = proc->extensions_list;
list2;
list2 = g_slist_next (list2))
{
GimpFileProcView *proc_view = GIMP_FILE_PROC_VIEW (view);
const gchar *ext = list2->data;
const gchar *dot = strchr (ext, '.');
if (dot && dot != ext)
proc_view->meta_extensions =
g_list_append (proc_view->meta_extensions,
g_strdup (dot + 1));
}
}
}
if (automatic)
{
gtk_list_store_prepend (store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_PROC, NULL,
COLUMN_LABEL, automatic,
COLUMN_HELP_ID, automatic_help_id,
COLUMN_FILTER, all_filter,
-1);
}
column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, _("File Type"));
gtk_tree_view_column_set_expand (column, TRUE);
cell = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (column, cell, TRUE);
gtk_tree_view_column_set_attributes (column, cell,
"text", COLUMN_LABEL,
NULL);
gtk_tree_view_append_column (view, column);
column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, _("Extensions"));
cell = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (column, cell, TRUE);
gtk_tree_view_column_set_attributes (column, cell,
"text", COLUMN_EXTENSIONS,
NULL);
gtk_tree_view_append_column (view, column);
g_signal_connect (gtk_tree_view_get_selection (view), "changed",
G_CALLBACK (gimp_file_proc_view_selection_changed),
view);
return GTK_WIDGET (view);
}
GimpPlugInProcedure *
gimp_file_proc_view_get_proc (GimpFileProcView *view,
gchar **label,
GtkFileFilter **filter)
{
GtkTreeModel *model;
GtkTreeSelection *selection;
GimpPlugInProcedure *proc;
GtkTreeIter iter;
gboolean has_selection;
g_return_val_if_fail (GIMP_IS_FILE_PROC_VIEW (view), NULL);
if (label) *label = NULL;
if (filter) *filter = NULL;
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
has_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
/* if there's no selected item, we return the "automatic" procedure, which,
* if exists, is the first item.
*/
if (! has_selection)
{
model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
if (! gtk_tree_model_get_iter_first (model, &iter))
return NULL;
}
gtk_tree_model_get (model, &iter,
COLUMN_PROC, &proc,
-1);
if (proc)
{
g_object_unref (proc);
/* there's no selected item, and no "automatic" procedure. return NULL.
*/
if (! has_selection)
return NULL;
}
if (label)
{
gtk_tree_model_get (model, &iter,
COLUMN_LABEL, label,
-1);
}
if (filter)
{
gtk_tree_model_get (model, &iter,
COLUMN_FILTER, filter,
-1);
}
return proc;
}
gboolean
gimp_file_proc_view_set_proc (GimpFileProcView *view,
GimpPlugInProcedure *proc)
{
GtkTreeModel *model;
GtkTreeIter iter;
gboolean iter_valid;
g_return_val_if_fail (GIMP_IS_FILE_PROC_VIEW (view), FALSE);
model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
iter_valid;
iter_valid = gtk_tree_model_iter_next (model, &iter))
{
GimpPlugInProcedure *this;
gtk_tree_model_get (model, &iter,
COLUMN_PROC, &this,
-1);
if (this)
g_object_unref (this);
if (this == proc)
break;
}
if (iter_valid)
{
GtkTreeSelection *selection;
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
gtk_tree_selection_select_iter (selection, &iter);
}
return iter_valid;
}
gchar *
gimp_file_proc_view_get_help_id (GimpFileProcView *view)
{
GtkTreeModel *model;
GtkTreeSelection *selection;
GtkTreeIter iter;
g_return_val_if_fail (GIMP_IS_FILE_PROC_VIEW (view), NULL);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
gchar *help_id;
gtk_tree_model_get (model, &iter,
COLUMN_HELP_ID, &help_id,
-1);
return help_id;
}
return NULL;
}
static void
gimp_file_proc_view_selection_changed (GtkTreeSelection *selection,
GimpFileProcView *view)
{
g_signal_emit (view, view_signals[CHANGED], 0);
}
/**
* gimp_file_proc_view_process_procedure:
* @file_proc:
* @all:
*
* Creates a #GtkFileFilter of @file_proc and adds the extensions to
* the @all filter.
* The returned #GtkFileFilter has a normal ref and must be unreffed
* when used.
**/
static GtkFileFilter *
gimp_file_proc_view_process_procedure (GimpPlugInProcedure *file_proc,
GtkFileFilter *all)
{
GtkFileFilter *filter;
GString *str;
GSList *list;
gint i;
if (! file_proc->extensions_list)
return NULL;
filter = gtk_file_filter_new ();
str = g_string_new (gimp_procedure_get_label (GIMP_PROCEDURE (file_proc)));
/* Take ownership directly so we don't have to mess with a floating
* ref
*/
g_object_ref_sink (filter);
for (list = file_proc->mime_types_list; list; list = g_slist_next (list))
{
const gchar *mime_type = list->data;
gtk_file_filter_add_mime_type (filter, mime_type);
gtk_file_filter_add_mime_type (all, mime_type);
}
for (list = file_proc->extensions_list, i = 0;
list;
list = g_slist_next (list), i++)
{
const gchar *extension = list->data;
gchar *pattern;
pattern = gimp_file_proc_view_pattern_from_extension (extension);
gtk_file_filter_add_pattern (filter, pattern);
gtk_file_filter_add_pattern (all, pattern);
g_free (pattern);
if (i == 0)
{
g_string_append (str, " (");
}
else if (i <= MAX_EXTENSIONS)
{
g_string_append (str, ", ");
}
if (i < MAX_EXTENSIONS)
{
g_string_append (str, "*.");
g_string_append (str, extension);
}
else if (i == MAX_EXTENSIONS)
{
g_string_append (str, "...");
}
if (! list->next)
{
g_string_append (str, ")");
}
}
gtk_file_filter_set_name (filter, str->str);
g_string_free (str, TRUE);
return filter;
}
static gchar *
gimp_file_proc_view_pattern_from_extension (const gchar *extension)
{
gchar *pattern;
gchar *p;
gint len, i;
g_return_val_if_fail (extension != NULL, NULL);
/* This function assumes that file extensions are 7bit ASCII. It
* could certainly be rewritten to handle UTF-8 if this assumption
* turns out to be incorrect.
*/
len = strlen (extension);
pattern = g_new (gchar, 4 + 4 * len);
strcpy (pattern, "*.");
for (i = 0, p = pattern + 2; i < len; i++, p+= 4)
{
p[0] = '[';
p[1] = g_ascii_tolower (extension[i]);
p[2] = g_ascii_toupper (extension[i]);
p[3] = ']';
}
*p = '\0';
return pattern;
}
|