-
Notifications
You must be signed in to change notification settings - Fork 0
/
RapaEdit.hws
522 lines (486 loc) · 12.7 KB
/
RapaEdit.hws
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
/*
** RapaEdit
**
** By Samuel D. Crow
*/
@VERSION 9,1
@REQUIRE "RapaGUI",{version=2,revision=1}
@FILE 1, "RapaEdit.xml"
@BRUSH 2, "icons/new.png", {LoadAlpha=True}
@BRUSH 3, "icons/open.png", {LoadAlpha=True}
@BRUSH 4, "icons/close.png", {LoadAlpha=True}
@BRUSH 5, "icons/save.png", {LoadAlpha=True}
@BRUSH 6, "icons/saveas.png", {LoadAlpha=True}
@BRUSH 7, "icons/add.png", {LoadAlpha=True}
@BRUSH 8, "icons/remove.png", {LoadAlpha=True}
@BRUSH 9, "icons/treeview.png", {LoadAlpha=True}
@BRUSH 10, "icons/moveup.png", {LoadAlpha=True}
@BRUSH 11, "icons/movedown.png", {LoadAlpha=True}
@BRUSH 12, "icons/ok.png", {LoadAlpha=True}
@BRUSH 13, "icons/cancel.png", {LoadAlpha=True}
;Import external classes
@INCLUDE "gadgets/application.hws"
@INCLUDE "gadgets/window.hws"
@INCLUDE "gadgets/dialog.hws"
@INCLUDE "gadgets/group.hws"
@INCLUDE "gadgets/button.hws"
@INCLUDE "gadgets/rectangle.hws"
@INCLUDE "gadgets/choice.hws"
@INCLUDE "gadgets/radio.hws"
/*****Global Variables*****/
Global prefixes={}
Global gadgetclasses={}
gadgetclasses["group"]=group
gadgetclasses["rectangle"]=rectangle
gadgetclasses["button"]=button
gadgetclasses["choice"]=choice
gadgetclasses["radio"]=radio
Global treeitems={}
/*****Global Functions*****/
Function p_Hierarchy(message, prefixLength)
Local suffix=UnrightStr(message.id, prefixLength)
DebugPrint(suffix)
Switch suffix
Case "deselect":
moai.Set("tree_hierarchy", "active", "off")
EndSwitch
EndFunction
/*
** Gadget Selector
*/
Function p_GadSelect(message, prefixLength)
Local suffix=UnrightStr(message.id, prefixLength)
DebugPrint(suffix)
Switch message.attribute
Case "DoubleClick":
p_addGadget()
EndSwitch
EndFunction
/*
** Clone Fields
**
** Clone the non-method fields of a table only for save methods
*/
Function p_CloneFields(data)
Local savebuf=CopyTable(data, True)
; kill the invariant methods
For k, v In Pairs(savebuf)
If (#FUNCTION=GetType(v)) Then savebuf[k]=Nil
Next
savebuf["slot"]=Nil
Return(savebuf)
EndFunction
/*
** Get Type
**
** Returns type name as a string and user data table
*/
Function p_GetType(current$)
Local uid$=moai.Get(current$, "UID")
Local ud=RawGet(treeitems, uid$)
Assert(Not IsNil(ud))
Assert(GetType(ud)=#TABLE)
Return(ud.type["kind$"], ud)
EndFunction
/*
** Add Gadget
*/
Function p_AddGadget()
Local type$=moai.Get("tree_hierarchy", "Active")
If (type$="off")
moai.Request("No Destination", "No destination of where to add the gadget is selected", "OK", "Error")
Return
EndIf
Local temp$, data=p_GetType(type$)
Switch (temp$)
Case "application":
moai.Request("Application", "Applications can only hold windows and dialogs.", "OK", "Error")
Return
Default:
Local current=moai.Get("lv_gadgets", "Active")
If (GetType(current)=#STRING)
moai.Request("No Gadget", "No selected gadget to add", "OK", "Error")
Return
EndIf
Local class=RawGet(gadgetclasses, moai.DoMethod("lv_gadgets", "GetEntry", current))
Assert(Not IsNil(class))
Local parent$=moai.Get("tree_hierarchy", "Active")
If (parent$="off")
moai.Request("Unselected", "Nowhere to put gadget", "OK", "Error")
Return
EndIf
Local parentTemp$, parentData = p_GetType(parent$)
Local dest=parentData.type
Assert(Not IsNil(dest))
dest.AddGadget(parentData, class)
parentData.application.saved=False
EndSwitch
EndFunction
/*
** Default BeRemoved method
*/
Function p_Remove(self)
moai.DoMethod("tree_hierarchy", "Remove", TreeItems[self.ID$])
TreeItems[self.ID$]=Nil
self=Nil
EndFunction
/*
** Remove Gadget
*/
Function p_RemGadget(msg, len)
Local type$=moai.Get("tree_hierarchy", "Active")
If (type$="off")
moai.Request("Not Selected", "No gadget is selected to remove", "OK", "Error")
Return
EndIf
Local temp$, data=p_GetType(type$)
Switch (temp$)
Case "application":
moai.Request("Application",
"Applications are closed using the close file option",
"OK", "Information")
Default:
data.parent.type.remGadget(temp$, data)
EndSwitch
EndFunction
/*
** Start Editor
**
** Starts editor function on selected gadget
*/
Function p_StartEditor()
Local current$=moai.Get("tree_hierarchy", "Active")
If (current$="off") Then Return
Local temp$, data = p_GetType(current$)
Local class=gadgetclasses[temp$]
class.editor(data)
EndFunction
/*
** Preview XML
*/
Function p_previewXML()
Local xml$=application.GenerateXML$()
moai.CreateDialog([[<dialog id="xml_dialog" width="800" height="600" title="XML Preview">
<vgroup>
<textview id="xml_preview"></textview>
<hgroup>
<rectangle />
<button id="xml_export" >Export</button>
<button id="xml_cancel" >Cancel</button>
</hgroup>
</vgroup>
</dialog>]], "win_")
moai.Set("xml_preview", "Text", xml$)
moai.DoMethod("xml_dialog", "showModal")
EndFunction
/*
** Regular Preview
*/
Function p_preview()
Local sel$=moai.Get("tree_hierarchy", "Active")
If (sel$="off")
moai.Request("Unselected", "No item selected", "OK", "Error")
Return
EndIf
Local temp$, pnl=p_GetType(sel$)
If temp$="application"
moai.Request("Illegal",
"Applications cannot be previewed.\nSelect a window or dialog.", "OK", "Error")
Return
EndIf
DebugPrint(pnl.kind$.." of "..temp$)
While pnl.kind$<>"dialog" And pnl.kind$<>"window"
pnl=pnl.parent
DebugPrint("parent is "..pnl.kind$)
Wend
DebugPrint("found "..pnl.kind$.." with "..IIf(IsNil(RawGet(pnl,"previewxml$")),"uninitialized",GetType(pnl.previewXML$)).." value.")
Local xml$=pnl:previewXML$()
DebugPrint(xml$)
moai.CreateDialog(xml$, "win_")
moai.DoMethod("pre_view", "showModal")
EndFunction
/*
** Preview handler
*/
Function p_previewHandle(msg, len)
moai.DoMethod("pre_view", "EndModal", 0)
EndFunction
/*
** Save As...
*/
Function p_saveas()
Local f$=FileRequest("Save As...", "rapa", #REQ_SAVEMODE)
If f$="" Then Return
If Exists(f$)
If moai.Request("File Exists",
"Are you sure you want to overwrite "..f$.."?", "OK|Cancel", "Warning")=0
Return
EndIf
EndIf
application.saveas(f$)
EndFunction
/*
** Unsaved
**
** Returns true if there are unsaved applications
*/
Function p_unsaved()
Local found, tbl=moai.DoMethod("tree_hierarchy", "GetEntry", "Root", "Head")
While found
Local u$=tbl.UID
Local app=treeitems[u$]
If app.saved=False Then Return(True)
found, tbl=moai.DoMethod("tree_hierarchy", "GetEntry", u$, "Next")
Wend
Return(False)
EndFunction
/*
** Save Gate
**
** Requests if data is unsaved if loss is undesirable
*/
Function p_saveGate(test)
If test And moai.Request("Unsaved",
"Document is unsaved!\nAre you sure you want to quit?",
"OK|Cancel", "Warning")=0
Return
EndIf
End
EndFunction
/*
** Window Handling
*/
Function p_windowNotify(msg, len)
Assert(GetType(msg)=#TABLE)
Switch (msg.attribute)
Case "CloseRequest":
p_saveGate(p_unsaved())
EndSwitch
Return
EndFunction
/*
** Swap Entries
*/
Function p_swapEntries(first, second, parent)
; swap entries
assert(Not IsNil(RawGet(first, "uid")))
assert(Not IsNil(RawGet(second, "uid")))
assert(Not IsNil(RawGet(parent, "uid")))
Local content=treeitems[parent.uid].contents
Local slot1=treeitems[first.uid].slot
Local slot2=treeitems[second.uid].slot
Local temp=content[slot2]
content[slot2]=content[slot1]
content[slot1]=temp
content[slot2].slot=slot1
content[slot1].slot=slot2
; move GUI items
If first.Node
; TODO swap node code
assert(false)
moai.DoMethod("tree_hierarchy", "InsertNode", first.ID, parent.ID, second.ID, first.name)
Else
moai.DoMethod("tree_hierarchy", "Remove", first.ID)
moai.DoMethod("tree_hierarchy", "InsertLeaf", first.ID, parent.ID, second.ID, Unpack(first.items))
EndIf
; refresh UID in treeitems
Local newUID=moai.Get(first.ID, "UID")
treeitems[newUID]=treeitems[first.UID]
treeitems[first.UID]=Nil
EndFunction
/*
** An item was not found that should have been
*/
Function NotFound(i$)
moai.Request("Not Found", i$.." item cannot be found", "OK", "Error")
EndFunction
/*
** Move Up
*/
Function p_moveUp()
Local found, tbl=moai.DoMethod("tree_hierarchy", "GetEntry", "Active", "Previous")
If Not found
NotFound("Higher")
Return
EndIf
Local found2, tbl2=moai.DoMethod("tree_hierarchy", "GetEntry", "Active", "Active")
If Not found2
NotFound("Current")
Return
EndIf
Local foundParent, tblParent=moai.DoMethod("tree_hierarchy", "GetEntry", "Active", "Parent")
If Not foundParent
NotFound("Parent")
Return
EndIf
p_swapEntries(tbl, tbl2, tblParent)
EndFunction
/*
** Move Down
*/
Function p_moveDown()
Local found, tbl=moai.DoMethod("tree_hierarchy", "GetEntry", "Active", "Active")
If Not found
NotFound("Current")
Return
EndIf
Local found2, tbl2=moai.DoMethod("tree_hierarchy", "GetEntry", "Active", "Next")
If Not found2
NotFound("Lower")
Return
EndIf
Local foundParent, tblParent=moai.DoMethod("tree_hierarchy", "GetEntry", "Active", "Parent")
If Not foundParent
NotFound("Parent")
Return
EndIf
p_swapEntries(tbl, tbl2, tblParent)
moai.Set("tree_hierarchy", "Active", tbl.ID)
EndFunction
/*
** Process Menu and Toolbar events
*/
Function p_MenuToolBar(msg, len)
Local prefix$=LeftStr(msg.id, len)
Switch msg.attribute
Case "Pressed":
Assert(prefix$="tb_")
Case "Selected":
Assert(prefix$="mn_")
Default:
; Should be unreachable
moai.Request("Error", "p_MenuToolbar is incorrect handler", "OK", "Error")
End
EndSwitch
Local suffix$=UnrightStr(msg.id, len)
DebugPrint(suffix$)
Switch suffix$
Case "quit":
p_saveGate(p_unsaved())
Case "encapsulate":
;TODO Replace this with actual Template functionality
moai.Request("Template", "Encapsulate selected", "OK", "Information")
Case "rapa":
moai.DoMethod("RapaEdit", "AboutRapaGUI")
Case "about":
moai.Request("About", "RapaEdit\nBy Samuel D. Crow", "OK", "Information")
Case "editgadget":
p_StartEditor()
Case "addgadget":
p_AddGadget()
Case "removegadget":
p_RemGadget(msg, len)
Case "new":
application:new()
Case "preview":
p_preview()
Case "export":
p_previewXML()
Case "upgadget":
p_moveUp()
Case "downgadget":
p_moveDown()
Case "open":
application.load()
Case "close":
application.close()
Case "saveas":
p_saveas()
Case "save":
Local f$=RawGet(application.getCurrent(), "filename$")
If IsNil(f$) Or f$="" Then p_saveas() Else application.saveas(f$)
EndSwitch
EndFunction
/*
** Handler
**
** Handles input from edit requester
*/
Function p_handler(message, prefixLength)
Local suffix=UnrightStr(message.id, prefixLength)
Assert(Not IsNil(ModalUserData))
Assert(GetType(ModalUserData)=#TABLE)
Local type=ModalUserData.type
Switch message.attribute
Case "Pressed":
Switch(suffix) FallThrough
Case "ok":
type.CopyBack()
ModalUserData.application.saved=False
Case "cancel":
type.editorDone()
ModalUserData=Nil
Default:
Break
EndSwitch
EndSwitch
EndFunction
/*
** XML Preview handler
*/
Function p_XML(msg, prefixLength)
Switch UnRightStr(msg.id, prefixLength)
Case "export":
Local filename$=FileRequest("XML Export", "xml", #REQ_SAVEMODE)
If filename$<>""
StringToFile(moai.Get("xml_preview", "Text"), filename$)
EndIf
Case "cancel":
moai.DoMethod("xml_dialog", "EndModal", 0)
EndSwitch
EndFunction
/*
** Global Event Handler
*/
Function p_ProcessGUI(message)
Switch message.action
Case "RapaGUI":
Local prefixLength=FindStr(message.id, "_")+1
Assert(prefixLength>1)
;Check for Local prefixes and event handlers
Local prefix$=LeftStr(message.id, prefixLength)
Local handler=RawGet(prefixes, prefix$)
DebugPrint("prefix="..prefix$)
;Invoke local event handler from class
handler(message, prefixLength)
EndSwitch
EndFunction
Function p_WindowWash()
moai.Request("Window Type", "No application selected", "OK", "Error")
EndFunction
/*****Initialization*****/
moai.CreateApp(ReadString(1))
;populate gadget list and prefix registry
For i$,j In Pairs(gadgetclasses)
moai.DoMethod("lv_gadgets", "Insert", "Sorted", i$)
If (Not IsNil(RawGet(j, "handler")))
prefixes[j["prefix$"]]=j.handler
Else
; Default to p_handler if no type-specific handler is specified
prefixes[j["prefix$"]]=p_handler
EndIf
Next
moai.Set("spl_split", "position", "128")
moai.Set("spl_split", "gravity", "5")
;not listed in gadget because each application is a new document
gadgetclasses["application"]=application
;also not listed because windows and dialogs can only be added to applications
gadgetclasses["window"]=window
gadgetclasses["dialog"]=dialog
;add global prefixes to registry
prefixes["wn_"]=p_handler
prefixes["dlg_"]=p_handler
prefixes["app_"]=p_handler
prefixes["mn_"]=p_MenuToolBar
prefixes["tb_"]=p_MenuToolBar
prefixes["tree_"]=p_Hierarchy
prefixes["lv_"]=p_GadSelect
prefixes["wa_"]=application.AddWindow
prefixes["xml_"]=p_XML
prefixes["pre_"]=p_previewHandle
prefixes["win_"]=p_WindowNotify
InstallEventHandler({RapaGUI=p_ProcessGUI})
/*****Event Loop*****/
Repeat
WaitEvent
Forever