summaryrefslogtreecommitdiff
path: root/libslab/tile.c
blob: 27175334d14da59c4e8512d7b2cb7ad15015aaf2 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * This file is part of libtile.
 *
 * Copyright (c) 2006 Novell, Inc.
 *
 * Libtile is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Libtile 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 Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libslab; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "tile.h"

#include <gdk/gdkkeysyms.h>
#include <string.h>

#include "double-click-detector.h"

G_DEFINE_TYPE (Tile, tile, GTK_TYPE_BUTTON)

typedef struct
{
	DoubleClickDetector *double_click_detector;
	
	gboolean is_dragging;
} TilePrivate;

static void tile_finalize (GObject *);
static void tile_dispose (GObject *);
static void tile_get_property (GObject *, guint, GValue *, GParamSpec *);
static void tile_set_property (GObject *, guint, const GValue *, GParamSpec *);
static GObject *tile_constructor (GType, guint, GObjectConstructParam *);

static void tile_setup (Tile *);

static void tile_enter (GtkButton * widget);
static void tile_leave (GtkButton * widget);
static void tile_clicked (GtkButton *widget);

static gboolean tile_focus_in (GtkWidget *, GdkEventFocus *);
static gboolean tile_focus_out (GtkWidget *, GdkEventFocus *);
static gboolean tile_draw (GtkWidget *, cairo_t *);
static gboolean tile_button_release (GtkWidget *, GdkEventButton *);
static gboolean tile_key_release (GtkWidget *, GdkEventKey *);
static gboolean tile_popup_menu (GtkWidget *);

static void tile_popup_menu_position (GtkMenu *, gint *, gint *, gboolean *, gpointer);

static void tile_drag_begin (GtkWidget *, GdkDragContext *);
static void tile_drag_data_get (GtkWidget *, GdkDragContext *, GtkSelectionData *, guint,
guint);

static void tile_tile_action_triggered (Tile *, TileEvent *, TileAction *);
static void tile_action_triggered_event_marshal (GClosure *, GValue *, guint, const GValue *,
gpointer, gpointer);

typedef void (*marshal_func_VOID__POINTER_POINTER) (gpointer, gpointer, gpointer, gpointer);

#define TILE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TILE_TYPE, TilePrivate))

enum
{
	TILE_ACTIVATED_SIGNAL,
	TILE_ACTION_TRIGGERED_SIGNAL,
	LAST_SIGNAL
};

static guint tile_signals[LAST_SIGNAL] = { 0 };

enum
{
	PROP_0,
	PROP_TILE_URI,
	PROP_TILE_CONTEXT_MENU,
	PROP_TILE_ACTIONS
};

