Widgets hidden in a text still taking focus?



Hi all,

I'm writing an app where I need to embed some widgets in a
Gtk2::TextView. This works fine, however sometimes those widgets need
to be hidden. I've run into an issue where if a hidden widget
'overlaps' a visible widget it will steal the mouse focus from the
visible widget.

Its a bit hard to explain, but the following script demonstrates it
nicely. Buttons 1 and 2, when clicked, print their number. Button 0
toggles Button 1 between hidden and visible. If you hit button 0 and
then mouse over button 2, you'll notice that button 2 only 'lights up'
when the mouse is near its bottom edge. Clicking on Button 2 when the
mouse is over it but its not lit fires Button 1's callback, even
though I shouldn't even be able to select it.

I _can_ fix this by making sure that any widget that gets hidden by
application of the HIDDEN text tag are either remove'd from the
textview or hide'n (see the commented code), but I'm just wondering if
this is necessary or am I forgetting something else?

Thanks,
MB

use strict;
use Gtk2 '-init';

my $win = Gtk2::Window->new;
my $scrl = Gtk2::ScrolledWindow->new;
my $txt = Gtk2::TextView->new;
$scrl->add($txt);
$scrl->show;
$win->add($scrl);
$txt->show;
$txt->set_editable(0);
my $buf = $txt->get_buffer;
$buf->create_tag('HIDDEN', invisible => 1);
#$buf->create_tag('HIDDEN', background => 'red');
my (@anchs, @hbars, @buttons);
# Create [BAR]\n[BUTTON]\n x 3
for my $i (0..2) {
  my $anch = $buf->create_child_anchor($txt->get_buffer->get_end_iter);
  my $hbar = Gtk2::HSeparator->new;
  $txt->add_child_at_anchor($hbar, $anch);
  $buf->insert($buf->get_end_iter, "\n");
  my $anch2 = $buf->create_child_anchor($txt->get_buffer->get_end_iter);
  my $but = Gtk2::Button->new_with_label("CLICK ME $i");
  $txt->add_child_at_anchor($but, $anch2);
  $buf->insert($buf->get_end_iter, "\n");
  push @anchs, [$anch, $anch2];
  push @hbars, $hbar;
  push @buttons, $but;
  $hbar->show;
  $but->show;
}
# Button 1 when clicked hides/unhides Button 2
my $showing = 1;
sub toggle_button {
  my $start = $buf->get_iter_at_child_anchor($anchs[1][0]);
  my $end = $buf->get_iter_at_child_anchor($anchs[1][1]);
  $end->forward_chars(2);
  if ($showing) {
    $buf->apply_tag_by_name('HIDDEN', $start, $end);
# One of the following lines will fix the problem
#    $buttons[1]->hide; $hbars[1]->hide;
#    $txt->remove($buttons[1]); $txt->remove($hbars[1]);
  } else {
    $buf->remove_tag_by_name('HIDDEN', $start, $end);
# One of the following lines will fix the problem
#    $buttons[1]->show; $hbars[1]->show;
#    $txt->add_child_at_anchor($buttons[1], $anchs[1][1]);
$txt->add_child_at_anchor($hbars[1], $anchs[1][0]);
  }
  $showing++;
  $showing %= 2;
}

sub show_button {
  my ($num) = @_;
  print "BUTTON NUMBER $num HAS BEEN HIT!\n";
}

$buttons[0]->signal_connect(clicked => sub {toggle_button()});
$buttons[1]->signal_connect(clicked => sub {show_button(1)});
$buttons[2]->signal_connect(clicked => sub {show_button(2)});
$txt->signal_connect(size_allocate =>
                     sub {my $rec = $txt->allocation;
                          my $wid = $rec->width;
                          for my $hbar (@hbars) {
                            $hbar->set_size_request($wid - 4, 4);
                          }
                          0;
                        });
$win->signal_connect('delete-event' => sub {Gtk2->main_quit});
$win->show;
Gtk2->main;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]