#
# $Log: not supported by cvs2svn $
# Revision 1.11  1999/01/05 22:50:21  soliday
# Added bindtag commands to ensure entries are updated before executing a bind command.
#
# Revision 1.10  1999/01/05 15:33:57  borland
# Added -fastClick option to all instances of APSButton.
#
# Revision 1.9  1998/06/25 13:49:13  borland
# Fixed error in previous revision.
#
# Revision 1.8  1998/06/24 18:14:05  borland
# Fixed problem introduced in last modification.  Might bomb when the
# user doesn't give a variable for the hour.
#
# Revision 1.7  1998/06/24 16:47:37  borland
# APSDateTimeEntry now makes the time entry box wider (8 characters) to
# accomodate HH:MM:SS.
#
# Revision 1.6  1996/12/11 21:21:25  borland
# Removed pointless use of APSUniqueName to make a random widget name.
#
# Revision 1.5  1996/11/23 02:03:58  borland
# Modified orientation of year/month entry boxes.
#
# Revision 1.4  1996/11/14 22:24:48  borland
# Now properly implements horizontal and vertical orientation for
# date/time entry with buttons.
#
# Revision 1.3  1996/11/14 19:03:04  borland
# Added controls of widget arrangement for APSDateTimeAdjEntry.
#
# Revision 1.2  1996/11/04 18:32:52  borland
# Removed the word "Widget" from the names of procedures, except for old
# ones that are kept for backward compatibility.
#
# Revision 1.1  1996/11/04 17:00:43  borland
# Formerly in mbdateEntry.tcl .   Now has help routines and better names.
#
# Revision 1.6  1996/09/30 18:09:46  borland
# Increased width of entry boxes for date/time to 6 characters from 4.
#
# Revision 1.5  1996/07/25 16:23:10  borland
# Added user command to TODAY buttons on date adjust widgets.
#
# Revision 1.4  1996/07/17  14:27:33  borland
# More consistent and simplified use of twoDigitYear and leadingZeros options
# to conversion routines.  Added -buttonSize option for DateTimeAdjEntry.
#
# Revision 1.3  1996/07/15  19:09:43  borland
# Added APSDateTimeAdjEntry for making up/down controls for day, week,
# month, year.
#
# Revision 1.2  1996/02/12  21:33:31  borland
# Added first version of APSMakeTimeDateEntryWidget.
#
# Revision 1.1  1996/01/16  21:38:38  saunders
# Unofficial routines from ~borland/tcl/rel added. These should
# ultimately be removed from use or modified to comply with
# current conventions.
#
#

proc APSDateEntryHelp {} {
    return "Usage: APSDateEntry widget -parent name
\n\t\[-label string\]\n\t\[-dayVariable name\]
\n\t\[-monthVariable name\]\n\t\[-yearVariable name\]
\n\t\[-twoDigitYear 0|1\]\n\t\[-leadingZeros 0|1\]"
}

proc APSMakeDateEntryWidget {widget args} {
    eval APSDateEntry $widget $args
}

proc APSDateEntry {widget args} {
    set parent ""
    set dayVariable ""
    set yearVariable ""
    set monthVariable ""
    set twoDigitYear 1
    set leadingZeros 1
    set label "Date (year, month, day): "
    set packOption ""
    APSStrictParseArguments {parent dayVariable yearVariable monthVariable label \
                     twoDigitYear leadingZeros packOption}
    global $monthVariable $yearVariable $dayVariable
    set dateList [split [clock format [clock seconds] -format %y:%m:%d] :]
    set $yearVariable  [lindex $dateList 0]
    if !$twoDigitYear {
        set $yearVariable [APSMakeFourDigitYear [subst \$$yearVariable ]]
    } 
    set fstring %ld
    if $leadingZeros {
        set fstring %02ld
    }
    scan [lindex $dateList 1] $fstring $monthVariable
    scan [lindex $dateList 2] $fstring $dayVariable
    APSLabeledEntryFrame $widget -parent $parent \
      -label $label -packOption $packOption \
      -variableList "$yearVariable $monthVariable $dayVariable" \
      -width 6 -orientation horizontal \
      -contextHelp "Enter the year, month, and day in the three boxes provided."
}

proc APSDateTimeEntryHelp {} {
    return "Usage: APSDateEntry widget -parent name
\n\t\[-label string\]\n\t\[-dayVariable name\]
\n\t\[-monthVariable name\]\n\t\[-yearVariable name\]
\n\t\[-hourVariable name\]
\n\t\[-twoDigitYear 0|1\]\n\t\[-leadingZeros 0|1\]"
}

proc APSMakeDateTimeEntryWidget {widget args} {
    eval APSDateTimeEntry $widget $args
}

