# 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.
# 
# misc.tcl - Miscellaneous routines. 
# Copyright (C) 1998 Cygnus Solutions.

# Put up splash graphic. Used for both splash screen and about box.
# type is either 'splash' or 'about'. It is up to the caller to catch errors
# generated by this routine. Returns the name of the window created.

proc sn_splash_dialog {type} {
    global sn_options sn_product_version
    global sn_path sn_product_name sn_suite_name
    global tcl_platform sn_elix

    set w .$type

    Toplevel& $w
    wm withdraw $w

    $w title [list $sn_product_name]
    image create photo cyg-sol -file \
	[file join $sn_path(bitmapdir) splash.gif] -palette 0/0/8

    $w config -bg white

    if {$tcl_platform(platform) == "windows"} {
    	set font_large "Arial 10 bold"
	set font_small "Arial 8 bold"
	set font_super "Arial 6 bold"
    } else {
	set font_large [sn_make_font "" Bold 12]
	set font_small [sn_make_font "" Bold 10]
	set font_super [sn_make_font "" Bold 8]
    }
    label $w.logos -bg white -image cyg-sol
    text $w.text \
    		-bg white -relief flat -width 50 \
	    -highlightthickness 0 \
	    -font $font_large -wrap word
    $w.text tag configure large -font $font_large
    $w.text tag configure small -font $font_small
    $w.text tag configure super -font $font_super -offset 6
    $w.text tag configure center -justify center
    if {$sn_elix} {
	$w.text insert end $sn_suite_name {center large}
	$w.text insert end "\n" {center large}
    }
    $w.text insert end $sn_product_name {center large}
    $w.text insert end "TM " {center super}
    $w.text insert end $sn_product_version {center large}
    $w.text insert end "\n\n" {center large}

    $w.text insert end [get_indep String SplashCopyrightText] \
	    {center large}
    $w.text insert end "\n"
    if {$type == "about"} {
#	$w.text insert end [get_indep String SplashSupportText] {center large}
	
	$w.text insert end "\n\n" {center small}
	$w.text insert end [get_indep String SplashSubCopyrightText] \
	    {center small}
	$w.text insert end "\n\n" {center small}
	$w.text insert end [get_indep String SplashAllRightsReserved] \
	    {center small}
	
	# Make all of the TM strings superscript
	set search_index [$w.text search -exact TM 0.0 end]
	while {$search_index != {}} {
	    $w.text tag add super $search_index "$search_index +2 chars"
	    set search_index [$w.text index "$search_index +2 chars"]
	    set search_index [$w.text search -exact TM $search_index end]
	}
    }

    set last_index [$w.text index end]
    set num_lines [string range $last_index 0 \
		       [expr [string first "." $last_index] - 1]]
    set num_lines [expr $num_lines + 3]
    $w.text configure -height $num_lines

    pack $w.logos -side top
    pack $w.text

    $w.text config -state disabled

    catch {$w resizable no no}
    return $w
}

proc sn_show_version_window { } {

    set w [sn_splash_dialog about]
    sn_motif_buttons $w bottom 0 [get_indep String ok]
    $w.button_0 configure -command "destroy $w; set tkPriv(button) -1"

    $w bind_tk <Return> "catch {$w.button_0 invoke}"
    $w bind_tk <Escape> "catch {$w.button_0 invoke}"

    catch {$w grab set}
    $w move_to_mouse

    set tkPriv(button) ""
    tkwait variable tkPriv(button)
}

proc sn_show_splash_screen {} {
    global splash_start_time

    if {[winfo exists .splash]} {
	return
    }

    set splash_start_time [clock seconds]
    set w [sn_splash_dialog splash]
    $w overrideredirect 1
    $w config_tk -relief solid -borderwidth 2
    $w move_to_mouse
    
    #hide the window after a reasonable time, don't
    #wait in active mode
	global splash_screen_open
	set splash_screen_open "open"
	after 2500 ".splash delete; set splash_screen_open closed"
	tkwait variable splash_screen_open
}


proc find_reusable_window {pat {raise 1}} {
	foreach w [sn_get_windows $pat] {
		upvar #0 $w.reusable reuse
		if {$reuse} {
			if {$raise} {
				if {[wm state $w] != "normal"} {
					wm deiconify $w
				} else {
					raise $w
				}
			}
			return $w
		}
	}
	return ""
}

