data-commands.c 9.91 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 24
#include "libgimpwidgets/gimpwidgets.h"

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

27
#include "core/gimp.h"
28 29 30 31 32
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpdata.h"
#include "core/gimpdatafactory.h"

33 34 35
#include "file/file-open.h"
#include "file/file-utils.h"

36
#include "widgets/gimpclipboard.h"
37
#include "widgets/gimpcontainerview.h"
38
#include "widgets/gimpdataeditor.h"
39
#include "widgets/gimpdatafactoryview.h"
40
#include "widgets/gimpdialogfactory.h"
41 42
#include "widgets/gimpmessagebox.h"
#include "widgets/gimpmessagedialog.h"
43 44

#include "dialogs/dialogs.h"
45

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

49
#include "gimp-intl.h"
50 51


Michael Natterer's avatar
Michael Natterer committed
52 53 54 55
typedef struct _GimpDataDeleteData GimpDataDeleteData;

struct _GimpDataDeleteData
{
56 57
  GimpDataFactoryView *view;
  GimpData            *data;
Michael Natterer's avatar
Michael Natterer committed
58 59 60 61 62
};


/*  local function prototypes  */

63 64 65
static void  data_delete_confirm_response (GtkWidget          *dialog,
                                           gint                response_id,
                                           GimpDataDeleteData *delete_data);
Michael Natterer's avatar
Michael Natterer committed
66 67 68 69


/*  public functions  */

70 71 72 73 74 75 76 77
void
data_open_as_image_cmd_callback (GtkAction *action,
                                 gpointer   user_data)
{
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
  GimpContext         *context;
  GimpData            *data;

78 79
  context =
    gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
80 81 82

  data = (GimpData *)
    gimp_context_get_by_type (context,
83
                              view->factory->container->children_type);
84 85 86 87 88 89 90 91 92 93 94

  if (data && data->filename)
    {
      gchar *uri = g_filename_to_uri (data->filename, NULL, NULL);

      if (uri)
        {
          GimpImage         *image;
          GimpPDBStatusType  status;
          GError            *error = NULL;

95 96
          image = file_open_with_display (context->gimp, context, NULL,
                                          uri, FALSE,
97 98 99 100
                                          &status, &error);

          if (! image && status != GIMP_PDB_CANCEL)
            {
101
              gchar *filename = file_utils_uri_display_name (uri);
102

103 104 105 106
              gimp_message (context->gimp, G_OBJECT (view),
                            GIMP_MESSAGE_ERROR,
                            _("Opening '%s' failed:\n\n%s"),
                            filename, error->message);
107 108 109 110 111 112 113 114 115 116
              g_clear_error (&error);

              g_free (filename);
            }

          g_free (uri);
        }
    }
}

117
void
118 119
data_new_cmd_callback (GtkAction *action,
                       gpointer   user_data)
120
{
121 122 123 124 125 126 127 128 129
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);

  if (view->factory->data_new_func)
    {
      GimpContext *context;
      GimpData    *data;

      context =
        gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
130

131 132 133
      data = gimp_data_factory_data_new (view->factory, _("Untitled"));

      if (data)
134 135 136 137
        {
          gimp_context_set_by_type (context,
                                    view->factory->container->children_type,
                                    GIMP_OBJECT (data));
138

139 140
          gtk_button_clicked (GTK_BUTTON (view->edit_button));
        }
141
    }
142 143 144
}

void
145 146
data_duplicate_cmd_callback (GtkAction *action,
                             gpointer   user_data)
147
{
148 149 150 151 152 153 154 155
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
  GimpContext         *context;
  GimpData            *data;

  context = gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);

  data = (GimpData *)
    gimp_context_get_by_type (context,
156
                              view->factory->container->children_type);
157

158
  if (data && gimp_container_have (view->factory->container,
159
                                   GIMP_OBJECT (data)))
160 161 162 163 164 165
    {
      GimpData *new_data;

      new_data = gimp_data_factory_data_duplicate (view->factory, data);

      if (new_data)
166 167 168 169
        {
          gimp_context_set_by_type (context,
                                    view->factory->container->children_type,
                                    GIMP_OBJECT (new_data));
170

171 172
          gtk_button_clicked (GTK_BUTTON (view->edit_button));
        }
173
    }
174 175
}

176 177 178 179 180 181 182 183 184 185 186 187
void
data_copy_location_cmd_callback (GtkAction *action,
                                 gpointer   user_data)
{
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
  GimpContext         *context;
  GimpData            *data;

  context = gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);

  data = (GimpData *)
    gimp_context_get_by_type (context,
188
                              view->factory->container->children_type);
