module Canis::WidgetShortcuts

Public Class Methods

def_widget(path, klass, short=nil) click to toggle source

create a shortcut for a class path is path of file to use in require starting with canis klass is name of class to instantiate

# File lib/canis/core/util/widgetshortcuts.rb, line 89
def self.def_widget(path, klass, short=nil)
  p=""
   if path
    p="require \"#{path}\""
  end
   short ||= klass.to_s.downcase
    eval %{
      def #{short}(config={}, &block)
        if config.is_a? String
           _s = config
           config = {}
           config[:text] = _s
        end
        #{p}
        w = #{klass}.new nil, config
        _position w
        w.command &block if block_given?
        return w
      end
    }
end

Public Instance Methods

_configure(s) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 281
def _configure s
  s[:row] ||= 0
  s[:col] ||= 0
  s[:row] += (s[:margin_top] || 0)
  s[:col] += (s[:margin_left] || 0)
  s[:width] = FFI::NCurses.COLS if s[:width] == :expand
  last = @_ws_active.last
  if last
    if last.is_a? WsStack
      s[:row] += (last[:row] || 0)
      s[:col] += (last[:col] || 0)  
    else
      s[:row] += (last[:row] || 0)
      s[:col] += (last[:col] || 0)  # we are updating with item_width as each st finishes
      s[:width] ||= last[:item_width] #
    end
  end
  s[:components] = []
end
_position(w) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 151
def _position w
  cur = @_ws_active.last
  # this is outside any stack or flow, so we do the minimal
  # user should specify row and col
  unless cur
    w.row ||= 0
    w.col ||= 0
    $log.debug "XXX:  LABEL #{w.row} , #{w.col} "
    w.set_form @form if @form # temporary,, only set if not inside an object FIXME
    if w.width == :expand
      w.width = FFI::NCurses.COLS-0 # or take windows width since this could be in a message box
    end
    if w.height == :expand
      # take from current row, and not zero  FIXME
      w.height = FFI::NCurses.LINES-0 # or take windows width since this could be in a message box
    end
    return
  end
  r = cur[:row] || 0
  c = cur[:col] || 0
  w.row = r
  w.col = c
  if cur.is_a? WsStack
    r += w.height || 1
    cur[:row] = r
  else
    wid = cur[:item_width] || w.width || 10
    c += wid + 1
    cur[:col] = c
  end
  if w.width == :expand
    w.width = cur[:width] or raise "Width not known for stack"
  end
  if w.height == :expand
    w.height = cur[:height] or raise "height not known for flow"
  end
  w.color   ||= cur[:color]
  w.bgcolor ||= cur[:bgcolor]
  w.set_form @form if @form # temporary
  @_ws_components << w
  cur[:components] << w
end
app_header(title, config={}) click to toggle source

add a standard application header

Example

header = app_header "canis ", :text_center => "Browser Demo", :text_right =>"New Improved!", 
     :color => :black, :bgcolor => :white, :attr => :bold
# File lib/canis/core/util/widgetshortcuts.rb, line 126
def app_header title, config={}, &block
  require 'canis/core/widgets/applicationheader'
  header = ApplicationHeader.new @form, title, config, &block
end
blank() click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 61
def blank
  label :text => ""
end
box(config={}) click to toggle source

flow and stack could have a border option

# File lib/canis/core/util/oldwidgetshortcuts.rb, line 243
def box config={}, &block
  require 'canis/core/widgets/box'
  # take current stacks row and col
  # advance row by one and col by one
  # at end note row and advance by one
  # draw a box around using these coordinates. width should be
  # provided unless we have item width or something.
  last = @_ws_active.last
  if last
    r = last[:row]
    c = last[:col]
    config[:row] = r
    config[:col] = c
    last[:row] += config[:margin_top] || 1
    last[:col] += config[:margin_left] || 1
    _box = Box.new @form, config # needs to be created first or will overwrite area after others painted
    yield_or_eval &block if block_given?
    h = config[:height] || last[:height] || (last[:row] - r)
    h = 2 if h < 2
    w = config[:width] || last[:width] || 15 # tmp
    case last
    when WsFlow
      w = last[:col]
    when WsStack
      #h += 1
    end
    config[:row] = r
    config[:col] = c
    config[:height] = h
    config[:width] = w
    _box.row r
    _box.col c
    _box.height h
    _box.width w
    last[:row] += 1
    last[:col] += 1 # ??? XXX if flow we need to increment properly or not ?
  end