proc convert_scope_to_num {scope} {
	switch $scope \
		"all" {return [get_indep Pos All]}\
		"cl" {return [get_indep Pos Classes]}\
		"com"	{return [get_indep Pos Commons]}\
		"con" {return [get_indep Pos Const]}\
		"cov"	{return [get_indep Pos CommonsVars]}\
		"iu"	{return [get_indep Pos Includes]}\
		"e"	{return [get_indep Pos Enums]}\
		"ec"	{return [get_indep Pos EnumCons]}\
		"f"	{return [get_indep Pos Files]}\
		"fr"	{return [get_indep Pos Friends]}\
		"fu"	{return [get_indep Pos Functions]}\
		"fd"	{return [get_indep Pos FunctionsDec]}\
		"gv"	{return [get_indep Pos Variables]}\
		"iv"	{return [get_indep Pos ClassVars]}\
		"iu"	{return [get_indep Pos Includes]}\
		"lv"	{return [get_indep Pos PafLocVars]}\
		"ma"	{return [get_indep Pos Defines]}\
		"md"	{return [get_indep Pos Methods]}\
		"mi"	{return [get_indep Pos MethodImps]}\
		"su" {return [get_indep Pos Subr]}\
		"t"	{return [get_indep Pos Typedefs]}\
		"ud"	{return [get_indep Pos Undefined]}\
		"un" {return [get_indep Pos Unions]}
	return 0
}

proc convert_scope_to_str {scope} {
	switch $scope \
		"all" {return [get_indep String All]}\
		"cl" {return [get_indep String Classes]}\
		"com"	{return [get_indep String Commons]}\
		"con" {return [get_indep String Const]}\
		"cov"	{return [get_indep String CommonsVars]}\
		"iu"	{return [get_indep String Includes]}\
		"e"	{return [get_indep String Enums]}\
		"ec"	{return [get_indep String EnumCons]}\
		"f"	{return [get_indep String Files]}\
		"fr"	{return [get_indep String Friends]}\
		"fu"	{return [get_indep String Functions]}\
		"fd"	{return [get_indep String FunctionsDec]}\
		"gv"	{return [get_indep String Variables]}\
		"iv"	{return [get_indep String ClassVars]}\
		"iu"	{return [get_indep String Includes]}\
		"lv"	{return [get_indep String PafLocVars]}\
		"ma"	{return [get_indep String Defines]}\
		"md"	{return [get_indep String Methods]}\
		"mi"	{return [get_indep String MethodImps]}\
		"su" {return [get_indep String Subr]}\
		"t"	{return [get_indep String Typedefs]}\
		"ud"	{return [get_indep String Undefined]}\
		"un" {return [get_indep String Unions]}
	return ""
}

proc convert_scope_to_plain_str {scope} {
	switch $scope \
		"all" {return [get_indep String All]}\
		"cl" {return [get_indep String ClassesNoKey]}\
		"com"	{return [get_indep String CommonsNoKey]}\
		"con" {return [get_indep String ConstantsNoKey]}\
		"cov"	{return [get_indep String CommonsVarsNoKey]}\
		"iu"	{return [get_indep String IncludeNoKey]}\
		"e"	{return [get_indep String Enums]}\
		"ec"	{return [get_indep String EnumConsNoKey]}\
		"f"	{return [get_indep String Files]}\
		"fr"	{return [get_indep String FriendsNoKey]}\
		"fu"	{return [get_indep String FunctionsNoKey]}\
		"fd"	{return [get_indep String FunctionsDecNoKey]}\
		"gv"	{return [get_indep String VariablesNoKey]}\
		"iv"	{return [get_indep String ClassVarsNoKey]}\
		"iu"	{return [get_indep String IncludeNoKey]}\
		"lv"	{return [get_indep String PafLocVars]}\
		"ma"	{return [get_indep String DefinesNoKey]}\
		"md"	{return [get_indep String MethodsNoKey]}\
		"mi"	{return [get_indep String MethodImpsNoKey]}\
		"su" {return [get_indep String SubroutinesNoKey]}\
		"t"	{return [get_indep String Typedefs]}\
		"ud"	{return [get_indep String Undefined]}\
		"un" {return [get_indep String UnionsNoKey]}
	return ""
}


proc convert_scope_to_sym {scope} {
	switch $scope \
		[get_indep String Classes] \
			{return "cl"} \
		[get_indep String Functions] \
			{return "fu"} \
		[get_indep String FunctionsDec] \
			{return "fd"} \
		[get_indep String Commons] \
			{return "com"} \
		[get_indep String CommonsVars] \
			{return "cov"} \
		[get_indep String Methods] \
			{return "md"} \
		[get_indep String MethodImps] \
			{return "mi"} \
		[get_indep String ClassVars] \
			{return "iv"} \
		[get_indep String Typedefs] \
			{return "t"} \
		[get_indep String Variables] \
			{return "gv"} \
		[get_indep String Enums] \
			{return "e"} \
		[get_indep String EnumCons] \
			{return "ec"} \
		[get_indep String Defines] \
			{return "ma"} \
		[get_indep String Subr] \
			{return "su"} \
		[get_indep String Const] \
			{return "con"} \
		[get_indep String Unions] \
			{return "un"} \
		[get_indep String PafLocVars] \
			{return "lv"} \
		[get_indep String Friends] \
			{return "fr"} \
		[get_indep String Includes] \
			{return "iu"} \
		[get_indep String Files] \
			{return "f"} \
		[get_indep String Undefined] \
			{return "ud"} \
		[get_indep String All] \
			{return "all"}

	return ""
}

