Class: RubyCurses::Label

Inherits:
Widget
  • Object
show all
Defined in:
lib/rbcurse/rwidget.rb

Overview

the preferred way of printing text on screen, esp if you want to modify it at run time. Use display_length to ensure no spillage.

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

#col_offset, #cols_panned, #config, #curpos, #ext_col_offset, #ext_row_offset, #form, #id, #parent_component, #row_offset, #rows_panned, #should_create_buffer, #state

Instance Method Summary collapse

Methods inherited from Widget

#OLDbind_key, #buffer_to_screen, #buffer_to_window, #create_buffer, #destroy, #destroy_buffer, #focus, #get_buffer, #get_color, #get_preferred_size, #getvalue_for_paint, #handle_key, #height, #height=, #hide, #init_vars, #is_double_buffered?, #modified?, #move, #on_enter, #on_leave, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #safe_create_buffer, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #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

Methods included from Utils

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

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Methods included from DSL

#OLD_method_missing

Constructor Details

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

Returns a new instance of Label.



2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
# File 'lib/rbcurse/rwidget.rb', line 2128

def initialize form, config={}, &block
  
  @row = config.fetch("row",-1) 
  @col = config.fetch("col",-1) 
  @bgcolor = config.fetch("bgcolor", $def_bg_color)
  @color = config.fetch("color", $def_fg_color)
  @text = config.fetch("text", "NOTFOUND")
  @editable = false
  @focusable = false
  super
  @justify ||= :left
  @name ||= @text
  @repaint_required = true
end

Instance Method Details

#bind_hotkeyObject

for a button, fire it when label invoked without changing focus for other widgets, attempt to change focus to that field



2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
# File 'lib/rbcurse/rwidget.rb', line 2154

def bind_hotkey
  if !@mnemonic.nil?
    ch = @mnemonic.downcase()[0].ord   ## FIXME 1.9 DONE 
    # meta key 
    mch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0))  ## FIXME 1.9
    if @label_for.is_a? RubyCurses::Button and @label_for.respond_to? :fire
      @form.bind_key(mch, @label_for) { |_form, _butt| _butt.fire }
    else
      $log.debug " bind_hotkey label for: #{@label_for}"
      @form.bind_key(mch, @label_for) { |_form, _field| _field.focus }
    end
  end
end

#getvalueObject



2142
2143
2144
# File 'lib/rbcurse/rwidget.rb', line 2142

def getvalue
  @text_variable && @text_variable.value || @text
end

#label_for(field) ⇒ Object



2145
2146
2147
2148
2149
# File 'lib/rbcurse/rwidget.rb', line 2145

def label_for field
  @label_for = field
  #$log.debug " label for: #{@label_for}"
  bind_hotkey unless @form.nil?   # GRRR!
end

#repaintObject

XXX need to move wrapping etc up and done once.



2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
# File 'lib/rbcurse/rwidget.rb', line 2170

def repaint
  return unless @repaint_required
    r,c = rowcol
    value = getvalue_for_paint
    lablist = []
    if @height && @height > 1
      lablist = wrap_text(value, @display_length).split("\n")
    else
      # ensure we do not exceed
      if !@display_length.nil?
        if value.length > @display_length
          value = value[0..@display_length-1]
        end
      end
      lablist << value
    end
    len = @display_length || value.length
    acolor = get_color $datacolor
    #$log.debug "label :#{@text}, #{value}, #{r}, #{c} col= #{@color}, #{@bgcolor} acolor  #{acolor} j:#{@justify} dlL: #{@display_length} "
    firstrow = r
    _height = @height || 1
    str = @justify.to_sym == :right ? "%*s" : "%-*s"  # added 2008-12-22 19:05 
    # loop added for labels that are wrapped.
    # TODO clear separately since value can change in status like labels
    $log.debug " RWID 1595 #{self.class} value: #{value} form:  #{form} "
    @graphic = @form.window if @graphic.nil? ## HACK messagebox givig this in repaint, 423 not working ??
    0.upto(_height-1) { |i| 
      @graphic.printstring r+i, c, " " * len , acolor,@attr
    }
    lablist.each_with_index do |_value, ix|
      break if ix >= _height
      if @justify.to_sym == :center
        padding = (@display_length - _value.length)/2
        _value = " "*padding + _value + " "*padding # so its cleared if we change it midway
      end
      @graphic.printstring r, c, str % [len, _value], acolor,@attr
      r += 1
    end
    if !@mnemonic.nil?
      ulindex = value.index(@mnemonic) || value.index(@mnemonic.swapcase)
      @graphic.mvchgat(y=firstrow, x=c+ulindex, max=1, Ncurses::A_BOLD|Ncurses::A_UNDERLINE, acolor, nil)
    end
    #@form.window.mvchgat(y=r, x=c, max=len, Ncurses::A_NORMAL, color, nil)
    @repaint_required = false
end