Class: RubyCurses::Widget

Inherits:
Object show all
Includes:
Io, ConfigSetup, EventHandler, Utils
Defined in:
lib/rbcurse/app.rb,
lib/rbcurse/rwidget.rb

Overview

Basic widget class. NOTE: I may soon remove the config hash. I don’t use it and its just making things heavy. Unless someone convinces me otherwise.

Constant Summary

Constants included from Io

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, aconfig = {}, &block) ⇒ Widget

Returns a new instance of Widget.



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/rbcurse/rwidget.rb', line 560

def initialize form, aconfig={}, &block
  @form = form
  @row_offset ||= 0
  @col_offset ||= 0
  #@ext_row_offset = @ext_col_offset = 0 # 2010-02-07 20:18  # removed on 2011-09-29 
  @state = :NORMAL
  @attr = nil

  @handler = nil # we can avoid firing if nil
  @event_args = {}
  # These are standard events for most widgets which will be fired by 
  # Form. In the case of CHANGED, form fires if it's editable property is set, so
  # it does not apply to all widgets.
  @_events ||= []
  @_events.push( *[:ENTER, :LEAVE, :CHANGED, :PROPERTY_CHANGE])

  config_setup aconfig # @config.each_pair { |k,v| variable_set(k,v) }
  #instance_eval &block if block_given?
  if block_given?
    if block.arity > 0
      yield self
    else
      self.instance_eval(&block)
    end
  end
  # 2010-09-20 13:12 moved down, so it does not create problems with other who want to set their
  # own default
  @bgcolor ||=  "black" # 0
  @color ||= "white" # $datacolor
  #    @id = form.add_widget(self) if !form.nil? and form.respond_to? :add_widget
  set_form(form) unless form.nil? 
end

Instance Attribute Details

#_object_createdObject

2010-09-16 12:12 to prevent needless property change firing when object being set



542
543
544
# File 'lib/rbcurse/rwidget.rb', line 542

def _object_created
  @_object_created
end

#col_offsetObject (readonly)

where should the cursor be placed to start with



532
533
534
# File 'lib/rbcurse/rwidget.rb', line 532

def col_offset
  @col_offset
end

#cols_pannedObject

moved from form, how many cols scrolled.panned 2010-02-11 15:26



553
554
555
# File 'lib/rbcurse/rwidget.rb', line 553

def cols_panned
  @cols_panned
end

#configObject (readonly)

can be used for popping user objects too



529
530
531
# File 'lib/rbcurse/rwidget.rb', line 529

def config
  @config
end

#curposObject

cursor position inside object - column, not row.



528
529
530
# File 'lib/rbcurse/rwidget.rb', line 528

def curpos
  @curpos
end

#focussedObject

sometimes inside a container there’s no way of knowing if an individual comp is in focus other than the explicitly set it and inquire . 2010-09-02 14:47 @since 1.1.5 NOTE state takes care of this and is set by form



558
559
560
# File 'lib/rbcurse/rwidget.rb', line 558

def focussed
  @focussed
end

#formObject

made accessor 2008-11-27 22:32 so menu can set



530
531
532
# File 'lib/rbcurse/rwidget.rb', line 530

def form
  @form
end

#idObject

, :zorder



527
528
529
# File 'lib/rbcurse/rwidget.rb', line 527

def id
  @id
end

#parent_componentObject

attr_accessor :frozen # true false attr_accessor :frozen_list # list of attribs that cannot be changed I think parent_form was not a good idea since i can’t add parent widget offsets + thus we should use parent_comp and push up.



548
549
550
# File 'lib/rbcurse/rwidget.rb', line 548

def parent_component
  @parent_component
end

#row_offsetObject (readonly)

where should the cursor be placed to start with



532
533
534
# File 'lib/rbcurse/rwidget.rb', line 532

def row_offset
  @row_offset
end

#rows_pannedObject

tired of getting the cursor wrong and guessing, i am now going to try to get absolute coordinates - 2010-02-07 20:17 this should be updated by parent. attr_accessor :ext_col_offset, :ext_row_offset # 2010-02-07 20:16 to get abs position for cursor rem 2011-09-29



