source: trunk/gtk/actions.c @ 3350

Last change on this file since 3350 was 3350, checked in by charles, 15 years ago

Better GNOME Integration (#338): (1) follow HIG recommendation that dialog titles be the name of the command that opened the dialog (2) rename stop-torrent as pause-torrent in the underlying gtk code

  • Property svn:keywords set to Date Rev Author Id
File size: 7.4 KB
Line 
1/*
2 * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: actions.c 3350 2007-10-10 17:25:56Z charles $
11 */
12
13#include <string.h>
14#include <glib/gi18n.h>
15#include <gtk/gtk.h>
16#include <libtransmission/transmission.h>
17#include "torrent-inspector.h"
18#include "img_icon_full.h"
19
20#define UNUSED G_GNUC_UNUSED
21
22extern void doAction (const char * action_name, gpointer user_data );
23
24static GtkActionGroup * myGroup = 0;
25
26static void action_cb ( GtkAction * a, gpointer user_data )
27{
28  doAction ( gtk_action_get_name(a), user_data );
29}
30
31#if !GTK_CHECK_VERSION(2,8,0)
32#define GTK_STOCK_INFO GTK_STOCK_PROPERTIES
33#endif
34
35#if !GTK_CHECK_VERSION(2,10,0)
36#define GTK_STOCK_SELECT_ALL NULL
37#endif
38
39static GtkRadioActionEntry priority_toggle_entries[] =
40{
41  { "priority-high", NULL, N_("_High"), NULL, NULL, TR_PRI_HIGH },
42  { "priority-normal", NULL, N_("_Normal"), NULL, NULL, TR_PRI_NORMAL },
43  { "priority-low", NULL, N_("_Low"), NULL, NULL, TR_PRI_LOW }
44};
45
46extern void set_selected_file_priority ( tr_priority_t ); 
47
48static void
49priority_changed_cb (GtkAction *action UNUSED, GtkRadioAction *current)
50{
51  const int priority = gtk_radio_action_get_current_value (current);
52  set_selected_file_priority ( priority );
53}
54
55static GtkToggleActionEntry show_toggle_entries[] = 
56{
57  { "toggle-main-window", NULL, 
58    N_("Show _Main Window"), NULL, NULL, G_CALLBACK(action_cb), TRUE }, 
59  { "toggle-debug-window", NULL,
60    N_("Show _Debug Window"), NULL, NULL, G_CALLBACK(action_cb), FALSE }
61};
62
63static GtkActionEntry entries[] =
64{
65  { "file-menu", NULL, N_("_File"), NULL, NULL, NULL },
66  { "edit-menu", NULL, N_("_Edit"), NULL, NULL, NULL },
67  { "help-menu", NULL, N_("_Help"), NULL, NULL, NULL },
68  { "priority-menu", NULL, N_("_Priority"), NULL, NULL, NULL },
69  { "add-torrent", GTK_STOCK_OPEN, NULL, NULL,  NULL, G_CALLBACK(action_cb) },
70  { "start-torrent", GTK_STOCK_MEDIA_PLAY,
71    N_("_Start"), "<control>S", NULL, G_CALLBACK(action_cb) },
72  { "recheck-torrent", GTK_STOCK_REFRESH,
73    N_("Re_check"), NULL, NULL, G_CALLBACK(action_cb) },
74  { "pause-torrent", GTK_STOCK_MEDIA_PAUSE,
75    N_("_Pause"), "<control>P", NULL, G_CALLBACK(action_cb) },
76  { "remove-torrent", GTK_STOCK_REMOVE,
77    N_("_Remove"), "<control>R", NULL, G_CALLBACK(action_cb) },
78  { "create-torrent", GTK_STOCK_NEW,
79    N_("_Create New Torrent"), NULL, NULL, G_CALLBACK(action_cb) },
80  { "close", GTK_STOCK_CLOSE,
81    N_("_Close"), "<control>C", NULL, G_CALLBACK(action_cb) },
82  { "quit", GTK_STOCK_QUIT,
83    N_("_Quit"), "<control>Q", NULL, G_CALLBACK(action_cb) },
84  { "select-all", GTK_STOCK_SELECT_ALL,
85    N_("Select _All"), "<control>A", NULL, G_CALLBACK(action_cb) },
86  { "unselect-all", NULL,
87    N_("_Deselect All"), "<control>U", NULL, G_CALLBACK(action_cb) },
88  { "edit-preferences", GTK_STOCK_PREFERENCES,
89    N_("Edit _Preferences"), NULL, NULL, G_CALLBACK(action_cb) },
90  { "show-torrent-inspector", GTK_STOCK_INFO,
91    N_("_Torrent Info"), NULL, NULL, G_CALLBACK(action_cb) },
92  { "show-about-dialog", GTK_STOCK_ABOUT,
93    N_("_About Transmission"), NULL, NULL, G_CALLBACK(action_cb) },
94  { "update-tracker", GTK_STOCK_REFRESH,
95    N_("Update Tracker"), NULL, NULL, G_CALLBACK(action_cb) }
96};
97
98static void
99ensure_tooltip (GtkActionEntry * e)
100{
101    if( !e->tooltip && e->label )
102    {
103        const char * src;
104        char *tgt;
105        e->tooltip = g_malloc( strlen( e->label ) + 1 );
106        for( src=e->label, tgt=(char*)e->tooltip; *src; ++src )
107            if( *src != '_' )
108                *tgt++ = *src;
109        *tgt++ = '\0';
110    }
111}
112
113typedef struct
114{
115   const guint8* raw;
116   const char * name;
117}
118BuiltinIconInfo;
119
120/* only one icon now... but room to grow ;) */
121const BuiltinIconInfo my_builtin_icons [] =
122{
123    { tr_icon_full, "transmission-logo" }
124};
125
126static void
127register_my_icons ( void )
128{
129   int i;
130   const int n = G_N_ELEMENTS( my_builtin_icons );
131   GtkIconFactory * factory = gtk_icon_factory_new ();
132   gtk_icon_factory_add_default( factory );
133
134   for( i=0; i<n; ++i )
135   {
136       GdkPixbuf * p;
137       int width;
138       GtkIconSet * icon_set;
139
140       p = gdk_pixbuf_new_from_inline( -1, my_builtin_icons[i].raw, FALSE, 0 );
141       width = gdk_pixbuf_get_width( p );
142       gtk_icon_theme_add_builtin_icon (my_builtin_icons[i].name, width, p );
143
144       icon_set = gtk_icon_set_new_from_pixbuf( p );
145       gtk_icon_factory_add( factory, my_builtin_icons[i].name, icon_set );
146
147       g_object_unref( p );
148       gdk_pixbuf_unref( p );
149       gtk_icon_set_unref (icon_set);
150    }
151
152    g_object_unref (G_OBJECT (factory));
153}
154
155static GtkUIManager * myUIManager = NULL;
156
157void
158actions_init( GtkUIManager * ui_manager, gpointer callback_user_data )
159{
160  int i;
161  const int n_entries = G_N_ELEMENTS( entries );
162  GtkActionGroup * action_group;
163
164  myUIManager = ui_manager;
165
166  register_my_icons ();
167
168  for( i=0; i<n_entries; ++i )
169    ensure_tooltip (&entries[i]);
170
171  action_group = myGroup = gtk_action_group_new( "Actions" );
172  gtk_action_group_set_translation_domain( action_group, NULL );
173
174  gtk_action_group_add_radio_actions( action_group,
175                                      priority_toggle_entries,
176                                      G_N_ELEMENTS(priority_toggle_entries),
177                                      TR_PRI_NORMAL,
178                                      G_CALLBACK(priority_changed_cb), NULL);
179
180  gtk_action_group_add_toggle_actions ( action_group, 
181                                        show_toggle_entries, 
182                                        G_N_ELEMENTS(show_toggle_entries), 
183                                        callback_user_data );
184
185  gtk_action_group_add_actions( action_group,
186                                entries, n_entries,
187                                callback_user_data );
188
189  gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
190  g_object_unref (G_OBJECT(action_group));
191}
192
193/****
194*****
195****/
196
197static GHashTable * key_to_action = NULL;
198
199static void
200ensure_action_map_loaded (GtkUIManager * uim)
201{
202    GList * l;
203
204    if ( key_to_action != NULL )
205        return;
206
207    key_to_action =
208        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL);
209
210    for( l=gtk_ui_manager_get_action_groups(uim); l!=NULL; l=l->next )
211    {
212        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
213        GList *ait, *actions = gtk_action_group_list_actions( action_group );
214        for( ait=actions; ait!=NULL; ait=ait->next )
215        {
216            GtkAction * action = GTK_ACTION( ait->data );
217            const char * name = gtk_action_get_name( action );
218            g_hash_table_insert( key_to_action, g_strdup(name), action );
219        }
220        g_list_free( actions );
221    }
222}
223
224static GtkAction*
225get_action( const char* name )
226{
227    ensure_action_map_loaded( myUIManager );
228    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
229}
230
231void
232action_activate ( const char * name )
233{
234    GtkAction * action = get_action( name );
235    g_assert( action != NULL );
236    gtk_action_activate( action );
237}
238
239void
240action_sensitize( const char * name, gboolean b )
241{
242    GtkAction * action = get_action( name );
243    g_assert( action != NULL );
244    g_object_set( action, "sensitive", b, NULL );
245}
246
247void
248action_toggle( const char * name, gboolean b )
249{
250    GtkAction * action = get_action( name );
251    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION(action), b );
252}
253
254GtkWidget*
255action_get_widget( const char * path )
256{
257    return gtk_ui_manager_get_widget( myUIManager, path );
258}
Note: See TracBrowser for help on using the repository browser.