end
button(config={}) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 84
def button config={}, &block
  w = Button.new nil, config #, &block
  _position w
  if block
    w.bind(:PRESS, &block)
  end
  return w
end
check(config={}) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 76
def check config={}, &block
  w = CheckBox.new nil, config #, &block
  _position w
  if block
    w.bind(:PRESS, &block)
  end
  return w
end
dock(labels, config={}) click to toggle source

prints pine-like key labels

# File lib/canis/core/util/widgetshortcuts.rb, line 198
def dock labels, config={}, &block
  require 'canis/core/widgets/keylabelprinter'
  klp = Canis::KeyLabelPrinter.new @form, labels, config, &block
end
field(config={}) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 48
def field config={}, &block 
  w = Field.new nil, config #, &block
  _position w
  if block
    w.bind(:CHANGED, &block)
  end
  return w
end
flow(config={}) click to toggle source

item_width - width to use per item

but the item width may apply to stacks inside not to items
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 221
def flow config={}, &block
  s = WsFlow.new config
  _configure s
  @_ws_active << s
  yield_or_eval &block if block_given?
  @_ws_active.pop 
  last = @_ws_active.last
  if last 
    case last
    when WsStack
      if s[:height]
        last[:row] += s[:height] 
      else
        #last[:row] += last[:highest_row]
        last[:row] += 1
      end
    when WsFlow
      last[:col] += last[:item_width] || 0 
    end
  end
end
label(config={}) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 56
def label config={}, &block 
  w = Label.new nil, config, &block
  _position w
  return w
end
line(config={}) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 64
def line config={}
  #horizontal line TODO
  #row = config[:row] || @app_row
  #width = config[:width] || 20
  #_position config
  #col = config[:col] || 1
  #@color_pair = config[:color_pair] || $datacolor
  #@attrib = config[:attrib] || Ncurses::A_NORMAL
  #@window.attron(Ncurses.COLOR_PAIR(@color_pair) | @attrib)
  #@window.mvwhline( row, col, FFI::NCurses::ACS_HLINE, width)
  #@window.attron(Ncurses.COLOR_PAIR(@color_pair) | @attrib)
end
listbox(config={}) click to toggle source
# File lib/canis/core/util/widgetshortcuts.rb, line 176
def listbox config={}, &block
  require 'canis/core/widgets/listbox'
  events = [ :PRESS, :ENTER_ROW, :LEAVE, :ENTER ]
  block_event = events[0]
  #_process_args args, config, block_event, events
  #config[:width] = config[:display_length] unless config.has_key? :width
  # if no width given, expand to flows width
  #config[:width] ||= @stack.last.width if @stack.last
  useform = nil
  #useform = @form if @current_object.empty?
  #w = List.new useform, config
  w = Listbox.new useform, config
  w.width = :expand unless w.width
  w.height ||= :expand # TODO We may need to push this before _position so it can be accounted for in stack
  _position(w)
  # need to expand to stack's width or flows itemwidth if given
  if block
    w.bind(block_event, &block)
  end
  return w
end
menubar(&block) click to toggle source
radio(config={}) click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 92
def radio config={}, &block
  a = config[:group]
  # should we not check for a nil
  if @variables.has_key? a
    v = @variables[a]
  else
    v = Variable.new
    @variables[a] = v
  end
  config[:variable] = v
  config.delete(:group)
  w = RadioButton.new nil, config #, &block
  _position w
  if block
    w.bind(:PRESS, &block)
  end
  return w
end
stack(config={}) click to toggle source

make it as simple as possible, don't try to be intelligent or clever, put as much on the user

# File lib/canis/core/util/oldwidgetshortcuts.rb, line 195
def stack config={}, &block
  s = WsStack.new config
  _configure s
  @_ws_active << s
  yield_or_eval &block if block_given?
  @_ws_active.pop 
  
  # ---- stack is finished now
  last = @_ws_active.last
  if last 
    case last
    when WsStack
    when WsFlow
      last[:col] += last[:item_width] || 0 
      # this tries to set height of outer flow based on highest row
      # printed, however that does not account for height of object,
      # so user should give a height to the flow.
      last[:height] = s[:row] if s[:row] > (last[:height]||0)
      $log.debug "XXX: STACK setting col to #{s[:col]} "
    end
  end

end
status_line(config={}) click to toggle source

prints a status line at bottom where mode's statuses et can be reflected

# File lib/canis/core/util/widgetshortcuts.rb, line 205
def status_line config={}, &block
  require 'canis/core/widgets/statusline'
  sl = Canis::StatusLine.new @form, config, &block
end
table(config={}) click to toggle source