552
553
554
# File 'lib/rbcurse/rwidget.rb', line 552

def rows_panned
  @rows_panned
end

#stateObject

normal, selected, highlighted



531
532
533
# File 'lib/rbcurse/rwidget.rb', line 531

def state
  @state
end

Instance Method Details

#changed(*args, &block) ⇒ Object

Since:

  • 1.2.0



48
49
50
# File 'lib/rbcurse/app.rb', line 48

def changed *args, &block
  bind :CHANGED, *args, &block
end

#click(*args, &block) ⇒ Object

actually we already have command() for buttons

Since:

  • 1.2.0



58
59
60
# File 'lib/rbcurse/app.rb', line 58

def click *args, &block
  bind :PRESS, *args, &block
end

#destroyObject



670
671
672
673
674
675
# File 'lib/rbcurse/rwidget.rb', line 670

def destroy
  $log.debug "DESTROY : widget #{@name} "
  panel = @window.panel
  Ncurses::Panel.del_panel(panel.pointer) if !panel.nil?   
  @window.delwin if !@window.nil?
end

#enter(*args, &block) ⇒ Object

Since:

  • 1.2.0



54
55
56
# File 'lib/rbcurse/app.rb', line 54

def enter *args, &block
  bind :ENTER, *args, &block
end

#event_listObject



928
929
930
931
# File 'lib/rbcurse/rwidget.rb', line 928

def event_list
  return @@events if defined? @@events
  nil
end

#focusObject

moves focus to this field we must look into running on_leave of previous field



751
752
753
754
755
756
# File 'lib/rbcurse/rwidget.rb', line 751

def focus
  return if !@focusable
  if @form.validate_field != -1
    @form.select_field @id
  end
end

#get_preferred_sizeObject

Since:

  • 0.1.3



779
780
781
# File 'lib/rbcurse/rwidget.rb', line 779

def get_preferred_size
  return @preferred_height, @preferred_width
end

#getvalueObject

return the value of the widget.

In cases where selection is possible, should return selected value/s


647
648
649
# File 'lib/rbcurse/rwidget.rb', line 647

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

#getvalue_for_paintObject

Am making a separate method since often value for print differs from actual value



652
653
654
# File 'lib/rbcurse/rwidget.rb', line 652

def getvalue_for_paint
  getvalue
end

#handle_key(ch) ⇒ Object

to be added at end of handle_key of widgets so instlalled actions can be checked



773
774
775
776
777
# File 'lib/rbcurse/rwidget.rb', line 773

def handle_key(ch)
  ret = process_key ch, self
  return :UNHANDLED if ret == :UNHANDLED
  0
end

#height(*val) ⇒ val

getter and setter for height - 2009-10-30 12:25 Using dsl_property style SO WE’ve finally succumbed and added height to widget

Parameters:

  • height (val, nil)

    to set

Returns:

  • (val)

    earlier height if nil param

Since:

  • 0.1.3



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/rbcurse/rwidget.rb', line 833

def height(*val)
  #$log.debug " inside  height() #{val[0]}"
  if val.empty?
    return @height
  else
    #$log.debug " inside #{@name} height()"
    oldvalue = @height || 0 # is this default okay, else later nil cries
    @height = val.size == 1 ? val[0] : val
    newvalue = @height
    @config[:height]=@height
    if oldvalue != newvalue
      fire_property_change(:height, oldvalue, newvalue)
      $log.debug " widget #{@name} setting repaint_all to true"
      @repaint_all=true
    end
  end
end

#height=(val) ⇒ Object



850
851
852
# File 'lib/rbcurse/rwidget.rb', line 850

def height=val
  height(val)
end

#hideObject



734
735
736
# File 'lib/rbcurse/rwidget.rb', line 734

def hide
  @visible = false
end

#init_varsObject



592
593
594
595
596
597
# File 'lib/rbcurse/rwidget.rb', line 592

def init_vars
  # just in case anyone does a super. Not putting anything here
  # since i don't want anyone accidentally overriding
  @buffer_modified = false 
  #@manages_cursor = false # form should manage it, I will pass row and col to it. 
end

