templates-commands.c 10.6 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 */

#include "config.h"

#include <gtk/gtk.h>

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

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

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

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

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

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

46
#include "actions.h"
47 48
#include "templates-commands.h"

49 50
#include "gimp-intl.h"

51

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


/*  local function prototypes  */

62 63 64 65 66 67 68 69 70 71
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);
72 73


74 75
/*  public functions */

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

  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)))
    {
94
      gimp_image_new_from_template (gimp, template, context);
95
      gimp_image_new_set_last_template (gimp, template);
96 97 98
    }
}

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

107
  context = gimp_container_view_get_context (editor->view);
108

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

117
  g_signal_connect (options->dialog, "response",
118
                    G_CALLBACK (templates_new_response),
119
                    options);
120

121
  gtk_widget_show (options->dialog);
122 123 124
}

void
125 126
templates_duplicate_cmd_callback (GtkAction *action,
                                  gpointer   data)
127
{
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  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));
145 146
      gimp_context_set_by_type (context,
                                gimp_container_get_children_type (container),
147 148
                                GIMP_OBJECT (new_template));
      g_object_unref (new_template);
149

150
      templates_edit_cmd_callback (action, data);
151
    }
152 153
}

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

163 164 165 166 167 168 169
  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)))
    {
170 171
      TemplateOptionsDialog *options;

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

      g_signal_connect (options->dialog, "response",
181
                        G_CALLBACK (templates_edit_response),
182 183 184
                        options);

      gtk_widget_show (options->dialog);
185
    }
186 187
}

188
void
189 190
templates_delete_cmd_callback (GtkAction *action,
                               gpointer   data)
191
{
192 193 194 195 196 197 198 199 200 201 202 203
  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)))
    {
204 205
      TemplateDeleteData *delete_data = g_slice_new (TemplateDeleteData);
      GtkWidget          *dialog;
206

207
      delete_data->context   = context;
208 209 210
      delete_data->container = container;
      delete_data->template  = template;

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

                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
218

219
                                 NULL);
220

221 222 223 224
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
225

226 227 228
      g_object_weak_ref (G_OBJECT (dialog),
                         (GWeakNotify) templates_delete_data_free, delete_data);

229 230 231
      g_signal_connect_object (template, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);
232

233
      g_signal_connect (dialog, "response",
234
                        G_CALLBACK (templates_delete_response),
235
                        delete_data);
236

237 238 239 240
      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?"),
241
                                         gimp_object_get_name (template));
242 243
      gtk_widget_show (dialog);
    }
244
}
245 246


247
/*  private functions  */
248 249

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

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

  gtk_widget_destroy (dialog);
}

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

276 277
      gimp_config_sync (G_OBJECT (editor->template),
                        G_OBJECT (options->template), 0);
278
    }
279

280 281 282 283
  gtk_widget_destroy (dialog);
}

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

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

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

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

  gtk_widget_destroy (dialog);
310
}
311 312 313 314 315 316

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