Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6581 | leency | 1 | -- |
2 | -- Event-driven GUI library |
||
3 | -- |
||
4 | -- |
||
5 | |||
6 | gui = { |
||
7 | |||
8 | -- |
||
9 | -- dialog() -- |
||
10 | -- |
||
11 | dialog = function(args) |
||
12 | local dia = { |
||
13 | title = args.title, |
||
14 | w = args.w, |
||
15 | h = args.h, |
||
16 | -- |
||
17 | widgets = {}, |
||
18 | -- an indexed array, starting at 1. Used for calling the relevant |
||
19 | -- callback when a numbered control is clicked. |
||
20 | callbacks = {}, |
||
21 | |||
22 | -- |
||
23 | -- dialog.run() -- |
||
24 | -- |
||
25 | run = function(this) |
||
26 | windowopen(this.w,this.h, this.title or ""); |
||
27 | -- examine all elements |
||
28 | for _,widget in ipairs(this.widgets) do |
||
29 | widget:render() |
||
30 | end |
||
31 | |||
32 | repeat |
||
33 | local button, button2, key = windowdodialog(); |
||
34 | |||
35 | if button > 0 then |
||
36 | local c = this.callbacks[button] |
||
37 | -- run the callback and stop the form if it returns true |
||
38 | if c ~= nil and c(this) then |
||
39 | break; |
||
40 | end |
||
41 | end |
||
42 | until key == 27; |
||
43 | windowclose(); |
||
44 | end |
||
45 | } |
||
46 | local id = 1; |
||
47 | -- examine all elements |
||
48 | for _,value in ipairs(args) do |
||
49 | -- all arguments that are tables are assumed to be widgets |
||
50 | if type(value)=="table" then |
||
51 | table.insert(dia.widgets, value) |
||
52 | -- clickable widgets take up an auto-numbered id |
||
53 | if (value.click) then |
||
54 | dia.callbacks[id] = value.click |
||
55 | id=id+1 |
||
56 | end |
||
57 | end |
||
58 | end |
||
59 | return dia; |
||
60 | end, |
||
61 | |||
62 | -- |
||
63 | -- button() -- |
||
64 | -- |
||
65 | button = function(args) |
||
66 | local but = { |
||
67 | x = args.x, |
||
68 | y = args.y, |
||
69 | w = args.w, |
||
70 | h = args.h, |
||
71 | key = args.key, |
||
72 | label = args.label, |
||
73 | click = args.click or donothing, |
||
74 | render = args.repeatable and function(this) |
||
75 | windowrepeatbutton(this.x, this.y, this.w, this.h, this.label, this.key or -1); |
||
76 | end |
||
77 | or function(this) |
||
78 | windowbutton(this.x, this.y, this.w, this.h, this.label, this.key or -1); |
||
79 | end |
||
80 | } |
||
81 | return but; |
||
82 | end, |
||
83 | |||
84 | -- |
||
85 | -- label() -- |
||
86 | -- |
||
87 | label = function(args) |
||
88 | local fld = { |
||
89 | x = args.x, |
||
90 | y = args.y, |
||
91 | value = args.value, |
||
92 | format = args.format, |
||
93 | fg = args.fg or 0, |
||
94 | bg = args.bg or 2, |
||
95 | render = function(this) |
||
96 | if type(this.format) then |
||
97 | windowprint(this.x, this.y, string.format(this.format, this.value), this.fg, this.bg); |
||
98 | else |
||
99 | windowprint(this.x, this.y, this.value, this.fg, this.bg); |
||
100 | end |
||
101 | end |
||
102 | } |
||
103 | return fld; |
||
104 | end, |
||
105 | |||
106 | |||
107 | -- "do nothing" function. Used as default callback |
||
108 | donothing = function(this) |
||
109 | end |
||
110 | |||
111 | } |