Re: Capture cursor-keys in treeview
| From: | kksou | Date: | Thu, 07 Dec 2006 07:30:37 +0000 |
| Subject: | Re: Capture cursor-keys in treeview | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-7493@lists.php.net to get a copy of this message | ||
ecc wrote:
>
> Hi,
> i have a main treeview showing 10 results per page. Above the treeview, i
> have the standards buttons (|< < > >|) to navigate through the resultset.
> The treeview contains more than one columns.
>
> I want to capture the left and right cursor-key input if i this treeview
> has the focus. Is this possible.
>
> Cursor top, bottom = navigation through the result on the current page
> Return key (or mouse doubble click)= open details
> Cursor left, right = prev/next page
> Cursor left, right (strg) = prev/next page+10
> Cursor left, right (alt) = first/last page
>
> All works fine.. only i cant capture the Cursor left/right keys
> (+modifiers)
>
> Could you help me? How to connect callbacks only for this key-combos if
> the treeview is focused?
>
> Regards,
> Andreas
>
>
Thought it would be simple with key-press-event and in_focus(). Turns out
that it's not that straightforward.
Have to use motion-notify-event and leave-notify-event to keep track of
whether treeview is in focus.
Here's the sample code:
<?php
$window = new GtkWindow();
$window->set_size_request(400, 300);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Capturing key-press-event in GtkTreeView\n");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);
$vbox->pack_start(new GtkLabel('The following is active only when cursor is
inside treeview'), 0, 0);
$vbox->pack_start(new GtkLabel('Return - display detail'), 0, 0);
$vbox->pack_start(new GtkLabel('Left Arrow - prev page'), 0, 0);
$vbox->pack_start(new GtkLabel('Right Arrow - next page'), 0, 0);
$vbox->pack_start(new GtkLabel('Alt Left Arrow - first page'), 0, 0);
$vbox->pack_start(new GtkLabel('Alt Right Arrow - last page'), 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0); // add a small gap between the
title and scroll_win
// Set up a scroll window
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$vbox->pack_start($scrolled_win);
// the 2D table
$data = array(
array('row0', 'item 19', 2, 3.1),
array('row1', 'item 16', 20, 6.21),
array('row2', 'item 13', 8, 9.36),
array('row3', 'item 10', 11, 12.4),
array('row4', 'item 7', 5, 15.5),
array('row5', 'item 4', 17, 18.6),
array('row6', 'item 3', 20, 21.73));
$view_in_focus = 0; // note 1
display_table ($scrolled_win, $data);
$window->show_all();
Gtk::main();
function display_table($scrolled_win, $data) {
// Creates the list store
$model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING,
Gtk::TYPE_LONG, Gtk::TYPE_DOUBLE);
$field_header = array('Row #', 'Description', 'Qty',
'Price');
$field_justification = array(0, 0, 0.5, 1);
// Creates the view to display the list store
$view = new GtkTreeView($model);
$scrolled_win->add($view);
// Creates the columns
for ($col=0; $col<count($field_header); ++$col) {
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property("xalign", $field_justification[$col]);
$column = new GtkTreeViewColumn($field_header[$col], $cell_renderer,
'text', $col);
$column->set_alignment($field_justification[$col]);
$column->set_sort_column_id($col);
// set the header font and color
$label = new GtkLabel($field_header[$col]);
$label->modify_font(new PangoFontDescription("Arial Bold"));
$label->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000FF"));
$column->set_widget($label);
$label->show();
// setup self-defined function to display alternate row color
$column->set_cell_data_func($cell_renderer, "format_col", $col);
$view->append_column($column);
}
// pupulates the data
for ($row=0; $row<count($data); ++$row) {
$values = array();
for ($col=0; $col<count($data[$row]); ++$col) {
$values[] = $data[$row][$col];
}
$model->append($values);
}
// setup event handlers to let us know if mouse is in treeview
$view->connect('motion-notify-event', 'on_motion'); // note 2
$view->connect('leave-notify-event', 'on_leave'); // note 2
// setup selection
$selection = $view->get_selection();
$selection->connect('changed', 'on_selection');
$view->connect('key-press-event', 'on_key_press', $selection); // note 3
}
// set alternate row color
function format_col($column, $cell, $model, $iter, $col_num) {
$path = $model->get_path($iter); // get the current path
$row_num = $path[0]; // get the row number
if ($col_num==3) {
$amt = $model->get_value($iter, 3);
$cell->set_property('text', '$'.number_format($amt,2));
}
$row_color = ($row_num%2==1) ? '#dddddd' : '#ffffff';
$cell->set_property('cell-background', $row_color);
}
// process user selection
function on_selection($selection) {
list($model, $iter) = $selection->get_selected();
if ($iter==NULL) return;
$desc = $model->get_value($iter, 1);
$qty = $model->get_value($iter, 2);
$price = $model->get_value($iter, 3);
print "You have selected $desc: $qty ($price)\n";
}
function on_motion($view, $event) { // note 4
global $view_in_focus;
// check if mouse is in column headers
$scroll_win_pos = $view->get_pointer(); // note 4
if ($scroll_win_pos[1]<=25) { // in column header?
$view_in_focus = 0;
} else {
$view_in_focus = 1;
}
return true;
}
function on_leave($view, $event) { // note 5
global $view_in_focus;
$view_in_focus = 0;
}
function on_key_press($widget, $event, $selection) {
global $view_in_focus;
if (!$view_in_focus) return; // note 6
switch($event->keyval) {
case Gdk::KEY_Return:
list($model, $iter) = $selection->get_selected();
if ($iter==NULL) {
echo "no selection!\n";
} else {
echo "key press. Return: open detail\n";
}
return true;
break;
case Gdk::KEY_Left:
if($event->state==0) {
echo "key press: Left Arrow - prev page\n";
return true;
} elseif ($event->state & Gdk::MOD1_MASK){
echo "key press. Alt Left Arrow: first page\n";
return true;
}
return false;
break;
case Gdk::KEY_Right:
if($event->state==0) {
echo "key press: Right Arrow - next page\n";
return true;
} elseif ($event->state & Gdk::MOD1_MASK){
echo "key press. Alt Right Arrow: last page\n";
return true;
}
return false;
break;
default:
return false;
}
}
?>
Details and explanation in :
http://www.kksou.com/php-gtk2/articles/respond-to-key-press-event-only-when-GtkTreeView-in-focus.php
Regards,
/kksou
--
View this message in context: http://www.nabble.com/Capture-cursor-keys-in-treeview-tf2769661.html#a7734647
Sent from the Php - GTK - General mailing list archive at Nabble.com.