summaryrefslogtreecommitdiff
path: root/thunar/thunar-statusbar.c
blob: 9eb8aebd6e0ad480def6a298798fcb557c24d636 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2005-2006 Benedikt Meurer <benny@xfce.org>
 * Copyright (c) 2011 Jannis Pohlmann <jannis@xfce.org>
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include <exo/exo.h>
#include <libxfce4ui/libxfce4ui.h>

#include <thunar/thunar-gobject-extensions.h>
#include <thunar/thunar-private.h>
#include <thunar/thunar-statusbar.h>
#include <thunar/thunar-gtk-extensions.h>
#include <thunar/thunar-preferences.h>
#include <thunar/thunar-application.h>
#include <thunar/thunar-window.h>



enum
{
  PROP_0,
  PROP_TEXT,
};



static void         thunar_statusbar_finalize                 (GObject              *object);
static void         thunar_statusbar_set_property             (GObject              *object,
                                                               guint                 prop_id,
                                                               const GValue         *value,
                                                               GParamSpec           *pspec);
static void         thunar_statusbar_set_text                 (ThunarStatusbar      *statusbar,
                                                               const gchar          *text);
static GtkWidget*   thunar_statusbar_context_menu             (ThunarStatusbar      *statusbar);
static gboolean     thunar_statusbar_button_press_event       (GtkWidget            *widget,
                                                               GdkEventButton       *event,
                                                               ThunarStatusbar      *statusbar);
static gboolean     thunar_statusbar_action_show_size         (ThunarStatusbar      *statusbar);
static gboolean     thunar_statusbar_action_show_size_bytes   (ThunarStatusbar      *statusbar);
static gboolean     thunar_statusbar_action_show_filetype     (ThunarStatusbar      *statusbar);
static gboolean     thunar_statusbar_action_show_display_name (ThunarStatusbar      *statusbar);
static void         thunar_statusbar_update_all               (void);


struct _ThunarStatusbarClass
{
  GtkStatusbarClass __parent__;
};

struct _ThunarStatusbar
{
  GtkStatusbar       __parent__;
  guint              context_id;

  ThunarPreferences *preferences;
};

static XfceGtkActionEntry thunar_status_bar_action_entries[] =
    {
        { THUNAR_STATUS_BAR_ACTION_TOGGLE_SIZE,           "<Actions>/ThunarStatusBar/toggle-size",            "", XFCE_GTK_CHECK_MENU_ITEM,       N_ ("Size"),          N_ ("Show size"),               NULL, G_CALLBACK (thunar_statusbar_action_show_size),            },
        { THUNAR_STATUS_BAR_ACTION_TOGGLE_SIZE_IN_BYTES,  "<Actions>/ThunarStatusBar/toggle-size-in-bytes",   "", XFCE_GTK_CHECK_MENU_ITEM,       N_ ("Size in bytes"), N_ ("Show size in bytes"),      NULL, G_CALLBACK (thunar_statusbar_action_show_size_bytes),      },
        { THUNAR_STATUS_BAR_ACTION_TOGGLE_FILETYPE,       "<Actions>/ThunarStatusBar/toggle-filetype",        "", XFCE_GTK_CHECK_MENU_ITEM,       N_ ("Filetype"),      N_ ("Show filetype"),           NULL, G_CALLBACK (thunar_statusbar_action_show_filetype),        },
        { THUNAR_STATUS_BAR_ACTION_TOGGLE_DISPLAY_NAME,   "<Actions>/ThunarStatusBar/toggle-display-name",    "", XFCE_GTK_CHECK_MENU_ITEM,       N_ ("Display Name"),  N_ ("Show display name"),       NULL, G_CALLBACK (thunar_statusbar_action_show_display_name),    },
    };

#define get_action_entry(id) xfce_gtk_get_action_entry_by_id (thunar_status_bar_action_entries, G_N_ELEMENTS (thunar_status_bar_action_entries) ,id)


G_DEFINE_TYPE (ThunarStatusbar, thunar_statusbar, GTK_TYPE_STATUSBAR)



