Class: HinnerFileDialog

Inherits:
HinnerDialog show all
Defined in:
lib/a-tkcommons.rb

Constant Summary collapse

SELECT_FILE_MODE =
0
SAVE_FILE_MODE =
1
SELECT_DIR_MODE =
2

Instance Method Summary collapse

Methods inherited from HinnerDialog

#is_modal?, #make_scrollable_frame, #release, #show_modal

Constructor Details

#initialize(mode = SELECT_FILE_MODE, must_exist = nil, label = nil, side = 'top', args = nil) ⇒ HinnerFileDialog

Returns a new instance of HinnerFileDialog.



3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
# File 'lib/a-tkcommons.rb', line 3408

def initialize(mode=SELECT_FILE_MODE, must_exist=nil, label=nil, side='top',args=nil)
  super(side, args)
  @mode = mode
  if must_exist.nil?
    must_exist = mode != SAVE_FILE_MODE
  end
  @must_exist = must_exist
  @label = label
  build_gui
  @closed = false
end

Instance Method Details

#build_guiObject



3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
# File 'lib/a-tkcommons.rb', line 3420

def build_gui
  @font = Arcadia.conf('edit.font')
  @font_bold = "#{Arcadia.conf('edit.font')} bold"
  @font_metrics = TkFont.new(@font).metrics
  @font_metrics_bold = TkFont.new(@font_bold).metrics
  if !@label.nil?
    Arcadia.wf.label(self, 'text' => @label).pack('side' =>'left')
  end
  @dir_text = TkText.new(self, Arcadia.style('text').update({"height"=>'1',"highlightcolor"=>Arcadia.conf('panel.background'), "bg"=>Arcadia.conf('panel.background')})).pack('side' =>'left','padx'=>5, 'pady'=>5, 'fill'=>'x', 'expand'=>'1')
  #{"bg"=>'white', "height"=>'1', "borderwidth"=>0, 'font'=>@font}
  @dir_text.bind_append("Enter", proc{ @dir_text.set_insert("end")})
  #@dir_text.tag_configure("entry",'foreground'=> "red",'borderwidth'=>0, 'relief'=>'flat',  'underline'=>true)

  @tag_file_exist = "file_exist"
  @dir_text.tag_configure(@tag_file_exist,'background'=> Arcadia.conf("hightlight.selected.background"), 'borderwidth'=>0, 'relief'=>'flat', 'underline'=>true)
  
  @tag_selected = "link_selected"
  @dir_text.tag_configure(@tag_selected,'borderwidth'=>0, 'relief'=>'flat', 'underline'=>true)
  @dir_text.tag_bind(@tag_selected,"ButtonRelease-1",  proc{ 
     self.release
     Tk.callback_break
  } )
  @dir_text.tag_bind(@tag_selected,"Enter", proc{@dir_text.configure('cursor'=> 'hand2')})
  @dir_text.tag_bind(@tag_selected,"Leave", proc{@dir_text.configure('cursor'=> @cursor)})
  _self=self
  @dir_text.bind_append('KeyPress', "%K"){|_keysym|
    case _keysym
    when 'Escape','Tab'
      i1 = @dir_text.index("insert")
      raise_candidates(i1, @dir_text.get("#{i1} linestart", i1))
    when "Return"
      if (@mode == SELECT_FILE_MODE || @mode == SAVE_FILE_MODE) && @must_exist
        str_file = @dir_text.get('1.0','end')
        if str_file && str_file.length > 0 && File.exists?(str_file.strip) && File.ftype(str_file.strip) == 'file'
          _self.release
        end
        Tk.callback_break
      elsif @mode == SELECT_DIR_MODE && @must_exist
        str_file = @dir_text.get('1.0','end')
        if str_file && str_file.length > 0 && File.exists?(str_file.strip) && File.ftype(str_file.strip) == 'directory'
          _self.release
        end
        Tk.callback_break
      else
        _self.release
      end
    end
  }   
  @dir_text.bind_append('KeyRelease', "%K"){|_keysym|
    case _keysym
    when 'Escape','Tab', "Return"
    else
      @dir_text.tag_remove(@tag_selected,'1.0','end')
      i1 = @dir_text.index("insert - 1 chars wordstart")
      while @dir_text.get("#{i1} -1 chars",i1) != File::SEPARATOR && @dir_text.get("#{i1} - 1 chars",i1) != ""
        i1 = @dir_text.index("#{i1} - 1 chars")
      end
      i2 = @dir_text.index("insert")
      
      @dir_text.tag_add(@tag_selected ,i1,i2) if @mode == SAVE_FILE_MODE
      
      if File.exists?(@dir_text.get('1.0',i2))
        @dir_text.tag_add(@tag_file_exist ,i1,i2)
        @dir_text.tag_add(@tag_selected ,i1,i2) if @mode == SELECT_FILE_MODE && File.ftype(@dir_text.get('1.0',i2)) == 'file'
        @dir_text.tag_add(@tag_selected ,i1,i2) if @mode == SELECT_DIR_MODE && File.ftype(@dir_text.get('1.0',i2)) == 'directory'
      else
        @dir_text.tag_remove(@tag_file_exist,'1.0','end')
      end
    end
  }   
  
  @dir_text.bind_append("Control-KeyPress", "%K"){|_keysym|
    case _keysym
    when 'd'
      _self.close
      Tk.callback_break
    end
  }    

  #@select_button = Tk::BWidget::Button.new(self, Arcadia.style('toolbarbutton')){
  @select_button = Arcadia.wf.toolbutton(self){
    command proc{_self.close}
    image Arcadia.image_res(CLOSE_FRAME_GIF)
  }.pack('side' =>'right','padx'=>5, 'pady'=>0)
