Sunday, October 3, 2010

GTK Programming in Ruby (6) - Adding Status Bar

1. Main
#
# statusbar01.rb
#

require 'gtk2'
require 'ui_module02.rb'

class App < Gtk::Window
 
 include UIModule # 'ui_module02.rb'
 
 def initialize
  super
  window_settings('Status Bar', 700, 500, Gtk::Window::POS_CENTER)
  ui_initialize                   
  layout_settings 
  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
  vbox = Gtk::VBox.new false, 5
  vbox.pack_start ui_create_menu, false, false, 0
  vbox.pack_start ui_create_toolbar, false, false, 0
  vbox.pack_start Gtk::Label.new(''), true, false, 0
  vbox.pack_start ui_create_statusbar, 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. UI Module
#
# ui_module02.rb
#
module UIModule

 def ui_initialize
 
  ui_create_statusbar
  
  # callback procs! 
  new       = Proc.new { set_statusbar "new" }
  open      = Proc.new { set_statusbar "open" }
  save      = Proc.new { set_statusbar "save" }
  saveas    = Proc.new { set_statusbar "saveas" }
  revert    = Proc.new { set_statusbar "revert" }
  pagesetup = Proc.new { set_statusbar "pagesetup" }
  preview   = Proc.new { set_statusbar "preview" }
  print     = Proc.new { set_statusbar "print" }
  quit    = Proc.new { app_quit }
  undo      = Proc.new { set_statusbar "undo" }
  redochg   = Proc.new { set_statusbar "redo" }
  cut       = Proc.new { set_statusbar "cut" }
  copy      = Proc.new { set_statusbar "copy" }
  paste     = Proc.new { set_statusbar "paste" }
  clear     = Proc.new { set_statusbar "clear" }
  selectall = Proc.new { set_statusbar "selectall" }
  deselect  = Proc.new { set_statusbar "deselect" }
  preferences = Proc.new { set_statusbar "preferences" }
  help      = Proc.new { set_statusbar "help" }
  about     = Proc.new { set_statusbar "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 

 # create status bar
 def ui_create_statusbar
  @statusbar = Gtk::Statusbar.new
  return @statusbar
 end

 # set statusbar message
 def set_statusbar(message)
  context = @statusbar.get_context_id('StatusMessage')
  @statusbar.pop(context)
  @statusbar.push(context,message)
 end
 
end


3. ui 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>


4. Run it.
$ ruby statusbar01.rb
5. Result.

No comments:

Post a Comment