static void
thunar_statusbar_class_init (ThunarStatusbarClass *klass)
{
  static gboolean style_initialized = FALSE;

  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->set_property = thunar_statusbar_set_property;
  gobject_class->finalize = thunar_statusbar_finalize;

  /**
   * ThunarStatusbar:text:
   *
   * The main text to be displayed in the statusbar. This property
   * can only be written.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_TEXT,
                                   g_param_spec_string ("text",
                                                        "text",
                                                        "text",
                                                        NULL,
                                                        EXO_PARAM_WRITABLE));

  if (!style_initialized)
    {
        gtk_widget_class_install_style_property (GTK_WIDGET_CLASS (gobject_class),
          g_param_spec_enum (
            "shadow-type",                //name
            "shadow-type",                //nick
            "type of shadow",             //blurb
            gtk_shadow_type_get_type(),   //type
            GTK_SHADOW_NONE,              //default
            G_PARAM_READWRITE ));         //flags
    }
}



static void
thunar_statusbar_init (ThunarStatusbar *statusbar)
{
  statusbar->context_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "Main text");

  /* make the status thinner */
  gtk_widget_set_margin_top (GTK_WIDGET (statusbar), 0);
  gtk_widget_set_margin_bottom (GTK_WIDGET (statusbar), 0);

  statusbar->preferences = thunar_preferences_get ();
}



static void
thunar_statusbar_finalize (GObject *object)
{
  ThunarStatusbar *statusbar = THUNAR_STATUSBAR (object);

  g_object_unref (statusbar->preferences);
}



/**
 * thunar_statusbar_setup_event:
 * @statusbar : a #ThunarStatusbar instance.
 * @event_box    : a #GtkWidget instance (actually a GtkEventBox passed as a GtkWidget).
 *
 * Sets the statusbar's GtkEventBox and uses it to receive signals.
 **/
void
thunar_statusbar_setup_event (ThunarStatusbar *statusbar,
                              GtkWidget       *event_box)
{
  g_signal_connect (G_OBJECT (event_box), "button-press-event", G_CALLBACK (thunar_statusbar_button_press_event), G_OBJECT (statusbar));
}



static gboolean
thunar_statusbar_button_press_event (GtkWidget        *widget,
                                     GdkEventButton   *event,
                                     ThunarStatusbar  *statusbar)
{
  if (event->type == GDK_BUTTON_PRESS && event->button == 3)
    {
      thunar_gtk_menu_run (GTK_MENU (thunar_statusbar_context_menu (statusbar)));
      return TRUE;
    }

  return FALSE;
}



static void
thunar_statusbar_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  ThunarStatusbar *statusbar = THUNAR_STATUSBAR (object);

  switch (prop_id)
    {
    case PROP_TEXT:
      thunar_statusbar_set_text (statusbar, g_value_get_string (value));
      break;

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



/**
 * thunar_statusbar_set_text:
 * @statusbar : a #ThunarStatusbar instance.
 * @text      : the main text to be displayed in @statusbar.
 *
 * Sets up a new main text for @statusbar.
 **/
static void
thunar_statusbar_set_text (ThunarStatusbar *statusbar,
                           const gchar     *text)
{
  _thunar_return_if_fail (THUNAR_IS_STATUSBAR (statusbar));
  _thunar_return_if_fail (text != NULL);

  gtk_statusbar_pop (GTK_STATUSBAR (statusbar), statusbar->context_id);
  gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar->context_id, text);
}