end

#closeObject



3520
3521
3522
3523
3524
# File 'lib/a-tkcommons.rb', line 3520

def close
  @closed=true
  self.release
  destroy  
end

#dir(_dir) ⇒ Object



3516
3517
3518
# File 'lib/a-tkcommons.rb', line 3516

def dir(_dir)
  file(_dir)
end

#file(_dir) ⇒ Object



3506
3507
3508
3509
3510
3511
3512
3513
3514
# File 'lib/a-tkcommons.rb', line 3506

def file(_dir)
  set_dir(_dir)
  show_modal(false)
  if @closed == false
    file_selected = @dir_text.get("0.1","end").strip
    destroy  
    file_selected
  end
end

#last_candidate_is_dir?(_name) ⇒ Boolean

Returns:

  • (Boolean)


3609
3610
3611
# File 'lib/a-tkcommons.rb', line 3609

def last_candidate_is_dir?(_name)
  @last_candidates_dir && @last_candidates_dir.include?(_name)
end

#last_candidate_is_file?(_name) ⇒ Boolean

Returns:

  • (Boolean)


3605
3606
3607
# File 'lib/a-tkcommons.rb', line 3605

def last_candidate_is_file?(_name)
  @last_candidates_file && @last_candidates_file.include?(_name)
end

#load_from_dir(_dir) ⇒ Object



3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
# File 'lib/a-tkcommons.rb', line 3844

def load_from_dir(_dir)
  childrens = Dir.entries(_dir)
  childrens_dir = Array.new
  childrens_file = Array.new
  childrens.sort.each{|c|
    if c != '.' && c != '..'
      child = File.join(_dir,c)
      fty = File.ftype(child)
      if fty == "file"
        childrens_file << c
        #childrens_file << child
      elsif fty == "directory"
        #childrens_dir << child
        childrens_dir << c
      end
    end
  }
  return childrens_dir,childrens_file
end

#raise_candidates(_inx, _dir) ⇒ Object



3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
# File 'lib/a-tkcommons.rb', line 3577

def raise_candidates(_inx, _dir)
  if _dir[-1..-1] != File::SEPARATOR && _dir !=nil && _dir.length > 0
    len = _dir.split(File::SEPARATOR)[-1].length+1
    _dir = _dir[0..-len]
    _inx = "#{_inx} - #{len-1} chars"
  end 
  _dir=Dir.pwd if !File.exists?(_dir)  
  @dir_text.set_insert("end")
  dirs_and_files=load_from_dir(_dir)
  if dirs_and_files[0].length + dirs_and_files[1].length == 1
    if dirs_and_files[0].length == 1
      one = "#{_dir}#{dirs_and_files[0][0]}"
    else
      one = "#{_dir}#{dirs_and_files[1][0]}"
    end
    set_dir(one)
  elsif dirs_and_files[0].length + dirs_and_files[1].length == 0
    # do not raise
  else 
    if @mode == SELECT_DIR_MODE
      raise_dir(_inx, _dir, dirs_and_files[0], [])
    else
      raise_dir(_inx, _dir, dirs_and_files[0], dirs_and_files[1])
    end
  end
  Tk.callback_break
end

#raise_dir(_index, _dir, _candidates_dir, _candidates_file = nil) ⇒ Object



3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
# File 'lib/a-tkcommons.rb', line 3613

