summaryrefslogtreecommitdiff
path: root/src/nautilus-window-toolbars.c
blob: 504d90d1174d82599e7d9d6819765d439826190f (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
/* -*- 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.
 *
 * Author: John Sullivan <sullivan@eazel.com> 
 */

/* nautilus-window-toolbars.c - implementation of nautilus window toolbar operations,
 * split into separate file just for convenience.
 */

#include <config.h>

#include "nautilus-application.h"
#include "nautilus-window-manage-views.h"
#include "nautilus-window-private.h"
#include "nautilus-window.h"
#include <bonobo.h>
#include <gtk/gtkframe.h>
#include <gtk/gtktogglebutton.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-app.h>
#include <libgnomeui/gnome-app-helper.h>
#include <libgnomeui/gnome-preferences.h>
#include <libnautilus-extensions/nautilus-bonobo-extensions.h>
#include <libnautilus-extensions/nautilus-bookmark.h>
#include <libnautilus-extensions/nautilus-file-utilities.h>
#include <libnautilus-extensions/nautilus-global-preferences.h>
#include <eel/eel-gnome-extensions.h>
#include <eel/eel-gtk-extensions.h>
#include <eel/eel-string.h>
#include <libnautilus-extensions/nautilus-theme.h>

static void
activate_back_or_forward_menu_item (GtkMenuItem *menu_item, 
				    NautilusWindow *window,
				    gboolean back)
{
	int index;
	
	g_assert (GTK_IS_MENU_ITEM (menu_item));
	g_assert (NAUTILUS_IS_WINDOW (window));

	index = GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (menu_item)));
	nautilus_window_back_or_forward (window, back, index);
}

static void
activate_back_menu_item_callback (GtkMenuItem *menu_item, NautilusWindow *window)
{
	activate_back_or_forward_menu_item (menu_item, window, TRUE);
}

static void
activate_forward_menu_item_callback (GtkMenuItem *menu_item, NautilusWindow *window)
{
	activate_back_or_forward_menu_item (menu_item, window, FALSE);
}

static GtkMenu *
create_back_or_forward_menu (NautilusWindow *window, gboolean back)
{
	GtkMenu *menu;
	GtkWidget *menu_item;
	GList *list_link;
	int index;

	g_assert (NAUTILUS_IS_WINDOW (window));
	
	menu = GTK_MENU (gtk_menu_new ());

	list_link = back ? window->back_list : window->forward_list;
	index = 0;
	while (list_link != NULL)
	{
		menu_item = nautilus_bookmark_menu_item_new (NAUTILUS_BOOKMARK (list_link->data));		
		gtk_object_set_user_data (GTK_OBJECT (menu_item), GINT_TO_POINTER (index));
		gtk_widget_show (GTK_WIDGET (menu_item));
  		gtk_signal_connect
			(GTK_OBJECT(menu_item), 
			 "activate",
			 back ? activate_back_menu_item_callback : activate_forward_menu_item_callback, 
			 window);
		
		gtk_menu_append (menu, menu_item);
		list_link = g_list_next (list_link);
		++index;
	}

	return menu;
}

static GtkWidget *
get_back_button (NautilusWindow *window)
{
	return GTK_WIDGET (bonobo_ui_toolbar_button_item_get_button_widget 
		(window->details->back_button_item));
}

static GtkWidget *
get_forward_button (NautilusWindow *window)
{
	return GTK_WIDGET (bonobo_ui_toolbar_button_item_get_button_widget 
		(window->details->forward_button_item));
}

static int
back_or_forward_button_pressed_callback (GtkWidget *widget, 
					 GdkEventButton *event, 
					 gpointer *user_data)
{
	NautilusWindow *window;
	gboolean back;

	g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
	g_return_val_if_fail (NAUTILUS_IS_WINDOW (user_data), FALSE);
	g_return_val_if_fail (event != NULL, FALSE);

	window = NAUTILUS_WINDOW (user_data);

	back = widget == get_back_button (window);
	g_assert (back || widget == get_forward_button (window));

	if (event->button == 3) {
		eel_pop_up_context_menu (
			create_back_or_forward_menu (NAUTILUS_WINDOW (user_data),
						     back),
                        EEL_DEFAULT_POPUP_MENU_DISPLACEMENT,
                        EEL_DEFAULT_POPUP_MENU_DISPLACEMENT,
                        event);

		return TRUE;
	}

	return FALSE;
	
}