# Update our display
proc sn_get_windows {{pat ""}} {
	set brws [lmatch [winfo children "."] ".sn-$pat*"]
}


#This function is called after updating project
#like file save, reparse, ...
proc SN_Refresh_Windows {} {
	global sn_statistic_run
	global SN_Refresh_Windows_active
	
	if {! [info exists SN_Refresh_Windows_active]} {
		set SN_Refresh_Windows_active 0
	}
	if {$SN_Refresh_Windows_active} {
		return
	}
	if {[info commands paf_db_f] == ""} {
		return
	}
	incr SN_Refresh_Windows_active 1
	
	#update the scope list
	sn_update_scope_list
	
	#refresh project editors
	Project& @@ Refresh_YourSelf
	
	#refresh statistic windows
	if {$sn_statistic_run} {
		sn_update_statistic
	}
	
	#refresh the availiable Multi Views
	MultiWindow& @@ Refresh_YourSelf
	
	incr SN_Refresh_Windows_active -1
}

#This function is called when XRef process (dbimp) is finished
proc SN_Refresh_XRef_Windows {} {
	global SN_Refresh_XRef_Windows_active
	if {[info exists SN_Refresh_XRef_Windows_active] && $SN_Refresh_XRef_Windows_active} {
		return
	}
	set SN_Refresh_XRef_Windows_active 1
	
	#refresh the availiable Multi Views
	MultiWindow& @@ Refresh_YourSelf_After_XRef
	
	set SN_Refresh_XRef_Windows_active 0
}

proc sn_hide_show_project {cmd {mainw ""}} {
	global sn_options
	global sn_HideShowParams sn_debug
	global iconize_old_close_command

	if {$sn_debug} {
		sn_log "HideShow: $cmd"
		catch {sn_log "  called from [info level -1]"}
	}
	
	#if toplevel is not specified, use any window
	if {$mainw == ""} {
		set mainw [lindex [itcl_info objects "*" -class SymBr&] 0]
	}
	if {$mainw == ""} {
		set mainw [lindex [itcl_info objects "*" -class MultiWindow&] 0]
	}

	if {$cmd == "withdraw"} {
		catch {unset sn_HideShowParams}
		set foc [focus]

		if {$foc != ""} {
			set active [winfo toplevel $foc]
		} else {
			set active $mainw
		}
		foreach win [winfo children .] {
			if {$win == ".loading" || $win == ".sn_pop_menu_listbox"} {
				continue
			}

			if {[catch {set win [winfo toplevel $win]}]} {
				continue
			}
			set geo [wm geometry $win]
			if {$win == $active} {
				set act 1
			} else {
				set act 0
			}

			set sn_HideShowParams($win,EXISTS)	1
			set sn_HideShowParams($win,WIN)		$win
			set sn_HideShowParams($win,GEOM)	$geo
			set sn_HideShowParams($win,STATE)	[wm state $win]
			set sn_HideShowParams($win,ACTIVE)	$act

			if {$mainw == $win} {
				#icon window
				wm iconify $win
				
				#save close command and replace it to close the project
				set iconize_old_close_command [wm protocol $mainw WM_DELETE_WINDOW]
				wm protocol $mainw WM_DELETE_WINDOW "MultiWindow& @@ file_close_project"
			} else {
				if {$sn_options(def,wm-deiconify-withdraw)} {
					wm deiconify $win
				}
				#hide window
				wm withdraw $win
			}
		}
		#use the same window to deiconify
		bind $mainw <Map> "sn_hide_show_project deiconify $mainw"
	} elseif {$cmd == "deiconify"} {
		bind $mainw <Map> " "
		if {[info exists iconize_old_close_command]} {
			wm protocol $mainw WM_DELETE_WINDOW $iconize_old_close_command
		}
		set act_geom ""
		foreach c [array names sn_HideShowParams "*,EXISTS"] {
			set p [lindex [split $c ","] 0]
			set win $sn_HideShowParams($p,WIN)
			set geo $sn_HideShowParams($p,GEOM)
			set sta $sn_HideShowParams($p,STATE)
			set act $sn_HideShowParams($p,ACTIVE)

			catch {
				if {$act == 0} {
					if {$sn_options(def,wm_geometry) && \
						[string range $geo 0 2] != "1x1"} {
						wm geometry $win $geo
					}
					switch $sta {
					"normal" {
							wm deiconify $win
						}
					"iconic" {
							wm iconify $win
						}
					}
				} else {
					set act_geom $geo
					set active $win
				}
			}

		}
		catch {unset sn_HideShowParams}

		if {$sn_options(def,wm_geometry)} {
			catch {wm geometry $active $act_geom}
		}
		catch {wm deiconify $active}
	}
}



