templates-commands.c 11.5 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
 */

#include "config.h"

20
#include <gegl.h>
21 22
#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/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
#include "widgets/gimptemplateview.h"
43
#include "widgets/gimpwidgets-utils.h"
44

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 74 75 76 77
static void   templates_new_callback     (GtkWidget          *dialog,
                                          GimpTemplate       *template,
                                          GimpTemplate       *edit_template,
                                          GimpContext        *context,
                                          gpointer            user_data);
static void   templates_edit_callback    (GtkWidget          *dialog,
                                          GimpTemplate       *template,
                                          GimpTemplate       *edit_template,
                                          GimpContext        *context,
                                          gpointer            user_data);
static void   templates_delete_response  (GtkWidget          *dialog,
                                          gint                response_id,
                                          TemplateDeleteData *delete_data);
static void   templates_delete_data_free (TemplateDeleteData *delete_data);
78 79


80 81
/*  public functions */

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

  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)))
    {
100
      GtkWidget *widget = GTK_WIDGET (editor);
101 102 103
      GimpImage *image;

      image = gimp_image_new_from_template (gimp, template, context);
104
      gimp_create_display (gimp, image, gimp_template_get_unit (template), 1.0,
105
                           G_OBJECT (gimp_widget_get_monitor (widget)));
106 107
      g_object_unref (image);

108
      gimp_image_new_set_last_template (gimp, template);
109 110 111
    }
}

112
void
113 114
templates_new_cmd_callback (GtkAction *action,
                            gpointer   data)
115
{
116 117 118
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext         *context;
  GtkWidget           *dialog;
119

120
  context = gimp_container_view_get_context (editor->view);
121

122 123 124
#define NEW_DIALOG_KEY "gimp-template-new-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (context->gimp), NEW_DIALOG_KEY);
125

126 127 128 129 130 131
  if (! dialog)
    {
      dialog = template_options_dialog_new (NULL, context,
                                            GTK_WIDGET (editor),
                                            _("New Template"),
                                            "gimp-template-new",
132
                                            GIMP_ICON_TEMPLATE,
133 134 135 136 137 138 139
                                            _("Create a New Template"),
                                            GIMP_HELP_TEMPLATE_NEW,
                                            templates_new_callback,
                                            NULL);

      dialogs_attach_dialog (G_OBJECT (context->gimp), NEW_DIALOG_KEY, dialog);
    }
140

141
  gtk_window_present (GTK_WINDOW (dialog));
142 143 144
}

void
145 146
templates_duplicate_cmd_callback (GtkAction *action,
                                  gpointer   data)
147
{
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  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));
165 166
      gimp_context_set_by_type (context,
                                gimp_container_get_children_type (container),
167 168
                                GIMP_OBJECT (new_template));
      g_object_unref (new_template);
169

170
      templates_edit_cmd_callback (action, data);
171
    }
172 173
}

174
void
175 176
templates_edit_cmd_callback (GtkAction *action,
                             gpointer   data)
177
{
178 179 180 181
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;
  GimpContext         *context;
  GimpTemplate        *template;
182

183 184 185 186 187 188 189
  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)))
    {
190
      GtkWidget *dialog;
191

192
#define EDIT_DIALOG_KEY "gimp-template-edit-dialog"
193

194
      dialog = dialogs_get_dialog (G_OBJECT (template), EDIT_DIALOG_KEY);
195

196 197 198 199 200 201
      if (! dialog)
        {
          dialog = template_options_dialog_new (template, context,
                                                GTK_WIDGET (editor),
                                                _("Edit Template"),
                                                "gimp-template-edit",
202
                                                GIMP_ICON_EDIT,
203 204 205 206 207 208 209 210 211
                                                _("Edit Template"),
                                                GIMP_HELP_TEMPLATE_EDIT,
                                                templates_edit_callback,
                                                NULL);

          dialogs_attach_dialog (G_OBJECT (template), EDIT_DIALOG_KEY, dialog);
        }

      gtk_window_present (GTK_WINDOW (dialog));
212
    }
213 214
}

215
void
216 217
templates_delete_cmd_callback (GtkAction *action,
                               gpointer   data)
218
{
219 220 221 222 223 224 225 226 227 228 229 230
  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)))
    {
231 232
      TemplateDeleteData *delete_data = g_slice_new (TemplateDeleteData);
      GtkWidget          *dialog;
233

234
      delete_data->context   = context;
235 236 237
      delete_data->container = container;
      delete_data->template  = template;

238
      dialog =
239
        gimp_message_dialog_new (_("Delete Template"), "edit-delete",
240 241 242
                                 GTK_WIDGET (editor), 0,
                                 gimp_standard_help_func, NULL,

243 244
                                 _("_Cancel"), GTK_RESPONSE_CANCEL,
                                 _("_Delete"), GTK_RESPONSE_OK,
245

246
                                 NULL);
247

248
      gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
249 250 251
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
252

253 254 255
      g_object_weak_ref (G_OBJECT (dialog),
                         (GWeakNotify) templates_delete_data_free, delete_data);

256 257 258
      g_signal_connect_object (template, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);
259

260
      g_signal_connect (dialog, "response",
261
                        G_CALLBACK (templates_delete_response),
262
                        delete_data);
263

264 265 266 267
      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?"),
268
                                         gimp_object_get_name (template));
269 270
      gtk_widget_show (dialog);
    }
271
}
272 273


274
/*  private functions  */
275 276

static void
277 278 279 280 281
templates_new_callback (GtkWidget    *dialog,
                        GimpTemplate *template,
                        GimpTemplate *edit_template,
                        GimpContext  *context,
                        gpointer      user_data)
282
{
283 284 285
  gimp_container_add (context->gimp->templates, GIMP_OBJECT (edit_template));
  gimp_context_set_template (gimp_get_user_context (context->gimp),
                             edit_template);
286 287 288 289

  gtk_widget_destroy (dialog);
}

290
static void
291 292 293 294 295
templates_edit_callback (GtkWidget    *dialog,
                         GimpTemplate *template,
                         GimpTemplate *edit_template,
                         GimpContext  *context,
                         gpointer      user_data)
296
{
297 298
  gimp_config_sync (G_OBJECT (edit_template),
                    G_OBJECT (template), 0);
299

300 301 302 303
  gtk_widget_destroy (dialog);
}

static void
304 305 306
templates_delete_response (GtkWidget          *dialog,
                           gint                response_id,
                           TemplateDeleteData *delete_data)
307 308 309
{
  if (response_id == GTK_RESPONSE_OK)
    {
310
      GimpObject *new_active = NULL;
311

312 313 314 315 316 317
      if (delete_data->template ==
          gimp_context_get_template (delete_data->context))
        {
          new_active = gimp_container_get_neighbor_of (delete_data->container,
                                                       GIMP_OBJECT (delete_data->template));
        }
318

319 320 321
      if (gimp_container_have (delete_data->container,
                               GIMP_OBJECT (delete_data->template)))
        {
322 323
          if (new_active)
            gimp_context_set_by_type (delete_data->context,
324
                                      gimp_container_get_children_type (delete_data->container),
325 326
                                      new_active);

327 328 329
          gimp_container_remove (delete_data->container,
                                 GIMP_OBJECT (delete_data->template));
        }
330
    }
331 332

  gtk_widget_destroy (dialog);
333
}
334 335 336 337 338 339

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