2
* This file is part of libtile.
4
* Copyright (c) 2006 Novell, Inc.
6
* Libtile is free software; you can redistribute it and/or modify it under the
7
* terms of the GNU Lesser General Public License as published by the Free
8
* Software Foundation; either version 2 of the License, or (at your option)
11
* Libtile is distributed in the hope that it will be useful, but WITHOUT ANY
12
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with libslab; if not, write to the Free Software Foundation, Inc., 51
18
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <gdk/gdkkeysyms.h>
26
#include "double-click-detector.h"
28
G_DEFINE_TYPE (Tile, tile, GTK_TYPE_BUTTON)
32
DoubleClickDetector *double_click_detector;
37
static void tile_finalize (GObject *);
38
static void tile_get_property (GObject *, guint, GValue *, GParamSpec *);
39
static void tile_set_property (GObject *, guint, const GValue *, GParamSpec *);
40
static GObject *tile_constructor (GType, guint, GObjectConstructParam *);
42
static void tile_setup (Tile *);
44
static void tile_enter (GtkButton * widget);
45
static void tile_leave (GtkButton * widget);
46
static void tile_clicked (GtkButton *widget);
48
static gboolean tile_focus_in (GtkWidget *, GdkEventFocus *);
49
static gboolean tile_focus_out (GtkWidget *, GdkEventFocus *);
50
static gboolean tile_expose (GtkWidget *, GdkEventExpose *);
51
static gboolean tile_button_release (GtkWidget *, GdkEventButton *);
52
static gboolean tile_key_release (GtkWidget *, GdkEventKey *);
53
static gboolean tile_popup_menu (GtkWidget *);
55
static void tile_popup_menu_position (GtkMenu *, gint *, gint *, gboolean *, gpointer);
57
static void tile_drag_begin (GtkWidget *, GdkDragContext *);
58
static void tile_drag_data_get (GtkWidget *, GdkDragContext *, GtkSelectionData *, guint,
61
static void tile_emit_resource_event (Tile *, TileEventType, guint32);
63
static void tile_tile_action_triggered (Tile *, TileEvent *, TileAction *);
64
static void tile_action_triggered_event_marshal (GClosure *, GValue *, guint, const GValue *,
67
typedef void (*marshal_func_VOID__POINTER_POINTER) (gpointer, gpointer, gpointer, gpointer);
69
#define TILE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TILE_TYPE, TilePrivate))
73
TILE_ACTIVATED_SIGNAL,
74
TILE_IMPLICIT_ENABLE_SIGNAL,
75
TILE_IMPLICIT_DISABLE_SIGNAL,
76
TILE_ACTION_TRIGGERED_SIGNAL,
80
static guint tile_signals[LAST_SIGNAL] = { 0 };
86
PROP_TILE_CONTEXT_MENU,
91
tile_class_init (TileClass * this_class)
93
GObjectClass *g_obj_class = G_OBJECT_CLASS (this_class);
94
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (this_class);
95
GtkButtonClass *button_class = GTK_BUTTON_CLASS (this_class);
97
g_obj_class->constructor = tile_constructor;
98
g_obj_class->get_property = tile_get_property;
99
g_obj_class->set_property = tile_set_property;
100
g_obj_class->finalize = tile_finalize;
102
widget_class->focus_in_event = tile_focus_in;
103
widget_class->focus_out_event = tile_focus_out;
104
widget_class->expose_event = tile_expose;
105
widget_class->button_release_event = tile_button_release;
106
widget_class->key_release_event = tile_key_release;
107
widget_class->drag_begin = tile_drag_begin;
108
widget_class->drag_data_get = tile_drag_data_get;
109
widget_class->popup_menu = tile_popup_menu;
111
button_class->enter = tile_enter;
112
button_class->leave = tile_leave;
113
button_class->clicked = tile_clicked;
115
this_class->tile_explicit_enable = NULL;
116
this_class->tile_explicit_disable = NULL;
117
this_class->tile_activated = NULL;
118
this_class->tile_implicit_enable = NULL;
119
this_class->tile_implicit_disable = NULL;
120
this_class->tile_action_triggered = tile_tile_action_triggered;
122
g_type_class_add_private (this_class, sizeof (TilePrivate));
124
g_object_class_install_property (g_obj_class, PROP_TILE_URI,
125
g_param_spec_string ("tile-uri", "tile-uri", "the uri of the tile", NULL,
126
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
128
g_object_class_install_property (g_obj_class, PROP_TILE_CONTEXT_MENU,
129
g_param_spec_object ("context-menu", "context-menu",
130
"the context menu for the tile", GTK_TYPE_MENU, G_PARAM_READWRITE));
132
tile_signals[TILE_ACTIVATED_SIGNAL] = g_signal_new ("tile-activated",
133
G_TYPE_FROM_CLASS (this_class),
134
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
135
G_STRUCT_OFFSET (TileClass, tile_activated),
136
NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
138
tile_signals[TILE_IMPLICIT_ENABLE_SIGNAL] = g_signal_new ("tile-implicit-enable",
139
G_TYPE_FROM_CLASS (this_class),
140
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
141
G_STRUCT_OFFSET (TileClass, tile_implicit_enable),
142
NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
144
tile_signals[TILE_IMPLICIT_DISABLE_SIGNAL] = g_signal_new ("tile-implicit-disable",
145
G_TYPE_FROM_CLASS (this_class),
146
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
147
G_STRUCT_OFFSET (TileClass, tile_implicit_disable),
148
NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
150
tile_signals[TILE_ACTION_TRIGGERED_SIGNAL] = g_signal_new ("tile-action-triggered",
151
G_TYPE_FROM_CLASS (this_class),
152
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
153
G_STRUCT_OFFSET (TileClass, tile_action_triggered),
154
NULL, NULL, tile_action_triggered_event_marshal, G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
158
tile_constructor (GType type, guint n_param, GObjectConstructParam * param)
163
g_obj = (*G_OBJECT_CLASS (tile_parent_class)->constructor) (type, n_param, param);
165
priv = TILE_GET_PRIVATE (g_obj);
166
priv->double_click_detector = double_click_detector_new ();
168
tile_setup (TILE (g_obj));
174
tile_init (Tile * tile)
176
TilePrivate *priv = TILE_GET_PRIVATE (tile);
179
tile->context_menu = NULL;
180
tile->entered = FALSE;
181
tile->enabled = TRUE;
183
tile->actions = NULL;
186
tile->default_action = NULL;
188
priv->double_click_detector = NULL;
189
priv->is_dragging = FALSE;
193
tile_finalize (GObject * g_object)
195
Tile *tile = TILE (g_object);
196
TilePrivate *priv = TILE_GET_PRIVATE (g_object);
198
if (tile->n_actions) /* this will also free "default_action" entry */
201
for (x = 0; x < tile->n_actions; x++)
203
if (tile->actions[x])
204
g_object_unref (tile->actions[x]);
206
g_free (tile->actions);
211
if (tile->context_menu)
212
gtk_widget_destroy (GTK_WIDGET (tile->context_menu));
214
g_object_unref (priv->double_click_detector);
216
(*G_OBJECT_CLASS (tile_parent_class)->finalize) (g_object);
220
tile_get_property (GObject * g_obj, guint prop_id, GValue * value, GParamSpec * param_spec)
222
if (!IS_TILE (g_obj))
228
g_value_set_string (value, TILE (g_obj)->uri);
231
case PROP_TILE_CONTEXT_MENU:
232
g_value_set_object (value, TILE (g_obj)->context_menu);
241
tile_set_property (GObject * g_obj, guint prop_id, const GValue * value, GParamSpec * param_spec)
246
if (!IS_TILE (g_obj))
254
tile->uri = g_strdup (g_value_get_string (value));
257
case PROP_TILE_CONTEXT_MENU:
258
menu = g_value_get_object (value);
260
if (menu == tile->context_menu)
263
if (tile->context_menu)
264
gtk_menu_detach (tile->context_menu);
266
tile->context_menu = menu;
268
if (tile->context_menu)
269
gtk_menu_attach_to_widget (tile->context_menu, GTK_WIDGET (tile), NULL);
279
tile_setup (Tile * tile)
281
gtk_button_set_relief (GTK_BUTTON (tile), GTK_RELIEF_NONE);
285
gtk_drag_source_set (GTK_WIDGET (tile), GDK_BUTTON1_MASK, NULL, 0,
286
GDK_ACTION_COPY | GDK_ACTION_MOVE);
288
gtk_drag_source_add_uri_targets (GTK_WIDGET (tile));
293
tile_enter (GtkButton * widget)
295
gtk_widget_set_state (GTK_WIDGET (widget), TILE_STATE_ENTERED);
297
TILE (widget)->entered = TRUE;
301
tile_leave (GtkButton * widget)
303
if (GTK_WIDGET_HAS_FOCUS (widget))
304
gtk_widget_set_state (GTK_WIDGET (widget), TILE_STATE_FOCUSED);
306
gtk_widget_set_state (GTK_WIDGET (widget), GTK_STATE_NORMAL);
308
TILE (widget)->entered = FALSE;
312
tile_clicked (GtkButton * widget)
314
TileEvent *tile_event;
316
tile_event = g_new0 (TileEvent, 1);
317
tile_event->type = TILE_EVENT_ACTIVATED_DOUBLE_CLICK;
318
tile_event->time = gtk_get_current_event_time ();
320
g_signal_emit (widget, tile_signals[TILE_ACTIVATED_SIGNAL], 0, tile_event);
322
gtk_button_released (widget);
327
tile_focus_in (GtkWidget * widget, GdkEventFocus * event)
329
gtk_widget_set_state (widget, TILE_STATE_FOCUSED);
335
tile_focus_out (GtkWidget * widget, GdkEventFocus * event)
337
if (TILE (widget)->entered)
338
gtk_widget_set_state (widget, TILE_STATE_ENTERED);
340
gtk_widget_set_state (widget, GTK_STATE_NORMAL);
346
tile_expose (GtkWidget * widget, GdkEventExpose * event)
348
/* FIXME: there ought to be a better way to prevent the focus from being rendered. */
353
if ((has_focus = GTK_WIDGET_HAS_FOCUS (widget)))
354
GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
356
retval = (*GTK_WIDGET_CLASS (tile_parent_class)->expose_event) (widget, event);
359
GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
365
tile_button_release (GtkWidget * widget, GdkEventButton * event)
367
Tile *tile = TILE (widget);
368
TilePrivate *priv = TILE_GET_PRIVATE (tile);
370
TileEvent *tile_event;
372
if (priv->is_dragging)
374
priv->is_dragging = FALSE;
379
switch (event->button)
382
tile_event = g_new0 (TileEvent, 1);
383
tile_event->time = event->time;
385
if (double_click_detector_is_double_click (priv->double_click_detector, event->time,
387
tile_event->type = TILE_EVENT_ACTIVATED_DOUBLE_CLICK;
389
tile_event->type = TILE_EVENT_ACTIVATED_SINGLE_CLICK;
391
g_signal_emit (tile, tile_signals[TILE_ACTIVATED_SIGNAL], 0, tile_event);
393
gtk_button_released (GTK_BUTTON (widget));
399
if (GTK_IS_MENU (tile->context_menu))
400
gtk_menu_popup (tile->context_menu, NULL, NULL, NULL, NULL, event->button,
413
tile_key_release (GtkWidget * widget, GdkEventKey * event)
415
TileEvent *tile_event;
417
if (event->keyval == GDK_Return)
419
tile_event = g_new0 (TileEvent, 1);
420
tile_event->type = TILE_EVENT_ACTIVATED_KEYBOARD;
421
tile_event->time = event->time;
423
g_signal_emit (widget, tile_signals[TILE_ACTIVATED_SIGNAL], 0, tile_event);
432
tile_popup_menu_position (GtkMenu * menu, gint * x, gint * y, gboolean * push_in, gpointer data)
434
Tile *tile = TILE (data);
439
if (!GTK_WIDGET_REALIZED (tile))
442
gtk_widget_size_request (GTK_WIDGET (menu), &req);
444
top = gtk_widget_get_toplevel (GTK_WIDGET (tile));
446
gdk_window_get_origin (top->window, x, y);
448
*x += (top->allocation.width / 2) - (req.width / 2);
449
*y += (top->allocation.height / 2) - (req.height / 2);
455
tile_popup_menu (GtkWidget * widget)
457
Tile *tile = TILE (widget);
459
if (GTK_IS_MENU (tile->context_menu))
461
gtk_menu_popup (tile->context_menu, NULL, NULL, tile_popup_menu_position, tile, 0,
462
gtk_get_current_event_time ());
472
tile_drag_begin (GtkWidget * widget, GdkDragContext * context)
474
TILE_GET_PRIVATE (widget)->is_dragging = TRUE;
478
tile_drag_data_get (GtkWidget * widget, GdkDragContext * context, GtkSelectionData * data,
479
guint info, guint time)
483
if (TILE (widget)->uri)
485
uris[0] = TILE (widget)->uri;
488
gtk_selection_data_set_uris (data, uris);
493
tile_tile_action_triggered (Tile * tile, TileEvent * event, TileAction * action)
495
if (action && action->func)
496
(*action->func) (tile, event, action);
500
tile_compare (gconstpointer a, gconstpointer b)
502
if (IS_TILE (a) && IS_TILE (b))
503
return strcmp (TILE (a)->uri, TILE (b)->uri);
509
tile_explicit_enable (Tile * this)
511
TileClass *this_class = TILE_GET_CLASS (this);
513
if (this_class->tile_explicit_enable)
514
(*this_class->tile_explicit_enable) (this);
518
tile_explicit_disable (Tile * this)
520
TileClass *this_class = TILE_GET_CLASS (this);
522
if (this_class->tile_explicit_disable)
523
(*this_class->tile_explicit_disable) (this);
527
tile_implicit_enable (Tile * tile)
529
tile_implicit_enable_with_time (tile, GDK_CURRENT_TIME);
533
tile_implicit_disable (Tile * tile)
535
tile_implicit_disable_with_time (tile, GDK_CURRENT_TIME);
539
tile_implicit_enable_with_time (Tile * tile, guint32 time)
541
tile_emit_resource_event (tile, TILE_EVENT_IMPLICIT_ENABLE, time);
545
tile_implicit_disable_with_time (Tile * tile, guint32 time)
547
tile_emit_resource_event (tile, TILE_EVENT_IMPLICIT_DISABLE, time);
551
tile_emit_resource_event (Tile * tile, TileEventType type, guint32 time)
556
event = g_new0 (TileEvent, 1);
560
if (type == TILE_EVENT_IMPLICIT_ENABLE)
562
signal_id = tile_signals[TILE_IMPLICIT_ENABLE_SIGNAL];
563
tile->enabled = TRUE;
567
signal_id = tile_signals[TILE_IMPLICIT_DISABLE_SIGNAL];
568
tile->enabled = FALSE;
571
g_signal_emit (tile, signal_id, 0, event);
576
tile_trigger_action (Tile * tile, TileAction * action)
578
tile_trigger_action_with_time (tile, action, GDK_CURRENT_TIME);
582
tile_trigger_action_with_time (Tile * tile, TileAction * action, guint32 time)
584
TileEvent *event = g_new0 (TileEvent, 1);
586
event->type = TILE_EVENT_ACTION_TRIGGERED;
589
g_signal_emit (tile, tile_signals[TILE_ACTION_TRIGGERED_SIGNAL], 0, event, action);
594
tile_action_triggered_event_marshal (GClosure * closure, GValue * retval, guint n_param,
595
const GValue * param, gpointer invocation_hint, gpointer marshal_data)
597
marshal_func_VOID__POINTER_POINTER callback;
598
GCClosure *cc = (GCClosure *) closure;
599
gpointer data_0, data_1;
601
g_return_if_fail (n_param == 3);
603
if (G_CCLOSURE_SWAP_DATA (closure))
605
data_0 = closure->data;
606
data_1 = g_value_peek_pointer (param);
610
data_0 = g_value_peek_pointer (param);
611
data_1 = closure->data;
615
callback = (marshal_func_VOID__POINTER_POINTER) marshal_data;
617
callback = (marshal_func_VOID__POINTER_POINTER) cc->callback;
619
callback (data_0, g_value_peek_pointer (param + 1), g_value_peek_pointer (param + 2),