#############################################
##       V.I.P Scilab v1.0 
##	    Video Image Processing by Scilab  
##
##	If you find any bug,just tell us 
##	we will glad to hear from you !
##
##	VIPScilabXMU@hotmail.com
##
##	
##
##############################################

##############################################
#!/bin/sh
# the next line restarts using wish\
#exec wish8.0 "$0" "$@"

##############################################
# get imScilab currdir and set global vars
global imScilab
global tcl_platform
   set imScilab(currdir) [file dirname [info script]]
   set imScilab(currdir) [file dirname $imScilab(currdir)]
   source [file join $imScilab(currdir) routines imScilab_global.tcl]
append auto_path \ \{$imScilab(libdir)\}

if {![info exists imScilab(sourcing)]} {
    switch $tcl_platform(platform) {
	windows {
	}
	default {
	    option add *Scrollbar.width 10
	}
    }
    
}
##################################################################
######  set default font
if {$tcl_platform(platform) == "windows"} {
option add *font "-family {MS Sans Serif} -size 8"
set imScilab(incr) 0
} else {
option add *font "-family Helvetica -size 10"
set imScilab(incr) 6
}

############################
# code to load stock images
#####################################################################
# Thank Stewart Allen & Christian Gavin writer of vTcl
# for gaving instructions on writing the following two procs
# imScilab:image:create_new_image & imScilab:image:get_image

proc imScilab:rename {name} {

    regsub -all "\\." $name "_" ret
    regsub -all "\\-" $ret "_" ret
    regsub -all " " $ret "_" ret
    regsub -all "/" $ret "__" ret
    regsub -all "::" $ret "__" ret
    
    return [string tolower $ret]
}


proc imScilab:image:create_new_image {filename description type} {
	set filename [string tolower $filename]
	set description [string tolower $description]
	set type [string tolower $type]
	global imScilab env

	# image already existing ?
	if [info exists imScilab(images,files)] {

		set index [lsearch -exact $imScilab(images,files) $filename]

		if {$index != "-1"} {
			# cool, no more work to do
			return
		}
	}

	# wait a minute... does the file actually exist?
	if {! [file exists $filename] } {

		# try current directory
		set script [file dirname [info script]]
		set filename [file join $script [file tail $filename] ]

		# puts "looking for $filename..."
	}
	if {! [file exists $filename] } {
		#try image dir
		set filename [file join $imScilab(imagedir) [file tail $filename]]
	}


	if {! [file exists $filename] } {
		set description "file not found!"
		set imScilab(broken) [imScilab:image:broken_image]
		set imScilab(object) [image create photo -data $imScilab(broken) ]

	} else {

		set imScilab(object) [image create  [imScilab:image:get_creation_type $filename]  -file $filename]
	}

	set reference [imScilab:rename $filename]

	set imScilab(images,$reference,image)       $imScilab(object)
	set imScilab(images,$reference,description) $description
	set imScilab(images,$reference,type)        $type
	set imScilab(images,filename,$imScilab(object))       $filename

	lappend imScilab(images,files) $filename

	# return image name in case caller might want it
	return $imScilab(object)
}

proc imScilab:image:get_image {filename} {
   set filename [string tolower $filename]
	global imScilab
	set reference [imScilab:rename $filename]

	# let's do some checking first
	if {! [info exists imScilab(images,$reference,image)] } {

		# well, the path may be wrong; in that case check
		# only the filename instead, without the path

		set imageTail [file tail $filename]

		foreach oneFile $imScilab(images,files) {

			if { [file tail $oneFile] == $imageTail } {

				set reference [imScilab:rename $oneFile]
				break
			}
		}
	}

	return $imScilab(images,$reference,image)
}

proc imScilab:image:get_creation_type {filename} {
        global imScilab
        set filename [string tolower $filename]
	set ext [file extension $filename]
	set ext [string tolower $ext]

	switch $ext {

		.ppm -
		.gif    {return photo}
		.xbm    {return bitmap}

		default {return photo}
	}
}

proc imScilab:image:broken_image {} {
    return {
        R0lGODdhFAAUAPcAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/
        AP//AAAA//8A/wD//////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAACwAAAAAFAAUAAAIhAAPCBxIsKDBgwgPAljIUOBC
        BAkBPJg4UeBEBBAVPkCI4EHGghIHChAwsKNHgyEPCFBA0mFDkBtVjiz4AADK
        mAds0tRJMCVBBkAl8hwYMsFPBwyE3jzQwKhAoASUwmTagCjDmksbVDWIderC
        g1174gQ71CHFigfOhrXKUGfbrwnjyp0bEAA7}
}

############################
# code to load user images

imScilab:image:create_new_image "22.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "open.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "copy.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "paste.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "undo.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "redo.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "new.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "save.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "cut.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "find.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "aboutus.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "ourgroup.gif" \
    "user image" \
    "user"  
imScilab:image:create_new_image "editor.gif" \
    "user image" \
    "user"   
imScilab:image:create_new_image "avi.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "bg1.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "bg.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "main_help.gif" \
    "user image" \
    "user"    
imScilab:image:create_new_image "vip_banner.gif" \
    "user image" \
    "user"
        
##
imScilab:image:create_new_image "text_new.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_new_g.GIF" \
    "user image" \
    "user"    
imScilab:image:create_new_image "text_open.GIF" \
    "user image" \
    "user"    
imScilab:image:create_new_image "text_open_g.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_save.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_save_g.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_run.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_run_g.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_clear.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_clear_g.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "editor_s.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "editor_s_g.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "font.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "font_g.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_find.GIF" \
    "user image" \
    "user"
imScilab:image:create_new_image "text_find_g.GIF" \
    "user image" \
    "user"  
####

#############################################################################
# imScilab v1.0 procedures
#

#################################
# GLOBAL VARIABLES
#

global Turn off; 
global Turn on; 
global classNewId;  
global textRedoer;
global textUndoer;
global udLifo;
global imScilab;
	set imScilab(imageNum) -1
global widget; 
    set widget(.vTcl.fontmgr.listbox) {.vTcl.fontmgr.fra28.cpd29.01}
    set widget(.vTcl.fontmgr.text) {.vTcl.fontmgr.cpd43.03}
    set widget(codetext) {.codeEditor.cpd45.02}
    set widget(main,HelpMsg) {.main.fra43.fra24.fra45.mes18}
    set widget(main,TextHelpMsg) {.codeEditor.fra47.fra45.mes18}
    set widget(rev,.codeEditor.cpd45.02) {codetext}
    set widget(rev,.vTcl.fontmgr.cpd43.03) {.vTcl.fontmgr.text}
    set widget(rev,.vTcl.fontmgr.fra28.cpd29.01) {.vTcl.fontmgr.listbox}
    
set imScilab(broken) [imScilab:image:broken_image]


#################################
# USER DEFINED PROCEDURES
#
proc {tempdir} {} {
     global imScilab
     file mkdir [file join $imScilab(currdir) temp]
}

proc {Redo} {} {
global widget
global imScilab
eval textRedoer:redo $imScilab(undoID)
imScilab:change
}

proc {image_redo} {} {
global imScilab
	set tempID $imScilab(changeID$imScilab(currimage))
	ScilabEval "                     
	imageRedo($imScilab(currimage),$tempID);"
	set imScilab(changeID$imScilab(currimage)) [expr $imScilab(changeID$imScilab(currimage))+1]
	set imScilab(image$imScilab(currimage),change) 1
	set imScilab(image$imScilab(currimage),storagetype) $imScilab(image$imScilab(currimage),typestack$imScilab(changeID$imScilab(currimage)))
}


proc {UnDodelete} {className id} {
# calls the destructor for the class and UnDodelete all the imScilab(object) data members

     if {[llength [info procs ${className}:~$className]]>0} {
         # avoid catch to track errors
         catch {${className}:~$className $id}
     }
     global $className
     # and UnDodelete all this imScilab(object) array members if any (assume that they were stored as $className($id,memberName))
     foreach name [array names $className "$id,*"] {
         unset ${className}($name)
     }
}

proc {UnDonew} {className args} {
# calls the constructor for the class with optional arguments
     # and returns a unique imScilab(object) identifier independent of the class name

     global classNewId
     # use local variable for id for new can be called recursively
     set id [incr classNewId]
     if {[llength [info procs ${className}:$className]]>0} {
         # avoid catch to track errors
         eval ${className}:$className $id $args
     }
     return $id
}

proc {Undo} {} {
global widget
global imScilab
eval textUndoer:undo $imScilab(undoID)	
imScilab:~change
}

proc {image_undo} {} {
global imScilab
set tempID [expr $imScilab(changeID$imScilab(currimage))-1]
	ScilabEval "                     
	imageUndo($imScilab(currimage),$tempID);"
	set imScilab(changeID$imScilab(currimage)) [expr $imScilab(changeID$imScilab(currimage))-1]
	set imScilab(image$imScilab(currimage),storagetype) $imScilab(image$imScilab(currimage),typestack$imScilab(changeID$imScilab(currimage)))
}

#######################		Image File Ctrl		##############
proc {image_file_save_as} {} {
global widget
global imScilab

	set file_types {
	        {"BMP" { .BMP .RLE .DIB}  }
		{"JPEG" { .JPG .JPEG .JPE } }
		{"GIF" { .GIF} }
		{"PNG" { .PNG} }
		{"PCX" { .PCX} }
		{"TIFF" {.TIF .TIFF} }
		{"All Files" * }
	}
	set imScilab(tail) [file tail $imScilab(openedfile)]
	set imScilab(openedfile) [tk_getSaveFile -filetypes $file_types -initialdir pwd -initialfile $imScilab(tail) -defaultextension .bmp -parent .main]
###	
	if {$imScilab(openedfile) != "" } {
	ScilabEval "imagesave($imScilab(currimage),'$imScilab(openedfile)');"
###	
	set imScilab(status_hint) $imScilab(openedfile)
	wm title .main "VIP for Scilab2.7(win) - $imScilab(status_hint)"
	}
	imScilab:image_reset_change $imScilab(currimage)
}

