Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

GTK3 Treeview question

by richardrosa (Initiate)
on Jan 23, 2023 at 13:53 UTC ( [id://11149788]=perlquestion: print w/replies, xml ) Need Help??

richardrosa has asked for the wisdom of the Perl Monks concerning the following question:

Greetings All.

I Am trying to generate (in PERL) a subroutine that prompts a user to select from a list of people. The list has both names and photos.

I've got most of this working using GTK3 Liststore/Treeview code. However, I cannot seem to figure out how to get the window to automatically follow the cursor. If the user moves the cursor past the bottom (or top) of the displayed window using up/down keys, they must manually adjust the scrollbar to see it.

Can some kind soul suggest the magic needed to do this? Suggestions on a better way to accomplish this task are also appreciated.

Thanx.

Richard Rosa

sub get_person_dialog { ###################################################### # Create a Liststore so that pictures can be used. # # Column 0 of the liststore is the person's full name# # Column 1 of the liststore is the person's dbid # # Column 2 of the liststore is the person's picture # ###################################################### my $col_name = 0; my $col_id = 1; my $col_picture = 2; my $liststore = Gtk3::ListStore->new ('Glib::String', 'Glib::String','Gtk3::Gdk::Pixbuf'); #### List of all DBindexes for every person in the database my @people_keys = sort_by_name(); ### Custom sorting routine foreach my $key (@people_keys) { my $iter = $liststore->append; $liststore->set ($iter, $col_last,FALSE); ### toggle $liststore->set ($iter, $col_name, "$dbase{$key}{'name'}"); $liststore->set ($iter, $col_id, "$key"); ### Is there a picture for this person? my $picture = $dbase{$key}{'picture'}; if ($picture) { my $pixbuf = Gtk3::Gdk::Pixbuf->new_from_file_at_size( "$picture",100,100); $liststore->set($iter,$col_picture,$pixbuf); } } my $treeview = Gtk3::TreeView->new ($liststore); $treeview->set_enable_search(TRUE); ### allow for search $treeview->set_headers_visible(FALSE); $treeview->set_search_column($col_name); $treeview->set_hover_selection(FALSE); ### Column and Cell renderer for the person's Full Name my $renderer = Gtk3::CellRendererText->new; $renderer ->set(editable => FALSE); my $treecol = Gtk3::TreeViewColumn->new_with_attributes('name', $renderer,'text' => $col_name); $treeview->append_column($treecol); ### Column and Cell renderer for the person's ID $renderer = Gtk3::CellRendererText->new; $renderer ->set(editable => FALSE); $treecol = Gtk3::TreeViewColumn->new_with_attributes( 'id',$renderer,'text' => $col_id); $treeview->append_column($treecol); ### Column and Cell renderer for the persons Photo $renderer = Gtk3::CellRendererPixbuf->new; $treecol = Gtk3::TreeViewColumn->new_with_attributes( 'picture',$renderer,'pixbuf' => $col_picture); $treeview->append_column($treecol); ### put this in a scrolled window my $tvsb = Gtk3::ScrolledWindow->new; $tvsb->add_with_viewport($treeview); $tvsb->set_min_content_height(400); $tvsb->set_min_content_width(900); $tvsb->set_policy('GTK_POLICY_ALWAYS', 'GTK_POLICY_ALWAYS'); ### Build a dialog to show this selection my $dl = Gtk3::MessageDialog->new( $main_window, ### parent [qw/modal destroy-with-parent/], ### flags 'GTK_MESSAGE_QUESTION', 'GTK_BUTTONS_NONE', "Please select the person to edit"); $dl->get_content_area()->add($tvsb); ### add an ICON for the Dialog my $stock = Gtk3::Image->new_from_icon_name( $stock_icons[3],'GTK_ICON_SIZE_DIALOG'); $dl->set_image($stock); ### Add some buttons $dl->add_button('gtk-ok','ok'); $dl->add_button('gtk-cancel','cancel'); $dl->show_all; my $response = $dl->run; ### Get selected item my $value = 0; my $selection = $treeview->get_selection(); if ($selection) { my ($md,$iter) = $selection->get_selected(); if ($iter) { $value = $liststore->get($iter,$col_id); if (!$value) {$value = 0;} } } $dl->destroy; if ($response eq 'cancel') {return 0;} return $value; ## Person's ID will be returned

Replies are listed 'Best First'.
Re: GTK3 Treeview question
by Anonymous Monk on Jan 24, 2023 at 10:32 UTC
    Show selection...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11149788]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found