#leave(*args, &block) ⇒ Object

Since:

  • 1.2.0



51
52
53
# File 'lib/rbcurse/app.rb', line 51

def leave *args, &block
  bind :LEAVE, *args, &block
end

#modified?Boolean

modified

typically read will be overridden to check if value changed from what it was on enter. getter and setter for modified (added 2009-01-18 12:31 )

Returns:

  • (Boolean)


603
604
605
# File 'lib/rbcurse/rwidget.rb', line 603

def modified?
  @modified
end

#move(row, col) ⇒ Object

is this required can we remove



744
745
746
747
# File 'lib/rbcurse/rwidget.rb', line 744

def move row, col
  @row = row
  @col = col
end

#on_enterObject

got left out by mistake 2008-11-26 20:20



624
625
626
627
628
629
# File 'lib/rbcurse/rwidget.rb', line 624

def on_enter
  @focussed = true
  if @handler && @handler.has_key?(:ENTER)
    fire_handler :ENTER, self
  end
end

#on_leaveObject

got left out by mistake 2008-11-26 20:20



631
632
633
634
635
636
# File 'lib/rbcurse/rwidget.rb', line 631

def on_leave
  @focussed = false
  if @handler && @handler.has_key?(:LEAVE)
    fire_handler :LEAVE, self
  end
end

#override_graphic(gr) ⇒ Object

When an enclosing component creates a pad (buffer) and the child component + should write onto the same pad, then the enclosing component should override + the default graphic of child. This applies mainly to editor components in + listboxes and tables. added 2010-01-05 15:25

Parameters:

  • graphic

    graphic object to use for writing contents

See Also:

  • in rlistbox.


871
872
873
# File 'lib/rbcurse/rwidget.rb', line 871

def override_graphic gr
  @graphic = gr
end

#printstring(win, r, c, string, color, att = Ncurses::A_NORMAL) ⇒ Object

Deprecated.

pls call windows method



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/rbcurse/rwidget.rb', line 677

def printstring(win, r,c,string, color, att = Ncurses::A_NORMAL)

  att = Ncurses::A_NORMAL if att.nil?
  case att.to_s.downcase
  when 'underline'
    att = Ncurses::A_UNDERLINE
    $log.debug "UL att #{att}"
  when 'bold'
    att = Ncurses::A_BOLD
  when 'blink'
    att = Ncurses::A_BLINK
  when 'reverse'
    att = Ncurses::A_REVERSE
  else
    att = Ncurses::A_NORMAL
  end
  #$log.debug "att #{att}"

  #att = bold ? Ncurses::A_BLINK|Ncurses::A_BOLD : Ncurses::A_NORMAL
  #     att = bold ? Ncurses::A_BOLD : Ncurses::A_NORMAL
  win.attron(Ncurses.COLOR_PAIR(color) | att)
  win.mvprintw(r, c, "%s", string);
  win.attroff(Ncurses.COLOR_PAIR(color) | att)
end

#process_key(keycode, object) ⇒ Object

e.g. process_key ch, self returns UNHANDLED if no block for it after form handles basic keys, it gives unhandled key to current field, if current field returns unhandled, then it checks this map.



768
769
770
# File 'lib/rbcurse/rwidget.rb', line 768

def process_key keycode, object
  return _process_key keycode, object, @graphic
end

#removeObject



740
741
742
# File 'lib/rbcurse/rwidget.rb', line 740

def remove
  @form.remove_widget(self)
end

#repaintObject

default repaint method. Called by form for all widgets.

widget does not have display_length.


658
659
660
661
662
663
664
665
666
667
668
# File 'lib/rbcurse/rwidget.rb', line 658

def repaint
    r,c = rowcol
    $log.debug("widget repaint : r:#{r} c:#{c} col:#{@color}" )
    value = getvalue_for_paint
    len = @display_length || value.length
    acolor = @color_pair || get_color($datacolor, @color, @bgcolor)
    @graphic.printstring r, c, "%-*s" % [len, value], acolor, @attr
    # next line should be in same color but only have @att so we can change att is nec
    #@form.window.mvchgat(y=r, x=c, max=len, Ncurses::A_NORMAL, @bgcolor, nil)
    #@buffer_modified = true # required for form to call buffer_to_screen CLEANUP
