# 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.
# 
# error.tcl - Routines for error handling.
# Copyright (C) 1998 Cygnus Solutions.

proc sn_error_dialog {TextString {ititle ""} {wait "wait"} {cmd ""}} {
	global sn_debug tkPriv
	global tkeWinNumber

	if {[sn_batch_mode]} {
		if {$ititle != ""} {
			set err "$ititle: $TextString"
		} else {
			set err $TextString
		}
		puts stdout $err
		return
	}
	
	set TextString [string range $TextString 0 999]
	sn_log "Error <$TextString>"
	
	set oldfocus [focus]
	if {[catch {set save_grab [$w grab current]}]} {
		set save_grab ""
	}

	if {$ititle == ""} {
		set title [get_indep String Error]
	} else {
		set title $ititle
	}

	incr tkeWinNumber
	set w .error-$tkeWinNumber
	if {[catch {Toplevel& $w} err]} {
	    return
	}
	$w withdraw
	$w title $title
	$w minsize 100 100
	
	$w on_close "$w delete; set tkPriv($w,button) -1"

	frame $w.msg_bitmap

	if {$ititle != ""} {
		label $w.msg_bitmap.bitmap -image info_image
	} else {
		label $w.msg_bitmap.bitmap -image error_image
	}

	label $w.msg_bitmap.msg\
		-text $TextString\
		-relief flat

	pack $w.msg_bitmap.bitmap -side left -padx 5m
	pack $w.msg_bitmap.msg \
			-side right \
			-expand y \
			-fill both \
			-padx 5 \
			-pady 5

	pack $w.msg_bitmap -expand y -fill both -pady 5

	sn_motif_buttons $w bottom 0 [get_indep String ok]
	
	if {$cmd == ""} {
		$w.button_0 configure \
			-command "destroy $w; set tkPriv($w,button) -1"
	} else {
		$w.button_0 configure \
			-command "eval $cmd; destroy $w; set tkPriv($w,button) -1"
	}

	$w bind_tk <Return> "catch {$w.button_0 invoke}"
	$w bind_tk <Escape> "catch {$w.button_0 invoke}"
	
	catch {$w grab set}
	bind_focus_enter $w "catch {$w focus_tk}"
	
	$w move_to_mouse
	$w take_focus
	
	if {$wait == "wait"} {
		set tkPriv($w,button) ""
		tkwait variable tkPriv($w,button)
		
		if {$save_grab != "" && [winfo exists $save_grab]} {
			catch {grab set $save_grab}
		}
		if {$oldfocus != "" && [winfo exists $oldfocus]} {
			take_focus [winfo toplevel $oldfocus] $oldfocus
		}
	}
}

proc sn_wait_dialog {parent TextString ititle {cmd ""} {ok_str ""}} {
	global sn_debug tkPriv

	set TextString [string range $TextString 0 999]

	set focus [focus]
	if {$focus == ""} {
		set focus "."
	}

	if {$ititle == ""} {
		set title [get_indep String Error]
	} else {
		set title $ititle
	}

	set w $parent.wait_dlg
	catch {destroy $w; $w delete}
	if {[catch {Toplevel& $w} err]} {
	    return
	}
	
	$w withdraw
	$w title $title
	$w minsize 100 100
	set ctrl [winfo toplevel $focus]
	
	if {[wm state $ctrl] == "normal"} {
	    $w transient $ctrl
	}
	$w on_close "$w delete; set tkPriv($w,button) -1"

	frame $w.msg_bitmap

	if {$ititle != ""} {
		label $w.msg_bitmap.bitmap -image info_image
	} else {
		label $w.msg_bitmap.bitmap -image error_image
	}

	label $w.msg_bitmap.msg\
		-text $TextString\
		-relief flat

	pack $w.msg_bitmap.bitmap -side left -padx 5m
	pack $w.msg_bitmap.msg \
			-side right \
			-expand y \
			-fill both \
			-padx 5 \
			-pady 5

	pack $w.msg_bitmap -expand y -fill both -pady 5

	if {$ok_str == ""} {
		set ok_str [get_indep String Cancel]
	}
	sn_motif_buttons $w bottom 0 $ok_str
	
	if {$cmd == ""} {
		$w.button_0 configure \
			-command "destroy $w; set tkPriv($w,button) -1"
	} else {
		$w.button_0 configure \
			-command "eval $cmd; destroy $w; set tkPriv($w,button) -1"
	}

	if {[catch {set save_grab [$w grab current]}]} {
		set save_grab ""
	}
	$w bind_tk <Return> "catch {$w.button_0 invoke}"
	$w bind_tk <Escape> "catch {$w.button_0 invoke}"
	
	catch {$w grab set}
	$w focus_tk
	bind_focus_enter $w "catch {$w focus_tk}"

	$w move_to_mouse
}