189 190 191 192 193 194 195

  if (data && data->filename && *data->filename)
    {
      gchar *uri = g_filename_to_uri (data->filename, NULL, NULL);

      if (uri)
        {
196
          gimp_clipboard_set_text (context->gimp, uri);
197 198 199 200 201
          g_free (uri);
        }
    }
}

202
void
203 204
data_delete_cmd_callback (GtkAction *action,
                          gpointer   user_data)
205
{
206 207 208 209
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
  GimpContext         *context;
  GimpData            *data;

210 211
  context =
    gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
212 213 214

  data = (GimpData *)
    gimp_context_get_by_type (context,
215
                              view->factory->container->children_type);
216 217 218 219 220 221 222 223 224

  if (data && data->deletable && gimp_container_have (view->factory->container,
                                                      GIMP_OBJECT (data)))
    {
      GimpDataDeleteData *delete_data;
      GtkWidget          *dialog;

      delete_data = g_new0 (GimpDataDeleteData, 1);

225 226
      delete_data->view = view;
      delete_data->data = data;
227

228
      dialog = gimp_message_dialog_new (_("Delete Object"), GIMP_STOCK_QUESTION,
229 230
                                        GTK_WIDGET (view), 0,
                                        gimp_standard_help_func, NULL,
231

232 233
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                        GTK_STOCK_DELETE, GTK_RESPONSE_OK,
234

235
                                        NULL);
236

237 238 239 240
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
241

242 243 244
      g_signal_connect_object (data, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);
245

246 247 248 249 250
      g_signal_connect (dialog, "response",
                        G_CALLBACK (data_delete_confirm_response),
                        delete_data);

      gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
251
                                         _("Delete '%s'?"),
252
                                         GIMP_OBJECT (data)->name);
253 254 255 256 257
      gimp_message_box_set_text(GIMP_MESSAGE_DIALOG (dialog)->box,
                                _("Are you sure you want to remove '%s' "
                                  "from the list and delete it on disk?"),
                                GIMP_OBJECT (data)->name);

258 259
      gtk_widget_show (dialog);
    }
260 261 262
}

void
263 264
data_refresh_cmd_callback (GtkAction *action,
                           gpointer   user_data)
265
{
266
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
267 268
  Gimp                *gimp;
  return_if_no_gimp (gimp, user_data);
269

270
  gimp_set_busy (gimp);
271
  gimp_data_factory_data_refresh (view->factory);
272
  gimp_unset_busy (gimp);
273
}
274 275

void
276 277 278
data_edit_cmd_callback (GtkAction   *action,
                        const gchar *value,
                        gpointer     user_data)
279 280 281 282 283 284 285 286 287
{
  GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
  GimpContext         *context;
  GimpData            *data;

  context = gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);

  data = (GimpData *)
    gimp_context_get_by_type (context,
288
                              view->factory->container->children_type);
289 290 291 292 293 294 295 296 297 298 299 300 301 302

  if (data && gimp_container_have (view->factory->container,
                                   GIMP_OBJECT (data)))
    {
      GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (view));
      GtkWidget *dockable;

      dockable = gimp_dialog_factory_dialog_raise (global_dock_factory, screen,
                                                   value, -1);

      gimp_data_editor_set_data (GIMP_DATA_EDITOR (GTK_BIN (dockable)->child),
                                 data);
    }
}
Michael Natterer's avatar
Michael Natterer committed
303 304 305 306 307


/*  private functions  */

static void
308 309 310
data_delete_confirm_response (GtkWidget          *dialog,
                              gint                response_id,
                              GimpDataDeleteData *delete_data)
Michael Natterer's avatar
Michael Natterer committed
311
{
312
  gtk_widget_destroy (dialog);
Michael Natterer's avatar
Michael Natterer committed
313

314
  if (response_id == GTK_RESPONSE_OK)
Michael Natterer's avatar
Michael Natterer committed
315 316 317
    {
      GError *error = NULL;

318
      if (! gimp_data_factory_data_delete (delete_data->view->factory,
Michael Natterer's avatar
Michael Natterer committed
319 320 321
                                           delete_data->data,
                                           TRUE, &error))
        {
322 323 324
          gimp_message (delete_data->view->factory->gimp,
                        G_OBJECT (delete_data->view), GIMP_MESSAGE_ERROR,
                        "%s", error->message);
Michael Natterer's avatar
Michael Natterer committed
325 326 327
          g_clear_error (&error);
        }
    }
328 329

  g_free (delete_data);
Michael Natterer's avatar
Michael Natterer committed
330
}