]> www.wagner.pp.ru Git - oss/fubar.git/blob - plugins/clock
Reimport after CVS crash
[oss/fubar.git] / plugins / clock
1 #
2 # Clock plugin for fubar
3 # Shows current time and pops up calendar for current month
4 #
5
6 option add *Calenar.Label.Font -*-times-bold-r-normal--12-*-*-*-*-*-iso10646-1 widgetDefault 
7 option add *Calendar.Canvas.BoldFont -*-times-bold-r-normal--12-*-*-*-*-*-iso10646-1 widgetDefault
8 option add *Calendar.Canvas.DateFont -*-times-medium-r-normal--12-*-*-*-*-iso10646-1 widgetDefault
9 namespace eval clock {
10 array set monthNames { 
11 1 {ÑÎ×ÁÒÑ ÑÎ×ÁÒØ}
12 2 {ÆÅ×ÒÁÌÑ ÆÅ×ÒÁÌØ}
13 3 {ÍÁÒÔÁ ÍÁÒÔ}
14 4 {ÁÐÒÅÌÑ ÁÐÒÅÌØ}
15 5 {ÍÁÑ ÍÁÊ}
16 6 {ÉÀÎÑ ÉÀÎØ}
17 7 {ÉÀÌÑ ÉÀÌØ}
18 8 {Á×ÇÕÓÔÁ Á×ÇÕÓÔ}
19 9 {ÓÅÎÔÑÂÒÑ ÓÅÎÔÑÂÒØ}
20 10 {ÏËÔÑÂÒÑ ÏËÔÑÂÒØ}
21 11 {ÎÏÑÂÒÑ ÎÏÑÂÒØ}
22 12 {ÄÅËÁÂÒÑ ÄÅËÁÂÒØ}
23 }
24 array set weekday {
25 0 {×ÏÓËÒÅÓÅÎØÅ red}
26 1 {ÐÏÎÅÄÅÌØÎÉË black}
27 2 {×ÔÏÒÎÉË black}
28 3 {ÓÒÅÄÁ  black}
29 4 {ÞÅÔ×ÅÒÇ black}
30 5 {ÐÑÔÎÉÃÁ black}
31 6 {ÓÕÂÂÏÔÁ red}
32 }
33 set weekDayAbbr {÷Ó ðΠ÷Ô óÒ þÔ ðÔ óÂ}
34 proc currentMonth {} {
35        eval fill_calendar [clock format [clock seconds] -format "%m %Y"]
36 }          
37 proc calendar {} {
38         catch {destroy .calendar}
39     popup .calendar -bd 3 -relief raised -class Calendar
40     label .calendar.title -anchor n 
41     canvas .calendar.m -width 150 -height 120
42     grid .calendar.title - - -sticky news
43     grid .calendar.m - - -sticky news
44     .calendar.m bind date <1> {::clock::showEvents [::clock::getCalendarDate %x %y]}
45     button .calendar.prev -text "<<" -command "::clock::other_month -1"
46     button .calendar.next -text ">>" -command "::clock::other_month [expr 32*86400]"
47     button .calendar.cur -text "Current" -command ::clock::currentMonth
48     grid .calendar.prev .calendar.cur .calendar.next -sticky ns
49     grid .calendar.prev -sticky nws
50     grid .calendar.next -sticky nes 
51     eval fill_calendar [clock format [clock seconds] -format "%m %Y"]
52 }
53
54 proc other_month {offset} {
55    variable firstOfMonth 
56    eval fill_calendar [clock format [expr {$firstOfMonth +$offset}] -format "%m %Y" -gmt y]
57 }
58
59 proc fill_calendar {month year} {
60    variable firstOfMonth 
61    variable holydays
62    variable weekDayAbbr
63    variable monthNames
64    variable weekday
65    variable holydaysStamp
66    global CONFIGDIR
67    if {[file exist $CONFIGDIR/holydays]&&
68           (![info exist holydaysStamp]||
69           [file mtime $CONFIGDIR/holydays]>$holydaysStamp)} {
70        read_holydays
71    }    
72    set month [string trimleft $month 0]
73    set firstOfMonth [clock scan "$month/01/$year" -gmt y] 
74    set t $firstOfMonth
75    set y 1
76    set x 25 
77    set color red
78    
79    .calendar.m delete all
80    foreach w $weekDayAbbr { #<=List of abbreviated weekdays here
81       .calendar.m create text $x $y -anchor ne -text $w -fill $color \
82            -font [option get .calendar.m boldFont BoldFont] 
83       set color black
84       incr x 19
85    }   
86    incr y 18
87    .calendar.title configure -text  "[lindex $monthNames($month) 1] $year"
88    for {set t $firstOfMonth} {[string trimleft \
89          [clock format $t -format "%m"] 0]==$month} {incr t 86400} {
90         set day  [clock format $t -format "%e" -gmt y] 
91         set wd  [clock format $t -format "%w" -gmt y] 
92         set x [expr $wd*19+25]
93         if {!$wd&&$day>1} { incr y 18 }
94         if [info exist holydays([string trimleft $day]/$month)] {
95               set color $holydays([string trimleft $day]/$month)
96         } else  {
97            set color [lindex $weekday($wd) 1]
98         }
99         .calendar.m create text $x $y -anchor ne -text $day -fill $color \
100             -font [option get .calendar.m dateFont DateFont]  -tags [list date d[string trimleft $day]]
101    }    
102    if {"[string trimleft [clock format [clock seconds] -format "%m-%Y"] 0]"
103        == "$month-$year"} {
104       set box [.calendar.m bbox\
105        "d[string trimleft [clock format [clock seconds] -format "%d"] 0]" ] 
106       if [llength $box] { 
107       eval .calendar.m create rectangle $box -fill green -outline green -tags today
108       .calendar.m lower today
109       }
110    }   
111       
112 }
113
114 proc getCalendarDate {x y} {
115  variable firstOfMonth
116  set dateId [.calendar.m find closest $x $y]
117  set day [.calendar.m itemcget $dateId -text]
118  return "[clock format $firstOfMonth -format %m]/$day/[clock format $firstOfMonth -format %y] [clock format [clock seconds] -format "%H:%M"]"  
119 }
120 proc read_holydays {} {
121     variable holydays 
122         variable holydaysStamp 
123         global CONFIGDIR
124     if {![catch {open $CONFIGDIR/holydays} f]} {
125        array set holydays [read $f]
126        close $f
127     } else {
128       set holydaysStamp 0
129       exit
130     }  
131     set holydaysStamp [file mtime $CONFIGDIR/holydays] 
132 }
133
134 proc showtime {seconds} {
135     variable TimeVar
136     set TimeVar [clock format $seconds -format "%H:%M"]
137     balloonhelp .clock [clock format $seconds] 
138 }
139
140 proc showEvents {date} {
141         if {![winfo exists .events]} {
142                 toplevel .events -class Calendar
143                 label .events.l -width 20
144                 pack .events.l
145                 button .events.ok -text Ok -command {wm withdraw .events}
146                 pack .events.ok
147                 wm protocol .events WM_DELETE_WINDOW {wm withdraw .events}
148         }       
149         .events.l configure -text $date
150 }       
151
152 #
153 # Setup
154 #
155 calendar
156 catch {destroy .clock}
157 catch {destroy .events}
158 button .clock -textvar ::clock::TimeVar -width 7 -bd 1 -relief sunken\
159                 -command {::clock::currentMonth 
160                                         show_popup .calendar .clock}
161 balloonhelp .clock "Click to view calendar"
162 pack .clock -side right -padx 5
163 showtime [clock seconds]
164 notifier ::clock::showtime
165 }