ruby-****@sourc*****
ruby-****@sourc*****
2009年 2月 6日 (金) 02:59:59 JST
------------------------- REMOTE_ADDR = 74.15.84.244 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-txtw-itrsmrks ------------------------- @@ -161,3 +161,66 @@ * Gtk::TextBuffer#insert_interactive_at_cursor(text, default_editable) * Gtk::TextBuffer#insert_range(iter, start, end) * Gtk::TextBuffer#insert_range_interactive(iter, start, end, default_editable) + + +=== Cutting, Copying and Pasting + +When you right-click a Gtk::TextView widget, you are presented with a pop-up menu containing multiple options, of which the tree we are interested in here are Cut, Copy, and Paste. These three options also have assigned to them a set of built-in accelerator keys Ctrl-X, Ctrl+C, and Ctrl-V respectively. In the next example program (cutcopypaste.rb) we will explore these three options: + + +{{image_right("txtw-itrsmrks-02.png")}} +{{br}} + +((*cutcopypaste.rb*)) + + #!/usr/bin/env ruby + require 'gtk2' + + # Copy the selected text to the clipboard and remove it from the buffer. + def cut_clicked (txtvu) + clipboard = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD) + txtvu.buffer.cut_clipboard(clipboard, true) + end + # Copy the selected text to the clipboard. + def copy_clicked (txtvu) + clipboard = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD) + txtvu.buffer.copy_clipboard(clipboard) + end + # Insert the text from the clipboard into the text buffer. + def paste_clicked (txtvu) + clipboard = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD) + txtvu.buffer.paste_clipboard(clipboard, nil, true) + end + + window = Gtk::Window.new(Gtk::Window::TOPLEVEL) + window.resizable = true + window.title = "Cut, Copy & Paste" + window.border_width = 10 + window.signal_connect('delete_event') { Gtk.main_quit } + + textview = Gtk::TextView.new + + cut = Gtk::Button.new(Gtk::Stock::CUT) + copy = Gtk::Button.new(Gtk::Stock::COPY) + paste = Gtk::Button.new(Gtk::Stock::PASTE) + cut.signal_connect('clicked') { cut_clicked(textview) } + copy.signal_connect('clicked') { copy_clicked(textview) } + paste.signal_connect('clicked') { paste_clicked(textview) } + + scrolled_win = Gtk::ScrolledWindow.new + scrolled_win.set_size_request(300, 200) + scrolled_win.add(textview) + + hbox = Gtk::HBox.new(true, 5) + hbox.pack_start(cut, true, true, 0) + hbox.pack_start(copy, true, true, 0) + hbox.pack_start(paste, true, true, 0) + vbox = Gtk::VBox.new(false, 5) + vbox.pack_start(scrolled_win, true, true, 0) + vbox.pack_start(hbox, false, true, 0) + + window.add(vbox) + window.show_all + Gtk.main + +=== Searching in the Text Buffer