static void
tile_class_init (TileClass * this_class)
{
	GObjectClass *g_obj_class = G_OBJECT_CLASS (this_class);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (this_class);
	GtkButtonClass *button_class = GTK_BUTTON_CLASS (this_class);

	g_obj_class->constructor = tile_constructor;
	g_obj_class->get_property = tile_get_property;
	g_obj_class->set_property = tile_set_property;
	g_obj_class->finalize = tile_finalize;
	g_obj_class->dispose = tile_dispose;

	widget_class->focus_in_event = tile_focus_in;
	widget_class->focus_out_event = tile_focus_out;
	widget_class->draw = tile_draw;
	widget_class->button_release_event = tile_button_release;
	widget_class->key_release_event = tile_key_release;
	widget_class->drag_begin = tile_drag_begin;
	widget_class->drag_data_get = tile_drag_data_get;
	widget_class->popup_menu = tile_popup_menu;

	button_class->enter = tile_enter;
	button_class->leave = tile_leave;
	button_class->clicked = tile_clicked;

	this_class->tile_activated = NULL;
	this_class->tile_action_triggered = tile_tile_action_triggered;

	g_type_class_add_private (this_class, sizeof (TilePrivate));

	g_object_class_install_property (g_obj_class, PROP_TILE_URI,
		g_param_spec_string ("tile-uri", "tile-uri", "the uri of the tile", NULL,
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

	g_object_class_install_property (g_obj_class, PROP_TILE_CONTEXT_MENU,
		g_param_spec_object ("context-menu", "context-menu",
			"the context menu for the tile", GTK_TYPE_MENU, G_PARAM_READWRITE));

	tile_signals[TILE_ACTIVATED_SIGNAL] = g_signal_new ("tile-activated",
		G_TYPE_FROM_CLASS (this_class),
		G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
		G_STRUCT_OFFSET (TileClass, tile_activated),
		NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);

	tile_signals[TILE_ACTION_TRIGGERED_SIGNAL] = g_signal_new ("tile-action-triggered",
		G_TYPE_FROM_CLASS (this_class),
		G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
		G_STRUCT_OFFSET (TileClass, tile_action_triggered),
		NULL, NULL, tile_action_triggered_event_marshal, G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
}

static GObject *
tile_constructor (GType type, guint n_param, GObjectConstructParam * param)
{
	GObject *g_obj;
	TilePrivate *priv;

	g_obj = (*G_OBJECT_CLASS (tile_parent_class)->constructor) (type, n_param, param);

	priv = TILE_GET_PRIVATE (g_obj);
	priv->double_click_detector = double_click_detector_new ();

	tile_setup (TILE (g_obj));

	return g_obj;
}

static void
tile_init (Tile * tile)
{
	TilePrivate *priv = TILE_GET_PRIVATE (tile);

	tile->uri = NULL;
	tile->context_menu = NULL;
	tile->entered = FALSE;
	tile->enabled = TRUE;

	tile->actions = NULL;
	tile->n_actions = 0;

	tile->default_action = NULL;

	priv->double_click_detector = NULL;
	priv->is_dragging = FALSE;
}

static void
tile_finalize (GObject * g_object)
{
	Tile *tile = TILE (g_object);
	TilePrivate *priv = TILE_GET_PRIVATE (g_object);

	if (tile->n_actions)	/* this will also free "default_action" entry */
	{
		g_free (tile->actions);
	}

	if (tile->uri)
		g_free (tile->uri);

	g_object_unref (priv->double_click_detector);

	(*G_OBJECT_CLASS (tile_parent_class)->finalize) (g_object);
}

static void
tile_dispose (GObject * g_object)
{
	Tile *tile = TILE (g_object);

	/* free the TileAction object */
	if (tile->n_actions)
	{
		gint x;
		for (x = 0; x < tile->n_actions; x++)
		{
			if (tile->actions[x] != NULL) {
				g_object_unref (tile->actions[x]);
				tile->actions[x] = NULL;
			}
		}
	}

	/* free the GtkMenu object */
	if (tile->context_menu != NULL) {
		gtk_widget_destroy (GTK_WIDGET (tile->context_menu));
		tile->context_menu = NULL;
	}

	(*G_OBJECT_CLASS (tile_parent_class)->dispose) (g_object);
}

static void
tile_get_property (GObject * g_obj, guint prop_id, GValue * value, GParamSpec * param_spec)
{
	if (!IS_TILE (g_obj))
		return;

	switch (prop_id)
	{
	case PROP_TILE_URI:
		g_value_set_string (value, TILE (g_obj)->uri);
		break;

	case PROP_TILE_CONTEXT_MENU:
		g_value_set_object (value, TILE (g_obj)->context_menu);
		break;

	default:
		break;
	}
}

static void
tile_set_property (GObject * g_obj, guint prop_id, const GValue * value, GParamSpec * param_spec)
{
	Tile *tile;
	GtkMenu *menu;

	if (!IS_TILE (g_obj))
		return;

	tile = TILE (g_obj);

	switch (prop_id)
	{
	case PROP_TILE_URI:
		tile->uri = g_strdup (g_value_get_string (value));
		break;

	case PROP_TILE_CONTEXT_MENU:
		menu = g_value_get_object (value);

		if (menu == tile->context_menu)
			break;

		if (tile->context_menu)
			gtk_menu_detach (tile->context_menu);

		tile->context_menu = menu;

		if (tile->context_menu)
			gtk_menu_attach_to_widget (tile->context_menu, GTK_WIDGET (tile), NULL);

		break;

	default:
		break;
	}
}

static void
tile_setup (Tile * tile)
{
	gtk_button_set_relief (GTK_BUTTON (tile), GTK_RELIEF_NONE);

	if (tile->uri)
	{
		gtk_drag_source_set (GTK_WIDGET (tile), GDK_BUTTON1_MASK, NULL, 0,
			GDK_ACTION_COPY | GDK_ACTION_MOVE);

		gtk_drag_source_add_uri_targets (GTK_WIDGET (tile));
	}
}

static void
tile_enter (GtkButton * widget)
{
	gtk_widget_set_state_flags (GTK_WIDGET (widget), TILE_STATE_ENTERED, TRUE);

	TILE (widget)->entered = TRUE;
}

static void
tile_leave (GtkButton * widget)
{
	if (gtk_widget_has_focus (GTK_WIDGET (widget)))
		gtk_widget_set_state_flags (GTK_WIDGET (widget), TILE_STATE_FOCUSED, TRUE);
	else
		gtk_widget_set_state_flags (GTK_WIDGET (widget), GTK_STATE_FLAG_NORMAL, TRUE);

	TILE (widget)->entered = FALSE;
}

static void
tile_clicked (GtkButton * widget)
{
	TileEvent *tile_event;

	tile_event = g_new0 (TileEvent, 1);
	tile_event->type = TILE_EVENT_ACTIVATED_DOUBLE_CLICK;
	tile_event->time = gtk_get_current_event_time ();

	g_signal_emit (widget, tile_signals[TILE_ACTIVATED_SIGNAL], 0, tile_event);

	gtk_button_released (widget);
	g_free (tile_event);
}

static gboolean
tile_focus_in (GtkWidget * widget, GdkEventFocus * event)
{
	gtk_widget_set_state_flags (widget, TILE_STATE_FOCUSED, TRUE);

	return FALSE;
}

static gboolean
tile_focus_out (GtkWidget * widget, GdkEventFocus * event)
{
	if (TILE (widget)->entered)
		gtk_widget_set_state_flags (widget, TILE_STATE_ENTERED, TRUE);
	else
		gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_NORMAL, TRUE);

	return FALSE;
}

static gboolean
tile_draw (GtkWidget * widget, cairo_t * cr)
{
	/* FIXME: there ought to be a better way to prevent the focus from being rendered. */

	gboolean has_focus;
	gboolean retval;

	if ((has_focus = gtk_widget_has_focus (widget)))
		gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_FOCUSED);

	retval = (*GTK_WIDGET_CLASS (tile_parent_class)->draw) (widget, cr);

	if (has_focus)
		gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_FOCUSED, TRUE);

	return retval;
}

