Click here to Skip to main content
15,892,005 members
Articles / Desktop Programming / WTL

WTL integration of Lightweight HTML layout and rendering engine

Rate me:
Please Sign up or sign in to vote.
3.67/5 (32 votes)
10 Oct 20034 min read 255.8K   4.9K   62  
WTL integration of Lightweight HTML layout and rendering engine
//
// Terra Informatica Lightweight Embeddable HTMLayout control
// http://terra-informatica.org/htmlayout
//
// The code and information provided "as-is" without
// warranty of any kind, either expressed or implied.
//
// (C) 2003, Andrew Fedoniouk (andrew@terra-informatica.org)
//

#ifndef __htmlayout_h__
#define __htmlayout_h__

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#ifndef _LPCBYTE_DEFINED
#define _LPCBYTE_DEFINED
typedef const BYTE *LPCBYTE;
#endif

#ifdef __cplusplus
#define EXTERN_C extern "C"
#else 
#define EXTERN_C extern
#endif /* __cplusplus */

#ifdef  HTMLAYOUT_EXPORTS
#define HLAPI __declspec(dllexport) __stdcall
#else
#define HLAPI __declspec(dllimport) __stdcall
#endif

//|
//| HTMLayoutClassName() will return string - name of  
//| HtmLayout window class. Use this string in CreateWindow(Ex)  
//|

EXTERN_C LPCSTR  HLAPI HTMLayoutClassName();
EXTERN_C LPCWSTR HLAPI HTMLayoutClassNameW();

#ifdef UNICODE
#define HTMLayoutClassNameT  HTMLayoutClassNameW
#else
#define HTMLayoutClassNameT  HTMLayoutClassName
#endif // !UNICODE


