Class: RubyCurses::Label

Inherits:
Widget show all
Defined in:
lib/rbcurse/core/widgets/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. This can use text or text_variable for setting and getting data (inh from Widget). 2011-11-12 making it simpler, and single line only. The original multiline label

has moved to extras/multilinelabel.rb

Since:

  • 1.2.0

Instance Attribute Summary

Attributes inherited from Widget

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

Instance Method Summary collapse

Methods inherited from Widget

#action_manager, #changed, #click, #color_pair, #command, #destroy, #enter, #event_list, #focus, #get_preferred_size, #getvalue_for_paint, #handle_key, #height, #height=, #hide, #init_vars, #leave, #modified?, #move, #override_graphic, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rbgetstr, #warn

Methods included from Utils

#OLDdefine_key, #_process_key, #bind_key, #bind_keys, #clean_string!, #define_key, #define_prefix_command, #display_app_help, #get_attrib, #get_color, #keycode_tos, #last_line, #one_line_window, #parse_formatted_text, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #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, config = {}, &block) ⇒ Label

Returns a new instance of Label.

Since:

  • 1.2.0



2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2698

def initialize form, config={}, &block
  
  # this crap was used in position_label, find another way. where is it used ?
  #@row = config.fetch("row",-1)  # why on earth this monstrosity ? 2011-11-5 
  #@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

Since:

  • 1.2.0



2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2732

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

#getvalueObject

get the value for the label

Since:

  • 1.2.0



2715
2716
2717
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2715

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

#label_for(field) ⇒ Object

Since:

  • 1.2.0



2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2718

def label_for field
  @label_for = field
  #$log.debug " label for: #{@label_for}"
  if @form
    bind_hotkey 
  else
    @when_form ||= []
    @when_form << lambda { bind_hotkey }
  end
end

#on_enterObject

Added 2011-10-22 to prevent some naive components from putting focus here.

Since:

  • 1.2.0



2791
2792
2793
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2791

def on_enter
  raise "Cannot enter Label"
end

#on_leaveObject

Since:

  • 1.2.0



2794
2795
2796
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2794

def on_leave
  raise "Cannot leave Label"
end

#repaintObject

label’s repaint - I am removing wrapping and Array stuff and making it simple 2011-11-12

Since:

  • 1.2.0



2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2748

def repaint
  return unless @repaint_required
  raise "Label row or col nil #{@row} , #{@col}, #{@text} " if @row.nil? || @col.nil?
  r,c = rowcol

  @bgcolor ||= $def_bg_color
  @color   ||= $def_fg_color
  # value often nil so putting blank, but usually some application error
  value = getvalue_for_paint || ""

  if value.is_a? Array
    value = value.join " "
  end
  # ensure we do not exceed
  if @display_length
    if value.length > @display_length
      value = value[0..@display_length-1]
    end
  end
  len = @display_length || value.length
  #acolor = get_color $datacolor
  # the user could have set color_pair, use that, else determine color
  # This implies that if he sets cp, then changing col and bg won't have an effect !
  # A general routine that only changes color will not work here.
  acolor = @color_pair || get_color($datacolor, @color, @bgcolor)
  #$log.debug "label :#{@text}, #{value}, r #{r}, c #{c} col= #{@color}, #{@bgcolor} acolor  #{acolor} j:#{@justify} dlL: #{@display_length} "
  str = @justify.to_sym == :right ? "%*s" : "%-*s"  # added 2008-12-22 19:05 

  @graphic ||= @form.window
  # clear the area
  @graphic.printstring r, c, " " * len , acolor, @attr
  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
  if @mnemonic
    ulindex = value.index(@mnemonic) || value.index(@mnemonic.swapcase)
    @graphic.mvchgat(y=r, x=c+ulindex, max=1, Ncurses::A_BOLD|Ncurses::A_UNDERLINE, acolor, nil)
  end
  @repaint_required = false
end