Gtk2::TextView
s This lesson will look at a very basic Gtk2::TextView
, and the methods that are available to it. These methods apply to the associated Gtk2::TextBuffer
as a whole. Later lessons will look at more granularity available on the Gtk2::TextBuffer
.
Gtk2::TextView
There is only one function for creating a new Gtk2::TextView
widget.
widget = Gtk2::TextView->new
When a Gtk2::TextView
is created it will create an associated Gtk2::TextBuffer
and Gtk2::TextTagTable
by default. If you want to use an existing Gtk2::TextBuffer
in a Gtk2::TextView
specify it in the above method. To change the Gtk2::TextBuffer
used by a Gtk2::TextView
use the following method:
$text_view->set_buffer ($buffer)
* $buffer (Gtk2::TextBuffer
)
Use the following method to retrieve a reference to the Gtk2::TextBuffer
from a Gtk2::TextView
:
textbuffer = $text_view->get_bufferA
Gtk2::TextView
widget doesn't have scrollbars to adjust the view in case the text is larger than the window. To provide scrollbars, you add the Gtk2::TextView
to a Gtk2::ScrolledWindow
.
A Gtk2::TextView
can be used to allow the user to edit a body of text, or to display multiple lines of read-only text to the user. To switch between these modes of operation, the use the following method:
$text_view->set_editable ($setting) * $setting (boolean)The "$setting" argument is a TRUE or FALSE value that specifies whether the user is permitted to edit the contents of the
Gtk2::TextView
widget. The editable mode of the Gtk2::TextView
can be overridden in text ranges within the Gtk2::TextBuffer
by Gtk2::TextTag
s.
You can retrieve the current editable setting using the method:
boolean = $text_view->get_editableWhen the
Gtk2::TextView
is not editable, you probably should hide the cursor using the method:
$text_view->set_cursor_visible ($setting) * $setting (boolean)The "$setting" argument is a TRUE or FALSE value that specifies whether the cursor should be visible The
Gtk2::TextView
can wrap lines of text that are too long to fit onto a single line of the display window. Its default behavior is not to wrap lines. This can be changed using the next method:
$text_view->set_wrap_mode ($wrap_mode) * $wrap_mode (Gtk2::WrapMode)This method allows you to specify that the text widget should wrap long lines on word or character boundaries. The word_wrap argument is one of:
none char wordThe default justification of the text in a
Gtk2::TextView
can be set and retrieved using the methods:
justification = $text_view->get_justification $text_view->set_justification ($justification) * $justification (Gtk2::Justification)where $justification is one of:
left right center
The justification will be "left" if the wrap_mode is "none". Tags in the associated |
#left margin integer = $text_view->get_left_margin $text_view->set_left_margin ($left_margin) * $left_margin (integer) #right margin integer = $text_view->get_right_margin $text_view->set_right_margin ($right_margin) * $right_margin (integer) #tabs tabarray = $text_view->get_tabs $text_view->set_tabs ($tabs) * $tabs (Gtk2::Pango::TabArray) #indentation integer = $text_view->get_indent $text_view->set_indent ($indent) * $indent (integer) #other integer = $text_view->get_pixels_above_lines $text_view->set_pixels_above_lines ($pixels_above_lines) * $pixels_above_lines (integer) integer = $text_view->get_pixels_below_lines $text_view->set_pixels_below_lines ($pixels_below_lines) * $pixels_below_lines (integer) integer = $text_view->get_pixels_inside_wrap $text_view->set_pixels_inside_wrap ($pixels_inside_wrap) * $pixels_inside_wrap (integer)$left_margin, $right_margin, $indent, $pixels_above_lines, $pixels_below_lines and $pixels_inside_wrap are specified in pixels. These default values may be overridden by tags in the associated
Gtk2::TextBuffer
. $tabs is a Gtk2::Pango::TabArray.
The program can be found here: 'TextView Demo'
1 #! /usr/bin/perl -w 2 3 use strict; 4 use Gtk2 '-init'; 5 use Glib qw/TRUE FALSE/; 6 7 #standard window creation, placement, and signal connecting 8 my $window = Gtk2::Window->new('toplevel'); 9 $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); 10 $window->set_border_width(5); 11 $window->set_position('center_always'); 12 13 #this vbox will geturn the bulk of the gui 14 my $vbox = &ret_vbox(); 15 16 #add and show the vbox 17 $window->add($vbox); 18 $window->show(); 19 20 #our main event-loop 21 Gtk2->main(); 22 23 24 sub ret_vbox { 25 26 my $vbox = Gtk2::VBox->new(FALSE,5); 27 28 my $frame = Gtk2::Frame->new("Basic Gtk2::TextView"); 29 30 #method of Gtk2::Container 31 $frame->set_border_width(5); 32 33 my $sw = Gtk2::ScrolledWindow->new (undef, undef); 34 $sw->set_shadow_type ('etched-out'); 35 $sw->set_policy ('automatic', 'automatic'); 36 #This is a method of the Gtk2::Widget class,it will force a minimum 37 #size on the widget. Handy to give intitial size to a 38 #Gtk2::ScrolledWindow class object 39 $sw->set_size_request (300, 300); 40 #method of Gtk2::Container 41 $sw->set_border_width(5); 42 43 my $tview = Gtk2::TextView->new(); 44 my $content = `cat ./lyrics.txt`; 45 my $buffer = $tview->get_buffer(); 46 $buffer->set_text($content); 47 48 $sw->add($tview); 49 $frame->add($sw); 50 $vbox->pack_start($frame,TRUE,TRUE,4); 51 52 my $table = Gtk2::Table->new(4,5,FALSE); 53 #editable toggle 54 my $btn_editable = Gtk2::CheckButton->new("_Editable"); 55 $btn_editable->set_active(TRUE); 56 $btn_editable->signal_connect('toggled' =>sub { 57 if($btn_editable->get_active){ 58 $tview->set_editable(TRUE); 59 }else{ 60 $tview->set_editable(FALSE); 61 } 62 }); 63 64 $table->attach_defaults($btn_editable,0,1,0,1); 65 #visible cursor toggle 66 my $btn_cursor = Gtk2::CheckButton->new("_Cursor Visible"); 67 $btn_cursor->set_active(TRUE); 68 $btn_cursor->signal_connect('toggled' =>sub { 69 if($btn_cursor->get_active){ 70 $tview->set_cursor_visible(TRUE); 71 }else{ 72 $tview->set_cursor_visible(FALSE); 73 } 74 }); 75 $table->attach_defaults($btn_cursor,0,1,1,2); 76 #left margin toggle 77 my $btn_mar_l = Gtk2::CheckButton->new("_Left Margin"); 78 $btn_mar_l->signal_connect('toggled' =>sub { 79 if($btn_mar_l->get_active){ 80 $tview->set_left_margin (50); 81 }else{ 82 $tview->set_left_margin (0); 83 } 84 }); 85 $table->attach_defaults($btn_mar_l,0,1,2,3); 86 #right margin toggle 87 my $btn_mar_r = Gtk2::CheckButton->new("_Right Margin"); 88 $btn_mar_r->signal_connect('toggled' =>sub { 89 if($btn_mar_r->get_active){ 90 $tview->set_right_margin (50); 91 }else{ 92 $tview->set_right_margin (0); 93 } 94 }); 95 96 $table->attach_defaults($btn_mar_r,0,1,3,4); 97 #wrap mode 98 my $lbl_wrap = Gtk2::Label->new("Wrap Mode:"); 99 $table->attach_defaults($lbl_wrap,1,2,0,1); 100 101 my $combo_wrap = Gtk2::ComboBox->new_text(); 102 103 $combo_wrap->append_text("none"); 104 $combo_wrap->append_text("word"); 105 $combo_wrap->append_text("char"); 106 $combo_wrap->set_active(0); 107 108 $combo_wrap->signal_connect('changed' =>sub{ 109 110 $tview->set_wrap_mode ($combo_wrap->get_active_text); 111 }); 112 113 $table->attach_defaults($combo_wrap,2,3,0,1); 114 #justification mode 115 my $lbl_just = Gtk2::Label->new("Justification:"); 116 $table->attach_defaults($lbl_just,1,2,1,2); 117 118 my $combo_just = Gtk2::ComboBox->new_text(); 119 120 $combo_just->append_text("left"); 121 $combo_just->append_text("right"); 122 $combo_just->append_text("center"); 123 $combo_just->set_active(0); 124 125 $combo_just->signal_connect('changed' =>sub{ 126 127 $tview->set_justification ($combo_just->get_active_text); 128 }); 129 130 $table->attach_defaults($combo_just,2,3,1,2); 131 132 $vbox->pack_start($table,FALSE,FALSE,0); 133 $vbox->show_all(); 134 return $vbox; 135 }