# OpenVerse InitMainWindow
# 
# this file initalizes the program and does any
# platform specific things/setup. It will then source 
# supporting modules.
#
# Module Name		- Init Main Window Module
# Sourced By		- Main Module
#
# Copyright (C) 1999 David Gale <cruise@openverse.com>
# For more information visit http://www.openverse.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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
# USA.

#
# Quick Keys!
# CRUISE - 10/21/2001 - moved this to the top of the file
# Some sourced things needed this process.
#
proc QuickKeys { window } {
	global MV

	# TODO: make this user-configurable

	#bind $window <Alt-a> {
	#	set MV(anim.stop) [expr !$MV(anim.stop)]; AnimateAvatar
	#} ;# Conflicts with Avatars menu
	bind $window <Alt-b> {set MV(bell) [expr !$MV(bell)]}
	bind $window <Alt-d> {ToggleDebug}
	bind $window <Alt-e> {AvEd}
	bind $window <F1> {source "$MV(helpdir)/$MV(help.file.main)"}
	bind $window <Alt-n> {set MV(names) [expr !$MV(names)]; ShowNames}
	bind $window <Alt-t> {ToggleText}
	bind $window <Alt-l> {if $MV(textwindow) {LogTog}}
	bind $window <Pause> {if $MV(textwindow) {ToggleChatLock}}
	bind $window <Alt-s> {StartServer}

	if {!$MV(swallowchat) && $window == "."} {
		bind $window <Control-k> "$MV(chat_entry) insert end \x03"
	}

        # New QuickKeys by djaman
	bind $window <Alt-c> {Disconnect; Connect}
	bind $window <Alt-i> {Disconnect}
	bind $window <Alt-j> {SendToServer "EFFECT jump"}
	bind $window <Alt-k> {KillServer}
	bind $window <Alt-l> {if $MV(textwindow) {LogTog}}
	bind $window <Alt-p> {PassagewayEditor}
	bind $window <Alt-q> {SendToServer "PUSH 100"}
	bind $window <Alt-r> {QueryServers 1}
	#bind $window <Alt-u> {Query_Disconnect} ;# TODO: add this to Query
	bind $window <Alt-v> {SendToServer "EFFECT shiver"}
	bind $window <Alt-w> {SendToServer "WHOIS *"}
	bind $window <Alt-x> {Disconnect; if $MV(log) {StopLog}; exit}
	bind $window <Alt-z> {Setup}
	
	bind $window <Control-b> {AddBookmark}
	#bind $window <Control-e> {EraseChat} ;# Conflicts with <end>
	bind $window <Control-q> {
		if ![tk_dialog .quitQuestion [Trns really_quit]\
				[Trns quit_question] "" 1 [Trns quit]\
				[Trns cancel]] {
			Disconnect
			if $MV(log) {StopLog}
			exit
		}
	}
	bind $window <Control-r> {RotTextChat}
}

set MV(default_host) "$MV(roomhost)"
set MV(default_port) "$MV(roomport)"
if ![file exists "$MV(configfile)"] {
	Setup
	set MV($MV(nick).haschat) 0
	set MV($MV(nick).chatque) {}
} else {
	source "$MV(configfile)"
	#This is a hack to change openverse.com defaults to openverse.com
	# - Sketch, 20011114
	switch -glob -- $MV(roomhost) {
	"*openverse.org*" - "207.0.217.22" - "datapex4.datapex.com" {
		set MV(roomhost) "openverse.com"
	}}
}

# Always source the English file first! this is the default.
source [file nativename "$MV(libbase)/language/English"]
if {$MV(language) != "English"} {
	source [file nativename "$MV(libbase)/language/$MV(language)"]
}

set MV($MV(nick).haschat) 0
set MV($MV(nick).moving) 0
set MV($MV(nick).chatque) {}
set MV($MV(nick).moves) {}

#
# BUG FIX - CRUISE - We load this from the icons dir so that the main room
# cannot be overwritten.
# 10/20/2001
#
image create photo OpenVerse_Image_room -file "$MV(icondir)/room.gif"

frame .menu 
frame .top -bd 0
canvas .top.c -height 480.0 -width 640.0 -highlightthickness 0
.top.c create image 320 240 -image OpenVerse_Image_room
bind .top.c <Button-1> {MoveTo %x %y 0}
bind .top.c <Button-3> {RightClick %x %y %X %Y}

# Get some default gui colors from the system
button .moo
set MV(defhl) [.moo cget -activebackground]
set MV(defbg) [.moo cget -background]
destroy .moo

# Panel Initalization
wm resizable . 0 0
source "$MV(homedir)/panels/$MV(panel)"
wm title . [Trns welcome_msg]
pack .menu -side top -fill both
pack .top.c
pack .top -fill both
QuickKeys "."

if {[file exists "$MV(images)/$MV(avatar)"]} {
	image create photo OpenVerse_Image_pic -file "$MV(images)/$MV(avatar)"
} else {
	image create photo OpenVerse_Image_pic -file "$MV(images)/default.gif"
}
AnimateMe "$MV(anim)" 3
set MV(img) [.top.c create image 320 200 -image OpenVerse_Image_pic]
#if $MV(names) {ShowName $MV(nick)}

# Done with OpenVerse initalization. Init external plugins.
source "$MV(libdir)/Plugin.tcl"

# Some final things to do before we exit.
#
# Delete old images from the remote images directory.
# TODO: conditionally enable this
#
# foreach file  [List_Rimages] {
#	if {[expr [clock seconds] - [file atime $file]] > [expr $MV(keep_images) * 64800]} {
#		catch {file delete $file}
#	}
# }

CheckTimeouts

# Connect!

# A filename can be passed on the command line. If we were given one, read it
# in and connect to the host listed in it.
#
# CRUISE - 10/25/2001 - bug fix for windows arguments which may include spaces
# and / or backslashes in the argument. This seems to make it work now.
if {$argv != ""} {
	if {[string first "\}" $argv] != -1} {
		set narg [join $argv " "]
	} else {
		set narg $argv
	}
	set infile [open [file nativename $narg] r]
	gets $infile input
	close $infile
	set parms [split $input ":"]
	ConnectToRoom [lindex $parms 0] [lindex $parms 1]
} elseif $MV(connect_on_startup) {
	ConnectToRoom $MV(roomhost) $MV(roomport)
}

