@@ -9,7 +9,6 @@ class Graphics(Handler):
9
9
def __init__ (self , compiler ):
10
10
Handler .__init__ (self , compiler )
11
11
self .utils = GUtils ()
12
- self .eventHandlers = {}
13
12
14
13
def getName (self ):
15
14
return 'graphics'
@@ -18,22 +17,27 @@ def getName(self):
18
17
# Keyword handlers
19
18
20
19
def k_add (self , command ):
21
- elements = []
22
20
token = self .nextToken ()
23
21
if self .isSymbol ():
24
22
symbolRecord = self .getSymbolRecord ()
25
23
name = symbolRecord ['name' ]
26
- if symbolRecord ['keyword' ] == 'layout' :
27
- elements . append ( name )
24
+ keyword = symbolRecord ['keyword' ]
25
+ if keyword == 'layout' :
28
26
command ['args' ] = name
29
- else : FatalError (self .compiler .program , f'\' { name } \' is not a layout' )
27
+ elif keyword in ['column' , 'frame' , 'tab' ]:
28
+ command ['name' ] = name
29
+ command ['type' ] = token
30
+ if self .peek () == 'to' :
31
+ command ['args' ] = []
32
+ else :
33
+ command ['args' ] = self .utils .getArgs (self )
30
34
else :
31
35
command ['type' ] = token
32
36
command ['args' ] = self .utils .getArgs (self )
33
37
if self .nextIs ('to' ):
34
38
if self .nextIsSymbol ():
35
39
symbolRecord = self .getSymbolRecord ()
36
- if symbolRecord ['keyword' ] == ' layout' :
40
+ if symbolRecord ['keyword' ] in [ 'column' , 'frame' , ' layout', 'tab' ] :
37
41
command ['target' ] = symbolRecord ['name' ]
38
42
self .addCommand (command )
39
43
return True
@@ -43,14 +47,19 @@ def r_add(self, command):
43
47
target = self .getVariable (command ['target' ])
44
48
type = command ['type' ]
45
49
args = command ['args' ]
50
+ param = None
46
51
if not 'layout' in target :
47
52
target ['layout' ] = []
48
53
if args [0 ] == '{' :
54
+ if type in ['Column' , 'Frame' , 'Tab' ]:
55
+ record = self .getVariable (command ['name' ])
56
+ param = record ['layout' ]
49
57
layout = json .loads (self .getRuntimeValue (json .loads (args )))
50
58
default = self .utils .getDefaultArgs (type )
51
59
for n in range (0 , len (layout )):
52
60
args = self .utils .decode (default , layout [n ])
53
- target ['layout' ].append (self .utils .createElement (type , args ))
61
+ item = self .utils .createElement (type , param , args )
62
+ target ['layout' ].append (item )
54
63
else :
55
64
v = self .getVariable (args )
56
65
target ['layout' ].append (v ['layout' ])
@@ -85,6 +94,12 @@ def r_close(self, command):
85
94
target ['window' ].close ()
86
95
return self .nextPC ()
87
96
97
+ def k_column (self , command ):
98
+ return self .compileVariable (command )
99
+
100
+ def r_column (self , command ):
101
+ return self .nextPC ()
102
+
88
103
# create {window} layout {layout}
89
104
# create {element} {args...}
90
105
def k_create (self , command ):
@@ -110,14 +125,30 @@ def r_create(self, command):
110
125
if type == 'window' :
111
126
layout = self .getVariable (command ['layout' ])
112
127
title = self .getRuntimeValue (command ['title' ])
113
- self .program .window = psg .Window (title , layout ['layout' ], finalize = True )
128
+ record ['window' ] = psg .Window (title , layout ['layout' ], finalize = True )
129
+ record ['eventHandlers' ] = {}
130
+ self .program .windowRecord = record
114
131
self .program .run (self .nextPC ())
115
132
self .mainLoop ()
116
- self .program .kill ()
133
+ # self.program.kill()
117
134
return 0
118
135
else :
119
136
RuntimeError (self .program , 'Variable is not a window or an element' )
120
137
138
+ def k_init (self , command ):
139
+ if self .nextIsSymbol ():
140
+ symbolRecord = self .getSymbolRecord ()
141
+ if symbolRecord ['keyword' ] in ['column' , 'frame' , 'layout' , 'tab' ]:
142
+ command ['target' ] = symbolRecord ['name' ]
143
+ self .add (command )
144
+ return True
145
+ return False
146
+
147
+ def r_init (self , command ):
148
+ target = self .getVariable (command ['target' ])
149
+ target ['layout' ] = []
150
+ return self .nextPC ()
151
+
121
152
def k_layout (self , command ):
122
153
return self .compileVariable (command )
123
154
@@ -128,33 +159,38 @@ def k_on(self, command):
128
159
token = self .nextToken ()
129
160
if token == 'event' :
130
161
command ['key' ] = self .nextValue ()
131
- command ['goto' ] = self .getPC () + 2
132
- self .add (command )
133
- self .nextToken ()
134
- pcNext = self .getPC ()
135
- cmd = {}
136
- cmd ['domain' ] = 'core'
137
- cmd ['lino' ] = command ['lino' ]
138
- cmd ['keyword' ] = 'gotoPC'
139
- cmd ['goto' ] = 0
140
- cmd ['debug' ] = False
141
- self .addCommand (cmd )
142
- self .compileOne ()
143
- cmd = {}
144
- cmd ['domain' ] = 'core'
145
- cmd ['lino' ] = command ['lino' ]
146
- cmd ['keyword' ] = 'stop'
147
- cmd ['debug' ] = False
148
- self .addCommand (cmd )
149
- # Fixup the link
150
- self .getCommandAt (pcNext )['goto' ] = self .getPC ()
151
- return True
162
+ if self .nextIs ('in' ):
163
+ if self .nextIsSymbol ():
164
+ record = self .getSymbolRecord ()
165
+ if record ['keyword' ] == 'window' :
166
+ command ['window' ] = record ['name' ]
167
+ command ['goto' ] = self .getPC () + 2
168
+ self .add (command )
169
+ self .nextToken ()
170
+ pcNext = self .getPC ()
171
+ cmd = {}
172
+ cmd ['domain' ] = 'core'
173
+ cmd ['lino' ] = command ['lino' ]
174
+ cmd ['keyword' ] = 'gotoPC'
175
+ cmd ['goto' ] = 0
176
+ cmd ['debug' ] = False
177
+ self .addCommand (cmd )
178
+ self .compileOne ()
179
+ cmd = {}
180
+ cmd ['domain' ] = 'core'
181
+ cmd ['lino' ] = command ['lino' ]
182
+ cmd ['keyword' ] = 'stop'
183
+ cmd ['debug' ] = False
184
+ self .addCommand (cmd )
185
+ # Fixup the link
186
+ self .getCommandAt (pcNext )['goto' ] = self .getPC ()
187
+ return True
152
188
return False
153
189
154
190
def r_on (self , command ):
155
191
key = self .getRuntimeValue (command ['key' ])
156
- pc = command ['goto' ]
157
- self . eventHandlers [key ] = lambda : self .run (pc )
192
+ window = self . getVariable ( command ['window' ])
193
+ window [ ' eventHandlers' ] [key ] = lambda : self .run (command [ 'goto' ] )
158
194
return self .nextPC ()
159
195
160
196
def k_popup (self , command ):
@@ -221,13 +257,14 @@ def compileValue(self):
221
257
return value
222
258
return None
223
259
260
+ if self .getToken () == 'the' :
261
+ self .nextToken ()
262
+
263
+ token = self .getToken ()
224
264
value ['type' ] = token
225
265
226
- if token == 'test' :
227
- value = {}
228
- value ['type' ] = 'text'
229
- value ['content' ] = 'test'
230
- return value
266
+ if token == 'event' :
267
+ return value
231
268
232
269
return None
233
270
@@ -246,7 +283,9 @@ def v_symbol(self, symbolRecord):
246
283
else :
247
284
return None
248
285
249
- def v_test (self , v ):
286
+ def v_event (self , v ):
287
+ v ['type' ] = 'text'
288
+ v ['content' ] = self .eventValues
250
289
return v
251
290
252
291
#############################################################################
@@ -261,13 +300,15 @@ def compileCondition(self):
261
300
#############################################################################
262
301
# The main loop
263
302
def mainLoop (self ):
303
+ windowRecord = self .program .windowRecord
304
+ window = windowRecord ['window' ]
305
+ eventHandlers = windowRecord ['eventHandlers' ]
264
306
while True :
265
- event , values = self . program . window .Read (timeout = 100 )
307
+ event , values = window .Read (timeout = 100 )
266
308
if event == psg .WINDOW_CLOSED or event == "EXIT" :
267
309
break
268
310
if event == '__TIMEOUT__' : self .program .flushCB ()
269
311
else :
270
- if event in self . eventHandlers :
312
+ if event in eventHandlers :
271
313
self .eventValues = values
272
- self .eventHandlers [event ]()
273
-
314
+ eventHandlers [event ]()
0 commit comments