#.============================================================================
#. Copyright 1999 SAS IP, Inc. Proprietary Data.
#. Unauthorized use, distribution, or duplication is prohibited.
#. All Rights Reserved.
#.============================================================================
#. $Revision: 145002 $
#. $Date: 2009-11-13 11:20:27 -0500 (Fri, 13 Nov 2009) $
#.============================================================================
#.File Name:     AnsysStreamlined.eui
#.Author:        Eric P. Clevenger
#.Description:   Procs that are used for ANSYS streamlined specific things
#.============================================================================

# Load the resources for structural loading
::euidl::loadResource AnsysStreamlined.rc
::euidl::loadResource AnsysStreamlined.hlp

# Properties
proc euidl::streamPropertiesInvoke {} {
   global tcl_platform

   variable engineeringDiscipline;  variable engDisciplineWin
   variable statusanalysis;         variable analysisTypes 
   variable analysisTypeWin;        variable fullAnsysKey 
   variable ansysToolbarKey;        variable ansysInputKey
   variable mainwinParent;          variable mainwinGrab
   variable euidlArray;             variable systemCommand
   variable resizeGraphics;         variable printPS
   variable userData

   if [winfo exists $mainwinGrab.streamprop] {
      raise $mainwinGrab.streamprop
      return
   }
   
#   if [string match windows $tcl_platform(platform)] {
#      option add *GeneralProps.entEditorString.value notepad widgetDefault
#      option add *GeneralProps.entCalcString.value calc widgetDefault
#   } else {
#      option add *GeneralProps.entEditorString.value \
#            "xterm -T \"ANSYS editor\" -e vi" widgetDefault
#      option add *GeneralProps.entCalcString.value "xcalc" widgetDefault
#   }

   # Check for a default file
   if [file exists [file join $euidlArray(defaults) AnsysStreamlined.def]] {
         option readfile \
               [file join $euidlArray(defaults) AnsysStreamlined.def]
   }

   set parent \
         [euidl::dialog $mainwinGrab.streamprop \
               -master $mainwinParent -grabwin $mainwinGrab]
   set child [$parent component childsite]

   set child [frame $child.frm1 -class StreamlinedProperties]
   pack $child -side top -fill both -expand TRUE

   # There must be a class to get the title so use the child
   $parent configure \
         -title [option get $child title {}]

   # Tabs
   set tabParent [euidl::tabnotebook $child.tnb]
   #pack $tabParent -padx 3 -pady 3 -fill both -expand 1
   grid $tabParent -column 0 -row 0 -sticky news -columnspan 2
   grid columnconfigure $child 0 -weight 1
   grid columnconfigure $child 1 -weight 1

   # General tab
   set page [$tabParent add]
   $tabParent pageconfigure 0 \
         -label [option get $child page0Label {}]
   
   set generalTab [frame $page.hold -class GeneralProps]
   grid columnconfigure $generalTab 0 -weight 1
   grid columnconfigure $generalTab 1 -weight 4

   # Create the start configuration group box
   frame $generalTab.grpStrtCnfg \
         -borderwidth 2 -relief groove
   grid $generalTab.grpStrtCnfg -column 0 -row 0 -sticky news -pady 10 -padx 5
   frame $generalTab.grpStrtCnfg.hld
   grid $generalTab.grpStrtCnfg.hld -column 0 -row 0 -sticky nw -pady 5 -padx 5
   grid columnconfigure $generalTab.grpStrtCnfg 0 -weight 1
   grid columnconfigure $generalTab.grpStrtCnfg 1 -minsize 10 -weight 1

   label $generalTab.grpStrCnfgTitle -anchor nw
   grid $generalTab.grpStrCnfgTitle -column 0 -row 0 -sticky nw -padx 12

#   checkbutton $generalTab.fullAnsys \
#         -variable [scope fullAnsysKey]
   checkbutton $generalTab.ansToolbar \
         -variable [scope ansysToolbarKey]
   checkbutton $generalTab.ansInput \
         -variable [scope ansysInputKey]
   checkbutton $generalTab.resizeGraphics \
         -variable [scope resizeGraphics]


#   pack $generalTab.fullAnsys -in $generalTab.grpStrtCnfg.hld -side top -anchor nw
   pack $generalTab.ansToolbar -in $generalTab.grpStrtCnfg.hld -side top -anchor nw
   pack $generalTab.ansInput -in $generalTab.grpStrtCnfg.hld -side top -anchor nw
   pack $generalTab.resizeGraphics -in $generalTab.grpStrtCnfg.hld -side top -anchor nw

   # Create the system group box
   frame $generalTab.grpSystem \
         -borderwidth 2 -relief groove
   grid $generalTab.grpSystem -column 1 -row 0 -sticky new -pady 10 -padx 5
   frame $generalTab.grpSystem.hld
   grid $generalTab.grpSystem.hld -column 0 -row 0 -sticky new -pady 5 -padx 5
   grid columnconfigure $generalTab.grpSystem 0 -weight 1
   grid columnconfigure $generalTab.grpSystem 1 -weight 1

   label $generalTab.grpSystemTitle -anchor nw
   grid $generalTab.grpSystemTitle -column 1 -row 0 -sticky nw -padx 12

   label $generalTab.lblCalc
   ::euidl::entry $generalTab.entCalcString \
         -variable [scope systemCommand(calc)]
   set systemCommand(calc) \
         [option get $generalTab.entCalcString value {}]
#   $generalTab.entCalcString insert end $systemCommand(calc)
   label $generalTab.lblEditor
   ::euidl::entry $generalTab.entEditorString \
         -variable [scope systemCommand(editor)]
   set systemCommand(editor) \
         [option get $generalTab.entEditorString value {}]
#   $generalTab.entEditorString insert end $systemCommand(editor)

   pack $generalTab.lblCalc \
         -in $generalTab.grpSystem.hld -side top -anchor nw -expand 1
   pack $generalTab.entCalcString \
         -in $generalTab.grpSystem.hld -side top -anchor nw -fill both -expand 1
   pack $generalTab.lblEditor \
         -in $generalTab.grpSystem.hld -side top -anchor nw -expand 1
   pack $generalTab.entEditorString \
         -in $generalTab.grpSystem.hld -side top -anchor nw -fill both -expand 1

   #
   #if [string match "Windows NT" $tcl_platform(os)] {
   #   checkbutton $generalTab.chkPrintPS \
   #         -variable [scope printPS]
   #   set printPS \
   #         [option get $generalTab.chkPrintPS value {}]
   #   if [string match {} $printPS] {
   #      set printPS 0
   #   }
   #pack $generalTab.chkPrintPS \
   #      -in $generalTab.grpSystem.hld -side top -anchor nw
   #}

   pack $generalTab -padx 4 -pady 1 -anchor nw -fill both

   # User Info tab
   set page [$tabParent add]
   $tabParent pageconfigure 1 \
         -label [option get $child page1Label {}]

   set userTab [frame $page.hold -class UserInfo]
   grid columnconfigure $userTab 0 -minsize 75
   grid columnconfigure $userTab 1 -weight 1

   label $userTab.lblName
   ::euidl::entry $userTab.entName \
         -variable [scope userData(name)]
#   $userTab.entName insert end [option get $userTab.entName value {}]
   
   label $userTab.lblCompany
   ::euidl::entry $userTab.entCompany \
         -variable [scope userData(company)]

#   $userTab.entCompany insert end [option get $userTab.entCompany value {}]
   
   label $userTab.lblAddress
   ::euidl::scrolledtext $userTab.txtAddress \
         -width 30 -height 4 \
         -vscrollmode none \
         -hscrollmode none

#   if ![string match {} [option get $userTab.txtAddress value {}]] {
#      $userTab.txtAddress insert 1.0 \
#            [subst [option get $userTab.txtAddress value {}]]
#   }

   grid $userTab.lblName \
         -column 0 -row 0 -sticky nw -pady 3
   grid $userTab.entName \
         -column 1 -row 0 -sticky new -pady 3
   grid $userTab.lblCompany \
         -column 0 -row 1 -sticky nw -pady 3
   grid $userTab.entCompany \
         -column 1 -row 1 -sticky new -pady 3
   grid $userTab.lblAddress \
         -column 0 -row 2 -sticky nw -pady 3
   grid $userTab.txtAddress \
         -column 1 -row 2 -sticky news -pady 3
   grid rowconfigure $userTab 2 -weight 1

   pack $userTab -padx 4 -pady 1 -anchor nw -fill both -expand 1

   # About tab
   set page [$tabParent add]
   $tabParent pageconfigure 2 \
         -label [option get $child page2Label {}]

   set aboutTab [frame $page.hold -class AboutInfo]

   label $aboutTab.lblName -justify left
   label $aboutTab.lblRelease -justify left
   label $aboutTab.lblCopyright -justify left

   grid $aboutTab.lblName \
      -column 0 -row 0 -sticky nw
   grid $aboutTab.lblRelease \
      -column 0 -row 2 -sticky nw
   grid $aboutTab.lblCopyright \
      -column 0 -row 4 -sticky nw
   grid rowconfigure $aboutTab 1 -minsize 20
   grid rowconfigure $aboutTab 2 -minsize 10

   pack $aboutTab -padx 4 -pady 1 -anchor nw

   $tabParent select 0

   # Define the action buttons
   $parent configure \
          -okcommand \
             [code [list streamProperties_ok \
               $parent $child $generalTab $userTab ]] \
          -cancelcommand [list $parent deactivate 1] \
          -helpcommand [option get $child helpCommand {}]
   ::euidl::registerWindow $parent

   # If we had a default file then pass the data onto ANSYS
#   if [file exists [file join $euidlArray(defaults) AnsysStreamlined.def]] {
#      streamPropertiesToAnsys $parent $child $generalTab $userTab
#   }
      streamPropertiesToAnsys $parent $child $generalTab $userTab
}