static GtkWidget*
thunar_statusbar_context_menu (ThunarStatusbar *statusbar)
{
  GtkWidget *context_menu = gtk_menu_new();
  GtkWidget *widget;
  guint      active;
  gboolean   show_size, show_size_in_bytes, show_filetype, show_display_name;

  g_object_get (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", &active, NULL);
  show_size = active & THUNAR_STATUS_BAR_INFO_SIZE;
  show_size_in_bytes = active & THUNAR_STATUS_BAR_INFO_SIZE_IN_BYTES;
  show_filetype = active & THUNAR_STATUS_BAR_INFO_FILETYPE;
  show_display_name = active & THUNAR_STATUS_BAR_INFO_DISPLAY_NAME;

  xfce_gtk_toggle_menu_item_new_from_action_entry (get_action_entry (THUNAR_STATUS_BAR_ACTION_TOGGLE_SIZE), G_OBJECT (statusbar), show_size, GTK_MENU_SHELL (context_menu));
  widget = xfce_gtk_toggle_menu_item_new_from_action_entry (get_action_entry (THUNAR_STATUS_BAR_ACTION_TOGGLE_SIZE_IN_BYTES), G_OBJECT (statusbar), show_size_in_bytes, GTK_MENU_SHELL (context_menu));
  if (show_size == FALSE)
    gtk_widget_set_sensitive (widget, FALSE);
  xfce_gtk_toggle_menu_item_new_from_action_entry (get_action_entry (THUNAR_STATUS_BAR_ACTION_TOGGLE_FILETYPE), G_OBJECT (statusbar), show_filetype, GTK_MENU_SHELL (context_menu));
  xfce_gtk_toggle_menu_item_new_from_action_entry (get_action_entry (THUNAR_STATUS_BAR_ACTION_TOGGLE_DISPLAY_NAME), G_OBJECT (statusbar), show_display_name, GTK_MENU_SHELL (context_menu));

  gtk_widget_show_all (GTK_WIDGET (context_menu));

  return context_menu;
}



static gboolean
thunar_statusbar_action_show_size (ThunarStatusbar *statusbar)
{
  guint active;

  g_object_get (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", &active, NULL);
  g_object_set (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", thunar_status_bar_info_toggle_bit (active, THUNAR_STATUS_BAR_INFO_SIZE), NULL);
  thunar_statusbar_update_all ();

  /* required in case of shortcut activation, in order to signal that the accel key got handled */
  return TRUE;
}



static gboolean
thunar_statusbar_action_show_size_bytes (ThunarStatusbar *statusbar)
{
  guint active;

  g_object_get (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", &active, NULL);
  g_object_set (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", thunar_status_bar_info_toggle_bit (active, THUNAR_STATUS_BAR_INFO_SIZE_IN_BYTES), NULL);
  thunar_statusbar_update_all ();

  /* required in case of shortcut activation, in order to signal that the accel key got handled */
  return TRUE;
}



static gboolean
thunar_statusbar_action_show_filetype (ThunarStatusbar *statusbar)
{
  guint active;

  g_object_get (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", &active, NULL);
  g_object_set (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", thunar_status_bar_info_toggle_bit (active, THUNAR_STATUS_BAR_INFO_FILETYPE), NULL);
  thunar_statusbar_update_all ();

  /* required in case of shortcut activation, in order to signal that the accel key got handled */
  return TRUE;
}



static gboolean
thunar_statusbar_action_show_display_name (ThunarStatusbar *statusbar)
{
  guint active;

  g_object_get (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", &active, NULL);
  g_object_set (G_OBJECT (statusbar->preferences), "misc-status-bar-active-info", thunar_status_bar_info_toggle_bit (active, THUNAR_STATUS_BAR_INFO_DISPLAY_NAME), NULL);
  thunar_statusbar_update_all ();

  /* required in case of shortcut activation, in order to signal that the accel key got handled */
  return TRUE;
}



static void
thunar_statusbar_update_all (void)
{
  ThunarApplication *app     = thunar_application_get ();
  GList             *windows = thunar_application_get_windows (app);
  GList             *lp;

  for (lp = windows; lp != NULL; lp = lp->next)
    thunar_window_update_statusbar (THUNAR_WINDOW (lp->data));

  g_list_free (windows);
  g_object_unref (app);
}



/**
 * thunar_statusbar_new:
 *
 * Allocates a new #ThunarStatusbar instance with no
 * text set.
 *
 * Return value: the newly allocated #ThunarStatusbar instance.
 **/
GtkWidget*
thunar_statusbar_new (void)
{
  return g_object_new (THUNAR_TYPE_STATUSBAR, NULL);
}



XfceGtkActionEntry*
thunar_statusbar_get_action_entries (void)
{
  return thunar_status_bar_action_entries;
}



/**
 * thunar_statusbar_append_accelerators:
 * @statusbar    : a #ThunarStatusbar.
 * @accel_group : a #GtkAccelGroup to be used used for new menu items
 *
 * Connects all accelerators and corresponding default keys of this widget to the global accelerator list
 **/
void thunar_statusbar_append_accelerators (ThunarStatusbar *statusbar,
                                           GtkAccelGroup   *accel_group)
{
  _thunar_return_if_fail (THUNAR_IS_STATUSBAR (statusbar));

  xfce_gtk_accel_map_add_entries (thunar_status_bar_action_entries, G_N_ELEMENTS (thunar_status_bar_action_entries));
  xfce_gtk_accel_group_connect_action_entries (accel_group,
                                               thunar_status_bar_action_entries,
                                               G_N_ELEMENTS (thunar_status_bar_action_entries),
                                               statusbar);
}