proc make_win_filter { {w .gid.filter_win} } {
  global SelectionPriv
  if {[winfo exists $w]} {     
    trace vdelete SelectionPriv(from) w change_entry_from_or_to
    trace vdelete SelectionPriv(to) w change_entry_from_or_to
    trace vdelete SelectionPriv(entry_value) w change_entry_value
    trace vdelete SelectionPriv(var_filter_menu) w change_filter_menu
    trace vdelete SelectionPriv(var_mode_menu) w change_mode_menu
    trace vdelete SelectionPriv(mode) w control_all_from
    destroy $w 
  } 

  if {![info exists SelectionPriv(status)]} {
    set entity_type "NONE"
  } else { set entity_type $SelectionPriv(status) }

  InitWindow $w [_ "Selection Window"] GiDFilterWin "FilterWindow"
  wm minsize $w 230 210
  #wm maxsize $w 230 2000
  #wm geometry $w 230x210

  set def_back [$w cget -background]

  frame $w.frame#1 -bg [CCColorActivo $def_back] -width 300
  
  set lo [string tolower $entity_type]
  radiobutton $w.radiobutton_all -text [_ "Select all %s" $lo] \
	  -variable SelectionPriv(mode) -value "ALL" 
  if {$entity_type=="NONE"} {$w.radiobutton_all configure -text [_ "Select all"]}
  radiobutton $w.radiobutton_from -text [_ "From"]: \
	  -variable SelectionPriv(mode) -value "FROM"

  $w.radiobutton_all select


  entry $w.entry_from -textvariable SelectionPriv(from) -width 4
  label $w.label_to -text [_ "To"]:
  entry $w.entry_to -textvariable SelectionPriv(to) -width 4 
  button $w.clear_sel -text [_ "Clear"] -width 6 -command "WarnClearSelection $w"
  tk_optionMenuRAM $w.menu_mode SelectionPriv(var_mode_menu) \
	  [list [_ "Swap"] [_ "Add"] [_ "Remove"]] \
	  {SWAP ADD REMOVE}


  label $w.label_mode -text [_ "Mode"]: -disabledforeground grey60
  label $w.label#2 -text [_ "Filter"]:
  label $w.label_value -text [_ "Value"]: -disabledforeground grey60
  label $w.label_type -text [_ "Type"]: -disabledforeground grey60
  entry $w.entry_value -textvariable SelectionPriv(entry_value) -width 5   


  #$w.label_value conf -state disabled
  #$w.SelectionPriv(entry_value) conf -state disabled
  create_filters_menu $w $entity_type
  
  button $w.frame#1.ok -text [_ "Apply"] -width 6 -command "OK_Select $w"
  button $w.frame#1.cancel -text [_ "Close"] -width 6 -command "destroy $w"

  # Geometry management

  grid $w.radiobutton_all -row 1 -column 2 -columnspan 4 -sticky w
  grid $w.radiobutton_from -row 2 -column 2 -sticky w
  grid $w.entry_from -row 2 -column 3 -sticky we
  grid $w.label_to -row 2 -column 5 -sticky w
  grid $w.entry_to -row 2 -column 6 -sticky we

  grid $w.menu_mode -row 4 -column 3 -pady 2 -columnspan 4 -sticky w
  grid $w.clear_sel -row 4 -column 6 -sticky ew -ipady 2
  grid $w.label_mode -row 4 -column 2 
  grid $w.label#2 -row 5 -column 2 
	  

  grid $w.frame#1.ok -row 1 -column 1 -padx 5 -pady 6
  grid $w.frame#1.cancel -row 1 -column 2 -padx 5 -pady 6
  grid $w.frame#1 -row 7 -column 1 -sticky ews -columnspan 7 

  # Resize behavior management

  grid rowconfigure $w 1 -weight 0 -minsize 30
  grid rowconfigure $w 2 -weight 0 -minsize 30
  grid rowconfigure $w 3 -weight 0 -minsize 5
  grid rowconfigure $w 4 -weight 0 -minsize 1
  grid rowconfigure $w 5 -weight 0 -minsize 1
  grid rowconfigure $w 6 -weight 0 -minsize 1
  grid rowconfigure $w 7 -weight 2 -minsize 1
 

  grid columnconfigure $w 1 -weight 1 -minsize 3
  grid columnconfigure $w 2 -weight 0 -minsize 3
  grid columnconfigure $w 3 -weight 0 -minsize 50
  grid columnconfigure $w 4 -weight 0 -minsize 25
  grid columnconfigure $w 5 -weight 0 -minsize 1
  grid columnconfigure $w 6 -weight 0 -minsize 55
  grid columnconfigure $w 7 -weight 1 -minsize 10

  #grid rowconfigure $w.frame#1 1 -weight 0
  #grid columnconfigure $w.frame#1 1 -weight 0

    
  trace var SelectionPriv(from) w "change_entry_from_or_to $w ;#"
  trace var SelectionPriv(to) w "change_entry_from_or_to $w ;#"
  trace var SelectionPriv(entry_value) w "change_entry_value $w ;#"
  trace var SelectionPriv(var_filter_menu) w "change_filter_menu $w ;#"
  trace var SelectionPriv(var_mode_menu) w "change_mode_menu $w ;#"
  trace var SelectionPriv(mode) w "control_all_from $w ;#"
  
  bind $w <Return> "OK_Select $w"
  #bind $w <Escape> "destroy $w"

  if {$entity_type=="NONE" || $entity_type=="ALL"} {
    disable_filter_win $w $entity_type
  } else {
    enable_filter_win $w $entity_type
  }
}