proc euidl::streamProperties {} {
   variable mainwinParent;    variable mainwinGrab
   set parent $mainwinGrab.streamprop
   debug::track streamProp 100
   if [winfo exists $parent] {
      $parent activate
   } else {
      euidl::streamPropertiesInvoke
      $parent activate
   }
}

proc euidl::streamProperties_ok { parent child generalTab userTab } {

   global ansysRevDot

   variable euidlArray;       variable materialWin 
   variable unitSystemANSYS

   variable systemCommand
   variable ansysToolbarKey
   variable ansysInputKey
   variable resizeGraphics

   variable ::euidl::mechtool::modelTab

#   ::euidl::writeDefaults GeneralProps $generalTab w AnsysStreamlined
#   ::euidl::writeDefaults UserInfo $userTab a AnsysStreamlined

   # Save to the registry the values
   ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
               ansysToolbar "$ansysToolbarKey"

   ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
               ansysInput "$ansysInputKey"

   ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
               resizeGraphics "$resizeGraphics"

   ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
               systemCalc "$systemCommand(calc)"
   ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
               systemEditor "$systemCommand(editor)"

    ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\Reports" \
               userName [$userTab.entName get]
    ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\Reports" \
               userCompany [$userTab.entCompany get]
    ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\Reports" \
               userAddress [$userTab.txtAddress get 1.0 end-1c]


   $parent deactivate 1

   streamPropertiesToAnsys $parent $child $generalTab $userTab

}

