' Copyright (C) 2004-2012 AGORA Software BV, http://www.helpgenerator.com/, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.

Option Explicit
Option Compare Text

Private Const mcstrModuleName As String = "clsHtmlHelp"

Private WithEvents mfrm As Access.Form ' form
Private WithEvents mcmdHelp As Access.CommandButton ' help button

Private mstrHelpFullPath As String ' help fullpath
Private mHwnd As Long ' form HWnd

Private mobjParent As CHTMLHelp ' clsHTMLHel class instance to call on form closing

Private mfTerminated As Boolean ' = true when class instance termination
' actions finished

Public Sub Init(ByRef rfrm As Access.Form, _
Optional ByRef rcmdHelp As Access.CommandButton = Nothing, _
Optional ByVal vstrHelpFileNameOrFullPath As String = "", _
Optional ByRef robjParent As CHTMLHelp = Nothing, _
Optional ByVal vfKeyPreview As Boolean = True)
' init form's help hanler
Set mfrm = rfrm
If mfrm.HasModule = False Then mfrm.HasModule = True

mfrm.KeyPreview = vfKeyPreview
If vfKeyPreview = True Then
mfrm.OnKeyDown = "[Event Procedure]"
End If
mfrm.OnClose = "[Event Procedure]"

mHwnd = mfrm.hWnd
mfTerminated = False

If Not rcmdHelp Is Nothing Then
Set mcmdHelp = rcmdHelp
mcmdHelp.OnClick = "[Event Procedure]"
End If

If Len(vstrHelpFileNameOrFullPath) = 0 Then
mstrHelpFullPath = AppPath() & rfrm.HelpFile
mstrHelpFullPath = HelpFileFullPath(vstrHelpFileNameOrFullPath)
End If

Set mobjParent = robjParent
End Sub

Public Sub Terminate()
On Error GoTo HandleExit
If mfTerminated = False Then
HTMLHelp.CloseHelpWindows mHwnd
Set mcmdHelp = Nothing
Set mfrm = Nothing
mfTerminated = True
End If
End Sub

Private Sub mcmdHelp_Click()
HTMLHelp.OpenTOC mstrHelpFullPath, mHwnd
End Sub

Private Sub mfrm_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF1 Then
KeyCode = 0
End If
End Sub

Private Sub mfrm_Close()
If Not mobjParent Is Nothing Then
mobjParent.FormClosing mfrm
Set mobjParent = Nothing
End If
End Sub

Private Sub OpenHelp()
HTMLHelp.OpenContext mfrm.HelpContextId, mstrHelpFullPath, mHwnd
End Sub