nautilus-window.c 69.7 KB
Newer Older
Darin Adler's avatar
Darin Adler committed
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4 5

/*
 *  Nautilus
 *
Alexander Larsson's avatar
Alexander Larsson committed
6
 *  Copyright (C) 1999, 2000, 2004 Red Hat, Inc.
7
 *  Copyright (C) 1999, 2000, 2001 Eazel, Inc.
8
 *
9
 *  Nautilus is free software; you can redistribute it and/or
10 11 12 13
 *  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.
 *
14
 *  Nautilus is distributed in the hope that it will be useful,
15 16 17 18 19
 *  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
20
 *  License along with this program; if not, write to the Free
21 22
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
23 24
 *  Authors: Elliot Lee <sopwith@redhat.com>
 *  	     John Sullivan <sullivan@eazel.com>
Alexander Larsson's avatar
Alexander Larsson committed
25
 *           Alexander Larsson <alexl@redhat.com>
26
 */
27

28
/* nautilus-window.c: Implementation of the main window object */
29

30
#include <config.h>
31

32
#include "nautilus-window-private.h"
33

Alexander Larsson's avatar
Alexander Larsson committed
34
#include "nautilus-actions.h"
35
#include "nautilus-bookmarks-window.h"
36
#include "nautilus-location-bar.h"
37
#include "nautilus-mime-actions.h"
38 39
#include "nautilus-notebook.h"
#include "nautilus-places-sidebar.h"
William Jon McCann's avatar
William Jon McCann committed
40
#include "nautilus-pathbar.h"
41
#include "nautilus-search-bar.h"
William Jon McCann's avatar
William Jon McCann committed
42
#include "nautilus-toolbar.h"
43
#include "nautilus-view-factory.h"
44
#include "nautilus-window-manage-views.h"
45
#include "nautilus-window-bookmarks.h"
46
#include "nautilus-window-slot.h"
47

Darin Adler's avatar
Darin Adler committed
48
#include <eel/eel-debug.h>
49
#include <eel/eel-gtk-extensions.h>
50
#include <eel/eel-string.h>
51
#include <gdk-pixbuf/gdk-pixbuf.h>
52
#include <gdk/gdkx.h>
Alexander Larsson's avatar
Alexander Larsson committed
53
#include <gdk/gdkkeysyms.h>
54
#include <gtk/gtk.h>
55
#include <glib/gi18n.h>
56 57 58
#ifdef HAVE_X11_XF86KEYSYM_H
#include <X11/XF86keysym.h>
#endif
59
#include <libnautilus-private/nautilus-file-utilities.h>
Alexander Larsson's avatar
Alexander Larsson committed
60
#include <libnautilus-private/nautilus-file-attributes.h>
61 62
#include <libnautilus-private/nautilus-global-preferences.h>
#include <libnautilus-private/nautilus-metadata.h>
Alexander Larsson's avatar
Alexander Larsson committed
63
#include <libnautilus-private/nautilus-clipboard.h>
64
#include <libnautilus-private/nautilus-search-directory.h>
Alexander Larsson's avatar
Alexander Larsson committed
65
#include <libnautilus-private/nautilus-signaller.h>
66 67 68 69

#define DEBUG_FLAG NAUTILUS_DEBUG_WINDOW
#include <libnautilus-private/nautilus-debug.h>

70
#include <math.h>
71
#include <sys/time.h>
72

73
/* dock items */
74
#define NAUTILUS_MENU_PATH_SHORT_LIST_PLACEHOLDER  	"/ViewMenu/View/View Choices/Short List"
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#define MAX_TITLE_LENGTH 180

/* Forward and back buttons on the mouse */
static gboolean mouse_extra_buttons = TRUE;
static int mouse_forward_button = 9;
static int mouse_back_button = 8;

static void mouse_back_button_changed		     (gpointer                  callback_data);
static void mouse_forward_button_changed	     (gpointer                  callback_data);
static void use_extra_mouse_buttons_changed          (gpointer                  callback_data);

/* Sanity check: highest mouse button value I could find was 14. 5 is our 
 * lower threshold (well-documented to be the one of the button events for the 
 * scrollwheel), so it's hardcoded in the functions below. However, if you have
 * a button that registers higher and want to map it, file a bug and 
 * we'll move the bar. Makes you wonder why the X guys don't have 
 * defined values for these like the XKB stuff, huh?
 */
#define UPPER_MOUSE_LIMIT 14

Elliot Lee's avatar
Elliot Lee committed
96
enum {
97
	PROP_DISABLE_CHROME = 1,
98
	NUM_PROPERTIES,
Elliot Lee's avatar
Elliot Lee committed
99 100
};

Alexander Larsson's avatar
Alexander Larsson committed
101 102
enum {
	GO_UP,
103 104
	RELOAD,
	PROMPT_FOR_LOCATION,
105 106
	LOADING_URI,
	HIDDEN_FILES_MODE_CHANGED,
107 108
	SLOT_ADDED,
	SLOT_REMOVED,
Alexander Larsson's avatar
Alexander Larsson committed
109 110 111 112
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };
113
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
Alexander Larsson's avatar
Alexander Larsson committed
114 115 116 117 118 119

typedef struct  {
	NautilusWindow *window;
	char *id;
} ActivateViewData;

120
static void cancel_view_as_callback         (NautilusWindowSlot      *slot);
Alexander Larsson's avatar
Alexander Larsson committed
121 122
static void action_view_as_callback         (GtkAction               *action,
					     ActivateViewData        *data);
123

124
G_DEFINE_TYPE (NautilusWindow, nautilus_window, GTK_TYPE_APPLICATION_WINDOW);
Alexander Larsson's avatar
Alexander Larsson committed
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
static const struct {
	unsigned int keyval;
	const char *action;
} extra_window_keybindings [] = {
#ifdef HAVE_X11_XF86KEYSYM_H
	{ XF86XK_AddFavorite,	NAUTILUS_ACTION_ADD_BOOKMARK },
	{ XF86XK_Favorites,	NAUTILUS_ACTION_EDIT_BOOKMARKS },
	{ XF86XK_Go,		NAUTILUS_ACTION_GO_TO_LOCATION },
	{ XF86XK_HomePage,      NAUTILUS_ACTION_GO_HOME },
	{ XF86XK_OpenURL,	NAUTILUS_ACTION_GO_TO_LOCATION },
	{ XF86XK_Refresh,	NAUTILUS_ACTION_RELOAD },
	{ XF86XK_Reload,	NAUTILUS_ACTION_RELOAD },
	{ XF86XK_Search,	NAUTILUS_ACTION_SEARCH },
	{ XF86XK_Start,		NAUTILUS_ACTION_GO_HOME },
	{ XF86XK_Stop,		NAUTILUS_ACTION_STOP },
	{ XF86XK_ZoomIn,	NAUTILUS_ACTION_ZOOM_IN },
142 143 144 145
	{ XF86XK_ZoomOut,	NAUTILUS_ACTION_ZOOM_OUT },
	{ XF86XK_Back,		NAUTILUS_ACTION_BACK },
	{ XF86XK_Forward,	NAUTILUS_ACTION_FORWARD }

146 147 148
#endif
};

149
void
Alexander Larsson's avatar
Alexander Larsson committed
150
nautilus_window_go_to (NautilusWindow *window, GFile *location)
151
{
152 153
	g_return_if_fail (NAUTILUS_IS_WINDOW (window));

154 155
	nautilus_window_slot_open_location (nautilus_window_get_active_slot (window),
					    location, 0);
156 157
}

