#----------------------------------------------------------------------
#  Eskil, Help functions
#
#  Copyright (c) 1998-2005, Peter Spjuth  (peter.spjuth@gmail.com)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; see the file COPYING.  If not, write to
#  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#  Boston, MA 02111-1307, USA.
#
#----------------------------------------------------------------------
# $Revision$
#----------------------------------------------------------------------

# A simple window for displaying e.g. help.
# Returns the frame where things can be put.
proc helpWin {W title} {
    destroy $W

    toplevel $W -padx 2 -pady 2
    wm title $W $title
    bind $W <Key-Return> [list destroy $W]
    bind $W <Key-Escape> [list destroy $W]
    ttk::frame $W.f
    ttk::button $W.b -text "Close" -command [list destroy $W] -width 10 \
            -default active
    pack $W.b -side bottom -pady 2
    pack $W.f -side top -expand y -fill both -padx 2 -pady 2
    focus $W
    return $W.f
}

proc makeAboutWin {} {
    set W [helpWin .ab "About Eskil"]

    set bg [ttk::style configure . -background]
    text $W.t -width 45 -height 11 -wrap none -relief flat \
            -background $bg
    pack $W.t -side top -expand y -fill both

    $W.t insert end "A graphical frontend to diff\n\n"
    $W.t insert end "$::eskil(diffver)\n\n"
    $W.t insert end "Made by Peter Spjuth\n"
    $W.t insert end "E-Mail: peter.spjuth@gmail.com\n"
    $W.t insert end "\nURL: http://eskil.tcl.tk\n"
    $W.t insert end "\nTcl version: [info patchlevel]\n"

    set du $::DiffUtil::version
    append du " ($::DiffUtil::implementation)"
    $W.t insert end "DiffUtil version: $du\n"

    # Provide debug info to help when DiffUtil does not load.
    if {[info exists ::DiffUtil::DebugLibFile]} {
        set lf $::DiffUtil::DebugLibFile
        set exist [file exists $lf]
        set lf [file join {*}[lrange [file split $lf] end-1 end]]
        if {$exist} {
            $W.t insert end "  DiffUtil debug: Could not load\n"
            $W.t insert end "    $lf\n"
        } else {
            $W.t insert end "  DiffUtil debug: Could not find\n"
            $W.t insert end "    $lf\n"
        }
    }

    if {[catch {package require pdf4tcl} pdf4tclVer]} { set pdf4tclVer None }
    $W.t insert end "Pdf4Tcl version: $pdf4tclVer\n"
    if {[catch {package require snit} snitVer]} { set snitVer None }
    $W.t insert end "Snit version: $snitVer\n"
    if {[catch {package require vfs} vfsVer]} { set vfsVer None }
    $W.t insert end "Vfs version: $vfsVer\n"
    if {[catch {package require wcb} wcbVer]} { set wcbVer None }
    $W.t insert end "Wcb version: $wcbVer\n"
    if {[catch {package require tablelist_tile} tblVer]} { set tblVer None }
    $W.t insert end "Tablelist version: $tblVer\n"
    if {[catch {package require tkdnd} tkdndVer]} { set tkdndVer None }
    $W.t insert end "TkDnd version: $tkdndVer\n"

    $W.t insert end "\nCredits:\n"
    $W.t insert end "Ideas for scrollbar map and merge function\n"
    $W.t insert end "taken from TkDiff"

    set last [lindex [split [$W.t index end] "."] 0]
    $W.t configure -height $last
    $W.t configure -state disabled
}

# Insert a text file into a text widget.
# Any XML-style tags in the file are used as tags in the text window.
proc insertTaggedText {W file} {
    set ch [open $file r]
    set data [read $ch]
    close $ch

    set tags {}
    while {$data != ""} {
        if {[regexp {^([^<]*)<(/?)([^>]+)>(.*)$} $data -> pre sl tag post]} {
            $W insert end [subst -nocommands -novariables $pre] $tags
            set i [lsearch $tags $tag]
            if {$sl != ""} {
                # Remove tag
                if {$i >= 0} {
                    set tags [lreplace $tags $i $i]
                }
            } else {
                # Add tag
                lappend tags $tag
            }
            set data $post
        } else {
            $W insert end [subst -nocommands -novariables $data] $tags
            set data ""
        }
    }
}

