1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Adding a Status Bar to Your WxRuby Applications

Using WxRuby: Add a Status Bar

By

Adding a status bar to your WxRuby applications is very easy, and can be done with just a few lines of code. Adding a StatusBar object to a Frame is done in much the same way as adding a MenuBar . A new StatusBar object is created, and is assigned to the Frame's status_bar attribute. This allows the frame to automatically update the status bar with certain messages when the mouse is over specific menu items.

Once your StatusBar object is created and assigned to the frame, it can be retrieved using the status_bar attribute. This means you don't also have to keep the StatusBar object in a variable, as it can be retrieved from the Frame at any time.

The StatusBar display text is manipulated as a stack. When you want to display a message, you push it to the status bar. When you're finished displaying the message, pop it from the stack. This allows you to push a message onto the status bar and, for example, when you mouse over aa menu item, it will push a second message onto the status bar stack. The status bar only displays the top-most message, so it will only display the message put there by the menu bar. However, when the menu bar is finished, it will pop the message from the status bar stack. Your original message will then become the top-most and will be displayed.

The following is a modification of the previous menu bar example with an added status bar. Note: The portions of code pertaining to the status bar are highlighted in bold.

 #!/usr/bin/env ruby
 require 'rubygems'
 require 'wx'
 
 class AppFrame < Wx::Frame
   def initialize(title)
     # Call parent class' initialize function, and add int
     # a few more arguments besides the title. Since this
     # is a top-level frame, there is no parent widget, so
     # use nil for the first argument.
     super( nil, :title => title, :size => [400, 300] )
 
      # Create the Wx::StatusBar object. This displays a
     # small bar at the bottom where status messages may be
     # displayed.
     status = Wx::StatusBar.new(self)
 
     # Assign the status bar to the Frame's status_bar
     # attribute. This will automaticaly show the status
     # bar and update it when the mouse is over certain
     # widgets such as menu items.
     self.status_bar = status
 
     # Display something on the status bar.
     status.push_status_text "Status bar test program"
  
     # Create the Wx::MenuBar object. This holds all the
     # Wx::Menu objects to create the menu hierarchy.
     menu = Wx::MenuBar.new
 
     # Start with the File menu. Only a single thing here,
     # the Exit option.
     file = Wx::Menu.new
 
     # These won't do anything, they're just there for show
     file.append( Wx::ID_ANY, "&Open\tAlt-O", "Open File" )
     file.append( Wx::ID_ANY, "&Close\tAlt-C", "Close File" )
 
     # Use seperators between groups of menu options
     file.append_separator
 
     # The Exit option has a special ID, Wx::ID_EXIT, which
     # will do any platform-specific magic to translate and
     # put the Exit option in the correct place.
     file.append( Wx::ID_EXIT, "E&xit\tAlt-X", "Quit" )
     menu.append( file, "&File" )
 
     # Create the Help menu
     help = Wx::Menu.new
     help.append( Wx::ID_ABOUT, "&About...\tF1", "Show about dialog" )
     menu.append( help, "&Help" )
 
     self.menu_bar = menu
 
     # Setup our callbacks to handle menu events
     evt_menu( Wx::ID_EXIT, :on_quit )
     evt_menu( Wx::ID_ABOUT, :on_about )
   end
 
   # Close the window when the user clicks Quit in the File
   # menu
   def on_quit
     close
   end
 
   # Display an "About this program" dialog
   def on_about
     Wx::about_box(
       :name => self.title,
       :version => "1.0",
       :description => "Status bar example"
     )
   end
 end
 
 class MyApp < Wx::App
   def on_init
     @frame = AppFrame.new("Menu test")
     @frame.show
   end
 end
 
 app = MyApp.new
 app.main_loop
 
  1. About.com
  2. Technology
  3. Ruby
  4. Graphical User Interfaces
  5. WxRuby
  6. Using WxRuby--Adding a Status Bar in WxRuby

©2014 About.com. All rights reserved.