forked from pkulchenko/wxlua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinting.wx.lua
More file actions
303 lines (246 loc) · 11.6 KB
/
printing.wx.lua
File metadata and controls
303 lines (246 loc) · 11.6 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
-----------------------------------------------------------------------------
-- Name: printing.wx.lua
-- Purpose: Printing wxLua sample
-- Author: J Winwood
-- Modified by:
-- Created: 4/7/2002
-- Modified
-- RCS-ID: $Id: printing.wx.lua,v 1.20 2011/11/16 04:40:48 jrl1 Exp $
-- Copyright: (c) 2002 J Winwood. 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")
local ID_PRINT = wx.wxID_HIGHEST + 1
local ID_PRINTPREVIEW = wx.wxID_HIGHEST + 2
local ID_PRINTSETUP = wx.wxID_HIGHEST + 3
local ID_PAGESETUP = wx.wxID_HIGHEST + 4
frame = nil
printData = wx.wxPrintData()
pageSetupDialogData = wx.wxPageSetupDialogData()
-- Setup the printer data with some useful defaults
printData:SetPaperId(wx.wxPAPER_LETTER)
pageSetupDialogData:SetMarginTopLeft(wx.wxPoint(20, 20))
pageSetupDialogData:SetMarginBottomRight(wx.wxPoint(20, 20))
function DisplayFigure(dc, pageNumber)
-- call some drawing functions
dc:SetBrush(wx.wxTRANSPARENT_BRUSH)
dc:SetPen(wx.wxRED_PEN)
dc:DrawRectangle(10, 10, 300, 300)
dc:SetPen(wx.wxBLACK_PEN)
dc:DrawRoundedRectangle(20, 20, 280, 280, 20)
dc:SetPen(wx.wxGREEN_PEN)
dc:DrawEllipse(30, 30, 260, 260)
if pageNumber then
dc:DrawText("Test page "..pageNumber, 50, 150)
else
dc:DrawText("A test string", 50, 150)
end
end
function ConnectPrintEvents(printOut)
printOut.HasPage = function(self, pageNum)
return (pageNum == 1) or (pageNum == 2)
end
-- These two functions are equivalent, you can either use SetPageInfo to
-- set the number of pages or override GetPageInfo() as shown and return
-- the number of pages
printOut:SetPageInfo(1, 16, 2, 15) -- we override this using GetPageInfo
printOut.GetPageInfo = function(self)
return 1, 2, 1, 2
end
-- You MUST call the base class functions for OnBeginDocument(...) and
-- OnEndDocument() if you override them in order for printing to work.
-- If you don't override them then printing works as expected.
printOut.OnBeginDocument = function(self, startPage, endPage)
return self:_OnBeginDocument(startPage, endPage)
end
printOut.OnEndDocument = function(self)
return self:_OnEndDocument()
end
-- You don't have to call the base class functions of these since they do
-- nothing anyway.
printOut.OnBeginPrinting = function(self)
end
printOut.OnEndPrinting = function(self)
end
printOut.OnPreparePrinting = function(self)
end
-- This is the actual function that is called for each page to print
printOut.OnPrintPage = function(self, pageNum)
local dc = self:GetDC()
local ppiScr_width, ppiScr_height = self:GetPPIScreen()
local ppiPrn_width, ppiPrn_height = self:GetPPIPrinter()
local ppi_scale_x = ppiPrn_width/ppiScr_width
local ppi_scale_y = ppiPrn_height/ppiScr_height
-- Get the size of DC in pixels and the number of pixels in the page
local dc_width, dc_height = dc:GetSize()
local pagepix_width, pagepix_height = self:GetPageSizePixels()
local dc_pagepix_scale_x = dc_width/pagepix_width
local dc_pagepix_scale_y = dc_height/pagepix_height
-- If printer pageWidth == current DC width, then this doesn't
-- change. But w might be the preview bitmap width, so scale down.
local dc_scale_x = ppi_scale_x * dc_pagepix_scale_x
local dc_scale_y = ppi_scale_y * dc_pagepix_scale_y
-- calculate the pixels / mm (25.4 mm = 1 inch)
local ppmm_x = ppiScr_width / 25.4
local ppmm_y = ppiScr_height / 25.4
-- Adjust the page size for the pixels / mm scaling factor
local pageMM_width, pageMM_height = self:GetPageSizeMM()
local pagerect_x, pagerect_y = 0, 0
local pagerect_w, pagerect_h = pageMM_width * ppmm_x, pageMM_height * ppmm_y
-- get margins informations and convert to printer pixels
local topLeft = pageSetupDialogData:GetMarginTopLeft()
local bottomRight = pageSetupDialogData:GetMarginBottomRight()
local top = topLeft:GetY() * ppmm_y
local bottom = bottomRight:GetY() * ppmm_y
local left = topLeft:GetX() * ppmm_x
local right = bottomRight:GetX() * ppmm_x
local printrect_x, printrect_y = left, top
local printrect_w, printrect_h = pagerect_w-(left+right), pagerect_h-(top+bottom)
-- finally, setup the dc scaling and origin for margins
dc:SetUserScale(dc_scale_x, dc_scale_y);
dc:SetDeviceOrigin(printrect_x*dc_scale_x, printrect_y*dc_scale_y)
-- draw our figure
DisplayFigure(dc, pageNum)
-- DON'T delete() this dc since we didn't create it
return true
end
end
function Print()
local printDialogData = wx.wxPrintDialogData(printData)
local printer = wx.wxPrinter(printDialogData)
local printout = wx.wxLuaPrintout("wxLua Test Print")
ConnectPrintEvents(printout)
if printer:Print(frame, printout, true) == false then
if printer:GetLastError() == wx.wxPRINTER_ERROR then
wx.wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?",
"Printing.wx.lua",
wx.wxOK)
else
wx.wxMessageBox("You cancelled printing",
"Printing.wx.lua",
wx.wxOK)
end
else
printData = printer:GetPrintDialogData():GetPrintData():Copy()
end
end
function PrintPreview()
local printerPrintout = wx.wxLuaPrintout("wxLua Test Print")
ConnectPrintEvents(printerPrintout)
local previewPrintout = wx.wxLuaPrintout("wxLua Test Print Preview")
ConnectPrintEvents(previewPrintout)
local printDialogData = wx.wxPrintDialogData(printData):GetPrintData()
local preview = wx.wxPrintPreview(printerPrintout, previewPrintout, printDialogData)
local result = preview:Ok()
if result == false then
wx.wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?",
"Printing.wx.lua",
wx.wxOK)
else
local previewFrame = wx.wxPreviewFrame(preview, frame,
"Test Print Preview",
wx.wxDefaultPosition,
wx.wxSize(600, 650))
previewFrame:Connect(wx.wxEVT_CLOSE_WINDOW,
function (event)
previewFrame:Destroy()
event:Skip()
end )
previewFrame:Centre(wx.wxBOTH)
previewFrame:Initialize()
previewFrame:Show(true)
end
end
function PrintSetup()
-- NOTE : This function crashes in wxWidgets GTK wxWidgets 2.8.12
-- The wxWidgets print sample doesn't even demonstate this.
local printDialogData = wx.wxPrintDialogData(printData)
local printerDialog = wx.wxPrintDialog(frame, printDialogData)
printerDialog:GetPrintDialogData():SetSetupDialog(true)
if (printerDialog:ShowModal() == wx.wxID_OK) then
printData = printerDialog:GetPrintDialogData():GetPrintData():Copy()
end
end
function PageSetup()
printData = pageSetupDialogData:GetPrintData():Copy()
local pageSetupDialog = wx.wxPageSetupDialog(frame, pageSetupDialogData)
if (pageSetupDialog:ShowModal() == wx.wxID_OK) then
printData = pageSetupDialog:GetPageSetupDialogData():GetPrintData():Copy()
pageSetupDialogData = pageSetupDialog:GetPageSetupDialogData():Copy()
end
pageSetupDialog:delete()
end
function main()
-- create the frame window
frame = wx.wxFrame( wx.NULL, wx.wxID_ANY, "wxLua Printing Demo",
wx.wxDefaultPosition, wx.wxSize(450, 450),
wx.wxDEFAULT_FRAME_STYLE )
-- paint event handler
function Paint(event)
-- create the paint DC
local dc = wx.wxPaintDC(frame)
-- clear the window
dc:SetPen(wx.wxTRANSPARENT_PEN)
dc:SetBrush(wx.wxWHITE_BRUSH)
local w, h = frame:GetClientSizeWH()
dc:DrawRectangle(0, 0, w, h)
-- draw our figure
DisplayFigure(dc)
-- the paint DC will be destroyed by the garbage collector,
-- however on Windows 9x/Me this may be too late (DC's are precious resource)
-- so delete it here
dc:delete() -- ALWAYS delete() any wxDCs created when done
end
-- connect the paint event handler with the paint event
frame:Connect(wx.wxEVT_PAINT, Paint)
-- create a simple file menu
local fileMenu = wx.wxMenu()
fileMenu:Append(ID_PAGESETUP, "Page S&etup...", "Set up the page")
--fileMenu:Append(ID_PRINTSETUP, "Print &Setup...", "Set up the printer")
fileMenu:Append(ID_PRINTPREVIEW, "Print Pre&view...", "Preview the test print")
fileMenu:Append(ID_PRINT, "&Print...", "Print the test print")
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 Printing Application")
-- 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
frame:SetMenuBar(menuBar)
-- create a simple status bar
frame:CreateStatusBar(1)
frame:SetStatusText("Welcome to wxLua.")
frame:Connect(ID_PAGESETUP, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event) PageSetup() end )
frame:Connect(ID_PRINTSETUP, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event) PrintSetup() end )
frame:Connect(ID_PRINTPREVIEW, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event) PrintPreview() end )
frame:Connect(ID_PRINT, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event) Print() 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 Printing wxLua sample.\n'..
wxlua.wxLUA_VERSION_STRING.." built with "..wx.wxVERSION_STRING,
"About wxLua",
wx.wxOK + wx.wxICON_INFORMATION,
frame )
end )
-- show the frame window
frame:Show(true)
end
main()
-- 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()