Attribute VB_Name = "Pages"
'#Uses "Const.inc"
'#Uses "Global.inc"
'#Uses "Format.inc"
'#Uses "..\classes\InterruptClass.cls"
'#Uses "..\classes\ModuleClass.cls"
'#Uses "..\classes\ManualLinkClass.cls"
Option Explicit
'*****************************************************************************
' Module:
'        Port.inc
'-----------------------------------------------------------------------------
' Description:
'          This module contains constants and global functions for
'          global pages (Interrupt, Notes ..)
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        3.0  ahe            28.07.00    final version for C161PI
'                                        - changes 'As ManualLinks' to
'                                          'As Object'
'
'             ahe            04.09.00    - changes PEC-frame 
'                                          width from 8425 to 8450
'
'*****************************************************************************

'*****************************************************************************
' @Global Constants
'*****************************************************************************


'*****************************************************************************
' @Global Types
'*****************************************************************************
' this type is used for the manual links in the function LoadMenu and 
' InitMenue
Type ManualLinkType
  sCaption As String     ' the caption AND key for the manual link
  bGroup As Boolean      ' specifies if a new group should be created (draws
                         ' a line about this link)
End Type


'*****************************************************************************
' @Global Functions
'*****************************************************************************


'*****************************************************************************
' @bfunc   Public | OnLoadPageInterrupts | Boolean |
'
'-----------------------------------------------------------------------------
'          This function adds a page to the peripheral.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
'
'*****************************************************************************
Public Function OnLoadPageInterrupts (ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageInterrupts = False

  If (Not ps.AddPage("Interrupts")) Then Exit Function

  OnLoadPageInterrupts = True
End Function


'*****************************************************************************
' @bfunc   Public | OnChangingPageInterrupts | Boolean |
'
'-----------------------------------------------------------------------------
'          This function makes all activities for the interrupt page in each
'          module function OnChangingPage.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
' @bparm   | Pagename | String | current page name
'
'*****************************************************************************
Public Function OnChangingPageInterrupts(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next
  OnChangingPageInterrupts = False

  Dim e As Collection

  Set e = ps.ExtenderCollection

  If (Not e("Interrupts").Loaded) And (Pagename = "Interrupts") Then
    e("Interrupts").Loaded = True
    If (Not ps.AddIRTab16("InterruptTable")) Then Exit Function
    If (Not ps.AddFrame("Interrupts_1", 0, 0, 0, 0, "Interrupts")) Then Exit Function
  End If

  OnChangingPageInterrupts = True
End Function


'*****************************************************************************
' @bfunc   Public | OnShowPageInterrupts | Boolean |
'
'-----------------------------------------------------------------------------
'          This function makes all activities for the interrupt page in each
'          module function OnShow.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
' @bparm   | Controlname | String | current control name
' @bparm   | v() | Variant | values for the controlls
' @bparm   | SeekName | String | name for searching in the interrupt list
' @bparm   | Pagename | String | current page name
'
'*****************************************************************************
Public Function OnShowPageInterrupts(ps As Peripherals, Controlname As String, v() As Variant, SeekName As String, Pagename As String) As Boolean
  On Error Resume Next
  OnShowPageInterrupts = False
  
    Dim r As ProjectData
    Dim p As Parameters
    Dim c As Collection
    Dim i As Integer
    Dim cp As DAvECollection
    Dim ir As InterruptClass

    Dim IntName As Variant
    Dim IntLevel As Variant
    Dim IntGroup As Variant
    Dim IntOldLevel As Variant
    Dim IntOldGroup As Variant

    Set r = ps.ProjectData
    Set p = ps.Parameters
    Set c = ps.ControlCollection
    Set cp = ObjectCollection("IRSOURCES")

    IntName = v(1)
    IntLevel = v(2)
    IntGroup = v(3)
    IntOldLevel = v(4)
    IntOldGroup = v(5)

  
    If (Controlname = "InterruptTable") Then
        If (IntOldLevel >= 14) Then
            r.v("PEC_CHANNEL_" & Format$(((IntOldLevel - 14) * 4) + IntOldGroup)) = ""

            'ahe
            Call SetPECChannelToDefault(r, ((IntOldLevel - 14) * 4) + IntOldGroup)
            'ahe

        End If
        If (IntLevel >= 14) Then
            r.v("PEC_CHANNEL_" & Format$(((IntLevel - 14) * 4) + IntGroup)) = IntName
        End If
        r.v(cp(IntName).LevelName) = IntLevel
        r.v(cp(IntName).GroupName) = IntGroup

    ElseIf (Pagename = "Interrupts") Then
        
        With c("InterruptTable")
            .Freeze
            .Clear
          For Each ir In cp
            If (r.v(ir.Enabled) = 1) Then
              .InterruptLevel = r.v(ir.LevelName)
              .GroupLevel = r.v(ir.GroupName)
              .Sourcename = ir.IntName
            End If
          Next ir
            .SeekList SeekName
            .Melt
        End With
    End If  
  

  OnShowPageInterrupts = True
End Function


'*****************************************************************************
' @bfunc   Public | OnLoadPagePEC | Boolean |
'
'-----------------------------------------------------------------------------
'          This function adds a page to the peripheral.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
'
'*****************************************************************************
Public Function OnLoadPagePEC (ps As Peripherals) As Boolean
  OnLoadPagePEC = False

  If (Not ps.AddPage("PEC")) Then Exit Function

  OnLoadPagePEC = True
End Function


'*****************************************************************************
' @bfunc   Public | OnChangingPagePEC | Boolean |
'
'-----------------------------------------------------------------------------
'          This function makes all activities for the PEC page in each
'          module function OnChangingPage.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
' @bparm   | Pagename | String | current page name
'
'*****************************************************************************
Public Function OnChangingPagePEC(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next
  OnChangingPagePEC = False

  Dim i As Integer
  Dim e As Collection

  Set e = ps.ExtenderCollection

   If (Not e("PEC").Loaded) And (Pagename = "PEC") Then
    e("PEC").Loaded = True
    if (Not ps.AddFrame("PEC_Channels", 100 , 100, 8450, 5150, "PEC Channels")) Then Exit Function
    For i = 0 To 7
      if (Not ps.AddButton("cmdPEC" & i, 200 , 400 + i * 600, 2100, 400, "Configure PEC channel " & i)) Then Exit Function
      if (Not ps.AddLabel("lblPEClbl" & i,0 , 2450 , 450 + i * 600, 1200, 400, "Interrupt source")) Then Exit Function
      if (Not ps.AddLabel("lblPEC" & i,1 , 3800 , 450 + i * 600, 2000, 315)) Then Exit Function
    Next i
  End If

  OnChangingPagePEC = True
End Function


'*****************************************************************************
' @bfunc   Public | OnShowPagePEC | Boolean |
'
'-----------------------------------------------------------------------------
'          This function makes all activities for the PEC page in each
'          module function OnShow.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
' @bparm   | Controlname | String | current control name
' @bparm   | Pagename | String | current page name
'
'*****************************************************************************
Public Function OnShowPagePEC(ps As Peripherals, Controlname As String, Pagename As String) As Boolean
  On Error Resume Next
  OnShowPagePEC = False  

  Dim r As ProjectData
  Dim c As Collection
  Dim i As Integer

  Set r = ps.ProjectData
  Set c = ps.ControlCollection

  If (Pagename = "PEC") Then
    For i = 0 To 7
      With c("lblPEC" & i)
        .Caption = r.v("PEC_CHANNEL_" & i)
      End With
    Next i
  End if

  OnShowPagePEC = True
End Function


'*****************************************************************************
' @bfunc   Public | OnClickPagePEC | Boolean |
'
'-----------------------------------------------------------------------------
'          This function makes all activities for the PEC page in each
'          module function OnClick.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
' @bparm   | Controlname | String | current control name
'
'*****************************************************************************
Public Function OnClickPagePEC (p As Peripherals, Controlname As String) As Boolean
  On Error Resume Next
  OnClickPagePEC = False

  Dim i As Integer
  Dim np As Peripherals

  For i = 0 To 7
    If (Controlname =  "cmdPEC" & i) Then
        Set np = New Peripherals
        np.Parameters.Add ("x")
        np.Parameters.v("x") = i
        np.Parameters.Add ("Channel")
        np.Parameters.v("Channel") = i
        np.Parameters.Add ("c")
        np.Parameters.v("c") = ""
        Set np.ProjectData = p.ProjectData
        np.Caption = "Configure PEC Channel " & i
        np.Scriptfile = DefGetPath("Scripts") & "\PEC\PEC.DAVE"
        np.Show
    End If
  Next i

  OnClickPagePEC = True
End Function


'*****************************************************************************
' @bfunc   Public | OnLoadPageNotes | Boolean |
'
'-----------------------------------------------------------------------------
'          This function adds a page to the peripheral.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
'
'*****************************************************************************
Public Function OnLoadPageNotes (ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageNotes = False

  If (Not ps.AddPage("Notes")) Then Exit Function

  OnLoadPageNotes = True
End Function


'*****************************************************************************
' @bfunc   Public | OnChangingPageNotes | Boolean |
'
'-----------------------------------------------------------------------------
'          This function makes all activities for the Notes page in each
'          module function OnChangingPage.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
' @bparm   | PageName | String | current page name
' @bparm   | ModuleName | String | current module name
'
'*****************************************************************************
Public Function OnChangingPageNotes(ps As Peripherals, Pagename As String, ModuleName As String) As Boolean
  On Error Resume Next
  OnChangingPageNotes = False

  Dim i As Integer
  Dim e As Collection
  Dim s As String

  Set e = ps.ExtenderCollection

  If (Not e("Notes").Loaded) And (Pagename = "Notes") Then
    e("Notes").Loaded = True
    s = ModuleName & "_Notes"
    If (Not ps.AddFrame("fra" & s, 100 , 100, 8450, 5200, "")) Then Exit Function
      If (Not ps.AddText("txt" & s, 1825, 600, 6000, 3780)) Then Exit Function
      If (Not ps.AddLabel("lbl" & s,0 , 200 , 600, 1575, 380, "Insert Notes:")) Then Exit Function
  End If

  OnChangingPageNotes = True
End Function


'*****************************************************************************
' @bfunc   Public | OnShowPageNotes | Boolean |
'
'-----------------------------------------------------------------------------
'          This function makes all activities for the Notes page in each
'          module function OnShow.
'
'-----------------------------------------------------------------------------
' @rdesc   one of the following values
' @flag    False| if an error has been occured in the function
' @flag    True|  else
'
'-----------------------------------------------------------------------------
' @bparm   | ps | Peripherals | reference to this object
' @bparm   | Controlname | String | current control name
' @bparm   | Pagename | String | current page name
' @bparm   | ModuleName | String | current module name
' @bparm   | v() | Variant | values for the controlls
'
'*****************************************************************************
Public Function OnShowPageNotes(ps As Peripherals, Controlname As String, Pagename As String, ModuleName As String, v () As Variant) As Boolean
  On Error Resume Next
  OnShowPageNotes = False
  
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim i As Integer
  Dim s As String

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  s = ModuleName & "_Notes"
  
  '------------------------------------------------------------------------------------
  ' page: Notes (action)
  '------------------------------------------------------------------------------------
  Select Case Controlname
      '--------------------------------------------------------------------------------
      ' control: txt.._Notes (action)
      '--------------------------------------------------------------------------------
      Case "txt" & s
           ' action on clicking the selected control
           OnShowPageNotes = True
           If (IsInputFormatOK(v(1), FORMAT_NOTES, c("lbl" & s).Caption, , )) Then
             r.v(s) = v(1)
           Else
               Exit Function
           End If 
  End Select

  '------------------------------------------------------------------------------------
  ' page: Notes (show)
  '------------------------------------------------------------------------------------
  If (Pagename = "Notes") Then
    '----------------------------------------------------------------------------------
    ' control: lbl.._Notes (show)
    '----------------------------------------------------------------------------------
    ' action on showing the control
    With c("lbl" & s)
      .Enabled = True
    End With

'    With e("lbl" & s)
        ' tooltip if control is disabled 
        ' tooltip if control is enabled 
'    End With

    '----------------------------------------------------------------------------------
    ' control: txt.._Notes
    '----------------------------------------------------------------------------------   
    ' action on showing the control
    With c("txt" & s)
      ' disable condition
      .Enabled = True
      ' show condition
      .Text = FormatTextOutput(r.v(s), "S")
    End With

    With e("txt" & s)
        ' tooltip if control is disabled 
        ' tooltip if control is enabled 
    End With

  End If         ' if Pagename

  OnShowPageNotes = True
End Function



'*****************************************************************************
'Description:
'        This function initializes the menu of the Peripherals window.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        sScriptName     name of the script for which the menu should be added
'                        (e.g. "SSCx\Dialog")
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function InitMenu(ps As Peripherals, sScriptName As String) As Boolean
    On Error GoTo InitMenuError
    
    Dim ts As Tool
    Dim bs As Band
    Dim bsMain As Band
    Dim pic As Object
    Dim i As Long
    Dim l As Long
    Dim m As Object
    
    InitMenu = False  
    Set m = ObjectCollection("MANUALLINKS")
    
    ' add toolbar band
    Set bsMain = ps.Menu.Bands.Add("MainBand")
    bsMain.Caption = "DAvE Tools"
    bsMain.Type = DDBTNormal
    bsMain.flags = DDBFFixed
    
    'close
    Set pic = LoadBitmap(DefGetPath("Icons") & "CLOSE.BMP")
    If (IsNull(pic)) Then Exit Function

    Set ts = bsMain.Tools.Add(1, "Close")
    ts.SetPicture Index:=0, Picture:=pic, Color:=QBColor(5)
    ts.ToolTipText = "Close this window"

    ' info
    Set pic = LoadBitmap(DefGetPath("Icons") & "INFO.BMP")
    If (IsNull(pic)) Then Exit Function

    Set ts = bsMain.Tools.Add(2, "Info")
    ts.SetPicture Index:=0, Picture:=pic, Color:=QBColor(5)
    ts.ControlType = DDTTButtonDropDown
    ts.SubBand = "Info"
    ts.ToolTipText = "Documentation"
    ts.BeginGroup = True
    Set bs = ps.Menu.Bands.Add("Info")
    bs.Type = DDBTPopup

    ' manuals
    Set pic = LoadBitmap(DefGetPath("Icons") & "DOC.BMP")
    If (IsNull(pic)) Then Exit Function

    For l = 1 To m.MenuCounter()
      If ( m.ScriptName(l) = sScriptName) Then
        For i = 1 To m.LinkCounter(l)
          Set ts = bs.Tools.Add(i, "Doc")
          ts.Caption = m.LinkName(l, i)
          ts.Text = m.KeyName(l, i)
          ts.SetPicture Index:=0, Picture:=pic, Color:=QBColor(5)
      		ts.BeginGroup = m.Group(l,i)
        Next i
        Exit For
      End If
    Next l

    ' help
    Set pic = LoadBitmap(DefGetPath("Icons") & "HELP.BMP")
    If (IsNull(pic)) Then Exit Function

    Set ts = bsMain.Tools.Add(3, "Help")
    ts.BeginGroup = True
    ts.SetPicture Index:=0, Picture:=pic, Color:=QBColor(5)
    ts.ToolTipText = "Help"

    ps.Menu.RecalcLayout
    
    InitMenu = True
    Exit Function

InitMenuError:
  Errorbox "Can not load the menu", Err.Description
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called if the user clicks on a menu entry.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Toolname        name of the entry
'        ToolId          id of the entry
'        Tooltext        additional text of the entry
'        sScriptName     name of the script for which the menu should be added
'                        (e.g. "SSCx\Dialog")
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function MenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String, sScriptName As String) As Boolean
    On Error GoTo MenuClickError
    Dim i As Long
    Dim l As Long
    Dim m As Object

    MenuClick = False
    Set m = ObjectCollection("MANUALLINKS")


    Select Case Toolname
        Case "Close"
            ps.Terminate
        Case "Info"
            UserMessage FormatMessageBoxText(MSG_DOCU_BUTTON)
        Case "Help"
            HelpContext 1008
        Case "Doc"
            For l = 1 To m.MenuCounter()
              If ( m.ScriptName(l) = sScriptName) Then
                For i = 1 To m.LinkCounter(l)
                  If (Tooltext = m.KeyName(l, i)) Then
                    Call ShowDocument(m.DefSection(l,i), Tooltext)
                    Exit For
                  End If
                Next i
                Exit For
              End If
            Next l
    End Select

    MenuClick = True
    Exit Function

MenuClickError:
    Exit Function
End Function
