123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
- #
- # $Id: MkSample.tcl,v 1.3 2001/12/09 05:34:59 idiscovery Exp $
- #
- # MkSample.tcl --
- #
- # This file implements the "Sample" page in the widget demo
- #
- # This file has not been properly documented. It is NOT intended
- # to be used as an introductory demo program about Tix
- # programming. For such demos, please see the files in the
- # demos/samples directory or go to the "Samples" page in the
- # "widget demo"
- #
- #
- # Copyright (c) 1996, Expert Interface Technologies
- #
- # See the file "license.terms" for information on usage and redistribution
- # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- #
- #
- #
- set tix_demo_running 1
- set samples_dir [file join $demo_dir samples]
- set sample_filename {}
- uplevel #0 source [list [file join $samples_dir AllSampl.tcl]]
- proc MkSample {nb page} {
- global tixOption
- #----------------------------------------------------------------------
- set w [$nb subwidget $page]
- set pane [tixPanedWindow $w.pane -orient horizontal]
- pack $pane -expand yes -fill both
- set f1 [$pane add 1 -expand 1]
- set f2 [$pane add 2 -expand 3]
- $f1 config -relief flat
- $f2 config -relief flat
- # Left pane: the Tree:
- #
- set lab [label $f1.lab -text "Select a sample program:" -anchor w]
- set tree [tixTree $f1.slb \
- -options {
- hlist.selectMode single
- hlist.width 40
- }]
- $tree config \
- -command "Sample:Action $w $tree run" \
- -browsecmd "Sample:Action $w $tree browse"
- pack $lab -side top -fill x -padx 5 -pady 5
- pack $tree -side top -fill both -expand yes -padx 5
- # Right pane: the Text
- #
- set labe [tixLabelEntry $f2.lab -label "Source:" -options {
- label.anchor w
- }]
- $labe subwidget entry config -state disabled
- set stext [tixScrolledText $f2.stext]
- set f3 [frame $f2.f3]
- set run [button $f3.run -text "Run ..." \
- -command "Sample:Action $w $tree run"]
- set view [button $f3.view -text "View Source in Browser ..." \
- -command "Sample:Action $w $tree view"]
- pack $run $view -side left -fill y -pady 2
- pack $labe -side top -fill x -padx 7 -pady 2
- pack $f3 -side bottom -fill x -padx 7
- pack $stext -side top -fill both -expand yes -padx 7
- #
- # Set up the text subwidget
- set text [$stext subwidget text]
- bind $text <Up> "%W yview scroll -1 unit"
- bind $text <Down> "%W yview scroll 1 unit"
- bind $text <Left> "%W xview scroll -1 unit"
- bind $text <Right> "%W xview scroll 1 unit"
- bind $text <Tab> {focus [tk_focusNext %W]; break}
- bindtags $text "$text Text [winfo toplevel $text] all"
- $text config -bg [$tree subwidget hlist cget -bg] \
- -state disabled -font $tixOption(fixed_font) -wrap none
- $run config -state disabled
- $view config -state disabled
- global demo
- set demo(w:run) $run
- set demo(w:view) $view
- set demo(w:tree) $tree
- set demo(w:lab1) $labe
- set demo(w:stext) $stext
- set hlist [$tree subwidget hlist]
- $hlist config -separator "." -width 30 -drawbranch 0 \
- -wideselect false
- set style [tixDisplayStyle imagetext -refwindow $hlist \
- -fg $tixOption(fg) -padx 4]
- set file [tix getimage textfile]
- set folder [tix getimage openfold]
- ForAllSamples root "" \
- [list AddSampleToHList $tree $hlist $style $file $folder]
- }
- # AddSampleToHList --
- #
- # A callback from ForAllSamples. Add all the possible sample files
- # into the Tree widget.
- #
- proc AddSampleToHList {tree hlist style file folder token type text dest} {
- case $type {
- d {
- return [$hlist addchild $token -itemtype imagetext -style $style \
- -image $folder -text $text]
- }
- done {
- if {![tixStrEq $token ""]} {
- $tree setmode $token close
- $tree close $token
- }
- }
- f {
- return [$hlist addchild $token -itemtype imagetext \
- -image $file -text $text -data [list $text $dest]]
- }
- }
- }
- proc Sample:Action {w slb action args} {
- global samples demo_dir demo samples_dir
- set hlist [$slb subwidget hlist]
- set ent [$hlist info anchor]
- if {$ent == ""} {
- $demo(w:run) config -state disabled
- $demo(w:view) config -state disabled
- return
- }
- if {[$hlist info data $ent] == {}} {
- # This is just a comment
- $demo(w:run) config -state disabled
- $demo(w:view) config -state disabled
- return
- } else {
- $demo(w:run) config -state normal
- $demo(w:view) config -state normal
- }
- set theSample [$hlist info data $ent]
- set title [lindex $theSample 0]
- set prog [lindex $theSample 1]
- case $action {
- "run" {
- RunProg $title $prog
- }
- "view" {
- LoadFile [file join $samples_dir $prog]
- }
- "browse" {
- # Bring up a short description of the sample program
- # in the scrolled text about
- set text [$demo(w:stext) subwidget text]
- uplevel #0 set sample_filename [list [file join $samples_dir $prog]]
- tixWidgetDoWhenIdle ReadFileWhenIdle $text
- $demo(w:lab1) subwidget entry config -state normal
- $demo(w:lab1) subwidget entry delete 0 end
- $demo(w:lab1) subwidget entry insert end [file join $samples_dir $prog]
- $demo(w:lab1) subwidget entry xview end
- $demo(w:lab1) subwidget entry config -state disabled
- }
- }
- }
- proc RunProg {title prog} {
- global samples demo_dir demo samples_dir
- set w .[lindex [split $prog .] 0]
- set w [string tolower $w]
- if [winfo exists $w] {
- wm deiconify $w
- raise $w
- return
- }
- uplevel #0 source [list [file join $samples_dir $prog]]
- toplevel $w
- wm title $w $title
- RunSample $w
- }
- proc LoadFile {filename} {
- global tixOption
- set tmp $filename
- regsub -all . $filename _ tmp
- set w [string tolower .$tmp]
- if [winfo exists $w] {
- wm deiconify $w
- raise $w
- return
- }
- toplevel $w
- wm title $w "Source View: $filename"
- button $w.b -text Close -command "destroy $w"
- set t [tixScrolledText $w.text]
- tixForm $w.b -left 0 -bottom -0 -padx 4 -pady 4
- tixForm $w.text -left 0 -right -0 -top 0 -bottom $w.b
- $t subwidget text config -highlightcolor [$t cget -bg] -bd 2 \
- -bg [$t cget -bg] -font $tixOption(fixed_font)
- if {$filename == {}} {
- return
- }
- set text [$w.text subwidget text]
- $text config -wrap none
- ReadFile $text $filename
- }
- proc ReadFileWhenIdle {text} {
- global sample_filename
- if ![file isdir $sample_filename] {
- ReadFile $text $sample_filename
- }
- }
- proc ReadFile {text filename} {
- set oldState [$text cget -state]
- $text config -state normal
- $text delete 0.0 end
- set fd [open $filename {RDONLY}]
- $text delete 1.0 end
-
- while {![eof $fd]} {
- $text insert end [gets $fd]\n
- }
- close $fd
- $text see 1.0
- $text config -state $oldState
- }
|