proc {image_get_file} {} {
global widget
global imScilab
if {$imScilab(image_new) == 0} {
 uplevel #0 {set imScilab(image_new) 1}
 } else {return}
if {$imScilab(image_change) > 0} {
   set ans [tk_messageBox -message "File Unsaved! Save Now?" -type yesnocancel -default no -title "imScilab"]
       if {$ans == "yes" } {image_file_save_as}
       if {$ans == "cancel"} {set imScilab(image_new) 0;return}
}
########begin open file ###

	set file_types {
	        {"BMP" { .BMP .RLE .DIB}  }
		{"JPEG" { .JPG .JPEG .JPE } }
		{"GIF" { .GIF} }
		{"PNG" { .PNG} }
		{"PCX" { .PCX} }
		{"TIFF" {.TIF .TIFF} }
		{"All Files" * }
	}
	set imScilab(openedfile) [tk_getOpenFile -filetypes $file_types -initialdir pwd -parent .main]
	set imScilab(status_hint) $imScilab(openedfile)
	wm title .main "VIP for Scilab2.7(win) - $imScilab(status_hint)"

   	if {$imScilab(openedfile) != ""} {	
###
	set imScilab(createimageSum) 0
	for {set i 0} {$i<4} {incr i 1} {
	if { $imScilab(createimage$i) == 1 } {
	set imScilab(imageNum) $i
	set imScilab(createimage$i) 0
	set imScilab(imagepath$i) $imScilab(openedfile)
	break
	} else {
	set imScilab(createimageSum) [expr $imScilab(createimageSum)+1]
	}
	}
	if {$imScilab(createimageSum) == 4} {
	tk_messageBox -message "You have got 4 pictures!"
	return
	}
	set imScilab(avi,exposeclose) 1
	set imScilab(imageready) 1
	set imScilab(changeID$imScilab(imageNum)) [expr $imScilab(changeID$imScilab(imageNum))+1]
		
	ScilabEval "imageopen($imScilab(imageNum),'$imScilab(openedfile)');"
	ScilabEval "                                                                      
	imagePush($imScilab(imageNum),$imScilab(changeID$imScilab(imageNum)));TK_EvalStr('set imScilab(avi,exposeclose) 0');image$imScilab(imageNum);map$imScilab(imageNum);"
	
	set imScilab(image,outputCode) xset('window',$imScilab(imageNum))\;\[image$imScilab(imageNum),map$imScilab(imageNum)\]=imread('$imScilab(openedfile)')\;imshow(image$imScilab(imageNum),map$imScilab(imageNum))\;
	import_data $imScilab(image,outputCode)
	imScilab:imageInfobtn $imScilab(imageNum)
	set imScilab(currimage) $imScilab(imageNum)
	set imScilab(imageChoose) image$imScilab(currimage)	
	set imScilab(imageState$imScilab(imageNum)) normal
	
	imScilab:image_change_savestate normal
	imScilab:image_reset_change $imScilab(imageNum)
	}
 uplevel #0 {set imScilab(image_new) 0}
}

proc {image_save} {} {
global widget
global imScilab
if {$imScilab(imagepath$imScilab(currimage))!=""} {
	if {$imScilab(image$imScilab(currimage),storagetype)!="indexed"} {
	ScilabEval "imwrite(image$imScilab(currimage)*255,'$imScilab(imagepath$imScilab(currimage))');"
	} else {
	ScilabEval "imwrite(image$imScilab(currimage),map$imScilab(currimage),'$imScilab(imagepath$imScilab(currimage))');"
	}	
	set imScilab(status_hint) $imScilab(imagepath$imScilab(currimage))
	wm title .main "VIP for Scilab2.7(win) - $imScilab(status_hint)"
}

imScilab:image_reset_change $imScilab(currimage)
}
#######################		Image File Ctrl End Here	#########

#######################		Text File Ctrl 		###############

proc {file_save_as} {} {
global widget
global imScilab

	set data [$widget(codetext) get 1.0 {end -1c}]
	set file_types {
	        {"Scilab Script" { .sci}  }
		{"Tcl Files" { .tcl .TCL .tk .TK} }
		{"Text Files" { .txt .TXT} }
		{"All Files" * }
	}

	set imScilab(openedtextfile) [tk_getSaveFile -filetypes $file_types -initialdir pwd -initialfile "New File" -defaultextension .sci -parent .codeEditor]
	focus .codeEditor
	focus $widget(codetext)
	if {$imScilab(openedtextfile) == "" } {
	return
	}
	set imScilab(status_hint) $imScilab(openedfile)
	wm title .main "VIP for Scilab2.7(win) - $imScilab(status_hint)"
	set fileid [open $imScilab(openedtextfile) w]
	puts -nonewline $fileid $data
	close $fileid
	imScilab:reset_change
}

proc {get_file} {} {
global widget
global imScilab

if {$imScilab(new) == 0} {
 uplevel #0 {set imScilab(new) 1}
 } else {return}
if {$imScilab(change) > 0} {
   set ans [tk_messageBox -message "File Unsaved! Save Now?" -type yesnocancel -default no -title "imScilab"]
       if {$ans == "yes" } {save}
       if {$ans == "cancel"} {set imScilab(new) 0;return}
}
### ###begin open file ###

	set file_types {
	        {"Scilab Scripts" {.sci}          }
		{"Tcl Files" { .tcl .TCL .tk .TK} }
		{"Text Files" { .txt .TXT } }
		{"All Files" * }
	}
	set imScilab(openedtextfile) [tk_getOpenFile -filetypes $file_types -initialdir pwd -parent .codeEditor]
	set imScilab(status_hint) $imScilab(openedtextfile)
	wm title .main "VIP for Scilab2.7(win) - $imScilab(status_hint)"
	focus .codeEditor 
	focus $widget(codetext)
#	catch {grab -global .codeEditor}
   if {$imScilab(openedtextfile) != "" } {
	set filesize [file size $imScilab(openedtextfile)]
	$widget(codetext) delete 1.0 end
	set fileid [open $imScilab(openedtextfile) r]
	set data [read $fileid $filesize]
        close $fileid
	$widget(codetext) insert end $data
	imScilab:reset_change
	}
 uplevel #0 {set imScilab(new) 0}
}

proc {new} {} {
global widget
global imScilab
  if {$imScilab(new) == 0} {
	 uplevel #0 {set imScilab(new) 1}
   } else {return}
  if {$imScilab(change) > 0} {
       set ans [tk_messageBox -message "File Unsaved! Save Now?" -type yesnocancel -default no -title "imScilab"]
       if {$ans == "yes" } {save}
       if {$ans == "cancel"} {set imScilab(new) 0;return}
}

    $widget(codetext) delete 1.0 end
	set imScilab(openedtextfile) "New File"
	set imScilab(status_hint) $imScilab(openedtextfile)
	wm title .main "VIP for Scilab2.7(win) - $imScilab(status_hint)"

	textRedoer:reset $imScilab(undoID)
	textUndoer:reset $imScilab(undoID)
	focus .codeEditor
	focus $widget(codetext)
    imScilab:reset_change
 set imScilab(new) 0
}

proc {save} {} {
global widget
global imScilab
if {$imScilab(openedtextfile) == "" } {
	set imScilab(openedtextfile) "NewEdit"
	file_save_as
	focus $widget(codetext)
return
}

if {$imScilab(openedtextfile) == "New File" } {
	set imScilab(openedtextfile) "NewEdit"
	file_save_as
	focus $widget(codetext)
return
}



	set data [$widget(codetext) get 1.0 {end -1c}]
	set fileid [open $imScilab(openedtextfile) w]
	puts -nonewline $fileid $data
	close $fileid

	focus .codeEditor
	focus $widget(codetext)

imScilab:reset_change
}

#######################		Text File Ctrl End Here		##########