158 159 160 161 162 163 164 165
void
nautilus_window_go_to_full (NautilusWindow *window,
			    GFile          *location,
			    NautilusWindowGoToCallback callback,
			    gpointer        user_data)
{
	g_return_if_fail (NAUTILUS_IS_WINDOW (window));

166 167
	nautilus_window_slot_open_location_full (nautilus_window_get_active_slot (window),
						 location, 0, NULL, callback, user_data);
168 169
}

170 171
static void
nautilus_window_go_up_signal (NautilusWindow *window)
Alexander Larsson's avatar
Alexander Larsson committed
172
{
173
	nautilus_window_slot_go_up (nautilus_window_get_active_slot (window), 0);
Alexander Larsson's avatar
Alexander Larsson committed
174 175
}

William Jon McCann's avatar
William Jon McCann committed
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
static int
bookmark_list_get_uri_index (GList *list, GFile *location)
{
	NautilusBookmark *bookmark;
	GList *l;
	GFile *tmp;
	int i;

	g_return_val_if_fail (location != NULL, -1);

	for (i = 0, l = list; l != NULL; i++, l = l->next) {
		bookmark = NAUTILUS_BOOKMARK (l->data);

		tmp = nautilus_bookmark_get_location (bookmark);
		if (g_file_equal (location, tmp)) {
			g_object_unref (tmp);
			return i;
		}
		g_object_unref (tmp);
	}

	return -1;
}

static void
search_bar_activate_callback (NautilusSearchBar *bar,
			      NautilusWindow    *window)
{
	char *uri, *current_uri;
	NautilusDirectory *directory;
	NautilusSearchDirectory *search_directory;
	NautilusQuery *query;
	GFile *location;
	GtkWidget *search_bar;

	uri = nautilus_search_directory_generate_new_uri ();
	location = g_file_new_for_uri (uri);

	directory = nautilus_directory_get (location);
	g_assert (NAUTILUS_IS_SEARCH_DIRECTORY (directory));

	search_directory = NAUTILUS_SEARCH_DIRECTORY (directory);

	search_bar = nautilus_toolbar_get_search_bar (NAUTILUS_TOOLBAR (window->details->toolbar));

	query = nautilus_search_bar_get_query (NAUTILUS_SEARCH_BAR (search_bar));

	if (query != NULL) {
		current_uri = nautilus_window_slot_get_location_uri (window->details->active_slot);

		nautilus_query_set_location (query, current_uri);
		nautilus_search_directory_set_query (search_directory, query);

		g_free (current_uri);
		g_object_unref (query);
	}

	nautilus_window_slot_open_location (window->details->active_slot, location, 0);

	nautilus_directory_unref (directory);
	g_object_unref (location);
	g_free (uri);
}

static void
nautilus_window_hide_temporary_bars (NautilusWindow *window)
{
	NautilusWindowSlot *slot;
	NautilusDirectory *directory;

	slot = window->details->active_slot;

	if (window->details->temporary_navigation_bar) {
		directory = nautilus_directory_get (slot->location);

		window->details->temporary_navigation_bar = FALSE;

		/* if we're in a search directory, hide the main bar, and show the search
		 * bar again; otherwise, just hide the whole toolbar.
		 */
		if (NAUTILUS_IS_SEARCH_DIRECTORY (directory)) {
			nautilus_toolbar_set_show_main_bar (NAUTILUS_TOOLBAR (window->details->toolbar), FALSE);
			nautilus_toolbar_set_show_search_bar (NAUTILUS_TOOLBAR (window->details->toolbar), TRUE);
		} else {
			gtk_widget_hide (window->details->toolbar);
		}

		nautilus_directory_unref (directory);
	}
}

static void
search_bar_cancel_callback (GtkWidget      *widget,
			    NautilusWindow *window)
{
	GtkAction *search;

	search = gtk_action_group_get_action (window->details->toolbar_action_group,
					      NAUTILUS_ACTION_SEARCH);

	gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (search), FALSE);
}

static void
unset_focus_widget (NautilusWindow *window)
{
	if (window->details->last_focus_widget != NULL) {
		g_object_remove_weak_pointer (G_OBJECT (window->details->last_focus_widget),
					      (gpointer *) &window->details->last_focus_widget);
		window->details->last_focus_widget = NULL;
	}
}

static gboolean
widget_is_in_temporary_bars (GtkWidget      *widget,
			     NautilusWindow *window)
{
	gboolean res = FALSE;

	if ((gtk_widget_get_ancestor (widget, NAUTILUS_TYPE_LOCATION_BAR) != NULL &&
	     window->details->temporary_navigation_bar) ||
	    (gtk_widget_get_ancestor (widget, NAUTILUS_TYPE_SEARCH_BAR) != NULL &&
	     window->details->temporary_search_bar))
		res = TRUE;

	return res;
}

static void
remember_focus_widget (NautilusWindow *window)
{
	GtkWidget *focus_widget;

	focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
	if (focus_widget != NULL &&
	    !widget_is_in_temporary_bars (focus_widget, window)) {
		unset_focus_widget (window);

		window->details->last_focus_widget = focus_widget;
		g_object_add_weak_pointer (G_OBJECT (focus_widget),
					   (gpointer *) &(window->details->last_focus_widget));
	}
}

void
nautilus_window_grab_focus (NautilusWindow *window)
{
	if (NAUTILUS_IS_WINDOW (window) && window->details->active_slot) {
		nautilus_view_grab_focus (window->details->active_slot->content_view);
	}
}

static void
restore_focus_widget (NautilusWindow *window)
{
	if (window->details->last_focus_widget != NULL) {
		if (NAUTILUS_IS_VIEW (window->details->last_focus_widget)) {
			nautilus_view_grab_focus (NAUTILUS_VIEW (window->details->last_focus_widget));
		} else {
			gtk_widget_grab_focus (window->details->last_focus_widget);
		}

		unset_focus_widget (window);
	}
}

static void
navigation_bar_cancel_callback (GtkWidget      *widget,
				NautilusWindow *window)
{
	nautilus_toolbar_set_show_location_entry (NAUTILUS_TOOLBAR (window->details->toolbar), FALSE);

	nautilus_window_hide_temporary_bars (window);
	restore_focus_widget (window);
}

static void
nautilus_window_ensure_search_bar (NautilusWindow *window)
{
	GtkWidget *search_bar;

	remember_focus_widget (window);
	nautilus_toolbar_set_show_search_bar (NAUTILUS_TOOLBAR (window->details->toolbar), TRUE);
	search_bar = nautilus_toolbar_get_search_bar (NAUTILUS_TOOLBAR (window->details->toolbar));
	nautilus_search_bar_grab_focus (NAUTILUS_SEARCH_BAR (search_bar));
}

static void
nautilus_window_hide_search_bar (NautilusWindow *window)
{
	nautilus_toolbar_set_show_search_bar (NAUTILUS_TOOLBAR (window->details->toolbar), FALSE);
	restore_focus_widget (window);

	if (window->details->temporary_search_bar) {
		window->details->temporary_search_bar = FALSE;

		gtk_widget_hide (window->details->toolbar);
	}
}

