summaryrefslogtreecommitdiff
path: root/src/nautilus-desktop-window.c
blob: c437e823d525d62f45ec8cc5c52b575e600740ed (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/*
 * Nautilus
 *
 * Copyright (C) 2000 Eazel, Inc.
 *
 * Nautilus 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.
 *
 * Nautilus 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Authors: Darin Adler <darin@eazel.com>
 */

/* nautilus-desktop-window.c
 */

#include <config.h>
#include "nautilus-desktop-window.h"

#include <libgnome/gnome-i18n.h>
#include <libgnomevfs/gnome-vfs-find-directory.h>
#include <libgnomeui/gnome-winhints.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <eel/eel-gtk-macros.h>
#include <eel/eel-gtk-extensions.h>
#include <libnautilus-private/nautilus-file-utilities.h>
#include <libnautilus-private/nautilus-link.h>
#include <X11/Xatom.h>
#include <gdk/gdkx.h>
#include <gtk/gtklayout.h>

struct NautilusDesktopWindowDetails {
	GList *unref_list;
};

static void nautilus_desktop_window_initialize_class (NautilusDesktopWindowClass *klass);
static void nautilus_desktop_window_initialize       (NautilusDesktopWindow      *window);
static void destroy                                  (GtkObject                  *object);
static void realize                                  (GtkWidget                  *widget);
static void map                                      (GtkWidget                  *widget);
static void real_add_current_location_to_history_list (NautilusWindow		 *window);


static void set_wmspec_desktop_hint                   (GdkWindow *window);

EEL_DEFINE_CLASS_BOILERPLATE (NautilusDesktopWindow, nautilus_desktop_window, NAUTILUS_TYPE_WINDOW)

static void
nautilus_desktop_window_initialize_class (NautilusDesktopWindowClass *klass)
{
	GTK_OBJECT_CLASS (klass)->destroy = destroy;
	GTK_WIDGET_CLASS (klass)->realize = realize;
	GTK_WIDGET_CLASS (klass)->map = map;
	NAUTILUS_WINDOW_CLASS (klass)->add_current_location_to_history_list 
		= real_add_current_location_to_history_list;
}

static void
nautilus_desktop_window_initialize (NautilusDesktopWindow *window)
{
	window->details = g_new0 (NautilusDesktopWindowDetails, 1);

	/* FIXME bugzilla.eazel.com 1251: 
	 * Although Havoc had this call to set_default_size in
	 * his code, it seems to have no effect for me. But the
	 * set_usize below does seem to work.
	 */
	gtk_window_set_default_size (GTK_WINDOW (window),
				     gdk_screen_width (),
				     gdk_screen_height ());

	/* These calls seem to have some effect, but it's not clear if
	 * they are the right thing to do.
	 */
	gtk_widget_set_uposition (GTK_WIDGET (window), 0, 0);
	gtk_widget_set_usize (GTK_WIDGET (window),
			      gdk_screen_width (),
			      gdk_screen_height ());

	/* Tell the window manager to never resize this. This is not
	 * known to have any specific beneficial effect with any
	 * particular window manager for the case of the desktop
	 * window, but it doesn't seem to do any harm.
	 */
	gtk_window_set_policy (GTK_WINDOW (window),
			       FALSE, FALSE, FALSE);
}

static void
nautilus_desktop_window_realized (NautilusDesktopWindow *window)
{
	/* Tuck the desktop windows xid in the root to indicate we own the desktop.
	 */
	Window window_xid;
	window_xid = GDK_WINDOW_XWINDOW (GTK_WIDGET (window)->window);
	gdk_property_change (NULL, gdk_atom_intern ("NAUTILUS_DESKTOP_WINDOW_ID", FALSE),
			     XA_WINDOW, 32, PropModeReplace, (guchar *) &window_xid, 1);
}

static gint
nautilus_desktop_window_delete_event (NautilusDesktopWindow *window)
{
	/* Returning true tells GTK+ not to delete the window. */
	return TRUE;
}

void
nautilus_desktop_window_update_directory (NautilusDesktopWindow *window)
{
	char *desktop_directory_path;
	char *desktop_directory_uri;

	g_assert (NAUTILUS_IS_DESKTOP_WINDOW (window));

	desktop_directory_path = nautilus_get_desktop_directory ();
	
	desktop_directory_uri = gnome_vfs_get_uri_from_local_path (desktop_directory_path);
	g_free (desktop_directory_path);
	window->affect_desktop_on_next_location_change = TRUE;
	nautilus_window_go_to (NAUTILUS_WINDOW (window), desktop_directory_uri);
	g_free (desktop_directory_uri);
}

NautilusDesktopWindow *
nautilus_desktop_window_new (NautilusApplication *application)
{
	NautilusDesktopWindow *window;

	window = NAUTILUS_DESKTOP_WINDOW
		(gtk_widget_new (nautilus_desktop_window_get_type(),
				 "app", application,
				 "app_id", "nautilus",
				 NULL));

	/* Special sawmill setting*/
	gtk_window_set_wmclass (GTK_WINDOW (window), "desktop_window", "Nautilus");

	gtk_signal_connect (GTK_OBJECT (window), "realize", GTK_SIGNAL_FUNC (nautilus_desktop_window_realized), NULL);
	gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (nautilus_desktop_window_delete_event), NULL);

	/* Point window at the desktop folder.
	 * Note that nautilus_desktop_window_initialize is too early to do this.
	 */
	nautilus_desktop_window_update_directory (window);

	gtk_widget_show (GTK_WIDGET (window));

	return window;
}

