{
  UseHTMLHelp Delphi unit

  This unit has been written by Jan Goyvaerts <http://www.jgsoft.com>
  It has been placed into the public domain.

  Add this unit to the uses clause of any unit in your application,
  and any help requests will automatically be translated into the
  proper HTML Help calls.

  You will need to specify the .chm file that contains your HTML Help
  in the help file setting in Delphi's project options.
  Otherwise, nothing will happen when the user presses F1.

  The unit does its work by assigning its own event handler to the
  Application.OnHelp event.  This means you must take care not to
  assign your own handler to Application.OnHelp, as this will
  defeat the purpose of this unit.

  Also, this simple unit will ignore any form's HelpFile property.
  This property is new in Delphi 4
}
unit UseHTML;
{$H+,R-,S-,Q-,I+,O+,F-,A+,U+,K+,W-,V+,B-,X+,T-,G+,P+,L+,Y+,D-}
//{$I CFL.INC}

interface

uses
  Windows, Messages, SysUtils, Forms;

// Commands to pass to HtmlHelp()
const
  HH_DISPLAY_TOPIC        = $0000;
  HH_HELP_FINDER          = $0000;  // WinHelp equivalent
  HH_DISPLAY_TOC          = $0001;
  HH_DISPLAY_INDEX        = $0002;
  HH_DISPLAY_SEARCH       = $0003;
  HH_SET_WIN_TYPE         = $0004;
  HH_GET_WIN_TYPE         = $0005;
  HH_GET_WIN_HANDLE       = $0006;
  HH_ENUM_INFO_TYPE       = $0007;  // Get Info type name, call repeatedly to enumerate, -1 at end
  HH_SET_INFO_TYPE        = $0008;  // Add Info type to filter.
  HH_SYNC                 = $0009;
  HH_RESERVED1            = $000A;
  HH_RESERVED2            = $000B;
  HH_RESERVED3            = $000C;
  HH_KEYWORD_LOOKUP       = $000D;
  HH_DISPLAY_TEXT_POPUP   = $000E;  // display string resource id or text in a popup window
  HH_HELP_CONTEXT         = $000F;  // display mapped numeric value in dwData
  HH_TP_HELP_CONTEXTMENU  = $0010;  // text popup help, same as WinHelp HELP_CONTEXTMENU
  HH_TP_HELP_WM_HELP      = $0011;  // text popup help, same as WinHelp HELP_WM_HELP
  HH_CLOSE_ALL            = $0012;  // close all windows opened directly or indirectly by the caller
  HH_ALINK_LOOKUP         = $0013;  // ALink version of HH_KEYWORD_LOOKUP
  HH_GET_LAST_ERROR       = $0014;  // not currently implemented // See HHERROR.h
  HH_ENUM_CATEGORY        = $0015;	// Get category name, call repeatedly to enumerate, -1 at end
  HH_ENUM_CATEGORY_IT     = $0016;  // Get category info type members, call repeatedly to enumerate, -1 at end
  HH_RESET_IT_FILTER      = $0017;  // Clear the info type filter of all info types.
  HH_SET_INCLUSIVE_FILTER = $0018;  // set inclusive filtering method for untyped topics to be included in display
  HH_SET_EXCLUSIVE_FILTER = $0019;  // set exclusive filtering method for untyped topics to be excluded from display
  HH_INITIALIZE           = $001C;  // Initializes the help system.
  HH_UNINITIALIZE         = $001D;  // Uninitializes the help system.
  HH_PRETRANSLATEMESSAGE  = $00FD;  // Pumps messages. (NULL, NULL, MSG*).
  HH_SET_GLOBAL_PROPERTY  = $00FC;  // Set a global property. (NULL, NULL, HH_GPROP)

function HtmlHelp(hwndCaller: THandle; pszFile: PChar; uCommand: cardinal; dwData: longint): THandle; stdcall;

implementation

function HtmlHelp(hwndCaller: THandle; pszFile: PChar; uCommand: cardinal; dwData: longint): THandle; stdcall;
         external 'hhctrl.ocx' name 'HtmlHelpA';

type
  TUseHTMLHelp = class(TObject)
    function ApplicationHelp(Command: Word; Data: Longint; var CallHelp: Boolean): Boolean;
  end;

function TUseHTMLHelp.ApplicationHelp(Command: Word; Data: Longint; var CallHelp: Boolean): Boolean;
const
  Hoh : pchar = 'VIEWHLP.CHM';
begin
  // Make sure VCL doesn't activate WinHelp
  CallHelp := False;
  Result := True;
  case Command of
    HELP_CONTEXT,
    HELP_CONTEXTPOPUP : // Ordinary context jump
      HtmlHelp(Application.MainForm.Handle, PChar(Application.HelpFile), HH_HELP_CONTEXT, Data);
    HELP_KEY,
    HELP_PARTIALKEY : // Keyword lookup
      HtmlHelp(Application.MainForm.Handle, PChar(Application.HelpFile), HH_DISPLAY_INDEX, Data);
    HELP_QUIT : // Application quits -> close all its help files
      HtmlHelp(Application.MainForm.Handle, nil, HH_CLOSE_ALL, 0);
    HELP_HELPONHELP : // Help on Help
      HtmlHelp(Application.MainForm.Handle, Hoh, HH_HELP_FINDER, 0)
  else // Any other command -> display table of contents
    HtmlHelp(Application.MainForm.Handle, PChar(Application.HelpFile), HH_DISPLAY_TOC, 0)
  end;
end;

var
  HTMLHelpUser: TUseHTMLHelp;

initialization
  HTMLHelpUser := TUseHTMLHelp.Create;
  Application.OnHelp := HTMLHelpUser.ApplicationHelp;
finalization
  Application.OnHelp := nil;
  HTMLHelpUser.Free;
end.

