home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / local / bin / agenda < prev    next >
Encoding:
Tcl/Tk script  |  2004-07-24  |  8.9 KB  |  432 lines

  1. #!/usr/bin/wish
  2.  
  3. #                 PARAMETERS
  4. #---------------------------------------------
  5.  
  6. #set EVENT_FILE "/dat/eventos.txt"
  7. set EVENT_FILE "/root/.events"
  8.  
  9. #                  VARIABLES
  10. #---------------------------------------------
  11.  
  12. # Current day/month/year
  13. set Cday 0
  14. set Cmonth 0
  15. set Cyear 0
  16.  
  17. # event array
  18. set event(0) {}
  19. set no_events 0
  20.  
  21. set file  0
  22. set fname $EVENT_FILE
  23.  
  24. # event record, array of fields
  25. set rec(0) 0
  26. set rec_size 0
  27.  
  28. set EditOk 0
  29.  
  30.  
  31. #            GENERAL PURPOSE PROCEDURES
  32. #------------------------------------------------
  33.  
  34. proc Abort message {
  35.   puts "## Error ##"
  36.   puts $message
  37.   puts "Aborting ..."
  38.   exit 
  39. }
  40.  
  41.  
  42. proc ShowBoolean { parent args } {
  43.  set b 0
  44.  foreach {lab item} $args {
  45.    checkbutton $parent.$b  -text $lab -variable $item 
  46.    pack $parent.$b -side left
  47.    incr b
  48.  }
  49. }
  50.  
  51.  
  52. proc ScrolledListbox { parent args } {
  53.   frame $parent
  54.   eval {listbox $parent.list \
  55.     -yscrollcommand [list $parent.sy set]  \
  56.     -xscrollcommand [list $parent.sx set]} $args
  57.   scrollbar $parent.sx -orient horizontal   -command [list $parent.list xview]
  58.   scrollbar $parent.sy -orient vertical     -command [list $parent.list yview]
  59.   pack $parent.sx -side bottom -fill x
  60.   pack $parent.sy -side right  -fill y
  61.   pack $parent.list -side left -fill both -expand true
  62.   return $parent.list
  63. }
  64.  
  65.  
  66. proc CommandEntry { name label width var args} {
  67.   frame $name
  68.   label $name.l -text $label -width 10
  69.   eval {entry $name.e -relief sunken -width $width -bg white -textvariable $var} $args
  70.   pack $name.l -side left
  71.   pack $name.e -side left 
  72.   pack  $name -fill x
  73.   return $name.e
  74. }  
  75.  
  76.  
  77. proc Line {n} {
  78.  frame $n -height 3 -borderwidth 2 -relief raised -background blue
  79.  pack $n -side top -fill both -pady 5 -padx 5
  80.  
  81.  
  82. proc MakeMenu { parent opts t } {
  83.  frame $parent.f
  84.  pack $parent.f -side top
  85.  set b 0
  86.  foreach {desc proced} $opts {
  87.   if { $t=="B"} then {
  88.     button $parent.f.$b -text $desc -command $proced -borderwidth 0
  89.     pack   $parent.f.$b -side left
  90.   } else {
  91.     button $parent.f.$b -text $desc -command $proced -borderwidth 2
  92.     pack   $parent.f.$b -side left -padx 3 -pady 0
  93.   }
  94.   incr b
  95.  }
  96. }
  97.  
  98.  
  99.  
  100. #             SPECIFIC PROCEDURES
  101. #------------------------------------------------
  102.  
  103. proc ParseAction a {
  104.   global flag
  105.   set flag(c) [expr [string first "c" $a] != -1]
  106.   set flag(o) [expr [string first "o" $a] != -1]
  107.   set flag(x) [expr [string first "x" $a] != -1]
  108. }
  109.  
  110.  
  111. proc BuildAction {} {
  112.   global flag
  113.   set r ""
  114.   if { $flag(c) } then {set r ${r}c}
  115.   if { $flag(o) } then {set r ${r}o}
  116.   if { $flag(x) } then {set r ${r}x}
  117.   return $r
  118. }
  119.  
  120.  
  121.  
  122. proc Parse {} {
  123. #---------------
  124. # Read file and parse it creating the data structure
  125. #
  126. global no_events event file
  127.  
  128. set level 0
  129. set line_no   1
  130. set no_events 0
  131. set n         0
  132. set Y 0
  133.  
  134. while {! [eof $file]} {
  135.  
  136. # Read line
  137.    set  l   [gets $file]
  138.    incr line_no
  139.    if { $l == ""} continue
  140.  
  141. # check if line continues
  142.    if {[string range $l end end] == "\\" } then {
  143.       set x 1
  144.       set l  [string range $l 0 [expr [string length $l]-2 ]]
  145.    } else { set x 0 }
  146.  
  147.  
  148.   if { $Y == 1 } then {
  149.      lappend event($n)  "$l"
  150.      if { $x } then {
  151.         set Y 1
  152.      } else { 
  153.         set Y 0
  154.         incr n
  155.      }
  156.      continue
  157.   }
  158.  
  159.   set event($n) [split $l :]
  160.  
  161.   # check for correct number of parameters
  162.   if { [llength $event($n)] < 8 } {
  163.         Abort "Incorrect number of parameters at line $line_no"
  164.   }  
  165.  
  166.  if { $x } then { 
  167.     set Y 1 
  168.   } else { 
  169.     set Y 0  
  170.     incr n
  171.   }
  172.  }
  173.  set no_events $n
  174. }
  175.  
  176.  
  177.  
  178.  
  179. proc ConvertText2Rec t {
  180.   global rec rec_size
  181.   set d [$t get 1.0 end]
  182.   if { $d == "\n" } then {set l {x}}  else  {set l [split $d "\n"]}
  183.   set size [expr [llength $l] - 1]
  184.   for {set j 0} {$j < $size } {incr j} { set rec([expr 8+$j]) [lindex $l $j] }
  185.   set rec_size [expr 8 + $size ]
  186. }
  187.  
  188.  
  189.  
  190. proc EditEvent {} {
  191. #------------------
  192. # Call the edit window and edit the rec variable
  193. #
  194. global rec rec_size flag EventOk
  195.  
  196.  destroy .t
  197.  toplevel .t
  198.  wm title .t  "Event Editor"
  199.    #add some space
  200.    label .t.xx -text ""
  201.    pack  .t.xx 
  202.  CommandEntry  .t.ev    Event 50  rec(7)
  203.  CommandEntry  .t.day   Day   3   rec(0)
  204.  CommandEntry  .t.month Month 3   rec(1)
  205.  CommandEntry  .t.year  Year  5   rec(2)
  206.  Line .t.l1
  207.  CommandEntry  .t.bef   Before 3  rec(3)
  208.  CommandEntry  .t.aft   After  3  rec(4)
  209.  
  210.  ParseAction $rec(5)
  211.  frame .t.o
  212.  pack  .t.o -side top 
  213.  ShowBoolean .t.o "Ciclic" flag(c) "Once" flag(o) "Don't Show" flag(x)
  214.  
  215.  CommandEntry  .t.cmd  Command 40 rec(6)
  216.  Line .t.l2 
  217.  
  218.  frame .t.e
  219.  pack  .t.e -side top
  220.  label .t.e.l -text "Comments"
  221.  pack  .t.e.l
  222.  set _event [text .t.e.ev -width 60 -height 5 -bg white \
  223.     -borderwidth 3 -relief sunken -setgrid true \
  224.     -yscrollcommand {.t.e.s set}]
  225.  scrollbar .t.e.s -command {.t.e.ev yview}
  226.  pack .t.e.s -side right -fill y
  227.  pack .t.e.ev -side left -fill both -expand true
  228.  set line 1
  229.   
  230.  for {set n 8} {$n < $rec_size} {incr n; incr line} {
  231.     .t.e.ev insert end $rec($n)
  232.     if { $n+1 < $rec_size } then {
  233.       .t.e.ev insert end \n
  234.     }
  235.  }
  236.  
  237.  frame .t.z 
  238.  pack  .t.z -side top
  239.  button .t.z.ok  -text "Ok" \
  240.  -command { 
  241.    set EventOk 1 
  242.    set rec(5) [BuildAction]
  243.    ConvertText2Rec .t.e.ev
  244.    destroy .t 
  245.    } 
  246.  button .t.z.cancel -text "Cancel"  \
  247.   -command {
  248.    set EventOk 0
  249.    destroy .t 
  250.    }
  251.  pack   .t.z.ok .t.z.cancel -padx 20 -pady 8 -side left
  252.  tkwait window .t
  253. }
  254.  
  255.  
  256.  
  257. proc CopyToRec n {
  258. #-----------------
  259. # Copy event(n) to rec. Before EditEvent
  260. #
  261.  global rec rec_size event 
  262.  set rec_size [llength $event($n)]
  263.  for {set i 0} {$i < $rec_size} {incr i} { set  rec($i)  [lindex $event($n) $i] }
  264. }
  265.  
  266.  
  267.  
  268. proc CopyFromRec n {
  269. #-------------------
  270. # Copy rec to event(n). After EditEvent
  271. #
  272.  global rec rec_size event
  273.  set event($n) {}
  274.  for {set i 0} {$i < $rec_size} {incr i} { lappend event($n) $rec($i) }
  275. }
  276.  
  277.  
  278.  
  279. #               MENU PROCEDURES
  280. #------------------------------------------------
  281.  
  282. proc Ev_Add {} {
  283.  global Cday Cmonth Cyear rec rec_size EventOk no_events
  284.  set  rec(0) $Cday
  285.  set  rec(1) $Cmonth
  286.  set  rec(2) $Cyear
  287.  set  rec(3) "0"
  288.  set  rec(4) "0"
  289.  set  rec(5) ""
  290.  set  rec(6) ""
  291.  set  rec(7) "Event here"
  292.  set  rec_size 8
  293.  EditEvent
  294.  if { $EventOk } {
  295.     CopyFromRec $no_events
  296.     .fr.box.list insert end " $rec(7)"
  297.     incr no_events
  298.  }
  299. }     
  300.  
  301.  
  302.  
  303. proc Ev_Edit {} {
  304.  global event rec EventOk no_events
  305.  set i [.fr.box.list index active]
  306.  CopyToRec $i
  307.  EditEvent
  308.  if { $EventOk } {
  309.     CopyFromRec $i
  310.     .fr.box.list delete $i
  311.     .fr.box.list insert $i  " $rec(7)"
  312.     .fr.box.list activate $i
  313.  }
  314. }
  315.  
  316.  
  317.  
  318. proc Ev_Delete {} {
  319.  set i [.fr.box.list index active]
  320.  set d [.fr.box.list get $i]
  321.  .fr.box.list delete $i
  322.  if { [string index $d 0] == "(" } then {
  323.    set d " [string range $d 4 end]"
  324.  } else { set d "(D) [string range $d 1 end]"  }
  325.  .fr.box.list insert $i  $d
  326.  .fr.box.list activate [expr $i + 1]
  327. }
  328.  
  329.  
  330.  
  331. proc Ev_Write {} {
  332.  global event no_events file fname
  333.  
  334.  if [catch "open $fname w" file] {Abort "File $fname could not be created"}
  335.  set i 0
  336.  while { $i < $no_events } {
  337.    set d [.fr.box.list get $i] 
  338.    if { [string index $d 0] == "(" } { incr i; continue }
  339.  
  340.    for {set k 0} {$k < 7} {incr k} {
  341.      puts -nonewline $file [lindex $event($i) $k]:
  342.    }
  343.    puts -nonewline $file [lindex $event($i) $k]
  344.    incr k
  345.    set s [llength $event($i)]
  346.  
  347.    while { $k < $s } {
  348.       puts $file "\\"
  349.       puts -nonewline $file [lindex $event($i) $k]
  350.       incr k
  351.    }
  352.    puts $file ""
  353.    incr i
  354.   }      
  355.  close $file  
  356. }
  357.  
  358.  
  359.  
  360. #                  MODULES
  361. #------------------------------------------------
  362.  
  363. proc Init_Vars {} {
  364.  global Cday Cmonth Cyear fname
  365.  set number [clock seconds]
  366.  set Cday   [clock format $number -format "%d"]
  367.  set Cmonth [clock format $number -format "%m"]
  368.  set Cyear  [clock format $number -format "%Y"]
  369. }
  370.  
  371.  
  372. proc Open_Event_File {} {
  373.  global file fname
  374.  if [catch "open $fname r" file] then { Abort "File $fname not found" }
  375. }
  376.  
  377.  
  378. proc About {} {
  379.  destroy .about
  380.  toplevel  .about
  381.  wm title .about "About"
  382.  label .about.label -text \
  383.     "\nAGENDA 2.0\n\nhttp://www.geocities.com/Silicon/Valley/6333/Agenda\
  384. \n\nRui Anastacio (ranastacio@mail.telepac.pt)\nMarch 1998\n" \
  385.     -borderwidth 0
  386.  pack  .about.label -padx 6
  387.  Line .about.sep
  388.  button .about.ok -text "Ok" -command { destroy .about }
  389.  pack .about.ok -side top -pady 4
  390. }
  391.  
  392.  
  393.  
  394. proc Manage_Events {} {
  395.   global event no_events
  396.   ScrolledListbox .fr.box -width 50 -height 25
  397.   for  {set n 0} {$n < $no_events} {incr n} {
  398.      .fr.box.list insert end " [lindex $event($n) 7]"
  399.   }
  400.   pack .fr.box
  401.   frame .fr.cmds
  402.   set menu_opts {"Edit" Ev_Edit  "Add"  Ev_Add  "Mark/Unmark as delete" Ev_Delete "Write to file" Ev_Write}
  403. #  Line .fr.l1 10
  404.   MakeMenu .fr.cmds  $menu_opts x
  405.   pack .fr.cmds -pady 10
  406. }
  407.  
  408.  
  409.  
  410. proc Build_GUI {} {
  411.  wm title . "Agenda 2.0"
  412.  frame  .menubar
  413.  button .menubar.about  -text About  -command About  -borderwidth 0
  414.  button .menubar.quit   -text Quit   -command exit   -borderwidth 0
  415.  pack   .menubar.about .menubar.quit -side right
  416.  pack   .menubar -side top -fill x
  417.  frame  .fr
  418.  pack   .fr -side top
  419. }
  420.  
  421.  
  422.  
  423. #                  MAIN 
  424. #------------------------------------------------
  425.  
  426. Init_Vars
  427. Open_Event_File
  428. Parse
  429. Build_GUI
  430. Manage_Events
  431.