' Gambas module file

Public Const DEFAULT_THEME As String = "gambas"

Property Theme As TextHighlighterTheme

Private $hTheme As New TextHighlighterTheme
Private $iBackground As Integer

Public Sub _init()
  
  Settings.Read($hTheme, "/Editor/Styles")
  SetBackgroundColor(Settings["/Editor/Background"])
  
End

Public Sub InitEditor(hEditor As TextEditor, Optional (bDesaturate) As Boolean)
  
  hEditor.Background = $iBackground
  hEditor.Theme = $hTheme
  
End

Public Sub GetColor(sStyle As String) As Integer

  Dim iColor As Integer
  
  Try iColor = $hTheme[sStyle].Color
  If Error Then
    iColor = Color.Pink
    Debug sStyle & "?"
  Endif
  
  Return iColor
  
End 

Public Sub GetBackground() As Integer
  
  Return $iBackground
  
End

Public Sub GetBackgroundColor(Optional sDefault As String) As String
  
  Return Settings["/Editor/Background", sDefault]
  
End

Private Sub UpdateTheme()

  $hTheme.Background = $iBackground

End

Public Sub SetBackgroundColor(sColor As String)
  
  If Not sColor Or If sColor = "Default" Then 
    Settings["/Editor/Background"] = ""
    $iBackground = Color.TextBackground
  Else
    Settings["/Editor/Background"] = sColor
    $iBackground = FColorChooser.FromString(sColor)
  Endif
  
  UpdateTheme
  
End

Public Sub InitControl(hControl As Control)

  hControl.Background = GetBackground()
  hControl.Foreground = GetColor("Normal")
  
End

Private Function Theme_Read() As TextHighlighterTheme

  Return $hTheme

End

Private Sub Theme_Write(Value As TextHighlighterTheme)

  $hTheme = Value.Copy()
  Settings.Write($hTheme, "/Editor/Styles")
  UpdateTheme
  
End

Public Sub GetTheme() As TextHighlighterTheme
  
  Return $hTheme
  
End
