summaryrefslogtreecommitdiff
path: root/applets/launcher/nautilus-launcher-applet.c
blob: f6f6367f2af083f3864c9dbc9f1bd8e51ee0ddf9 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* 
   Copyright (C) 2000 Eazel, Inc.

   The Gnome 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.

   The Gnome 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 the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Mathieu Lacage  <mathieu@eazel.com>
            Ramiro Estrugo <ramiro@eazel.com>
 */

/* Everything beyond this point is pure evil. */

#include <config.h>
#include <applet-widget.h>
#include <libnautilus-extensions/nautilus-image.h>
#include <libnautilus-extensions/nautilus-graphic-effects.h>
#include <libgnome/gnome-exec.h>
#include <gtk/gtkobject.h>
#include <gtk/gtkeventbox.h>
#include <gdk/gdkx.h>
#include <gdk/gdkprivate.h>
#include <X11/Xatom.h>
#include <gdk/gdkx.h>

/*
 * The purpose of this applet is to launch Nautilus.  The applet tries very hard to 
 * give good feedback to the user by changing the appearance of the launch icon.
 * The launch icon cursor and the desktop cursor are also updated in concert when 
 * a new Nautilus window is launching.
 */

#define ICON_NAME "nautilus-launch-icon.png"
#define VERTICAL_OFFSET 2
#define HORIZONTAL_OFFSET 2

static GdkPixbuf *icon_pixbuf = NULL;
static GdkPixbuf *icon_prelight_pixbuf = NULL;
static long last_window_realize_time = 0;
static GtkWidget *icon_image = NULL;
static GtkWidget *icon_event_box = NULL;

static void     set_is_launching (gboolean state);
static gboolean get_is_launching (void);

static void
create_pixbufs ()
{
	if (icon_pixbuf == NULL) {
		char *path;
		
		path = g_strdup_printf ("%s/pixmaps/%s", DATADIR, ICON_NAME);
		
		icon_pixbuf = gdk_pixbuf_new_from_file (path);
		g_free (path);
		
		g_assert (icon_pixbuf != NULL);

		icon_prelight_pixbuf = nautilus_create_spotlight_pixbuf (icon_pixbuf);
	}
}

static void
applet_change_pixel_size(GtkWidget *widget, int size, gpointer data)
{
	/* need to change the size of the button here */
}

static gint
image_enter_event (GtkWidget *event_box,
		   GdkEventCrossing *event,
		   gpointer client_data)
{
	g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), TRUE);
	g_return_val_if_fail (NAUTILUS_IS_IMAGE (client_data), TRUE);

	if (!get_is_launching ()) {
		nautilus_image_set_pixbuf (NAUTILUS_IMAGE (client_data), icon_prelight_pixbuf);
	}

	return TRUE;
}

static gint
image_leave_event (GtkWidget *event_box,
		   GdkEventCrossing *event,
		   gpointer client_data)
{
	g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), TRUE);
	g_return_val_if_fail (NAUTILUS_IS_IMAGE (client_data), TRUE);
	
	nautilus_image_set_pixbuf (NAUTILUS_IMAGE (client_data), icon_pixbuf);
	gtk_object_set_data (GTK_OBJECT (event_box), "was-pressed", FALSE);

	return TRUE;
}

static GdkWindow *
get_root_window (void)
{
	return GDK_ROOT_PARENT ();
}

static void
window_set_cursor_for_state (GdkWindow *window, gboolean busy)
{
	GdkCursor *cursor;

	g_return_if_fail (window != NULL);

	cursor = gdk_cursor_new (busy ? GDK_WATCH : GDK_LEFT_PTR);
 	gdk_window_set_cursor (window, cursor);
	gdk_cursor_destroy (cursor);
}

static gboolean is_launching = FALSE;

static void
set_is_launching (gboolean state)
{
	if (is_launching == state) {
		return;
	}

	is_launching = state;

	window_set_cursor_for_state (get_root_window (), is_launching);
	window_set_cursor_for_state (GTK_WIDGET (icon_event_box)->window, is_launching);	

	nautilus_image_set_pixbuf_opacity (NAUTILUS_IMAGE (icon_image), is_launching ? 128 : 255);
}

static gboolean
get_is_launching (void)
{
	return is_launching;
}

static gint
image_button_press_event (GtkWidget *event_box,
			  GdkEventButton *event,
			  gpointer client_data)
{
	GtkWidget *icon = GTK_WIDGET (client_data);

	g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), TRUE);
	g_return_val_if_fail (NAUTILUS_IS_IMAGE (icon), TRUE);

	if (!get_is_launching ()) {
		gtk_object_set_data (GTK_OBJECT (event_box), "was-pressed", GINT_TO_POINTER (TRUE));
	}
		
	return TRUE;
}