#############################	close	
proc {imScilab:close} {} {
global widget
global imScilab

if {$imScilab(quit) == 0 } {uplevel #0 { set imScilab(quit) 1}
} else {return}
if {$imScilab(change) > 0} {
        set ans [tk_messageBox -message "File Unsaved! Save Now?" -type yesnocancel -default no -title "imScilab"]
       if {$ans == "yes" } {save}
       if {$ans == "cancel"} {
           uplevel #0 {set imScilab(quit) 0 };
           return
           }
   }
if {$imScilab(image_change) > 0} {
        set ans [tk_messageBox -message "Image_File Unsaved! Save Now?" -type yesnocancel -default no -title "imScilab"]
       if {$ans == "yes" } {image_file_save_as
       return}
       if {$ans == "cancel"} {
           uplevel #0 {set imScilab(quit) 0 };
           return
           }
   }
   
      set ans [tk_messageBox -message "Are you sure you want to quit?" -type yesno -default no -title "imScilab"]
       if {$ans == "no" } {uplevel #0 {set imScilab(quit) 0 }; return;}
    imScilab:quit
}



proc {imScilab:quit} {} {
global widget
global imScilab

### clear the temp directory
tempdir

### save system information to start.ini

set fileid [open [file join $imScilab(mainfuncdir) "start.tcl"] "w+" ]
   puts $fileid "global imScilab\n"
   puts $fileid "set imScilab(fontsize) $imScilab(fontsize)"
   if {$imScilab(openedfile) != "" } {
      puts $fileid "set imScilab(lastfile) \"$imScilab(openedfile)\""
      }
      
close $fileid
ScilabEval "stacksize(1e7);"

### destroy existing windows

set win [winfo children .]
  foreach i $win {
  destroy $i
}
}
#######################		quit end here		#################

#######################		Other Function		##########
proc {imScilab:run} {} {
global imScilab
global widget
set fixbugstream " "
append fixbugstream [$widget(codetext) get 1.0 {end-1c} ]	
regsub -all "\n" $fixbugstream "" fixbugstream
ScilabEval $fixbugstream
}

proc {imScilab:about} {} {
global imScilab
    toplevel .a -bd 3 -relief raised
    wm withdraw .a
    set sw [winfo screenwidth .]
    set sh [winfo screenheight .]
    image create photo "AboutUs"  -file [file join $imScilab(imagedir) ourgroup.gif]
    	wm overrideredirect .a 1
    	label .a.l -image AboutUs -bd 1 -relief sunken
    	pack .a.l -side top -expand 1 -fill both
    	set x [expr {($sw - 560)/2}]
   	set y [expr {($sh - 370)/2}]
    wm geometry .a +$x+$y
    bind .a <Button-1> {
    		catch {destroy .a}
        }
    wm deiconify .a
    update idletasks
    focus -force .a
    grab -global .a
}

###	change
proc {imScilab:change} {} {
global widget
global imScilab
uplevel #0 {incr imScilab(change) 1}
}
proc {imScilab:image_change} {imageID} {
global widget
global imScilab
set imScilab(image$imageID,change) 1
}
proc {imScilab:avi_change} {} {
global widget
global imScilab
uplevel #0 {incr imScilab(avi_change) 1}
}

proc {imScilab:reset_change} {} {
global widget
global imScilab
uplevel #0 {set imScilab(change) 0}
}

proc {imScilab:image_reset_change} {imageID} {
global widget
global imScilab
set imScilab(image$imageID,change) 0
}
proc {imScilab:avi_reset_change} {} {
global widget
global imScilab
uplevel #0 {set imScilab(avi_change) 0}
}

proc {imScilab:~change} {} {
global widget
global imScilab
uplevel #0 {incr imScilab(change) -1}
uplevel #0 {set imScilab(image_change) -1}
}
###	change end here


###	font set
proc {imScilab:changeFont} {wd fontsize} {
$wd configure -font "-size $fontsize"
}

proc {imScilab:set_font} {args} {
global widget
global imScilab
if {[llength $args] == 0 } {
	   set sz $imScilab(fontsize)
	} else {
  	set sz [lindex $args 0]
	}
set sz [expr $sz + $imScilab(incr)]
set imScilab(fontsize) $sz
imScilab:changeFont $widget(codetext) $sz
incr sz
imScilab:changeFont $widget(outputtext) $sz
}
###	font set end here

###	Load Other Tcl Script
proc {imScilab:loadFun} {name} {
global imScilab
	if {![winfo exists .$name]} {
		source [file join $imScilab(imfuncdir) $name.tcl]
	} else {
		Window show .$name
	}
}
###

###	toggle CodeEditor
proc {toggle_win} {winname} {
global widget
global imScilab
upvar imScilab(gui,$winname) win
if {$win == 1 } {
# hide window named winid
  set win 0
  eval Window hide .$winname
  return 0
} else {
 # eval set imScilab(gui,$winname) 1
  set win 1
  eval Window show {.$winname}
  eval wm deiconify {.$winname}
  return 1
}
}
###

###	
proc {imScilab:ErrMsg} {number} {
global widget imScilab
	switch $number {
		0 {set imScilab(ErrMsg) "Required parameter insufficient"}
		1 {set imScilab(ErrMsg) "Matrix overflow"}
		{set imScilab(ErrMsg) "Unknown Error"}
	}
	tk_messageBox -message $imScilab(ErrMsg)
}
###

###	import data to Scilab
proc {import_data} {args} {
global widget
global imScilab
#$widget(codetext) delete 1.0 end
  if {[llength $args] == 0 } {
  	 set data $imScilab(codeEditor,in_dump)
} else {
  	set data [lindex $args 0]
}

  if {$data == "" } { return 0}

  $widget(codetext) insert insert $data
 return 1
}
###

###	help index
proc {imScilab:userguide} {} {
global widget imScilab tcl_platform env
if {$tcl_platform(platform) == "windows"} {
set tmp [file join $imScilab(html) User\ Guide.htm]
regsub -all "/" $tmp "\\" tmp
	exec $env(COMSPEC) /c $tmp &
} else {
	set imScilab(guide) {file:}
	append imScilab(guide) $imScilab(html)
	append imScilab(guide) /
	append imScilab(guide) {User\ Guide.htm}
	exec mozilla $imScilab(guide) &
}
}


proc {imScilab:showHelpIndex} {} {
global widget imScilab tcl_platform env
if {$tcl_platform(platform) == "windows"} {
        set tmp [file join $imScilab(html) home.htm]
        regsub -all "/" $tmp "\\" tmp
        exec $env(COMSPEC) /c $tmp &
} else {
	set imScilab(help) {file:}
	append imScilab(help) $imScilab(html)
	append imScilab(help) /
	append imScilab(help) {home.htm}
	exec mozilla $imScilab(help) &
}
}
###

#######################		animated_gif	###################

proc update_animated_gif {option num} {
    global imScilab
    set imScilab(giflab) .splash.lab
    
    if {[info exists imScilab(sp,args)]} {
	set im [image create photo ".splash.lab_[expr $num+1]"]
	$im copy $imScilab(sp,$num)
	incr num
	if {[catch "$im configure $imScilab(sp,args) -format \{gif -index $num\}"]} {
	    unset imScilab(sp,args)
	    if {$num>1} {
		set num 0
		set im $imScilab(sp,$num)
	    } else {
		# this is not an animated GIF; just stop
		unset imScilab(sp,0)
		return
	    }
	} else {
	    $im configure -data {} -file {};#free storage
	    set imScilab(sp,$num) $im
	}
    } else {
	incr num
	if {![info exists imScilab(sp,$num)]} {
	    set num 0
	}
	set im $imScilab(sp,$num)
    }
    if {[catch ".splash.lab $option $im"]} {
	catch {unset imScilab(sp,args)}
	foreach im [array names imScilab sp,*] {
	    image delete $imScilab($im)
	    unset imScilab($im)
	}
    } else {
	update
	after 100 [list update_animated_gif $option $num]
    }
}

proc show_animated_gif {args} {
    global imScilab
    
    toplevel .splash -bd 3 -relief raised
    wm withdraw .splash
    set sw [winfo screenwidth .]
    set sh [winfo screenheight .]
    wm overrideredirect .splash 1
    label .splash.lab
    pack .splash.lab -side top -expand 1 -fill both
    set x [expr {($sw - 400)/2}]
    set y [expr {($sh - 400)/2}]
    wm geometry .splash +$x+$y
    wm deiconify .splash
    
    if {[catch "image create photo .splash.lab_0 $args -format \{gif -index 0\}" image]} {
	.splash.lab configure -text [mc "error displaying animated gif"]:\n$image" \
		-relief ridge
    } else {
	$image configure -data {} -file {} -format {};# free storage
	.splash.lab configure -image $image
	set imScilab(sp,0) $image
	set imScilab(sp,args) $args
	update idletasks
	after 100 [list update_animated_gif {configure -image} 0]
    }
    update
    after 1000 { catch { destroy .splash } }
}

##############################

##############################	image process start here	##########################

proc {imScilab:image_change_savestate} {changestate} {
.main.fra17.fra17.btnimsave configure -state $changestate
}

proc {imScilab:image_change_filterstate} {changestate} {
.main.fra17.fra17.btnimsave configure -state $changestate
}

proc {imScilab:im2gray} {} {
global imScilab

if {$imScilab(imageChoose) == "AviFrame" } {
ScilabEval "         aviframe;aviframe=im2gray(aviframe);aviframe_temp=aviframe;impreview(avilabid,aviframe_temp,320,240);"
append imScilab(aviprocess) aviframe=im2gray\(aviframe\)\;
append imScilab(avibsubprocess) aviframe_sub_bsub=im2gray\(aviframe_sub_bsub\)\;
append imScilab(avitdiffprocess) aviframe_sub_tdiff=im2gray\(aviframe_sub_tdiff\)\;

set imScilab(image4,storagetype) "gray"
set imScilab(aviframe,storagetype) "gray"
imScilab:avi_change
} else {
set imScilab(changeID$imScilab(currimage)) [expr $imScilab(changeID$imScilab(currimage))+1]
ScilabEval "image$imScilab(currimage);map$imScilab(currimage);image$imScilab(currimage)=im2gray(image$imScilab(currimage),map$imScilab(currimage));map$imScilab(currimage)=\[\];imshow(image$imScilab(currimage));imagePush($imScilab(currimage),$imScilab(changeID$imScilab(currimage)));"
set imScilab(image$imScilab(currimage),storagetype) "gray"
set imScilab(image$imScilab(currimage),typestack$imScilab(changeID$imScilab(currimage))) "gray"
imScilab:image_change $imScilab(currimage)
}
}

proc {imScilab:ind2rgb} {} {
global imScilab
set rgb 0
if {$imScilab(imageChoose) == "AviFrame" } {
tk_messageBox -message "Current image is not an index image!"
return
}
if { $imScilab(image$imScilab(currimage),storagetype) == "indexed"} {

set imScilab(changeID$imScilab(currimage)) [expr $imScilab(changeID$imScilab(currimage))+1]
ScilabEval "         image$imScilab(currimage);image$imScilab(currimage)=ind2rgb(image$imScilab(currimage),map$imScilab(currimage));imshow(image$imScilab(currimage));imagePush($imScilab(currimage),$imScilab(changeID$imScilab(currimage)));"
set imScilab(image$imScilab(currimage),storagetype) "truecolor"
set imScilab(image$imScilab(currimage),typestack$imScilab(changeID$imScilab(currimage))) "truecolor"
imScilab:image_change $imScilab(currimage)
} else {
tk_messageBox -message "Current image is not an index image!"
return
}
}

proc {imScilab:rgb2yiq} {} {
global imScilab
set rgb 0
set yiq 0
if {$imScilab(currimage) == 4} {
if {$imScilab(imageChoose) == "AviFrame" && $imScilab(aviframe,storagetype) == "truecolor"} {
ScilabEval " aviframe;aviframe=rgb2ntsc(aviframe);aviframe_temp=aviframe;impreview(avilabid,aviframe_temp,320,240);"
append imScilab(aviprocess) aviframe=rgb2ntsc\(aviframe\)\;
append imScilab(avibsubprocess) aviframe_sub_bsub=rgb2ntsc\(aviframe_sub_bsub\)\;
append imScilab(avitdiffprocess) aviframe_sub_tdiff=rgb2ntsc\(aviframe_sub_tdiff\)\;
imScilab:avi_change
} else {
tk_messageBox -message "Current image is not an turecolor image!"
}
} else {
if {$imScilab(image$imScilab(currimage),storagetype) == "truecolor"} {

set imScilab(changeID$imScilab(currimage)) [expr $imScilab(changeID$imScilab(currimage))+1]
ScilabEval "         image$imScilab(currimage);image$imScilab(currimage)=rgb2ntsc(image$imScilab(currimage));imshow(image$imScilab(currimage));imagePush($imScilab(currimage),$imScilab(changeID$imScilab(currimage)));"
imScilab:image_change $imScilab(currimage)
set imScilab(image$imScilab(currimage),typestack$imScilab(changeID$imScilab(currimage))) "truecolor"
} else {
tk_messageBox -message "Current image is not an turecolor image!"
return
}
}
}

proc {imScilab:yiq2rgb} {} {
global imScilab
set rgb 0
set yiq 0
if {$imScilab(currimage) == 4} {
if {$imScilab(imageChoose) == "AviFrame" && $imScilab(aviframe,storagetype) == "truecolor"} {
ScilabEval "        aviframe(:,:,2)=aviframe(:,:,2)/4;aviframe(:,:,3)=aviframe(:,:,3)/4;aviframe=ntsc2rgb(aviframe);aviframe_temp=aviframe;impreview(avilabid,aviframe_temp,320,240);"
append imScilab(aviprocess) aviframe\(:,:,2\)=aviframe\(:,:,2\)\/4\;aviframe\(:,:,3\)=aviframe\(:,:,3\)\/4;aviframe=ntsc2rgb\(aviframe\)\;
append imScilab(avibsubprocess) aviframe_sub_bsub\(:,:,2\)=aviframe_sub_bsub\(:,:,2\)\/4\;aviframe_sub_bsub\(:,:,3\)=aviframe_sub_bsub\(:,:,3\)\/4;aviframe_sub_bsub=ntsc2rgb\(aviframe_sub_bsub\)\;
append imScilab(avitdiffprocess) aviframe_sub_tdiff\(:,:,2\)=aviframe_sub_tdiff\(:,:,2\)\/4\;aviframe_sub_tdiff\(:,:,3\)=aviframe_sub_tdiff\(:,:,3\)\/4;aviframe_sub_tdiff=ntsc2rgb\(aviframe_sub_tdiff\)\;

imScilab:avi_change
} else {
tk_messageBox -message "Current image is not an turecolor image!"
}
} else {
if {$imScilab(image$imScilab(currimage),storagetype) == "truecolor"} {

set imScilab(changeID$imScilab(currimage)) [expr $imScilab(changeID$imScilab(currimage))+1]
ScilabEval "        image$imScilab(currimage)(:,:,2)=image$imScilab(currimage)(:,:,2)/4;image$imScilab(currimage)(:,:,3)=image$imScilab(currimage)(:,:,3)/4;image$imScilab(currimage)=ntsc2rgb(image$imScilab(currimage));imshow(image$imScilab(currimage));imagePush($imScilab(currimage),$imScilab(changeID$imScilab(currimage)));"
imScilab:image_change $imScilab(currimage)
set imScilab(image$imScilab(currimage),typestack$imScilab(changeID$imScilab(currimage))) "truecolor"
} else {
tk_messageBox -message "Current image is not an turecolor image!"
return
}
}
}

proc {imScilab:RGB_Info_Click} {} {
global imScilab
	if {$imScilab(imageready) ==1} {
	ScilabEval " xgetpixel(image$imScilab(currimage),map$imScilab(currimage),$imScilab(currimage));"
	} else {
	tk_messageBox -message "Please open an image first!"
	}
}

proc {imScilab:imageInfobtn} {btnID} {
global imScilab
set imScilab(tail) [file tail $imScilab(openedfile)]
pack .main.fra17.fra2$btnID \
	-in .main.fra17.lab21 -anchor sw -side left
pack .main.fra17.fra2$btnID.btnim$btnID \
	-in .main.fra17.fra2$btnID -anchor sw -side left
pack .main.fra17.fra2$btnID.btnimclose$btnID \
	-in .main.fra17.fra2$btnID -anchor se -side right
.main.fra17.fra2$btnID.btnim$btnID configure -text $imScilab(tail)
}

proc {imScilab:imageInfo} {infoID} {
global imScilab
	ScilabEval "xset('window',$infoID);imshow(image$infoID,map$infoID);"
	set imScilab(infoID) $infoID
	ScilabEval "                                                                      
	Image$imScilab(infoID)Info;TK_SetVar('imScilab(main,HelpMsg)','Image$infoID'+'   '+Image$imScilab(infoID)Info.FileName+'   '+string(Image$imScilab(infoID)Info.Width)+'*'+string(Image$imScilab(infoID)Info.Width)+'   '+Image$imScilab(infoID)Info.StorageType+'   '+Image$imScilab(infoID)Info.Format+'   '+string(Image$imScilab(infoID)Info.FileSize)+'B');"
}



proc {imScilab:currentim} {id} {
global imScilab
	if {$id!=$imScilab(currimage)} {
	.main.fra17.fra17.btnundo configure -state $imScilab(undostate$id)
	.main.fra17.fra17.btnredo configure -state $imScilab(redostate$id)
	} 
	set imScilab(currimage) $id
	set imScilab(imageChoose) image$imScilab(currimage)
}

proc {imScilab:image_win_close} {btnid} {
global imScilab
set imScilab(createimageSum) 0
set imScilab(imageState$btnid) disable
pack forget .main.fra17.fra2$btnid
.main.fra17.fra17.btnundo configure -state disable
.main.fra17.fra17.btnredo configure -state disable
set imScilab(undostate$btnid) "disable"
set imScilab(redostate$btnid) "disable"
set imScilab(createimage$btnid) 1
set imScilab(imagepath$btnid) ""
set imScilab(changeID$btnid) 0
wm title .main "VIP for Scilab2.7(win)"
set imScilab(mainmsg) "V.I.P-Video.Image.Processing by Scilab"
set imScilab(main,HelpMsg) $imScilab(mainmsg)
imScilab:image_reset_change $btnid
ScilabEval "                                                                                           
                       xdel($btnid);image$btnid=\[\];map$btnid=\[\];st$btnid=list();mark$btnid=list();head$btnid=0;rear$btnid=0;top$btnid=0;"
for {set i 0} {$i<4} {incr i 1} {
	if {$imScilab(createimage$i) == 0} {
	set imScilab(currimage) $i
	.main.fra17.fra17.btnundo configure -state $imScilab(undostate$i)
	.main.fra17.fra17.btnredo configure -state $imScilab(redostate$i)
	set imScilab(imageChoose) $i
	set imScilab(relstat$i) sunken  
    	 .main.fra17.fra21.btnim$i configure -relief $imScilab(relstat$i)
	break
	} else {
	set imScilab(createimageSum) [expr $imScilab(createimageSum)+1]
	}
	}
if {$imScilab(avifilepath) == "" } {
	set imScilab(createimageSum) [expr $imScilab(createimageSum)+1]
	}
if {$imScilab(createimageSum)==5 } {
	imScilab:image_change_savestate disable
	set imScilab(imageready) 0
	}
}

proc {imScilab:avichoose} {} {
global imScilab
	.main.fra17.fra17.btnundo configure -state disable
	.main.fra17.fra17.btnredo configure -state disable
 
	set imScilab(currimage) 4
	set imScilab(imagepath4) ""
	set imScilab(imageChoose) AviFrame
}

proc {imScilab:avichooseinfo} {} {
global imScilab
	ScilabEval "                                                                                    
	impreview(avilabid,aviframe,320,240);TK_SetVar('imScilab(main,HelpMsg)','$imScilab(avifilepath)'+'   '+string(ppavi(1))+'K'+'   '+string(ppavi(2))+'(FRAMES)');"
}

proc {imScilab:aviinfobtn} {} {
global imScilab
	set imScilab(tail) [file tail $imScilab(avifilepath)]
pack .main.fra17.fra24 \
	-in .main.fra17.lab21 -anchor sw -side left
pack .main.fra17.fra24.btnim4 \
	-in .main.fra17.fra24 -anchor sw -side left
.main.fra17.fra24.btnim4 configure -text $imScilab(tail)
}

proc {imScilab:image_zoomout} {} {
global imScilab
	ScilabEval "                                                                                       
	\[sizex,sizey\]=size(image$imScilab(currimage));sizex=sizex*1.1;sizey=sizey*1.1;xset('window',$imScilab(currimage));xset('wdim',sizex,sizey);"
}

proc {imScilab:image_zoomin} {} {
global imScilab
	ScilabEval "                                                                                       
	\[sizex,sizey\]=size(image$imScilab(currimage));sizex=sizex*0.91;sizey=sizey*0.91;xset('window',$imScilab(currimage));xset('wdim',sizex,sizey);"
}	

#############################	image process end here	#########################

#################################	main func
proc {main} {} {
# global var init
focus .main
global widget
global imScilab

uplevel #0 { append auto_path \ $imScilab(currdir)}
wm protocol .main WM_DELETE_WINDOW {imScilab:close}
wm protocol .codeEditor  WM_DELETE_WINDOW {
	toggle_win codeEditor
	focus -force .codeEditor
}



####################################################
####package
package require imScilab_balloon 1.0
package require Undoer 1.0

global textUndoer
set imScilab(undoID) [UnDonew textUndoer $widget(codetext)]


::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btnimopen "Open image file"
::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btnimsave "Save image file"
::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btnundo "Undo"
::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btnredo "Redo"
::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btnabout "About Us"
::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btntoggle "Toggle codeEditor"
::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btnavi "Process AviFile"
::::vBalloon::imScilab:set_balloon .main.fra17.fra17.btnhelp "Help"

::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnNew "New file"
::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnOpen "Open file"
::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnSave "Save file"
::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnRun "Run Script"
::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnHide "Toggle codeEditor"
::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnClr "Clear graphic window"
::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnFind "Find something"
::::vBalloon::imScilab:set_balloon .codeEditor.fra47.toolbar.btnFont "Set Font"

#####################################################
###### set welcome message
set imScilab(mainmsg) "V.I.P-Video.Image.Processing by Scilab"
set imScilab(main,HelpMsg) $imScilab(mainmsg)

#####################################################
######  try to open last file and set last font size

set imScilab(lastfile) "00000000"
set imScilab(tempfileszie) 0


catch [source [ file join $imScilab(mainfuncdir) start.tcl ] ]
$widget(codetext) configure -font "-size $imScilab(fontsize)"
#$widget(codetext) delete 1.0 end
#if { $imScilab(lastfile) != "00000000" && $imScilab(lastfile) != "New File" } {
#   catch [set imScilab(tempfilesize) [file size $imScilab(lastfile)]]
#   if { $imScilab(tempfilesize) > 0 } {
#	set imScilab(status_hint) $imScilab(openedfile)
#	wm title .main "VIP for Scilab2.7(win) - $imScilab(status_hint)"
#	focus .codeEditor 
#	focus $widget(codetext)
#	set fileid [open $imScilab(lastfile) r]
#	set data [read $fileid $imScilab(tempfilesize)]
#        close $fileid
#	$widget(codetext) insert end $data
#	set imScilab(opendfile) $imScilab(lastfile)
#	$widget(codetext) delete 1.0 end
#    }
#}


###################################################################
######  set clear  change record
imScilab:reset_change
for {set i 0} {$i<4} {incr i 1} {
imScilab:image_reset_change $i
}
}
####################	main func end here

proc {Window} {args} {
global vTcl
    set cmd [lindex $args 0]
    set name [lindex $args 1]
    set newname [lindex $args 2]
    set rest [lrange $args 3 end]
    if {$name == "" || $cmd == ""} {return}
    if {$newname == ""} {
        set newname $name
    }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists == "1" && $name != "."} {wm deiconify $name; return}
            if {[info procs vTclWindow(pre)$name] != ""} {
                eval "vTclWindow(pre)$name $newname $rest"
            }
            if {[info procs vTclWindow$name] != ""} {
                eval "vTclWindow$name $newname $rest"
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                eval "vTclWindow(post)$name $newname $rest"
            }
        }
        hide    { if $exists {wm withdraw $newname; return} }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

