Re: Gtk2::Entry with Pango markup

On Tue, 25 Nov 2008 12:47:43 +0100
"Emmanuel Rodriguez" <emmanuel rodriguez gmail com> wrote:


I'm have Gtk2::Entry where users are expected to enter an XPath
expression. I would like to let the user know when an expression isn't
valid. I thought of using Pango markup but it seems very hard to do
with a Gtk2::Entry.

For the moment I can use a callback to 'changed' and set the markup this way:
$widget->get_layout->set_markup("<span underline='error'

The problem is that it doesn't work too well. The markup is not always
shown. When I edit the widget for the first time the error is not
displayed only after I start erasing at least one character while
keeping the XPath expression invalid.

I tried using a Gtk2::TextView but I didn't find out how to give the
text view the exact height of one line (just as a Gtk2::Entry).

Is there another way of achieving this?

Emmanuel Rodriguez

This is just a buggy prototype of what you might do.
Manually take control of the Entry and analyze it key-by-key,
probably with a regex for good xpath syntax,
and set the appropriate markup and entry text.

#!/usr/bin/perl -w
use strict;
use Gtk2::Gdk::Keysyms;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;

my $window = Gtk2::Window->new;
$window->signal_connect (delete_event => sub { Gtk2->main_quit });
my $vbox = new Gtk2::VBox( FALSE, 0 );
my $entry = Gtk2::Entry->new();

$vbox->pack_start( $entry, FALSE, FALSE, FALSE );
$vbox->set_focus_child ($entry); # works
$window->add ($vbox);

my $string = '';

$entry->signal_connect ('key-press-event' => sub {
         my ($widget,$event)= @_;
         my $key =  chr( $event->keyval );
         # do your checking and markup here
        $string = $string.$key;

        #calculate string length and set cursor position

        #False let the event propagate, so entry will still work
        #but we want manual control
        #return FALSE;
        return TRUE;





I'm not really a human, but I play one on earth. 

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