static void
navigation_bar_location_changed_callback (GtkWidget      *widget,
					  GFile          *location,
					  NautilusWindow *window)
{
	nautilus_toolbar_set_show_location_entry (NAUTILUS_TOOLBAR (window->details->toolbar), FALSE);
	nautilus_window_hide_search_bar (window);
	nautilus_window_hide_temporary_bars (window);

	restore_focus_widget (window);

	nautilus_window_slot_open_location (window->details->active_slot, location, 0);
}

static gboolean
notebook_switch_page_cb (GtkNotebook    *notebook,
			 GtkWidget      *page,
			 unsigned int    page_num,
			 NautilusWindow *window)
{
	NautilusWindowSlot *slot;
	GtkWidget *widget;

	widget = gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->details->notebook), page_num);
	g_assert (widget != NULL);

	/* find slot corresponding to the target page */
	slot = NAUTILUS_WINDOW_SLOT (widget);
	g_assert (slot != NULL);

	nautilus_window_set_active_slot (nautilus_window_slot_get_window (slot),
					 slot);

	return FALSE;
}

static void
action_show_hide_search_callback (GtkAction *action,
				  gpointer user_data)
{
	NautilusWindow *window = user_data;

	if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
		nautilus_window_ensure_search_bar (window);
	} else {
		NautilusWindowSlot *slot;
		GFile *location = NULL;

		slot = window->details->active_slot;
		nautilus_window_hide_search_bar (window);

		/* Use the location bar as the return location */
		if (slot->query_editor != NULL) {
			NautilusQuery *query;
			char *uri;

			query = nautilus_query_editor_get_query (slot->query_editor);
			if (query != NULL) {
				uri = nautilus_query_get_location (query);
				if (uri != NULL) {
					location = g_file_new_for_uri (uri);
					g_free (uri);
				}
				g_object_unref (query);
			}

			/* Last try: use the home directory as the return location */
			if (location == NULL) {
				location = g_file_new_for_path (g_get_home_dir ());
			}

			nautilus_window_go_to (window, location);
			g_object_unref (location);
		}
	}
}

static void
close_slot (NautilusWindow     *window,
	    NautilusWindowSlot *slot)
{
	int page_num;
	GtkNotebook *notebook;

	g_assert (NAUTILUS_IS_WINDOW_SLOT (slot));

	DEBUG ("Closing slot %p", slot);

464 465
	g_signal_emit (window, signals[SLOT_REMOVED], 0, slot);

William Jon McCann's avatar
William Jon McCann committed
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
	nautilus_window_manage_views_close_slot (slot);

	notebook = GTK_NOTEBOOK (window->details->notebook);
	page_num = gtk_notebook_page_num (notebook, GTK_WIDGET (slot));
	g_assert (page_num >= 0);

	g_signal_handlers_block_by_func (notebook,
					 G_CALLBACK (notebook_switch_page_cb),
					 window);
	/* this will call gtk_widget_destroy on the slot */
	gtk_notebook_remove_page (notebook, page_num);
	g_signal_handlers_unblock_by_func (notebook,
					   G_CALLBACK (notebook_switch_page_cb),
					   window);

	gtk_notebook_set_show_tabs (notebook,
				    gtk_notebook_get_n_pages (notebook) > 1);
	window->details->slots = g_list_remove (window->details->slots, slot);
}

NautilusWindowSlot *
nautilus_window_open_slot (NautilusWindow             *window,
			   NautilusWindowOpenSlotFlags flags)
{
	NautilusWindowSlot *slot;

	g_assert (NAUTILUS_IS_WINDOW (window));

	slot = nautilus_window_slot_new (window);

	g_signal_handlers_block_by_func (window->details->notebook,
					 G_CALLBACK (notebook_switch_page_cb),
					 window);
	nautilus_notebook_add_tab (NAUTILUS_NOTEBOOK (window->details->notebook),
				   slot,
				   (flags & NAUTILUS_WINDOW_OPEN_SLOT_APPEND) != 0 ?
				   -1 :
				   gtk_notebook_get_current_page (GTK_NOTEBOOK (window->details->notebook)) + 1,
				   FALSE);
	g_signal_handlers_unblock_by_func (window->details->notebook,
					   G_CALLBACK (notebook_switch_page_cb),
					   window);

	window->details->slots = g_list_append (window->details->slots, slot);
510
	g_signal_emit (window, signals[SLOT_ADDED], 0, slot);
William Jon McCann's avatar
William Jon McCann committed
511 512 513 514

	return slot;
}

515 516 517 518 519 520 521 522 523 524
void
nautilus_window_new_tab (NautilusWindow *window)
{
	NautilusWindowSlot *current_slot;
	NautilusWindowSlot *new_slot;
	NautilusWindowOpenFlags flags;
	GFile *location;
	int new_slot_position;
	char *scheme;

525
	current_slot = nautilus_window_get_active_slot (window);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	location = nautilus_window_slot_get_location (current_slot);

	if (location != NULL) {
		flags = 0;

		new_slot_position = g_settings_get_enum (nautilus_preferences, NAUTILUS_PREFERENCES_NEW_TAB_POSITION);
		if (new_slot_position == NAUTILUS_NEW_TAB_POSITION_END) {
			flags = NAUTILUS_WINDOW_OPEN_SLOT_APPEND;
		}

		scheme = g_file_get_uri_scheme (location);
		if (!strcmp (scheme, "x-nautilus-search")) {
			g_object_unref (location);
			location = g_file_new_for_path (g_get_home_dir ());
		}
		g_free (scheme);

William Jon McCann's avatar
William Jon McCann committed
543
		new_slot = nautilus_window_open_slot (window, flags);
544
		nautilus_window_set_active_slot (window, new_slot);
545
		nautilus_window_slot_open_location (new_slot, location, 0);
546 547 548 549
		g_object_unref (location);
	}
}

550 551 552
static void
update_cursor (NautilusWindow *window)
{
553
	NautilusWindowSlot *slot;
554 555
	GdkCursor *cursor;

556
	slot = nautilus_window_get_active_slot (window);
557 558

	if (slot->allow_stop) {
559
		cursor = gdk_cursor_new (GDK_WATCH);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
560
		gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (window)), cursor);
561
		g_object_unref (cursor);
562
	} else {
Cosimo Cecchi's avatar
Cosimo Cecchi committed
563
		gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (window)), NULL);
564 565 566
	}
}

567
void
568 569
nautilus_window_sync_allow_stop (NautilusWindow *window,
				 NautilusWindowSlot *slot)
570
{
571 572
	GtkAction *stop_action;
	GtkAction *reload_action;
573
	gboolean allow_stop, slot_is_active;
574

575 576 577 578 579
	stop_action = gtk_action_group_get_action (nautilus_window_get_main_action_group (window),
						   NAUTILUS_ACTION_STOP);
	reload_action = gtk_action_group_get_action (nautilus_window_get_main_action_group (window),
						     NAUTILUS_ACTION_RELOAD);
	allow_stop = gtk_action_get_sensitive (stop_action);
580

581 582 583
	slot_is_active = (slot == nautilus_window_get_active_slot (window));

	if (!slot_is_active ||
584
	    allow_stop != slot->allow_stop) {
585
		if (slot_is_active) {
586 587
			gtk_action_set_visible (stop_action, slot->allow_stop);
			gtk_action_set_visible (reload_action, !slot->allow_stop);
588
		}
589

590
		if (gtk_widget_get_realized (GTK_WIDGET (window))) {
591 592
			update_cursor (window);
		}
593

William Jon McCann's avatar
William Jon McCann committed
594
		nautilus_notebook_sync_loading (NAUTILUS_NOTEBOOK (window->details->notebook), slot);
595 596 597
	}
}

