#!/usr/bin/wish
#
# $Id $
#
# Formerly, this file was called 'xcec'
# 	MULTI-PLATFORM STARTUP SEQUENCE
# 	(c) 1997 Alexandre Burton
# 	v1.80a (10/08/97)
#
########################################
#
# on Mac:
# 
#    Cecilia:        Main app (wish stub + TCL stuff)
#    Cecilia.shlb:   custom C code
#    TCL librairies: required to run the stub
#    perf:           Csound engine
#    soundapp:       Soundfile info & playing
#    Cecilia Files:  Resources (graphics, HTML) for Cecilia
#    ISSUES:         Bugs & such stuff
#    README:         General Docs
#
#
### Preparation of Wish



wm withdraw .

if {$tcl_platform(platform) == "macintosh"} { 
	set ceclib [pwd]
	catch {load Example.shlb} error
} {
    if [info exists env(CEC_LIBRARY)] {
	if [file exists [file join $env(CEC_LIBRARY) files]] {
	    puts "Cecilia installation located in $env(CEC_LIBRARY) (environment)"
	    set ceclib $env(CEC_LIBRARY)
	} { puts "

error: environmental variable CEC_LIBRARY is set but dangling!
set it so that it points to your Cecilia installation, or place the
distribution in /usr/local/lib/cecilia or /usr/lib/cecilia.

if you're not in charge of this, contact your sysadmin.

now exiting.

"
	    exit 0
	}
    } {
	foreach lib [list /usr/local/lib/cecilia /usr/lib/cecilia [exec pwd]] {
	    if [file exists  [file join $lib files]] {
		puts "Cecilia installation located in $lib (automatic)"
		set ceclib $lib
		break
	    }
	}
	if ![info exists ceclib] {
	    puts "

error: could not locate your installation of Cecilia. Either set your
CEC_LIBRARY environment variable to point to the directory where the
Cecilia support files were installed, or place them either in
/usr/local/lib/cecilia (recommended) or /usr/lib/cecilia for them 
to be found automatically.

if you're not in charge of this, contact your sysadmin.

now exiting.

"

            exit 0
        }
	} 				 
   
}

if {[winfo fpixels . 1i] > 80} {set dpi highres} {set dpi lowres}

# no zoom icon on main window (for Mac)
if {$tcl_platform(platform) == "macintosh"} { 
	unsupported1 style . documentProc  
}

#hide the console on non-unix
if {$tcl_platform(platform) != "unix"} { 
	console hide
}

###
########################################
### debug stuff

proc bug {args} {
    #puts ":: $args"
}

proc modebug {mode args} {
    .temp.statusL config -text $mode 
    .temp.statusR config -text [join $args]
    update ;#idletasks
}

###
########################################
### splash screen

proc showBulle {w} {
	global _db
	if $w {
		.temp.cecile raise bulle3
		set _db(cancel,bulle) [after 100 ".temp.cecile raise bulle2"]
 		lappend _db(cancel,bulle) [after 200 ".temp.cecile raise bulle"]
 		lappend _db(cancel,bulle) [after 200 ".temp.cecile raise texte"]
	} {
		foreach id $_db(cancel,bulle) [after cancel $id]
		after 300 ".temp.cecile lower bulle"
		after 200 ".temp.cecile lower bulle2"
		after 100 ".temp.cecile lower bulle3"
		after 300 ".temp.cecile lower texte"
	}
	
}



proc center {w} {
	update
	set x [expr ([winfo screenwidth $w] - ([winfo width $w] -1))/2]
	set y [expr ([winfo screenheight $w] - ([winfo height $w] -1))/2]
	wm geometry $w +$x+$y 
	update
}

proc buildSplash {} {
	global tcl_platform ceclib dpi
	
	destroy .temp
	toplevel .temp -bg black
	
	### no decoration on splash screen 
	#(Other ways)
	if {$tcl_platform(platform) != "macintosh"} { 
		wm transient .temp
		wm overrideredirect .temp 1
	}
	
	wm withdraw .temp
	
	#(Mac way)
	if {$tcl_platform(platform) == "macintosh"} { 
		unsupported1 style .temp plainDBox  
	}
	#
	###
	
	set cecile [image create photo -file [file join $ceclib files graphics $dpi splash.gif]]
	
	grid columnconfigure .temp  0 -weight 0
#	pack [label .temp.cecile -bg gray20 -image $cecile] -side top
	
	pack [canvas .temp.cecile -width [image width $cecile] \
	  -bg black -height [image height $cecile]] -fill both -expand t
	.temp.cecile create oval 10 110 110 150 -fill \#f6dc71 -tags bulle
	.temp.cecile create oval 80 149 100 160 -fill \#f6dc71 -tags bulle2
	.temp.cecile create oval 100 158 110 165 -fill \#f6dc71 -tags bulle3
	.temp.cecile create text 60 131 -text "getting the Job\ndone... right!" \
	  -tags texte -justify center -font "helvetica 10 bold"
	.temp.cecile create image 0 0 -anchor nw -image $cecile
	.temp.cecile create oval 108 152 142 180 -outline "" -tags hotspot
	.temp.cecile bind hotspot <Enter> "showBulle 1"
	.temp.cecile bind hotspot <Leave> "showBulle 0"

	pack [label .temp.statusL -bg black -fg orange -font "helvetica 9" -text "" -anchor e] -side left
	pack [label .temp.statusR -bg black -fg orange -font "helvetica 9" -text ""] -side left

	update
	center .temp
	center .temp
	
	wm deiconify .temp
	raise .temp
	update
}

proc showfirst {} {
	return
	global ceclib tcl_platform dpi
	destroy .first
	toplevel .first -bg gray20
	bind .first <Button-1> "destroy .first"

	# no decoration on splash screen (Other ways)
	if {$tcl_platform(platform) != "macintosh"} { 
		wm transient .first
		wm overrideredirect .first 1
	}
	wm withdraw .first

	if {$tcl_platform(platform) == "macintosh"} { 
		unsupported1 style .first plainDBox  
	}

	bug [file join $ceclib files graphics first.gif]
	set cecile [image create photo -file [file join $ceclib graphics $dpi first.gif]]
	
	pack [button .first.cecile -bg gray20 -image $cecile] -padx 0 -pady 0
	
	center .first
	center .first
	
	while {[winfo rooty .first] < [winfo rooty .temp]} {
		wm geometry .temp +[expr [winfo rootx .temp]-0]+[expr [winfo rooty .temp]-8]
		after 10
		update idletasks
	}
	wm deiconify .first
	raise .first
	update
}

###
########################################
### source proc

proc loadSourceFiles {} {
	global env ceclib tcl_platform
    foreach item [lsort [glob -nocomplain -- [file join $ceclib lib *.tcl]]] { 
    	modebug "loading code:" [file tail $item]; source $item
    } 
    foreach item [lsort [glob -nocomplain -- [file join $ceclib lib $tcl_platform(platform) *.tcl]]] { 
    	modebug "loading code:" [file tail $item]; source $item
    } 
}

###
########################################
### MAIN

buildSplash

if {$tcl_platform(platform) != "macintosh"} {
	if ![info exists tcload] {puts "Loading the sources on $tcl_platform(os)!";loadSourceFiles}
	if {[llength $argv] > 0} {
    	set arr $argv
	} else {
    	set arr ""
	}
	doCecilia $arr
} {doCecilia ""}
###
########################################