proc init {} {
global imScilab
global widget
set imScilab(currimage) 0
set imScilab(gui,codeEditor) 1
for {set i 0} {$i<4} {incr i 1} {
set imScilab(changeID$i) 0
set imScilab(undostate$i) ""
set imScilab(redostate$i) ""
set imScilab(imageState$i) disable
set imScilab(firstprev$i) 1
set imScilab(createimage$i) 1
set imScilab(imagepath$i) ""
set imScilab(image$i,storagetype) ""
}
set imScilab(aviState) disable
set imScilab(imageready) 0
set imScilab(aviprev) 0

set imScilab(openedavifile) ""
set imScilab(openedtextfile) ""
set imScilab(imageChoose) ""
set imScilab(avifilepath) ""
set imScilab(aviprocess) ""
set imScilab(avibsubprocess) ""
set imScilab(avitdiffprocess) ""

ScilabEval "xset('auto clear','on');"
}
init

#################################
# imScilab GUI PROCEDURES
#

proc vTclWindow. {base {container 0}} {
global imScilab
    if {$base == ""} {
        set base .
    }
    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    wm focusmodel $base passive
    wm geometry $base 200x200+0+0;update
    wm maxsize $base 1028 753
    wm minsize $base 119 2
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm withdraw $base
    wm title $base "vt"
    }
    ###################
    # SETTING GEOMETRY
    ###################
    set imScilab(winid) [winfo id .]
}