598
static void
599 600
nautilus_window_prompt_for_location (NautilusWindow *window,
				     const char     *initial)
William Jon McCann's avatar
William Jon McCann committed
601 602
{
	GtkWidget *bar;
603

604
	g_return_if_fail (NAUTILUS_IS_WINDOW (window));
605

William Jon McCann's avatar
William Jon McCann committed
606
	bar = nautilus_window_ensure_location_bar (window);
607 608

	if (initial) {
William Jon McCann's avatar
William Jon McCann committed
609
		nautilus_location_bar_set_location (NAUTILUS_LOCATION_BAR (bar),
610 611
						    initial);
	}
612 613
}

614 615 616 617
/* Code should never force the window taller than this size.
 * (The user can still stretch the window taller if desired).
 */
static guint
618
get_max_forced_height (GdkScreen *screen)
619
{
620
	return (gdk_screen_get_height (screen) * 90) / 100;
621 622 623 624 625 626
}

/* Code should never force the window wider than this size.
 * (The user can still stretch the window wider if desired).
 */
static guint
627
get_max_forced_width (GdkScreen *screen)
628
{
629
	return (gdk_screen_get_width (screen) * 90) / 100;
630 631
}

Alexander Larsson's avatar
Alexander Larsson committed
632 633 634 635
/* This must be called when construction of NautilusWindow is finished,
 * since it depends on the type of the argument, which isn't decided at
 * construction time.
 */
636
static void
Alexander Larsson's avatar
Alexander Larsson committed
637
nautilus_window_set_initial_window_geometry (NautilusWindow *window)
638
{
639
	GdkScreen *screen;
640
	guint max_width_for_screen, max_height_for_screen;
641
	guint default_width, default_height;
642

643 644 645 646
	screen = gtk_window_get_screen (GTK_WINDOW (window));
	
	max_width_for_screen = get_max_forced_width (screen);
	max_height_for_screen = get_max_forced_height (screen);
647 648 649
	
	default_width = NAUTILUS_WINDOW_DEFAULT_WIDTH;
	default_height = NAUTILUS_WINDOW_DEFAULT_HEIGHT;
Alexander Larsson's avatar
Alexander Larsson committed
650

651
	gtk_window_set_default_size (GTK_WINDOW (window), 
652
				     MIN (default_width, 
653
				          max_width_for_screen), 
654
				     MIN (default_height, 
655 656 657
				          max_height_for_screen));
}

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
static gboolean
save_sidebar_width_cb (gpointer user_data)
{
	NautilusWindow *window = user_data;

	window->details->sidebar_width_handler_id = 0;

	DEBUG ("Saving sidebar width: %d", window->details->side_pane_width);

	g_settings_set_int (nautilus_window_state,
			    NAUTILUS_WINDOW_STATE_SIDEBAR_WIDTH,
			    window->details->side_pane_width);

	return FALSE;
}
673 674 675 676 677 678 679 680 681 682 683

/* side pane helpers */
static void
side_pane_size_allocate_callback (GtkWidget *widget,
				  GtkAllocation *allocation,
				  gpointer user_data)
{
	NautilusWindow *window;

	window = user_data;

684 685 686 687 688 689 690
	if (window->details->sidebar_width_handler_id != 0) {
		g_source_remove (window->details->sidebar_width_handler_id);
		window->details->sidebar_width_handler_id = 0;
	}

	if (allocation->width != window->details->side_pane_width &&
	    allocation->width > 1) {
691 692
		window->details->side_pane_width = allocation->width;

693 694
		window->details->sidebar_width_handler_id =
			g_idle_add (save_sidebar_width_cb, window);
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	}
}

static void
setup_side_pane_width (NautilusWindow *window)
{
	g_return_if_fail (window->details->sidebar != NULL);

	window->details->side_pane_width =
		g_settings_get_int (nautilus_window_state,
				    NAUTILUS_WINDOW_STATE_SIDEBAR_WIDTH);

	gtk_paned_set_position (GTK_PANED (window->details->content_paned),
				window->details->side_pane_width);
}

static void
nautilus_window_set_up_sidebar (NautilusWindow *window)
{
	GtkWidget *sidebar;

	window->details->sidebar = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
717 718
	gtk_style_context_add_class (gtk_widget_get_style_context (window->details->sidebar),
				     GTK_STYLE_CLASS_SIDEBAR);
719 720 721 722 723 724 725 726 727 728 729

	gtk_paned_pack1 (GTK_PANED (window->details->content_paned),
			 GTK_WIDGET (window->details->sidebar),
			 FALSE, FALSE);

	setup_side_pane_width (window);
	g_signal_connect (window->details->sidebar, 
			  "size_allocate",
			  G_CALLBACK (side_pane_size_allocate_callback),
			  window);

William Jon McCann's avatar
William Jon McCann committed
730
	sidebar = nautilus_places_sidebar_new (window);
731 732 733 734 735 736 737 738 739 740
	gtk_box_pack_start (GTK_BOX (window->details->sidebar), sidebar, TRUE, TRUE, 0);
	gtk_widget_show (sidebar);
	gtk_widget_show (GTK_WIDGET (window->details->sidebar));
}

static void
nautilus_window_tear_down_sidebar (NautilusWindow *window)
{
	DEBUG ("Destroying sidebar");

741 742 743 744
	if (window->details->sidebar != NULL) {
		gtk_widget_destroy (GTK_WIDGET (window->details->sidebar));
		window->details->sidebar = NULL;
	}
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
}

void
nautilus_window_hide_sidebar (NautilusWindow *window)
{
	DEBUG ("Called hide_sidebar()");

	if (window->details->sidebar == NULL) {
		return;
	}

	nautilus_window_tear_down_sidebar (window);
	g_settings_set_boolean (nautilus_window_state, NAUTILUS_WINDOW_STATE_START_WITH_SIDEBAR, FALSE);
}

void
nautilus_window_show_sidebar (NautilusWindow *window)
{
	DEBUG ("Called show_sidebar()");

	if (window->details->sidebar != NULL) {
		return;
	}

	if (window->details->disable_chrome) {
		return;
	}

	nautilus_window_set_up_sidebar (window);
	g_settings_set_boolean (nautilus_window_state, NAUTILUS_WINDOW_STATE_START_WITH_SIDEBAR, TRUE);
}

gboolean
nautilus_window_disable_chrome_mapping (GValue *value,
					GVariant *variant,
					gpointer user_data)
{
	NautilusWindow *window = user_data;

	g_value_set_boolean (value,
			     g_variant_get_boolean (variant) &&
			     !window->details->disable_chrome);

	return TRUE;
}

William Jon McCann's avatar
William Jon McCann committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
static inline NautilusWindowSlot *
get_first_inactive_slot (NautilusWindow *window)
{
	GList *l;
	NautilusWindowSlot *slot;

	for (l = window->details->slots; l != NULL; l = l->next) {
		slot = NAUTILUS_WINDOW_SLOT (l->data);
		if (slot != window->details->active_slot) {
			return slot;
		}
	}

	return NULL;
}