static gboolean
tile_button_release (GtkWidget * widget, GdkEventButton * event)
{
	Tile *tile = TILE (widget);
	TilePrivate *priv = TILE_GET_PRIVATE (tile);

	TileEvent *tile_event;

	if (priv->is_dragging)
	{
		priv->is_dragging = FALSE;

		return TRUE;
	}

	switch (event->button)
	{
	case 1:
		tile_event = g_new0 (TileEvent, 1);
		tile_event->time = event->time;

		if (double_click_detector_is_double_click (priv->double_click_detector, event->time,
				TRUE))
			tile_event->type = TILE_EVENT_ACTIVATED_DOUBLE_CLICK;
		else
			tile_event->type = TILE_EVENT_ACTIVATED_SINGLE_CLICK;

		g_signal_emit (tile, tile_signals[TILE_ACTIVATED_SIGNAL], 0, tile_event);

		gtk_button_released (GTK_BUTTON (widget));
		g_free (tile_event);

		break;

	case 3:
		if (GTK_IS_MENU (tile->context_menu))
			gtk_menu_popup (tile->context_menu, NULL, NULL, NULL, NULL, event->button,
				event->time);

		break;

	default:
		break;
	}

	return TRUE;
}

static gboolean
tile_key_release (GtkWidget * widget, GdkEventKey * event)
{
	TileEvent *tile_event;

	if (event->keyval == GDK_KEY_Return)
	{
		tile_event = g_new0 (TileEvent, 1);
		tile_event->type = TILE_EVENT_ACTIVATED_KEYBOARD;
		tile_event->time = event->time;

		g_signal_emit (widget, tile_signals[TILE_ACTIVATED_SIGNAL], 0, tile_event);

		return TRUE;
	}

	return FALSE;
}

