File: FitsFileSelection.tcl

package info (click to toggle)
ftools-fv 5.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,908 kB
  • ctags: 2,922
  • sloc: tcl: 48,319; ansic: 16,926; cpp: 169; makefile: 157; sh: 121; csh: 10; exp: 2
file content (944 lines) | stat: -rw-r--r-- 27,224 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
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
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
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
841
842
843
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# modified by Jianjun Xu 

#
#                          FitsFileselectionbox
# ------------------------------------------------------------------
itcl::class FitsFileselectionbox {
    inherit itk::Toplevel

    constructor {args} {}
    destructor {}

    private common _ffbFilter *
    private common _ffbFileName ""
    private common _displayFitsOnly 0
    private common _userName ""
    private common _passwd ""
    private common _ftpHost ""

    private variable _pwd .
    private variable _selected ""
    private variable _fileCmd Open
    private variable _isFtp 0
    private variable _lastLocalDir  "" 
    private variable _lastRemoteDir ""
    private variable _nameList {}
    private variable _sizeList {}
    private variable _dateList {}
    private variable _dirChar "/"
    
    private method _fillContent {cDir}
    private method _upDir {}
    private method _setDir { n }
    private method _openThisFile {}
    private method _openSelection {} 
    private method _completeFileName {}
    private method _selectThat { box }
    private method _listFits {}
    private method _setFilter {}
    private method _helpCmd {} 
    private method _initFtp {} 
    private method _isDir {}
    private method _pwd { {full 1} }
    private method _updateAll {}
    private method _switchRemoteLocal {}
    private method _closeFTP {}
    private method _showNamedFile {}
    private method _searchCharsForCompletion {root list} 
    private method _pickElemByName {name list} 
    private method _ftpOpen {}
    private method _buildMenu {}
    private method _postMenu {time x y}
    private method _buttonRelease {time}

    private method _scrollBoxes  { args }
    private method _scrollOthers { box args }
    private method _updateBoxes  { data }

    private method _cancelCmd   {}
    private method _openFileCmd {}
    private method _saveFileCmd {}
    private method _setCmd {cmd} 

    private variable _postTime
    private variable _pathElems
    private variable _fileCols "files sizes dates"
    private variable _fileLbls "Name Size {Mod Date}"
    private variable _driveList
    private variable _dsizeList
    private variable _ddateList

    public method get {} 
    public method chgDir {dir}
    public method init {}
    public method activate { cmd {defaultName ""} }
    public method deactivate {}
    public method changeListFITSoption {}
}

