Hi, graff gave you a nice example, but I would just like to throw in this "coffee-machine-chatter". Most Tk list widgets have their own underlying method ( often incompatible with other list types) of storing their list data, and it often is easiest, to keep your data in a separate data structure which you control. Then do all your sorting in that data structure. Then just rebuid the whole HList, instead of trying to splice into the HList.

I ran into considerable difficulty with HList's internal list counter when I played with it. What I mean, is if you add 100 items, delete them, then add another 100, the second set's first internal counter number will be 100. The internal counter assignes 0-99 for the first 100, the 100-199 for the second 100, even if the first 100 were deleted. If you try to splice into the HList, you MAY run into trouble with this internal counter, so it is best to just rebuild the entire HList in my opinion, to avoid this trouble. Here is a sortable HList example which may be useful to you

#!/usr/bin/perl use strict; use Tk; use Tk::HList; my $mw = MainWindow->new(); #create some sample data my %data; foreach (0..100) { $data{$_}{'name'} = 'name'.$_; $data{$_}{'id'} = rand(time); $data{$_}{'priority'} = int rand 50; } #get random list of keys my @keys = keys %data; ################# my $h = $mw->Scrolled( 'HList', -header => 1, -columns => 3, -width => 40, -height => 40, -takefocus => 1, -background => 'steelblue', -foreground =>'snow', -selectmode => 'single', -selectforeground => 'pink', -selectbackground => 'black', # -browsecmd => \&browseThis, )->pack(-side => "left", -anchor => "n"); $h->header('create', 0, -text => ' Name ', -borderwidth => 3, -headerbackground => 'steelblue', -relief => 'raised'); $h->header('create', 1, -text => ' ID ', -borderwidth => 3, -headerbackground => 'lightsteelblue', -relief => 'raised'); $h->header('create', 2, -text => ' Priority ', -borderwidth => 3, -headerbackground => 'lightgreen', -relief => 'raised'); foreach (@keys) { my $e = $h->addchild(""); #will add at end $h->itemCreate ($e, 0, -itemtype => 'text', -text => $data{$_}{'name'}, ); $h->itemCreate($e, 1, -itemtype => 'text', -text => $data{$_}{'id'}, ); $h->itemCreate($e, 2, -itemtype => 'text', -text => $data{$_}{'priority'}, ); } my $button = $mw->Button(-text => 'exit', -command => sub{exit})->pack; my $sortid = $mw->Button(-text => 'Sort by Id', -command => [\&sort_me,1] )->pack; my $sortpriority = $mw->Button(-text => 'Sort by Priority', -command => [\&sort_me,2] )->pack; MainLoop; ######################################################### sub sort_me{ my $col = shift; my @entries = $h->info('children'); my @to_be_sorted =(); foreach my $entry(@entries){ push @to_be_sorted, [ $h->itemCget($entry,0,'text'), $h->itemCget($entry,1,'text'), $h->itemCget($entry,2,'text'), ]; } my @sorted = sort{ $a->[$col] cmp $b->[$col] || #primary sort ascii $a->[1] <=> $b->[1] #secondary sort num +eric } @to_be_sorted; my $entry = 0; foreach my $aref (@sorted){ # print $aref->[0],' ',$aref->[1],' ',$aref->[1],"\n"; $h->itemConfigure( $entry, 0, 'text' => $aref->[0] ); $h->itemConfigure( $entry, 1, 'text' => $aref->[1] ); $h->itemConfigure( $entry, 2, 'text' => $aref->[2] ); $entry++; } $mw->update; }

BTW, GTk2's treeview, handles this stuff much better. This particular example allows you to drag'n'drop. You can drag the parent (which will drag all children) or you can drag individual children. This clearly demonstrates why Gtk2 is technically superior to Tk...... everything in Gtk2 is based on a common object, whereas Tk is a hodgpodge of incompatible widgets( although it is easier to use)