void
nautilus_window_slot_close (NautilusWindow     *window,
			    NautilusWindowSlot *slot)
{
	NautilusWindowSlot *next_slot;

	DEBUG ("Requesting to remove slot %p from window %p", slot, window);
	if (window == NULL)
		return;

	if (window->details->active_slot == slot) {
		next_slot = get_first_inactive_slot (window);
		nautilus_window_set_active_slot (window, next_slot);
	}

	close_slot (window, slot);

	/* If that was the last slot in the window, close the window. */
	if (window->details->slots == NULL) {
		DEBUG ("Last slot removed, closing the window");
		nautilus_window_close (window);
	}
}

static void
toggle_toolbar_search_button (NautilusWindow *window)
{
	GtkAction *action;

	action = gtk_action_group_get_action (window->details->toolbar_action_group, NAUTILUS_ACTION_SEARCH);

	g_signal_handlers_block_by_func (action,
					 action_show_hide_search_callback, window);
	gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
	g_signal_handlers_unblock_by_func (action,
					   action_show_hide_search_callback, window);
}

void
nautilus_window_sync_search_widgets (NautilusWindow *window)
{
	NautilusDirectory *directory;
	NautilusSearchDirectory *search_directory;

	search_directory = NULL;

	directory = nautilus_directory_get (window->details->active_slot->location);
	if (NAUTILUS_IS_SEARCH_DIRECTORY (directory)) {
		search_directory = NAUTILUS_SEARCH_DIRECTORY (directory);
	}

	if (search_directory != NULL) {
		if (!nautilus_search_directory_is_saved_search (search_directory)) {
			nautilus_toolbar_set_show_search_bar (NAUTILUS_TOOLBAR (window->details->toolbar), TRUE);
			window->details->temporary_search_bar = FALSE;
		} else {
			toggle_toolbar_search_button (window);
		}
	} else {
		GtkWidget *search_bar;

		search_bar = nautilus_toolbar_get_search_bar (NAUTILUS_TOOLBAR (window->details->toolbar));
		search_bar_cancel_callback (search_bar, window);
	}

	nautilus_directory_unref (directory);
}

void
nautilus_window_sync_location_widgets (NautilusWindow *window)
{
	NautilusWindowSlot *slot, *active_slot;
	NautilusNavigationState *nav_state;

	slot = window->details->active_slot;

	nautilus_window_hide_temporary_bars (window);

	/* Change the location bar and path bar to match the current location. */
	if (slot->location != NULL) {
		char *uri;
		GtkWidget *location_bar;
		GtkWidget *path_bar;

		location_bar = nautilus_toolbar_get_location_bar (NAUTILUS_TOOLBAR (window->details->toolbar));
		path_bar = nautilus_toolbar_get_path_bar (NAUTILUS_TOOLBAR (window->details->toolbar));

		/* this may be NULL if we just created the slot */
		uri = nautilus_window_slot_get_location_uri (slot);
		nautilus_location_bar_set_location (NAUTILUS_LOCATION_BAR (location_bar), uri);
		g_free (uri);
		nautilus_path_bar_set_path (NAUTILUS_PATH_BAR (path_bar), slot->location);
	}

	nautilus_window_sync_up_button (window);

	/* Check if the back and forward buttons need enabling or disabling. */
	active_slot = nautilus_window_get_active_slot (window);
	nav_state = nautilus_window_get_navigation_state (window);

	nautilus_navigation_state_set_boolean (nav_state,
					       NAUTILUS_ACTION_BACK,
					       active_slot->back_list != NULL);
	nautilus_navigation_state_set_boolean (nav_state,
					       NAUTILUS_ACTION_FORWARD,
					       active_slot->forward_list != NULL);
}

GtkWidget *
nautilus_window_ensure_location_bar (NautilusWindow *window)
{
	GtkWidget *location_bar;

	remember_focus_widget (window);

	nautilus_toolbar_set_show_main_bar (NAUTILUS_TOOLBAR (window->details->toolbar), TRUE);
	nautilus_toolbar_set_show_location_entry (NAUTILUS_TOOLBAR (window->details->toolbar), TRUE);

	location_bar = nautilus_toolbar_get_location_bar (NAUTILUS_TOOLBAR (window->details->toolbar));
	nautilus_location_bar_activate (NAUTILUS_LOCATION_BAR (location_bar));

	return location_bar;
}

static void
path_bar_location_changed_callback (GtkWidget      *widget,
				    GFile          *location,
				    NautilusWindow *window)
{
	NautilusWindowSlot *slot;
	int i;

	slot = window->details->active_slot;
	/* check whether we already visited the target location */
	i = bookmark_list_get_uri_index (slot->back_list, location);
	if (i >= 0) {
		nautilus_window_back_or_forward (window, TRUE, i, 0);
	} else {
		nautilus_window_slot_open_location (slot, location, 0);
	}
}

static void
path_bar_path_event_callback (NautilusPathBar *path_bar,
			      GFile           *location,
			      GdkEventButton  *event,
			      NautilusWindow  *window)
{
	NautilusWindowSlot *slot;
	NautilusWindowOpenFlags flags;
	int mask;
	NautilusView *view;
	char *uri;

	if (event->type == GDK_BUTTON_RELEASE) {
		mask = event->state & gtk_accelerator_get_default_mod_mask ();
		flags = 0;

		if (event->button == 2 && mask == 0) {
			flags = NAUTILUS_WINDOW_OPEN_FLAG_NEW_TAB;
		} else if (event->button == 1 && mask == GDK_CONTROL_MASK) {
			flags = NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW;
		}

		if (flags != 0) {
			slot = nautilus_window_get_active_slot (window);
			nautilus_window_slot_open_location (slot, location, flags);
		}
	} else if (event->button == 3) {
		slot = nautilus_window_get_active_slot (window);
		view = slot->content_view;
		if (view != NULL) {
			uri = g_file_get_uri (location);
			nautilus_view_pop_up_location_context_menu (view, event, uri);
			g_free (uri);
		}
	}
}

static void
notebook_popup_menu_new_tab_cb (GtkMenuItem *menuitem,
				gpointer user_data)
{
	NautilusWindow *window = user_data;

	nautilus_window_new_tab (window);
}

static void
notebook_popup_menu_move_left_cb (GtkMenuItem *menuitem,
				  gpointer user_data)
{
	NautilusWindow *window = user_data;

	nautilus_notebook_reorder_current_child_relative (NAUTILUS_NOTEBOOK (window->details->notebook), -1);
}

static void
notebook_popup_menu_move_right_cb (GtkMenuItem *menuitem,
				   gpointer user_data)
{
	NautilusWindow *window = user_data;

	nautilus_notebook_reorder_current_child_relative (NAUTILUS_NOTEBOOK (window->details->notebook), 1);
}

static void
notebook_popup_menu_close_cb (GtkMenuItem *menuitem,
			      gpointer user_data)
{
	NautilusWindow *window = user_data;
	NautilusWindowSlot *slot;

	slot = window->details->active_slot;
	nautilus_window_slot_close (window, slot);
}

