# copyrigth charles vidal author vidalc@club-internet.fr 
global fileid
global toto0 " "
global rgb 
global textmode 
global mixix 
global debug 
global light
global ilace

global shm 
global clear
global stats
global fps
global physics

set currentpad .titi

proc quit {} {
	
    destroy .
    exit
}

# function to into  move the textarea
proc moveintext { indx } {
upvar toto0 t1
 .help.f.t yview  [ lindex $t1 $indx ]
}
proc launch { } {
global filemap 
global fileexec 
global rgb 
global textmode 
global mixix
global debug 
global light
global ilace
global shm 
global clear
global stats
global fps
global physics

set command "$fileexec $debug $light $ilace $shm $clear $stats $fps $physics "
if {$rgb!=""} {append command "$rgb "}
if {$textmode!=""} {append command "-textmode $textmode "}
if {$mixix!=""} {append command " -mixing $mixing" }
#puts "$filemap $rgb  $textmode $mixix $debug $light $ilace $shm $clear $stats $fps $physics"
append command " $filemap"
puts "$command"
eval exec $command
}

proc insertWithTags {w text args} {
    set start [$w index insert]
    $w insert insert $text
    foreach tag [$w tag names $start] {
        $w tag remove $tag $start insert
    }
    foreach i $args {
        $w tag add $i $start insert
    }
}

proc comm2xaos { outstr } {
}
proc scale2xaos { outstr value } {
}
proc check2xaos { outstr boolvalue} {
}
#----------------------------
proc openfilesel { var } {
upvar $var toto
set toto [ tk_getOpenFile -parent . ]
}
#----------------------------
proc changepad { nom } {
global currentpad
pack forget $currentpad
pack $nom -fill both -expand yes
set currentpad  $nom
}

#----------------------------
# mknotepadmisc
# nom the path in window
# manebutton
# variable variable for the checkbutton
# args the liste  of window to insert in frame
# For the args syntaxe are 
# FILE est cree
#	UN LABEL  
#	UN ENTRY
#	UN fileselectionbox 
#	UN BUTTON vers fileselection 
# la valeur file selection de retour sera dans l'ENTRY
# ENTRY est cree
#	UN LABEL  
#	UN ENTRY

# SCALE  
#	1 TEXT
#	2 FROM
#	3 TO
#	4 COMMAND
#	5 VARIABLE
# RADIO
#	1 TEXT
#	2 ????
#	3 VALUE
# ENTRY
# 	1 TEXT
#	2 VARIABLE 
#	3 ?????
# FILE 
#	1  TEXT in  LABEL
#	2 VARIABLE in TEXTE
#	3 TEXT in fileselectionbox
# CHECK
#	1  TEXT
#	2  VARIABLE
#	3  ???  
#	4  ???
#	5a ???
#LABEL 
#	1 TEXT
proc mknotepadmisc { nom manebutton textbutton variable args } {
 set nbf 0
 global $variable
 frame $nom
 set next "#define next_width 16
#define next_height 16
static unsigned char next_bits[] = {
   0x00, 0x00, 0x08, 0x02, 0x18, 0x06, 0x38, 0x0e, 0x78, 0x1e, 0xf8, 0x1e,
   0xf8, 0x3f, 0xf8, 0x7f, 0xf8, 0x7f, 0xf8, 0x3f, 0xf8, 0x1e, 0x78, 0x0e,
   0x78, 0x0e, 0x38, 0x06, 0x18, 0x02, 0x00, 0x00};
"
#image create bitmap logo -data $bitmapstop

 button .boutons.$manebutton -text $textbutton -command "changepad $nom"
 pack .boutons.$manebutton -side left

  foreach i $args {
	switch -regexp  [lindex $i 0] {
		SCALE {
		set  $variable [lindex $i 5]
		scale $nom.e$nbf  -orient horizontal \
				-label [lindex $i 1]  \
				-length 300\
				-from [lindex $i 2]\
				-to [lindex $i 3]\
				-showvalue 1 
        	pack $nom.e$nbf 
		}
        	LABEL   {label $nom.e$nbf -text [lindex $i 1]
        		pack $nom.e$nbf 
			}
        	RADIO   {
			radiobutton $nom.e$nbf -text [lindex $i 1]  \
					-variable [lindex $i 2] \
					-value [lindex $i 3]
        		pack $nom.e$nbf 
			#$nom.e$nbf invoke
			}
		ENTRY {
			frame $nom.f$nbf 
			label $nom.f$nbf.l$nbf  -text [lindex $i 1]
			entry $nom.f$nbf.e$nbf -textvariable [lindex $i 2] 
			pack  $nom.f$nbf.l$nbf $nom.f$nbf.e$nbf -side left
			pack $nom.f$nbf
		}
		FILE {
			frame  $nom.f$nbf 
			label $nom.f$nbf.l$nbf -text [lindex $i 1]
			entry $nom.f$nbf.e$nbf -textvariable [lindex $i 2]
			button $nom.f$nbf.b$nbf -text "File" -command "openfilesel [lindex $i 2]"
			pack $nom.f$nbf 
			pack $nom.f$nbf.l$nbf -side left
			pack $nom.f$nbf.e$nbf -expand y -side left
			pack  $nom.f$nbf.b$nbf  -side right
	      	}
		CHECK {
			checkbutton $nom.e$nbf -variable  [lindex $i 2]\
			-onvalue "[lindex $i 3]" -offvalue "[lindex $i 4]"\
			-text  [lindex $i 1]
			pack $nom.e$nbf  
		}
	    }
        set nbf [ expr $nbf +1 ]
        }
}

