templates-commands.c 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* The GIMP -- an image manipulation program
 * 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>

#include "libgimpwidgets/gimpwidgets.h"

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

27
#include "config/gimpconfig.h"
28
#include "config/gimpconfig-utils.h"
29
#include "config/gimpcoreconfig.h"
30 31

#include "core/gimp.h"
32
#include "core/gimpcontainer.h"
33 34 35
#include "core/gimpcontext.h"
#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/gimpviewabledialog.h"
44

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

48 49
#include "templates-commands.h"

50 51
#include "gimp-intl.h"

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
typedef struct _GimpTemplateDeleteData GimpTemplateDeleteData;

struct _GimpTemplateDeleteData
{
  GimpContainer *container;
  GimpTemplate  *template;
};


/*  local function prototypes  */

static void   templates_new_template_dialog     (Gimp         *gimp,
                                                 GtkWidget    *parent);
static void   templates_new_template_response   (GtkWidget    *widget,
                                                 gint          response_id,
                                                 GtkWidget    *dialog);
static void   templates_edit_template_dialog    (Gimp         *gimp,
                                                 GimpTemplate *template,
                                                 GtkWidget    *parent);
static void   templates_edit_template_response  (GtkWidget    *widget,
                                                 gint          response_id,
                                                 GtkWidget    *dialog);
static void   templates_delete_confirm_response (GtkWidget    *dialog,
                                                 gint          response_id,
                                                 GimpTemplateDeleteData *delete_data);


80 81
/*  public functions */

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
void
templates_create_image_cmd_callback (GtkAction *action,
                                     gpointer   data)
{
  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)))
    {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      GdkScreen *screen;
      GtkWidget *dialog;

      screen = gtk_widget_get_screen (GTK_WIDGET (editor));

      dialog = gimp_dialog_factory_dialog_new (global_dialog_factory, screen,
                                               "gimp-image-new-dialog",
                                               -1, FALSE);

      if (dialog)
        {
          image_new_dialog_set (dialog, NULL, template);

          gtk_window_present (GTK_WINDOW (dialog));
        }
113 114 115
    }
}

116
void
117
templates_new_template_cmd_callback (GtkAction *action,
118 119
                                     gpointer   data)
{
120 121 122 123 124 125 126
  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);
127

128 129 130 131 132 133
  template = gimp_context_get_template (context);

  if (template && gimp_container_have (container, GIMP_OBJECT (template)))
    {
    }

134
  templates_new_template_dialog (context->gimp, GTK_WIDGET (editor));
135 136 137
}

void
138
templates_duplicate_template_cmd_callback (GtkAction *action,
139 140
                                           gpointer   data)
{
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  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));

      gimp_context_set_by_type (context, container->children_type,
                                GIMP_OBJECT (new_template));

      templates_edit_template_dialog (context->gimp, new_template,
                                      GTK_WIDGET (editor));
164

165 166
      g_object_unref (new_template);
    }
167 168
}

169
void
170
templates_edit_template_cmd_callback (GtkAction *action,
171 172
                                      gpointer   data)
{
173 174 175 176
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;
  GimpContext         *context;
  GimpTemplate        *template;
177

178 179 180 181 182 183 184 185 186 187
  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)))
    {
      templates_edit_template_dialog (context->gimp, template,
                                      GTK_WIDGET (editor));
    }
188 189
}

190
void
191
templates_delete_template_cmd_callback (GtkAction *action,
192 193
                                        gpointer   data)
{
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  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)))
    {
      GimpTemplateDeleteData *delete_data;
      GtkWidget              *dialog;

      delete_data = g_new0 (GimpTemplateDeleteData, 1);

      delete_data->container = container;
      delete_data->template  = template;

214 215 216 217 218 219 220
      dialog =
        gimp_message_dialog_new (_("Delete Template"), GIMP_STOCK_QUESTION,
                                 GTK_WIDGET (editor), 0,
                                 gimp_standard_help_func, NULL,

                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
221

222
                                 NULL);
223

224 225 226
      g_signal_connect_object (template, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);
227

228 229 230
      g_signal_connect (dialog, "response",
                        G_CALLBACK (templates_delete_confirm_response),
                        delete_data);
231

232 233 234 235 236
      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);
237 238
      gtk_widget_show (dialog);
    }
239
}
240

241
/*  private functions  */
242

243
static void
244
templates_new_template_dialog (Gimp         *gimp,
245
                               GtkWidget    *parent)
246 247 248 249 250 251
{
  GimpTemplate *template;
  GtkWidget    *dialog;
  GtkWidget    *main_vbox;
  GtkWidget    *editor;

252
  dialog = gimp_viewable_dialog_new (NULL,
253
                                     _("New Template"), "gimp-template-new",
254 255
                                     GIMP_STOCK_TEMPLATE,
                                     _("Create a New Template"),
256
                                     parent,
257 258
                                     gimp_standard_help_func,
                                     GIMP_HELP_TEMPLATE_NEW,
259

260 261
                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                     GTK_STOCK_OK,     GTK_RESPONSE_OK,
262

263
                                     NULL);
264

265 266 267
  g_signal_connect (dialog, "response",
                    G_CALLBACK (templates_new_template_response),
                    dialog);
268

269 270
  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
271 272 273 274
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), main_vbox,
                      TRUE, TRUE, 0);
  gtk_widget_show (main_vbox);

