summaryrefslogtreecommitdiff
path: root/libmdock/mdock-item.c
blob: 87a4f4902fead52d14f4033970c81082e0b753b2 (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
/*
 * Copyright 2015 Javier S. Pedro <dev.git@javispedro.com>
 *
 * This file is part of MDock.
 *
 * MDock 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 3 of the License, or
 * (at your option) any later version.
 *
 * MDock 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 General Public License
 * along with MDock.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <math.h>
#include "mdock-item.h"

#define MDOCK_ITEM_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), MDOCK_TYPE_ITEM, MDockItemPrivate))

#define REQUISITION_ICON_SIZE 32

struct _MDockItemPrivate
{
	gboolean pinned;
	GDesktopAppInfo *appinfo;
	guint icon_size;
	GdkPixbuf *icon;
	GList *windows;

	WnckWindow *last_active;
};

G_DEFINE_TYPE (MDockItem, mdock_item, GTK_TYPE_WIDGET)

enum {
	PROP_0,
	PROP_PINNED,
	PROP_DESKTOP_APP_INFO,
	PROP_N_WINDOWS,
	PROP_DISPLAY_NAME,
	N_PROPERTIES
};

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL };

static GdkPixbuf *mdock_item_load_icon(MDockItem *self, guint icon_size)
{
	if (self->priv->appinfo) {
		GtkIconTheme *theme = gtk_icon_theme_get_default();
		GIcon *icon = g_app_info_get_icon(G_APP_INFO(self->priv->appinfo));
		if (icon) {
			GtkIconInfo *info = gtk_icon_theme_lookup_by_gicon(theme, icon, icon_size,
			                                                   GTK_ICON_LOOKUP_GENERIC_FALLBACK | GTK_ICON_LOOKUP_FORCE_SIZE);
			GdkPixbuf *pixbuf = gtk_icon_info_load_icon(info, NULL);
			gtk_icon_info_free(info);
			if (pixbuf) {
				return pixbuf;
			}
		}
	}

	if (self->priv->windows) {
		GdkPixbuf *pixbuf = wnck_window_get_icon(self->priv->windows->data);
		if (pixbuf) {
			return gdk_pixbuf_scale_simple(pixbuf, self->priv->icon_size, icon_size, GDK_INTERP_BILINEAR);
		}
	}

	return NULL;
}

static void mdock_item_update_icon(MDockItem *self)
{
	g_clear_object(&self->priv->icon);
	self->priv->icon = mdock_item_load_icon(self, self->priv->icon_size);
	gtk_widget_queue_draw(GTK_WIDGET(self));
}

static void mdock_item_realize(GtkWidget *widget)
{
	GdkWindowAttr attributes;
	guint attributes_mask;

	attributes.window_type = GDK_WINDOW_CHILD;
	attributes.x = widget->allocation.x;
	attributes.y = widget->allocation.y;
	attributes.width = widget->allocation.width;
	attributes.height = widget->allocation.height;

	attributes.wclass = GDK_INPUT_OUTPUT;
	attributes.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;

	attributes_mask = GDK_WA_X | GDK_WA_Y;

	widget->window = gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, attributes_mask);

	gdk_window_set_back_pixmap(widget->window, NULL, TRUE);
	gdk_window_set_user_data(widget->window, widget);
	gtk_widget_set_realized(widget, TRUE);

	widget->style = gtk_style_attach(widget->style, widget->window);
}

static void mdock_item_size_request(GtkWidget *widget, GtkRequisition *requisition)
{
	requisition->width = requisition->height = REQUISITION_ICON_SIZE;
}

static void mdock_item_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
	MDockItem *self = MDOCK_ITEM(widget);
	self->priv->icon_size = MIN(allocation->width, allocation->height);
	GTK_WIDGET_CLASS(mdock_item_parent_class)->size_allocate(widget, allocation);
	mdock_item_update_icon(self);
}

static gboolean mdock_item_expose(GtkWidget *widget, GdkEventExpose *event)
{
	MDockItem *self = MDOCK_ITEM(widget);
	cairo_t *cr = gdk_cairo_create(widget->window);

	if (self->priv->icon) {
		cairo_save(cr);
		gdk_cairo_set_source_pixbuf(cr, self->priv->icon, 0, 0);
		cairo_paint(cr);
		cairo_restore(cr);
	}
	if (self->priv->windows) {
		double circle_radius = self->priv->icon_size / 8.0;
		cairo_save(cr);
		cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 0.6);
		cairo_translate(cr, self->priv->icon_size / 2.0, self->priv->icon_size - circle_radius / 2.0);
		cairo_arc(cr, 0, 0, circle_radius, 0, 2 * M_PI);
		cairo_fill(cr);
		cairo_restore(cr);
	}

	cairo_destroy(cr);

	return FALSE;
}

static void mdock_item_set_property(GObject        *object,
                                    guint           property_id,
                                    const GValue   *value,
                                    GParamSpec     *pspec)
{
	MDockItem *self = MDOCK_ITEM(object);
	switch (property_id) {
	case PROP_PINNED:
		self->priv->pinned = g_value_get_boolean(value);
		break;
	case PROP_DESKTOP_APP_INFO:
		self->priv->appinfo = g_value_dup_object(value);
		mdock_item_update_icon(self);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
		break;
	}
}

static void mdock_item_get_property(GObject        *object,
                                    guint           property_id,
                                    GValue         *value,
                                    GParamSpec     *pspec)
{
	MDockItem *self = MDOCK_ITEM(object);
	switch (property_id) {
	case PROP_PINNED:
		g_value_set_boolean(value, self->priv->pinned);
		break;
	case PROP_DESKTOP_APP_INFO:
		g_value_set_object(value, self->priv->appinfo);
		break;
	case PROP_N_WINDOWS:
		g_value_set_uint(value, g_list_length(self->priv->windows));
		break;
	case PROP_DISPLAY_NAME:
		g_value_set_string(value, mdock_item_get_display_name(self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
		break;
	}
}

static void mdock_item_dispose(GObject *object)
{
	MDockItem *self = MDOCK_ITEM(object);
	g_clear_object(&self->priv->appinfo);
	g_clear_object(&self->priv->icon);
	G_OBJECT_CLASS(mdock_item_parent_class)->dispose(object);
}

static void mdock_item_finalize(GObject *object)
{
	MDockItem *self = MDOCK_ITEM(object);
	g_list_free(self->priv->windows);
	G_OBJECT_CLASS(mdock_item_parent_class)->finalize(object);
}

static void mdock_item_init(MDockItem *self)
{
	self->priv = MDOCK_ITEM_GET_PRIVATE (self);
	self->priv->pinned = FALSE;
	self->priv->icon_size = REQUISITION_ICON_SIZE;

	gtk_widget_add_events(GTK_WIDGET(self),
	                      GDK_BUTTON_PRESS_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
}

static void
mdock_item_class_init(MDockItemClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS (klass);
	obj_class->set_property = mdock_item_set_property;
	obj_class->get_property = mdock_item_get_property;
	obj_class->dispose = mdock_item_dispose;
	obj_class->finalize = mdock_item_finalize;

	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
	widget_class->realize = mdock_item_realize;
	widget_class->size_request = mdock_item_size_request;
	widget_class->size_allocate = mdock_item_size_allocate;
	widget_class->expose_event = mdock_item_expose;

	obj_properties[PROP_PINNED] = g_param_spec_boolean("pinned",
	                                                   "Pinned launcher",
	                                                   "Set whether this launcher is pinned",
	                                                   FALSE,
	                                                   G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_DESKTOP_APP_INFO] = g_param_spec_object("desktop-app-info",
	                                                            "The GDesktopAppInfo this application or launcher refers to",
	                                                            "Set the GDesktopAppInfo this application or launcher refers to",
	                                                            G_TYPE_DESKTOP_APP_INFO,
	                                                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_N_WINDOWS] = g_param_spec_uint("n-windows",
	                                                   "Number of windows",
	                                                   "Reads the current number of windows under this item",
	                                                   0, G_MAXINT32,
	                                                   0,
	                                                   G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_DISPLAY_NAME] = g_param_spec_string("display-name",
	                                                        "Name of this item",
	                                                        "Reads the current name of this item",
	                                                        "",
	                                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, obj_properties);

	g_type_class_add_private (obj_class, sizeof (MDockItemPrivate));
}

MDockItem *mdock_item_new()
{
	return g_object_new(MDOCK_TYPE_ITEM, NULL);
}

gboolean mdock_item_get_pinned(MDockItem *self)
{
	return self->priv->pinned;
}

void mdock_item_set_pinned(MDockItem *self, gboolean pinned)
{
	g_object_set(self, "pinned", pinned, NULL);
}

void mdock_item_toggle_pinned(MDockItem *self)
{
	mdock_item_set_pinned(self, !self->priv->pinned);
}

GDesktopAppInfo *mdock_item_get_desktop_app_info(MDockItem *self)
{
	return self->priv->appinfo;
}

void mdock_item_set_desktop_app_info(MDockItem *self, GDesktopAppInfo *app_info)
{
	g_object_set(self, "desktop-app-info", app_info, NULL);
}

void mdock_item_launch_files(MDockItem *self, GList *files)
{
	g_return_if_fail(self->priv->appinfo);
	GError *error = NULL;
	g_debug("Launching '%s'", g_app_info_get_commandline(G_APP_INFO(self->priv->appinfo)));

	if (!g_app_info_launch(G_APP_INFO(self->priv->appinfo), files, NULL, &error)) {
		GtkWidget *msg;

		g_warning("Cannot launch '%s': %s",
		          g_app_info_get_display_name(G_APP_INFO(self->priv->appinfo)),
                  error->message);

		msg = gtk_message_dialog_new(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(self))),
		                             GTK_DIALOG_DESTROY_WITH_PARENT,
		                             GTK_MESSAGE_WARNING,
		                             GTK_BUTTONS_OK,
		                             "Cannot launch '%s': %s",
		                             g_app_info_get_display_name(G_APP_INFO(self->priv->appinfo)),
		                             error->message);

		g_signal_connect_swapped(msg, "response",
		                         G_CALLBACK(gtk_widget_destroy), msg);

		gtk_widget_show_all(msg);
		g_error_free(error);
	}


}

void mdock_item_launch_file(MDockItem *self, GFile *file)
{
	GList *files = g_list_append(NULL, file);
	mdock_item_launch_files(self, files);
	g_list_free(files);
}

void mdock_item_launch(MDockItem *self)
{
	mdock_item_launch_files(self, NULL);
}

const gchar *mdock_item_get_display_name(MDockItem *self)
{
	if (self->priv->appinfo) {
		return g_app_info_get_display_name(G_APP_INFO(self->priv->appinfo));
	} else if (self->priv->windows) {
		WnckApplication *app = wnck_window_get_application(self->priv->windows->data);
		return wnck_application_get_name(app);
	} else {
		return NULL;
	}
}

GdkPixbuf *mdock_item_get_icon_pixbuf(MDockItem *self)
{
	return self->priv->icon;
}

void mdock_item_add_window(MDockItem *self, WnckWindow *window)
{
	if (!self->priv->windows) {
		// If there were no windows before adding this one,
		// we want to redraw in order to get the "active" indicator.
		gtk_widget_queue_draw(GTK_WIDGET(self));
	}
	self->priv->windows = g_list_append(self->priv->windows, window);
	if (!self->priv->icon) {
		mdock_item_update_icon(self);
	}
	g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_N_WINDOWS]);
}

void mdock_item_remove_window(MDockItem *self, WnckWindow *window)
{
	self->priv->windows = g_list_remove(self->priv->windows, window);
	if (self->priv->last_active == window) {
		self->priv->last_active = NULL;
	}
	if (!self->priv->windows) {
		// If there are no windows left,
		// we want to redraw in order to remove the "active" indicator.
		gtk_widget_queue_draw(GTK_WIDGET(self));
	}
	g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_N_WINDOWS]);
}

void mdock_item_set_last_active_window(MDockItem *self, WnckWindow *window)
{
	g_return_if_fail(g_list_find(self->priv->windows, window));
	self->priv->last_active = window;
}

gint mdock_item_get_num_windows(MDockItem *self)
{
	return g_list_length(self->priv->windows);
}

gboolean mdock_item_has_windows(MDockItem *self)
{
	return self->priv->windows ? TRUE : FALSE;
}

void mdock_item_close_all_windows(MDockItem *self)
{
	for (GList *l = self->priv->windows; l; l = g_list_next(l)) {
		WnckWindow *window = WNCK_WINDOW(l->data);
		wnck_window_close(window, GDK_CURRENT_TIME);
	}
}

void mdock_item_activate(MDockItem *self)
{
	if (self->priv->windows) {
		// This item has one window at least
		if (self->priv->windows->next) {
			// There is more than one window
			gboolean found_window = FALSE;
			for (GList *l = self->priv->windows; l; l = g_list_next(l)) {
				WnckWindow *window = WNCK_WINDOW(l->data);
				if (wnck_window_is_most_recently_activated(window)
				        || wnck_window_transient_is_most_recently_activated(window)) {
					// This is the currently focused window, let's focus the next in the list
					GList *n = l->next ? g_list_next(l) : self->priv->windows;
					window = WNCK_WINDOW(n->data);
					wnck_window_activate_transient(window, gtk_get_current_event_time());
					found_window = TRUE;
					break;
				}
			}
			if (!found_window) {
				WnckWindow *window = WNCK_WINDOW(self->priv->windows->data);
				wnck_window_activate_transient(window, gtk_get_current_event_time());
			}
		} else {
			// There is only one window in this group
			WnckWindow *window = WNCK_WINDOW(self->priv->windows->data);
			if (wnck_window_is_minimized(window)) {
				wnck_window_unminimize(window, gtk_get_current_event_time());
			} else if (wnck_window_is_most_recently_activated(window)
			           || wnck_window_transient_is_most_recently_activated(window)) {
				wnck_window_minimize(window);
			} else {
				wnck_window_activate_transient(window, gtk_get_current_event_time());
			}
		}
	} else if (self->priv->appinfo) {
		// No windows, but we have an application to launch!
		mdock_item_launch(self);
	} else {
		g_warning("A MDockItem has no windows and no appinfo to launch");
	}
}