static void
back_or_forward_button_clicked_callback (GtkWidget *widget, 
				   	 gpointer *user_data)
{
	NautilusWindow *window;
	gboolean back;

	g_return_if_fail (GTK_IS_BUTTON (widget));
	g_return_if_fail (NAUTILUS_IS_WINDOW (user_data));

	window = NAUTILUS_WINDOW (user_data);

	back = widget == get_back_button (window);
	g_assert (back || widget == get_forward_button (window));

	if (back) {
		nautilus_window_go_back (window);
	} else {
		nautilus_window_go_forward (window);
	}
}

static char *
get_file_name_from_icon_name (const char *icon_name, gboolean is_custom)
{
	char *full_path_name, *icon_theme, *theme_path_name;

	/* look in the theme to see if there's a redirection found; if so, prefer the
	 * redirection to the ordinary theme look-up */
	icon_theme = nautilus_theme_get_theme_data ("toolbar", "icon_theme");
	if (icon_theme != NULL) {
		/* special case the "standard" theme which indicates using the stock gnome icons,
		 * except for the custom ones, that are not present in stock
		 */
		if (!is_custom && eel_strcmp (icon_theme, "standard") == 0) {
			g_free (icon_theme);
			return NULL;
		}
		
		theme_path_name = g_strdup_printf ("%s/%s.png", icon_theme, icon_name);
		full_path_name = nautilus_pixmap_file (theme_path_name);
		if (full_path_name == NULL) {
			full_path_name = nautilus_theme_get_image_path (icon_name);
		}
		g_free (theme_path_name);
		g_free (icon_theme);
	} else {
		full_path_name = nautilus_theme_get_image_path (icon_name);
	}

	return full_path_name;
}

static void
set_up_standard_bonobo_button (NautilusWindow *window, 
			       const char *item_path, 
			       const char *icon_name,
			       gboolean is_custom)
{
	char *file_name;

	file_name = get_file_name_from_icon_name (icon_name, is_custom);
		
	nautilus_window_ui_freeze (window);

	/* set up the toolbar component with the new image */
	bonobo_ui_component_set_prop (window->details->shell_ui, 
				      item_path,
				      "pixtype",
				      file_name == NULL ? "stock" : "filename",
			      	      NULL);
	bonobo_ui_component_set_prop (window->details->shell_ui, 
				      item_path,
				      "pixname",
				      file_name == NULL ? icon_name : file_name,
			      	      NULL);

	g_free (file_name);

	nautilus_window_ui_thaw (window);
}

static GdkPixbuf *
get_pixbuf_for_xml_node (NautilusWindow *window, const char *node_path)
{
	BonoboUINode *node;
	GdkPixbuf *pixbuf;

	node = bonobo_ui_component_get_tree (window->details->shell_ui, node_path, FALSE, NULL);
	pixbuf = bonobo_ui_util_xml_get_icon_pixbuf (node, FALSE);
	bonobo_ui_node_free (node);

	return pixbuf;
}

/* Use only for toolbar buttons that had to be explicitly created so they
 * could have behaviors not present in standard Bonobo toolbar buttons.
 */
static void
set_up_special_bonobo_button (NautilusWindow *window,
			      BonoboUIToolbarButtonItem *item,
			      const char *control_path,
			      const char *icon_name)
{
	char *icon_file_name;
	GdkPixbuf *pixbuf;	

	icon_file_name = get_file_name_from_icon_name (icon_name, FALSE);

	if (icon_file_name == NULL) {
		pixbuf = get_pixbuf_for_xml_node (window, control_path);
	} else {
		pixbuf = gdk_pixbuf_new_from_file (icon_file_name);
		g_free (icon_file_name);
	}
	
	nautilus_window_ui_freeze (window);

	bonobo_ui_toolbar_button_item_set_icon (item, pixbuf);
	gdk_pixbuf_unref (pixbuf);

	/* FIXME bugzilla.eazel.com 5005:
	 * Setting the style here accounts for the preference, but does not
	 * account for a hard-wired toolbar style or later changes in style
	 * (such as if the toolbar is detached and made vertical). There
	 * is currently no Bonobo API to support matching the style properly.
	 */
	bonobo_ui_toolbar_item_set_style 
		(BONOBO_UI_TOOLBAR_ITEM (item),
		 gnome_preferences_get_toolbar_labels ()
		 	? BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_AND_TEXT_VERTICAL
		 	: BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_ONLY);

	nautilus_window_ui_thaw (window);
}			      


