' Gambas module file

Public Const MIME As String = "text/x-gambas-action"

Public Actions As Collection
Public Toolbars As Collection

Private $cShortcut As Collection

Public Sub SetShortcut(sAction As String, sShortcut As String)
  
  If Left(sAction) = "." Then sAction = Mid$(sAction, 2)
  Settings["gb.form.mdi/Shortcuts" &/ sAction] = sShortcut
  
End

Public Sub GetShortcut(sAction As String) As String
  
  If Left(sAction) = "." Then sAction = Mid$(sAction, 2)
  Return Settings["gb.form.mdi/Shortcuts" &/ sAction] 
  
End

Private Sub InitShortcut()
  
  Dim iInd As Integer
  Dim sKey As String
  Dim sList As String
  
  $cShortcut = New Collection

  sList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-*/."

  For iInd = 1 To Len(sList)
    sKey = Mid$(sList, iInd, 1)
    $cShortcut[Key[sKey]] = sKey
  Next

  For iInd = 1 To 12
    sKey = "F" & CStr(iInd)
    $cShortcut[Key[sKey]] = sKey
  Next
  
  For Each sKey In ["Backspace", "Del", "Down", "End", "Enter", "Esc", "Home", "Ins", "Left", "Pause", "PgDown", "PgUp", "Return", "Space", "Right", "Up"]
    $cShortcut[Key[sKey]] = sKey
  Next

End


Public Sub FindShortcut() As String
  
  Dim sShortcut As String
  Dim sMod As String

  If Not $cShortcut Then InitShortcut
  
  sShortcut = $cShortcut[Key.Code]

  If sShortcut Then

    If Len(sShortcut) > 1 Or If Not Key.Normal Then
      
      If Key.Control Then sMod &= "Ctrl+"
      If Key.Meta Then sMod &= "Meta+"
      If Key.Alt Then sMod &= "Alt+"
      If Key.Shift Then sMod &= "Shift+"
  
      Return sMod & sShortcut
      
    Endif
    
  Endif
  
End

Private Sub LoadAction(sFile As String)
  
  Dim hFile As File
  Dim sAction As String
  Dim sToolbar As String
  Dim sLine As String
  Dim aLine As String[]
  Dim hAction As CAction
  Dim hToolbar As CToolbar
  Dim cSlot As New Collection
  Dim bHasShortcut As Boolean

  hFile = Open sFile
  While Not Eof(hFile)
    Line Input #hFile, sLine
    sLine = Trim(sLine)
    If Not sLine Then Continue
    If Left(sLine) = "#" Then Continue
    
    If sLine Begins "{ Action " Then
      cSlot.Clear
      bHasShortcut = False
      sAction = Trim(Mid$(sLine, 10))
    Else If sLine Begins "{ Toolbar " Then
      cSlot.Clear
      sToolbar = File.BaseName(sFile) & "." & Trim(Mid$(sLine, 10))
    Else If sLine = "}" Then
      If sAction Then
        hAction = New CAction(sAction, bHasShortcut, cSlot, Actions[sAction])
        Actions[hAction.Key] = hAction
        sAction = ""
      Else If sToolbar Then
        hToolbar = New CToolbar(sToolbar, cSlot)
        Toolbars[hToolbar.Key] = hToolbar
        sToolbar = ""
      Endif
    Else
      aLine = Scan(sLine, "* = *")
      If aLine.Count <> 2 Then Continue
      cSlot[aLine[0]] = UnQuote(aLine[1])
      If aLine[0] = "Shortcut" Then bHasShortcut = True
    Endif
  Wend
  Close #hFile
  
End


Public Sub InitAction()
  
  Dim sFile As String
  
  If Actions Then Return

  'Debug "Loading actions..."

  Actions = New Collection
  Toolbars = New Collection

  ' Why loading gb.form.mdi actions??
  ' For Each sFile In Dir(".action", "*.action")
  '   LoadAction(".action" &/ sFile)
  ' Next

  For Each sFile In Dir("../.action", "*.action")
    LoadAction("../.action" &/ sFile)
  Next
  
End

Public Sub Add(sKey As String, sText As String, sPicture As String, bHasShortcut As Boolean, sDefaultShortcut As String)
  
  Dim hAction As CAction
  
  InitAction
  
  hAction = New CAction(sKey, bHasShortcut, ["Text": sText, "Picture": sPicture, "Shortcut": sDefaultShortcut], Actions[sKey])
  Actions[hAction.Key] = hAction
  
End

Public Sub GetList() As String[]
  
  Dim aList As New String[]
  
  InitAction
  
  For Each Actions
    aList.Add(Actions.Key)
  Next
  
  Return aList.Sort()
  
End
