source: trunk/gtk/actions.c @ 2553

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

(gtk) add select-all, deselect-all

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