end

#repaint_all(tf = true) ⇒ Object



858
859
860
861
# File 'lib/rbcurse/rwidget.rb', line 858

def repaint_all(tf=true)
  @repaint_all = tf
  @repaint_required = tf
end

#repaint_required(tf = true) ⇒ Object

to give simple access to other components, (eg, parent) to tell a comp to either paint its data, or to paint all - borders, headers, footers due to a big change (ht/width)



855
856
857
# File 'lib/rbcurse/rwidget.rb', line 855

def repaint_required(tf=true)
  @repaint_required = tf
end

#rowcolObject

row and col is where a widget starts. offsets usually take into account borders. the offsets typically are where the cursor should be positioned inside, upon on_enter.

Returns:

  • row and col of a widget where painting data actually starts



641
642
643
644
# File 'lib/rbcurse/rwidget.rb', line 641

def rowcol
# $log.debug "widgte rowcol : #{@row+@row_offset}, #{@col+@col_offset}"
  return @row+@row_offset, @col+@col_offset
end

#set_buffer_modified(tf = true) ⇒ Object

Inform the system that the buffer has been modified and should be blitted over the screen or copied to parent.



787
788
789
# File 'lib/rbcurse/rwidget.rb', line 787

def set_buffer_modified(tf=true)
  @buffer_modified = tf
end

#set_buffering(params) ⇒ Object

I was removing this altogether but vimsplit needs this, or masterdetail gives form and window to vimsplit. So i ‘ve removed everything but the form and window setting. 2011-09-29 SETBUFF move from TextView parameters relating to buffering - new 2010-02-12 12:09 RFED16 I am merging so i can call multiple times WARNING NOTE : this does not set Pad’s top and left since Pad may not be created yet, or at all



919
920
921
922
923
924
925
926
# File 'lib/rbcurse/rwidget.rb', line 919

def set_buffering params

  @target_window ||= params[:target_window]
  @form = params[:form] unless @form
  if @graphic.nil? 
    @graphic = @target_window
  end
end

#set_form(form) ⇒ Object

in those rare cases where we create widget without a form, and later give it to some other program which sets the form. Dirty, we should perhaps create widgets without forms, and add explicitly.



704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/rbcurse/rwidget.rb', line 704

def set_form form
  raise "Form is nil in set_form" if form.nil?
  @form = form
  @id = form.add_widget(self) if !form.nil? and form.respond_to? :add_widget
  # 2009-10-29 15:04 use form.window, unless buffer created
  # should not use form.window so explicitly everywhere.
  # added 2009-12-27 20:05 BUFFERED in case child object needs a form.
  # We don;t wish to overwrite the graphic object
  if @graphic.nil?
    $log.debug " setting graphic to form window for #{self.class}, #{form} "
    @graphic = form.window unless form.nil? # use screen for writing, not buffer
  end
end

#set_form_col(col1 = @curpos) ⇒ Object

set cursor on correct column, widget Ideally, this should be overriden, as it is not likely to be correct.



727
728
729
730
731
732
733
# File 'lib/rbcurse/rwidget.rb', line 727

def set_form_col col1=@curpos
  @curpos = col1 || 0 # 2010-01-14 21:02 
  #@form.col = @col + @col_offset + @curpos
  c = @col + @col_offset + @curpos
  $log.warn " #{@name} empty set_form_col #{c}, #{@form} "
  setrowcol nil, c
end

#set_form_rowObject

puts cursor on correct row.



718
719
720
721
722
723
724
# File 'lib/rbcurse/rwidget.rb', line 718

def set_form_row
#  @form.row = @row + 1 + @winrow
  #@form.row = @row + 1 
  r, c = rowcol
  $log.warn " empty set_form_row in widget #{self} r = #{r} , c = #{c}  "
  setrowcol row, nil
end

#set_modified(tf = true) ⇒ Object Also known as: modified



606
607
608
609
# File 'lib/rbcurse/rwidget.rb', line 606

def set_modified tf=true
  @modified = tf
  @form.modified = true if tf