#! /usr/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use Data::Dumper; my $window = Gtk2::Window->new( 'toplevel' ); $window->signal_connect( 'delete_event' => sub { Gtk2->main_quit; } ); $window->set_border_width( 5 ); $window->set_position( 'center_always' ); #this vbox will geturn the bulk of the gui my $vbox = &ret_vbox(); #add and show the vbox $window->add( $vbox ); $window->show(); #our main event-loop Gtk2->main(); sub ret_vbox { my $vbox = Gtk2::VBox->new( FALSE, 5 ); my $sw = Gtk2::ScrolledWindow->new( undef, undef ); $sw->set_shadow_type( 'etched-out' ); $sw->set_policy( 'automatic', 'automatic' ); $sw->set_size_request( 300, 300 ); $sw->set_border_width( 5 ); my $tree_store = Gtk2::TreeStore->new( qw/Glib::String Glib::Strin +g / ); #fill it with arbitry data foreach ( 1 .. 30 ) { my $parent_nr = $_; #the iter is a pointer in the treestore. We #use to add data. my $iter = $tree_store->append( undef ); $tree_store->set( $iter, 0 => "Parent $parent_nr" ); $tree_store->set( $iter, 1 => "----------" ); foreach ( 1 .. 3 ) { my $iter_child = $tree_store->append( $iter ); $tree_store->set( $iter_child, 0 => "Child $_ of Parent $pare +nt_nr" ); } } my $tree_view = Gtk2::TreeView->new( $tree_store ); $tree_view->set_reorderable( 1 ); #drag and drop reordering my $tree_column = Gtk2::TreeViewColumn->new(); $tree_column->set_title( "Click to sort" ); my $renderer = Gtk2::CellRendererText->new; #add a data rider to store column $renderer->{ 'column' } = 0; $renderer->set_property( 'editable', TRUE ); $renderer->signal_connect( edited => \&cell_edited, $tree_store ); $tree_column->pack_start( $renderer, FALSE ); $tree_column->add_attribute( $renderer, text => 0 ); $tree_view->append_column( $tree_column ); #secon column my $tree_column1 = Gtk2::TreeViewColumn->new(); $tree_column1->set_title( "-----------------" ); my $renderer1 = Gtk2::CellRendererText->new; #add a data rider to store column # $renderer1->{ 'column' } = 1; #1 way $renderer1->set_data('column',1); $renderer1->set_property( 'editable', TRUE ); $renderer1->signal_connect( edited => \&cell_edited, $tree_store ); $tree_column1->pack_end( $renderer1, FALSE ); $tree_column1->add_attribute( $renderer1, text => 1 ); $tree_view->append_column( $tree_column1 ); $tree_view->set_search_column( 0 ); $tree_column1->set_sort_column_id( 0 ); $tree_view->set_reorderable( TRUE ); $sw->add( $tree_view ); $vbox->pack_start( $sw, TRUE, TRUE, 0 ); $vbox->show_all(); return $vbox; } ###################################################################### +# sub cell_edited { print "@_\n"; my ( $cell, $path_string, $new_text, $model ) = @_; # print Dumper( [ \$cell ] ), "\n"; # my @props = $cell->list_properties; # print join "\n",@props; # print "@props\n\n"; # foreach my $prop( @props ){ # print Dumper([\$prop]),"\n"; # } print "$path_string\n"; my $path = Gtk2::TreePath->new_from_string( $path_string ); my @indices = $path->get_indices; print "indices->@indices\n"; #Glib::Object::get_data(object, key) #my $column = $cell->get_data('column'); # this is what I did to get the column number # my $column = $cell->{ 'column' }; my $column = $cell->get_data('column'); print "column->$column\n"; my $iter = $model->get_iter( $path ); $model->set_value( $iter, $column, $new_text ); return FALSE; }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: Tk::Hlist item movement by zentara
in thread Tk::Hlist item movement by otto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.