static gint
image_button_release_event (GtkWidget *event_box,
			    GdkEventButton *event,
			    gpointer client_data)
{
	GtkWidget *icon = GTK_WIDGET (client_data);

	g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), TRUE);
	g_return_val_if_fail (NAUTILUS_IS_IMAGE (icon), TRUE);

	if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (event_box), "was-pressed"))) {
		gtk_object_set_data (GTK_OBJECT (event_box), "was-pressed", FALSE);

		if (!get_is_launching ())
		{
			char *path;
			gint pid;

			path = g_strdup_printf ("%s/%s", BINDIR, "run-nautilus");
			
			pid = gnome_execute_async (NULL, 1, &path);
			
			if (pid != 0) {
				set_is_launching (TRUE);
			}

			g_free (path);
		}
	}
	
	return TRUE;
}

static GdkFilterReturn
event_filter (GdkXEvent *gdk_xevent,
	      GdkEvent *event,
	      gpointer client_data)
{
	XEvent *xevent = (XEvent *) gdk_xevent;
	
	if (xevent->type == PropertyNotify) {
		GdkAtom actual_property_type;
		gint actual_format;
		gint actual_length;
		guchar *data;
	
		if (gdk_property_get (get_root_window (),
				      gdk_atom_intern ("_NAUTILUS_LAST_WINDOW_REALIZE_TIME", FALSE),
				      0,
				      0,
				      1L,
				      FALSE,
				      &actual_property_type,
				      &actual_format,
				      &actual_length,
				      &data)) {
			
			if (actual_format == 32 && actual_length == 4) {
				long realize_time;
				
				realize_time = *((long *) data);
				
				if (last_window_realize_time != realize_time) {
					last_window_realize_time = realize_time;
					set_is_launching (FALSE);
				}
			}
			
			g_free (data);
			
		}
	}

	return GDK_FILTER_CONTINUE;
}

static void
root_listen_for_property_changes (void)
{
	XWindowAttributes attribs = { 0 };

	gdk_window_add_filter (get_root_window (), event_filter, NULL);
	
	XGetWindowAttributes (GDK_DISPLAY (), GDK_ROOT_WINDOW (), &attribs);

	XSelectInput (GDK_DISPLAY (), GDK_ROOT_WINDOW (), attribs.your_event_mask | PropertyChangeMask);
	
	gdk_flush ();
}

int
main (int argc, char **argv)
{
	GtkWidget *applet;
	int size;

	bindtextdomain (PACKAGE, GNOMELOCALEDIR);
	textdomain (PACKAGE);
	
	applet_widget_init ("nautilus_launcher_applet", VERSION, argc,
			    argv, NULL, 0, NULL);

	applet = applet_widget_new ("nautilus_launcher_applet");
	if (applet == NULL)
		g_error (_("Can't create nautilus-launcher-applet!"));

	root_listen_for_property_changes ();

	create_pixbufs ();

	icon_event_box = gtk_event_box_new ();
	gtk_object_set_data (GTK_OBJECT (icon_event_box), "was-pressed", FALSE);

	icon_image = nautilus_image_new (NULL);
	gtk_misc_set_padding (GTK_MISC (icon_image), 2, 2);

	gtk_signal_connect (GTK_OBJECT (icon_event_box), "enter_notify_event", GTK_SIGNAL_FUNC (image_enter_event), icon_image);
	gtk_signal_connect (GTK_OBJECT (icon_event_box), "leave_notify_event", GTK_SIGNAL_FUNC (image_leave_event), icon_image);
	gtk_signal_connect (GTK_OBJECT (icon_event_box), "button_press_event", GTK_SIGNAL_FUNC (image_button_press_event), icon_image);
	gtk_signal_connect (GTK_OBJECT (icon_event_box), "button_release_event", GTK_SIGNAL_FUNC (image_button_release_event), icon_image);

	nautilus_image_set_pixbuf (NAUTILUS_IMAGE (icon_image), icon_pixbuf);

	gtk_container_add (GTK_CONTAINER (icon_event_box), icon_image);
	gtk_object_set_data (GTK_OBJECT (applet), "widget", icon_event_box);
	applet_widget_add (APPLET_WIDGET (applet), icon_event_box);

	size = applet_widget_get_panel_pixel_size(APPLET_WIDGET(applet)) - 2;
	applet_change_pixel_size (GTK_WIDGET (applet), size, NULL);

	gtk_widget_show_all (applet);

	gtk_signal_connect(GTK_OBJECT(applet),"change_pixel_size",
			   GTK_SIGNAL_FUNC(applet_change_pixel_size),
			   NULL);

	applet_widget_gtk_main ();

	return 0;
}