#.============================================================================
#. Copyright $Date: 2009-11-13 11:20:27 -0500 (Fri, 13 Nov 2009) $ SAS IP, Inc. Proprietary Data.
#. Unauthorized use, distribution, or duplication is prohibited.
#. All Rights Reserved.
#.============================================================================
#. $Revision: 145002 $
#.============================================================================

namespace eval ansys::RealConstants {

   variable helpfile
   variable rc_next
   variable displaylist
   variable edisplaylist
   variable selected
   variable eselected
   variable realwin
   variable num_elements
   variable etypes
   variable rcon_ref_vect
   variable etyp_ref_vect
   variable etyp_stf_vect
   variable func

   proc RConState { state } {
      if {[winfo exists .realconst]} {
         switch -- $state {
            normal {
               # Allow the dialog to be closed
               wm protocol .realconst WM_DELETE_WINDOW [code CloseDialog]
            }
            disabled {
               # Don't allow the dialog to be closed
               wm protocol .realconst WM_DELETE_WINDOW { }
            }
         }

         foreach child [winfo child .realconst] {
            catch {$child configure -state $state} err
         }
      }
   }

   proc RConInit { callRCon } {
      # Can't see that we ever go through here but will leave it in
      if {$callRCon == 0} {
         RCon
      }
   }

   proc RConCB { {uidlButton 1} } {
      variable uidlCall
      variable rconArr

      if {![winfo exists .realconst]} {
         return
      }
      switch -- $uidlButton {
        0 -
        1 {
             RConState normal
#             RCon
             if {![string equal -nocase [string trim [lindex $::ansys::RealConstants::displaylist 0]] "NONE DEFINED"]} {
                set realwinchild [.realconst.frame childsite]
                $realwinchild.edit configure -state normal
                $realwinchild.delete configure -state normal
             }
          }
        2 { # Apply
#            RCon
            RConState normal
            if {![string equal -nocase [string trim [lindex $::ansys::RealConstants::displaylist 0]] "NONE DEFINED"]} {
               set realwinchild [.realconst.frame childsite]
               $realwinchild.edit configure -state normal
               $realwinchild.delete configure -state normal
            }
            RConState disabled
      	   #CustFrame 
            if {[string compare $::tcl_platform(platform) unix] == 0} {
               uidl::callFnc $uidlCall [code ansys::RealConstants::RConCB] 0 1
            } else {
               uidl::callFnc uidlRegister [code ansys::RealConstants::RConCB] 0 1
            }

          }
      }
   }

