Class: RubyCurses::MultiForm

Inherits:
Widget
  • Object
show all
Defined in:
lib/rbcurse/experimental/widgets/multiform.rb

Overview

Trying to implement TabbedPanes using a multiform approach, but no more windows or pads. Try to keep it as simple as possible

Instance Method Summary collapse

Constructor Details

#initialize(form = nil, config = {}, &block) ⇒ MultiForm

Returns a new instance of MultiForm.



23
24
25
26
27
28
29
30
31
32
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 23

def initialize form = nil, config={}, &block
  @focusable = true
  @window = form.window
  @row_offset = @col_offset = 1
  @bmanager = BufferManager.new self
  @forms = []
  super
  init_vars

end

Instance Method Details

#_add_to(index, component) ⇒ Object

Raises:

  • (ArgumentError)


184
185
186
187
188
189
190
191
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 184

def _add_to(index, component)
  raise ArgumentError, "index out of bounds" unless @forms[index]
  component.set_form(@forms[index])
  $log.debug "XXX: comp r c #{component.row} #{component.col} "
  component.row += @row
  component.col += @col
  $log.debug "XXX: after comp r c #{component.row} #{component.col} "
end

#add(component, title) ⇒ Object

Add a component with a title

Parameters:

  • component (Widget)
  • title (String)


196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 196

def add component, title
  component.row = @row+@row_offset+0 # FFI changed 1 to 0 2011-09-12 
  component.col = @col+@col_offset+0 # FFI changed 1 to 0 2011-09-12 
  component.width = @width-2
  component.height = @height-2
  component.form = @form
  component.override_graphic(@graphic)
  @current_component = @bmanager.add component, title
  set_current_component
  set_form_row ## FFI added 2011-09-12 to get cursor at start when adding
  $log.debug " ADD got cb : #{@current_component} "
end

#add_form(title, config = {}) {|frm| ... } ⇒ Object

Yields:

  • (frm)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 169

def add_form title, config={}, &blk
  $log.debug "XXX: add_form window coords were hwtl #{@height} , #{@width} , #{@top} , #{@left} "
  #w = VER::Window.new 16, 30, 2, 2 #@height-1, @width-1, @top, @left
  w = VER::Window.new @height-2, @width-2, @row+1, @col+1
  #w.box(0,0)
  frm = Form.new w
  @forms << frm
  frm.parent_form = @form
  frm.add_cols=@col+@col_offset
  frm.add_rows=@row+@row_offset
  @current_component = @bmanager.add frm, title
  set_current_component
  yield frm if block_given?
  return @current_component # not a form but a Rcomponent containing component and title
end

#buffer_menuObject

this is just a test of the simple “most” menu can use this for next, prev, first, last, new, delete, overwrite etc



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 110

def buffer_menu
  menu = PromptMenu.new self 
  menu.add(menu.create_mitem( 'l', "list buffers", "list buffers ", :list_components ))
  item = menu.create_mitem( 'b', "Buffer Options", "Buffer Options" )
  menu1 = PromptMenu.new( self, "Buffer Options")
  menu1.add(menu1.create_mitem( 'n', "Next", "Switched to next buffer", :goto_next_component ))
  menu1.add(menu1.create_mitem( 'p', "Prev", "Switched to previous buffer", :goto_prev_component ))
  menu1.add(menu1.create_mitem( 'f', "First", "Switched to first buffer", :goto_first_component ))
  menu1.add(menu1.create_mitem( 'l', "Last", "Switched to last buffer", :goto_last_component ))
  menu1.add(menu1.create_mitem( 'd', "Delete", "Deleted buffer", :delete_component ))
  item.action = menu1
  menu.add(item)
  # how do i know what's available. the application or window should know where to place
  menu.display @form.window, $error_message_row, $error_message_col, $datacolor #, menu
end

#component_at(index) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 162

def component_at index
  cc = @bmanager.element_at index
  return unless cc 
  @current_component = cc 
  #$log.debug " buffer_last got #{@current_component} "
  set_current_component
end

#current_componentRBuffer

returns current buffer

Returns:

  • (RBuffer)

    current buffer



52
53
54
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 52

def current_component
  @bmanager.current
end

#delete_componentObject



152
153
154
155
156
157
158
159
160
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 152

def delete_component
  if @bmanager.size > 1
    @bmanager.delete_at
    @current_component = @bmanager.previous
    set_current_component
  else
    perror "Only one buffer. Cannot delete."
  end
end

#goto_first_componentObject



142
143
144
145
146
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 142

def goto_first_component
  @current_component = @bmanager.first
  $log.debug " buffer_first got #{@current_component} "
  set_current_component
end

#goto_last_componentObject



147
148
149
150
151
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 147

def goto_last_component
  @current_component = @bmanager.last
  $log.debug " buffer_last got #{@current_component} "
  set_current_component
end

#goto_next_componentObject



127
128
129
130
131
132
133
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 127

def goto_next_component
  perror "No other buffer" and return if @bmanager.size < 2

  @current_component = @bmanager.next
  set_current_component
  # set_form_row and shit
