forked from pkulchenko/wxlua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlwin.wx.lua
More file actions
187 lines (156 loc) · 6.32 KB
/
htmlwin.wx.lua
File metadata and controls
187 lines (156 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
-------------------------------------------------------------------------=---
-- Name: htmlwin.wx.lua
-- Purpose: wxHtmlWindow wxLua sample
-- Author: J Winwood
-- Created: May 2002
-- Copyright: (c) 2002 Lomtick Software. All rights reserved.
-- Licence: wxWidgets licence
-------------------------------------------------------------------------=---
-- Load the wxLua module, does nothing if running from wxLua, wxLuaFreeze, or wxLuaEdit
package.cpath = package.cpath..";./?.dll;./?.so;../lib/?.so;../lib/vc_dll/?.dll;../lib/bcc_dll/?.dll;../lib/mingw_dll/?.dll;"
require("wx")
frame = nil -- the main frame of the program
html = nil -- the wxLuaHtmlWindow child of the frame (subclassed wxHtmlWindow)
htmlTextPage =
[[<html>
<head>
<title>wxLua Bound Widget demonstration</title>
</head>
<body>
<h3>wxHtmlWidgetCell demonstration</h3>
There are three bound widgets below.
<hr>
<center>
<lua text="first widget"
x=100 y=70>
</center>
<hr>
<lua text="widget with lots of text in it"
x=100 y=50>
<hr>
<lua text="widget with floating width"
float=y x=70 y=40>
</body>
</html>]]
function CreateBoundWindow(event)
local ax, ay, rc
local fl = 0
-- parse the X parameter in the custom lua tag
rc, ax = event.HtmlTag:GetParamAsInt("X")
-- parse the Y parameter
rc, ay = event.HtmlTag:GetParamAsInt("Y")
-- if there is a float tag set the float
if event.HtmlTag:HasParam("FLOAT") then
fl = ax
end
-- create the control to embed
local parent = nil
if wx.wxCHECK_VERSION(2,7,0) then
if event:GetHtmlParser() and event:GetHtmlParser():GetWindowInterface()
and event:GetHtmlParser():GetWindowInterface():GetHTMLWindow() then
parent = event:GetHtmlParser():GetWindowInterface():GetHTMLWindow()
else
print("FIXME: wxWidgets does not provide the html window for print previews?")
print("1:", event:GetHtmlParser())
print("2:", event:GetHtmlParser():GetWindowInterface())
print("3:", event:GetHtmlParser():GetWindowInterface():GetHTMLWindow())
end
else
parent = event.HtmlParser.Window
end
if parent then
local wnd = wx.wxTextCtrl( parent, wx.wxID_ANY,
event.HtmlTag:GetParam("TEXT"),
wx.wxPoint(0, 0), wx.wxSize(ax, ay),
wx.wxTE_MULTILINE )
-- show the control
wnd:Show(true)
-- create the container widget cell
local widget = wx.wxHtmlWidgetCell(wnd, fl)
-- insert the cell into the document
event.HtmlParser:OpenContainer():InsertCell(widget)
event:SetParseInnerCalled(false)
end
end
-- create the frame window
frame = wx.wxFrame( wx.NULL, wx.wxID_ANY, "wxLuaHtmlWindow Demo",
wx.wxDefaultPosition, wx.wxSize(450, 450),
wx.wxDEFAULT_FRAME_STYLE )
-- create a simple file menu
local fileMenu = wx.wxMenu()
fileMenu:Append(wx.wxID_PREVIEW, "Print Pre&view", "Preview the HTML document")
fileMenu:Append(wx.wxID_PRINT, "&Print", "Print the HTML document")
fileMenu:Append(wx.wxID_EXIT, "E&xit", "Quit the program")
-- create a simple help menu
local helpMenu = wx.wxMenu()
helpMenu:Append(wx.wxID_ABOUT, "&About", "About the wxLua wxHtmlWindow sample")
-- create a menu bar and append the file and help menus
local menuBar = wx.wxMenuBar()
menuBar:Append(fileMenu, "&File")
menuBar:Append(helpMenu, "&Help")
-- insert the menu bar into the frame using the %property binding tag (eg. SetMenubar function)
frame.MenuBar = menuBar
-- create a simple status bar
frame:CreateStatusBar(2)
frame:SetStatusText("Welcome to wxLua.")
frame:Connect(wx.wxID_PREVIEW, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event)
local printing = wx.wxHtmlEasyPrinting("HtmlWindow.wx.lua", frame)
printing:PreviewText(htmlTextPage)
end )
frame:Connect(wx.wxID_PRINT, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event)
local printing = wx.wxHtmlEasyPrinting("HtmlWindow.wx.lua", frame)
printing:PrintText(htmlTextPage)
end )
-- connect the selection event of the exit menu item to an
-- event handler that closes the window
frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event)
frame:Close(true)
end )
-- connect the selection event of the about menu item
frame:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event)
wx.wxMessageBox('This is the "About" dialog of the wxHtmlWindow wxLua sample.\n'..
wxlua.wxLUA_VERSION_STRING.." built with "..wx.wxVERSION_STRING,
"About wxLua",
wx.wxOK + wx.wxICON_INFORMATION,
frame )
end)
-- create the html window
html = wx.wxLuaHtmlWindow(frame)
-- Override the virtual function
-- virtual void wxLuaHtmlWindow::OnSetTitle(const wxString& title)
html.OnSetTitle = function(self, title)
frame.Title = frame.Title.." - "..title
end
-- when a lua custom tag is parsed in the html, this event handler
-- will be invoked
wx.wxGetApp():Connect(wx.wxID_ANY, wx.wxEVT_HTML_TAG_HANDLER,
function (event) CreateBoundWindow(event) end)
-- set the frame window and status bar
html:SetRelatedFrame(frame, "wxHtmlWindow wxLua Sample : %s")
html:SetRelatedStatusBar(1)
-- load the document
html:SetPage(htmlTextPage)
-- html:LoadPage("testpage.html")
-- Create a URL drop target, this has nothing to do with the HTML win, just a demo.
urlDropTarget = wx.wxLuaURLDropTarget()
urlDropTarget.OnData = function(self, x, y, def)
print("URL OnData ", x, y, def)
return self:_OnData(x, y, def)
end
urlDropTarget.OnDropURL = function(self, x, y, text)
print("URL OnDropURL ", x, y, text)
return true
end
html:SetDropTarget(urlDropTarget)
-- show the frame window
wx.wxGetApp().TopWindow = frame
frame:Show(true)
-- Call wx.wxGetApp():MainLoop() last to start the wxWidgets event loop,
-- otherwise the wxLua program will exit immediately.
-- Does nothing if running from wxLua, wxLuaFreeze, or wxLuaEdit since the
-- MainLoop is already running or will be started by the C++ program.
wx.wxGetApp():MainLoop()