############################################################
#
# SETUP crea les finestres,
# els butons, l'arbre i fa el pack
# Tambe obre les imatges (gif) que s'utilitzen a l'arbre
# 
############################################################

set insidegid 1

if { !$insidegid } {
    proc WarnWinText { msg } {
	puts $msg
    }
    
    set GIDDEFAULT [file join "/" "gid project"]
    set GIDDEFAULTTCL [file join $GIDDEFAULT "scripts"]

    #WarnWinText "Cargando Htmlhelp"
}

proc GIDHELP_setup {help_type} {

    global GidPriv
    global var finestra
    global arbre
    global indexString
    global searchString
    global tcl_platform



    #toplevel .help_win
    #set whlp .help_win
    
    # no hace falta
    ## RAMSAN
    #catch {
    #         load [file join $::GIDDEFAULT img12[info sharedlibextension]]
    #}
    ## RAMSAN
    #set htmldllname [file join $::GIDDEFAULT libTkHtml[info sharedlibextension]]
    #if { [info command html] == "" && [file exists $htmldllname] } {
    #         load $htmldllname
    #}
    
    set _whlp .gid.help_win
    
    # Poner un pannedwindow para contener "dreta" y "esq"
    
    catch { destroy $_whlp }
    #InitWindow $whlp "GiD Help" GiDHelpWin "" "" 1
    InitWindow $_whlp [_ "HELP"] GiDHelpWindowGeom "HelpWindow"
    
    #set parent [winfo parent $whlp]
    #set x [expr [winfo x $parent]+[winfo width $parent]/2-450]
    #set y [expr [winfo y $parent]+[winfo height $parent]/2-300]    
    #wm geometry $whlp  +${x}+${y}

    if { $help_type == "REFERENCE" || $help_type == "SEARCH" \
	    || $help_type == "CUSTOM_HELP_REFERENCE" } {
	set whlp $_whlp.pn
	panedwindow $whlp -orient horizontal -opaque 1
	set twopanes 1
    } else {
	set whlp $_whlp.frm
	frame $whlp
	set twopanes 0
    }

    $whlp config -bg #add8d8
    #con minsize 350 200 se cuelga en el tutorial 1 al bajar arrastrando
    #la barra de desplazamiento por debajo de la figura 9 !!!!
    wm minsize $_whlp 400 200
    set var(win) $whlp
    
    #  frames
    #------------------------------------------------
    
    frame $whlp.dreta -bg grey93
    frame $whlp.dreta.fondo -bg white -height 400 -width 800 
    frame $whlp.dreta.base -bg grey93 -height 50 -width 500
    frame $whlp.esq -width 300 -bg grey93
    frame $whlp.esq.mode   -bg grey93 -height 48 -width 150
    frame $whlp.esq.index -bg grey93  -width 600 
    frame $whlp.esq.index_top -bg grey93
    frame $whlp.esq.search_top -bg grey93
    
    # butons
    #------------------------------------------------
    
    button $whlp.esq.mode.contents -text [_ "Contents"] -command "ShowContents" -background        grey  -width 8
    
    button $whlp.esq.mode.index    -text [_ "index"] -command    "ShowIndex" -background        grey  -width 8
    button $whlp.esq.mode.search    -text [_ "search"] -command  "ShowSearch" -background        grey  -width 8
    label  $whlp.esq.mode.espai -text "   "  -bg grey93 
    
    if { $help_type == "CUSTOM_HELP_REFERENCE"} {
	pack $whlp.esq.mode.espai $whlp.esq.mode.contents $whlp.esq.mode.search -side left -fill x
    } else {
	pack $whlp.esq.mode.espai $whlp.esq.mode.contents $whlp.esq.mode.index $whlp.esq.mode.search -side left -fill x
    }
    
    set w $whlp.dreta.fondo
    set e $whlp.esq.index
    
    
    # Index: entry i listbox
    #------------------------------------------------
    
    entry $whlp.esq.index_top.index_entry -textvariable indexString 
    label $whlp.esq.index_top.text_entry -text [_ "Look for:"] -bg grey93
    
    trace var indexString w "actualitzar_llista ;#"
    
    bind $whlp.esq.index_top.index_entry <Return>    "click_list"
    
    
    listbox $e.list -yscroll "$e.scroll_list set"  -height 23 -width 20 -bg #add8d8 -borderwidth 0 -relief raised
    scrollbar $e.scroll_list -command "$e.list yview"
    
    bind $e.list <Double-1> "click_list"
    bind $e.list <1> {
	if {[winfo exists %W]} {
	    tk::ListboxBeginSelect %W [%W index @%x,%y]
	}
	break
    }
    
    
    
    # Search: entry i 2 listbox + frame .index2
    #------------------------------------------------
    
    frame $whlp.esq.index2
    set e2 $whlp.esq.index2
    
    entry $whlp.esq.search_top.search_entry -textvariable searchString 
    label $whlp.esq.search_top.text_entry -text [_ "Look for:"] -bg grey93
    
    trace variable searchString w "realitzar_busqueda ;#"
    
    bind $whlp.esq.search_top.search_entry <Return>  "search_tecla_return"
    
    label $e.text -text [_ "Choose a word:"] -background grey93
    
    listbox $e.list_paraules  -yscroll "$e.scroll_list_paraules set"  -height 11 -width 10 -bg #add8d8 -borderwidth 0 -relief raised
    scrollbar $e.scroll_list_paraules -command "$e.list_paraules yview" 
    
    bind $e.list_paraules <Double-1> "mostrar_temes"
    bind $e.list_paraules <1> {
	if {[winfo exists %W]} {
	    tk::ListboxBeginSelect %W [%W index @%x,%y]
	}
	break
    }
    
    
    
    label $e2.text -text [_ "Choose a topic:"] -background grey93
    
    listbox $e2.list_temes -yscroll "$e2.scroll_list_temes set"  -height 9 -width 10 -bg #add8d8 -borderwidth 0 -relief raised
    scrollbar $e2.scroll_list_temes -command "$e2.list_temes yview"
    
    bind $e2.list_temes <Double-1> "click_list_temes"
    bind $e2.list_temes <1> {
	if {[winfo exists %W]} {
	    tk::ListboxBeginSelect %W [%W index @%x,%y]
	}
	break
    }
    
    
    
    
    # Contents: arbre
    #------------------------------------------------
    
    set arbre [Tree $e.tree \
		   -relief flat -borderwidth 3 -width 20 -height 5 -highlightthickness 0 -bg #add8d8 -padx 12 -deltax 8    -yscrollcommand "$e.scroll set"\
		   -droptypes {
		       TREE_NODE    {copy {} move {} link {}}
		       LISTBOX_ITEM {copy {} move {} link {}}
		   } ]
    
    
    scrollbar $e.scroll -orient vertical -command "$e.tree yview"
    pack $e.scroll -side left -fill y 
    
    
    $arbre bindImage <1> {click_arbre}
    $arbre bindText  <1> {click_arbre}
    
    $e.tree configure -cursor hand2
    
    set var(imatge1) [image create photo -file \
		          [file join $var(path_images) prof1.gif]]
    
    set var(imatge2) [image create photo -file \
		          [file join $var(path_images) prof2.gif]]
    
    set var(imatge3) [image create photo -file \
		          [file join $var(path_images) prof3.gif]]
    
    
    pack $e.tree -side bottom -expand yes  -anchor nw  -fill both 
    
    
    # widget html
    #------------------------------------------------
    
    
    html $w.html \
	-yscrollcommand "$whlp.dreta.fondo.scroll set" \
	-padx 5 \
	-pady 9 \
	-formcommand FormCmd \
	-imagecommand ImageCmd \
	-scriptcommand ScriptCmd \
	-appletcommand AppletCmd \
	-resolvercommand ResolverCmd \
	-underlinehyperlinks 0 -tablerelief sunken  -width 600 -height 463\
	-bg white -borderwidth 0
    
    if { $GidPriv(GiDRuedas)} {
	if {[string equal "windows" $::tcl_platform(platform)]} {
	    proc ProcWheelHelp { w incre } {
		if { $incre > 0 } {
		    $w yview scroll -1 units
		} else {
		    $w yview scroll 1 units
		}
	    }
	    bind [winfo toplevel $w.html] <MouseWheel> "ProcWheelHelp $w.html %D"        
	} else {
	    bind [winfo toplevel $w.html] <4> "$w.html yview scroll -1 units"
	    bind [winfo toplevel $w.html] <5> "$w.html yview scroll  1 units"
	}
    }
    
    
    
    
    scrollbar $w.scroll -orient vertical -command "$whlp.dreta.fondo.html yview"
    
    pack $w.scroll -side right -fill y 
    pack $w.html -expand yes -fill both
    
    
    # botons back, forw, print i quit
    #------------------------------------------------
    
    set var(imatge_fletxa_e) [image create photo -file [file join $var(path_images) fletxa_e.gif]]
    set var(imatge_fletxa_d) [image create photo -file [file join $var(path_images) fletxa_d.gif]]
    set var(imatge_quit) [image create photo -file [file join $var(path_images) quit.gif]]
    
    button $whlp.dreta.base.back  -text [_ "BACK"] -command "BACK" -image $var(imatge_fletxa_e) -background        grey93 -relief flat
    button $whlp.dreta.base.forw  -text [_ "FORW"] -command "FORW" -image $var(imatge_fletxa_d) -background        grey93 -relief flat
    button $whlp.dreta.base.quit  -text [_ "Quit"] -command "destroy $_whlp" -image $var(imatge_quit) -background grey93 -relief flat
    button $whlp.dreta.base.print -text [_ "Print"] -command Print
    #label $whlp.dreta.base.espai1 -text "                                          "  -bg grey93
    #label $whlp.dreta.base.espai2 -text "    "  -bg grey93
    #label $whlp.dreta.base.espai3 -text "    "  -bg grey93
    #label $whlp.dreta.base.espai4 -text "                "  -bg grey93
    
    
    #pack $whlp.dreta.base.espai2 $whlp.dreta.base.quit  $whlp.dreta.base.espai4  $whlp.dreta.base.espai1 $whlp.dreta.base.forw $whlp.dreta.base.espai3 $whlp.dreta.base.back -side right
    grid $whlp.dreta.base.back -row 0 -column 0 -sticky w -padx 10
    grid $whlp.dreta.base.forw -row 0 -column 1 -sticky w -padx 10
    grid $whlp.dreta.base.quit -row 0 -column 2 -sticky e

    grid rowconfigure $whlp.dreta.base 0 -pad 10
    grid columnconfigure $whlp.dreta.base 2 -weight 1
    
    # packs dels widgets actuals
    #------------------------------------------------
    
    #pack $whlp.dreta.base -side bottom -fill x -expand yes -ipadx 10 -ipady 10
    #pack $whlp.dreta.fondo -side top -expand true -fill both  -anchor n 
    
    grid $whlp.dreta.fondo -row 0 -column 0 -sticky "snew"
    grid $whlp.dreta.base -row 1 -column 0 -sticky "ew"
    grid rowconfigure $whlp.dreta 0 -weight 1
    grid columnconfigure $whlp.dreta 0 -weight 1
    
    
    #pack propagate $whlp.esq.mode 0
    pack $whlp.esq.mode -side top -ipadx 10 -ipady 8  -fill x
    pack $whlp.esq.index -side bottom -expand true -fill both  -anchor n
    
    if {$twopanes} {
	$whlp add $whlp.esq $whlp.dreta
	#pack $whlp.esq -side left  -fill both -expand true -anchor nw
	#pack $whlp.dreta -side left -fill both  -expand true 
	
    } else {
	pack $whlp.dreta -side left -fill both  -expand true
    }
    pack $whlp -fill both -expand yes
    
    bind $whlp.dreta.fondo.html.x <1> {HrefBinding %x %y $finestra}
    
    # keyboard bindings for the html widget
    #------------------------------------------------
    
    bind $_whlp <End>   "$whlp.dreta.fondo.html yview end"
    bind $_whlp <Home>  "$whlp.dreta.fondo.html yview 0.0"
    bind $_whlp <Next>  "$whlp.dreta.fondo.html yview scroll 1 page"
    bind $_whlp <Prior> "$whlp.dreta.fondo.html yview scroll -1 page"
    bind $_whlp <space> "$whlp.dreta.fondo.html yview scroll 1 page"
    
    
    bind $_whlp <Up>     "moviment_list_up"
    bind $_whlp <Down>   "moviment_list_down"
    bind all   <Tab>    "canvi_tab $whlp"
    if { $help_type != "TUTORIAL" } {
	bind $_whlp <Escape> "destroy $_whlp"
    }
    
    
    bind $whlp.dreta.fondo.html <Configure> "update ; $whlp.dreta.fondo.html xview moveto 0.0 "
    
    
    
    
    
    # binding for selection
    #------------------------------------------------
    
    bind $_whlp <1> "ManageSel $whlp.dreta.fondo.html %W %x %y press"    
    bind $_whlp <B1-Motion> "ManageSel $whlp.dreta.fondo.html %W %x %y motion"    
    bind $_whlp <ButtonRelease-1> "ManageSel $whlp.dreta.fondo.html %W %x %y release"
    
    bind $_whlp <Control-c> "CopySelected $whlp.dreta.fondo.html"
    if { $tcl_platform(platform) != "windows"} {selection handle $whlp.dreta.fondo.html "CopySelected $whlp.dreta.fondo.html"}
    
}


############################################################
#
# a continuació hi ha les funcions que executen els botons:
# Print, forward, backward, ShowContents, ShowIndex
#  
############################################################

proc Print {} {

}



############################################################
#
#  BACK i FORW recuperen els links de les pagines ja visitades
#  que estan guardats a la llista $var(history). 
#  (a la llista es guarda tot el link: path + nom_fitxer)
#  
#  La variable $var(pos_history) indica en quin punt de la llista 
#  s'esta llegint. 
#
#  Si s'esta a la posicio "i" de la llista i l'usuari escull
#  un nou link, s'esborra la llista des de la posicio "i"
#  fins al final
#
############################################################



proc FORW {} {
global var arbre estat

    set win $var(win)

 if {$var(pos_history)>1} {
     set var(pos_history) [expr $var(pos_history)-2] 
     set link [lindex $var(history) end-$var(pos_history)] 
     set var(pos_history) [expr $var(pos_history)+1] 
     IndexLink $link $win.dreta.fondo.html
     if {$estat(actual) == "index"} {$win.dreta.fondo.html yview scroll -1 unit}
    }
}



proc BACK {} {
global var arbre estat

    set win $var(win)

 set link [lindex $var(history) end-$var(pos_history)] 
 if {$link != ""} {
   IndexLink $link $win.dreta.fondo.html
   if {$estat(actual) == "index"} {$win.dreta.fondo.html yview scroll -1 unit}
   set var(pos_history) [expr $var(pos_history)+1] 
 } else {
      #IndexLink [file join $var(path_manual) intro.html] $win.dreta.fondo.html
      #set var(history) [lreplace $var(history) end end]     
	}
}




############################################################
#
#  ShowContents, ShowIndex i ShowSearch fan el pack dels frames corresponents.
#  S'executen quan es prem el botó "contents", "index" o "search"
#  respectivament.
#  La variable $estat indica quin dels dos modes es l'actual.
#
############################################################


proc ShowContents {} {
global estat finestra arbre var LastFile GIDDEFAULT

    if {[winfo exists .wait]} return

    set win $var(win)

if {$estat(actual) !="contents"} {
    set e $win.esq.index
    set e2 $win.esq.index2
    
	pack forget $e.list
	pack forget $e.scroll_list
	pack forget $win.esq.index_top.index_entry
	pack forget $win.esq.index_top.text_entry
	pack forget $win.esq.index_top

	pack forget $e.search_entry
	pack forget $e.text
	pack forget $e.scroll_list_paraules
	pack forget $e.list_paraules

	pack forget $e2.text
	pack forget $e2.scroll_list_temes
	pack forget $e2.list_temes
	pack forget $e2
	pack forget $win.esq.search_top

	pack $e.scroll -side left -fill y
	pack $e.tree -side bottom -expand yes  -anchor nw  -fill both
	pack $e -side top  -fill both -expand yes

	set estat(actual) contents
    }


       if {$estat(contents)=="not_loaded"} {
	   set whlp $win
	      set LastFile [file join $var(path_manual) $var(path_intro)]
	   IndexLink [file join $var(path_manual) $var(path_intro)] $whlp.dreta.fondo.html
	   cursor_wait
	   wait_win
	   update

	   main [file join $var(path_manual) $var(toc_file)]

	   #$arbre delete  [expr $var(node)-1] 
	   destroy .wait
	   cursor_normal
	   set estat(contents) loaded
	}

}



