CommonCtrl.HTMLRenderer

A simple HTML renderer

Title A simple HTML renderer
Author(s) LiXizhi
Date 2007/10/3
File script/ide/HTMLRenderer.lua

Description

It only support formatting to the following tag and their attributes: h1, h2,h3, h4, li, img(attr: src,height, width, title), a(href), anyTag(attr: style="color: #006699; left: -60px; position: relative; top: 30px;width: 100px;height: 100px"),title It also support relative image path as well as HTTP file path. In addition to per tag css, it also support global CSS via a table called css during initialization. All images are displayed on the left block, where all text are displayed in the right block. images is aligned vertically at the its normal text flow position. Note1: the HTML must use ansi encoding. Unicode or UTF8 encoding will render Chinese text unreadable. Note2: all HTML tag and attribute must use lower case. Note3: we can use tag css to position a text or image any where relative to its normal text flow. We can also specify fixed block size. Example file: script/test/testHTMLrenderer.html

TIP Sample Code

NPL.load("(gl)script/ide/HTMLRenderer.lua");
local ctl = CommonCtrl.GetControl("HTMLRenderer1");
if(not ctl) then
   ctl = CommonCtrl.HTMLRenderer:new{
      name = "HTMLRenderer1",
      alignment = "_lt",
      left=0, top=0,
      width = 512,
      height = 290,
      parent = _parent,
      source = "script/test/TestHTMLRenderer.html"
   };
end   
ctl:Show();

-- call to load another source
-- ctl:LoadFile("readme.html", true);

-- call to unload resources
-- ctl:Unload()

Member Functions

HTMLRenderer:new

 common control library
NPL.load("(gl)script/ide/commonlib.lua");
NPL.load("(gl)script/ide/common_control.lua");

 define a new control in the common control libary

 default member attributes
local HTMLRenderer = {
   -- the top level control name
   name = "HTMLRenderer1",
   -- normal window size
   alignment = "_lt",
   left = 0,
   top = 0,
   width = 512,
   height = 400, 
   -- the fixed text width. the remaining is the image bar width. By default it is 0.6 of the total width
   TextWidth = nil,
   parent = nil,
   -- HTML file path, it also support pure txt file. 
   source = nil,
   -- HREF link callback, it should be a function of type function(href) end, where href is the string. If this function is not provided, a default function is used.
   HRefLinkCallback = nil,
   -- TODO: shall we implement some css in HTML, so that we can set default h1, etc tag style
   css = {
      ["title"] = {
         scaling = 1.2,
         indent = -20,
      },
      ["h1"] = {
         scaling = 1.2,
         indent = -5,
         headimage = "Texture/unradiobox.png",
         headimagewidth = 16,
      },
      ["h2"] = {
         scaling = 1.15,
         indent = -5,
         headimage = "Texture/unradiobox.png",
         headimagewidth = 16,
      },
      ["h3"] = {
         scaling = 1.1,
         indent = -5,
         headimage = "Texture/unradiobox.png",
         headimagewidth = 16,
      },
      ["h4"] = {
         scaling = 1.1,
         indent = 0,
         headimage = "Texture/unradiobox.png",
         headimagewidth = 16,
      },
      ["li"] = {
         scaling = 1,
         indent = 3,
         headimage = "Texture/unradiobox.png",
         headimagewidth = 8,
      },
      ["Text"] = {
         scaling = 1,
         indent = -10,
      },
      ["a"] = {
         scaling = 1,
         color = "0 0 255",
      },
   },
}
CommonCtrl.HTMLRenderer = HTMLRenderer;

constructor

syntax

function HTMLRenderer:new (o)

parameters

o  

HTMLRenderer:Destroy

Destroy the UI control

syntax

function HTMLRenderer:Destroy ()

HTMLRenderer:Show

  • param bShow : boolean to show or hide. if nil, it will toggle current setting.

syntax

function HTMLRenderer:Show(bShow)

parameters

bShow boolean to show or hide. if nil, it will toggle current setting.

HTMLRenderer:LoadFile

load a file to memory and display it.

  • param bReload : true to reload

syntax

function HTMLRenderer:LoadFile(source, bReload)

parameters

source  
bReload true to reload

HTMLRenderer:Unload

unload all resources used by this ctl

syntax

function HTMLRenderer:Unload()

HTMLRenderer.BuildHTML

private: build internal treeview node to display a table.

  • param o : the current xml table
  • param treeNode : to which treeview node the content is saved to.
  • param sizeUI : the UI object to calculate size of the UI control
  • param style : nil or a table containing style{color=string, href=string}. This is a style object to be associated with each node.

syntax

function HTMLRenderer.BuildHTML(o, node, sizeUI, style)

parameters

o the current xml table
node  
sizeUI the UI object to calculate size of the UI control
style  

HTMLRenderer.HTMLGetAllTextInTable

private: retrieve and concartinate all text in the HTML table o, ignoring all tag or hierachies. e,g, "

hello LiXizhi !

" will "return hello LiXizhi !" as a string.

syntax

function HTMLRenderer.HTMLGetAllTextInTable(o)

parameters

o  

HTMLRenderer.NewStyle

private: create a new copy of style object.

  • param baseStyle : nil or a table containing styles.
  • param cssStyle : nil or a string of css style, such as "color = #006699;"
  • return __ : The new style returned will copy all attribute of baseStyle and overriding it by cssStyle

syntax

function HTMLRenderer.NewStyle(baseStyle, cssStyle)

parameters

baseStyle nil or a table containing styles.
cssStyle  
return The new style returned will copy all attribute of baseStyle and overriding it by cssStyle

HTMLRenderer.OnClose

close the given control

syntax

function HTMLRenderer.OnClose(sCtrlName)

parameters

sCtrlName  

HTMLRenderer.OnClickHRefNode

when user clicked an href node. possibly open with an external browser.

syntax

function HTMLRenderer.OnClickHRefNode(sCtrlName, href)

parameters

sCtrlName  
href  

HTMLRenderer.DrawHTMLNodeHandler

default node renderer: it display a clickable check box for expandable node, followed by node text

syntax

function HTMLRenderer.DrawHTMLNodeHandler(_parent,treeNode)

parameters

parent  
treeNode  
Topic revision: r1 - 2008-02-29 - LiXizhi
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback