summaryrefslogtreecommitdiff
path: root/gladeui/glade-clipboard.c
blob: a77c72b52a9b2248de23bbefea1b6709774b277f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
206
207
208
209
210
211
212
213
214
215
216
/*
 * glade-clipboard.c - An object for handling Cut/Copy/Paste.
 *
 * Copyright (C) 2001 The GNOME Foundation.
 *
 * Author(s):
 *      Archit Baweja <bighead@users.sourceforge.net>
 *
 * 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"

/**
 * SECTION:glade-clipboard
 * @Short_Description: A list of #GladeWidget objects not in any #GladeProject.
 *
 * The #GladeClipboard is a singleton and is an accumulative shelf
 * of all cut or copied #GladeWidget in the application. A #GladeWidget
 * can be cut from one #GladeProject and pasted to another.
 */

#include <glib/gi18n-lib.h>
#include "glade.h"
#include "glade-clipboard.h"
#include "glade-widget.h"
#include "glade-placeholder.h"
#include "glade-project.h"

struct _GladeClipboardPrivate
{
  GList     *widgets;       /* A list of GladeWidget's on the clipboard */
  gboolean   has_selection; /* TRUE if clipboard has selection */
};

enum
{
  PROP_0,
  PROP_HAS_SELECTION,
  N_PROPERTIES
};

static GParamSpec *properties[N_PROPERTIES];

G_DEFINE_TYPE_WITH_PRIVATE (GladeClipboard, glade_clipboard, G_TYPE_OBJECT);

static void
glade_project_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  GladeClipboard *clipboard = GLADE_CLIPBOARD (object);

  switch (prop_id)
    {
      case PROP_HAS_SELECTION:
        g_value_set_boolean (value, clipboard->priv->has_selection);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
glade_clipboard_class_init (GladeClipboardClass * klass)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = glade_project_get_property;

  properties[PROP_HAS_SELECTION] =
    g_param_spec_boolean ("has-selection",
                          "Has Selection",
                          "Whether clipboard has a selection of items to paste",
                          FALSE,
                          G_PARAM_READABLE);
  
  /* Install all properties */
  g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}

static void
glade_clipboard_init (GladeClipboard *clipboard)
{
  clipboard->priv = glade_clipboard_get_instance_private (clipboard);

  clipboard->priv->widgets = NULL;
  clipboard->priv->has_selection = FALSE;
}

static void
glade_clipboard_set_has_selection (GladeClipboard *clipboard,
                                   gboolean        has_selection)
{
  if (clipboard->priv->has_selection != has_selection)
    {
      clipboard->priv->has_selection = has_selection;
      g_object_notify_by_pspec (G_OBJECT (clipboard), properties[PROP_HAS_SELECTION]);
    }

}

/**
 * glade_clipboard_get_has_selection:
 * @clipboard: a #GladeClipboard
 * 
 * Returns: TRUE if this clipboard has selected items to paste.
 */
gboolean
glade_clipboard_get_has_selection (GladeClipboard *clipboard)
{
  g_return_val_if_fail (GLADE_IS_CLIPBOARD (clipboard), FALSE);

  return clipboard->priv->has_selection;
}

/**
 * glade_clipboard_widgets:
 * @clipboard: a #GladeClipboard
 * 
 * Returns: (element-type GladeWidget) (transfer none): a #GList of #GladeWidgets
 */
GList *
glade_clipboard_widgets (GladeClipboard *clipboard)
{
  g_return_val_if_fail (GLADE_IS_CLIPBOARD (clipboard), NULL);


  return clipboard->priv->widgets;
}

/**
 * glade_clipboard_new:
 * 
 * Returns: a new #GladeClipboard object
 */
GladeClipboard *
glade_clipboard_new (void)
{
  return GLADE_CLIPBOARD (g_object_new (GLADE_TYPE_CLIPBOARD, NULL));
}

/**
 * glade_clipboard_add:
 * @clipboard: a #GladeClipboard
 * @widgets: (element-type GladeWidget): a #GList of #GladeWidgets
 * 
 * Adds @widgets to @clipboard.
 * This increases the reference count of each #GladeWidget in @widgets.
 */
void
glade_clipboard_add (GladeClipboard *clipboard, GList *widgets)
{
  GladeWidget *widget;
  GList *list;

  g_return_if_fail (GLADE_IS_CLIPBOARD (clipboard));

  glade_clipboard_clear (clipboard);

  /*
   * Add the widgets to the list of children.
   */
  for (list = widgets; list && list->data; list = list->next)
    {
      widget = list->data;
      clipboard->priv->widgets =
          g_list_prepend (clipboard->priv->widgets, g_object_ref_sink (G_OBJECT (widget)));
    }

  glade_clipboard_set_has_selection (clipboard, TRUE);
}

/**
 * glade_clipboard_clear:
 * @clipboard: a #GladeClipboard
 * 
 * Removes all widgets from the @clipboard.
 */
void
glade_clipboard_clear (GladeClipboard *clipboard)
{
  GladeWidget *widget;
  GList *list;

  g_return_if_fail (GLADE_IS_CLIPBOARD (clipboard));

  for (list = clipboard->priv->widgets; list && list->data; list = list->next)
    {
      widget = list->data;

      g_object_unref (G_OBJECT (widget));
    }

  clipboard->priv->widgets = 
    (g_list_free (clipboard->priv->widgets), NULL);

  glade_clipboard_set_has_selection (clipboard, FALSE);
}