file-pat.c 9.57 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
/*
 * pat plug-in version 1.01
3
 * Loads/exports version 1 GIMP .pat files, by Tim Newsome <drz@frody.bloke.com>
4
 *
5
 * GIMP - The GNU Image Manipulation Program
6
7
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
12
13
14
15
16
17
18
 * (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
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
20
21
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
22
23
24
#include "config.h"

#include <libgimp/gimp.h>
Sven Neumann's avatar
Sven Neumann committed
25
#include <libgimp/gimpui.h>
26

27
#include "libgimp/stdplugins-intl.h"
Tor Lillqvist's avatar
Tor Lillqvist committed
28

29

30
#define SAVE_PROC      "file-pat-save"
31
#define PLUG_IN_BINARY "file-pat"
Tor Lillqvist's avatar
Tor Lillqvist committed
32

Elliot Lee's avatar
Elliot Lee committed
33

34
35
typedef struct _Pat      Pat;
typedef struct _PatClass PatClass;
Elliot Lee's avatar
Elliot Lee committed
36

37
38
39
40
struct _Pat
{
  GimpPlugIn      parent_instance;
};
Elliot Lee's avatar
Elliot Lee committed
41

42
struct _PatClass
Elliot Lee's avatar
Elliot Lee committed
43
{
44
  GimpPlugInClass parent_class;
Elliot Lee's avatar
Elliot Lee committed
45
46
};

47

48
49
#define PAT_TYPE  (pat_get_type ())
#define PAT (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PAT_TYPE, Pat))
50

51
GType                   pat_get_type         (void) G_GNUC_CONST;
Elliot Lee's avatar
Elliot Lee committed
52

53
54
55
static GList          * pat_query_procedures (GimpPlugIn           *plug_in);
static GimpProcedure  * pat_create_procedure (GimpPlugIn           *plug_in,
                                              const gchar          *name);
Elliot Lee's avatar
Elliot Lee committed
56

57
58
static GimpValueArray * pat_save             (GimpProcedure        *procedure,
                                              GimpRunMode           run_mode,
59
                                              GimpImage            *image,
60
61
                                              gint                  n_drawables,
                                              GimpDrawable        **drawables,
62
63
64
                                              GFile                *file,
                                              const GimpValueArray *args,
                                              gpointer              run_data);
Elliot Lee's avatar
Elliot Lee committed
65

66
67
static gboolean         save_dialog          (GimpProcedure        *procedure,
                                              GObject              *config);
Elliot Lee's avatar
Elliot Lee committed
68

69

70
G_DEFINE_TYPE (Pat, pat, GIMP_TYPE_PLUG_IN)
Elliot Lee's avatar
Elliot Lee committed
71

72
GIMP_MAIN (PAT_TYPE)
73
DEFINE_STD_SET_I18N
74

75

76
77
78
79
static void
pat_class_init (PatClass *klass)
{
  GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
Sven Neumann's avatar
Sven Neumann committed
80

81
82
  plug_in_class->query_procedures = pat_query_procedures;
  plug_in_class->create_procedure = pat_create_procedure;
83
  plug_in_class->set_i18n         = STD_SET_I18N;
84
}
85

86
87
88
89
static void
pat_init (Pat *pat)
{
}
90

91
92
93
94
95
static GList *
pat_query_procedures (GimpPlugIn *plug_in)
{
  return g_list_append (NULL, g_strdup (SAVE_PROC));
}
96

97
98
99
100
101
static GimpProcedure *
pat_create_procedure (GimpPlugIn  *plug_in,
                      const gchar *name)
{
  GimpProcedure *procedure = NULL;
102

103
104
  if (! strcmp (name, SAVE_PROC))
    {
105
106
      procedure = gimp_save_procedure_new (plug_in, name,
                                           GIMP_PDB_PROC_TYPE_PLUGIN,
107
108
                                           pat_save, NULL, NULL);

109
      gimp_procedure_set_image_types (procedure, "*");
110

111
      gimp_procedure_set_menu_label (procedure, _("GIMP pattern"));
112
      gimp_procedure_set_icon_name (procedure, GIMP_ICON_PATTERN);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

      gimp_procedure_set_documentation (procedure,
                                        "Exports Gimp pattern file (.PAT)",
                                        "New Gimp patterns can be created "
                                        "by exporting them in the "
                                        "appropriate place with this plug-in.",
                                        SAVE_PROC);
      gimp_procedure_set_attribution (procedure,
                                      "Tim Newsome",
                                      "Tim Newsome",
                                      "1997");

      gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
                                          "image/x-gimp-pat");
      gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
                                          "pat");
129
130
      gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
                                              TRUE);
131

132
133
134
135
136
      GIMP_PROC_ARG_STRING (procedure, "description",
                            "Description",
                            "Short description of the pattern",
                            "GIMP Pattern",
                            GIMP_PARAM_READWRITE);
137
    }
138

139
140
  return procedure;
}
Elliot Lee's avatar
Elliot Lee committed
141

142
143
144
static GimpValueArray *
pat_save (GimpProcedure        *procedure,
          GimpRunMode           run_mode,
145
          GimpImage            *image,
146
147
          gint                  n_drawables,
          GimpDrawable        **drawables,
148
149
150
151
          GFile                *file,
          const GimpValueArray *args,
          gpointer              run_data)
{
152
153
154
155
156
  GimpProcedureConfig *config;
  GimpPDBStatusType    status = GIMP_PDB_SUCCESS;
  GimpExportReturn     export = GIMP_EXPORT_CANCEL;
  gchar               *description;
  GError              *error  = NULL;
157

158
  config = gimp_procedure_create_config (procedure);
159
  gimp_procedure_config_begin_run (config, image, run_mode, args);
160

161
162
163
  g_object_get (config,
                "description", &description,
                NULL);
David Odin's avatar
David Odin committed
164

165
166
167
  if (! description || ! strlen (description))
    {
      gchar *name = g_path_get_basename (gimp_file_get_utf8_name (file));
168

169
170
      if (g_str_has_suffix (name, ".pat"))
        name[strlen (name) - 4] = '\0';
171

172
173
174
175
      if (strlen (name))
        g_object_set (config,
                      "description", name,
                      NULL);
176

177
      g_free (name);
178
    }
Sven Neumann's avatar
Sven Neumann committed
179

180
181
  g_free (description);

182
183
184
185
186
187
  switch (run_mode)
    {
    case GIMP_RUN_INTERACTIVE:
    case GIMP_RUN_WITH_LAST_VALS:
      gimp_ui_init (PLUG_IN_BINARY);

188
      export = gimp_export_image (&image, &n_drawables, &drawables, "PAT",
189
190
191
192
193
194
195
196
197
198
199
200
201
202
                                  GIMP_EXPORT_CAN_HANDLE_GRAY    |
                                  GIMP_EXPORT_CAN_HANDLE_RGB     |
                                  GIMP_EXPORT_CAN_HANDLE_INDEXED |
                                  GIMP_EXPORT_CAN_HANDLE_ALPHA);

      if (export == GIMP_EXPORT_CANCEL)
        return gimp_procedure_new_return_values (procedure, GIMP_PDB_CANCEL,
                                                 NULL);
      break;

    default:
      break;
    }

203
  if (run_mode == GIMP_RUN_INTERACTIVE)
204
    {
205
      if (! save_dialog (procedure, G_OBJECT (config)))
206
        status = GIMP_PDB_CANCEL;
207
    }
208

209
210
211
  if (status == GIMP_PDB_SUCCESS)
    {
      GimpValueArray *save_retvals;
212
      GimpValueArray *args;
213
214
215
216

      g_object_get (config,
                    "description", &description,
                    NULL);
217

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
      args = gimp_value_array_new_from_types (NULL,
                                              GIMP_TYPE_RUN_MODE,     GIMP_RUN_NONINTERACTIVE,
                                              GIMP_TYPE_IMAGE,        image,
                                              G_TYPE_INT,             n_drawables,
                                              GIMP_TYPE_OBJECT_ARRAY, NULL,
                                              G_TYPE_FILE,            file,
                                              G_TYPE_STRING,          description,
                                              G_TYPE_NONE);
      gimp_value_set_object_array (gimp_value_array_index (args, 3),
                                   GIMP_TYPE_ITEM, (GObject **) drawables, n_drawables);

      save_retvals = gimp_pdb_run_procedure_array (gimp_get_pdb (),
                                                   "file-pat-save-internal",
                                                   args);
      gimp_value_array_unref (args);
233

234
      if (GIMP_VALUES_GET_ENUM (save_retvals, 0) != GIMP_PDB_SUCCESS)
235
        {
236
237
238
          g_set_error (&error, 0, 0,
                       "Running procedure 'file-pat-save-internal' "
                       "failed: %s",
239
                       gimp_pdb_get_last_error (gimp_get_pdb ()));
240
241

          status = GIMP_PDB_EXECUTION_ERROR;
242
        }
243
244

      gimp_value_array_unref (save_retvals);
Sven Neumann's avatar
Sven Neumann committed
245
    }
246

247
  gimp_procedure_config_end_run (config, status);
248
249
  g_object_unref (config);

250
  if (export == GIMP_EXPORT_EXPORT)
251
252
253
254
    {
      gimp_image_delete (image);
      g_free (drawables);
    }
255

256
  return gimp_procedure_new_return_values (procedure, status, error);
Sven Neumann's avatar
Sven Neumann committed
257
}
Elliot Lee's avatar
Elliot Lee committed
258

259
static gboolean
260
261
save_dialog (GimpProcedure *procedure,
             GObject       *config)
Elliot Lee's avatar
Elliot Lee committed
262
{
263
  GtkWidget *dialog;
264
  GtkWidget *grid;
265
  GtkWidget *entry;
266
  gboolean   run;
Sven Neumann's avatar
Sven Neumann committed
267

268
269
270
  dialog = gimp_procedure_dialog_new (procedure,
                                      GIMP_PROCEDURE_CONFIG (config),
                                      _("Export Image as Pattern"));
271

272
273
  grid = gtk_grid_new ();
  gtk_container_set_border_width (GTK_CONTAINER (grid), 12);
274
275
  gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
276
277
                      grid, TRUE, TRUE, 0);
  gtk_widget_show (grid);
278

279
  entry = gimp_prop_entry_new (config, "description", 256);
280
  gtk_entry_set_width_chars (GTK_ENTRY (entry), 20);
281
  gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
282

283
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 0,
284
                            _("_Description:"), 1.0, 0.5,
285
                            entry, 1);
Elliot Lee's avatar
Elliot Lee committed
286

287
  gtk_widget_show (dialog);
288

289
  run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
290

291
  gtk_widget_destroy (dialog);
Elliot Lee's avatar
Elliot Lee committed
292

293
  return run;
Elliot Lee's avatar
Elliot Lee committed
294
}