# ------------------------------------------------------------------
#                        CONSTRUCTOR
# ------------------------------------------------------------------
itcl::body FitsFileselectionbox::constructor {args} {
    global g_fitsFileMode
    global isMac
    global isWin

    if { $isWin } {
       set _driveList {}
       foreach drive { c d e f g h i j k l m n o p q r s } {
           catch { [glob ${drive}:/*] } returnCodeList
           catch { [glob ${drive}:/*.*] } returnCodeFile
           if { [string first "no files matched" $returnCodeList] < 0 ||
                [string first "no files matched" $returnCodeFile] < 0 } {
              lappend _driveList "$drive:/"
              lappend _dsizeList "(dir)"
              lappend _ddateList {}
           }
       }
    }
    
    wm withdraw $itk_component(hull)
    wm geometry $itk_component(hull) 350x450
    if { $isMac } { set _dirChar ":" }

    component hull configure -borderwidth 0

    #
    # Create an internal frame to contain the components.
    #
    itk_component add frame {
        frame $itk_interior.frame
    } 
    pack $itk_component(frame) -fill both -expand yes
#    pack propagate $itk_component(frame) no

    # Create the dir entry.
    #
    itk_component add dframe {
	frame $itk_component(frame).df 
    }
    pack $itk_component(dframe) -fill x -expand 0 -pady 4 -padx 4

    itk_component add dirBtn {
       menubutton $itk_component(dframe).dir -indicatoron 1 -relief raised \
             -menu $itk_component(dframe).dir.menu
    }
    pack $itk_component(dirBtn) -side left -fill x -expand 1 -padx 2

    itk_component add dirMenu {
       menu $itk_component(dirBtn).menu -tearoff 0
    }

    bind $itk_component(dirBtn) <ButtonPress-1> \
	    "[itcl::code $this _postMenu %t %X %Y]; break"
    bind $itk_component(dirMenu) <ButtonRelease-1> \
	    [itcl::code $this _buttonRelease %t]
    
    image create bitmap _upDirIcon -data {
	#define updir_width 28
	#define updir_height 16
	static char updir_bits[] = {
           0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20,
           0x00, 0x00, 0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01,
           0x10, 0x00, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x07,
           0x00, 0x01, 0x90, 0x0f, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01,
           0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0xfe,
           0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01,
           0xf0, 0xff, 0xff, 0x01};
    }
    
    itk_component add updir {
 	button $itk_component(dframe).up \
	    -image _upDirIcon \
	    -command  [itcl::code $this _upDir]
    }
    pack $itk_component(updir) -side left -expand 0 -padx 2

    itk_component add ftpB {
 	button $itk_component(dframe).ftpb -text "FTP..."\
	    -command  [itcl::code $this _switchRemoteLocal] \
	    -padx 1 -pady 1 -width 8
    } 
    pack $itk_component(ftpB) -side left -expand 0 -padx 2

    ############################
    #
    #   Create the files list.
    #

    itk_component add fframe {
	frame $itk_component(frame).fframe -relief sunken
    }
    pack $itk_component(fframe) -side top -fill both -expand 1 -padx 4 -pady 6
    grid columnconfig $itk_component(fframe) 0 -weight 1
    grid rowconfig    $itk_component(fframe) 1 -weight 1

    # Create file columns

    set i 0
    foreach l $_fileLbls lb $_fileCols {
       itk_component add lbl$i {
          label $itk_component(fframe).lbl$i -text $l -relief raised
       }
       itk_component add $lb {
          listbox $itk_component(fframe).$lb -height 3 -relief flat \
                -exportselection 0 -bd 0 -highlightthickness 0 \
                -yscrollcommand [itcl::code $this _scrollOthers $lb] \
                -takefocus 0
       }
       grid $itk_component(lbl$i) -row 0 -column $i -sticky "news"
       grid $itk_component($lb)   -row 1 -column $i -sticky "news"

       bind $itk_component($lb) <ButtonRelease-1> \
             +[itcl::code $this _selectThat $lb]
       bind $itk_component($lb) <Double-1> \
             "setWatchCursor $itk_component(hull) [itcl::code $this _openSelection]"

       incr i
    }
    $itk_component(sizes) config -width 7
    $itk_component(dates) config -width 12

    # scrollbar

    itk_component add fscroll {
        scrollbar $itk_component(fframe).fscroll \
              -command [itcl::code $this _scrollBoxes]
    } 
    grid $itk_component(fscroll) -row 1 -column $i -sticky news

    #  
    #
    ############################

# the bottom portion using grid
    itk_component add bframe {
        frame $itk_component(frame).bframe
    } 
    pack $itk_component(bframe) -side top -fill x -expand 0   
# File name 
    itk_component add fnamel {
	label $itk_component(bframe).fnamel -text "File Name"
    }
    itk_component add fnamee {
	entry $itk_component(bframe).fnamee -textvariable [itcl::scope _ffbFileName]
    }
    bind $itk_component(fnamee) <Return> \
	  "setWatchCursor $itk_component(hull) [itcl::code $this _openThisFile]"

# 
#    bind all <Tab> {}
    bind $itk_component(fnamee) <Tab> "[itcl::code $this _completeFileName]; break"
# Filter 
    itk_component add filterl {
	label $itk_component(bframe).filterl -text "File of type"
    }
    itk_component add filtere {
	entry $itk_component(bframe).filtere -textvariable [itcl::scope _ffbFilter]
    }
    bind $itk_component(filtere) <Return> [itcl::code $this _setFilter]
# file mode
    itk_component add filemode {
	checkbutton $itk_component(bframe).filemode \
	    -variable g_fitsFileMode \
	    -selectcolor $fvPref::checkBBgColor  \
	    -activeforeground black -activebackground $fvPref::globalBgColor \
	    -text "Open as read-only"
    }
# fitsfile
    set _displayFitsOnly $fvPref::ifDispFitsOnly

    itk_component add fitsmode {
	checkbutton $itk_component(bframe).fitsmode \
	    -variable [itcl::scope _displayFitsOnly] \
	    -text "List FITS files only" \
	    -selectcolor $fvPref::checkBBgColor \
	    -activeforeground black -activebackground $fvPref::globalBgColor \
	    -command [itcl::code $this _listFits] 
    }
# buttons
    itk_component add openB {
	button $itk_component(bframe).openB -text Open \
	    -command "setWatchCursor $itk_component(hull) [itcl::code $this _openThisFile]"
    }
    itk_component add cancelB {
	button $itk_component(bframe).cancelB -text Cancel \
	    -command [itcl::code $this _cancelCmd]
    }
    itk_component add helpB {
	button $itk_component(bframe).helpB -text Help \
	    -command [itcl::code $this _helpCmd]
    }
# geometry
    grid config $itk_component(fnamel) -column 0 -row 0 \
	-columnspan 1 -rowspan 1 -sticky "w" 
    grid config $itk_component(fnamee) -column 1 -row 0 \
	-columnspan 1 -rowspan 1 -sticky "snew" 
    grid config $itk_component(filterl) -column 0 -row 1 \
	-columnspan 1 -rowspan 1 -sticky "w"
    grid config $itk_component(filtere) -column 1 -row 1 \
	-columnspan 1 -rowspan 1 -sticky "snew" 
    grid config $itk_component(filemode) -column 1 -row 2 \
	-columnspan 1 -rowspan 1 -sticky "w"
    grid config $itk_component(fitsmode) -column 1 -row 3 \
	-columnspan 1 -rowspan 1 -sticky "w"
    grid config $itk_component(openB) -column 2 -row 0 \
	-columnspan 1 -rowspan 1 -sticky "snew" -padx 4
    grid config $itk_component(cancelB) -column 2 -row 1 \
	-columnspan 1 -rowspan 1 -sticky "snew" -padx 4
    grid config $itk_component(helpB) -column 2 -row 2 \
	-columnspan 1 -rowspan 1 -sticky "snew" -padx 4

    grid columnconfigure $itk_component(bframe) 1 -weight 5

    wm protocol $itk_interior WM_DELETE_WINDOW [itcl::code $this _cancelCmd]

    eval itk_initialize $args
}

# ------------------------------------------------------------------
#                           DESTRUCTOR
# ------------------------------------------------------------------
itcl::body FitsFileselectionbox::destructor {} {
}

itcl::body FitsFileselectionbox::_fillContent {cDir} {
    global isMac isWin
    
    if { $cDir != "" } {
        chgDir $cDir
    }
    _buildMenu

    set fileNameList ""
    set fileSizeList ""
    set fileDateList ""
    set dirNameList  ""
    set dirSizeList  ""
    set dirDateList  ""
    set selCO ""

    if { $_isFtp } {
        _updateBoxes [list {\[Loading...\]} {} {}]
        set rlist [setWatchCursor $itk_component(hull) \
              [itcl::code ftpClient list ""]]
	set dirNameList  [lindex $rlist 0]
        set dirSizeList  [lindex $rlist 1]
        set dirDateList  [lindex $rlist 2]
	set fileNameList [lindex $rlist 3]	
        set fileSizeList [lindex $rlist 4]
        set fileDateList [lindex $rlist 5]
    } else {
	set _lastLocalDir $_pwd
	set _ffbFilter [string trim $_ffbFilter " "]
	if { $_ffbFilter  == "" } {
	    set _ffbFilter *
	}
	
	set tmpID [$itk_component(files) curselection]
	if { $tmpID != "" } {
	    $itk_component(files) see $tmpID
	} 
	
        if { $cDir == "" } {
            set curContent [file volumes]
        } else {
            #multi filter separated by comma
            set _ffbFilter [join [split $_ffbFilter ","]]
            set curContent [lsort -increasing [eval glob -nocomplain $_ffbFilter]]
        }

        foreach i $curContent {
	   if ![file readable $i ] continue
           if {  ( [string index $i 0]=="~" ) || \
                 ( ($isMac || $isWin) && [file attributes $i -hidden]) || \
                 ( $isMac && $i=="Trash" ) } {
              continue
           }
           if { [catch {set fType [file type [resolveSymLinks $i]]}] } {
              continue
           }
           switch $fType {
              file {
                 if { $_displayFitsOnly } {
                    if { [catch {set isfits [isFits $i]}] } continue
                    if { $isfits == 0 } continue
                 }
                 lappend fileNameList $i
                 lappend fileSizeList [calcSizeStr [file size $i]]
                 lappend fileDateList [file mtime $i]
              }
              directory {
                 lappend dirNameList [string trimright $i $_dirChar]$_dirChar
                 lappend dirSizeList "(dir)"
                 lappend dirDateList [file mtime $i]
              }
              default {
                 ;
              }
           }
        }

    }
    if { $isMac } {
	set _nameList [eval list $dirNameList $fileNameList]
        set _sizeList [eval list $dirSizeList $fileSizeList]
        set _dateList [eval list $dirDateList $fileDateList]
    } else {
	set _nameList [eval list "../" $dirNameList $fileNameList]
        set _sizeList [eval list "(dir)" $dirSizeList $fileSizeList]
        set _dateList [eval list "-" $dirDateList $fileDateList]
    }

    set selID [$itk_component(files) curselection]
    if { $selID != "" } {
	set selCO [$itk_component(files) get $selID]
    }

    if { [llength $_nameList] == 0 } {
       _updateBoxes [list {\[  \]} {} {}]
    } elseif { $_isFtp } {
       _updateBoxes [list $_nameList $_sizeList $_dateList]
    } else {
       set _dateListFmt {}
       foreach d $_dateList {
          lappend _dateListFmt [calcDateStr $d]
       }
       _updateBoxes [list $_nameList $_sizeList $_dateListFmt]
    }

    set newID [lsearch $_nameList $selCO] 
    if { $newID != -1} {
	$itk_component(files) selection set $newID
	$itk_component(files) see $newID
        _selectThat files
    }

}

itcl::body FitsFileselectionbox::_upDir {} {
    global isMac
    global isWin
    
    set preUpDir $_pwd
    if { $_isFtp } {
	ftpClient cd ..
    } else {
        if { $isMac && [string first : $_pwd] == [expr [string length $_pwd]-1] } {
            set _pwd ""
            _fillContent ""
            return
        } elseif { $_pwd != "" } {
            cd ..
        }
    }
    set _pwd [_pwd]
    set postUpDir $_pwd
   
    if { $isWin && $preUpDir == $postUpDir } {
       _updateBoxes [list $_driveList $_dsizeList $_ddateList]
    } else {
       _fillContent $_pwd
    }
}

itcl::body FitsFileselectionbox::_selectThat { box } {
   set tmpIdx [$itk_component($box) curselection]
   if { $tmpIdx == "" } {
      set _selected ""
      return
   }
   foreach b $_fileCols {
      if { $box!=$b } {
         $itk_component($b) selection clear 0 end
         $itk_component($b) selection set $tmpIdx
      }
   }
   set _selected [$itk_component(files) get $tmpIdx]
   if { ![_isDir] } {
      set _ffbFileName $_selected
      $itk_component(fnamee) selection range 0 end
      focus $itk_component(fnamee)
      $itk_component(fnamee) icursor end
   }
}


itcl::body FitsFileselectionbox::_isDir {} {
    global isMac
    if { $_isFtp } {
	if { [string range $_selected end end] == "/" } {
	    return 1
	} elseif { [string trim $_selected .] == "" } {
	    return 1
        } elseif { [lsearch -exact $_nameList $_ffbFileName/] != -1 } {
	    return 1
        } elseif { [string first / $_ffbFileName] != -1 } {
            return 1
	} else {
	    return 0
	} 
    } else {
        if { $isMac && $_pwd=="" } {
            return 1
        } else {
            return [file isdirectory [string trimright $_selected $_dirChar]]
        }
    }
}

itcl::body FitsFileselectionbox::_openThisFile {} {
   set _ffbFileName [string trim $_ffbFileName " "]
   if { $_ffbFileName != "" } {
      set _selected $_ffbFileName
      set dirFlag [_isDir]
      _openSelection
      if { $dirFlag } {
         set _ffbFileName ""
      }
   }
}


itcl::body FitsFileselectionbox::_openSelection {} {
    global isMac
    if { $_selected=="" } return
    if { [_isDir] } {
	# open contents of directory
	if { !$isMac && [file pathtype $_selected]=="absolute" } {
	    _fillContent $_selected
	} else {
	    _fillContent $_pwd$_selected
	}
    } else {
	# it's a file
	if { $_fileCmd == "Save" } {
            _saveFileCmd
	} elseif { $_fileCmd == "Open"} {
	    _openFileCmd
	} else {
	    puts "Un-supported file command"
	}
    }
}


itcl::body FitsFileselectionbox::_listFits {} {
    _fillContent $_pwd
}

itcl::body FitsFileselectionbox::_setFilter {} {
    _fillContent $_pwd
}

itcl::body FitsFileselectionbox::_helpCmd {} {
    hhelp fileSelection
}

itcl::body FitsFileselectionbox::get {} {
    if { $_isFtp } {
       # Need to call _pwd here to remove the starting directory from filename
       set r_string "[_pwd 0]$_ffbFileName"
       set r_string \
             "ftp://${_userName}:${_passwd}\@[string range $r_string 6 end]"
    } else {
       set r_string [string trim $_ffbFileName " "]

       if { [file pathtype $_ffbFileName]!="absolute" } {
          set r_string "$_pwd$_ffbFileName"
       }

    }
    return $r_string
}

itcl::body FitsFileselectionbox::_setCmd {cmd} {
    set _fileCmd $cmd
    if { $cmd == "Open" } {
	$itk_component(openB) configure -text Open 
	$itk_component(filemode) configure -state normal
    } elseif {$cmd == "Save" } { 
	$itk_component(openB) configure -text Save 
	$itk_component(filemode) configure -state disabled
    } else {
	error "Not recognized command"
    }
}

itcl::body FitsFileselectionbox::init {} {
    $itk_component(files) select clear 0 end
    if { $_isFtp } {
        # Reinitialzing, so return to local directory
        _switchRemoteLocal
    } else {
        _fillContent [_pwd]
    }
}

itcl::body FitsFileselectionbox::chgDir {dir} {
    if { $_isFtp } {
       set dir [string range $dir 6 end]
       set idx [string first / $dir]
       if { $idx==-1 } {
          set tmpdir "/"
       } else {
          set tmpdir [string range $dir $idx end]
          if { $tmpdir=="/." } {set tmpdir "."}
       }
       ftpClient cd $tmpdir
    } else {
       cd $dir
    }
    
    set _pwd [_pwd]
}

itcl::body FitsFileselectionbox::_initFtp {} {
     global g_titleFont

    _closeFTP

    RemoteClass ftpClient
# ask for user name and _passwd
    powToplevel .ftp .dummy

    iwidgets::entryfield .ftp.rhost -labeltext "Remote Host" \
	-labelpos nw -textvariable [itcl::scope _ftpHost] \
	-command  [itcl::code $this _ftpOpen] -textfont g_titleFont -labelfont g_titleFont
    pack .ftp.rhost -padx 4 -pady 4 -fill x

    iwidgets::entryfield .ftp.login -labeltext "Optional User Name (if not 'anonymous')" \
	-labelpos nw -textvariable [itcl::scope _userName] \
	-command  [itcl::code $this _ftpOpen] -textfont g_titleFont -labelfont g_titleFont
    pack .ftp.login -padx 4 -pady 4 -fill x

    iwidgets::entryfield .ftp._passwd -labeltext "Password" \
	-labelpos nw -show "\267" -textvariable [itcl::scope _passwd] \
	-command  [itcl::code $this _ftpOpen] -textfont g_titleFont -labelfont g_titleFont
    pack .ftp._passwd -padx 4 -pady 4 -fill x

    iwidgets::Buttonbox .ftp.butts -padx 4 -pady 4 -orient horizontal

    .ftp.butts add connect -text "Connect" \
          -font g_titleFont \
	  -command [itcl::code $this _ftpOpen]
    .ftp.butts add cancel -text "Cancel" \
          -font g_titleFont \
	  -command { destroy .ftp           }
    .ftp.butts default connect
    pack .ftp.butts 
    focus [.ftp.rhost component entry]

    tkwait window .ftp
}

itcl::body FitsFileselectionbox::_ftpOpen {} {
   if { $_userName=="" } {
      set _userName anonymous
   }
   if { $_passwd=="" && ($_userName=="anonymous" || $_userName=="ftp") } {
      set _passwd "fv@fv.gsfc.nasa.gov"
   }      
   setWatchCursor .ftp [itcl::code ftpClient openConn $_ftpHost $_userName $_passwd]
   set _isFtp 1
   destroy .ftp
}

itcl::body FitsFileselectionbox::_pwd { {full 1} } {
    # Always return the _dirChar at end of the _pwd string
    if { $_isFtp } {
	return [string trimright [ftpClient pwd $full] "/"]/
    } else {
	return [string trimright [pwd] $_dirChar]$_dirChar
    } 
}

itcl::body FitsFileselectionbox::_updateAll {} {
    if { $_isFtp } return
    _setFilter
}

itcl::body FitsFileselectionbox::_switchRemoteLocal {} {
    if { $_isFtp } {
	set ffbCurrentDir $_lastLocalDir
	set _isFtp 0
	$itk_component(ftpB) configure -text "FTP..."
	_closeFTP
    } else {
       _initFtp
       if { $_isFtp } {
	  set ffbCurrentDir "ftp://$_ftpHost/."
	  $itk_component(ftpB) configure -text "Local..."
       } else {
          set ffbCurrentDir $_lastLocalDir
       }
    }

    _fillContent $ffbCurrentDir
}

itcl::body FitsFileselectionbox::_closeFTP {} {
# if another ftp is in session, close it first
    if { [itcl::find objects -class RemoteClass] != "" } {
	itcl::delete object ftpClient
    } 
}

itcl::body FitsFileselectionbox::_completeFileName {} {
    if { $_ffbFileName == "" } return

    set tmpContent [lsort -increasing \
        [_pickElemByName ${_ffbFileName} $_nameList]]

    if { [llength $tmpContent] == 0 } return

    set tmpRootName [lindex $tmpContent 0]
    set tmpRootLength [string length $tmpRootName]

    set tmpList [lrange $tmpContent 1 end]
    if { [llength $tmpList ] == 0 } {
       set i [string length $tmpRootName]
    } else {
       for {set i 0} {$i < $tmpRootLength} {incr i} {
          set tmpRoot [string range $tmpRootName 0 $i]
          
          if { [_searchCharsForCompletion $tmpRoot $tmpList] == 1} {
             break
          }
       }
    }

    set _ffbFileName [string range $tmpRootName 0 [expr $i-1]]
    _showNamedFile
    $itk_component(fnamee) selection range $i end
}

itcl::body FitsFileselectionbox::_pickElemByName {name list} {
    set tmplist ""

    set last [expr [string length $name]-1]
    foreach i $list {
	if { $name==[string range $i 0 $last]} {
	    lappend tmplist $i
	}
    }
    return $tmplist
}

itcl::body FitsFileselectionbox::_searchCharsForCompletion {root list} {
    set last [expr [string length $root]-1]
    foreach i $list {
	if { $root!=[string range $i 0 $last] } {
	    return 1
	}
    }
    return 0
}


itcl::body FitsFileselectionbox::_showNamedFile {} {

    $itk_component(fnamee) icursor end
    
    set tmpIndex [lsearch -glob $_nameList ${_ffbFileName}*]
    if { $tmpIndex == -1} return
    $itk_component(files) see $tmpIndex
    $itk_component(files) select clear 0 end
    $itk_component(files) select set $tmpIndex
    _selectThat files
}

###################################################

itcl::body FitsFileselectionbox::activate { cmd {defaultName ""} } {
    init
    _setCmd $cmd
    if { $defaultName != "" } {
       set _ffbFileName $defaultName
    }
    wm deiconify $itk_component(hull)
    raise $itk_component(hull)
    focus $itk_component(hull)
    $itk_component(fnamee) selection range 0 end
    $itk_component(fnamee) icursor end
}

itcl::body FitsFileselectionbox::deactivate {} {
     _cancelCmd
}

###################################################

itcl::body FitsFileselectionbox::_cancelCmd {} {
   global fileselect

   wm withdraw $itk_component(hull)
   set fileselect ""
   checkForExit
}


itcl::body FitsFileselectionbox::_openFileCmd {} {
   global g_isScript fileselect
   set fileselect [get]
   if { $_isFtp } {
      itcl::delete object ftpClient
      if { [catch {openFitsFileWM $fileselect 1} err] } {
         # reopen ftpClient
         RemoteClass ftpClient
         setWatchCursor "" \
               [itcl::code ftpClient openConn $_ftpHost $_userName $_passwd]
         _fillContent $_pwd
         error $err
      }

   } else {

      if {$fileselect == "" } {
         error "Please select a file"
         return
      } 

      if { [file exist $fileselect] == 0 } {
         set oldFile $fileselect

         foreach ext { gz Z z zip } {
            if { [file exists $fileselect.$ext] } {
               set fileselect $fileselect.$ext
               break     
            }
         }
         if { $fileselect == $oldFile } {
            error "File $fileselect does not exist"
            return
         }
      }
      if { [file readable $fileselect] == 0 } {
         error "File $fileselect is not readable"
         return
      }
      if { [file extension $fileselect] == ".fv" } {
         set g_isScript 1
         namespace eval ::fvCmds [list source $fileselect]
         set g_isScript 0
      } elseif { [openFitsFile $fileselect] == "ERROR" } { 
         return 
      }

   }
   if { $fvPref::keepFileDialogVisible == 0 } {
      wm withdraw $itk_component(hull)
   }
}


itcl::body FitsFileselectionbox::_saveFileCmd {} {
   global fileselect

   set fileselect [get]
   set saveToDir  [file dir $fileselect]
   if {$fileselect == "" } {
      error "No file name given"
   } elseif { ![file writable $saveToDir] } {
      error "$saveToDir is not writable"
   } else {
      wm withdraw $itk_component(hull)
   }
}


###################################################

itcl::body FitsFileselectionbox::_buildMenu {} {
   global isMac

   $itk_component(dirMenu) delete 0 end
   set _pathElems [urlSplit $_pwd]
   if { $_isFtp } {
      set newP "[lindex $_pathElems 0][lindex $_pathElems 1]"
      eval lappend newP [lrange $_pathElems 2 end]
      set _pathElems $newP
   } elseif { $isMac } {
      set _pathElems [linsert $_pathElems 0 Desktop]
   }

   if { !$isMac || $_isFtp } {
      set pathSym $_dirChar
   } else {
      set pathSym ""
   }

   set c 0
   foreach i $_pathElems {
      if { $i==$pathSym } { set i "" }
      $itk_component(dirMenu) add command -label $i$pathSym \
            -command [itcl::code $this _setDir $c]
      incr c
   }
   $itk_component(dirBtn) configure -text $i$pathSym
}

itcl::body FitsFileselectionbox::_setDir { n } {
   global isMac
   
   if { $_isFtp } {
      set newPath [join [lrange $_pathElems 0 $n] /]
   } elseif { $isMac } {
      if { $n==0 } {
          set newPath ""
          set _pwd ""
      } else {
          set newPath [eval file join [lrange $_pathElems 1 $n]]
      }
   } else {
      set newPath [eval file join [lrange $_pathElems 0 $n]]
   }
   _fillContent $newPath
}

itcl::body FitsFileselectionbox::_postMenu {time X Y} {

   set _postTime $time

   set x [expr $X - [winfo reqwidth $itk_component(dirMenu)]/2  ]
   set y [expr $Y - [$itk_component(dirMenu) yposition end] - 10]

   tk_popup $itk_component(dirMenu) $x $y
}


itcl::body FitsFileselectionbox::_buttonRelease {time} {
    if { [expr abs([expr $_postTime - $time])] <= 150 } {
        return -code break
    }
}

############################################################

itcl::body FitsFileselectionbox::_scrollBoxes { args } {
   foreach b $_fileCols {
      eval $itk_component($b) yview $args
   }
}

itcl::body FitsFileselectionbox::_scrollOthers { box args } {
   eval $itk_component(fscroll) set $args
   set view [$itk_component($box) yview]
   foreach b $_fileCols {
      if { $box!=$b } {
         $itk_component($b) yview moveto [lindex $view 0]
      }
   }
}

itcl::body FitsFileselectionbox::_updateBoxes { data } {
   foreach b $_fileCols d $data {
           $itk_component($b) delete 0 end
      eval $itk_component($b) insert 0 $d
   }
}

itcl::body FitsFileselectionbox::changeListFITSoption {} {
     set _displayFitsOnly $fvPref::ifDispFitsOnly
}