###########################	codeeditor	###############################

proc vTclWindow.codeEditor {base {container 0}} {
   global imScilab
    if {$base == ""} {
        set base .codeEditor
    }
    if {[winfo exists $base] && (!$container)} {
        wm deiconify $base; return
    }
    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    toplevel $base -class Toplevel
    wm focusmodel $base passive
    wm geometry $base 496x375+95+95
    wm maxsize $base 1024 768
    wm minsize $base 104 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm deiconify $base
    wm title $base "VIP CodeEditor"
    }

    frame $base.fra47 \
        -borderwidth 2 -height 26 -relief ridge -width 125
    frame $base.fra47.toolbar \
        -borderwidth 1 -relief ridge -width 194
###	New
    button $base.fra47.toolbar.btnNew \
        -command {new
focus $widget(codetext)} -height 16 -width 15 \
        -image [imScilab:image:get_image "text_new_g.GIF"] \
        -borderwidth 2 -relief groove
    bind $base.fra47.toolbar.btnNew <Enter> {
        .codeEditor.fra47.toolbar.btnNew configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_new.GIF"]
            set imScilab(main,TextHelpMsg) "Creates a new file"
    }
    bind $base.fra47.toolbar.btnNew <Leave> {
        .codeEditor.fra47.toolbar.btnNew configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_new_g.GIF"]
    set imScilab(main,TextHelpMsg) $imScilab(mainmsg)
    }
###	Open
    button $base.fra47.toolbar.btnOpen \
        -command get_file -height 16 -width 15 \
        -image [imScilab:image:get_image "text_open_g.GIF"] \
        -borderwidth 2 -relief groove
    bind $base.fra47.toolbar.btnOpen <Enter> {
        .codeEditor.fra47.toolbar.btnOpen configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_open.GIF"]
            set imScilab(main,TextHelpMsg) "Open a new file"
    }
    bind $base.fra47.toolbar.btnOpen <Leave> {
        .codeEditor.fra47.toolbar.btnOpen configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_open_g.GIF"]
    set imScilab(main,TextHelpMsg) $imScilab(mainmsg)
    }    
###	Save
    button $base.fra47.toolbar.btnSave \
        -command save -height 16 -width 15 \
        -image [imScilab:image:get_image "text_save_g.GIF"] \
        -borderwidth 2 -relief groove
     bind $base.fra47.toolbar.btnSave <Enter> {
        .codeEditor.fra47.toolbar.btnSave configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_save.GIF"] 
            set imScilab(main,TextHelpMsg) "Save the file"
    }
    bind $base.fra47.toolbar.btnSave <Leave> {
        .codeEditor.fra47.toolbar.btnSave configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_save_g.GIF"]
    set imScilab(main,TextHelpMsg) $imScilab(mainmsg)
    }       
###	Run
    button $base.fra47.toolbar.btnRun \
        -command imScilab:run -height 16 -width 15 \
        -image [imScilab:image:get_image "text_run_g.GIF"] \
        -borderwidth 2 -relief groove
     bind $base.fra47.toolbar.btnRun <Enter> {
        .codeEditor.fra47.toolbar.btnRun configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_run.GIF"]
            set imScilab(main,TextHelpMsg) "Run the script"
    }
    bind $base.fra47.toolbar.btnRun <Leave> {
        .codeEditor.fra47.toolbar.btnRun configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_run_g.GIF"]
    set imScilab(main,TextHelpMsg) $imScilab(mainmsg)
    }       
###	Hide
    button $base.fra47.toolbar.btnHide \
        -command {toggle_win codeEditor
focus -force .codeEditor} -height 16 -width 15 \
        -image [imScilab:image:get_image "editor_s_g.GIF"] \
        -borderwidth 2 -relief groove
    bind $base.fra47.toolbar.btnHide <Enter> {
        .codeEditor.fra47.toolbar.btnHide configure -borderwidth 2 \
        -image [imScilab:image:get_image "editor_s.GIF"]
            set imScilab(main,TextHelpMsg) "Hide the codeEditor"
    }
    bind $base.fra47.toolbar.btnHide <Leave> {
        .codeEditor.fra47.toolbar.btnHide configure -borderwidth 2 \
        -image [imScilab:image:get_image "editor_s_g.GIF"]
    set imScilab(main,TextHelpMsg) $imScilab(mainmsg)
    }        
###	Clear
    button $base.fra47.toolbar.btnClr \
        -command {ScilabEval "xbasc()\;"} -height 15 -width 15 \
        -image [imScilab:image:get_image "text_clear_g.GIF"] \
        -borderwidth 2 -relief groove
    bind $base.fra47.toolbar.btnClr <Enter> {
        .codeEditor.fra47.toolbar.btnClr configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_clear.GIF"]
            set imScilab(main,TextHelpMsg) "Clear the graphic window"
    }
    bind $base.fra47.toolbar.btnClr <Leave> {
        .codeEditor.fra47.toolbar.btnClr configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_clear_g.GIF"]
    set imScilab(main,TextHelpMsg) $imScilab(mainmsg)
    }    
###	Find
     button $base.fra47.toolbar.btnFind \
        -borderwidth 2 -image [imScilab:image:get_image "text_find_g.gif"] \
        -relief groove -height 15 -width 15 \
        -command { source [file join $imScilab(mainfuncdir) find&replace.tcl] } 
    bind $base.fra47.toolbar.btnFind <Enter> {
        .codeEditor.fra47.toolbar.btnFind configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_find.GIF"]
            set imScilab(main,HelpMsg) "Finds the specified code"
    }
    bind $base.fra47.toolbar.btnFind <Leave> {
        .codeEditor.fra47.toolbar.btnFind configure -borderwidth 2 \
        -image [imScilab:image:get_image "text_find_g.GIF"]
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }
###	Font
    menubutton $base.fra47.toolbar.btnFont \
        -borderwidth 2 -image [imScilab:image:get_image "font_g.gif"] -menu "$base.fra47.toolbar.btnFont.m" \
        -relief groove -height 15 -width 15
    bind $base.fra47.toolbar.btnFont <Enter> {
        .codeEditor.fra47.toolbar.btnFont configure -borderwidth 2 \
        -image [imScilab:image:get_image "font.GIF"]
            set imScilab(main,HelpMsg) "Set the FontSize"
    }
    bind $base.fra47.toolbar.btnFont <Leave> {
        .codeEditor.fra47.toolbar.btnFont configure -borderwidth 2 \
        -image [imScilab:image:get_image "font_g.GIF"]
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }
    menu $base.fra47.toolbar.btnFont.m \
        -activeborderwidth 1 -borderwidth 2 -font {times 10 bold} -tearoff 0 
    $base.fra47.toolbar.btnFont.m add command \
        -command {imScilab:set_font 8}  -label VerySmall
    $base.fra47.toolbar.btnFont.m add command \
        -command {imScilab:set_font 10} -label Small    
    $base.fra47.toolbar.btnFont.m add command \
        -command {imScilab:set_font 12} -label Normal
    $base.fra47.toolbar.btnFont.m add command \
        -command {imScilab:set_font 15} -label Big
    $base.fra47.toolbar.btnFont.m add command \
        -command {imScilab:set_font 20} -label VeryBig
            
    frame $base.fra47.fra45 \
        -borderwidth 2 -height 30 -relief groove -width 110
    message $base.fra47.fra45.mes18 \
        -anchor w -font {-family {MS Sans Serif} -size 8} -padx 5 -pady 2 \
        -text {V.I.P-Video.Image.Processing by Scilab} \
        -textvariable imScilab(main,TextHelpMsg) -width 900 
