#******************************************************************************
#
#       Copyright:      2005-2026 Paul Obermeier (obermeier@tcl3d.org)
#
#                       See the file "Tcl3D_License.txt" for information on
#                       usage and redistribution of this file, and for a
#                       DISCLAIMER OF ALL WARRANTIES.
#
#       Module:         Tcl3D -> tcl3dOgl
#       Filename:       tcl3dUtilLogo.tcl
#
#       Author:         Paul Obermeier
#
#       Description:    Tcl module showing the Tcl/Tk and poSoft logo.
#
#******************************************************************************

proc __tcl3dLogoShrinkWindow { tw dir } {
    set width  [winfo width $tw]
    set height [winfo height $tw]
    set x      [winfo x $tw]
    set y      [winfo y $tw]
    set inc -1

    if { [string compare $dir "x"] == 0 } {
        for { set w $width } { $w >= 20 } { incr w $inc } {
            wm geometry $tw [format "%dx%d+%d+%d" $w $height $x $y]
            update
            incr inc -1
        }
    } else {
        for { set h $height } { $h >= 20 } { incr h $inc } {
            wm geometry $tw [format "%dx%d+%d+%d" $width $h $x $y]
            update
            incr inc -1
        }
    }
}

proc __tcl3dLogoSetWackelDelay { mouseX mouseY } {
    global __tcl3dLogoWackel

    set __tcl3dLogoWackel(wackelSpeed) [expr $mouseX + $mouseY]
}

proc __tcl3dLogoSwitchLogo { b } {
    global __tcl3dLogoWackel

    if { $__tcl3dLogoWackel(onoff) == 1 } {
        set __tcl3dLogoWackel(onoff) 0
    }
    set __tcl3dLogoWackel(curImg) [expr 1 - $__tcl3dLogoWackel(curImg)]
    $b configure -image $__tcl3dLogoWackel($__tcl3dLogoWackel(curImg))
}

proc __tcl3dLogoStartWackel { b } {
    global __tcl3dLogoWackel

    if { $__tcl3dLogoWackel(onoff) == 0 } {
        set __tcl3dLogoWackel(onoff) 1
        __tcl3dLogoWackel $b
    } else {
        __tcl3dLogoStopWackel $b
    }
}

proc __tcl3dLogoStopWackel { b } {
    global __tcl3dLogoWackel 

    set __tcl3dLogoWackel(onoff)  0
    set __tcl3dLogoWackel(curImg) 1
    after $__tcl3dLogoWackel(wackelSpeed) __tcl3dLogoWackel $b
}

proc __tcl3dLogoWackel { b } {
    global __tcl3dLogoWackel

    if { $__tcl3dLogoWackel(onoff) == 1 } {
        set __tcl3dLogoWackel(curImg) [expr 1 - $__tcl3dLogoWackel(curImg)]
        $b configure -image $__tcl3dLogoWackel($__tcl3dLogoWackel(curImg))
        update
        after $__tcl3dLogoWackel(wackelSpeed) "__tcl3dLogoWackel $b"
    }
}

###############################################################################
#[@e
#       Name:           tcl3dLogoDestroyPoSoft - Destroy poSoft logo window.
#
#       Synopsis:       tcl3dLogoDestroyPoSoft {}
#
#       Description:    Destroy a previously opened poSoft logo window.
#
#       See also:       tcl3dLogoShowPoSoft
#                       tcl3dLogoShowTcl
#
###############################################################################

proc tcl3dLogoDestroyPoSoft {} {
    global __tcl3dLogoWackel
    global __tcl3dLogoWinId
    global __tcl3dLogoWithdrawnWinId

    if { [info exists __tcl3dLogoWinId] && [winfo exists $__tcl3dLogoWinId] } {
        __tcl3dLogoShrinkWindow $__tcl3dLogoWinId x
        set __tcl3dLogoWackel(onoff) 0
        image delete $__tcl3dLogoWackel(0)
        image delete $__tcl3dLogoWackel(1)
        destroy $__tcl3dLogoWinId
        if { [winfo exists $__tcl3dLogoWithdrawnWinId] } {
            wm deiconify $__tcl3dLogoWithdrawnWinId
        }
    }
}

