GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 24, 2014 11:53 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: treeview and keys
PostPosted: Tue Apr 01, 2014 5:29 am 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
I'm trying to to navigate my treeview with the keys up and down:
Code:
gboolean window1_key_press_event_cb(GtkWidget *toplevel, GdkEventKey *event, MyData *data) {
   switch (event->keyval) {
      case GDK_KEY_Up: {
         moveItemUP(data);
         break;
      }
      case GDK_KEY_Down: {
         moveItemDown(data);
         break;
      }
      case GDK_KEY_End: {
         break;
      }
   }
   return FALSE;
}

void moveItemUP(MyData *dat) {
   GtkTreeSelection *selection;
   GtkTreeIter iter;
   selection = gtk_tree_view_get_selection(dat->tv);
   if (gtk_tree_selection_get_selected(selection, NULL, &iter)) {
      GtkTreePath *path;
      path = gtk_tree_model_get_path(dat->model, &iter);
      if (!gtk_tree_path_prev(path)) {
         // here means we are in the top list, send me to the bottom
         gint lastIndex;
         lastIndex = gtk_tree_model_iter_n_children(dat->model, NULL);
         gtk_tree_path_free(path);
         path = gtk_tree_path_new_from_indices(lastIndex-1, -1);
      }
      gtk_tree_selection_select_path(selection, path);
      gtk_tree_path_free(path);
   }
}

void moveItemDown(MyData *dat) {
   GtkTreeSelection *selection;
   GtkTreeIter iter;
   selection = gtk_tree_view_get_selection(dat->tv);
   if (gtk_tree_selection_get_selected(selection, NULL, &iter)) {
      if (!gtk_tree_model_iter_next(dat->model, &iter)) {
         // here means we are in the bottom list, let's got to the top
         gtk_tree_model_get_iter_first(dat->model, &iter);
      }
      gtk_tree_selection_select_iter(selection, &iter);
   }
}

Well, it works in a way...
Image
As you can see, from the top list when I hit "up" it goes to the last item, as expected....but it keeps selected the first item and the system bell is played :-/...my question is how to avoid the bell and the static selection of the 1st item?

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject: Re: treeview and keys
PostPosted: Wed Apr 02, 2014 7:44 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hello,

What you have are two things.

- You have the cursor which is the first item.
- You have the selected item, the one moving around.

The GtkTreeView widget already has a number of bindings for keyboard buttons set up. These include what you are using which move the cursor.

Your code is moving the selected item and is set up to loop around using the keyboard. Your code is allowing the selected item to change on every key press. The built in keyboard binding hits the end and stops and sounds the bell.

_________________
E.


Top
 Profile  
 
 Post subject: Re: treeview and keys
PostPosted: Thu Apr 03, 2014 5:45 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
I got help on this, now works as expected :)
Code:
gboolean moveItemUP(MyData *dat) {
   GtkTreePath* path;
   gtk_tree_view_get_cursor(dat->tv, &path, NULL);
   if (!gtk_tree_path_prev(path)) {
      GtkTreeIter iter;
      gtk_tree_path_free(path);
      gtk_tree_model_iter_nth_child(dat->model, &iter, NULL, dat->totalItems-1);
      path = gtk_tree_model_get_path(dat->model, &iter);
      gtk_tree_view_set_cursor(dat->tv, path, NULL, FALSE);
      return TRUE;
   }
   gtk_tree_path_free(path);
   return FALSE;
}

gboolean moveItemDown(MyData *dat) {
   GtkTreeIter iter;
   GtkTreePath* path;
   gtk_tree_view_get_cursor(dat->tv, &path, NULL);
   gtk_tree_model_get_iter(dat->model, &iter, path);
   if (!gtk_tree_model_iter_next(dat->model, &iter)) {
      gtk_tree_path_free(path);
      path = gtk_tree_path_new_first();
      gtk_tree_view_scroll_to_cell(dat->tv, path, NULL, TRUE, 0.5, 0);
      gtk_tree_view_set_cursor(dat->tv, path, NULL, FALSE);
      return TRUE;
   }
   gtk_tree_path_free(path);
   return FALSE;
}

gboolean window1_key_press_event_cb(GtkWidget *toplevel, GdkEventKey *event, MyData *data) {
   switch (event->keyval) {
      case GDK_KEY_Up: {
         return moveItemUP(data);
      }
      case GDK_KEY_Down: {
         return moveItemDown(data);
      }
   }
   return FALSE;
}

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] 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