blib/lib/Gtk2/Ex/Entry/Pango.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 188 CC 14 SCAR 23.8
CC
14
Cov
0%
set_layout_attributes
30.0
SET_PROPERTY
20.0
apply_markup
20.0
CRAP 10.8
1 package Gtk2::Ex::Entry::Pango;
2  
3 =head1 NAME
4  
5 Gtk2::Ex::Entry::Pango - Gtk2 Entry that accepts Pango markup.
6  
7 =head1 SYNOPSIS
8  
9 use Gtk2::Ex::Entry::Pango;
10
11
12 # You can use any method defined in Gtk2::Entry or set_markup()
13 my $entry = Gtk2::Ex::Entry::Pango->new();
14 $entry->set_markup('<i>Pan</i><b>go</b> is <span color="red">fun</span>');
15
16
17 # Create a simple search field
18 my $search = Gtk2::Ex::Entry::Pango->new();
19 $search->set_empty_markup("<span color='grey' size='smaller'>Search...</span>");
20
21
22 # Realtime validation - accept only ASCII letters
23 my $validation = Gtk2::Ex::Entry::Pango->new();
24 $validation->signal_connect(changed => sub {
25 my $text = $validation->get_text;
26
27 # Validate the entry's text
28 if ($text =~ /^[a-z]*$/) {
29 return;
30 }
31
32 # Mark the string as being erroneous
33 my $escaped = Glib::Markup::escape_text($text);
34 $validation->set_markup("<span underline='error' underline_color='red'>$escaped</span>");
35 $validation->signal_stop_emission_by_name('changed');
36 });
37  
38 =head1 HIERARCHY
39  
40 C<Gtk2::Ex::Entry::Pango> is a subclass of L<Gtk2::Entry>.
41  
42 Glib::Object
43 +----Glib::InitiallyUnowned
44 +----Gtk2::Object
45 +----Gtk2::Widget
46 +----Gtk2::Entry
47 +----Gtk2::Ex::Entry::Pango
48  
49 =head1 DESCRIPTION
50  
51 C<Gtk2::Ex::Entry::Pango> is a C<Gtk2::Entry> that can accept Pango markup for
52 various purposes (for more information about Pango text markup language see
53 L<http://library.gnome.org/devel/pango/stable/PangoMarkupFormat.html>).
54  
55 The widget allows Pango markup to be used for input as an alternative to
56 C<set_text> or for setting a default value when the widget is empty. The default
57 value when empty is ideal for standalone text entries that have no accompanying
58 label (such as a text field for a search).
59  
60 This widget allows for the text data to be entered either through the normal
61 methods provided by C<Gtk2::Entry> or to use the method L</set_markup>. It's
62 possible to switch between two methods for applying the text. The standard
63 C<Gtk2::Entry> methods will always apply a text without styles while
64 C<set_markup()> will use a style.
65  
66 The widget C<Gtk2::Ex::Entry::Pango> keeps track of which style to apply by
67 listening to the signal I<changed>. This has some important consequences. If an
68 instance needs to provide it's own I<changed> listener that calls
69 C<set_markup()> then the signal I<changed> has to be stopped otherwise the
70 layout will be lost. The following code snippet show how to stop the emission of
71 the I<changed> signal:
72  
73 my $entry = Gtk2::Ex::Entry::Pango->new();
74 $entry->signal_connect(changed => sub {
75
76 # Validate the text
77 my $text = $entry->get_text;
78 if (validate($text)) {
79 return;
80 }
81
82 # Mark the text as being erroneous
83 my $escaped = Glib::Markup::escape_text($text);
84 $entry->set_markup("<span underline='error' underline_color='red'>$escaped</span>");
85 $entry->signal_stop_emission_by_name('changed');
86 });
87  
88 Another important thing to note is that C<Gtk2::Entry::set_text()> will not
89 update it's content if the input text is the same as the text already stored.
90 This means that if set text is called with the same string it will not emit the
91 signal I<changed> and the widget will not pickup that the markup styles have to
92 be dropped. This is true even it the string displayed uses markup, as long as
93 the contents are the same C<set_text()> will not make an update. The method
94 L</clear_markup> can be used for safely clearing the markup text.
95  
96 =head1 CAVEATS
97  
98 A C<Gtk2::Entry> keeps track of both the text and the markup styles (Pango
99 layout) as two different entities . The markup styles are just styles applied
100 over the internal text. Because of this it's possible to have the widget display
101 a different text than the one stored internally.
102  
103 Because a C<Gtk2::Entry> keeps track of both the text and the style layouts.
104 It's important to always keep track of both. If the styles and text are not
105 totally synchronized strange things will happen. In the worst case it's even
106 possible to make the C<Gtk2::Entry> widget display a different text than the one
107 stored (the text value). This can make things more confusing.
108  
109 This widget tries as hard as possible to synchronize the text data and the
110 layout data.
111  
112 =head1 INTERFACES
113  
114 Glib::Object::_Unregistered::AtkImplementorIface
115 Gtk2::Buildable
116 Gtk2::CellEditable
117 Gtk2::Editable
118  
119 =head1 METHODS
120  
121 The following methods are added by this widget:
122  
123 =head2 new
124  
125 Creates a new instance.
126  
127 =cut
128  
129  
130 use strict;
131 use warnings;
132  
133 use Glib qw(TRUE FALSE);
134 # Gtk2 with Pango support
135 use Gtk2 1.100;
136 use Gtk2::Pango;
137 use Carp;
138  
139 # Module version
140 our $VERSION = '0.10';
141  
142  
143 # Emty Pango attributes list that's used to clear the previous markup
144 my ($EMPTY_ATTRLIST) = ($Gtk2::VERSION >= 1.160)
145 ? (Gtk2::Pango::AttrList->new())
146 : Gtk2::Pango->parse_markup('')
147 ;
148  
149  
150 # See http://gtk2-perl.sourceforge.net/doc/pod/Glib/Object/Subclass.html
151 use Glib::Object::Subclass 'Gtk2::Entry' =>
152  
153 signals => {
154 'changed' => \&callback_changed,
155 'expose-event' => \&callback_expose_event,
156 'button-press-event' => \&callback_button_press_event,
157  
158 'markup-changed' => {
159 flags => ['run-last'],
160 param_types => ['Glib::String'],
161 },
162  
163 'empty-markup-changed' => {
164 flags => ['run-last'],
165 param_types => ['Glib::String'],
166 },
167 },
168  
169  
170 properties => [
171 Glib::ParamSpec->string(
172 'markup',
173 'Markup',
174 'The Pango markup used for displaying the contents of the entry.',
175 '',
176 ['writable'],
177 ),
178  
179 Glib::ParamSpec->string(
180 'empty-markup',
181 'Markup when empty',
182 'The default Pango markup to display when the entry is empty.',
183 '',
184 ['readable', 'writable'],
185 ),
186  
187 Glib::ParamSpec->boolean(
188 'clear-on-focus',
189 'Clear the markup when the widget has focus',
190 'If the Pango markup to display has to cleared when the entry has focus.',
191 TRUE,
192 ['readable', 'writable'],
193 ),
194 ],
195 ;
196  
197  
198  
199 #
200 # Gtk2 constructor.
201 #
202 sub INIT_INSTANCE {
203 my $self = shift;
204  
205 # The Pango attributes to apply to the text. If set to undef then there are no
206 # attributes and the text is rendered normally.
207 $self->{attributes} = undef;
208
209 # The Pango text and attributes to apply when the entry has no text.
210 $self->{empty_attributes} = undef;
211 $self->{empty_text} = '';
212 }
213  
214  
215  
216 #
217 # Gtk2 generic property setter.
218 #
219 sub SET_PROPERTY {
220 my ($self, $pspec, $value) = @_;
221
222 my $field = $pspec->get_name;
223  
224 if ($field eq 'markup') {
225 # The markup isn't stored, instead it is parsed and the attributes are
226 # stored.
227 $self->apply_markup($value);
228 }
229 elsif ($field eq 'empty_markup') {
230 if (defined $value) {
231 ($self->{empty_attributes}, $self->{empty_text}) = Gtk2::Pango->parse_markup($value);
232 }
233 else {
234 ($self->{empty_attributes}, $self->{empty_text}) = (undef, '');
235 }
236 $self->{$field} = $value;
237 $self->signal_emit('empty-markup-changed' => $value);
238 }
239 else {
240 $self->{$field} = $value;
241 }
242 }
243  
244  
245  
246 =head2 set_markup
247  
248 Sets the text of the entry using Pango markup. This method can die if the markup
249 is not valid and fails to parse (see L<Gtk2::Pango/parse_markup>).
250  
251 Parameters:
252  
253 =over
254  
255 =item * $markup
256  
257 The text to add to the entry, the text is expected to be using Pango markup.
258 This means that even if no markup is used special characters like E<lt>, E<gt>,
259 &, ' and " need to be escaped. Keep in mind that Pango markup is a subset of
260 XML.
261  
262 You might want to use the following code snippet for escaping the characters:
263  
264 $entry->set_markup(
265 sprintf "The <i>%s</i> <b>%s</b> fox <sup>%s</sup> over the lazy dog",
266 map { Glib::Markup::escape_text($_) } qw(quick brown jumps)
267 );
268  
269 =back
270  
271 =cut
272  
273 sub set_markup {
274 my $self = shift;
275 my ($markup) = @_;
276  
277 # NOTE: In order to have the markup applied properly both the widget's
278 # internal text value and the Pango style have to be applied. Calling
279 # $self->get_layout->set_markup($markup); is not enough as it will only apply
280 # the markup and render the text in $markup but will not update the internal
281 # text representation of the widget.
282 #
283 # For instance, if the text within the markup differs from the actual text in
284 # the Gtk2::Entry and changes in width there will be some problems. Sure the
285 # entry's text will be rendered properly but the entry will not have the right
286 # data within it's buffer. This means that $self->get_text() will still return
287 # the old text even though the widget displays the new string. Furthermore,
288 # the widget will fail to edit text because the cursor could be placed at a
289 # position that's further than the actual data in the widget.
290 #
291 # To solve this problem the new text has to be added to the entry and the
292 # style has to be applied afterwards. The text is added to the widget through
293 # $self->set(text => $text); by the method apply_markup() while the styles are
294 # applied each time that the widget is rendered (see callback_changed()).
295 $self->set(markup => $markup);
296 }
297  
298  
299  
300 =head2 clear_markup
301  
302 Clears the Pango markup that was applied to the widget. This method can be
303 called even if no markup was applied previously.
304  
305 B<NOTE>: That this method will emit the signal I<markup-changed>.
306  
307 =cut
308  
309 sub clear_markup {
310 my $self = shift;
311 $self->set_markup(undef);
312 }
313  
314  
315  
316 =head2 set_empty_markup
317  
318 Sets the Pango markup that was applied to the widget when there's the entry is
319 empty. This method can die if the markup is not valid and fails to parse
320 (see L<Gtk2::Pango/parse_markup>).
321  
322 C<NOTE:> Setting an empty markup string has no effect on C<get_text>. When an
323 empty markup string is used the entry holds no data thus C<get_text> will return
324 an empty string.
325  
326 Parameters:
327  
328 =over
329  
330 =item * $markup
331  
332 The text to add to the entry, the text is expected to be using Pango markup.
333 Make sure to escape all characters with L<Glib::Markup/escape_text>. For more
334 details about escaping the markup see L</set_markup>.
335  
336 =back
337  
338 =cut
339  
340 sub set_empty_markup {
341 my $self = shift;
342 my ($markup) = @_;
343 $self->set(empty_markup => $markup);
344 }
345  
346  
347  
348 =head2 clear_empty_markup
349  
350 Clears the Pango markup that was applied to the widget. This method can be
351 called even if no markup was applied previously.
352  
353 =cut
354  
355 sub clear_empty_markup {
356 my $self = shift;
357 $self->set_empty_markup(undef);
358 }
359  
360  
361  
362 =head2 get_clear_on_focus
363  
364 Returns if the widget's Pango markup will be cleared once the widget is focused
365 and has no user text.
366  
367 =cut
368  
369 sub get_clear_on_focus {
370 my $self = shift;
371 return $self->get('clear_on_focus');
372 }
373  
374  
375  
376 =head2 set_clear_on_focus
377  
378 Returns if the widget's Pango markup will be cleared once the widget is focused
379 and has no user text.
380  
381 Parameters:
382  
383 =over
384  
385 =item * $value
386  
387 A boolean value that dictates if the Pango markup has to be cleared when the
388 widget is focused and there's no text entered (the entry is empty).
389  
390 =back
391  
392 =cut
393  
394 sub set_clear_on_focus {
395 my $self = shift;
396 my ($value) = @_;
397 return $self->set(clear_on_focus => $value);
398 }
399  
400  
401 #
402 # Applies the markup to the widget. The markup string is parsed into a text to
403 # be displayed and an attribute list (the styles to apply). The text is added
404 # normally to the widget as if it was a Gtk2::Entry, while the attributes are
405 # stored in order to be applied latter to the widget.
406 #
407 sub apply_markup {
408 my $self = shift;
409 my ($markup) = @_;
410  
411 # Parse the markup, this will die if the markup is invalid.
412 my $text = '';
413 $self->{attributes} = undef;
414 if (defined $markup) {
415 ($self->{attributes}, $text) = Gtk2::Pango->parse_markup($markup);
416 }
417  
418
419 if ($text eq $self->get_text) {
420 # $widget->set_text() only changes the text if it's different, since this is
421 # the same text we can just apply the markup and request a redraw.
422 $self->set_layout_attributes();
423 $self->request_redraw();
424 }
425 else {
426 # Change the entry's text. Mark this as an internal change as we can't let
427 # the 'changed' callback reset the markup.
428 local $self->{internal} = TRUE;
429 $self->set(text => $text);
430
431 if ($self->{internal}) {
432 # The signal 'changed' wasn't emited (it can happen sometimes) so lets
433 # request a refresh of the UI manually.
434 $self->request_redraw();
435 }
436 }
437  
438 $self->signal_emit_markup_changed($markup);
439 }
440  
441  
442  
443 #
444 # Schedules a redraw of the widget.
445 #
446 # The text region must be invalidated in order to be repainted. This is true
447 # even if the markup text is the same as the one in the widget. Remember that
448 # the text in the Pango markup could turn out to be the same text that was
449 # previously in the widget but with new styles (this is most common when showing
450 # an error with a red underline). In such case the Gtk2::Entry will not refresh
451 # its appearance because the text didn't change. Here we are forcing the update.
452 #
453 sub request_redraw {
454 my $self = shift;
455  
456 return unless $self->realized;
457  
458 my $size = $self->allocation;
459 my $rectangle = Gtk2::Gdk::Rectangle->new(0, 0, $size->width, $size->height);
460 $self->window->invalidate_rect($rectangle, TRUE);
461 }
462  
463  
464  
465 #
466 # Notifies the others that the markup has changed by emitting the signal
467 # 'markup-changed'.
468 #
469 sub signal_emit_markup_changed {
470 my $self = shift;
471 my ($markup) = @_;
472 $self->signal_emit('markup-changed'=> $markup);
473 }
474  
475  
476  
477 #
478 # Applies the attributes to the widget. Gtk2::Pango::Layout::set_attributes()
479 # doesn't accept an undef value (a patch has been submitted in order to address
480 # this issue). So if the attributes are undef an empty attribute list has to be
481 # submitted instead.
482 #
483 sub set_layout_attributes {
484 my $self = shift;
485  
486 if ($self->get_text ne '') {
487 # There's text in the widget apply the attributes (the requested pango
488 # text). If the're attributes simply clear the previous ones.
489 my $attributes = $self->{attributes};
490 if (! defined $attributes) {
491 # Clear the previous attributes, just in case...
492 $attributes = $EMPTY_ATTRLIST;
493 }
494 $self->get_layout->set_attributes($attributes);
495 }
496 elsif ($self->get_clear_on_focus and $self->has_focus) {
497 # The widget has focus and is empty, if the user requested that it be
498 # cleared when focused we have to honor it here.
499 my $attributes = $EMPTY_ATTRLIST;
500 $self->get_layout->set_text('');
501 $self->get_layout->set_attributes($attributes);
502 return;
503 }
504 elsif ($self->{empty_markup}) {
505 # The widget is empty and the user wants it filled with a default text at
506 # all times.
507 $self->get_layout->set_text($self->{empty_text});
508 $self->get_layout->set_attributes($self->{empty_attributes});
509 }
510 }
511  
512  
513  
514 #
515 # Called when the text of the entry is changed. The callback is used for monitor
516 # when the user resets the text of the widget without markup. In that case we
517 # need to erase the markup.
518 #
519 sub callback_changed {
520 my $self = shift;
521  
522 if (! $self->{internal}) {
523 # The text was changed as if it was a normal Gtk2::Entry either through
524 # $widget->set_text($text) or $widget->set(text => $text). This means that
525 # the markup style has to be removed from the widget. Now the widget will
526 # rendered in plain text without any styles.
527 $self->{attributes} = undef;
528 $self->signal_emit_markup_changed(undef);
529 }
530 else {
531 # Tell us that the callback was called
532 $self->{internal} = FALSE;
533 }
534  
535
536 # Apply the markup
537 $self->set_layout_attributes();
538 $self->request_redraw();
539
540 return $self->signal_chain_from_overridden(@_);
541 }
542  
543  
544  
545 #
546 # Called each time that the widget needs to be rendered. This happens quite
547 # often as an entry field can have a cursor blinking. Without this callback the
548 # Pango style would be lost at each redraw.
549 #
550 sub callback_expose_event {
551 my $self = shift;
552 my ($event) = @_;
553  
554 $self->set_layout_attributes();
555 return $self->signal_chain_from_overridden(@_);
556 }
557  
558  
559  
560 #
561 # This handler stops the widget from generating critical Pango warnings when the
562 # text selection gesture is performed. If there's no text in the widget we
563 # simply cancel the gesture.
564 #
565 # The gesture is done with: mouse button 1 pressed and dragged over the widget
566 # while the button is still pressed.
567 #
568 sub callback_button_press_event {
569 my $self = shift;
570 my ($event) = @_;
571
572 if ($self->get_text or $event->button != 1) {
573 # Propagate the event further since there's text in the widget
574 return $self->signal_chain_from_overridden(@_);
575 }
576
577 # Give focus to the widget but stop the text selection
578 $self->grab_focus();
579 return TRUE;
580 }
581  
582  
583  
584 # Return a true value
585 1;
586  
587 =head1 PROPERTIES
588  
589 The following properties are added by this widget:
590  
591 =head2 markup
592  
593 (string: writable)
594  
595 The markup text used by this widget. This property is a string that's only
596 writable. That's right, there's no way for extracting the markup from the
597 widget.
598  
599 =head2 empty-markup
600  
601 (string: readable writable)
602  
603 The markup text used by this widget when the entry field is empty. If this
604 property is set the entry will display a default string in the widget when
605 there's no text provided by the user.
606  
607 =head2 clear-on-focus
608 '',
609 'Clear the markup when the widget has focus',
610 'If the Pango markup to display has to cleared when the entry has focus.',
611 TRUE,
612 ['readable', 'writable'],
613  
614 (boolean: readable writable)
615  
616 Indicates if the C<empty-makrup> has to be cleared when the entry is empty and
617 the widget has gained focus.
618  
619 =head1 SIGNALS
620  
621 =head2 markup-changed
622  
623 Emitted when the markup has been changed.
624  
625 Signature:
626  
627 sub markup_changed {
628 my ($widget, $markup) = @_;
629 # Returns nothing
630 }
631  
632 Parameters:
633  
634 =over
635  
636 =item * $markup
637  
638 The new markup that's been applied. This field is a normal Perl string. If
639 C<$markup> is C<undef> then the markup was removed.
640  
641 =back
642  
643 =head2 empty-markup-changed
644  
645 Emitted when the markup used when the widget is empty has been changed.
646  
647 Signature:
648  
649 sub empty_markup_changed {
650 my ($widget, $markup) = @_;
651 # Returns nothing
652 }
653  
654 Parameters:
655  
656 =over
657  
658 =item * $markup
659  
660 The new markup that's been applied when the widget is empty. This field is a
661 normal Perl string. If C<$markup> is C<undef> then the markup was removed.
662  
663 =back
664  
665 =head1 SEE ALSO
666  
667 Take a look at the examples for getting some ideas or inspiration on how to use
668 this widget. For a more powerful text widget that supports more operations take
669 a look at L<Gtk2::TextView>.
670  
671 =head1 AUTHORS
672  
673 Emmanuel Rodriguez E<lt>potyl@cpan.orgE<gt>.
674  
675 =head1 COPYRIGHT AND LICENSE
676  
677 Copyright (C) 2008-2010 by Emmanuel Rodriguez.
678  
679 This library is free software; you can redistribute it and/or modify
680 it under the same terms as Perl itself, either Perl version 5.8.8 or,
681 at your option, any later version of Perl 5 you may have available.
682  
683 =cut