Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\ModuleClass.cls"
'#Uses "..\classes\ManualLinkClass.cls"
'#Uses "..\Classes\Port.cls"
'#Uses "..\Classes\PortPin.cls"
'#Uses "..\include\Install.inc"

Option Explicit

'*****************************************************************************
' Module:
'        START
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which create controls for the
'        module 'START'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil         06.10.04     Initial version
'
'
'
'*****************************************************************************


'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        m               reference to the main MDI window
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnProjectNew(m As MainMDI) As Boolean
    On Error GoTo OnProjectNewError

    Dim ps          As Peripherals
    Dim d           As DAvECollection
    Dim sc          As Script
    Dim sMainDialog As String

    OnProjectNew = False

    '------------------------------------------------------
    ' create project specific collections, display the graphics window
    '------------------------------------------------------
    If (Not InstallProject(m)) Then Exit Function
    
    '------------------------------------------------------
    ' initialize the project variables and registers
    ' by calling the "Initialize" function of the main module
    ' (the first module in the collection)
    '------------------------------------------------------
    Set d = ObjectCollection("MODULENAMES")
    sMainDialog = DefGetPath("Scripts") & d(1).ModuleDirectory & "\Dialog.dave"

    Set sc = New Script
    sc.Load (sMainDialog)
    If (Not sc.Functions.Initialize(m.ProjectData, d(1).ParameterValue)) Then Exit Function

    '------------------------------------------------------
    ' show project settings
    '------------------------------------------------------
    Set ps = New Peripherals
    Set ps.ProjectData = m.ProjectData
    ps.Caption = "Project Settings "
    ps.Scriptfile = sMainDialog
    ps.Show
    
    OnProjectNew = True
     
    Exit Function

OnProjectNewError:
    errorbox "Can not create a new project", Err.Description
    Exit Function
End Function

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        m               reference to the main MDI window
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnProjectOpen(m As MainMDI) As Boolean
    On Error GoTo OnProjectOpenError

    OnProjectOpen = False

    Dim r As ProjectData
    Set r = m.ProjectData
    
    r.v("WDT_WinEna") = "WDT_vEnable"
    r.v("GEN_UserDef0") = "WDT_vDisable"
    
    If (r.v("WDT_USE_Init") = 1) Then
        If (r.v("WDT_PARA_CLR_BIT") = "") Then
            r.v("WDT_PARA_CLR_BIT") = 0
        End If
        If (r.v("WDT_PARA_NO_CLR_BIT") = "") Then
            r.v("WDT_PARA_NO_CLR_BIT") = 0
        End If
    Else
       r.v("WDT_PARA_CLR_BIT") = 0
       r.v("WDT_PARA_NO_CLR_BIT") = 0
    End If
    '------------------------------------------------------
    ' create project specific collections, display the graphics window
    '------------------------------------------------------
    If (Not InstallProject(m)) Then Exit Function

    OnProjectOpen = True
    Exit Function

OnProjectOpenError:
    errorbox "Can not open the project", Err.Description
    Exit Function
End Function

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        m               reference to the main MDI window
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnProjectClose(m As MainMDI) As Boolean
    On Error GoTo OnProjectCloseError

    OnProjectClose = False

    '------------------------------------------------------
    ' disable menus
    '------------------------------------------------------
    Call EnableMenu(m, False)

    '------------------------------------------------------
    ' save project
    '------------------------------------------------------
    If (m.ProjectData.Changed) Then
        If (Ask(MSG_CLOSE_APPLICATION)) Then
            m.ProjectData.Save
        End If
    End If

    OnProjectClose = True
    Exit Function

OnProjectCloseError:
    Exit Function
End Function

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        m               reference to the main MDI window
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnProjectSave(m As MainMDI) As Boolean
    On Error GoTo OnProjectSaveError

    OnProjectSave = m.ProjectData.Save
    Exit Function

OnProjectSaveError:
    Exit Function
