I created a list with strings "1" to "5" on it. when I hit key up, must select the last cell if we are in the top... for example if current cell is in "1", hitting up will go and select "5"...
sandbox.builder:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name gchararray1 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkTreeModelSort" id="treemodelsort1">
<property name="model">liststore1</property>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="border_width">10</property>
<property name="window_position">center</property>
<property name="default_width">250</property>
<property name="default_height">450</property>
<signal name="destroy" handler="window1_destroy_cb" swapped="no"/>
<signal name="key-press-event" handler="window1_key_press_event_cb" swapped="no"/>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="is_focus">True</property>
<property name="events">GDK_KEY_PRESS_MASK | GDK_STRUCTURE_MASK</property>
<property name="model">treemodelsort1</property>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title" translatable="yes">List</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use Gtk2;
use Gtk2::Gdk::Keysyms;
use Glib qw{TRUE FALSE};
sub moveUP {
my $treeview = shift;
my ($path, $focus_column) = $treeview->get_cursor;
my $modelSort = $treeview->get_model();
if (!$path->prev) {
$path = undef;
my $totalItems = $modelSort->iter_n_children();
my $iter = $modelSort->iter_nth_child(undef, $totalItems-1);
$path = $modelSort->get_path($iter);
$treeview->set_cursor($path, $focus_column);
}
$path = undef;
}
sub moveDown {
my $treeview = shift;
my ($path, $focus_column) = $treeview->get_cursor;
my $modelSort = $treeview->get_model();
my $iter = $modelSort->get_iter($path);
if (!defined $modelSort->iter_next($iter)) {
$path = undef;
$path = Gtk2::TreePath->new_first();
$treeview->set_cursor($path, $focus_column);
}
$path = undef;
}
sub window1_destroy_cb {
my ($widget, $data) = @_;
Gtk2->main_quit();
}
sub window1_key_press_event_cb {
my ($widget, $event, $data) = @_;
my $tv = $data->{'treeview'};
if ($event->keyval == $Gtk2::Gdk::Keysyms{Up}) {
moveUP($tv);
} elsif ($event->keyval == $Gtk2::Gdk::Keysyms{Down}) {
moveDown($tv);
}
return FALSE;
}
sub loadItems {
my $treeview = shift;
my $modelSort = $treeview->get_model();
my $realModel = $modelSort->get_model();
foreach (1..5) {
my $iter = $realModel->append();
$realModel->set($iter, 0, $_);
}
}
sub Main {
my %MyData = ();
Gtk2->init();
my $builder = Gtk2::Builder->new();
$builder->add_from_file('sandbox.builder');
$MyData{'toplevel'} = $builder->get_object('window1');
$MyData{'treeview'} = $builder->get_object('treeview1');
$builder->connect_signals(\%MyData);
$builder = undef;
loadItems($MyData{'treeview'});
$MyData{'toplevel'}->show_all();
Gtk2->main();
%MyData = ();
return 0;
}
exit (Main);
my problem is if the current cell is "1", when hit up key doesn't go to "5" as expected, it goes to "4"....any ideas, please?