Class: CDK::BUTTON

Inherits:
CDKOBJS show all
Defined in:
lib/cdk/scroll.rb

Instance Attribute Summary

Attributes inherited from CDKOBJS

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #accepts_focus, #binding_list, #border_size, #box, #exit_type, #has_focus, #is_visible, #screen, #screen_index

Instance Method Summary collapse

Methods inherited from CDKOBJS

#SCREEN_XPOS, #SCREEN_YPOS, #bind, #bindableObject, #checkBind, #cleanBindings, #cleanTitle, #drawTitle, #getBox, #getc, #getch, #isBind, #move_specific, #refreshData, #saveData, #setBXattr, #setBackgroundColor, #setBox, #setExitType, #setHZchar, #setLLchar, #setLRchar, #setPostProcess, #setPreProcess, #setTitle, #setULchar, #setURchar, #setVTchar, #unbind, #validCDKObject, #validObjType

Constructor Details

#initialize(cdkscreen, xplace, yplace, text, callback, box, shadow) ⇒ BUTTON

Returns a new instance of BUTTON.



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/cdk/scroll.rb', line 629

def initialize(cdkscreen, xplace, yplace, text, callback, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = 0
  xpos = xplace
  ypos = yplace

  self.setBox(box)
  box_height = 1 + 2 * @border_size

  # Translate the string to a chtype array.
  info_len = []
  info_pos = []
  @info = CDK.char2Chtype(text, info_len, info_pos)
  @info_len = info_len[0]
  @info_pos = info_pos[0]
  box_width = [box_width, @info_len].max + 2 * @border_size

  # Create the string alignments.
  @info_pos = CDK.justifyString(box_width - 2 * @border_size,
      @info_len, @info_pos)

  # Make sure we didn't extend beyond the dimensions of the window.
  box_width = if box_width > parent_width
              then parent_width
              else box_width
              end
  box_height = if box_height > parent_height
               then parent_height
               else box_height
               end

  # Rejustify the x and y positions if we need to.
  xtmp = [xpos]
  ytmp = [ypos]
  CDK.alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Create the button.
  @screen = cdkscreen
  # ObjOf (button)->fn = &my_funcs;
  @parent = cdkscreen.window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)
  @shadow_win = nil
  @xpos = xpos
  @ypos = ypos
  @box_width = box_width
  @box_height = box_height
  @callback = callback
  @input_window = @win
  @accepts_focus = true
  @shadow = shadow

  if @win.nil?
    self.destroy
    return nil
  end

  @win.keypad(true)

  # If a shadow was requested, then create the shadow window.
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

  # Register this baby.
  cdkscreen.register(:BUTTON, self)
end

Instance Method Details

#activate(actions) ⇒ Object

This was added for the builder.



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/cdk/scroll.rb', line 702

def activate(actions)
  self.draw(@box)
  ret = -1

  if actions.nil? || actions.size == 0
    while true
      input = self.getch([])

      # Inject the character into the widget.
      ret = self.inject(input)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  else
    # Inject each character one at a time.
    actions.each do |x|
      ret = self.inject(action)
      if @exit_type == :EARLY_EXIT
        return ret
      end
    end
  end

  # Set the exit type and exit
  self.setExitType(0)
  return -1
end

#destroyObject

This destroys the button object pointer.



930
931
932
933
934
935
936
937
# File 'lib/cdk/scroll.rb', line 930

def destroy
  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

  self.cleanBindings(:BUTTON)

  CDK::SCREEN.unregister(:BUTTON, self)
end

#draw(box) ⇒ Object

This draws the button widget



782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/cdk/scroll.rb', line 782

def draw(box)
  # Is there a shadow?
  unless @shadow_win.nil?
    Draw.drawShadow(@shadow_win)
  end

  # Box the widget if asked.
  if @box
    Draw.drawObjBox(@win, self)
  end
  self.drawText
  @win.wrefresh
end

#drawTextObject



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/cdk/scroll.rb', line 760

def drawText
  box_width = @box_width

  # Draw in the message.
  (0...(box_width - 2 * @border_size)).each do |i|
    pos = @info_pos
    len = @info_len
    if i >= pos && (i - pos) < len
      c = @info[i - pos]
    else
      c = ' '
    end

    if @has_focus
      c = Ncurses::A_REVERSE | c
    end

    @win.mvwaddch(@border_size, i + @border_size, c)
  end
end

#eraseObject

This erases the button widget.



797
798
799
800
801
802
# File 'lib/cdk/scroll.rb', line 797

def erase
  if self.validCDKObject
    CDK.eraseCursesWindow(@win)
    CDK.eraseCursesWindow(@shadow_win)
  end
end

#focusObject



980
981
982
983
# File 'lib/cdk/scroll.rb', line 980

def focus
  self.drawText
  @win.wrefresh
end

#getMessageObject



751
752
753
# File 'lib/cdk/scroll.rb', line 751

def getMessage
  return @info
end

#inject(input) ⇒ Object

This injects a single character into the widget.



940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
# File 'lib/cdk/scroll.rb', line 940

def inject(input)
  ret = -1
  complete = false

  self.setExitType(0)

  # Check a predefined binding.
  if self.checkBind(:BUTTON, input)
    complete = true
  else
    case input
    when CDK::KEY_ESC
      self.setExitType(input)
      complete = true
    when Ncurses::ERR
      self.setExitType(input)
      complete = true
    when ' '.ord, CDK::KEY_RETURN, Ncurses::KEY_ENTER
      unless @callback.nil?
        @callback.call(self)
      end
      self.setExitType(Ncurses::KEY_ENTER)
      ret = 0
      complete = true
    when CDK::REFRESH
      @screen.erase
      @screen.refresh
    else
      CDK.Beep
    end
  end

  unless complete
    self.setExitType(0)
  end

  @result_data = ret
  return ret
end

#move(xplace, yplace, relative, refresh_flag) ⇒ Object

This moves the button field to the given location.



805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/cdk/scroll.rb', line 805

def move(xplace, yplace, relative, refresh_flag)
  current_x = @win.getbegx
  current_y = @win.getbegy
  xpos = xplace
  ypos = yplace

  # If this is a relative move, then we will adjust where we want
  # to move to.
  if relative
    xpos = @win.getbegx + xplace
    ypos = @win.getbegy + yplace
  end

  # Adjust the window if we need to.
  xtmp = [xpos]
  ytmp = [ypos]
  CDK.alignxy(@screen.window, xtmp, ytmp, @box_width, @box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Get the difference
  xdiff = current_x - xpos
  ydiff = current_y - ypos

  # Move the window to the new location.
  CDK.moveCursesWindow(@win, -xdiff, -ydiff)
  CDK.moveCursesWindow(@shadow_win, -xdiff, -ydiff)

  # Thouch the windows so they 'move'.
  CDK::SCREEN.refreshCDKWindow(@screen.window)

  # Redraw the window, if they asked for it.
  if refresh_flag
    self.draw(@box)
  end
end

#object_typeObject



990
991
992
# File 'lib/cdk/scroll.rb', line 990

def object_type
  :BUTTON
end

#positionObject

This allows the user to use the cursor keys to adjust the position of the widget.



844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/cdk/scroll.rb', line 844

def position
  # Declare some variables
  orig_x = @win.getbegx
  orig_y = @win.getbegy
  key = 0

  # Let them move the widget around until they hit return
  while key != Ncurses::KEY_ENTER && key != CDK::KEY_RETURN
    key = self.getch([])
    if key == Ncurses::KEY_UP || key == '8'.ord
      if @win.getbegy > 0
        self.move(0, -1, true, true)
      else
        CDK.Beep
      end
    elsif key == Ncurses::KEY_DOWN || key == '2'.ord
      if @win.getbegy + @win.getmaxy < @screen.window.getmaxy - 1
        self.move(0, 1, true, true)
      else
        CDK.Beep
      end
    elsif key == Ncurses::KEY_LEFT || key == '4'.ord
      if @win.getbegx > 0
        self.move(-1, 0, true, true)
      else
        CDK.Beep
      end
    elsif key == Ncurses::KEY_RIGHT || key == '6'.ord
      if @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1
        self.move(1, 0, true, true)
      else
        CDK.Beep
      end
    elsif key == '7'.ord
      if @win.getbegy > 0 && @win.getbegx > 0
        self.move(-1, -1, true, true)
      else
        CDK.Beep
      end
    elsif key == '9'.ord
      if @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1 &&
          @win.getbegy > 0
        self.move(1, -1, true, true)
      else
        CDK.Beep
      end
    elsif key == '1'.ord
      if @win.getbegx > 0 &&
          @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1
        self.move(-1, 1, true, true)
      else
        CDK.Beep
      end
    elsif key == '3'.ord
      if @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1 &&
          @win.getbegy + @win.getmaxy < @screen.window.getmaxy - 1
        self.move(1, 1, true, true)
      else
        CDK.Beep
      end
    elsif key == '5'.ord
      self.move(CDK::CENTER, CDK::CENTER, false, true)
    elsif key == 't'.ord
      self.move(@win.getbegx, CDK::TOP, false, true)
    elsif key == 'b'.ord
      self.move(@win.getbegx, CDK::BOTTOM, false, true)
    elsif key == 'l'.ord
      self.move(CDK::LEFT, @win.getbegy, false, true)
    elsif key == 'r'
      self.move(CDK::RIGHT, @win.getbegy, false, true)
    elsif key == 'c'
      self.move(CDK::CENTER, @win.getbegy, false, true)
    elsif key == 'C'
      self.move(@win.getbegx, CDK::CENTER, false, true)
    elsif key == CDK::REFRESH
      @screen.erase
      @screen.refresh
    elsif key == CDK::KEY_ESC
      self.move(orig_x, orig_y, false, true)
    elsif key != CDK::KEY_RETURN && key != Ncurses::KEY_ENTER
      CDK.Beep
    end
  end
end

#set(mesg, box) ⇒ Object

This sets multiple attributes of the widget.



732
733
734
735
# File 'lib/cdk/scroll.rb', line 732

def set(mesg, box)
  self.setMessage(mesg)
  self.setBox(box)
end

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



756
757
758
# File 'lib/cdk/scroll.rb', line 756

def setBKattr(attrib)
  @win.wbkgd(attrib)
end

#setMessage(info) ⇒ Object

This sets the information within the button.



738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/cdk/scroll.rb', line 738

def setMessage(info)
  info_len = []
  info_pos = []
  @info = CDK.char2Chtype(info, info_len, info_pos)
  @info_len = info_len[0]
  @info_pos = CDK.justifyString(@box_width - 2 * @border_size,
      info_pos[0])

  # Redraw the button widget.
  self.erase
  self.draw(box)
end

#unfocusObject



985
986
987
988
# File 'lib/cdk/scroll.rb', line 985

def unfocus
  self.drawText
  @win.wrefresh
end