###############################################################################
#[@e
#       Name:           tcl3dLogoShowPoSoft - Display poSoft logo.
#
#       Synopsis:       tcl3dLogoShowPoSoft { version copyright withdrawWin }
#
#       Description:    version     : string
#                       copyright   : string
#                       withdrawWin : string (Widget name)
#
#                       Display the poSoft logo in two possible ways:
#                       If "withdrawWin" is set to the empty string, the logo
#                       is shown in a window with decoration. This may be used
#                       for displaying the logo as an action for an "About"
#                       menu entry. 
#                       If "withdrawWin" is set to an existing window name
#                       (typically the name of the main application window), the
#                       logo window is shown without decoration as a splash
#                       window, which automatically disappears after a second.
#                       The logo window has two label widgets to display 
#                       additional text messages, which are specified in 
#                       "version" and "copyright".
#
#       See also:       tcl3dLogoDestroyPoSoft
#                       tcl3dLogoShowTcl
#
###############################################################################

proc tcl3dLogoShowPoSoft { version copyright {withdrawWin ""} } {
    global __tcl3dLogoWackel
    global __tcl3dLogoWinId
    global __tcl3dLogoWithdrawnWinId

    set t ".poShowLogo"
    set __tcl3dLogoWinId $t
    set __tcl3dLogoWithdrawnWinId $withdrawWin
    if { [winfo exists $t] } {
        tcl3dWinRaise $t
        return
    }

    set __tcl3dLogoWackel(0) [image create photo -data [__tcl3dLogoPoLogo200_text]]
    set __tcl3dLogoWackel(1) [image create photo -data [__tcl3dLogoPoLogo200_text_flip]]
    set __tcl3dLogoWackel(onoff)  0
    set __tcl3dLogoWackel(curImg) 0
    set __tcl3dLogoWackel(wackelSpeed) 500

    toplevel $t
    ttk::frame $t.f
    pack $t.f
    wm resizable $t false false
    if { [string compare $withdrawWin ""] == 0 } {
        wm title $t "poSoft Information"
    } else {
        if { [string compare $withdrawWin "."] != 0 } {
            wm withdraw .
        }
        if { [winfo exists $withdrawWin] } {
            wm withdraw $withdrawWin
            set __tcl3dLogoWithdrawnWinId $withdrawWin
        }
        wm overrideredirect $t 1
        set xmax [winfo screenwidth $t]
        set ymax [winfo screenheight $t]
        set x0 [expr {($xmax - [image width  $__tcl3dLogoWackel(0)])/2}]
        set y0 [expr {($ymax - [image height $__tcl3dLogoWackel(0)])/2}]
        wm geometry $t "+$x0+$y0"
        $t.f configure -borderwidth 10
        raise $t
        update
    }

    ttk::label $t.f.l1 -text "Paul Obermeier's Portable Software" -anchor center
    pack $t.f.l1 -fill x
    ttk::button $t.f.l2 -image $__tcl3dLogoWackel(0)
    bind $t.f.l2 <Motion> { __tcl3dLogoSetWackelDelay %x %y }
    bind $t.f.l2 <Shift-Button-1> "__tcl3dLogoStartWackel $t.f.l2"
    bind $t.f.l2 <Button-1> "__tcl3dLogoSwitchLogo $t.f.l2"
    pack $t.f.l2
    ttk::label $t.f.l3 -text $version -anchor center
    pack $t.f.l3 -fill x
    ttk::label $t.f.l4 -text $copyright -anchor center
    pack $t.f.l4 -fill x
    if { [string compare $withdrawWin ""] == 0 } {
        ttk::button $t.f.b -text "OK" -command "tcl3dLogoDestroyPoSoft" \
                    -default active
        pack $t.f.b -fill x
        bind $t <KeyPress-Escape> "tcl3dLogoDestroyPoSoft" 
        bind $t <KeyPress-Return> "tcl3dLogoDestroyPoSoft"
        focus $t
        update
    } else {
        focus $t
        update
        after 500
        __tcl3dLogoSwitchLogo $t.f.l2
        update
        after 300
    }
}

