Adding accelerator group to the application



Hi,

I wanted to know how to add an accelerator group to the entire
application. Right now, for the keystroke to actually work, the window
to which I had attached the accelerator group should have focus.

 

But if an application has multiple windows and dialogs, I want the
accelerator keystroke to work if any of the windows (of this
application) has the focus at the time.

One way is to add the accelerator group to every window created, but
that is prone to errors and if somebody misses this for a new window
then it won't work for that. 

 

Another method I tried was creating a toplevel window(hidden) and adding
the accelerator group to it. This does not seem to work either. ( I have
attached the code for this.)

 

So is there a way so that the same accelerator group is attached to the
whole application, no matter which of application windows has focus.

 

Thanks,

Aniket Ray

 

 

 

/*************************SOURCE**************************************/

#include <gtk/gtk.h>

 

 

GtkAccelGroup *gag;  //commonAccelGroup

 

 

static gboolean delete_event(GtkWidget *widget, gpointer data)

{

            gtk_main_quit();

            return FALSE;

}

 

 

 

 

static void handle_dia(GtkDialog *dia, gint arg1, gpointer a)

{

            if (arg1 == GTK_RESPONSE_ACCEPT)

            {

                        g_print("Accepting\n");

            }

}

 

 

static void showDialog()

{

            GtkWidget *dia = gtk_dialog_new_with_buttons(

                                    "Hello",

                                    NULL,

                                    0,

                                    GTK_STOCK_OK,

                                    GTK_RESPONSE_ACCEPT,

                                    NULL);

 

            gtk_widget_show(dia);

            g_signal_connect(G_OBJECT(dia),"response",G_CALLBACK
(handle_dia), NULL);

 

 


}

 

static void printMe(gpointer data,gpointer others)

{

            g_print("%s\n", (gchar*)data);

            if(strncmp((gchar *)data,"<Control>a",10) == 0)

                        showDialog();

 

}

 

static void printMeToo(gpointer data,gpointer others)

{

            g_print("%s\n", (gchar*)data);

}

 

 

static void add_global_accelerator(const gchar* accelerator,
GtkSignalFunc func )

{

            guint key;

            GdkModifierType mods;

        gtk_accelerator_parse(accelerator,&key,&mods);

        GClosure *gcl = g_cclosure_new_swap(func, (gpointer)
accelerator, NULL);

        gtk_accel_group_connect(gag,key,mods,GTK_ACCEL_VISIBLE, gcl);

}

 

 

 

int main(int argc, char* argv[])

{

            

            GtkWidget *mainWindow;/*main window*/

            gag = gtk_accel_group_new();

 

            gtk_init(&argc, &argv);/*init this always has to be called*/

            

            mainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);/*creating
the main window*/

            gtk_window_set_title(GTK_WINDOW(mainWindow), "0");

            gtk_window_maximize(GTK_WINDOW(mainWindow));

            g_signal_connect (G_OBJECT (mainWindow), "delete_event",
G_CALLBACK(delete_event), NULL);/*delete_event signal handler*/

 

 

            /*custom stuff here*/

            GtkWidget* hiddenWindow =
gtk_window_new(GTK_WINDOW_TOPLEVEL);

            

 

 

            add_global_accelerator("<Control>a",G_CALLBACK(printMe));

            add_global_accelerator("<Control>b",G_CALLBACK(printMeToo));

            

            

                                    

            gtk_window_add_accel_group(GTK_WINDOW(hiddenWindow), gag);

            

            

            gtk_widget_realize(hiddenWindow);

            gtk_widget_show(mainWindow);

 

 

            gtk_main();/*main loop*/

}

 

 

 

 

 

 

 

 

 

 

 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]