source: trunk/gtk/actions.c @ 2525

Last change on this file since 2525 was 2525, checked in by charles, 15 years ago
  • better `info' icon for torrent inspector.
  • fix internal.h ifdefs s.t. we can use openssl for SHA1 when using configure.ac
File size: 6.8 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  { "edit-preferences", GTK_STOCK_PREFERENCES,
72    N_("Edit _Preferences"), NULL, NULL, G_CALLBACK(action_cb) },
73  { "show-torrent-inspector", GTK_STOCK_INFO,
74    N_("_Torrent Info"), NULL, NULL, G_CALLBACK(action_cb) },
75  { "show-about-dialog", GTK_STOCK_ABOUT,
76    N_("_About Transmission"), NULL, NULL, G_CALLBACK(action_cb) },
77  { "show-debug-window", NULL,
78    N_("Show _Debug Window"), NULL, NULL, G_CALLBACK(action_cb) },
79  { "toggle-main-window", "ICON_TRANSMISSION",
80    N_("Show / Hide _Transmission"), NULL, NULL, G_CALLBACK(action_cb) }
81};
82
83static void
84ensure_tooltip (GtkActionEntry * e)
85{
86    if( !e->tooltip && e->label )
87    {
88        const char * src;
89        char *tgt;
90        e->tooltip = g_malloc( strlen( e->label ) + 1 );
91        for( src=e->label, tgt=(char*)e->tooltip; *src; ++src )
92            if( *src != '_' )
93                *tgt++ = *src;
94        *tgt++ = '\0';
95    }
96}
97
98typedef struct
99{
100   const guint8* raw;
101   const char * name;
102}
103BuiltinIconInfo;
104
105/* only one icon now... but room to grow ;) */
106const BuiltinIconInfo my_builtin_icons [] =
107{
108    { tr_icon_full, "ICON_TRANSMISSION" }
109};
110
111static void
112register_my_icons ( void )
113{
114   int i;
115   const int n = G_N_ELEMENTS( my_builtin_icons );
116   GtkIconFactory * factory = gtk_icon_factory_new ();
117   gtk_icon_factory_add_default( factory );
118
119   for( i=0; i<n; ++i )
120   {
121       GdkPixbuf * p;
122       int width;
123       GtkIconSet * icon_set;
124
125       p = gdk_pixbuf_new_from_inline( -1, my_builtin_icons[i].raw, FALSE, 0 );
126       width = gdk_pixbuf_get_width( p );
127       gtk_icon_theme_add_builtin_icon (my_builtin_icons[i].name, width, p );
128
129       icon_set = gtk_icon_set_new_from_pixbuf( p );
130       gtk_icon_factory_add( factory, my_builtin_icons[i].name, icon_set );
131
132       g_object_unref( p );
133       gdk_pixbuf_unref( p );
134       gtk_icon_set_unref (icon_set);
135    }
136
137    g_object_unref (G_OBJECT (factory));
138}
139
140static GtkUIManager * myUIManager = NULL;
141
142void
143actions_init( GtkUIManager * ui_manager, gpointer callback_user_data )
144{
145  int i;
146  const int n_entries = G_N_ELEMENTS( entries );
147  GtkActionGroup * action_group;
148
149  myUIManager = ui_manager;
150
151  register_my_icons ();
152
153  for( i=0; i<n_entries; ++i )
154    ensure_tooltip (&entries[i]);
155
156  action_group = myGroup = gtk_action_group_new( "Actions" );
157  gtk_action_group_set_translation_domain( action_group, NULL );
158
159  gtk_action_group_add_radio_actions( action_group,
160                                      priority_toggle_entries,
161                                      G_N_ELEMENTS(priority_toggle_entries),
162                                      TR_PRI_NORMAL,
163                                      G_CALLBACK(priority_changed_cb), NULL);
164
165  gtk_action_group_add_actions( action_group,
166                                entries, n_entries,
167                                callback_user_data );
168
169  gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
170  g_object_unref (G_OBJECT(action_group));
171}
172
173/****
174*****
175****/
176
177static GHashTable * key_to_action = NULL;
178
179static void
180ensure_action_map_loaded (GtkUIManager * uim)
181{
182    GList * l;
183
184    if ( key_to_action != NULL )
185        return;
186
187    key_to_action =
188        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL);
189
190    for( l=gtk_ui_manager_get_action_groups(uim); l!=NULL; l=l->next )
191    {
192        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
193        GList *ait, *actions = gtk_action_group_list_actions( action_group );
194        for( ait=actions; ait!=NULL; ait=ait->next )
195        {
196            GtkAction * action = GTK_ACTION( ait->data );
197            const char * name = gtk_action_get_name( action );
198            g_hash_table_insert( key_to_action, g_strdup(name), action );
199        }
200        g_list_free( actions );
201    }
202}
203
204static GtkAction*
205get_action( const char* name )
206{
207    ensure_action_map_loaded( myUIManager );
208    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
209}
210
211void
212action_activate ( const char * name )
213{
214    GtkAction * action = get_action( name );
215    g_assert( action != NULL );
216    gtk_action_activate( action );
217}
218
219void
220action_sensitize( const char * name, gboolean b )
221{
222    GtkAction * action = get_action( name );
223    g_assert( action != NULL );
224    g_object_set( action, "sensitive", b, NULL );
225}
226
227void
228action_toggle( const char * name, gboolean b )
229{
230    GtkAction * action = get_action( name );
231    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION(action), b );
232}
233
234GtkWidget*
235action_get_widget( const char * path )
236{
237    return gtk_ui_manager_get_widget( myUIManager, path );
238}
Note: See TracBrowser for help on using the repository browser.