###############################################################################
#[@e
#       Name:           tcl3dLogoDestroyTcl - Destroy Tcl logo window.
#
#       Synopsis:       tcl3dLogoDestroyTcl { w img }
#
#       Description:    Destroy a previously opened Tcl logo window.
#
#       See also:       tcl3dLogoShowTcl
#                       tcl3dLogoShowPoSoft
#
###############################################################################

proc tcl3dLogoDestroyTcl { w img } {
    __tcl3dLogoShrinkWindow $w y
    image delete $img
    destroy $w
}

###############################################################################
#[@e
#       Name:           tcl3dLogoShowTcl - Display Tcl logo.
#
#       Synopsis:       tcl3dLogoShowTcl { args }
#
#       Description:    args : variable parameter list
#
#                       Display the Tcl logo with additional text messages in
#                       a window with decoration. This may be used
#                       for displaying the logo as an action for an "About"
#                       menu entry. 
#                       "args" may contain any combination of the following
#                       package names:
#                       Tk Img Tktable combobox mysqltcl tcom
#
#       See also:       tcl3dLogoShowPoSoft
#
###############################################################################

proc tcl3dLogoShowTcl { args } {
    global __url

    set t ".tcl3dLogoShowTcl"
    if { [winfo exists $t] } {
        tcl3dWinRaise $t
        return
    }

    toplevel $t
    wm title $t "Tcl/Tk Information"
    wm resizable $t false false

    set ph [image create photo -data [__tcl3dLogoPwrdLogo200]]
    ttk::button $t.img -image $ph
    pack $t.img
    ttk::label $t.l1 -anchor w -text "With a little help from my Tcl friends ..."
    pack $t.l1

    set row 0
    ttk::frame $t.f
    pack $t.f -fill both -expand 1
    foreach extension $args {
        set retVal [catch {package present $extension} versionStr]
        if { $retVal != 0 } {
            set versionStr "(not loaded)"
        }
        switch -exact -- $extension {
            Tcl            { set progName     "Tcl [info patchlevel]"
                             set __url($row) "https://www.tcl-lang.org/"
                             set author      "all Tcl developers" 
                           }
            Tk             { set progName     "Tk $versionStr"
                             set __url($row) "https://www.tcl-lang.org/"
                             set author      "all Tk developers" 
                           }
            Img            { set progName    "Img $versionStr"
                             set __url($row) "https://tkimg.sourceforge.net/"
                             set author      "Jan Nijtmans, Andreas Kupries"
                           }
            tablelist -
            tablelist_tile { set progName    "tablelist $versionStr"
                             set __url($row) "https://www.nemethi.de/tablelist/"
                             set author      "Csaba Nemethi" 
                           }
            tkdnd          { set progName    "tkdnd $versionStr"
                             set __url($row) "https://github.com/petasis/tkdnd/"
                             set author      "Georgios Petasis"
                           }
        }
        ttk::label $t.f.lext$row -text $progName
        eval ttk::entry $t.f.rext$row -state normal -width 35 \
                        -textvariable __url($row)
        grid $t.f.lext$row -row $row -column 0 -sticky w
        grid $t.f.rext$row -row $row -column 1 -sticky w
        incr row
    }
    ttk::button $t.b -text "OK" -command "tcl3dLogoDestroyTcl $t $ph" \
                -default active
    pack $t.b -fill x
    bind $t <KeyPress-Escape> "tcl3dLogoDestroyTcl $t $ph" 
    bind $t <KeyPress-Return> "tcl3dLogoDestroyTcl $t $ph"
    focus $t
}

# Inlined logo images.

proc __tcl3dLogoPoLogo200_text {} {
return {
[encoded data, 16897 characters]
Ow==}
} ; # End of proc __tcl3dLogoPoLogo200_text

proc __tcl3dLogoPoLogo200_text_flip {} {
return {
[encoded data, 7869 characters]
YAIZYARvYANbzBinIWQukAJc+tOJEAgAOw==}
} ; # End of proc __tcl3dLogoPoLogo200_text_flip

proc __tcl3dLogoPwrdLogo200 {} {
return {
[encoded data, 4697 characters]
LvOc1AmdqDmV1ZmdwhmW6aGd3vmdSxAEADs=}
} ; # End of proc __tcl3dLogoPwrdLogo200