End Function

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        m               reference to the main MDI window
'        Toolname
'        ToolId
'        Tooltext
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnMenuClick(m As MainMDI, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    Dim r As ProjectData
    Dim sc As Script
    Dim p As Parameters
    Dim ps As Peripherals
    Dim cp As DAvECollection
    Dim mo As ModuleClass

    OnMenuClick = False

    Set cp = ObjectCollection("MODULENAMES")
    Set r = m.ProjectData

    Select Case Toolname
        Case "Settings"
          If (Ask(MSG_CALL_PROJECTSETTINGS)) Then
            Set ps = New Peripherals
            Set ps.ProjectData = m.ProjectData
            ps.Caption = "Project Settings "
            ps.Scriptfile = DefGetPath("Scripts") & "Main\Dialog.DAVE"
            ps.Show
          End If

        Case "Generate"
          If (m.ProjectSave) Then

            StartClip
            If (Not r.ResetFiles()) Then Exit Function

            For Each mo In cp
              If (mo.ModuleWithCG) Then
                Set sc = New Script
                sc.Load (DefGetPath("Scripts") & mo.ModuleDirectory & "\Codegenerator.dave")
                If (Not sc.Functions.StartGenerateCode(r, mo.ParameterValue)) Then Exit Function
              End If
            Next mo

            If (Not GenerateAssemblerFile(r, StripExtension(r.LoadedFile) & ".asm")) Then Exit Function
            If (Not r.AddFile(StripPath(StripExtension(r.LoadedFile)) & ".asm", "ASM")) Then Exit Function

            If (Not Rtf.GenerateRTFFile(r, StripExtension(r.LoadedFile) & ".rtf", r.s("MAIN_CPU_TYPE"), (r.s("MAIN_COMPILER")), r.s("MAIN_MEMORY"))) Then Exit Function
            If (Not r.AddFile(StripPath(StripExtension(r.LoadedFile)) & ".rtf", "DOC")) Then Exit Function

            ' the project file must be generated as the last one, because it contains a list of all other generated files
            If (Not GenerateProjectFile(r, StripExtension(r.LoadedFile) & ".dpt")) Then Exit Function
            If (Not r.AddFile(StripPath(StripExtension(r.LoadedFile)) & ".dpt")) Then Exit Function

            StopClip

            If (GetShowCodegenerationProtocol()) Then
                m.ShowFileView (StripExtension(m.ProjectData.LoadedFile) & ".rtf")
            Else
                m.RefreshFileView
            End If

          End If

        Case "Info"
          UserMessage FormatMessageBoxText(MSG_DOCU_BUTTON)

        Case "Help"
          HelpContext 1008

        Case "Doc"
           Call ShowDocument(Tooltext, "Main")

        Case "WWW"
          StartDoc (Tooltext)

        Case Else

    End Select

  OnMenuClick = True
  Exit Function

OnMenuClickError:
         StopClip
  Call errorbox("Code generation failed", Error$)
  Exit Function
End Function


'-----------------------------------------------------------------------------
' local functions
'-----------------------------------------------------------------------------

'*****************************************************************************
'Description:
'        This function generates the Project File.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the Project data object
'        szFile
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Private Function GenerateProjectFile(r As ProjectData, szFile As String) As Boolean
    On Error GoTo GenerateProjectFileError

        Dim l As Long
        Dim s As String
        Dim p As String

    GenerateProjectFile = False
    
        s = ""
        p = ""

        ' create version section
        p = p & "[Version]" & vbCrLf
        p = p & "Format=1" & vbCrLf
        p = p & "Product=" & DefGetValue("ProductVersion") & vbCrLf
        p = p & "Register=" & r.Regs.Revision & vbCrLf
        p = p & vbCrLf

        ' create general section
        s = s & "[General]" & vbCrLf
        s = s & "Device=""XC886CLM""" & vbCrLf
        s = s & "DeviceType=""" & r.s("MAIN_CPU_TYPE") & """" & vbCrLf
        s = s & vbCrLf

        ' create interface section
        s = s & "[Interface]" & vbCrLf
        s = s & "Compiler=""" & StrConv(r.s("MAIN_COMPILER"), vbUpperCase) & """" & vbCrLf
        s = s & "MemoryModel=""" & StrConv(r.s("MAIN_MEMORY"), vbUpperCase) & """" & vbCrLf
        s = s & vbCrLf

        ' create environment section
        s = s & "[Environment]" & vbCrLf
        s = s & "OscillatorFrequency=" & r.d("MAIN_FCLK") & vbCrLf
        s = s & "DeviceFrequency=" & GetSystemClock(r) & vbCrLf
        s = s & vbCrLf

        ' create files section
        s = s & "[Files]" & vbCrLf
        For l = 1 To r.CountFiles
                s = s & "File" & Format$(l) & "=""" & r.File(l) & """" & vbCrLf
                s = s & "Type" & Format$(l) & "=""" & r.FileType(l) & """" & vbCrLf
        Next l
        s = s & vbCrLf

        If (Not GenerateDescriptionFile(r, szFile, s, p)) Then Exit Function

    GenerateProjectFile = True
    Exit Function
    
GenerateProjectFileError:
    Call errorbox("Can not generate the project file", Error$)
    Exit Function
End Function

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        m               reference to the main MDI window
'        b               requested state of the menu
'                        True=Enabled, False=Disabled
'*****************************************************************************
Private Sub EnableMenu(m As MainMDI, b As Boolean)
    On Error Resume Next
    
    Dim ts As Tool
    Dim bs As Band
    Dim pic As Object
    Dim pos As Integer
    
    m.Menu.Bands("File").Tools("Generate").Enabled = b
    m.Menu.Bands("Main").Tools("Generate").Enabled = b
    m.Menu.Bands("File").Tools("Settings").Enabled = b
    m.Menu.Bands("Main").Tools("Info").Enabled = b
    m.Menu.Bands("View").Tools("Files").Enabled = b
    m.Menu.Bands("Main").Tools("Files").Enabled = b
    m.Menu.Bands("View").Tools("Registers").Enabled = b
    m.Menu.Bands("Main").Tools("Registers").Enabled = b
    
    Set bs = m.Menu.Bands("Info")
    
    If (b) Then
        Set pic = LoadBitmap(DefGetPath("Icons") & "DOC.BMP")
        If (IsNull(pic)) Then Exit Sub

        pos = 1000

        Set ts = bs.Tools.Add(pos, "Doc")
        ts.Caption = "User's Manual"
        ts.Text = "UsersManual"
        ts.SetPicture Index:=0, Picture:=pic, Color:=QBColor(5)
        ts.BeginGroup = True
        pos = pos + 1

        Set pic = LoadBitmap(DefGetPath("Icons") & "IE.BMP")
        If (IsNull(pic)) Then Exit Sub
        Set ts = bs.Tools.Add(pos, "WWW")
        ts.Caption = "Additional Information"
        ts.Text = "http://www.infineon.com/"
        ts.SetPicture Index:=0, Picture:=pic, Color:=QBColor(5)
        ts.BeginGroup = True
        pos = pos + 1
    Else
        bs.Tools.RemoveAll
    End If
    
End Sub

'*****************************************************************************
'Description:
'        This functions creates project specific collections
'        (modules, interrupts, ports and portpins),
'        then creates and displays the window that contains the controller
'        controller graphics, and enables the menu of the main MDI window.
'-----------------------------------------------------------------------------
'Parameters:
'        m               reference to the main MDI window
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Private Function InstallProject(m As MainMDI) As Boolean
    On Error GoTo InstallProjectError

    Dim gc          As New GraphicConfigure
    Dim d           As DAvECollection
    Dim c           As DAvECollection
    Dim cPorts      As DAvECollection
    Dim cPortPins   As PortPin
    Dim cLinks      As ManualLinkClass

    InstallProject = False
    Set d = New DAvECollection
    Set c = New DAvECollection
    Set cPorts = New DAvECollection
    Set cPortPins = New PortPin
    Set cLinks = New ManualLinkClass

    '------------------------------------------------------
    ' create a collection of the modules
    '------------------------------------------------------
    If (Not InstallModule(d)) Then Exit Function
        ObjectCollection.Add d, "MODULENAMES"

    '------------------------------------------------------
    ' create a collection of the interrupts
    '------------------------------------------------------
    If (Not InstallInterrupts(c)) Then Exit Function
        ObjectCollection.Add c, "IRSOURCES"

    '------------------------------------------------------
    ' create a collection of the port pins
    '------------------------------------------------------
    If (Not InstallPorts(cPorts)) Then Exit Function
        ObjectCollection.Add cPorts, "PORTS"

    If (Not InstallPortPins(cPortPins)) Then Exit Function
        ObjectCollection.Add cPortPins, "PORTPINS"

    '------------------------------------------------------
    ' create a collection of the manual links
    '------------------------------------------------------
    If (Not InstallManualLinks(cLinks)) Then Exit Function
        ObjectCollection.Add cLinks, "MANUALLINKS"

    '------------------------------------------------------
    ' create a graphics configure window
    '------------------------------------------------------
    Set gc.ProjectData = m.ProjectData
    gc.Caption = "DAvE " & DefGetValue("Name") & " ( Release v" & DefGetValue("ProductVersion") & " )"

    gc.Scriptfile = DefGetPath("Startup") & "GRAPH.DAVE"
    If (Not gc.Show()) Then Exit Function

    '------------------------------------------------------
    ' enable menus
    '------------------------------------------------------
    Call EnableMenu(m, True)

    InstallProject = True
    Exit Function

InstallProjectError:
    Exit Function
End Function


