Set Nautilus-Actions as being the actual official product name
[nautilus-actions.git] / src / nact / nact-main-toolbar.c
blob48bd5afc58b6620c1497e51b6c4ee725271ae777
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-iprefs.h"
39 #include "nact-main-toolbar.h"
41 typedef struct {
42 int id;
43 gchar *prefs_key;
44 gboolean displayed_per_default;
45 gchar *ui_item;
46 gchar *ui_path;
48 ToolbarProps;
50 static ToolbarProps toolbar_props[] = {
51 { MAIN_TOOLBAR_FILE_ID , "main-file-toolbar" , TRUE, "ViewFileToolbarItem" , "/ui/FileToolbar" },
52 { MAIN_TOOLBAR_EDIT_ID , "main-edit-toolbar" , FALSE, "ViewEditToolbarItem" , "/ui/EditToolbar" },
53 { MAIN_TOOLBAR_TOOLS_ID, "main-tools-toolbar", FALSE, "ViewToolsToolbarItem", "/ui/ToolsToolbar" },
54 { MAIN_TOOLBAR_HELP_ID , "main-help-toolbar" , TRUE, "ViewHelpToolbarItem" , "/ui/HelpToolbar" }
57 /* defines the relative position of the main toolbars
58 * that is: they are listed here in the order they should be displayed
60 static int toolbar_pos[] = {
61 MAIN_TOOLBAR_FILE_ID,
62 MAIN_TOOLBAR_EDIT_ID,
63 MAIN_TOOLBAR_TOOLS_ID,
64 MAIN_TOOLBAR_HELP_ID
67 static void init_toolbar( NactMainWindow *window, GtkActionGroup *group, int toolbar_id );
68 static void reorder_toolbars( GtkHBox *hbox, int toolbar_id, GtkWidget *handle );
69 static void on_handle_finalize( gpointer data, GObject *handle );
70 static void on_attach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window );
71 static void on_detach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window );
72 static ToolbarProps *get_toolbar_properties( int toolbar_id );
74 /**
75 * nact_main_toolbar_init:
76 * @window: this #NactMainWindow window.
78 * Setup the initial display of the standard main toolbars.
80 * This actually only setup the initial state of the toggle options in
81 * View > Toolbars menu; when an option is activated, this will trigger
82 * the 'on_view_toolbar_activated()' which will actually display the
83 * toolbar.
85 void
86 nact_main_toolbar_init( NactMainWindow *window, GtkActionGroup *group )
88 static const gchar *thisfn = "nact_main_toolbar_init";
89 int i;
91 g_debug( "%s: window=%p, group=%p", thisfn, ( void * ) window, ( void * ) group );
93 for( i = 0 ; i < G_N_ELEMENTS( toolbar_pos ) ; ++i ){
94 init_toolbar( window, group, toolbar_pos[i] );
98 static void
99 init_toolbar( NactMainWindow *window, GtkActionGroup *group, int toolbar_id )
101 NactApplication *application;
102 NAUpdater *updater;
103 ToolbarProps *props;
104 gboolean is_active;
105 GtkToggleAction *action;
107 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
108 updater = nact_application_get_updater( application );
109 props = get_toolbar_properties( toolbar_id );
110 if( props ){
111 is_active = na_iprefs_read_bool( NA_IPREFS( updater ), props->prefs_key, props->displayed_per_default );
112 if( is_active ){
113 action = GTK_TOGGLE_ACTION( gtk_action_group_get_action( group, props->ui_item ));
114 gtk_toggle_action_set_active( action, TRUE );
120 * nact_main_toolbar_activate:
121 * @window: this #NactMainWindow.
122 * @toolbar_id: the id of the activated toolbar.
123 * @ui_manager: the #GtkUIManager.
124 * @is_active: whether this toolbar is activated or not.
126 * Activate or desactivate the toolbar.
128 void
129 nact_main_toolbar_activate( NactMainWindow *window, int toolbar_id, GtkUIManager *ui_manager, gboolean is_active )
131 static const gchar *thisfn = "nact_main_toolbar_activate";
132 ToolbarProps *props;
133 GtkWidget *toolbar, *hbox, *handle;
134 gulong attach_id, detach_id;
136 props = get_toolbar_properties( toolbar_id );
137 if( !props ){
138 return;
141 toolbar = gtk_ui_manager_get_widget( ui_manager, props->ui_path );
142 g_debug( "%s: toolbar=%p, path=%s, ref=%d", thisfn, ( void * ) toolbar, props->ui_path, G_OBJECT( toolbar )->ref_count );
143 hbox = base_window_get_widget( BASE_WINDOW( window ), "ToolbarHBox" );
145 if( is_active ){
146 handle = gtk_handle_box_new();
147 gtk_handle_box_set_snap_edge( GTK_HANDLE_BOX( handle ), GTK_POS_LEFT );
148 g_object_set_data( G_OBJECT( toolbar ), "nact-main-toolbar-handle", handle );
149 attach_id = g_signal_connect( handle, "child-attached", (GCallback ) on_attach_toolbar, window );
150 g_object_set_data( G_OBJECT( handle ), "nact-handle-attach-id", ( gpointer ) attach_id );
151 detach_id = g_signal_connect( handle, "child-detached", (GCallback ) on_detach_toolbar, window );
152 g_object_set_data( G_OBJECT( handle ), "nact-handle-detach-id", ( gpointer ) detach_id );
153 g_object_weak_ref( G_OBJECT( handle ), ( GWeakNotify ) on_handle_finalize, NULL );
154 gtk_container_add( GTK_CONTAINER( handle ), toolbar );
155 gtk_container_add( GTK_CONTAINER( hbox ), handle );
156 reorder_toolbars( GTK_HBOX( hbox ), toolbar_id, handle );
157 gtk_widget_show_all( handle );
158 g_debug( "%s: ref=%d", thisfn, G_OBJECT( toolbar )->ref_count );
160 } else {
161 handle = ( GtkWidget * ) g_object_get_data( G_OBJECT( toolbar ), "nact-main-toolbar-handle" );
162 detach_id = ( gulong ) g_object_get_data( G_OBJECT( handle ), "nact-handle-detach-id" );
163 g_signal_handler_disconnect( handle, detach_id );
164 attach_id = ( gulong ) g_object_get_data( G_OBJECT( handle ), "nact-handle-attach-id" );
165 g_signal_handler_disconnect( handle, attach_id );
166 gtk_container_remove( GTK_CONTAINER( handle ), toolbar );
167 gtk_container_remove( GTK_CONTAINER( hbox ), handle );
168 g_debug( "%s: ref=%d", thisfn, G_OBJECT( toolbar )->ref_count );
171 nact_iprefs_write_bool( BASE_WINDOW( window ), props->prefs_key, is_active );
175 * reposition the newly activated toolbar in handle
176 * so that the relative positions of toolbars are respected in hbox
178 static void
179 reorder_toolbars( GtkHBox *hbox, int toolbar_id, GtkWidget *handle )
181 int this_canonic_rel_pos;
182 int i;
183 GList *children, *ic;
184 int pos;
185 int canonic_pos;
187 this_canonic_rel_pos = 0;
188 for( i = 0 ; i < G_N_ELEMENTS( toolbar_pos ); ++ i ){
189 if( toolbar_pos[i] == toolbar_id ){
190 this_canonic_rel_pos = i;
191 break;
194 g_object_set_data( G_OBJECT( handle ), "toolbar-canonic-pos", GINT_TO_POINTER( this_canonic_rel_pos ));
196 pos = 0;
197 children = gtk_container_get_children( GTK_CONTAINER( hbox ));
198 for( ic = children ; ic ; ic = ic->next ){
199 canonic_pos = GPOINTER_TO_INT( g_object_get_data( G_OBJECT( ic->data ), "toolbar-canonic-pos" ));
200 if( canonic_pos >= this_canonic_rel_pos ){
201 break;
203 pos += 1;
206 gtk_box_reorder_child( GTK_BOX( hbox ), handle, pos );
209 static void
210 on_handle_finalize( gpointer data, GObject *handle )
212 g_debug( "nact_main_toolbar_on_handle_finalize: handle=%p", ( void * ) handle );
215 static void
216 on_attach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window )
218 static const gchar *thisfn = "nact_main_toolbar_on_attach_toolbar";
220 g_debug( "%s: handle=%p, toolbar=%p, window=%p", thisfn, ( void * ) handle, ( void * ) toolbar, ( void * ) window );
222 gtk_toolbar_set_show_arrow( toolbar, TRUE );
225 static void
226 on_detach_toolbar( GtkHandleBox *handle, GtkToolbar *toolbar, NactMainWindow *window )
228 static const gchar *thisfn = "nact_main_toolbar_on_detach_toolbar";
230 g_debug( "%s: handle=%p, toolbar=%p, window=%p", thisfn, ( void * ) handle, ( void * ) toolbar, ( void * ) window );
232 gtk_toolbar_set_show_arrow( toolbar, FALSE );
235 static ToolbarProps *
236 get_toolbar_properties( int toolbar_id )
238 static const gchar *thisfn = "nact_main_toolbar_get_toolbar_properties";
239 ToolbarProps *props;
240 int i;
242 props = NULL;
244 for( i = 0 ; i < G_N_ELEMENTS( toolbar_props ) && props == NULL ; ++i ){
245 if( toolbar_props[i].id == toolbar_id ){
246 props = &toolbar_props[i];
250 if( !props ){
251 g_warning( "%s: unable to find toolbar properties for id=%d", thisfn, toolbar_id );
254 return( props );