[Fwd: Gtk and Glib::Timeout]

Discussion in 'Perl Misc' started by Y.G., Jun 8, 2006.

  1. Y.G.

    Y.G. Guest

    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::Dialog( );

    (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.
    Y.G., Jun 8, 2006
    #1
    1. Advertising

  2. Y.G.

    zentara Guest

    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::Dialog( );
    >
    >(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::Dialog->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
    zentara, Jun 9, 2006
    #2
    1. Advertising

  3. Y.G.

    Y.G. Guest

    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::Dialog( );
    >>
    >> (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::Dialog->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
    Y.G., Jun 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel C Bastos

    glib/gtk and the standard

    Daniel C Bastos, Jul 6, 2003, in forum: C Programming
    Replies:
    3
    Views:
    533
    Dan Pop
    Jul 7, 2003
  2. Davide Carboni
    Replies:
    0
    Views:
    357
    Davide Carboni
    Dec 18, 2003
  3. Luke Kenneth Casson Leighton
    Replies:
    0
    Views:
    467
    Luke Kenneth Casson Leighton
    Dec 31, 2008
  4. Tor Lillqvist
    Replies:
    0
    Views:
    427
    Tor Lillqvist
    Dec 23, 2009
  5. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,279
    Brian Candler
    Oct 6, 2004
Loading...

Share This Page