-
Notifications
You must be signed in to change notification settings - Fork 59
/
wxlua.html
1358 lines (1351 loc) · 147 KB
/
wxlua.html
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<meta name="author" content="John Labenski" />
<meta name="date" content="2022-11-06" />
<title>wxLua 3.2.0.2 - User Manual</title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<link rel="stylesheet" href="wxlua.css" type="text/css" />
</head>
<body>
<div id="header">
<h1 class="title">wxLua 3.2.0.2 - User Manual</h1>
<h2 class="author">John Labenski</h2>
<h3 class="date">2022-11-06</h3>
</div>
<p><a href="http://wxlua.sourceforge.net">wxLua</a> is a <a href="http://www.lua.org">Lua</a> scripting language wrapper around the <a href="http://www.wxwidgets.org">wxWidgets</a> cross-platform C++ GUI library. It consists of two IDE type editors that can edit, debug, and run Lua programs (wxLua and wxLuaEdit), an executable for running standalone wxLua scripts (wxLuaFreeze), a Lua module that may be loaded using <code class="sourceCode lua"><span class="fu">require</span><span class="ot">(</span><span class="st">"wx"</span><span class="ot">)</span></code> when using the standard Lua executable, and a library for extending C++ programs with a fast, small, fully embeddable scripting language.</p>
<p>Lua is a small scripting language written in ANSI C that can load and run interpreted scripts as either files or strings. The Lua language is fast, dynamic, and easy to learn. Lua contains a limited number of data types, mainly numbers, booleans, strings, functions, tables, and userdata. Perhaps the most powerful feature of the Lua language is that tables can be used as either numerically indexed arrays or associative arrays that can cross-reference any variable type to any other variable type.</p>
<p>wxLua adds to this small and elegant language the power of the C++ wxWidgets cross-platform GUI library. This includes the ability to create complex user interface dialogs, file and image manipulation, drawing, networking, displaying HTML, and printing to name a few. You can use as much or as little of wxWidgets as you like and C++ developers can trim down the size the bindings by turning off preprocessor directives.</p>
<p>Additionally, wxLua adds a library for manipulating the bits of integer numbers using a back-ported <code>bit32</code> library from Lua 5.2.</p>
<h3 id="references">References:</h3>
<table>
<tbody>
<tr class="odd">
<td align="left">wxLua website</td>
<td align="left">-</td>
<td align="left"><a href="http://wxlua.sourceforge.net">http://wxlua.sourceforge.net</a></td>
</tr>
<tr class="even">
<td align="left">wxLua Sourceforge page</td>
<td align="left">-</td>
<td align="left"><a href="http://sourceforge.net/projects/wxlua">http://sourceforge.net/projects/wxlua</a></td>
</tr>
<tr class="odd">
<td align="left">Lua website</td>
<td align="left">-</td>
<td align="left"><a href="http://www.lua.org">http://www.lua.org</a></td>
</tr>
<tr class="even">
<td align="left">wxWidgets website</td>
<td align="left">-</td>
<td align="left"><a href="http://www.wxwidgets.org">http://www.wxwidgets.org</a></td>
</tr>
<tr class="odd">
<td align="left">Mailing list</td>
<td align="left">-</td>
<td align="left"><a href="[email protected]">[email protected]</a></td>
</tr>
</tbody>
</table>
<h2 id="table-of-contents">Table of Contents</h2>
<ol style="list-style-type: decimal">
<li><a href="#C1">Version Information</a></li>
<li><a href="#C2">Requirements</a></li>
<li><a href="#C3">Brief Introduction to Lua</a></li>
<li><a href="#C4">Bit Library</a></li>
<li><a href="#C5">Programming in wxLua</a><br /> 5.1 <a href="#C5.1">Naming, location, and usage of the wxWidgets objects declared in the C++ header files in the wx Lua table</a></li>
<li><a href="#C6">wxLua Samples and How to Run Them</a><br /> 6.1 <a href="#C6.1">How to Run the Samples</a><br /> 6.2 <a href="#C6.2">Provided Samples</a></li>
<li><a href="#C7">wxLua Applications</a><br /> 7.1 <a href="#C7.1">wxLua</a><br /> 7.2 <a href="#C7.2">wxLuaEdit</a><br /> 7.3 <a href="#C7.3">wxLuaFreeze</a><br /> 7.4 <a href="#C7.4">Lua Module using require()</a></li>
<li><a href="#C8">wxLua Utils</a><br /> 8.1 <a href="#C8.1">bin2c.lua</a></li>
<li><a href="#C9">wxLua Sourcecode Modules</a></li>
<li><a href="#C10">wxLua C++ Programming Guide</a><br /> 10.1 <a href="#C10.1">Data stored in Lua's LUA_REGISTRYINDEX table</a><br /> 10.2 <a href="#C10.2">Functions to Create a wxLuaState</a><br /> 10.3 <a href="#C10.3">Using a wxLuaState</a></li>
</ol>
<p><a name="C1"/></p>
<h2 id="version-information">1 - Version Information</h2>
<ul>
<li>The wxLua version number is set to the stable version of wxWidgets that it has been updated to.
<ul>
<li>The revision number (X.Y.Z.revision) can be thought of as the wxLua version number.</li>
</ul></li>
<li>Lua 5.1.5, 5.2.3, or 5.3
<ul>
<li>wxLua uses a nearly unmodified copy of Lua 5.1.5 or 5.2.3.</li>
<li>Official patches on <a href="www.lua.org">www.lua.org</a> are applied as they are released.</li>
<li>In MSW the Lua executable has a manifest set to load version 6 of the comctrl32.dll (if available) so that in MS Windows 7+ the controls look modern. When not running wxLua GUI code the executable behaves no differently.</li>
<li>Any program that works using the official release of Lua will work in wxLua.</li>
</ul></li>
<li>wxWidgets 3.x
<ul>
<li>wxLua currently compiles with wxWidgets versions 2.8.x, 2.9.x, and 3.x.</li>
<li>It may compile with newer versions of wxWidgets as well as older ones.</li>
<li>The interface files have <code>#ifdefs</code> for 2.6, but they are not maintained anymore since in some cases the complexity of maintaining backwards compatibility is not worth it and it is better to take advantage of the fixes and additions to newer versions of wxWidgets. With a little work you may be able to resurrect it to work with wxWidgets 2.6.</li>
<li>Note for wxWidgets < 2.9 : wxLua makes use of the wxStyledTextCtrl contrib library in <code>wxWidgets/contrib/src/stc</code>. You need to have compiled this into a library if you want to compile the wxLua apps. In wxWidgets >= 2.9 the wxStyledTextCtrl is now part of the main distribution.</li>
<li>The wxLua library links to these wxWidgets libs: <code>stc, xrc, html, media, adv, net, xml, core, base, tiff, jpeg, png, zlib, regex, expat</code>.</li>
<li>See the wxLua <a href="install.html">install.html</a> for more information.</li>
</ul></li>
</ul>
<p><a name="C2"/></p>
<h2 id="requirements">2 - Requirements</h2>
<p>Lua programmers can use the binary packages of wxLua and everything that's needed is contained within it. C++ programmers or users on platforms that we don't provide binaries for will need a development library of wxWidgets; typically the source code that you have compiled on your system. More information about compiling wxLua is contained in the wxLua <a href="install.html">install.html</a> file.</p>
<p><a name="C3"/></p>
<h2 id="brief-introduction-to-lua">3 - Brief Introduction to Lua</h2>
<p>This short primer is meant to give you a good enough feeling for Lua that you should be able to understand the sample programs and begin to write your own. It assumes that you have a cursory understanding of general programming techniques. You should, in any case, read the Lua documentation at <a href="http://www.lua.org">www.lua.org</a>.</p>
<ul>
<li><strong>Comments</strong>
<ul>
<li>Single line comments
<ul>
<li><code class="sourceCode lua"><span class="co">-- rest of line is commented</span></code></li>
</ul></li>
<li>Multiple line block or inline comments
<ul>
<li><code class="sourceCode lua"><span class="co">--[[ multiple line or inline comment ... ]]</span></code></li>
</ul></li>
</ul></li>
<li><strong>Variables</strong>
<ul>
<li>Variables are not permanently typed and you can freely overwrite them with other values or types, there is no "const" keyword.</li>
<li>Variables are global unless you put the keyword <code class="sourceCode lua"><span class="kw">local</span></code> in front of them, this is sometimes good practice.</li>
<li>The scope of <code class="sourceCode lua"><span class="kw">local</span></code> variables is limited to the current scope and its children.</li>
<li>Local variables can be harder to debug because they are stored on the stack and you need the Lua debug functions to resolve the name.</li>
<li>Local variables are faster than global variables because they do not require a table lookup for use.</li>
<li>A <code class="sourceCode lua"><span class="kw">local</span></code> variable created with the same name as a global variable supersedes the global variable within the scope of the local variable.</li>
<li>The function <code class="sourceCode lua"><span class="fu">type</span><span class="ot">(</span><span class="kw">var_name</span><span class="ot">)</span></code> returns the variable type as a string.
<ul>
<li>The eight variables types are: <code class="sourceCode lua"><span class="kw">nil</span><span class="ot">,</span> <span class="kw">boolean</span><span class="ot">,</span> <span class="kw">number</span><span class="ot">,</span> <span class="kw">string</span><span class="ot">,</span> <span class="kw">table</span><span class="ot">,</span> <span class="kw">function</span><span class="ot">,</span> <span class="kw">userdata</span><span class="ot">,</span> <span class="kw">thread</span></code></li>
</ul></li>
</ul></li>
<li><strong>Lua Types</strong>
<ul>
<li><strong>nil</strong> : A special value meaning NULL or nothing.
<ul>
<li><code class="sourceCode lua"><span class="kw">a</span> <span class="ot">=</span> <span class="kw">nil</span><span class="ot">;</span> <span class="kw">local</span> <span class="kw">b</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">a</span><span class="ot">),</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">b</span><span class="ot">))</span></code> ; prints "nil nil nil nil"</li>
<li>Variables that have not been assigned a value are <code class="sourceCode lua"><span class="kw">nil</span></code> and any variable can be reset back to <code class="sourceCode lua"><span class="kw">nil</span></code> at any time to allow the Lua garbage collector to delete them if there are no other references to it.</li>
<li>This value is often returned for functions that fail.</li>
<li>You can provide an inline alternative to <code class="sourceCode lua"><span class="kw">nil</span></code> using the <code class="sourceCode lua"><span class="kw">or</span></code> keyword since <code class="sourceCode lua"><span class="kw">nil</span></code> evaluates to false.
<ul>
<li><code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="fu">tonumber</span><span class="ot">(</span><span class="st">"a"</span><span class="ot">),</span> <span class="fu">tonumber</span><span class="ot">(</span><span class="st">"a"</span><span class="ot">)</span> <span class="kw">or</span> <span class="dv">1</span><span class="ot">)</span></code> ; prints "nil 1"</li>
</ul></li>
</ul></li>
<li><strong>boolean</strong> : <code class="sourceCode lua"><span class="kw">true</span></code> or <code class="sourceCode lua"><span class="kw">false</span></code>
<ul>
<li><code class="sourceCode lua"><span class="kw">a</span> <span class="ot">=</span> <span class="kw">true</span><span class="ot">;</span> <span class="kw">b</span> <span class="ot">=</span> <span class="kw">false</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">a</span><span class="ot">),</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">b</span><span class="ot">))</span></code> ; prints "true false boolean boolean"</li>
<li>Note that <code class="sourceCode lua"><span class="kw">nil</span></code> works as <code class="sourceCode lua"><span class="kw">false</span></code>, but the number 0 evaluates as <code class="sourceCode lua"><span class="kw">true</span></code> since it has a value, i.e. not <code class="sourceCode lua"><span class="kw">nil</span></code>, use <code class="sourceCode lua"><span class="ot">(</span><span class="kw">a</span> <span class="ot">~=</span> <span class="dv">0</span><span class="ot">)</span></code>.</li>
<li><code class="sourceCode lua"><span class="kw">a</span> <span class="ot">=</span> <span class="dv">0</span><span class="ot">;</span> <span class="kw">if</span> <span class="kw">a</span> <span class="kw">then</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">)</span> <span class="kw">end</span></code> ; prints "0" since the variable "<code class="sourceCode lua"><span class="kw">a</span></code>" evaluates to <code class="sourceCode lua"><span class="kw">true</span></code>.</li>
</ul></li>
<li><strong>number</strong> : All numbers in Lua are double valued floating point numbers.
<ul>
<li><code class="sourceCode lua"><span class="kw">a</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">;</span> <span class="kw">b</span> <span class="ot">=</span> <span class="dv">3.14</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">a</span><span class="ot">),</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">b</span><span class="ot">))</span></code> ; prints "1 3.14 number number"</li>
<li><code class="sourceCode lua"><span class="kw">n</span> <span class="ot">=</span> <span class="ot">(</span>1E1 <span class="ot">*</span> <span class="dv">3.14</span> <span class="ot">*</span> <span class="fu">math.sin</span><span class="ot">(</span><span class="dv">1</span><span class="ot">)</span> <span class="ot">/</span> <span class="dv">4</span><span class="ot">)*</span><span class="kw">math</span><span class="ot">.</span>pow<span class="ot">(</span><span class="dv">2.5e-1</span><span class="ot">,</span> <span class="dv">4</span><span class="ot">)</span></code></li>
<li>Variables can be coerced into numbers using the function <code class="sourceCode lua"><span class="fu">tonumber</span><span class="ot">(</span><span class="kw">variable</span><span class="ot">)</span></code> which returns <code class="sourceCode lua"><span class="kw">nil</span></code> on failure.</li>
<li>Additional math functions are in the <code class="sourceCode lua"><span class="kw">math</span></code> table.</li>
</ul></li>
<li><strong>string</strong> : Strings in Lua can have embedded nulls "\0" and use the same escape characters as C.
<ul>
<li><code class="sourceCode lua"><span class="kw">a</span> <span class="ot">=</span> <span class="st">"hello"</span><span class="ot">;</span> <span class="kw">b</span> <span class="ot">=</span> <span class="kw">a</span><span class="ot">;</span> <span class="kw">b</span> <span class="ot">=</span> <span class="st">"hi"</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="ot">#</span><span class="kw">a</span><span class="ot">,</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">a</span><span class="ot">))</span></code> ; prints "hello hi 5 string"</li>
<li>Strings are internally hashed by Lua so that there is only one copy of a particular string stored at any one time no matter how many variables reference it.</li>
<li>String variables are copy on write and the example above shows that overwriting "b" does not change "a".</li>
<li><code class="sourceCode lua"><span class="kw">s</span> <span class="ot">=</span> <span class="st">"How are 'you'!"</span></code> or <code class="sourceCode lua"><span class="kw">s</span> <span class="ot">=</span> <span class="st">'How</span><span class="ot">\t</span><span class="st">are "You"!</span><span class="ot">\n</span><span class="st">'</span></code> are both valid since either " or ' can be used to quote strings. ('\t' = tab, '\n' = line feed)</li>
<li><code class="sourceCode lua"><span class="kw">s</span> <span class="ot">=</span> <span class="st">[[How are "'you'"!]]</span></code> ; double brackets can be used to create multiline strings that include new lines and whitespace.</li>
<li>Concatenate strings using the <code class="sourceCode lua"><span class="ot">..</span></code> operator
<ul>
<li><code class="sourceCode lua"><span class="kw">str1</span> <span class="ot">=</span> <span class="st">"hello"</span><span class="ot">;</span> <span class="kw">str2</span> <span class="ot">=</span> <span class="st">"number"</span><span class="ot">;</span> <span class="kw">str3</span> <span class="ot">=</span> <span class="kw">str1</span><span class="ot">..</span><span class="st">" "</span><span class="ot">..</span><span class="kw">str2</span><span class="ot">..</span><span class="st">" "</span><span class="ot">..</span>tostring<span class="ot">(</span><span class="dv">2</span><span class="ot">)..</span><span class="st">"!"</span></code></li>
<li>Numbers can be coerced into strings as <code class="sourceCode lua"><span class="ot">(</span><span class="st">"A "</span><span class="ot">..</span><span class="dv">2</span><span class="ot">)</span></code>, but not <code class="sourceCode lua"><span class="ot">(</span><span class="dv">2</span><span class="ot">..</span><span class="st">"A"</span><span class="ot">)</span></code> since the left hand side of the <code class="sourceCode lua"><span class="ot">..</span></code> operator must be a string.</li>
<li>Many strings should be concatenated together by putting them into a table array using <code class="sourceCode lua"><span class="fu">table.insert</span><span class="ot">()</span></code> (or appended using <code class="sourceCode lua"><span class="kw">table</span><span class="ot">[#</span><span class="kw">table</span><span class="ot">+</span><span class="dv">1</span><span class="ot">]</span> <span class="ot">=</span> <span class="st">"hello"</span></code>) and then <code class="sourceCode lua"><span class="fu">table.concat</span><span class="ot">()</span></code> called to create the single string result. The concatenation operator <code class="sourceCode lua"><span class="ot">..</span></code> is slower since each intermediary string has to be reallocated and hashed. The <code class="sourceCode lua"><span class="fu">table.concat</span><span class="ot">()</span></code> method only has to allocate and hash the resultant string once and its speed is quite competitive to other scripting languages.</li>
</ul></li>
<li>Variables can be coerced into strings using the function <code class="sourceCode lua"><span class="fu">tostring</span><span class="ot">(</span><span class="kw">variable</span><span class="ot">)</span></code> which returns <code class="sourceCode lua"><span class="kw">nil</span></code> on failure.</li>
<li>Additional string functions are in the <code class="sourceCode lua"><span class="kw">string</span></code> table.</li>
</ul></li>
<li><strong>table</strong> : Tables can be indexed by and hold values of numbers, strings, functions, userdata, other tables...
<ul>
<li><code class="sourceCode lua"><span class="kw">a</span> <span class="ot">=</span> <span class="ot">{</span><span class="dv">5</span><span class="ot">};</span> <span class="kw">b</span> <span class="ot">=</span> <span class="kw">a</span><span class="ot">;</span> <span class="kw">b</span><span class="ot">[</span><span class="dv">1</span><span class="ot">]</span> <span class="ot">=</span> <span class="dv">6</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="kw">a</span><span class="ot">[</span><span class="dv">1</span><span class="ot">],</span> <span class="kw">b</span><span class="ot">[</span><span class="dv">1</span><span class="ot">],</span> <span class="kw">b</span><span class="ot">[</span><span class="dv">2</span><span class="ot">],</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">a</span><span class="ot">),</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">b</span><span class="ot">))</span></code>
<ul>
<li>prints "table: 01780F98 table: 01780F98 6 6 nil table table"</li>
<li>Printing a table is the same as calling <code class="sourceCode lua"><span class="fu">tostring</span><span class="ot">(</span><span class="kw">t</span><span class="ot">)</span></code> on the table which displays the variable type and its memory address.</li>
<li>Assigning a variable to an existing table does not copy the table values and you can use either variable to access and modify the table elements.</li>
<li>There is no built-in deep or shallow table copy function, but it is easy enough to write your own. The reason is that tables can contain back-references to themselves and to other parts of the table and having a prior knowlege of how the table is constructed helps in writing a copy function that works quickly and behaves as desired.</li>
</ul></li>
<li><code class="sourceCode lua"><span class="kw">t</span> <span class="ot">=</span> <span class="ot">{}</span></code> creates an empty table.
<ul>
<li>You must declare a variable as a table before using its indexes.</li>
</ul></li>
<li><code class="sourceCode lua"><span class="kw">t</span> <span class="ot">=</span> <span class="ot">{</span> <span class="ot">[</span><span class="st">"a"</span><span class="ot">]</span> <span class="ot">=</span> <span class="dv">5</span><span class="ot">,</span> <span class="st">"first"</span><span class="ot">,</span> <span class="st">"second"</span><span class="ot">,</span> <span class="kw">B</span> <span class="ot">=</span> <span class="dv">7</span> <span class="ot">};</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">t</span><span class="ot">.</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">t</span><span class="ot">[</span><span class="st">"a"</span><span class="ot">],</span> <span class="kw">t</span><span class="ot">[</span><span class="dv">0</span><span class="ot">],</span> <span class="kw">t</span><span class="ot">[</span><span class="dv">1</span><span class="ot">],</span> <span class="kw">t</span><span class="ot">[</span><span class="dv">2</span><span class="ot">],</span> <span class="kw">t</span><span class="ot">.</span><span class="kw">B</span><span class="ot">,</span> <span class="kw">t</span><span class="ot">.</span><span class="kw">b</span><span class="ot">,</span> <span class="kw">t</span><span class="ot">.</span><span class="kw">c</span><span class="ot">)</span></code>
<ul>
<li>prints "5 5 nil first second 7 nil nil"</li>
<li>Set values as : <code class="sourceCode lua"><span class="kw">t</span><span class="ot">.</span><span class="kw">a</span> <span class="ot">=</span> <span class="dv">2</span><span class="ot">;</span> <span class="kw">t</span><span class="ot">[</span><span class="st">"a"</span><span class="ot">]</span> <span class="ot">=</span> <span class="dv">3</span><span class="ot">;</span> <span class="kw">t</span><span class="ot">[</span><span class="dv">10</span><span class="ot">]</span> <span class="ot">=</span> <span class="dv">4</span></code></li>
<li>Elements are automatically created when assigning new values to elements that don't already exist and accessing elements that don't exist returns <code class="sourceCode lua"><span class="kw">nil</span></code>.</li>
<li>Clear values by setting them to <code class="sourceCode lua"><span class="kw">nil</span></code>, e.g. <code class="sourceCode lua"><span class="kw">t</span><span class="ot">.</span><span class="kw">a</span> <span class="ot">=</span> <span class="kw">nil</span></code></li>
<li>The length operator <code class="sourceCode lua"><span class="ot">#</span><span class="kw">t</span></code> returns 2 since there are only two contiguous integer table indexes starting from 1 even though in this case there are actually 4 entries in the table.
<ul>
<li>The length of a table used as an array with holes (nil values) is undefined.</li>
<li>The only guarantee is that the value at <code class="sourceCode lua"><span class="kw">t</span><span class="ot">[#</span><span class="kw">t</span><span class="ot">+</span><span class="dv">1</span><span class="ot">]</span></code> will be <code class="sourceCode lua"><span class="kw">nil</span></code>.</li>
<li>This is an optimization detail, if you want to use a table as an array you must simply fill the 1..N indicies with values.</li>
</ul></li>
<li>Lua table arrays have a starting index of 1.</li>
<li><code class="sourceCode lua"><span class="kw">for</span> <span class="kw">k</span><span class="ot">,</span> <span class="kw">v</span> <span class="kw">in</span> <span class="fu">ipairs</span><span class="ot">(</span><span class="kw">t</span><span class="ot">)</span> <span class="kw">do</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">k</span><span class="ot">,</span> <span class="kw">v</span><span class="ot">)</span> <span class="kw">end</span></code> ; Note: (k = key, v = value)
<ul>
<li>prints only the array values : "1 first" and "2 second"</li>
</ul></li>
<li><code class="sourceCode lua"><span class="kw">for</span> <span class="kw">k</span><span class="ot">,</span> <span class="kw">v</span> <span class="kw">in</span> <span class="fu">pairs</span><span class="ot">(</span><span class="kw">t</span><span class="ot">)</span> <span class="kw">do</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">k</span><span class="ot">,</span> <span class="kw">v</span><span class="ot">)</span> <span class="kw">end</span></code> ; Note: (k = key, v = value)
<ul>
<li>prints all table values (unordered) : "1 first", "2 second", "a 5", "B 7"</li>
</ul></li>
</ul></li>
<li>Functions defined by Lua are placed into tables, a namespace if you will, to keep the global namespace uncluttered.
<ul>
<li>See <code class="sourceCode lua"><span class="kw">table</span><span class="ot">.</span><span class="kw">XXX</span><span class="ot">,</span> <span class="kw">string</span><span class="ot">.</span><span class="kw">XXX</span><span class="ot">,</span> <span class="kw">math</span><span class="ot">.</span><span class="kw">XXX</span><span class="ot">,</span> <span class="kw">os</span><span class="ot">.</span><span class="kw">XXX</span></code> etc. in the Lua documentation.</li>
<li>wxLua places the wxWidgets bindings into the <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">XXX</span></code> table "namespace".</li>
</ul></li>
<li><p>The global table is called <code class="sourceCode lua"><span class="kw">_G</span></code> and you can display it as you would any table using : - <code class="sourceCode lua"><span class="kw">for</span> <span class="kw">k</span><span class="ot">,</span> <span class="kw">v</span> <span class="kw">in</span> <span class="fu">pairs</span><span class="ot">(</span><span class="kw">_G</span><span class="ot">)</span> <span class="kw">do</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">k</span><span class="ot">,</span> <span class="kw">v</span><span class="ot">)</span> <span class="kw">end</span></code></p></li>
<li><p>Additional table functions are in the <code class="sourceCode lua"><span class="kw">table</span></code> table.</p></li>
</ul></li>
<li><strong>userdata</strong> : A pointer to a C/C++ object.
<ul>
<li>A metatable (see Lua documentation) may be assigned to it to allow it to act as a table or be called as a function, among other things.</li>
<li>wxLua uses userdata types to wrap the wxWidgets C++ objects to make them useable in a Lua program.</li>
</ul></li>
<li><strong>function</strong> : A handle to a function.
<ul>
<li><code class="sourceCode lua"><span class="kw">function</span> f<span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">)</span> <span class="kw">return</span> <span class="kw">a</span><span class="ot">+</span><span class="kw">b</span> <span class="kw">end</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">f</span><span class="ot">,</span> f<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">),</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">f</span><span class="ot">))</span></code></li>
<li><code class="sourceCode lua"><span class="kw">f</span> <span class="ot">=</span> <span class="kw">function</span> <span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">)</span> <span class="kw">return</span> <span class="kw">a</span><span class="ot">+</span><span class="kw">b</span> <span class="kw">end</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">f</span><span class="ot">,</span> f<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">),</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">f</span><span class="ot">))</span></code>
<ul>
<li>Both lines above print "function: 01DE2AF8 3 function", where 01DE2AF8 is the unique memory address of the function.</li>
<li>Printing a function is the same as calling <code class="sourceCode lua"><span class="fu">tostring</span><span class="ot">(</span><span class="kw">f</span><span class="ot">)</span></code> on the function which displays the variable type and its memory address.</li>
</ul></li>
<li>Functions can return multiple values and be passed more or less variables than specified.</li>
<li><p>Unassigned inputs are set to <code class="sourceCode lua"><span class="kw">nil</span></code> and unassigned return values are discarded.</p></li>
<li>Functions can be assigned to variables or put into tables.
<ul>
<li><code class="sourceCode lua"><span class="kw">t</span> <span class="ot">=</span> <span class="ot">{};</span> <span class="kw">t</span><span class="ot">[</span><span class="st">"Add"</span><span class="ot">]</span> <span class="ot">=</span> <span class="kw">f</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">t</span><span class="ot">.</span>Add<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">),</span> <span class="kw">t</span><span class="ot">.</span><span class="kw">Add</span><span class="ot">)</span></code> ; prints "3 function: 01DE2AF8</li>
<li>If you plan on putting a function into a table you may want to declare the function local since it won't be garbage collected until the local variable goes out of scope and the table is garbage collected or the table's index to the function is set to <code class="sourceCode lua"><span class="kw">nil</span></code>.</li>
</ul></li>
<li>Boolean, number, and string parameters are passed to functions by value and changes to them within the function body does not modify the original variable's value.</li>
<li>Tables and userdata are passed to functions by reference and changes to them within the function body does modify the original variable's value.</li>
<li><code class="sourceCode lua"><span class="kw">function</span> table_append<span class="ot">(</span><span class="kw">t</span><span class="ot">,</span> <span class="kw">v</span><span class="ot">)</span> <span class="kw">t</span><span class="ot">[#</span><span class="kw">t</span><span class="ot">+</span><span class="dv">1</span><span class="ot">]</span> <span class="ot">=</span> <span class="kw">v</span><span class="ot">;</span> <span class="kw">v</span> <span class="ot">=</span> <span class="dv">0</span><span class="ot">;</span> <span class="kw">end</span><span class="ot">;</span> <span class="kw">tbl</span> <span class="ot">=</span> <span class="ot">{};</span> <span class="kw">item</span> <span class="ot">=</span> <span class="st">"hello"</span><span class="ot">;</span> table_append<span class="ot">(</span><span class="kw">tbl</span><span class="ot">,</span> <span class="kw">item</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">tbl</span><span class="ot">[#</span><span class="kw">tbl</span><span class="ot">],</span> <span class="kw">item</span><span class="ot">)</span></code></li>
<li><p>prints "hello hello", i.e. <code class="sourceCode lua"><span class="kw">tbl</span><span class="ot">[</span><span class="dv">1</span><span class="ot">]</span> <span class="ot">==</span> <span class="st">"hello"</span></code> and the variable "<code class="sourceCode lua"><span class="kw">item</span></code>" still equals "hello" and was not changed.</p></li>
</ul></li>
</ul></li>
<li><strong>Operators</strong>
<ul>
<li>Relational : <code class="sourceCode lua"><span class="ot">==</span> <span class="ot">~=</span> <span class="ot"><</span> <span class="ot">></span> <span class="ot"><=</span> <span class="ot">>=</span></code> (Note: not equal is <code class="sourceCode lua"><span class="ot">~=</span></code>)</li>
<li>Logical: <code class="sourceCode lua"><span class="kw">and</span><span class="ot">,</span> <span class="kw">or</span><span class="ot">,</span> <span class="kw">not</span></code></li>
<li>Precedence (low to high):
<ol style="list-style-type: decimal">
<li><code class="sourceCode lua"><span class="kw">or</span></code></li>
<li><code class="sourceCode lua"><span class="kw">and</span></code></li>
<li><code class="sourceCode lua"><span class="ot"><</span> <span class="ot">></span> <span class="ot"><=</span> <span class="ot">>=</span> <span class="ot">~=</span> <span class="ot">==</span></code></li>
<li><code class="sourceCode lua"><span class="ot">..</span></code> (string concatenation)</li>
<li><code class="sourceCode lua"><span class="ot">+</span> <span class="ot">-</span></code></li>
<li><code class="sourceCode lua"><span class="ot">*</span> <span class="ot">/</span> %</code></li>
<li><code class="sourceCode lua"><span class="kw">not</span> <span class="ot">#</span> <span class="ot">-</span></code> (unary)</li>
<li><code class="sourceCode lua"><span class="ot">^</span></code></li>
</ol></li>
</ul></li>
<li><strong>Keywords</strong>
<ul>
<li><code class="sourceCode lua"><span class="kw">and</span> <span class="kw">break</span> <span class="kw">do</span> <span class="kw">else</span> <span class="kw">elseif</span> <span class="kw">end</span> <span class="kw">false</span> <span class="kw">for</span> <span class="kw">function</span> <span class="kw">if</span> <span class="kw">in</span> <span class="kw">local</span> <span class="kw">nil</span> <span class="kw">not</span> <span class="kw">or</span> <span class="kw">repeat</span> <span class="kw">return</span> <span class="kw">then</span> <span class="kw">true</span> <span class="kw">until</span> <span class="kw">while</span></code></li>
</ul></li>
<li><strong>do ... end</strong>
<ul>
<li>Create a new local scope in a <code class="sourceCode lua"><span class="kw">do</span> <span class="ot">...</span> <span class="kw">end</span></code> block.</li>
<li>Note : You cannot write <code class="sourceCode lua"><span class="kw">function</span> printHi<span class="ot">()</span> <span class="kw">return</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="st">"hi"</span><span class="ot">)</span> <span class="kw">end</span></code>, but you can write <code class="sourceCode lua"><span class="kw">function</span> printHi<span class="ot">()</span> <span class="kw">do</span> <span class="kw">return</span> <span class="kw">end</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="st">"hi"</span><span class="ot">)</span> <span class="kw">end</span></code> which can be useful for debugging functions.</li>
</ul></li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="kw">do</span>
<span class="co">-- create a new local scope</span>
<span class="kw">local</span> <span class="kw">a</span> <span class="ot">=</span> <span class="dv">2</span>
<span class="kw">end</span></code></pre>
<ul>
<li><strong>if (bool) then ... elseif (bool) then ... else ... end</strong></li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="kw">local</span> <span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="kw">c</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">,</span> <span class="dv">3</span> <span class="co">-- can assign multiple values</span>
<span class="kw">a</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">;</span> <span class="kw">b</span> <span class="ot">=</span> <span class="dv">2</span><span class="ot">;</span> <span class="kw">c</span> <span class="ot">=</span> <span class="dv">3</span> <span class="co">-- use ; for multiple lines of code on single line</span>
<span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="kw">c</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">,</span> <span class="dv">3</span> <span class="co">-- this works too</span>
<span class="kw">if</span> <span class="ot">(</span><span class="kw">a</span> <span class="ot">==</span> <span class="dv">1</span><span class="ot">)</span> <span class="kw">and</span> <span class="ot">((</span><span class="kw">b</span> <span class="ot"><=</span> <span class="dv">2</span><span class="ot">)</span> <span class="kw">or</span> <span class="ot">(</span><span class="kw">c</span> <span class="ot">~=</span> <span class="dv">3</span><span class="ot">))</span> <span class="kw">then</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">+</span><span class="kw">b</span><span class="ot">/</span><span class="kw">c</span><span class="ot">)</span>
<span class="kw">elseif</span> <span class="kw">a</span> <span class="ot">==</span> <span class="dv">2</span> <span class="kw">then</span> <span class="co">-- no parentheses necessary</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">)</span>
<span class="kw">else</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">b</span><span class="ot">)</span>
<span class="kw">end</span></code></pre>
<ul>
<li><strong>There is no case statement</strong>, but <code class="sourceCode lua"><span class="kw">table</span><span class="ot">[</span><span class="kw">value</span><span class="ot">]</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">()</span> <span class="ot">...</span> <span class="kw">end</span></code> may be used to simulate one.</li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="kw">case</span> <span class="ot">=</span> <span class="ot">{}</span>
<span class="kw">case</span><span class="ot">[</span><span class="dv">1</span><span class="ot">]</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">()</span> <span class="fu">print</span><span class="ot">(</span><span class="st">"Hello #1"</span><span class="ot">)</span> <span class="kw">end</span> <span class="co">-- anonymous function</span>
<span class="kw">case</span><span class="ot">[</span><span class="dv">2</span><span class="ot">]</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">()</span> <span class="fu">print</span><span class="ot">(</span><span class="st">"Hello #2"</span><span class="ot">)</span> <span class="kw">end</span>
<span class="ot">...</span>
<span class="kw">if</span> <span class="kw">case</span><span class="ot">[</span><span class="kw">value</span><span class="ot">]</span> <span class="kw">then</span>
<span class="kw">case</span><span class="ot">[</span><span class="kw">value</span><span class="ot">]()</span>
<span class="kw">else</span>
<span class="fu">print</span><span class="ot">(</span><span class="st">"Unknown case value"</span><span class="ot">)</span>
<span class="kw">end</span></code></pre>
<ul>
<li><strong>while (bool) ... end</strong>
<ul>
<li>Note : there is no "continue" keyword only <code class="sourceCode lua"><span class="kw">break</span></code></li>
</ul></li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="kw">function</span> Check5<span class="ot">(</span><span class="kw">val</span><span class="ot">)</span> <span class="co">-- returns nil if val ~= 5</span>
<span class="kw">if</span> <span class="kw">val</span> <span class="ot">==</span> <span class="dv">5</span> <span class="kw">then</span>
<span class="kw">return</span> <span class="kw">true</span>
<span class="kw">end</span>
<span class="kw">end</span>
<span class="kw">local</span> <span class="kw">a</span> <span class="ot">=</span> <span class="dv">1</span>
<span class="kw">while</span> <span class="kw">a</span> <span class="ot"><</span> <span class="dv">10</span> <span class="kw">do</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">)</span>
<span class="kw">if</span> Check5<span class="ot">(</span><span class="kw">a</span><span class="ot">)</span> <span class="kw">then</span> <span class="kw">break</span> <span class="kw">end</span>
<span class="kw">a</span> <span class="ot">=</span> <span class="kw">a</span> <span class="ot">+</span> <span class="dv">1</span> <span class="co">-- no increment operator</span>
<span class="kw">end</span></code></pre>
<ul>
<li>You can simulate a "continue" statement by adding an inner while loop (doesn't print # 5).</li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="kw">local</span> <span class="kw">a</span> <span class="ot">=</span> <span class="dv">0</span>
<span class="kw">while</span> <span class="kw">a</span> <span class="ot"><</span> <span class="dv">10</span> <span class="kw">do</span> <span class="kw">while</span> <span class="kw">true</span> <span class="kw">do</span>
<span class="kw">a</span> <span class="ot">=</span> <span class="kw">a</span> <span class="ot">+</span> <span class="dv">1</span> <span class="co">-- no increment operator</span>
<span class="kw">if</span> Check5 <span class="ot">(</span><span class="kw">a</span><span class="ot">)</span> <span class="kw">then</span>
<span class="kw">break</span> <span class="co">-- break in the inner while loop to "continue" in the outer loop</span>
<span class="kw">else</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="kw">break</span> <span class="kw">end</span> <span class="co">-- break out of inner while loop</span>
<span class="kw">end</span></code></pre>
<ul>
<li><strong>repeat ... until (bool)</strong>
<ul>
<li>Note : there is no "continue" keyword only <code class="sourceCode lua"><span class="kw">break</span></code></li>
</ul></li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="kw">local</span> <span class="kw">a</span> <span class="ot">=</span> <span class="dv">10</span>
<span class="kw">repeat</span>
<span class="kw">local</span> <span class="kw">temp</span> <span class="ot">=</span> <span class="kw">a</span> <span class="ot">*</span> <span class="dv">2</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">temp</span><span class="ot">,</span> <span class="fu">type</span><span class="ot">(</span><span class="kw">temp</span><span class="ot">))</span>
<span class="kw">a</span> <span class="ot">=</span> <span class="kw">a</span> <span class="ot">-</span> <span class="dv">1</span> <span class="co">-- no decrement operator</span>
<span class="kw">until</span> <span class="kw">a</span> <span class="ot"><</span> <span class="dv">0</span></code></pre>
<ul>
<li><strong>for var = init_value, end_value [, increment] do ... end</strong>
<ul>
<li>Note : there is no "continue" keyword only <code class="sourceCode lua"><span class="kw">break</span></code></li>
<li>You cannot modify the loop variable, limit, or increment from within the loop.</li>
<li>The loop counter variable is local to the loop and the final print(a) statement below will print that a = "hello" as it did before the loop.</li>
<li>Copy the loop counter variable to a separate variable if you need to save it when breaking for example.</li>
</ul></li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="kw">local</span> <span class="kw">a</span> <span class="ot">=</span> <span class="st">"hello"</span>
<span class="kw">for</span> <span class="kw">a</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">,</span> <span class="dv">10</span> <span class="co">--[[, increment]]</span> <span class="kw">do</span> <span class="co">-- optional increment value, default increment is 1</span>
<span class="kw">local</span> <span class="kw">temp</span> <span class="ot">=</span> <span class="kw">a</span> <span class="ot">*</span> <span class="dv">2</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">temp</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">a</span><span class="ot">)</span> <span class="co">-- a == "hello" since loop counter variable is local to the loop</span></code></pre>
<ul>
<li><strong>functions</strong>
<ul>
<li>Input any number of parameters by value, tables and userdata are passed by reference.</li>
<li>Missing input variables are assigned the value <code class="sourceCode lua"><span class="kw">nil</span></code>, extra inputs are discarded.</li>
<li>Return values using the "<code class="sourceCode lua"><span class="kw">return</span></code>" keyword.</li>
<li>Not all return values need to be used and extra ones will be discarded.
<ul>
<li>The symbol "_" is a valid variable name and is often used as a dummy variable to receive return values that you do not want.
<ul>
<li><code class="sourceCode lua"><span class="kw">function</span> GetNums<span class="ot">()</span> <span class="kw">return</span> <span class="dv">3</span><span class="ot">,</span> <span class="dv">4</span><span class="ot">,</span> <span class="dv">5</span><span class="ot">,</span> <span class="dv">6</span> <span class="kw">end</span><span class="ot">;</span> <span class="kw">local</span> <span class="kw">_</span><span class="ot">,</span> <span class="kw">num2</span> <span class="ot">=</span> GetNums<span class="ot">()</span></code></li>
</ul></li>
<li>You may also use the <code class="sourceCode lua">select<span class="ot">(</span><span class="kw">n</span><span class="ot">,</span> <span class="ot">...)</span></code> function to choose return values to use.
<ul>
<li><code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span>select<span class="ot">(</span><span class="st">"#"</span><span class="ot">,</span> GetNums<span class="ot">()),</span> <span class="st">"and"</span><span class="ot">,</span> select<span class="ot">(</span><span class="dv">3</span><span class="ot">,</span> GetNums<span class="ot">()))</span></code> ; prints "4 and 5 6".</li>
<li>Note <code class="sourceCode lua">select<span class="ot">(</span><span class="kw">n</span><span class="ot">,</span> <span class="ot">...)</span></code> returns all args after <code class="sourceCode lua"><span class="kw">n</span></code>, but if the left hand side is a single variable the others are discarded.</li>
</ul></li>
</ul></li>
<li>Tables can be used as containers for input values to functions using the <code class="sourceCode lua"><span class="fu">unpack</span><span class="ot">(</span><span class="kw">table</span><span class="ot">,</span> <span class="ot">[</span><span class="kw">i</span><span class="ot">,</span> <span class="ot">[,</span> <span class="kw">j</span><span class="ot">]])</span></code> function.
<ul>
<li>This is useful for creating complex inputs to a function or storing them for reuse.</li>
<li><code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="fu">string.find</span><span class="ot">(</span><span class="fu">unpack</span><span class="ot">({</span><span class="st">"Hello"</span><span class="ot">,</span> <span class="st">"ll"</span><span class="ot">,</span> <span class="dv">1</span><span class="ot">,</span> <span class="dv">1</span><span class="ot">})))</span></code> ; prints "3 4"</li>
</ul></li>
<li>Vararg inputs are written as <code class="sourceCode lua"><span class="kw">function</span> dostuff<span class="ot">(</span> <span class="ot">...</span> <span class="ot">)</span> <span class="kw">end</span></code>
<ul>
<li>The number of args is found using: <code class="sourceCode lua"><span class="kw">local</span> <span class="kw">n_args</span> <span class="ot">=</span> select<span class="ot">(</span><span class="st">"#"</span><span class="ot">,</span> <span class="ot">...)</span></code></li>
<li>The args can be converted into table using: <code class="sourceCode lua"><span class="kw">local</span> <span class="kw">args</span> <span class="ot">=</span> <span class="ot">{</span> <span class="ot">...</span> <span class="ot">}</span></code></li>
<li>A particular arg can be chosen using: <code class="sourceCode lua"><span class="kw">local</span> <span class="kw">arg3</span> <span class="ot">=</span> select<span class="ot">(</span><span class="dv">3</span><span class="ot">,</span> <span class="ot">...)</span></code>
<ul>
<li>See above about using <code class="sourceCode lua">select<span class="ot">()</span></code> to pick return values of a function as well.</li>
</ul></li>
</ul></li>
</ul></li>
</ul>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="co">-- Print the keys in table t that have the given values.</span>
<span class="kw">function</span> PrintKeys<span class="ot">(</span><span class="kw">t</span><span class="ot">,</span> <span class="kw">values</span><span class="ot">,</span> <span class="kw">cmp_case</span><span class="ot">)</span>
<span class="co">-- use nested functions for repetitive code or to simplify code</span>
<span class="kw">local</span> <span class="kw">function</span> cmp_values<span class="ot">(</span><span class="kw">a</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">)</span>
<span class="kw">if</span> <span class="kw">cmp_case</span> <span class="kw">then</span> <span class="co">-- can use upvalue variables</span>
<span class="kw">return</span> <span class="kw">a</span> <span class="ot">==</span> <span class="kw">b</span>
<span class="kw">else</span>
<span class="kw">return</span> <span class="fu">string.lower</span><span class="ot">(</span><span class="kw">a</span><span class="ot">)</span> <span class="ot">==</span> <span class="fu">string.lower</span><span class="ot">(</span><span class="kw">b</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="kw">end</span>
<span class="kw">local</span> <span class="kw">function</span> find_key<span class="ot">(</span><span class="kw">t</span><span class="ot">,</span> <span class="kw">val</span><span class="ot">)</span>
<span class="kw">for</span> <span class="kw">k</span><span class="ot">,</span><span class="kw">v</span> <span class="kw">in</span> <span class="fu">pairs</span><span class="ot">(</span><span class="kw">t</span><span class="ot">)</span> <span class="kw">do</span>
<span class="kw">if</span> cmp_values<span class="ot">(</span><span class="kw">val</span><span class="ot">,</span> <span class="kw">v</span><span class="ot">)</span> <span class="kw">then</span> <span class="kw">return</span> <span class="kw">k</span> <span class="kw">end</span>
<span class="kw">end</span>
<span class="kw">end</span>
<span class="kw">for</span> <span class="kw">i</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">,</span> <span class="ot">#</span><span class="kw">values</span> <span class="kw">do</span>
<span class="fu">print</span><span class="ot">(</span>find_key<span class="ot">(</span><span class="kw">t</span><span class="ot">,</span> <span class="kw">values</span><span class="ot">[</span><span class="kw">i</span><span class="ot">]),</span> <span class="kw">values</span><span class="ot">[</span><span class="kw">i</span><span class="ot">])</span>
<span class="kw">end</span>
<span class="kw">end</span>
<span class="kw">data</span> <span class="ot">=</span> <span class="ot">{</span><span class="kw">a1</span> <span class="ot">=</span> <span class="st">"a2"</span><span class="ot">,</span> <span class="kw">b1</span> <span class="ot">=</span> <span class="st">"b2"</span><span class="ot">,</span> <span class="kw">c1</span> <span class="ot">=</span> <span class="st">"c2"</span><span class="ot">}</span>
<span class="co">-- prints "a1 a2", "b1 b2", "nil C2"</span>
PrintKeys<span class="ot">(</span><span class="kw">data</span><span class="ot">,</span> <span class="ot">{</span><span class="st">"a2"</span><span class="ot">,</span> <span class="st">"b2"</span><span class="ot">,</span> <span class="st">"C2"</span><span class="ot">},</span> <span class="kw">true</span><span class="ot">)</span></code></pre>
<pre class="sourceCode lua"><code class="sourceCode lua"> <span class="co">-- Varargs example demonstrating the different ways to handle them.</span>
<span class="kw">function</span> Varargs<span class="ot">(...)</span>
<span class="kw">local</span> <span class="kw">args</span> <span class="ot">=</span> <span class="ot">{...}</span>
<span class="fu">print</span><span class="ot">(</span>select<span class="ot">(</span><span class="st">"#"</span><span class="ot">,</span> <span class="ot">...),</span> <span class="ot">#{...},</span> <span class="ot">#</span><span class="kw">args</span><span class="ot">,</span> <span class="kw">args</span><span class="ot">[</span><span class="dv">2</span><span class="ot">],</span> <span class="fu">unpack</span><span class="ot">({...},</span> <span class="dv">2</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">))</span>
<span class="kw">return</span> <span class="kw">args</span><span class="ot">,</span> <span class="fu">unpack</span><span class="ot">(</span><span class="kw">args</span><span class="ot">)</span> <span class="co">-- same as return ...</span>
<span class="kw">end</span>
<span class="co">-- prints "4 4 4 20 20" and "table: 0183B820 10 30 5"</span>
<span class="kw">vals</span><span class="ot">,</span> <span class="kw">val1</span><span class="ot">,</span> <span class="kw">_</span><span class="ot">,</span> <span class="kw">val3</span> <span class="ot">=</span> Varargs<span class="ot">(</span><span class="dv">10</span><span class="ot">,</span> <span class="dv">20</span><span class="ot">,</span> <span class="dv">30</span><span class="ot">,</span> <span class="dv">40</span><span class="ot">)</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">vals</span><span class="ot">,</span> <span class="kw">val1</span><span class="ot">,</span> <span class="kw">val3</span><span class="ot">,</span> select<span class="ot">(</span><span class="st">"#"</span><span class="ot">,</span> Varargs<span class="ot">(</span><span class="dv">10</span><span class="ot">,</span> <span class="dv">20</span><span class="ot">,</span> <span class="dv">30</span><span class="ot">,</span> <span class="dv">40</span><span class="ot">)))</span></code></pre>
<p><a name="C4"/></p>
<h2 id="bit-library">4 - Bit Library</h2>
<p>wxLua automatically loads a library for manipulating the bits of an integer number and puts it into the global <code class="sourceCode lua"><span class="kw">bit32</span></code> table. This is because wxWidgets often uses enumeration flags to control the behavior of functions and for compactly storing status information. You can easily "or" bits by adding them together and this is the preferred method, for example 0x02 + 0x04 = 0x06 or bitwise 0110. If the bits you're trying to "or" are not powers of 2 (perhaps one is a bit mask) this fails, 0x01 + 0x03 = 0x04 or bitwise 0100 (oops) instead of the desired 0011.</p>
<p>wxLua uses a bitlib library backported from Lua 5.2 and since the code for it is very small, it's embedded into the wxLua sourcecode.</p>
<p>All function arguments should be integers. The number of bits available for logical operations depends on the data type used to represent Lua numbers; this is typically 8-byte IEEE floats, which give 53 bits (the size of the mantissa).</p>
<p>The logical operations start with "b" for "bit" to avoid clashing with reserved words; although "xor" isn't a reserved word, it is consistent.</p>
<table>
<tbody>
<tr class="odd">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>rshift<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">disp</span><span class="ot">)</span></code></td>
<td align="left">-</td>
<td align="left">returns <code>x</code> shifted logically right <code>disp</code> places or left if negative</td>
</tr>
<tr class="even">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>band<span class="ot">(...)</span></code></td>
<td align="left">-</td>
<td align="left">returns the bitwise <code>and</code> of the input values</td>
</tr>
<tr class="odd">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>bnot<span class="ot">(</span><span class="kw">x</span><span class="ot">)</span></code></td>
<td align="left">-</td>
<td align="left">returns the one's complement of <code>x</code></td>
</tr>
<tr class="even">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>bor<span class="ot">(...)</span></code></td>
<td align="left">-</td>
<td align="left">returns the bitwise <code>or</code> of the input values</td>
</tr>
<tr class="odd">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>btest<span class="ot">(</span>···<span class="ot">)</span></code></td>
<td align="left">-</td>
<td align="left">returns true if the <code>and</code> of the input values is not 0</td>
</tr>
<tr class="even">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>bxor<span class="ot">(...)</span></code></td>
<td align="left">-</td>
<td align="left">returns the bitwise exclusive <code>or</code> of the input values</td>
</tr>
<tr class="odd">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>extract<span class="ot">(</span><span class="kw">n</span><span class="ot">,</span> <span class="kw">field</span> <span class="ot">[,</span> <span class="kw">width</span><span class="ot">])</span></code></td>
<td align="left">-</td>
<td align="left">returns the number of bits set in <code>n</code> for the given 0-31 starting <code>field</code> to optionally <code>field+width-1</code></td>
</tr>
<tr class="even">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>replace<span class="ot">(</span><span class="kw">n</span><span class="ot">,</span> <span class="kw">v</span><span class="ot">,</span> <span class="kw">field</span> <span class="ot">[,</span> <span class="kw">width</span><span class="ot">])</span></code></td>
<td align="left">-</td>
<td align="left">returns <code>n</code> with the bits in <code>field</code> to optionally <code>field+width-1</code> replaced by the value <code>v</code></td>
</tr>
<tr class="odd">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>lrotate<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">disp</span><span class="ot">)</span></code></td>
<td align="left">-</td>
<td align="left">returns <code>x</code> rotated <code>disp</code> bits to the left</td>
</tr>
<tr class="even">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>lshift<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">disp</span><span class="ot">)</span></code></td>
<td align="left">-</td>
<td align="left">returns <code>x</code> shifted left <code>disp</code> places or right if negative</td>
</tr>
<tr class="odd">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>rrotate<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">disp</span><span class="ot">)</span></code></td>
<td align="left">-</td>
<td align="left">returns <code>x</code> rotated <code>disp</code> bits to the right</td>
</tr>
<tr class="even">
<td align="left"><code class="sourceCode lua"><span class="kw">bit32</span><span class="ot">.</span>rshift<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">disp</span><span class="ot">)</span></code></td>
<td align="left">-</td>
<td align="left">returns <code>x</code> shifted right <code>disp</code> places or left if negative</td>
</tr>
</tbody>
</table>
<p><a name="C5"/></p>
<h2 id="programming-in-wxlua">5 - Programming in wxLua</h2>
<p>Programming in wxLua means that you're writing programs in the Lua language using an additional table of functions, objects, numbers, strings, and "classes" in the namespace table <code class="sourceCode lua"><span class="kw">wx</span></code> from wxWidgets. Additional wxWidgets libraries are added as their own bindings and placed in their own "namespace" table, but for the examples below the <code class="sourceCode lua"><span class="kw">wx</span></code> table is used.</p>
<p>wxLua creates five Lua tables for the binding functions and objects. These are in addition to the standard Lua tables; <code class="sourceCode lua"><span class="kw">coroutine</span><span class="ot">,</span> <span class="kw">debug</span><span class="ot">,</span> <span class="kw">io</span><span class="ot">,</span> <span class="kw">math</span><span class="ot">,</span> <span class="kw">os</span><span class="ot">,</span> <span class="kw">package</span><span class="ot">,</span> <span class="kw">string</span><span class="ot">,</span> <span class="kw">table</span></code>. Note that the <code>wxaui</code> and <code>wxstc</code> libraries have been separated into their own tables since they are fairly specialized libraries.</p>
<table>
<tbody>
<tr class="odd">
<td align="left"><strong>bit32</strong></td>
<td align="left">-</td>
<td align="left">A backport of the Lua 5.2 bit32 library for manipulating integer bits added to Lua 5.1.</td>
</tr>
<tr class="even">
<td align="left"><strong>wxlua</strong></td>
<td align="left">-</td>
<td align="left">Special functions for introspecting wxLua or</td>
</tr>
<tr class="odd">
<td align="left"></td>
<td align="left"></td>
<td align="left">generic functions that wxLua provides that are independent of wxWidgets.</td>
</tr>
<tr class="even">
<td align="left"><strong>wx</strong></td>
<td align="left">-</td>
<td align="left">wxWidgets functions, classes, defines, enums, strings,</td>
</tr>
<tr class="odd">
<td align="left"></td>
<td align="left"></td>
<td align="left">events, and objects are placed here.</td>
</tr>
<tr class="even">
<td align="left"><strong>wxaui</strong></td>
<td align="left">-</td>
<td align="left">The wxWidgets Advanced User Interface library.</td>
</tr>
<tr class="odd">
<td align="left"><strong>wxstc</strong></td>
<td align="left">-</td>
<td align="left">The wxStyledTextCtrl wrapper around the Scintilla text editor.</td>
</tr>
</tbody>
</table>
<p>The semantics for accessing wxWidgets elements in wxLua tries to map closely to the underlying C++ notation so that the official C++ documentation may be used as a reference, <a href="http://www.wxwidgets.org/docs">http://www.wxwidgets.org/docs</a>. The most common cases where wxLua deviates from C++ are for functions with values passed by reference that will be changed; wxLua will typically return multiple values instead. Please see the <a href="wxluaref.html">wxluaref.html</a> document that lists all the wxWidgets objects wrapped by wxLua and take note of the functions that are marked <em>%override</em> since you will need to use them as described in that document. You should also look at the <a href="binding.html">binding.html</a> file, even if you do not plan to write your own bindings, to get a better understanding of the <a href="wxluaref.html">wxluaref.html</a> file.</p>
<p><strong>Strings</strong> : wxLua does not typically use the wxString class for strings, rather it uses Lua strings. This means that all wxWidgets functions that take a wxString parameter take either a wxString userdata or preferrably a Lua string (Lua variables that are of type(var) == "string"). Functions that return wxStrings convert the value into a Lua string for convenience. The conversion from the Lua ANSI C 8-bit char* string to a wxString (which may be a Unicode wchar* string) is done internally.</p>
<p><strong>wxArrayString and wxSortedArrayString</strong> : Function parameters that take a "const wxArrayString& arr" or "wxArrayString arr" will accept either a wxArrayString userdata or a Lua table that has numeric indexes and string values and convert it into a wxArrayString for the function call. If the function call is "wxArrayString& arr" or "wxArrayString* arr" you must provide a wxArrayString userdata since the C++ function will most likely modify the wxArrayString that's passed to it.</p>
<p><strong>wxArrayInt</strong> : Function parameters that take a "const wxArrayInt& arr" or "wxArrayInt arr" will accept either a wxArrayInt userdata or a Lua table that has numeric indexes and numeric values and convert it into a wxArrayInt for the function call. If the function call is "wxArrayInt& arr" or "wxArrayInt* arr" you must provide a wxArrayInt userdata since the C++ function will most likely modify the wxArrayInt that's passed to it.</p>
<p><strong>Coroutines</strong> : wxLua works with coroutines with one important caveat.<br /><code class="boldred">NEVER connect an event handler function callback from within a coroutine</code></p>
<ol style="list-style-type: decimal">
<li>There is no way for wxLua to detect that a coroutine is closed until the garbage collector runs which is usually too late and the program crashes.</li>
<li>The callback function will probably only be called when the coroutine is yielded and Lua throws an error for this condition.</li>
<li>The <strong>one</strong> exception to this rule is if the whole wxLua program is run in a coroutine that never yields, which doesn't make typically make sense.</li>
</ol>
<p><a name="C5.1"/></p>
<h3 id="naming-location-and-usage-of-the-wxwidgets-objects-declared-in-the-c-header-files-in-the-wx-lua-table">5.1 - Naming, location, and usage of the wxWidgets objects declared in the C++ header files in the <code class="sourceCode lua"><span class="kw">wx</span></code> Lua table</h3>
<ul>
<li><strong>#define NUMBER_DEFINE VALUE</strong>
<ul>
<li>All #defined numerical values are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">NUMBER_DEFINE</span></code></li>
<li>Example <em>"#define wxID_ANY -1"</em> is accessed as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxID_ANY</span></code></li>
<li>Declared in the bindings using the <em>%define</em> tag.</li>
</ul></li>
<li><strong>[int, double, etc] NUMBER_VARIABLE;</strong>
<ul>
<li>All global numerical variables are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">NUMBER_VARIABLE</span></code></li>
<li>Example : <em>"extern const int wxInvalidOffset;"</em> is accessible as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxInvalidOffset</span></code>.</li>
<li>Declared in the bindings using the <em>%define</em> tag.</li>
</ul></li>
<li><strong>enum ENUM_NAMESPACE [or CLASSNAME::ENUM_NAMESPACE] { ENUM_NAME }</strong>
<ul>
<li>All global enums, named or not, are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">ENUM_NAME</span></code>
<ul>
<li>Example : <em>"enum wxDirection { wxLEFT, ... }"</em> is accessible as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxLEFT</span></code></li>
</ul></li>
<li>All enums that are members of classes are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">CLASSNAME</span><span class="ot">.</span><span class="kw">ENUM_NAME</span></code>
<ul>
<li>Example : <em>"enum wxFTP::TransferMode { ASCII, ... }"</em> is accessible as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxFTP</span><span class="ot">.</span><span class="kw">ASCII</span></code></li>
</ul></li>
<li>This follows the C++ semantics that you do not specify the name of an enum, but you do have to use its scope if it is a class member.</li>
<li>Declared in the bindings using the <em>%enum</em> tag.</li>
</ul></li>
<li><strong>#define STRING_DEFINE wxT("String Value")</strong>
<ul>
<li>All #defined string values are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">STRING_DEFINE</span></code></li>
<li>Example : <em>"#define wxIMAGE_OPTION_CUR_HOTSPOT_X wxT("HotSpotX")"</em> is accessible as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxIMAGE_OPTION_CUR_HOTSPOT_X</span></code></li>
<li>Declared in the bindings using the <em>%define_string</em> tag.</li>
</ul></li>
<li><strong>const wxChar* STRING_VARIABLE;</strong>
<ul>
<li>All global string variables are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">STRING_VARIABLE</span></code></li>
<li>No examples yet.</li>
<li>Declared in the bindings using the <em>%define_string</em> tag.</li>
</ul></li>
<li><strong>wxEVT_XXX for wxEvtHandler::Connect()</strong>
<ul>
<li>All <em>wxEVT_XXX</em> wxEventTypes (an integer) are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxEVT_XXX</span></code>
<ul>
<li>Example : <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxEVT_COMMAND_MENU_SELECTED</span></code> for menu item selection.</li>
</ul></li>
<li>wxLua does not use the static event tables, the EVT_XXX() macros, since it is not a compiled language, but rather the corresponding wxEVT_XXX identifier. The <a href="wxluaref.html">wxluaref.html</a> manual contains a complete mapping between the two.</li>
<li>Use the wxEvtHandler::Connect() function to connect event types to a wxEvtHandler; typically a wxWindow derived class.
<ul>
<li><p>Example : <em>EVT_MENU(id, func)</em> use <code class="sourceCode lua"><span class="kw">window</span>:Connect<span class="ot">(</span><span class="kw">menuId</span><span class="ot">,</span> <span class="kw">wx</span><span class="ot">.</span><span class="kw">wxEVT_COMMAND_MENU_SELECTED</span><span class="ot">,</span> <span class="kw">Lua</span> <span class="kw">function</span><span class="ot">)</span></code>. The Lua function must have the signature of <code class="sourceCode lua"><span class="kw">function</span> MenuEvent<span class="ot">(</span><span class="kw">event</span><span class="ot">)</span> <span class="ot">...</span> <span class="kw">handle</span> <span class="kw">event</span> <span class="ot">...</span> <span class="kw">return</span></code> where the <code>event</code> variable will be the wxEvent dervied class that the wxEventType was declared in, which in this case is a wxCommandEvent.</p></li>
<li>Example : <em>EVT_PAINT(func)</em> use <code class="sourceCode lua"><span class="kw">window</span>:Connect<span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxEVT_PAINT</span><span class="ot">,</span> <span class="kw">Lua</span> <span class="kw">function</span><span class="ot">)</span></code>. There is no Id used for this connect event function call since the paint event handler function is directly connected to the window. In the menu case, the handler is typically connected to a parent window, perhaps a wxFrame. The menu event event generated by the menu travels up the chain of window parents until a handler is found. The Id is needed to determine where the event came from.
<ul>
<li>Note: You must always create a wxPaintDC for wxEVT_PAINT to clear the update region.</li>
<li><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">dc</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPaintDC<span class="ot">(</span><span class="kw">event</span>:GetEventObject<span class="ot">()</span>:DynamicCast<span class="ot">(</span><span class="st">"wxWindow"</span><span class="ot">))</span></code> and then call <code class="sourceCode lua"><span class="kw">dc</span>:delete<span class="ot">()</span></code> at the end of the function because the paint event clears the "dirty" region to repaint and if it is not cleared another paint event will be sent... and so on.</li>
</ul></li>
</ul></li>
<li>Declared in the bindings using the <em>%define_event</em> tag.</li>
</ul></li>
<li><strong>Objects of classes or structs OBJECT_NAME</strong>
<ul>
<li>All global objects that are classes or structs are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">OBJECT_NAME</span></code></li>
<li>Example : <em>"const wxImage wxNullImage;"</em> is accessible as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxNullImage</span></code> and functions from the wxImage class can be called as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxNullImage</span>:Ok<span class="ot">()</span></code> which should return false.</li>
<li>Declared in the bindings using the <em>%define_object</em> tag.</li>
</ul></li>
<li><strong>Pointers to classes or structs POINTER_NAME</strong>
<ul>
<li>All global pointers that are classes or structs are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">POINTER_NAME</span></code></li>
<li>Example : <em>"extern wxPenList</em> wxThePenList;"* is accessible as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxThePenList</span></code> and functions from the wxPenList class can be made as <code class="sourceCode lua"><span class="kw">pen</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span><span class="kw">wxThePenList</span>:FindOrCreatePen<span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span>wxColour<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">,</span><span class="dv">3</span><span class="ot">),</span> <span class="dv">1</span><span class="ot">,</span> <span class="kw">wx</span><span class="ot">.</span><span class="kw">wxSOLID</span><span class="ot">)</span></code></li>
<li>Declared in the bindings using the <em>%define_object</em> tag.</li>
</ul></li>
<li><strong>Global C style functions VAR_TYPE FUNCTION_NAME(int a, const wxString& str)</strong>
<ul>
<li>All global C style functions are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span>FUNCTION_NAME<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span> <span class="st">"Hello"</span><span class="ot">)</span></code></li>
<li>Example : "<code class="sourceCode lua"><span class="kw">extern</span> <span class="kw">wxString</span> wxGetUserHome<span class="ot">(</span><span class="kw">const</span> <span class="kw">wxString</span>& <span class="kw">name</span><span class="ot">)</span><span class="st">"</span></code> is accessible as <code class="sourceCode lua"><span class="kw">home_dir</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxGetUserHome<span class="ot">(</span><span class="st">"john"</span><span class="ot">)</span></code> where wxString means to input a Lua string and a Lua string is returned.</li>
<li>Declared in the bindings using the <em>%function</em> tag.</li>
</ul></li>
<li><strong>C++ Classes CLASS_NAME</strong>
<ul>
<li>All C++ classes are available as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">CLASS_NAME</span></code>, however in order to use one you must call one of the constructors first or get the class as a return value from another function call.
<ul>
<li>Example : <code class="sourceCode lua"><span class="kw">pt</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">);</span> <span class="kw">pt2</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">(</span><span class="kw">pt</span><span class="ot">)</span></code></li>
<li>Multiple member functions with the same name are overloaded as in C++ and the proper function to call is determined at runtime. This is one of the reasons why wxLua is stricter about type than Lua. For example; string arguments do not accept numbers which Lua would silently convert.</li>
<li>Member functions inherited from the base class(es) are also available and may be overloaded as well.</li>
<li>The C++ classes are exposed as tables in Lua, but have a <code class="sourceCode lua"><span class="kw">__call</span></code> metatable item so they can be called as a function. If you need to get the constructor function itself you can use <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">CLASS_NAME</span><span class="ot">.</span>new<span class="ot">(...)</span></code> which is the constructor exposed as a Cfunction.</li>
</ul></li>
<li>The C++ class objects are pushed into Lua as a userdata wrapping a void* pointer to the C++ object.
<ul>
<li>A special metatable is set on the userdata with these entries :
<ul>
<li><code class="sourceCode lua"><span class="kw">__index</span></code> to call functions on the object or to get member variable values.</li>
<li><code class="sourceCode lua"><span class="kw">__newindex</span></code> to set new functions or values or set member variable values.</li>
<li><code class="sourceCode lua"><span class="kw">__tostring</span></code> to allow print() to show something useful
<ul>
<li><code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">())</span></code> prints "userdata: 0x858df5c [wxPoint(0x84ab550, 251)]", where 0x858df5c is the Lua userdata address, 0x84ab550 is the address of the wxPoint object, and 251 is the wxLua type that wxLua uses to determine that this Lua userdata wraps a wxPoint. The wxPoint type may not always be 251 since it depends on the number and order in which the bindings were initialized, but it will be unique within a run.</li>
</ul></li>
<li><code class="sourceCode lua"><span class="kw">__gc</span></code> to tell wxLua when the userdata is no longer used so wxLua can delete the C++ object if appropriate.</li>
</ul></li>
</ul></li>
<li><p>Declared in the bindings using the <em>%class</em> tag.</p></li>
<li><strong>Deleting class userdata</strong> can be done using the wxLua added class member function delete().
<ul>
<li>All classes that have the <em>%delete</em> binding tag will be eventually garbage collected when they go out of scope.</li>
<li>Classes without the <em>%delete</em> tag are assumed to be eventually attached to another object that will delete them for you.</li>
<li>The Lua garbage collector uses an incremental collector that waits until the data size reaches a limit and slowly removes them to avoid program slowdown. This is a good thing and makes Lua programs run at an even pace.</li>
<li>However! Some graphical device interface (GDI) classes need to be deleted immediately after you are done using them.
<ul>
<li>This is really a problem only in MS Windows. Windows 95 based systems are allowed only dozens of GDI objects at any one time, but even in NT systems (XP, Vista, 7) you will have problems if you've created hundreds of them. One visible sign that something is wrong is when controls, like menus, stop redrawing themselves properly and the program becomes sluggish.</li>
<li>In any case, just <code class="sourceCode lua">delete<span class="ot">()</span></code> them when done so that your program will work equally well in MSW as it would in Linux or OSX.</li>
</ul></li>
<li>Additionally, since the Lua userdata that wxLua pushes into Lua only store a void* pointer to the C++ class object, Lua can only think they are of size void* which are 8 bytes on 64-bit x86 machines. However, the class might store a 1024x768 x 3 bytes/pixel image as a wxImage (2.36Mb). There have been a number of discussions about this on the Lua mailing list, but currently there is no way to let Lua know the true size of a userdata to help it better decide when and what to garbage collect. The core of Lua could be modified, but that would make it harder to use wxLua as a loadable module.</li>
<li>The solution is to use the <code class="sourceCode lua">delete<span class="ot">()</span></code> function on certain types of userdata when you are done.</li>
<li>Care must be taken to ensure that you're not silently creating too many temporary objects.
<ul>
<li>Example: <code class="sourceCode lua"><span class="kw">wxDC</span>:SetPen<span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span>wxPen<span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span>wxColour<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">,</span><span class="dv">3</span><span class="ot">),</span> <span class="dv">1</span><span class="ot">,</span> <span class="kw">wx</span><span class="ot">.</span><span class="kw">wxSOLID</span><span class="ot">))</span></code>; notice that both a wxPen and a wxColour have been created, but there is no way for you to call <code class="sourceCode lua">delete<span class="ot">()</span></code> on them so they will collect until Lua runs its garbage collector.</li>
<li>You can force garbage collection using <code class="sourceCode lua"><span class="fu">collectgarbage</span><span class="ot">(</span><span class="st">"collect"</span><span class="ot">)</span></code> in Lua, but this may cause slight pauses in your program's execution.</li>
<li>It is a good idea to collect all the garbage at the end of your initialization function to at least start out with a clean slate since program startup time is usually not a concern.</li>
</ul></li>
<li>This is a list of classes by order of importance of deleting them:
<ul>
<li><code class="boldred">Must delete : wxDC, wxPaintDC, and ALL classed derived from wxDC.</code></li>
<li><code class="boldred">Must delete if > 50 : wxBitmap, wxBrush, wxColour, wxCursor, wxFont, wxIcon, wxPen, wxRegion</code>
<ul>
<li>To be sure, delete them when you're done and not worry.</li>
</ul></li>
<li>Must delete if large and you're creating many of them : wxImage, wxBitmap, etc.</li>
<li>Don't bother : wxPoint, wxRect... etc</li>
<li>Never delete : wxWindows attached their their parents (use Destroy()) or the wxMenuItems returned from a wxMenu.
<ul>
<li>Safe rule, don't <code class="sourceCode lua">delete<span class="ot">()</span></code> things that aren't yours. Refer to the wxWidgets documentation about whether an object will take ownership of an object passed to it.</li>
</ul></li>
</ul></li>
<li>How to tell how many userdata objects you currently have?
<ul>
<li>Print the output of <code class="sourceCode lua"><span class="kw">wxlua</span><span class="ot">.</span>GetGCUserdataInfo<span class="ot">(</span><span class="kw">true</span><span class="ot">)</span></code> to show what objects will be garbage collected when their reference count goes to zero and the Lua garbage collector runs.</li>
<li><p>Print the output of <code class="sourceCode lua"><span class="kw">wxlua</span><span class="ot">.</span>GetTrackedObjectInfo<span class="ot">(</span><span class="kw">true</span><span class="ot">)</span></code> to get class objects that wxLua has pushed into Lua that may or may not be garbage collected.</p></li>
<li><p>Call the function <code class="sourceCode lua"><span class="kw">wxlua</span><span class="ot">.</span>LuaStackDialog<span class="ot">()</span></code> when you run your program and examine the items in the Lua LUA_REGISTRYINDEX table. Expand "wxLua objects pushed" and "wxLua gc objects to delete" tables.</p></li>
</ul></li>
</ul></li>
<li><strong>Member functions</strong> of the class are called using the colon ':' convention and NOT the period '.'. This is because ':' in Lua puts the object itself on the stack, the "self" as it's called. The "self" is used by the binding code to call the function with. If you <em>really</em> want to use the '.' notation you can pass the "self" to the function as the first parameter. There are two exceptions to the ':' calling convention rule, <strong>properties</strong> and <strong>static</strong> functions. Please see the sections below about why they only use the '.' convention.
<ul>
<li>Example : <code class="sourceCode lua"><span class="kw">size</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxSize<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">);</span> <span class="kw">size</span>:SetWidth<span class="ot">(</span><span class="dv">10</span><span class="ot">);</span> <span class="kw">size</span><span class="ot">.</span>SetHeight<span class="ot">(</span><span class="kw">size</span><span class="ot">,</span> <span class="dv">11</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">size</span>:GetWidth<span class="ot">(),</span> <span class="kw">size</span><span class="ot">.</span>GetHeight<span class="ot">(</span><span class="kw">size</span><span class="ot">))</span></code> where we create a wxSize, set a new width and height, and then print the numerical values.</li>
<li>Functions may be renamed in the bindings using the <em>%rename</em> tag in the interface files. You call the functions in Lua using the name after the <em>%rename</em> tag with the same parameters as the original function. This is only done for special cases that would be awkward, if not impossible, to wrap otherwise.</li>
</ul></li>
<li><strong>Property functions</strong> allow you to read and/or write values to a class using the '.' convention and a shortened name.
<ul>
<li>These are generated on the fly when the function is called on a wxLua userdata and work only for functions following these rules.
<ul>
<li>GetXXX() takes no values and returns one.</li>
<li>SetXXX(value) takes one value and returns none.</li>
</ul></li>
<li>The Get/Set part of the function name is removed leaving only XXX and you do not call them like a function using "()", but rather like accessing a table member, without the "()".</li>
<li>Example : "<code class="sourceCode lua"><span class="kw">rect</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxRect<span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">),</span> <span class="kw">wx</span><span class="ot">.</span>wxSize<span class="ot">(</span><span class="dv">3</span><span class="ot">,</span><span class="dv">4</span><span class="ot">));</span> <span class="kw">rect</span>:SetX<span class="ot">(</span><span class="dv">20</span><span class="ot">);</span> <span class="kw">rect</span><span class="ot">.</span><span class="kw">X</span> <span class="ot">=</span> <span class="dv">10</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">rect</span><span class="ot">.</span><span class="kw">X</span><span class="ot">,</span> <span class="kw">rect</span><span class="ot">.</span><span class="kw">X</span> <span class="ot">==</span> <span class="kw">rect</span>:GetX<span class="ot">(),</span> <span class="kw">rect</span><span class="ot">.</span><span class="kw">X</span> <span class="ot">==</span> <span class="kw">rect</span><span class="ot">.</span>GetX<span class="ot">(</span><span class="kw">rect</span><span class="ot">))</span></code>" should print "10, true, true".</li>
<li>Note : There is no way to find out from Lua if the code used a '.' or a ':' to call the function and therefore properties cannot be made to work for the ':' calling convention since in that case we have to remove the object (the self) that Lua automatically pushes onto the stack that we don't need or want.</li>
<li>Note : Since these methods are generated at runtime they will not work for static functions in the class table, but they will work for static functions called from a userdata object.</li>
<li>Below is a list of what works and doesn't, the example is for a static class member function, but the same works for a regular class member function.
<ul>
<li>Works (static functions only) : <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxFileName</span><span class="ot">.</span>GetCwd<span class="ot">())</span></code> and <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxFileName</span><span class="ot">.</span><span class="kw">GetCwd</span><span class="ot">)</span></code> prints that it's a function.</li>
<li>Fails (static functions only) : <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxFileName</span>:GetCwd<span class="ot">())</span></code> and <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxFileName</span>:<span class="kw">GetCwd</span><span class="ot">)</span></code> since there is no "self" for the ':' operator to push.</li>
<li>Works : <code class="sourceCode lua"><span class="kw">f</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxFileName<span class="ot">(</span><span class="st">"a"</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">f</span><span class="ot">.</span>GetCwd<span class="ot">())</span></code> and <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">f</span><span class="ot">.</span><span class="kw">GetCwd</span><span class="ot">)</span></code> prints that it's a function.</li>
<li>Fails : <code class="sourceCode lua"><span class="kw">f</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxFileName<span class="ot">(</span><span class="st">"a"</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">f</span>:GetCwd<span class="ot">())</span></code> and <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">f</span>:<span class="kw">GetCwd</span><span class="ot">)</span></code> since there is no "self" for the ':' operator to push.</li>
<li>Works : <code class="sourceCode lua"><span class="kw">f</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxFileName<span class="ot">(</span><span class="st">"a"</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">f</span><span class="ot">.</span><span class="kw">Cwd</span><span class="ot">)</span></code></li>
<li>Fails : <code class="sourceCode lua"><span class="kw">f</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxFileName<span class="ot">(</span><span class="st">"a"</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">f</span><span class="ot">.</span>Cwd<span class="ot">())</span></code> and <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">f</span>:<span class="kw">Cwd</span><span class="ot">)</span></code> and <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">f</span>:Cwd<span class="ot">())</span></code></li>
</ul></li>
<li>Note : Are properties really necessary? Confusing? Useful? I'd stick with the Get/Set functions. - JL</li>
</ul></li>
<li><strong>Member variables</strong> allow you to read and/or write to member variables of a class.
<ul>
<li>Declared in the interface files using the <em>%member</em> or <em>%member_func</em> tag.</li>
<li>Example : In the interface file <em>gdi.i</em> this is declared for wxPoint : <em>"%rename X %member_func int x"</em></li>
<li>The wxPoint class does not have functions to access the int x, y member variables so we create our own.</li>
<li>The <em>%member_func</em> tag creates functions called Get/Set[variable name] or in this case Getx() and Setx(value), but these aren't too pretty so we use the <em>%rename</em> tag to rename them to GetX() and SetX(value). It additionally creates properties for the x variable so the '.' calling convention also works.</li>
<li>These two methods of getting and setting the x and y member of the wxPoint class are interchangeable.
<ul>
<li>Example : <code class="sourceCode lua"><span class="kw">pt</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">);</span> <span class="kw">pt</span><span class="ot">.</span><span class="kw">x</span> <span class="ot">=</span> <span class="dv">10</span><span class="ot">;</span> <span class="kw">pt</span>:SetY<span class="ot">(</span><span class="dv">11</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">pt</span><span class="ot">.</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">pt</span>:GetY<span class="ot">())</span></code></li>
</ul></li>
<li>If the <em>%member</em> tag is used (as opposed to the <em>%member_func</em>) the Get/Set functions are not generated and the member variables are accessible only through the properties.</li>
<li>If the member variable is constant (const) the variable is read-only and you cannot set its value.</li>
</ul></li>
<li><strong>Static functions</strong> are part of the table that holds the class and can be called with or without a class instance (a userdata).
<ul>
<li>Example : <code class="sourceCode lua"><span class="kw">f</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxFileName<span class="ot">(</span><span class="st">'dummy'</span><span class="ot">);</span> <span class="kw">f</span><span class="ot">.</span>GetCwd<span class="ot">()</span> <span class="ot">==</span> <span class="kw">wx</span><span class="ot">.</span><span class="kw">wxFileName</span><span class="ot">.</span>GetCwd<span class="ot">()</span></code></li>
<li>Note that you always use the '.' calling convention since static C++ functions do not require the object "self" or in Lua's case the userdata.</li>
<li>See the <strong>properties</strong> section about the difference between '.' and ':' and why using a ':' cannot be made to work reliably when you don't want or need the self pushed onto the stack.</li>
</ul></li>
<li><strong>Enum members</strong> are also part of the table that holds the class and are used by accessing the class table itself.
<ul>
<li>Example : <em>"enum wxFTP::TransferMode { ASCII, ... }"</em> is accessible as <code class="sourceCode lua"><span class="kw">wx</span><span class="ot">.</span><span class="kw">wxFTP</span><span class="ot">.</span><span class="kw">ASCII</span></code></li>
</ul></li>
<li><strong>Operator functions</strong> allow you to use C++ operators in Lua.
<ul>
<li>Lua has a limited set of operators, see the Lua primer, whereas C++ can define many more. Also, not all C++ operators are defined for all classes and very few classes can be mixed with other classes. Operators for classes must be declared in C++. Therefore Lua uses functions to call these operators rather than try to directly modify the existing ones in Lua and then declare the remaining ones as functions.</li>
<li>Note also that declaring every operator for all classes in wxLua is usually not necessary because in C++ they typically shadow an existing function that wxLua already wraps.</li>
<li>The Lua = operator for a class object will merely create a reference to the object and this itself is useful and why wxLua does not try to override the default behavior.</li>
<li>Declared in the interface files using the <em>%operator</em> tag.</li>
<li><p>This is a list of all possible operator functions:</p>
<table>
<tbody>
<tr class="odd">
<td align="left"><strong>Relational and equality operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">==</td>
<td align="left">op_eq()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">!=</td>
<td align="left">op_ne()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">></td>
<td align="left">op_gt()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><</td>
<td align="left">op_lt()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">>=</td>
<td align="left">op_ge()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><=</td>
<td align="left">op_le()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><strong>Logical operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">!</td>
<td align="left">op_not()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">&&</td>
<td align="left">op_land()</td>
<td align="left">"l" stands for logical</td>
</tr>
<tr class="odd">
<td align="left">||</td>
<td align="left">op_lor()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><strong>Bitwise operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">~</td>
<td align="left">op_comp()</td>
<td align="left">bitwise NOT or complement</td>
</tr>
<tr class="even">
<td align="left">&</td>
<td align="left">op_and()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">|</td>
<td align="left">op_or()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">^</td>
<td align="left">op_xor()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><<</td>
<td align="left">op_lshift()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">>></td>
<td align="left">op_rshift()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><strong>Inplace bitwise assignment operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">&=</td>
<td align="left">op_iand()</td>
<td align="left">"i" stands for inplace</td>
</tr>
<tr class="odd">
<td align="left">|=</td>
<td align="left">op_ior()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">^=</td>
<td align="left">op_ixor()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">>>=</td>
<td align="left">op_irshift()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><<=</td>
<td align="left">op_ilshift()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><strong>Arithmetic operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">=</td>
<td align="left">op_set()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">+</td>
<td align="left">op_add()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">-</td>
<td align="left">op_sub()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">*</td>
<td align="left">op_mul()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">/</td>
<td align="left">op_div()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">%</td>
<td align="left">op_mod()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><strong>Unary arithmetic operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">-</td>
<td align="left">op_neg()</td>
<td align="left">negate</td>
</tr>
<tr class="even">
<td align="left"><strong>Inplace arithmetic assignment operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">+=</td>
<td align="left">op_iadd()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">-=</td>
<td align="left">op_isub()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">*=</td>
<td align="left">op_imul()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">/=</td>
<td align="left">op_idiv()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">%=</td>
<td align="left">op_imod()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><strong>Increment arithmetic operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">++</td>
<td align="left">op_inc()</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">--</td>
<td align="left">op_dec()</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><strong>Other operators</strong></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">[]</td>
<td align="left">op_index()</td>
<td align="left">Array indexing</td>
</tr>
<tr class="odd">
<td align="left">()</td>
<td align="left">op_func()</td>
<td align="left">Function call</td>
</tr>
<tr class="even">
<td align="left">*</td>
<td align="left">op_deref()</td>
<td align="left">Dereference/Indirection</td>
</tr>
</tbody>
</table></li>
<li>Example : <code class="sourceCode lua"><span class="kw">pt</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">);</span> <span class="kw">pt</span> <span class="ot">=</span> <span class="kw">pt</span> <span class="ot">+</span> <span class="dv">1</span></code> gives an error since we're trying to use the Lua operator and even in C++ this wouldn't compile since there is no operator defined for adding a single number to a wxPoint.</li>
<li>Example : <code class="sourceCode lua"><span class="kw">pt1</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">);</span> <span class="kw">pt2</span> <span class="ot">=</span> <span class="kw">pt1</span><span class="ot">;</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">pt2</span>:GetX<span class="ot">());</span> <span class="kw">pt2</span>:SetX<span class="ot">(</span><span class="dv">10</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">pt1</span>:GetX<span class="ot">(),</span> <span class="kw">pt1</span> <span class="ot">==</span> <span class="kw">pt2</span><span class="ot">)</span></code> the = operator works in this case because we are not copying the values of pt1, but rather the address of pt1 to pt2, meaning that pt2 <strong><em>is</em></strong> pt1 and when we set the value for pt2 we're also setting the value for pt1. We know this because the Lua == operators tells us that they have the same address and if you type <code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="kw">pt1</span><span class="ot">,</span> <span class="kw">pt2</span><span class="ot">)</span></code> the result is <em>"userdata: 0x858df5c [wxPoint(0x84ab550, 251)], userdata: 0x858df5c [wxPoint(0x84ab550, 251)]"</em>. See the wxLua userdata metatable function <code class="sourceCode lua"><span class="kw">__tostring</span></code> above.</li>
<li><p>Example : <code class="sourceCode lua"><span class="kw">pt1</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">(</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">);</span> <span class="kw">pt2</span> <span class="ot">=</span> <span class="kw">wx</span><span class="ot">.</span>wxPoint<span class="ot">();</span> <span class="kw">pt2</span>:op_set<span class="ot">(</span><span class="kw">pt1</span><span class="ot">);</span> <span class="kw">pt1</span>:SetX<span class="ot">(</span><span class="dv">10</span><span class="ot">);</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">pt2</span>:GetX<span class="ot">());</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">pt2</span>:op_eq<span class="ot">(</span><span class="kw">pt1</span><span class="ot">))</span></code> Creates pt1 and also creates pt2, but we don't care what value it has since we use op_set() to call the C++ <code>=</code> operator to copy the values from pt1. We then change the value of pt1, test if pt2 has changed, it hasn't, and the test to see if they're still equal and as expected, they're not.</p></li>
</ul></li>
<li>Virtual functions
<ul>
<li>You cannot arbitrarily override C++ virtual functions in wxLua as this must be done in C++. The C++ class must be subclassed and the virtual functions overridden to check to see if there is a Lua function to call instead of the base class function. This has only been implemented for cases where it is necessary. In many cases you can intercept the appropriate wxEvent and change the behavior from within the handler.</li>
<li>Examples of virtual functions that must or can be overridden are in the wxLuaPrintout class, see the printing sample, and wxLuaHtmlWindow, see html sample. The only virtual functions that you can override are marked with comments in the binding files and <a href="wxluaref.htm">wxluaref.htm</a>.</li>
<li>Prepend the function name with an underscore, '_' in order to directly access the C++ base class function that you have overridden in Lua.</li>
<li>Adding virtual functions to wxLua will be done on a case by case basis. If it is absolutely necessary to override a virtual function for a class you should ask about adding it on the wxLua mailing list. The reason why they have not already been added is that there is a price to pay in terms of binding size and speed.</li>
</ul></li>
<li><strong>Overriding member functions</strong> with Lua functions
<ul>
<li>You may override class member functions for a wxLua userdata and still be able to call back to the Cfunction by prepending the function name with an underscore '_'.</li>
<li>The Lua function that you assign to a userdata will be called directly from Lua without regard to the existing Cfunction(s), but will never be called by the C++ class functions.</li>