static void
tile_popup_menu_position (GtkMenu * menu, gint * x, gint * y, gboolean * push_in, gpointer data)
{
	Tile *tile = TILE (data);

	GtkAllocation all;
	GtkRequisition req;
	GtkWidget *top;

	if (!gtk_widget_get_realized (GTK_WIDGET (tile)))
		return;

	gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
	gtk_widget_get_allocation (GTK_WIDGET (menu), &all);

	top = gtk_widget_get_toplevel (GTK_WIDGET (tile));

	gdk_window_get_origin (gtk_widget_get_window (top), x, y);

	*x += (all.width / 2) - (req.width / 2);
	*y += (all.height / 2) - (req.height / 2);

	*push_in = FALSE;
}

static gboolean
tile_popup_menu (GtkWidget * widget)
{
	Tile *tile = TILE (widget);

	if (GTK_IS_MENU (tile->context_menu))
	{
		gtk_menu_popup (tile->context_menu, NULL, NULL, tile_popup_menu_position, tile, 0,
			gtk_get_current_event_time ());

		return TRUE;
	}

	else
		return FALSE;
}

static void
tile_drag_begin (GtkWidget * widget, GdkDragContext * context)
{
	TILE_GET_PRIVATE (widget)->is_dragging = TRUE;
}

static void
tile_drag_data_get (GtkWidget * widget, GdkDragContext * context, GtkSelectionData * data,
	guint info, guint time)
{
	gchar *uris[2];

	if (TILE (widget)->uri)
	{
		uris[0] = TILE (widget)->uri;
		uris[1] = NULL;

		gtk_selection_data_set_uris (data, uris);
	}
}

static void
tile_tile_action_triggered (Tile * tile, TileEvent * event, TileAction * action)
{
	if (action && action->func)
		(*action->func) (tile, event, action);
}

void
tile_trigger_action (Tile * tile, TileAction * action)
{
	tile_trigger_action_with_time (tile, action, GDK_CURRENT_TIME);
}

void
tile_trigger_action_with_time (Tile * tile, TileAction * action, guint32 time)
{
	TileEvent *event = g_new0 (TileEvent, 1);

	event->type = TILE_EVENT_ACTION_TRIGGERED;
	event->time = time;

	g_signal_emit (tile, tile_signals[TILE_ACTION_TRIGGERED_SIGNAL], 0, event, action);
	g_free (event);
}

static void
tile_action_triggered_event_marshal (GClosure * closure, GValue * retval, guint n_param,
	const GValue * param, gpointer invocation_hint, gpointer marshal_data)
{
	marshal_func_VOID__POINTER_POINTER callback;
	GCClosure *cc = (GCClosure *) closure;
	gpointer data_0, data_1;

	g_return_if_fail (n_param == 3);

	if (G_CCLOSURE_SWAP_DATA (closure))
	{
		data_0 = closure->data;
		data_1 = g_value_peek_pointer (param);
	}
	else
	{
		data_0 = g_value_peek_pointer (param);
		data_1 = closure->data;
	}

	if (marshal_data)
		callback = (marshal_func_VOID__POINTER_POINTER) marshal_data;
	else
		callback = (marshal_func_VOID__POINTER_POINTER) cc->callback;

	callback (data_0, g_value_peek_pointer (param + 1), g_value_peek_pointer (param + 2),
		data_1);
}