data-commands.c 9.51 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
/* 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>

23 24
#include "libgimpwidgets/gimpwidgets.h"

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

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

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

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

#include "dialogs/dialogs.h"
43

44
#include "actions.h"
45 46
#include "data-commands.h"

47
#include "gimp-intl.h"
48 49


Michael Natterer's avatar
Michael Natterer committed
50 51 52 53 54 55 56 57 58 59 60
typedef struct _GimpDataDeleteData GimpDataDeleteData;

struct _GimpDataDeleteData
{
  GimpDataFactory *factory;
  GimpData        *data;
};


/*  local function prototypes  */

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


/*  public functions  */

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
void
data_open_as_image_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,
			      view->factory->container->children_type);

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

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

          image = file_open_with_display (context->gimp, context, NULL, uri,
                                          &status, &error);

          if (! image && status != GIMP_PDB_CANCEL)
            {
              gchar *filename;

99
              filename = file_utils_uri_display_name (uri);
100 101 102 103 104 105 106 107 108 109 110 111
              g_message (_("Opening '%s' failed:\n\n%s"),
                         filename, error->message);
              g_clear_error (&error);

              g_free (filename);
            }

          g_free (uri);
        }
    }
}

112
void
113 114
data_new_cmd_callback (GtkAction *action,
                       gpointer   user_data)
115
{
116 117 118 119 120 121 122 123 124
  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);
125

126 127 128 129 130 131 132 133 134 135 136
      data = gimp_data_factory_data_new (view->factory, _("Untitled"));

      if (data)
	{
	  gimp_context_set_by_type (context,
				    view->factory->container->children_type,
				    GIMP_OBJECT (data));

	  gtk_button_clicked (GTK_BUTTON (view->edit_button));
	}
    }
137 138 139
}

void
140 141
data_duplicate_cmd_callback (GtkAction *action,
                             gpointer   user_data)
142
{
143 144 145 146 147 148 149 150 151
  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,
			      view->factory->container->children_type);
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  if (data && gimp_container_have (view->factory->container,
				   GIMP_OBJECT (data)))
    {
      GimpData *new_data;

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

      if (new_data)
	{
	  gimp_context_set_by_type (context,
				    view->factory->container->children_type,
				    GIMP_OBJECT (new_data));

	  gtk_button_clicked (GTK_BUTTON (view->edit_button));
	}
    }
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
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,
			      view->factory->container->children_type);

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

      if (uri)
        {
          GtkClipboard *clipboard;

          clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                                     GDK_SELECTION_CLIPBOARD);
          gtk_clipboard_set_text (clipboard, uri, -1);

          clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                                     GDK_SELECTION_PRIMARY);
          gtk_clipboard_set_text (clipboard, uri, -1);

          g_free (uri);
        }
    }
}

206
void
207 208
data_delete_cmd_callback (GtkAction *action,
                          gpointer   user_data)
209
{
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  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,
			      view->factory->container->children_type);

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

      delete_data = g_new0 (GimpDataDeleteData, 1);

      delete_data->factory = view->factory;
      delete_data->data    = data;

231
      dialog = gimp_message_dialog_new (_("Delete Object"), GIMP_STOCK_QUESTION,
232 233
                                        GTK_WIDGET (view), 0,
                                        gimp_standard_help_func, NULL,
234

235 236
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                        GTK_STOCK_DELETE, GTK_RESPONSE_OK,
237

238
                                        NULL);
239

240 241 242 243
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
244

245 246 247
      g_signal_connect_object (data, "disconnect",
                               G_CALLBACK (gtk_widget_destroy),
                               dialog, G_CONNECT_SWAPPED);
248

249 250 251 252 253 254 255 256
      g_signal_connect (dialog, "response",
                        G_CALLBACK (data_delete_confirm_response),
                        delete_data);

      gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                                         _("Are you sure you want to delete "
                                           "'%s' from the list and from disk?"),
                                         GIMP_OBJECT (data)->name);
257 258
      gtk_widget_show (dialog);
    }
259 260 261
}

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

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

void
275 276 277
data_edit_cmd_callback (GtkAction   *action,
                        const gchar *value,
                        gpointer     user_data)
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
{
  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,
			      view->factory->container->children_type);

  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
302 303 304 305 306


/*  private functions  */

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

313
  if (response_id == GTK_RESPONSE_OK)
Michael Natterer's avatar
Michael Natterer committed
314 315 316 317 318 319 320 321 322 323 324
    {
      GError *error = NULL;

      if (! gimp_data_factory_data_delete (delete_data->factory,
                                           delete_data->data,
                                           TRUE, &error))
        {
          g_message (error->message);
          g_clear_error (&error);
        }
    }
325 326

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