creates a simple readonly table, that allows users to click on rows and also on the header. Header clicking is for column-sorting.

# File lib/canis/core/util/widgetshortcuts.rb, line 273
def table config={}, &block
#def tabular_widget config={}, &block
  require 'canis/core/widgets/table'
  events = [:PROPERTY_CHANGE, :LEAVE, :ENTER, :CHANGE, :ENTER_ROW, :PRESS ]
  block_event = nil
  # if no width given, expand to stack width
  #config.delete :title
  useform = nil

  w = Table.new useform, config # NO BLOCK GIVEN
  w.width ||= :expand 
  w.height ||= :expand # TODO This has to come before other in stack next one will overwrite.
  _position(w)
  if block_given?
    #@current_object << w
    yield_or_eval &block
    #@current_object.pop
  end
  return w
end
textarea(config={}) click to toggle source

editable text area

# File lib/canis/core/util/oldwidgetshortcuts.rb, line 111
def textarea config={}, &block
  require 'canis/rtextarea'
  # TODO confirm events many more
  events = [ :CHANGE,  :LEAVE, :ENTER ]
  block_event = events[0]
  #_process_args args, config, block_event, events
  #config[:width] = config[:display_length] unless config.has_key? :width
  # if no width given, expand to flows width
  #config[:width] ||= @stack.last.width if @stack.last
  useform = nil
  #useform = @form if @current_object.empty?
  w = TextArea.new useform, config
  w.width = :expand unless w.width
  w.height ||= 8 # TODO
  _position(w)
  # need to expand to stack's width or flows itemwidth if given
  if block
    w.bind(block_event, &block)
  end
  return w
end
textpad(config={}) click to toggle source
# File lib/canis/core/util/widgetshortcuts.rb, line 154
def textpad config={}, &block
  events = [ :LEAVE, :ENTER ]
  block_event = events[0]
  #_process_args args, config, block_event, events
  #config[:width] = config[:display_length] unless config.has_key? :width
  # if no width given, expand to flows width
  #config[:width] ||= @stack.last.width if @stack.last
  useform = nil
  #useform = @form if @current_object.empty?
  #w = TextView.new useform, config
  w = TextPad.new useform, config
  w.width = :expand unless w.width
  w.height ||= :expand # TODO This has to come before other in stack next one will overwrite.
  _position(w)
  # need to expand to stack's width or flows itemwidth if given
  if block
    w.bind(block_event, &block)
  end
  return w
end
Also aliased as: textview
textview(config={}) click to toggle source

deprecate and move textview soon TODO

# File lib/canis/core/util/oldwidgetshortcuts.rb, line 132
def textview config={}, &block
  events = [ :LEAVE, :ENTER ]
  block_event = events[0]
  #_process_args args, config, block_event, events
  #config[:width] = config[:display_length] unless config.has_key? :width
  # if no width given, expand to flows width
  #config[:width] ||= @stack.last.width if @stack.last
  useform = nil
  #useform = @form if @current_object.empty?
  w = TextView.new useform, config
  w.width = :expand unless w.width
  w.height ||= 8 # TODO
  _position(w)
  # need to expand to stack's width or flows itemwidth if given
  if block
    w.bind(block_event, &block)
  end
  return w
end
tree(config={}) click to toggle source
# File lib/canis/core/util/widgetshortcuts.rb, line 247
def tree config={}, &block
  require 'canis/core/widgets/tree'
  events = [:TREE_WILL_EXPAND_EVENT,
            :TREE_EXPANDED_EVENT,
            :TREE_SELECTION_EVENT,
            :PROPERTY_CHANGE,
            :LEAVE,
            :ENTER ,
            :ENTER_ROW,
            :TREE_COLLAPSED_EVENT,
            :TREE_WILL_EXPAND_EVENT] # EXPAND double?
  block_event = :TREE_WILL_EXPAND_EVENT
  #config[:height] ||= 10
  # if no width given, expand to flows width
  useform = nil
  #useform = @form if @current_object.empty?
  w = Tree.new useform, config, &block
  w.width ||= :expand 
  w.height ||= :expand # TODO This has to come before other in stack next one will overwrite.
  _position w
  # calling the block here was causing a problem since a tree may define root etc in the block
  # containers like to define elements in a block and not have an event called by default
  return w
end
widget_shortcuts_init() click to toggle source
# File lib/canis/core/util/oldwidgetshortcuts.rb, line 42
def widget_shortcuts_init
  @_ws_app_row = @_ws_app_col = 0
  @_ws_active = []
  @_ws_components = []
  @variables = {}
end