Combobox Focus Challenge!!!




Hi,

 

In the attached code  gtk_widget_grab_focus() is not able to shift  the focus to combobox.

 

The behavior should be: 

When button1 is clicked, focus should come to button and when button is clicked focus should come to combobox.

But focus is not coming on the combobox.

 

Why the focus is not coming on the combobox and which api should I use for this?

 

 

################################################################################################

 

#include <stdlib.h>

#include <gtk/gtk.h>

 

GtkWidget* combo;

GtkWidget *button;

GtkWidget *button1;

 

void button_callback( GtkWidget *widget,

               gpointer   data )

{

            g_print ("button is pressed : shifting focus to combobox\n");

            gtk_widget_grab_focus(combo);   //not working

}

 

 

void button1_callback( GtkWidget *widget,

               gpointer   data )

{

            gtk_widget_grab_focus(button);

            g_print ("button1 is pressed : shifting focus to button\n");

}

 

 

 

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

{

            gtk_init (&argc, &argv);

            GtkWidget *dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);

 

 

            combo = gtk_combo_new();

            GList *glist = NULL;

            glist = g_list_append(glist, "string 2");

            glist = g_list_append(glist, "string 3");

            glist = g_list_append(glist, "string 4");

            glist = g_list_append(glist, "string 5");

            gtk_combo_set_popdown_strings (GTK_COMBO (combo), glist);

 

 

            button = gtk_button_new_with_label ("button");

            button1 = gtk_button_new_with_label ("button1");

 

            g_signal_connect (G_OBJECT (button), "clicked",

                      G_CALLBACK (button_callback), NULL);

            g_signal_connect (G_OBJECT (button1), "clicked",

                      G_CALLBACK (button1_callback), NULL);

 

            GtkWidget *box;

            box = gtk_vbox_new (FALSE, 0);

            gtk_container_set_border_width (GTK_CONTAINER (box), 10);

            gtk_box_pack_start(GTK_BOX(box), combo, FALSE, FALSE, 10);

            gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 10);

            gtk_box_pack_start(GTK_BOX(box), button1, FALSE, FALSE, 10);

            gtk_box_set_homogeneous(GTK_BOX(box), TRUE);

 

            gtk_container_add (GTK_CONTAINER (dialog), box);

 

            gtk_widget_show(combo);

            gtk_widget_show(button);

            gtk_widget_show(button1);

            gtk_widget_show (box);

            gtk_widget_show(dialog);

 

            g_signal_connect (G_OBJECT (dialog), "destroy",

                      G_CALLBACK (gtk_main_quit), NULL);

        g_signal_connect (G_OBJECT (dialog), "delete_event",

                      G_CALLBACK (gtk_main_quit), NULL);

 

            gtk_main();

 

            return 0;

}

 

################################################################################################

 

 

 

Thanks in Advance

Anmol

 



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

#include <stdlib.h>
#include <gtk/gtk.h>

GtkWidget* combo;
GtkWidget *button;
GtkWidget *button1;

void button_callback( GtkWidget *widget,
               gpointer   data )
{
        g_print ("button is pressed : shifting focus to combobox\n");
        gtk_widget_grab_focus(combo);
}


void button1_callback( GtkWidget *widget,
               gpointer   data )
{
        gtk_widget_grab_focus(button);
        g_print ("button1 is pressed : shifting focus to button\n");
}



int main( int   argc, char *argv[] )
{
        gtk_init (&argc, &argv);
        GtkWidget *dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);


        combo = gtk_combo_new();
        GList *glist = NULL;
        glist = g_list_append(glist, "string 2");
        glist = g_list_append(glist, "string 3");
        glist = g_list_append(glist, "string 4");
        glist = g_list_append(glist, "string 5");
        gtk_combo_set_popdown_strings (GTK_COMBO (combo), glist);


        button = gtk_button_new_with_label ("button");
        button1 = gtk_button_new_with_label ("button1");

        g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (button_callback), NULL);
        g_signal_connect (G_OBJECT (button1), "clicked",
                      G_CALLBACK (button1_callback), NULL);

        GtkWidget *box;
        box = gtk_vbox_new (FALSE, 0);
        gtk_container_set_border_width (GTK_CONTAINER (box), 10);
        gtk_box_pack_start(GTK_BOX(box), combo, FALSE, FALSE, 10);
        gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 10);
        gtk_box_pack_start(GTK_BOX(box), button1, FALSE, FALSE, 10);
        gtk_box_set_homogeneous(GTK_BOX(box), TRUE);

        gtk_container_add (GTK_CONTAINER (dialog), box);

        gtk_widget_show(combo);
        gtk_widget_show(button);
        gtk_widget_show(button1);
        gtk_widget_show (box);
        gtk_widget_show(dialog);

        g_signal_connect (G_OBJECT (dialog), "destroy",
                      G_CALLBACK (gtk_main_quit), NULL);
        g_signal_connect (G_OBJECT (dialog), "delete_event",
                      G_CALLBACK (gtk_main_quit), NULL);

        gtk_main();

        return 0;
}


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