#########
    
    frame $base.cpd45 \
        -borderwidth 1 -height 529 -relief raised -width 30 
    scrollbar $base.cpd45.01 \
        -command [list $base.cpd45.02 yview] -width 15 
    text $base.cpd45.02 \
        -borderwidth 0 -font {-size 20} -height 130 -width 195 \
        -yscrollcommand "$base.cpd45.01 set" 
    bind $base.cpd45.02 <Alt-Key-F4> {
        imScilab:close
    }
    bind $base.cpd45.02 <Control-Key-A> {
       file_save_as
    }
     bind $base.cpd45.02 <Control-Key-a> {
        file_save_as
    }
    bind $base.cpd45.02 <Control-Key-S> {
        save
    }
    bind $base.cpd45.02 <Control-Key-s> {
        save
    }
    bind $base.cpd45.02 <Control-Key-N> {
        new
    }
    bind $base.cpd45.02 <Control-Key-n> {
        new
    }
    bind $base.cpd45.02 <Control-Key-Y> {
        Redo
    }
   bind $base.cpd45.02 <Control-Key-y> {
        Redo
    }
    bind $base.cpd45.02 <Control-Key-Z> {
        Undo
    }
   bind $base.cpd45.02 <Control-Key-z> {
        Undo
    }
    bind $base.cpd45.02 <Control-Key-O> {
        get_file
    }
    bind $base.cpd45.02 <Control-Key-o> {
        get_file
    }
    bind $base.cpd45.02 <Control-Key-R> {
        source [file join $imScilab(mainfuncdir) find&replace.tcl]
    }
    bind $base.cpd45.02 <Control-Key-r> {
        source [file join $imScilab(mainfuncdir) find&replace.tcl]
    }
    bind $base.cpd45.02 <Key> {
        imScilab:change
    }
    
	###################
    	# SETTING codeEditor GEOMETRY
    	###################

##################

    pack $base.fra47 \
        -in $base -anchor sw -expand 0 -fill x 
    pack $base.fra47.toolbar \
        -in $base.fra47 -anchor center -expand 0 -fill x -side right
    pack $base.fra47.toolbar.btnNew \
        -in $base.fra47.toolbar -anchor center -expand 0 -fill none \
        -side right
    pack $base.fra47.toolbar.btnOpen \
        -in $base.fra47.toolbar -anchor center -expand 0 -fill none \
        -side right
    pack $base.fra47.toolbar.btnSave \
        -in $base.fra47.toolbar -anchor center -expand 0 -fill none \
        -side right
    pack $base.fra47.toolbar.btnRun \
        -in $base.fra47.toolbar -anchor center -expand 0 -fill none \
        -side right
    pack $base.fra47.toolbar.btnHide \
        -in $base.fra47.toolbar -anchor center -expand 1 -fill x \
        -side left
    pack $base.fra47.toolbar.btnClr \
        -in $base.fra47.toolbar -anchor center -expand 0 -fill y \
        -side left
    pack $base.fra47.toolbar.btnFind \
        -in $base.fra47.toolbar -anchor center -expand 0 -fill y \
        -side left
    pack $base.fra47.toolbar.btnFont \
        -in $base.fra47.toolbar -anchor center -expand 0 -fill y \
        -side left 
          
    pack $base.fra47.fra45 \
        -in $base.fra47 -anchor center -expand 1 -fill both \
        -side bottom 
    pack $base.fra47.fra45.mes18 \
        -in $base.fra47.fra45 -anchor center -expand 1 -fill x \
        -side top 
###################



        pack $base.cpd45 \
        -in $base -anchor n -expand 1 -fill y -side top
    grid columnconf $base.cpd45 0 -weight 1
    grid rowconf $base.cpd45 0 -weight 1
    grid $base.cpd45.01 \
        -in $base.cpd45 -column 1 -row 0 -columnspan 1 -rowspan 1 \
        -sticky ns 
    grid $base.cpd45.02 \
        -in $base.cpd45 -column 0 -row 0 -columnspan 1 -rowspan 1 \
        -sticky nesw
}

########	.main


proc vTclWindow.main {base {container 0}} {
    if {$base == ""} {
        set base .main
    }
    if {[winfo exists $base] && (!$container)} {
        wm deiconify $base; return
    }
    
    global imScilab
    	
    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    toplevel $base -class Toplevel \
        -menu "$base.m18" -background #c0c0c0
    wm focusmodel $base passive
    wm geometry $base 800x600+114+67
    wm maxsize $base 1028 734
    wm minsize $base 420 50
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm deiconify $base
    wm title $base "VIP for Scilab2.7(win)"
    }
    frame $base.fra17 \
        -borderwidth 2 -height 43 -relief groove -width 506
    label $base.fra17.lab21 \
    	-borderwidth 2 -height 600 -relief sunken -width 600 -background #c0c0c0 \
    	-image [imScilab:image:get_image "bg.gif"]
###
    bind $base.fra17 <Alt-Key-F4> {
        imScilab:close
    }
    bind $base.fra17 <Control-Key-A> {
       image_file_save_as
    }
    bind $base.fra17 <Control-Key-a> {
        image_file_save_as
    }
    bind $base.fra17 <Control-Key-a> {
       image_file_save_as
    }
    bind $base.fra17 <Control-Key-S> {
        image_save
    }
    bind $base.fra17 <Control-Key-s> {
        image_save
    }
    bind $base.fra17 <Control-Key-N> {
        image_new
    }
    bind $base.fra17 <Control-Key-n> {
        image_new
    }
    bind $base.fra17 <Control-Key-Y> {
        image_redo
    }
   bind $base.fra17 <Control-Key-y> {
        image_redo
    }
    bind $base.fra17 <Control-Key-Z> {
        image_undo
    }
   bind $base.fra17 <Control-Key-z> {
        image_undo
    }
    bind $base.fra17 <Control-Key-O> {
        image_get_file
    }  
    bind $base.fra17 <Control-Key-o> {
        image_get_file
    }
    	

    for {set i 0} {$i<5} {incr i 1} {
    set imScilab(relstat$i) flat
    }
    
