Class: RubyCurses::MenuItem

Inherits:
Object show all
Defined in:
lib/rbcurse/core/widgets/rmenu.rb

Overview

Items in menus. These will usually result in an action which closes the entire

menubar.

Direct Known Subclasses

CheckBoxMenuItem, Menu

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, mnemonic = nil, &block) ⇒ MenuItem

Returns a new instance of MenuItem.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 78

def initialize text, mnemonic=nil, &block
  @text = text
  @enabled = true
  # check for mnem that is not one char, could be an accelerator
  if mnemonic
    if mnemonic.length != 1
      $log.error "MenuItem #{text} mnemonic #{mnemonic}  should be one character. Maybe you meant accelerator? " 
      mnemonic = nil
    end
  end
  @mnemonic = mnemonic
  instance_eval &block if block_given?
end

Instance Attribute Details

#accelerator(*val) ⇒ Object

add accelerator for a menu item NOTE: accelerator means that the application has tied this string to some action, outside of the menu bar. It does not mean that the menu bar will trigger the action. So the app still has to define the action and bind a key to that accelerator. This is only informative. Had to do this since dsl_accessor was throwing some nilclass does not have []= nomethod error This allows user to put accelerator inside dsl block

Examples:

accelerator "Ctrl-X"


108
109
110
111
112
113
114
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 108

def accelerator(*val)
  if val.empty?
    return @accelerator
  else
    @accelerator = val[0]
  end
end

#active_indexObject (readonly)

2011-09-24 V1.3.1 trying to do a right



76
77
78
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 76

def active_index
  @active_index
end

#bgcolorObject

2011-09-25 V1.3.1



74
75
76
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 74

def bgcolor
  @bgcolor
end

#coffsetObject

Returns the value of attribute coffset.



70
71
72
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 70

def coffset
  @coffset
end

#colObject

Returns the value of attribute col.



69
70
71
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 69

def col
  @col
end

#colorObject

2011-09-25 V1.3.1



74
75
76
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 74

def color
  @color
end

#color_pairObject

2011-09-25 V1.3.1



75
76
77
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 75

def color_pair
  @color_pair
end

#enabledObject

Returns the value of attribute enabled.



73
74
75
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 73

def enabled
  @enabled
end

#mnemonicObject

changed reader to accessor



77
78
79
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 77

def mnemonic
  @mnemonic
end

#parentObject

Returns the value of attribute parent.



66
67
68
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 66

def parent
  @parent
end

#rowObject

attr_accessor :window



68
69
70
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 68

def row
  @row
end

#textObject

changed reader to accessor



77
78
79
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 77

def text
  @text
end

#widthObject

Returns the value of attribute width.



71
72
73
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 71

def width
  @width
end

Instance Method Details

#command(*args, &block) ⇒ Object



94
95
96
97
98
99
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 94

def command *args, &block 
  $log.debug ">>>command : #{@text} "
  @command = block if block_given?
  alert "Command nil or some error! #{text} " unless @command
  @args = args
end

#destroyObject



179
180
181
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 179

def destroy
 $log.debug "DESTROY menuitem #{@text}"
end

#fireObject

XXX it could be a menu again

We should not be firing a :NO_MENUITEMS


124
125
126
127
128
129
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 124

def fire
  $log.debug ">>>fire menuitem : #{@text} #{@command} "
  @command.call self, *@args if !@command.nil?
  @parent.clear_menus
  return :CLOSE # added 2009-01-02 00:09 to close only actions, not submenus
end

#highlight(tf = true) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 130

def highlight tf=true
  if @parent.nil? or @parent.window.nil?
    #$log.debug "HL XXX #{self} parent nil"
    #$log.debug "HL XXX #{self} - > #{@parent} parent nil"
  end
  if tf
    #color = $datacolor
    #@parent.window.mvchgat(y=@row, x=1, @width, Ncurses::A_NORMAL, color, nil)
    # above line did not work in vt100, 200 terminals, next works.
#        @parent.window.mvchgat(y=@row, x=1, @width, Ncurses::A_REVERSE, $reversecolor, nil) # changed 2011 dts  2011-09-24  multicolumn, 1 skips the border
    @color_pair  ||= get_color($reversecolor, @color, @bgcolor)
    @parent.window.mvchgat(y=@row, x=@col+1, @width, Ncurses::A_REVERSE, @color_pair, nil)
    #@parent.window.mvaddch @row, @col, "*".ord
    #@parent.window.wmove @row, @col # 2011-09-25 V1.3.1  NO EFFECT
  else
    repaint
  end
  @parent.window.wrefresh  unless @parent.window.nil? ## XXX 2009-01-21 22:00 
end

#on_enterObject

item



115
116
117
118
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 115

def on_enter #item
  highlight
  #@parent.window.wmove @row, @col+1  # 2011-09-25 V1.3.1  NO EFFECT
end

#on_leaveObject



119
120
121
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 119

def on_leave
  highlight false
end

#repaintObject

menuitem.repaint



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 149

def repaint # menuitem.repaint
  if @parent.nil? or @parent.window.nil?
    $log.debug "repaint #{self} parent nil"
  #  return
  end
  r = @row
  c = @col
  ltext = text
  ltext = "* No Items *" if text == :NO_MENUITEMS
  @color_pair  = get_color($reversecolor, @color, @bgcolor)
  #acolor = $reversecolor
  acolor = @color_pair
  acolor = get_color($reversecolor, 'green', @bgcolor) if !@enabled
#      @parent.window.printstring( @row, 0, "|%-*s|" % [@width, ltext], acolor) # changed 2011 2011-09-24  
  @parent.window.printstring( @row, c, "|%-*s|" % [@width, ltext], acolor)
  if @enabled # 2010-09-10 23:56 
  if !@accelerator.nil?
    # FIXME add c earlier 0 was offset
    @parent.window.printstring( r, (@width+1)-@accelerator.length, @accelerator, acolor)
  elsif !@mnemonic.nil?
    m = @mnemonic
    ix = text.index(m) || text.index(m.swapcase)
    charm = text[ix,1]
    #@parent.window.printstring( r, ix+1, charm, $datacolor) if !ix.nil?
    # prev line changed since not working in vt100 and vt200
    @parent.window.printstring( r, ix+1, charm, $reversecolor, 'reverse') if !ix.nil?
  end
  #@parent.window.wmove r, c # NO EFFECT
  end
end

#to_sObject



91
92
93
# File 'lib/rbcurse/core/widgets/rmenu.rb', line 91

def to_s
  "#{@text} #{@accelerator}"
end