//|
//| notification callback interface
//| this make sense only for C++
//|
#ifdef __cplusplus
typedef struct tagHTMLAYOUT_CALLBACK
{
  // htmlayout will call this function on  
  // NMHL_CREATE_CONTROL, NMHL_HYPERLINK and NMHL_LOAD_URI_DATA notifications 
  virtual LRESULT __stdcall notification(UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;
  
  // htmlayout will call this function on  
  // all WM_NOTIFY and WM_COMMAND comming from its' child controls 
  virtual LRESULT __stdcall child_ctl_notification(UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;
  
  // windowless controls support. (not available in current) 
  virtual LRESULT __stdcall windowless_ctl_handler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) = 0;

} HTMLAYOUT_CALLBACK;
#endif


//| 
//| HtmLayout specific window message id. 
//| Do HtmLayout action through SendMessage(hwnd, WM_HL_ACTION, wParam, lParam) call.
//|
//| wParam - is code of action to perform
//|
#define WM_HL_ACTION (WM_USER + 0xAF1)
//|
//| wParam value is a type of action to perform
//|
#define WP_HL_ACTION_LOAD_HTML    0x01 // lParam - (const unsigned char *) html text (zero terminated)
#define WP_HL_ACTION_OPEN_FILE    0x02 // lParam - (const TCHAR *) file path (zero terminated)

#define WP_HL_ACTION_SET_CALLBACK 0x03 // lParam - (const HTMLAYOUT_CALLBACK *)
                                       //    address of HTMLAYOUT_CALLBACK structure (interface)
#define WP_HL_ACTION_GET_MIN_DOCUMENT_WIDTH   0x04 // lParam - N/A
#define WP_HL_ACTION_GET_MIN_DOCUMENT_HEIGHT  0x05 // lParam - desired width

#define WP_HL_ACTION_FORCE_RELAYOUT  0x06 // lParam - N/A, Force relayout of html. 


//|
//|   HtmLayout notifications.
//|   The same principle as for Windows Common Controls notifications. 
//|

#define HLN_CREATE_CONTROL  0x01 // lParam is LPNMHL_CREATE_CONTROL 
#define HLN_HYPERLINK       0x02 // lParam is LPNMHL_HYPERLINK
#define HLN_LOAD_DATA       0x03 // lParam is LPNMHL_LOAD_DATA
#define HLN_CONTROL_CREATED 0x04 // lParam is LPNMHL_CREATE_CONTROL 
// HtmLayout will send HLN_CONTROL_CREATED notification as indicator
// that control creation process completed. 
// Host application could set e.g. GWL_ID to the control using its own policy. 
 

//|
//| HtmLayout will send this notification to the parent window  
//| for each <INPUT>, <TEXTAREA>, <SELECT> or <WIDGET> tag encountered in input html.
//| Parent should set into outControlHwnd either HWND of created control in LRESULT or one of:
//| HWND_TRY_DEFAULT - for creating "standard html controls" 
//| HWND_DISCARD_CREATION - ignore this control tag at all

#define HWND_TRY_DEFAULT      0
#define HWND_DISCARD_CREATION 1

typedef struct tagNMHL_CREATE_CONTROL
{
    NMHDR     hdr;
    LPCWSTR   type;     //zero terminated string, type attribute value, e.g. "text","textarea","checkbox"
    LPCWSTR   name;     //zero terminated string, name attribute value
    LPCWSTR   id;       //zero terminated string, id attribute value
    int       tagType;  // 0-input, 1-textarea, 2-select, 3-widget 

    LPCSTR*   attributeNames;   // vector of pointers to attribute names (char strings) 
    LPCWSTR*  attributeValues;  // vector of pointers to attribute values (wchar_t strings) 
    int       attributeCount;
    //<SELECT> and <WIDGET> collection of <OPTION>'s or <PARAM>'s
    LPCWSTR*  optionNames;      // vector of pointers to param names (wchar_t strings) 
    LPCWSTR*  optionValues;     // vector of pointers to param values (wchar_t strings) 
    int       optionCount;
    
    HWND      outControlHwnd;   // 

    DWORD     reserved1;
    DWORD     reserved2;
    DWORD     reserved3;

} NMHL_CREATE_CONTROL, FAR *LPNMHL_CREATE_CONTROL;

//|
//| Hyperlink notifications
//|

#define HL_HYPERLINK_LEAVE 0
#define HL_HYPERLINK_ENTER 1
#define HL_HYPERLINK_CLICK 2

typedef struct tagNMHL_HYPERLINK
{

    NMHDR   hdr;
    LPCWSTR name;   // zero terminated name string
    LPCWSTR href;   // zero terminated URI string
    LPCWSTR target; // zero terminated target string
    DWORD   status; // status, one of HL_HYPERLINK_LEAVE, HL_HYPERLINK_ENTER, HL_HYPERLINK_CLICK

    DWORD   reserved1;
    DWORD   reserved2;
    DWORD   reserved3;

} NMHL_HYPERLINK, FAR *LPNMHL_HYPERLINK;

//|
//| Resource loading requests
//| HtmLayout will send this structure in WM_NOTIFY/HLN_LOAD_DATA notification for loading images.
//| 
//| If host window cannot load resource with the given name it should 
//| left outData and outDataSize untouched and return in LRESULT one of:
//| 
//| LOAD_TRY_DEFAULT to proceed with default loader
//| LOAD_DISCARD to cancel loading at all
//| 

#define LOAD_TRY_DEFAULT  0
#define LOAD_DISCARD      1
#define LOAD_DATA_VALID   2

typedef struct tagNMHL_LOAD_DATA
{
    NMHDR   hdr;
    LPCWSTR uri;          //zero terminated string, fully qualified uri, e.g. "http://server/folder/file.ext" 

    LPVOID  outData;      // out, pointer to loaded data 
    DWORD   outDataSize;  // out, loaded data size
                          // (HtmLayout will not store outData pointer internally) 
                          // You may use HTMLayoutDataReady call instead of filling these fields.
                          // Slightly slower but allows you to destroy yours outData buffer immediately
                          // after HTMLayoutDataReady call.
    DWORD   reserved1;

} NMHL_LOAD_DATA, FAR *LPNMHL_LOAD_DATA;

//|
//| This function is used in response if WM_NOTIFY/HLN_LOAD_DATA request. 
//| WARNING: If used, call of this function MUST be done ONLY while handling 
//| WM_NOTIFY/HLN_LOAD_DATA request and in the same thread. HtmLayout v.1.0 does not
//| support asynchronous resource loading. 
//|
//| parameters:
//|
//| HWND hwnd - HtmLayout window handle 
//| LPCWSTR uri - URI of the data requested by HtmLayout
//| LPBYTE data - pointer to data buffer 
//| DWORD dataLength - length of the data in bytes
//|
//| return values:
//| TRUE - if HtmLayout accepts the data
//| FALSE - otherwise. E.g. this function was called outside of WM_NOTIFY/HLN_LOAD_DATA request. 
//|

EXTERN_C BOOL HLAPI HTMLayoutDataReady(HWND hwnd,LPCWSTR uri,LPBYTE data, DWORD dataLength);

//|
//| HtmLayout WndProc
//|
EXTERN_C LRESULT HLAPI HTMLayoutProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

//|
//| HtmLayout GetDlgItemByName 
//|
//| Get HWND of the child control by its name patern
//|
//| parameters:
//|
//| HWND hwndHtmLayout - HtmLayout window handle 
//| LPCTSTR lpszNamePattern - name pattern of group of controls.
//| DWORD index - index of the control in the group
//|
//| Name Pattern explanation - see below.
//|
//| Example:
//| for the HTML: "<INPUT type=text name=ClientName0> and <INPUT type=text name=ClientName1>"
//| GetDlgItemByName("ClientName*",1) will return HWND of ClientName1 text box
//| GetDlgItemByName("ClientName1",0) will return HWND of ClientName1 text box
//|

EXTERN_C HWND HLAPI HTMLayoutGetDlgItemByNameA(HWND hwndHtmLayout, LPCSTR lpszNamePattern, DWORD index);
EXTERN_C HWND HLAPI HTMLayoutGetDlgItemByNameW(HWND hwndHtmLayout, LPCWSTR lpszNamePattern, DWORD index);
#ifdef UNICODE 
  #define GetDlgItemByName HTMLayoutGetDlgItemByNameW
#else
  #define GetDlgItemByName HTMLayoutGetDlgItemByNameA
#endif

//|
//| HtmLayout GetDlgNameItemsCount
//|
//| Get number of child controls which names match given name patern
//|
//| parameters:
//|
//| HWND hwndHtmLayout - HtmLayout window handle 
//| LPCTSTR lpszNamePattern - name pattern of group of controls.
//|
//| Name Pattern explanation - see below.
//|
//| Example:
//| for the HTML: "<INPUT type=text name=ClientName0> and <INPUT type=text name=ClientName1>"
//| GetDlgNameItemsCount("ClientName*") will return 2
//| GetDlgNameItemsCount("ClientName1") will return 1
//|
EXTERN_C DWORD HLAPI HTMLayoutGetDlgNameItemsCountA(HWND hwndHtmLayout, LPCSTR lpszNamePattern);
EXTERN_C DWORD HLAPI HTMLayoutGetDlgNameItemsCountW(HWND hwndHtmLayout, LPCWSTR lpszNamePattern);
#ifdef UNICODE 
  #define GetDlgNameItemsCount HTMLayoutGetDlgNameItemsCountW
#else
  #define GetDlgNameItemsCount HTMLayoutGetDlgNameItemsCountA
#endif

//|
//| Name Patterns Explanation
//|
//| pattern chars:
//|  '*' - any substring
//|  '?' - any one char
//|  '['char set']' = any one char in set
//|    e.g.  [a-z] - all lowercase letters
//|          [a-zA-Z] - all letters
//|          [abd-z] - all lowercase letters except of 'c'
//|          [-a-z] - all lowercase letters and '-'
//|  any other character is literal 
//|
//|  to match characters '*', '?', '[', ']' use [*] , [?], [[] and []] sequences.
//|



//|
//| HtmLayout HTMLayoutGetDlgItemName
//|
//| Get name of child widget window.
//|
//| parameters:
//|
//| HWND hwndHtmLayout - HtmLayout window handle 
//| LPCTSTR lpszNamePattern - name pattern of group of controls.
//|
//| return values:
//| NULL - if there are no such hwnd among HtmLayout widgets
//| "" (empty string) - if hwndChild exists but has no name attribute set
//| otherwise - zero terminated string - name of the widget as given in html
//|

EXTERN_C LPCSTR HLAPI HTMLayoutGetDlgItemNameA(HWND hwndHtmLayout, HWND hwndChild);
EXTERN_C LPCWSTR HLAPI HTMLayoutGetDlgItemNameW(HWND hwndHtmLayout, HWND hwndChild);

#ifdef UNICODE
#define HTMLayoutGetDlgItemNameT  HTMLayoutGetDlgItemNameW
#else
#define HTMLayoutGetDlgItemNameT  HTMLayoutGetDlgItemNameA
#endif // !UNICODE



//|
//| HtmLayout HTMLayoutPrint - simple print function
//|

//| parameters:
//|   paperType - DEVMODE.dmPaperSize 
//|   paperOrientation - DEVMODE.dmOrientation
//|   paperSource - DEVMODE.dmDefaultSource
//|   printOptions - DEVMODE.dmDuplex
//|   margin* - text margins in MM_HIENGLISH units
//|   scaledPageWidth - number of screen pixels to scale. 
//|                     E.g. value 800 - will emulate 800 pixels width screen on the printer.
//|   text and textSize - html text buffer.
//|   baseUri - is the base URI for resolving relative links to images. Could be NULL.

EXTERN_C BOOL HLAPI HTMLayoutPrintA
(
  LPCSTR      printerName, 
  int         paperType, 
  int         paperSource, 
  int         paperOrientation, 
  int         printOptions,
  int         marginLeft,
  int         marginTop,
  int         marginRight,
  int         marginBottom,
  int         scaledPageWidth,
  const LPBYTE text,
  DWORD        textSize,
  LPCSTR       baseUri
); 

EXTERN_C BOOL HLAPI HTMLayoutPrintW
(
  LPCWSTR     printerName, 
  int         paperType, 
  int         paperSource, 
  int         paperOrientation, 
  int         printOptions,
  int         marginLeft,
  int         marginTop,
  int         marginRight,
  int         marginBottom,
  int         scaledPageWidth,
  const LPBYTE text,
  DWORD        textSize,
  LPCWSTR      baseUri
);

#ifdef UNICODE 
  #define HTMLayoutPrint HTMLayoutPrintW
#else
  #define HTMLayoutPrint HTMLayoutPrintA
#endif


#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder Terra Informatica Software
Canada Canada
Andrew Fedoniouk.

MS in Physics and Applied Mathematics.
Designing software applications and systems since 1991.

W3C HTML5 Working Group, Invited Expert.

Terra Informatica Software, Inc.
http://terrainformatica.com

Comments and Discussions