static void
notebook_popup_menu_show (NautilusWindow *window,
			  GdkEventButton *event)
{
	GtkWidget *popup;
	GtkWidget *item;
	GtkWidget *image;
	int button, event_time;
	gboolean can_move_left, can_move_right;
	NautilusNotebook *notebook;

	notebook = NAUTILUS_NOTEBOOK (window->details->notebook);

	can_move_left = nautilus_notebook_can_reorder_current_child_relative (notebook, -1);
	can_move_right = nautilus_notebook_can_reorder_current_child_relative (notebook, 1);

	popup = gtk_menu_new ();

	item = gtk_menu_item_new_with_mnemonic (_("_New Tab"));
	g_signal_connect (item, "activate",
			  G_CALLBACK (notebook_popup_menu_new_tab_cb),
			  window);
	gtk_menu_shell_append (GTK_MENU_SHELL (popup),
			       item);

	gtk_menu_shell_append (GTK_MENU_SHELL (popup),
			       gtk_separator_menu_item_new ());

	item = gtk_menu_item_new_with_mnemonic (_("Move Tab _Left"));
	g_signal_connect (item, "activate",
			  G_CALLBACK (notebook_popup_menu_move_left_cb),
			  window);
	gtk_menu_shell_append (GTK_MENU_SHELL (popup),
			       item);
	gtk_widget_set_sensitive (item, can_move_left);

	item = gtk_menu_item_new_with_mnemonic (_("Move Tab _Right"));
	g_signal_connect (item, "activate",
			  G_CALLBACK (notebook_popup_menu_move_right_cb),
			  window);
	gtk_menu_shell_append (GTK_MENU_SHELL (popup),
			       item);
	gtk_widget_set_sensitive (item, can_move_right);

	gtk_menu_shell_append (GTK_MENU_SHELL (popup),
			       gtk_separator_menu_item_new ());

	item = gtk_image_menu_item_new_with_mnemonic (_("_Close Tab"));
	image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
	g_signal_connect (item, "activate",
			  G_CALLBACK (notebook_popup_menu_close_cb), window);
	gtk_menu_shell_append (GTK_MENU_SHELL (popup),
			       item);

	gtk_widget_show_all (popup);

	if (event) {
		button = event->button;
		event_time = event->time;
	} else {
		button = 0;
		event_time = gtk_get_current_event_time ();
	}

	/* TODO is this correct? */
	gtk_menu_attach_to_widget (GTK_MENU (popup),
				   window->details->notebook,
				   NULL);

	gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL,
			button, event_time);
}

/* emitted when the user clicks the "close" button of tabs */
static void
notebook_tab_close_requested (NautilusNotebook   *notebook,
			      NautilusWindowSlot *slot,
			      NautilusWindow     *window)
{
	nautilus_window_slot_close (window, slot);
}

static gboolean
notebook_button_press_cb (GtkWidget *widget,
			  GdkEventButton *event,
			  gpointer user_data)
{
	NautilusWindow *window = user_data;

	if (GDK_BUTTON_PRESS == event->type && 3 == event->button) {
		notebook_popup_menu_show (window, event);
		return TRUE;
	}

	return FALSE;
}

static gboolean
notebook_popup_menu_cb (GtkWidget *widget,
			gpointer user_data)
{
	NautilusWindow *window = user_data;
	notebook_popup_menu_show (window, NULL);
	return TRUE;
}

static GtkWidget *
create_toolbar (NautilusWindow *window)
{
	GtkSizeGroup *header_size_group;
	GtkActionGroup *action_group;
	GtkAction *action;
	GtkWidget *toolbar;
	GtkWidget *path_bar;
	GtkWidget *location_bar;
	GtkWidget *search_bar;

	header_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
	gtk_size_group_set_ignore_hidden (header_size_group, FALSE);

	/* build the toolbar */
	action_group = nautilus_window_create_toolbar_action_group (window);
	window->details->toolbar_action_group = action_group;
	action = gtk_action_group_get_action (action_group, NAUTILUS_ACTION_SEARCH);
	g_signal_connect (action, "activate",
			  G_CALLBACK (action_show_hide_search_callback), window);
	nautilus_navigation_state_set_master (window->details->nav_state, window->details->toolbar_action_group );

1153
	toolbar = nautilus_toolbar_new (nautilus_window_get_ui_manager (NAUTILUS_WINDOW (window)), action_group);
William Jon McCann's avatar
William Jon McCann committed
1154

1155 1156 1157 1158
	g_object_bind_property (window, "disable-chrome",
				toolbar, "visible",
				G_BINDING_INVERT_BOOLEAN);

William Jon McCann's avatar
William Jon McCann committed
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	/* connect to the pathbar signals */
	path_bar = nautilus_toolbar_get_path_bar (NAUTILUS_TOOLBAR (toolbar));
	gtk_size_group_add_widget (header_size_group, path_bar);

	g_signal_connect_object (path_bar, "path-clicked",
				 G_CALLBACK (path_bar_location_changed_callback), window, 0);
	g_signal_connect_object (path_bar, "path-event",
				 G_CALLBACK (path_bar_path_event_callback), window, 0);

	/* connect to the location bar signals */
	location_bar = nautilus_toolbar_get_location_bar (NAUTILUS_TOOLBAR (toolbar));
	gtk_size_group_add_widget (header_size_group, location_bar);

	nautilus_clipboard_set_up_editable
		(GTK_EDITABLE (nautilus_location_bar_get_entry (NAUTILUS_LOCATION_BAR (location_bar))),
		 nautilus_window_get_ui_manager (NAUTILUS_WINDOW (window)),
		 TRUE);

	g_signal_connect_object (location_bar, "location-changed",
				 G_CALLBACK (navigation_bar_location_changed_callback), window, 0);
	g_signal_connect_object (location_bar, "cancel",
				 G_CALLBACK (navigation_bar_cancel_callback), window, 0);

	/* connect to the search bar signals */
	search_bar = nautilus_toolbar_get_search_bar (NAUTILUS_TOOLBAR (toolbar));

	g_signal_connect_object (search_bar, "activate",
				 G_CALLBACK (search_bar_activate_callback), window, 0);
	g_signal_connect_object (search_bar, "cancel",
				 G_CALLBACK (search_bar_cancel_callback), window, 0);

	g_object_unref (header_size_group);

	return toolbar;
}

static GtkWidget *
create_notebook (NautilusWindow *window)
{
	GtkWidget *notebook;

	notebook = g_object_new (NAUTILUS_TYPE_NOTEBOOK, NULL);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1201
	g_signal_connect (notebook, "tab-close-request",
William Jon McCann's avatar
William Jon McCann committed
1202 1203 1204 1205 1206
			  G_CALLBACK (notebook_tab_close_requested),
			  window);
	g_signal_connect (notebook, "popup-menu",
			  G_CALLBACK (notebook_popup_menu_cb),
			  window);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1207
	g_signal_connect (notebook, "switch-page",
William Jon McCann's avatar
William Jon McCann committed
1208 1209
			  G_CALLBACK (notebook_switch_page_cb),
			  window);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1210 1211 1212
	g_signal_connect_after (notebook, "button-press-event",
				G_CALLBACK (notebook_button_press_cb),
				window);
William Jon McCann's avatar
William Jon McCann committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

	gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
	gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
	gtk_widget_show (notebook);
	gtk_container_set_border_width (GTK_CONTAINER (notebook), 0);

	gtk_box_pack_start (GTK_BOX (window->details->main_view),
			    notebook,
			    TRUE, TRUE, 0);

	return notebook;
}

1226 1227
static void
nautilus_window_constructed (GObject *self)
1228
{
1229
	NautilusWindow *window;
1230
	GtkWidget *grid;
1231
	NautilusWindowSlot *slot;
1232 1233

	window = NAUTILUS_WINDOW (self);
1234

1235 1236
	G_OBJECT_CLASS (nautilus_window_parent_class)->constructed (self);

1237 1238 1239 1240
	/* disable automatic menubar handling, since we show our regular
	 * menubar together with the app menu.
	 */
	gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (self), FALSE);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1241

