Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > [Fwd: Gtk and Glib::Timeout]

Reply
Thread Tools

[Fwd: Gtk and Glib::Timeout]

 
 
Y.G.
Guest
Posts: n/a
 
      06-08-2006
I am writing a program in perl which invokes a small window. This window
should time out after some time. For the window I use Gtk2. For the
timeout I use Glib::Timeout

The code looks something like this:

my $dialog = new Gtk2:ialog( );

(creating some buttons and all)

my $timer = Glib::Timeout->add( $interval, \&some_func));

Gtk2->main;


This returns Not a CODE reference at /program_path line line_number

Should I use Glib (and how?) or is there any other function I should use.
 
Reply With Quote
 
 
 
 
zentara
Guest
Posts: n/a
 
      06-09-2006
On Thu, 08 Jun 2006 18:47:17 +0200, "Y.G." <yg_blah_@this_domain.bla>
wrote:

>I am writing a program in perl which invokes a small window. This window
>should time out after some time. For the window I use Gtk2. For the
>timeout I use Glib::Timeout
>
>The code looks something like this:
>
>my $dialog = new Gtk2:ialog( );
>
>(creating some buttons and all)
>
>my $timer = Glib::Timeout->add( $interval, \&some_func));
>
>Gtk2->main;
>
>
>This returns Not a CODE reference at /program_path line line_number


That seems to say that the sub some_func isn't declared yet.

>
>Should I use Glib (and how?) or is there any other function I should use.


All you need to do is return FALSE from the sub after the timeout
period. Timers keep running as long as they return TRUE from the
code reference.

$id = Glib::Timeout->add ($milliseconds, \&timeout_handler);

sub timeout_handler {
do_cool_stuff ();
return $keep_running_or_not;
}


but since it's a 1 shot timer, it can all be in one line,
for a 10 second timeout

my $id = Glib::Timeout->add (10000, sub{ return 0 } );


are you trying to make the dialog only stay up 10 seconds?
Only moderately tested code follows....................

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw(TRUE FALSE);
use Gtk2 '-init';

my $dialog = Gtk2:ialog->new ('Confirmation Dialog', undef, 'modal',
'gtk-ok' => 'ok', # response ids may be one of the built-in enums...
'_Cancel' => 2, # or any positive integer.
);

# put an hbox with a label and entry into the dialog's vbox area.
my $hbox = Gtk2::HBox->new (FALSE, 6);
$hbox->pack_start (Gtk2::Label->new ('Continue Real Transfer? '), TRUE,
TRUE, 0);
$hbox->show_all;

$dialog->vbox->pack_start ($hbox, TRUE, TRUE, 0);

# Set which response is the default:
$dialog->set_default_response ('ok');

# connect a signal to the dialog's response signal.
$dialog->signal_connect (response => sub {
# get our params
shift; # we don't need the dialog
my $response = shift; # the clicked button

print "The user clicked: $response\n";

if ($response eq 'ok')
{
Gtk2->main_quit;
}
elsif ($response eq 'delete-event')
{
$response = 2;
}
elsif ($response == 2)
{

}
});

# show the dialog
$dialog->show;

my $id = Glib::Timeout->add (5000, sub{
print "timeded out\n";
$dialog->destroy;
return FALSE;
});

Gtk2->main;
__END__


--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
 
Reply With Quote
 
 
 
 
Y.G.
Guest
Posts: n/a
 
      06-12-2006
zentara wrote:
> On Thu, 08 Jun 2006 18:47:17 +0200, "Y.G." <yg_blah_@this_domain.bla>
> wrote:
>
>> I am writing a program in perl which invokes a small window. This window
>> should time out after some time. For the window I use Gtk2. For the
>> timeout I use Glib::Timeout
>>
>> The code looks something like this:
>>
>> my $dialog = new Gtk2:ialog( );
>>
>> (creating some buttons and all)
>>
>> my $timer = Glib::Timeout->add( $interval, \&some_func));
>>
>> Gtk2->main;
>>
>>
>> This returns Not a CODE reference at /program_path line line_number

>
> That seems to say that the sub some_func isn't declared yet.
>
>> Should I use Glib (and how?) or is there any other function I should use.

>
> All you need to do is return FALSE from the sub after the timeout
> period. Timers keep running as long as they return TRUE from the
> code reference.
>
> $id = Glib::Timeout->add ($milliseconds, \&timeout_handler);
>
> sub timeout_handler {
> do_cool_stuff ();
> return $keep_running_or_not;
> }
>
>
> but since it's a 1 shot timer, it can all be in one line,
> for a 10 second timeout
>
> my $id = Glib::Timeout->add (10000, sub{ return 0 } );
>
>
> are you trying to make the dialog only stay up 10 seconds?
> Only moderately tested code follows....................
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Glib qw(TRUE FALSE);
> use Gtk2 '-init';
>
> my $dialog = Gtk2:ialog->new ('Confirmation Dialog', undef, 'modal',
> 'gtk-ok' => 'ok', # response ids may be one of the built-in enums...
> '_Cancel' => 2, # or any positive integer.
> );
>
> # put an hbox with a label and entry into the dialog's vbox area.
> my $hbox = Gtk2::HBox->new (FALSE, 6);
> $hbox->pack_start (Gtk2::Label->new ('Continue Real Transfer? '), TRUE,
> TRUE, 0);
> $hbox->show_all;
>
> $dialog->vbox->pack_start ($hbox, TRUE, TRUE, 0);
>
> # Set which response is the default:
> $dialog->set_default_response ('ok');
>
> # connect a signal to the dialog's response signal.
> $dialog->signal_connect (response => sub {
> # get our params
> shift; # we don't need the dialog
> my $response = shift; # the clicked button
>
> print "The user clicked: $response\n";
>
> if ($response eq 'ok')
> {
> Gtk2->main_quit;
> }
> elsif ($response eq 'delete-event')
> {
> $response = 2;
> }
> elsif ($response == 2)
> {
>
> }
> });
>
> # show the dialog
> $dialog->show;
>
> my $id = Glib::Timeout->add (5000, sub{
> print "timeded out\n";
> $dialog->destroy;
> return FALSE;
> });
>
> Gtk2->main;
> __END__
>
>

Seems to work. Thanks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] Builds of PyWebkitGtk and Webkit-Glib-Gtk(r39359+#16401.master) for Debian i386,Debian AMD64 and Macports MacOSX 10.4 Luke Kenneth Casson Leighton Python 0 12-31-2008 08:23 PM
Ruby GTK Fortune Cookies (gtk-fortune-0.1.1) Alex Shulgin Ruby 1 11-21-2007 01:05 AM
GTK+, segmentation fault in gtK-editable_get_chars zombek@gmail.com C Programming 2 05-29-2007 03:27 PM
compiling ruby under windows (+ gtk stuff, is the gtk list dead?) Gergely Kontra Ruby 2 06-18-2004 10:20 PM
glib/gtk and the standard Daniel C Bastos C Programming 3 07-07-2003 12:29 PM



Advertisments