   proc RCon {} {
      global tcl_platform

      variable helpfile
      variable displaylist
      variable edisplaylist
      variable selected
      variable eselected
      variable realwin
      variable etypes
      variable selection

   
      after 250 ::euidl::graphicPicking::finishPicking
      set class Ansys
      if {![winfo exists .realconst]} {
         ans_sendcommand Fnc_RemoveDialogs
         set selection -2
         if {[ans_getvalue common,,mccom,,int,16]} {
            set class Ansys
            set realwin [toplevel .realconst -class Ansys]
            wm geometry $realwin +40+100
          } else {
            set class EUIDL
            set realwin [toplevel .realconst -class EUIDL]
            wm transient $realwin $AnsysGUI::ansGUIArray(parent)
            set geom [split [wm geometry $AnsysGUI::ansGUIArray(parent)] +-]
            set x [expr [lindex $geom 1] + 40]
            set y [expr [lindex $geom 2] + 100]
            if {$x > 0} { set x "+$x"}
            if {$y > 0} { set y "+$y"}
            wm geometry $realwin "$x$y"
         }
         wm title $realwin [msgcat::mc "Real Constants"]

         wm protocol $realwin WM_DELETE_WINDOW [code CloseDialog]
   
         euidl::labeledframe $realwin.frame \
               -text [msgcat::mc "Defined Real Constant Sets"] \
               -relief flat

         set realwinchild [$realwin.frame childsite]
         euidl::scrolledlistbox $realwinchild.list -exportselection false \
               -height 250 -selectmode single
         grid $realwinchild.list -sticky news 
         euidl::button $realwinchild.add -text [msgcat::mc "Add..."] \
               -width 8 \
               -command {
#         grab release .realwin
                  ::ansys::RealConstants::RealConstantsFunc RC_ADD
               }

         euidl::button $realwinchild.edit -text [msgcat::mc "Edit..."] \
               -width 8 \
               -command {
                     ::ansys::RealConstants::RealConstantsFunc RC_EDIT
               }

         euidl::button $realwinchild.delete -text [msgcat::mc "Delete"] \
               -width 8 \
               -command "::ansys::RealConstants::RealConstantsFunc RC_DELETE"

         grid $realwinchild.add $realwinchild.edit $realwinchild.delete \
               -padx 2 -pady 10

         euidl::button $realwin.close \
               -width 8 \
               -text [msgcat::mc Close] \
               -command { ::ansys::RealConstants::CloseDialog }
   
         euidl::button $realwin.help \
               -width 8 \
               -text [msgcat::mc Help] \
               -command "ans_loadhelp $ansys::RealConstants::helpfile"
      
   
         grid $realwinchild.list -columnspan 3
         grid $realwin.frame -columnspan 2 -sticky news
         grid $realwin.close $realwin.help -pady 10
         # Still need to grab focus
         # Creating secondary window (do not display)
         if {![winfo exists .etypes]} {
            update
            set etypes [toplevel .etypes -class $class]
            wm transient $etypes $realwin
            wm geometry $etypes \
                  [string trimleft [wm geometry $realwin] 0123456789xX]
            wm withdraw $etypes
            wm title $etypes [msgcat::mc "Element Type for Real Constants"]
            button $etypes.ok -text [msgcat::mc OK] -width 8 -command {
#                  grab release $ansys::RealConstants::etypes
                  ansys::RealConstants::RealConstantsEFunc $ansys::RealConstants::func
                  wm withdraw $ansys::RealConstants::etypes
            }
            button $etypes.cancel -text [msgcat::mc Cancel] -width 8 -command {
#                  grab release $ansys::RealConstants::etypes
                  wm withdraw $ansys::RealConstants::etypes
            }
            euidl::labeledframe $etypes.frame \
                  -text [msgcat::mc "Choose element type:"] \
                  -relief flat
            set etypeschild [$etypes.frame childsite]
            euidl::scrolledlistbox $etypeschild.list -exportselection false \
                  -selectmode single
            grid $etypeschild.list -sticky news
            grid $etypes.frame -sticky news -columnspan 2
            grid $etypes.ok $etypes.cancel -padx 2 -pady 10
            grid rowconfigure $etypes 0 -weight 1
            grid rowconfigure $etypeschild 0 -weight 1
            grid columnconfigure $etypes 0 -weight 1
            grid columnconfigure $etypes 1 -weight 1
            grid columnconfigure $etypeschild 0 -weight 1
         }
         update
#         focus $realwin
#         catch {grab $realwin}
         ans_greygui 0 0
         ans_greygui 1 0
         ans_greygui 2 0
         ans_greygui 3 0
         ans_greygui 4 0
      } else {
         # Set up the environment
         set realwin .realconst
         set realwinchild [$realwin.frame childsite]
         if {![winfo ismapped $realwin]} { wm deiconify $realwin }
         # It may be that this is being called again from the last
         # in a chain of dialogs
         RConCB 0
      }
      set etypes .etypes
      set etypeschild [$etypes.frame childsite]
   
      if {[catch {llength $displaylist} result]} {
         # The "displaylist" variable is unset, assume no list
         set displaylist [list "None Defined"]
      }
      if {$result == 0} {
         # The "displaylist" variable is empty
         set displaylist [list "None Defined"]
      }
   
      # Populate the listbox
      $realwinchild.list delete 0 end
      set listData [list]
      foreach item $displaylist {
         lappend listData  [msgcat::mc $item] 0
      }
      $realwinchild.list insert end $listData

      # Activate the correct item
      if {($selected == -2) || ([string equal -nocase [string trim [lindex $displaylist 0]] "NONE DEFINED"])} {
         $realwinchild.edit configure -state disabled
         $realwinchild.delete configure -state disabled
         set selected 0
      } else {
         $realwinchild.edit configure -state normal
         $realwinchild.delete configure -state normal
      }
      if {$selection == -2} {
         set selection $selected
      } else {
         set selection [$realwinchild.list index active]
      }
      if {$selection < 0} {set selection 0}
      $realwinchild.list activate $selection
      $realwinchild.list selection set $selection

# Need to populate the element types list
      if {[catch {llength $edisplaylist} result]} {
         # The "edisplaylist" variable is unset, assume no list
         set edisplaylist [list "None Defined"]
      }
      if {$result == 0} {
         # The "edisplaylist" variable is empty
         set edisplaylist [list "None Defined"]
      }
   
      # Populate the listbox
      $etypeschild.list delete 0 end
      set listData [list]
      foreach item $edisplaylist {
         lappend listData  [msgcat::mc $item] 0
      }
      $etypeschild.list insert end $listData


      # Activate the correct item
      set eselection $eselected 
      if {$eselection >= [$etypeschild.list index end]} { 
         set eselection [expr [$etypeschild.list index end] - 1]
      }
      if {$eselection < 0} {set eselection 0}
      $etypeschild.list activate $eselection
      $etypeschild.list selection set $eselection

      grid rowconfigure $realwin 0 -weight 1
      grid rowconfigure $realwinchild 0 -weight 1
      grid columnconfigure $realwin 0 -weight 1
      grid columnconfigure $realwin 1 -weight 1
      grid columnconfigure $realwinchild 0 -weight 1
      grid columnconfigure $realwinchild 1 -weight 1
      grid columnconfigure $realwinchild 2 -weight 1

# No longer seem to need this at 8.1
#      if {[string match unix $tcl_platform(platform)]} {
#         ans_sendcommand Fnc_RemoveDialogs
#      }
   }