def raise_dir(_index, _dir, _candidates_dir, _candidates_file=nil) 
  @raised_listbox_frame.destroy if @raised_listbox_frame != nil
  @last_candidates_dir = _candidates_dir
  @last_candidates_file = _candidates_file
  _index_now = @dir_text.index('insert')
  _index_for_raise =  @dir_text.index("#{_index} wordstart")
  _candidates = [] 
  _candidates.concat(_candidates_dir) if _candidates_dir 
  _candidates.concat(_candidates_file) if _candidates_file 
      
  if _candidates.length >= 1 
      _rx, _ry, _width, heigth = @dir_text.bbox(_index_for_raise);
      _x = _rx + TkWinfo.rootx(@dir_text)  
      _y = _ry + TkWinfo.rooty(@dir_text)  + @font_metrics[2][1]
      _xroot = _x
      _yroot = _y

      max_width = TkWinfo.screenwidth(Arcadia.layout.root) - _x
      
      @raised_listbox_frame = TkFrame.new(Arcadia.layout.root, {
        :padx=>"1",
        :pady=>"1",
        :background=> "yellow"
      })
      
      @raised_listbox = TkTextListBox.new(@raised_listbox_frame, {
        :takefocus=>true}.update(Arcadia.style('listbox')))
      @raised_listbox.tag_configure('file','foreground'=> Arcadia.conf('hightlight.link.foreground'),'borderwidth'=>0, 'relief'=>'flat')

      
      _char_height = @font_metrics[2][1]
      _width = 0
      _docs_entries = Hash.new
      _item_num = 0

      _select_value = proc{
        if @raised_listbox.selected_line && @raised_listbox.selected_line.strip.length>0
          #_value = @raised_listbox.selected_line.split('-')[0].strip
          seldir = File.join(_dir,@raised_listbox.selected_line)

          set_dir(seldir)
          @raised_listbox_frame.grab("release")
          @raised_listbox_frame.destroy
        end
      }    

      _update_list = proc{|_in|
          _in.strip!
          @raised_listbox.clear
          _length = 0
          _candidates.each{|value|
            _doc = value.strip
            _class, _key, _arity = _doc.split('#')
            if _key && _arity
              args = arity_to_str(_arity.to_i)
              if args.length > 0
                _key = "#{_key}(#{args})"
              end
            end
            
            if _key && _class && _key.strip.length > 0 && _class.strip.length > 0 
              _item = "#{_key.strip} #{TkTextListBox::SEP} #{_class.strip}"
            elsif _key && _key.strip.length > 0
              _item = "#{_key.strip}"
            else
              _key = "#{_doc.strip}"
              _item = "#{_doc.strip}"
            end
            array_include = proc{|_a, _str, _asterisk_first_char|
              ret = true
              str = _str
              _a.each_with_index{|x, j|
                next if x.length == 0
                if j == 0 && !_asterisk_first_char
                  ret = ret && str[0..x.length-1] == x
                else
                  ret = ret && str.include?(x)
                end
                if ret 
                  i = str.index(x)
                  str = str[i+x.length..-1]
                else
                  break
                end
              }
              ret
            }
            if _in.nil? || _in.strip.length == 0 || _item[0.._in.length-1] == _in || 
               (_in.include?('*') &&  array_include.call(_in.split("*"), _item, _in[0..1]=='*'))
#                 (_in[0..0] == '*' && _item.include?(_in[1..-1]))

              _docs_entries[_item]= _doc
     #         @raised_listbox.insert('end', _item)
              if last_candidate_is_dir?(_item)
                @raised_listbox.add(_item, 'file')
              else
                @raised_listbox.add(_item)
              end
              _temp_length = _item.length
              _length = _temp_length if _temp_length > _length 
              _item_num = _item_num+1 
              _last_valid_key = _key
            end
          }
          _width = _length*8
          if @raised_listbox.length == 0
            @raised_listbox.grab("release")
            @raised_listbox_frame.destroy
            @dir_text.focus
            Tk.callback_break
            #Tk.event_generate(@raised_listbox, "KeyPress" , :keysym=>"Escape") if TkWinfo.mapped?(@raised_listbox)
          else
            @raised_listbox.select(1)
            Tk.event_generate(@raised_listbox, "1") if TkWinfo.mapped?(@raised_listbox)
          end
      }

      get_filter = proc{
        filter = ""
        if @dir_text.get("insert -1 chars", "insert") != File::SEPARATOR  
          file_str = @dir_text.get("insert linestart", "insert")
          parts = file_str.split(File::SEPARATOR)
          if _dir == File::SEPARATOR
            original_parts = [""] 
          else
            original_parts = _dir.split(File::SEPARATOR)
          end
          if parts && parts.length == original_parts.length + 1
            filter = parts[-1]
          end
        end
        filter = "" if filter.nil?
        filter
      }
      #filter = @dir_text.get("insert -1 chars wordstart", "insert")


      @raised_listbox.bind_append('KeyPress', "%K %A"){|_keysym, _char|
        is_list_for_update = false
        case _keysym
          when 'a'..'z','A'..'Z','0'..'9'
            @dir_text.insert('end', _keysym)
            @dir_text.see("end")
            is_list_for_update = true
          when 'minus'
            @dir_text.insert('end', _char)
            @dir_text.see("end")
            is_list_for_update = true
          when 'period'
            @dir_text.insert('end', '.')
            @dir_text.see("end")
            is_list_for_update = true
          when 'BackSpace'
            if @dir_text.get("insert -1 chars", "insert") != File::SEPARATOR 
              @dir_text.delete('end -2 chars','end')
            end
            is_list_for_update = true
          when 'Escape'
            @raised_listbox.grab("release")
            @raised_listbox_frame.destroy
            @dir_text.focus
            Tk.callback_break
          when "Next","Prior"
          when "Down","Up"
            Tk.callback_break
          else
            Tk.callback_break
        end
        _update_list.call(get_filter.call) if is_list_for_update
        @raised_listbox.focus 
        Tk.callback_break if  !["Next","Prior"].include?(_keysym)
      }

      @raised_listbox.bind_append('Shift-KeyPress', "%K %A"){|_keysym, _char|
        is_list_for_update = false
        case _keysym
          when 'asterisk','underscore'
            @dir_text.insert('end', _char)
            @dir_text.see("end")
            is_list_for_update = true
          when 'a'..'z','A'..'Z'
            @dir_text.insert('end', _keysym)
            @dir_text.see("end")
            is_list_for_update = true
          
        end
        _update_list.call(get_filter.call) if is_list_for_update
        @raised_listbox.focus 
        Tk.callback_break
      }

      @raised_listbox.bind_append('KeyRelease', "%K"){|_keysym|
        case _keysym
          when 'Return'
            _select_value.call
        end
      }
      
      _update_list.call(get_filter.call)

      if @raised_listbox.length == 1
        _select_value.call
      else
        _width = _width + 30
        _width = max_width if _width > max_width
        _height = 15*_char_height
        
        @raised_listbox_frame.place('x'=>_x,'y'=>_ry, 'width'=>_width, 'height'=>_height)
        @raised_listbox.extend(TkScrollableWidget).show(0,0) 
        @raised_listbox.place('x'=>0,'y'=>0, 'relwidth'=>1, 'relheight'=>1) 
        @raised_listbox.focus
        @raised_listbox.select(1)

        Tk.update
        @raised_listbox_frame.grab("set")

     
     
        @raised_listbox.bind_append("Double-ButtonPress-1", 
          proc{|x,y| 
            _select_value.call
            Tk.callback_break
              }, "%x %y")

      end  
    elsif _candidates.length == 1 && _candidates[0].length>0
      @dir_text.set_dir(_candidates[0])
    end
