templates-commands.c 10.7 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

23
#include "libgimpconfig/gimpconfig.h"
24 25
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
26
#include "actions-types.h"
27

28
#include "config/gimpcoreconfig.h"
29 30

#include "core/gimp.h"
31
#include "core/gimp-utils.h"
32
#include "core/gimpcontainer.h"
33
#include "core/gimpcontext.h"
34
#include "core/gimpimage-new.h"
35 36
#include "core/gimptemplate.h"

37
#include "widgets/gimpcontainerview.h"
38
#include "widgets/gimpdialogfactory.h"
39
#include "widgets/gimphelp-ids.h"
40 41
#include "widgets/gimpmessagebox.h"
#include "widgets/gimpmessagedialog.h"
42
#include "widgets/gimptemplateeditor.h"
43 44
#include "widgets/gimptemplateview.h"

45
#include "dialogs/dialogs.h"
46
#include "dialogs/template-options-dialog.h"
Michael Natterer's avatar
Michael Natterer committed
47

48
#include "actions.h"
49 50
#include "templates-commands.h"

51 52
#include "gimp-intl.h"

53

54
typedef struct
55
{
56
  GimpContext   *context;
57 58
  GimpContainer *container;
  GimpTemplate  *template;
59
} TemplateDeleteData;
60 61 62 63


/*  local function prototypes  */

64 65 66 67 68 69 70 71 72 73
static void   templates_new_response     (GtkWidget             *dialog,
                                          gint                   response_id,
                                          TemplateOptionsDialog *options);
static void   templates_edit_response    (GtkWidget             *widget,
                                          gint                   response_id,
                                          TemplateOptionsDialog *options);
static void   templates_delete_response  (GtkWidget             *dialog,
                                          gint                   response_id,
                                          TemplateDeleteData    *delete_data);
static void   templates_delete_data_free (TemplateDeleteData    *delete_data);
74 75


76 77
/*  public functions */

78 79 80 81
void
templates_create_image_cmd_callback (GtkAction *action,
                                     gpointer   data)
{
82
  Gimp                *gimp;
83 84 85 86
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;
  GimpContext         *context;
  GimpTemplate        *template;
87
  return_if_no_gimp(gimp,data);
88 89 90 91 92 93 94 95

  container = gimp_container_view_get_container (editor->view);
  context   = gimp_container_view_get_context (editor->view);

  template = gimp_context_get_template (context);

  if (template && gimp_container_have (container, GIMP_OBJECT (template)))
    {
96 97
      gimp_template_create_image (gimp, template, context);
      gimp_image_new_set_last_template (gimp, template);
98 99 100
    }
}

101
void
102 103
templates_new_cmd_callback (GtkAction *action,
                            gpointer   data)
104
{
105 106 107
  GimpContainerEditor   *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext           *context;
  TemplateOptionsDialog *options;
108

109
  context = gimp_container_view_get_context (editor->view);
110

111
  options = template_options_dialog_new (NULL, context,
112 113 114 115 116 117
                                         GTK_WIDGET (editor),
                                         _("New Template"),
                                         "gimp-template-new",
                                         GIMP_STOCK_TEMPLATE,
                                         _("Create a New Template"),
                                         GIMP_HELP_TEMPLATE_NEW);
118

119
  g_signal_connect (options->dialog, "response",
120
                    G_CALLBACK (templates_new_response),
121
                    options);
122

123
  gtk_widget_show (options->dialog);
124 125 126
}

void
127 128
templates_duplicate_cmd_callback (GtkAction *action,
                                  gpointer   data)
129
{
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;
  GimpContext         *context;
  GimpTemplate        *template;

  container = gimp_container_view_get_container (editor->view);
  context   = gimp_container_view_get_context (editor->view);

  template = gimp_context_get_template (context);

  if (template && gimp_container_have (container, GIMP_OBJECT (template)))
    {
      GimpTemplate *new_template;

      new_template = gimp_config_duplicate (GIMP_CONFIG (template));

      gimp_container_add (container, GIMP_OBJECT (new_template));
147 148
      gimp_context_set_by_type (context,
                                gimp_container_get_children_type (container),
149 150
                                GIMP_OBJECT (new_template));
      g_object_unref (new_template);
151

152
      templates_edit_cmd_callback (action, data);
153
    }
154 155
}

156
void
157 158
templates_edit_cmd_callback (GtkAction *action,
                             gpointer   data)