proc ShowIndex {} {
    global estat var
    
    if {[winfo exists .wait]} return
    
    set win $var(win)

    if {$estat(actual) !="index"} {
	set e $win.esq.index
	set e2 $win.esq.index2
	
	pack forget $e.tree
	pack forget $e.scroll
	
	pack forget $e.search_entry
	pack forget $e.text
	pack forget $e.scroll_list_paraules
	pack forget $e.list_paraules
	
	pack forget $e2.text
	pack forget $e2.scroll_list_temes
	pack forget $e2.list_temes
	pack forget $e2
	pack forget $e
	pack forget $win.esq.search_top
	
	
	pack $win.esq.index_top.text_entry -side left
	pack $win.esq.index_top.index_entry  -pady 5 -fill x 
	pack $win.esq.index_top -side top  -anchor w -padx 11 -fill x
	pack $win.esq.index.scroll_list -side left -fill y 
	pack $e.list -side bottom -expand yes -fill both 
	pack $win.esq.index -side top -fill both  -expand yes  -anchor nw
    }
    
    set estat(actual) index
    focus $win.esq.index_top.index_entry
    
    if {$estat(index)=="not_loaded"} {
	wait_win
	set var(compt) 0
	update
	main [file join $var(path_manual) $var(path_cindex)]
	destroy .wait
	$win configure -cursor arrow
	set estat(index) loaded
    }
    set estat(actual) "index"
}



proc ShowSearch {} {
global estat var searchString LastFile


    if {[winfo exists .wait]} return
    
    set win $var(win)

if {$estat(actual) != "search"} {
    set e  $win.esq.index
    set e2 $win.esq.index2

	pack forget $e.tree
	pack forget $e.scroll

	pack forget $e.list
	pack forget $e.scroll_list
	pack forget $win.esq.index_top.index_entry
	pack forget $win.esq.index_top.text_entry
	pack forget $win.esq.index_top
	pack forget $e


    pack $win.esq.search_top.text_entry  -side left
    pack $win.esq.search_top.search_entry -pady 5 -fill x
    pack $win.esq.search_top -side top -anchor w -padx 11 -fill x

    pack $e.text -side top -fill x 
    pack $e.scroll_list_paraules -side left -fill y 
    pack $e.list_paraules  -side bottom -expand yes -fill both

    pack $e -side top  -fill both -expand yes


    pack $e2.text -side top -fill x 
    pack $e2.scroll_list_temes -side left -fill y 
    pack $e2.list_temes -side bottom -expand yes -fill both

    pack $e2 -fill both -expand yes
}

focus $win.esq.search_top.search_entry

set whlp $win

if {$estat(search) != $var(path_search_file)} {
    
    set LastFile [file join $var(path_manual) $var(path_intro)]   
    IndexLink [file join $var(path_manual) $var(path_intro)] $whlp.dreta.fondo.html
    wait_win
    
    update
    omplir_array_search $var(path_search_file)
    destroy .wait
    $win configure -cursor arrow
    set estat(search) $var(path_search_file)
} 

set estat(actual) search
}




############################################################
#
#  click_arbre (node) s'executa quan des de l'arbre s'escull un
#  tema. Es crida la funcio IndexLink de la llibreria HTML,
#  per tal de mostrar la informació al frame HTML.
#  El link s'afegeix al final de la llista $var(history).
#
############################################################


proc click_arbre {node} {
global arbre var
set win $var(win)

    set link [$arbre itemcget $node -data ]
    if { [$arbre itemcget $node -open] } {
	$arbre itemconfigure $node -open 0
    } else {
	$arbre opentree $node 0
    }
    generic_click $link $win.dreta.fondo.html
}


############################################################
#
#  click_list (la llista index)
#
############################################################

proc click_list {} {
    global var cindex
    
    set win $var(win)
    
    set num [$win.esq.index.list curselection]
    
    if {$num!=""} {
	set link $cindex($num)
	
	generic_click $link $win.dreta.fondo.html
	$win.dreta.fondo.html yview scroll -1 unit
    }
}


