Friday, September 24, 2010

GTK Programming in Ruby (5) - Adding Toolbar, using UIManager

1. Main program showing a window with a toolbar.
#
# toolbar01.rb
#
require 'gtk2'
require 'ui_module.rb'

class App < Gtk::Window
 
 include UIModule # 'ui_module.rb'
 
 def initialize
  super
  window_settings('Toolbar using UIManager', 700, 500, Gtk::Window::POS_CENTER)
  ui_initialize # 'ui_module.rb'
  vbox = layout_settings(ui_create_menu,ui_create_toolbar) # 'ui_module.rb'
  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,toolbar)
  vbox = Gtk::VBox.new false, 4
  vbox.pack_start menubar, false, false, 0
  vbox.pack_start toolbar, false, false, 0
  self.add vbox
  return vbox
 end
 
 def event_settings
  self.signal_connect "destroy" do
   app_quit
  end
 end   
 
 def app_quit
  Gtk.main_quit 
 end
 
end

Gtk.init
 window = App.new
Gtk.main


2. Define Menu and Tool bar structure in a separate file.

menu_toolbar.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>
  <toolbar name="ToolBar">
   <toolitem name="FileNew" action="New"/>
      <toolitem name="FileOpen" action="Open"/>
      <toolitem name="FileSave" action="Save"/>
      <toolitem name="FileSaveAs" action="SaveAs"/>
      <toolitem name="FileRevert" action="Revert"/>
      <toolitem name="FilePreview" action="Preview"/> 
      <toolitem name="FilePrint" action="Print"/> 
      <separator/>  
   <toolitem name="EditUndo" action="Undo"/>
   <toolitem name="EditRedo" action="Redo"/>
      <toolitem name="EditCut" action="Cut"/>
      <toolitem name="EditCopy" action="Copy"/>
      <toolitem name="EditPaste" action="Paste"/>
      <toolitem name="EditClear" action="Clear"/>
      <toolitem name="EditSelectAll" action="SelectAll"/>
      <toolitem name="EditPreferences" action="Preferences"/>
   <separator/>
      <toolitem name="HelpContents" action="Contents"/>
      <toolitem name="HelpAbout" action="About"/>
      <separator/>
      <toolitem name="FileQuit" action="Quit"/>
  </toolbar>
</ui>


3. UI manager handler.

#
# ui_module.rb
#

module UIModule

 def ui_initialize
 
  # callback procs! 
  new       = Proc.new { puts "new" }
  open      = Proc.new { puts "open" }
  save      = Proc.new { puts "save" }
  saveas    = Proc.new { puts "saveas" }entries
  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]
   ]
  
  # create an action group with entries defined above
  group = Gtk::ActionGroup.new("ActionGroup")
  group.add_actions(entries)
  
  # create ui manager object and add action group
  @ui_mgr = Gtk::UIManager.new
   @ui_mgr.insert_action_group group,0
   self.add_accel_group @ui_mgr.accel_group
   
 end
 
 # create menubar using ui manager
 def ui_create_menu   
  return ui_get_widget("menu_toolbar.ui", "/MenuBar") 
 end
 
 # create toolbar using ui manager
 def ui_create_toolbar
   return ui_get_widget("menu_toolbar.ui", "/ToolBar") 
 end

 # utility function to create a widget using ui manager
 def ui_get_widget(uifile, name)
  @ui_mgr.add_ui uifile
   widget = @ui_mgr.get_widget name   
   return widget
 end 
 
end

4. Run it.
$ ruby toolbar01.rb

5. Result.

No comments:

Post a Comment