WMComponents.Mod 124 KB

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