# Copyright (c) 2000, Red Hat, Inc.
# 
# This file is part of Source-Navigator.
# 
# Source-Navigator 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, or (at your option)
# any later version.
# 
# Source-Navigator 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 Source-Navigator; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
# 
# window.tcl - Routines for handling windows?
# Copyright (C) 1998 Cygnus Solutions.

proc sn_create_window {w {add_msg 1}} {
	global sn_options
	global sn_path sn_product_name sn_ide
	
	set m $w.menu
	set exp $w.exp
	set date $w.status.date
	set bitpath $sn_path(bitmapdir)
	
	if {![winfo exists $w]} {
		Toplevel& $w -menu $m
	} else {
	        set iconfile paf.xbm
		$w iconbitmap @[file join $bitpath $iconfile]
		$w config -menu $m
	}
	$w minsize 20 1
	
	menu $m -tearoff 0
	
	## File menu
	####################
	menu $m.file \
		-tearoff 0 \
		-postcommand "ProjectMenuEntries_post $m.file subwindow"
	$m add cascade -label [get_indep String EditFile] \
		-menu $m.file \
		-underline [get_indep Pos EditFile]
	
	#project menu entries
	AddProjectMenuEntries $m.file $w
	
	$m.file add separator
	
	#close window
	$m.file add command \
		-command "eval \[wm protocol $w WM_DELETE_WINDOW\]" \
		-label [get_indep String WindowsClose] \
		-underline [get_indep Pos WindowsClose]
	
	$m.file add command \
		-label [get_indep String Exit] \
		-underline [get_indep Pos Exit]\
		-command "sn_exit"
	
	## Edit menu
	menu $m.edit -tearoff 0
	
	$m add cascade \
		-label [get_indep String EditEdit] \
		-menu $m.edit \
		-underline [get_indep Pos EditEdit]
	
	#project preferences
	$m.edit add command \
		-label [get_indep String ProjectPreferencesMenu] \
		-underline [get_indep Pos ProjectPreferencesMenu] \
		-command sn_project_preferences
	
	## History
	AddHistMenu $m
	
	##Windows
	AddWindowsMenu $m $w
	
	##Help
	AddHelpMenu $m $w
	$w config -menu $m
	
	frame $exp -relief groove -bd 2
	
	pack $exp -side top -fill x
	
	frame $w.status
	pack $w.status -side bottom -fill x
	
	global $w.reusable
	set $w.reusable 1
	checkbutton $w.status.reuse \
		-relief groove\
		-text [get_indep String Reuse]\
		-variable $w.reusable \
		-font $sn_options(def,layout-font)
	
	pack $w.status.reuse -side left
	
	if {$add_msg} {
		set info $w.status.msg
		pack [label $info \
				-font $sn_options(def,layout-font) \
				-anchor w \
				-relief groove \
				-bd 2] \
			-side left -anchor w -fill both -expand y
		pack propagate $info 0
	}
}

proc sn_window_add_icons {w buttons} {
	set exp $w.exp
	
	#Hierarchy
	if {[lsearch -exact $buttons "ctree"] != -1} {
		button $exp.tree \
			-takefocus 0 \
			-image tree_image\
			-command "watch y $w;sn_classtree;watch n $w"\
			-state disabled
		bind_history $exp.tree tree
		balloon_bind_info $exp.tree [get_indep String INFOHierarchy]
		pack $exp.tree -side left
	}

	#Class
	if {[lsearch -exact $buttons "class"] != -1} {
		button $exp.class \
			-takefocus 0 \
			-image watch_image\
			-command "watch y $w;sn_classbrowser;watch n $w"\
			-state disabled
		bind_history $exp.class browse
		balloon_bind_info $exp.class [get_indep String INFOStartClassBrw]
		pack $exp.class -side left
	}

	#Xref
	if {[lsearch -exact $buttons "xref"] != -1} {
		button $exp.cross \
			-takefocus 0 \
			-image crossref_image\
			-command "watch y $w;sn_xref;watch n $w"
		bind_history $exp.cross xref
		balloon_bind_info $exp.cross [get_indep String PafCrossRef]
		pack $exp.cross -side left
	}

	#Include
	if {[lsearch -exact $buttons "inc"] != -1} {
		button $exp.inc \
			-takefocus 0 \
			-image include_image\
			-command "watch y $w;sn_include;watch n $w"
		bind_history $exp.inc xref
		balloon_bind_info $exp.inc [get_indep String IncludesINFO]
		pack $exp.inc -side left
	}
}

proc sn_window_append_print {w cmd} {
	set m $w.menu
	set exp $w.exp
	set info $w.status.msg
	
	$m.edit insert 0 separator
	$m.edit insert 0 command \
		-label [get_indep String Print]\
		-underline [get_indep Pos Print]\
		-command $cmd
}


proc sn_set_window_size {this {x ""} {y ""} {width ""} {height ""} {geometry ""}} {
	global sn_options
	#Try to use the 65% size of the screen
	if {![info exists sn_options(def,window-size)] || $sn_options(def,window-size) == ""} {
		set sn_options(def,window-size) 65
	}
	
	#use the geometry manager to set the widthxheight
	#else it doesn't work !!!
	if {$geometry != ""} {
		wm geometry $this $geometry
	} else {
		set geom ""
		if {$width == ""} {
			set width  [expr [winfo screenwidth  .] * $sn_options(def,window-size) / 100]
		}
		if {$width != "-"} {
			append geom $width
		}
		if {$height == ""} {
			set height [expr [winfo screenheight .] * $sn_options(def,window-size) / 100]
		}
		if {$height == "-"} {
			set height [winfo reqwidth $this]
		}
		if {$height != "-"} {
			append geom "x$height"
		}
		if {$x != ""} {
			append geom "+$x"
			append geom "+$y"
		}
		if {$geom != ""} {
			wm geometry $this $geom
		}
		return [list $width $height]
		if {$x != ""} {
			wm geometry $this [expr int($width)]x[expr int($height)]+$x+$y
		} else {
			wm geometry $this [expr int($width)]x[expr int($height)]
		}
	}
	return [list $width $height]
}

