Wednesday, September 15, 2010

GTK Programming in Ruby (4) - Adding Menubar, using UIManager

1. Create a ui file (xml format) to define its menu structure.
File Name: menu03.ui
<ui>
  <menubar name="MenuBar">
    <menu name="FileMenu" action="File">
   <menuitem name="FileNew" action="New"/>
      <menuitem name="FileOpen" action="Open"/>
      <separator/>
      <menuitem name="FileSave" action="Save"/>
      <menuitem name="FileSaveAs" action="SaveAs"/>
      <menuitem name="FileRevert" action="Revert"/>
      <separator/>
      <menuitem name="FilePageSetup" action="PageSetup"/>
      <menuitem name="FilePreview" action="Preview"/> 
      <menuitem name="FilePrint" action="Print"/> 
      <separator/>      
      <menuitem name="FileQuit" action="Quit"/>
    </menu>
    <menu name="EditMenu" action="Edit">
   <menuitem name="EditUndo" action="Undo"/>
   <menuitem name="EditRedo" action="Redo"/>
   <separator/>  
      <menuitem name="EditCut" action="Cut"/>
      <menuitem name="EditCopy" action="Copy"/>
      <menuitem name="EditPaste" action="Paste"/>
      <menuitem name="EditClear" action="Clear"/>
      <separator/>
      <menuitem name="EditSelectAll" action="SelectAll"/>
      <menuitem name="EditDeselect" action="Deselect"/>
      <separator/>
      <menuitem name="EditPreferences" action="Preferences"/>
    </menu>
    <menu name="HelpMenu" action="Help">
      <menuitem name="HelpContents" action="Contents"/>
      <menuitem name="HelpAbout" action="About"/>
    </menu>
  </menubar>
</ui>

2. Use it in the program to create menu.
#!/usr/bin/ruby
#
# menu03.rb
# using UIManager
# requires menu03.ui

require 'gtk2'

class App < Gtk::Window
 
 def initialize
  super
  window_settings('Menubar using UIManager', 400, 300, Gtk::Window::POS_CENTER)
  mb = menu_settings
  vbox = layout_settings(mb)
  event_settings
  self.show_all
 end
  
 def window_settings(title, width, height, position)
  self.set_title title
  self.set_default_size width, height
    self.set_window_position position
 end

 def layout_settings(menubar)
  vbox = Gtk::VBox.new false, 3
  vbox.pack_start menubar, false, false, 0
  self.add vbox
  return vbox
 end
 
 def menu_settings    
 
  # callback procs! 
  new      = Proc.new { puts "new" }
  open      = Proc.new { puts "open" }
  save      = Proc.new { puts "save" }
  saveas    = Proc.new { puts "saveas" }
  revert    = Proc.new { puts "revert" }
  pagesetup = Proc.new { puts "pagesetup" }
  preview   = Proc.new { puts "preview" }
  print     = Proc.new { puts "print" }
  quit    = Proc.new { app_quit }
  undo      = Proc.new { puts "undo" }
  redochg   = Proc.new { puts "redo" }
  cut       = Proc.new { puts "cut" }
  copy      = Proc.new { puts "copy" }
  paste     = Proc.new { puts "paste" }
  clear     = Proc.new { puts "clear" }
  selectall = Proc.new { puts "selectall" }
  deselect  = Proc.new { puts "deselect" }
  preferences = Proc.new { puts "preferences" }
  help      = Proc.new { puts "help" }
  about     = Proc.new { puts "about" }

  # menu items: name, stock_id, label, accelerator, tool-tip, callback
  entries = [
   ["File", nil, "_File", nil, nil, nil ],
    ["New",      Gtk::Stock::NEW,  nil, nil, "Create New",   new],
    ["Open",      Gtk::Stock::OPEN,  nil, nil, "Open a file",   open],
    ["Save",      Gtk::Stock::SAVE,  nil, nil, "Save the doc.", save],
    ["SaveAs",    Gtk::Stock::SAVE_AS,  nil, nil, "Save the doc. as", saveas],
    ["Revert",    Gtk::Stock::REVERT_TO_SAVED,  nil, nil, "Revert to saved", revert],
    ["PageSetup", nil,"Page Setup", nil, "Page setup", pagesetup],
    ["Preview",    Gtk::Stock::PRINT_PREVIEW,  nil, nil, "Print Preview", preview],
    ["Print",      Gtk::Stock::PRINT,  nil, nil, "Save the doc.", save],
    ["Quit",      Gtk::Stock::QUIT,  nil, nil, "Quit the app.", quit],
   ["Edit", nil, "_Edit", nil, nil, nil],
    ["Undo",       Gtk::Stock::UNDO,    nil, nil, "Undo Change",  undo],
    ["Redo",       Gtk::Stock::REDO,    nil, nil, "Redo Change",  redochg],
    ["Cut",       Gtk::Stock::CUT,    nil, nil, "Cut the sel.",  cut],
    ["Copy",      Gtk::Stock::COPY,   nil, nil, "Copy the sel.", copy],
    ["Paste",     Gtk::Stock::PASTE,  nil, nil, "Paste text",    paste],
    ["Clear",     Gtk::Stock::CLEAR,  nil, nil, "Clear text",    clear],
    ["SelectAll", Gtk::Stock::SELECT_ALL, nil, nil, "Sel all",  selectall],
    ["Deselect",  nil, "_Deselect", "d", "Deselect all",   deselect],
    ["Preferences",Gtk::Stock::PREFERENCES,  nil, nil, "Preferences", preferences],
   ["Help", nil, "_Help", nil, nil, nil],
    ["Contents",  Gtk::Stock::HELP,  nil, nil, "Get help",      help],
    ["About",     Gtk::Stock::ABOUT, nil, nil, "More info ...", about]
  ]
    
  return create_menu(entries,"menu03.ui")
  
 end
 
 def event_settings
  self.signal_connect "destroy" do
   app_quit
  end
 end   
 
 def create_menu(entries, menu_ui)
  
  group = Gtk::ActionGroup.new("MenuActionGroup")
  group.add_actions(entries)
  
  ui = Gtk::UIManager.new
   ui.insert_action_group group,0
   ui.add_ui menu_ui
   
   menubar = ui.get_widget "/MenuBar"
   
  self.add_accel_group ui.accel_group
   
   return menubar
 end
 
 def app_quit
  Gtk.main_quit 
 end
 
end

Gtk.init
 window = App.new
Gtk.main

3. Run it:
$ ruby menu03.rb 

4. Results:
a. File menu

b. Edit menu

5, Issues.
a.Again, it automatically translate (into Japanese). How do I suppress, or display in another language such as German or Chinese?
b. Is there a stock item for Page Setup?

6. Reference:
a. http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-dynmc
b. http://zetcode.com/tutorials/rubygtktutorial/menustoolbars/

No comments:

Post a Comment