summaryrefslogtreecommitdiff
path: root/itcl/iwidgets3.0.0/generic/timefield.itk
blob: c9b8c54c437fc0dd2fe42793a5f161dbae7e16d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
#
# Timefield
# ----------------------------------------------------------------------
# Implements a time entry field with adjustable built-in intelligence
# levels.
# ----------------------------------------------------------------------
#   AUTHOR:  John A. Tucker          E-mail: jatucker@austin.dsccc.com
#
#   @(#) $Id$
# ----------------------------------------------------------------------
#            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.
# ======================================================================

#
# Use option database to override default resources of base classes.
#
option add *Timefield.justify center widgetDefault


#
# Usual options.
#
itk::usual Timefield {
    keep -background -borderwidth -cursor -foreground -highlightcolor \
       -highlightthickness -labelfont -textbackground -textfont
}

# ------------------------------------------------------------------
#                               TIMEFIELD
# ------------------------------------------------------------------
class iwidgets::Timefield {

    inherit iwidgets::Labeledwidget 
    
    constructor {args} {}

    itk_option define -childsitepos childSitePos Position e
    itk_option define -command command Command {}
    itk_option define -seconds seconds Seconds on
    itk_option define -format format Format civilian
    itk_option define -iq iq Iq high
    itk_option define -gmt gmt GMT no
    itk_option define -state state State normal

    public {
      method get {{format "-string"}}
      method isvalid {}
      method show {{time "now"}}
    }

    protected {
      method _backwardCivilian {}
      method _backwardMilitary {}
      method _focusIn {}
      method _forwardCivilian {}
      method _forwardMilitary {}
      method _keyPress {char sym state}
      method _moveField {direction}
      method _setField {field}
      method _whichField {}
      method _toggleAmPm {}

      variable _cfield hour
      variable _formatString "%r"
      variable _fields {}
      variable _numFields 4
      variable _forward {}
      variable _backward {}
      variable _timeVar ""

      common _militaryFields {hour minute second}
      common _civilianFields {hour minute second ampm}
    }
}

#
# Provide a lowercased access method for the timefield class.
# 
proc iwidgets::timefield {pathName args} {
    uplevel iwidgets::Timefield $pathName $args
}

# ------------------------------------------------------------------
#                        CONSTRUCTOR
# ------------------------------------------------------------------
body iwidgets::Timefield::constructor {args} {
    component hull configure -borderwidth 0
    
    #
    # Create an entry field for entering the time.
    #
    itk_component add time {
      entry $itk_interior.time
    } {
      keep -borderwidth -cursor -exportselection \
          -foreground -highlightcolor -highlightthickness \
          -insertbackground -justify -relief -textvariable
      
      rename -font -textfont textFont Font
      rename -highlightbackground -background background Background
      rename -background -textbackground textBackground Background
    }

    #
    # Create the child site widget.
    #
    itk_component add -protected dfchildsite {
      frame $itk_interior.dfchildsite
    } 
    set itk_interior $itk_component(dfchildsite)
    
    #
    # Add timefield event bindings for focus in and keypress events.
    #
    bind $itk_component(time) <FocusIn>   [code $this _focusIn]
    bind $itk_component(time) <KeyPress>  [code $this _keyPress %A %K %s]
    bind $itk_component(time) <1> "focus $itk_component(time); break"

    #
    # Disable some mouse button event bindings:
    #   Button Motion
    #   Double-Clicks
    #   Triple-Clicks
    #   Button2
    #
    bind $itk_component(time) <Button1-Motion>	break
    bind $itk_component(time) <Button2-Motion>	break
    bind $itk_component(time) <Double-Button>	break
    bind $itk_component(time) <Triple-Button>	break
    bind $itk_component(time) <2>		break

    #
    # Initialize the widget based on the command line options.
    #
    eval itk_initialize $args

    #
    # Initialize the time to the current time.
    #
    show
}

