📄 timefield.itk
字号:
# # 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 } else { set newclicks [::clock scan "$prevtime 1 $_cfield ago"] show [::clock format $newclicks -format $_formatString] } } Tab { # # A tab key moves the "hour:minute:second" field forward by one unless # the current field is the second. In that case we'll let tab # do what is supposed to and pass the focus onto the next widget. # if {$state == 0} { if {($itk_option(-format) == "civilian" && $_cfield == $lastField)} { _setField hour return -code continue } _moveField forward # # A ctrl-tab key moves the hour:minute:second field backwards by one # unless the current field is the hour. In that case we'll let # tab take the focus to a previous widget. # } elseif {$state == 4} { if {$_cfield == "hour"} { _setField hour return -code continue } _moveField backward } } Right { # # A right arrow key moves the insert cursor to the right one. # $_forward } Left - BackSpace - Delete { # # A left arrow, backspace, or delete key moves the insert cursor # to the left one. This is what you expect for the left arrow # and since the whole widget always operates in overstrike mode, # it makes the most sense for backspace and delete to do the same. # $_backward } Return { # # A Return key invokes the optionally specified command option. # uplevel #0 $itk_option(-command) } default { } } return -code break}# ------------------------------------------------------------------# PROTECTED METHOD: _toggleAmPm## Internal method which toggles the displayed time# between "AM" and "PM" when format is "civilian".# ------------------------------------------------------------------body iwidgets::Timefield::_toggleAmPm {} { set firstChar [string index $_timeVar 9] $itk_component(time) delete 9 10 $itk_component(time) insert 9 [expr {($firstChar == "A") ? "P" : "A"}] $itk_component(time) icursor 9 set _timeVar [$itk_component(time) get]}# ------------------------------------------------------------------# PROTECTED METHOD: _setField field## Adjusts the current field to be that of the argument, setting the# insert cursor appropriately.# ------------------------------------------------------------------body iwidgets::Timefield::_setField {field} { # Move the position of the cursor to the first character of the # field given by the argument: # # Field First Character Index # ----- --------------------- # hour 0 # minute 3 # second 6 # ampm 9 # switch $field { hour { $itk_component(time) icursor 0 } minute { $itk_component(time) icursor 3 } second { $itk_component(time) icursor 6 } ampm { if {$itk_option(-format) == "military"} { error "bad field: \"$field\", must be hour, minute or second" } $itk_component(time) icursor 9 } default { if {$itk_option(-format) == "military"} { error "bad field: \"$field\", must be hour, minute or second" } else { error "bad field: \"$field\", must be hour, minute, second or ampm" } } } set _cfield $field return $_cfield}# ------------------------------------------------------------------# PROTECTED METHOD: _moveField## Moves the cursor one field forward or backward.# ------------------------------------------------------------------body iwidgets::Timefield::_moveField {direction} { # Since the value "_fields" list variable is always either value: # military => {hour minute second} # civilian => {hour minute second ampm} # # the index of the previous or next field index can be determined # by subtracting or adding 1 to current the index, respectively. # set index [lsearch $_fields $_cfield] expr {($direction == "forward") ? [incr index] : [incr index -1]} if {$index == $_numFields} { set index 0 } elseif {$index < 0} { set index [expr $_numFields-1] } _setField [lindex $_fields $index]}# ------------------------------------------------------------------# PROTECTED METHOD: _whichField## Returns the current field that the cursor is positioned within.# ------------------------------------------------------------------body iwidgets::Timefield::_whichField {} { # Return the current field based on the position of the cursor. # # Field Index # ----- ----- # hour 0,1 # minute 3,4 # second 6,7 # ampm 9,10 # set icursor [$itk_component(time) index insert] switch $icursor { 0 - 1 { set _cfield hour } 3 - 4 { set _cfield minute } 6 - 7 { set _cfield second } 9 - 10 { set _cfield ampm } } return $_cfield}# ------------------------------------------------------------------# PROTECTED METHOD: _forwardCivilian## Internal method which moves the cursor forward by one character# jumping over the slashes and wrapping.# ------------------------------------------------------------------body iwidgets::Timefield::_forwardCivilian {} { # # If the insertion cursor is at the second digit # of either the hour, minute or second field, then # move the cursor to the first digit of the right-most field. # # else move the insertion cursor right one character # set icursor [$itk_component(time) index insert] switch $icursor { 1 { _setField minute } 4 { _setField second } 7 { _setField ampm } 9 - 10 { _setField hour } default { $itk_component(time) icursor [expr $icursor+1] } }}# ------------------------------------------------------------------# PROTECTED METHOD: _forwardMilitary## Internal method which moves the cursor forward by one character# jumping over the slashes and wrapping.# ------------------------------------------------------------------body iwidgets::Timefield::_forwardMilitary {} { # # If the insertion cursor is at the second digit of either # the hour, minute or second field, then move the cursor to # the first digit of the right-most field. # # else move the insertion cursor right one character # set icursor [$itk_component(time) index insert] switch $icursor { 1 { _setField minute } 4 { _setField second } 7 { _setField hour } default { $itk_component(time) icursor [expr $icursor+1] } }}# ------------------------------------------------------------------# PROTECTED METHOD: _backwardCivilian## Internal method which moves the cursor backward by one character# jumping over the ":" and wrapping.# ------------------------------------------------------------------body iwidgets::Timefield::_backwardCivilian {} { # # If the insertion cursor is at the first character # of either the minute or second field or at the ampm # field, then move the cursor to the second character # of the left-most field. # # else if the insertion cursor is at the first digit of the # hour field, then move the cursor to the first character # of the ampm field. # # else move the insertion cursor left one character # set icursor [$itk_component(time) index insert] switch $icursor { 9 { _setField second $itk_component(time) icursor 7 } 6 { _setField minute $itk_component(time) icursor 4 } 3 { _setField hour $itk_component(time) icursor 1 } 0 { _setField ampm $itk_component(time) icursor 9 } default { $itk_component(time) icursor [expr $icursor-1] } }}# ------------------------------------------------------------------# PROTECTED METHOD: _backwardMilitary## Internal method which moves the cursor backward by one character# jumping over the slashes and wrapping.# ------------------------------------------------------------------body iwidgets::Timefield::_backwardMilitary {} { # # If the insertion cursor is at the first digit of either # the minute or second field, then move the cursor to the # second character of the left-most field. # # else if the insertion cursor is at the first digit of the # hour field, then move the cursor to the second digit # of the second field. # # else move the insertion cursor left one character # set icursor [$itk_component(time) index insert] switch $icursor { 6 { _setField minute $itk_component(time) icursor 4 } 3 { _setField hour $itk_component(time) icursor 1 } 0 { _setField second $itk_component(time) icursor 7 } default { $itk_component(time) icursor [expr $icursor-1] } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -