summaryrefslogtreecommitdiff
path: root/thunarx/thunarx-property-page.c
blob: 952c590354ed7872e850b5c4d920387d3ab4cb9e (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n-lib.h>

#include <thunarx/thunarx-private.h>
#include <thunarx/thunarx-property-page.h>



/**
 * SECTION: thunarx-property-page
 * @short_description: The base class for pages added to the properties dialog
 * @title: ThunarxPropertyPage
 * @include: thunarx/thunarx.h
 *
 * The class for pages that can be added to Thunar's file properties dialog
 * by extensions implementing the #ThunarxPropertyPageProvider interface. The
 * pages returned by extensions from thunarx_property_page_provider_get_pages()
 * method are instances of this class or a derived class. Note that extensions
 * do not need to subclass #ThunarxPropertyPage, but may also instantiate it
 * directly and add widgets to it, but I strongly suggest to create a subclass
 * as it usually leads to better modularization and thereby better maintainability
 * in the code.
 *
 * To pick up the #TagPage example from the thunarx_property_page_provider_get_pages()
 * description again, you'd create a new class #TagPage, that inherits #ThunarxPropertyPage
 * (using the #THUNARX_DEFINE_TYPE macro), which provides several user interface elements
 * in the property, and defines atleast one property named <literal>"file"</literal>, which
 * is the #ThunarxFileInfo whose tags are displayed in the property page. For example, the
 * <filename>tag-page.h</filename> header file would look like this (this is really just
 * an example of the suggested way to implement property pages, you may of course choose
 * a different way)
 */

/* Property identifiers */
enum
{
  PROP_0,
  PROP_LABEL,
  PROP_LABEL_WIDGET,
};



static void thunarx_property_page_get_property  (GObject                  *object,
                                                 guint                     prop_id,
                                                 GValue                   *value,
                                                 GParamSpec               *pspec);
static void thunarx_property_page_set_property  (GObject                  *object,
                                                 guint                     prop_id,
                                                 const GValue             *value,
                                                 GParamSpec               *pspec);
static void thunarx_property_page_destroy       (GtkWidget                *object);



struct _ThunarxPropertyPagePrivate
{
  GtkWidget *label_widget;
};



G_DEFINE_TYPE_WITH_PRIVATE (ThunarxPropertyPage, thunarx_property_page, GTK_TYPE_BIN)



static void
thunarx_property_page_class_init (ThunarxPropertyPageClass *klass)
{
  GtkWidgetClass *gtkwidget_class;
  GObjectClass   *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->get_property = thunarx_property_page_get_property;
  gobject_class->set_property = thunarx_property_page_set_property;

  gtkwidget_class = GTK_WIDGET_CLASS (klass);
  gtkwidget_class->destroy = thunarx_property_page_destroy;

  /**
   * ThunarxPropertyPage::label:
   *
   * Text of the page's label.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
                                                        _("Label"),
                                                        _("Text of the page's label"),
                                                        NULL,
                                                        G_PARAM_READWRITE));

  /**
   * ThunarxPropertyPage::label-widget:
   *
   * A widget to display in place of the usual page label.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_LABEL_WIDGET,
                                   g_param_spec_object ("label-widget",
                                                        _("Label widget"),
                                                        _("A widget to display in place of the usual page label"),
                                                        GTK_TYPE_WIDGET,
                                                        G_PARAM_READWRITE));
}



static void
thunarx_property_page_init (ThunarxPropertyPage *property_page)
{
  property_page->priv = thunarx_property_page_get_instance_private (property_page);
}



static void
thunarx_property_page_get_property (GObject    *object,
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  ThunarxPropertyPage *property_page = THUNARX_PROPERTY_PAGE (object);

  switch (prop_id)
    {
    case PROP_LABEL:
      g_value_set_string (value, thunarx_property_page_get_label (property_page));
      break;

    case PROP_LABEL_WIDGET:
      g_value_set_object (value, thunarx_property_page_get_label_widget (property_page));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
thunarx_property_page_set_property (GObject      *object,
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
  ThunarxPropertyPage *property_page = THUNARX_PROPERTY_PAGE (object);

  switch (prop_id)
    {
    case PROP_LABEL:
      thunarx_property_page_set_label (property_page, g_value_get_string (value));
      break;

    case PROP_LABEL_WIDGET:
      thunarx_property_page_set_label_widget (property_page, g_value_get_object (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
thunarx_property_page_destroy (GtkWidget *object)
{
  ThunarxPropertyPage *property_page = THUNARX_PROPERTY_PAGE (object);

  /* destroy the label widget (if any) */
  if (G_LIKELY (property_page->priv->label_widget != NULL))
    {
      gtk_widget_destroy (GTK_WIDGET (property_page->priv->label_widget));
      g_object_unref (G_OBJECT (property_page->priv->label_widget));
      property_page->priv->label_widget = NULL;
    }

  (*GTK_WIDGET_CLASS (thunarx_property_page_parent_class)->destroy) (object);
}



/**
 * thunarx_property_page_new:
 * @label : the text to use as the label of the page.
 *
 * Allocates a new #ThunarxPropertyPage widget and sets its label to the
 * specified @label. If @label is %NULL, the label is omitted.
 *
 * Return value: the newly allocated #ThunarxPropertyPage
 *               widget.
 **/
GtkWidget*
thunarx_property_page_new (const gchar *label)
{
  return g_object_new (THUNARX_TYPE_PROPERTY_PAGE, "label", label, NULL);
}



/**
 * thunarx_property_page_new_with_label_widget:
 * @label_widget : a #GtkWidget, which should be used as label.
 *
 * Allocates a new #ThunarxPropertyPage widget and sets its label to
 * the specified @label_widget.
 *
 * Return value: the newly allocated #ThunarxPropertyPage widget.
 **/
GtkWidget*
thunarx_property_page_new_with_label_widget (GtkWidget *label_widget)
{
  return g_object_new (THUNARX_TYPE_PROPERTY_PAGE, "label-widget", label_widget, NULL);
}



/**
 * thunarx_property_page_get_label:
 * @property_page : a #ThunarxPropertyPage.
 *
 * If the @property_page's label widget is a #GtkLabel, returns the text
 * in the label widget (the @property_page will have a #GtkLabel for the
 * label widget if a non-%NULL argument was passed to thunarx_property_page_new()).
 *
 * Return value: the text in the label or %NULL if there was no label widget or
 *               the label widget was not a #GtkLabel. The returned string is
 *               owned by the @property_page and must not be modified or freed.
 **/
const gchar*
thunarx_property_page_get_label (ThunarxPropertyPage *property_page)
{
  g_return_val_if_fail (THUNARX_IS_PROPERTY_PAGE (property_page), NULL);

  if (property_page->priv->label_widget != NULL && GTK_IS_LABEL (property_page->priv->label_widget))
    return gtk_label_get_text (GTK_LABEL (property_page->priv->label_widget));
  else
    return NULL;
}



/**
 * thunarx_property_page_set_label:
 * @property_page : a #ThunarxPropertyPage.
 * @label         : the text to use as the label of the page.
 *
 * Sets the text of the label. If @label is %NULL, the current label is
 * removed.
 **/
void
thunarx_property_page_set_label (ThunarxPropertyPage *property_page,
                                 const gchar         *label)
{
  GtkWidget *widget;

  g_return_if_fail (THUNARX_IS_PROPERTY_PAGE (property_page));

  if (label == NULL)
    {
      thunarx_property_page_set_label_widget (property_page, NULL);
    }
  else
    {
      widget = gtk_label_new (label);
      thunarx_property_page_set_label_widget (property_page, widget);
      gtk_widget_show (widget);
    }
}



/**
 * thunarx_property_page_get_label_widget:
 * @property_page : a #ThunarxPropertyPage.
 *
 * Returns the label widget for the @property_page. See
 * thunarx_property_page_set_label_widget().
 *
 * Returns: (transfer none): the label widget or %NULL if there is none.
 **/
GtkWidget*
thunarx_property_page_get_label_widget (ThunarxPropertyPage *property_page)
{
  g_return_val_if_fail (THUNARX_IS_PROPERTY_PAGE (property_page), NULL);
  return property_page->priv->label_widget;
}



/**
 * thunarx_property_page_set_label_widget:
 * @property_page : a #ThunarxPropertyPage.
 * @label_widget  : the new label widget.
 *
 * Sets the label widget for the @property_page. This is the widget
 * that will appear in the notebook header for the @property_page.
 **/
void
thunarx_property_page_set_label_widget (ThunarxPropertyPage *property_page,
                                        GtkWidget           *label_widget)
{
  g_return_if_fail (THUNARX_IS_PROPERTY_PAGE (property_page));
  g_return_if_fail (label_widget == NULL || (GTK_IS_WIDGET (label_widget) && gtk_widget_get_parent (label_widget) == NULL));

  if (G_UNLIKELY (label_widget == property_page->priv->label_widget))
    return;

  /* disconnect from the previous label widget */
  if (G_LIKELY (property_page->priv->label_widget != NULL))
    g_object_unref (G_OBJECT (property_page->priv->label_widget));

  /* activate the new label widget */
  property_page->priv->label_widget = label_widget;

  /* connect to the new label widget */
  if (G_LIKELY (label_widget != NULL))
    g_object_ref_sink (G_OBJECT (label_widget));

  /* notify listeners */
  g_object_freeze_notify (G_OBJECT (property_page));
  g_object_notify (G_OBJECT (property_page), "label");
  g_object_notify (G_OBJECT (property_page), "label-widget");
  g_object_thaw_notify (G_OBJECT (property_page));
}