1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wxLuaPrinting.cpp
3 // Purpose:     Provide an interface to wxPrintout for wxLua.
4 // Author:      J. Winwood.
5 // Created:     July 2002.
6 // Copyright:   (c) 2002 Lomtick Software. All rights reserved.
7 // Licence:     wxWidgets licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 #include "wx/wxprec.h"
11 
12 #ifdef __BORLANDC__
13     #pragma hdrstop
14 #endif
15 
16 #ifndef WX_PRECOMP
17     #include "wx/wx.h"
18 #endif
19 
20 #include "wx/datetime.h"
21 
22 #include "wxbind/include/wxcore_wxlcore.h"
23 #include "wxbind/include/wxcore_bind.h" // for wxLua_wxObject_wxSize
24 
25 // ----------------------------------------------------------------------------
26 // wxLuaDataObjectSimple
27 // ----------------------------------------------------------------------------
28 
29 #if wxLUA_USE_wxDataObject && wxUSE_DATAOBJ
30 
31 // This lua tag is defined in bindings
32 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaDataObjectSimple;
33 
wxLuaDataObjectSimple(const wxLuaState & wxlState,const wxDataFormat & format)34 wxLuaDataObjectSimple::wxLuaDataObjectSimple(const wxLuaState& wxlState,
35                                              const wxDataFormat& format)
36                       :wxDataObjectSimple(format)
37 {
38     m_wxlState = wxlState;
39 }
40 
GetDataSize() const41 size_t wxLuaDataObjectSimple::GetDataSize() const
42 {
43     size_t result = 0;
44 
45     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
46         m_wxlState.HasDerivedMethod(this, "GetDataSize", true))
47     {
48         int nOldTop = m_wxlState.lua_GetTop();
49         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaDataObjectSimple, true);
50 
51         if (m_wxlState.LuaPCall(1, 1) == 0)
52             result = m_wxlState.GetNumberType(-1);
53 
54         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
55     }
56     else
57         result = wxDataObjectSimple::GetDataSize();
58 
59     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
60 
61     return result;
62 }
63 
GetDataHere(void * buf) const64 bool wxLuaDataObjectSimple::GetDataHere(void* buf) const
65 {
66     bool result = false;
67 
68     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
69         m_wxlState.HasDerivedMethod(this, "GetDataHere", true))
70     {
71         int nOldTop = m_wxlState.lua_GetTop();
72         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaDataObjectSimple, true);
73 
74         if (m_wxlState.LuaPCall(0, 2) == 0)
75         {
76             result = m_wxlState.GetBooleanType(-2);
77 
78             const void *lua_buf = m_wxlState.lua_ToString(-1);
79             size_t len = (size_t)m_wxlState.lua_StrLen(-1);
80 
81             memcpy(buf, lua_buf, len);
82         }
83 
84         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
85     }
86     else
87         result = wxDataObjectSimple::GetDataHere(buf);
88 
89     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
90 
91     return result;
92 }
93 
SetData(size_t len,const void * buf)94 bool wxLuaDataObjectSimple::SetData(size_t len, const void* buf)
95 {
96     bool result = false;
97 
98     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
99         m_wxlState.HasDerivedMethod(this, "SetData", true))
100     {
101         int nOldTop = m_wxlState.lua_GetTop();
102         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaDataObjectSimple, true);
103         m_wxlState.lua_PushLString((const char*)buf, len);
104 
105         if (m_wxlState.LuaPCall(2, 1) == 0)
106             result = m_wxlState.GetBooleanType(-1);
107 
108         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
109     }
110     else
111         result = wxDataObjectSimple::SetData(len, buf);
112 
113     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
114 
115     return result;
116 }
117 
118 #endif //wxLUA_USE_wxDataObject && wxUSE_DATAOBJ
119 
120 // ----------------------------------------------------------------------------
121 // wxLuaFileDropTarget
122 // ----------------------------------------------------------------------------
123 
124 #if wxLUA_USE_wxDataObject && wxUSE_DRAG_AND_DROP
125 
126 // This lua tag is defined in bindings
127 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaFileDropTarget;
128 
wxLuaFileDropTarget(const wxLuaState & wxlState)129 wxLuaFileDropTarget::wxLuaFileDropTarget(const wxLuaState& wxlState)
130                     :wxFileDropTarget()
131 {
132     m_wxlState = wxlState;
133 }
134 
OnDropFiles(wxCoord x,wxCoord y,const wxArrayString & filenames)135 bool wxLuaFileDropTarget::OnDropFiles(wxCoord x, wxCoord y,
136                                       const wxArrayString& filenames)
137 {
138     bool result = false;
139 
140     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
141         m_wxlState.HasDerivedMethod(this, "OnDropFiles", true))
142     {
143         int nOldTop = m_wxlState.lua_GetTop();
144         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaFileDropTarget, true);
145         m_wxlState.lua_PushInteger(x);
146         m_wxlState.lua_PushInteger(y);
147         m_wxlState.PushwxArrayStringTable(filenames);
148 
149         if (m_wxlState.LuaPCall(4, 1) == 0)
150             result = m_wxlState.GetBooleanType(-1);
151 
152         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
153     }
154     //else - do nothing, the base class function is pure virtual
155     //    result = wxFileDropTarget::OnDropFiles(x, y, filenames);
156 
157     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
158 
159     return result;
160 }
161 
OnData(wxCoord x,wxCoord y,wxDragResult def)162 wxDragResult wxLuaFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
163 {
164     wxDragResult result = wxDragNone;
165 
166     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
167         m_wxlState.HasDerivedMethod(this, "OnData", true))
168     {
169         int nOldTop = m_wxlState.lua_GetTop();
170         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaFileDropTarget, true);
171         m_wxlState.lua_PushInteger(x);
172         m_wxlState.lua_PushInteger(y);
173         m_wxlState.lua_PushInteger(def);
174 
175         if (m_wxlState.LuaPCall(4, 1) == 0)
176             result = (wxDragResult)m_wxlState.GetIntegerType(-1);
177 
178         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
179     }
180     else
181         result = wxFileDropTarget::OnData(x, y, def);
182 
183     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
184 
185     return result;
186 }
187 
188 #endif //wxLUA_USE_wxDataObject && wxUSE_DRAG_AND_DROP
189 
190 // ----------------------------------------------------------------------------
191 // wxLuaFileDropTarget
192 // ----------------------------------------------------------------------------
193 
194 #if wxLUA_USE_wxDataObject && wxUSE_DRAG_AND_DROP
195 
196 // This lua tag is defined in bindings
197 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaTextDropTarget;
198 
wxLuaTextDropTarget(const wxLuaState & wxlState)199 wxLuaTextDropTarget::wxLuaTextDropTarget(const wxLuaState& wxlState)
200                     :wxTextDropTarget()
201 {
202     m_wxlState = wxlState;
203 }
204 
OnDropText(wxCoord x,wxCoord y,const wxString & text)205 bool wxLuaTextDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& text)
206 {
207     bool result = false;
208 
209     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
210         m_wxlState.HasDerivedMethod(this, "OnDropText", true))
211     {
212         int nOldTop = m_wxlState.lua_GetTop();
213         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaTextDropTarget, true);
214         m_wxlState.lua_PushInteger(x);
215         m_wxlState.lua_PushInteger(y);
216         m_wxlState.lua_PushString(wx2lua(text));
217 
218         if (m_wxlState.LuaPCall(4, 1) == 0)
219             result = m_wxlState.GetBooleanType(-1);
220 
221         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
222     }
223     //else - do nothing, the base class function is pure virtual
224     //    result = wxTextDropTarget::OnDropText(x, y, text);
225 
226     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
227 
228     return result;
229 }
230 
OnData(wxCoord x,wxCoord y,wxDragResult def)231 wxDragResult wxLuaTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
232 {
233     wxDragResult result = wxDragNone;
234 
235     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
236         m_wxlState.HasDerivedMethod(this, "OnData", true))
237     {
238         int nOldTop = m_wxlState.lua_GetTop();
239         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaTextDropTarget, true);
240         m_wxlState.lua_PushInteger(x);
241         m_wxlState.lua_PushInteger(y);
242         m_wxlState.lua_PushInteger(def);
243 
244         if (m_wxlState.LuaPCall(4, 1) == 0)
245             result = (wxDragResult)m_wxlState.GetIntegerType(-1);
246 
247         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
248     }
249     else
250         result = wxTextDropTarget::OnData(x, y, def);
251 
252     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
253 
254     return result;
255 }
256 
OnEnter(wxCoord x,wxCoord y,wxDragResult def)257 wxDragResult wxLuaTextDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def)
258 {
259     wxDragResult result = wxDragNone;
260 
261     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
262         m_wxlState.HasDerivedMethod(this, "OnEnter", true))
263     {
264         int nOldTop = m_wxlState.lua_GetTop();
265         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaTextDropTarget, true);
266         m_wxlState.lua_PushInteger(x);
267         m_wxlState.lua_PushInteger(y);
268         m_wxlState.lua_PushInteger(def);
269 
270         if (m_wxlState.LuaPCall(4, 1) == 0)
271             result = (wxDragResult)m_wxlState.GetIntegerType(-1);
272 
273         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
274     }
275     else
276     {
277         // do nothing if function is not set in Lua
278     }
279 
280     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
281 
282     return result;
283 }
284 
OnLeave()285 void wxLuaTextDropTarget::OnLeave()
286 {
287     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
288         m_wxlState.HasDerivedMethod(this, "OnLeave", true))
289     {
290         int nOldTop = m_wxlState.lua_GetTop();
291         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaTextDropTarget, true);
292 
293         if (m_wxlState.LuaPCall(1, 0) == 0)
294         {
295             // All is OK, do nothing
296         }
297 
298         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
299     }
300     else
301     {
302         // do nothing if function is not set in Lua
303     }
304 
305     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
306 }
307 
OnDragOver(wxCoord x,wxCoord y,wxDragResult def)308 wxDragResult wxLuaTextDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def)
309 {
310     wxDragResult result = wxDragNone;
311 
312     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
313         m_wxlState.HasDerivedMethod(this, "OnDragOver", true))
314     {
315         int nOldTop = m_wxlState.lua_GetTop();
316         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaTextDropTarget, true);
317         m_wxlState.lua_PushInteger(x);
318         m_wxlState.lua_PushInteger(y);
319         m_wxlState.lua_PushInteger(def);
320 
321         if (m_wxlState.LuaPCall(4, 1) == 0)
322             result = (wxDragResult)m_wxlState.GetIntegerType(-1);
323 
324         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
325     }
326     else
327         result = wxTextDropTarget::OnDragOver(x, y, def);
328 
329     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
330 
331     return result;
332 }
333 
334 #endif //wxLUA_USE_wxDataObject && wxUSE_DRAG_AND_DROP
335 
336 
337 // ----------------------------------------------------------------------------
338 // wxLuaURLDropTarget
339 // ----------------------------------------------------------------------------
340 
341 #if wxLUA_USE_wxDataObject && wxUSE_DRAG_AND_DROP
342 
343 // This lua tag is defined in bindings
344 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaURLDropTarget;
345 
wxLuaURLDropTarget(const wxLuaState & wxlState)346 wxLuaURLDropTarget::wxLuaURLDropTarget(const wxLuaState& wxlState)
347                    :wxDropTarget()
348 {
349     SetDataObject(new wxURLDataObject);
350     m_wxlState = wxlState;
351 }
352 
OnDropURL(wxCoord x,wxCoord y,const wxString & text)353 bool wxLuaURLDropTarget::OnDropURL(wxCoord x, wxCoord y, const wxString& text)
354 {
355     bool result = false;
356 
357     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
358         m_wxlState.HasDerivedMethod(this, "OnDropURL", true))
359     {
360         int nOldTop = m_wxlState.lua_GetTop();
361         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaURLDropTarget, true);
362         m_wxlState.lua_PushInteger(x);
363         m_wxlState.lua_PushInteger(y);
364         m_wxlState.lua_PushString(wx2lua(text));
365 
366         if (m_wxlState.LuaPCall(4, 1) == 0)
367             result = m_wxlState.GetBooleanType(-1);
368 
369         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
370     }
371     //else - do nothing, there is no base class function
372 
373     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
374 
375     return result;
376 }
377 
OnData(wxCoord x,wxCoord y,wxDragResult def)378 wxDragResult wxLuaURLDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
379 {
380     wxDragResult result = wxDragNone;
381 
382     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
383         m_wxlState.HasDerivedMethod(this, "OnData", true))
384     {
385         int nOldTop = m_wxlState.lua_GetTop();
386         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaURLDropTarget, true);
387         m_wxlState.lua_PushInteger(x);
388         m_wxlState.lua_PushInteger(y);
389         m_wxlState.lua_PushInteger(def);
390 
391         if (m_wxlState.LuaPCall(4, 1) == 0)
392             result = (wxDragResult)m_wxlState.GetIntegerType(-1);
393 
394         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
395     }
396     else
397     {
398         // result = wxDropTarget::OnData(x, y, def); this is pure virtual
399 
400         if ( !GetData() )
401             return wxDragNone;
402 
403         m_wxlState.SetCallBaseClassFunction(false); // clear flag before next virtual call
404 
405         wxURLDataObject *dobj = (wxURLDataObject *)m_dataObject;
406         return OnDropURL( x, y, dobj->GetURL() ) ? def : wxDragNone;
407     }
408 
409     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
410 
411     return result;
412 }
413 
414 #endif //wxLUA_USE_wxDataObject && wxUSE_DRAG_AND_DROP
415 
416 // ----------------------------------------------------------------------------
417 // wxLuaPrintout
418 // ----------------------------------------------------------------------------
419 
420 #if wxLUA_USE_wxLuaPrintout
421 
422 // This lua tag is defined in bindings
423 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaPrintout;
424 
425 int wxLuaPrintout::ms_test_int = -1;
426 
IMPLEMENT_ABSTRACT_CLASS(wxLuaPrintout,wxPrintout)427 IMPLEMENT_ABSTRACT_CLASS(wxLuaPrintout, wxPrintout)
428 
429 wxLuaPrintout::wxLuaPrintout(const wxLuaState& wxlState,
430                              const wxString& title, wxLuaObject *pObject)
431               :wxPrintout(title), m_wxlState(wxlState), m_pObject(pObject),
432                 m_minPage(0), m_maxPage(0), m_pageFrom(0), m_pageTo(0)
433 {
434 }
435 
SetPageInfo(int minPage,int maxPage,int pageFrom,int pageTo)436 void wxLuaPrintout::SetPageInfo(int minPage, int maxPage, int pageFrom, int pageTo)
437 {
438     m_minPage  = minPage;
439     m_maxPage  = maxPage;
440     m_pageFrom = pageFrom;
441     m_pageTo   = pageTo;
442 }
443 
GetPageInfo(int * minPage,int * maxPage,int * pageFrom,int * pageTo)444 void wxLuaPrintout::GetPageInfo(int *minPage, int *maxPage, int *pageFrom, int *pageTo)
445 {
446     *minPage = *maxPage = *pageFrom = *pageTo = 0;
447 
448     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
449         m_wxlState.HasDerivedMethod(this, "GetPageInfo", true))
450     {
451         int nOldTop = m_wxlState.lua_GetTop();
452         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
453 
454         if (m_wxlState.LuaPCall(1, 4) == 0)
455         {
456             *minPage  = (int)m_wxlState.GetNumberType(-4);
457             *maxPage  = (int)m_wxlState.GetNumberType(-3);
458             *pageFrom = (int)m_wxlState.GetNumberType(-2);
459             *pageTo   = (int)m_wxlState.GetNumberType(-1);
460         }
461 
462         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
463     }
464     else
465     {
466         *minPage  = m_minPage;
467         *maxPage  = m_maxPage;
468         *pageFrom = m_pageFrom;
469         *pageTo   = m_pageTo;
470     }
471 
472     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
473 }
474 
HasPage(int pageNum)475 bool wxLuaPrintout::HasPage(int pageNum)
476 {
477     bool fResult = false;
478 
479     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
480         m_wxlState.HasDerivedMethod(this, "HasPage", true))
481     {
482         int nOldTop = m_wxlState.lua_GetTop();
483         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
484         m_wxlState.lua_PushNumber(pageNum);
485 
486         if (m_wxlState.LuaPCall(2, 1) == 0)
487             fResult = m_wxlState.GetBooleanType(-1);
488 
489         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
490     }
491     else
492         fResult = wxPrintout::HasPage(pageNum);
493 
494     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
495 
496     return fResult;
497 }
498 
499 // Notes about virtual functions:
500 //
501 // This is the call list using the wxPrintf statements in wxLuaPrintout::OnBeginDocument
502 //    for the wxLua code (see printing.wx.lua sample for complete listing)
503 //
504 //    previewPrintout = wxLuaPrintout("Test print")
505 //    ...
506 //    previewPrintout.OnBeginDocument = function(self, startPage, endPage)
507 //                                   return self:base_OnBeginDocument(startPage, endPage)
508 //                               end
509 //    ...
510 //    local preview = wx.wxPrintPreview(printerPrintout, previewPrintout, printDialogData)
511 //
512 // wxLuaPrintout::OnBeginDocument 1 call base 0
513 // wxlua_getTableFunc func 'base_OnBeginDocument' pClass -1220355700 'wxLuaPrintout', userdata 1, lightuserdata 0, ttag 207, class_tag 207 lua_State 139252808 wxLuaStateRefData 139155808 call base 1
514 // wxLua_wxPrintout_OnBeginDocument 1 (this is the wxLua binding function for wxPrintout::OnBeginDocument)
515 // wxLuaPrintout::OnBeginDocument 1 call base 1
516 // wxLuaPrintout::OnBeginDocument 3 call base 1
517 // wxPrintout::OnBeginDocument (this is the call to the wxWidgets function in its library)
518 // wxLuaPrintout::OnBeginDocument 4 call base 1
519 // wxLuaPrintout::OnBeginDocument 2 call base 0
520 // wxLuaPrintout::OnBeginDocument 4 call base 0
521 
OnBeginDocument(int startPage,int endPage)522 bool wxLuaPrintout::OnBeginDocument(int startPage, int endPage)
523 {
524     // NOTE: The wxLua program MUST call the base class, see printing.wx.lua
525     bool fResult = true;
526 
527     //wxPrintf(wxT("wxLuaPrintout::OnBeginDocument 1 call base %d\n"), m_wxlState.GetCallBaseClassFunction());
528 
529     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
530         m_wxlState.HasDerivedMethod(this, "OnBeginDocument", true))
531     {
532         int nOldTop = m_wxlState.lua_GetTop();
533         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
534         m_wxlState.lua_PushNumber(startPage);
535         m_wxlState.lua_PushNumber(endPage);
536 
537         if (m_wxlState.LuaPCall(3, 1) == 0)
538             fResult = m_wxlState.GetBooleanType(-1);
539 
540         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
541         //wxPrintf(wxT("wxLuaPrintout::OnBeginDocument 2 call base %d\n"), m_wxlState.GetCallBaseClassFunction());
542     }
543     else
544     {
545         //wxPrintf(wxT("wxLuaPrintout::OnBeginDocument 3 call base %d\n"), m_wxlState.GetCallBaseClassFunction());
546         fResult = wxPrintout::OnBeginDocument(startPage, endPage);
547     }
548 
549     //wxPrintf(wxT("wxLuaPrintout::OnBeginDocument 4 call base %d\n"), m_wxlState.GetCallBaseClassFunction());
550 
551     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
552 
553     return fResult;
554 }
555 
OnEndDocument()556 void wxLuaPrintout::OnEndDocument()
557 {
558     // NOTE: The wxLua program MUST call the base class, see printing.wx.lua
559     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
560         m_wxlState.HasDerivedMethod(this, "OnEndDocument", true))
561     {
562         int nOldTop = m_wxlState.lua_GetTop();
563         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
564         m_wxlState.LuaPCall(1, 0);
565         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
566     }
567     else
568         wxPrintout::OnEndDocument();
569 
570     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
571 }
572 
OnBeginPrinting()573 void wxLuaPrintout::OnBeginPrinting()
574 {
575     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
576         m_wxlState.HasDerivedMethod(this, "OnBeginPrinting", true))
577     {
578         int nOldTop = m_wxlState.lua_GetTop();
579         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
580         m_wxlState.LuaPCall(1, 0);
581         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
582     }
583     else
584         wxPrintout::OnBeginPrinting();
585 
586     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
587 }
588 
OnEndPrinting()589 void wxLuaPrintout::OnEndPrinting()
590 {
591     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
592         m_wxlState.HasDerivedMethod(this, "OnEndPrinting", true))
593     {
594         int nOldTop = m_wxlState.lua_GetTop();
595         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
596         m_wxlState.LuaPCall(1, 0);
597         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
598     }
599     else
600         wxPrintout::OnEndPrinting();
601 
602     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
603 }
604 
OnPreparePrinting()605 void wxLuaPrintout::OnPreparePrinting()
606 {
607     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
608         m_wxlState.HasDerivedMethod(this, "OnPreparePrinting", true))
609     {
610         int nOldTop = m_wxlState.lua_GetTop();
611         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
612         m_wxlState.LuaPCall(1, 0);
613         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
614     }
615     else
616         wxPrintout::OnPreparePrinting();
617 
618     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
619 }
620 
OnPrintPage(int pageNum)621 bool wxLuaPrintout::OnPrintPage(int pageNum)
622 {
623     bool fResult = false;
624 
625     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
626         m_wxlState.HasDerivedMethod(this, "OnPrintPage", true))
627     {
628         int nOldTop = m_wxlState.lua_GetTop();
629         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
630         m_wxlState.lua_PushNumber(pageNum);
631 
632         if (m_wxlState.LuaPCall(2, 1) == 0)
633             fResult = m_wxlState.GetBooleanType(-1);
634 
635         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
636     }
637     // no else since this is pure virtual
638 
639     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
640 
641     return fResult;
642 }
643 
TestVirtualFunctionBinding(const wxString & val)644 wxString wxLuaPrintout::TestVirtualFunctionBinding(const wxString& val)
645 {
646     wxString result(val + wxT("-Base"));
647 
648     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
649         m_wxlState.HasDerivedMethod(this, "TestVirtualFunctionBinding", true))
650     {
651         int nOldTop = m_wxlState.lua_GetTop();
652         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaPrintout, true);
653         m_wxlState.lua_PushString(val.c_str());
654 
655         if (m_wxlState.LuaPCall(2, 1) == 0)
656             result = m_wxlState.GetwxStringType(-1);
657 
658         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
659     }
660     // no else since wxPrintout doesn't have this function
661 
662     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
663 
664     return result;
665 }
666 
667 #endif // wxLUA_USE_wxLuaPrintout
668 
669 // ----------------------------------------------------------------------------
670 // wxLuaArtProvider
671 // ----------------------------------------------------------------------------
672 
673 IMPLEMENT_ABSTRACT_CLASS(wxLuaArtProvider, wxArtProvider)
674 
675 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaArtProvider;
676 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxSize;
677 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxBitmap;
678 
wxLuaArtProvider(const wxLuaState & wxlState)679 wxLuaArtProvider::wxLuaArtProvider(const wxLuaState& wxlState) : m_wxlState(wxlState)
680 {
681 }
682 
DoGetSizeHint(const wxArtClient & client)683 wxSize wxLuaArtProvider::DoGetSizeHint(const wxArtClient& client)
684 {
685     wxSize size;
686 
687     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
688         m_wxlState.HasDerivedMethod(this, "DoGetSizeHint", true))
689     {
690         int nOldTop = m_wxlState.lua_GetTop();
691         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaArtProvider, true);
692         m_wxlState.lua_PushString(client.c_str());
693 
694         if (m_wxlState.LuaPCall(2, 1) == 0)
695         {
696             wxSize *s = (wxSize*)m_wxlState.GetUserDataType(-1, wxluatype_wxSize);
697             if (s) size = *s;
698         }
699 
700         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
701     }
702     else
703         size = wxArtProvider::DoGetSizeHint(client);
704 
705     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
706 
707     return size;
708 }
709 
CreateBitmap(const wxArtID & id,const wxArtClient & client,const wxSize & size)710 wxBitmap wxLuaArtProvider::CreateBitmap(const wxArtID& id, const wxArtClient& client, const wxSize& size)
711 {
712     wxBitmap bitmap;
713 
714     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
715         m_wxlState.HasDerivedMethod(this, "CreateBitmap", true))
716     {
717         int nOldTop = m_wxlState.lua_GetTop();
718         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaArtProvider, true);
719         m_wxlState.lua_PushString(id.c_str());
720         m_wxlState.lua_PushString(client.c_str());
721 
722         // allocate a new object using the copy constructor
723         wxSize* s = new wxSize(size);
724         // add the new object to the tracked memory list
725         m_wxlState.AddGCObject((void*)s, wxluatype_wxSize);
726         m_wxlState.wxluaT_PushUserDataType(s, wxluatype_wxSize, true);
727 
728         if (m_wxlState.LuaPCall(4, 1) == 0)
729         {
730             wxBitmap *b = (wxBitmap*)m_wxlState.GetUserDataType(-1, wxluatype_wxBitmap);
731             if (b) bitmap = *b;
732         }
733 
734         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
735     }
736     // no else since this is pure virtual
737 
738     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
739 
740     return bitmap;
741 }
742 
743 
744 // ----------------------------------------------------------------------------
745 // wxLuaListCtrl
746 // ----------------------------------------------------------------------------
747 
748 #if wxLUA_USE_wxListCtrl && wxUSE_LISTCTRL
749 
750 IMPLEMENT_ABSTRACT_CLASS(wxLuaListCtrl, wxListCtrl)
751 
752 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaListCtrl;
753 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxListItemAttr;
754 
wxLuaListCtrl(const wxLuaState & wxlState)755 wxLuaListCtrl::wxLuaListCtrl(const wxLuaState& wxlState)
756               :m_wxlState(wxlState)
757 {
758 }
759 
wxLuaListCtrl(const wxLuaState & wxlState,wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)760 wxLuaListCtrl::wxLuaListCtrl(const wxLuaState& wxlState, wxWindow *parent, wxWindowID id,
761                              const wxPoint &pos, const wxSize &size, long style,
762                              const wxValidator &validator, const wxString &name)
763               :wxListCtrl(parent, id, pos, size, style, validator, name), m_wxlState(wxlState)
764 {
765 }
766 
OnGetItemAttr(long item) const767 wxListItemAttr * wxLuaListCtrl::OnGetItemAttr(long item) const
768 {
769     wxListItemAttr * attr = NULL;
770 
771     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
772         m_wxlState.HasDerivedMethod(this, "OnGetItemAttr", true))
773     {
774         int nOldTop = m_wxlState.lua_GetTop();
775         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaListCtrl, true);
776         m_wxlState.lua_PushNumber(item);
777 
778         if (m_wxlState.LuaPCall(2, 1) == 0)
779             attr = (wxListItemAttr*)m_wxlState.GetUserDataType(-1, wxluatype_wxListItemAttr);
780 
781         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
782     }
783     else
784         attr = wxListCtrl::OnGetItemAttr(item);
785 
786     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
787 
788     return attr;
789 }
790 
791 #if wxCHECK_VERSION(3,0,0) && defined(__WXMSW__)
OnGetItemColumnAttr(long item,long column) const792 wxListItemAttr * wxLuaListCtrl::OnGetItemColumnAttr(long item, long column) const
793 {
794     wxListItemAttr * attr = NULL;
795 
796     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
797         m_wxlState.HasDerivedMethod(this, "OnGetItemColumnAttr", true))
798     {
799         int nOldTop = m_wxlState.lua_GetTop();
800         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaListCtrl, true);
801         m_wxlState.lua_PushNumber(item);
802         m_wxlState.lua_PushNumber(column);
803 
804         if (m_wxlState.LuaPCall(3, 1) == 0)
805             attr = (wxListItemAttr*)m_wxlState.GetUserDataType(-1, wxluatype_wxListItemAttr);
806 
807         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
808     }
809     else
810         attr = wxListCtrl::OnGetItemColumnAttr(item, column);
811 
812     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
813 
814     return attr;
815 }
816 #endif //wxCHECK_VERSION(3,0,0) && defined(__WXMSW__)
817 
OnGetItemColumnImage(long item,long column) const818 int wxLuaListCtrl::OnGetItemColumnImage(long item, long column) const
819 {
820     int image = 0;
821 
822     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
823         m_wxlState.HasDerivedMethod(this, "OnGetItemColumnImage", true))
824     {
825         int nOldTop = m_wxlState.lua_GetTop();
826         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaListCtrl, true);
827         m_wxlState.lua_PushNumber(item);
828         m_wxlState.lua_PushNumber(column);
829 
830         if (m_wxlState.LuaPCall(3, 1) == 0)
831             image = m_wxlState.GetIntegerType(-1);
832 
833         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
834     }
835     else
836         image = wxListCtrl::OnGetItemColumnImage(item, column);
837 
838     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
839 
840     return image;
841 }
842 
OnGetItemImage(long item) const843 int wxLuaListCtrl::OnGetItemImage(long item) const
844 {
845     int image = 0;
846 
847     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
848         m_wxlState.HasDerivedMethod(this, "OnGetItemImage", true))
849     {
850         int nOldTop = m_wxlState.lua_GetTop();
851         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaListCtrl, true);
852         m_wxlState.lua_PushNumber(item);
853 
854         if (m_wxlState.LuaPCall(2, 1) == 0)
855             image = m_wxlState.GetIntegerType(-1);
856 
857         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
858     }
859     // no else since the class must override this function
860 
861     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
862 
863     return image;
864 }
865 
OnGetItemText(long item,long column) const866 wxString wxLuaListCtrl::OnGetItemText(long item, long column) const
867 {
868     wxString str;
869 
870     if (m_wxlState.Ok() && !m_wxlState.GetCallBaseClassFunction() &&
871         m_wxlState.HasDerivedMethod(this, "OnGetItemText", true))
872     {
873         int nOldTop = m_wxlState.lua_GetTop();
874         m_wxlState.wxluaT_PushUserDataType(this, wxluatype_wxLuaListCtrl, true);
875         m_wxlState.lua_PushNumber(item);
876         m_wxlState.lua_PushNumber(column);
877 
878         if (m_wxlState.LuaPCall(3, 1) == 0)
879             str = m_wxlState.GetwxStringType(-1);
880 
881         m_wxlState.lua_SetTop(nOldTop-1); // -1 to remove pushed derived method func too
882     }
883     else
884         str = wxListCtrl::OnGetItemText(item, column);
885 
886     m_wxlState.SetCallBaseClassFunction(false); // clear flag always
887 
888     return str;
889 }
890 
891 #endif //wxLUA_USE_wxListCtrl && wxUSE_LISTCTRL
892 
893 // ----------------------------------------------------------------------------
894 // wxLuaProcess - Allows overriding onTerminate event
895 // ----------------------------------------------------------------------------
896 #if wxLUA_USE_wxProcess
897 
898 IMPLEMENT_ABSTRACT_CLASS(wxLuaProcess, wxProcess)
899 
900 extern WXDLLIMPEXP_DATA_BINDWXCORE(int) wxluatype_wxLuaProcess;
901 
wxLuaProcess(int flags)902 wxLuaProcess::wxLuaProcess(int flags)
903     : wxProcess(flags)
904 {
905 }
906 
wxLuaProcess(wxEvtHandler * parent,int nId)907 wxLuaProcess::wxLuaProcess(wxEvtHandler *parent, int nId)
908     : wxProcess(parent, nId)
909 {
910 }
911 
OnTerminate(int pid,int status)912 void wxLuaProcess::OnTerminate(int pid, int status)
913 {
914     wxProcessEvent event(m_id, pid, status);
915     ProcessEvent(event);
916 }
917 
918 #endif //WX_LUA_WXLCORE_H
919