#####	image0-image3    imfinfo
    frame $base.fra17.fra20 \
    	-borderwidth 2 -height 1 -relief groove -width 15
    button $base.fra17.fra20.btnimclose0 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text x -height 1 -width 3 \
    	-command {imScilab:image_win_close 0}
    bind $base.fra17.fra20.btnimclose0 <Enter> {
        .main.fra17.fra20.btnimclose0 configure -borderwidth 2 \
        -relief solid
    }
    bind $base.fra17.fra20.btnimclose0 <Leave> {
        .main.fra17.fra20.btnimclose0 configure -borderwidth 2 \
        -relief flat
    }	
    button $base.fra17.fra20.btnim0 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text image0 -height 1 -width 10 \
    	-command { imScilab:currentim 0
    	set imScilab(relstat0) sunken  
    	 .main.fra17.fra21.btnim1 configure -relief flat
    	 set imScilab(relstat1) flat
    	 .main.fra17.fra22.btnim2 configure -relief flat
    	 set imScilab(relstat2) flat
    	 .main.fra17.fra23.btnim3 configure -relief flat
    	 set imScilab(relstat3) flat
    	 .main.fra17.fra24.btnim4 configure -relief flat
    	set imScilab(relstat4) flat
	imScilab:imageInfo 0}
    bind $base.fra17.fra20.btnim0 <Enter> {
        .main.fra17.fra20.btnim0 configure -borderwidth 2 \
        -relief raise
    }
    bind $base.fra17.fra20.btnim0 <Leave> {
        .main.fra17.fra20.btnim0 configure -borderwidth 2 \
        -relief $imScilab(relstat0)
    }
    
    frame $base.fra17.fra21 \
    	-borderwidth 2 -height 1 -relief groove -width 15
    button $base.fra17.fra21.btnimclose1 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text x -height 1 -width 3 \
    	-command {imScilab:image_win_close 1}
    bind $base.fra17.fra21.btnimclose1 <Enter> {
        .main.fra17.fra21.btnimclose1 configure -borderwidth 2 \
        -relief solid
    }
    bind $base.fra17.fra21.btnimclose1 <Leave> {
        .main.fra17.fra21.btnimclose1 configure -borderwidth 2 \
        -relief flat
    }	
    button $base.fra17.fra21.btnim1 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text image1 -height 1 -width 10 \
    	-command { imScilab:currentim 1
    	set imScilab(relstat1) sunken 
    	.main.fra17.fra20.btnim0 configure -relief flat
    	set imScilab(relstat0) flat
    	.main.fra17.fra22.btnim2 configure -relief flat
    	set imScilab(relstat2) flat
    	.main.fra17.fra23.btnim3 configure -relief flat
    	set imScilab(relstat3) flat
    	.main.fra17.fra24.btnim4 configure -relief flat
    	set imScilab(relstat4) flat
	imScilab:imageInfo 1}    
    bind $base.fra17.fra21.btnim1 <Enter> {
        .main.fra17.fra21.btnim1 configure -borderwidth 2 \
        -relief raise  
    }
    bind $base.fra17.fra21.btnim1 <Leave> {
        .main.fra17.fra21.btnim1 configure -borderwidth 2 \
        -relief $imScilab(relstat1)
    }
    
    frame $base.fra17.fra22 \
    	-borderwidth 2 -height 1 -relief groove -width 15
    button $base.fra17.fra22.btnimclose2 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text x -height 1 -width 3 \
    	-command {imScilab:image_win_close 2}
    bind $base.fra17.fra22.btnimclose2 <Enter> {
        .main.fra17.fra22.btnimclose2 configure -borderwidth 2 \
        -relief solid
    }
    bind $base.fra17.fra22.btnimclose2 <Leave> {
        .main.fra17.fra22.btnimclose2 configure -borderwidth 2 \
        -relief flat
    }	
    button $base.fra17.fra22.btnim2 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text image2 -height 1 -width 10 \
    	-command { imScilab:currentim 2
    	set imScilab(relstat2) sunken 
    	.main.fra17.fra20.btnim0 configure -relief flat
    	set imScilab(relstat0) flat
    	.main.fra17.fra21.btnim1 configure -relief flat
    	set imScilab(relstat1) flat
    	.main.fra17.fra23.btnim3 configure -relief flat
    	set imScilab(relstat3) flat
    	.main.fra17.fra24.btnim4 configure -relief flat
    	set imScilab(relstat4) flat
	imScilab:imageInfo 2}    
    bind $base.fra17.fra22.btnim2 <Enter> {
        .main.fra17.fra22.btnim2 configure -borderwidth 2 \
        -relief raise  
    }
    bind $base.fra17.fra22.btnim2 <Leave> {
        .main.fra17.fra22.btnim2 configure -borderwidth 2 \
        -relief $imScilab(relstat2)
    }
    
    frame $base.fra17.fra23 \
    	-borderwidth 2 -height 1 -relief groove -width 15
    button $base.fra17.fra23.btnimclose3 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text x -height 1 -width 3 \
    	-command {imScilab:image_win_close 3}
    bind $base.fra17.fra23.btnimclose3 <Enter> {
        .main.fra17.fra23.btnimclose3 configure -borderwidth 2 \
        -relief solid
    }
    bind $base.fra17.fra23.btnimclose3 <Leave> {
        .main.fra17.fra23.btnimclose3 configure -borderwidth 2 \
        -relief flat
    }
    button $base.fra17.fra23.btnim3 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text image3 -height 1 -width 10 \
    	-command { imScilab:currentim 3
    	set imScilab(relstat3) sunken 
    	.main.fra17.fra20.btnim0 configure -relief flat
    	set imScilab(relstat0) flat
    	.main.fra17.fra21.btnim1 configure -relief flat
    	set imScilab(relstat1) flat
    	.main.fra17.fra22.btnim2 configure -relief flat
    	set imScilab(relstat2) flat
    	.main.fra17.fra24.btnim4 configure -relief flat
    	set imScilab(relstat4) flat
    	imScilab:imageInfo 3}    
    bind $base.fra17.fra23.btnim3 <Enter> {
        .main.fra17.fra23.butim3 configure -borderwidth 2 \
        -relief raise  
    }
    bind $base.fra17.fra23.btnim3 <Leave> {
        .main.fra17.fra23.btnim3 configure -borderwidth 2 \
        -relief $imScilab(relstat3)  
    }

### aviframe    infobutton
    
    frame $base.fra17.fra24 \
    	-borderwidth 2 -height 1 -relief groove -width 15
    button $base.fra17.fra24.btnimclose4 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text x -height 1 -width 3 \
    	-command {}
    bind $base.fra17.fra24.btnimclose4 <Enter> {
        .main.fra17.fra24.btnimclose4 configure -borderwidth 2 \
        -relief solid
    }
    bind $base.fra17.fra24.btnimclose4 <Leave> {
        .main.fra17.fra24.btnimclose4 configure -borderwidth 2 \
        -relief flat
    }
    button $base.fra17.fra24.btnim4 \
    	-borderwidth 2 -relief flat -background #7b7b7b -activebackground #787878 \
    	-text image3 -height 1 -width 10 \
    	-command { imScilab:avichoose
    	set imScilab(relstat4) sunken 
    	.main.fra17.fra20.btnim0 configure -relief flat
    	set imScilab(relstat0) flat
    	.main.fra17.fra21.btnim1 configure -relief flat
    	set imScilab(relstat1) flat
    	.main.fra17.fra22.btnim2 configure -relief flat
    	set imScilab(relstat2) flat
    	.main.fra17.fra23.btnim3 configure -relief flat
    	set imScilab(relstat3) flat
    	imScilab:avichooseinfo }    
    bind $base.fra17.fra24.btnim4 <Enter> {
        .main.fra17.fra24.btnim4 configure -borderwidth 2 \
        -relief raise  
    }
    bind $base.fra17.fra24.btnim4 <Leave> {
        .main.fra17.fra24.btnim4 configure -borderwidth 2 \
        -relief $imScilab(relstat4)
    }

    
    
    frame $base.fra17.fra17 \
        -borderwidth 2 -height 44 -relief groove -width 535
    label $base.fra17.fra17.lab17 \
    	-borderwidth 0 -height 40 -relief flat -width 535 -image [imScilab:image:get_image "vip_banner.gif"]    
###	image open
    button $base.fra17.fra17.btnimopen \
        -borderwidth 0 -command image_get_file \
        -image [imScilab:image:get_image "open.gif"] -relief groove \
        -text Open
    bind $base.fra17.fra17.btnimopen <Enter> {
        .main.fra17.fra17.btnimopen configure -borderwidth 2
            set imScilab(main,HelpMsg) "Opens a file"
    }
    bind $base.fra17.fra17.btnimopen <Leave> {
        .main.fra17.fra17.btnimopen configure -borderwidth 0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }
###	image save
    button $base.fra17.fra17.btnimsave \
        -borderwidth 0 -command image_file_save_as \
        -image [imScilab:image:get_image "save.gif"] -relief groove \
        -text Save -state disable
    bind $base.fra17.fra17.btnimsave <Enter> {
        .main.fra17.fra17.btnimsave configure -borderwidth 2
            set imScilab(main,HelpMsg) "Saves the file "
    }
    bind $base.fra17.fra17.btnimsave <Leave> {
        .main.fra17.fra17.btnimsave configure -borderwidth 0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }
###	undo
    button $base.fra17.fra17.btnundo \
       -borderwidth 0 -command image_undo \
        -image [imScilab:image:get_image "undo.gif"] -relief groove \
        -text Undo -state disable
    bind $base.fra17.fra17.btnundo <Enter> {
        .main.fra17.fra17.btnundo configure -borderwidth 2
            set imScilab(main,HelpMsg) "Undoes the previously undone action "
    }
    bind $base.fra17.fra17.btnundo <Leave> {
        .main.fra17.fra17.btnundo configure -borderwidth  0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }
###	redo
    button $base.fra17.fra17.btnredo \
        -borderwidth 0 -command image_redo \
        -image [imScilab:image:get_image "redo.gif"] -relief groove \
        -text Redo -state disable
    bind $base.fra17.fra17.btnredo <Enter> {
        .main.fra17.fra17.btnredo configure -borderwidth 2
            set imScilab(main,HelpMsg) "Redoes the previously undone action"
    }
    bind $base.fra17.fra17.btnredo <Leave> {
        .main.fra17.fra17.btnredo configure -borderwidth 0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }
    frame $base.fra17.fra17.fra36 \
        -borderwidth 2 -height 32 -relief groove -width 2 
    
###	toggle codeEditor
    button $base.fra17.fra17.btntoggle \
        -command {toggle_win codeEditor
focus -force .codeEditor} -height 16 \
        -image [imScilab:image:get_image "editor.GIF"] \
        -text hide -width 15 -borderwidth 0 -relief groove
    bind $base.fra17.fra17.btntoggle <Enter> {
        .main.fra17.fra17.btntoggle configure -borderwidth 2
            set imScilab(main,HelpMsg) "Hide the codeEditor"
    }
    bind $base.fra17.fra17.btntoggle <Leave> {
        .main.fra17.fra17.btntoggle configure -borderwidth 0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }

###	avi
    button $base.fra17.fra17.btnavi \
        -borderwidth 0 -command {source [file join $imScilab(mainfuncdir) avi.tcl]
        wm title .main "VIP for Scilab2.7(win) - AVI Process"} \
        -image [imScilab:image:get_image "avi.gif"] -relief groove \
        -text about
    bind $base.fra17.fra17.btnavi <Enter> {
        .main.fra17.fra17.btnavi configure -borderwidth 2
            set imScilab(main,HelpMsg) "Process AviFile "
    }
    bind $base.fra17.fra17.btnavi <Leave> {
        .main.fra17.fra17.btnavi configure -borderwidth 0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }

###	about us
    button $base.fra17.fra17.btnabout \
        -borderwidth 0 \
        -image [imScilab:image:get_image "aboutus.gif"] -relief groove \
        -text about \
        -command { imScilab:about 
#        puts a\n
#        set tmp [file join F:\\vc6\\MSDev98\\MyProjects\\test1\\Debug\\test1.exe]
#	exec $tmp & 
	}
    bind $base.fra17.fra17.btnabout <Enter> {
        .main.fra17.fra17.btnabout configure -borderwidth 2
            set imScilab(main,HelpMsg) "About Our Group "
    }
    bind $base.fra17.fra17.btnabout <Leave> {
        .main.fra17.fra17.btnabout configure -borderwidth 0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }

