Convert NASettings to a private singleton
[nautilus-actions.git] / src / nact / nact-main-toolbar.c
blobce9f6a18d60cbc5ea2dd86d03581f4a4c26aa720
1 /*
2 * Nautilus-Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program 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
12 * the License, or (at your option) any later version.
14 * This Program 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
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <core/na-iprefs.h>
37 #include "nact-application.h"
38 #include "nact-main-toolbar.h"
40 typedef struct {
41 int id;
42 gchar *prefs_key;
43 gboolean displayed_per_default;
44 gchar *ui_item;
45 gchar *ui_path;
47 ToolbarProps;
49 static ToolbarProps toolbar_props[] = {
50 { MAIN_TOOLBAR_FILE_ID , NA_IPREFS_MAIN_TOOLBAR_FILE_DISPLAY, TRUE, "ViewFileToolbarItem" , "/ui/FileToolbar" },
51 { MAIN_TOOLBAR_EDIT_ID , NA_IPREFS_MAIN_TOOLBAR_EDIT_DISPLAY, FALSE, "ViewEditToolbarItem" , "/ui/EditToolbar" },
52 { MAIN_TOOLBAR_TOOLS_ID, NA_IPREFS_MAIN_TOOLBAR_TOOLS_DISPLAY, FALSE, "ViewToolsToolbarItem", "/ui/ToolsToolbar" },
53 { MAIN_TOOLBAR_HELP_ID , NA_IPREFS_MAIN_TOOLBAR_HELP_DISPLAY, TRUE, "ViewHelpToolbarItem" , "/ui/HelpToolbar" }
56 /* defines the relative position of the main toolbars
57 * that is: they are listed here in the order they should be displayed
59 static int toolbar_pos[] = {
60 MAIN_TOOLBAR_FILE_ID,
61 MAIN_TOOLBAR_EDIT_ID,
62 MAIN_TOOLBAR_TOOLS_ID,
63 MAIN_TOOLBAR_HELP_ID
66 static void init_toolbar( BaseWindow *window, GtkActionGroup *group, int toolbar_id );
67 static void reorder_toolbars( GtkHBox *hbox, int toolbar_id, GtkWidget *handle );
68 static void on_handle_finalize( gpointer data, GObject *handle );
69 static void on_attach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window );
70 static void on_detach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window );
71 static ToolbarProps *get_toolbar_properties( int toolbar_id );
73 /**
74 * nact_main_toolbar_init:
75 * @window: this #NactMainWindow window.
77 * Setup the initial display of the standard main toolbars.
79 * This actually only setup the initial state of the toggle options in
80 * View > Toolbars menu; when an option is activated, this will trigger
81 * the 'on_view_toolbar_activated()' which will actually display the
82 * toolbar.
84 void
85 nact_main_toolbar_init( BaseWindow *window, GtkActionGroup *group )
87 static const gchar *thisfn = "nact_main_toolbar_init";
88 int i;
90 g_debug( "%s: window=%p, group=%p", thisfn, ( void * ) window, ( void * ) group );
92 for( i = 0 ; i < G_N_ELEMENTS( toolbar_pos ) ; ++i ){
93 init_toolbar( window, group, toolbar_pos[i] );
97 static void
98 init_toolbar( BaseWindow *window, GtkActionGroup *group, int toolbar_id )
100 ToolbarProps *props;
101 gboolean is_active;
102 GtkToggleAction *action;
104 props = get_toolbar_properties( toolbar_id );
105 if( props ){
106 is_active = na_settings_get_boolean( props->prefs_key, NULL, NULL );
107 if( is_active ){
108 action = GTK_TOGGLE_ACTION( gtk_action_group_get_action( group, props->ui_item ));
109 gtk_toggle_action_set_active( action, TRUE );
115 * nact_main_toolbar_activate:
116 * @window: this #NactMainWindow.
117 * @toolbar_id: the id of the activated toolbar.
118 * @ui_manager: the #GtkUIManager.
119 * @is_active: whether this toolbar is activated or not.
121 * Activate or desactivate the toolbar.
123 void
124 nact_main_toolbar_activate( NactMainWindow *window, int toolbar_id, GtkUIManager *ui_manager, gboolean is_active )
126 static const gchar *thisfn = "nact_main_toolbar_activate";
127 ToolbarProps *props;
128 GtkWidget *toolbar, *hbox, *handle;
129 gulong attach_id, detach_id;
131 props = get_toolbar_properties( toolbar_id );
132 if( !props ){
133 return;
136 toolbar = gtk_ui_manager_get_widget( ui_manager, props->ui_path );
137 g_debug( "%s: toolbar=%p, path=%s, ref_count=%d", thisfn, ( void * ) toolbar, props->ui_path, G_OBJECT( toolbar )->ref_count );
138 hbox = base_window_get_widget( BASE_WINDOW( window ), "ToolbarHBox" );
140 if( is_active ){
141 handle = gtk_handle_box_new();
142 gtk_handle_box_set_snap_edge( GTK_HANDLE_BOX( handle ), GTK_POS_LEFT );
143 g_object_set_data( G_OBJECT( toolbar ), "nact-main-toolbar-handle", handle );
144 attach_id = g_signal_connect( handle, "child-attached", (GCallback ) on_attach_toolbar, window );
145 g_object_set_data( G_OBJECT( handle ), "nact-handle-attach-id", ( gpointer ) attach_id );
146 detach_id = g_signal_connect( handle, "child-detached", (GCallback ) on_detach_toolbar, window );
147 g_object_set_data( G_OBJECT( handle ), "nact-handle-detach-id", ( gpointer ) detach_id );
148 g_object_weak_ref( G_OBJECT( handle ), ( GWeakNotify ) on_handle_finalize, NULL );
149 gtk_container_add( GTK_CONTAINER( handle ), toolbar );
150 gtk_container_add( GTK_CONTAINER( hbox ), handle );
151 reorder_toolbars( GTK_HBOX( hbox ), toolbar_id, handle );
152 gtk_widget_show_all( handle );
154 } else {
155 handle = ( GtkWidget * ) g_object_get_data( G_OBJECT( toolbar ), "nact-main-toolbar-handle" );
156 detach_id = ( gulong ) g_object_get_data( G_OBJECT( handle ), "nact-handle-detach-id" );
157 g_signal_handler_disconnect( handle, detach_id );
158 attach_id = ( gulong ) g_object_get_data( G_OBJECT( handle ), "nact-handle-attach-id" );
159 g_signal_handler_disconnect( handle, attach_id );
160 gtk_container_remove( GTK_CONTAINER( handle ), toolbar );
161 gtk_container_remove( GTK_CONTAINER( hbox ), handle );
164 na_settings_set_boolean( props->prefs_key, is_active );
168 * reposition the newly activated toolbar in handle
169 * so that the relative positions of toolbars are respected in hbox
171 static void
172 reorder_toolbars( GtkHBox *hbox, int toolbar_id, GtkWidget *handle )
174 int this_canonic_rel_pos;
175 int i;
176 GList *children, *ic;
177 int pos;
178 int canonic_pos;
180 this_canonic_rel_pos = 0;
181 for( i = 0 ; i < G_N_ELEMENTS( toolbar_pos ); ++ i ){
182 if( toolbar_pos[i] == toolbar_id ){
183 this_canonic_rel_pos = i;
184 break;
187 g_object_set_data( G_OBJECT( handle ), "toolbar-canonic-pos", GINT_TO_POINTER( this_canonic_rel_pos ));
189 pos = 0;
190 children = gtk_container_get_children( GTK_CONTAINER( hbox ));
191 for( ic = children ; ic ; ic = ic->next ){
192 canonic_pos = GPOINTER_TO_INT( g_object_get_data( G_OBJECT( ic->data ), "toolbar-canonic-pos" ));
193 if( canonic_pos >= this_canonic_rel_pos ){
194 break;
196 pos += 1;
199 gtk_box_reorder_child( GTK_BOX( hbox ), handle, pos );
202 static void
203 on_handle_finalize( gpointer data, GObject *handle )
205 g_debug( "nact_main_toolbar_on_handle_finalize: handle=%p", ( void * ) handle );
208 static void
209 on_attach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window )
211 static const gchar *thisfn = "nact_main_toolbar_on_attach_toolbar";
213 g_debug( "%s: handle=%p, toolbar=%p, window=%p", thisfn, ( void * ) handle, ( void * ) toolbar, ( void * ) window );
215 gtk_toolbar_set_show_arrow( toolbar, TRUE );
218 static void
219 on_detach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window )
221 static const gchar *thisfn = "nact_main_toolbar_on_detach_toolbar";
223 g_debug( "%s: handle=%p, toolbar=%p, window=%p", thisfn, ( void * ) handle, ( void * ) toolbar, ( void * ) window );
225 gtk_toolbar_set_show_arrow( toolbar, FALSE );
228 static ToolbarProps *
229 get_toolbar_properties( int toolbar_id )
231 static const gchar *thisfn = "nact_main_toolbar_get_toolbar_properties";
232 ToolbarProps *props;
233 int i;
235 props = NULL;
237 for( i = 0 ; i < G_N_ELEMENTS( toolbar_props ) && props == NULL ; ++i ){
238 if( toolbar_props[i].id == toolbar_id ){
239 props = &toolbar_props[i];
243 if( !props ){
244 g_warning( "%s: unable to find toolbar properties for id=%d", thisfn, toolbar_id );
247 return( props );