   proc CloseDialog {} {
      
      # Need to release focus and destroy widget
      destroy .realconst
      ans_greygui 0 1
      ans_greygui 1 1
      ans_greygui 2 1
      ans_greygui 3 1
      ans_greygui 4 1
      ::apdl::noprint 1
      catch { ans_sendcommand )_BUTTON=1 }
      catch { ans_sendcommand )KEYW,CUWIDGET,0 }
      ::apdl::noprint 0
      if {[ans_getvalue common,,mccom,,int,16] == 0} {
         after 250 [list catch {raise $::AnsysGUI::ansGUIArray(parent)}]
         after 300 [list catch {::AnsysGUI::keybFocus}]
      }
      after 250 ::euidl::graphicPicking::activatePicking

   }


   proc RealConstantsFunc {command} {
      variable num_elements
      variable realwin
      variable rc_next
      variable rcon_ref_vect
      variable func
      variable etypes
 
      switch -- $command {
         RC_ADD {
               if {$num_elements > 0} {
                  set func RC_ADD
                  wm geometry $etypes [wm geometry $realwin]
                  wm deiconify $etypes
#                  catch {grab $etypes}
                } else {
                  ::apdl::noprint 1
                  CallCommand ")KEYW,RC_ADD,1"
                  CallCommand ")KEYW,RC_EDIT,0"
                  CallCommand ")_RC_SET = $rc_next"
                  ::apdl::noprint 0
                  CallCommand "Fnc_rcon_0"
               }
            }
         RC_EDIT {
            if {$num_elements > 0} {
               set func RC_EDIT
               wm geometry $etypes [wm geometry $realwin]
               wm deiconify $etypes
#               catch {grab $etypes}
            } else {
               set realwinchild [$realwin.frame childsite]
               append realwinchild ".list"
               ::apdl::noprint 1
               CallCommand ")KEYW,RC_ADD,0"
               CallCommand ")KEYW,RC_EDIT,1"
               set pos $rcon_ref_vect([$realwinchild index active])
               CallCommand ")_RC_SET = $pos"
               ::apdl::noprint 0
               CallCommand "Fnc_rcon_0"
            }
         }
         RC_DELETE {
            set realwinchild [$realwin.frame childsite]
            append realwinchild ".list"
            set pos $rcon_ref_vect([$realwinchild index active])
            CallCommand "RDEL,$pos"
            ::apdl::noprint 1
#            CallCommand "Fnc_R"
            catch {ans_sendcommand )RCON}
            catch {ans_sendcommand )STAT,,, ,,, ,,1}
            ::apdl::noprint 0
         }
      }
   }


   proc RealConstantsEFunc {command} {
      variable etypes
      variable realwin
      variable rc_next
      variable etyp_ref_vect
      variable etyp_stf_vect
      variable rcon_ref_vect

      ::ansys::RealConstants::RConState disabled
      set etypechild [$etypes.frame childsite]
      append etypechild ".list"
      set realwinchild [$realwin.frame childsite]
      append realwinchild ".list"
      switch -- $command {
         RC_ADD {
               set epos1 $etyp_ref_vect([$etypechild index active])
               set epos2 $etyp_stf_vect([$etypechild index active])
               ::apdl::noprint 1
               CallCommand ")KEYW,RC_ADD,1"
               CallCommand ")KEYW,RC_EDIT,0"
               CallCommand ")_RC_SET = $rc_next"
               CallCommand ")_ET_TYPE = $epos1"
               ::apdl::noprint 0
               CallCommand "Fnc_rcon_$epos2"
            }
         RC_EDIT {
               set pos $rcon_ref_vect([$realwinchild index active])
               set epos $etyp_ref_vect([$etypechild index active])
               set epos2 $etyp_stf_vect([$etypechild index active])
               ::apdl::noprint 1
               CallCommand ")KEYW,RC_ADD,0"
               CallCommand ")KEYW,RC_EDIT,1"
               CallCommand ")_RC_SET = $pos"
               CallCommand ")_ET_TYPE = $epos"
               ::apdl::noprint 0
               CallCommand "Fnc_rcon_$epos2"
            }
      }
   }

   proc CallCommand { command } {
      variable uidlCall

      set uidlCall $command
      if {[string match -nocase FNC_ [string range $command 0 3]]} {
         ::uidl::callFnc $command [code ansys::RealConstants::RConCB] 0 1
       } else {
         catch {ans_sendcommand $command}
      }

   }


}