# ------------------------------------------------------------------
#                             OPTIONS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# OPTION: -childsitepos
#
# Specifies the position of the child site in the widget.  Valid
# locations are n, s, e, and w.
# ------------------------------------------------------------------
configbody iwidgets::Timefield::childsitepos {
    set parent [winfo parent $itk_component(time)]

    switch $itk_option(-childsitepos) {
      n {
          grid $itk_component(dfchildsite) -row 0 -column 0 -sticky ew
          grid $itk_component(time) -row 1 -column 0 -sticky nsew

          grid rowconfigure $parent 0 -weight 0
          grid rowconfigure $parent 1 -weight 1
          grid columnconfigure $parent 0 -weight 1
          grid columnconfigure $parent 1 -weight 0
      }
      
      e {
          grid $itk_component(dfchildsite) -row 0 -column 1 -sticky ns
          grid $itk_component(time) -row 0 -column 0 -sticky nsew

          grid rowconfigure $parent 0 -weight 1
          grid rowconfigure $parent 1 -weight 0
          grid columnconfigure $parent 0 -weight 1
          grid columnconfigure $parent 1 -weight 0
      }
      
      s {
          grid $itk_component(dfchildsite) -row 1 -column 0 -sticky ew
          grid $itk_component(time) -row 0 -column 0 -sticky nsew

          grid rowconfigure $parent 0 -weight 1
          grid rowconfigure $parent 1 -weight 0
          grid columnconfigure $parent 0 -weight 1
          grid columnconfigure $parent 1 -weight 0
      }
      
      w {
          grid $itk_component(dfchildsite) -row 0 -column 0 -sticky ns
          grid $itk_component(time) -row 0 -column 1 -sticky nsew

          grid rowconfigure $parent 0 -weight 1
          grid rowconfigure $parent 1 -weight 0
          grid columnconfigure $parent 0 -weight 0
          grid columnconfigure $parent 1 -weight 1
      }
      
      default {
          error "bad childsite option\
                \"$itk_option(-childsitepos)\":\
                should be n, e, s, or w"
      }
    }
}

# ------------------------------------------------------------------
# OPTION: -command
#
# Command invoked upon detection of return key press event.
# ------------------------------------------------------------------
configbody iwidgets::Timefield::command {}

# ------------------------------------------------------------------
# OPTION: -iq
#
# Specifies the level of intelligence to be shown in the actions
# taken by the time field during the processing of keypress events.
# Valid settings include high or low.  With a high iq,
# the time prevents the user from typing in an invalid time.  For 
# example, if the current time is 05/31/1997 and the user changes
# the hour to 04, then the minute will be instantly modified for them 
# to be 30.  In addition, leap seconds are fully taken into account.
# A setting of low iq instructs the widget to do no validity checking
# at all during time entry.  With a low iq level, it is assumed that
# the validity will be determined at a later time using the time's
# isvalid command.
# ------------------------------------------------------------------
configbody iwidgets::Timefield::iq {

  switch $itk_option(-iq) {
    high - low {

    }
    default {
      error "bad iq option \"$itk_option(-iq)\": should be high or low"
    }
  }
}

# ------------------------------------------------------------------
# OPTION: -format
#
# Specifies the time format displayed in the entry widget.
# ------------------------------------------------------------------
configbody iwidgets::Timefield::format {

  switch $itk_option(-format) {
    civilian {
      set _backward _backwardCivilian
      set _forward _forwardCivilian
      set _fields $_civilianFields
      set _numFields 4
      set _formatString "%r"
      $itk_component(time) config -width 11
    }
    military {
      set _backward _backwardMilitary
      set _forward _forwardMilitary
      set _fields $_militaryFields
      set _numFields 3
      set _formatString "%T"
      $itk_component(time) config -width 8
    }
    default {
      error "bad iq option \"$itk_option(-iq)\":\
             should be civilian or military"
    }
  }

  #
  # Update the current contents of the entry field to reflect
  # the configured format.
  #
  show $_timeVar
}

# ------------------------------------------------------------------
# OPTION: -gmt
#
# This option is used for GMT time.  Must be a boolean value.
# ------------------------------------------------------------------
configbody iwidgets::Timefield::gmt {
  switch $itk_option(-gmt) {
    0 - no - false - off { }
    1 - yes - true - on { }
    default {
      error "bad gmt option \"$itk_option(-gmt)\": should be boolean"
    }
  }
}

# ------------------------------------------------------------------
# OPTION: -state
#
# Disable the 
# ------------------------------------------------------------------
configbody iwidgets::Timefield::state {
  switch -- $itk_option(-state) {
    normal {
      $itk_component(time) configure -state normal
    }
    disabled {
      focus $itk_component(hull)
      $itk_component(time) configure -state disabled
    }
    default {
      error "Invalid value for -state: $itk_option(-state).  Should be\
        \"normal\" or \"disabled\"."
    }
  }
}


# ------------------------------------------------------------------
#                            METHODS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# PUBLIC METHOD: get ?format?
#
# Return the current contents of the timefield in one of two formats
# 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.
# ------------------------------------------------------------------
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.
# ------------------------------------------------------------------
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.
# ------------------------------------------------------------------
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.
# ------------------------------------------------------------------
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.
# ------------------------------------------------------------------
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
      } 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]
    }
  }
}