Class: TKXXS_CLASSES::MultiChoiceD

Inherits:
TkListbox
  • Object
show all
Includes:
Tk::Tile
Defined in:
lib/tkxxs/tkxxs_classes.rb

Overview

MultiChoiceD has no search box like SingleChoiceD, up to now.

Constant Summary collapse

CONF =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aryWithChoices, help = nil, hash = nil) ⇒ MultiChoiceD

See: TKXXS::multi_choice



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/tkxxs/tkxxs_classes.rb', line 684

def initialize( aryWithChoices, help=nil, hash=nil  )
  aryWithChoices, help, hash = TKXXS_CLASSES.args_1(aryWithChoices, help, hash)
  hash = {
    :selectmode => :multiple,
    :question => "Choose multiple:",
    :help => nil,
    :bd=>'2',
    :title=>'Please choose multiple...',
    :returnChoiceAndClient=>false
  }.merge(hash)

  # Necessary, because hash[:configSection]  is deleted later on
  # in args_2.
  CONF.section = hash[:configSection] if CONF && hash[:configSection]

  help,hash,question,title,searchFieldHelp,@returnChoiceAndClient= 
    TKXXS_CLASSES.args_2(help,hash,question,:title,:searchFieldHelp,:returnChoiceAndClient)

  ##/ question = hash[:question] unless question  
  ##/ help = hash[:help] unless help
  ##/ CONF.section = hash[:configSection]  if CONF
  ##/ 
  ##/ hash.delete :question
  ##/ hash.delete :help
  ##/ hash.delete :configSection
  
  teardownDone = false
  @ans = nil
  @goOn = goOn = TkVariable.new 
  # Because you cannot use instance-vars in procs. TODO: smarter way?
  case aryWithChoices[0].class.to_s
  when 'String'
    choices = aryWithChoices
    clients = choices.dup
    choicesAndClients = choices.zip(clients)
  when 'Array'
    choices, clients, tmpHelp = aryWithChoices.transpose 
    choicesAndClients = aryWithChoices
    help = tmpHelp if tmpHelp
  else
    raise "ERROR: aryWithChoices[0].class must be 'String' or 'Array',\nbut is #{ aryWithChoices[0].inspect }.class = #{ aryWithChoices[0].class } "
  end

  Tk.update # Show any calling widget first to make it lower than @dialog 
  #----  Toplevel: @dialog
  @dialog=Toplevel.new() {title(title)} 
  @dialog.geometry = CONF[:dialogGeom]  if CONF
  @dialog.bind('Destroy') {
    unless teardownDone
      goOn.value = '1' # !
      if CONF
        CONF[:dialogGeom] = @dialog.geometry
      end
      teardownDone = true
    end # unless
  }

  #----  Frame
  @frame = Frame.new(@dialog) {padding "3 3 3 3"}.
    pack(:fill=>:both,:expand=>true)

  #----  Label
  @lbl = Label.new(@frame){
    text question
    font $font if $font
    wraplength '400'
    justify 'left'
  }
  @lbl.grid(:sticky=>'news')
  
  #----  self = TkListbox 
  super(@frame, hash) 
  # TODO: Is there no way to increase the internal padding?? 
  #self.pack(:side=>:left,:expand=>1,:fill=>:both)
  self.grid(:sticky=>'news')
  
  #----  Scrollbar 
  scrollb = Scrollbar.new(@frame).
    grid(:row=>1,:column=>1,:sticky=>'news')
  self.yscrollbar(scrollb)

  #----  Button-Cancel
  @btn = Button.new(@frame, :text=>"Cancel").
    grid(:row=>2,:column=>0,:sticky=>'w')
  @btn.command { 
    self.destroy 
  }

  #----  Button-OK
  @btn = Button.new(@frame, :text=>"OK").
    grid(:row=>2,:column=>0,:sticky=>'e')
  @btn.command { 
    ans = []
    # TODO: Change to self.curselection like in SingleChoiceD
    begin
      choicesAndClients.each_with_index {|cc, idx|
        ans << cc  if self.selection_includes(idx)
      }
    rescue
      ans = []
    end
    ans_( ans )
    goOn.value = '1'
  } 

  @frame.grid_columnconfigure(0, :weight=>1)
  @frame.grid_columnconfigure(1, :weight=>0)
  @frame.grid_rowconfigure([0,2], :weight=>0)
  @frame.grid_rowconfigure(1, :weight=>1)
  insert(0, *choices)
  @lbl.bind('Configure'){ 
    @lbl.wraplength =  TkWinfo.width(@frame) - 40
  }

  #----  Balloonhelp
  if help
    BalloonHelp.new(self, 
                    :command=>proc{|x,y,bhelp,parent|
                      idx = parent.nearest(y)
                      bhelp.text( help[idx] )
                    }
    )
  end

  update
end

Instance Attribute Details

#dialogObject

in: “self.dialog



680
681
682
# File 'lib/tkxxs/tkxxs_classes.rb', line 680

def dialog
  @dialog
end

Instance Method Details

#answerObject

/ def answer( ) / @goOn.wait / self.dialog.destroy # includes grab(“release”) / @ans = [] unless @ans / @ans / end # ans

Returns:

  • An Array of the chosen right sides of aryWithChoices if :returnChoiceAndClient == false (default),

  • An Array of the chosen right and left sides of aryWithChoices if :returnChoiceAndClient == true,

  • nil, if ‘Cancel’ was clicked.



823
824
825
826
827
828
829
830
831
832
833
# File 'lib/tkxxs/tkxxs_classes.rb', line 823

def answer(  )
  @goOn.wait
  self.dialog.destroy
  @ans = []  unless @ans

  if @returnChoiceAndClient
    return @ans.transpose[0].zip(@ans.transpose[1]) # wg. help
  else
    return @ans.transpose[1]
  end
end