WMComponents.Mod 122 KB

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