[ruby-gnome2-doc-cvs] [Ruby-GNOME2 Project Website] update - tut-treeview-selection-contextmenu

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2012年 8月 14日 (火) 09:20:31 JST


-------------------------
REMOTE_ADDR = 184.145.90.35
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-treeview-selection-contextmenu
-------------------------
@@ -24,3 +24,96 @@
 
   # Popup the menu on Shift-F10
   view.signal_connect("popup_menu") { menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME) }
+
+Following is a program you have already seen in this tutorial, however, now the above ((*Context Menu*)) is included in it:
+
+ #!/usr/bin/env ruby
+ require 'gtk2'
+ 
+ # Add three columns to the GtkTreeView. 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
+   renderer.foreground = "#ff0000"
+   column   = Gtk::TreeViewColumn.new("Buy", renderer,  :text => $buy_it)
+   treeview.append_column(column)
+   renderer = Gtk::CellRendererText.new
+   column   = Gtk::TreeViewColumn.new("Count", renderer, :text => $quantity)
+   treeview.append_column(column)
+   renderer = Gtk::CellRendererText.new
+   column   = Gtk::TreeViewColumn.new("Product", renderer, :text => $product)
+   treeview.append_column(column)
+ end
+ 
+ window = Gtk::Window.new("Grocery List")
+ window.resizable = true
+ window.border_width = 10
+ 
+ window.signal_connect('destroy') { Gtk.main_quit }
+ window.set_size_request(250, 165)
+ 
+ class GroceryItem
+   attr_accessor :buy, :quantity, :product
+   def initialize(b, q, p); @buy, @quantity, @product = b, q, p; end
+ end
+ $buy_it = 0; $quantity = 1; $product  = 2
+ 
+ list = Array.new
+ list[0] = GroceryItem.new(true,  1, "Paper Towels") 
+ list[1] = GroceryItem.new(true,  2, "Bread")
+ list[2] = GroceryItem.new(false, 1, "Butter")
+ list[3] = GroceryItem.new(true,  1, "Milk")
+ list[4] = GroceryItem.new(false, 3, "Chips")
+ list[5] = GroceryItem.new(true,  4, "Soda") 
+ 
+ treeview = Gtk::TreeView.new
+ setup_tree_view(treeview)
+ 
+ # Create a new tree model with three columns, as Boolean,
+ # integer and string.
+ store = Gtk::ListStore.new(TrueClass, Integer, String)
+ 
+ # Add all of the products to the GtkListStore.
+ list.each_with_index do |e, i|
+     iter = store.append
+ 
+     iter[$buy_it]   = list[i].buy       # same as: >>> # store.set_value(iter, $buy_it,   list[i].buy)
+     iter[$quantity] = list[i].quantity  # same as: >>> # store.set_value(iter, $quantity, list[i].quantity)
+     iter[$product]  = list[i].product   # same as: >>> # store.set_value(iter, $product,  list[i].product)
+ end
+ 
+ # Add the tree model to the tree view
+ treeview.model = store
+ 
+ ### Context Menu section ---------------------------------------------------- (start) -----
+ # Create a menu
+ menu = Gtk::Menu.new
+ 
+ # Add an item with a silly callback
+ item = Gtk::MenuItem.new("Do Something")
+ item.signal_connect("activate") { puts "Did something!" }
+ menu.append(item)
+ 
+ menu.show_all
+ 
+ # Popup the menu on right click
+ treeview.signal_connect("button_press_event") do |widget, event|
+   if event.kind_of? Gdk::EventButton and event.button == 3
+     menu.popup(nil, nil, event.button, event.time)
+   end
+ end
+ 
+ # Popup the menu on Shift-F10
+ treeview.signal_connect("popup_menu") { menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME) }
+ ### Context Menu section ------------------------------------------------------ (end) -----
+ 
+ scrolled_win = Gtk::ScrolledWindow.new
+ scrolled_win.add(treeview)
+ scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
+ 
+ window.add(scrolled_win)
+ window.show_all
+ Gtk.main




ruby-gnome2-cvs メーリングリストの案内
Back to archive index