/****************************************************************************/
/* WebWorks Help API Example                                                */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

/*$Id: main.c,v 1.12 2006/11/22 21:40:18 allums Exp $*/


#include "wwhelp/wwhapi.h"
#include <stdio.h>
#include <string.h>

#if defined(_WIN32)
#include <objbase.h>
#elif TARGET_OS_MAC
#if (TARGET_API_MAC_CARBON)
#include <CodeFragments.h>
#include <LaunchServices.h>
#endif
#else
#endif


/****************************************************************************/
/* main                                                                     */
/*                                                                          */
/****************************************************************************/

int
main(int   argc,
     char* argv[])
{
  int   ReturnCode;
  char  BaseHelpURL[1024];
#if defined(_WIN32)
  int   ProgramPathLength;
  char  ProgramPath[1024];
  int   bDone;
  char* pPathComponent;
#elif TARGET_OS_MAC
#else
#endif


  /* To try out WWHelp 4.0 smart URLs, set BaseHelpURL to point at a       */
  /* valid WWHelp 4.0 helpset (e.g. the sample "helpset")                  */
  /*                                                                       */
  /* Example:                                                              */
  /*   char* pBaseHelpURL = "file:///C:/wwhapi/example/helpset"            */
  /*                                                                       */
#if defined(_WIN32)
  /* Define a default path */
  /*                       */
  strcpy(BaseHelpURL, "file:///C:/wwhapi/example/helpset");

  /* Attempt to auto locate help set */
  /*                                 */
  ProgramPathLength = GetModuleFileName(NULL, ProgramPath, sizeof(ProgramPath) - 1);
  if (ProgramPathLength > 0)
  {
    /* Build URL to help set */
    /*                       */
    strcpy(BaseHelpURL, "file://");

    bDone = 0;
    ProgramPath[ProgramPathLength] = '\0';
    pPathComponent = strtok(ProgramPath, "\\");
    while (( ! bDone) &&
           (pPathComponent != NULL))
    {
      if ( ! bDone)
      {
        strcat(BaseHelpURL, "/");
        strcat(BaseHelpURL, pPathComponent);

        if (strcmp(pPathComponent, "wwhapi") == 0)
        {
          /* Found location */
          /*                */
          strcat(BaseHelpURL, "/example/helpset");

          bDone = 1;
        }
      }

      pPathComponent = strtok(NULL, "\\");
    }
  }
#elif TARGET_OS_MAC
  /* Define a default path */
  /*                       */
  strcpy(BaseHelpURL, "file:/Desktop Folder/wwhapi/example/helpset");
#else
  /* Define a default path */
  /*                       */
  strcpy(BaseHelpURL, "file:/tmp/wwhapi/example/helpset");
#endif

  /* Platform specific initializations */
  /*                                   */
#if defined(_WIN32)
  CoInitialize(NULL);
#elif TARGET_OS_MAC
#if (TARGET_API_MAC_CARBON)
  if (((UInt32)LSInit) != ((UInt32)kUnresolvedCFragSymbolAddress))
  {
    LSInit(kLSInitializeDefaults);
  }
#endif
#else
#endif

  printf("Starting...\n");
  printf("\n");

  /* Reset return code */
  /*                   */
  ReturnCode = 0;

  /* Initialize WWHelp API */
  /*                       */
  ReturnCode = WWHelp_Startup(BaseHelpURL);
  if (ReturnCode == 0)
  {
    /* Display help URLs */
    /*                   */
    if (ReturnCode == 0)
    {
      /* Delay between URLs */
      /*                    */
      printf("\n");
      printf("Press the return key to display topic \"beta\", \"section2\"...\n");
      getchar();

      ReturnCode = WWHelp_DisplayHelp("beta", "section2");
    }
    if (ReturnCode == 0)
    {
      /* Delay between URLs */
      /*                    */
      printf("\n");
      printf("Press the return key to display topic \"alpha\", \"section2\"...\n");
      getchar();

      ReturnCode = WWHelp_DisplayHelpWithNavigation("alpha", "section2");
    }
    if (ReturnCode == 0)
    {
      /* Delay between URLs */
      /*                    */
      printf("\n");
      printf("Press the return key to display topic \"beta\", \"section1\"...\n");
      getchar();

      ReturnCode = WWHelp_DisplayHelp("beta", "section1");
    }

    printf("\n");
    printf("Press the return key to exit...\n");
    getchar();

    /* Shutdown WWHelp API */
    /*                     */
    WWHelp_Shutdown();
  }

  /* Platform specific cleanup */
  /*                           */
#if defined(_WIN32)
  CoUninitialize();
#elif TARGET_OS_MAC
#if (TARGET_API_MAC_CARBON)
  if (((UInt32)LSTerm) != ((UInt32)kUnresolvedCFragSymbolAddress))
  {
    LSTerm();
  }
#endif
#else
#endif

  return ReturnCode;
}
