#!tclsh
#########################################################################
# _mmucl - some common code between xmmucl and winmmucl
#
# Copyright (C) 1997 Mark Patton
#
# 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; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#########################################################################

package require balloon

# Builds a toolbar .tools

# Load in images used for buttons.    
foreach image [glob [file join $Mmucl(lib_dir) icons *.gif]] {
    image create photo [file tail $image] -format gif -file $image
}
unset image

# Have to use frames and place to get the right appearance.
frame .tools -height 35 -relief ridge -bd 2

frame .tools.bar1 -bd 0
button .tools.bar1.con -image connect.gif -command Mgui_connect -bd 1
balloon_add .tools.bar1.con "Connect to mud"

frame .tools.bar2 -bd 0
button .tools.bar2.load -image open.gif -bd 1\
    -command {tkOpen "Load a file" source}
balloon_add .tools.bar2.load "Load a file"
button .tools.bar2.save -image save.gif -bd 1\
    -command {tkSave "Save Current Aliases and Actions" save}
balloon_add .tools.bar2.save "Save aliases and actions"

frame .tools.bar3 -bd 0
button .tools.bar3.alias -image alias.gif -command \
    {formMenu alias {{Name: Commands:}} alias unalias} -bd 1
balloon_add .tools.bar3.alias "Edit aliases"
button .tools.bar3.action -image action.gif -command \
    {formMenu action {{Pattern: Commands:}} action unaction} -bd 1
balloon_add .tools.bar3.action "Edit actions"
button .tools.bar3.char -image char.gif -command \
    {formMenu character {{Character: {Mud Name:} {Mud Location:}\
	{Mud Port:} Login:}} charadd chardelete} -bd 1
balloon_add .tools.bar3.char "Edit characters"

frame .tools.bar4 -bd 0
button .tools.bar4.help -image help.gif -command help -bd 1
balloon_add .tools.bar4.help "Help"
button .tools.bar4.quit -image quit.gif -command \
    {tkDialog Mmucl "Really Quit?" exit} -bd 1
balloon_add .tools.bar4.quit "Quit"

pack .tools.bar1.con .tools.bar2.load .tools.bar2.save .tools.bar3.alias\
    .tools.bar3.action .tools.bar3.char .tools.bar4.help \
    .tools.bar4.quit -side left

place .tools.bar1 -x 2
place .tools.bar2 -relx 0.2
place .tools.bar3 -relx 0.5 
place .tools.bar4 -relx 1.0 -x -60

# hook up to the mmucl package
bind .input  <Key-Return>  {echo "$Mmucl(input)";parse_input}

# History scrolling.
bind .input <Key-Up> \
     {set Mmucl(input) [_scrollHistory Mmucl(history) Mmucl(histloc) -1]}
bind .input <Key-Down> \
     {set Mmucl(input) [_scrollHistory Mmucl(history) Mmucl(histloc) 1]}

# Load some init files and make Mmucl(rc_dir) if it doesn't exist
if {![file exists $Mmucl(rc_dir)]} {
    file mkdir $Mmucl(rc_dir)
}
if {[file exists [file join $Mmucl(rc_dir) mmuclrc]]} {
    source [file join $Mmucl(rc_dir) mmuclrc]
} 
if {[file exists [file join $Mmucl(rc_dir) charlist]]} {
    source [file join $Mmucl(rc_dir) charlist]
}

# Right click in any entry widget to insert the selection or then 
# the clipboard if present.
bind Entry <Button-3> { 
    if {[catch {selection get}]==0} {
	%W insert insert [selection get]
    } else {
	catch {%W insert insert [selection get -selection CLIPBOARD]}
    }
}

# Shared Mmucl gui procs
#############################################

# Construct a nice dialog box for connecting to a mud either by
# selecting a defined character or typing the location and port in.

proc Mgui_connect {} {
    global character_array Mmucl

    if {[winfo exists .con]} {return}
    
    toplevel .con
    wm title .con "Connect"
    
    frame .con.f -relief groove -bd 2
    grid [label .con.f.hostl -text "Host:"]
    grid [entry .con.f.host -textvariable Mmucl(host)] -row 0 -column 1
    grid [label .con.f.portl -text "Port:"] 
    grid [entry .con.f.port -textvariable Mmucl(port)] -row 1 -column 1
  
    if {[array names character_array]!=""} {
	if {![info exists Mmucl(char)]} {set Mmucl(char) ""}

	grid [label .con.f.lchar -text "Character:"]

	set list [array names character_array]
	set i [lsearch $list $Mmucl(char)]
	set list "$Mmucl(char) [lreplace $list $i $i]"
	eval tk_optionMenu .con.f.char Mmucl(char) $list
	    
	grid .con.f.char -row 2 -column 1

	.con.f.char.menu add separator
	.con.f.char.menu add command \
	    -label None -command {set Mmucl(char) ""}
    } else {set Mmucl(char) ""}

    frame .con.b -relief ridge -bd 2
    pack [button .con.b.ok -text Connect -command \
	      {if {$Mmucl(char)==""} then\
		   {connect $Mmucl(host) $Mmucl(port)}\
		   else {charload $Mmucl(char)};destroy .con}] -side left
    pack [button .con.b.quit -text Cancel -command {destroy .con}] -side left
  
    pack .con.f .con.b
    wm geometry .con +[winfo pointerx .]+[winfo pointery .]
    wm resizable .con 0 0
}

# Useful not Mmucl specific routines. Perhaps collect them in a package?
##########################################

# Use to scroll through a list
#   list is the name of a global history list
#   listloc is the location in the list
#   dir is a direction to go through the list, should be 1 or -1
proc _scrollHistory {list listloc dir} {
    upvar \#0 $list history $listloc loc
    
    incr loc $dir
    if {$loc<-1 || $loc>[llength $history]} {
        incr loc -$dir
        bell
    }
    return [lindex $history $loc] 
}

# dialog box for giving an existing file as an arg to a command
proc tkOpen {title open_cmd {ftypes {{{All Files} {*}}}}} {
    set filename  [tk_getOpenFile -title $title -filetypes $ftypes]
    if {$filename != ""} {eval $open_cmd $filename}
}

# dialog box for giving a file to a command as an arg
proc tkSave {title save_cmd {ftypes {{{All Files} {*}}}}} {
    set filename [tk_getSaveFile -title $title -filetypes $ftypes]
    if {$filename != ""} {eval $save_cmd $filename}
}

# yes/no dialog box that executes cmd on yes.
proc tkDialog {title message cmd} {
    set result [tk_messageBox -message $message \
		    -icon question -title $title -type yesno]
    if {$result=="yes"} {eval $cmd}
}