proc makeHelpWin {} {
    set doc [file join $::eskil(thisDir) .. doc/eskil.txt]
    if { ! [file exists $doc]} return

    set W [helpWin .he "Eskil Help"]
    set t [Scroll y text $W.t -width 85 -height 35]
    pack $W.t -side top -expand 1 -fill both

    configureDocWin $t

    # Set up tags for change marks
    $t tag configure new1 -foreground $::Pref(colornew1) \
            -background $::Pref(bgnew1)
    $t tag configure new2 -foreground $::Pref(colornew2) \
            -background $::Pref(bgnew2)
    $t tag configure change -foreground $::Pref(colorchange) \
            -background $::Pref(bgchange)
    $t tag configure ul -underline 1

    set width [font measure [$t cget -font] [string repeat x 20]]
    $t configure -tabs [list $width [expr {$width * 3/2}] [expr {$width * 2}]]

    set width [font measure docFontP [string repeat x 36]]
    $t tag configure example -tabs [list $width] -wrap none

    insertTaggedText $t $doc
    $t configure -state disabled
}

proc createDocFonts {} {
    if {[catch {font create docFont -family Helvetica -size -16}]} return
    font create docFontB {*}[font configure docFont] -weight bold

    set h [font metrics docFont -linespace]
    # Use negative size to get pixels. Search from small to bigger
    set t [expr {-$h + 4}]
    font create docFontP -family Courier -size $t
    for {} {$t > -20} {incr t -1} {
        font configure docFontP -size $t
        if {[font metrics docFontP -linespace] >= $h} break
    }
}

# Configure a text window as Doc viewer
proc configureDocWin {W} {
    createDocFonts
    $W configure -font docFont -wrap word
    $W tag configure ul -underline 1
    $W tag configure b -font docFontB
    $W tag configure bullet -tabs "1c" -lmargin2 "1c"
    $W tag configure pre -font docFontP

    set top [winfo toplevel $W]
    foreach event {<Key-Prior> <Key-Next>} {
        bind $top $event [string map [list "%W" $W] [bind Text $event]]
    }
}

proc makeDocWin {fileName} {
    set W [helpWin .doc "Eskil Help"]
    set t [Scroll y text $W.t -width 80 -height 25]
    pack $W.t -side top -expand 1 -fill both

    configureDocWin $t

    if { ! [file exists $::eskil(thisDir)/../doc/$fileName]} {
        $t insert end "ERROR: Could not find doc file "
        $t insert end \"$fileName\"
        return
    }
    insertTaggedText $t $::eskil(thisDir)/../doc/$fileName

    #focus $t
    $t configure -state disabled
}

proc makeTutorialWin {} {
    set doc [file join $::eskil(thisDir) .. doc/tutorial.txt]
    if { ! [file exists $doc]} return

    if {[catch {cd [file join $::eskil(thisDir) .. examples]}]} {
        tk_messageBox -icon error -title "Eskil Error" -message \
                "Could not locate examples directory." \
                -type ok
        return
    }

    # Start up a dirdiff in the examples directory
    set ::dirdiff(leftDir) [file join [pwd] dir1]
    set ::dirdiff(rightDir) [file join [pwd] dir2]
    makeDirDiffWin

    set W [helpWin .ht "Eskil Tutorial"]

    text $W.t -width 82 -height 35 -yscrollcommand "$W.sb set"
    ttk::scrollbar $W.sb -orient vert -command "$W.t yview"
    pack $W.sb -side right -fill y
    pack $W.t -side left -expand 1 -fill both

    configureDocWin $W.t

    # Move border properties to frame
    set bw [$W.t cget -borderwidth]
    set relief [$W.t cget -relief]
    $W configure -relief $relief -borderwidth $bw
    $W.t configure -borderwidth 0

    insertTaggedText $W.t $doc
    $W.t configure -state disabled
}