static void
destroy (GtkObject *object)
{
	NautilusDesktopWindow *window;

	window = NAUTILUS_DESKTOP_WINDOW (object);

	gdk_property_delete (NULL, gdk_atom_intern ("NAUTILUS_DESKTOP_WINDOW_ID", TRUE));

	eel_gtk_object_list_free (window->details->unref_list);
	g_free (window->details);

	EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}

static void
set_gdk_window_background (GdkWindow *window,
			   gboolean   have_pixel,
			   Pixmap     pixmap,
			   gulong     pixel)
{
	Window w;

	w = GDK_WINDOW_XWINDOW (window);

	if (pixmap != None) {
		XSetWindowBackgroundPixmap (GDK_DISPLAY (), w,
					    pixmap);
	} else if (have_pixel) {
		XSetWindowBackground (GDK_DISPLAY (), w,
				      pixel);
	} else {
		XSetWindowBackgroundPixmap (GDK_DISPLAY (), w,
					    None);
	}
}

static void
set_window_background (GtkWidget *widget,
		       gboolean   already_have_root_bg,
		       gboolean   have_pixel,
		       Pixmap     pixmap,
		       gulong     pixel)
{
	GdkAtom type;
	gulong nitems, bytes_after;
	gint format;
	guchar *data;
	
	/* Set the background to show the root window to avoid a flash that
	 * would otherwise occur.
	 */

	if (GTK_IS_WINDOW (widget)) {
		gtk_widget_set_app_paintable (widget, TRUE);
	}
	
	if (!already_have_root_bg) {
		have_pixel = FALSE;
		already_have_root_bg = TRUE;
		
		/* We want to do this round-trip-to-server work only
		 * for the first invocation, not on recursions
		 */
		
		XGetWindowProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
				    gdk_atom_intern ("_XROOTPMAP_ID", FALSE),
				    0L, 1L, False, XA_PIXMAP,
				    &type, &format, &nitems, &bytes_after,
				    &data);
  
		if (type == XA_PIXMAP) {
			if (format == 32 && nitems == 1 && bytes_after == 0) {
				pixmap = *(Pixmap *) data;
			}
  
			XFree (data);
		}

		if (pixmap == None) {
			XGetWindowProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
					    gdk_atom_intern ("_XROOTCOLOR_PIXEL", FALSE),
					    0L, 1L, False, AnyPropertyType,
					    &type, &format, &nitems, &bytes_after,
					    &data);
			
			if (type != None) {
				if (format == 32 && nitems == 1 && bytes_after == 0) {
					pixel = *(gulong *) data;
					have_pixel = TRUE;
				}
				
				XFree (data);
			}
		}
	}
	
	set_gdk_window_background (widget->window,
				   have_pixel, pixmap, pixel);
	
	if (GTK_IS_BIN (widget) &&
	    GTK_BIN (widget)->child) {
		/* Ensure we're realized */
		gtk_widget_realize (GTK_BIN (widget)->child);
		
		set_window_background (GTK_BIN (widget)->child,
				       already_have_root_bg, have_pixel,
				       pixmap, pixel);
	}

	/* For both parent and child, if it's a layout then set on the
	 * bin window as well.
	 */
	if (GTK_IS_LAYOUT (widget))
		set_gdk_window_background (GTK_LAYOUT (widget)->bin_window,
					   have_pixel,
					   pixmap, pixel);
}