proc APSDateTimeEntry {widget args} {
    set parent ""
    set dayVariable ""
    set yearVariable ""
    set monthVariable ""
    set hourVariable ""
    set twoDigitYear 1
    set leadingZeros 1
    set label "Date and time (year, month, day, hour): "
    set packOption ""
    APSStrictParseArguments {parent dayVariable yearVariable monthVariable \
                         hourVariable label twoDigitYear leadingZeros packOption}
    global $monthVariable $yearVariable $dayVariable $hourVariable
    set dateList [split [clock format [clock seconds] -format %y:%m:%d:%H:%M] :]
    scan [lindex $dateList 0] "%ld" $yearVariable 
    if !$twoDigitYear {
        set $yearVariable [APSMakeFourDigitYear [subst \$$yearVariable]]
    }
    set fstring %ld
    if $leadingZeros {
        set fstring %02ld
    }
    scan [lindex $dateList 1] $fstring $monthVariable
    scan [lindex $dateList 2] $fstring $dayVariable
    scan [lindex $dateList 3] "%ld" hour
    scan [lindex $dateList 4] "%ld" minute
    set $hourVariable  [expr $hour+$minute/60.0]
    APSLabeledEntryFrame $widget -parent $parent \
      -label $label -packOption $packOption \
      -variableList "$yearVariable $monthVariable $dayVariable $hourVariable" \
      -width 6 -orientation horizontal \
      -contextHelp "Enter the year, month, day, and hour in the four boxes provided." 
    if [winfo exists $parent$widget.frame.entry4] {
        $parent$widget.frame.entry4 configure -width 8
    }
}

proc APSYearMonthEntryHelp {} {
    return "Usage: APSYearMonthEntry widget -parent name
\n\t\[-label string\]
\n\t\[-monthVariable name\]\n\t\[-yearVariable name\]
\n\t\[-twoDigitYear 0|1\]\n\t\[-leadingZeros 0|1\]"
}

proc APSMakeYearMonthEntryWidget {widget args} {
    eval APSYearMonthEntry $widget $args
}

proc APSYearMonthEntry {widget args} {
    set parent ""
    set yearVariable ""
    set monthVariable ""
    set twoDigitYear 1
    set leadingZeros 1
    set label "Date (year, month): "
    APSStrictParseArguments {parent  yearVariable monthVariable label \
                     twoDigitYear leadingZeros}
    global $monthVariable $yearVariable 
    set dateList [split [clock format [clock seconds] -format %y:%m] :]
    scan [lindex $dateList 0] "%ld" $yearVariable 
    if !$twoDigitYear {
        set $yearVariable [APSMakeFourDigitYear [subst \$$yearVariable]]
    }
    if $leadingZeros {
        scan [lindex $dateList 1] %02ld $monthVariable
    } else {
        scan [lindex $dateList 1] %ld $monthVariable
    }

    APSLabeledEntryFrame $widget -parent $parent \
      -label $label \
      -variableList "$yearVariable $monthVariable" \
      -width 6 -orientation horizontal \
      -contextHelp "Enter the year and month in the two boxes provided."
}

proc APSDateTimeAdjEntryHelp {} {
    return "Usage: APSDateTimeAdjEntry widget -parent name\n\t\[-dayVariable name\]
\n\t\[-monthVariable name\]\n\t\[-yearVariable name\]
\n\t\[-hourVariable name\]\n\t\[-parent name\]\n\t\[-label string\]
\n\t\[-defaultHour number\]\n\t\[-command string\]\n\t\[-twoDigitYear 0|1\]"
}

