"mattia.bar...@libero.it" <mattia.bar...@libero.it> writes:

>   Wx::StaticBitmap and Wx::StaticText are not meant to be "active";
> actually, I'm surprised that on Windows you get the mouse up events;

When playing with Mark's suggestions I noticed that StaticText and
StaticBitmap get both the mouse up and mouse down events.

Following Mark's suggestions, I've changed the setup to:

  $widget = Wx::StaticBitmap->new($panel, -1, ... );
  My::EvtHandler->new($widget);

  My::EvtHandler->new($panel);
  Wx::Event::EVT_COMMAND($panel, -1,
                         $My::EvtHandler::TRACK_MOUSEUP_EVENT,
                         sub {
                           $_[0]->SetBackgroundColour($back);
                           ... } );
  Wx::Event::EVT_COMMAND($panel, -1,
                         $My::EvtHandler::TRACK_MOUSEDOWN_EVENT,
                         sub {
                           $_[0]->SetBackgroundColour($flash);
                           ... } );

(My::EvtHandler at end.)

Now the events are fired when the StaticBitmap etc. is clicked, and also
when the click is elsewhere inside the panel.

On Linux, this does what it is supposed to do: it momentarily flashes
the background while the widget/panel is clicked, to provide visual
feedback.

On Windows, this doesn't work. In contrast to my earlier experiments the
events are triggered, but the background doesn't change. 

When I change the event subroutines to:

          $_[0]->SetBackgroundColour(...);
          $_[0]->Layout;

The background changes, but only for the widget, not for the whole
panel. When I print $_[0] is does say Wx::Panel, so it must be the panel
receiving the event and I'd expect the panel to change it's colour.

So apparently, I'm still overlooking something.

Mattia> if you need a clickable image, it's better to use a Wx::Window
Mattia> directly. 

Could you elaborate on that?

Finally, as promised:

---- snip ----
package My::EvtHandler;

use strict;
use warnings;
use Wx;
use Wx::Event qw( EVT_LEFT_DOWN EVT_LEFT_UP );
use base qw (Wx::EvtHandler);

our $TRACK_MOUSEUP_EVENT   = Wx::NewEventType;
our $TRACK_MOUSEDOWN_EVENT = Wx::NewEventType;

sub new {
   my ($class, $window) = @_;
   my $handler = $class->SUPER::new;
   $window->PushEventHandler( $handler );

   EVT_LEFT_DOWN($window,sub { $handler->OnMouse( $window, $_[1] ); });
   EVT_LEFT_UP($window,sub   { $handler->OnMouse( $window, $_[1] ); });

   return $handler;
}

sub OnMouse {
   my($handler, $window, $event) = @_;
   $event->Skip(1);
   my $id = $window->GetId;
   my $newevent = Wx::CommandEvent->new(
                $event->LeftDown
                ? $TRACK_MOUSEDOWN_EVENT
                : $TRACK_MOUSEUP_EVENT,
                $id);
   $window->ProcessEvent($newevent);
}
---- snip ----

-- Johan

Reply via email to