[Kde-bindings] Gtk1 Gtk2 and Tk constructors (was: PerlQt constructor syntax)

Dominique Dumont dominique.dumont at hp.com
Thu Jun 4 12:18:38 UTC 2009


Richard Dale <rdale at foton.es> writes:


>> Dominique, myself, and apparently the people hacking on Tk2, a module
>> with similar constraints to ours.

Hmm, for the record, I used Tk2 as a theorical migration
example. AFAIK, there's no Tk2 in development.

>> It doesn't hurt to look at what the rest of the community is doing.
> Yes, that's true. Probably the GTK perl bindings are the closest to the PerlQt 
> ones, and I don't know what they did about GTK 1.x vs GTK 2.x.

Gtk doc and example use a constructor with indirect object call [1]
(which is considered bad see [2]):

  init Gtk;
  sub hello { ... }
 
  $window = new Gtk::Widget	"GtkWindow",
  	GtkWindow::type			=>	-toplevel,
  	GtkWindow::title		=>	"hello world",
  	GtkWindow::allow_grow		=>	0,
  	GtkWindow::allow_shrink		=>	0,
  	GtkContainer::border_width	=>	10;
  
  $button = new_child $window "GtkButton",
  	GtkButton::label		=>	"hello world",
  	GtkObject::signal::clicked	=>	"hello", # sub name !!!
  	GtkWidget::visible		=>	1;
  show $window;
  
  main Gtk;

This calls could use direct object notation without change to the API
like:

$window = Gtk::Widget->new("GtkWindow",
	GtkWindow::type			=>	-toplevel,
	GtkWindow::title		=>	"hello world",
	GtkWindow::allow_grow		=>	0,
	GtkWindow::allow_shrink		=>	0,
	GtkContainer::border_width	=>	10 );

For GTk2, the api [3] is saner:
- documentation specified direct object notation 
- call back are passed by sub ref and not strings

OTOH, I do not know if Gtk1 parameters can be understood by Gtk2

  use Gtk2 -init;
  # Gtk2->init; works if you didn't use -init on use
  my $window = Gtk2::Window->new ('toplevel');
  my $button = Gtk2::Button->new ('Quit');
  $button->signal_connect (clicked => sub { Gtk2->main_quit });
  $window->add ($button);
  $window->show_all;
  Gtk2->main;

If we compare with the venerable Perl/Tk [4], we'd have :

use Tk;
my $window = MainWindow->new;
my $button = $mw->Button(-text => 'quit',
                         -command => sub { exit; }) ->pack;
Mainloop;


Note that the parent-child relation is created implicitely by 
the $mw->Button call and the widget is loaded on demand by Perl. On the
other hand, this feature require an ugly AUTOLOAD hack [5]. 

Last but not least, none of the graphic toolkits available on CPAN
(including Wx [6]) use Qt trick that desguise a constructor call behind
a class call.  Who knows if this trick will be workable with Perl6 ?

I hope this message will keep the debate constructive and based on
facts.

All the best


[1]
http://cpansearch.perl.org/src/MLEHMANN/Gtk-Perl-0.7009/Gtk/samples/simple.pl
[2] "Perl Best Practices" by damian Conway p 349
[3] http://search.cpan.org/~tsch/Gtk2-1.220/Gtk2.pm
[4] http://cpansearch.perl.org/src/SREZIC/Tk-804.028/t/button.t
[5] See AUTOLOAD sub in http://cpansearch.perl.org/src/SREZIC/Tk-804.028/Tk/Widget.pm
[6] http://search.cpan.org/~mbarbon/Wx-0.91/Wx.pm

-- 
Dominique Dumont 
"Delivering successful solutions requires giving people what they
need, not what they want." Kurt Bittner

irc:
  domidumont at irc.freenode.net
  ddumont at irc.debian.org



More information about the Kde-bindings mailing list