proc APSDateTimeAdjEntry {widget args} {
    set dayVariable ""
    set monthVariable ""
    set yearVariable ""
    set hourVariable ""
    set parent .
    set label ""
    set defaultHour 0
    set command ""
    set twoDigitYear 0
    set leadingZeros 0
    set buttonSize medium
    set orientation vertical
    set stackButtons 0
    APSStrictParseArguments \
      {dayVariable monthVariable yearVariable hourVariable buttonSize \
         parent label defaultHour command description twoDigitYear leadingZeros \
         orientation stackButtons}

    global $dayVariable $monthVariable $yearVariable apsttk
    set excludeHour 1
    if [string length $hourVariable] {
        global $hourVariable
        set $hourVariable $defaultHour
        set excludeHour 0
    }
    if {[info exists apsttk] && ($apsttk != 0)} {
        set ttkPrefix ttk::
        set ttk 1
    } else {
        set ttkPrefix ""
        set ttk 0
    }

    set wf $parent$widget
    ${ttkPrefix}frame $wf 
    pack $wf -side top
    if ![string length $label] {
        if !$excludeHour {
            set label "Date/time (year, month, day, hour): "
        } else {
            set label "Date (year, month, day): "
        }
    }

    if [string compare $orientation vertical] {
        set packOption "-side left"
    } else {
        set packOption "-side top"
    }

    if !$excludeHour {
        APSDateTimeEntry .entries -parent $wf \
          -dayVariable $dayVariable -monthVariable $monthVariable \
          -yearVariable $yearVariable -hourVariable $hourVariable \
          -label $label -twoDigitYear $twoDigitYear \
          -leadingZeros $leadingZeros -packOption "$packOption"
    } else {
        APSDateEntry .entries -parent $wf \
          -dayVariable $dayVariable -monthVariable $monthVariable \
          -yearVariable $yearVariable -label $label -twoDigitYear $twoDigitYear \
          -leadingZeros $leadingZeros -packOption "$packOption"
    }

    if [string length $command] {
        if {$ttk} {
            bindtags $wf.entries.frame.entry1 "TEntry . all $wf.entries.frame.entry1"
            bindtags $wf.entries.frame.entry2 "TEntry . all $wf.entries.frame.entry2"
            bindtags $wf.entries.frame.entry3 "TEntry . all $wf.entries.frame.entry3"
        } else {
            bindtags $wf.entries.frame.entry1 "Entry . all $wf.entries.frame.entry1"
            bindtags $wf.entries.frame.entry2 "Entry . all $wf.entries.frame.entry2"
            bindtags $wf.entries.frame.entry3 "Entry . all $wf.entries.frame.entry3"
        }
        bind $wf.entries.frame.entry1 <Key> $command
        bind $wf.entries.frame.entry2 <Key> $command
        bind $wf.entries.frame.entry3 <Key> $command
    }

    set wb $wf.bframe
    ${ttkPrefix}frame $wb
    if [string compare $orientation vertical] {
        pack $wb -side left
    } else {
        pack $wb -side bottom
    }
    # set qualifier [APSUniqueName ""]
    set qualifier ""
    if !$excludeHour {
        APSButton .today${qualifier} -parent $wb -text TODAY -size $buttonSize \
          -command "APSDateBreakDown -dayVariable $dayVariable \
-monthVariable $monthVariable -yearVariable $yearVariable \
-hourVariable $hourVariable -twoDigitYear 0 -leadingZeros 0 ; set $hourVariable $defaultHour; $command" \
          -contextHelp "Resets date to today." \
          -fastClick 1
    } else {
        APSButton .today${qualifier} -parent $wb -text TODAY -size $buttonSize \
          -command "APSDateBreakDown -dayVariable $dayVariable \
-monthVariable $monthVariable -yearVariable $yearVariable \
-twoDigitYear 0 -leadingZeros 0 ; $command " \
          -contextHelp "Resets date to today." \
          -fastClick 1
    }

    set wb1 $wb
    set buttonPackOption "-side left"
    if $stackButtons {
        set wb1 $wb.day
        ${ttkPrefix}frame $wb1
        pack $wb1 -side left
        set buttonPackOption "-side top"
    }
    APSButton .decr1${qualifier} -parent $wb1 -text "-DAY" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset -1 -unit day; $command" \
      -contextHelp "Decrements date by 1 day." -packOption $buttonPackOption -fastClick 1
    APSButton .incr1${qualifier} -parent $wb1 -text "+DAY" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset 1 -unit day; $command" \
      -contextHelp "Increments date by 1 day." -packOption $buttonPackOption -fastClick 1

    if $stackButtons {
        set wb1 $wb.week
        ${ttkPrefix}frame $wb1
        pack $wb1 -side left
    }
    APSButton .decr7${qualifier} -parent $wb1 -text "-WEEK" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset -7 -unit day; $command" \
      -contextHelp "Decrements date by 7 days." -packOption $buttonPackOption -fastClick 1
    APSButton .incr7${qualifier} -parent $wb1 -text "+WEEK" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset 7 -unit day; $command" \
      -contextHelp "Increments date by 7 days." -packOption $buttonPackOption -fastClick 1

    if $stackButtons {
        set wb1 $wb.month
        ${ttkPrefix}frame $wb1
        pack $wb1 -side left
    }
    APSButton .decr30${qualifier} -parent $wb1 -text "-MONTH" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset -1 -unit month; $command" \
      -contextHelp "Decrements date by 1 month." -packOption $buttonPackOption -fastClick 1
    APSButton .incr30${qualifier} -parent $wb1 -text "+MONTH" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset 1 -unit month; $command" \
      -contextHelp "Increments date by 1 month." -packOption $buttonPackOption -fastClick 1

    if $stackButtons {
        set wb1 $wb.year
        ${ttkPrefix}frame $wb1
        pack $wb1 -side left
    }
    APSButton .decr365${qualifier} -parent $wb1 -text "-YEAR" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset -1 -unit year; $command" \
      -contextHelp "Decrements date by 1 year." -packOption $buttonPackOption -fastClick 1
    APSButton .incr365${qualifier} -parent $wb1 -text "+YEAR" -size $buttonSize \
      -command "APSIncrementDateVariables \
-dayVariable $dayVariable -monthVariable $monthVariable -yearVariable $yearVariable \
-offset 1 -unit year; $command" \
      -contextHelp "Increments date by 1 year." -packOption $buttonPackOption -fastClick 1
}

