Gtk2-Ex-Entry-Pango-0.09/0000755000175000017500000000000011314465161015156 5ustar emmanuelemmanuelGtk2-Ex-Entry-Pango-0.09/META.yml0000644000175000017500000000076311314465161016435 0ustar emmanuelemmanuel--- #YAML:1.0 name: Gtk2-Ex-Entry-Pango version: 0.09 abstract: Gtk2 Entry that accepts Pango markup. license: ~ author: - Emmanuel Rodriguez generated_by: ExtUtils::MakeMaker version 6.42 distribution_type: module requires: Gtk2: 1.100 Gtk2::Pango: 0 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.3.html version: 1.3 Gtk2-Ex-Entry-Pango-0.09/lib/0000755000175000017500000000000011314465161015724 5ustar emmanuelemmanuelGtk2-Ex-Entry-Pango-0.09/lib/Gtk2/0000755000175000017500000000000011314465161016533 5ustar emmanuelemmanuelGtk2-Ex-Entry-Pango-0.09/lib/Gtk2/Ex/0000755000175000017500000000000011314465161017107 5ustar emmanuelemmanuelGtk2-Ex-Entry-Pango-0.09/lib/Gtk2/Ex/Entry/0000755000175000017500000000000011314465161020210 5ustar emmanuelemmanuelGtk2-Ex-Entry-Pango-0.09/lib/Gtk2/Ex/Entry/Pango.pm0000644000175000017500000004356411314465114021624 0ustar emmanuelemmanuelpackage Gtk2::Ex::Entry::Pango; =head1 NAME Gtk2::Ex::Entry::Pango - Gtk2 Entry that accepts Pango markup. =head1 SYNOPSIS use Gtk2::Ex::Entry::Pango; # You can use any method defined in Gtk2::Entry or set_markup() my $entry = Gtk2::Ex::Entry::Pango->new(); $entry->set_markup('Pango is fun'); # Create a simple search field my $search = Gtk2::Ex::Entry::Pango->new(); $search->set_empty_markup("Search..."); # Realtime validation - accept only ASCII letters my $validation = Gtk2::Ex::Entry::Pango->new(); $validation->signal_connect(changed => sub { my $text = $validation->get_text; # Validate the entry's text if ($text =~ /^[a-z]*$/) { return; } # Mark the string as being erroneous my $escaped = Glib::Markup::escape_text($text); $validation->set_markup("$escaped"); $validation->signal_stop_emission_by_name('changed'); }); =head1 HIERARCHY C is a subclass of L. Glib::Object +----Glib::InitiallyUnowned +----Gtk2::Object +----Gtk2::Widget +----Gtk2::Entry +----Gtk2::Ex::Entry::Pango =head1 DESCRIPTION C is a C that can accept Pango markup for various purposes (for more information about Pango text markup language see L). The widget allows Pango markup to be used for input as an alternative to C or for setting a default value when the widget is empty. The default value when empty is ideal for standalone text entries that have no accompanying label (such as a text field for a search). This widget allows for the text data to be entered either through the normal methods provided by C or to use the method L. It's possible to switch between two methods for applying the text. The standard C methods will always apply a text without styles while C will use a style. The widget C keeps track of which style to apply by listening to the signal I. This has some important consequences. If an instance needs to provide it's own I listener that calls C then the signal I has to be stopped otherwise the layout will be lost. The following code snippet show how to stop the emission of the I signal: my $entry = Gtk2::Ex::Entry::Pango->new(); $entry->signal_connect(changed => sub { # Validate the text my $text = $entry->get_text; if (validate($text)) { return; } # Mark the text as being erroneous my $escaped = Glib::Markup::escape_text($text); $entry->set_markup("$escaped"); $entry->signal_stop_emission_by_name('changed'); }); Another important thing to note is that C will not update it's content if the input text is the same as the text already stored. This means that if set text is called with the same string it will not emit the signal I and the widget will not pickup that the markup styles have to be dropped. This is true even it the string displayed uses markup, as long as the contents are the same C will not make an update. The method L can be used for safely clearing the markup text. =head1 CAVEATS A C keeps track of both the text and the markup styles (Pango layout) as two different entities . The markup styles are just styles applied over the internal text. Because of this it's possible to have the widget display a different text than the one stored internally. Because a C keeps track of both the text and the style layouts. It's important to always keep track of both. If the styles and text are not totally synchronized strange things will happen. In the worst case it's even possible to make the C widget display a different text than the one stored (the text value). This can make things more confusing. This widget tries as hard as possible to synchronize the text data and the layout data. =head1 INTERFACES Glib::Object::_Unregistered::AtkImplementorIface Gtk2::Buildable Gtk2::CellEditable Gtk2::Editable =head1 METHODS The following methods are added by this widget: =head2 new Creates a new instance. =cut use strict; use warnings; use Glib qw(TRUE FALSE); # Gtk2 with Pango support use Gtk2 1.100; use Gtk2::Pango; use Carp; # Module version our $VERSION = '0.09'; # Emty Pango attributes list that's used to clear the previous markup my ($EMPTY_ATTRLIST) = ($Gtk2::VERSION >= 1.160) ? (Gtk2::Pango::AttrList->new()) : Gtk2::Pango->parse_markup('') ; # See http://gtk2-perl.sourceforge.net/doc/pod/Glib/Object/Subclass.html use Glib::Object::Subclass 'Gtk2::Entry' => signals => { 'changed' => \&callback_changed, 'expose-event' => \&callback_expose_event, 'button-press-event' => \&callback_button_press_event, 'markup-changed' => { flags => ['run-last'], param_types => ['Glib::String'], }, 'empty-markup-changed' => { flags => ['run-last'], param_types => ['Glib::String'], }, }, properties => [ Glib::ParamSpec->string( 'markup', 'Markup', 'The Pango markup used for displaying the contents of the entry.', '', ['writable'], ), Glib::ParamSpec->string( 'empty-markup', 'Markup when empty', 'The default Pango markup to display when the entry is empty.', '', ['readable', 'writable'], ), Glib::ParamSpec->boolean( 'clear-on-focus', 'Clear the markup when the widget has focus', 'If the Pango markup to display has to cleared when the entry has focus.', TRUE, ['readable', 'writable'], ), ], ; # # Gtk2 constructor. # sub INIT_INSTANCE { my $self = shift; # The Pango attributes to apply to the text. If set to undef then there are no # attributes and the text is rendered normally. $self->{attributes} = undef; # The Pango text and attributes to apply when the entry has no text. $self->{empty_attributes} = undef; $self->{empty_text} = ''; } # # Gtk2 generic property setter. # sub SET_PROPERTY { my ($self, $pspec, $value) = @_; my $field = $pspec->get_name; if ($field eq 'markup') { # The markup isn't stored, instead it is parsed and the attributes are # stored. $self->apply_markup($value); } elsif ($field eq 'empty_markup') { if (defined $value) { ($self->{empty_attributes}, $self->{empty_text}) = Gtk2::Pango->parse_markup($value); } else { ($self->{empty_attributes}, $self->{empty_text}) = (undef, ''); } $self->{$field} = $value; $self->signal_emit('empty-markup-changed' => $value); } else { $self->{$field} = $value; } } =head2 set_markup Sets the text of the entry using Pango markup. This method can die if the markup is not valid and fails to parse (see L). Parameters: =over =item * $markup The text to add to the entry, the text is expected to be using Pango markup. This means that even if no markup is used special characters like E, E, &, ' and " need to be escaped. Keep in mind that Pango markup is a subset of XML. You might want to use the following code snippet for escaping the characters: $entry->set_markup( sprintf "The %s %s fox %s over the lazy dog", map { Glib::Markup::escape_text($_) } qw(quick brown jumps) ); =back =cut sub set_markup { my $self = shift; my ($markup) = @_; # NOTE: In order to have the markup applied properly both the widget's # internal text value and the Pango style have to be applied. Calling # $self->get_layout->set_markup($markup); is not enough as it will only apply # the markup and render the text in $markup but will not update the internal # text representation of the widget. # # For instance, if the text within the markup differs from the actual text in # the Gtk2::Entry and changes in width there will be some problems. Sure the # entry's text will be rendered properly but the entry will not have the right # data within it's buffer. This means that $self->get_text() will still return # the old text even though the widget displays the new string. Furthermore, # the widget will fail to edit text because the cursor could be placed at a # position that's further than the actual data in the widget. # # To solve this problem the new text has to be added to the entry and the # style has to be applied afterwards. The text is added to the widget through # $self->set(text => $text); by the method apply_markup() while the styles are # applied each time that the widget is rendered (see callback_changed()). $self->set(markup => $markup); } =head2 clear_markup Clears the Pango markup that was applied to the widget. This method can be called even if no markup was applied previously. B: That this method will emit the signal I. =cut sub clear_markup { my $self = shift; $self->set_markup(undef); } =head2 set_empty_markup Sets the Pango markup that was applied to the widget when there's the entry is empty. This method can die if the markup is not valid and fails to parse (see L). C Setting an empty markup string has no effect on C. When an empty markup string is used the entry holds no data thus C will return an empty string. Parameters: =over =item * $markup The text to add to the entry, the text is expected to be using Pango markup. Make sure to escape all characters with L. For more details about escaping the markup see L. =back =cut sub set_empty_markup { my $self = shift; my ($markup) = @_; $self->set(empty_markup => $markup); } =head2 clear_empty_markup Clears the Pango markup that was applied to the widget. This method can be called even if no markup was applied previously. =cut sub clear_empty_markup { my $self = shift; $self->set_empty_markup(undef); } =head2 get_clear_on_focus Returns if the widget's Pango markup will be cleared once the widget is focused and has no user text. =cut sub get_clear_on_focus { my $self = shift; return $self->get('clear_on_focus'); } =head2 set_clear_on_focus Returns if the widget's Pango markup will be cleared once the widget is focused and has no user text. Parameters: =over =item * $value A boolean value that dictates if the Pango markup has to be cleared when the widget is focused and there's no text entered (the entry is empty). =back =cut sub set_clear_on_focus { my $self = shift; my ($value) = @_; return $self->set(clear_on_focus => $value); } # # Applies the markup to the widget. The markup string is parsed into a text to # be displayed and an attribute list (the styles to apply). The text is added # normally to the widget as if it was a Gtk2::Entry, while the attributes are # stored in order to be applied latter to the widget. # sub apply_markup { my $self = shift; my ($markup) = @_; # Parse the markup, this will die if the markup is invalid. my $text = ''; $self->{attributes} = undef; if (defined $markup) { ($self->{attributes}, $text) = Gtk2::Pango->parse_markup($markup); } if ($text eq $self->get_text) { # $widget->set_text() only changes the text if it's different, since this is # the same text we can just apply the markup and request a redraw. $self->set_layout_attributes(); $self->request_redraw(); } else { # Change the entry's text. Mark this as an internal change as we can't let # the 'changed' callback reset the markup. local $self->{internal} = TRUE; $self->set(text => $text); if ($self->{internal}) { # The signal 'changed' wasn't emited (it can happen sometimes) so lets # request a refresh of the UI manually. $self->request_redraw(); } } $self->signal_emit_markup_changed($markup); } # # Schedules a redraw of the widget. # # The text region must be invalidated in order to be repainted. This is true # even if the markup text is the same as the one in the widget. Remember that # the text in the Pango markup could turn out to be the same text that was # previously in the widget but with new styles (this is most common when showing # an error with a red underline). In such case the Gtk2::Entry will not refresh # its appearance because the text didn't change. Here we are forcing the update. # sub request_redraw { my $self = shift; return unless $self->realized; my $size = $self->allocation; my $rectangle = Gtk2::Gdk::Rectangle->new(0, 0, $size->width, $size->height); $self->window->invalidate_rect($rectangle, TRUE); } # # Notifies the others that the markup has changed by emitting the signal # 'markup-changed'. # sub signal_emit_markup_changed { my $self = shift; my ($markup) = @_; $self->signal_emit('markup-changed'=> $markup); } # # Applies the attributes to the widget. Gtk2::Pango::Layout::set_attributes() # doesn't accept an undef value (a patch has been submitted in order to address # this issue). So if the attributes are undef an empty attribute list has to be # submitted instead. # sub set_layout_attributes { my $self = shift; if ($self->get_text ne '') { # There's text in the widget apply the attributes (the requested pango # text). If the're attributes simply clear the previous ones. my $attributes = $self->{attributes}; if (! defined $attributes) { # Clear the previous attributes, just in case... $attributes = $EMPTY_ATTRLIST; } $self->get_layout->set_attributes($attributes); } elsif ($self->get_clear_on_focus and $self->has_focus) { # The widget has focus and is empty, if the user requested that it be # cleared when focused we have to honor it here. my $attributes = $EMPTY_ATTRLIST; $self->get_layout->set_text(''); $self->get_layout->set_attributes($attributes); return; } elsif ($self->{empty_markup}) { # The widget is empty and the user wants it filled with a default text at # all times. $self->get_layout->set_text($self->{empty_text}); $self->get_layout->set_attributes($self->{empty_attributes}); } } # # Called when the text of the entry is changed. The callback is used for monitor # when the user resets the text of the widget without markup. In that case we # need to erase the markup. # sub callback_changed { my $self = shift; if (! $self->{internal}) { # The text was changed as if it was a normal Gtk2::Entry either through # $widget->set_text($text) or $widget->set(text => $text). This means that # the markup style has to be removed from the widget. Now the widget will # rendered in plain text without any styles. $self->{attributes} = undef; $self->signal_emit_markup_changed(undef); } else { # Tell us that the callback was called $self->{internal} = FALSE; } # Apply the markup $self->set_layout_attributes(); $self->request_redraw(); return $self->signal_chain_from_overridden(@_); } # # Called each time that the widget needs to be rendered. This happens quite # often as an entry field can have a cursor blinking. Without this callback the # Pango style would be lost at each redraw. # sub callback_expose_event { my $self = shift; my ($event) = @_; $self->set_layout_attributes(); return $self->signal_chain_from_overridden(@_); } # # This handler stops the widget from generating critical Pango warnings when the # text selection gesture is performed. If there's no text in the widget we # simply cancel the gesture. # # The gesture is done with: mouse button 1 pressed and dragged over the widget # while the button is still pressed. # sub callback_button_press_event { my $self = shift; my ($event) = @_; if ($self->get_text or $event->button != 1) { # Propagate the event further since there's text in the widget return $self->signal_chain_from_overridden(@_); } # Give focus to the widget but stop the text selection $self->grab_focus(); return TRUE; } # Return a true value 1; =head1 PROPERTIES The following properties are added by this widget: =head2 markup (string: writable) The markup text used by this widget. This property is a string that's only writable. That's right, there's no way for extracting the markup from the widget. =head2 empty-markup (string: readable writable) The markup text used by this widget when the entry field is empty. If this property is set the entry will display a default string in the widget when there's no text provided by the user. =head2 clear-on-focus '', 'Clear the markup when the widget has focus', 'If the Pango markup to display has to cleared when the entry has focus.', TRUE, ['readable', 'writable'], (boolean: readable writable) Indicates if the C has to be cleared when the entry is empty and the widget has gained focus. =head1 SIGNALS =head2 markup-changed Emitted when the markup has been changed. Signature: sub markup_changed { my ($widget, $markup) = @_; # Returns nothing } Parameters: =over =item * $markup The new markup that's been applied. This field is a normal Perl string. If C<$markup> is C then the markup was removed. =back =head2 empty-markup-changed Emitted when the markup used when the widget is empty has been changed. Signature: sub empty_markup_changed { my ($widget, $markup) = @_; # Returns nothing } Parameters: =over =item * $markup The new markup that's been applied when the widget is empty. This field is a normal Perl string. If C<$markup> is C then the markup was removed. =back =head1 SEE ALSO Take a look at the examples for getting some ideas or inspiration on how to use this widget. For a more powerful text widget that supports more operations take a look at L. =head1 AUTHORS Emmanuel Rodriguez Epotyl@cpan.orgE. =head1 COPYRIGHT AND LICENSE Copyright (C) 2008-2009 by Emmanuel Rodriguez. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. =cut Gtk2-Ex-Entry-Pango-0.09/Changes0000644000175000017500000000243411314465114016452 0ustar emmanuelemmanuelRevision history for Perl extension Gtk2::Ex::Entry::Pango. 0.09 Wed Dec 23 19:53:57 CET 2009 - Issue 1 - test failure under locale. 0.08 Sun Dec 6 20:30:19 CET 2009 - Fix an undef warning when reverting to the default empty text. 0.07 Tue May 5 07:45:03 CEST 2009 - Bug fix: Old versions of Gtk2 would fail to have a proper $EMPTY_ATTRLIST Patch by Matthew Braid 0.06 Mon May 4 20:01:15 CEST 2009 - New release with all the changes from the 0.06_XX releases. 0.06_02 Sun May 3 09:16:07 CEST 2009 - Fixed the property 'clear-on-focus'. 0.06_01 Fri May 1 13:23:07 CEST 2009 - Default markup when the entry is empty is not displayed if when focused. - Depending on Gtk2::Pango and on a version of Gtk2 that has Pango support. 0.05 Thu Apr 30 09:34:57 CEST 2009 - New release with all the changes from the 0.05_XX releases. 0.05_01 Sun Feb 8 08:04:30 CET 2009 - More unit tests. 0.04 Mon Jan 5 20:32:42 CET 2009 - Fixed the return value in the unit tests. 0.03 Sat Jan 3 17:37:46 CET 2009 - Fixed the paste with a middle click when the widget is empty. - Fixed the right click popup menu when the widget is empty. 0.02 Fri Jan 2 22:08:00 CET 2009 - Can display a default value when the widget is empty. 0.01 Fri Jan 2 10:06:06 CET 2009 - Initial release. Gtk2-Ex-Entry-Pango-0.09/commands0000755000175000017500000000242311307004051016673 0ustar emmanuelemmanuel#!/usr/bin/make -f # Local installation place DEST=target VERSION=$(shell perl -le "print `grep VERSION lib/Gtk2/Ex/Entry/Pango.pm`") PACKAGE=Gtk2-Ex-Entry-Pango EMAIL=$(shell git config --global user.email) .PHONY: info info: @echo "EMAIL ${EMAIL}" @echo "VERSION ${VERSION}" @echo "PACKAGE ${PACKAGE}" .PHONY: install install: rm -rf ${DEST} || true mkdir -p ${DEST} perl Makefile.PL INSTALLDIRS=vendor && make && sudo make install PREFIX=target .PHONY: dist dist: ${PACKAGE}-${VERSION}.tar.gz ${PACKAGE}-${VERSION}.tar.gz: perl Makefile.PL && make && make dist .PHONY: distcheck distcheck: dist make distcheck .PHONY: test test: perl Makefile.PL && make && make test .PHONY: tag tag: git tag "${VERSION}" .PHONY: push push: git push --tags origin master .PHONY: upload upload: dist cpan-upload -verbose -mailto "${EMAIL}" -user potyl "${PACKAGE}-${VERSION}.tar.gz" .PHONY: release release: clean test dist distcheck tag push upload @echo "Release ${PACKAGE} ${VERSION} done." .PHONY: clean clean: - [ -f make ] && make clean > /dev/null 2>&1 || true -rm -f Makefile.old 2> /dev/null || true -rm -rf ${PACKAGE}-*/ 2> /dev/null || true -rm ${PACKAGE}-*.tar.gz 2> /dev/null || true -rm -f pm_to_blib 2> /dev/null || true -rm -rf blib 2> /dev/null || true Gtk2-Ex-Entry-Pango-0.09/t/0000755000175000017500000000000011314465161015421 5ustar emmanuelemmanuelGtk2-Ex-Entry-Pango-0.09/t/Gtk2-Ex-Entry-Pango.t0000644000175000017500000001331111314465114021125 0ustar emmanuelemmanuel#!/usr/bin/perl use strict; use warnings; use Carp; use Gtk2::TestHelper tests => 42; BEGIN { use_ok('Gtk2::Ex::Entry::Pango') }; my $MARKUP_VOID = -1; my $MAX_INT = 0; exit main(); sub main { test_set_markup(); test_set_empty_markup(); test_bad_usage(); return 0; } sub test_set_markup { my $entry = Gtk2::Ex::Entry::Pango->new(); # The styles always end at MAX INT and not at the lenght of the string. This # code finds the maximum size that a style can have. $MAX_INT = get_styles($entry)->[0][1]; ok($MAX_INT > 0); # Intercept all markup changes. $MARKUP_VOID indicates that the callback # wasn't called. my $markup_signal = $MARKUP_VOID; $entry->signal_connect(markup_changed => sub{ my ($widget, $markup) = @_; $markup_signal = $markup; }); # Use some markup $markup_signal = $MARKUP_VOID; $entry->set_markup("markup"); is($entry->get_text(), "markup"); is($markup_signal, "markup"); is_deeply( get_styles($entry), [ [0, 6, 'bold'], [6, $MAX_INT, undef], ] ); # Use some markup with the same text but the styles are different $markup_signal = $MARKUP_VOID; $entry->set_markup("markup"); is($entry->get_text(), "markup"); is($markup_signal, "markup"); is_deeply( get_styles($entry), [ [0, 1, undef], [1, 2, 'bold'], [2, 4, undef], [4, 5, 'bold'], [5, $MAX_INT, undef], ] ); # Try to remove the markup of the same input text. # NOTE: this fails as set_text() doesn't detect a text difference. $markup_signal = $MARKUP_VOID; $entry->set_text("markup"); is($entry->get_text(), "markup"); is($markup_signal, $MARKUP_VOID); is_deeply( get_styles($entry), [ [0, 1, undef], [1, 2, 'bold'], [2, 4, undef], [4, 5, 'bold'], [5, $MAX_INT, undef], ] ); # Reset the text $markup_signal = $MARKUP_VOID; $entry->set_text("reset"); is($entry->get_text(), "reset"); is($markup_signal, undef); is_deeply( get_styles($entry), [ [0, $MAX_INT, undef], ] ); # Use some markup $markup_signal = $MARKUP_VOID; $entry->set_markup("markup"); is($entry->get_text(), "markup"); is($markup_signal, "markup"); is_deeply( get_styles($entry), [ [0, 6, 'bold'], [6, $MAX_INT, undef], ] ); # Clear the markup $markup_signal = $MARKUP_VOID; $entry->clear_markup(); is($entry->get_text(), ""); is($markup_signal, undef); is_deeply( get_styles($entry), [ [0, $MAX_INT, undef], ] ); # Test the clear on focus property do_clear_on_focus($entry); } # # Testing set_emtpy_markup is tricky because the widget is not realized. This # means that the 'expose_event' signal is never called. In order to test this # we need to cheat a little bit and access the private data of the widget. # sub test_set_empty_markup { my $entry = Gtk2::Ex::Entry::Pango->new(); # The styles always end at MAX INT and not at the lenght of the string. This # code finds the maximum size that a style can have. $MAX_INT = get_styles($entry)->[0][1]; ok($MAX_INT > 0); # Intercept all markup changes. $MARKUP_VOID indicates that the callback # wasn't called. my $markup_signal = $MARKUP_VOID; $entry->signal_connect(empty_markup_changed => sub{ my ($widget, $markup) = @_; $markup_signal = $markup; }); # Use some markup $markup_signal = $MARKUP_VOID; $entry->set_empty_markup("markup"); $entry->request_redraw(); is($entry->get_text(), ""); is($markup_signal, "markup"); is_deeply( get_styles($entry, $entry->{empty_attributes}), [ [0, 6, 'bold'], [6, $MAX_INT, undef], ] ); # Use some markup with the same text but the styles are different $markup_signal = $MARKUP_VOID; $entry->set_empty_markup("markup"); is($entry->get_text(), ""); is($markup_signal, "markup"); is_deeply( get_styles($entry, $entry->{empty_attributes}), [ [0, 1, undef], [1, 2, 'bold'], [2, 4, undef], [4, 5, 'bold'], [5, $MAX_INT, undef], ] ); # Clear the markup $markup_signal = $MARKUP_VOID; $entry->clear_empty_markup(); is($entry->get_text(), ""); is($markup_signal, undef); is($entry->{empty_attributes}, undef); do_clear_on_focus($entry); } # # Generic tests on the property 'clear_on_focus' # sub do_clear_on_focus { my ($entry) = @_; # Test the clear on focus property my $count = 0; $entry->signal_connect('notify::clear-on-focus' => sub{++$count}); ok($entry->get_clear_on_focus); is($count, 0); $entry->set_clear_on_focus(FALSE); is($count, 1); ok(!$entry->get_clear_on_focus); } sub get_styles { my ($widget, $attributes) = @_; my @collected = (); $attributes ||= $widget->get_layout->get_attributes; my $iter = $attributes->get_iterator; do { my ($start, $end) = $iter->range; my $attribute = $iter->get('weight'); $attribute = defined $attribute ? $attribute->value : undef; push @collected, [$start, $end, $attribute]; } while ($iter->next); return \@collected; } # # Testing that using wrong input will throw an exception. # sub test_bad_usage { my $entry = Gtk2::Ex::Entry::Pango->new(); test_die( sub { $entry->set_markup("Me & You");}, "set_markup is passed a character not escaped", ); test_die( sub { $entry->set_markup("4 < 5");}, "set_markup is passed broken XML", ); test_die( sub { $entry->set_empty_markup("Me & You");}, "set_empty_markup is passed a character not escaped", ); test_die( sub { $entry->set_empty_markup("4 < 5");}, "set_empty_markup is passed broken XML", ); } sub test_die { my ($code, $name) = @_; croak "First parameter isn't a code referce (sub)" unless ref $code eq 'CODE'; my $test = 0; eval { $code->(); 1; } or do { $test = 1; }; my $tb = Test::More->builder; return $tb->ok($test, $name); } Gtk2-Ex-Entry-Pango-0.09/README0000644000175000017500000000202411264155300016027 0ustar emmanuelemmanuelGtk2-Ex-Entry-Pango =================== Gtk2::Ex::Entry::Pango is a Gtk2::Entry that can accept Pango markup as input (for more information about Pango text markup language see http://library.gnome.org/devel/pango/stable/PangoMarkupFormat.html). This widget allows for the text data to be entered either through the normal methods provided by Gtk2::Entry or to use the method set_markup. It's possible to switch between two methods for applying the text. The standard Gtk2::Entry methods will always apply a text without styles while set_markup() will use a style. INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES This module requires these other modules and libraries: Gtk2 COPYRIGHT AND LICENCE Copyright (C) 2008-2009 by Emmanuel Rodriguez This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. Gtk2-Ex-Entry-Pango-0.09/examples/0000755000175000017500000000000011314465161016774 5ustar emmanuelemmanuelGtk2-Ex-Entry-Pango-0.09/examples/live.pl0000755000175000017500000000254211264155300020271 0ustar emmanuelemmanuel#!/usr/bin/perl =head1 NAME simple.pl - Type in Pango markup and apply it on the fly. =head1 DESCRIPTION This sample program allows the user to type in Pango markup and to see the results live. =cut use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 '-init'; use Gtk2::Ex::Entry::Pango; exit main(); sub main { my $window = Gtk2::Window->new(); my $markup = Gtk2::Entry->new(); my $entry = Gtk2::Ex::Entry::Pango->new(); my $button_apply = Gtk2::Button->new('Apply'); my $hbox_bottom = new Gtk2::HBox(FALSE, 0); $hbox_bottom->pack_start($markup, TRUE, TRUE, 0); $hbox_bottom->pack_start($button_apply, FALSE, FALSE, 0); my $vbox = new Gtk2::VBox(FALSE, 0); $vbox->pack_start($entry, TRUE, TRUE, 0); $vbox->pack_start($hbox_bottom, TRUE, TRUE, 0); $window->set_focus_child($markup); $window->add($vbox); # Use Pango markup $entry->set_markup( 'Pango is fun' ); # Connect the signals $window->signal_connect(delete_event => sub { Gtk2->main_quit(); }); # Apply the user's Pango text $button_apply->signal_connect(clicked => sub { $markup->signal_emit('activate'); }); $markup->signal_connect(activate => sub { $entry->set_markup($markup->get_text); }); $window->set_default_size(450, -1); $window->show_all(); Gtk2->main(); return 0; } Gtk2-Ex-Entry-Pango-0.09/examples/simple.pl0000755000175000017500000000300611264155300020617 0ustar emmanuelemmanuel#!/usr/bin/perl =head1 NAME simple.pl - Apply Pango markup through buttons. =head1 DESCRIPTION This sample program provides some buttons that apply Pango markup. =cut use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 '-init'; use Gtk2::Ex::Entry::Pango; exit main(); sub main { my $window = Gtk2::Window->new(); my $entry = Gtk2::Ex::Entry::Pango->new(); my $button_markup1 = Gtk2::Button->new('Markup 1'); my $button_markup2 = Gtk2::Button->new('Markup 2'); my $button_text = Gtk2::Button->new('Text'); my $hbox_bottom = new Gtk2::HBox(FALSE, 0); $hbox_bottom->pack_start($button_markup1, FALSE, FALSE, 0); $hbox_bottom->pack_start($button_markup2, FALSE, FALSE, 0); $hbox_bottom->pack_start($button_text, FALSE, FALSE, 0); my $vbox = new Gtk2::VBox(FALSE, 0); $vbox->pack_start($entry, TRUE, TRUE, 0); $vbox->pack_start($hbox_bottom, TRUE, TRUE, 0); $window->set_focus_child($button_markup1); $window->add($vbox); # Use Pango markup $entry->set_markup( 'Pango is fun' ); # Connect the signals $window->signal_connect(delete_event => sub { Gtk2->main_quit(); }); $button_markup1->signal_connect(clicked => sub { $entry->set_markup('smaller text'); }); $button_markup2->signal_connect(clicked => sub { $entry->set_markup('smaller text'); }); $button_text->signal_connect(clicked => sub { $entry->set_text('smaOOOOller text'); }); $window->show_all(); Gtk2->main(); return 0; } Gtk2-Ex-Entry-Pango-0.09/examples/validation.pl0000755000175000017500000000317711264155300021471 0ustar emmanuelemmanuel#!/usr/bin/perl =head1 NAME validation.pl - Accepts only ASCII letters. =head1 DESCRIPTION This sample program shows how to make a simple text validation widget. This particular example considers ASCII letters as being the only valid characters, any other character will be underlined in red but still accepted by the widget. =cut use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 '-init'; use Gtk2::Ex::Entry::Pango; exit main(); sub main { my $window = Gtk2::Window->new(); my $entry = Gtk2::Ex::Entry::Pango->new(); my $vbox = new Gtk2::VBox(FALSE, 0); $vbox->pack_start($entry, FALSE, FALSE, FALSE); $vbox->set_focus_child($entry); $window->add($vbox); $entry->signal_connect(changed => \&on_change); $window->signal_connect(delete_event => sub { Gtk2->main_quit(); }); $window->show_all(); Gtk2->main(); return 0; } # # Each time that the text is changed we validate it. If there's an error within # the text we use Pango markup to highlight it. # sub on_change { my ($widget) = @_; my $string = $widget->get_text; # Validate the entry's text (accepting only letters) $string =~ s/([^a-z]+)/apply_pango_makup($1)/egi; $widget->set_markup($string); $widget->signal_stop_emission_by_name('changed'); } # # Applies Pango markup to the given text. The text has the conflicting XML # characters encoded with entities first. # sub apply_pango_makup { my ($text) = @_; # Escape the XML entities - MUST be done before applying the Pango markup $text = Glib::Markup::escape_text($text); # Apply the Pango markup to the escaped text return qq($text); } Gtk2-Ex-Entry-Pango-0.09/examples/podview.pl0000755000175000017500000000720611307002777021020 0ustar emmanuelemmanuel#!/usr/bin/perl =head1 NAME podview.pl - Simple POD viewer. =head1 SYNOPSIS podview.pl [page] =head1 DESCRIPTION This sample program shows how to use Gtk2::Ex::Entry::Pango in order to make a search box entry. This consists of a field that displays a default text (POD name...). In this particular case the POD file names entered are validated on the fly, if a page doesn't exist it is marked as being wrong with a red underlining stroke. This is a very basic POD viewer provided as a lame example. For a more complete program performing the same task take a look at L. =cut use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 '-init'; use Gtk2::Ex::Entry::Pango; use Pod::Simple::Search; use Pod::Simple; use Pod::Text; use IO::String; exit main(); sub main { my ($page) = @ARGV; # POD stuff my $pod_search = Pod::Simple::Search->new(); my $pod_parser = Pod::Text->new(sentence => 0, width => 80); my ($entry, $textview) = create_widgets(); $entry->set_empty_markup("POD name..."); # Check if there page entered exists (realtime validation) $entry->signal_connect('changed' => sub { my $page = $entry->get_text; if ($page eq "") { return; } # Check if the page exists my $file = $pod_search->find($page); if ($file) { return; } # The page doesn't exist, mark it as being wrong my $markup = Glib::Markup::escape_text($page); $markup = qq($markup); $entry->set_markup($markup); $entry->signal_stop_emission_by_name('changed'); }); # Load a POD page when enter is pressed $entry->signal_connect('activate' => sub { # The POD page to load my $page = $entry->get_text; if ($page eq "") { return; } # Find the POD file my $content; if (my $file = $pod_search->find($page)) { # Parse the POD file my $handle = IO::String->new(); $pod_parser->parse_from_file($file, $handle); $content = ${ $handle->string_ref }; } else { $content = "No such POD page: '$page'"; } # Update the POD text my $buffer = $textview->get_buffer; $buffer->set_text($content); # Set the text and scroll to the beginning $textview->scroll_to_iter($buffer->get_start_iter, 0.0, TRUE, 0.0, 0.0); }); # Load a default page if (@ARGV) { my ($page) = @ARGV; $entry->set_text($page); } Gtk2->main(); return 0; } # Creates the widgets and returns the main widgets to be used by the # application. sub create_widgets { my $window = Gtk2::Window->new(); # Search entry field my $entry = Gtk2::Ex::Entry::Pango->new(); # Textview where the POD document will be displayed my $textview = Gtk2::TextView->new(); $textview->set_size_request(600, 400); $textview->set_editable(FALSE); $textview->set_cursor_visible(FALSE); my $scrolls = Gtk2::ScrolledWindow->new(undef, undef); $scrolls->set_policy('automatic', 'always'); $scrolls->set_shadow_type('out'); $scrolls->add($textview); my $label = Gtk2::Label->new("POD Page: "); my $button = Gtk2::Button->new("Search"); # Packaging my $top_box = Gtk2::HBox->new(); $top_box->pack_start($label, FALSE, FALSE, 0); $top_box->pack_start($entry, TRUE, TRUE, 0); $top_box->pack_start($button, FALSE, FALSE, 0); my $vbox = Gtk2::VBox->new(); $vbox->pack_start($top_box, FALSE, FALSE, 0); $vbox->pack_start($scrolls, TRUE, TRUE, 0); $window->add($vbox); $window->signal_connect(delete_event => sub { Gtk2->main_quit(); }); $button->signal_connect(clicked => sub { $entry->signal_emit('activate'); }); $button->grab_focus(); $window->show_all(); return ($entry, $textview); } Gtk2-Ex-Entry-Pango-0.09/MANIFEST0000644000175000017500000000037611314465161016315 0ustar emmanuelemmanuelcommands Changes Makefile.PL MANIFEST README examples/live.pl examples/simple.pl examples/podview.pl examples/validation.pl t/Gtk2-Ex-Entry-Pango.t lib/Gtk2/Ex/Entry/Pango.pm META.yml Module meta-data (added by MakeMaker) Gtk2-Ex-Entry-Pango-0.09/Makefile.PL0000644000175000017500000000063311264155300017125 0ustar emmanuelemmanuel#!/usr/bin/perl use strict; use warnings; use 5.006000; use ExtUtils::MakeMaker; my $file = 'lib/Gtk2/Ex/Entry/Pango.pm'; WriteMakefile( NAME => 'Gtk2::Ex::Entry::Pango', VERSION_FROM => $file, ABSTRACT_FROM => $file, AUTHOR => 'Emmanuel Rodriguez ', PREREQ_PM => { 'Gtk2' => '1.100', # Pango was first introduced here 'Gtk2::Pango' => 0, }, );