proc euidl::streamPropertiesToAnsys { parent child generalTab userTab } {
   global ansysRevDot
   global tcl_platform

   variable systemCommand
   variable ansysToolbarKey
   variable ansysInputKey
   variable userData

#   set fullAnsysKey [option get $generalTab.fullAnsys value {}]
#   if [string match {} $fullAnsysKey] {
#      set fullAnsysKey 0
#   }
   if {[catch {ansys::registry get \
         "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
         ansysToolbar} lVar(ansysToolbarKey)]} { 
      set ansysToolbarKey 0
   } else {
      set ansysToolbarKey $lVar(ansysToolbarKey)
   }

   if {[catch {ansys::registry get \
         "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
         ansysInput} lVar(ansysInputKey)]} { 
      set ansysInputKey 0
   } else {
      set ansysInputKey $lVar(ansysInputKey)
   }

   if {[catch {ansys::registry get \
         "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
         resizeGraphics} lVar(resizeGraphics)]} { 
      set resizeGraphics 0
   } else {
      set resizeGraphics $lVar(resizeGraphics)
   }

   if {[catch {ansys::registry get \
         "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\Reports" \
         userName} lVar(userName)]} { 
      set userName ""
   } else {
      set userData(name) $lVar(userName)
   }

   if {[catch {ansys::registry get \
         "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\Reports" \
         userCompany} lVar(userCompany)]} { 
      set userCompany ""
   } else {
      set userData(company) $lVar(userCompany)
   }

   if {[catch {ansys::registry get \
         "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\Reports" \
         userAddress} lVar(userAddress)]} { 
      set userData(address) ""
   } else {
      set userData(address) $lVar(userAddress)
   }

   $userTab.txtAddress delete 1.0 end
   $userTab.txtAddress insert end $userData(address)

   passStringToAnsys $userData(name) _USRNAME 64 1 1 1 1 0
   passStringToAnsys $userData(company) _USRCOMP 64 1 1 1 1 0

   set i 1
   foreach line $userData(address) {
      passStringToAnsys $line _USRADDR 64 1 $i 1 1 0
      incr i
      if {$i > 5} {
         break
      }
   }

   if [string match windows $tcl_platform(platform)] {
      if {[catch {ansys::registry get \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemCalc} lVar(systemCalc)]} { 
         ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemCalc calc
         set systemCommand(calc) calc
      } else {
         set systemCommand(calc) $lVar(systemCalc)
      }
      if {[catch {ansys::registry get \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemEditor} lVar(systemEditor)]} { 
         ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemEditor notepad
         set systemCommand(editor) notepad
      } else {
         set systemCommand(editor) $lVar(systemEditor)
      }
   } else {
      if {[catch {ansys::registry get \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemCalc} lVar(systemCalc)]} { 
         ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemCalc xcalc
         set systemCommand(calc) xcalc
      } else {
         set systemCommand(calc) $lVar(systemCalc)
      }
      if {[catch {ansys::registry get \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemEditor} lVar(systemEditor)]} { 
         ansys::registry set \
            "HKEY_CURRENT_USER\\Software\\ANSYS, Inc.\\ANSYS\\ANSYS $ansysRevDot\\GUI\\MTB" \
            systemEditor "xterm -T \"ANSYS editor\" -e vi"
         set systemCommand(editor) "xterm -T \"ANSYS editor\" -e vi"
      } else {
         set systemCommand(editor) $lVar(systemEditor)
      }
   }

}