proc click_list_temes {} {
global temes_actuals var

    set w $var(win)

   set num [$w.esq.index2.list_temes curselection]
   if {$num != ""} {
     set selection [$w.esq.index2.list_temes get $num] 
     set link $temes_actuals($selection)
     generic_click $link $w.dreta.fondo.html 
     cursor_wait
     update
     if {[regexp {(.*)#(.*)} $link tot fitxer num_link]} {
	 seleccionar_paraula_search $num_link $w
     }
     cursor_normal
   }


}


############################################################
#
#  seleccionar_paraula_search
#
############################################################

proc seleccionar_paraula_search {num_link_actual win} {
global var
set w $win.dreta.fondo.html
set i 0
cursor_wait
update idletasks
set $var(paraula_search_actual) [string tolower $var(paraula_search_actual)]
set tema_trobat "FALSE"

foreach tok [$w token list 1 end] {
	set i [expr $i + 1]
	set type [lindex $tok 0]

	set contents [lrange $tok 1 end]
	set contents [string tolower $contents]
	set link_tema [lrange $tok 3 3]

	if {$type == "Markup" && $link_tema == $num_link_actual}  {
	       set tema_trobat "TRUE"
	    }


	if {$tema_trobat == "TRUE" && $type == "Text" && ($var(paraula_search_actual)==$contents || "$var(paraula_search_actual),"==$contents || "$var(paraula_search_actual)."==$contents)} {
	    $w sel set [expr $i-1] [expr 1 + $i + [llength $var(paraula_search_actual)]]
	    return
	    }
  }
}




############################################################
#
#  generic_click
#
############################################################

proc generic_click {link w} {
global var

     IndexLink [file join $var(path_manual) $link] $w

#tkMessageBox -message $link

     if {$var(pos_history) != 1} {
	set var(history) [lreplace $var(history) [expr [llength $var(history)] - $var(pos_history)+1] end ] }
     lappend var(history) [file join $var(path_manual) $link]
     set var(pos_history) 1

}


############################################################
#
#  canvi_tab      (us tabulador)
#
############################################################

proc canvi_tab {w} { 
global estat


 if {$estat(actual)=="search"} {
     set actuals [$w.esq.index2.list_temes curselection]
     
     if {$actuals !=""} {
     # vaig cap a list paraules
	 set estat(list_temes) $actuals 
	 if {$estat(list_paraules)!=""} {
	   $w.esq.index.list_paraules selection set $estat(list_paraules)
	 } else {$w.esq.index.list_paraules selection set 0}
     # vaig cap a list temes         
     } else {
	 set estat(list_paraules) [$w.esq.index.list_paraules curselection]
	 if {$estat(list_temes)!=""} { 
	   if {[expr $estat(list_temes)+1] > [$w.esq.index2.list_temes size]} {
	      $w.esq.index2.list_temes selection set 0
	      } else {$w.esq.index2.list_temes selection set $estat(list_temes)}
	 } else {$w.esq.index2.list_temes selection set 0}
       }
     }
  
}


############################################################
#
#  moviment_list_up i down      (seleccio amb tecles)
#
############################################################

proc moviment_list_up {} {
    global estat var
    
    set w $var(win)
    
    if {$estat(actual)=="index"}  {set actual_list $w.esq.index.list}
    if {$estat(actual)=="search"} {
	set actuals [$w.esq.index2.list_temes curselection]
	if {$actuals !=""} {
	    set actual_list $w.esq.index2.list_temes
	} else {
	    set actual_list $w.esq.index.list_paraules
	}
    }

    if {$estat(actual)=="contents"} {
	$w.dreta.fondo.html yview scroll -1 units
    } else {
	if { [info exists actual_list] } {
	    set actuals [$actual_list curselection]
	    if {$actuals !="" && $actuals!= 0} {
		$actual_list selection clear $actuals
		$actual_list selection set [expr $actuals-1]
		$actual_list yview scroll -1 unit  
	    } else {
		$actual_list selection set 0
	    }
	}
    }
}


proc moviment_list_down {} {
    global estat var

    set w $var(win)

    if {$estat(actual)=="index"}  {set actual_list $w.esq.index.list}
    if {$estat(actual)=="search"} {
	set actuals [$w.esq.index2.list_temes curselection]
	if {$actuals !=""} {
	    set actual_list $w.esq.index2.list_temes
	} else {
	    set actual_list $w.esq.index.list_paraules
	}
    }

   if {$estat(actual)=="contents"} {
       $w.dreta.fondo.html yview scroll 1 units
   } else {
       if { [info exists actual_list] } {
	   set actuals [$actual_list curselection]
	   if {$actuals !=""} {
	       if {$actuals != [$actual_list index end]-1} {
		   $actual_list selection clear $actuals
		   $actual_list selection set [expr $actuals+1]
		   $actual_list yview scroll +1 unit 
	       }        
	   } else {
	       $actual_list selection set 0
	   }
       }
   }
}

############################################################
#
#  search_tecla_return  - tecla return en el search
#
############################################################

proc search_tecla_return {} {
    global var

    set w $var(win)

    set actuals [$w.esq.index2.list_temes curselection]
    if {$actuals !=""} { 
	click_list_temes
    } else {
	mostrar_temes
    }
}




############################################################
#
#  actualitzar_llista (index)
#
############################################################

proc actualitzar_llista {} {
global var indexString
set w $var(win)
set entrada $indexString

  set actuals [$w.esq.index.list curselection]
  if {$actuals!=""} {$w.esq.index.list selection clear $actuals}

  set entrada [string tolower $entrada]
  set i 0

  set nom  [$w.esq.index.list get $i]
  set nom  [string tolower $nom]
  set size [$w.esq.index.list size]

  while {$entrada>$nom && $entrada != ""} {
 
	  set i [expr $i + 1] 
	  if {$size<$i} {set i [expr $size-1]
		         set entrada ""
	  } else {
	    set nom [$w.esq.index.list get $i]
	    set nom [string tolower $nom]
		 }
  }

    $w.esq.index.list selection set   $i
    $w.esq.index.list yview [expr $i - 1]

}


############################################################
#
#  realitzar_busqueda (search)
#
############################################################

proc realitzar_busqueda {} {
global searchString search_array var
set w $var(win)

$w.esq.index.list_paraules delete 0 end

 if {$searchString != "" && [regexp {(([a-z]*[A-Z]*)*)} $searchString paraula]} { 
    
   set paraula [string tolower $paraula] 
   set llista  [array names search_array $paraula*]
   set llista  [lsort -ascii $llista]
   set i 0
    while {$i <= [llength $llista] } {
     if {[lindex $llista $i]!=""} {
	  $w.esq.index.list_paraules insert end [lindex $llista $i] 
	  $w.esq.index.list_paraules selection set 0
	}
     set i [expr $i+1]
    }
 }
}

############################################################
#
#  mostrar_temes   (en el search)
#
############################################################

proc mostrar_temes {} {
global search_array temes_actuals var searchString estat

    set w $var(win)
set ok 0
set selection ""

   set num [$w.esq.index.list_paraules curselection]
   set estat(list_paraules) [$w.esq.index.list_paraules curselection]
   if {$num != ""} {
     set selection [$w.esq.index.list_paraules get $num] 
     set var(paraula_search_actual) $selection
   } 
 

 if {$selection !=""} {
   $w.esq.index2.list_temes delete 0 end

   set llista1 [buscar_temes_actuals  $selection]
   if {[regexp {(([a-z]*[A-Z]*)*)([ ]+)(([a-z]*[A-Z]*)*)} $searchString tot p1 e e p2] && $p2!=""} {
	set llista2 [buscar_temes_actuals  $p2]
	set llista [interseccio_llistes $llista1 $llista2]
      } else {set llista $llista1}

	 
      set i 0
      set llista [lsort -ascii $llista]
      while {$i <= [llength $llista] } {
       if {[lindex $llista $i]!=""} {
	    $w.esq.index2.list_temes insert end [lindex $llista $i] 
	  }
       set i [expr $i+1]
      } 
 }


 if {$selection == ""} {
   set num [$w.esq.index2.list_temes curselection]
   if {$num != ""} {
     set selection [$w.esq.index2.list_temes get $num] 
   }
 }

$w.esq.index2.list_temes selection set 0
}


############################################################
#
#  interseccio_llistes
#
############################################################

proc interseccio_llistes {llista1 llista2} {
set llista        [list ""]
set i 0

    while {$i <= [llength $llista1] } {
      set actual [lindex $llista1 $i]
      if {$actual != ""} {
	 if {[lsearch $llista2 $actual] >= 0} {lappend llista $actual}
      }
      set i [expr $i+1]
    }

return $llista
}


############################################################
#
#  buscar_temes_actuals
#
############################################################

proc buscar_temes_actuals {selection} {
    global search_array temes_actuals var
    
    set llista        [list ""]
    set i 0
    if {[info exists temes_actuals] == 1} {unset temes_actuals}
    
    regsub {_toc.*} $var(toc_file) {} basefilename
    
if {[info exists search_array($selection)] == 1} {
      while {$i <= [llength $search_array($selection)] } {
	     
	 set tema [string toupper [lindex $search_array($selection) $i]]
	 set i [expr $i+1]
	 set link_tema [lindex $search_array($selection) $i]
	  if {[regexp {([0-9]*)#([0-9]*)} $link_tema tot num_fitxer num_link]} { 
	      #set link_tema "gid_$num_fitxer.html#SEC$num_link"
	      set link_tema "${basefilename}_$num_fitxer.html#${var(search_prefix)}$num_link"
	  }
	 set i [expr $i+1]

	 if {[info exists temes_actuals($tema)] == 0} {
		set temes_actuals($tema) $link_tema 
		lappend llista $tema
	    }      
      }  
 }
return $llista
}


############################################################
#
#  omplir_array_search
#
############################################################

proc omplir_array_search {nomfitxer} {
    global search_array var
    
    catch { unset search_array }
    set i 0
    set fitxer [open $nomfitxer r] 

    set var(capitol_actual) ""
    while { ![eof $fitxer] } {
	gets $fitxer linea
	 set linea [string tolower $linea]
	
	if {[regexp "(.*)\t(.*)\t(.*)" $linea {} paraula capitol link] } {

	    if {$capitol != "="} {
		 set var(capitol_actual) $capitol
		 } else {set capitol $var(capitol_actual) }

	    lappend search_array($paraula) $capitol

	  
 
	    lappend search_array($paraula) $link        
       }
	 
	 if {[expr $i % 600] == 0} {
	       barra "  "
	       update}
	 set i [expr $i+1]
    } 
    close $fitxer
}


#-----------------------------------------------------------------------------#
# A partir d'aqui hi ha les funcions que generen l'arbre i el seu contingut   #
#-----------------------------------------------------------------------------#

############################################################
#
#   omplir_arbre rep els parametres "link" i "title" i omple
#   l'arbre en funció de la profunditat. 
#
#   - La variable $var(nivell) indica si es puja, baixa o es
#   manté en la profunditat de l'arbre.
#
#   - La variable $var(correccio_error) corregeix els errors
#   de profunditat introduits per les incoherčncies del HTML.
#
#   - La variable $var(pares) es una llista que guarda els pares
#   de l'element actual.
#
#   - La variable $var(profunditat) indica la profunditat de
#   cada element. Serveix per posar diferents imatges en funció
#   de la profunditat
#
#    
############################################################



proc omplir_arbre {link title} {
    global var arbre
    
    if {$var(nivell)>0} {
	lappend var(pares) $var(node)
	set pare [lindex $var(pares) end-1] 
	set var(correccio_error) [expr $var(nivell)-1]
	set var(profunditat) [expr $var(profunditat)+1] 
    }
    
    if {$var(nivell)==0} {
	set var(pares) [lreplace $var(pares) end end]
	lappend var(pares) $var(node)
	set pare [lindex $var(pares) end-1] 
    }
    
    if {$var(nivell)<0} {
	
	while {$var(nivell)<[expr 0 - $var(correccio_error)]} {
	    set var(pares) [lreplace $var(pares) end end]
	    set var(pares) [lreplace $var(pares) end end]
	    lappend var(pares) $var(node) 
	    set var(nivell) [expr $var(nivell) +1]
	    set var(profunditat) [expr $var(profunditat)-1] 
	}
	set pare [lindex $var(pares) end-1] 
	if {$pare==""} {
	    set pare "root"
	    set var(profunditat) 1}
    }
    
    
    if {$var(profunditat) <= 1} {
	$arbre insert end $pare $var(node)  -text $title   -data $link -image $var(imatge1) }
    
    if {$var(profunditat) == 2} {
	$arbre insert end $pare $var(node)  -text $title   -data $link -image $var(imatge2) }
    
    if {$var(profunditat) >= 3} {
	$arbre insert end $pare $var(node)  -text $title   -data $link -image $var(imatge3) }
    
    
    set var(nivell) 0
    set var(node) [expr $var(node) +1]
}


############################################################
#
#  omplir_list
#    
############################################################

proc omplir_list {link title} {
  global var cindex

  set w $var(win)

  $w.esq.index.list insert end $title

  if {[expr $var(compt) % 45] == 0} {
      barra "   "
      update}

  set var(compt) [expr $var(compt) + 1]  

  set cindex($var(id_list)) $link
  set var(id_list) [expr $var(id_list) + 1]
}


############################################################
#
#  Inicialitzacio de variables globals
#  A la llista $var(history) s'hi afegeix la pagina intro.html
#  que conte el logo de GiD.
#    
############################################################


proc GIDHELP_ini_var {} {
 global var estat llista_termes GIDDEFAULT GIDDEFAULTTCL
    set var(index)  0
    set var(nivell) 0
    set var(node)   1
    set var(correccio_error) 0
    set var(profunditat) 0
    set var(pos_history) 1

    set var(path_manual) [file join [GiDGetInfoDir] html-version]
    set var(path_search_file) [file join $var(path_manual) search_data.txt]
    set var(path_cindex) [file join $var(path_manual) gid_18.html]
    set var(path_intro)  [file join $var(path_manual) intro intro.html]
    set var(path_images) [file join $GIDDEFAULT bitmaps]
    set var(search_prefix) SEC

    set var(pares)   [list root] 





    set var(id_list) 0
    set var(paraula_search_actual) ""

    set estat(actual)      none
    set estat(contents)    not_loaded
    set estat(index)       not_loaded
    if { ![info exists estat(search)] } {
	set estat(search)      not_loaded
    }
    set estat(list_temes)     ""
    set estat(list_paraules)  ""

    
}




proc ini_ref {} {
global var
   
    set var(history) [list ""] 
    lappend var(history) [file join $var(path_manual) $var(path_intro)]

}

proc ini_faq {} {
global var GIDDEFAULT
    set var(path_manual) [file join [GiDGetInfoDir] html-faq]

    set var(history) [list ""] 
    lappend var(history) [file join $var(path_manual) faq_toc.html]

}

proc ini_cust {} {
global var GIDDEFAULT
    set var(path_manual) [file join [GiDGetInfoDir] html-customization]

    set var(history) [list ""] 
    lappend var(history) [file join $var(path_manual) ptype_toc.html]

}


proc ini_other {dir} {
  global var GIDDEFAULT
    
  if { [string equal [file pathtype $dir] absolute] } {
    set var(path_manual) $dir
  } else {
     set var(path_manual) [file join $GIDDEFAULT problemtypes $dir "html"]
 }

  set var(history) [list ""] 
  lappend var(history) [file join $var(path_manual) index.html]

}

proc ini_other_file {file} {
    global var GIDDEFAULT
    set var(path_manual) [file join $GIDDEFAULT $file]
    set var(path_manual) [file dirname $var(path_manual)]

    set var(history) [list ""] 
    lappend var(history) [file join $GIDDEFAULT $file]

}


############################################################
#
#   La funció main es la funció que inicia el proces de lectura
#   del fitxer HTML que conté l'index, a partir del qual es 
#   genera l'arbre de continguts. 
#
#   - La variable $HTML_file conté el fitxer HTML d'entrada
#   - La variable $var(tam_fitxer) conte el tamany del fitxer
#   - La variable $var(index) indica la posicio actual de lectura
#     de la variable $HTML_file
#
#   
#
############################################################

proc main {nomfitxer} {
global var arbre 

    catch {
	set fitxer [open $nomfitxer r]
    }

    global errorInfo

    seek $fitxer 0 start
    set var(tam_fitxer) [file size $nomfitxer]
 
    set var(index) 0

    set HTML_file [read $fitxer $var(tam_fitxer)]
    close $fitxer

    while {$var(index) < [expr $var(tam_fitxer)-1] } {
	 set tag [analitzar_tag $HTML_file]
	 tractar_tag $tag $HTML_file 
     }

}



############################################################
#
#  Llegeix el seguent TAG des de la posició $var(index)
#    
############################################################


proc analitzar_tag {HTML_file} {
global var
   set aux [llegir_caracter $HTML_file]
   while {$var(index) < $var(tam_fitxer) &&  [string match $aux <] == 0} { 
     set aux [llegir_caracter $HTML_file]
   }
   

   set tag [llegir_opcio $HTML_file]
   return $tag
}




############################################################
#
#  La funcio tractar_tag distribueix les tasques en funció
#  de si s'ha llegit un tag <LI>, <UL> o </UL>. Els altres
#  tags son ignorats.
#
#   - El tag <UL> indica que s'augmenta la profunditat de
#     l'arbre, la variable $var(nivell) valdrą 1 o mes.
#
#   - El tag </UL> indica que disminueix la profunditat de
#     l'arbre, la variable $var(nivell) valdrą 1 o menys.
#    
############################################################

proc tractar_tag {tag HTML_file} {
    global var estat

  switch -exact -- $tag { 
    
    LI      {set link  [get_link  $HTML_file]
	     set title [get_title $HTML_file]

	     if {$estat(actual)=="contents"} {omplir_arbre $link $title}
	     if {$estat(actual)=="index"}   {omplir_list  $link $title}  }        

    UL      {   if {$estat(actual)=="contents"} {
		  set var(index)  [expr $var(index) +1] 
		  if {$var(nivell)>0} {set var(nivell) [expr $var(nivell) +1]
		  } else { set var(nivell) 1}
		}    
	    }

   /UL      {   if {$estat(actual)=="contents"} {
		  set var(index)  [expr $var(index) +1]
		  if {$var(nivell)<0} {set var(nivell) [expr $var(nivell) -1]
		  } else { set var(nivell) -1} 
		}   
	    }

    default {}

  }

}




############################################################
#
#  get_link i get_title obtenen el link i el nom del link
#  respectivament, del contingut de $HTML_file des de la 
#  posició $var(index)
#    
############################################################

proc get_link {HTML_file} {
global var estat

  if {$estat(actual)=="contents"} {set jump 18
      } else { set jump 7}
  set var(index)  [expr $var(index) + $jump]

  set aux [llegir_caracter $HTML_file]
  while {[string match $aux =] == 0 && $var(index) < $var(tam_fitxer)} {
     set aux [llegir_caracter $HTML_file]
  }

  set var(index)  [expr $var(index) +1]


   if {$var(index) >= $var(tam_fitxer)} {
    set link -1
   } else {
     set aux [llegir_caracter $HTML_file]
     while  {[string match $aux \"] == 0 && $var(index) < $var(tam_fitxer)}  {
      append link $aux  
      set aux [llegir_caracter $HTML_file]
     }
   }
   set var(index)  [expr $var(index) +1]

   return $link 
} 


proc get_title {HTML_file} {
global var

   if {$var(index) >= $var(tam_fitxer)} {
    set title -1
   } else {
     set aux [llegir_caracter $HTML_file]
     while  {[string match $aux <] == 0 && $var(index) < $var(tam_fitxer)}  {
      append title $aux  
      set aux [llegir_caracter $HTML_file]
     }
   }
   set var(index)  [expr $var(index) +3]
   return $title 



}





############################################################
#
#  llegir_opcio serveix per a llegir un tag del fitxer contingut
#  a $HTML_file des de la posició $var(index)
#    
############################################################

proc llegir_opcio {HTML_file} {
global var


   if {$var(index) >= $var(tam_fitxer)} {
    set opcio -1
   } else {
     set aux [llegir_caracter $HTML_file]
     while  {[string match $aux =] == 0 && [string match $aux >] == 0 && [string match $aux { }] == 0 && $var(index) < $var(tam_fitxer)}  {
      append opcio $aux  
      set aux [llegir_caracter $HTML_file]
     }
   
   }
   if {$var(index)>0} {set var(index) [expr $var(index) -1]}  
   return [string toupper $opcio]
}


############################################################
#
#  llegir_caracter retorna un caracter del fitxer contingut 
#  a la varialbe $HTML_file. S'actualitza la posicio de 
#  lectura incrementant $var(index).
#    
############################################################

proc llegir_caracter {HTML_file}  {
global var

set var(index) [expr $var(index) +1]
return [string index $HTML_file $var(index)]
}




############################################################
#
#  wait_win genera una finestra amb el text "Please wait" que
#  apareix mentre s'estą generant l'arbre.
#    
############################################################

proc wait_win {} {
    global var

    $var(win) configure -cursor watch
    toplevel .wait 
    text .wait.t -height 8 -width 35 
    
    .wait.t tag configure estil -font {Arial 16 bold} -foreground mediumblue
    .wait.t tag configure estil2 -font {Arial 10 bold} -background mediumblue  -relief raised -borderwidth 3
    .wait.t insert end "\n \n"
    .wait.t insert end [_ "Please wait ... "] estil
    .wait.t insert end "\n \n"
     pack .wait.t -side top -padx 2m -pady 2m 
    grab .wait
    wm overrideredirect .wait 1
    
    set top_whlp [winfo toplevel $var(win)]
    set x [expr [winfo x $top_whlp]+[winfo width $var(win)]/2-[winfo reqwidth .wait]]
    set y [expr [winfo y $top_whlp]+[winfo height $var(win)]/2-[winfo reqheight .wait]]
    
    #wm geometry .wait +300+200
    wm geometry .wait  +${x}+${y}
    .wait configure -cursor watch
    update
}


############################################################
#
#   barra d'espera
#    
############################################################

proc barra {espai} {
      .wait.t insert end $espai estil2
      update
}



#-------------------------------------------------------------------------#
#-------------------------------------------------------------------------#
#                                 EXECUCIO                                #
#-------------------------------------------------------------------------#
#-------------------------------------------------------------------------#

proc GiDCustomHelp { args } {
  array set options {
    -start ""
    -report 0
  }
  set options(-dir) [file join [GetDirectoryProblemType] "html"]
  set options(-title) "Help on [lindex [.central.s info Project] 0]"
  array set options $args
  GiDHelpWindow -type CUSTOM -dir $options(-dir) -start $options(-start) \
      -report $options(-report) -title $options(-title)
}

proc GiDGetInfoDir { } {
  set lan [GidUtils::GetCurrentLanguage]
  set info_dir [file join $::GIDDEFAULT info]
  set base_dir [file join $info_dir $lan]
  if {![file exists $base_dir]} {
    set base_dir [file join $info_dir en]
  }
  set base_dir
}

proc GiDHelpWindow { args } {
  array set options {
    -type "REFERENCE"
    -dir ""
    -start ""
    -report 0
    -toplevel ""
    -tab tree
  }
  set options(-title) [_ "Help"]
  array set options $args
  
  switch [string toupper $options(-type)] {
    REFERENCE {
      set options(-dir) [file join $::GIDDEFAULT [GiDGetInfoDir]]
      set options(-title) [_ "GiD Reference"]
    }
    REPORT {
      set options(-title) [_ "Report"]
      set options(-report) 1
    }
    CUSTOM {
      set options(-toplevel) .ctmhelp
    }
  }
  
  if {$options(-dir) eq ""} {
    error [_ "Calling GiDHelpWindow: should provide a -dir option"] 
  }
  
  if {$options(-toplevel) eq ""} {
    GiDHelpViewer::Show $options(-dir) \
	-title $options(-title) \
	-try $options(-start) \
	-report $options(-report) \
	-tab $options(-tab)
    
  } else {
    GiDHelpViewer::Show $options(-dir) \
	-title $options(-title) \
	-try $options(-start) \
	-report $options(-report) \
	-base $options(-toplevel) \
	-tab $options(-tab)
  }
} 

proc HelpWindow { help_type dir { intro_file "" } } {
    global var estat arbre finestra LastFile GIDDEFAULT
  
    GIDHELP_ini_var
    GIDHELP_setup $help_type
  
  
    set whlp $var(win)
    set top_whlp [winfo toplevel $whlp]
    set finestra $whlp.dreta.fondo.html
    
    
    
    if {$help_type == "REFERENCE"} {
	ini_ref
	wm geometry $top_whlp 800x550
	set var(path_manual) [file join [GiDGetInfoDir] html-version]
	set var(toc_file) gid_toc.html
	ShowContents
	if {$dir!=""} {generic_click $dir $whlp.dreta.fondo.html}
    }
    
    if {$help_type == "SEARCH"} {
	ini_ref
	
	set var(path_manual) [file join [GiDGetInfoDir] html-version]
	set var(toc_file) gid_toc.html       
	ShowSearch
    }
    
    if {$help_type == "CUSTOMIZATION"} {
	ini_cust
	
	set LastFile [file join $var(path_manual) ptype_toc.html]
	IndexLink [file join $var(path_manual) ptype_toc.html] $whlp.dreta.fondo.html  
	
    }
    
    if {$help_type == "FAQ"} {
	ini_faq
	
	set LastFile [file join $var(path_manual) faq_toc.html]
	IndexLink [file join $var(path_manual) faq_toc.html] $whlp.dreta.fondo.html    
    }
    
    if {$help_type == "CUSTOM_HELP"} {
	ini_other $dir
	
	set LastFile [file join $var(path_manual) index.html]
	IndexLink [file join $var(path_manual) index.html] $whlp.dreta.fondo.html    
    }
    
    if {$help_type == "CUSTOM_HELP_FILE" || $help_type == "TUTORIAL"} {
	
	ini_other_file $dir
	if {$help_type == "CUSTOM_HELP_FILE"} {wm geometry $top_whlp 679x580}
	if {$help_type == "TUTORIAL"} {wm geometry $top_whlp 600x500}
	
	set LastFile [file join $GIDDEFAULT $dir]
	IndexLink [file join $GIDDEFAULT $dir] $whlp.dreta.fondo.html    
    }
    
    if {$help_type == "CUSTOM_HELP_REFERENCE"} {
	ini_ref
	
	set var(path_manual) [file dirname $dir]
	set var(toc_file) [file tail $dir]
	set var(search_prefix) "_Toc"
	
	set var(path_search_file) [file join $var(path_manual) search_data.txt]
	set var(path_cindex) ""
	if { $intro_file == "" } {
	    set var(path_intro) $dir
	} else {
	    set var(path_intro) $intro_file
	}
	
	ShowContents
	if {$dir!="" && $intro_file=="" } {
	    generic_click $dir $whlp.dreta.fondo.html
	}
    }
    
}

if { !$insidegid } {
    source html_lib.tcl
    source bwidget.tcl
    
    WarnWinText [_ "Termine de cargar"]
}

proc directHelp {link} {
    global var estat
    
    #HelpWindow "REFERENCE" $link
    GiDHelpWindow -type REFERENCE -start [file join html-version $link]
    
}

proc ToolbarHelp {toolbar} {

   #tkMessageBox -message $toolbar
   if {$toolbar == "New GiD project"} {return  "gid_6.html#SEC26"
} elseif {$toolbar == "Open GiD project"} {return  "gid_6.html#SEC27"
} elseif {$toolbar == "Save GiD project"} {return  "gid_6.html#SEC28"
} elseif {$toolbar == "Take a snapshot"} {return  "gid_6.html#SEC51"
} elseif {$toolbar == "Print current view"} {return  "gid_1.html#SEC1"
} elseif {$toolbar == "Toggle between pre and postprocess"} {return  "gid_13.html#SEC180"
} elseif {$toolbar == "Copy/Transform"} {return  "gid_9.html#SEC124"
} elseif {$toolbar == "Preferences"} {return  "gid_9.html#SEC114"
} elseif {$toolbar == "Help search"} {return  "gid_1.html#SEC1"
} elseif {$toolbar == "Help"} {return  "gid_1.html#SEC1"
} elseif {$toolbar == "Exit GiD"} {return  "gid_5.html#SEC23"
} elseif {$toolbar == "Zoom in"} {return  "gid_7.html#SEC54"
} elseif {$toolbar == "Zoom out"} {return  "gid_7.html#SEC54"
} elseif {$toolbar == "Zoom frame"} {return  "gid_7.html#SEC54"
} elseif {$toolbar == "Redraw"} {return  "gid_7.html#SEC64"
} elseif {$toolbar == "Rotate trackball"} {return  "gid_7.html#SEC55"
} elseif {$toolbar == "Pan dynamic"} {return  "gid_7.html#SEC63"
} elseif {$toolbar == "Pan"} {return  "gid_7.html#SEC63"
} elseif {$toolbar == "Layers"} {return  "gid_7.html#SEC68"
} elseif {$toolbar == "Create line"} {return  "gid_8.html#SEC76"
} elseif {$toolbar == "Create arc"} {return  "gid_8.html#SEC77"
} elseif {$toolbar == "Create nurbs line"} {return  "gid_8.html#SEC79"
} elseif {$toolbar == "Create polyline"} {return  "gid_8.html#SEC80"
} elseif {$toolbar == "Create NURBS surface"} {return  "gid_8.html#SEC85"
} elseif {$toolbar == "Create volume"} {return  "gid_8.html#SEC86"
} elseif {[regexp {Create Object} $toolbar]==1} {return  "gid_8.html#SEC98"
} elseif {$toolbar == "Delete"} {return  "gid_8.html#SEC99"
} elseif {$toolbar == "List entities" || $toolbar == "List Entities"
} {return  "gid_9.html#SEC119"
} elseif {$toolbar == "Toggle geometry-mesh view"} {return  "gid_8.html#SEC73"
} elseif {$toolbar == "Change light vector direction"
} {return  "gid_9.html#SEC137"
} elseif {$toolbar == "Display Style"} {return  "gid_13.html#SEC184"
} elseif {$toolbar == "View style"} {return  "gid_13.html#SEC184"
} elseif {$toolbar == "Culling Style"} {return  "gid_13.html#SEC184"
} elseif {[regexp {Switch Volume sets} $toolbar]==1} {return  "gid_13.html#SEC184"
} elseif {[regexp {Switch Surface set} $toolbar]==1} {return  "gid_13.html#SEC184"
} elseif {[regexp {Switch Cut set} $toolbar]==1} {return  "gid_13.html#SEC184"
} elseif {[regexp {Cut Volume/Surface sets} $toolbar]==1} {return  "gid_14.html#SEC202"
} elseif {$toolbar == "Set maximum value"} {return  "gid_14.html#SEC188"
} elseif {$toolbar == "Set minimum value" } {return  "gid_14.html#SEC188"
} elseif {$toolbar == "Reset Contour Limit Values"
} {return  "gid_14.html#SEC188"
} else {
       return ""
   }
}

# tutorials ini window#

proc CreateTutorial_win {} {
  global GIDDEFAULT GidPriv

  set base .gid.tutorials
  InitWindow $base [_ "GiD tutorials"] GiDTutorialswin "" "" 1
  $base configure -background #EEEEEE
  #wm minsize $base 430 430
  #wm maxsize $base 430 430
  set image_tutorials [image create photo -file [file join [GiDGetInfoDir] html-tutorials images_1 gidtutorials.gif]]       
  set image_tutorial1 [image create photo -file [file join [GiDGetInfoDir] html-tutorials images_1 tutorial1.gif]]       
  set image_tutorial2 [image create photo -file [file join [GiDGetInfoDir] html-tutorials images_1 tutorial2.gif]]       
  set image_tutorial3 [image create photo -file [file join [GiDGetInfoDir] html-tutorials images_1 tutorial3.gif]]       
  
  set image_tutorial1_over [image create photo -file [file join [GiDGetInfoDir] html-tutorials images_1 tutorial1_o.gif]]       
  set image_tutorial2_over [image create photo -file [file join [GiDGetInfoDir] html-tutorials images_1 tutorial2_o.gif]]       
  set image_tutorial3_over [image create photo -file [file join [GiDGetInfoDir] html-tutorials images_1 tutorial3_o.gif]]       
  
  label $base.label_imgtop -image $image_tutorials -bd 0
  
  button $base.button#1 \
      -relief flat \
      -image $image_tutorial1 -background #EEEEEE \
      -command {GiDHelpWindow -type REFERENCE -start [file join html-tutorials tutorial_1]
	destroy .gid.tutorials
      }
  
  
  button $base.button#2 \
      -relief flat \
      -image $image_tutorial2 -background #EEEEEE \
      -command {GiDHelpWindow -type REFERENCE  -start [file join html-tutorials tutorial_2] 
	destroy .gid.tutorials
      }
  
  button $base.button#3 \
      -relief flat \
      -image $image_tutorial3 -background #EEEEEE \
      -command {GiDHelpWindow -type REFERENCE -start [file join html-tutorials tutorial_3] 
	destroy .gid.tutorials
      }
  
  bind $base.button#1 <Enter> ".gid.tutorials.button#1 configure -image $image_tutorial1_over"
  bind $base.button#1 <Leave> ".gid.tutorials.button#1 configure -image $image_tutorial1"
  
  bind $base.button#2 <Enter> ".gid.tutorials.button#2 configure -image $image_tutorial2_over"
  bind $base.button#2 <Leave> ".gid.tutorials.button#2 configure -image $image_tutorial2"
  
  bind $base.button#3 <Enter> ".gid.tutorials.button#3 configure -image $image_tutorial3_over"
  bind $base.button#3 <Leave> ".gid.tutorials.button#3 configure -image $image_tutorial3"
  
  
  
  
  # Geometry management
  
  grid $base.label_imgtop -in $base        -row 0 -column 0 -columnspan 2 -sticky ne
  grid $base.button#1 -in $base        -row 1 -column 0 
  grid $base.button#2 -in $base        -row 1 -column 1 
  grid $base.button#3 -in $base        -row 2 -column 0 -columnspan 2
  
  
  # Resize behavior management
  
  grid rowconfigure $base 1 -weight 1 -minsize 30
  #grid rowconfigure $base 2 -weight 0 -minsize 174
  #grid columnconfigure $base 1 -weight 0 -minsize 199
  #grid columnconfigure $base 2 -weight 0 -minsize 253
  
  #grid rowconfigure $base.frame 1 -weight 0 -minsize 30
  #grid columnconfigure $base.frame 1 -weight 0 -minsize 30
  
  tkwait visibility $base
  grab  $base 
}

proc CreateGiDInfo_win {} {
global GIDDEFAULT GidPriv tcl_platform

	set base .gid.info_gid
	InitWindow $base [_ "GiD Info"] GiDInfowin "" "" 1
	$base configure -background #EEEEEE
	#wm minsize $base 459 330
	#wm maxsize $base 459 330
	set image_top  [image create photo -file [file join $GIDDEFAULT bitmaps gidinfo.gif]]       
	set image_but1 [image create photo -file [file join $GIDDEFAULT bitmaps buton_about.gif]]       
	set image_but2 [image create photo -file [file join $GIDDEFAULT bitmaps buton_help.gif]]       
	set image_but3 [image create photo -file [file join $GIDDEFAULT bitmaps buton_tutorials.gif]]       
	set image_but4 [image create photo -file [file join $GIDDEFAULT bitmaps buton_web.gif]]       
	set image_but5 [image create photo -file [file join $GIDDEFAULT bitmaps buton_register.gif]]       
	set image_but6 [image create photo -file [file join $GIDDEFAULT bitmaps buton_close.gif]]       

	set image_but1_over [image create photo -file [file join $GIDDEFAULT bitmaps buton_about_o.gif]]       
	set image_but2_over [image create photo -file [file join $GIDDEFAULT bitmaps buton_help_o.gif]]       
	set image_but3_over [image create photo -file [file join $GIDDEFAULT bitmaps buton_tutorials_o.gif]]       
	set image_but4_over [image create photo -file [file join $GIDDEFAULT bitmaps buton_web_o.gif]]       
	set image_but5_over [image create photo -file [file join $GIDDEFAULT bitmaps buton_register_o.gif]]       
	set image_but6_over [image create photo -file [file join $GIDDEFAULT bitmaps buton_close_o.gif]]       




	set image_txt1 [image create photo -file [file join $GIDDEFAULT bitmaps text_about.gif]]       
	set image_txt2 [image create photo -file [file join $GIDDEFAULT bitmaps text_gidhelp.gif]]       
	set image_txt3 [image create photo -file [file join $GIDDEFAULT bitmaps text_gidtutorials.gif]]       
	set image_txt4 [image create photo -file [file join $GIDDEFAULT bitmaps text_www.gif]]       
	set image_txt5 [image create photo -file [file join $GIDDEFAULT bitmaps text_register.gif]] 
	set image_txt6 [image create photo -file [file join $GIDDEFAULT bitmaps text_close.gif]] 
	#set image_txt7 [image create photo -file [file join $GIDDEFAULT bitmaps text_customization.gif]] 
	#set image_txt8 [image create photo -file [file join $GIDDEFAULT bitmaps text_whatsnew.gif]] 
	#set image_txt9 [image create photo -file [file join $GIDDEFAULT bitmaps text_faq.gif]] 

	label $base.label_imgtop -image $image_top -bd 0

	label $base.label_about                -image $image_txt1 -bd 0  -bg #EEEEEE
	label $base.label_help                -image $image_txt2 -bd 0  -bg #EEEEEE
	label $base.label_tutorials        -image $image_txt3 -bd 0  -bg #EEEEEE
	label $base.label_web                -image $image_txt4 -bd 0  -bg #EEEEEE
	label $base.label_register        -image $image_txt5 -bd 0  -bg #EEEEEE
	label $base.label_close         -image $image_txt6 -bd 0  -bg #EEEEEE


	
	button $base.button#1 \
		 -relief flat -bd 0 -bg #EEEEEE -highlightthickness 0\
		-image $image_but1  \
		-command {CreateWelcomeWin }
		
	
	menubutton $base.button#2 \
		-relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
		-image $image_but2  \
		-menu $base.button#2.m -relief flat -bd 0 
		
		
	menu $base.button#2.m -tearoff no -bd 1  -bg $GidPriv(ToolbarBg)
	$base.button#2.m add command -label [_ "GiD Help"] \
		         -command {GiDHelpWindow -type REFERENCE -start html-version}
	$base.button#2.m add command  -label [_ "Customization Help"] \
		         -command {GiDHelpWindow -type REFERENCE -start html-customization}
	$base.button#2.m add command -label [_ "FAQ"]  \
		         -command {GiDHelpWindow -type REFERENCE -start html-faq}
	$base.button#2.m add command  -label [_ "What's new"]...  \
		         -command {GiDHelpWindow -type REFERENCE -start html_whatsnew}

	button $base.button#3 \
		-relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
		-image $image_but3 \
		-command {CreateTutorial_win 
		          destroy .gid.info_gid
		         }

	button $base.button#4 \
		-relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
		-image $image_but4  \
		-command "VisitWeb $GidPriv(ProgWeb)"
		

	menubutton $base.button#5 \
		-relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
		-image $image_but5  \
		-menu $base.button#5.m -relief flat -bd 0
	
	menu $base.button#5.m -tearoff no -bd 1  -bg $GidPriv(ToolbarBg)
	$base.button#5.m add command -label [_ "Register GiD"] \
		         -command {gidregister}
	$base.button#5.m add command  -label [_ "Register Module"] \
		         -command {gidregister module}
	
		


	button $base.button#6 \
		-relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
		-image $image_but6  \
		-command {destroy .gid.info_gid }



#        button $base.button_h1 \
#                -relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
#                -image $image_txt7 \
#                -command {destroy .gid.info_gid }
#
#        button $base.button_h2 \
#                -relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
#                -image $image_txt8 \
#                -command {destroy .gid.info_gid }
#
#        button $base.button_h3 \
#                -relief flat -bd 0 -bg #EEEEEE -highlightthickness 0 \
#                -image $image_txt9 \
#                -command {destroy .gid.info_gid }


	#binds - on mouse over 

	bind $base.button#1 <Enter> ".gid.info_gid.button#1 configure -image $image_but1_over"
	bind $base.button#1 <Leave> ".gid.info_gid.button#1 configure -image $image_but1 "

	bind $base.button#2 <Enter> ".gid.info_gid.button#2 configure -image $image_but2_over"
	bind $base.button#2 <Leave> ".gid.info_gid.button#2 configure -image $image_but2 "

	bind $base.button#3 <Enter> ".gid.info_gid.button#3 configure -image $image_but3_over"
	bind $base.button#3 <Leave> ".gid.info_gid.button#3 configure -image $image_but3 "

	bind $base.button#4 <Enter> ".gid.info_gid.button#4 configure -image $image_but4_over"
	bind $base.button#4 <Leave> ".gid.info_gid.button#4 configure -image $image_but4 "

	bind $base.button#5 <Enter> ".gid.info_gid.button#5 configure -image $image_but5_over"
	bind $base.button#5 <Leave> ".gid.info_gid.button#5 configure -image $image_but5 "

	bind $base.button#6 <Enter> ".gid.info_gid.button#6 configure -image $image_but6_over"
	bind $base.button#6 <Leave> ".gid.info_gid.button#6 configure -image $image_but6 "

	# Geometry management



	grid $base.label_imgtop -in $base        -row 1 -column 1  \
		-columnspan 6 -sticky ne
	grid $base.button#1        -in $base        -row 2 -column 2  
	grid $base.label_about        -in $base        -row 2 -column 3 
	grid $base.button#3        -in $base        -row 2 -column 4 -ipady 5
	grid $base.label_tutorials  -in $base        -row 2 -column 5 

	grid $base.button#2        -in $base        -row 3 -column 2 -ipady 5 
	grid $base.label_help        -in $base        -row 3 -column 3 
	
#        grid $base.button_h1 -in $base        -row 5 -column 3 -sticky wn -pady 0
#        grid $base.button_h2 -in $base        -row 6 -column 3 -sticky wn -pady 0
#        grid $base.button_h3 -in $base        -row 7 -column 3 -sticky wn -pady 0

	

	grid $base.button#5        -in $base        -row 3 -column 4 -ipady 5 
	grid $base.label_register        -in $base        -row 3 -column 5  
	grid $base.button#4        -in $base        -row 4 -column 2 -ipady 5
	grid $base.label_web        -in $base        -row 4 -column 3 
	grid $base.button#6        -in $base        -row 4 -column 4 -ipady 5
	grid $base.label_close        -in $base        -row 4 -column 5
	


	# Resize behavior management

	grid rowconfigure $base 1 -weight 1 
	#grid rowconfigure $base 2 -weight 0 -minsize 2
	grid rowconfigure $base 5 -weight 0 -minsize 10

	grid columnconfigure $base 1 -weight 0 -minsize 15
	grid columnconfigure $base 2 -weight 0 -minsize 30
	grid columnconfigure $base 3 -weight 0 -minsize 30
	grid columnconfigure $base 4 -weight 0 -minsize 30
	grid columnconfigure $base 5 -weight 0 -minsize 40
	grid columnconfigure $base 6 -weight 0 -minsize 10
}







# whatisnewViwer is not used any more. see function HelpWindow "CUSTOM_HELP_FILE" #
proc WhatIsNewViewer { { name  "" } } {
    global GIDDEFAULT

    if { $name == "" } {
	set name $GIDDEFAULT/info/WHATISNEW
    }

    set w .gid.__fileviewer
    InitWindow $w [_ "What is new"] WhatIsNewWindowGeom 

    $w config -bg grey93

    set imatge_quit [image create photo -file [file join $GIDDEFAULT bitmaps quit.gif]] 




    text $w.t -relief raised -bd 2 -xscrollcommand "$w.sx set" \
	    -yscrollcommand "$w.sy set"
    scrollbar $w.sy -relief flat -command "$w.t yview"
    scrollbar $w.sx -relief flat -command "$w.t xview" -orient h
    button $w.b -text [_ "Close"] -command "destroy $w" -image $imatge_quit -background grey93 -relief flat

    grid $w.t -row 1 -column 1 -sticky nsew
    grid $w.sy -row 1 -column 2 -sticky ns
    #grid $w.sx -row 2 -column 1 -sticky ew
    grid $w.b -row 3 -column 1 -columnspan 2 -pady 7 -sticky e

    grid columnconf $w 1 -weight 1
    grid rowconf $w 1 -weight 1

    set f [open $name r]
    while { ![eof $f] } {
	$w.t ins end [gets $f]\n
    }
    close $f
    focus $w.t
    $w.t conf -state disabled
}


proc VisitWeb { dir } { 
    global tcl_platform
  
    if { $tcl_platform(platform) == "windows" } { 
      catch {eval exec [auto_execok start] $dir >& NUL: &}
    } else {
	#catch {exec netscape $dir >& /dev/null &} 
      catch {exec netscape $dir &} 
    }
}



