Saturday, September 11, 2010

GTK Programming in Ruby (3) - Adding Menubar, using Stock items

#!/usr/bin/ruby
#
# menu02.rb
# using Stock Menu Items

require 'gtk2'

class App < Gtk::Window
 
 def initialize
  super
  window_settings('Window with Menu', 400, 300)
  mb = menu_settings
  vbox = layout_settings(mb)
  event_settings
  show_all
 end
 
 def window_settings(title, width, height)
  set_title title
  set_default_size width, height
    set_window_position Gtk::Window::POS_CENTER
 end

 def layout_settings(menubar)
  vbox = Gtk::VBox.new false, 3
  vbox.pack_start menubar, false, false, 0
  add vbox
  return vbox
 end
 
 def menu_settings   
 
  # create keyboard accelerator group (keyboard shot cut)
  @keyaccgrp = Gtk::AccelGroup.new
    add_accel_group @keyaccgrp
   
  # menu bar
  menubar = Gtk::MenuBar.new
      
  # menus
  filemenu = new_menu(menubar, 'File')
  editmenu = new_menu(menubar, 'Edit')
  helpmenu = new_menu(menubar, 'Help')
  
  # file menu items
  quitmenuitem = new_menu_item (filemenu, Gtk::Stock::QUIT, 'Q').signal_connect "activate" do
   app_quit
  end     

  # edit menu items
  copymenuitem = new_menu_item (editmenu, Gtk::Stock::COPY, 'C')
  cutmenuitem = new_menu_item (editmenu, Gtk::Stock::CUT, 'X')
  pastemenuitem = new_menu_item (editmenu, Gtk::Stock::PASTE, 'V')
 
  return menubar
  
 end
 
 def event_settings
  signal_connect "destroy" do
   app_quit
  end   
 end  
 
 def new_menu(menubar,title)
  menu = Gtk::Menu.new
  item = Gtk::MenuItem.new title
  item.set_submenu menu
  menubar.append item
  return menu 
 end
 
 def new_menu_item(menu, stock, char)
  item = Gtk::ImageMenuItem.new stock, @keyaccgrp
    key, mod = Gtk::Accelerator.parse char
    item.add_accelerator("activate", @keyaccgrp, key, mod, Gtk::ACCEL_VISIBLE)  
  menu.append item
  return item
 end
 
 def app_quit
  Gtk.main_quit
 end
 
end

Gtk.init
 window = App.new
Gtk.main




The result:

Issues:
1. I did not expect automatic translation (to Japanese). How do I suppress?
2. How do I add accelerator to Menu bar items such as "File"? Using underscore ("_File") does not work.

No comments:

Post a Comment