159
{
160 161 162 163
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;
  GimpContext         *context;
  GimpTemplate        *template;
164

165 166 167 168 169 170 171
  container = gimp_container_view_get_container (editor->view);
  context   = gimp_container_view_get_context (editor->view);

  template = gimp_context_get_template (context);

  if (template && gimp_container_have (container, GIMP_OBJECT (template)))
    {
172 173
      TemplateOptionsDialog *options;

174
      options = template_options_dialog_new (template, context,
175 176 177
                                             GTK_WIDGET (editor),
                                             _("Edit Template"),
                                             "gimp-template-edit",
178
                                             GTK_STOCK_EDIT,
179 180 181 182
                                             _("Edit Template"),
                                             GIMP_HELP_TEMPLATE_EDIT);

      g_signal_connect (options->dialog, "response",
183
                        G_CALLBACK (templates_edit_response),
184 185 186
                        options);

      gtk_widget_show (options->dialog);
187
    }
188 189
}

190
void
191 192
templates_delete_cmd_callback (GtkAction *action,
                               gpointer   data)
193
{
194 195 196 197 198 199 200 201 202 203 204 205
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;
  GimpContext         *context;
  GimpTemplate        *template;

  container = gimp_container_view_get_container (editor->view);
  context   = gimp_container_view_get_context (editor->view);

  template = gimp_context_get_template (context);

  if (template && gimp_container_have (container, GIMP_OBJECT (template)))
    {
206 207
      TemplateDeleteData *delete_data = g_slice_new (TemplateDeleteData);
      GtkWidget          *dialog;
208

209
      delete_data->context   = context;
210 211 212
      delete_data->container = container;
      delete_data->template  = template;

213
      dialog =
Sven Neumann's avatar
Sven Neumann committed
214
        gimp_message_dialog_new (_("Delete Template"), GTK_STOCK_DELETE,
215 216 217 218 219
                                 GTK_WIDGET (editor), 0,
                                 gimp_standard_help_func, NULL,

                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
220

221
                                 NULL);
222

223 224 225 226
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
227

228 229 230
      g_object_weak_ref (G_OBJECT (dialog),
                         (GWeakNotify) templates_delete_data_free, delete_data);

231 232 233
      g_signal_connect_object (template, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);
234

235
      g_signal_connect (dialog, "response",
236
                        G_CALLBACK (templates_delete_response),
237
                        delete_data);
238

239 240 241 242 243
      gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                                         _("Are you sure you want to delete "
                                           "template '%s' from the list and "
                                           "from disk?"),
                                         GIMP_OBJECT (template)->name);
244 245
      gtk_widget_show (dialog);
    }
246
}
247 248


249
/*  private functions  */
250 251

static void
252 253 254
templates_new_response (GtkWidget             *dialog,
                        gint                   response_id,
                        TemplateOptionsDialog *options)
255
{
256 257
  if (response_id == GTK_RESPONSE_OK)
    {
258 259
      GimpTemplateEditor *editor   = GIMP_TEMPLATE_EDITOR (options->editor);
      GimpTemplate       *template = editor->template;
260

261 262 263
      gimp_container_add (options->gimp->templates, GIMP_OBJECT (template));
      gimp_context_set_template (gimp_get_user_context (options->gimp),
                                 template);
264
    }
265 266 267 268

  gtk_widget_destroy (dialog);
}

269
static void
270 271 272
templates_edit_response (GtkWidget             *dialog,
                         gint                   response_id,
                         TemplateOptionsDialog *options)
273
{
274 275
  if (response_id == GTK_RESPONSE_OK)
    {
276
      GimpTemplateEditor *editor = GIMP_TEMPLATE_EDITOR (options->editor);
277

278 279
      gimp_config_sync (G_OBJECT (editor->template),
                        G_OBJECT (options->template), 0);
280
    }
281

282 283 284 285
  gtk_widget_destroy (dialog);
}

static void
286 287 288
templates_delete_response (GtkWidget          *dialog,
                           gint                response_id,
                           TemplateDeleteData *delete_data)
289 290 291
{
  if (response_id == GTK_RESPONSE_OK)
    {
292 293 294 295 296 297
      GimpObject *new_active;

      new_active = gimp_container_get_neighbor_of_active (delete_data->container,
                                                          delete_data->context,
                                                          GIMP_OBJECT (delete_data->template));

298 299 300
      if (gimp_container_have (delete_data->container,
                               GIMP_OBJECT (delete_data->template)))
        {
301 302
          if (new_active)
            gimp_context_set_by_type (delete_data->context,
303
                                      gimp_container_get_children_type (delete_data->container),
304 305
                                      new_active);

306 307 308
          gimp_container_remove (delete_data->container,
                                 GIMP_OBJECT (delete_data->template));
        }
309
    }
310 311

  gtk_widget_destroy (dialog);
312
}
313 314 315 316 317 318

static void
templates_delete_data_free (TemplateDeleteData *delete_data)
{
  g_slice_free (TemplateDeleteData, delete_data);
}