ruby-****@sourc*****
ruby-****@sourc*****
2012年 9月 5日 (水) 09:15:12 JST
------------------------- REMOTE_ADDR = 70.49.49.99 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-txtw-textview ------------------------- @@ -44,6 +44,44 @@ Here the buffer component is the Gtk::TextBuffer. Text buffers hold text, images, child widgets, text tags and other information necessary for rendering the documents. A Gtk::TextBuffer buffer is an independent object and can be displayed by many text view widgets. However any text view has only a single text buffer associated with it. Most programmers do not take advantage of this feature, but it will become important latter when you learn how to embed child widgets into a text buffer. + +:Short D-tour: + + If you are not exactly sure what was just said, following is a short demonstration simulating how Gtk::TextView and Gtk::TextBuffer are implemented, and how you can exploit this design to create some Gtk::TextViews that share the same Gtk::TextBuffer and some that do not: + + In tv-N-tb-example.rb sample program pay attention to three different ((*tview_instance_#*)) TextView instances. The first one, designated with number zero (0) has its own (not shared) TextBuffer, however, the other two (#1 and #2) share the TextBuffer instance: + + ((*tv-N-tb-example.rb*)) + #!/usr/bin/env ruby + class TextView + attr_accessor :buffer + def initialize(buffer); @buffer = buffer; end + end + class TextBuffer + attr_accessor :text + def initialize(text); @text = text; end + def to_s; "Class TextBuffer: text=#@text"; end + end + def show_text_views_buffer(id, tv_inst) + puts "Buffer ID=#{id} - #{tv_inst.class}: #{tv_inst.buffer.to_s}" + end + + tview_instance_0 = TextView.new(nil) + show_text_views_buffer(0, tview_instance_0) + tb=TextBuffer.new("Some initial text") + tview_instance_1 = TextView.new(tb) + tview_instance_2 = TextView.new(tb) + show_text_views_buffer(1, tview_instance_1) + tview_instance_2.buffer.text = "Override the initial text with new text." + puts "----- after overriding text buffer -------" + show_text_views_buffer(0, tview_instance_0) + show_text_views_buffer(1, tview_instance_1) + show_text_views_buffer(2, tview_instance_2) + + + + + Most new text views are created with the Gtk::TextView.new(buffer=nil). When using this method with the default nil argument an empty Gtk::TextBuffer is created for you. This empty text buffer can be replaced or retrieved by "Gtk::TextView#buffer=(buff)", or "Gtk::TextView#set_buffer(buff)", and "buff = Gtk::TextView#buffer" respectively. With the former two methods the contents of the buffer gets completely replaced. However, as we will see in the following page((*Text Iterators and Marks,*))one can also cause changes to a text buffer only partially. Recall that we have pointed out that a few widgets provide native scrolling support. Well, Gtk::TextView is one of them. This means you should use Gtk::Container#add method, rather than Gtk::ScrolledWindow#add_with_viewport(child), to add Gtk::TextView to the scrolled windows.