Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello folks,

I am not quite sure whether the solution to the problem described below which I intend to implement is, firstly, feasible and, secondly, could be done in a less cumbersome way, potentially using "built-in" features of Gtk.

The problem:

I created a GUI which, when the user wants to save his work, presents the user a summary in a notebook page which has to be accepted or rejected ("Ok" and "Cancel" button at the bottom of the page) by the user prior to the actual saving . Most of the GUI is deactivated at this point, but a few buttons and the notebook page with the results to be saved stay active, i.e., a part of the GUI stays responsive while waiting for the user to press one of the two buttons (it could be necessary for the user to switch to the other notebook page prior to proceed with saving; if any other button is pressed apart from the both mentioned the user will be asked to only press "Ok" or "Cancel" since this is the only way to proceed with the application).

On the code level this means that the application should wait within the _save_work() subroutine for a 'button-press' event and, when it was passed to either the "Ok"-button or the "Cancel"-button, the program should execute the necessary actions and leave the loop. If any other event will occur the application should stay in that loop to force the user to press one of the buttons.

The code fragments I have in mind look as follows (for the sake of simplicity I present it only for the "Ok"-button, "Cancel"-button should be analogous):

... use Scalar::Util; use Glib qw(TRUE FALSE); ... sub _init { ... # Creating the "Ok"-button $button_ok = Gtk2::ToolButton->new( undef, 'Ok' ); $self->{ButtonOk} = $button_ok; # Connecting the signal-handler to it $button_ok->signal_connect( 'clicked' => sub { $self->_ok_pressed( @_ +) } ); } # The signal handler sub _ok_pressed { my ( $self, $button, $event ) = @_; # Make button and event object properties to have them available ou +tside the # signal handler's scope $self->{EventHash}->{Button} = $button; $self->{EventHash}->{Event} = $event; # Clear the summary, navigate to other notebook page ... } # The _save_work subroutine sub _save_work { # Parameter processing, declarations, etc. ... # The actual loop which waits for the event while ( TRUE ) { # Give the main loop the chance to intercept events with the +program # being in this loop while ( Gtk2->events_pending() ) { Gtk2->main_iteration(); } if ( %{$self->{EventHash}} ) { # This checks whether the expected TYPE of event ocurred and wheth +er it # was passed to the expected WIDGET (the "Ok"-button) if ( ( refaddr( $self->{EventHash}->{Button} ) == refaddr( $self->{ButtonOk} ) ) && ( defined( $self->{EventHash}->{Event}->{'type'} ) + ) && ( $self->{EventHash}->{Event}->{'type'} eq 'button +_press' ) && ( $self->{EventHash}->{Event}->{'button'} == 1 ) ) + { # Code executed after pressing the "Ok"-button goes here ... $self->{EventHash} = (); last; } } } }

To me, this approach seems a little bit cumbersome (and, consequently, not very readable)

Any suggestions for a more straightforward solution to the problem?



In reply to Gtk2-Perl: Waiting for particular event by Bloehdian

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and a moth chases the moon...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (2)
    As of 2018-05-23 00:03 GMT
    Find Nodes?
      Voting Booth?