templates-commands.c 10.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 43
#include "widgets/gimptemplateview.h"

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

48 49
#include "templates-commands.h"

50 51
#include "gimp-intl.h"

52

53
typedef struct _TemplateDeleteData TemplateDeleteData;
54

55
struct _TemplateDeleteData
56 57 58 59 60 61 62 63
{
  GimpContainer *container;
  GimpTemplate  *template;
};


/*  local function prototypes  */

64 65 66 67 68 69 70 71 72
static void templates_new_template_response   (GtkWidget             *dialog,
                                               gint                   response_id,
                                               TemplateOptionsDialog *options);
static void templates_edit_template_response  (GtkWidget             *widget,
                                               gint                   response_id,
                                               TemplateOptionsDialog *options);
static void templates_delete_template_response (GtkWidget            *dialog,
                                                gint                  response_id,
                                                TemplateDeleteData   *delete_data);
73 74


75 76
/*  public functions */

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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)))
    {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      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));
        }
108 109 110
    }
}

111
void
112
templates_new_template_cmd_callback (GtkAction *action,
113 114
                                     gpointer   data)
{
115 116 117
  GimpContainerEditor   *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext           *context;
  TemplateOptionsDialog *options;
118

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

121 122 123 124 125 126 127
  options = template_options_dialog_new (context->gimp, NULL,
                                         GTK_WIDGET (editor),
                                         _("New Template"),
                                         "gimp-template-new",
                                         GIMP_STOCK_TEMPLATE,
                                         _("Create a New Template"),
                                         GIMP_HELP_TEMPLATE_NEW);
128

129 130 131
  g_signal_connect (options->dialog, "response",
                    G_CALLBACK (templates_new_template_response),
                    options);
132

133
  gtk_widget_show (options->dialog);
134 135 136
}

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

      templates_edit_template_cmd_callback (action, data);
162
    }
163 164
}

165
void
166
templates_edit_template_cmd_callback (GtkAction *action,
167 168
                                      gpointer   data)
{
169 170 171 172
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;
  GimpContext         *context;
  GimpTemplate        *template;
173

174 175 176 177 178 179 180
  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)))
    {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      TemplateOptionsDialog *options;

      options = template_options_dialog_new (context->gimp, template,
                                             GTK_WIDGET (editor),
                                             _("Edit Template"),
                                             "gimp-template-edit",
                                             GIMP_STOCK_EDIT,
                                             _("Edit Template"),
                                             GIMP_HELP_TEMPLATE_EDIT);

      g_signal_connect (options->dialog, "response",
                        G_CALLBACK (templates_edit_template_response),
                        options);

      gtk_widget_show (options->dialog);
196
    }
197 198
}

199
void
200
templates_delete_template_cmd_callback (GtkAction *action,
201 202
                                        gpointer   data)
{
203 204 205 206 207 208 209 210 211 212 213 214
  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)))
    {
215 216
      TemplateDeleteData *delete_data;
      GtkWidget           *dialog;
217

218
      delete_data = g_new0 (TemplateDeleteData, 1);
219 220 221 222

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

223 224 225 226 227 228 229
      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,
230

231
                                 NULL);
232

233
      g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) g_free, delete_data);
234 235 236
      g_signal_connect_object (template, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);
237

238
      g_signal_connect (dialog, "response",
239
                        G_CALLBACK (templates_delete_template_response),
240
                        delete_data);
241

242 243 244 245 246
      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);
247 248
      gtk_widget_show (dialog);
    }
249
}
250 251


252
/*  private functions  */
253 254

static void
255 256 257
templates_new_template_response (GtkWidget             *dialog,
                                 gint                   response_id,
                                 TemplateOptionsDialog *options)
258
{
259 260
  if (response_id == GTK_RESPONSE_OK)
    {
261 262
      GimpTemplateEditor *editor   = GIMP_TEMPLATE_EDITOR (options->editor);
      GimpTemplate       *template = editor->template;
263

264 265 266
      gimp_container_add (options->gimp->templates, GIMP_OBJECT (template));
      gimp_context_set_template (gimp_get_user_context (options->gimp),
                                 template);
267
    }
268 269 270 271

  gtk_widget_destroy (dialog);
}

272
static void
273 274 275
templates_edit_template_response (GtkWidget             *dialog,
                                  gint                   response_id,
                                  TemplateOptionsDialog *options)
276
{
277 278
  if (response_id == GTK_RESPONSE_OK)
    {
279
      GimpTemplateEditor *editor = GIMP_TEMPLATE_EDITOR (options->editor);
280

281
      gimp_config_sync (GIMP_CONFIG (editor->template),
282
                        GIMP_CONFIG (options->template), 0);
283
    }
284

285 286 287 288
  gtk_widget_destroy (dialog);
}

static void
289 290 291
templates_delete_template_response (GtkWidget          *dialog,
                                    gint                response_id,
                                    TemplateDeleteData *delete_data)
292 293 294 295 296 297 298 299 300
{
  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));
        }
301
    }
302 303

  gtk_widget_destroy (dialog);
304
}