ComponentViewer.Mod 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. MODULE ComponentViewer; (** AUTHOR "TF"; PURPOSE "Testbed for the component system"; *)
  2. IMPORT
  3. Modules, Commands, Options, XML, Repositories, WMMessages, WMWindowManager, WMComponents,
  4. WMRestorable, Streams, D:= Debugging, Files, WMRectangles, WMGraphicsSmooth;
  5. CONST
  6. DefaultWidth = 320;
  7. DefaultHeight = 240;
  8. InvalidPosition* =MIN(LONGINT);
  9. FlagMoveable* = 20;
  10. FlagSmoothGraphics* = 21;
  11. TYPE
  12. KillerMsg = OBJECT
  13. END KillerMsg;
  14. Window* = OBJECT(WMComponents.FormWindow)
  15. VAR dragging: BOOLEAN; lastX, lastY: LONGINT;
  16. PROCEDURE RestoreWindow*(c : WMRestorable.Context);
  17. BEGIN
  18. ReInit(c.r-c.l, c.b-c.t);
  19. (*
  20. Init(c.r - c.l, c.b - c.t, FALSE);
  21. *)
  22. IF c.appData # NIL THEN
  23. DisableUpdate;
  24. LoadComponents(c.appData(XML.Element));
  25. EnableUpdate;
  26. END;
  27. WMRestorable.AddByContext(SELF, c);
  28. Resized(c.r-c.l,c.b-c.t);
  29. END RestoreWindow;
  30. PROCEDURE &InitWindow(width, height : LONGINT; alpha : BOOLEAN);
  31. BEGIN
  32. IncCount;
  33. Init(width, height, alpha);
  34. END InitWindow;
  35. PROCEDURE Close;
  36. BEGIN
  37. Close^;
  38. DecCount
  39. END Close;
  40. PROCEDURE Handle(VAR m : WMMessages.Message);
  41. VAR data: XML.Element;
  42. BEGIN
  43. IF (m.msgType = WMMessages.MsgExt) & (m.ext # NIL) THEN
  44. IF (m.ext IS KillerMsg) THEN Close
  45. ELSIF (m.ext IS WMRestorable.Storage) THEN
  46. data := StoreComponents();
  47. m.ext(WMRestorable.Storage).Add("ComponentViewer", "ComponentViewer.Restore", SELF, data)
  48. ELSE Handle^(m);
  49. END;
  50. ELSE Handle^(m);
  51. END;
  52. END Handle;
  53. PROCEDURE PointerDown(x, y:LONGINT; keys:SET);
  54. BEGIN
  55. lastX := bounds.l + x; lastY:=bounds.t + y;
  56. IF (keys = {0}) & (FlagMoveable IN flags) THEN
  57. dragging := TRUE;
  58. PointerDown^(x,y,keys);
  59. ELSE
  60. PointerDown^(x,y,keys);
  61. END;
  62. END PointerDown;
  63. PROCEDURE PointerMove(x,y:LONGINT; keys:SET);
  64. VAR dx, dy, width, height : LONGINT;
  65. BEGIN
  66. IF dragging THEN
  67. x := bounds.l + x; y := bounds.t + y; dx := x - lastX; dy := y - lastY;
  68. lastX := lastX + dx; lastY := lastY + dy;
  69. IF (dx # 0) OR (dy # 0) THEN
  70. manager.SetWindowPos(SELF, bounds.l + dx, bounds.t + dy);
  71. END;
  72. END;
  73. END PointerMove;
  74. PROCEDURE PointerUp(x, y:LONGINT; keys:SET);
  75. BEGIN
  76. dragging := FALSE;
  77. PointerDown^(x,y,keys);
  78. END PointerUp;
  79. END Window;
  80. VAR
  81. nofWindows : LONGINT;
  82. PROCEDURE DoShow*( vc: WMComponents.VisualComponent; VAR window: Window; x,y,width, height: LONGINT; client, alpha, fullscreen: BOOLEAN;flags: SET);
  83. VAR
  84. fx,fy,fw,fh: LONGINT;
  85. viewPort: WMWindowManager.ViewPort;
  86. manager: WMWindowManager.WindowManager;
  87. BEGIN
  88. IF width # 0 THEN
  89. vc.bounds.SetWidth(width);
  90. ELSE
  91. width := vc.bounds.GetWidth();
  92. IF (width <= 0) THEN width := DefaultWidth; vc.bounds.SetWidth(width) END;
  93. END;
  94. IF height # 0 THEN
  95. vc.bounds.SetHeight(height);
  96. ELSE
  97. height := vc.bounds.GetHeight();
  98. IF (height <= 0) THEN height := DefaultHeight; vc.bounds.SetHeight(height) END;
  99. END;
  100. IF client THEN vc.alignment.Set(WMComponents.AlignClient) END;
  101. IF fullscreen THEN
  102. viewPort := WMWindowManager.GetDefaultView();
  103. fx := 0; fy := 0; fw := 1; fh := 1; (* full screen on screen number 4 *)
  104. x := fx * viewPort.width0;
  105. y := fy * viewPort.height0;
  106. width := fw* viewPort.width0;
  107. height := fh * viewPort.height0;
  108. END;
  109. IF window = NIL THEN
  110. NEW(window, width, height, alpha);
  111. IF FlagSmoothGraphics IN flags THEN
  112. window.SetCanvasGenerator(WMGraphicsSmooth.GenCanvas);
  113. END;
  114. window.SetTitle(vc.GetName());
  115. window.SetContent(vc);
  116. window.flags := window.flags + flags;
  117. manager := WMWindowManager.GetDefaultManager();
  118. IF (x = InvalidPosition) OR (y = InvalidPosition) THEN
  119. WMWindowManager.GetNextPosition(window, manager, WMWindowManager.GetDefaultView(),x,y);
  120. ELSIF fullscreen THEN
  121. x := 0; y := 0
  122. END;
  123. manager := WMWindowManager.GetDefaultManager();
  124. IF vc.sequencer # NIL THEN vc.sequencer.WaitFree() END;
  125. manager.Add(x, y, window, flags);
  126. ELSE
  127. window.SetContent(vc);
  128. END;
  129. END DoShow;
  130. PROCEDURE DoLoad*(CONST filename: ARRAY OF CHAR; error: Streams.Writer): WMComponents.VisualComponent;
  131. VAR
  132. repositoryName, componentName : ARRAY 128 OF CHAR;
  133. moduleName, procedureName : Modules.Name;
  134. ignoreMsg : ARRAY 1 OF CHAR;
  135. generatorProc : XML.GeneratorProcedure;
  136. c : XML.Content; component : Repositories.Component;
  137. id, res : LONGINT;
  138. BEGIN
  139. IF Repositories.SplitName(filename, repositoryName, componentName, id) & (repositoryName # "") THEN
  140. (* Retrieve component from repository *)
  141. Repositories.GetComponentByString(filename, component, res);
  142. IF (res = Repositories.Ok) THEN
  143. c := component;
  144. ELSIF error # NIL THEN
  145. error.String("Could not load "); error.String(filename);
  146. error.String(" from repository, res: "); error.Int(res, 0); error.Ln;
  147. END;
  148. ELSE
  149. Commands.Split(filename, moduleName, procedureName, res, ignoreMsg);
  150. IF (res = Commands.Ok) THEN
  151. (* Assume argument is a generator procedure *)
  152. GETPROCEDURE(moduleName, procedureName, generatorProc);
  153. IF (generatorProc # NIL) THEN
  154. c := generatorProc();
  155. ELSE
  156. (* Maybe argument is a filename *)
  157. c := WMComponents.Load(filename);
  158. END;
  159. ELSE
  160. (* Load component from XML file *)
  161. c := WMComponents.Load(filename);
  162. END;
  163. END;
  164. IF ( c # NIL ) & (c IS WMComponents.VisualComponent) THEN RETURN c(WMComponents.VisualComponent) ELSE RETURN NIL END;
  165. END DoLoad;
  166. PROCEDURE DoOpen*(CONST filename: ARRAY OF CHAR; error: Streams.Writer; x,y,width, height: LONGINT; client, alpha, fullscreen: BOOLEAN; flags:SET): WMComponents.VisualComponent;
  167. VAR
  168. window : Window;
  169. c : WMComponents.VisualComponent
  170. BEGIN
  171. c := DoLoad(filename, error);
  172. IF (c # NIL) THEN
  173. DoShow(c(WMComponents.VisualComponent), window, x,y,width,height, client, alpha, fullscreen, flags);
  174. ELSIF error # NIL THEN
  175. IF (c = NIL) THEN error.String("Could not load/generate component "); error.String(filename);
  176. ELSE error.String(filename); error.String(" is not a VisualComponent.");
  177. END;
  178. error.Ln;
  179. END;
  180. IF (c # NIL) & (c IS WMComponents.VisualComponent) THEN RETURN c(WMComponents.VisualComponent)
  181. ELSE RETURN NIL
  182. END
  183. END DoOpen;
  184. PROCEDURE SetProperties(c:WMComponents.Component; CONST attr: ARRAY OF CHAR);
  185. VAR property, value: ARRAY 32 OF CHAR; ch: CHAR;
  186. VAR r: Streams.StringReader;
  187. BEGIN
  188. NEW(r, LEN(attr));
  189. r.Set(attr);
  190. WHILE r.GetString(property) & r.GetString(value) DO
  191. IF ~c.properties.SetPropertyValue(property, value) THEN END;
  192. END;
  193. END SetProperties;
  194. PROCEDURE Open*(context : Commands.Context); (** [Options] <RepositoryName:ComponentName:ID> | <ModuleName.ProcedureName> | <Filename> ~ *)
  195. VAR
  196. options : Options.Options;
  197. filename : ARRAY 128 OF CHAR;
  198. x,y, width, height: LONGINT;
  199. flags: SET;
  200. c: WMComponents.Component;
  201. properties: ARRAY 256 OF CHAR;
  202. BEGIN
  203. NEW(options);
  204. options.Add("x", "xPosition", Options.Integer);
  205. options.Add("y", "yPosition", Options.Integer);
  206. options.Add("h", "height", Options.Integer);
  207. options.Add("w", "width", Options.Integer);
  208. options.Add("c", "client", Options.Flag);
  209. options.Add("a","alpha", Options.Flag);
  210. options.Add("f","fullscreen", Options.Flag);
  211. options.Add("n","noFocus", Options.Flag);
  212. options.Add("t","onTop", Options.Flag);
  213. options.Add("F","noFrame",Options.Flag);
  214. options.Add("m","moveable",Options.Flag);
  215. options.Add("s","smoothGraphics",Options.Flag);
  216. options.Add("p","properties",Options.String);
  217. IF options.Parse(context.arg, context.error) & context.arg.GetString(filename) THEN
  218. IF ~options.GetInteger("width",width) THEN width := 0 END;
  219. IF ~options.GetInteger("height",height) THEN height := 0 END;
  220. IF ~options.GetInteger("x",x) THEN x := InvalidPosition END;
  221. IF ~options.GetInteger("y",y) THEN y := InvalidPosition END;
  222. IF options.GetFlag("fullscreen") THEN flags := {} ELSE flags := {WMWindowManager.FlagFrame, WMWindowManager.FlagClose, WMWindowManager.FlagMinimize} END;
  223. IF options.GetFlag("noFrame") THEN flags := {} ELSE flags := {WMWindowManager.FlagFrame, WMWindowManager.FlagClose, WMWindowManager.FlagMinimize} END;
  224. IF options.GetFlag("moveable") THEN flags := {FlagMoveable} ELSE flags := {WMWindowManager.FlagFrame, WMWindowManager.FlagClose, WMWindowManager.FlagMinimize} END;
  225. IF options.GetFlag("noFocus") THEN INCL(flags, WMWindowManager.FlagNoFocus) END;
  226. IF options.GetFlag("onTop") THEN INCL(flags, WMWindowManager.FlagStayOnTop) END;
  227. IF options.GetFlag("smoothGraphics") THEN INCL(flags,FlagSmoothGraphics); END;
  228. c := DoOpen(filename, context.error, x , y, width, height, options.GetFlag("client"), options.GetFlag("alpha"), options.GetFlag("fullscreen"), flags);
  229. IF options.GetString("properties",properties) & (c # NIL) THEN SetProperties(c,properties) END;
  230. ELSE
  231. context.error.String("Usage: ComponentViewer.Open [Options] <string> ~"); context.error.Ln;
  232. END;
  233. END Open;
  234. PROCEDURE Store*(context: Commands.Context);
  235. VAR
  236. filename, name, ext, formName : ARRAY 256 OF CHAR;
  237. form: WMComponents.Component;
  238. id,res: LONGINT;
  239. originator: WMComponents.Component;
  240. parent: XML.Element;
  241. BEGIN{EXCLUSIVE}
  242. context.arg.SkipWhitespace; context.arg.String(filename); D.String(filename); D.Ln;
  243. IF (context # NIL) & (context IS WMComponents.EventContext) THEN
  244. originator := context(WMComponents.EventContext).originator;
  245. parent := originator.GetParent();
  246. WHILE (parent # NIL) & (parent IS WMComponents.Component) & ~(parent IS WMComponents.Form) DO
  247. originator := parent(WMComponents.Component);
  248. parent := originator.GetParent();
  249. END;
  250. END;
  251. form := originator;
  252. (*form := GetForm(current);*)
  253. IF (form # NIL) & (filename # "") THEN
  254. Repositories.CreateRepository(filename,res);
  255. ASSERT(res = Repositories.Ok);
  256. Files.SplitExtension(filename, name, ext);
  257. id:= 1;
  258. COPY(form.GetName()^,formName);
  259. Repositories.PutComponent(form,name,form.GetName()^,id,res);
  260. ASSERT(res = Repositories.Ok);
  261. Repositories.StoreRepository(name,res);
  262. ASSERT(res = Repositories.Ok);
  263. Repositories.UnloadRepository(name,res);
  264. ASSERT(res = Repositories.Ok);
  265. context.out.String("stored component in repository "); context.out.String(filename); context.out.Ln;
  266. END;
  267. FINALLY
  268. END Store;
  269. PROCEDURE Restore*(context : WMRestorable.Context);
  270. VAR w : Window;
  271. BEGIN
  272. IF context # NIL THEN
  273. NEW(w, 100,100,FALSE);
  274. w.RestoreWindow(context);
  275. END;
  276. END Restore;
  277. PROCEDURE IncCount;
  278. BEGIN {EXCLUSIVE}
  279. INC(nofWindows)
  280. END IncCount;
  281. PROCEDURE DecCount;
  282. BEGIN {EXCLUSIVE}
  283. DEC(nofWindows)
  284. END DecCount;
  285. PROCEDURE Cleanup;
  286. VAR
  287. die : KillerMsg;
  288. msg : WMMessages.Message;
  289. m : WMWindowManager.WindowManager;
  290. BEGIN {EXCLUSIVE}
  291. NEW(die);
  292. msg.ext := die;
  293. msg.msgType := WMMessages.MsgExt;
  294. m := WMWindowManager.GetDefaultManager();
  295. m.Broadcast(msg);
  296. (*AWAIT(nofWindows = 0)*)
  297. END Cleanup;
  298. BEGIN
  299. nofWindows := 0;
  300. Modules.InstallTermHandler(Cleanup)
  301. END ComponentViewer.
  302. SystemTools.FreeDownTo ComponentViewer ~
  303. ComponentViewer.Open FractalDemo.XML ~
  304. ComponentViewer.Open ComponentHelper:Panel:1 ~
  305. ComponentViewer.Open --moveable --width=100 --height=100 WMStandardComponents.GenButton ~