[ruby-gnome2-doc-cvs] [Hiki] create - gtk2-tut-itemfactory-example

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2003年 9月 19日 (金) 01:06:43 JST


-------------------------
REMOTE_ADDR = 61.26.70.211
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/ja/?gtk2-tut-itemfactory-example
-------------------------
= アイテムファクトリの例
== メニューバー
{{image_left "itemfactory1.png"}}
{{br}}

== メニュー
{{image_left "itemfactory2.png"}}
{{br}}

== オプションメニュー
{{image_left "itemfactory3.png"}}
{{br}}

== サンプルコード

  #! /usr/bin/env ruby
  require 'gtk2'
  
  print_hello = Proc.new {|data, w| puts "Hello World! with #{w}, #{data}"}
  print_toggle = Proc.new {|data, w| puts "Check button state - #{w.active?}"}
  print_selected = Proc.new {|data, w| puts "Radio button #{data} #{w.active? ? "selected" :   "deselected"}"}
  
  IF = Gtk::ItemFactory
  menu_items = [
  	["/_File"],
  	["/File/_New",     IF::STOCK_ITEM, "<control>N", Gtk::Stock::NEW,  print_hello, "New"],
  	["/File/_Open",    IF::STOCK_ITEM, "<control>O", Gtk::Stock::OPEN, print_hello, "Open"],
  	["/File/_Save",    IF::STOCK_ITEM, "<control>S", Gtk::Stock::SAVE, print_hello, "Save"],
  	["/File/Save _As", IF::ITEM],
  	["/File/sep1",     IF::SEPARATOR],
  	["/File/_Quit",    IF::STOCK_ITEM, "<CTRL>Q",    Gtk::Stock::QUIT, Proc.new {Gtk.main_quit}],
  
  	["/_Options",      IF::BRANCH],
  	["/Options/tear",  IF::TEAROFF],
  	["/Options/Check", IF::CHECK_ITEM, nil, nil, print_toggle],
  	["/Options/sep",   IF::SEPARATOR],
  	["/Options/Rad1",  IF::RADIO_ITEM,  nil, nil, print_selected, 1],
  	["/Options/Rad2",  "/Options/Rad1", nil, nil, print_selected, 2],
  	["/Options/Rad3",  "/Options/Rad1", nil, nil, print_selected, 3],
  
  	["/_Help",       IF::LAST_BRANCH],
  	["/_Help/About", IF::ITEM],
  ]
  
  def get_menubar_menu(window, menu_items)
  	accel_group = Gtk::AccelGroup.new
  	item_factory = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_MENU_BAR, "<main>", accel_group)
  	
  	item_factory.create_items(menu_items)
  	window.add_accel_group(accel_group)
  	item_factory.get_widget("<main>")
  end
  
  def get_popup_menu(window, menu_items)
  	accel_group = Gtk::AccelGroup.new
  	item_factory = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_MENU, "<main>", accel_group)
  	item_factory.create_items(menu_items)
  	menu = item_factory.get_widget("<main>")
  	button = Gtk::Button.new("Popup")
  	button.signal_connect("event") do |w, e|
  		if Gdk::Event::BUTTON_PRESS == e.event_type 
  			menu.popup(nil, nil, e.button, e.time)
  			true
  		else
  			false
  		end
  	end
  	window.add_accel_group(accel_group)
  	button
  end
  
  def get_option_menu(window, menu_items)
  	accel_group = Gtk::AccelGroup.new
  	item_factory = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_OPTION_MENU, "<main>", accel_group)
  	item_factory.create_items(menu_items)
  	window.add_accel_group(accel_group)
  	item_factory.get_widget("<main>")
  end
  
  Gtk.init
   
  window = Gtk::Window.new
  window.signal_connect("destroy") {Gtk.main_quit}
  window.title = "Item Factory"
  window.set_size_request(300, 200)
   
  main_vbox = Gtk::VBox.new(false, 1)
  main_vbox.border_width = 1
  window.add(main_vbox)
   
  case ARGV[0]
  when 'menu'
  	popup_button = get_popup_menu(window, menu_items)
  	main_vbox.pack_end(popup_button, false, true, 0)
  when 'option'
  	option_menu = get_option_menu(window, menu_items)
  	main_vbox.pack_end(option_menu, false, true, 0)
  else # menu_bar
  	menubar = get_menubar_menu(window, menu_items)
  	main_vbox.pack_start(menubar, false, true, 0)
  end
  
  window.show_all
  Gtk.main






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