static void
set_up_toolbar_images (NautilusWindow *window)
{
	nautilus_window_ui_freeze (window);

	bonobo_ui_component_freeze (window->details->shell_ui, NULL);

	set_up_special_bonobo_button (window, window->details->back_button_item, "/Toolbar/BackWrapper", "Back");
	set_up_special_bonobo_button (window, window->details->forward_button_item, "/Toolbar/ForwardWrapper", "Forward");
	
	set_up_standard_bonobo_button (window, "/Toolbar/Up", "Up", FALSE);
	set_up_standard_bonobo_button (window, "/Toolbar/Home", "Home", FALSE);
	set_up_standard_bonobo_button (window, "/Toolbar/Reload", "Refresh", FALSE);
	set_up_standard_bonobo_button (window, "/Toolbar/Toggle Find Mode", "Search", FALSE);
	set_up_standard_bonobo_button (window, "/Toolbar/Go to Web Search", "SearchWeb", TRUE);
	set_up_standard_bonobo_button (window, "/Toolbar/Stop", "Stop", FALSE);
#ifdef EAZEL_SERVICES	
	set_up_standard_bonobo_button (window, "/Toolbar/Extra Buttons Placeholder/Services", "Services", TRUE);
#endif
	bonobo_ui_component_thaw (window->details->shell_ui, NULL);

	nautilus_window_ui_thaw (window);
}


/* handle theme changes */
static void
theme_changed_callback (gpointer callback_data)
{
	NautilusWindow *window;
	
	window = NAUTILUS_WINDOW (callback_data);
	
	set_up_toolbar_images (window);
	
	/* if the toolbar is visible, toggle it's visibility to force a relayout */
	if (nautilus_window_toolbar_showing (window)) {
		nautilus_window_hide_toolbar (window);
		nautilus_window_show_toolbar (window);
	}
}

static void
set_widget_for_bonobo_control (NautilusWindow *window,
			       GtkWidget *widget,
			       const char *control_path)
{
	BonoboControl *wrapper;

	wrapper = bonobo_control_new (widget);
	bonobo_ui_component_object_set (window->details->shell_ui,
					control_path,
					bonobo_object_corba_objref (BONOBO_OBJECT (wrapper)),
					NULL);
	bonobo_object_unref (BONOBO_OBJECT (wrapper));
}

static BonoboUIToolbarButtonItem *
set_up_back_or_forward_toolbar_item (NautilusWindow *window, 
				      const char *label, 
				      const char *control_path)
{
	BonoboUIToolbarButtonItem *item;
	GtkButton *button;

	item = BONOBO_UI_TOOLBAR_BUTTON_ITEM 
		(bonobo_ui_toolbar_button_item_new (NULL, label)); /* Fill in image later */
	gtk_widget_show (GTK_WIDGET (item));

	button = bonobo_ui_toolbar_button_item_get_button_widget (item);
	gtk_signal_connect (GTK_OBJECT (button),
			    "button_press_event",
			    GTK_SIGNAL_FUNC (back_or_forward_button_pressed_callback),
			    window);
	gtk_signal_connect (GTK_OBJECT (button),
			    "clicked",
			    GTK_SIGNAL_FUNC (back_or_forward_button_clicked_callback),
			    window);
	set_widget_for_bonobo_control (window, GTK_WIDGET (item), control_path);

	return item;
}
			       

void
nautilus_window_initialize_toolbars (NautilusWindow *window)
{
	CORBA_Environment ev;
	CORBA_exception_init (&ev);
	window->throbber = bonobo_get_object ("OAFIID:nautilus_throbber", "IDL:Bonobo/Control:1.0", &ev);
	
	
	if (BONOBO_EX (&ev)) {
		char *txt;
		g_warning ("Throbber Activation exception '%s'",
			   (txt = bonobo_exception_get_text (&ev)));
		g_free (txt);
		window->throbber = CORBA_OBJECT_NIL;
	}

	nautilus_window_ui_freeze (window);

	bonobo_ui_component_object_set (window->details->shell_ui,
					"/Toolbar/ThrobberWrapper",
					window->throbber,
					&ev);
	CORBA_exception_free (&ev);
	
	window->details->back_button_item = set_up_back_or_forward_toolbar_item 
		(window, _("Back"), "/Toolbar/BackWrapper");
	window->details->forward_button_item = set_up_back_or_forward_toolbar_item 
		(window, _("Forward"), "/Toolbar/ForwardWrapper");

	set_up_toolbar_images (window);

	nautilus_preferences_add_callback
		(NAUTILUS_PREFERENCES_THEME, 
		 theme_changed_callback,
		 window);

	nautilus_window_ui_thaw (window);
}
 
void
nautilus_window_toolbar_remove_theme_callback (NautilusWindow *window)
{
	nautilus_preferences_remove_callback
		(NAUTILUS_PREFERENCES_THEME,
		 theme_changed_callback,
		 window);
}