1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#!/usr/bin/env ruby
=begin
bindings.rb - Ruby/GTK sample script.
Copyright (c) 2002-2006 Ruby-GNOME2 Project Team
This program is licenced under the same licence as Ruby-GNOME2.
$Id: bindings.rb,v 1.7 2006/06/17 13:18:12 mutoh Exp $
=end
=begin
Usage:
bindings.rb <filename>
Following key bindings are effective in the TextView area.
<space> : scroll down by one page
<backspace> : scroll up by one page
j : move cursor donw by one line
k : move cursor up by one line
clicking buttons cause following effect
"space" : same as pressing <space> in text view area.
"back_space" : same as pressing <backspace> in text view area.
"cancel j/k" : disable 'j' and 'k' binding
=end
require 'gtk2'
class Pager < Gtk::TextView
type_register
# widget's key binding can be defined like this
binding_set.add_signal(Gdk::Keyval::GDK_space, 0,
"move_cursor",
Gtk::MOVEMENT_PAGES, 1, false)
binding_set.add_signal(Gdk::Keyval::GDK_BackSpace, 0,
"move_cursor",
Gtk::MOVEMENT_PAGES, -1, false)
def initialize(path)
@path = path
super()
@buffer = self.buffer
load
set_editable(false)
set_size_request(400, 400)
end
def load
open(@path).read.each do |line|
@buffer.insert_at_cursor(line)
end
@buffer.place_cursor(@buffer.start_iter)
end
end
path = ARGV[0] || __FILE__
window = Gtk::Window.new
window.name = "pager_window"
sw = Gtk::ScrolledWindow.new
vbox = Gtk::VBox.new
hbox = Gtk::HBox.new
pager = Pager.new(path)
hbox.add(button1 = Gtk::Button.new("space"))
hbox.add(button2 = Gtk::Button.new("back_space"))
hbox.add(button3 = Gtk::Button.new("cancel j/k"))
button1.signal_connect("clicked") do
Pager.binding_set.activate(Gdk::Keyval::GDK_space, 0, pager)
end
button2.signal_connect("clicked") do
pager.bindings_activate(Gdk::Keyval::GDK_BackSpace, 0)
end
# Key bindings can be attached to any widget by
# Gtk::BindingSet#add_path
# see RC Files section of GTK+ documentation for more detail.
bset = Gtk::BindingSet.new("j_and_k")
bset.add_signal(Gdk::Keyval::GDK_j, 0,
"move_cursor",
Gtk::MOVEMENT_DISPLAY_LINES, 1, false)
bset.add_signal(Gdk::Keyval::GDK_k, 0,
"move_cursor",
Gtk::MOVEMENT_DISPLAY_LINES, -1, false)
bset.add_path(Gtk::PathType::WIDGET, "pager_window.*.Pager",
Gtk::PathPriorityType::APPLICATION)
button3.signal_connect("clicked") do
bset.entry_clear(Gdk::Keyval::GDK_j, 0)
bset.entry_clear(Gdk::Keyval::GDK_k, 0)
end
sw.add(pager)
vbox.add(hbox).add(sw)
window.add(vbox)
window.show_all
pager.grab_focus
window.signal_connect("destroy") { Gtk.main_quit }
Gtk.main
|