proc create_filters_menu {w entity_type} {
global SelectionPriv
  
  destroy $w.menu_filters
  destroy $w.menu_type

  if {$entity_type=="POINTS"} {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"] [_ "Higher entities"]] \
	      {NONE HIGHERENTITY}
      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) a a
  } elseif {$entity_type=="LINES"} {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"] [_ "Higher entities"] [_ "Min. length"] [_ "Max. length"] [_ "Entity type"]] \
	      {NONE HIGHERENTITY MINLENGTH MAXLENGTH ENTITYTYPE}
      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) \
	      [list [_ "StLine"] [_ "ArcLine"] [_ "PolyLine"] [_ "NurbLine"]] \
	      {STLINE ARCLINE POLYLINE NURBLINE}
  } elseif {$entity_type=="SURFACES"} {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"] [_ "Higher entities"] [_ "Entity type"]] \
	      {NONE HIGHERENTITY ENTITYTYPE}
      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) \
	      [list [_ "PLSurface"] [_ "CoonSurface"] [_ "ContactSurface"] [_ "MeshSurface"] \
	      [_ "NurbSurface"] [_ "PolySurface"]] \
	      {PLSURFACE COONSURFACE CONTACTSURFACE MESHSURFACE NURBSURFACE POLYSURFACE}
  } elseif {$entity_type=="VOLUMES"} {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"] [_ "Entity type"]] \
	      {NONE ENTITYTYPE}
      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) \
	      [list [_ "Volume"]  [_ "ContactVolume"]] \
	      {VOLUME CONTACTVOLUME}
  } elseif {$entity_type=="ALL"} {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"]] \
	      {NONE}
      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) a a
  } elseif {$entity_type=="ELEMENTS"} {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"] [_ "Bad min angle"] [_ "Bad max angle"] [_ "Shape quality"] [_ "Entity type"]] \
	      {NONE BADMINANGLE BADMAXANGLE SHAPEQUALITY ELEMENTTYPE}

      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) \
	      [list [_ "Linear"]  [_ "Triangle"] [_ "Quadrilateral"] \
	      [_ "Tetrahedra"] [_ "Hexahedra"] [_ "Prisma"] [_ "OnlyPoints"]] \
	      {LINEAR TRIANGLE QUADRILATERAL TETRAHEDRA HEXAHEDRA PRISMA ONLYPOINTS}
  } elseif {$entity_type=="NODES"} {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"]] \
	      {NONE}
      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) a a
  } else {
      tk_optionMenuRAM $w.menu_filters SelectionPriv(var_filter_menu) \
	      [list [_ "None"]] \
	      {NONE}
      tk_optionMenuRAM $w.menu_type SelectionPriv(var_type_menu) a a
  }

  grid $w.menu_filters -row 5 -column 3 -columnspan 4 -sticky ew -pady 2
  if  {$entity_type=="ALL"} {
      $w.menu_filters configure -state disabled
  }
      
  set SelectionPriv(var_filter_menu) "NONE"

}

proc control_all_from {w} {
global SelectionPriv

  if {$SelectionPriv(mode) == "ALL"} {focus .gid.filter_win}

}

proc contr_entry {t_string t_entry} {
  if {[regexp {.*([a-z]+).*} $t_string] || [regexp {.*([A-Z]+).*} $t_string]} {
    set last [expr [string length $t_string]-1] 
    $t_entry delete $last
  }
}

proc change_entry_from_or_to {w} {
  global SelectionPriv
  $w.radiobutton_from select
  contr_entry $SelectionPriv(from) $w.entry_from
  contr_entry $SelectionPriv(to) $w.entry_to
}

proc change_entry_value {w} {
    global SelectionPriv
    contr_entry $SelectionPriv(entry_value) $w.entry_value
}


proc change_filter_menu {w} {
    global SelectionPriv
    
    if { $SelectionPriv(var_filter_menu) != "NONE" } {
	if { $SelectionPriv(var_filter_menu) == "ENTITYTYPE" || \
		$SelectionPriv(var_filter_menu) == "ELEMENTTYPE" } { 
	    grid forget $w.entry_value
	    grid forget $w.label_value 
	    grid $w.menu_type -row 6 -column 3 -sticky w -columnspan 5 -pady 2
	    grid $w.label_type -row 6 -column 2 -pady 4
	} else {
	    grid forget $w.menu_type 
	    grid forget $w.label_type 
	    grid $w.entry_value -row 6 -column 3 -sticky w -columnspan 4 -pady 2
	    grid $w.label_value -row 6 -column 2  -pady 8
	}
    } else {
	grid forget $w.menu_type 
	grid forget $w.label_type 
	grid forget $w.entry_value 
	grid forget $w.label_value 
    }
}