1242 1243 1244 1245
	grid = gtk_grid_new ();
	gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
	gtk_widget_show (grid);
	gtk_container_add (GTK_CONTAINER (window), grid);
1246 1247 1248 1249 1250 1251 1252

	nautilus_window_initialize_menus (window);
	nautilus_window_initialize_actions (window);

	/* Register to menu provider extension signal managing menu updates */
	g_signal_connect_object (nautilus_signaller_get_current (), "popup_menu_changed",
			 G_CALLBACK (nautilus_window_load_extension_menus), window, G_CONNECT_SWAPPED);
1253 1254 1255 1256 1257
	window->details->toolbar = create_toolbar (window);
	gtk_container_add (GTK_CONTAINER (grid), window->details->toolbar);
	gtk_widget_set_hexpand (window->details->toolbar, TRUE);
	gtk_widget_set_vexpand (window->details->toolbar, FALSE);
	gtk_widget_show (window->details->toolbar);
1258 1259

	window->details->content_paned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
1260 1261 1262 1263
	gtk_widget_set_hexpand (window->details->content_paned, TRUE);
	gtk_widget_set_vexpand (window->details->content_paned, TRUE);

	gtk_container_add (GTK_CONTAINER (grid), window->details->content_paned);
1264 1265
	gtk_widget_show (window->details->content_paned);

William Jon McCann's avatar
William Jon McCann committed
1266 1267
	window->details->main_view = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
	gtk_paned_pack2 (GTK_PANED (window->details->content_paned), window->details->main_view,
1268
			 TRUE, FALSE);
William Jon McCann's avatar
William Jon McCann committed
1269
	gtk_widget_show (window->details->main_view);
1270

William Jon McCann's avatar
William Jon McCann committed
1271
	window->details->notebook = create_notebook (window);
1272

1273
	nautilus_window_initialize_bookmarks_menu (window);
Alexander Larsson's avatar
Alexander Larsson committed
1274
	nautilus_window_set_initial_window_geometry (window);
1275

William Jon McCann's avatar
William Jon McCann committed
1276
	slot = nautilus_window_open_slot (window, 0);
1277
	nautilus_window_set_active_slot (window, slot);
Elliot Lee's avatar
Elliot Lee committed
1278 1279 1280
}

static void
1281 1282
nautilus_window_set_property (GObject *object,
			      guint arg_id,
1283
			      const GValue *value,
1284
			      GParamSpec *pspec)
Elliot Lee's avatar
Elliot Lee committed
1285
{
1286 1287 1288
	NautilusWindow *window;

	window = NAUTILUS_WINDOW (object);
Darin Adler's avatar
Darin Adler committed
1289
	
1290
	switch (arg_id) {
1291 1292 1293 1294 1295 1296
	case PROP_DISABLE_CHROME:
		window->details->disable_chrome = g_value_get_boolean (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, pspec);
		break;
Darin Adler's avatar
Darin Adler committed
1297
	}
Elliot Lee's avatar
Elliot Lee committed
1298 1299 1300
}

static void
1301 1302 1303 1304
nautilus_window_get_property (GObject *object,
			      guint arg_id,
			      GValue *value,
			      GParamSpec *pspec)
Elliot Lee's avatar
Elliot Lee committed
1305
{
1306 1307 1308 1309
	NautilusWindow *window;

	window = NAUTILUS_WINDOW (object);

1310
	switch (arg_id) {
1311 1312
	case PROP_DISABLE_CHROME:
		g_value_set_boolean (value, window->details->disable_chrome);
Darin Adler's avatar
Darin Adler committed
1313 1314
		break;
	}
Elliot Lee's avatar
Elliot Lee committed
1315 1316
}

1317 1318 1319
static void
free_stored_viewers (NautilusWindow *window)
{
1320
	g_list_free_full (window->details->short_list_viewers, g_free);
1321 1322 1323
	window->details->short_list_viewers = NULL;
}

1324
static void
William Jon McCann's avatar
William Jon McCann committed
1325
destroy_slots_foreach (gpointer data,
1326 1327
		       gpointer user_data)
{
William Jon McCann's avatar
William Jon McCann committed
1328
	NautilusWindowSlot *slot = data;
1329 1330
	NautilusWindow *window = user_data;

William Jon McCann's avatar
William Jon McCann committed
1331
	close_slot (window, slot);
1332 1333
}

Darin Adler's avatar
Darin Adler committed
1334
static void
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1335
nautilus_window_destroy (GtkWidget *object)
Darin Adler's avatar
Darin Adler committed
1336 1337
{
	NautilusWindow *window;
William Jon McCann's avatar
William Jon McCann committed
1338
	GList *slots_copy;
1339

Darin Adler's avatar
Darin Adler committed
1340 1341
	window = NAUTILUS_WINDOW (object);

1342 1343
	DEBUG ("Destroying window");

1344 1345
	/* close the sidebar first */
	nautilus_window_tear_down_sidebar (window);
1346

William Jon McCann's avatar
William Jon McCann committed
1347 1348 1349 1350
	/* close all slots safely */
	slots_copy = g_list_copy (window->details->slots);
	g_list_foreach (slots_copy, (GFunc) destroy_slots_foreach, window);
	g_list_free (slots_copy);
Darin Adler's avatar
Darin Adler committed
1351

William Jon McCann's avatar
William Jon McCann committed
1352 1353 1354 1355
	/* the slots list should now be empty */
	g_assert (window->details->slots == NULL);

	window->details->active_slot = NULL;
Darin Adler's avatar
Darin Adler committed
1356

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1357
	GTK_WIDGET_CLASS (nautilus_window_parent_class)->destroy (object);
Darin Adler's avatar
Darin Adler committed
1358 1359
}

1360 1361
static void
nautilus_window_finalize (GObject *object)
1362 1363
{
	NautilusWindow *window;
1364

1365 1366
	window = NAUTILUS_WINDOW (object);

1367 1368 1369 1370 1371
	if (window->details->sidebar_width_handler_id != 0) {
		g_source_remove (window->details->sidebar_width_handler_id);
		window->details->sidebar_width_handler_id = 0;
	}

1372
	nautilus_window_finalize_menus (window);
1373

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1374 1375 1376 1377 1378 1379
	g_clear_object (&window->details->nav_state);
	g_clear_object (&window->details->bookmark_list);
	g_clear_object (&window->details->ui_manager);

	free_stored_viewers (window);

1380
	/* nautilus_window_close() should have run */
William Jon McCann's avatar
William Jon McCann committed
1381
	g_assert (window->details->slots == NULL);
1382

Alexander Larsson's avatar
Alexander Larsson committed
1383
	G_OBJECT_CLASS (nautilus_window_parent_class)->finalize (object);
1384 1385
}

1386
void
1387
nautilus_window_view_visible (NautilusWindow *window,
William Jon McCann's avatar
William Jon McCann committed
1388
			      NautilusView   *view)
