ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 15日 (月) 08:28:13 JST
------------------------- REMOTE_ADDR = 184.145.82.7 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-kbda ------------------------- @@ -18,48 +18,51 @@ #!/usr/bin/env ruby require 'gtk2' - # Add three columns to the Gtk::TreeView. All three of the - # columns will be displayed as text, although one is a Boolean - # value and another is an integer. def setup_tree_view(treeview) # Create a new GtkCellRendererText, add it to the tree # view column and append the column to the tree view. renderer = Gtk::CellRendererText.new - column = Gtk::TreeViewColumn.new("Action", renderer, "text" => Accl::ACTION) + column = Gtk::TreeViewColumn.new("Action", renderer, "text" => ACTION_COLUMN) treeview.append_column(column) renderer = Gtk::CellRendererAccel.new - column = Gtk::TreeViewColumn.new( - "Accelerator", renderer, - "accel_mods" => Accl::MASK, - "accel_key" => Accl::VALUE + column = Gtk::TreeViewColumn.new("Accelerator", renderer, + { "accel_mods" => MASK_COLUMN, "accel_key" => VALUE_COLUMN } ) - # BUG: Gtk::CellRendererAccel::MODE_GTK is undefined - # renderer.accel_mode = Gtk::CellRendererAccel::MODE_GTK - renderer.accel_mode = 0 # BUG: Gtk::CellRendererAccel::MODE_GTK is undefined renderer.editable = true - - treeview.append_column(column) + treeview.append_column(column) + renderer.signal_connect('accel_edited') do |w, path, accl_k, mask, hw_k| if (iter = treeview.model.get_iter(path)) - iter[Accl::MASK] = mask - iter[Accl::VALUE] = accl_k + iter[MASK_COLUMN] = mask + iter[VALUE_COLUMN] = accl_k + puts "accel_k:#{accl_k}=[" + + "#{Gdk::Keyval.to_name(accl_k)}] hw_k:#{hw_k} " + + "Modifier keys=#{get_string_for_modifier_keys(mask)}" end end end - window = Gtk::Window.new(Gtk::Window::TOPLEVEL) - window.resizable = true - window.title = "Accelerator Keys" - window.border_width = 10 - window.signal_connect('delete_event') { Gtk.main_quit } - window.set_size_request(250, 175) + def need_plus(arg) + arg == '' ? '' : '+' + end + + def get_string_for_modifier_keys(mask) + mods = "" + mods += "Shift" if mask & Gdk::Window::SHIFT_MASK != 0 + mods += "#{need_plus(mods)}Ctrl" if mask & Gdk::Window::CONTROL_MASK != 0 + mods += "#{need_plus(mods)}Alt" if mask & Gdk::Window::MOD1_MASK != 0 + mods += "#{need_plus(mods)}Mod2" if mask & Gdk::Window::MOD2_MASK != 0 + mods += "#{need_plus(mods)}Meta" if mask & Gdk::Window::META_MASK != 0 + mods += "#{need_plus(mods)}Button1" if mask & Gdk::Window::BUTTON1_MASK != 0 + mods += "#{need_plus(mods)}Super" if mask & Gdk::Window::SUPER_MASK != 0 + mods == "" ? "" : "<"+mods+">" + end class Accl attr_accessor :action, :mask, :value def initialize(a, m, v); @action, @mask, @value = a, m, v; end - ACTION = 0; MASK = 1; VALUE = 2 end - + ACTION_COLUMN = 0; MASK_COLUMN = 1; VALUE_COLUMN = 2 list = [ Accl.new("Cut", Gdk::Window::CONTROL_MASK, Gdk::Keyval::GDK_X), Accl.new("Copy", Gdk::Window::CONTROL_MASK, Gdk::Keyval::GDK_C), @@ -69,29 +72,38 @@ Accl.new("Print", Gdk::Window::CONTROL_MASK, Gdk::Keyval::GDK_P), ] - treeview = Gtk::TreeView.new + ### -- There's a BUG in {{ Gtk::CellRendererAccel }} + ### ------------------------------------------------ + ### All the above definitions of modifier keys when "single" modifier is used + ### work, however, if you added the line ad the end of this comment to the list + ### above, it would not be rendered properly because more modifier keys are OR'ed + ### together. Following would not work correctly: + ### + ### Accl.new("Purge", Gdk::Window::CONTROL_MASK & Gdk::Window::MOD1_MASK, Gdk::Keyval::GDK_P), + + treeview = Gtk::TreeView.new(store = Gtk::ListStore.new(String, Integer, Integer)) setup_tree_view(treeview) - - # Create a new tree model with three columns, as Boolean, - # integer and string. - store = Gtk::ListStore.new(String, Integer, Integer) # Add all of the products to the GtkListStore. list.each_with_index do |e, i| iter = store.append - store.set_value(iter, Accl::ACTION, list[i].action) - store.set_value(iter, Accl::MASK, list[i].mask) - store.set_value(iter, Accl::VALUE, list[i].value) + iter[ACTION_COLUMN] = list[i].action + iter[MASK_COLUMN] = list[i].mask # see: BUG in {{ Gtk::CellRendererAccel }} above + iter[VALUE_COLUMN] = list[i].value iter.next! end - # Add the tree model to the tree view - treeview.model = store - scrolled_win = Gtk::ScrolledWindow.new scrolled_win.add(treeview) scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC) - + window = Gtk::Window.new("Accelerator Keys") + window.resizable = true + window.border_width = 10 + window.signal_connect('destroy') { Gtk.main_quit } + window.set_size_request(250, 175) window.add(scrolled_win) window.show_all Gtk.main