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
  <menubar name="MenuBar">
    <menu name="FileMenu" action="File">
   <menuitem name="FileNew" action="New"/>
      <menuitem name="FileOpen" action="Open"/>
      <menuitem name="FileSave" action="Save"/>
      <menuitem name="FileSaveAs" action="SaveAs"/>
      <menuitem name="FileRevert" action="Revert"/>
      <menuitem name="FilePageSetup" action="PageSetup"/>
      <menuitem name="FilePreview" action="Preview"/> 
      <menuitem name="FilePrint" action="Print"/> 
      <menuitem name="FileQuit" action="Quit"/>
    <menu name="EditMenu" action="Edit">
   <menuitem name="EditUndo" action="Undo"/>
   <menuitem name="EditRedo" action="Redo"/>
      <menuitem name="EditCut" action="Cut"/>
      <menuitem name="EditCopy" action="Copy"/>
      <menuitem name="EditPaste" action="Paste"/>
      <menuitem name="EditClear" action="Clear"/>
      <menuitem name="EditSelectAll" action="SelectAll"/>
      <menuitem name="EditDeselect" action="Deselect"/>
      <menuitem name="EditPreferences" action="Preferences"/>
    <menu name="HelpMenu" action="Help">
      <menuitem name="HelpContents" action="Contents"/>
      <menuitem name="HelpAbout" action="About"/>

2. Use it in the program to create menu.
# menu03.rb
# using UIManager
# requires menu03.ui

require 'gtk2'

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

 def layout_settings(menubar)
  vbox = false, 3
  vbox.pack_start menubar, false, false, 0
  self.add vbox
  return vbox
 def menu_settings    
  # callback procs! 
  new      = { puts "new" }
  open      = { puts "open" }
  save      = { puts "save" }
  saveas    = { puts "saveas" }
  revert    = { puts "revert" }
  pagesetup = { puts "pagesetup" }
  preview   = { puts "preview" }
  print     = { puts "print" }
  quit    = { app_quit }
  undo      = { puts "undo" }
  redochg   = { puts "redo" }
  cut       = { puts "cut" }
  copy      = { puts "copy" }
  paste     = { puts "paste" }
  clear     = { puts "clear" }
  selectall = { puts "selectall" }
  deselect  = { puts "deselect" }
  preferences = { puts "preferences" }
  help      = { puts "help" }
  about     = { 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")
 def event_settings
  self.signal_connect "destroy" do
 def create_menu(entries, menu_ui)
  group ="MenuActionGroup")
  ui =
   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
 def app_quit

 window =

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:

No comments:

Post a Comment