1389 1390
{
	NautilusWindowSlot *slot;
William Jon McCann's avatar
William Jon McCann committed
1391
	GList *l;
1392 1393 1394 1395

	g_return_if_fail (NAUTILUS_IS_WINDOW (window));

	slot = nautilus_window_get_slot_for_view (window, view);
1396

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1397
	if (gtk_widget_get_visible (GTK_WIDGET (slot))) {
1398 1399 1400
		return;
	}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1401
	gtk_widget_show (GTK_WIDGET (slot));
1402 1403

	/* Look for other non-visible slots */
William Jon McCann's avatar
William Jon McCann committed
1404
	for (l = window->details->slots; l != NULL; l = l->next) {
1405 1406
		slot = l->data;

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1407
		if (!gtk_widget_get_visible (GTK_WIDGET (slot))) {
1408 1409
			return;
		}
1410

William Jon McCann's avatar
William Jon McCann committed
1411 1412
		nautilus_window_slot_update_title (slot);
		nautilus_window_slot_update_icon (slot);
1413 1414
	}

William Jon McCann's avatar
William Jon McCann committed
1415
	nautilus_window_grab_focus (window);
1416

William Jon McCann's avatar
William Jon McCann committed
1417
	/* All slots, show window */
1418
	gtk_widget_show (GTK_WIDGET (window));
1419 1420
}

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
static void
nautilus_window_save_geometry (NautilusWindow *window)
{
	char *geometry_string;
	gboolean is_maximized;

	g_assert (NAUTILUS_IS_WINDOW (window));

	if (gtk_widget_get_window (GTK_WIDGET (window))) {
		geometry_string = eel_gtk_window_get_geometry_string (GTK_WINDOW (window));
		is_maximized = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (window)))
				& GDK_WINDOW_STATE_MAXIMIZED;

		if (!is_maximized) {
			g_settings_set_string
				(nautilus_window_state, NAUTILUS_WINDOW_STATE_GEOMETRY,
				 geometry_string);
		}
		g_free (geometry_string);

		g_settings_set_boolean
			(nautilus_window_state, NAUTILUS_WINDOW_STATE_MAXIMIZED,
			 is_maximized);
	}
}

1447 1448 1449
void
nautilus_window_close (NautilusWindow *window)
{
1450
	NAUTILUS_WINDOW_CLASS (G_OBJECT_GET_CLASS (window))->close (window);
1451 1452
}

1453
void
1454
nautilus_window_set_active_slot (NautilusWindow *window, NautilusWindowSlot *new_slot)
1455 1456
{
	NautilusWindowSlot *old_slot;
1457

1458 1459
	g_assert (NAUTILUS_IS_WINDOW (window));

1460 1461
	DEBUG ("Setting new slot %p as active", new_slot);

1462
	if (new_slot) {
1463
		g_assert ((window == nautilus_window_slot_get_window (new_slot)));
1464 1465
	}

1466
	old_slot = nautilus_window_get_active_slot (window);
1467 1468 1469 1470 1471

	if (old_slot == new_slot) {
		return;
	}

1472
	/* make old slot inactive if it exists (may be NULL after init, for example) */
1473 1474 1475
	if (old_slot != NULL) {
		/* inform window */
		if (old_slot->content_view != NULL) {
1476
			nautilus_window_disconnect_content_view (window, old_slot->content_view);
1477
		}
1478

1479 1480 1481 1482
		/* inform slot & view */
		g_signal_emit_by_name (old_slot, "inactive");
	}

William Jon McCann's avatar
William Jon McCann committed
1483
	window->details->active_slot = new_slot;
1484

1485 1486
	/* make new slot active, if it exists */
	if (new_slot) {
1487
		/* inform sidebar panels */
1488
                nautilus_window_report_location_change (window);
1489 1490 1491
		/* TODO decide whether "selection-changed" should be emitted */

		if (new_slot->content_view != NULL) {
1492
                        /* inform window */
1493
                        nautilus_window_connect_content_view (window, new_slot->content_view);
1494
                }
1495 1496

		/* inform slot & view */
1497
                g_signal_emit_by_name (new_slot, "active");
1498 1499 1500
	}
}

1501
static void
1502 1503 1504
nautilus_window_get_preferred_width (GtkWidget *widget,
				     gint *minimal_width,
				     gint *natural_width)
1505
{
1506
	GdkScreen *screen;
1507
	gint max_w, min_w, default_w;
1508

1509 1510 1511
	screen = gtk_window_get_screen (GTK_WINDOW (widget));	

	max_w = get_max_forced_width (screen);
1512 1513 1514
	min_w = NAUTILUS_WINDOW_MIN_WIDTH;

	default_w = NAUTILUS_WINDOW_DEFAULT_WIDTH;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

	*minimal_width = MIN (min_w, max_w);
	*natural_width = MIN (default_w, max_w);
}

static void
nautilus_window_get_preferred_height (GtkWidget *widget,
				      gint *minimal_height,
				      gint *natural_height)
{
	GdkScreen *screen;
1526
	gint max_h, min_h, default_h;
1527 1528 1529 1530

	screen = gtk_window_get_screen (GTK_WINDOW (widget));	

	max_h = get_max_forced_height (screen);
1531 1532 1533 1534

	min_h = NAUTILUS_WINDOW_MIN_HEIGHT;

	default_h = NAUTILUS_WINDOW_DEFAULT_HEIGHT;
1535 1536 1537

	*minimal_height = MIN (min_h, max_h);
	*natural_height = MIN (default_h, max_h);
1538 1539
}

1540 1541 1542 1543 1544 1545
static void
nautilus_window_realize (GtkWidget *widget)
{
	GTK_WIDGET_CLASS (nautilus_window_parent_class)->realize (widget);
	update_cursor (NAUTILUS_WINDOW (widget));
}
1546

1547 1548 1549 1550 1551
static gboolean
nautilus_window_key_press_event (GtkWidget *widget,
				 GdkEventKey *event)
{
	NautilusWindow *window;
1552 1553
	NautilusWindowSlot *active_slot;
	NautilusView *view;
1554
	GtkWidget *focus_widget;
1555 1556 1557 1558
	int i;

	window = NAUTILUS_WINDOW (widget);

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
	active_slot = nautilus_window_get_active_slot (window);
	view = active_slot->content_view;

	if (view != NULL && nautilus_view_get_is_renaming (view)) {
		/* if we're renaming, just forward the event to the
		 * focused widget and return. We don't want to process the window
		 * accelerator bindings, as they might conflict with the 
		 * editable widget bindings.
		 */
		if (gtk_window_propagate_key_event (GTK_WINDOW (window), event)) {
			return TRUE;
		}
	}

1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
	focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
	if (view != NULL && focus_widget != NULL &&
	    GTK_IS_EDITABLE (focus_widget)) {
		/* if we have input focus on a GtkEditable (e.g. a GtkEntry), forward
		 * the event to it before activating accelerator bindings too.
		 */
		if (gtk_window_propagate_key_event (GTK_WINDOW (window), event)) {
			return TRUE;
		}
	}

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
	for (i = 0; i < G_N_ELEMENTS (extra_window_keybindings); i++) {
		if (extra_window_keybindings[i].keyval == event->keyval) {
			const GList *action_groups;
			GtkAction *action;

			action = NULL;

			action_groups = gtk_ui_manager_get_action_groups (window->details->ui_manager);
			while (action_groups != NULL && action == NULL) {
				action = gtk_action_group_get_action (action_groups->data, extra_window_keybindings[i].action);
				action_groups = action_groups->next;
			}

			g_assert (action != NULL);
			if (gtk_action_is_sensitive (action)) {
				gtk_action_activate (action);
1600
				return TRUE;
1601
			}
1602 1603

			break;
1604 1605
		}
	}
1606

1607
	return GTK_WIDGET_CLASS (nautilus_window_parent_class)->key_press_event (widget, event);
1608 1609
}

1610