275 276
  template = gimp_config_duplicate (GIMP_CONFIG (gimp->config->default_image));
  gimp_object_set_name (GIMP_OBJECT (template), _("Unnamed"));
277

278
  editor = gimp_template_editor_new (template, gimp, TRUE);
279

280 281 282 283
  g_object_unref (template);

  g_object_set_data (G_OBJECT (dialog), "gimp",          gimp);
  g_object_set_data (G_OBJECT (dialog), "gimp-template", template);
284

285 286
  gtk_box_pack_start (GTK_BOX (main_vbox), editor, FALSE, FALSE, 0);
  gtk_widget_show (editor);
287 288 289 290 291

  gtk_widget_show (dialog);
}

static void
292 293 294
templates_new_template_response (GtkWidget *widget,
                                 gint       response_id,
                                 GtkWidget *dialog)
295
{
296 297
  if (response_id == GTK_RESPONSE_OK)
    {
298 299
      GimpTemplate *template;
      Gimp         *gimp;
300

301 302
      template = g_object_get_data (G_OBJECT (dialog), "gimp-template");
      gimp     = g_object_get_data (G_OBJECT (dialog), "gimp");
303

304 305
      gimp_container_add (gimp->templates, GIMP_OBJECT (template));
      gimp_context_set_template (gimp_get_user_context (gimp), template);
306
    }
307 308 309 310

  gtk_widget_destroy (dialog);
}

311
static void
312
templates_edit_template_dialog (Gimp         *gimp,
313 314
                                GimpTemplate *template,
                                GtkWidget    *parent)
315 316 317 318 319
{
  GtkWidget *dialog;
  GtkWidget *main_vbox;
  GtkWidget *editor;

320
  dialog = gimp_viewable_dialog_new (GIMP_VIEWABLE (template),
321
                                     _("Edit Template"), "gimp-template-edit",
322 323
                                     GIMP_STOCK_EDIT,
                                     _("Edit Template"),
324
                                     parent,
325 326
                                     gimp_standard_help_func,
                                     GIMP_HELP_TEMPLATE_EDIT,
327

328 329
                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                     GTK_STOCK_OK,     GTK_RESPONSE_OK,
330

331
                                     NULL);
332

333 334 335
  g_signal_connect (dialog, "response",
                    G_CALLBACK (templates_edit_template_response),
                    dialog);
336

337 338
  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
339 340 341 342
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), main_vbox,
                      TRUE, TRUE, 0);
  gtk_widget_show (main_vbox);

343 344 345 346 347 348 349 350 351
  g_object_set_data (G_OBJECT (dialog), "gimp",          gimp);
  g_object_set_data (G_OBJECT (dialog), "gimp-template", template);

  template = gimp_config_duplicate (GIMP_CONFIG (template));

  editor = gimp_template_editor_new (template, gimp, TRUE);

  g_object_unref (template);

352 353 354 355 356 357 358 359
  gtk_box_pack_start (GTK_BOX (main_vbox), editor, FALSE, FALSE, 0);
  gtk_widget_show (editor);

  g_object_set_data (G_OBJECT (dialog), "gimp-template-editor", editor);

  gtk_widget_show (dialog);
}

360 361 362 363
static void
templates_edit_template_response (GtkWidget *widget,
                                  gint       response_id,
                                  GtkWidget *dialog)
364
{
365 366 367 368 369
  if (response_id == GTK_RESPONSE_OK)
    {
      GimpTemplateEditor *editor;
      GimpTemplate       *template;
      Gimp               *gimp;
370

371 372 373
      editor   = g_object_get_data (G_OBJECT (dialog), "gimp-template-editor");
      template = g_object_get_data (G_OBJECT (dialog), "gimp-template");
      gimp     = g_object_get_data (G_OBJECT (dialog), "gimp");
374

375 376 377
      gimp_config_sync (GIMP_CONFIG (editor->template),
                        GIMP_CONFIG (template), 0);
    }
378

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  gtk_widget_destroy (dialog);
}

static void
templates_delete_confirm_response (GtkWidget              *dialog,
                                   gint                    response_id,
                                   GimpTemplateDeleteData *delete_data)
{
  gtk_widget_destroy (dialog);

  if (response_id == GTK_RESPONSE_OK)
    {
      if (gimp_container_have (delete_data->container,
                               GIMP_OBJECT (delete_data->template)))
        {
          gimp_container_remove (delete_data->container,
                                 GIMP_OBJECT (delete_data->template));
        }
397
    }
398
}