ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 2日 (木) 05:05:50 JST
------------------------- REMOTE_ADDR = 184.145.84.152 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-txtw-itrsmrks ------------------------- @@ -47,53 +47,47 @@ #!/usr/bin/env ruby require 'gtk2' - + # Insert the text from the GtkEntry into the GtkTextView. def insert_text(ent, txtvu) mark = txtvu.buffer.selection_bound iter = txtvu.buffer.get_iter_at_mark(mark) txtvu.buffer.insert(iter, ent.text) end - + # Retrieve the selected text from the GtkTextView and # display it to the user. def retrieve_text(tw) - start_iter, end_iter = tw.buffer.bounds - - # Problems as of: Ruby 1.8.6 and Ruby-GNOME2 - # rel.: 2-0.17.0-rc1. - # - # Does not allow start, end iterator arguments - # text = tw.buffer.text ##### (startm, endm) - # ------------------------------------------ - # However, the following works on the surface - # but doesn't do what is expected. - + + start_iter, end_iter, selected = tw.buffer.selection_bounds + if !selected + start_iter, end_iter = tw.buffer.bounds + end + text = tw.buffer.get_text(start_iter, end_iter) - puts "#{text}" + puts "selected=#{selected}; TEXT=#{text}" end - - window = Gtk::Window.new(Gtk::Window::TOPLEVEL) + + window = Gtk::Window.new("Text Iterators") window.resizable = true - window.title = "Text Iterators" window.border_width = 10 - window.signal_connect('delete_event') { Gtk.main_quit } + window.signal_connect('destroy') { Gtk.main_quit } window.set_size_request(250, 150) - + textview = Gtk::TextView.new - + entry = Gtk::Entry.new insert = Gtk::Button.new("Insert Text") retrieve = Gtk::Button.new("Get Text") - + insert.signal_connect('clicked') { insert_text(entry, textview) } retrieve.signal_connect('clicked') { retrieve_text(textview) } - + scrolled_win = Gtk::ScrolledWindow.new scrolled_win.border_width = 5 scrolled_win.add(textview) scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS) - + hbox = Gtk::HBox.new(false, 5) hbox.pack_start_defaults(entry) hbox.pack_start_defaults(insert)