#!/usr/bin/wish # PARAMETERS #--------------------------------------------- set EVENT_FILE "/dat/eventos.txt" # VARIABLES #--------------------------------------------- # Current day/month/year set Cday 0 set Cmonth 0 set Cyear 0 # event array set event(0) {} set no_events 0 set file 0 set fname $EVENT_FILE # event record, array of fields set rec(0) 0 set rec_size 0 set EditOk 0 # GENERAL PURPOSE PROCEDURES #------------------------------------------------ proc Abort message { puts "## Error ##" puts $message puts "Aborting ..." exit } proc ShowBoolean { parent args } { set b 0 foreach {lab item} $args { checkbutton $parent.$b -text $lab -variable $item pack $parent.$b -side left incr b } } proc ScrolledListbox { parent args } { frame $parent eval {listbox $parent.list \ -yscrollcommand [list $parent.sy set] \ -xscrollcommand [list $parent.sx set]} $args scrollbar $parent.sx -orient horizontal -command [list $parent.list xview] scrollbar $parent.sy -orient vertical -command [list $parent.list yview] pack $parent.sx -side bottom -fill x pack $parent.sy -side right -fill y pack $parent.list -side left -fill both -expand true return $parent.list } proc CommandEntry { name label width var args} { frame $name label $name.l -text $label -width 10 eval {entry $name.e -relief sunken -width $width -bg white -textvariable $var} $args pack $name.l -side left pack $name.e -side left pack $name -fill x return $name.e } proc Line {n} { frame $n -height 3 -borderwidth 2 -relief raised -background blue pack $n -side top -fill both -pady 5 -padx 5 } proc MakeMenu { parent opts t } { frame $parent.f pack $parent.f -side top set b 0 foreach {desc proced} $opts { if { $t=="B"} then { button $parent.f.$b -text $desc -command $proced -borderwidth 0 pack $parent.f.$b -side left } else { button $parent.f.$b -text $desc -command $proced -borderwidth 2 pack $parent.f.$b -side left -padx 3 -pady 0 } incr b } } # SPECIFIC PROCEDURES #------------------------------------------------ proc ParseAction a { global flag set flag(c) [expr [string first "c" $a] != -1] set flag(o) [expr [string first "o" $a] != -1] set flag(x) [expr [string first "x" $a] != -1] } proc BuildAction {} { global flag set r "" if { $flag(c) } then {set r ${r}c} if { $flag(o) } then {set r ${r}o} if { $flag(x) } then {set r ${r}x} return $r } proc Parse {} { #--------------- # Read file and parse it creating the data structure # global no_events event file set level 0 set line_no 1 set no_events 0 set n 0 set Y 0 while {! [eof $file]} { # Read line set l [gets $file] incr line_no if { $l == ""} continue # check if line continues if {[string range $l end end] == "\\" } then { set x 1 set l [string range $l 0 [expr [string length $l]-2 ]] } else { set x 0 } if { $Y == 1 } then { lappend event($n) "$l" if { $x } then { set Y 1 } else { set Y 0 incr n } continue } set event($n) [split $l :] # check for correct number of parameters if { [llength $event($n)] < 8 } { Abort "Incorrect number of parameters at line $line_no" } if { $x } then { set Y 1 } else { set Y 0 incr n } } set no_events $n } proc ConvertText2Rec t { global rec rec_size set d [$t get 1.0 end] if { $d == "\n" } then {set l {x}} else {set l [split $d "\n"]} set size [expr [llength $l] - 1] for {set j 0} {$j < $size } {incr j} { set rec([expr 8+$j]) [lindex $l $j] } set rec_size [expr 8 + $size ] } proc EditEvent {} { #------------------ # Call the edit window and edit the rec variable # global rec rec_size flag EventOk destroy .t toplevel .t wm title .t "Event Editor" #add some space label .t.xx -text "" pack .t.xx CommandEntry .t.ev Event 50 rec(7) CommandEntry .t.day Day 3 rec(0) CommandEntry .t.month Month 3 rec(1) CommandEntry .t.year Year 5 rec(2) Line .t.l1 CommandEntry .t.bef Before 3 rec(3) CommandEntry .t.aft After 3 rec(4) ParseAction $rec(5) frame .t.o pack .t.o -side top ShowBoolean .t.o "Ciclic" flag(c) "Once" flag(o) "Don't Show" flag(x) CommandEntry .t.cmd Command 40 rec(6) Line .t.l2 frame .t.e pack .t.e -side top label .t.e.l -text "Comments" pack .t.e.l set _event [text .t.e.ev -width 60 -height 5 -bg white \ -borderwidth 3 -relief sunken -setgrid true \ -yscrollcommand {.t.e.s set}] scrollbar .t.e.s -command {.t.e.ev yview} pack .t.e.s -side right -fill y pack .t.e.ev -side left -fill both -expand true set line 1 for {set n 8} {$n < $rec_size} {incr n; incr line} { .t.e.ev insert end $rec($n) if { $n+1 < $rec_size } then { .t.e.ev insert end \n } } frame .t.z pack .t.z -side top button .t.z.ok -text "Ok" \ -command { set EventOk 1 set rec(5) [BuildAction] ConvertText2Rec .t.e.ev destroy .t } button .t.z.cancel -text "Cancel" \ -command { set EventOk 0 destroy .t } pack .t.z.ok .t.z.cancel -padx 20 -pady 8 -side left tkwait window .t } proc CopyToRec n { #----------------- # Copy event(n) to rec. Before EditEvent # global rec rec_size event set rec_size [llength $event($n)] for {set i 0} {$i < $rec_size} {incr i} { set rec($i) [lindex $event($n) $i] } } proc CopyFromRec n { #------------------- # Copy rec to event(n). After EditEvent # global rec rec_size event set event($n) {} for {set i 0} {$i < $rec_size} {incr i} { lappend event($n) $rec($i) } } # MENU PROCEDURES #------------------------------------------------ proc Ev_Add {} { global Cday Cmonth Cyear rec rec_size EventOk no_events set rec(0) $Cday set rec(1) $Cmonth set rec(2) $Cyear set rec(3) "0" set rec(4) "0" set rec(5) "" set rec(6) "" set rec(7) "Event here" set rec_size 8 EditEvent if { $EventOk } { CopyFromRec $no_events .fr.box.list insert end " $rec(7)" incr no_events } } proc Ev_Edit {} { global event rec EventOk no_events set i [.fr.box.list index active] CopyToRec $i EditEvent if { $EventOk } { CopyFromRec $i .fr.box.list delete $i .fr.box.list insert $i " $rec(7)" .fr.box.list activate $i } } proc Ev_Delete {} { set i [.fr.box.list index active] set d [.fr.box.list get $i] .fr.box.list delete $i if { [string index $d 0] == "(" } then { set d " [string range $d 4 end]" } else { set d "(D) [string range $d 1 end]" } .fr.box.list insert $i $d .fr.box.list activate [expr $i + 1] } proc Ev_Write {} { global event no_events file fname if [catch "open $fname w" file] {Abort "File $fname could not be created"} set i 0 while { $i < $no_events } { set d [.fr.box.list get $i] if { [string index $d 0] == "(" } { incr i; continue } for {set k 0} {$k < 7} {incr k} { puts -nonewline $file [lindex $event($i) $k]: } puts -nonewline $file [lindex $event($i) $k] incr k set s [llength $event($i)] while { $k < $s } { puts $file "\\" puts -nonewline $file [lindex $event($i) $k] incr k } puts $file "" incr i } close $file } # MODULES #------------------------------------------------ proc Init_Vars {} { global Cday Cmonth Cyear fname set number [clock seconds] set Cday [clock format $number -format "%d"] set Cmonth [clock format $number -format "%m"] set Cyear [clock format $number -format "%Y"] } proc Open_Event_File {} { global file fname if [catch "open $fname r" file] then { Abort "File $fname not found" } } proc About {} { destroy .about toplevel .about wm title .about "About" label .about.label -text \ "\nAGENDA 2.0\n\nhttp://www.geocities.com/Silicon/Valley/6333/Agenda\ \n\nRui Anastacio (ranastacio@mail.telepac.pt)\nMarch 1998\n" \ -borderwidth 0 pack .about.label -padx 6 Line .about.sep button .about.ok -text "Ok" -command { destroy .about } pack .about.ok -side top -pady 4 } proc Manage_Events {} { global event no_events ScrolledListbox .fr.box -width 50 -height 25 for {set n 0} {$n < $no_events} {incr n} { .fr.box.list insert end " [lindex $event($n) 7]" } pack .fr.box frame .fr.cmds set menu_opts {"Edit" Ev_Edit "Add" Ev_Add "Mark/Unmark as delete" Ev_Delete "Write to file" Ev_Write} # Line .fr.l1 10 MakeMenu .fr.cmds $menu_opts x pack .fr.cmds -pady 10 } proc Build_GUI {} { wm title . "Agenda 2.0" frame .menubar button .menubar.about -text About -command About -borderwidth 0 button .menubar.quit -text Quit -command exit -borderwidth 0 pack .menubar.about .menubar.quit -side right pack .menubar -side top -fill x frame .fr pack .fr -side top } # MAIN #------------------------------------------------ Init_Vars Open_Event_File Parse Build_GUI Manage_Events