ComponentViewer.Mod 9.9 KB

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