forked from pkulchenko/wxlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxlua.txt
1756 lines (1516 loc) · 89.1 KB
/
wxlua.txt
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
% wxLua 3.0.0.8 - User Manual
% John Labenski, Paul Kulchenko
% 2019-12-30
[wxLua](http://wxlua.sourceforge.net) is a [Lua](http://www.lua.org) scripting
language wrapper around the [wxWidgets](http://www.wxwidgets.org)
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 `require("wx")`{.lua} when using the standard Lua executable,
and a library for extending C++ programs with a fast, small, fully embeddable
scripting language.
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.
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.
Additionally, wxLua adds a library for manipulating the bits of integer
numbers using a back-ported `bit32` library from Lua 5.2.
### References:
------------------------ --- --------------------------------------------------------------------------------
wxLua website - [http://wxlua.sourceforge.net](http://wxlua.sourceforge.net)
wxLua Sourceforge page - [http://sourceforge.net/projects/wxlua](http://sourceforge.net/projects/wxlua)
Lua website - [http://www.lua.org](http://www.lua.org)
wxWidgets website - [http://www.wxwidgets.org](http://www.wxwidgets.org)
Mailing list - [wxlua-users@lists.sourceforge.net](wxlua-users@lists.sourceforge.net)
------------------------ --- --------------------------------------------------------------------------------
## Table of Contents
1. [Version Information](#C1)
2. [Requirements](#C2)
3. [Brief Introduction to Lua](#C3)
4. [Bit Library](#C4)
5. [Programming in wxLua](#C5) \
5.1 [Naming, location, and usage of the wxWidgets objects declared in the C++ header files in the wx Lua table](#C5.1)
6. [wxLua Samples and How to Run Them](#C6) \
6.1 [How to Run the Samples](#C6.1) \
6.2 [Provided Samples](#C6.2)
7. [wxLua Applications](#C7) \
7.1 [wxLua](#C7.1) \
7.2 [wxLuaEdit](#C7.2) \
7.3 [wxLuaFreeze](#C7.3) \
7.4 [Lua Module using require()](#C7.4)
8. [wxLua Utils](#C8) \
8.1 [bin2c.lua](#C8.1)
9. [wxLua Sourcecode Modules](#C9)
10. [wxLua C++ Programming Guide](#C10) \
10.1 [Data stored in Lua's LUA_REGISTRYINDEX table](#C10.1) \
10.2 [Functions to Create a wxLuaState](#C10.2) \
10.3 [Using a wxLuaState](#C10.3)
<a name="C1"/>
## 1 - Version Information
- The wxLua version number is set to the stable version of wxWidgets
that it has been updated to.
- The revision number (X.Y.Z.revision) can be thought of as the wxLua
version number.
- Lua 5.1.5, 5.2.3, or 5.3
- wxLua uses a nearly unmodified copy of Lua 5.1.5 or 5.2.3.
- Official patches on [www.lua.org](www.lua.org) are applied as
they are released.
- 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.
- Any program that works using the official release of Lua will
work in wxLua.
- wxWidgets 3.x
- wxLua currently compiles with wxWidgets versions 2.8.x, 2.9.x, and 3.x.
- It may compile with newer versions of wxWidgets as well as older ones.
- The interface files have `#ifdefs` 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.
- Note for wxWidgets < 2.9 : wxLua makes use of the
wxStyledTextCtrl contrib library in `wxWidgets/contrib/src/stc`.
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.
- The wxLua library links to these wxWidgets libs:
`stc, xrc, html, media, adv, net, xml, core, base, tiff, jpeg, png, zlib, regex, expat`.
- See the wxLua [install.html](install.html) for more information.
<a name="C2"/>
## 2 - Requirements
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
[install.html](install.html) file.
<a name="C3"/>
## 3 - Brief Introduction to Lua
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
[www.lua.org](http://www.lua.org).
- **Comments**
- Single line comments
- `-- rest of line is commented`{.lua}
- Multiple line block or inline comments
- `--[[ multiple line or inline comment ... ]]`{.lua}
- **Variables**
- Variables are not permanently typed and you can freely overwrite
them with other values or types, there is no "const" keyword.
- Variables are global unless you put the keyword
`local`{.lua} in front of them, this is sometimes good practice.
- The scope of `local`{.lua} variables is limited to the
current scope and its children.
- 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.
- Local variables are faster than global variables because they do
not require a table lookup for use.
- A `local`{.lua} variable created with the same name as a global
variable supersedes the global variable within the scope of the
local variable.
- The function `type(var_name)`{.lua} returns the
variable type as a string.
- The eight variables types are: `nil, boolean, number, string,
table, function, userdata, thread`{.lua}
- **Lua Types**
- **nil** : A special value meaning NULL or nothing.
- `a = nil; local b; print(a, b, type(a), type(b))`{.lua} ;
prints "nil nil nil nil"
- Variables that have not been assigned a value are `nil`{.lua}
and any variable can be reset back to `nil`{.lua} at any time
to allow the Lua garbage collector to delete them if there are
no other references to it.
- This value is often returned for functions that fail.
- You can provide an inline alternative to `nil`{.lua} using the
`or`{.lua} keyword since `nil`{.lua} evaluates to false.
- `print(tonumber("a"), tonumber("a") or 1)`{.lua}
; prints "nil 1"
- **boolean** : `true`{.lua} or `false`{.lua}
- `a = true; b = false; print(a, b, type(a), type(b))`{.lua} ;
prints "true false boolean boolean"
- Note that `nil`{.lua} works as `false`{.lua}, but the
number 0 evaluates as `true`{.lua} since it has a value,
i.e. not `nil`{.lua}, use `(a ~= 0)`{.lua}.
- `a = 0; if a then print(a) end`{.lua} ; prints "0" since
the variable "`a`{.lua}" evaluates to `true`{.lua}.
- **number** : All numbers in Lua are double valued floating point
numbers.
- `a = 1; b = 3.14; print(a, b, type(a), type(b))`{.lua} ;
prints "1 3.14 number number"
- `n = (1E1 * 3.14 * math.sin(1) / 4)*math.pow(2.5e-1, 4)`{.lua}
- Variables can be coerced into numbers using the function
`tonumber(variable)`{.lua} which returns `nil`{.lua}
on failure.
- Additional math functions are in the `math`{.lua} table.
- **string** : Strings in Lua can have embedded nulls "\\0" and use the
same escape characters as C.
- `a = "hello"; b = a; b = "hi"; print(a, b, #a, type(a))`{.lua}
; prints "hello hi 5 string"
- 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.
- String variables are copy on write and the example above
shows that overwriting "b" does not change "a".
- `s = "How are 'you'!"`{.lua} or
`s = 'How\tare "You"!\n'`{.lua} are both valid since
either " or ' can be used to quote strings.
('\\t' = tab, '\\n' = line feed)
- `s = [[How are "'you'"!]]`{.lua} ; double brackets can be used
to create multiline strings that include new lines and whitespace.
- Concatenate strings using the `..`{.lua} operator
- `str1 = "hello"; str2 = "number"; str3 = str1.." "..str2.." "..tostring(2).."!"`{.lua}
- Numbers can be coerced into strings as `("A "..2)`{.lua},
but not `(2.."A")`{.lua} since the left hand side of the
`..`{.lua} operator must be a string.
- Many strings should be concatenated together by putting them
into a table array using `table.insert()`{.lua}
(or appended using `table[#table+1] = "hello"`{.lua}) and
then `table.concat()`{.lua} called to create the
single string result. The concatenation operator `..`{.lua}
is slower since each intermediary string has to be reallocated
and hashed. The `table.concat()`{.lua} method only has to
allocate and hash the resultant string once and its speed is
quite competitive to other scripting languages.
- Variables can be coerced into strings using the function
`tostring(variable)`{.lua} which returns `nil`{.lua}
on failure.
- Additional string functions are in the `string`{.lua} table.
- **table** : Tables can be indexed by and hold values of numbers,
strings, functions, userdata, other tables...
- `a = {5}; b = a; b[1] = 6; print(a, b, a[1], b[1], b[2], type(a), type(b))`{.lua}
- prints "table: 01780F98 table: 01780F98 6 6 nil table table"
- Printing a table is the same as calling `tostring(t)`{.lua}
on the table which displays the variable type and its
memory address.
- 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.
- 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.
- `t = {}`{.lua} creates an empty table.
- You must declare a variable as a table before using its
indexes.
- `t = { ["a"] = 5, "first", "second", B = 7 };
print(t.a, t["a"], t[0], t[1], t[2], t.B, t.b, t.c)`{.lua}
- prints "5 5 nil first second 7 nil nil"
- Set values as : `t.a = 2; t["a"] = 3; t[10] = 4`{.lua}
- Elements are automatically created when assigning new values
to elements that don't already exist and accessing elements
that don't exist returns `nil`{.lua}.
- Clear values by setting them to `nil`{.lua}, e.g.
`t.a = nil`{.lua}
- The length operator `#t`{.lua} 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.
- The length of a table used as an array with holes
(nil values) is undefined.
- The only guarantee is that the value at
`t[#t+1]`{.lua} will be `nil`{.lua}.
- 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.
- Lua table arrays have a starting index of 1.
- `for k, v in ipairs(t) do print(k, v) end`{.lua}
; Note: (k = key, v = value)
- prints only the array values :
"1 first" and "2 second"
- `for k, v in pairs(t) do print(k, v) end`{.lua}
; Note: (k = key, v = value)
- prints all table values (unordered) :
"1 first", "2 second", "a 5", "B 7"
- Functions defined by Lua are placed into tables, a namespace
if you will, to keep the global namespace uncluttered.
- See `table.XXX, string.XXX, math.XXX, os.XXX`{.lua} etc.
in the Lua documentation.
- wxLua places the wxWidgets bindings into the
`wx.XXX`{.lua} table "namespace".
- The global table is called `_G`{.lua} and you can display it
as you would any table using :
- `for k, v in pairs(_G) do print(k, v) end`{.lua}
- Additional table functions are in the `table`{.lua} table.
- **userdata** : A pointer to a C/C++ object.
- 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.
- wxLua uses userdata types to wrap the wxWidgets C++ objects to make
them useable in a Lua program.
- **function** : A handle to a function.
- `function f(a, b) return a+b end; print(f, f(1,2), type(f))`{.lua}
- `f = function (a, b) return a+b end; print(f, f(1,2), type(f))`{.lua}
- Both lines above print "function: 01DE2AF8 3 function", where
01DE2AF8 is the unique memory address of the function.
- Printing a function is the same as calling
`tostring(f)`{.lua} on the function which displays the
variable type and its memory address.
- Functions can return multiple values and be passed more or
less variables than specified.
- Unassigned inputs are set to `nil`{.lua} and unassigned
return values are discarded.
- Functions can be assigned to variables or put into tables.
- `t = {}; t["Add"] = f; print(t.Add(1, 2), t.Add)`{.lua}
; prints "3 function: 01DE2AF8
- 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 `nil`{.lua}.
- 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.
- Tables and userdata are passed to functions by reference and
changes to them within the function body
does modify the original variable's value.
- `function table_append(t, v) t[#t+1] = v; v = 0; end; tbl = {};
item = "hello"; table_append(tbl, item); print(tbl[#tbl], item)`{.lua}
- prints "hello hello", i.e.
`tbl[1] == "hello"`{.lua} and the variable
"`item`{.lua}" still equals "hello" and was not changed.
- **Operators**
- Relational : `== ~= < > <= >=`{.lua}
(Note: not equal is `~=`{.lua})
- Logical: `and, or, not`{.lua}
- Precedence (low to high):
1. `or`{.lua}
2. `and`{.lua}
3. `< > <= >= ~= ==`{.lua}
4. `..`{.lua} (string concatenation)
5. `+ -`{.lua}
6. `* / %`{.lua}
7. `not # -`{.lua} (unary)
8. `^`{.lua}
- **Keywords**
- `and break do else elseif end false for function if in local nil not
or repeat return then true until while`{.lua}
- **do ... end**
- Create a new local scope in a `do ... end`{.lua} block.
- Note : You cannot write
`function printHi() return; print("hi") end`{.lua},
but you can write
`function printHi() do return end; print("hi") end`{.lua}
which can be useful for debugging functions.
~~~{.lua}
do
-- create a new local scope
local a = 2
end
~~~
- **if (bool) then ... elseif (bool) then ... else ... end**
~~~{.lua}
local a, b, c = 1, 2, 3 -- can assign multiple values
a = 1; b = 2; c = 3 -- use ; for multiple lines of code on single line
a, b, c = 1, 2, 3 -- this works too
if (a == 1) and ((b <= 2) or (c ~= 3)) then
print(a+b/c)
elseif a == 2 then -- no parentheses necessary
print(a)
else
print(b)
end
~~~
- **There is no case statement**, but
`table[value] = function() ... end`{.lua} may be used to simulate one.
~~~{.lua}
case = {}
case[1] = function() print("Hello #1") end -- anonymous function
case[2] = function() print("Hello #2") end
...
if case[value] then
case[value]()
else
print("Unknown case value")
end
~~~
- **while (bool) ... end**
- Note : there is no "continue" keyword only `break`{.lua}
~~~{.lua}
function Check5(val) -- returns nil if val ~= 5
if val == 5 then
return true
end
end
local a = 1
while a < 10 do
print(a)
if Check5(a) then break end
a = a + 1 -- no increment operator
end
~~~
- You can simulate a "continue" statement by adding an inner
while loop (doesn't print \# 5).
~~~{.lua}
local a = 0
while a < 10 do while true do
a = a + 1 -- no increment operator
if Check5 (a) then
break -- break in the inner while loop to "continue" in the outer loop
else
print(a)
end
break end -- break out of inner while loop
end
~~~
- **repeat ... until (bool)**
- Note : there is no "continue" keyword only `break`{.lua}
~~~{.lua}
local a = 10
repeat
local temp = a * 2
print(temp, type(temp))
a = a - 1 -- no decrement operator
until a < 0
~~~
- **for var = init_value, end_value [, increment] do ... end**
- Note : there is no "continue" keyword only `break`{.lua}
- You cannot modify the loop variable, limit, or increment from
within the loop.
- 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.
- Copy the loop counter variable to a separate variable if you
need to save it when breaking for example.
~~~{.lua}
local a = "hello"
for a = 1, 10 --[[, increment]] do -- optional increment value, default increment is 1
local temp = a * 2
print(temp)
end
print(a) -- a == "hello" since loop counter variable is local to the loop
~~~
- **functions**
- Input any number of parameters by value, tables and userdata are
passed by reference.
- Missing input variables are assigned the value `nil`{.lua},
extra inputs are discarded.
- Return values using the "`return`{.lua}" keyword.
- Not all return values need to be used and extra ones will be discarded.
- The symbol "_" is a valid variable name and is often used as a
dummy variable to receive return values that you do not want.
- `function GetNums() return 3, 4, 5, 6 end; local _, num2 = GetNums()`{.lua}
- You may also use the `select(n, ...)`{.lua}
function to choose return values to use.
- `print(select("#", GetNums()), "and", select(3, GetNums()))`{.lua}
; prints "4 and 5 6".
- Note `select(n, ...)`{.lua} returns all args after
`n`{.lua}, but if the left hand side is a single
variable the others are discarded.
- Tables can be used as containers for input values to functions
using the `unpack(table, [i, [, j]])`{.lua} function.
- This is useful for creating complex inputs to a function or
storing them for reuse.
- `print(string.find(unpack({"Hello", "ll", 1, 1})))`{.lua}
; prints "3 4"
- Vararg inputs are written as `function dostuff( ... ) end`{.lua}
- The number of args is found using:
`local n_args = select("#", ...)`{.lua}
- The args can be converted into table using:
`local args = { ... }`{.lua}
- A particular arg can be chosen using:
`local arg3 = select(3, ...)`{.lua}
- See above about using `select()`{.lua} to pick
return values of a function as well.
~~~{.lua}
-- Print the keys in table t that have the given values.
function PrintKeys(t, values, cmp_case)
-- use nested functions for repetitive code or to simplify code
local function cmp_values(a, b)
if cmp_case then -- can use upvalue variables
return a == b
else
return string.lower(a) == string.lower(b)
end
end
local function find_key(t, val)
for k,v in pairs(t) do
if cmp_values(val, v) then return k end
end
end
for i = 1, #values do
print(find_key(t, values[i]), values[i])
end
end
data = {a1 = "a2", b1 = "b2", c1 = "c2"}
-- prints "a1 a2", "b1 b2", "nil C2"
PrintKeys(data, {"a2", "b2", "C2"}, true)
~~~
~~~{.lua}
-- Varargs example demonstrating the different ways to handle them.
function Varargs(...)
local args = {...}
print(select("#", ...), #{...}, #args, args[2], unpack({...}, 2, 2))
return args, unpack(args) -- same as return ...
end
-- prints "4 4 4 20 20" and "table: 0183B820 10 30 5"
vals, val1, _, val3 = Varargs(10, 20, 30, 40)
print(vals, val1, val3, select("#", Varargs(10, 20, 30, 40)))
~~~
<a name="C4"/>
## 4 - Bit Library
wxLua automatically loads a library for manipulating the bits of an integer
number and puts it into the global `bit32`{.lua} 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.
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.
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).
The logical operations start with "b" for "bit" to avoid clashing with
reserved words; although "xor" isn't a reserved word, it is consistent.
--------------------------------------------- --- ---------------------------------------------------------------------------------
`bit32.rshift(x, disp)`{.lua} - returns `x` shifted logically right `disp` places or left if negative
`bit32.band(...)`{.lua} - returns the bitwise `and` of the input values
`bit32.bnot(x)`{.lua} - returns the one's complement of `x`
`bit32.bor(...)`{.lua} - returns the bitwise `or` of the input values
`bit32.btest(···)`{.lua} - returns true if the `and` of the input values is not 0
`bit32.bxor(...)`{.lua} - returns the bitwise exclusive `or` of the input values
`bit32.extract(n, field [, width])`{.lua} - returns the number of bits set in `n` for the given 0-31 starting `field` to optionally `field+width-1`
`bit32.replace(n, v, field [, width])`{.lua} - returns `n` with the bits in `field` to optionally `field+width-1` replaced by the value `v`
`bit32.lrotate(x, disp)`{.lua} - returns `x` rotated `disp` bits to the left
`bit32.lshift(x, disp)`{.lua} - returns `x` shifted left `disp` places or right if negative
`bit32.rrotate(x, disp)`{.lua} - returns `x` rotated `disp` bits to the right
`bit32.rshift(x, disp)`{.lua} - returns `x` shifted right `disp` places or left if negative
--------------------------------------------- --- ---------------------------------------------------------------------------------
<a name="C5"/>
## 5 - Programming in wxLua
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 `wx`{.lua} from wxWidgets.
Additional wxWidgets libraries are added as their own bindings and placed in
their own "namespace" table, but for the examples below the `wx`{.lua} table
is used.
wxLua creates five Lua tables for the binding functions and objects.
These are in addition to the standard Lua tables;
`coroutine, debug, io, math, os, package, string, table`{.lua}.
Note that the `wxaui` and `wxstc` libraries have been separated into their own
tables since they are fairly specialized libraries.
---------- --- ------------------------------------------------------------
**bit32** - A backport of the Lua 5.2 bit32 library for manipulating integer bits added to Lua 5.1.
**wxlua** - Special functions for introspecting wxLua or
generic functions that wxLua provides that are independent of wxWidgets.
**wx** - wxWidgets functions, classes, defines, enums, strings,
events, and objects are placed here.
**wxaui** - The wxWidgets Advanced User Interface library.
**wxstc** - The wxStyledTextCtrl wrapper around the Scintilla text editor.
---------- --- ------------------------------------------------------------
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,
[http://www.wxwidgets.org/docs](http://www.wxwidgets.org/docs). 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 [wxluaref.html](wxluaref.html) document that lists all
the wxWidgets objects wrapped by wxLua and take note of the functions that are
marked *%override* since you will need to use them as described in that
document. You should also look at the [binding.html](binding.html) file,
even if you do not plan to write your own bindings, to get a better
understanding of the [wxluaref.html](wxluaref.html) file.
**Strings** : 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.
**wxArrayString and wxSortedArrayString** : 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.
**wxArrayInt** : 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.
**Coroutines** : wxLua works with coroutines with one important caveat. \
`NEVER connect an event handler function callback from within a coroutine`{.boldred}
1. 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.
2. The callback function will probably only be called when the
coroutine is yielded and Lua throws an error for this condition.
3. The **one** 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.
<a name="C5.1"/>
### 5.1 - Naming, location, and usage of the wxWidgets objects declared in the C++ header files in the `wx`{.lua} Lua table
- **#define NUMBER_DEFINE VALUE**
- All #defined numerical values are available as
`wx.NUMBER_DEFINE`{.lua}
- Example *"#define wxID_ANY -1"* is accessed as `wx.wxID_ANY`{.lua}
- Declared in the bindings using the *%define* tag.
- **[int, double, etc] NUMBER_VARIABLE;**
- All global numerical variables are available as
`wx.NUMBER_VARIABLE`{.lua}
- Example : *"extern const int wxInvalidOffset;"* is accessible as
`wx.wxInvalidOffset`{.lua}.
- Declared in the bindings using the *%define* tag.
- **enum ENUM_NAMESPACE [or CLASSNAME::ENUM_NAMESPACE] { ENUM_NAME }**
- All global enums, named or not, are available as
`wx.ENUM_NAME`{.lua}
- Example : *"enum wxDirection { wxLEFT, ... }"* is accessible
as `wx.wxLEFT`{.lua}
- All enums that are members of classes are available as
`wx.CLASSNAME.ENUM_NAME`{.lua}
- Example : *"enum wxFTP::TransferMode { ASCII, ... }"* is
accessible as `wx.wxFTP.ASCII`{.lua}
- 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.
- Declared in the bindings using the *%enum* tag.
- **#define STRING_DEFINE wxT("String Value")**
- All #defined string values are available as
`wx.STRING_DEFINE`{.lua}
- Example : *"#define wxIMAGE_OPTION_CUR_HOTSPOT_X wxT("HotSpotX")"*
is accessible as `wx.wxIMAGE_OPTION_CUR_HOTSPOT_X`{.lua}
- Declared in the bindings using the *%define_string* tag.
- **const wxChar* STRING_VARIABLE;**
- All global string variables are available as
`wx.STRING_VARIABLE`{.lua}
- No examples yet.
- Declared in the bindings using the *%define_string* tag.
- **wxEVT_XXX for wxEvtHandler::Connect()**
- All *wxEVT_XXX* wxEventTypes (an integer) are available as
`wx.wxEVT_XXX`{.lua}
- Example : `wx.wxEVT_COMMAND_MENU_SELECTED`{.lua} for
menu item selection.
- 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 [wxluaref.html](wxluaref.html) manual
contains a complete mapping between the two.
- Use the wxEvtHandler::Connect() function to connect event types
to a wxEvtHandler; typically a wxWindow derived class.
- Example : *EVT_MENU(id, func)* use
`window:Connect(menuId, wx.wxEVT_COMMAND_MENU_SELECTED, Lua function)`{.lua}.
The Lua function must have the signature of
`function MenuEvent(event) ... handle event ... return`{.lua}
where the `event` variable will be the wxEvent dervied class that
the wxEventType was declared in, which in this case is a
wxCommandEvent.
- Example : *EVT_PAINT(func)* use
`window:Connect(wx.wxEVT_PAINT, Lua function)`{.lua}.
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.
- Note: You must always create a wxPaintDC for wxEVT_PAINT to
clear the update region.
- `local dc = wx.wxPaintDC(event:GetEventObject():DynamicCast("wxWindow"))`{.lua}
and then call `dc:delete()`{.lua} 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.
- Declared in the bindings using the *%define_event* tag.
- **Objects of classes or structs OBJECT_NAME**
- All global objects that are classes or structs are available as
`wx.OBJECT_NAME`{.lua}
- Example : *"const wxImage wxNullImage;"* is accessible as
`wx.wxNullImage`{.lua} and functions from the wxImage class can be
called as `wx.wxNullImage:Ok()`{.lua} which should return false.
- Declared in the bindings using the *%define_object* tag.
- **Pointers to classes or structs POINTER_NAME**
- All global pointers that are classes or structs are available as
`wx.POINTER_NAME`{.lua}
- Example : *"extern wxPenList* wxThePenList;"* is accessible as
`wx.wxThePenList`{.lua} and functions from the wxPenList class
can be made as
`pen = wx.wxThePenList:FindOrCreatePen(wx.wxColour(1,2,3), 1, wx.wxSOLID)`{.lua}
- Declared in the bindings using the *%define_object* tag.
- **Global C style functions VAR_TYPE FUNCTION_NAME(int a, const wxString& str)**
- All global C style functions are available as
`wx.FUNCTION_NAME(1, "Hello")`{.lua}
- Example :
"`extern wxString wxGetUserHome(const wxString& name)"`{.lua}
is accessible as
`home_dir = wx.wxGetUserHome("john")`{.lua} where wxString
means to input a Lua string and a Lua string is returned.
- Declared in the bindings using the *%function* tag.
- **C++ Classes CLASS_NAME**
- All C++ classes are available as `wx.CLASS_NAME`{.lua}, 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.
- Example : `pt = wx.wxPoint(1, 2); pt2 = wx.wxPoint(pt)`{.lua}
- 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.
- Member functions inherited from the base class(es) are also
available and may be overloaded as well.
- The C++ classes are exposed as tables in Lua, but have a
`__call`{.lua} metatable item so they can be called as a
function. If you need to get the constructor function itself
you can use `wx.CLASS_NAME.new(...)`{.lua} which is the
constructor exposed as a Cfunction.
- The C++ class objects are pushed into Lua as a userdata wrapping
a void* pointer to the C++ object.
- A special metatable is set on the userdata with these entries :
- `__index`{.lua} to call functions on the object or to
get member variable values.
- `__newindex`{.lua} to set new functions or values or
set member variable values.
- `__tostring`{.lua} to allow print() to show something useful
- `print(wx.wxPoint())`{.lua} 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.
- `__gc`{.lua} to tell wxLua when the userdata is no
longer used so wxLua can delete the C++ object if appropriate.
- Declared in the bindings using the *%class* tag.
- **Deleting class userdata** can be done using the wxLua added
class member function delete().
- All classes that have the *%delete* binding tag will be
eventually garbage collected when they go out of scope.
- Classes without the *%delete* tag are assumed to be eventually
attached to another object that will delete them for you.
- 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.
- However! Some graphical device interface (GDI) classes need
to be deleted immediately after you are done using them.
- 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.
- In any case, just `delete()`{.lua} them when done so that your
program will work equally well in MSW as it would in
Linux or OSX.
- 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.
- The solution is to use the `delete()`{.lua} function on certain
types of userdata when you are done.
- Care must be taken to ensure that you're not silently
creating too many temporary objects.
- Example:
`wxDC:SetPen(wx.wxPen(wx.wxColour(1,2,3), 1, wx.wxSOLID))`{.lua};
notice that both a wxPen and a wxColour have been created,
but there is no way for you to call `delete()`{.lua} on them so
they will collect until Lua runs its garbage collector.
- You can force garbage collection using
`collectgarbage("collect")`{.lua} in Lua, but this
may cause slight pauses in your program's execution.
- 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.
- This is a list of classes by order of importance of deleting them:
- `Must delete : wxDC, wxPaintDC, and ALL classed derived from wxDC.`{.boldred}
- `Must delete if > 50 : wxBitmap, wxBrush, wxColour,
wxCursor, wxFont, wxIcon, wxPen, wxRegion`{.boldred}
- To be sure, delete them when you're done and not worry.
- Must delete if large and you're creating many of them :
wxImage, wxBitmap, etc.
- Don't bother : wxPoint, wxRect... etc
- Never delete : wxWindows attached their their parents
(use Destroy()) or the wxMenuItems returned from a wxMenu.
- Safe rule, don't `delete()`{.lua} things that aren't yours.
Refer to the wxWidgets documentation about whether an
object will take ownership of an object passed to it.
- How to tell how many userdata objects you currently have?
- Print the output of `wxlua.GetGCUserdataInfo(true)`{.lua} to
show what objects will be garbage collected when their
reference count goes to zero and the Lua garbage collector runs.
- Print the output of `wxlua.GetTrackedObjectInfo(true)`{.lua} to
get class objects that wxLua has pushed into Lua that may or
may not be garbage collected.
- Call the function `wxlua.LuaStackDialog()`{.lua} 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.
- **Member functions** 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 *really* 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, **properties** and **static**
functions. Please see the sections below about why they only use
the '.' convention.
- Example :
`size = wx.wxSize(1, 2); size:SetWidth(10); size.SetHeight(size, 11);
print(size:GetWidth(), size.GetHeight(size))`{.lua}
where we create a wxSize, set a new width and height, and
then print the numerical values.
- Functions may be renamed in the bindings using the *%rename* tag
in the interface files. You call the functions in Lua using the
name after the *%rename* 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.
- **Property functions** allow you to read and/or write values to
a class using the '.' convention and a shortened name.
- These are generated on the fly when the function is called on a
wxLua userdata and work only for functions following these rules.
- GetXXX() takes no values and returns one.
- SetXXX(value) takes one value and returns none.
- 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 "()".
- Example :
"`rect = wx.wxRect(wx.wxPoint(1,2), wx.wxSize(3,4)); rect:SetX(20);
rect.X = 10; print(rect.X, rect.X == rect:GetX(), rect.X == rect.GetX(rect))`{.lua}"
should print "10, true, true".
- 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.
- 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.
- 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.
- Works (static functions only) :
`print(wx.wxFileName.GetCwd())`{.lua} and
`print(wx.wxFileName.GetCwd)`{.lua} prints that it's
a function.
- Fails (static functions only) :
`print(wx.wxFileName:GetCwd())`{.lua} and
`print(wx.wxFileName:GetCwd)`{.lua} since there is no "self"
for the ':' operator to push.
- Works :
`f = wx.wxFileName("a"); print(f.GetCwd())`{.lua}
and `print(f.GetCwd)`{.lua} prints that it's a function.
- Fails :
`f = wx.wxFileName("a"); print(f:GetCwd())`{.lua}
and `print(f:GetCwd)`{.lua} since there is no "self"
for the ':' operator to push.
- Works :
`f = wx.wxFileName("a"); print(f.Cwd)`{.lua}
- Fails :
`f = wx.wxFileName("a"); print(f.Cwd())`{.lua}
and `print(f:Cwd)`{.lua} and `print(f:Cwd())`{.lua}
- Note : Are properties really necessary? Confusing? Useful?
I'd stick with the Get/Set functions. - JL
- **Member variables** allow you to read and/or write to member
variables of a class.
- Declared in the interface files using the *%member* or
*%member_func* tag.
- Example : In the interface file *gdi.i* this is declared for
wxPoint : *"%rename X %member_func int x"*
- The wxPoint class does not have functions to access the int
x, y member variables so we create our own.
- The *%member_func* 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 *%rename* tag to rename
them to GetX() and SetX(value). It additionally creates properties
for the x variable so the '.' calling convention also works.
- These two methods of getting and setting the x and y member
of the wxPoint class are interchangeable.
- Example :
`pt = wx.wxPoint(1,2); pt.x = 10; pt:SetY(11); print(pt.x, pt:GetY())`{.lua}
- If the *%member* tag is used (as opposed to the *%member_func*)
the Get/Set functions are not generated and the member variables
are accessible only through the properties.
- If the member variable is constant (const) the variable is
read-only and you cannot set its value.
- **Static functions** are part of the table that holds the class and
can be called with or without a class instance (a userdata).
- Example :
`f = wx.wxFileName('dummy'); f.GetCwd() == wx.wxFileName.GetCwd()`{.lua}
- 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.
- See the **properties** 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.
- **Enum members** are also part of the table that holds the class
and are used by accessing the class table itself.
- Example : *"enum wxFTP::TransferMode { ASCII, ... }"* is
accessible as `wx.wxFTP.ASCII`{.lua}
- **Operator functions** allow you to use C++ operators in Lua.
- 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.
- 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.
- 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.