Text Widget
Creating Text Widget
Gtk::Text is the widget to edit multi-lines text.
Gtk::Text.new creates Text widget.
Program text.rb:
require 'gtk'
window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
text = Gtk::Text.new
window.add(text)
text.show
window.show
Gtk.main
Result
Text widget doesn't receive user's input as default.
It is done by using "set_editable" method.
text = Gtk::Text.new
text.set_editable true
Using Scrollbar with Text Widget
Gtk::Adjustment class represents abstruct data structure to fetch
a part of fuge data (Gtk::Adjustment is NOT a widget).
Using the Gtk::Adjustment enable to scroll text in Text widget.
Here's a sample of Text widget with a scroll bar.
Program text3.rb:
require 'gtk'
window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
vadj = Gtk::Adjustment.new(0,0,0,0,0,0)
text = Gtk::Text.new(nil, vadj)
vs = Gtk::VScrollbar.new(vadj)
text.set_editable true
hbox = Gtk::HBox.new(false, 0)
hbox.pack_start text, true, true, 0
hbox.pack_start vs, false, false, 0
window.add hbox
text.show
vs.show
hbox.show
window.show
Gtk.main
Result
Discription
vadj = Gtk::Adjustment.new(0,0,0,0,0,0)
This creates Adjustment object.
Both Text widget and Scrollbar widget owns this object.
text = Gtk::Text.new(nil, vadj)
This creates Text widget.
First argument is the Adjustment for horizontal direction, and
second is also the Adjustmen for vertical direction.
Argument "nil" means using default adjustments.
(attention: Text widget does not support horizontal Adjustment yet)
vs = Gtk::VScrollbar.new(vadj)
This creates scrollbar. VScrollbar is vertical scrollbar,
HScrollbar is horizontal one.
Argument "vadj" is the Adjustment object which is same one for Text widget.
hbox = Gtk::HBox.new(false, 0)
hbox.pack_start text, true, true, 0
hbox.pack_start vs, false, false, 0
This creates horizontal box and pack Text and Scrollbar.
Methods of Text widget
Here's main methods of Text widget.
- text.
set_point(index)
-
This method set the "inserting point".
index is the index of the string in Text widget.
index = text.get_point
-
This method returns the current "inserting point".
length = text.get_length
-
This method returns the length of the string in Text widget.
It also includes the length of carrige return or line feed.
- text.
insert_text(chars, index)
-
This method insert string to Text widget.
It is inherited from Gtk::Editable.
This insert the string chars into the index.
- text.
get_chars(start_pos, end_pos)
-
This method returns the string from start_pos to end_pos.
Gtk::Text.get_chars is inherited from Gtk::Editable.
- text.
insert(font, fore, back, ch
ars)
-
This method inserts string into current "inserting point".
Font, foreground color, Background color, can be designated.
font is Gdk::Font, fore and back is Gdk::Color object.
Argument "nil" for font/color means using default value (one of widget style).
- text.
backward_delete(nchars)
- text.
forward_delete(nchars)
-
These methods delete nchars characters from current "inserting point".
"backward_delete" delete string from right to left.
"forward_delete" delete string from left to right.
Next
[TOP]
Written by
akaishi@ruby.freak.ne.jp