# CREATION OF GUI
frame .boutons
pack .boutons
set filescript ""
set filemap "../data/large.zip"
set nbiter ""
set dfplane ""
set fileexec "./cryst"

#
set rgb ""
set textmode "" 
set mixix ""
set debug "-nodebug"
set light "-light"
set ilace "-noilace"
set shm "-shm"
set clear "-noclear"
set stats "-nostats"
set fps "-fps"
set physics "-nophysics"

mknotepadmisc .titi opt "General options"  toto \
	{CHECK debug debug "-debug" "-nodebug" } \
	{CHECK "lighting system" light "-light" -"nolight"} \
	{CHECK  interlacing ilace "-ilace" "-noilace"  } \
	{CHECK "SHM extension" shm "-shm" "-noshm" } \
	{CHECK "clear display every frame" clear "-clear" "-noclear" } \
	{CHECK "statistics" stats "-stats" "-nostats" } \
	{CHECK "frame rate printing" fps "-fps" "-nofps" } \
	{CHECK "preliminary physics system" physics "-physics" "-nophysics" }
	#{SCALE "set the mipmap distance 1" 1 1000 dmipmap1 100 } \
	#{SCALE "set the mipmap distance 3"  1 1000 dmipmap2 100}\
	#{SCALE "set the mipmap distance 2" 1 1000 dmipmap3 100 } 

mknotepadmisc .mixing mix  "Mixix & Files" mixing \
	{LABEL "Set for mixix"}\
	{RADIO true_rgb mixix true_rgb}\
	{RADIO xfast_wrb mixix xfast_wrb}\
	{RADIO fast_wrg mixix fast_wrg}\
	{RADIO nocolor mixix nocolor}\
	{LABEL "Files options"}\
	{FILE "the exec cryst " fileexec }\
	{FILE "pathnman to execute script data" filescript }\
	{FILE "load world file from " filemap }

mknotepadmisc .txtmode bmode "texture & Rgm Mapper" txtmode \
	{LABEL "Set the texture mode"}\
	{RADIO global textmode global}\
	{RADIO private textmode private}\
	{RADIO 24bit textmode 24bit}\
	{LABEL "Fast RGB MAPPER"}\
	{RADIO "use fast (less accurate) RGB mapper" rgb -fastrgb}\
	{RADIO "use slow (accurate) RGB mapper" rgb -slowrgb}

#mknotepadmisc .fastrgb  rgbf  "Fast RGB MAPPER" toto \
#	{RADIO "use fast (less accurate) RGB mapper" rgb -fastrgb}\
#	{RADIO "use slow (accurate) RGB mapper" rgb -slowrgb}

#mknotepadmisc .file file "Files"  fichier \
#	{FILE "pathnman to execute script data" filescript }\
#	{FILE "load world file from " filemap }
	
button .boutons.help -text "Help" -command "source helpread.tcl"
pack .boutons.help  -side left
pack .titi -expand yes 

#the button
frame .button
set f .button
button $f.launch -text "Launch" -command launch
#button $f.save -text "Save"
button $f.exit -text "Exit" -command quit 
pack $f.launch $f.exit -side left
#pack $f.save
pack $f -side bottom 
