Class: RubyCurses::MultiContainer

Inherits:
Widget show all
Defined in:
lib/rbcurse/rmulticontainer.rb

Overview

Extends TextView with ability to load more than one file or content and switch between files (buffers). NOTE: ideally, i should be able to dynamically add this functionality to either Textview or TextArea or even ListBox or Table someday. Should then be a Module rather than a class.

Constant Summary

Constants included from Io

Io::ERROR_COLOR_PAIR, Io::FOOTER_COLOR_PAIR, Io::LINEONE, Io::MAIN_WINDOW_COLOR_PAIR

Instance Attribute Summary

Attributes inherited from Widget

#_object_created, #col_offset, #cols_panned, #config, #curpos, #focussed, #form, #id, #parent_component, #row_offset, #rows_panned, #state

Instance Method Summary collapse

Methods inherited from Widget

#changed, #click, #destroy, #enter, #event_list, #focus, #get_preferred_size, #getvalue, #getvalue_for_paint, #height, #height=, #hide, #leave, #modified?, #move, #on_leave, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key, #width, #width=

Methods included from Io

#askchoice, #askyesno, #askyesnocancel, #clear_error, #clear_this, #get_string, #newaskyesno, #old_print_header, #old_print_top_right, #print_action, #print_error, #print_footer_help, #print_header, #print_headers, #print_help, #print_help_page, #print_in_middle, #print_key_labels, #print_key_labels_row, #print_screen_labels, #print_status, #print_this, #print_top_right, #rbgetstr, #warn

Methods included from Utils

#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #view, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Constructor Details

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

include ListScrollable



30
31
32
33
34
35
36
37
# File 'lib/rbcurse/rmulticontainer.rb', line 30

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

end

Instance Method Details

#add(component, title) ⇒ Object

Add a component with a title



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rbcurse/rmulticontainer.rb', line 179

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

#buffer_menuObject

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



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

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



168
169
170
171
172
173
174
# File 'lib/rbcurse/rmulticontainer.rb', line 168

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



57
58
59
# File 'lib/rbcurse/rmulticontainer.rb', line 57

def current_component
  @bmanager.current
end

#delete_componentObject



158
159
160
161
162
163
164
165
166
# File 'lib/rbcurse/rmulticontainer.rb', line 158

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



148
149
150
151
152
# File 'lib/rbcurse/rmulticontainer.rb', line 148

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

#goto_last_componentObject



153
154
155
156
157
# File 'lib/rbcurse/rmulticontainer.rb', line 153

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

#goto_next_componentObject

end



134
135
136
137
138
139
# File 'lib/rbcurse/rmulticontainer.rb', line 134

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

  @current_component = @bmanager.next
  set_current_component
end

#goto_prev_componentObject



141
142
143
144
145
146
147
# File 'lib/rbcurse/rmulticontainer.rb', line 141

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



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
# File 'lib/rbcurse/rmulticontainer.rb', line 62

def handle_key ch  #:nodoc:
  #$log.debug " MULTI handlekey #{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
  # check for any keys not handled and check our own ones
  return ret # 
end

#init_varsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbcurse/rmulticontainer.rb', line 38

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 ||= "multicontainer"
end

#list_componentsObject



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rbcurse/rmulticontainer.rb', line 200

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



213
214
215
# File 'lib/rbcurse/rmulticontainer.rb', line 213

def on_enter
  set_form_row
end

#perror(errmess) ⇒ Object



196
197
198
199
# File 'lib/rbcurse/rmulticontainer.rb', line 196

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

:nodoc:



97
98
99
100
101
102
# File 'lib/rbcurse/rmulticontainer.rb', line 97

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:



103
104
105
106
# File 'lib/rbcurse/rmulticontainer.rb', line 103

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

#repaintObject



91
92
93
94
95
96
# File 'lib/rbcurse/rmulticontainer.rb', line 91

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

#set_current_componentObject



191
192
193
194
195
# File 'lib/rbcurse/rmulticontainer.rb', line 191

def set_current_component
  @current_component = @current_component.component
  @current_title = @current_component.title
  @current_component.repaint_all true
end

#set_form_rowObject

:nodoc:



216
217
218
219
220
221
222
# File 'lib/rbcurse/rmulticontainer.rb', line 216

def set_form_row  #:nodoc:
  if !@current_component.nil?
    #$log.debug " #{@name} set_form_row calling sfr for #{@current_component.name} "
    @current_component.set_form_row 
    @current_component.set_form_col 
  end
end