static void
realize (GtkWidget *widget)
{
	NautilusDesktopWindow *window;

	window = NAUTILUS_DESKTOP_WINDOW (widget);

	/* Make sure we get keyboard events */
	gtk_widget_set_events (widget, gtk_widget_get_events (widget) 
			      | GDK_KEY_PRESS_MASK | GDK_KEY_PRESS_MASK);
			      
	/* Do the work of realizing. */
	EEL_CALL_PARENT (GTK_WIDGET_CLASS, realize, (widget));

	/* This is the new way to set up the desktop window */
	set_wmspec_desktop_hint (widget->window);
	
	/* FIXME all this gnome_win_hints stuff is legacy cruft */
	
	/* Put this window behind all the others. */
	gnome_win_hints_set_layer (widget, WIN_LAYER_DESKTOP);
	
	/* Make things like the task list ignore this window and make
	 * it clear that it it's at its full size.
	 *
	 * We originally included WIN_STATE_HIDDEN here, but IceWM
	 * interprets that (wrongly, imho) as meaning `don't display
	 * this window'. Not including this bit seems to make
	 * no difference though, so..
	 */
	gnome_win_hints_set_state (widget,
				   WIN_STATE_STICKY
				   | WIN_STATE_MAXIMIZED_VERT
				   | WIN_STATE_MAXIMIZED_HORIZ
				   | WIN_STATE_FIXED_POSITION
				   | WIN_STATE_ARRANGE_IGNORE);

	/* Make sure that focus, and any window lists or task bars also
	 * skip the window.
	 */
	gnome_win_hints_set_hints (widget,
				   WIN_HINTS_SKIP_WINLIST
				   | WIN_HINTS_SKIP_TASKBAR
				   | WIN_HINTS_SKIP_FOCUS);

	/* FIXME bugzilla.eazel.com 1255: 
	 * Should we do a gdk_window_move_resize here, in addition to
	 * the calls in initialize above that set the size?
	 */
	gdk_window_move_resize (widget->window,
				0, 0,
				gdk_screen_width (),
				gdk_screen_height ());

	/* Get rid of the things that window managers add to resize
	 * and otherwise manipulate the window.
	 */
        gdk_window_set_decorations (widget->window, 0);
        gdk_window_set_functions (widget->window, 0);
}

static void
map (GtkWidget *widget)
{
	NautilusDesktopWindow *window;

	window = NAUTILUS_DESKTOP_WINDOW (widget);
	
	set_window_background (widget, FALSE, FALSE, None, 0);
	
	/* Chain up to realize our children */
	EEL_CALL_PARENT (GTK_WIDGET_CLASS, map, (widget));
}

static void
real_add_current_location_to_history_list (NautilusWindow *window)
{
	/* Do nothing. The desktop window's location should not
	 * show up in the history list.
	 */
}

static void
set_wmspec_desktop_hint (GdkWindow *window)
{
        Atom atom;
        
        atom = XInternAtom (gdk_display,
                            "_NET_WM_WINDOW_TYPE_DESKTOP",
                            False);

        XChangeProperty (GDK_WINDOW_XDISPLAY (window),
                         GDK_WINDOW_XWINDOW (window),
                         XInternAtom (gdk_display,
                                      "_NET_WM_WINDOW_TYPE",
                                      False),
                         XA_ATOM, 32, PropModeReplace,
                         (guchar *)&atom, 1);
}