GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 16, 2014 5:29 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: How to change Tab by Enter?
PostPosted: Mon Apr 06, 2009 3:07 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
When i press Enter i want change the focus to the next GtkEntry. Is possible?

I was learn about gtk_widget_grab_focus to change the focus to the next entry, but i d'ont know which is.
I was read about the key_press_event but i can't see how make it.

Thank you...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 06, 2009 6:20 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Since learning by examining code is the way to go, I wrote a simple application for you to dissect. It implements the behavior you desire - move to the next entry field when you press Return.

Code:
#include <gtk/gtk.h>

#define NO_ENTRIES 10

typedef struct _Data Data;
struct _Data
{
   GtkWidget *entries[NO_ENTRIES];
};

static void
cb_activate( GtkEntry *entry,
          Data     *data )
{
   gint index =
      GPOINTER_TO_INT( g_object_get_data( G_OBJECT( entry ), "idx" ) ) + 1;

   if( index < NO_ENTRIES )
      gtk_widget_grab_focus( data->entries[index] );
}

int
main( int    argc,
     char **argv )
{
   GtkWidget *window;
   GtkWidget *vbox;
   GtkWidget *entry;

   Data data;
   gint i;

   gtk_init( &argc, &argv );

   window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
   g_signal_connect( G_OBJECT( window ), "destroy",
                 G_CALLBACK( gtk_main_quit ), NULL );

   vbox = gtk_vbox_new( TRUE, 6 );
   gtk_container_add( GTK_CONTAINER( window ), vbox );

   for( i = 0; i < NO_ENTRIES; i++ )
   {
      entry = gtk_entry_new();
      data.entries[i] = entry;
      g_object_set_data( G_OBJECT( entry ), "idx", GINT_TO_POINTER( i ) );
      g_signal_connect( G_OBJECT( entry ), "activate",
                    G_CALLBACK( cb_activate ), &data );
      gtk_box_pack_start( GTK_BOX( vbox ), entry, FALSE, FALSE, 0 );
   }

   gtk_widget_show_all( window );

   gtk_main();

   return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 7:41 am 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
Thank you very much tadeboro.

Hummmmmmm... post number 666... That's means anything?... :wink:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 8:39 am 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
Solved:

gtk_widget_child_focus(widget,GTK_DIR_TAB_FORWARD);

Code:
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h> ///usr/include/gtk-2.0/gdk/gdkkeysyms.h
#include <glib/gprintf.h>

static gboolean
key_control(GtkWidget *widget,GdkEventKey  *event,gpointer data)
{
switch(event->keyval)
      {
      case GDK_F1: g_printf("\n F1 \n"); break;
      case GDK_Scroll_Lock: g_printf("\n SCROLL \n"); break;
      case GDK_Caps_Lock: {
      if(event->state & (GDK_LOCK_MASK)) g_printf("\n Scroll Lock OFF\n");
      else g_printf("\n Scroll Lock ON\n");} break;
      case GDK_Num_Lock: {
      if(event->state & (GDK_MOD2_MASK))g_printf("\n Num Lock OFF\n");
      else g_printf("\n Num Lock ON\n");} break;
      case GDK_Return: {
      gtk_widget_child_focus(widget,GTK_DIR_TAB_FORWARD);} break;
       }
return FALSE;
}


int
main( int    argc,
      char **argv )
{
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *entry;

    gint i;

    gtk_init( &argc, &argv );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    vbox = gtk_vbox_new( TRUE, 6 );
    gtk_container_add( GTK_CONTAINER( window ), vbox );

    for( i = 0; i < 10; i++ )
    {
        entry = gtk_entry_new();
        gtk_box_pack_start( GTK_BOX( vbox ), entry, FALSE, FALSE, 0 );
    }

    g_signal_connect(G_OBJECT(window),"key_press_event",G_CALLBACK(key_control),NULL);

    gtk_widget_show_all( window );

    gtk_main();

    return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 14, 2009 2:34 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
another way:


Code:
void  LookForWidget(GtkContainer *box,gchar *name,gchar *data,gint way)
{
GList *list = NULL;
GtkWidget *children  = NULL;

list = gtk_container_get_children(box);

while(list != NULL)
     {
     if(GTK_IS_CONTAINER(GTK_WIDGET(list->data)))
       {
       LookForWidget(GTK_CONTAINER(GTK_WIDGET(list->data)),name,data,way);
       } 
    else {
         if(strcmp(gtk_widget_get_name(GTK_WIDGET(list->data)),name) == 0)
           {
           children = GTK_WIDGET(list->data);
           switch(way)
                {
                case 1: {
                        if(GTK_IS_ENTRY(children)) gtk_entry_set_text(GTK_ENTRY(children),data);
                        } break;
                case 2: {
                        if(GTK_IS_ENTRY(children)) strcpy(data,gtk_entry_get_text(GTK_ENTRY(children)));
                        } break;
                case 3: gtk_widget_grab_focus(children); break;
                }
           
           }
         }
     list = list->next;
     }
g_list_free(list);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 12:28 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
if you use gtk_widget_child_focus in key-press-event, the event doing before the press.
In this way the event is when you validate the entry.


Code:
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h> ///usr/include/gtk-2.0/gdk/gdkkeysyms.h
#include <glib/gprintf.h>


typedef struct {
GtkWidget *base; //GtkWindow (main)
int other; //example
} for_entry;



static gboolean
Control_Activate(GtkEntry *entry,gpointer data)
{
for_entry *ent = (for_entry *) data;

printf("\n %s\n",gtk_widget_get_name(GTK_WIDGET(entry)));

gtk_widget_child_focus(ent->base,GTK_DIR_TAB_FORWARD);

return FALSE; 
}


int
main( int    argc,
      char **argv )
{
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *entry;
    for_entry *ent = g_slice_new(for_entry);

    //Data data;
    gint i;

    gtk_init( &argc, &argv );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    vbox = gtk_vbox_new( TRUE, 6 );
    gtk_container_add( GTK_CONTAINER( window ), vbox );

    for( i = 0; i < 10; i++ )
    {
        entry = gtk_entry_new();
        //data.entries[i] = entry;
        gtk_box_pack_start( GTK_BOX( vbox ), entry, FALSE, FALSE, 0 );
        ent->base = window;
        ent->other = 0;
        g_signal_connect(entry,"activate",G_CALLBACK(Control_Activate),ent);
    }


    gtk_widget_show_all( window );

    gtk_main();

    return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 1:50 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
Oooooooops:
Code:
gtk_widget_show_all( window );

    gtk_main();

    g_slice_free(for_entry,ent);

    return( 0 );



g_slice_free(for_entry,ent);


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group