📄 calendar.itk
字号:
## Calendar# ----------------------------------------------------------------------# Implements a calendar widget for the selection of a date. It displays# a single month at a time. Buttons exist on the top to change the # month in effect turning th pages of a calendar. As a page is turned, # the dates for the month are modified. Selection of a date visually # marks that date. The selected value can be monitored via the # -command option or just retrieved using the get method. Methods also# exist to select a date and show a particular month. The option set# allows the calendars appearance to take on many forms.# ----------------------------------------------------------------------# AUTHOR: Mark L. Ulferts E-mail: mulferts@austin.dsccc.com# # ACKNOWLEDGEMENTS: Michael McLennan E-mail: mmclennan@lucent.com## This code is an [incr Tk] port of the calendar code shown in Michael # J. McLennan's book "Effective Tcl" from Addison Wesley. Small # modificiations were made to the logic here and there to make it a # mega-widget and the command and option interface was expanded to make # it even more configurable, but the underlying logic is the same.## @(#) $Id: calendar.itk 144 2003-02-05 10:56:26Z mdejong $# ----------------------------------------------------------------------# Copyright (c) 1997 DSC Technologies Corporation# ======================================================================# Permission to use, copy, modify, distribute and license this software # and its documentation for any purpose, and without fee or written # agreement with DSC, is hereby granted, provided that the above copyright # notice appears in all copies and that both the copyright notice and # warranty disclaimer below appear in supporting documentation, and that # the names of DSC Technologies Corporation or DSC Communications # Corporation not be used in advertising or publicity pertaining to the # software without specific, written prior permission.# # DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL # DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS # SOFTWARE.# ======================================================================## Usual options.#itk::usual Calendar { keep -background -cursor }# ------------------------------------------------------------------# CALENDAR# ------------------------------------------------------------------class iwidgets::Calendar { inherit itk::Widget constructor {args} {} itk_option define -days days Days {Su Mo Tu We Th Fr Sa} itk_option define -command command Command {} itk_option define -forwardimage forwardImage Image {} itk_option define -backwardimage backwardImage Image {} itk_option define -weekdaybackground weekdayBackground Background \#d9d9d9 itk_option define -weekendbackground weekendBackground Background \#d9d9d9 itk_option define -outline outline Outline \#d9d9d9 itk_option define -buttonforeground buttonForeground Foreground blue itk_option define -foreground foreground Foreground black itk_option define -selectcolor selectColor Foreground red itk_option define -selectthickness selectThickness SelectThickness 3 itk_option define -titlefont titleFont Font \ -*-helvetica-bold-r-normal--*-140-* itk_option define -dayfont dayFont Font \ -*-helvetica-medium-r-normal--*-120-* itk_option define -datefont dateFont Font \ -*-helvetica-medium-r-normal--*-120-* itk_option define -currentdatefont currentDateFont Font \ -*-helvetica-bold-r-normal--*-120-* itk_option define -startday startDay Day sunday public method get {{format "-string"}} ;# Returns the selected date public method select {{date_ "now"}} ;# Selects date, moving select ring public method show {{date_ "now"}} ;# Displays a specific date protected method _drawtext {canvas_ day_ date_ now_ x0_ y0_ x1_ y1_} private method _change {delta_} private method _configureHandler {} private method _redraw {} private method _days {{wmax {}}} private method _layout {time_} private method _select {date_} private method _selectEvent {date_} private method _adjustday {day_} private method _percentSubst {pattern_ string_ subst_} private variable _time {} private variable _selected {} private variable _initialized 0 private variable _offset 0}## Provide a lowercased access method for the Calendar class.# proc ::iwidgets::calendar {pathName args} { uplevel ::iwidgets::Calendar $pathName $args}## Use option database to override default resources of base classes.#option add *Calendar.width 200 widgetDefaultoption add *Calendar.height 165 widgetDefault# ------------------------------------------------------------------# CONSTRUCTOR# ------------------------------------------------------------------body iwidgets::Calendar::constructor {args} { # # Create the canvas which displays each page of the calendar. # itk_component add page { canvas $itk_interior.page } { keep -background -cursor -width -height } pack $itk_component(page) -expand yes -fill both # # Create the forward and backward buttons. Rather than pack # them directly in the hull, we'll waittill later and make # them canvas window items. # itk_component add backward { button $itk_component(page).backward \ -command [code $this _change -1] } { keep -background -cursor } itk_component add forward { button $itk_component(page).forward \ -command [code $this _change +1] } { keep -background -cursor } # # Set the initial time to now. # set _time [clock seconds] # # Bind to the configure event which will be used to redraw # the calendar and display the month. # bind $itk_component(page) <Configure> [code $this _configureHandler] # # Evaluate the option arguments. # eval itk_initialize $args}# ------------------------------------------------------------------# OPTIONS# ------------------------------------------------------------------# ------------------------------------------------------------------# OPTION: -command## Sets the selection command for the calendar. When the user # selects a date on the calendar, the date is substituted in# place of "%d" in this command, and the command is executed.# ------------------------------------------------------------------configbody iwidgets::Calendar::command {}# ------------------------------------------------------------------# OPTION: -days## The days option takes a list of values to set the text used to display the # days of the week header above the dates. The default value is # {Su Mo Tu We Th Fr Sa}.# ------------------------------------------------------------------configbody iwidgets::Calendar::days { if {$_initialized} { if {[$itk_component(page) find withtag days] != {}} { $itk_component(page) delete days _days } }}# ------------------------------------------------------------------# OPTION: -backwardimage## Specifies a image to be displayed on the backwards calendar # button. If none is specified, a default is provided.# ------------------------------------------------------------------configbody iwidgets::Calendar::backwardimage { # # If no image is given, then we'll use the default image. # if {$itk_option(-backwardimage) == {}} { # # If the default image hasn't yet been created, then we # need to create it. # if {[lsearch [image names] $this-backward] == -1} { image create bitmap $this-backward \ -foreground $itk_option(-buttonforeground) -data { #define back_width 16 #define back_height 16 static unsigned char back_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x30, 0xe0, 0x38, 0xf0, 0x3c, 0xf8, 0x3e, 0xfc, 0x3f, 0xfc, 0x3f, 0xf8, 0x3e, 0xf0, 0x3c, 0xe0, 0x38, 0xc0, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; } } # # Configure the button to use the default image. # $itk_component(backward) configure -image $this-backward # # Else, an image has been specified. First, we'll need to make sure # the image really exists before configuring the button to use it. # If it doesn't generate an error. # } else { if {[lsearch [image names] $itk_option(-backwardimage)] != -1} { $itk_component(backward) configure \ -image $itk_option(-backwardimage) } else { error "bad image name \"$itk_option(-backwardimage)\":\ image does not exist" } # # If we previously created a default image, we'll just remove it. # if {[lsearch [image names] $this-backward] != -1} { image delete $this-backward } }}# ------------------------------------------------------------------# OPTION: -forwardimage## Specifies a image to be displayed on the forwards calendar # button. If none is specified, a default is provided.# ------------------------------------------------------------------configbody iwidgets::Calendar::forwardimage { # # If no image is given, then we'll use the default image. # if {$itk_option(-forwardimage) == {}} { # # If the default image hasn't yet been created, then we # need to create it. # if {[lsearch [image names] $this-forward] == -1} { image create bitmap $this-forward \ -foreground $itk_option(-buttonforeground) -data { #define fwd_width 16 #define fwd_height 16 static unsigned char fwd_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x1c, 0x07, 0x3c, 0x0f, 0x7c, 0x1f, 0xfc, 0x3f, 0xfc, 0x3f, 0x7c, 0x1f, 0x3c, 0x0f, 0x1c, 0x07, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; } } # # Configure the button to use the default image. # $itk_component(forward) configure -image $this-forward # # Else, an image has been specified. First, we'll need to make sure # the image really exists before configuring the button to use it. # If it doesn't generate an error. # } else { if {[lsearch [image names] $itk_option(-forwardimage)] != -1} { $itk_component(forward) configure \ -image $itk_option(-forwardimage) } else { error "bad image name \"$itk_option(-forwardimage)\":\ image does not exist" } # # If we previously created a default image, we'll just remove it. # if {[lsearch [image names] $this-forward] != -1} { image delete $this-forward } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -