' Gambas class file

' Examples:
' Beispiele:

' a)  Functions in a Cartesian coordinate system
     'Mehrere Funktionen in einem kartesischen Koordinatensystem
' b)  3D (grid)
     'Körperdarstellung (Gitter-Modell)
' c)  Figure in polar coordinates
     'Abbildung in Polarkoordinaten
' d)  Regression curve (FIT) with data from an external text file (*. dat or *. csv)
     'Regressionskurve (FIT) mit Daten aus einer externen Textdatei (*.dat oder *.csv)
     
' As of gnuplot version> 4.4, there are also animated graphs (with iterations FOR control structure)!
' Ab GnuPlot-Version > 4.4 gibt es auch animierte Graphen (Iterationen mit FOR-Kontrollstruktur) !

' The launch window of GnuPlot has dimensions 640x480 pixels.
' Das Startfenster von GnuPlot hat die Maße 640x480 Pixel; ein typisches Bildmaß.

Public sScriptFilePath As String

Public hGnuPlot As Process ' hGnuPlot ist eine Prozess-Variable für das SHELL-Kommando

Public Sub Form_Open()
  
  FMain.Center
  FMain.Border = Border.Plain
  FMain.Resizable = False
  
  txaPlotCode.Text = File.Load(Application.Path &/ "Example/start.plot") ' Voreinstellung
  sScriptFilePath = User.Home &/ "GNUPLOT_S" ' Programm-Sammlung für GnuPlot
  If Not Exist(sScriptFilePath) Then Mkdir sScriptFilePath
  txaPlotCode.Height = 280
  txaErrorAndFit.Visible = False
  btnRedrawGraph.Enabled = False
  btnScriptFileSave.Enabled = False
  btnScriptFileOpen.Enabled = False
  btnGnuPlotStop.Enabled = False
  btnResultSave.Visible = False
End ' Form_Open

Public Sub GnuPlotStart()
  Dim hWhich As Process
  Dim sShellCommand As String
    
  hWhich = Exec ["which", "gnuplot"] Wait For Read
  If hWhich.Value = 1 Then ' 1 = Gnuplot ist NICHT installiert
     If Message.Error("Das Programm GNUPLOT ist nicht installiert." & gb.NewLine & "GNUPLOT jetzt installieren?", "Ja", "Nein!") = 1 Then
        If MTerminal.TerminalInstalled().Count > 0 Then
           sShellCommand = "echo 'Installation GNUPLOT'; echo '---------------------------'; sudo apt-get install gnuplot"
           MTerminal.GoToTerminal(MTerminal.TerminalInstalled()[0], sShellCommand)
           Return
        Else
           Message.Info("Es wurde kein Terminal-Programm gefunden!")
           Return
        Endif ' MTerminal.TerminalInstalled().Count > 0
     Else
        Return
     Endif ' Message.Error(...) = 1?
  Endif ' hWhich.Value = 1?
  
  GnuPlotStop
  hGnuPlot = Shell "gnuplot" For Read Write As "hGnuplot"
  
  If Not IsNull(hGnuPlot) Then
     If hGnuPlot.State = hGnuPlot.Running Then
        Print #hGnuPlot, txaPlotCode.Text
        btnGnuPlotStop.Enabled = True
     Endif ' hGnuPlot.State = 1?
  Endif ' NOT IsNull(hGnuPlot)?
  btnRedrawGraph.Enabled = True
  btnScriptFileOpen.Enabled = True

End ' GnuPlotStart

Public Sub btnGnuPlotStart_Click()
  GnuPlotStart()
End ' btnGnuPlotStart_Click()

Public Sub GnuPlotStop()
  If Not IsNull(hGnuPlot) Then
     If hGnuPlot.State = hGnuPlot.Running Then
        Print #hGnuPlot, "q"
     Endif ' hGnuPlot.State = hGnuPlot.Running?
  Endif ' NOT IsNull(hGnuPlot)?
  
  ' Alternative: System-Aufruf:
  ' IF NOT IsNull(hGnuPlot) THEN 
  '    IF hGnuPlot.State = 1 THEN SHELL "kill -s 9 " & hGnuPlot.Id
  ' ENDIF 
End ' GnuPlotStop()

Public Sub btnGnuPlotStop_Click()
  GnuPlotStop()
End ' GnuPlotStop

Public Sub hGnuplot_Error(sError_OR_Result As String)
  
  txaPlotCode.Height = 140
  'txaErrorAndFit.Background = Color.RGB(255, 223, 223)
  txaErrorAndFit.Visible = True
  txaErrorAndFit.SetFocus
' Über den Kanal 2 kommen alle Ausgaben von GnuPlot!
  If InStr(sError_OR_Result, "Iteration 0") <> 0 Then
     sError_OR_Result = gb.NewLine & "|====   RESULTATE   =======================================================|" & sError_OR_Result
     btnResultSave.Visible = True
  Endif
  txaErrorAndFit.Text &= sError_OR_Result ' Alternative:  txaErrorAndFit.Insert(sError_OR_Result)
  Error.Clear
 
End ' hGnuplot_Error

Public Sub hGnuplot_Read()
  ' Hier kommen keine Daten ...
End ' Process_Read

Public Sub ReDrawGraph()
  txaErrorAndFit.Clear
  txaPlotCode.Height = 280
  txaErrorAndFit.Visible = False
  
  If Not IsNull(hGnuPlot) And hGnuPlot.State = hGnuPlot.Running Then
     Print #hGnuPlot, txaPlotCode.Text
  Endif ' NOT IsNull(hGnuPlot)?
End ' ReDrawGraph

Public Sub btnRedrawGraph_Click()
  ReDrawGraph
End ' RedrawGraph

Public Sub txaPlotCode_Change()
  If Len(txaPlotCode.Text) > 0 Then btnScriptFileSave.Enabled = True
End ' PlotCode_Change

Public Sub btnScriptFileOpen_Click()
  Dim sMessage1, sMessage2 As String
  
  Dialog.Path = sScriptFilePath
  Dialog.Title = ("Laden Sie ein GnuPlot-Skript in den Editor!")
  Dialog.Filter = ["*.plot", ("GnuPlot-Skript-Datei"), "*", ("Alle Dateien")]
  If Dialog.OpenFile() Then Return
  txaPlotCode.Clear
  txaPlotCode.Text = File.Load(Dialog.Path)
  FMain.Text = "GNUPLOT - Skript-Dateiname: " & File.Name(Dialog.Path)
  If Not IsNull(hGnuPlot) Then
     If hGnuPlot.State = 1 Then
        Print #hGnuPlot, "clear"
     Endif ' hGnuPlot.State = 1
  Endif ' NOT IsNull(hGnuPlot)
  txaPlotCode.SetFocus
  GnuPlotStart
  Catch
  sMessage1 = ("Die Skript-Datei ")
  sMessage2 = (" kann NICHT geöffnet werden!")
  Message.Error("FEHLER!" & Chr(10) & sMessage1 & File.Name(Dialog.Path) & sMessage2)
  
End ' FileOpen

Public Sub btnScriptFileSave_Click()
  Dim sMessage1, sMessage2 As String
  
  Dialog.Path = sScriptFilePath
  Dialog.Title = ("Speichern Sie den GnuPlot-Quelltext!")
  Dialog.Filter = ["*.plot", ("GnuPlot-Skript-Datei"), "*", ("Alle Dateien")]
  
  If Dialog.SaveFile() = True Then
     Message.Warning("Das Speichern wurde abgebrochen!")
     Return ' Cancel-Button
  Else
     If File.Ext(Dialog.Path) = Null Or File.Ext(Dialog.Path) <> "plot" Then
        Dialog.Path = File.SetExt(Dialog.Path, "plot")
     Endif
     File.Save(Dialog.Path, txaPlotCode.Text)
     Finally
     btnScriptFileSave.Enabled = False
     Catch
     sMessage1 = ("Die Skript-Datei ")
     sMessage2 = (" kann NICHT gespeichert werden!")
     Message.Error("FEHLER!" & Chr(10) & sMessage1 & File.Name(Dialog.Path) & sMessage2)
  Endif ' Dialog.SaveFile() = TRUE
End ' FileSave

Public Sub btnResultSave_Click()
  Dim sMessage1, sMessage2 As String
  
  Dialog.Path = sScriptFilePath
  Dialog.Title = ("Speichern Sie die Ergebnis-Matrix!")
  Dialog.Filter = ["*.fit", ("GnuPlot-Skript-Datei"), "*", ("Alle Dateien")]
  
  If Dialog.SaveFile() = True Then
     Message.Warning("Das Speichern wurde abgebrochen!")
     Return ' Cancel-Button
  Else
     If File.Ext(Dialog.Path) = Null Or File.Ext(Dialog.Path) <> "fit" Then
        Dialog.Path = File.SetExt(Dialog.Path, "fit")
     Endif
     File.Save(Dialog.Path, txaErrorAndFit.Text)
     Finally
     btnScriptFileSave.Enabled = False
     Catch
     sMessage1 = ("Die Ergebnis-Datei ")
     sMessage2 = (" kann NICHT gespeichert werden!")
     Message.Error("FEHLER!" & Chr(10) & sMessage1 & File.Name(Dialog.Path) & sMessage2)
  Endif
End ' ResultSave

Public Sub btnDisplayManualGnuplot_Click()
  MANF.DisplayManualFormular()
End ' DisplayManualGnuplot

Public Sub Form_Close()
   GnuPlotStop()
End ' Form_Close

Public Sub btnCloseProgram_Click()
  FMain.Close
End ' CloseProgram