end

#setformrowcol(r, c) ⇒ Object

passing a cursor up and adding col and row offsets Added 2010-01-13 13:27 I am checking this out. I would rather pass the value down and store it than do this recursive call + for each cursor display

See Also:



880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/rbcurse/rwidget.rb', line 880

def setformrowcol r, c
      @form.row = r unless r.nil?
      @form.col = c unless c.nil?
      # this is stupid, going through this route i was losing windows top and left
      # And this could get repeated if there are mult objects. 
   if !@parent_component.nil? and @parent_component != self
      r+= @parent_component.form.window.top unless  r.nil?
      c+= @parent_component.form.window.left unless c.nil?
      $log.debug " (#{@name}) calling parents setformrowcol #{r}, #{c} pa: #{@parent_component.name} self: #{name}, #{self.class}, poff #{@parent_component.row_offset}, #{@parent_component.col_offset}, top:#{@form.window.left} left:#{@form.window.left} "
      @parent_component.setformrowcol r, c
   else
      # no more parents, now set form
      $log.debug " name NO MORE parents setting #{r}, #{c}    in #{@form} "
      @form.setrowcol r, c
   end
end

#setrowcol(r, c) ⇒ Object

widget: i am putting one extra level of indirection so i can switch here between form#setrowcol and setformrowcol, since i am not convinced either are giving the accurate result. i am not sure what the issue is.



899
900
901
902
903
904
905
906
907
908
909
910
911
# File 'lib/rbcurse/rwidget.rb', line 899

def setrowcol r, c
    # 2010-02-07 21:32 is this where i should add ext_offsets
   #$log.debug " #{@name}  w.setrowcol #{r} + #{@ext_row_offset}, #{c} + #{@ext_col_offset}  "
   # commented off 2010-02-15 18:22 
   #r += @ext_row_offset unless r.nil?
   #c += @ext_col_offset unless c.nil?
   if @form
     @form.setrowcol r, c
   else
     @parent_component.setrowcol r, c
   end
   #setformrowcol r,c 
end

#showObject



737
738
739
# File 'lib/rbcurse/rwidget.rb', line 737

def show
  @visible = true
end

#text_variable(*val) ⇒ Object

getter and setter for text_variable



613
614
615
616
617
618
619
620
621
# File 'lib/rbcurse/rwidget.rb', line 613

def text_variable(*val)
  if val.empty?
    @text_variable
  else
    @text_variable = val[0] 
    $log.debug " GOING TO CALL ADD DELPENDENT #{self}"
    @text_variable.add_dependent(self)
  end
end

#unbind_key(keycode) ⇒ Object

remove a binding that you don’t want



759
760
761
762
# File 'lib/rbcurse/rwidget.rb', line 759

def unbind_key keycode
  @key_args.delete keycode unless @key_args.nil?
  @key_handler.delete keycode unless @key_handler.nil?
end

#width(*val) ⇒ val

getter and setter for width - 2009-10-29 22:45 Using dsl_property style

Parameters:

  • value (val, nil)

    to set

Returns:

  • (val)

    earlier value if nil param

Since:

  • 0.1.3



801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/rbcurse/rwidget.rb', line 801

def width(*val)
  #$log.debug " inside  width() #{val}"
  if val.empty?
    return @width
  else
    #$log.debug " inside  width()"
    oldvalue = @width || 0 # is this default okay, else later nil cries
    #@width = val.size == 1 ? val[0] : val
    @width = val[0]
    newvalue = @width
    @config["width"]=@width
    if oldvalue != newvalue
      fire_property_change(:width, oldvalue, newvalue)
      repaint_all(true)  # added 2010-01-08 18:51 so widgets can redraw everything.
    end
    #if is_double_buffered? and newvalue != oldvalue # removed on 2011-09-29 
      #$log.debug " #{@name} w calling resize of screen buffer with #{newvalue}. WARNING: does not change buffering_params"
      #@screen_buffer.resize(0, newvalue)
    #end
  end
end

#width=(val) ⇒ Object



822
823
824
# File 'lib/rbcurse/rwidget.rb', line 822

def width=val
  width(val)
end