📄 timefield.itk
字号:
# string or as an integer clock value using the -string and -clicks# options respectively. The default is by string. Reference the # clock command for more information on obtaining times and their # formats.# ------------------------------------------------------------------itcl::body iwidgets::Timefield::get {{format "-string"}} { set _timeVar [$itk_component(time) get] switch -- $format { "-string" { return $_timeVar } "-clicks" { return [::clock scan $_timeVar -gmt $itk_option(-gmt)] } default { error "bad format option \"$format\":\ should be -string or -clicks" } }}# ------------------------------------------------------------------# PUBLIC METHOD: show time## Changes the currently displayed time to be that of the time # argument. The time may be specified either as a string or an# integer clock value. Reference the clock command for more # information on obtaining times and their formats.# ------------------------------------------------------------------itcl::body iwidgets::Timefield::show {{time "now"}} { set icursor [$itk_component(time) index insert] if {$time == {}} { set time "now" } switch -regexp -- $time { {^now$} { set seconds [::clock seconds] } {^[0-9]+$} { if { [catch {::clock format $time -gmt $itk_option(-gmt)}] } { error "bad time: \"$time\", must be a valid time \ string, clock clicks value or the keyword now" } set seconds $time } default { if {[catch {set seconds [::clock scan $time -gmt $itk_option(-gmt)]}]} { error "bad time: \"$time\", must be a valid time \ string, clock clicks value or the keyword now" } } } set _timeVar [::clock format $seconds -format $_formatString \ -gmt $itk_option(-gmt)] $itk_component(time) delete 0 end $itk_component(time) insert end $_timeVar $itk_component(time) icursor $icursor return $_timeVar}# ------------------------------------------------------------------# PUBLIC METHOD: isvalid## Returns a boolean indication of the validity of the currently# displayed time value. For example, 09:59::59 is valid whereas# 26:59:59 is invalid.# ------------------------------------------------------------------itcl::body iwidgets::Timefield::isvalid {} { set _timeVar [$itk_component(time) get] return [expr {([catch {::clock scan $_timeVar -gmt $itk_option(-gmt)}] == 0)}]}# ------------------------------------------------------------------# PROTECTED METHOD: _focusIn## This method is bound to the <FocusIn> event. It resets the # insert cursor and field settings to be back to their last known# positions.# ------------------------------------------------------------------itcl::body iwidgets::Timefield::_focusIn {} { _setField $_cfield}# ------------------------------------------------------------------# PROTECTED METHOD: _keyPress ## This method is the workhorse of the class. It is bound to the# <KeyPress> event and controls the processing of all key strokes.# ------------------------------------------------------------------itcl::body iwidgets::Timefield::_keyPress {char sym state} { # # Determine which field we are in currently. This is needed # since the user may have moved to this position via a mouse # selection and so it would not be in the position we last # knew it to be. # set _cfield [_whichField ] # # Set up a few basic variables we'll be needing throughout the # rest of the method such as the position of the insert cursor # and the currently displayed minute, hour, and second. # set inValid 0 set icursor [$itk_component(time) index insert] set lastField [lindex $_fields end] set prevtime $_timeVar regexp {^([0-9])([0-9]):([0-9])([0-9]):([0-9])([0-9]).*$} \ $_timeVar dummy \ hour1 hour2 minute1 minute2 second1 second2 set hour "$hour1$hour2" set minute "$minute1$minute2" set second "$second1$second2" # # Process numeric keystrokes. This involes a fair amount of # processing with step one being to check and make sure we # aren't attempting to insert more that 6 characters. If # so ring the bell and break. # if {![catch {expr {int($char)}}]} { # If we are currently in the hour field then we process the # number entered based on the cursor position. If we are at # at the first position and our iq is low, then accept any # input. # # if the current format is military, then # validate the hour field which can be [00 - 23] # switch $_cfield { hour { if {$itk_option(-iq) == "low"} { $itk_component(time) delete $icursor $itk_component(time) insert $icursor $char } elseif {$itk_option(-format) == "military"} { if {$icursor == 0} { # # if the digit is less than 2, then # the second hour digit is valid for 0-9 # if {$char < 2} { $itk_component(time) delete 0 1 $itk_component(time) insert 0 $char # # if the digit is equal to 2, then # the second hour digit is valid for 0-3 # } elseif {$char == 2} { $itk_component(time) delete 0 1 $itk_component(time) insert 0 $char if {$hour2 > 3} { $itk_component(time) delete 1 2 $itk_component(time) insert 1 "0" $itk_component(time) icursor 1 } # # if the digit is greater than 2, then # set the first hour digit to 0 and the # second hour digit to the value. # } elseif {$char > 2} { $itk_component(time) delete 0 2 $itk_component(time) insert 0 "0$char" set icursor 1 } else { set inValid 1 } # # if the insertion cursor is for the second hour digit, then # format is military, then it can only be valid if the first # hour digit is less than 2 or the new digit is less than 4 # } else { if {$hour1 < 2 || $char < 4} { $itk_component(time) delete 1 2 $itk_component(time) insert 1 $char } else { set inValid 1 } } # # The format is civilian, so we need to # validate the hour field which can be [01 - 12] # } else { if {$icursor == 0} { # # if the digit is 0, then # the second hour digit is valid for 1-9 # so just insert it. # if {$char == 0 && $hour2 != 0} { $itk_component(time) delete 0 1 $itk_component(time) insert 0 $char # # if the digit is equal to 1, then # the second hour digit is valid for 0-2 # } elseif {$char == 1} { $itk_component(time) delete 0 1 $itk_component(time) insert 0 $char if {$hour2 > 2} { $itk_component(time) delete 1 2 $itk_component(time) insert 1 0 set icursor 1 } # # if the digit is greater than 1, then # set the first hour digit to 0 and the # second hour digit to the value. # } elseif {$char > 1} { $itk_component(time) delete 0 2 $itk_component(time) insert 0 "0$char" set icursor 1 } else { set inValid 1 } # # The insertion cursor is at the second hour digit, so # it can only be valid if the firs thour digit is 0 # or the new digit is less than or equal to 2 # } else { if {$hour1 == 0 || $char <= 2} { $itk_component(time) delete 1 2 $itk_component(time) insert 1 $char } else { set inValid 1 } } } if {$inValid} { bell } elseif {$icursor == 1} { _setField minute } } minute { if {$itk_option(-iq) == "low" || $char < 6 || $icursor == 4} { $itk_component(time) delete $icursor $itk_component(time) insert $icursor $char } elseif {$itk_option(-iq) == "high"} { if {$char > 5} { $itk_component(time) delete 3 5 $itk_component(time) insert 3 "0$char" set icursor 4 } } if {$icursor == 4} { _setField second } } second { if {$itk_option(-iq) == "low" || $char < 6 || $icursor == 7} { $itk_component(time) delete $icursor $itk_component(time) insert $icursor $char } elseif {$itk_option(-iq) == "high"} { if {$char > 5} { $itk_component(time) delete 6 8 $itk_component(time) insert 6 "0$char" set icursor 7 } } if {$icursor == 7} { _moveField forward } } } set _timeVar [$itk_component(time) get] return -code break } # # Process the plus and the up arrow keys. They both yield the same # effect, they increment the minute by one. # switch $sym { p - P { if {$itk_option(-format) == "civilian"} { $itk_component(time) delete 9 10 $itk_component(time) insert 9 P _setField hour } } a - A { if {$itk_option(-format) == "civilian"} { $itk_component(time) delete 9 10 $itk_component(time) insert 9 A _setField hour } } plus - Up { if {$_cfield == "ampm"} { _toggleAmPm } else { set newclicks [::clock scan "$prevtime 1 $_cfield"] show [::clock format $newclicks -format $_formatString] } } minus - Down { # # Process the minus and the down arrow keys which decrement the value # of the field in which the cursor is currently positioned. # if {$_cfield == "ampm"} { _toggleAmPm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -