1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
6 * Copyright (C) 1999, 2000 Red Hat, Inc.
7 * Copyright (C) 2000, 2001 Eazel, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Authors: Elliot Lee <sopwith@redhat.com>
24 * Darin Adler <darin@bentspoon.com>
30 #include <eel/eel-debug.h>
31 #include <eel/eel-gtk-extensions.h>
32 #include <eel/eel-glib-extensions.h>
33 #include <eel/eel-preferences.h>
34 #include <gtk/gtkcellrendererpixbuf.h>
35 #include <gtk/gtkcellrenderertext.h>
36 #include <gtk/gtkenums.h>
37 #include <gtk/gtkliststore.h>
38 #include <gtk/gtktreemodel.h>
39 #include <gtk/gtktreeselection.h>
40 #include <libgnome/gnome-macros.h>
41 #include <glib/gi18n.h>
42 #include <libnautilus-private/nautilus-bookmark.h>
43 #include <libnautilus-private/nautilus-global-preferences.h>
44 #include <libnautilus-private/nautilus-sidebar-provider.h>
45 #include <libnautilus-private/nautilus-module.h>
46 #include <libnautilus-private/nautilus-signaller.h>
48 #include "nautilus-history-sidebar.h"
50 #define NAUTILUS_HISTORY_SIDEBAR_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_HISTORY_SIDEBAR, NautilusHistorySidebarClass))
51 #define NAUTILUS_IS_HISTORY_SIDEBAR(obj) (GTK_CHECK_TYPE ((obj), NAUTILUS_TYPE_HISTORY_SIDEBAR))
52 #define NAUTILUS_IS_HISTORY_SIDEBAR_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_HISTORY_SIDEBAR))
55 GtkScrolledWindowClass parent
;
56 } NautilusHistorySidebarClass
;
60 } NautilusHistorySidebarProvider
;
64 } NautilusHistorySidebarProviderClass
;
68 HISTORY_SIDEBAR_COLUMN_ICON
,
69 HISTORY_SIDEBAR_COLUMN_NAME
,
70 HISTORY_SIDEBAR_COLUMN_BOOKMARK
,
71 HISTORY_SIDEBAR_COLUMN_COUNT
74 static void nautilus_history_sidebar_iface_init (NautilusSidebarIface
*iface
);
75 static void sidebar_provider_iface_init (NautilusSidebarProviderIface
*iface
);
76 static GType
nautilus_history_sidebar_provider_get_type (void);
77 static void nautilus_history_sidebar_style_set (GtkWidget
*widget
,
78 GtkStyle
*previous_style
);
80 G_DEFINE_TYPE_WITH_CODE (NautilusHistorySidebar
, nautilus_history_sidebar
, GTK_TYPE_SCROLLED_WINDOW
,
81 G_IMPLEMENT_INTERFACE (NAUTILUS_TYPE_SIDEBAR
,
82 nautilus_history_sidebar_iface_init
));
84 G_DEFINE_TYPE_WITH_CODE (NautilusHistorySidebarProvider
, nautilus_history_sidebar_provider
, G_TYPE_OBJECT
,
85 G_IMPLEMENT_INTERFACE (NAUTILUS_TYPE_SIDEBAR_PROVIDER
,
86 sidebar_provider_iface_init
));
89 update_history (NautilusHistorySidebar
*sidebar
)
92 GtkTreeSelection
*selection
;
93 NautilusBookmark
*bookmark
;
99 store
= GTK_LIST_STORE (gtk_tree_view_get_model (sidebar
->tree_view
));
101 gtk_list_store_clear (store
);
103 history
= nautilus_window_info_get_history (sidebar
->window
);
104 for (l
= history
; l
!= NULL
; l
= l
->next
) {
105 bookmark
= nautilus_bookmark_copy (l
->data
);
107 pixbuf
= nautilus_bookmark_get_pixbuf (bookmark
, GTK_ICON_SIZE_MENU
);
108 name
= nautilus_bookmark_get_name (bookmark
);
109 gtk_list_store_append (store
, &iter
);
110 gtk_list_store_set (store
, &iter
,
111 HISTORY_SIDEBAR_COLUMN_ICON
, pixbuf
,
112 HISTORY_SIDEBAR_COLUMN_NAME
, name
,
113 HISTORY_SIDEBAR_COLUMN_BOOKMARK
, bookmark
,
115 g_object_unref (bookmark
);
117 if (pixbuf
!= NULL
) {
118 g_object_unref (pixbuf
);
122 eel_g_object_list_free (history
);
124 selection
= GTK_TREE_SELECTION (gtk_tree_view_get_selection (sidebar
->tree_view
));
126 if (gtk_tree_model_get_iter_root (GTK_TREE_MODEL (store
), &iter
)) {
127 gtk_tree_selection_select_iter (selection
, &iter
);
132 history_changed_callback (GObject
*signaller
,
133 NautilusHistorySidebar
*sidebar
)
135 update_history (sidebar
);
139 row_activated_callback (GtkTreeView
*tree_view
,
141 GtkTreeViewColumn
*column
,
144 NautilusHistorySidebar
*sidebar
;
147 NautilusBookmark
*bookmark
;
150 sidebar
= NAUTILUS_HISTORY_SIDEBAR (user_data
);
151 model
= gtk_tree_view_get_model (tree_view
);
153 if (!gtk_tree_model_get_iter (model
, &iter
, path
)) {
158 (model
, &iter
, HISTORY_SIDEBAR_COLUMN_BOOKMARK
, &bookmark
, -1);
160 /* Navigate to the clicked location. */
161 location
= nautilus_bookmark_get_location (NAUTILUS_BOOKMARK (bookmark
));
162 nautilus_window_info_open_location
164 location
, NAUTILUS_WINDOW_OPEN_ACCORDING_TO_MODE
, 0, NULL
);
165 g_object_unref (location
);
169 update_click_policy (NautilusHistorySidebar
*sidebar
)
173 policy
= eel_preferences_get_enum (NAUTILUS_PREFERENCES_CLICK_POLICY
);
175 eel_gtk_tree_view_set_activate_on_single_click
176 (sidebar
->tree_view
, policy
== NAUTILUS_CLICK_POLICY_SINGLE
);
180 click_policy_changed_callback (gpointer user_data
)
182 NautilusHistorySidebar
*sidebar
;
184 sidebar
= NAUTILUS_HISTORY_SIDEBAR (user_data
);
186 update_click_policy (sidebar
);
190 nautilus_history_sidebar_init (NautilusHistorySidebar
*sidebar
)
192 GtkTreeView
*tree_view
;
193 GtkTreeViewColumn
*col
;
194 GtkCellRenderer
*cell
;
196 GtkTreeSelection
*selection
;
198 tree_view
= GTK_TREE_VIEW (gtk_tree_view_new ());
199 gtk_tree_view_set_headers_visible (tree_view
, FALSE
);
200 gtk_widget_show (GTK_WIDGET (tree_view
));
202 col
= GTK_TREE_VIEW_COLUMN (gtk_tree_view_column_new ());
204 cell
= gtk_cell_renderer_pixbuf_new ();
205 gtk_tree_view_column_pack_start (col
, cell
, FALSE
);
206 gtk_tree_view_column_set_attributes (col
, cell
,
207 "pixbuf", HISTORY_SIDEBAR_COLUMN_ICON
,
210 cell
= gtk_cell_renderer_text_new ();
211 gtk_tree_view_column_pack_start (col
, cell
, TRUE
);
212 gtk_tree_view_column_set_attributes (col
, cell
,
213 "text", HISTORY_SIDEBAR_COLUMN_NAME
,
216 gtk_tree_view_column_set_fixed_width (col
, NAUTILUS_ICON_SIZE_SMALLER
);
217 gtk_tree_view_append_column (tree_view
, col
);
219 store
= gtk_list_store_new (HISTORY_SIDEBAR_COLUMN_COUNT
,
222 NAUTILUS_TYPE_BOOKMARK
);
224 gtk_tree_view_set_model (tree_view
, GTK_TREE_MODEL (store
));
225 g_object_unref (store
);
227 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sidebar
),
228 GTK_POLICY_AUTOMATIC
,
229 GTK_POLICY_AUTOMATIC
);
230 gtk_scrolled_window_set_hadjustment (GTK_SCROLLED_WINDOW (sidebar
), NULL
);
231 gtk_scrolled_window_set_vadjustment (GTK_SCROLLED_WINDOW (sidebar
), NULL
);
232 gtk_container_add (GTK_CONTAINER (sidebar
), GTK_WIDGET (tree_view
));
233 gtk_widget_show (GTK_WIDGET (sidebar
));
235 sidebar
->tree_view
= tree_view
;
237 selection
= gtk_tree_view_get_selection (tree_view
);
238 gtk_tree_selection_set_mode (selection
, GTK_SELECTION_SINGLE
);
240 g_signal_connect_object
241 (tree_view
, "row_activated",
242 G_CALLBACK (row_activated_callback
), sidebar
, 0);
244 g_signal_connect_object (nautilus_signaller_get_current (),
245 "history_list_changed",
246 G_CALLBACK (history_changed_callback
), sidebar
, 0);
248 eel_preferences_add_callback (NAUTILUS_PREFERENCES_CLICK_POLICY
,
249 click_policy_changed_callback
,
251 update_click_policy (sidebar
);
255 nautilus_history_sidebar_finalize (GObject
*object
)
257 NautilusHistorySidebar
*sidebar
;
259 sidebar
= NAUTILUS_HISTORY_SIDEBAR (object
);
261 eel_preferences_remove_callback (NAUTILUS_PREFERENCES_CLICK_POLICY
,
262 click_policy_changed_callback
,
265 G_OBJECT_CLASS (nautilus_history_sidebar_parent_class
)->finalize (object
);
269 nautilus_history_sidebar_class_init (NautilusHistorySidebarClass
*class)
271 G_OBJECT_CLASS (class)->finalize
= nautilus_history_sidebar_finalize
;
273 GTK_WIDGET_CLASS (class)->style_set
= nautilus_history_sidebar_style_set
;
277 nautilus_history_sidebar_get_sidebar_id (NautilusSidebar
*sidebar
)
279 return NAUTILUS_HISTORY_SIDEBAR_ID
;
283 nautilus_history_sidebar_get_tab_label (NautilusSidebar
*sidebar
)
285 return g_strdup (_("History"));
289 nautilus_history_sidebar_get_tab_tooltip (NautilusSidebar
*sidebar
)
291 return g_strdup (_("Show History"));
295 nautilus_history_sidebar_get_tab_icon (NautilusSidebar
*sidebar
)
301 nautilus_history_sidebar_is_visible_changed (NautilusSidebar
*sidebar
,
308 nautilus_history_sidebar_iface_init (NautilusSidebarIface
*iface
)
310 iface
->get_sidebar_id
= nautilus_history_sidebar_get_sidebar_id
;
311 iface
->get_tab_label
= nautilus_history_sidebar_get_tab_label
;
312 iface
->get_tab_tooltip
= nautilus_history_sidebar_get_tab_tooltip
;
313 iface
->get_tab_icon
= nautilus_history_sidebar_get_tab_icon
;
314 iface
->is_visible_changed
= nautilus_history_sidebar_is_visible_changed
;
318 nautilus_history_sidebar_set_parent_window (NautilusHistorySidebar
*sidebar
,
319 NautilusWindowInfo
*window
)
321 sidebar
->window
= window
;
322 update_history (sidebar
);
326 nautilus_history_sidebar_style_set (GtkWidget
*widget
,
327 GtkStyle
*previous_style
)
329 NautilusHistorySidebar
*sidebar
;
331 sidebar
= NAUTILUS_HISTORY_SIDEBAR (widget
);
333 update_history (sidebar
);
336 static NautilusSidebar
*
337 nautilus_history_sidebar_create (NautilusSidebarProvider
*provider
,
338 NautilusWindowInfo
*window
)
340 NautilusHistorySidebar
*sidebar
;
342 sidebar
= g_object_new (nautilus_history_sidebar_get_type (), NULL
);
343 nautilus_history_sidebar_set_parent_window (sidebar
, window
);
344 g_object_ref (sidebar
);
345 gtk_object_sink (GTK_OBJECT (sidebar
));
347 return NAUTILUS_SIDEBAR (sidebar
);
351 sidebar_provider_iface_init (NautilusSidebarProviderIface
*iface
)
353 iface
->create
= nautilus_history_sidebar_create
;
357 nautilus_history_sidebar_provider_init (NautilusHistorySidebarProvider
*sidebar
)
362 nautilus_history_sidebar_provider_class_init (NautilusHistorySidebarProviderClass
*class)
367 nautilus_history_sidebar_register (void)
369 nautilus_module_add_type (nautilus_history_sidebar_provider_get_type ());