Module: WidgetWrapper::Menus::Menu

Defined in:
lib/wx_wrapper/menu.rb

Overview

Menu Syntax (Called after a menu bar block)

menu "&File" do |item|
  item.add :new
  item.add :open
  item.separator
  item.add :save
  item.separator
  item.add :preview
  item.add :print
  item.separator
  item.add :close
  item.add :quit
end

Default menu items you can add are: new, open, close, exit, quit, save, print, preview, about, undo, redo, cut, copy, paste, delete, selectall, preferences, find, replace

Default menu items have their operating system level icons loaded automatically.

Instance Method Summary collapse

Instance Method Details

#add(item, options = {}) ⇒ Object

Creates a menu item on the menu. Example syntax:

item.add :close


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/wx_wrapper/menu.rb', line 120

def add(item, options={})
  icon = options.include?(:icon) ? get_icon(options[:icon]) : default_items[item][:icon]
  
  # 1st argument is the icon id, the next is menu item text, and then the next is the help text
  args = []
  args << icon
  args << (options.include?(:item) ? options[:item] : default_items[item][:item])
  args << (options.include?(:help) ? options[:help] : default_items[item][:help])
  
  append(*args)
end

#default_itemsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wx_wrapper/menu.rb', line 93

def default_items
  { 
    :new => { :icon => get_icon(:new), :item => "&New\tCtrl-N", :help => "Create a file" },
    :open => { :icon => get_icon(:open), :item => "&Open\tCtrl-O", :help => "Open a file" }, 
    :close => { :icon => get_icon(:close), :item => "&Close\tCtrl-W", :help => "Close the file" },
    :exit => { :icon => get_icon(:exit), :item => "E&xit\tAlt-F4", :help => "Exit Application" },
    :quit => { :icon => get_icon(:exit), :item => "&Quit\tCtrl-Q", :help => "Quit Application" },
    :save => { :icon => get_icon(:save), :item => "&Save\tCtrl-S", :help => "Save the file" },
    :print => { :icon => get_icon(:print), :item => "&Print...\tCtrl-P", :help => "Print the file" },
    :preview => { :icon => get_icon(:preview), :item => "Print Previe&w\tShift-Ctrl-P", :help => "Print preview the file" },
    :about => { :icon => get_icon(:about), :item => "&About\tF1", :help => "About" },
    :undo => { :icon => get_icon(:undo), :item => "&Undo\tCtrl+Z", :help => "Undo" },
    :redo => { :icon => get_icon(:redo), :item => "&Redo\tCtrl+Shift+Z", :help => "Redo" },
    :cut => { :icon => get_icon(:cut), :item => "Cu&t\tCtrl+X", :help => "Undo" },
    :copy => { :icon => get_icon(:copy), :item => "&Copy\tCtrl+C", :help => "Copy" },
    :paste => { :icon => get_icon(:paste), :item => "&Paste\tCtrl+V", :help => "Paste" },
    :delete => { :icon => get_icon(:delete), :item => "&Delete", :help => "Delete" },
    :selectall => { :icon => get_icon(:selectall), :item => "Select &All\tCtrl+A", :help => "Select All" },
    :preferences => { :icon => get_icon(:preferences), :item => "Pr&eferences", :help => "Preferences" },
    :find => { :icon => get_icon(:find), :item => "&Find...\tCtrl+F", :help => "Find" },
    :replace => { :icon => get_icon(:replace), :item => "&Replace...\tCtrl+H", :help => "Replace" }
  }
end

#get_icon(icon) ⇒ Object

TODO: PUT into another module



147
148
149
# File 'lib/wx_wrapper/menu.rb', line 147

def get_icon(icon)
  Wx.const_get("ID_#{icon.to_s.upcase}")
end

#items(options = {}) ⇒ Object

Lists the menu item objects for a given menu



138
139
140
141
142
143
144
# File 'lib/wx_wrapper/menu.rb', line 138

def items(options={})
  items_list = (1..get_menu_item_count-1).collect do |position|
    value = find_item_by_position(position)
    value unless value.get_kind == -1 && !options.include?(:include_separators)
  end
  items_list.compact
end

#separatorObject

Creates a new separator for the menu



133
134
135
# File 'lib/wx_wrapper/menu.rb', line 133

def separator
  append_separator
end