WMComponents.Mod 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747
  1. MODULE WMComponents; (** AUTHOR "TF"; PURPOSE "Component Framework based on XML"; *)
  2. (**
  3. -- Events: --
  4. Each VisualComponent can produce keyboard and mouse events which can trigger A2 commands.
  5. The command string for a given event can by specified by the usage of XML attributes and component properties.
  6. The following attributes are defined:
  7. Keyboard: onReturn, onEscape, onKeyPressed, onKeyReleased
  8. Mouse: onLeftClick, onRightClick, onMiddleClick, onClick
  9. The command strings are processed (macro substitution) before the actual command is called.
  10. -- Macro substitution: --
  11. General form: "^" [namespace ":"] macrostring
  12. A macro always start with MacroCharacter ("^"). The next occurence of a whitespace character determines the end of the macro.
  13. Two consequent MacroCharacter's ("^^") will be replaced by the MacroCharacter ("^") not triggering macro substitution at all.
  14. The user can install MacroHandlerProcedures for a given namespace. At most one handler per namespace can be installed.
  15. If the namespace is omitted, the default macro handler is triggered.
  16. The DefaultMacroHandler currently supports the following macro substitutions:
  17. ^selection is replaced by the last selection of the user
  18. ^clipboard is replaced by the content of the clipboard
  19. ^attribute=[component "."] attribute
  20. ^property=[component "."] property
  21. is replaced by the value of <attribute> or <property>.
  22. If the component qualifier is omitted, <attribute> or <property> is supposed to be an attribute or property of the originator of the event.
  23. If no MacroHandlerProcedure is found for a given macro, no substitution takes place.
  24. Example:
  25. onLeftClick = SystemTools.Show ^attribute=generator
  26. onMiddleClick = SystemTools.Show ^property=FillColor
  27. *)
  28. (*PH 08/14:
  29. - avoid parallel call of FormWindow.SetContent, Component.AddContent, Form.InvalidateRect by different processes, through use of EXCLUSIVE sections.
  30. - send an "invalidate content" message to a window after it appears on the display, which is handled after "form" field is ready
  31. - restructure FormWindow.SetContent() to assure coherent displays and to assure FormWindow.content is consistent
  32. *)
  33. IMPORT
  34. KernelLog, Inputs, Streams, Events, Files, Texts, TextUtilities,
  35. XML, XMLScanner, XMLParser, XMLObjects, Codecs, Localization, Repositories,
  36. Messages := WMMessages, Rectangles := WMRectangles,
  37. WMEvents, WMProperties, WMGraphics, Strings, WM := WMWindowManager, Raster,
  38. Commands, Modules, Kernel, Locks, Objects, WMDropTarget;
  39. CONST
  40. Ok* = 0;
  41. DuplicateNamespace* = 1;
  42. AlignNone* = 0; AlignLeft* = 1; AlignTop* = 2; AlignRight* = 3; AlignBottom* = 4; AlignClient* = 5; AlignRelative*=6;
  43. None=0; Left=1; Right=2; Lower=3; Upper=4; LowerRight=5; UpperRight=6; LowerLeft=7; UpperLeft=8; Inside = 9;
  44. MaxRel = 16*1024;
  45. MaxComponentNameSize* = 64; (* including 0X *)
  46. TraceFocus = 0;
  47. TraceFinalize = 1;
  48. Trace = {};
  49. (* Enable event logging? *)
  50. Logging = TRUE;
  51. (* Macro handling *)
  52. (* General form of macro: MacroCharacter [Namespace + NamespaceCharacter] MacroName *)
  53. MacroCharacter = "^";
  54. NamespaceCharacter = ":";
  55. NoNamespace = "";
  56. (* Namespace used if no namespace is specified *)
  57. DefaultNamespace = "system";
  58. (* Macros names of default macro handler *)
  59. MacroSelection = "selection";
  60. MacroClipboard = "clipboard";
  61. MacroAttributePrefix = "attribute=";
  62. MacroPropertyPrefix = "property=";
  63. CanYield = TRUE;
  64. (*temporary - to be removed*)
  65. FlagDirty=13;
  66. TYPE
  67. (** Installable event preview handlers. Are called by the components sequencer thread *)
  68. PointerHandler* = PROCEDURE {DELEGATE} (x, y : LONGINT; keys : SET; VAR handled : BOOLEAN);
  69. PointerLeaveHandler* = PROCEDURE {DELEGATE} (VAR handled : BOOLEAN);
  70. DragDropHandler* = PROCEDURE {DELEGATE} (x, y : LONGINT; dragInfo : WM.DragInfo; VAR handled : BOOLEAN);
  71. DragResultHandler* = PROCEDURE {DELEGATE} (accepted : BOOLEAN; recipient : ANY; dragInfo : WM.DragInfo; VAR handled : BOOLEAN);
  72. DragAutoStartHandler* = PROCEDURE {DELEGATE} (VAR handled : BOOLEAN);
  73. FocusHandler* = PROCEDURE {DELEGATE} (hasFocus : BOOLEAN);
  74. ContextMenuHandler* = PROCEDURE {DELEGATE} (sender : ANY; x, y: LONGINT);
  75. KeyEventHandler* = PROCEDURE {DELEGATE} (ucs : LONGINT; flags : SET; VAR keySym : LONGINT; VAR handled : BOOLEAN);
  76. DrawHandler* = PROCEDURE {DELEGATE} (canvas : WMGraphics.Canvas);
  77. Recursion*= ENUM None*, FromComponent*, FromBottom* END;
  78. TYPE
  79. SetStringProcedure = PROCEDURE {DELEGATE} (CONST string : ARRAY OF CHAR; x,y : LONGINT; VAR res : LONGINT);
  80. DropTarget = OBJECT(WMDropTarget.DropTarget)
  81. VAR
  82. originator : ANY;
  83. setString : SetStringProcedure;
  84. x,y : LONGINT;
  85. PROCEDURE &Init(originator : ANY; setString : SetStringProcedure; x,y : LONGINT);
  86. BEGIN
  87. ASSERT(setString # NIL);
  88. SELF.originator := originator;
  89. SELF.setString := setString;
  90. SELF.x := x;
  91. SELF.y := y;
  92. END Init;
  93. PROCEDURE GetInterface(type : LONGINT) : WMDropTarget.DropInterface;
  94. VAR sdi : DropString;
  95. BEGIN
  96. IF (type = WMDropTarget.TypeString) THEN
  97. NEW(sdi, originator, setString, x,y); RETURN sdi;
  98. ELSE
  99. RETURN NIL;
  100. END;
  101. END GetInterface;
  102. END DropTarget;
  103. DropString = OBJECT(WMDropTarget.DropString)
  104. VAR
  105. originator : ANY;
  106. setString : SetStringProcedure;
  107. x,y : LONGINT;
  108. PROCEDURE &Init(originator : ANY; setString : SetStringProcedure; x,y : LONGINT);
  109. BEGIN
  110. ASSERT(setString # NIL);
  111. SELF.originator := originator;
  112. SELF.setString := setString;
  113. SELF.x := x; SELF.y := y;
  114. END Init;
  115. PROCEDURE Set(CONST string : ARRAY OF CHAR; VAR res : LONGINT);
  116. BEGIN
  117. setString(string, x,y, res);
  118. END Set;
  119. END DropString;
  120. LanguageExtension* = POINTER TO RECORD(Messages.MessageExtension)
  121. languages* : Localization.Languages;
  122. END;
  123. ToggleEditMode* = POINTER TO RECORD
  124. recursion*: Recursion;
  125. END;
  126. FindComponentMode* = POINTER TO RECORD END;
  127. Event* = RECORD
  128. END;
  129. KeyPressedEvent* = RECORD(Event)
  130. ucs- : LONGINT;
  131. flags- : SET;
  132. keysym- : LONGINT;
  133. END;
  134. PointerEvent* = RECORD(Event)
  135. x-, y-, z- : LONGINT;
  136. keys- : SET;
  137. END;
  138. EventContext* = OBJECT(Repositories.Context)
  139. VAR
  140. originator- : Component; (** {originator # NIL} *)
  141. command- : Strings.String; (** {command # NIL}, immutable *)
  142. timestamp- : LONGINT;
  143. PROCEDURE &New*(originator : Component; command : Strings.String; in, arg : Streams.Reader; out, error : Streams.Writer; caller : OBJECT);
  144. BEGIN
  145. ASSERT((originator # NIL) & (command # NIL));
  146. SELF.originator := originator;
  147. SELF.command := command;
  148. Init(in, arg, out, error, caller);
  149. END New;
  150. END EventContext;
  151. PointerContext* = OBJECT(EventContext)
  152. VAR
  153. pointer- : PointerEvent;
  154. END PointerContext;
  155. KeyContext* = OBJECT(EventContext)
  156. VAR
  157. key- : KeyPressedEvent;
  158. END KeyContext;
  159. TYPE
  160. (** Basic component *)
  161. ComponentStyleChanged = OBJECT
  162. END ComponentStyleChanged;
  163. Component* = OBJECT(Repositories.Component)
  164. VAR
  165. sequencer- : Messages.MsgSequencer;
  166. initialized- : BOOLEAN;
  167. properties- : WMProperties.PropertyList;
  168. events- : WMEvents.EventSourceList;
  169. eventListeners- : WMEvents.EventListenerList;
  170. id-, uid- : WMProperties.StringProperty;
  171. enabled- : WMProperties.BooleanProperty;
  172. (* discard property changes that come from a property change within the same component*)
  173. inPropertyUpdate, inLinkUpdate : BOOLEAN;
  174. (* If TRUE, this component is supposed to be created and managed by its parent. It is not externalized. *)
  175. internal- : BOOLEAN;
  176. (* after Init() , calling Reset() implicitely by insertion into FormWindow or explicitely, thereby triggering Initialize() is required to render component responsive to messages *)
  177. PROCEDURE &Init*;
  178. BEGIN
  179. Init^;
  180. SetNameAsString(StrComponent);
  181. sequencer := NIL;
  182. initialized := FALSE;
  183. NEW(properties); properties.onPropertyChanged.Add(SELF.InternalPropertyChanged); properties.onLinkChanged.Add(SELF.InternalLinkChanged);
  184. NEW(events);
  185. NEW(eventListeners);
  186. NEW(id, PrototypeID, NIL, NIL); properties.Add(id);
  187. NEW(uid, PrototypeUID, NIL, NIL); properties.Add(uid);
  188. NEW(enabled, PrototypeEnabled, NIL, NIL); properties.Add(enabled);
  189. inPropertyUpdate := FALSE;
  190. inLinkUpdate := FALSE;
  191. internal := FALSE;
  192. SetGenerator("WMComponents.NewComponent");
  193. END Init;
  194. PROCEDURE Write*(w : Streams.Writer;context: ANY; level : LONGINT);
  195. VAR enum: XMLObjects.Enumerator; c: ANY; name : Strings.String; nextLevel : LONGINT;
  196. BEGIN
  197. IF IsLocked() THEN (* D.String("Component.Write: islocked"); D.Ln; *) RETURN; END;
  198. IF ~internal THEN
  199. name := GetName();
  200. w.Char('<'); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^) END;
  201. WriteAttributes(w, context, level);
  202. w.Char('>');
  203. properties.WriteXML(w, context, level);
  204. nextLevel := level + 1;
  205. ELSE
  206. (* D.String("Component.Write: isInternal"); D.Ln; *)
  207. nextLevel := level;
  208. END;
  209. enum := GetContents();
  210. WHILE enum.HasMoreElements() DO
  211. c := enum.GetNext();
  212. IF ~(c IS WMProperties.Properties) THEN
  213. IF ~((c IS Component) & ((c(Component).internal) OR c(Component).IsLocked())) THEN NewLine(w, 0); NewLine(w, nextLevel); END;
  214. c(XML.Content).Write(w, context, nextLevel);
  215. (*c(Component).Write(w, context, nextLevel)*)
  216. END;
  217. END;
  218. IF ~internal THEN
  219. NewLine(w, level);
  220. w.String("</"); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^); END; w.Char('>')
  221. END;
  222. END Write;
  223. (*
  224. PROCEDURE ToRepository*(CONST repository: ARRAY OF CHAR; w: Streams.Writer; level: LONGINT);
  225. VAR enum: XMLObjects.Enumerator; c: ANY; name : Strings.String; nextLevel : LONGINT;
  226. BEGIN
  227. IF IsLocked() THEN RETURN; END;
  228. IF ~internal THEN
  229. name := GetName();
  230. w.Char('<'); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^) END;
  231. WriteAttributes(w, NIL, level);
  232. w.Char('>');
  233. properties.ToRepository(repository,w, level);
  234. nextLevel := level + 1;
  235. ELSE
  236. nextLevel := level;
  237. END;
  238. enum := GetContents();
  239. WHILE enum.HasMoreElements() DO
  240. c := enum.GetNext();
  241. IF ~(c IS WMProperties.Properties) THEN
  242. IF ~((c IS Component) & ((c(Component).internal) OR c(Component).IsLocked())) THEN NewLine(w, 0); NewLine(w, nextLevel); END;
  243. IF (c IS Repositories.Component) THEN
  244. c(Repositories.Component).ToRepository(repository, w, level);
  245. ELSE
  246. c(XML.Content).Write(w, NIL, nextLevel);
  247. END;
  248. END;
  249. END;
  250. IF ~internal THEN
  251. NewLine(w, level);
  252. w.String("</"); IF name = NIL THEN w.String("_NILNAME_") ELSE w.String(name^); END; w.Char('>')
  253. END;
  254. END ToRepository;
  255. *)
  256. PROCEDURE FromXML*(xml: XML.Element);
  257. VAR component: Component; enum: XMLObjects.Enumerator; c: ANY; element: XML.Element;
  258. BEGIN
  259. element := GetElementByName(xml,"Properties");
  260. IF (element = NIL) & (xml IS Component) THEN (* trick to get XML description of properties if not already there (new components) *)
  261. xml(Component).properties.ToXML(element)
  262. END;
  263. properties.FromXML(element);
  264. (* was: supercall to Repositories *)
  265. enum := xml.GetContents();
  266. WHILE enum.HasMoreElements() DO
  267. c := enum.GetNext();
  268. IF c IS XML.Element THEN
  269. IF ~(c IS Component) OR ~c(Component).internal THEN
  270. component := ComponentFromXML(c(XML.Element));
  271. IF component # NIL THEN
  272. AddContent(component)
  273. END;
  274. END;
  275. END;
  276. END;
  277. enum :=xml.GetAttributes();
  278. WHILE enum.HasMoreElements() DO
  279. c := enum.GetNext();
  280. IF c(XML.Attribute).GetName()^ # "generator" THEN
  281. SetAttributeValue(c(XML.Attribute).GetName()^, c(XML.Attribute).GetValue()^);
  282. END;
  283. END;
  284. (*Initialize;*) (* redundant *)
  285. END FromXML;
  286. PROCEDURE IsCallFromSequencer*():BOOLEAN;
  287. BEGIN
  288. ASSERT (sequencer # NIL);
  289. RETURN sequencer.IsCallFromSequencer()
  290. END IsCallFromSequencer;
  291. PROCEDURE AssertLock*;
  292. BEGIN
  293. ASSERT((sequencer = NIL) OR sequencer.IsCallFromSequencer() OR sequencer.lock.HasReadLock())
  294. END AssertLock;
  295. (** Atomically set the components sequencer *)
  296. PROCEDURE SetSequencer*(s : Messages.MsgSequencer);
  297. VAR old : Messages.MsgSequencer; c : XML.Content;
  298. BEGIN
  299. old := sequencer;
  300. IF old # NIL THEN old.lock.AcquireWrite() END;
  301. sequencer := s;
  302. c := GetFirst();
  303. WHILE (c # NIL) DO
  304. IF c IS Component THEN c(Component).SetSequencer(s); END; (*? what happens to old sequencers/active objects ?*)
  305. c := GetNext(c);
  306. END;
  307. IF old # NIL THEN old.lock.ReleaseWrite() END
  308. END SetSequencer;
  309. PROCEDURE Acquire*;
  310. BEGIN
  311. IF sequencer # NIL THEN sequencer.lock.AcquireWrite END
  312. END Acquire;
  313. PROCEDURE Release*;
  314. BEGIN
  315. IF sequencer # NIL THEN sequencer.lock.ReleaseWrite END
  316. END Release;
  317. PROCEDURE CheckReadLock*;
  318. BEGIN
  319. IF (sequencer # NIL) & (~sequencer.lock.HasReadLock()) THEN
  320. KernelLog.String("WMComponents.Component.CheckReadLock: FAILED!"); KernelLog.Ln;
  321. sequencer.lock.WriteLock
  322. END;
  323. IF sequencer # NIL THEN ASSERT(sequencer.lock.HasReadLock()) END
  324. END CheckReadLock;
  325. (** AddContent adds a content (element or subtree) to the element *)
  326. PROCEDURE AddContent*(c : XML.Content);
  327. VAR m:Messages.Message; rect:Rectangles.Rectangle;
  328. BEGIN
  329. ASSERT(c # NIL);
  330. Acquire;
  331. BEGIN (*{EXCLUSIVE}*)(* EXCLUSIVE leads to deadlock ?*)
  332. IF c IS WMProperties.Properties THEN
  333. properties.SetXML(c(WMProperties.Properties));
  334. ELSIF c IS Component THEN
  335. IF sequencer#NIL THEN
  336. c(Component).SetSequencer(sequencer);
  337. c(Component).Reset(SELF,NIL); (* will be scheduled by sequencer. implied RecacheProperties*)
  338. 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 *)
  339. ELSE (* no tree traversal - is less costly *)
  340. c(Component).initialized:=FALSE;
  341. c(Component).sequencer:=NIL;
  342. END;
  343. ELSIF ~(c IS XML.Comment) THEN
  344. Release; RETURN
  345. END;
  346. END;
  347. (*Acquire;*)
  348. AddContent^(c);
  349. Release;
  350. END AddContent;
  351. PROCEDURE RemoveContent*(c : XML.Content);
  352. BEGIN
  353. (*ASSERT(c # NIL);*)
  354. IF c = NIL THEN RETURN END;
  355. Acquire;
  356. RemoveContent^(c);
  357. Release;
  358. END RemoveContent;
  359. (** Add internal component. Internal components are supposed to be created and managed by its parent component.
  360. Internal components and their subcomponents are not externalized *)
  361. PROCEDURE AddInternalComponent*(component : Component);
  362. BEGIN
  363. IF (component # NIL) THEN
  364. component.internal := TRUE;
  365. AddContent(component);
  366. END;
  367. END AddInternalComponent;
  368. (** Return the root element of the component hierarchy. This is not necessarily the same as the
  369. root element of XML since it is possible to have multiple component hierarchies in an XML file *)
  370. PROCEDURE GetComponentRoot*(): Component;
  371. VAR p, c : XML.Element;
  372. BEGIN
  373. c := SELF;
  374. LOOP
  375. p := c.GetParent();
  376. IF (p # NIL) & (p IS Component) THEN c := p ELSE RETURN c(Component) END
  377. END
  378. END GetComponentRoot;
  379. PROCEDURE Find*(id : ARRAY OF CHAR) : Component;
  380. VAR
  381. root, component : Component;
  382. PROCEDURE IsUID(CONST id : ARRAY OF CHAR) : BOOLEAN;
  383. BEGIN
  384. RETURN id[0] = "&";
  385. END IsUID;
  386. PROCEDURE RemoveAmpersand(VAR id : ARRAY OF CHAR);
  387. VAR i : LONGINT;
  388. BEGIN
  389. ASSERT(id[0] = "&");
  390. FOR i := 0 TO LEN(id)-2 DO
  391. id[i] := id[i + 1];
  392. END;
  393. END RemoveAmpersand;
  394. BEGIN
  395. component := NIL;
  396. IF IsUID(id) THEN
  397. RemoveAmpersand(id);
  398. root := GetComponentRoot();
  399. component := root.FindByUID(id);
  400. ELSE
  401. component := FindByPath(id, 0);
  402. END;
  403. RETURN component;
  404. END Find;
  405. (** Find a sub component by its uid *)
  406. PROCEDURE FindByUID*(CONST uid : ARRAY OF CHAR) : Component;
  407. VAR c : XML.Content; result : Component; s : Strings.String;
  408. BEGIN
  409. IF (uid = "") THEN RETURN NIL END;
  410. s := SELF.uid.Get();
  411. IF (s # NIL) & (s^ = uid) THEN
  412. RETURN SELF
  413. ELSE
  414. result := NIL;
  415. Acquire;
  416. c := GetFirst();
  417. WHILE (result = NIL) & (c # NIL) DO
  418. IF (c IS Component) THEN result := c(Component).FindByUID(uid) END;
  419. c := GetNext(c);
  420. END;
  421. Release;
  422. RETURN result
  423. END
  424. END FindByUID;
  425. (** find a component by relative path *)
  426. PROCEDURE FindByPath*(CONST path : ARRAY OF CHAR; pos : LONGINT) : Component;
  427. VAR component : Component;
  428. BEGIN
  429. Acquire;
  430. component := FindRelativePath(SELF, path, pos);
  431. Release;
  432. RETURN component;
  433. END FindByPath;
  434. PROCEDURE StringToComponent*(str : Strings.String) : Component;
  435. VAR
  436. id : ARRAY 100 OF CHAR;
  437. isUID : BOOLEAN;
  438. ch : CHAR;
  439. sr : Streams.StringReader;
  440. r, target : Component;
  441. BEGIN
  442. NEW(sr, LEN(str)); sr.Set(str^);
  443. isUID := FALSE; IF sr.Peek() = "%" THEN isUID := TRUE; ch := sr.Get() END;
  444. sr.Token(id);
  445. IF isUID THEN r := GetComponentRoot(); target := r.FindByUID(id);
  446. IF target = NIL THEN KernelLog.String("StringToComponent : UID target not found: "); KernelLog.String(id); KernelLog.Ln; END
  447. ELSE target := FindByPath(id, 0);
  448. IF target = NIL THEN KernelLog.String("StringToComponent : Path target not found: "); KernelLog.String(id); KernelLog.Ln; END
  449. END;
  450. RETURN target
  451. END StringToComponent;
  452. (** Search a CompCommand by string *)
  453. PROCEDURE StringToCompCommand*(eventstr : Strings.String) : WMEvents.EventListener;
  454. VAR
  455. id, name : ARRAY 100 OF CHAR;
  456. isUID : BOOLEAN;
  457. ch : CHAR;
  458. sr : Streams.StringReader;
  459. r, target : Component;
  460. BEGIN
  461. NEW(sr, LEN(eventstr)); sr.Set(eventstr^);
  462. isUID := FALSE; IF sr.Peek() = "%" THEN isUID := TRUE; ch := sr.Get() END;
  463. sr.Token(id); sr.SkipWhitespace; sr.Token(name);
  464. IF isUID THEN r := GetComponentRoot(); target := r.FindByUID(id);
  465. IF target = NIL THEN KernelLog.String("StringToEvent : UID target not found: "); KernelLog.String(id); KernelLog.Ln; END
  466. ELSE target := FindByPath(id, 0);
  467. IF target = NIL THEN KernelLog.String("StringToEvent : Path target not found: "); KernelLog.String(id); KernelLog.Ln; END
  468. END;
  469. IF target # NIL THEN RETURN target.eventListeners.GetHandlerByName(NewString(name))
  470. ELSE RETURN NIL
  471. END
  472. END StringToCompCommand;
  473. (** The Finalize Method is asynchronous since queuing could result in modules being freed before finalize ispropagated..
  474. Active components should terminate, external resources should be released *)
  475. PROCEDURE Finalize*; (** PROTECTED *)
  476. VAR c : XML.Content;
  477. BEGIN
  478. IF TraceFinalize IN Trace THEN IF uid # NIL THEN (* KernelLog.String(uid.string) *) KernelLog.String(".Finalize") END END;
  479. Acquire;
  480. c := GetFirst();
  481. WHILE (c # NIL) DO
  482. IF (c IS Component) THEN c(Component).Finalize END;
  483. c := GetNext(c);
  484. END;
  485. properties.Finalize;
  486. Release;
  487. END Finalize;
  488. (* reset/initialize a hierarchy of components *)
  489. PROCEDURE Reset*(sender, data : ANY); (** PROTECTED *)
  490. VAR c : XML.Content;
  491. BEGIN
  492. IF ~IsCallFromSequencer() THEN
  493. sequencer.ScheduleEvent(SELF.Reset, sender, data);
  494. IF CanYield THEN Objects.Yield END;
  495. ELSE
  496. BEGIN (* how about exclusivity ?*)
  497. RecacheProperties;
  498. c := GetFirst();
  499. WHILE (c # NIL) DO
  500. IF c IS Component THEN
  501. c(Component).Reset(sender, data)
  502. END;
  503. c := GetNext(c);
  504. END;
  505. IF ~initialized THEN Initialize END;
  506. END;
  507. END
  508. END Reset;
  509. (* Initialize is called by Reset() and is required to render components responsive *)
  510. PROCEDURE Initialize*; (** PROTECTED *)
  511. BEGIN
  512. BEGIN{EXCLUSIVE}
  513. initialized := TRUE
  514. END;
  515. END Initialize;
  516. (** Internal interface of the message handler. This method may only be called via the Handle method.
  517. Components that need to handle messages should implement HandleInternal. *)
  518. PROCEDURE HandleInternal*(VAR msg : Messages.Message); (** PROTECTED *)
  519. VAR pa : WMProperties.PropertyArray; i : LONGINT;
  520. BEGIN
  521. ASSERT(IsCallFromSequencer());
  522. IF (msg.msgType = Messages.MsgSetLanguage) & (msg.ext # NIL) & (msg.ext IS LanguageExtension) THEN
  523. pa := properties.Enumerate();
  524. IF (pa # NIL) THEN
  525. FOR i := 0 TO LEN(pa) - 1 DO
  526. IF (pa[i] # NIL) & (pa[i] IS WMProperties.StringProperty) THEN
  527. pa[i](WMProperties.StringProperty).SetLanguage(msg.ext(LanguageExtension).languages);
  528. END;
  529. END;
  530. END;
  531. LanguageChanged(msg.ext(LanguageExtension).languages);
  532. BroadcastSubcomponents(msg);
  533. ELSE
  534. BroadcastSubcomponents(msg); (*added PH 0816 - unhandled messages may be meant for children !*)
  535. END;
  536. END HandleInternal;
  537. (** External interface to the message handler. Asynchronous messages are synchronized by
  538. the sequencer of the Container *)
  539. PROCEDURE Handle*(VAR msg : Messages.Message); (** FINAL *)
  540. VAR s : Strings.String;
  541. BEGIN
  542. (* if asynchronous call --> synchronize *)
  543. IF sequencer=NIL THEN RETURN
  544. ELSIF ~IsCallFromSequencer() THEN
  545. IF ~sequencer.Add(msg) THEN
  546. s := uid.Get();
  547. KernelLog.String("A message sent to ");
  548. IF s # NIL THEN KernelLog.String(s^) ELSE KernelLog.String(" <uid = NIL>") END;
  549. KernelLog.String(" was discarded")
  550. END;
  551. IF CanYield THEN Objects.Yield END (* give the sequencer an immediate chance to react -- important on single-processor machines *)
  552. ELSE HandleInternal(msg) END
  553. END Handle;
  554. (** Broadcast a message to all direct subcomponents. The subcomponent can then decide
  555. whether to further propagate the message to its children or not *)
  556. PROCEDURE BroadcastSubcomponents*(VAR msg : Messages.Message); (** FINAL *)
  557. VAR c : XML.Content;
  558. BEGIN
  559. Acquire;
  560. c := GetFirst();
  561. WHILE (c # NIL) DO
  562. IF c IS Component THEN c(Component).Handle(msg) END;
  563. c := GetNext(c);
  564. END;
  565. Release
  566. END BroadcastSubcomponents;
  567. (* not to be called from user *)
  568. PROCEDURE LanguageChanged*(languages : Localization.Languages);
  569. BEGIN
  570. ASSERT(languages # NIL);
  571. ASSERT(IsCallFromSequencer());
  572. END LanguageChanged;
  573. (* LinkChanged can be called to inform about changes of the state of links (i.e. objects in reference properties)
  574. Unlike PropertyChanged which informs about an actual replacement of the link *)
  575. PROCEDURE LinkChanged*(sender, link: ANY);
  576. BEGIN ASSERT(IsCallFromSequencer());
  577. END LinkChanged;
  578. (* will be called synchronously if a property of the component changes. May not be called directly.
  579. Call Invalidate in this procedure whenever a property changed that impacts the visualization.
  580. No such messages are sent until the component is initialized *)
  581. PROCEDURE PropertyChanged*(sender, property : ANY);(** PROTECTED *)
  582. BEGIN ASSERT(IsCallFromSequencer());
  583. END PropertyChanged;
  584. (** called by the internal property changed handler via the sequencer, either if multiple properties have
  585. changed or a Reset occured. The PropertyChanged method is called, too in case of multi-property changes
  586. The component should call the inherited RecacheProperties method.
  587. Do not call Invalidate in RecacheProperties, but rather in PropertyChanged(). *)
  588. PROCEDURE RecacheProperties*;
  589. BEGIN
  590. END RecacheProperties;
  591. PROCEDURE InternalPropertyChanged(sender, property : ANY);
  592. BEGIN
  593. IF ~initialized THEN RETURN END;
  594. IF ~IsCallFromSequencer() THEN
  595. sequencer.ScheduleEvent(SELF.InternalPropertyChanged, sender, property);
  596. IF CanYield THEN Objects.Yield END;
  597. ELSE
  598. IF ~inPropertyUpdate THEN
  599. inPropertyUpdate := TRUE;
  600. IF property = properties THEN RecacheProperties END;
  601. PropertyChanged(sender, property);
  602. inPropertyUpdate := FALSE
  603. END;
  604. END
  605. END InternalPropertyChanged;
  606. PROCEDURE InternalLinkChanged(sender, link : ANY);
  607. BEGIN
  608. IF ~initialized THEN RETURN END;
  609. IF ~IsCallFromSequencer() THEN
  610. sequencer.ScheduleEvent(SELF.InternalLinkChanged, sender, link);
  611. IF CanYield THEN Objects.Yield END;
  612. ELSE
  613. IF ~inLinkUpdate THEN
  614. inLinkUpdate := TRUE;
  615. LinkChanged(sender, link);
  616. inLinkUpdate := FALSE
  617. END;
  618. END
  619. END InternalLinkChanged;
  620. END Component;
  621. TYPE
  622. Macro* = ARRAY 128 OF CHAR;
  623. (** Installable macro handler procedure. {(originator # NIL) & (w # NIL)} *)
  624. MacroHandlerProcedure* = PROCEDURE {DELEGATE} (CONST macro : Macro; originator : Component; w : Streams.Writer; VAR handled : BOOLEAN);
  625. Namespace = ARRAY 16 OF CHAR;
  626. MacroHandler = POINTER TO RECORD
  627. handler : MacroHandlerProcedure;
  628. namespace : Namespace;
  629. next : MacroHandler;
  630. END;
  631. TYPE
  632. (** Basic visual component *)
  633. VisualComponent* = OBJECT(Component)
  634. VAR
  635. bounds-, bearing-, relativeBounds-: WMProperties.RectangleProperty;
  636. alignment- : WMProperties.Int32Property;
  637. fillColor- : WMProperties.ColorProperty;
  638. font- : WMProperties.FontProperty;
  639. scaleFont-: WMProperties.Int32Property;
  640. visible-, takesFocus-, needsTab-, editMode- : WMProperties.BooleanProperty;
  641. focusPrevious-, focusNext- : WMProperties.StringProperty;
  642. model- : WMProperties.ReferenceProperty;
  643. onStartDrag- : WMEvents.EventSource;
  644. canvasState- : WMGraphics.CanvasState; (** PROTECTED *)
  645. fPointerOwner : VisualComponent;
  646. hasFocus- : BOOLEAN;
  647. focusComponent : VisualComponent; (** Subcomponent that has the keyboard focus, if any *)
  648. extPointerDown, extPointerUp, extPointerMove : PointerHandler;
  649. extPointerLeave : PointerLeaveHandler;
  650. extDragOver, extDragDropped : DragDropHandler;
  651. extDragResult : DragResultHandler;
  652. extKeyEvent : KeyEventHandler;
  653. extDraw : DrawHandler;
  654. extFocus : FocusHandler;
  655. extContextMenu : ContextMenuHandler;
  656. extGetPositionOwner : GetPositionOwnerHandler;
  657. layoutManager : LayoutManager;
  658. aligning* : BOOLEAN;
  659. pointerInfo : WM.PointerInfo;
  660. editRegion: LONGINT;
  661. editX, editY: LONGINT;
  662. keyFlags: SET; (*! remove *)
  663. oldPointerInfo : WM.PointerInfo;
  664. PROCEDURE &Init*;
  665. BEGIN
  666. Init^;
  667. SetGenerator("WMComponents.NewVisualComponent");
  668. SetNameAsString(StrVisualComponent);
  669. NEW(bounds, PrototypeBounds, NIL, NIL); properties.Add(bounds);
  670. NEW(relativeBounds, PrototypeBoundsRelative, NIL, NIL); properties.Add(relativeBounds);
  671. NEW(bearing, PrototypeBearing, NIL, NIL); properties.Add(bearing);
  672. NEW(alignment, PrototypeAlignment, NIL, NIL); properties.Add(alignment);
  673. NEW(fillColor, PrototypeFillColor, NIL, NIL); properties.Add(fillColor);
  674. NEW(visible, PrototypeVisible, NIL, NIL); properties.Add(visible);
  675. NEW(takesFocus, PrototypeTakesFocus, NIL, NIL); properties.Add(takesFocus);
  676. NEW(needsTab, PrototypeNeedsTab, NIL, NIL); properties.Add(needsTab);
  677. NEW(focusPrevious, PrototypeFocusPrevious, NIL, NIL); properties.Add(focusPrevious);
  678. NEW(focusNext, PrototypeFocusNext, NIL, NIL); properties.Add(focusNext);
  679. NEW(editMode, PrototypeEditMode, NIL,NIL); properties.Add(editMode); editMode.Set(FALSE);
  680. NEW(model, ModelPrototype, NIL, NIL); properties.Add(model);
  681. NEW(font, PrototypeFont, NIL, NIL); properties.Add(font);
  682. NEW(scaleFont, PrototypeScaleFont, NIL,NIL); properties.Add(scaleFont);
  683. NEW(onStartDrag, SELF, GSonStartDrag,GSonStartDragInfo, SELF.StringToCompCommand);
  684. events.Add(onStartDrag);
  685. extGetPositionOwner := NIL;
  686. aligning := FALSE; fPointerOwner := SELF; focusComponent := SELF;
  687. END Init;
  688. (** Focus handling *)
  689. PROCEDURE TraceFocusChain*;
  690. BEGIN
  691. KernelLog.String(" -> ");
  692. ShowComponent(SELF);
  693. IF focusComponent = SELF THEN
  694. KernelLog.String(" <END>"); KernelLog.Ln;
  695. ELSIF focusComponent = NIL THEN
  696. KernelLog.String("ERROR focusComponent is NIL"); KernelLog.Ln;
  697. ELSE
  698. focusComponent.TraceFocusChain;
  699. END;
  700. END TraceFocusChain;
  701. (** Set the keyboard focus chain to this component its takesFocus field is set and unset the old chain *)
  702. PROCEDURE SetFocus*;
  703. VAR root, vc : VisualComponent; p : XML.Element;
  704. BEGIN
  705. Acquire;
  706. IF (takesFocus.Get() OR editMode.Get()) & visible.Get() THEN
  707. IF TraceFocus IN Trace THEN KernelLog.String("Set focus to: "); ShowComponent(SELF); KernelLog.Ln; END;
  708. root := GetVisualComponentRoot();
  709. IF (root IS Form) THEN root(Form).lastFocusComponent := SELF; END;
  710. (* unset the old focus chain *)
  711. (* find the leaf component that has the focus *)
  712. vc := root;
  713. WHILE (vc # NIL) & (vc.focusComponent # NIL) & (vc.focusComponent # vc) DO vc := vc.focusComponent; END;
  714. (* clear the focus chain until the root or this component *)
  715. p := vc;
  716. WHILE (p # SELF) & (p # NIL) & (p IS VisualComponent) DO
  717. vc := p(VisualComponent);
  718. vc.focusComponent := vc;
  719. vc.FocusLost;
  720. IF (vc.extFocus # NIL) THEN vc.extFocus(FALSE); END;
  721. p := p.GetParent();
  722. END;
  723. (* set the new chain *)
  724. vc := SELF; vc.focusComponent := SELF;
  725. WHILE (vc # NIL) DO
  726. IF ~vc.hasFocus THEN
  727. vc.FocusReceived;
  728. IF vc.extFocus # NIL THEN vc.extFocus(TRUE) END;
  729. END;
  730. p := vc.GetParent();
  731. IF (p # NIL) & (p IS VisualComponent) THEN
  732. p(VisualComponent).focusComponent := vc; vc := p(VisualComponent);
  733. ELSE
  734. vc := NIL;
  735. END;
  736. END;
  737. ELSE (* component does not take focus or is not visible *)
  738. IF TraceFocus IN Trace THEN ShowComponent(SELF); KernelLog.String("does not take focus."); KernelLog.Ln END;
  739. END;
  740. Release;
  741. END SetFocus;
  742. PROCEDURE FocusReceived*;
  743. BEGIN
  744. hasFocus := TRUE
  745. END FocusReceived;
  746. PROCEDURE FocusLost*;
  747. BEGIN
  748. hasFocus := FALSE
  749. END FocusLost;
  750. PROCEDURE SetFocusTo(CONST id : ARRAY OF CHAR);
  751. VAR vc : Component;
  752. BEGIN
  753. vc := Find(id);
  754. IF (vc # NIL) & (vc IS VisualComponent) THEN
  755. vc(VisualComponent).SetFocus;
  756. ELSE
  757. KernelLog.String("Warning: WMComponents.VisualComponent.SetFocusTo: Component ");
  758. KernelLog.String(id); KernelLog.String(" not found."); KernelLog.Ln;
  759. END;
  760. END SetFocusTo;
  761. PROCEDURE FocusNext*;
  762. VAR string : Strings.String;
  763. BEGIN
  764. string := focusNext.Get();
  765. IF (string # NIL) THEN
  766. SetFocusTo(string^);
  767. END;
  768. END FocusNext;
  769. PROCEDURE FocusPrev*;
  770. VAR string : Strings.String;
  771. BEGIN
  772. string := focusPrevious.Get();
  773. IF (string # NIL) THEN
  774. SetFocusTo(string^);
  775. END;
  776. END FocusPrev;
  777. (* LinkChanged can be called to inform about changes of the state of links (i.e. objects in reference properties)
  778. Unlike PropertyChanged which informs about an actual replacement of the link *)
  779. PROCEDURE LinkChanged*(sender, link: ANY);
  780. BEGIN
  781. IF sender = model THEN
  782. Invalidate
  783. END;
  784. END LinkChanged;
  785. PROCEDURE PropertyChanged*(sender, property : ANY);
  786. BEGIN
  787. IF property = bounds THEN
  788. (*ScaleFont(bounds.GetHeight(), scaleFont.Get());*)
  789. Resized (*implicit Invalidate*)
  790. ELSIF property = bearing THEN Resized;
  791. (* ELSIF bounds=relativeBounds THEN ? *)
  792. ELSIF property = alignment THEN AlignmentChanged; Invalidate (*moved here from implicit Invalidate*)
  793. ELSIF property = fillColor THEN Invalidate;
  794. ELSIF property = font THEN
  795. IF scaleFont.Get() # 0 THEN
  796. ScaleFont(bounds.GetHeight(), scaleFont.Get()); (* implicit Invalidate*)
  797. END;
  798. Invalidate;
  799. ELSIF (property = scaleFont) THEN ScaleFont(bounds.GetHeight(),scaleFont.Get()); (*implicit Invalidate*)
  800. ELSIF property = visible THEN Resized (*Implicit Invalidate*)
  801. (* ELSIF takesFocus, needsTab...*)
  802. ELSIF property = editMode THEN Invalidate;
  803. ELSIF property = model THEN LinkChanged(model, model.Get());
  804. ELSE PropertyChanged^(sender, property)
  805. END;
  806. END PropertyChanged;
  807. PROCEDURE RecacheProperties;
  808. BEGIN
  809. RecacheProperties^;
  810. IF scaleFont.Get() # 0 THEN ScaleFont(bounds.GetHeight(), scaleFont.Get()) END;
  811. IF (model # NIL) & (model.Get() # NIL) THEN LinkChanged(model,model.Get()) END;
  812. END RecacheProperties;
  813. (** Get the root of visible components. Not neccessarily the same as GetComponentRoot() OR GetRoot() *)
  814. PROCEDURE GetVisualComponentRoot*(): VisualComponent;
  815. VAR p, c : XML.Element;
  816. BEGIN
  817. c := SELF;
  818. LOOP
  819. p := c.GetParent();
  820. IF (p # NIL) & (p IS VisualComponent) THEN c := p
  821. ELSE RETURN c(VisualComponent)
  822. END
  823. END
  824. END GetVisualComponentRoot;
  825. PROCEDURE AdaptRelativeBounds(inner: Rectangles.Rectangle; parent: XML.Element);
  826. VAR outer: Rectangles.Rectangle;
  827. BEGIN
  828. Acquire;
  829. IF (parent # NIL) & (parent IS VisualComponent) THEN
  830. (* inner := bounds.Get();*)
  831. outer := parent(VisualComponent).bounds.Get();
  832. IF (outer.b - outer.t > 0) & (outer.r - outer.l > 0) THEN
  833. relativeBounds.Set(Rectangles.MakeRect( (inner.l * MaxRel) DIV (outer.r-outer.l), (inner.t * MaxRel) DIV (outer.b-outer.t),
  834. (inner.r * MaxRel) DIV (outer.r - outer.l), (inner.b * MaxRel) DIV (outer.b - outer.t)));
  835. END;
  836. END;
  837. Release
  838. END AdaptRelativeBounds;
  839. (** Position handling *)
  840. PROCEDURE AlignmentChanged;
  841. VAR p : XML.Element; inner, outer: Rectangles.Rectangle;
  842. BEGIN
  843. Acquire;
  844. IF alignment.Get()= AlignRelative THEN
  845. AdaptRelativeBounds(bounds.Get(), GetParent());
  846. END;
  847. p := SELF.GetParent();
  848. IF (p # NIL) & (p IS VisualComponent) THEN
  849. p(VisualComponent).AlignSubComponents
  850. END;
  851. (*Invalidate;*)
  852. Release
  853. END AlignmentChanged;
  854. (** Get the bounds of the component *)
  855. PROCEDURE GetClientRect*() : Rectangles.Rectangle;
  856. VAR r, t : Rectangles.Rectangle;
  857. BEGIN
  858. r := bounds.Get();
  859. t := Rectangles.MakeRect(0, 0, r.r - r.l, r.b - r.t);
  860. RETURN t
  861. END GetClientRect;
  862. PROCEDURE SetLayoutManager*(layoutManager : LayoutManager);
  863. BEGIN
  864. Acquire;
  865. SELF.layoutManager := layoutManager;
  866. Release
  867. END SetLayoutManager;
  868. PROCEDURE AlignEvent(sender, data: ANY);
  869. BEGIN
  870. AlignSubComponents;
  871. END AlignEvent;
  872. PROCEDURE AlignSubComponents*;
  873. VAR c : XML.Content; vc : VisualComponent;
  874. r, b, rel : Rectangles.Rectangle;
  875. BEGIN
  876. Acquire;
  877. IF (sequencer # NIL) & ~IsCallFromSequencer() THEN sequencer.ScheduleEvent(SELF.AlignEvent, NIL,NIL); Release; RETURN
  878. ELSIF sequencer = NIL THEN Release; RETURN
  879. END;
  880. IF aligning THEN Release; RETURN END;
  881. DisableUpdate;
  882. aligning := TRUE;
  883. IF layoutManager # NIL THEN layoutManager(SELF)
  884. ELSE
  885. r := GetClientRect();
  886. c := GetFirst();
  887. WHILE (c # NIL) DO
  888. IF c IS VisualComponent THEN
  889. vc := c(VisualComponent);
  890. IF vc.visible.Get() THEN
  891. b := vc.bearing.Get();
  892. CASE vc.alignment.Get() OF
  893. | AlignTop : vc.bounds.Set(Rectangles.MakeRect(r.l + b.l , r.t + b.t, r.r - b.r, r.t + b.t + vc.bounds.GetHeight())); INC(r.t, vc.bounds.GetHeight() + b.t + b.b)
  894. | AlignLeft : vc.bounds.Set(Rectangles.MakeRect(r.l + b.l, r.t + b.t, r.l + b.l + vc.bounds.GetWidth(), r.b - b.b)); INC(r.l, vc.bounds.GetWidth() + b.l + b.r)
  895. | AlignBottom : vc.bounds.Set(Rectangles.MakeRect(r.l + b.l, r.b - vc.bounds.GetHeight() - b.b, r.r - b.r, r.b - b.b)); DEC(r.b, vc.bounds.GetHeight() + b.t + b.b)
  896. | AlignRight : vc.bounds.Set(Rectangles.MakeRect(r.r - vc.bounds.GetWidth() - b.r , r.t + b.t, r.r - b.r, r.b - b.b)); DEC(r.r, vc.bounds.GetWidth() + b.l + b.r);
  897. | AlignClient : IF ~Rectangles.RectEmpty(r) THEN vc.bounds.Set(Rectangles.MakeRect(r.l + b.l , r.t + b.t, r.r - b.r, r.b - b.b)) END
  898. | AlignRelative:
  899. IF ~editMode.Get() THEN
  900. rel := vc.relativeBounds.Get();
  901. vc.bounds.Set(Rectangles.MakeRect(r.l + ((r.r-r.l)*rel.l+MaxRel DIV 2) DIV MaxRel, r.t + ((r.b-r.t)*rel.t+MaxRel DIV 2) DIV MaxRel,
  902. r.l + ((r.r-r.l)*rel.r +MaxRel DIV 2) DIV MaxRel, r.t+((r.b-r.t)*rel.b + MaxRel DIV 2) DIV MaxRel));
  903. ELSE
  904. vc.AdaptRelativeBounds(vc.bounds.Get(),SELF);
  905. END;
  906. ELSE (* nothing *)
  907. END;
  908. END;
  909. END;
  910. c := GetNext(c);
  911. END;
  912. END;
  913. EnableUpdate;
  914. aligning := FALSE;
  915. Release;
  916. END AlignSubComponents;
  917. PROCEDURE Initialize*;
  918. BEGIN
  919. Initialize^;
  920. AlignSubComponents;
  921. IF sequencer#NIL THEN Invalidate END;
  922. END Initialize;
  923. (** Locating *)
  924. (** transform the local component coordinates into global window manager coordinates *)
  925. PROCEDURE ToWMCoordinates*(x, y : LONGINT; VAR gx, gy : LONGINT);
  926. VAR cr : Component; tc : XML.Element; r : Rectangles.Rectangle;
  927. BEGIN
  928. gx := x; gy := y; tc := SELF;
  929. REPEAT
  930. IF (tc # NIL) & (tc IS VisualComponent) THEN
  931. r := tc(VisualComponent).bounds.Get();
  932. INC(gx, r.l); INC(gy, r.t)
  933. END;
  934. tc := tc.GetParent()
  935. UNTIL (tc = NIL) OR ~(tc IS VisualComponent);
  936. cr := GetComponentRoot();
  937. IF (cr # NIL) & (cr IS Form) THEN
  938. INC(gx, cr(Form).window.bounds.l);
  939. INC(gy, cr(Form).window.bounds.t)
  940. END
  941. END ToWMCoordinates;
  942. (** Return if the component is hit at (x, y) in component coordinates *)
  943. PROCEDURE IsHit*(x, y: LONGINT): BOOLEAN;
  944. BEGIN
  945. RETURN visible.Get() & enabled.Get() & Rectangles.PointInRect(x, y, GetClientRect())
  946. END IsHit;
  947. (** Return the topmost first child component at (x, y) *)
  948. PROCEDURE GetPositionOwner*(x, y: LONGINT): VisualComponent;
  949. VAR c: XML.Content; result, vc : VisualComponent; r : Rectangles.Rectangle;
  950. BEGIN
  951. Acquire;
  952. result := SELF;
  953. c := GetFirst();
  954. WHILE (c # NIL) DO
  955. IF c IS VisualComponent THEN
  956. vc := c(VisualComponent);
  957. r := vc.bounds.Get();
  958. IF Rectangles.PointInRect(x, y, r) & vc.IsHit(x - r.l, y - r.t) THEN
  959. result := vc
  960. END;
  961. END;
  962. c := GetNext(c);
  963. END;
  964. Release;
  965. RETURN result
  966. END GetPositionOwner;
  967. (** DragOver is called via the message handler. The should call manager.SetDragAccept(SELF, .... *)
  968. PROCEDURE DragOver*(x, y: LONGINT; dragInfo : WM.DragInfo);
  969. END DragOver;
  970. (** Dropped is called via the message handler to indicate an item has been dropped. *)
  971. PROCEDURE DragDropped*(x, y: LONGINT; dragInfo : WM.DragInfo);
  972. BEGIN
  973. IF dragInfo.onReject # NIL THEN dragInfo.onReject(SELF,dragInfo) END;
  974. END DragDropped;
  975. (*
  976. PROCEDURE EditDragOver(x,y: LONGINT; dragInfo: WMWindowManager.DragInfo);
  977. BEGIN
  978. END EditDragOver;
  979. *)
  980. PROCEDURE FromXML(xml: XML.Element);
  981. BEGIN
  982. FromXML^(xml);
  983. END FromXML;
  984. (*
  985. PROCEDURE AddContent*(c : XML.Content);
  986. VAR m:Messages.Message;
  987. BEGIN
  988. AddContent^(c);
  989. IF c IS VisualComponent THEN
  990. m.sender:=SELF (*c*); (*move to VisualComponent ?*)
  991. m.msgType := Messages.MsgInvalidate;
  992. m.msgSubType := Messages.MsgSubAll;
  993. (* SELF(VisualComponent).Invalidate ...*)
  994. IF sequencer.Add(m) THEN END;
  995. END;
  996. END AddContent;
  997. *)
  998. PROCEDURE AddVisualComponent(c :VisualComponent; x, y : LONGINT);
  999. VAR bounds : Rectangles.Rectangle;canvas: WMGraphics.BufferCanvas; relativeAlignment: BOOLEAN;
  1000. BEGIN
  1001. ASSERT(c # NIL);
  1002. IF (c.bounds.GetWidth() < 10) OR (c.bounds.GetHeight() < 10) THEN
  1003. c.bounds.SetExtents(40, 20);
  1004. END;
  1005. bounds := c.bounds.Get();
  1006. Rectangles.MoveRel(bounds, x, y);
  1007. c.bounds.Set(bounds);
  1008. c.AdaptRelativeBounds(c.bounds.Get(), SELF);
  1009. (*
  1010. IF c.sequencer # sequencer THEN c.SetSequencer(sequencer) END; (* redundant - implicit in AddContent *)
  1011. c.Reset(NIL, NIL); (*currently redundant - already in happens Component.AddContent() *)
  1012. c.RecacheProperties; (*currently redundant - already in happens Reset() *)
  1013. *)
  1014. Acquire;
  1015. AddContent(c);
  1016. Release;
  1017. END AddVisualComponent;
  1018. PROCEDURE EditDragDropped(x,y: LONGINT; dragInfo: WM.DragInfo): BOOLEAN;
  1019. VAR data: ANY; e: ComponentListEntry; parent: XML.Element; dt: DropTarget; pos: LONGINT;
  1020. BEGIN
  1021. data := dragInfo.data;
  1022. IF (data # NIL) & (data IS VisualComponent) THEN
  1023. IF dragInfo.sender # SELF THEN
  1024. IF dragInfo.onAccept # NIL THEN dragInfo.onAccept(SELF,dragInfo) END;
  1025. data(VisualComponent).bounds.Set(Rectangles.MakeRect(0, 0, data(VisualComponent).bounds.GetWidth(), data(VisualComponent).bounds.GetHeight()));
  1026. AddVisualComponent(data(VisualComponent),x+dragInfo.offsetX,y+dragInfo.offsetY);
  1027. Invalidate;
  1028. ELSE
  1029. parent := GetParent();
  1030. IF parent = NIL THEN RETURN FALSE END;
  1031. x := x + bounds.GetLeft();
  1032. y := y + bounds.GetTop();
  1033. RETURN parent(VisualComponent).EditDragDropped(x,y,dragInfo);
  1034. END;
  1035. RETURN TRUE
  1036. ELSIF (data # NIL) & (data IS Repositories.Component) THEN
  1037. IF dragInfo.onAccept # NIL THEN dragInfo.onAccept(SELF,dragInfo) END;
  1038. model.Set(data(Repositories.Component));
  1039. RETURN TRUE
  1040. ELSIF (data # NIL) & (data IS SelectionList) THEN
  1041. IF (dragInfo.sender # SELF) & ~data(SelectionList).Has(SELF) THEN
  1042. IF dragInfo.onAccept # NIL THEN dragInfo.onAccept(SELF,dragInfo) END;
  1043. e := data(SelectionList).first;
  1044. WHILE e # NIL DO
  1045. e.component.bounds.Set(Rectangles.MakeRect(0, 0, e.component.bounds.GetWidth(), e.component.bounds.GetHeight()));
  1046. ASSERT(e.component IS VisualComponent);
  1047. AddVisualComponent(e.component,x+e.dx+dragInfo.offsetX, y+e.dy + dragInfo.offsetY);
  1048. e := e.next;
  1049. END;
  1050. Invalidate;
  1051. ELSE
  1052. parent := GetParent();
  1053. IF parent = NIL THEN RETURN FALSE END;
  1054. x := x + bounds.GetLeft();
  1055. y := y + bounds.GetTop();
  1056. RETURN parent(VisualComponent).EditDragDropped(x,y,dragInfo);
  1057. END;
  1058. RETURN TRUE
  1059. ELSE
  1060. NEW(dt, SELF, SetDroppedString, x,y);
  1061. dragInfo.data := dt;
  1062. ConfirmDrag(TRUE, dragInfo);
  1063. RETURN FALSE
  1064. END;
  1065. END EditDragDropped;
  1066. PROCEDURE SetDroppedString( CONST string : ARRAY OF CHAR; x,y : LONGINT; VAR res : LONGINT);
  1067. VAR gen: XML.GeneratorProcedure; moduleName, procedureName ,msg: Modules.Name; element: XML.Element;
  1068. context: Repositories.Context; repositoryName, componentName: ARRAY 265 OF CHAR; componentID: LONGINT; object: Repositories.Component;
  1069. BEGIN
  1070. Commands.Split(string, moduleName, procedureName, res, msg);
  1071. IF (res = Commands.Ok) THEN
  1072. GETPROCEDURE(moduleName, procedureName, gen);
  1073. END;
  1074. IF gen # NIL THEN
  1075. element := gen();
  1076. ELSIF Repositories.IsCommandString(string) THEN
  1077. Repositories.CallCommand(string, context, res);
  1078. IF (res = Repositories.Ok) & (context.object # NIL) & (context.object IS Repositories.Component) THEN
  1079. element := context.object(Repositories.Component);
  1080. END;
  1081. ELSIF Repositories.SplitName(string, repositoryName, componentName, componentID) THEN
  1082. Repositories.GetComponent(repositoryName, componentName, componentID, object, res);
  1083. element := object;
  1084. END;
  1085. IF (element # NIL) & (element IS VisualComponent) THEN
  1086. AddVisualComponent(element(VisualComponent),x,y);
  1087. Invalidate;
  1088. ELSIF (element # NIL) & (element IS Repositories.Component) THEN
  1089. model.Set(element(Repositories.Component))
  1090. END;
  1091. res := 1; (* to avoid removal of source *)
  1092. END SetDroppedString;
  1093. (** Is called via the message handler to inform about the result of a recent drag operation *)
  1094. PROCEDURE DragResult*(accepted : BOOLEAN; recipient : ANY; dragInfo : WM.DragInfo);
  1095. END DragResult;
  1096. (** Start a drag operation. *)
  1097. PROCEDURE StartDrag*(data : ANY; img : WMGraphics.Image; offsetX, offsetY: LONGINT; onAccept, onReject : Messages.CompCommand) : BOOLEAN;
  1098. VAR rc : Component;
  1099. BEGIN
  1100. rc := GetVisualComponentRoot();
  1101. IF (rc # NIL) & (rc IS Form) THEN
  1102. RETURN rc(Form).window.StartDrag(SELF, data, img, offsetX, offsetY, onAccept, onReject)
  1103. ELSE
  1104. RETURN FALSE
  1105. END
  1106. END StartDrag;
  1107. (** confirm a drag operation. *)
  1108. PROCEDURE ConfirmDrag*(accept : BOOLEAN; dragInfo : WM.DragInfo);
  1109. VAR rc : Component;
  1110. BEGIN
  1111. rc := GetVisualComponentRoot();
  1112. IF (rc # NIL) & (rc IS Form) THEN rc(Form).window.ConfirmDrag(accept, dragInfo)
  1113. END
  1114. END ConfirmDrag;
  1115. (** Is called by the component if it detects a default drag action. The subclass should then call StartDrag with
  1116. the respective coordinates. If it wants to start the drag operation *)
  1117. PROCEDURE AutoStartDrag*;
  1118. BEGIN
  1119. onStartDrag.Call(NIL)
  1120. END AutoStartDrag;
  1121. (** Is called by the component if it detects a request for a context menu. The subclass should open the
  1122. context menu if applicable *)
  1123. PROCEDURE ShowContextMenu*(x, y : LONGINT);
  1124. BEGIN
  1125. IF extContextMenu # NIL THEN extContextMenu(SELF, x, y) END;
  1126. END ShowContextMenu;
  1127. (** Special methods *)
  1128. PROCEDURE Resized*;
  1129. VAR p : XML.Element;
  1130. BEGIN
  1131. (*
  1132. AdaptRelativeBounds(GetParent());
  1133. *)
  1134. IF sequencer # NIL THEN ASSERT(sequencer.lock.HasWriteLock()) END;
  1135. DisableUpdate;
  1136. p := SELF.GetParent();
  1137. IF (p # NIL) & (p IS VisualComponent) & (alignment.Get() # AlignNone) THEN p(VisualComponent).AlignSubComponents END;
  1138. IF visible.Get() THEN
  1139. AlignSubComponents;
  1140. IF scaleFont.Get() # 0 THEN ScaleFont(bounds.GetHeight(), scaleFont.Get()) END;
  1141. END;
  1142. EnableUpdate;
  1143. IF (p # NIL) & (p IS VisualComponent) THEN p(VisualComponent).Invalidate
  1144. ELSE Invalidate()
  1145. END
  1146. END Resized;
  1147. (** Is called before any sub-components are drawn *)
  1148. PROCEDURE DrawBackground*(canvas : WMGraphics.Canvas);
  1149. VAR color,i : LONGINT; name:Strings.String;
  1150. (* DebugUpdates can be used in order to visualize updates via some color cycling
  1151. Moreover, it slows down display extremely such that updates can be seen
  1152. *)
  1153. CONST DebugUpdates = FALSE;
  1154. BEGIN
  1155. (* message tracing
  1156. IF sequencer = Messages.debug THEN
  1157. D.Enter;
  1158. D.Ln;
  1159. D.String("##############"); D.Ln;
  1160. name := GetName();
  1161. IF name # NIL THEN D.String(name^); D.Ln; END;
  1162. name := id.Get();
  1163. IF name # NIL THEN D.String(name^); D.Ln; END;
  1164. D.Int(Kernel.GetTicks(),1); D.Ln;
  1165. (*D.TraceBack;*)
  1166. D.Exit;
  1167. END;
  1168. *)
  1169. CheckReadLock;
  1170. IF DebugUpdates THEN
  1171. canvas.Fill(GetClientRect(), Kernel.GetTicks()*100H +0FFH, WMGraphics.ModeSrcOverDst);
  1172. FOR i := 0 TO 10000000 DO END;
  1173. ELSE
  1174. color := fillColor.Get();
  1175. IF color # 0 THEN canvas.Fill(GetClientRect(), color, WMGraphics.ModeSrcOverDst) END;
  1176. END;
  1177. END DrawBackground;
  1178. (** Is called after all sub-components are drawn *)
  1179. PROCEDURE DrawForeground*(canvas : WMGraphics.Canvas);
  1180. END DrawForeground;
  1181. PROCEDURE DrawSelection(canvas : WMGraphics.Canvas);
  1182. VAR r,r0: Rectangles.Rectangle; x,y,x0,y0: LONGINT; color: LONGINT;
  1183. PROCEDURE MarkSelected(r: Rectangles.Rectangle; w, color: LONGINT);
  1184. VAR r0: Rectangles.Rectangle;
  1185. BEGIN
  1186. r0 :=r; r0.r := r.l+w; r0.b := r.t+w;
  1187. canvas.Fill(r0, color, WMGraphics.ModeSrcOverDst);
  1188. r0 :=r; r0.r := r.l+w; r0.t := r.b-w;
  1189. canvas.Fill(r0, color, WMGraphics.ModeSrcOverDst);
  1190. r0 :=r; r0.l := r.r-w; r0.b := r.t+w;
  1191. canvas.Fill(r0, color, WMGraphics.ModeSrcOverDst);
  1192. r0 :=r; r0.l := r.r-w; r0.t := r.b-w;
  1193. canvas.Fill(r0, color, WMGraphics.ModeSrcOverDst);
  1194. r0 := r; r0.l := r.r-1; canvas.Fill(r0,color, WMGraphics.ModeSrcOverDst);
  1195. r0 := r; r0.r := r.l+1; canvas.Fill(r0,color, WMGraphics.ModeSrcOverDst);
  1196. r0 := r; r0.b := r.t+1; canvas.Fill(r0,color, WMGraphics.ModeSrcOverDst);
  1197. r0 := r; r0.t := r.b-1; canvas.Fill(r0,color, WMGraphics.ModeSrcOverDst);
  1198. END MarkSelected;
  1199. BEGIN
  1200. CheckReadLock;
  1201. r := GetClientRect();
  1202. IF editMode.Get() THEN
  1203. y := r.t + (-r.t) MOD 8;
  1204. y0 := 0;
  1205. WHILE y < r.b DO
  1206. r0.t := y; r0.b := y+2;
  1207. x := r.l + (-r.l) MOD 8; x0 := 0;
  1208. WHILE x < r.r DO
  1209. r0.l := x; r0.r := x+2;
  1210. IF ODD(x DIV 8+y DIV 8) THEN color := 060H;
  1211. ELSE color := LONGINT(0FFFFFF60H);
  1212. END;
  1213. canvas.Fill(r0,color, WMGraphics.ModeSrcOverDst);
  1214. INC(x,8); INC(x0);
  1215. END;
  1216. INC(y,8);INC(y0);
  1217. END;
  1218. END;
  1219. IF selection.Has(SELF) THEN
  1220. IF selection.state = 0 THEN
  1221. MarkSelected(r,8,LONGINT(080H));
  1222. ELSE
  1223. MarkSelected(r,8,LONGINT(0FFFFFFFF80H));
  1224. END;
  1225. END;
  1226. END DrawSelection;
  1227. PROCEDURE DrawSubComponents*(canvas : WMGraphics.Canvas);
  1228. VAR c : XML.Content; vc : VisualComponent; cr, r : Rectangles.Rectangle;
  1229. BEGIN
  1230. CheckReadLock;
  1231. canvas.GetClipRect(cr);
  1232. canvas.SaveState(canvasState);
  1233. (* draw all sub-components *)
  1234. c := GetFirst();
  1235. WHILE (c # NIL) DO
  1236. IF c IS VisualComponent THEN
  1237. vc := c(VisualComponent); r := vc.bounds.Get();
  1238. IF Rectangles.Intersect(r, cr) THEN (* only draw if the component has a chance to be visible *)
  1239. canvas.SetClipRect(r); canvas.SetClipMode({WMGraphics.ClipRect});
  1240. canvas.ClipRectAsNewLimits(r.l, r.t);
  1241. vc.Draw(canvas);
  1242. canvas.RestoreState(canvasState);
  1243. END;
  1244. END;
  1245. c := GetNext(c);
  1246. END;
  1247. END DrawSubComponents;
  1248. PROCEDURE GetFont*() : WMGraphics.Font;
  1249. BEGIN
  1250. IF font.Get() = NIL THEN RETURN WMGraphics.GetDefaultFont()
  1251. ELSE RETURN font.Get()
  1252. END
  1253. END GetFont;
  1254. PROCEDURE SetFont*(font : WMGraphics.Font);
  1255. BEGIN
  1256. Acquire;
  1257. IF SELF.font.Get() # font THEN
  1258. SELF.font.Set(font);
  1259. (*?Invalidate()*) (* Invalidate already in PropertyChanged() *)
  1260. END;
  1261. Release
  1262. END SetFont;
  1263. PROCEDURE ScaleFont*(height: LONGINT; percent: LONGINT);
  1264. VAR fh,newSize: LONGINT; f: WMGraphics.Font;
  1265. BEGIN
  1266. IF height < 4 THEN height := 4 END;
  1267. IF percent <= 0 THEN RETURN END;
  1268. Acquire;
  1269. f := GetFont();
  1270. f := WMGraphics.GetFont(f.name, 100, f.style); (* expensive ? *)
  1271. fh := f.GetAscent() + f.GetDescent();
  1272. fh := height * percent DIV fh;
  1273. IF fh > 100 THEN fh := fh - fh MOD 8
  1274. ELSIF fh > 32 THEN fh := fh - fh MOD 4
  1275. ELSIF fh > 12 THEN fh := fh - fh MOD 2
  1276. END;
  1277. IF font.GetSize() # fh THEN
  1278. font.SetSize(fh);
  1279. Invalidate;
  1280. END;
  1281. Release;
  1282. END ScaleFont;
  1283. (** Called by the component owner whenever a redraw to a canvas is needed. Caller must hold hierarchy lock *)
  1284. PROCEDURE Draw*(canvas : WMGraphics.Canvas);
  1285. VAR command: Strings.String; event: Event;
  1286. BEGIN
  1287. (*
  1288. can lead to deadlock:
  1289. we hold the lock "lock"
  1290. onDraw tries to get the Objects lock, but this may be held by other component (should better not, but did, dead: WMPartitionsComponents.OperationEventHandler
  1291. command := GetAttributeValue("onDraw");
  1292. IF (command # NIL) THEN HandleEvent(event, SELF, command); END;
  1293. *)
  1294. CheckReadLock;
  1295. IF ~visible.Get() THEN RETURN END;
  1296. canvas.SaveState(canvasState);
  1297. IF font # NIL THEN canvas.SetFont(font.Get()) END;
  1298. DrawBackground(canvas);
  1299. IF extDraw # NIL THEN extDraw(canvas) END;
  1300. DrawSelection(canvas);
  1301. DrawSubComponents(canvas);
  1302. DrawForeground(canvas);
  1303. canvas.RestoreState(canvasState)
  1304. END Draw;
  1305. (** declare a rectangle area as dirty *)
  1306. PROCEDURE InvalidateRect*(r: Rectangles.Rectangle);
  1307. VAR parent : XML.Element;
  1308. m : Messages.Message; b : Rectangles.Rectangle;
  1309. BEGIN
  1310. IF ~initialized THEN RETURN END;
  1311. IF ~visible.Get() THEN RETURN END;
  1312. IF ~IsCallFromSequencer() THEN
  1313. m.msgType := Messages.MsgInvalidate;
  1314. m.msgSubType := Messages.MsgSubRectangle;
  1315. (*
  1316. m.msgType := Messages.MsgExt;
  1317. m.ext := invalidateRectMsg;
  1318. *)
  1319. m.x := r.l; m.y := r.t; m.dx := r.r; m.dy := r.b; m.sender := SELF;
  1320. IF sequencer.Add(m) THEN IF CanYield THEN Objects.Yield END END;
  1321. ELSE
  1322. parent := GetParent();
  1323. IF (parent # NIL) & (parent IS VisualComponent) THEN
  1324. b := bounds.Get();
  1325. Rectangles.MoveRel(r, b.l, b.t);
  1326. parent(VisualComponent).InvalidateRect(r)
  1327. END
  1328. END
  1329. END InvalidateRect;
  1330. PROCEDURE PostInvalidateCommand*(sender, par : ANY);
  1331. VAR m: Messages.Message; r, b: Rectangles.Rectangle; client: VisualComponent; parent: XML.Element;
  1332. BEGIN
  1333. IF ~initialized OR ~visible.Get() THEN RETURN END; (*? double call to visible.Get here and below. Which one is better ?*)
  1334. r := GetClientRect();
  1335. client := SELF;
  1336. parent := GetParent();
  1337. WHILE (parent # NIL) & (parent IS VisualComponent) DO
  1338. IF ~parent(VisualComponent).visible.Get() THEN RETURN END;
  1339. b := client.bounds.Get();
  1340. Rectangles.MoveRel(r, b.l, b.t);
  1341. client := parent(VisualComponent);
  1342. parent := client.GetParent();
  1343. END;
  1344. m.msgType := Messages.MsgInvalidate;
  1345. m.msgSubType := Messages.MsgSubRectangle;
  1346. m.x := r.l; m.y := r.t; m.dx := r.r; m.dy := r.b;
  1347. m.sender := client;
  1348. IF sequencer.Add(m) THEN IF CanYield THEN Objects.Yield END END;
  1349. END PostInvalidateCommand;
  1350. PROCEDURE InvalidateCommand*(sender, par : ANY);
  1351. VAR m: Messages.Message; r, b: Rectangles.Rectangle; client: VisualComponent; parent: XML.Element;
  1352. BEGIN
  1353. IF ~initialized OR ~visible.Get() THEN RETURN END; (*? double call to visible.Get here and below. Which one is better ?*)
  1354. IF ~IsCallFromSequencer() OR ~visible.Get() THEN
  1355. PostInvalidateCommand(sender, par);
  1356. ELSE
  1357. InvalidateRect(GetClientRect());
  1358. END;
  1359. END InvalidateCommand;
  1360. PROCEDURE Invalidate*; (* For convenience in component internal use *)
  1361. BEGIN
  1362. PostInvalidateCommand(SELF, NIL)
  1363. END Invalidate;
  1364. (** recursively disable the redrawing of any components in the hierarchy *)
  1365. (** dont forget to re-enable it ;-). Use with care to optimize sub-component operations *)
  1366. PROCEDURE DisableUpdate*;
  1367. VAR vc: VisualComponent;
  1368. BEGIN
  1369. ASSERT(IsCallFromSequencer());
  1370. vc := GetVisualComponentRoot();
  1371. IF (vc # NIL) & (vc IS Form) THEN vc(Form).DisableUpdate() END
  1372. END DisableUpdate;
  1373. (** recursively enable the redrawing of any components in the hierarchy *)
  1374. (** Only enable drawing if it was disabled before, but dont forget it, then ! *)
  1375. PROCEDURE EnableUpdate*;
  1376. VAR vc: VisualComponent;
  1377. BEGIN
  1378. ASSERT(IsCallFromSequencer());
  1379. vc := GetVisualComponentRoot();
  1380. IF (vc # NIL) & (vc IS Form) THEN vc(Form).EnableUpdate() END
  1381. END EnableUpdate;
  1382. PROCEDURE GetInternalPointerInfo*() : WM.PointerInfo;
  1383. VAR vc: VisualComponent;
  1384. BEGIN
  1385. ASSERT(IsCallFromSequencer());
  1386. vc := GetVisualComponentRoot();
  1387. IF (vc # NIL) & (vc IS Form) THEN
  1388. RETURN vc(Form).GetPointerInfo()
  1389. ELSE
  1390. RETURN NIL
  1391. END
  1392. END GetInternalPointerInfo;
  1393. PROCEDURE SetInternalPointerInfo*(pi : WM.PointerInfo);
  1394. VAR vc: VisualComponent;
  1395. BEGIN
  1396. AssertLock;
  1397. vc := GetVisualComponentRoot();
  1398. IF (vc # NIL) & (vc IS Form) THEN vc(Form).SetPointerInfo(pi) END
  1399. END SetInternalPointerInfo;
  1400. PROCEDURE SetPointerInfo*(pi : WM.PointerInfo);
  1401. BEGIN
  1402. Acquire;
  1403. SetInternalPointerInfo(pi);
  1404. pointerInfo := pi;
  1405. Release
  1406. END SetPointerInfo;
  1407. PROCEDURE GetPointerInfo*() : WM.PointerInfo;
  1408. BEGIN
  1409. RETURN pointerInfo
  1410. END GetPointerInfo;
  1411. (** User interaction messages *)
  1412. PROCEDURE SetExtPointerLeaveHandler*(handler : PointerLeaveHandler);
  1413. BEGIN
  1414. Acquire; extPointerLeave := handler; Release
  1415. END SetExtPointerLeaveHandler;
  1416. PROCEDURE SetExtPointerDownHandler*(handler : PointerHandler);
  1417. BEGIN
  1418. Acquire; extPointerDown := handler; Release
  1419. END SetExtPointerDownHandler;
  1420. PROCEDURE SetExtPointerMoveHandler*(handler : PointerHandler);
  1421. BEGIN
  1422. Acquire; extPointerMove := handler; Release
  1423. END SetExtPointerMoveHandler;
  1424. PROCEDURE SetExtPointerUpHandler*(handler : PointerHandler);
  1425. BEGIN
  1426. Acquire; extPointerUp := handler; Release
  1427. END SetExtPointerUpHandler;
  1428. PROCEDURE SetExtDragOverHandler*(handler : DragDropHandler);
  1429. BEGIN
  1430. Acquire; extDragOver := handler; Release
  1431. END SetExtDragOverHandler;
  1432. PROCEDURE SetExtDragDroppedHandler*(handler : DragDropHandler);
  1433. BEGIN
  1434. Acquire; extDragDropped := handler; Release
  1435. END SetExtDragDroppedHandler;
  1436. PROCEDURE SetExtDragResultHandler*(handler : DragResultHandler);
  1437. BEGIN
  1438. Acquire; extDragResult := handler; Release
  1439. END SetExtDragResultHandler;
  1440. PROCEDURE SetExtKeyEventHandler*(handler : KeyEventHandler);
  1441. BEGIN
  1442. Acquire; extKeyEvent := handler; Release
  1443. END SetExtKeyEventHandler;
  1444. PROCEDURE SetExtDrawHandler*(handler : DrawHandler);
  1445. BEGIN
  1446. Acquire; extDraw := handler; Release
  1447. END SetExtDrawHandler;
  1448. PROCEDURE SetExtFocusHandler*(handler : FocusHandler);
  1449. BEGIN
  1450. Acquire; extFocus := handler; Release
  1451. END SetExtFocusHandler;
  1452. PROCEDURE SetExtContextMenuHandler*(handler : ContextMenuHandler);
  1453. BEGIN
  1454. Acquire; extContextMenu := handler; Release
  1455. END SetExtContextMenuHandler;
  1456. PROCEDURE SetExtGetPositionOwnerHandler*(handler : GetPositionOwnerHandler);
  1457. BEGIN
  1458. Acquire; extGetPositionOwner := handler; Release;
  1459. END SetExtGetPositionOwnerHandler;
  1460. (** Indicates the pointing device has left the component without a key pressed down.
  1461. May only be called from the sequencer thread.
  1462. Components interested in this message can override this method instead of searching for the message in HandleInternal. *)
  1463. PROCEDURE PointerLeave*; (** PROTECTED *)
  1464. BEGIN ASSERT(IsCallFromSequencer());
  1465. END PointerLeave;
  1466. (** Indicates one of the pointer keys went down. keys is the set of buttons currently pressed. x, y is the position in component
  1467. coordinates.
  1468. May only be called from the sequencer thread.
  1469. Components interested in this message can override this method instead of searching for the message in HandleInternal. *)
  1470. PROCEDURE PointerDown*(x, y: LONGINT; keys: SET); (** PROTECTED *)
  1471. BEGIN ASSERT(IsCallFromSequencer());
  1472. IF keys = {2} THEN ShowContextMenu(x, y)
  1473. END;
  1474. END PointerDown;
  1475. (** Indicates the pointer was moved. keys is the set of buttons currently pressed. x, y is the position in component
  1476. coordinates.
  1477. May only be called from the sequencer thread.
  1478. Components interested in this message can override this method instead of searching for the message in HandleInternal.
  1479. When using PointerMove to move the component itself within a context (window or parent component),
  1480. remember that Component.PointerMove are given in component coordinates (thus, a moving coordinate origin ...), but you want to move the component in context coordinates !
  1481. *)
  1482. PROCEDURE PointerMove*(x, y: LONGINT; keys: SET); (** PROTECTED *)
  1483. BEGIN ASSERT(IsCallFromSequencer());
  1484. END PointerMove;
  1485. PROCEDURE WheelMove*(dz: LONGINT); (** PROTECTED *)
  1486. BEGIN ASSERT(IsCallFromSequencer());
  1487. END WheelMove;
  1488. (** Indicates one of the pointer keys went up. keys is the set of buttons currently pressed. x, y is the position in component
  1489. coordinates.
  1490. May only be called from the sequencer thread.
  1491. Components interested in this message can override this method instead of searching for the message in HandleInternal. *)
  1492. PROCEDURE PointerUp*(x, y: LONGINT; keys: SET); (** PROTECTED *)
  1493. BEGIN ASSERT(IsCallFromSequencer());
  1494. END PointerUp;
  1495. (** The component can determine wheter the key was pressed or released by examining the
  1496. Inputs.Release flag in flags. ucs contains the unicode equivalent of the key. Special input editors
  1497. send the generated unicode characters via KeyEvent.
  1498. May only be called from the sequencer thread.
  1499. Components interested in this message can override this method instead of searching for the message in HandleInternal. *)
  1500. PROCEDURE KeyEvent*(ucs : LONGINT; flags: SET; VAR keySym: LONGINT); (** PROTECTED *)
  1501. BEGIN ASSERT(IsCallFromSequencer());
  1502. END KeyEvent;
  1503. PROCEDURE EditKeyEvents(ucs : LONGINT; flags: SET; VAR keySym: LONGINT): BOOLEAN; (** FINAL *)
  1504. VAR event : KeyPressedEvent; command : Strings.String; scale: LONGINT;
  1505. clone: Repositories.Component; parent: XML.Content; parentEditMode: BOOLEAN;
  1506. enum: XMLObjects.Enumerator; obj: ANY;
  1507. BEGIN
  1508. ASSERT(IsCallFromSequencer());
  1509. event.ucs := ucs; event.flags := flags; event.keysym := keySym;
  1510. parent := GetParent();
  1511. IF (parent # NIL) & (parent IS VisualComponent) & parent(VisualComponent).editMode.Get() THEN
  1512. parentEditMode := TRUE
  1513. ELSE
  1514. parentEditMode := FALSE
  1515. END;
  1516. IF ({Inputs.Release} * flags = {}) THEN
  1517. IF (keySym = Inputs.KsF1) & (Inputs.Shift * flags # {}) THEN
  1518. SetEditMode(~editMode.Get(), FALSE);
  1519. RETURN TRUE
  1520. ELSIF (keySym = Inputs.KsEscape) THEN
  1521. selection.Reset(NIL);
  1522. RETURN FALSE
  1523. ELSIF parentEditMode OR editMode.Get() THEN
  1524. IF Inputs.Shift * flags # {} THEN scale := 1 ELSE scale := 4 END;
  1525. IF keySym = Inputs.KsLeft THEN selection.Shift(-scale,0); RETURN TRUE
  1526. ELSIF keySym = Inputs.KsRight THEN selection.Shift(scale,0); RETURN TRUE
  1527. ELSIF keySym = Inputs.KsDown THEN selection.Shift(0,scale); RETURN TRUE
  1528. ELSIF keySym = Inputs.KsUp THEN selection.Shift(0,-scale); RETURN TRUE
  1529. ELSIF keySym=4 (* CTRL-D *) THEN
  1530. clone := Clone(selection.first.component);
  1531. parent := selection.first.component.GetParent(); parent(Component).AddContent(clone);
  1532. RETURN TRUE
  1533. ELSIF keySym=1 THEN (* CTRL-A *)
  1534. enum := GetContents();
  1535. WHILE enum.HasMoreElements() DO
  1536. obj := enum.GetNext();
  1537. IF obj IS VisualComponent THEN
  1538. selection.Add(obj(VisualComponent))
  1539. END;
  1540. END;
  1541. ELSIF keySym = Inputs.KsDelete THEN
  1542. RemoveSelection();
  1543. RETURN TRUE
  1544. END;
  1545. END
  1546. END;
  1547. RETURN FALSE;
  1548. END EditKeyEvents;
  1549. PROCEDURE CheckKeyEvents(ucs : LONGINT; flags: SET; VAR keySym: LONGINT); (** FINAL *)
  1550. VAR event : KeyPressedEvent; command : Strings.String; scale: LONGINT; clone: Repositories.Component; parent: XML.Content;
  1551. BEGIN
  1552. ASSERT(IsCallFromSequencer());
  1553. event.ucs := ucs; event.flags := flags; event.keysym := keySym;
  1554. IF ({Inputs.Release} * flags = {}) THEN
  1555. IF (keySym = Inputs.KsReturn) THEN
  1556. command := GetAttributeValue("onReturn");
  1557. ELSIF (keySym = Inputs.KsEscape) THEN
  1558. command := GetAttributeValue("onEscape");
  1559. selection.Reset(NIL);
  1560. (*ELSIF (keySym = Inputs.KsF1) & (Inputs.Shift * flags # {}) THEN
  1561. SetEditMode(~editMode.Get(), TRUE);
  1562. ELSIF editMode.Get() THEN
  1563. IF Inputs.Shift * flags # {} THEN scale := 1 ELSE scale := 4 END;
  1564. IF keySym = Inputs.KsLeft THEN selection.Shift(-scale,0)
  1565. ELSIF keySym = Inputs.KsRight THEN selection.Shift(scale,0)
  1566. ELSIF keySym = Inputs.KsDown THEN selection.Shift(0,scale)
  1567. ELSIF keySym = Inputs.KsUp THEN selection.Shift(0,-scale)
  1568. ELSIF keySym=4 (* CTRL-D *) THEN
  1569. clone := Clone(selection.first.component);
  1570. parent := selection.first.component.GetParent(); parent(Component).AddContent(clone);
  1571. ELSIF keySym = Inputs.KsDelete THEN
  1572. RemoveSelection();
  1573. END;
  1574. *)
  1575. END;
  1576. IF (command # NIL) THEN HandleEvent(event, SELF, command); END;
  1577. command := GetAttributeValue("onKeyPressed");
  1578. IF (command # NIL) THEN HandleEvent(event, SELF, command); END;
  1579. ELSE
  1580. command := GetAttributeValue("onKeyReleased");
  1581. IF (command # NIL) THEN HandleEvent(event, SELF, command); END;
  1582. END;
  1583. END CheckKeyEvents;
  1584. PROCEDURE CheckPointerEvent(x, y, z : LONGINT; keys : SET);
  1585. VAR event : PointerEvent; command : Strings.String;
  1586. BEGIN
  1587. ASSERT(IsCallFromSequencer());
  1588. event.x := x; event.y := y; event.z := z; event.keys := keys;
  1589. IF ({0} * keys = {0}) THEN
  1590. command := GetAttributeValue("onLeftClick");
  1591. ELSIF ({2} * keys = {2}) THEN
  1592. command := GetAttributeValue("onRightClick");
  1593. ELSIF ({1} * keys = {1}) THEN
  1594. command := GetAttributeValue("onMiddleClick");
  1595. END;
  1596. IF (command # NIL) THEN HandleEvent(event, SELF, command); END;
  1597. command := GetAttributeValue("onClick");
  1598. IF (command # NIL) THEN HandleEvent(event, SELF, command); END;
  1599. END CheckPointerEvent;
  1600. PROCEDURE CheckPointerUpEvent(x, y, z : LONGINT; keys : SET);
  1601. VAR event : PointerEvent; command : Strings.String;
  1602. BEGIN
  1603. ASSERT(IsCallFromSequencer());
  1604. event.x := x; event.y := y; event.z := z; event.keys := keys;
  1605. command := GetAttributeValue("onRelease");
  1606. IF (command # NIL) THEN HandleEvent(event, SELF, command); END;
  1607. END CheckPointerUpEvent;
  1608. PROCEDURE InEditBounds(x,y: LONGINT): LONGINT;
  1609. CONST Border = 8;
  1610. VAR left, right, top, bottom: LONGINT;
  1611. BEGIN
  1612. left := bounds.GetLeft();
  1613. right := bounds.GetRight();
  1614. top := bounds.GetTop();
  1615. bottom := bounds.GetBottom();
  1616. INC(x,left); INC(y,top); (* relative -> absolute *)
  1617. IF (ABS(left-x) <= Border) THEN
  1618. IF (ABS(top-y) <= Border) THEN
  1619. RETURN UpperLeft
  1620. ELSIF (ABS(bottom-y) <= Border) THEN
  1621. RETURN LowerLeft
  1622. ELSE
  1623. RETURN Left
  1624. END
  1625. ELSIF (ABS(right-x) <= Border) THEN
  1626. IF (ABS(top-y) <= Border) THEN
  1627. RETURN UpperRight
  1628. ELSIF (ABS(bottom-y) <= Border) THEN
  1629. RETURN LowerRight
  1630. ELSE
  1631. RETURN Right
  1632. END
  1633. ELSIF (ABS(y-top) <= Border) THEN
  1634. RETURN Upper
  1635. ELSIF (ABS(bottom-y) <= Border) THEN
  1636. RETURN Lower
  1637. ELSIF (x > left+Border) & (x < right-Border) & (y > top+Border) & (y< bottom-Border) THEN
  1638. RETURN Inside
  1639. ELSE
  1640. RETURN None
  1641. END;
  1642. END InEditBounds;
  1643. PROCEDURE Edit(VAR msg: Messages.Message);
  1644. VAR region: LONGINT; dx,dy: LONGINT; b: Rectangles.Rectangle; manager: WM.WindowManager;
  1645. w,h: LONGINT; img: WMGraphics.Image; canvas: WMGraphics.BufferCanvas; e: ComponentListEntry;
  1646. alignRelative : BOOLEAN;
  1647. BEGIN
  1648. IF msg.msgSubType = Messages.MsgSubPointerUp THEN
  1649. editRegion := None;
  1650. SetPointerInfo(oldPointerInfo);
  1651. RETURN
  1652. END;
  1653. dx := msg.x-editX; dy := msg.y-editY;
  1654. b := bounds.Get();
  1655. IF editRegion = Right THEN
  1656. b.r := b.r + dx
  1657. ELSIF editRegion = Left THEN
  1658. b.l := b.l + dx; dx := 0;
  1659. ELSIF editRegion = Lower THEN
  1660. b.b := b.b + dy
  1661. ELSIF editRegion = Upper THEN
  1662. b.t := b.t + dy; dy := 0;
  1663. ELSIF editRegion = LowerLeft THEN
  1664. b.b := b.b + dy;
  1665. b.l := b.l + dx; dx := 0;
  1666. ELSIF editRegion = LowerRight THEN
  1667. b.b := b.b + dy;
  1668. b.r := b.r + dx
  1669. ELSIF editRegion = UpperLeft THEN
  1670. b.t := b.t + dy; dy := 0;
  1671. b.l := b.l + dx; dx := 0;
  1672. ELSIF editRegion = UpperRight THEN
  1673. b.t := b.t + dy; dy := 0;
  1674. b.r := b.r + dx
  1675. ELSIF (editRegion = Inside) & ((dx # 0) OR (dy # 0)) THEN
  1676. img := selection.ToImg(SELF,e);
  1677. IF e # NIL THEN
  1678. IF StartDrag(selection,img,-msg.x-e.dx,-msg.y-e.dy, EditMoved,NIL) THEN END;
  1679. END;
  1680. RETURN
  1681. END;
  1682. AdaptRelativeBounds(b, GetParent());
  1683. bounds.Set(b);
  1684. editX := editX + dx; editY := editY + dy;
  1685. END Edit;
  1686. PROCEDURE SetEditMode*(mode: BOOLEAN; recurse: BOOLEAN);
  1687. VAR vc: VisualComponent; c: XML.Content;
  1688. BEGIN
  1689. Acquire;
  1690. editMode.Set(mode);
  1691. IF recurse THEN
  1692. c := GetFirst();
  1693. WHILE (c # NIL) DO
  1694. IF c IS VisualComponent THEN
  1695. vc := c(VisualComponent);
  1696. vc.SetEditMode(mode, TRUE);
  1697. END;
  1698. c := GetNext(c);
  1699. END;
  1700. END;
  1701. Release;
  1702. END SetEditMode;
  1703. PROCEDURE EditMoved(sender, data: ANY);
  1704. VAR parent: XML.Element; ldata: ANY; e: ComponentListEntry;
  1705. BEGIN
  1706. IF (sender # SELF) THEN
  1707. IF (data # NIL) & (data IS WM.DragInfo) THEN
  1708. ldata := data(WM.DragInfo).data;
  1709. IF (ldata # NIL) & (ldata IS XML.Element) THEN
  1710. parent := ldata(XML.Element).GetParent();
  1711. parent.RemoveContent(ldata(XML.Element));
  1712. parent(VisualComponent).Invalidate;
  1713. ELSIF (ldata # NIL) & (ldata IS SelectionList) THEN
  1714. e := ldata(SelectionList).first;
  1715. WHILE e # NIL DO
  1716. parent := e.component.GetParent();
  1717. ldata := e.component;
  1718. parent.RemoveContent(ldata(XML.Element));
  1719. parent(VisualComponent).Invalidate;
  1720. e := e.next;
  1721. END;
  1722. END;
  1723. END;
  1724. END;
  1725. END EditMoved;
  1726. PROCEDURE HandleInternal*(VAR msg : Messages.Message); (** PROTECTED *)
  1727. VAR
  1728. po : VisualComponent; nm : Messages.Message; handled : BOOLEAN; b : Rectangles.Rectangle;
  1729. r, v : VisualComponent;
  1730. p : XML.Element;
  1731. keyFlags: SET; manager : WM.WindowManager;
  1732. currentEditRegion: LONGINT;
  1733. parent: XML.Element;
  1734. parentEditMode: BOOLEAN;
  1735. BEGIN
  1736. ASSERT(IsCallFromSequencer());
  1737. handled := FALSE;
  1738. IF msg.msgType = Messages.MsgPointer THEN
  1739. parent := GetParent();
  1740. IF (parent # NIL) & (parent IS VisualComponent) & parent(VisualComponent).editMode.Get() THEN
  1741. parentEditMode := TRUE
  1742. ELSE
  1743. parentEditMode := FALSE
  1744. END;
  1745. IF msg.msgSubType = Messages.MsgSubPointerMove THEN
  1746. IF (msg.flags * {0, 1, 2} = {}) OR (fPointerOwner = NIL) THEN
  1747. IF parentEditMode & ~editMode.Get() THEN fPointerOwner := SELF; handled := TRUE
  1748. ELSIF ~parentEditMode & (extGetPositionOwner # NIL) THEN extGetPositionOwner(msg.x, msg.y, fPointerOwner, handled);
  1749. END;
  1750. IF ~handled THEN
  1751. po := GetPositionOwner(msg.x, msg.y);
  1752. IF po # fPointerOwner THEN
  1753. nm.msgType := Messages.MsgPointer;
  1754. nm.msgSubType := Messages.MsgSubPointerLeave;
  1755. HandleInternal(nm)
  1756. END;
  1757. fPointerOwner := po
  1758. ELSE
  1759. handled := FALSE;
  1760. END;
  1761. END
  1762. END;
  1763. IF (fPointerOwner = SELF) THEN
  1764. IF (msg.originator # NIL) & (msg.originator IS WM.ViewPort) THEN
  1765. manager := msg.originator(WM.ViewPort).manager;
  1766. msg.originator(WM.ViewPort).GetKeyState(keyFlags);
  1767. END;
  1768. IF parentEditMode & (editRegion # None) THEN
  1769. Edit(msg)
  1770. ELSE
  1771. IF msg.msgSubType = Messages.MsgSubPointerMove THEN
  1772. IF (parentEditMode) & (msg.originator # NIL) & (msg.originator IS WM.ViewPort) THEN
  1773. currentEditRegion := InEditBounds(msg.x, msg.y);
  1774. CASE currentEditRegion OF
  1775. | Lower, Upper: SetPointerInfo(manager.pointerUpDown)
  1776. | Left, Right:SetPointerInfo(manager.pointerLeftRight)
  1777. | LowerLeft, UpperRight:SetPointerInfo(manager.pointerURDL)
  1778. | UpperLeft, LowerRight: SetPointerInfo(manager.pointerULDR)
  1779. | Inside: SetPointerInfo(manager.pointerMove)
  1780. ELSE
  1781. IF oldPointerInfo # NIL THEN
  1782. SetPointerInfo(oldPointerInfo); oldPointerInfo := NIL;
  1783. ELSE oldPointerInfo := GetPointerInfo();
  1784. END;
  1785. END;
  1786. END;
  1787. IF extPointerMove # NIL THEN extPointerMove(msg.x, msg.y, msg.flags, handled) END;
  1788. SetInternalPointerInfo(pointerInfo);
  1789. IF ~handled THEN PointerMove(msg.x, msg.y, msg.flags) END;
  1790. IF msg.dz # 0 THEN WheelMove(msg.dz) END
  1791. ELSIF msg.msgSubType = Messages.MsgSubPointerDown THEN
  1792. IF parentEditMode THEN
  1793. editRegion := InEditBounds(msg.x, msg.y);
  1794. END;
  1795. (*
  1796. IF (msg.originator # NIL) & (msg.originator IS WM.ViewPort) THEN
  1797. msg.originator(WM.ViewPort).GetKeyState(keyFlags);
  1798. IF (keyFlags # {}) & (keyFlags <= Inputs.Ctrl) THEN editRegion := InEditBounds(msg.x, msg.y) ELSE editRegion := None END;
  1799. ELSE
  1800. editRegion := None
  1801. END;
  1802. *)
  1803. IF editRegion # None THEN
  1804. IF (keyFlags # {}) & (keyFlags <= Inputs.Shift) THEN
  1805. selection.Toggle(SELF)
  1806. ELSIF ~selection.Has(SELF) THEN selection.Reset(SELF)
  1807. END;
  1808. manager := msg.originator(WM.ViewPort).manager;
  1809. editX := msg.x; editY := msg.y;
  1810. ELSE
  1811. IF extPointerDown # NIL THEN extPointerDown(msg.x, msg.y, msg.flags, handled) END;
  1812. IF ~handled THEN PointerDown(msg.x, msg.y, msg.flags) END;
  1813. END;
  1814. SetFocus
  1815. ELSIF msg.msgSubType = Messages.MsgSubPointerUp THEN
  1816. IF extPointerUp # NIL THEN extPointerUp(msg.x, msg.y, msg.flags, handled) END;
  1817. IF ~handled THEN PointerUp(msg.x, msg.y, msg.flags) END
  1818. ELSIF msg.msgSubType = Messages.MsgSubPointerLeave THEN
  1819. IF extPointerLeave # NIL THEN extPointerLeave(handled) END;
  1820. IF ~handled THEN PointerLeave END
  1821. END;
  1822. IF ~parentEditMode & (msg.flags * {0, 1, 2} # {}) THEN
  1823. IF (msg.msgSubType = Messages.MsgSubPointerDown) THEN
  1824. CheckPointerEvent(msg.x, msg.y, msg.z, msg.flags);
  1825. ELSIF msg.msgSubType = Messages.MsgSubPointerUp THEN
  1826. CheckPointerUpEvent(msg.x, msg.y, msg.z, msg.flags);
  1827. END;
  1828. END;
  1829. END;
  1830. ELSE
  1831. b := fPointerOwner.bounds.Get();
  1832. msg.x := msg.x - b.l; msg.y := msg.y - b.t;
  1833. fPointerOwner.Handle(msg)
  1834. END
  1835. ELSIF msg.msgType = Messages.MsgKey THEN
  1836. IF focusComponent # SELF THEN focusComponent.Handle(msg)
  1837. ELSIF EditKeyEvents(msg.x, msg.flags, msg.y) THEN
  1838. handled := TRUE;
  1839. ELSIF (visible.Get()) THEN
  1840. IF ~needsTab.Get() & (msg.y = 0FF09H) THEN
  1841. IF (Inputs.Shift * msg.flags # {}) THEN FocusPrev ELSE FocusNext END
  1842. ELSIF msg.y = 0FF67H THEN ShowContextMenu(0, 0)
  1843. ELSE
  1844. IF extKeyEvent # NIL THEN extKeyEvent(msg.x, msg.flags, msg.y, handled) END;
  1845. IF ~handled THEN KeyEvent(msg.x, msg.flags, msg.y) END;
  1846. CheckKeyEvents(msg.x, msg.flags, msg.y);
  1847. END
  1848. END;
  1849. ELSIF msg.msgType = Messages.MsgDrag THEN
  1850. IF extGetPositionOwner # NIL THEN extGetPositionOwner(msg.x, msg.y, po, handled); END;
  1851. IF ~handled THEN
  1852. po := GetPositionOwner(msg.x, msg.y);
  1853. ELSE
  1854. handled := FALSE;
  1855. END;
  1856. IF (po # SELF) & editMode.Get() & (~po.editMode.Get() OR (msg.ext # NIL) & (msg.ext(WM.DragInfo).data=po)) THEN
  1857. po := SELF
  1858. ELSIF (msg.ext # NIL) & (msg.ext(WM.DragInfo).data # NIL) & (msg.ext(WM.DragInfo).data IS ToggleEditMode) & (msg.ext(WM.DragInfo).data(ToggleEditMode).recursion = Recursion.FromBottom) THEN
  1859. po := SELF
  1860. END;
  1861. IF (po # SELF) THEN (* Let child handle the drag and drop message *)
  1862. b := po.bounds.Get();
  1863. msg.x := msg.x - b.l; msg.y := msg.y - b.t;
  1864. po.Handle(msg)
  1865. ELSE (* handle the drag and drop message *)
  1866. IF msg.msgSubType = Messages.MsgDragOver THEN
  1867. IF (msg.ext # NIL) THEN
  1868. IF extDragOver # NIL THEN extDragOver(msg.x, msg.y, msg.ext(WM.DragInfo), handled) END;
  1869. IF ~handled THEN po.DragOver(msg.x, msg.y, msg.ext(WM.DragInfo)) END
  1870. END
  1871. ELSIF msg.msgSubType = Messages.MsgDragDropped THEN
  1872. IF (msg.ext # NIL) THEN
  1873. IF (msg.ext(WM.DragInfo).data # NIL) & (msg.ext(WM.DragInfo).data IS FindComponentMode) THEN
  1874. IF msg.ext(WM.DragInfo).onAccept # NIL THEN
  1875. msg.ext(WM.DragInfo).onAccept(po, msg.ext(WM.DragInfo));
  1876. END;
  1877. ELSIF (msg.ext(WM.DragInfo).data # NIL) & (msg.ext(WM.DragInfo).data IS ToggleEditMode) THEN
  1878. SetEditMode(~editMode.Get(), msg.ext(WM.DragInfo).data(ToggleEditMode).recursion # Recursion.None);
  1879. Invalidate;
  1880. ELSIF editMode.Get() THEN
  1881. handled := EditDragDropped(msg.x,msg.y,msg.ext(WM.DragInfo));
  1882. ELSIF extDragDropped # NIL THEN
  1883. extDragDropped(msg.x, msg.y, msg.ext(WM.DragInfo), handled)
  1884. END;
  1885. IF ~handled THEN
  1886. po.DragDropped(msg.x, msg.y, msg.ext(WM.DragInfo))
  1887. END
  1888. END
  1889. END
  1890. END
  1891. ELSIF (msg.msgType = Messages.MsgFocus) & (msg.msgSubType = Messages.MsgSubFocusLost) THEN
  1892. (* unset the old focus chain *)
  1893. r := GetVisualComponentRoot(); (* find the leaf component that has the focus *)
  1894. WHILE (r # NIL) & (r.focusComponent # NIL) & (r.focusComponent # r) DO r := r.focusComponent END;
  1895. p := r; (* clear the focus chain until the root or this component *)
  1896. WHILE (p # SELF) & (p # NIL) & (p IS VisualComponent) DO
  1897. v := p(VisualComponent);
  1898. v.focusComponent := v;
  1899. v.FocusLost; IF v.extFocus # NIL THEN v.extFocus(FALSE) END; p := p.GetParent()
  1900. END;
  1901. ELSIF msg.msgType = Messages.MsgInvalidate THEN
  1902. IF msg.msgSubType = Messages.MsgSubAll THEN
  1903. msg.sender(VisualComponent).InvalidateRect(GetClientRect());
  1904. ELSIF msg.msgSubType = Messages.MsgSubRectangle THEN
  1905. msg.sender(VisualComponent).InvalidateRect(Rectangles.MakeRect(msg.x, msg.y, msg.dx, msg.dy));
  1906. ELSE (* nothing to do *)
  1907. END;
  1908. ELSIF msg.msgType = Messages.MsgExt THEN
  1909. IF msg.ext = invalidateRectMsg THEN
  1910. TRACE("WARNING: OLD MESSAGE FORM");
  1911. msg.sender(VisualComponent).InvalidateRect(Rectangles.MakeRect(msg.x, msg.y, msg.dx, msg.dy))
  1912. ELSE
  1913. BroadcastSubcomponents(msg);
  1914. END
  1915. ELSE HandleInternal^(msg)
  1916. END;
  1917. END HandleInternal;
  1918. END VisualComponent;
  1919. GetPositionOwnerHandler* = PROCEDURE {DELEGATE} (x, y : LONGINT; VAR positionOwner : VisualComponent; VAR handled : BOOLEAN);
  1920. TYPE
  1921. (* Layout Manager *)
  1922. LayoutManager* = PROCEDURE {DELEGATE} (vc : VisualComponent);
  1923. FormWindow* = OBJECT(WM.DoubleBufferWindow)
  1924. VAR
  1925. form- : Form;
  1926. cs : WMGraphics.CanvasState;
  1927. disableUpdate : LONGINT;
  1928. content : VisualComponent;
  1929. scaling* : BOOLEAN;
  1930. PROCEDURE ToXML*():XML.Content;
  1931. VAR winx: XML.Element; a: XML.Attribute; string: ARRAY 128 OF CHAR; title:Strings.String;
  1932. BEGIN {EXCLUSIVE}
  1933. NEW(winx); winx.SetName("FormWindow");
  1934. NEW(a); a.SetName("name");
  1935. title:=GetTitle(); IF title=NIL THEN a.SetValue("componentWindow") ELSE a.SetValue(title^) END;
  1936. winx.AddAttribute(a);
  1937. NEW(a); a.SetName("loader"); a.SetValue("WMComponents.FormWindowGen"); winx.AddAttribute(a);
  1938. NEW(a); a.SetName("l"); Strings.IntToStr(bounds.l, string); a.SetValue(string); winx.AddAttribute(a);
  1939. NEW(a); a.SetName("t"); Strings.IntToStr(bounds.t, string); a.SetValue(string); winx.AddAttribute(a);
  1940. NEW(a); a.SetName("r"); Strings.IntToStr(bounds.r, string); a.SetValue(string); winx.AddAttribute(a);
  1941. NEW(a); a.SetName("b"); Strings.IntToStr(bounds.b, string); a.SetValue(string); winx.AddAttribute(a);
  1942. NEW(a); a.SetName("flags"); Strings.SetToStr(flags, string); a.SetValue(string); winx.AddAttribute(a);
  1943. NEW(a); a.SetName("canvasGenerator"); a.SetValue(canvas.generator^); winx.AddAttribute(a);
  1944. winx.AddContent(form);
  1945. RETURN winx
  1946. END ToXML;
  1947. PROCEDURE LoadComponents*(xml: XML.Element);
  1948. VAR component: Repositories.Component;
  1949. BEGIN
  1950. IF xml # NIL THEN
  1951. component := Repositories.ComponentFromXML(xml);
  1952. IF (component # NIL) & (component IS VisualComponent) THEN
  1953. SetContent(component);
  1954. ELSE
  1955. KernelLog.String("formwindow could not load content"); KernelLog.Ln;
  1956. END;
  1957. END;
  1958. END LoadComponents;
  1959. PROCEDURE StoreComponents*(): XML.Element;
  1960. BEGIN RETURN content (* do not store form separately *)
  1961. END StoreComponents;
  1962. PROCEDURE SetContent*(x : XML.Content);
  1963. VAR c: XML.Content;
  1964. m:Messages.Message;
  1965. BEGIN
  1966. IF sequencer # NIL THEN sequencer.WaitFree() END;
  1967. BEGIN{EXCLUSIVE}
  1968. INC(disableUpdate);
  1969. INCL(flags, 13); (* render windows background non-displayed*)
  1970. IF form # NIL THEN form.Finalize; form.sequencer.Stop; content:=NIL END;
  1971. IF x IS Form THEN
  1972. form := x(Form);
  1973. form.initialized:=FALSE;
  1974. form.SetWindow(SELF); (* includes new sequencer *)
  1975. c:=form.GetFirst(); (* get first VisualComponent content of form*)
  1976. WHILE (c#NIL) & (c IS XML.Container) & ~(c IS VisualComponent) DO
  1977. c:=c(XML.Container).GetNext(c);
  1978. END;
  1979. IF c#NIL THEN form.RemoveContent(c) END; (* avoid duplicates. will be added in a systematic way below in AddContent *)
  1980. ELSE
  1981. NEW(form, SELF); (* includes new sequencer; initialized=FALSE *)
  1982. form.uid.Set(NewString("form"));
  1983. c:=x;
  1984. END;
  1985. IF (c#NIL) & (c IS VisualComponent) THEN
  1986. content := c(VisualComponent);
  1987. form.initialized:=TRUE;
  1988. form.AddContent(content);
  1989. form.focusComponent := content;
  1990. form.fPointerOwner := content;
  1991. END;
  1992. DEC(disableUpdate);
  1993. END;
  1994. (*form.Initialize;*)(*implied above*)
  1995. (*form.Invalidate;*)(*implied above*)
  1996. END SetContent;
  1997. PROCEDURE DisableUpdate*;
  1998. BEGIN {EXCLUSIVE}
  1999. INC(disableUpdate);
  2000. ASSERT(disableUpdate # -1); (* overflow *)
  2001. END DisableUpdate;
  2002. PROCEDURE EnableUpdate*;
  2003. BEGIN {EXCLUSIVE}
  2004. DEC(disableUpdate);
  2005. ASSERT(disableUpdate # -1); (* underflow *)
  2006. END EnableUpdate;
  2007. PROCEDURE Resized( width, height: LONGINT);
  2008. BEGIN
  2009. IF ~scaling THEN
  2010. DisableUpdate;
  2011. form.Acquire;
  2012. ReInit(width, height);
  2013. form.Release;
  2014. form.bounds.Set(Rectangles.MakeRect(0, 0, GetWidth(), GetHeight()));
  2015. content.bounds.Set(Rectangles.MakeRect(0, 0, GetWidth(), GetHeight()));
  2016. EnableUpdate;
  2017. form.Invalidate()
  2018. END
  2019. END Resized;
  2020. PROCEDURE Trap():BOOLEAN;
  2021. BEGIN
  2022. KernelLog.String("WMComponents.FormWindow.Trap !!! --> Resetting Locks "); KernelLog.Ln;
  2023. form.sequencer.lock.Reset;
  2024. RETURN TRUE
  2025. END Trap;
  2026. PROCEDURE Update(rect : Rectangles.Rectangle);
  2027. BEGIN
  2028. (*KernelLog.String("Update "); KernelLog.Int(disableUpdate,0); KernelLog.Ln;*)
  2029. IF disableUpdate > 0 THEN RETURN END;
  2030. form.Acquire;
  2031. canvas.SaveState(cs);
  2032. canvas.SetClipRect(rect);
  2033. canvas.ClipRectAsNewLimits(0, 0);
  2034. IF Raster.alpha IN img.fmt.components THEN
  2035. canvas.Fill(rect, 0H, WMGraphics.ModeCopy)
  2036. ELSE
  2037. canvas.Fill(rect, 0H (*0FFH*), Raster.clear(*WMGraphics.ModeCopy*))
  2038. END;
  2039. form.Draw(canvas);
  2040. canvas.RestoreState(cs);
  2041. form.Release;
  2042. CopyRect(rect);
  2043. Invalidate(rect)
  2044. END Update;
  2045. PROCEDURE Handle*(VAR m : Messages.Message);
  2046. VAR pendingM: Messages.Message;
  2047. BEGIN
  2048. Handle^(m);
  2049. IF (m.msgType = Messages.MsgExt) & (m.ext # NIL) THEN
  2050. IF (m.ext = componentStyleMsg) THEN CSChanged
  2051. END;
  2052. ELSIF (m.msgType = Messages.MsgFocus) & (m.msgSubType = Messages.MsgSubFocusGot) THEN
  2053. IF (form # NIL) & (form.lastFocusComponent # NIL) THEN
  2054. form.lastFocusComponent.SetFocus;
  2055. END;
  2056. ELSIF (m.msgType = Messages.MsgSetLanguage) & (m.ext # NIL) & (m.ext IS LanguageExtension) THEN
  2057. LanguageChanged(m.ext(LanguageExtension).languages);
  2058. ELSIF (m.msgType=Messages.MsgInvalidate) THEN (* sent by WindowManager when a window is added to display space to assure it is up-to-date*)
  2059. IF form=NIL THEN RETURN
  2060. ELSE m.sender:=form; (* will be passed to form below, which will call sender.InvalidateRect *)
  2061. END;
  2062. END;
  2063. IF (TraceFocus IN Trace) THEN
  2064. IF (m.msgType = Messages.MsgFocus) THEN
  2065. IF (m.msgSubType = Messages.MsgSubFocusGot) THEN
  2066. KernelLog.String("Got Focus: "); form.TraceFocusChain;
  2067. ELSIF (m.msgSubType = Messages.MsgSubMasterFocusGot) THEN
  2068. KernelLog.String("Got Master Focus: "); form.TraceFocusChain;
  2069. END;
  2070. ELSIF (m.msgType = Messages.MsgKey) & (m.x = ORD("f")) THEN
  2071. KernelLog.String("Focus chain: "); form.TraceFocusChain;
  2072. END;
  2073. END;
  2074. IF (form # NIL) THEN form.Handle(m); END;
  2075. END Handle;
  2076. PROCEDURE LanguageChanged*(languages : Localization.Languages);
  2077. BEGIN
  2078. ASSERT(languages # NIL);
  2079. END LanguageChanged;
  2080. PROCEDURE CSChanged*;
  2081. BEGIN
  2082. DisableUpdate; (* the components are going to redraw like crazy *)
  2083. form.Acquire;
  2084. form.Reset(SELF, NIL);
  2085. form.Release;
  2086. EnableUpdate;
  2087. END CSChanged;
  2088. PROCEDURE Close*;
  2089. BEGIN
  2090. Close^; (* remove the form to avoid further messages *)
  2091. IF form # NIL THEN
  2092. form.Acquire;
  2093. form.Finalize; form.sequencer.Stop;
  2094. form.Release
  2095. END;
  2096. END Close;
  2097. END FormWindow;
  2098. Form* = OBJECT(VisualComponent)
  2099. VAR
  2100. window- : FormWindow;
  2101. lastFocusComponent : VisualComponent;
  2102. PROCEDURE &New*(window : FormWindow);
  2103. BEGIN
  2104. Init;
  2105. SetGenerator("WMComponents.NewForm");
  2106. lastFocusComponent := NIL;
  2107. SetNameAsString(StrForm);
  2108. SetWindow(window);
  2109. END New;
  2110. PROCEDURE SetWindow*(window: FormWindow);
  2111. VAR seq: Messages.MsgSequencer;
  2112. BEGIN {EXCLUSIVE}
  2113. IF window # NIL THEN
  2114. SELF.window := window;
  2115. window.form := SELF;
  2116. bounds.Set(Rectangles.MakeRect(0, 0, window.GetWidth(), window.GetHeight()));
  2117. NEW(seq, Handle); seq.SetTrapHandler(window.Trap); SetSequencer(seq);
  2118. END;
  2119. END SetWindow;
  2120. PROCEDURE GetPointerInfo*() : WM.PointerInfo;
  2121. BEGIN
  2122. ASSERT(IsCallFromSequencer());
  2123. IF window # NIL THEN RETURN window.pointerInfo ELSE RETURN NIL END
  2124. END GetPointerInfo;
  2125. PROCEDURE SetPointerInfo*(pi : WM.PointerInfo);
  2126. BEGIN
  2127. ASSERT(IsCallFromSequencer());
  2128. IF window # NIL THEN window.SetPointerInfo(pi) END;
  2129. END SetPointerInfo;
  2130. PROCEDURE DisableUpdate*;
  2131. BEGIN
  2132. ASSERT(IsCallFromSequencer());
  2133. IF window # NIL THEN window.DisableUpdate END
  2134. END DisableUpdate;
  2135. PROCEDURE EnableUpdate*;
  2136. BEGIN
  2137. ASSERT(IsCallFromSequencer());
  2138. IF window # NIL THEN window.EnableUpdate END
  2139. END EnableUpdate;
  2140. PROCEDURE InvalidateRect*(rect : Rectangles.Rectangle);
  2141. BEGIN
  2142. IF window # NIL THEN
  2143. BEGIN{EXCLUSIVE} AWAIT(initialized) END;
  2144. window.Update(rect)
  2145. END;
  2146. END InvalidateRect;
  2147. PROCEDURE PropertyChanged*(sender, property : ANY);
  2148. VAR w,h: LONGINT;
  2149. BEGIN
  2150. IF property = bounds THEN
  2151. IF ~ Rectangles.IsEqual(window.bounds, bounds.Get()) THEN
  2152. bounds.GetExtents(w,h);
  2153. IF window # NIL THEN
  2154. window.manager.SetWindowSize(window,w,h);
  2155. END;
  2156. ELSE
  2157. (*ScaleFont(bounds.GetHeight(), scaleFont.Get());*)
  2158. Resized
  2159. END;
  2160. END
  2161. END PropertyChanged;
  2162. END Form;
  2163. TYPE
  2164. (** PropertyLists for style support *)
  2165. PropertyListEntry = POINTER TO RECORD
  2166. next : PropertyListEntry;
  2167. name : Strings.String;
  2168. list : WMProperties.PropertyList;
  2169. END;
  2170. ListArray* = POINTER TO ARRAY OF WMProperties.PropertyList;
  2171. PropertyListList* = OBJECT
  2172. VAR
  2173. first : PropertyListEntry;
  2174. PROCEDURE Find*(CONST name : ARRAY OF CHAR) : WMProperties.PropertyList;
  2175. VAR cur : PropertyListEntry;
  2176. BEGIN {EXCLUSIVE}
  2177. cur := first;
  2178. WHILE (cur # NIL) & (cur.name^ # name) DO cur := cur.next END;
  2179. IF cur # NIL THEN RETURN cur.list
  2180. ELSE RETURN NIL
  2181. END
  2182. END Find;
  2183. PROCEDURE RemoveInternal(CONST name : ARRAY OF CHAR);
  2184. VAR cur : PropertyListEntry;
  2185. BEGIN
  2186. IF first = NIL THEN RETURN END;
  2187. IF (first # NIL) & (first.name^ = name) THEN first := first.next
  2188. ELSE
  2189. cur := first;
  2190. WHILE (cur.next # NIL) DO
  2191. IF (cur.next.name^ = name) THEN cur.next := cur.next.next END;
  2192. cur := cur.next
  2193. END
  2194. END
  2195. END RemoveInternal;
  2196. PROCEDURE Remove*(CONST name : ARRAY OF CHAR);
  2197. BEGIN {EXCLUSIVE}
  2198. RemoveInternal(name)
  2199. END Remove;
  2200. PROCEDURE Add*(CONST name : ARRAY OF CHAR; pl : WMProperties.PropertyList);
  2201. VAR new : PropertyListEntry;
  2202. BEGIN {EXCLUSIVE}
  2203. RemoveInternal(name);
  2204. NEW(new); new.name := NewString(name); new.list := pl; new.next := first; first := new
  2205. END Add;
  2206. PROCEDURE Enumerate*() : ListArray;
  2207. VAR array : ListArray; current : PropertyListEntry; i : LONGINT;
  2208. BEGIN {EXCLUSIVE}
  2209. i := 0;
  2210. current := first;
  2211. WHILE current # NIL DO INC(i); current := current.next END;
  2212. NEW(array, i );
  2213. current := first;
  2214. i := 0;
  2215. WHILE current # NIL DO
  2216. array[i] := current.list;
  2217. INC(i);
  2218. current := current.next
  2219. END;
  2220. RETURN array
  2221. END Enumerate;
  2222. PROCEDURE UpdateStyle*;
  2223. VAR
  2224. en : XMLObjects.Enumerator;
  2225. p : ANY; s : Strings.String;
  2226. pl : WMProperties.PropertyList;
  2227. BEGIN
  2228. IF currentStyle = NIL THEN RETURN END;
  2229. en := currentStyle.GetContents();
  2230. WHILE en.HasMoreElements() DO
  2231. p := en.GetNext();
  2232. IF p IS XML.Element THEN
  2233. s := p(XML.Element).GetName();
  2234. pl := propertyListList.Find(s^);
  2235. IF pl # NIL THEN pl.SetXML(p(XML.Element)) END
  2236. END
  2237. END
  2238. END UpdateStyle;
  2239. END PropertyListList;
  2240. ComponentListEntry= POINTER TO RECORD
  2241. component: VisualComponent;
  2242. dx,dy: LONGINT;
  2243. next: ComponentListEntry
  2244. END;
  2245. SelectionArray* = POINTER TO ARRAY OF VisualComponent;
  2246. SelectionList*= OBJECT
  2247. VAR first, last: ComponentListEntry; number: LONGINT; state: LONGINT; timer: Kernel.Timer;
  2248. onChanged-: WMEvents.EventSource;
  2249. lock: Locks.RecursiveLock;
  2250. PROCEDURE &Init;
  2251. BEGIN
  2252. NEW(lock);
  2253. first := NIL; last := NIL; number := 0; state := 0; NEW(onChanged, NIL, NIL, NIL, NIL);
  2254. END Init;
  2255. PROCEDURE Reset(this: VisualComponent);
  2256. VAR entry: ComponentListEntry;
  2257. BEGIN
  2258. lock.Acquire;
  2259. entry := first;
  2260. first := NIL; last := NIL; number := 0;
  2261. WHILE entry # NIL DO entry.component.Invalidate; entry := entry.next END;
  2262. lock.Release;
  2263. Add(this);
  2264. onChanged.Call(SELF);
  2265. END Reset;
  2266. PROCEDURE Has*(this: ANY): BOOLEAN;
  2267. VAR entry: ComponentListEntry;
  2268. BEGIN
  2269. IF first = NIL THEN RETURN FALSE END; (* no lock for usual case *)
  2270. lock.Acquire;
  2271. entry := first;
  2272. WHILE (entry # NIL) & (entry.component # this) DO entry := entry.next END;
  2273. lock.Release;
  2274. RETURN entry # NIL
  2275. END Has;
  2276. PROCEDURE Add*(this: VisualComponent);
  2277. VAR entry: ComponentListEntry;
  2278. BEGIN
  2279. IF (this = NIL) OR Has(this) THEN RETURN END;
  2280. lock.Acquire;
  2281. NEW(entry); entry.component := this; entry.next := NIL;
  2282. IF last = NIL THEN
  2283. ASSERT(first = NIL);
  2284. first := entry; last := entry;
  2285. ELSE
  2286. last.next := entry; last := entry
  2287. END;
  2288. INC(number);
  2289. lock.Release;
  2290. this.Invalidate;
  2291. onChanged.Call(SELF);
  2292. END Add;
  2293. PROCEDURE Remove*(this: VisualComponent);
  2294. VAR entry, prev: ComponentListEntry;
  2295. BEGIN
  2296. lock.Acquire;
  2297. entry := first; prev := NIL;
  2298. WHILE (entry # NIL) & (entry.component # this) DO
  2299. prev := entry;
  2300. entry := entry.next;
  2301. END;
  2302. IF entry = NIL THEN lock.Release; RETURN END;
  2303. IF prev # NIL THEN prev.next := entry.next END;
  2304. IF entry = first THEN first := first.next END;
  2305. IF entry = last THEN last := prev END;
  2306. DEC(number);
  2307. lock.Release;
  2308. this.Invalidate;
  2309. onChanged.Call(SELF);
  2310. END Remove;
  2311. PROCEDURE GetSelection*(): SelectionArray;
  2312. VAR array: SelectionArray; i: LONGINT; e: ComponentListEntry;
  2313. BEGIN
  2314. lock.Acquire;
  2315. NEW(array, number);
  2316. e := first; i := 0;
  2317. WHILE e # NIL DO
  2318. array[i] := e.component;
  2319. INC(i);
  2320. e := e.next;
  2321. END;
  2322. lock.Release;
  2323. RETURN array;
  2324. END GetSelection;
  2325. PROCEDURE Toggle*(this: VisualComponent);
  2326. BEGIN
  2327. IF Has(this) THEN Remove(this) ELSE Add(this) END;
  2328. END Toggle;
  2329. PROCEDURE Update;
  2330. VAR e: ComponentListEntry;
  2331. BEGIN
  2332. e := first;
  2333. WHILE e # NIL DO
  2334. e.component.Invalidate;
  2335. e := e.next;
  2336. END;
  2337. END Update;
  2338. PROCEDURE Shift(dx, dy: LONGINT);
  2339. VAR e: ComponentListEntry; rect: Rectangles.Rectangle;
  2340. BEGIN
  2341. e := first;
  2342. WHILE e # NIL DO
  2343. rect := e.component.bounds.Get();
  2344. INC(rect.l,dx); INC(rect.r,dx);
  2345. INC(rect.t,dy); INC(rect.b,dy);
  2346. e.component.AdaptRelativeBounds(rect,e.component.GetParent());
  2347. e.component.bounds.Set(rect);
  2348. e := e.next
  2349. END;
  2350. END Shift;
  2351. PROCEDURE ToImg(start: VisualComponent; VAR this: ComponentListEntry): WMGraphics.Image;
  2352. VAR l,t,r,b: LONGINT; e: ComponentListEntry; rect: Rectangles.Rectangle; img, image: WMGraphics.Image; w,h: LONGINT;
  2353. canvas: WMGraphics.BufferCanvas; srcCopy: Raster.Mode;
  2354. BEGIN
  2355. l := MAX(LONGINT); r := MIN(LONGINT);
  2356. t := MAX(LONGINT); b := MIN(LONGINT);
  2357. e := first;
  2358. WHILE e # NIL DO
  2359. rect := e.component(VisualComponent).bounds.Get();
  2360. IF rect.l < l THEN l := rect.l END;
  2361. IF rect.r > r THEN r := rect.r END;
  2362. IF rect.t < t THEN t := rect.t END;
  2363. IF rect.b > b THEN b := rect.b END;
  2364. e := e.next;
  2365. END;
  2366. Raster.InitMode(srcCopy, Raster.srcCopy);
  2367. NEW(image);
  2368. w := r-l+1; h := b-t+1;
  2369. Raster.Create(image, w,h, Raster.BGRA8888);
  2370. e := first;
  2371. WHILE e # NIL DO
  2372. rect := e.component.bounds.Get();
  2373. NEW(img);
  2374. Raster.Create(img,rect.r-rect.l+1, rect.b-rect.t+1, Raster.BGRA8888);
  2375. NEW(canvas,img);
  2376. e.component.Draw(canvas);
  2377. Raster.Copy(img,image,0,0,img.width-1, img.height-1,rect.l-l, rect.t-t, srcCopy);
  2378. e.dx := rect.l-l; e.dy := rect.t-t;
  2379. IF e.component = start THEN this := e END;
  2380. e := e.next
  2381. END;
  2382. RETURN image
  2383. END ToImg;
  2384. BEGIN {ACTIVE}
  2385. NEW(timer);
  2386. LOOP
  2387. timer.Sleep(400);
  2388. state := (state + 1) MOD 2;
  2389. Update;
  2390. END
  2391. END SelectionList;
  2392. WindowGenerator*= PROCEDURE(xml: XML.Content): WM.Window;
  2393. VAR
  2394. hasErrors : BOOLEAN; (* accessed only from (EXCLUSIVE) *)
  2395. invalidateRectMsg- : Messages.MessageExtension; (* used as unique ID *)
  2396. PrototypeID, PrototypeUID : WMProperties.StringProperty;
  2397. PrototypeBounds-, PrototypeBoundsRelative-, PrototypeBearing : WMProperties.RectangleProperty;
  2398. PrototypeEnabled : WMProperties.BooleanProperty;
  2399. PrototypeFillColor : WMProperties.ColorProperty;
  2400. PrototypeAlignment : WMProperties.Int32Property;
  2401. PrototypeVisible, PrototypeTakesFocus, PrototypeNeedsTab, PrototypeEditMode: WMProperties.BooleanProperty;
  2402. PrototypeScaleFont: WMProperties.Int32Property;
  2403. PrototypeFocusPrevious, PrototypeFocusNext : WMProperties.StringProperty;
  2404. PrototypeFont- : WMProperties.FontProperty;
  2405. StrComponent, StrVisualComponent, StrForm, StrFormWindow, StrModel, StrModelInfo : Strings.String;
  2406. GSonStartDrag, GSonStartDragInfo : Strings.String;
  2407. ModelPrototype-: WMProperties.ReferenceProperty;
  2408. propertyListList- : PropertyListList;
  2409. currentStyle- : XML.Element;
  2410. componentStyleMsg- : ComponentStyleChanged;
  2411. timestamp : LONGINT;
  2412. macroHandlers : MacroHandler; (* the head of the list is always the DefaultMacroHandler *)
  2413. selection-: SelectionList;
  2414. (*
  2415. PROCEDURE ResetInternal(parent:Component);
  2416. VAR c: XML.Content;
  2417. BEGIN
  2418. IF ~parent.initialized THEN parent.Initialize END;
  2419. (*parent.RecacheProperties;*)
  2420. c := parent.GetFirst();
  2421. WHILE (c # NIL) DO
  2422. IF c IS Component THEN
  2423. ResetInternal(c(Component))
  2424. END;
  2425. c := parent.GetNext(c);
  2426. END;
  2427. END ResetInternal;
  2428. *)
  2429. PROCEDURE IsWhiteSpace(ch : CHAR) : BOOLEAN;
  2430. BEGIN
  2431. RETURN ch <= " ";
  2432. END IsWhiteSpace;
  2433. PROCEDURE SkipWhiteSpace(CONST string : ARRAY OF CHAR; VAR index : LONGINT);
  2434. VAR length : LONGINT;
  2435. BEGIN
  2436. length := LEN(string);
  2437. WHILE (index < length) & (string[index] # 0X) & IsWhiteSpace(string[index]) DO INC(index); END;
  2438. ASSERT(index < LEN(string));
  2439. END SkipWhiteSpace;
  2440. PROCEDURE ReadWord*(CONST string : ARRAY OF CHAR; VAR word : ARRAY OF CHAR; VAR index : LONGINT) : BOOLEAN;
  2441. VAR length, wordLength, i : LONGINT;
  2442. BEGIN
  2443. SkipWhiteSpace(string, index);
  2444. length := LEN(string);
  2445. wordLength := LEN(word);
  2446. i := 0;
  2447. WHILE (index < length) & (string[index] # 0X) & ~IsWhiteSpace(string[index]) & (i < wordLength) DO
  2448. word[i] := string[index];
  2449. INC(i);
  2450. INC(index);
  2451. END;
  2452. IF (i < wordLength) THEN word[i] := 0X; END;
  2453. ASSERT(index < LEN(string));
  2454. RETURN (i > 0) & (index < length) & (i < wordLength);
  2455. END ReadWord;
  2456. (* Split <string> into two strings separated by <separator> *)
  2457. PROCEDURE SplitMacroString(CONST string : ARRAY OF CHAR; VAR namespace, name : ARRAY OF CHAR; separator : CHAR);
  2458. VAR i, j : LONGINT;
  2459. BEGIN
  2460. ASSERT((LEN(namespace) >= LEN(string)) & (LEN(name) >= LEN(string)));
  2461. i := 0;
  2462. WHILE (i < LEN(string)) & (string[i] # 0X) & (string[i] # separator) DO
  2463. namespace[i] := string[i];
  2464. INC(i);
  2465. END;
  2466. namespace[i] := 0X;
  2467. INC(i); (* skip separator *)
  2468. j := 0;
  2469. WHILE (i < LEN(string)) & (string[i] # 0X) DO
  2470. name[j] := string[i];
  2471. INC(i); INC(j);
  2472. END;
  2473. name[j] := 0X;
  2474. IF (name = "") THEN COPY(namespace, name); COPY(NoNamespace, namespace); END; (* no namespace *)
  2475. END SplitMacroString;
  2476. PROCEDURE ReportError(CONST text, argument1, argument2 : ARRAY OF CHAR);
  2477. VAR
  2478. message : Events.Message;
  2479. textIdx, messageIdx : LONGINT;
  2480. secondArgument : BOOLEAN;
  2481. PROCEDURE Append(VAR message : ARRAY OF CHAR; CONST argument : ARRAY OF CHAR; VAR index : LONGINT);
  2482. VAR i : LONGINT;
  2483. BEGIN
  2484. i := 0;
  2485. WHILE (i < LEN(argument)) & (argument[i] # 0X) & (index < LEN(message) - 1) DO
  2486. message[index] := argument[i];
  2487. INC(i);
  2488. INC(index);
  2489. END;
  2490. END Append;
  2491. BEGIN
  2492. secondArgument := FALSE;
  2493. textIdx := 0;
  2494. messageIdx := 0;
  2495. WHILE (textIdx < LEN(text)) & (text[textIdx] # 0X) & (messageIdx < LEN(message) - 1) DO
  2496. IF (text[textIdx] # "%") THEN
  2497. message[messageIdx] := text[textIdx];
  2498. INC(messageIdx);
  2499. ELSE
  2500. IF ~secondArgument THEN
  2501. secondArgument := TRUE;
  2502. Append(message, argument1, messageIdx);
  2503. ELSE
  2504. Append(message, argument2, messageIdx);
  2505. END;
  2506. END;
  2507. INC(textIdx);
  2508. END;
  2509. message[messageIdx] := 0X;
  2510. Events.AddEvent("Components", Events.Error, 0, 0, 0, message, FALSE);
  2511. END ReportError;
  2512. PROCEDURE GetArgumentStream*(command: Strings.String; offset: LONGINT; VAR arguments: Streams.StringReader);
  2513. VAR i: LONGINT;
  2514. BEGIN
  2515. IF command = NIL THEN arguments := NIL; RETURN END;
  2516. i := offset;
  2517. WHILE (i < LEN(command)) & (command[i] # 0X) DO INC(i); END;
  2518. IF (i # offset) THEN
  2519. NEW(arguments, i - offset + 1);
  2520. arguments.SetRaw(command^, offset, i - offset + 1);
  2521. ELSE
  2522. arguments := NIL;
  2523. END;
  2524. END GetArgumentStream;
  2525. PROCEDURE GenerateContext*(oldCommand, command : Strings.String; index : LONGINT; originator : Component; CONST event : Event) : EventContext;
  2526. VAR
  2527. context : EventContext; pointerContext : PointerContext; keyContext : KeyContext;
  2528. arguments : Streams.StringReader;
  2529. i : LONGINT;
  2530. BEGIN
  2531. ASSERT((command # NIL) & (0 <= index) & (index < LEN(command)));
  2532. ASSERT(originator # NIL);
  2533. GetArgumentStream(command,index,arguments);
  2534. IF (event IS PointerEvent) THEN
  2535. NEW(pointerContext, originator, oldCommand, NIL, arguments, NIL, NIL, NIL); pointerContext.pointer := event(PointerEvent);
  2536. context := pointerContext;
  2537. ELSIF (event IS KeyPressedEvent) THEN
  2538. NEW(keyContext, originator, oldCommand, NIL, arguments, NIL, NIL, NIL); keyContext.key := event(KeyPressedEvent);
  2539. context := keyContext;
  2540. ELSE
  2541. NEW(context, originator, oldCommand, NIL, arguments, NIL, NIL, NIL);
  2542. END;
  2543. BEGIN {EXCLUSIVE}
  2544. context.timestamp := timestamp;
  2545. INC(timestamp);
  2546. END;
  2547. ASSERT(context # NIL);
  2548. RETURN context;
  2549. END GenerateContext;
  2550. PROCEDURE HandleEvent*(CONST event : Event; originator : Component; command : Strings.String);
  2551. VAR
  2552. commandString : ARRAY 128 OF CHAR;
  2553. newCommand : Strings.String;
  2554. context : EventContext;
  2555. msg : Events.Message;
  2556. index : LONGINT;
  2557. BEGIN
  2558. ASSERT((originator # NIL) & (command # NIL));
  2559. index := 0;
  2560. IF Logging THEN
  2561. COPY(command^, msg);
  2562. Events.AddEvent("Components", Events.Information, 0, 0, 0, msg, FALSE);
  2563. END;
  2564. SubstituteMacros(command, newCommand, originator);
  2565. IF ReadWord(newCommand^, commandString, index) THEN
  2566. context := GenerateContext(command, newCommand, index, originator, event);
  2567. Commands.Activate(commandString, context, {}, context.result, msg); (* asynchronous call since holding the originators lock! *)
  2568. IF (context.result # Commands.Ok) THEN
  2569. Events.AddEvent("Components", Events.Error, 0, 0, 0, msg, FALSE);
  2570. END;
  2571. ELSE
  2572. Events.AddEvent("Components", Events.Error, 0, 0, 0, "Expected command", FALSE);
  2573. END;
  2574. END HandleEvent;
  2575. PROCEDURE ContainsMacros(CONST string : ARRAY OF CHAR) : BOOLEAN;
  2576. VAR result : BOOLEAN; length, i : LONGINT;
  2577. BEGIN
  2578. result := FALSE;
  2579. i := 0; length := LEN(string);
  2580. WHILE (i < length) & (string[i] # 0X) & ~result DO
  2581. IF (string[i] = MacroCharacter) THEN
  2582. result := (i + 1 < length) & (string[i+1] # MacroCharacter);
  2583. IF ~result THEN (* two consequent MacroCharacter's are used to escape *)
  2584. INC(i); (*skip string[i+1] *)
  2585. END;
  2586. END;
  2587. INC(i);
  2588. END;
  2589. RETURN result;
  2590. END ContainsMacros;
  2591. PROCEDURE WriteSelectionToStream(w : Streams.Writer);
  2592. VAR text : Texts.Text; from, to : Texts.TextPosition; a, b : LONGINT;
  2593. BEGIN
  2594. ASSERT(w # NIL);
  2595. IF Texts.GetLastSelection(text, from, to) THEN
  2596. text.AcquireRead;
  2597. a := Strings.Min(from.GetPosition(), to.GetPosition());
  2598. b := Strings.Max(from.GetPosition(), to.GetPosition());
  2599. IF (text.GetLength() > 0) THEN
  2600. TextUtilities.SubTextToStream(text, a, b - a + 1, w);
  2601. END;
  2602. text.ReleaseRead;
  2603. END;
  2604. END WriteSelectionToStream;
  2605. PROCEDURE SubstituteMacro(CONST command : Strings.String; VAR index : LONGINT; originator : Component; w : Streams.Writer);
  2606. VAR oldIndex : LONGINT; macro, namespace, name : Macro; handler : MacroHandlerProcedure; handled : BOOLEAN;
  2607. BEGIN
  2608. ASSERT((command # NIL) & (0 <= index) & (index < LEN(command)) & (command[index] = MacroCharacter));
  2609. ASSERT(originator # NIL);
  2610. ASSERT(w # NIL);
  2611. oldIndex := index;
  2612. INC(index); (* skip MacroCharacter *)
  2613. IF ReadWord(command^, macro, index) THEN (*? TBD error handling *)
  2614. SplitMacroString(macro, namespace, name, NamespaceCharacter);
  2615. IF (namespace = NoNamespace) OR (namespace = DefaultNamespace) THEN
  2616. handler := DefaultMacroHandler;
  2617. ELSE
  2618. BEGIN {EXCLUSIVE}
  2619. handler := FindMacroHandler(namespace);
  2620. END;
  2621. END;
  2622. handled := FALSE;
  2623. IF (handler # NIL) THEN handler(name, originator, w, handled); END;
  2624. IF ~handled THEN
  2625. w.Char(MacroCharacter); w.String(macro); (* don't substitute *)
  2626. END;
  2627. END;
  2628. ASSERT(index > oldIndex);
  2629. END SubstituteMacro;
  2630. PROCEDURE SubstituteMacros*(CONST command : Strings.String; VAR newCommand : Strings.String; originator : Component);
  2631. VAR index, oldIndex, length : LONGINT; w : Streams.Writer; buffer : Strings.Buffer;
  2632. BEGIN
  2633. ASSERT((command # NIL) & (originator # NIL));
  2634. IF ContainsMacros(command^) THEN
  2635. NEW(buffer, 256);
  2636. w := buffer.GetWriter();
  2637. index := 0; length := LEN(command^);
  2638. WHILE (index < length) & (command[index] # 0X) DO
  2639. oldIndex := index;
  2640. IF (command[index] = MacroCharacter) THEN
  2641. IF (index + 1 < length) & (command[index + 1] = MacroCharacter) THEN (* escape *)
  2642. w.Char(MacroCharacter);
  2643. index := index + 2; (* skip both MacroCharacter's *)
  2644. ELSE
  2645. (* substitute macro *)
  2646. SubstituteMacro(command, index, originator, w);
  2647. END;
  2648. ELSE
  2649. w.Char(command[index]);
  2650. INC(index);
  2651. END;
  2652. ASSERT(index > oldIndex);
  2653. END;
  2654. newCommand := buffer.GetString();
  2655. ELSE
  2656. newCommand := command;
  2657. END;
  2658. ASSERT(newCommand # NIL);
  2659. END SubstituteMacros;
  2660. PROCEDURE GetAttributeValue(originator : Component; CONST fullname : ARRAY OF CHAR) : Strings.String;
  2661. VAR value : Strings.String; c : Component; component, attribute : ARRAY 64 OF CHAR;
  2662. BEGIN
  2663. ASSERT(originator # NIL);
  2664. value := NIL;
  2665. Strings.GetExtension(fullname, component, attribute);
  2666. IF (attribute = "") THEN
  2667. COPY(component, attribute);
  2668. COPY("", component);
  2669. END;
  2670. IF (component[0] = "@") THEN component[0] := "&"; END; (*? TBD: Hack to avoid ampersand in XML *)
  2671. IF (component = "") THEN
  2672. c := originator;
  2673. ELSE
  2674. c := originator.Find(component);
  2675. END;
  2676. IF (c # NIL) THEN
  2677. IF c.HasAttribute(attribute) THEN
  2678. RETURN c.GetAttributeValue(attribute);
  2679. ELSE
  2680. ReportError("Attribute % of component % not found", attribute, component);
  2681. END;
  2682. ELSE
  2683. ReportError("Component % not found", component, "");
  2684. END;
  2685. RETURN value;
  2686. END GetAttributeValue;
  2687. PROCEDURE GetPropertyValue(originator : Component; CONST fullname : ARRAY OF CHAR) : Strings.String;
  2688. VAR value : ARRAY 256 OF CHAR; string:Strings.String; c : Component; component, property : ARRAY 64 OF CHAR;
  2689. BEGIN
  2690. ASSERT(originator # NIL);
  2691. Strings.GetExtension(fullname, component, property);
  2692. IF (property = "") THEN COPY(component, property); COPY("", component);
  2693. END;
  2694. IF (component[0] = "@") THEN component[0] := "&"; END; (*? TBD: Hack to avoid ampersand in XML *)
  2695. IF (component = "") THEN c := originator;
  2696. ELSE c := originator.Find(component);
  2697. END;
  2698. IF (c # NIL) THEN
  2699. IF c.properties.GetPropertyValue(property,value) THEN RETURN Strings.NewString(value)
  2700. ELSE ReportError("Property % of component % not found", property, component);
  2701. END;
  2702. ELSE ReportError("Component % not found", component, "");
  2703. END;
  2704. RETURN NIL;
  2705. END GetPropertyValue;
  2706. PROCEDURE DefaultMacroHandler(CONST macro : Macro; originator : Component; w : Streams.Writer; VAR handled : BOOLEAN);
  2707. VAR string, value : Strings.String;
  2708. BEGIN
  2709. ASSERT((originator # NIL) & (w # NIL));
  2710. handled := TRUE;
  2711. IF (macro = MacroSelection) THEN
  2712. WriteSelectionToStream(w);
  2713. ELSIF (macro = MacroClipboard) THEN
  2714. TextUtilities.TextToStream(Texts.clipboard, w);
  2715. ELSIF Strings.StartsWith(MacroAttributePrefix, 0, macro) THEN
  2716. string := Strings.Substring(Strings.Length(MacroAttributePrefix), Strings.Length(macro), macro);
  2717. value := GetAttributeValue(originator, string^);
  2718. IF (value # NIL) THEN
  2719. w.String(value^);
  2720. ELSE
  2721. handled := FALSE;
  2722. END;
  2723. ELSIF Strings.StartsWith(MacroPropertyPrefix, 0, macro) THEN
  2724. string := Strings.Substring(Strings.Length(MacroPropertyPrefix), Strings.Length(macro), macro);
  2725. value := GetPropertyValue(originator,string^);
  2726. IF (value # NIL) THEN
  2727. w.String(value^);
  2728. ELSE
  2729. handled := FALSE;
  2730. END;
  2731. ELSE
  2732. handled := FALSE;
  2733. END;
  2734. END DefaultMacroHandler;
  2735. PROCEDURE FindMacroHandler(CONST namespace : ARRAY OF CHAR) : MacroHandlerProcedure;
  2736. VAR node : MacroHandler; handler : MacroHandlerProcedure;
  2737. BEGIN (* caller must hold module lock! *)
  2738. node := macroHandlers;
  2739. WHILE (node # NIL) & (node.namespace # namespace) DO node := node.next; END;
  2740. IF (node # NIL) THEN
  2741. handler := node.handler;
  2742. ELSE
  2743. handler := NIL;
  2744. END;
  2745. RETURN handler;
  2746. END FindMacroHandler;
  2747. PROCEDURE AddMacroHandler*(CONST namespace : Namespace; handler : MacroHandlerProcedure; VAR res : LONGINT);
  2748. VAR new, node : MacroHandler; h : MacroHandlerProcedure;
  2749. BEGIN {EXCLUSIVE}
  2750. ASSERT((namespace # NoNamespace) & (handler # NIL));
  2751. ASSERT(macroHandlers # NIL);
  2752. h := FindMacroHandler(namespace);
  2753. IF (h = NIL) THEN (* append new handler to list *)
  2754. NEW(new);
  2755. new.handler := handler;
  2756. new.namespace := namespace;
  2757. new.next := NIL;
  2758. node := macroHandlers;
  2759. WHILE (node.next # NIL) DO node := node.next; END;
  2760. node.next := new;
  2761. res := Ok;
  2762. ELSE
  2763. res := DuplicateNamespace;
  2764. END;
  2765. END AddMacroHandler;
  2766. PROCEDURE RemoveMacroHandler*(handler : MacroHandlerProcedure);
  2767. VAR node : MacroHandler;
  2768. BEGIN {EXCLUSIVE}
  2769. ASSERT((handler # NIL) & (handler # DefaultMacroHandler));
  2770. ASSERT(macroHandlers # NIL);
  2771. node := macroHandlers;
  2772. WHILE (node.next # NIL) & (node.next.handler # handler) DO node := node.next; END;
  2773. ASSERT((node.next # NIL) & (node.next.handler = handler));
  2774. node.next := node.next.next;
  2775. END RemoveMacroHandler;
  2776. PROCEDURE SetAttribute*(context : Commands.Context); (** component attribute value ~ *)
  2777. VAR originator, target : Component; name, attribute, value : ARRAY 128 OF CHAR; (*? TBD array size *)
  2778. BEGIN
  2779. IF (context IS EventContext) THEN
  2780. originator := context(EventContext).originator;
  2781. IF context.arg.GetString(name) & context.arg.GetString(attribute) & context.arg.GetString(value) THEN
  2782. target := originator.Find(name);
  2783. IF (target # NIL) THEN
  2784. IF target.HasAttribute(attribute) THEN
  2785. target.SetAttributeValue(attribute, value);
  2786. ELSE
  2787. context.result := Commands.CommandError;
  2788. END;
  2789. ELSE
  2790. context.result := Commands.CommandError;
  2791. END;
  2792. ELSE
  2793. context.error.String("Expected component name, attribute and value parameters"); context.error.Ln;
  2794. context.result := Commands.CommandParseError;
  2795. END;
  2796. ELSE
  2797. context.error.String("Command requires EventContext."); context.error.Ln;
  2798. context.result := Commands.CommandParseError;
  2799. END;
  2800. END SetAttribute;
  2801. (** Activate a string of commands, including their parameters.
  2802. The string is parsed from left to right and Activate is called for every command.
  2803. Parsing stops at the end of the string, or when Activate returns an error.
  2804. The flags are applied to every command, i.e., for sequential execution,
  2805. use the Wait flag (the caller waits until all commands return).
  2806. Syntax:
  2807. cmds = [mode " " ] cmd {";" cmd} .
  2808. mode = "PAR" | "SEQ" .
  2809. cmd = mod ["." proc] [" " params] .
  2810. params = {<any character except ";">} .
  2811. REMARK: For now, this is almost the same as Commands.Call. This procedure will either be enhanced to
  2812. support some component-related macro substitution or be replaced by Commands.Call
  2813. *)
  2814. PROCEDURE Call*(cmds : ARRAY OF CHAR; caller : Component; flags : SET; VAR res : LONGINT; VAR msg : ARRAY OF CHAR);
  2815. VAR
  2816. context : Commands.Context; arg : Streams.StringReader;
  2817. buffer : Strings.Buffer; w : Streams.Writer; par : Strings.String;
  2818. length, i, k : LONGINT;
  2819. PROCEDURE Expand(CONST string : ARRAY OF CHAR; w : Streams.Writer; start : LONGINT; VAR end : LONGINT);
  2820. VAR
  2821. component : Component;
  2822. componentStr, attributeStr : ARRAY 256 OF CHAR;
  2823. property : WMProperties.Property; attribute : XML.Attribute;
  2824. value : Strings.String;
  2825. lastDotIdx, i, j : LONGINT; error : BOOLEAN;
  2826. BEGIN
  2827. ASSERT((string[start] = "&") & (start + 1 < LEN(string)) & (w # NIL));
  2828. end := start; WHILE (end < LEN(string)) & (string[end] # 0X) & (string[end] # ";") & (string[end] > " ") DO INC(end); END;
  2829. DEC(end);
  2830. lastDotIdx := end;
  2831. WHILE (lastDotIdx > start) & (string[lastDotIdx] # ".") DO DEC(lastDotIdx); END;
  2832. error := (lastDotIdx <= start); (* missing dot *)
  2833. IF ~error THEN
  2834. i := start + 1; (* skip ampersand *)
  2835. IF (string[i] = "&") OR (string[i] = "/") THEN
  2836. j := 0;
  2837. WHILE (i < lastDotIdx) & (j < LEN(componentStr) - 1) DO
  2838. componentStr[j] := string[i];
  2839. INC(i); INC(j);
  2840. END;
  2841. componentStr[j] := 0X;
  2842. component := caller.Find(componentStr);
  2843. ELSE
  2844. componentStr := "";
  2845. component := caller;
  2846. END;
  2847. ASSERT(string[i] = ".");
  2848. INC(i); (* skip dot *)
  2849. attributeStr := "";
  2850. j := 0;
  2851. WHILE (j < LEN(attributeStr)) & (i <= end) DO
  2852. attributeStr[j] := string[i];
  2853. INC(i); INC(j);
  2854. END;
  2855. error := (attributeStr = "");
  2856. IF ~error THEN
  2857. IF (component # NIL) THEN
  2858. property := component.properties.Get(attributeStr);
  2859. IF (property # NIL) THEN
  2860. property.ToStream(w);
  2861. ELSE
  2862. attribute := component.GetAttribute(attributeStr);
  2863. IF (attribute # NIL) THEN
  2864. value := attribute.GetValue();
  2865. IF (value # NIL) THEN w.String(value^); ELSE w.String("NIL"); END;
  2866. ELSE
  2867. error := TRUE;
  2868. END;
  2869. END;
  2870. ELSE
  2871. error := TRUE;
  2872. END;
  2873. END;
  2874. END;
  2875. IF error THEN (* don't expand macro *)
  2876. FOR i := start TO end DO w.Char(string[i]); END;
  2877. END;
  2878. ASSERT(end >= start);
  2879. END Expand;
  2880. BEGIN
  2881. ASSERT(caller # NIL);
  2882. NEW(buffer, LEN(cmds)); w := buffer.GetWriter();
  2883. IF Strings.StartsWith2(Repositories.CommandPrefix, cmds) THEN i := Strings.Length(Repositories.CommandPrefix); ELSE i := 0; END;
  2884. LOOP
  2885. buffer.Clear;
  2886. w.Reset;
  2887. k := 0;
  2888. WHILE (i < LEN(cmds)) & (cmds[i] # " ") & (cmds[i] # 09X) & (cmds[i] # 0DX) & (cmds[i] # 0AX) & (cmds[i] # 0X) & (cmds[i] # ";") DO cmds[k] := cmds[i]; INC(k); INC(i); END;
  2889. IF k = 0 THEN EXIT; END; (* end of string *)
  2890. IF (i < LEN(cmds)) & (cmds[i] # ";") & (cmds[i] # 0X) THEN (* parameters *)
  2891. INC(i); (* skip delimiter *)
  2892. WHILE (i < LEN(cmds)) & (cmds[i] # 0X) & (cmds[i] # ";") DO
  2893. IF (cmds[i] = "&") & (i + 1 < LEN(cmds)) & ((cmds[i+1] = "&") OR (cmds[i+1] = ".") OR (cmds[i+1] = "/")) THEN
  2894. Expand(cmds, w, i, i);
  2895. ELSE
  2896. w.Char(cmds[i]);
  2897. END;
  2898. INC(i);
  2899. END;
  2900. END;
  2901. IF (i < LEN(cmds)) & (cmds[i] = ";") THEN (* skip command delimiter *) INC(i); END;
  2902. cmds[k] := 0X;
  2903. length := buffer.GetLength();
  2904. IF (length > 0) THEN
  2905. par := buffer.GetString();
  2906. NEW(arg, length + 1); arg.SetRaw(par^, 0, length + 1);
  2907. ELSE
  2908. arg := NIL;
  2909. END;
  2910. NEW(context, NIL, arg, NIL, NIL, caller);
  2911. Commands.Activate(cmds, context, flags, res, msg);
  2912. IF (res # Commands.Ok) THEN KernelLog.String("WMComponents.Call error, res = "); KernelLog.Int(res, 0); KernelLog.Ln; EXIT; END;
  2913. END;
  2914. END Call;
  2915. PROCEDURE GetComponent*(CONST name : ARRAY OF CHAR) : Component;
  2916. VAR component : Component; c : Repositories.Component; res : LONGINT;
  2917. BEGIN
  2918. component := NIL;
  2919. Repositories.GetComponentByString(name, c, res);
  2920. IF (res = Repositories.Ok) THEN
  2921. IF (c # NIL) & (c IS Component) THEN
  2922. component := c (Component);
  2923. ELSE
  2924. KernelLog.String("WMComponents.GetComponent: Could not generate component ");
  2925. KernelLog.String(name); KernelLog.String(": Wrong type"); KernelLog.Ln;
  2926. END;
  2927. ELSE
  2928. KernelLog.String("WMComponents.GetComponent: Could not generate component ");
  2929. KernelLog.String(name); KernelLog.String(", res: "); KernelLog.Int(res, 0); KernelLog.Ln;
  2930. END;
  2931. RETURN component;
  2932. END GetComponent;
  2933. PROCEDURE GetVisualComponent*(CONST name : ARRAY OF CHAR) : VisualComponent;
  2934. VAR component : VisualComponent; c : Repositories.Component; res : LONGINT;
  2935. BEGIN
  2936. component := NIL;
  2937. Repositories.GetComponentByString(name, c, res);
  2938. IF (res = Repositories.Ok) THEN
  2939. IF (c # NIL) & (c IS VisualComponent) THEN
  2940. component := c (VisualComponent);
  2941. ELSE
  2942. KernelLog.String("WMComponents.GetVisualComponent: Could not generate component ");
  2943. KernelLog.String(name); KernelLog.String(": Wrong type"); KernelLog.Ln;
  2944. END;
  2945. ELSE
  2946. KernelLog.String("WMComponents.GetVisualComponent: Could not generate component ");
  2947. KernelLog.String(name); KernelLog.String(", res: "); KernelLog.Int(res, 0); KernelLog.Ln;
  2948. END;
  2949. RETURN component;
  2950. END GetVisualComponent;
  2951. PROCEDURE SetStyle*(style : XML.Element);
  2952. BEGIN
  2953. SetStyleInternal(style)
  2954. END SetStyle;
  2955. PROCEDURE SetStyleInternal(style : XML.Element);
  2956. VAR msg : Messages.Message; m : WM.WindowManager;
  2957. BEGIN
  2958. currentStyle := style;
  2959. IF propertyListList # NIL THEN propertyListList.UpdateStyle END;
  2960. msg.msgType := Messages.MsgExt; msg.ext := componentStyleMsg;
  2961. m := WM.GetDefaultManager();
  2962. m.Broadcast(msg)
  2963. END SetStyleInternal;
  2964. PROCEDURE FindRelativePath(x : Component; CONST path : ARRAY OF CHAR; pos : LONGINT) : Component;
  2965. VAR c : XML.Content;
  2966. sn : ARRAY MaxComponentNameSize OF CHAR;
  2967. i : LONGINT; id : Strings.String;
  2968. BEGIN
  2969. IF x = NIL THEN RETURN NIL
  2970. ELSIF path[pos] = 0X THEN RETURN x
  2971. ELSIF (pos = 0) & (path[0] = "/") THEN RETURN FindRelativePath(x.GetComponentRoot(), path, pos + 1)
  2972. ELSIF (path[pos] = ".") & (path[pos + 1] = ".") THEN
  2973. INC(pos, 2); IF path[pos]="/" THEN INC(pos) END;
  2974. c := x.GetParent();
  2975. IF (c # NIL) & (c IS Component) THEN
  2976. RETURN FindRelativePath(c(Component), path, pos)
  2977. ELSE
  2978. RETURN NIL
  2979. END
  2980. ELSE
  2981. i := 0; WHILE (i < MaxComponentNameSize - 1) & (path[pos] # 0X) & (path[pos] # "/") DO
  2982. sn[i] := path[pos]; INC(i); INC(pos)
  2983. END;
  2984. IF (path[pos] = "/") THEN INC(pos) END;
  2985. sn[i] := 0X;
  2986. c := x.GetFirst();
  2987. WHILE (c # NIL) DO
  2988. IF (c IS Component) THEN
  2989. id := c(Component).id.Get();
  2990. IF (id # NIL) & (id^ = sn) THEN
  2991. RETURN FindRelativePath(c(Component), path, pos);
  2992. END;
  2993. END;
  2994. c := x.GetNext(c);
  2995. END;
  2996. RETURN NIL
  2997. END
  2998. END FindRelativePath;
  2999. (* Report errors while parsing *)
  3000. PROCEDURE Error(pos, line, row: LONGINT; CONST msg: ARRAY OF CHAR);
  3001. BEGIN
  3002. KernelLog.String("Parse error at pos "); KernelLog.Int(pos, 5); KernelLog.String(" in line "); KernelLog.Int(line, 5);
  3003. KernelLog.String(" row "); KernelLog.Int(row, 5); KernelLog.String(" - "); KernelLog.String(msg); KernelLog.Ln;
  3004. hasErrors := TRUE
  3005. END Error;
  3006. (** Load an XML file. Return NIL if errors occured *)
  3007. PROCEDURE Load*(CONST filename : ARRAY OF CHAR) : XML.Content;
  3008. VAR scanner : XMLScanner.Scanner;
  3009. parser : XMLParser.Parser;
  3010. doc : XML.Document;
  3011. in : Streams.Reader;
  3012. BEGIN {EXCLUSIVE}
  3013. hasErrors := FALSE;
  3014. in := Codecs.OpenInputStream(filename);
  3015. IF in # NIL THEN
  3016. NEW(scanner, in); scanner.reportError := Error;
  3017. NEW(parser, scanner); parser.reportError := Error;
  3018. parser.elemReg := Repositories.registry; doc := parser.Parse();
  3019. IF hasErrors THEN RETURN NIL END;
  3020. RETURN doc.GetRoot()
  3021. END;
  3022. RETURN NIL
  3023. END Load;
  3024. PROCEDURE FormWindowGen*(xml:XML.Content): WM.Window;
  3025. VAR winx: XML.Element; formx: XML.Content; window: FormWindow; name, string:Strings.String; canvas:WMGraphics.BufferCanvas;
  3026. canvasGenerator: WMGraphics.CanvasGenerator;
  3027. moduleName, procedureName : Modules.Name;
  3028. msg : ARRAY 128 OF CHAR;
  3029. res: LONGINT;
  3030. l,t,r,b: LONGINT;
  3031. BEGIN
  3032. IF xml IS XML.Element THEN
  3033. winx:=xml(XML.Element);
  3034. string:=winx.GetName();
  3035. IF string^="FormWindow" THEN
  3036. string:=winx.GetAttributeValue("l"); Strings.StrToInt(string^,l);
  3037. string:=winx.GetAttributeValue("t"); Strings.StrToInt(string^,t);
  3038. string:=winx.GetAttributeValue("r"); Strings.StrToInt(string^,r);
  3039. string:=winx.GetAttributeValue("b"); Strings.StrToInt(string^,b);
  3040. NEW(window, r-l, b-t, TRUE);
  3041. name:=winx.GetAttributeValue("name"); window.SetTitle(name);
  3042. window.bounds.r:=r; window.bounds.l:=l; window.bounds.t:=t; window.bounds.b:=b;
  3043. string:=winx.GetAttributeValue("flags"); Strings.StrToSet(string^,window.flags);
  3044. string:=winx.GetAttributeValue("canvasGenerator"); (* allow to plug in alternative canvas versions,e.g. WMGraphicsGfx.Canvas *)
  3045. IF (string#NIL) THEN
  3046. Commands.Split(string^, moduleName, procedureName, res, msg);
  3047. IF (res = Commands.Ok) THEN
  3048. GETPROCEDURE(moduleName, procedureName, canvasGenerator);
  3049. IF (canvasGenerator # NIL) THEN
  3050. window.SetCanvasGenerator(canvasGenerator);
  3051. END;
  3052. END;
  3053. END;
  3054. formx:=winx.GetFirst(); (* this typically has name="Form" *)
  3055. IF (formx#NIL)&(formx IS XML.Element) THEN
  3056. window.LoadComponents(formx(XML.Element)); (* at the price of duplication of component tree construction ...*)
  3057. window.form.Reset(NIL,NIL);
  3058. ELSE window:=NIL;
  3059. END;
  3060. END;
  3061. END;
  3062. RETURN window
  3063. END FormWindowGen;
  3064. (* generic loading of any form window using the generator procedure supplied in the XML as 'loader' attribute *)
  3065. PROCEDURE LoadFormWindow*(xml:XML.Content): WM.Window;
  3066. VAR winx: XML.Element; window: WM.Window; formWindow:FormWindow;
  3067. formx, c:Component;
  3068. name, string, load:Strings.String;
  3069. moduleName, procedureName : Modules.Name;
  3070. msg : ARRAY 128 OF CHAR;
  3071. res: LONGINT;
  3072. gen:WindowGenerator;
  3073. bounds:Rectangles.Rectangle;
  3074. BEGIN
  3075. IF xml IS XML.Element THEN
  3076. winx:=xml(XML.Element);
  3077. name:=winx.GetName();
  3078. IF name^="FormWindow" THEN
  3079. string:=winx.GetAttributeValue("loader");
  3080. Commands.Split(string^, moduleName, procedureName, res, msg);
  3081. IF (res = Commands.Ok) THEN
  3082. GETPROCEDURE(moduleName, procedureName, gen);
  3083. IF (gen # NIL) THEN
  3084. window:=gen(xml);
  3085. END;
  3086. END;
  3087. ELSE (*generate FormWindow from generic Visual Component*)
  3088. c:=ComponentFromXML(xml(XML.Element));
  3089. IF (c#NIL) & (c IS VisualComponent) THEN
  3090. bounds:=c(VisualComponent).bounds.Get();
  3091. NEW(formWindow, bounds.r-bounds.l, bounds.b-bounds.t, TRUE);
  3092. formWindow.SetContent(c);
  3093. (*formWindow.SetTitle(c.GetName());*)
  3094. window:=formWindow;
  3095. END;
  3096. END;
  3097. END;
  3098. RETURN window
  3099. END LoadFormWindow;
  3100. (** Open form window and build its component tree *)
  3101. PROCEDURE Open*(context : Commands.Context);
  3102. VAR filename: Files.FileName; window: WM.Window; xml:XML.Content;
  3103. BEGIN
  3104. IF context.arg.GetString(filename) & (Strings.Length(filename)>0) THEN
  3105. xml:=Load(filename); (* here, the xml tree is already constructed, however not in the right sequence for component contruction ( [Init()..loadProperties()..Initialize()] )*)
  3106. window:=LoadFormWindow(xml);
  3107. IF window#NIL THEN
  3108. WM.AddWindow(window,window.bounds.l,window.bounds.t);
  3109. END;
  3110. END;
  3111. END Open;
  3112. PROCEDURE LoadStyleInternal(CONST filename : ARRAY OF CHAR);
  3113. VAR f : Files.File;
  3114. scanner : XMLScanner.Scanner;
  3115. parser : XMLParser.Parser;
  3116. reader : Files.Reader;
  3117. doc : XML.Document;
  3118. BEGIN
  3119. hasErrors := FALSE;
  3120. f := Files.Old(filename);
  3121. IF f # NIL THEN
  3122. NEW(reader, f, 0);
  3123. NEW(scanner, reader); scanner.reportError := Error;
  3124. NEW(parser, scanner); parser.reportError := Error;
  3125. parser.elemReg := Repositories.registry; doc := parser.Parse();
  3126. IF hasErrors THEN KernelLog.String("Stylefile not ok"); KernelLog.Ln
  3127. ELSE
  3128. SetStyleInternal(doc.GetRoot())
  3129. END
  3130. END
  3131. END LoadStyleInternal;
  3132. (** Load Component registry file. Return NIL if errors occured *)
  3133. PROCEDURE LoadStyle*(context : Commands.Context);
  3134. VAR filename : ARRAY 64 OF CHAR;
  3135. BEGIN {EXCLUSIVE}
  3136. IF context.arg.GetString(filename) THEN
  3137. LoadStyleInternal(filename);
  3138. ELSE
  3139. context.result := Commands.CommandParseError;
  3140. END;
  3141. END LoadStyle;
  3142. PROCEDURE NewString*(CONST x : ARRAY OF CHAR) : Strings.String;
  3143. VAR t : Strings.String;
  3144. BEGIN
  3145. NEW(t, LEN(x)); COPY(x, t^); RETURN t
  3146. END NewString;
  3147. PROCEDURE InitStrings;
  3148. BEGIN
  3149. StrComponent := NewString("Component");
  3150. StrVisualComponent := NewString("VisualComponent");
  3151. StrForm := NewString("Form");
  3152. StrFormWindow := NewString("FormWindow");
  3153. GSonStartDrag := NewString("onStartDrag");
  3154. GSonStartDragInfo := NewString("Event generated whenever a drag is started");
  3155. StrModel := NewString("Model");
  3156. StrModelInfo := NewString("Model used by component");
  3157. END InitStrings;
  3158. PROCEDURE InitPrototypes;
  3159. BEGIN
  3160. (* General component properties *)
  3161. NEW(PrototypeID, NIL, NewString("ID"),
  3162. NewString("identifier of the component"));
  3163. NEW(PrototypeUID, NIL, NewString("UID"),
  3164. NewString("unique identifier of the component"));
  3165. NEW(PrototypeEnabled, NIL, NewString("Enabled"),
  3166. NewString("defines if the component is enabled"));
  3167. PrototypeEnabled.Set(TRUE);
  3168. (* Visual component properties *)
  3169. NEW(PrototypeBounds, NIL, NewString("Bounds"),
  3170. NewString("the bounding box of the component in parent coordinates"));
  3171. NEW(PrototypeBoundsRelative, NIL, NewString("RelBounds"),
  3172. NewString("the bounding box of the component in relative parent coordinates"));
  3173. NEW(PrototypeBearing, NIL, NewString("Bearing"),
  3174. NewString("the bearing (empty space) aroung the component if auto aligned"));
  3175. NEW(PrototypeFillColor, NIL, NewString("FillColor"),
  3176. NewString("the main fill color of the component. i.e. background"));
  3177. NEW(PrototypeAlignment, NIL, NewString("Alignment"),
  3178. NewString("defines the alignment none, left, right, top, bottom or client"));
  3179. PrototypeAlignment.Set(0);
  3180. NEW(PrototypeVisible, NIL, NewString("Visible"),
  3181. NewString("defines if the component is visible"));
  3182. PrototypeVisible.Set(TRUE);
  3183. NEW(PrototypeTakesFocus, NIL, NewString("TakesFocus"),
  3184. NewString("defines if the component takes the keyboard focus"));
  3185. NEW(PrototypeNeedsTab, NIL, NewString("NeedsTab"),
  3186. NewString("defines if the component handles the tabulator key"));
  3187. NEW(PrototypeFocusPrevious, NIL, NewString("FocusPrevious"), NewString("Previous focus component ID"));
  3188. PrototypeFocusPrevious.Set(NIL);
  3189. NEW(PrototypeFocusNext, NIL, NewString("FocusNext"), NewString("Next focus component ID"));
  3190. PrototypeFocusNext.Set(NIL);
  3191. NEW(PrototypeEditMode, NIL, NewString("EditMode"), NewString("defines if the contents of the component can be edited"));
  3192. PrototypeEditMode.Set(FALSE);
  3193. NEW(PrototypeFont, NIL, NewString("Font"), NewString("Font"));
  3194. PrototypeFont.Set(WMGraphics.GetDefaultFont());
  3195. NEW(PrototypeScaleFont, NIL, Strings.NewString("ScaleFont"), Strings.NewString("percentage that fonts scales with height (0=none)"));
  3196. NEW(ModelPrototype, NIL, StrModel, StrModelInfo);
  3197. END InitPrototypes;
  3198. PROCEDURE ShowComponent(component : Component);
  3199. VAR string : Strings.String;
  3200. BEGIN
  3201. IF (component # NIL) THEN
  3202. string := component.GetName();
  3203. IF (string # NIL) THEN KernelLog.String(string^); ELSE KernelLog.String("NoName"); END;
  3204. KernelLog.String(" [");
  3205. string := component.uid.Get();
  3206. IF (string # NIL) THEN KernelLog.String(string^); ELSE KernelLog.String("NIL"); END;
  3207. IF (component IS VisualComponent) THEN
  3208. KernelLog.String(", "); KernelLog.Boolean(component(VisualComponent).takesFocus.Get());
  3209. END;
  3210. KernelLog.String("]");
  3211. ELSE
  3212. KernelLog.String("NIL?");
  3213. END;
  3214. END ShowComponent;
  3215. PROCEDURE NewLine(w : Streams.Writer; level : LONGINT);
  3216. BEGIN
  3217. w.Ln; WHILE level > 0 DO w.Char(09X); DEC(level) END
  3218. END NewLine;
  3219. PROCEDURE InstallDefaultMacroHandler;
  3220. BEGIN
  3221. NEW(macroHandlers);
  3222. macroHandlers.handler := DefaultMacroHandler;
  3223. macroHandlers.namespace := DefaultNamespace;
  3224. macroHandlers.next := NIL;
  3225. END InstallDefaultMacroHandler;
  3226. (*! ---- xml tool --- move to where appropriate *)
  3227. PROCEDURE GetElementByName(parent : XML.Element; CONST name : ARRAY OF CHAR) : XML.Element;
  3228. VAR elem : XML.Element; enum : XMLObjects.Enumerator; ptr : ANY; string : Strings.String;
  3229. BEGIN
  3230. IF parent # NIL THEN
  3231. enum := parent.GetContents(); enum.Reset();
  3232. WHILE enum.HasMoreElements() DO
  3233. ptr := enum.GetNext();
  3234. IF ptr IS XML.Element THEN
  3235. elem := ptr (XML.Element);
  3236. string := elem.GetName();
  3237. IF (string # NIL) & (string^ = name) THEN
  3238. RETURN elem;
  3239. END;
  3240. END;
  3241. END;
  3242. END;
  3243. RETURN NIL;
  3244. END GetElementByName;
  3245. PROCEDURE NewComponent*(): XML.Element;
  3246. VAR component: Component;
  3247. BEGIN NEW(component); RETURN component;
  3248. END NewComponent;
  3249. PROCEDURE NewVisualComponent*(): XML.Element;
  3250. VAR component: VisualComponent;
  3251. BEGIN NEW(component); RETURN component;
  3252. END NewVisualComponent;
  3253. (* does not work like this its own because a form is statically bound to a window, but for completeness.. *)
  3254. PROCEDURE NewForm*(): XML.Element;
  3255. VAR component: Form;
  3256. BEGIN NEW(component, NIL); RETURN component
  3257. END NewForm;
  3258. PROCEDURE Align*(context: Commands.Context);
  3259. VAR width,height,bwidth,bheight: LONGINT; entry: ComponentListEntry; b,rect: Rectangles.Rectangle; string: ARRAY 32 OF CHAR; l,t: LONGINT; done: BOOLEAN;
  3260. BEGIN
  3261. entry := selection.first;
  3262. rect.l := MAX(LONGINT); rect.r := MIN(LONGINT);
  3263. rect.t := MAX(LONGINT); rect.b := MIN(LONGINT);
  3264. width := 0; height := 0;
  3265. WHILE entry # NIL DO
  3266. b := entry.component.bounds.Get();
  3267. bwidth := b.r-b.l; bheight := b.b-b.t;
  3268. IF b.l < rect.l THEN rect.l := b.l END;
  3269. IF b.r > rect.r THEN rect.r := b.r END;
  3270. IF b.t < rect.t THEN rect.t := b.t END;
  3271. IF b.b > rect.b THEN rect.b := b.b END;
  3272. IF width < bwidth THEN width := bwidth END;
  3273. IF height < bheight THEN height := bheight END;
  3274. entry := entry.next
  3275. END;
  3276. done := FALSE;
  3277. WHILE ~done & context.arg.GetString(string) DO
  3278. l := rect.l; t := rect.t;
  3279. entry := selection.first;
  3280. WHILE ~done & (entry # NIL) DO
  3281. b := entry.component.bounds.Get(); bwidth := b.r-b.l; bheight := b.b-b.t;
  3282. entry.component.AdaptRelativeBounds(b,entry.component.GetParent());
  3283. IF string = "left" THEN b.l := rect.l; b.r := rect.l + bwidth;
  3284. ELSIF string = "right" THEN b.r := rect.r; b.l := rect.r-bwidth
  3285. ELSIF string = "top" THEN b.t := rect.t; b.b := rect.t + bheight;
  3286. ELSIF string = "bottom" THEN b.b := rect.b; b.t := rect.b-bheight
  3287. ELSIF string = "width" THEN b.r := b.l + width;
  3288. ELSIF string = "height" THEN b.b := b.t + height;
  3289. ELSIF string = "size" THEN b.r := b.l + width; b.b := b.t + height;
  3290. ELSIF string = "hcenter" THEN b.l := (rect.l+rect.r) DIV 2 - bwidth DIV 2; b.r := b.l + bwidth;
  3291. ELSIF string = "vcenter" THEN b.t := (rect.t + rect.b) DIV 2 - bheight DIV 2; b.b := b.t + bheight;
  3292. ELSIF string = "horizontal" THEN b.l := l; b.r := b.l + bwidth; l := b.r+1
  3293. ELSIF string = "vertical" THEN b.t := t; b.b := b.t + bheight; t := b.b + 1;
  3294. ELSIF string = "none" THEN entry.component.alignment.Set(AlignNone)
  3295. ELSIF string = "relative" THEN entry.component.alignment.Set(AlignRelative)
  3296. ELSE done := TRUE
  3297. END;
  3298. entry.component.AdaptRelativeBounds(b,entry.component.GetParent());
  3299. entry.component.bounds.Set(b);
  3300. entry := entry.next
  3301. END;
  3302. END;
  3303. END Align;
  3304. PROCEDURE SetProperty*(context: Commands.Context);
  3305. VAR name, value: ARRAY 256 OF CHAR; entry: ComponentListEntry;
  3306. BEGIN
  3307. IF context.arg.GetString(name) & context.arg.GetString(value) THEN
  3308. entry := selection.first;
  3309. WHILE entry # NIL DO
  3310. IF entry.component.properties.SetPropertyValue(name, value) THEN END;
  3311. entry := entry.next;
  3312. END;
  3313. END;
  3314. END SetProperty;
  3315. PROCEDURE RemoveSelection*;
  3316. VAR entry: ComponentListEntry; parent: XML.Element;
  3317. BEGIN
  3318. entry := selection.first;
  3319. WHILE entry # NIL DO
  3320. parent := entry.component.GetParent();
  3321. IF parent # NIL THEN parent(VisualComponent).RemoveContent(entry.component); parent(VisualComponent).Invalidate END;
  3322. entry := entry.next
  3323. END;
  3324. END RemoveSelection;
  3325. PROCEDURE ComponentFromXML*(xml: XML.Element): Component;
  3326. VAR generator: PROCEDURE(): XML.Element;
  3327. VAR
  3328. l,name: Strings.String;
  3329. moduleName, procedureName: Modules.Name;
  3330. res: LONGINT; msg: ARRAY 32 OF CHAR;
  3331. component: Component;
  3332. element: XML.Element;
  3333. BEGIN
  3334. component := NIL;
  3335. IF xml # NIL THEN
  3336. name := xml.GetName();
  3337. l := xml.GetAttributeValue("generator");
  3338. IF l # NIL THEN
  3339. Commands.Split(l^, moduleName, procedureName, res, msg);
  3340. IF (res = Commands.Ok) THEN
  3341. GETPROCEDURE(moduleName, procedureName, generator);
  3342. IF (generator # NIL) THEN
  3343. element := generator();
  3344. IF (element # NIL) & (element IS Component) THEN
  3345. component := element(Component);
  3346. component.SetName(name^);
  3347. component.FromXML(xml);
  3348. END;
  3349. ELSE KernelLog.String("WMComponents error: invalid generator "); KernelLog.String(l^); KernelLog.Ln;
  3350. END;
  3351. ELSE KernelLog.String("WMComponents error: could not generate component "); KernelLog.String(l^); KernelLog.Ln;
  3352. END;
  3353. END;
  3354. END;
  3355. RETURN component
  3356. END ComponentFromXML;
  3357. PROCEDURE Clone*(x: Component): Repositories.Component;
  3358. BEGIN
  3359. RETURN ComponentFromXML(x)
  3360. END Clone;
  3361. BEGIN
  3362. timestamp := 0;
  3363. NEW(componentStyleMsg);
  3364. NEW(propertyListList);
  3365. InitStrings;
  3366. InitPrototypes;
  3367. NEW(invalidateRectMsg);
  3368. InstallDefaultMacroHandler;
  3369. NEW(selection);
  3370. END WMComponents.
  3371. WMComponents.Open FigureExample.Cwd ~
  3372. WMComponents.Open DictEntry.wm ~
  3373. The message sequencer contains a reader writer lock that can be used to block the hierarchy.
  3374. Each message-call from the sequencer posesses the writer lock.
  3375. WMComponents.LoadStyle ComponentStyle.XML ~
  3376. If a focusComponent is set in an non-focus container-component, the focus can not escape the "isolated" component group