proc change_mode_menu {w} {
  global SelectionPriv
  if { $SelectionPriv(var_mode_menu) == "SWAP" } {
    .central.s process SwapSelection
  } elseif { $SelectionPriv(var_mode_menu) == "ADD" } {
	.central.s process AddToSelection
  } elseif { $SelectionPriv(var_mode_menu) == "REMOVE"} {
	.central.s process RemoveFromSel
  }  
}


proc OK_Select {w} {
  global SelectionPriv  

  .central.s process filter:

  if { $SelectionPriv(var_filter_menu) == "ENTITYTYPE" } {
     .central.s process filter:ENTITYTYPE=$SelectionPriv(var_type_menu)
  } elseif { $SelectionPriv(var_filter_menu) == "HIGHERENTITY" } {
    .central.s process filter:HIGHERENTITY=$SelectionPriv(entry_value)
  } elseif { $SelectionPriv(var_filter_menu) == "MINLENGTH" } {
     .central.s process filter:MINLENGTH=$SelectionPriv(entry_value)
  } elseif { $SelectionPriv(var_filter_menu) == "MAXLENGTH" } {
     .central.s process filter:MAXLENGTH=$SelectionPriv(entry_value)
  } elseif { $SelectionPriv(var_filter_menu) == "BADMINANGLE" } {
     .central.s process filter:BADMINANGLE=$SelectionPriv(entry_value)
  } elseif { $SelectionPriv(var_filter_menu) == "BADMAXANGLE" } {
     .central.s process filter:BADMAXANGLE=$SelectionPriv(entry_value)
  } elseif { $SelectionPriv(var_filter_menu) == "SHAPEQUALITY" } {
     .central.s process filter:MINQUALITY=$SelectionPriv(entry_value)
  } elseif { $SelectionPriv(var_filter_menu) == "ELEMENTTYPE" } {
      .central.s process filter:ELEMENTTYPE=$SelectionPriv(var_type_menu)
  }
  

  #tkMessageBox -message "filter:$comm=$SelectionPriv(entry_value)"

  if { $SelectionPriv(mode) == "ALL" } {
	.central.s process 1:
  } elseif { $SelectionPriv(mode) == "FROM" } {
	.central.s process $SelectionPriv(from):$SelectionPriv(to)
  }
}




proc disable_filter_win {w entity_type} {
  global SelectionPriv


  $w.frame#1.ok configure -state disabled
  $w.entry_to configure -state disabled
  $w.entry_from configure -state disabled
  $w.radiobutton_all configure -state disabled
  $w.radiobutton_from configure -state disabled
  $w.menu_filters configure -state disabled
  $w.label_to configure -state disabled
  $w.label#2 configure -state disabled
  $w.menu_type configure -state disabled
  $w.clear_sel configure -state disabled
  $w.menu_mode configure -state disabled
  $w.label_mode configure -state disabled
  $w.entry_value configure -state disabled
  $w.label_value configure -state disabled
  $w.label_type configure -state disabled

  
  $w.radiobutton_all configure -text [_ "Select all"]
  bind $w <Return> ""
  set SelectionPriv(enabled) "NO"
}


proc enable_filter_win {w entity_type} {
  global SelectionPriv

  $w.radiobutton_all configure -text [_ "Select all %s" [string tolower $entity_type]]
  
  $w.frame#1.ok configure -state normal 
  $w.entry_to configure -state normal
  $w.entry_from configure -state normal
  $w.radiobutton_all configure -state normal
  $w.radiobutton_from configure -state normal
  $w.label_mode configure -state normal
  $w.menu_mode configure -state normal
  
  
  grid forget $w.menu_filters
  grid forget $w.menu_type
  create_filters_menu $w $entity_type



  $w.menu_filters configure -state normal
  $w.label_to configure -state normal
  $w.label#2 configure -state normal
  $w.menu_type configure -state normal
  $w.clear_sel configure -state normal
  

  $w.entry_value configure -state normal
  $w.label_value configure -state normal
  $w.label_type configure -state normal

  bind $w <Return> "OK_Select $w"
  set SelectionPriv(enabled) "YES"
}



proc SetSelectionEnable { status OpenWindow } {  
  global SelectionPriv

  set SelectionPriv(status) $status
  set w .gid.filter_win
  if { ![winfo exists $w] && !$OpenWindow } return 
  
  if { ![winfo exists $w] } {
    make_win_filter $w
  } else {
    if { $status == "NONE" || $status == "ALL" } {
      disable_filter_win $w $status
    } else {    
      if { $SelectionPriv(enabled) == "NO" } { enable_filter_win $w $status }
    }
  }
}


proc WarnClearSelection {w} {
    set ret [tk_dialogRAM $w.dialog [_ "Warning"] \
		 [_ "This will clear your current selection!"] \
		 "" 1 [_ "Ok"] [_ "Cancel"]]

  if {$ret ==0} {
    .central.s process ClearSelection
  }
}