###	help
    button $base.fra17.fra17.btnhelp \
        -borderwidth 0 -command imScilab:showHelpIndex \
        -image [imScilab:image:get_image "main_help.gif"] -relief groove \
        -text about
    bind $base.fra17.fra17.btnhelp <Enter> {
        .main.fra17.fra17.btnhelp configure -borderwidth 2
            set imScilab(main,HelpMsg) "About Our Group "
    }
    bind $base.fra17.fra17.btnhelp <Leave> {
        .main.fra17.fra17.btnhelp configure -borderwidth 0
    set imScilab(main,HelpMsg) $imScilab(mainmsg)
    }
     
    
    frame $base.fra17.fra43 \
        -borderwidth 1 -height 30 -relief groove -width 125 
    frame $base.fra17.fra43.fra24 \
        -borderwidth 1 -height 30 -width 115 
###	mainmessage    
    frame $base.fra17.fra43.fra45 \
        -borderwidth 1 -height 30 -relief groove -width 125 
    message $base.fra17.fra43.fra45.mes18 \
        -anchor w -font {-family {MS Sans Serif} -size 10} -padx 5 -pady 2 \
        -text {Why not i to do it!} \
        -textvariable imScilab(main,HelpMsg) -width 900 


############################menu here############################3    
    menu $base.m18 \
        -cursor {} -tearoff 0
    $base.m18 add cascade \
        -label File -menu "$base.m18.men19" -underline 0
    $base.m18 add cascade \
        -label Edit -menu "$base.m18.men20" -underline 0
    $base.m18 add cascade \
        -label Image -menu "$base.m18.men21" -underline 0
    $base.m18 add cascade \
        -label Filter -menu "$base.m18.men23" -underline 3
    $base.m18 add cascade \
        -label View -menu "$base.m18.men24" -underline 0
    $base.m18 add cascade \
        -label Window -menu "$base.m18.men25" -underline 0        
    $base.m18 add cascade \
        -label Help -menu "$base.m18.men26" -underline 0
########	File    
    menu $base.m18.men19 \
        -tearoff 0 
    $base.m18.men19 add command \
        -accelerator Ctrl+O -command image_get_file -label Open -underline 0
    $base.m18.men19 add separator    
    $base.m18.men19 add command \
        -accelerator Ctrl+S -command image_save -label Save -underline 0
    $base.m18.men19 add command \
        -accelerator Shift+Ctrl+S -command image_file_save_as -label {Save as} -underline 5
    $base.m18.men19 add separator
    $base.m18.men19 add command \
        -accelerator Alt+F4 -command imScilab:close -label Quit -underline 0
########	Edit	    
    menu $base.m18.men20 \
        -tearoff 0 
    $base.m18.men20 add command \
         -accelerator Ctrl+Z -command Undo -label Undo 
    $base.m18.men20 add command \
         -accelerator Ctrl+Y -command Redo -label Redo 
    $base.m18.men20 add separator
    $base.m18.men20 add command \
         -accelerator Ctrl+X -command {global widget
tk_textCut $widget(codetext)} \
        -label Cut -underline 2
    $base.m18.men20 add command \
         -accelerator Ctrl+C -command {global widget
tk_textCopy $widget(codetext)} \
        -label Copy -underline 0
    $base.m18.men20 add command \
        -accelerator Ctrl+V -command {global widget
tk_textPaste $widget(codetext)} \
        -label Paste -underline 0
########	Image        
    menu $base.m18.men21 \
        -tearoff 0 
    $base.m18.men21 add cascade \
    	-label Mode -menu "$base.m18.men21.men50"
    menu $base.m18.men21.men50 \
        -tearoff 0
    $base.m18.men21.men50 add radiobutton \
    	-command {imScilab:im2gray} -label {GRAY Color} -underline 0
    $base.m18.men21.men50 add radiobutton \
    	-command {imScilab:ind2rgb} -label {Index To RGB} -underline 0
    $base.m18.men21.men50 add radiobutton \
    	-command {imScilab:rgb2yiq} -label {RGB To YIQ} -underline 0
    $base.m18.men21.men50 add radiobutton \
    	-command {imScilab:yiq2rgb} -label {YIQ To RGB} -underline 0	
    $base.m18.men21 add separator		
    $base.m18.men21 add cascade \
        -label FontSize -menu "$base.m18.men21.men51" 
    menu $base.m18.men21.men51 \
        -tearoff 0 
    $base.m18.men21.men51 add radiobutton \
        -command {imScilab:set_font 8} -label VerySmall -value VerySmall -underline 0
    $base.m18.men21.men51 add radiobutton \
        -command {imScilab:set_font 10} -label Small -value Small -underline 0
    $base.m18.men21.men51 add radiobutton \
        -command {imScilab:set_font 12} -label Normal -value Normal -underline 0
    $base.m18.men21.men51 add radiobutton \
        -command {imScilab:set_font 15} -label Big -value Big -underline 0
    $base.m18.men21.men51 add radiobutton \
        -command {imScilab:set_font 20} -label VeryBig -value VeryBig -underline 6

########	Filter    	
     menu $base.m18.men23 \
     	-tearoff 0
     $base.m18.men23 add cascade \
    	-label Thresholding -menu "$base.m18.men23.men60"
     menu $base.m18.men23.men60 \
        -tearoff 0
     $base.m18.men23.men60 add command \
     	-command {imScilab:loadFun im2bw } -label im2bw
     $base.m18.men23 add cascade \
    	-label {Mathematical Morphology} -menu "$base.m18.men23.men61"
     menu $base.m18.men23.men61 \
        -tearoff 0
     $base.m18.men23.men61 add command \
     	-command {imScilab:loadFun dilate} -label Dilation/Erosion 
     $base.m18.men23.men61 add command \
     	-command {imScilab:loadFun thin} -label Thining 
     $base.m18.men23 add cascade \
    	-label {Filtering} -menu "$base.m18.men23.men62"
     menu $base.m18.men23.men62 \
        -tearoff 0
     $base.m18.men23.men62 add command \
     	-command {imScilab:loadFun imconv} -label Convolution 
     $base.m18.men23.men62 add command \
     	-command {imScilab:loadFun gsm2d} -label {Gaussian Smoothing}
     $base.m18.men23.men62 add command \
     	-command {imScilab:loadFun minmax} -label {Noise Removal(Min/Max)}
     $base.m18.men23.men62 add command \
     	-command {imScilab:loadFun imnoise} -label {Noise Addition}	
     $base.m18.men23 add cascade \
    	-label {Edge and Border} -menu "$base.m18.men23.men63"
     menu $base.m18.men23.men63 \
        -tearoff 0
     $base.m18.men23.men63 add command \
     	-command {imScilab:loadFun edge} -label {Edge Detection }
     $base.m18.men23.men63 add command \
     	-command {imScilab:loadFun bwborder} -label {Border Detection}
        
########	View
    menu $base.m18.men24 \
    	-tearoff 0
    $base.m18.men24 add command \
    	-accelerator Ctrl++ -command {imScilab:image_zoomout} -label {Zoom In} -underline 5
    $base.m18.men24 add command \
    	-accelerator Ctrl+- -command {imScilab:image_zoomin} -label {Zoom Out} -underline 5
########	Window
    menu $base.m18.men25 \
    	-tearoff 0
    $base.m18.men25 add checkbutton \
    	-command {toggle_win codeEditor
focus -force .codeEditor} -label {Code Editor} -offvalue 0 -onvalue 1 -variable ToolsOn -underline 0 \
	-variable {CodeEditor check}
    $base.m18.men25 add checkbutton \
    	-command {source [file join $imScilab(mainfuncdir) avi.tcl]} -label {AVI operate} -offvalue 2 -onvalue 3 -variable ToolsOn -underline 0 \
    	-variable {AVI check}
    $base.m18.men25 add checkbutton \
    	-command {imScilab:RGB_Info_Click} -label {RGB info} -offvalue 4 -onvalue 5 -variable ToolsOn -underline 0 \
    	-variable {RGBInfo check}
						    		     
########	Help    
    menu $base.m18.men26 \
        -font {-family {MS Sans Serif} -size 8} -tearoff 0 
    $base.m18.men26 add command \
        -command imScilab:userguide -label {User's Guide}  
    $base.m18.men26 add command \
        -command imScilab:showHelpIndex -label {Help Index} 
    $base.m18.men26 add separator
    $base.m18.men26 add command \
        -command imScilab:about -label {About Us} 
########    
    
    
    ###################
    # SETTING GEOMETRY
    ###################
    pack $base.fra17 \
        -in $base -anchor center -expand 1 -fill both -side top
    pack $base.fra17.fra17 \
        -in $base.fra17 -anchor center -expand 0 -fill both -side top
    pack $base.fra17.fra17.lab17 \
        -in $base.fra17.fra17 -anchor center -expand 0 -fill x -side top
    pack $base.fra17.fra43 \
        -in $base.fra17 -anchor center -expand 0 -fill x -side bottom 
    pack $base.fra17.lab21 \
    	-in $base.fra17 -anchor center -expand 1 -fill both -side top  

    place $base.fra17.fra17.btnimopen \
        -x 7 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore 
    place $base.fra17.fra17.btnimsave \
        -x 47 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore 
    place $base.fra17.fra17.btnundo \
        -x 87 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore 
    place $base.fra17.fra17.btnredo \
        -x 127 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore 
    place $base.fra17.fra17.btntoggle \
        -x 167 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore
    place $base.fra17.fra17.btnavi \
        -x 207 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore
    place $base.fra17.fra17.btnabout \
        -x 247 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore
    place $base.fra17.fra17.btnhelp \
        -x 287 -y 5 -width 35 -height 35 -anchor nw -bordermode ignore

    pack $base.fra17.fra43.fra24 \
        -in $base.fra17.fra43 -anchor center -expand 1 -fill x -side right 


    pack $base.fra17.fra43.fra45 \
        -in $base.fra17.fra43.fra24 -anchor center -expand 1 -fill x \
        -side bottom 
    pack $base.fra17.fra43.fra45.mes18 \
        -in $base.fra17.fra43.fra45 -anchor center -expand 1 -fill x \
        -side top 
}
	

####################################################################################

Window show .
show_animated_gif -file [file join $imScilab(imagedir) splash.gif]

after 1000 {
Window show .codeEditor
toggle_win codeEditor
Window show .main

main

}