end

#goto_prev_componentObject



135
136
137
138
139
140
141
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 135

def goto_prev_component
  perror "No other buffer" and return if @bmanager.size < 2

  @current_component = @bmanager.previous
  $log.debug " buffer_prev got #{@current_component} "
  set_current_component
end

#handle_key(ch) ⇒ Object

multi-container



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 57

def handle_key ch  #:nodoc:
  @current_component.repaint
  @current_component.window.wrefresh
  $log.debug " MULTI handle_key #{ch}, #{@current_component}"
  ret = :UNHANDLED
  return :UNHANDLED unless @current_component
  ret = @current_component.handle_key(ch)
  $log.debug " MULTI comp #{@current_component} returned #{ret} "
  if ret == :UNHANDLED
    # check for bindings, these cannot override above keys since placed at end
    begin
      ret = process_key ch, self
      $log.debug " MULTI = process_key returned #{ret} "
      if ch > 177 && ch < 187
        n = ch - 177
        component_at(n)
        # go to component n
      end
    rescue => err
#          $error_message = err # changed 2010 dts  
      $error_message.value = err.to_s
      #@form.window.print_error_message PLEASE CREATE LABEL
      $log.error " Multicomponent process_key #{err} "
      $log.debug(err.backtrace.join("\n"))
      alert err.to_s
    end
    return :UNHANDLED if ret == :UNHANDLED
  end
  @current_component.repaint
  @current_component.window.wrefresh
  FFI::NCurses.update_panels

  # check for any keys not handled and check our own ones
  return ret # 
end

#init_varsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 33

def init_vars
  super
  # the following allows us to navigate buffers with :bn :bp etc (with Alt pressed)
  bind_key(?\M-:, :buffer_menu)
  bind_key(?\M-;, :buffer_menu)
  # bind_key([?\C-x, ?f], :file_edit)
  bind_key([?\C-x, ?k], :delete_component)
  bind_key([?\C-x, ?\C-b], :list_components)
  bind_key(?\M-n, :goto_next_component)
  bind_key(?\M-p, :goto_prev_component)
  bind_key(?\M-1, :goto_first_component)
  # easily cycle using p. n is used for next search.
  #bind_key(?p, :buffer_previous)
  @suppress_borders = false 
  @repaint_all = true 
  @name ||= "multiform"
end

#list_componentsObject



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 223

def list_components
  $log.debug " TODO buffers_list: #{@bmanager.size}  "
  menu = PromptMenu.new self 
  @bmanager.each_with_index{ |b, ix|
    aproc = Proc.new { component_at(ix) }
    name = b.title
    num = ix + 1
    menu.add(menu.create_mitem( num.to_s, name, "Switched to buffer #{ix}", aproc ))
  }
  menu.display @form.window, $error_message_row, $error_message_col, $datacolor
end

#on_enterObject

required otherwise some components may not get correct cursor position on entry e.g. table



236
237
238
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 236

def on_enter
  set_form_row # 2011-10-17 
end

#perror(errmess) ⇒ Object



219
220
221
222
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 219

def perror errmess
  alert errmess
  #@form.window.print_error_message errmess
end

:nodoc:



98
99
100
101
102
103
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 98

def print_border  #:nodoc:
  $log.debug " #{@name} print_borders,  #{@graphic.name} "
  color = $datacolor
  @graphic.print_border_only @row, @col, @height-1, @width, color #, Ncurses::A_REVERSE
  print_title
end

:nodoc:



104
105
106
107
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 104

def print_title  #:nodoc:
  $log.debug " print_title #{@row}, #{@col}, #{@width} #{@title} "
  @graphic.printstring( @row, @col+(@width-@title.length)/2, @title, $datacolor, @title_attrib) unless @title.nil?
end

#repaintObject



92
93
94
95
96
97
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 92

def repaint
  print_border if (@suppress_borders == false && @repaint_all) # do this once only, unless everything changes
  return unless @current_component
  $log.debug " MULTIFORM REPAINT "
  ret = @current_component.repaint
end

#set_current_componentObject



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 208

def set_current_component
  @title = @current_component.title
  @current_component = @current_component.component
  @current_component.repaint #2011-10-17 need to set all comps to repaint XXX
  @current_component.window.wrefresh
  @current_component.window.show
  FFI::NCurses.update_panels
  set_form_row
  #@current_title = @current_component.title
  #@current_component.repaint_all true 2011-10-17 need to set all comps to repaint XXX
end

#set_form_rowObject

nothing happening, the cursor goes to 1,1 and does not move alhough the fields can be edited XXX



241
242
243
244
245
246
247
248
249
250
# File 'lib/rbcurse/experimental/widgets/multiform.rb', line 241

def set_form_row  #:nodoc:
  if !@current_component.nil?
    #$log.debug " #{@name} set_form_row calling sfr for #{@current_component.name} "
    #fc = @current_component.widgets[0]
    fc = @current_component.get_current_field
    fc ||= @current_component.widgets[0]
    fc.set_form_row  if fc
    #fc.set_form_col 
  end
end