-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathComponents.Mod
More file actions
executable file
·3715 lines (3351 loc) · 122 KB
/
Components.Mod
File metadata and controls
executable file
·3715 lines (3351 loc) · 122 KB
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
MODULE WMComponents; (** AUTHOR "TF"; PURPOSE "Component Framework based on XML"; *)
(**
-- Events: --
Each VisualComponent can produce keyboard and mouse events which can trigger A2 commands.
The command string for a given event can by specified by the usage of XML attributes and component properties.
The following attributes are defined:
Keyboard: onReturn, onEscape, onKeyPressed, onKeyReleased
Mouse: onLeftClick, onRightClick, onMiddleClick, onClick
The command strings are processed (macro substitution) before the actual command is called.
-- Macro substitution: --
General form: "^" [namespace ":"] macrostring
A macro always start with MacroCharacter ("^"). The next occurence of a whitespace character determines the end of the macro.
Two consequent MacroCharacter's ("^^") will be replaced by the MacroCharacter ("^") not triggering macro substitution at all.
The user can install MacroHandlerProcedures for a given namespace. At most one handler per namespace can be installed.
If the namespace is omitted, the default macro handler is triggered.
The DefaultMacroHandler currently supports the following macro substitutions:
^selection is replaced by the last selection of the user
^clipboard is replaced by the content of the clipboard
^attribute=[component "."] attribute
^property=[component "."] property
is replaced by the value of <attribute> or <property>.
If the component qualifier is omitted, <attribute> or <property> is supposed to be an attribute or property of the originator of the event.
If no MacroHandlerProcedure is found for a given macro, no substitution takes place.
Example:
onLeftClick = SystemTools.Show ^attribute=generator
onMiddleClick = SystemTools.Show ^property=FillColor
*)
(*PH 08/14:
- avoid parallel call of FormWindow.SetContent, Component.AddContent, Form.InvalidateRect by different processes, through use of EXCLUSIVE sections.
- send an "invalidate content" message to a window after it appears on the display, which is handled after "form" field is ready
- restructure FormWindow.SetContent() to assure coherent displays and to assure FormWindow.content is consistent
*)
IMPORT
KernelLog, Inputs, Streams, Events, Files, Texts, TextUtilities,
XML, XMLScanner, XMLParser, XMLObjects, Codecs, Localization, Repositories,
Messages := WMMessages, Rectangles := WMRectangles,
WMEvents, WMProperties, WMGraphics, Strings, WM := WMWindowManager, Raster,
Commands, Modules, Kernel, Locks, Objects, WMDropTarget;
CONST
Ok* = 0;
DuplicateNamespace* = 1;
AlignNone* = 0; AlignLeft* = 1; AlignTop* = 2; AlignRight* = 3; AlignBottom* = 4; AlignClient* = 5; AlignRelative*=6;
None=0; Left=1; Right=2; Lower=3; Upper=4; LowerRight=5; UpperRight=6; LowerLeft=7; UpperLeft=8; Inside = 9;
MaxRel = 16*1024;
MaxComponentNameSize* = 64; (* including 0X *)
TraceFocus = 0;
TraceFinalize = 1;
Trace = {};
(* Enable event logging? *)
Logging = TRUE;
(* Macro handling *)
(* General form of macro: MacroCharacter [Namespace + NamespaceCharacter] MacroName *)
MacroCharacter = "^";
NamespaceCharacter = ":";
NoNamespace = "";
(* Namespace used if no namespace is specified *)
DefaultNamespace = "system";
(* Macros names of default macro handler *)
MacroSelection = "selection";
MacroClipboard = "clipboard";
MacroAttributePrefix = "attribute=";
MacroPropertyPrefix = "property=";
CanYield = TRUE;
(*temporary - to be removed*)
FlagDirty=13;
TYPE
(** Installable event preview handlers. Are called by the components sequencer thread *)
PointerHandler* = PROCEDURE {DELEGATE} (x, y : LONGINT; keys : SET; VAR handled : BOOLEAN);
PointerLeaveHandler* = PROCEDURE {DELEGATE} (VAR handled : BOOLEAN);
DragDropHandler* = PROCEDURE {DELEGATE} (x, y : LONGINT; dragInfo : WM.DragInfo; VAR handled : BOOLEAN);
DragResultHandler* = PROCEDURE {DELEGATE} (accepted : BOOLEAN; recipient : ANY; dragInfo : WM.DragInfo; VAR handled : BOOLEAN);
DragAutoStartHandler* = PROCEDURE {DELEGATE} (VAR handled : BOOLEAN);
FocusHandler* = PROCEDURE {DELEGATE} (hasFocus : BOOLEAN);
ContextMenuHandler* = PROCEDURE {DELEGATE} (sender : ANY; x, y: LONGINT);
KeyEventHandler* = PROCEDURE {DELEGATE} (ucs : LONGINT; flags : SET; VAR keySym : LONGINT; VAR handled : BOOLEAN);
DrawHandler* = PROCEDURE {DELEGATE} (canvas : WMGraphics.Canvas);
Recursion*= ENUM None*, FromComponent*, FromBottom* END;
TYPE
SetStringProcedure = PROCEDURE {DELEGATE} (CONST string : ARRAY OF CHAR; x,y : LONGINT; VAR res : LONGINT);
DropTarget = OBJECT(WMDropTarget.DropTarget)
VAR
originator : ANY;
setString : SetStringProcedure;
x,y : LONGINT;
PROCEDURE &Init(originator : ANY; setString : SetStringProcedure; x,y : LONGINT);
BEGIN
ASSERT(setString # NIL);
SELF.originator := originator;
SELF.setString := setString;
SELF.x := x;
SELF.y := y;
END Init;
PROCEDURE GetInterface(type : LONGINT) : WMDropTarget.DropInterface;
VAR sdi : DropString;
BEGIN
IF (type = WMDropTarget.TypeString) THEN
NEW(sdi, originator, setString, x,y); RETURN sdi;
ELSE
RETURN NIL;
END;
END GetInterface;
END DropTarget;
DropString = OBJECT(WMDropTarget.DropString)
VAR
originator : ANY;
setString : SetStringProcedure;
x,y : LONGINT;
PROCEDURE &Init(originator : ANY; setString : SetStringProcedure; x,y : LONGINT);
BEGIN
ASSERT(setString # NIL);
SELF.originator := originator;
SELF.setString := setString;
SELF.x := x; SELF.y := y;
END Init;
PROCEDURE Set(CONST string : ARRAY OF CHAR; VAR res : LONGINT);
BEGIN
setString(string, x,y, res);
END Set;
END DropString;
LanguageExtension* = POINTER TO RECORD(Messages.MessageExtension)
languages* : Localization.Languages;
END;
ToggleEditMode* = POINTER TO RECORD
recursion*: Recursion;
END;
Event* = RECORD
END;
KeyPressedEvent* = RECORD(Event)
ucs- : LONGINT;
flags- : SET;
keysym- : LONGINT;
END;
PointerEvent* = RECORD(Event)
x-, y-, z- : LONGINT;
keys- : SET;
END;
EventContext* = OBJECT(Repositories.Context)
VAR
originator- : Component; (** {originator # NIL} *)
command- : Strings.String; (** {command # NIL}, immutable *)
timestamp- : LONGINT;
PROCEDURE &New*(originator : Component; command : Strings.String; in, arg : Streams.Reader; out, error : Streams.Writer; caller : OBJECT);
BEGIN
ASSERT((originator # NIL) & (command # NIL));
SELF.originator := originator;
SELF.command := command;
Init(in, arg, out, error, caller);
END New;
END EventContext;
PointerContext* = OBJECT(EventContext)
VAR
pointer- : PointerEvent;
END PointerContext;
KeyContext* = OBJECT(EventContext)
VAR
key- : KeyPressedEvent;
END KeyContext;
TYPE
(** Basic component *)
ComponentStyleChanged = OBJECT
END ComponentStyleChanged;
Component* = OBJECT(Repositories.Component)
VAR
sequencer- : Messages.MsgSequencer;
initialized- : BOOLEAN;
properties- : WMProperties.PropertyList;
events- : WMEvents.EventSourceList;
eventListeners- : WMEvents.EventListenerList;
id-, uid- : WMProperties.StringProperty;
enabled- : WMProperties.BooleanProperty;
(* discard property changes that come from a property change within the same component*)
inPropertyUpdate, inLinkUpdate : BOOLEAN;
(* If TRUE, this component is supposed to be created and managed by its parent. It is not externalized. *)
internal- : BOOLEAN;
(* after Init() , calling Reset() implicitely by insertion into FormWindow or explicitely, thereby triggering Initialize() is required to render component responsive to messages *)
PROCEDURE &Init*;
BEGIN
Init^;
SetNameAsString(StrComponent);
sequencer := NIL;
initialized := FALSE;
NEW(properties); properties.onPropertyChanged.Add(SELF.InternalPropertyChanged); properties.onLinkChanged.Add(SELF.InternalLinkChanged);
NEW(events);
NEW(eventListeners);
NEW(id, PrototypeID, NIL, NIL); properties.Add(id);
NEW(uid, PrototypeUID, NIL, NIL); properties.Add(uid);
NEW(enabled, PrototypeEnabled, NIL, NIL); properties.Add(enabled);
inPropertyUpdate := FALSE;
inLinkUpdate := FALSE;
internal := FALSE;
SetGenerator("WMComponents.NewComponent");
END Init;
PROCEDURE Write*(w : Streams.Writer;context: ANY; level : LONGINT);
VAR enum: XMLObjects.Enumerator; c: ANY; name : Strings.String; nextLevel : LONGINT;
BEGIN
IF IsLocked() THEN (* D.String("Component.Write: islocked"); D.Ln; *) RETURN; END;
IF ~internal THEN
name := GetName();
w.Char('<'); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^) END;
WriteAttributes(w, context, level);
w.Char('>');
properties.WriteXML(w, context, level);
nextLevel := level + 1;
ELSE
(* D.String("Component.Write: isInternal"); D.Ln; *)
nextLevel := level;
END;
enum := GetContents();
WHILE enum.HasMoreElements() DO
c := enum.GetNext();
IF ~(c IS WMProperties.Properties) THEN
IF ~((c IS Component) & ((c(Component).internal) OR c(Component).IsLocked())) THEN NewLine(w, 0); NewLine(w, nextLevel); END;
c(XML.Content).Write(w, context, nextLevel);
(*c(Component).Write(w, context, nextLevel)*)
END;
END;
IF ~internal THEN
NewLine(w, level);
w.String("</"); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^); END; w.Char('>')
END;
END Write;
(*
PROCEDURE ToRepository*(CONST repository: ARRAY OF CHAR; w: Streams.Writer; level: LONGINT);
VAR enum: XMLObjects.Enumerator; c: ANY; name : Strings.String; nextLevel : LONGINT;
BEGIN
IF IsLocked() THEN RETURN; END;
IF ~internal THEN
name := GetName();
w.Char('<'); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^) END;
WriteAttributes(w, NIL, level);
w.Char('>');
properties.ToRepository(repository,w, level);
nextLevel := level + 1;
ELSE
nextLevel := level;
END;
enum := GetContents();
WHILE enum.HasMoreElements() DO
c := enum.GetNext();
IF ~(c IS WMProperties.Properties) THEN
IF ~((c IS Component) & ((c(Component).internal) OR c(Component).IsLocked())) THEN NewLine(w, 0); NewLine(w, nextLevel); END;
IF (c IS Repositories.Component) THEN
c(Repositories.Component).ToRepository(repository, w, level);
ELSE
c(XML.Content).Write(w, NIL, nextLevel);
END;
END;
END;
IF ~internal THEN
NewLine(w, level);
w.String("</"); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^); END; w.Char('>')
END;
END ToRepository;
*)
PROCEDURE FromXML*(xml: XML.Element);
VAR component: Component; enum: XMLObjects.Enumerator; c: ANY; element: XML.Element;
BEGIN
element := GetElementByName(xml,"Properties");
IF (element = NIL) & (xml IS Component) THEN (* trick to get XML description of properties if not already there (new components) *)
xml(Component).properties.ToXML(element)
END;
properties.FromXML(element);
(* was: supercall to Repositories *)
enum := xml.GetContents();
WHILE enum.HasMoreElements() DO
c := enum.GetNext();
IF c IS XML.Element THEN
IF ~(c IS Component) OR ~c(Component).internal THEN
component := ComponentFromXML(c(XML.Element));
IF component # NIL THEN
AddContent(component)
END;
END;
END;
END;
enum :=xml.GetAttributes();
WHILE enum.HasMoreElements() DO
c := enum.GetNext();
IF c(XML.Attribute).GetName()^ # "generator" THEN
SetAttributeValue(c(XML.Attribute).GetName()^, c(XML.Attribute).GetValue()^);
END;
END;
(*Initialize;*) (* redundant *)
END FromXML;
PROCEDURE IsCallFromSequencer*():BOOLEAN;
BEGIN
ASSERT (sequencer # NIL);
RETURN sequencer.IsCallFromSequencer()
END IsCallFromSequencer;
PROCEDURE AssertLock*;
BEGIN
ASSERT((sequencer = NIL) OR sequencer.IsCallFromSequencer() OR sequencer.lock.HasReadLock())
END AssertLock;
(** Atomically set the components sequencer *)
PROCEDURE SetSequencer*(s : Messages.MsgSequencer);
VAR old : Messages.MsgSequencer; c : XML.Content;
BEGIN
old := sequencer;
IF old # NIL THEN old.lock.AcquireWrite() END;
sequencer := s;
c := GetFirst();
WHILE (c # NIL) DO
IF c IS Component THEN c(Component).SetSequencer(s); END; (*? what happens to old sequencers/active objects ?*)
c := GetNext(c);
END;
IF old # NIL THEN old.lock.ReleaseWrite() END
END SetSequencer;
PROCEDURE Acquire*;
BEGIN
IF sequencer # NIL THEN sequencer.lock.AcquireWrite END
END Acquire;
PROCEDURE Release*;
BEGIN
IF sequencer # NIL THEN sequencer.lock.ReleaseWrite END
END Release;
PROCEDURE CheckReadLock*;
BEGIN
IF (sequencer # NIL) & (~sequencer.lock.HasReadLock()) THEN
KernelLog.String("WMComponents.Component.CheckReadLock: FAILED!"); KernelLog.Ln;
sequencer.lock.WriteLock
END;
IF sequencer # NIL THEN ASSERT(sequencer.lock.HasReadLock()) END
END CheckReadLock;
(** AddContent adds a content (element or subtree) to the element *)
PROCEDURE AddContent*(c : XML.Content);
VAR m:Messages.Message; rect:Rectangles.Rectangle;
BEGIN
ASSERT(c # NIL);
Acquire;
BEGIN (*{EXCLUSIVE}*)(* EXCLUSIVE leads to deadlock ?*)
IF c IS WMProperties.Properties THEN
properties.SetXML(c(WMProperties.Properties));
ELSIF c IS Component THEN
IF sequencer#NIL THEN
c(Component).SetSequencer(sequencer);
c(Component).Reset(SELF,NIL); (* will be scheduled by sequencer. implied RecacheProperties*)
Initialize; (*? there is also a Initialize() within Reset() above, but that one seems sometimes not be effective because scheduled later; however ,this is partial redundancy *)
ELSE (* no tree traversal - is less costly *)
c(Component).initialized:=FALSE;
c(Component).sequencer:=NIL;
END;
ELSIF ~(c IS XML.Comment) THEN
Release; RETURN
END;
END;
(*Acquire;*)
AddContent^(c);
Release;
END AddContent;
PROCEDURE RemoveContent*(c : XML.Content);
BEGIN
(*ASSERT(c # NIL);*)
IF c = NIL THEN RETURN END;
Acquire;
RemoveContent^(c);
Release;
END RemoveContent;
(** Add internal component. Internal components are supposed to be created and managed by its parent component.
Internal components and their subcomponents are not externalized *)
PROCEDURE AddInternalComponent*(component : Component);
BEGIN
IF (component # NIL) THEN
component.internal := TRUE;
AddContent(component);
END;
END AddInternalComponent;
(** Return the root element of the component hierarchy. This is not necessarily the same as the
root element of XML since it is possible to have multiple component hierarchies in an XML file *)
PROCEDURE GetComponentRoot*(): Component;
VAR p, c : XML.Element;
BEGIN
c := SELF;
LOOP
p := c.GetParent();
IF (p # NIL) & (p IS Component) THEN c := p ELSE RETURN c(Component) END
END
END GetComponentRoot;
PROCEDURE Find*(id : ARRAY OF CHAR) : Component;
VAR
root, component : Component;
PROCEDURE IsUID(CONST id : ARRAY OF CHAR) : BOOLEAN;
BEGIN
RETURN id[0] = "&";
END IsUID;
PROCEDURE RemoveAmpersand(VAR id : ARRAY OF CHAR);
VAR i : LONGINT;
BEGIN
ASSERT(id[0] = "&");
FOR i := 0 TO LEN(id)-2 DO
id[i] := id[i + 1];
END;
END RemoveAmpersand;
BEGIN
component := NIL;
IF IsUID(id) THEN
RemoveAmpersand(id);
root := GetComponentRoot();
component := root.FindByUID(id);
ELSE
component := FindByPath(id, 0);
END;
RETURN component;
END Find;
(** Find a sub component by its uid *)
PROCEDURE FindByUID*(CONST uid : ARRAY OF CHAR) : Component;
VAR c : XML.Content; result : Component; s : Strings.String;
BEGIN
IF (uid = "") THEN RETURN NIL END;
s := SELF.uid.Get();
IF (s # NIL) & (s^ = uid) THEN
RETURN SELF
ELSE
result := NIL;
Acquire;
c := GetFirst();
WHILE (result = NIL) & (c # NIL) DO
IF (c IS Component) THEN result := c(Component).FindByUID(uid) END;
c := GetNext(c);
END;
Release;
RETURN result
END
END FindByUID;
(** find a component by relative path *)
PROCEDURE FindByPath*(CONST path : ARRAY OF CHAR; pos : LONGINT) : Component;
VAR component : Component;
BEGIN
Acquire;
component := FindRelativePath(SELF, path, pos);
Release;
RETURN component;
END FindByPath;
PROCEDURE StringToComponent*(str : Strings.String) : Component;
VAR
id : ARRAY 100 OF CHAR;
isUID : BOOLEAN;
ch : CHAR;
sr : Streams.StringReader;
r, target : Component;
BEGIN
NEW(sr, LEN(str)); sr.Set(str^);
isUID := FALSE; IF sr.Peek() = "%" THEN isUID := TRUE; ch := sr.Get() END;
sr.Token(id);
IF isUID THEN r := GetComponentRoot(); target := r.FindByUID(id);
IF target = NIL THEN KernelLog.String("StringToComponent : UID target not found: "); KernelLog.String(id); KernelLog.Ln; END
ELSE target := FindByPath(id, 0);
IF target = NIL THEN KernelLog.String("StringToComponent : Path target not found: "); KernelLog.String(id); KernelLog.Ln; END
END;
RETURN target
END StringToComponent;
(** Search a CompCommand by string *)
PROCEDURE StringToCompCommand*(eventstr : Strings.String) : WMEvents.EventListener;
VAR
id, name : ARRAY 100 OF CHAR;
isUID : BOOLEAN;
ch : CHAR;
sr : Streams.StringReader;
r, target : Component;
BEGIN
NEW(sr, LEN(eventstr)); sr.Set(eventstr^);
isUID := FALSE; IF sr.Peek() = "%" THEN isUID := TRUE; ch := sr.Get() END;
sr.Token(id); sr.SkipWhitespace; sr.Token(name);
IF isUID THEN r := GetComponentRoot(); target := r.FindByUID(id);
IF target = NIL THEN KernelLog.String("StringToEvent : UID target not found: "); KernelLog.String(id); KernelLog.Ln; END
ELSE target := FindByPath(id, 0);
IF target = NIL THEN KernelLog.String("StringToEvent : Path target not found: "); KernelLog.String(id); KernelLog.Ln; END
END;
IF target # NIL THEN RETURN target.eventListeners.GetHandlerByName(NewString(name))
ELSE RETURN NIL
END
END StringToCompCommand;
(** The Finalize Method is asynchronous since queuing could result in modules being freed before finalize ispropagated..
Active components should terminate, external resources should be released *)
PROCEDURE Finalize*; (** PROTECTED *)
VAR c : XML.Content;
BEGIN
IF TraceFinalize IN Trace THEN IF uid # NIL THEN (* KernelLog.String(uid.string) *) KernelLog.String(".Finalize") END END;
Acquire;
c := GetFirst();
WHILE (c # NIL) DO
IF (c IS Component) THEN c(Component).Finalize END;
c := GetNext(c);
END;
properties.Finalize;
Release;
END Finalize;
(* reset/initialize a hierarchy of components *)
PROCEDURE Reset*(sender, data : ANY); (** PROTECTED *)
VAR c : XML.Content;
BEGIN
IF ~IsCallFromSequencer() THEN
sequencer.ScheduleEvent(SELF.Reset, sender, data);
IF CanYield THEN Objects.Yield END;
ELSE
BEGIN (* how about exclusivity ?*)
RecacheProperties;
c := GetFirst();
WHILE (c # NIL) DO
IF c IS Component THEN
c(Component).Reset(sender, data)
END;
c := GetNext(c);
END;
Initialize;
END;
END
END Reset;
(* Initialize is called by Reset() and is required to render components responsive *)
PROCEDURE Initialize*; (** PROTECTED *)
BEGIN
BEGIN{EXCLUSIVE}
initialized := TRUE
END;
END Initialize;
(** Internal interface of the message handler. This method may only be called via the Handle method.
Components that need to handle messages should implement HandleInternal. *)
PROCEDURE HandleInternal*(VAR msg : Messages.Message); (** PROTECTED *)
VAR pa : WMProperties.PropertyArray; i : LONGINT;
BEGIN
ASSERT(IsCallFromSequencer());
IF (msg.msgType = Messages.MsgSetLanguage) & (msg.ext # NIL) & (msg.ext IS LanguageExtension) THEN
pa := properties.Enumerate();
IF (pa # NIL) THEN
FOR i := 0 TO LEN(pa) - 1 DO
IF (pa[i] # NIL) & (pa[i] IS WMProperties.StringProperty) THEN
pa[i](WMProperties.StringProperty).SetLanguage(msg.ext(LanguageExtension).languages);
END;
END;
END;
LanguageChanged(msg.ext(LanguageExtension).languages);
BroadcastSubcomponents(msg);
END;
END HandleInternal;
(** External interface to the message handler. Asynchronous messages are synchronized by
the sequencer of the Container *)
PROCEDURE Handle*(VAR msg : Messages.Message); (** FINAL *)
VAR s : Strings.String;
BEGIN
(* if asynchronous call --> synchronize *)
IF sequencer=NIL THEN RETURN
ELSIF ~IsCallFromSequencer() THEN
IF ~sequencer.Add(msg) THEN
s := uid.Get();
KernelLog.String("A message sent to ");
IF s # NIL THEN KernelLog.String(s^) ELSE KernelLog.String(" <uid = NIL>") END;
KernelLog.String(" was discarded")
END;
IF CanYield THEN Objects.Yield END (* give the sequencer an immediate chance to react -- important on single-processor machines *)
ELSE HandleInternal(msg) END
END Handle;
(** Broadcast a message to all direct subcomponents. The subcomponent can then decide
whether to further propagate the message to its children or not *)
PROCEDURE BroadcastSubcomponents*(VAR msg : Messages.Message); (** FINAL *)
VAR c : XML.Content;
BEGIN
Acquire;
c := GetFirst();
WHILE (c # NIL) DO
IF c IS Component THEN c(Component).Handle(msg) END;
c := GetNext(c);
END;
Release
END BroadcastSubcomponents;
(* not to be called from user *)
PROCEDURE LanguageChanged*(languages : Localization.Languages);
BEGIN
ASSERT(languages # NIL);
ASSERT(IsCallFromSequencer());
END LanguageChanged;
(* LinkChanged can be called to inform about changes of the state of links (i.e. objects in reference properties)
Unlike PropertyChanged which informs about an actual replacement of the link *)
PROCEDURE LinkChanged*(sender, link: ANY);
BEGIN ASSERT(IsCallFromSequencer());
END LinkChanged;
(* will be called synchronously if a property of the component changes. May not be called directly.
Call Invalidate in this procedure whenever a property changed that impacts the visualization.
No such messages are sent until the component is initialized *)
PROCEDURE PropertyChanged*(sender, property : ANY);(** PROTECTED *)
BEGIN ASSERT(IsCallFromSequencer());
END PropertyChanged;
(** called by the internal property changed handler via the sequencer, either if multiple properties have
changed or a Reset occured. The PropertyChanged method is called, too in case of multi-property changes
The component should call the inherited RecacheProperties method.
Do not call Invalidate in RecacheProperties, but rather in PropertyChanged(). *)
PROCEDURE RecacheProperties*;
BEGIN
END RecacheProperties;
PROCEDURE InternalPropertyChanged(sender, property : ANY);
BEGIN
IF ~initialized THEN RETURN END;
IF ~IsCallFromSequencer() THEN
sequencer.ScheduleEvent(SELF.InternalPropertyChanged, sender, property);
IF CanYield THEN Objects.Yield END;
ELSE
IF ~inPropertyUpdate THEN
inPropertyUpdate := TRUE;
IF property = properties THEN RecacheProperties END;
PropertyChanged(sender, property);
inPropertyUpdate := FALSE
END;
END
END InternalPropertyChanged;
PROCEDURE InternalLinkChanged(sender, property : ANY);
BEGIN
IF ~initialized THEN RETURN END;
IF ~IsCallFromSequencer() THEN
sequencer.ScheduleEvent(SELF.InternalLinkChanged, sender, property);
IF CanYield THEN Objects.Yield END;
ELSE
IF ~inLinkUpdate THEN
inLinkUpdate := TRUE;
LinkChanged(sender, property);
inLinkUpdate := FALSE
END;
END
END InternalLinkChanged;
END Component;
TYPE
Macro* = ARRAY 128 OF CHAR;
(** Installable macro handler procedure. {(originator # NIL) & (w # NIL)} *)
MacroHandlerProcedure* = PROCEDURE {DELEGATE} (CONST macro : Macro; originator : Component; w : Streams.Writer; VAR handled : BOOLEAN);
Namespace = ARRAY 16 OF CHAR;
MacroHandler = POINTER TO RECORD
handler : MacroHandlerProcedure;
namespace : Namespace;
next : MacroHandler;
END;
TYPE
(** Basic visual component *)
VisualComponent* = OBJECT(Component)
VAR
bounds-, bearing-, relativeBounds-: WMProperties.RectangleProperty;
alignment- : WMProperties.Int32Property;
fillColor- : WMProperties.ColorProperty;
font- : WMProperties.FontProperty;
scaleFont-: WMProperties.Int32Property;
visible-, takesFocus-, needsTab-, editMode- : WMProperties.BooleanProperty;
focusPrevious-, focusNext- : WMProperties.StringProperty;
model- : WMProperties.ReferenceProperty;
onStartDrag- : WMEvents.EventSource;
canvasState- : WMGraphics.CanvasState; (** PROTECTED *)
fPointerOwner : VisualComponent;
hasFocus- : BOOLEAN;
focusComponent : VisualComponent; (** Subcomponent that has the keyboard focus, if any *)
extPointerDown, extPointerUp, extPointerMove : PointerHandler;
extPointerLeave : PointerLeaveHandler;
extDragOver, extDragDropped : DragDropHandler;
extDragResult : DragResultHandler;
extKeyEvent : KeyEventHandler;
extDraw : DrawHandler;
extFocus : FocusHandler;
extContextMenu : ContextMenuHandler;
extGetPositionOwner : GetPositionOwnerHandler;
layoutManager : LayoutManager;
aligning* : BOOLEAN;
pointerInfo : WM.PointerInfo;
editRegion: LONGINT;
editX, editY: LONGINT;
keyFlags: SET; (*! remove *)
oldPointerInfo : WM.PointerInfo;
PROCEDURE &Init*;
BEGIN
Init^;
SetGenerator("WMComponents.NewVisualComponent");
SetNameAsString(StrVisualComponent);
NEW(bounds, PrototypeBounds, NIL, NIL); properties.Add(bounds);
NEW(relativeBounds, PrototypeBoundsRelative, NIL, NIL); properties.Add(relativeBounds);
NEW(bearing, PrototypeBearing, NIL, NIL); properties.Add(bearing);
NEW(alignment, PrototypeAlignment, NIL, NIL); properties.Add(alignment);
NEW(fillColor, PrototypeFillColor, NIL, NIL); properties.Add(fillColor);
NEW(visible, PrototypeVisible, NIL, NIL); properties.Add(visible);
NEW(takesFocus, PrototypeTakesFocus, NIL, NIL); properties.Add(takesFocus);
NEW(needsTab, PrototypeNeedsTab, NIL, NIL); properties.Add(needsTab);
NEW(focusPrevious, PrototypeFocusPrevious, NIL, NIL); properties.Add(focusPrevious);
NEW(focusNext, PrototypeFocusNext, NIL, NIL); properties.Add(focusNext);
NEW(editMode, PrototypeEditMode, NIL,NIL); properties.Add(editMode); editMode.Set(FALSE);
NEW(model, ModelPrototype, NIL, NIL); properties.Add(model);
NEW(font, PrototypeFont, NIL, NIL); properties.Add(font);
NEW(scaleFont, PrototypeScaleFont, NIL,NIL); properties.Add(scaleFont);
NEW(onStartDrag, SELF, GSonStartDrag,GSonStartDragInfo, SELF.StringToCompCommand);
events.Add(onStartDrag);
extGetPositionOwner := NIL;
aligning := FALSE; fPointerOwner := SELF; focusComponent := SELF;
END Init;
(** Focus handling *)
PROCEDURE TraceFocusChain*;
BEGIN
KernelLog.String(" -> ");
ShowComponent(SELF);
IF focusComponent = SELF THEN
KernelLog.String(" <END>"); KernelLog.Ln;
ELSIF focusComponent = NIL THEN
KernelLog.String("ERROR focusComponent is NIL"); KernelLog.Ln;
ELSE
focusComponent.TraceFocusChain;
END;
END TraceFocusChain;
(** Set the keyboard focus chain to this component its takesFocus field is set and unset the old chain *)
PROCEDURE SetFocus*;
VAR root, vc : VisualComponent; p : XML.Element;
BEGIN
Acquire;
IF (takesFocus.Get() OR editMode.Get()) & visible.Get() THEN
IF TraceFocus IN Trace THEN KernelLog.String("Set focus to: "); ShowComponent(SELF); KernelLog.Ln; END;
root := GetVisualComponentRoot();
IF (root IS Form) THEN root(Form).lastFocusComponent := SELF; END;
(* unset the old focus chain *)
(* find the leaf component that has the focus *)
vc := root;
WHILE (vc # NIL) & (vc.focusComponent # NIL) & (vc.focusComponent # vc) DO vc := vc.focusComponent; END;
(* clear the focus chain until the root or this component *)
p := vc;
WHILE (p # SELF) & (p # NIL) & (p IS VisualComponent) DO
vc := p(VisualComponent);
vc.focusComponent := vc;
vc.FocusLost;
IF (vc.extFocus # NIL) THEN vc.extFocus(FALSE); END;
p := p.GetParent();
END;
(* set the new chain *)
vc := SELF; vc.focusComponent := SELF;
WHILE (vc # NIL) DO
IF ~vc.hasFocus THEN
vc.FocusReceived;
IF vc.extFocus # NIL THEN vc.extFocus(TRUE) END;
END;
p := vc.GetParent();
IF (p # NIL) & (p IS VisualComponent) THEN
p(VisualComponent).focusComponent := vc; vc := p(VisualComponent);
ELSE
vc := NIL;
END;
END;
ELSE (* component does not take focus or is not visible *)
IF TraceFocus IN Trace THEN ShowComponent(SELF); KernelLog.String("does not take focus."); KernelLog.Ln END;
END;
Release;
END SetFocus;
PROCEDURE FocusReceived*;
BEGIN
hasFocus := TRUE
END FocusReceived;
PROCEDURE FocusLost*;
BEGIN
hasFocus := FALSE
END FocusLost;
PROCEDURE SetFocusTo(CONST id : ARRAY OF CHAR);
VAR vc : Component;
BEGIN
vc := Find(id);
IF (vc # NIL) & (vc IS VisualComponent) THEN
vc(VisualComponent).SetFocus;
ELSE
KernelLog.String("Warning: WMComponents.VisualComponent.SetFocusTo: Component ");
KernelLog.String(id); KernelLog.String(" not found."); KernelLog.Ln;
END;
END SetFocusTo;
PROCEDURE FocusNext*;
VAR string : Strings.String;
BEGIN
string := focusNext.Get();
IF (string # NIL) THEN
SetFocusTo(string^);
END;
END FocusNext;
PROCEDURE FocusPrev*;
VAR string : Strings.String;
BEGIN
string := focusPrevious.Get();
IF (string # NIL) THEN
SetFocusTo(string^);
END;
END FocusPrev;
(* LinkChanged can be called to inform about changes of the state of links (i.e. objects in reference properties)
Unlike PropertyChanged which informs about an actual replacement of the link *)
PROCEDURE LinkChanged*(sender, link: ANY);
BEGIN
IF sender = model THEN
Invalidate
END;
END LinkChanged;
PROCEDURE PropertyChanged*(sender, property : ANY);
BEGIN
IF property = bounds THEN
(*ScaleFont(bounds.GetHeight(), scaleFont.Get());*)
Resized (*implicit Invalidate*)
ELSIF property = bearing THEN Resized;
(* ELSIF bounds=relativeBounds THEN ? *)
ELSIF property = alignment THEN AlignmentChanged; Invalidate (*moved here from implicit Invalidate*)
ELSIF property = fillColor THEN Invalidate;
ELSIF property = font THEN
IF scaleFont.Get() # 0 THEN
ScaleFont(bounds.GetHeight(), scaleFont.Get()); (* implicit Invalidate*)
END;
Invalidate;
ELSIF (property = scaleFont) THEN ScaleFont(bounds.GetHeight(),scaleFont.Get()); (*implicit Invalidate*)
ELSIF property = visible THEN Resized (*Implicit Invalidate*)
(* ELSIF takesFocus, needsTab...*)
ELSIF property = editMode THEN Invalidate;
ELSIF property = model THEN LinkChanged(model, model.Get()); Invalidate;
ELSE PropertyChanged^(sender, property)
END;
END PropertyChanged;
PROCEDURE RecacheProperties;
BEGIN
RecacheProperties^;
IF scaleFont.Get() # 0 THEN ScaleFont(bounds.GetHeight(), scaleFont.Get()) END;
IF (model # NIL) & (model.Get() # NIL) THEN LinkChanged(model,model.Get()) END;
END RecacheProperties;
(** Get the root of visible components. Not neccessarily the same as GetComponentRoot() OR GetRoot() *)
PROCEDURE GetVisualComponentRoot*(): VisualComponent;
VAR p, c : XML.Element;
BEGIN
c := SELF;
LOOP
p := c.GetParent();
IF (p # NIL) & (p IS VisualComponent) THEN c := p
ELSE RETURN c(VisualComponent)
END
END
END GetVisualComponentRoot;
PROCEDURE AdaptRelativeBounds(inner: Rectangles.Rectangle; parent: XML.Element);
VAR outer: Rectangles.Rectangle;
BEGIN
Acquire;
IF (parent # NIL) & (parent IS VisualComponent) THEN
(* inner := bounds.Get();*)
outer := parent(VisualComponent).bounds.Get();
IF (outer.b - outer.t > 0) & (outer.r - outer.l > 0) THEN
relativeBounds.Set(Rectangles.MakeRect( (inner.l * MaxRel) DIV (outer.r-outer.l), (inner.t * MaxRel) DIV (outer.b-outer.t),
(inner.r * MaxRel) DIV (outer.r - outer.l), (inner.b * MaxRel) DIV (outer.b - outer.t)));
END;
END;
Release
END AdaptRelativeBounds;
(** Position handling *)
PROCEDURE AlignmentChanged;
VAR p : XML.Element; inner, outer: Rectangles.Rectangle;
BEGIN
Acquire;
IF alignment.Get()= AlignRelative THEN
AdaptRelativeBounds(bounds.Get(), GetParent());
END;
p := SELF.GetParent();
IF (p # NIL) & (p IS VisualComponent) THEN
p(VisualComponent).AlignSubComponents
END;
(*Invalidate;*)
Release
END AlignmentChanged;
(** Get the bounds of the component *)
PROCEDURE GetClientRect*() : Rectangles.Rectangle;
VAR r, t : Rectangles.Rectangle;
BEGIN
r := bounds.Get();
t := Rectangles.MakeRect(0, 0, r.r - r.l, r.b - r.t);
RETURN t
END GetClientRect;
PROCEDURE SetLayoutManager*(layoutManager : LayoutManager);
BEGIN
Acquire;
SELF.layoutManager := layoutManager;
Release
END SetLayoutManager;
PROCEDURE AlignEvent(sender, data: ANY);
BEGIN
AlignSubComponents;