source: trunk/gtk/actions.c @ 2556

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

fix r2553 gtk hiccup: use the `select all' stock icon iff gtk+ >= 2.10

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