end

#set_dir(_dir) ⇒ Object



3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
# File 'lib/a-tkcommons.rb', line 3526

def set_dir(_dir)
  _dir=Dir.pwd if !File.exists?(_dir)
  #load_from_dir(_dir)
  @dir_text.state("normal")
  @dir_text.delete("0.1","end")
  @cursor =  @dir_text.cget('cursor')
  dir_seg = _dir.split(File::SEPARATOR)
  incr_dir = ""
  get_dir = proc{|i| 
    res = ""
    0.upto(i){|j|
       if res == File::SEPARATOR
        res=res+dir_seg[j]
       elsif res.length == 0 && dir_seg[j].length == 0
        res=File::SEPARATOR+dir_seg[j]
       elsif res.length == 0 && dir_seg[j].length > 0
        res=dir_seg[j]
       else
        res=res+File::SEPARATOR+dir_seg[j]
       end
    }
    is_dir = File.ftype(res) == "directory"
    res=res+File::SEPARATOR if is_dir && res[-1..-1]!=File::SEPARATOR
    res
  }
  
  dir_seg.each_with_index{|seg,i|
    tag_name = "link#{i}"
    @dir_text.tag_configure(tag_name,'foreground'=> Arcadia.conf('hightlight.link.foreground'),'borderwidth'=>0, 'relief'=>'flat', 'underline'=>true)

    dir = get_dir.call(i)
    if File.ftype(dir) == "directory"
      @dir_text.insert("end", seg, tag_name)
      @dir_text.insert("end", "#{File::SEPARATOR}")
      @dir_text.tag_bind(tag_name,"ButtonRelease-1",  proc{ 
        inx = @dir_text.index("insert wordend +1 chars")
        @dir_text.set_insert("end")
        raise_candidates(inx, dir)
      } )
      @dir_text.tag_bind(tag_name,"Enter", proc{@dir_text.configure('cursor'=> 'hand2')})
      @dir_text.tag_bind(tag_name,"Leave", proc{@dir_text.configure('cursor'=> @cursor)})
    else
      @dir_text.insert("end", seg, @tag_selected)
    end
  }
  
  @dir_text.focus
  @dir_text.set_insert("end")
  @dir_text.see("end")
end