ComponentViewer.Mod 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, 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 : 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 vc = NIL THEN RETURN END;
  89. IF width # 0 THEN
  90. vc.bounds.SetWidth(width);
  91. ELSE
  92. width := vc.bounds.GetWidth();
  93. IF (width <= 0) THEN width := DefaultWidth; vc.bounds.SetWidth(width) END;
  94. END;
  95. IF height # 0 THEN
  96. vc.bounds.SetHeight(height);
  97. ELSE
  98. height := vc.bounds.GetHeight();
  99. IF (height <= 0) THEN height := DefaultHeight; vc.bounds.SetHeight(height) END;
  100. END;
  101. IF client THEN vc.alignment.Set(WMComponents.AlignClient) END;
  102. IF fullscreen THEN
  103. viewPort := WMWindowManager.GetDefaultView();
  104. fx := 0; fy := 0; fw := 1; fh := 1; (* full screen on screen number 4 *)
  105. x := fx * viewPort.width0;
  106. y := fy * viewPort.height0;
  107. width := fw* viewPort.width0;
  108. height := fh * viewPort.height0;
  109. END;
  110. IF window = NIL THEN
  111. NEW(window, width, height, alpha);
  112. IF FlagSmoothGraphics IN flags THEN
  113. window.SetCanvasGenerator(WMGraphicsSmooth.GenCanvas);
  114. END;
  115. window.SetTitle(vc.GetName());
  116. window.SetContent(vc);
  117. window.flags := window.flags + flags;
  118. manager := WMWindowManager.GetDefaultManager();
  119. IF (x = InvalidPosition) OR (y = InvalidPosition) THEN
  120. WMWindowManager.GetNextPosition(window, manager, WMWindowManager.GetDefaultView(),x,y);
  121. ELSIF fullscreen THEN
  122. x := 0; y := 0
  123. END;
  124. manager := WMWindowManager.GetDefaultManager();
  125. IF vc.sequencer # NIL THEN vc.sequencer.WaitFree() END;
  126. manager.Add(x, y, window, flags);
  127. ELSE
  128. window.SetContent(vc);
  129. END;
  130. END DoShow;
  131. PROCEDURE DoLoad*(CONST filename: ARRAY OF CHAR; error: Streams.Writer): WMComponents.VisualComponent;
  132. VAR
  133. repositoryName, componentName : ARRAY 128 OF CHAR;
  134. moduleName, procedureName : Modules.Name;
  135. ignoreMsg : ARRAY 1 OF CHAR;
  136. generatorProc : XML.GeneratorProcedure;
  137. c : XML.Content; component : Repositories.Component;
  138. id: LONGINT; res: WORD;
  139. BEGIN
  140. IF Repositories.SplitName(filename, repositoryName, componentName, id) & (repositoryName # "") THEN
  141. (* Retrieve component from repository *)
  142. Repositories.GetComponentByString(filename, component, res);
  143. IF (res = Repositories.Ok) THEN
  144. c := component;
  145. ELSIF error # NIL THEN
  146. error.String("Could not load "); error.String(filename);
  147. error.String(" from repository, res: "); error.Int(res, 0); error.Ln;
  148. END;
  149. ELSE
  150. Commands.Split(filename, moduleName, procedureName, res, ignoreMsg);
  151. IF (res = Commands.Ok) THEN
  152. (* Assume argument is a generator procedure *)
  153. GETPROCEDURE(moduleName, procedureName, generatorProc);
  154. IF (generatorProc # NIL) THEN
  155. c := generatorProc();
  156. ELSE
  157. (* Maybe argument is a filename *)
  158. c := WMComponents.Load(filename);
  159. END;
  160. ELSE
  161. (* Load component from XML file *)
  162. c := WMComponents.Load(filename);
  163. END;
  164. END;
  165. IF ( c # NIL ) & (c IS WMComponents.VisualComponent) THEN RETURN c(WMComponents.VisualComponent) ELSE RETURN NIL END;
  166. END DoLoad;
  167. PROCEDURE DoOpen*(CONST filename: ARRAY OF CHAR; error: Streams.Writer; x,y,width, height: LONGINT; client, alpha, fullscreen: BOOLEAN; flags:SET): WMComponents.VisualComponent;
  168. VAR
  169. window : Window;
  170. c : WMComponents.VisualComponent
  171. BEGIN
  172. c := DoLoad(filename, error);
  173. IF (c # NIL) THEN
  174. DoShow(c(WMComponents.VisualComponent), window, x,y,width,height, client, alpha, fullscreen, flags);
  175. ELSIF error # NIL THEN
  176. IF (c = NIL) THEN error.String("Could not load/generate component "); error.String(filename);
  177. ELSE error.String(filename); error.String(" is not a VisualComponent.");
  178. END;
  179. error.Ln;
  180. END;
  181. IF (c # NIL) & (c IS WMComponents.VisualComponent) THEN RETURN c(WMComponents.VisualComponent)
  182. ELSE RETURN NIL
  183. END
  184. END DoOpen;
  185. PROCEDURE SetProperties(c:WMComponents.Component; CONST attr: ARRAY OF CHAR);
  186. VAR property, value: ARRAY 32 OF CHAR;
  187. VAR r: Streams.StringReader;
  188. BEGIN
  189. NEW(r, LEN(attr));
  190. r.Set(attr);
  191. WHILE r.GetString(property) & r.GetString(value) DO
  192. IF ~c.properties.SetPropertyValue(property, value) THEN END;
  193. END;
  194. END SetProperties;
  195. PROCEDURE Open*(context : Commands.Context); (** [Options] <RepositoryName:ComponentName:ID> | <ModuleName.ProcedureName> | <Filename> ~ *)
  196. VAR
  197. options : Options.Options;
  198. filename : ARRAY 128 OF CHAR;
  199. x,y, width, height: LONGINT;
  200. flags: SET;
  201. c: WMComponents.Component;
  202. properties: ARRAY 256 OF CHAR;
  203. BEGIN
  204. NEW(options);
  205. options.Add("x", "xPosition", Options.Integer);
  206. options.Add("y", "yPosition", Options.Integer);
  207. options.Add("h", "height", Options.Integer);
  208. options.Add("w", "width", Options.Integer);
  209. options.Add("c", "client", Options.Flag);
  210. options.Add("a","alpha", Options.Flag);
  211. options.Add("f","fullscreen", Options.Flag);
  212. options.Add("n","noFocus", Options.Flag);
  213. options.Add("t","onTop", Options.Flag);
  214. options.Add("F","noFrame",Options.Flag);
  215. options.Add("m","moveable",Options.Flag);
  216. options.Add("s","smoothGraphics",Options.Flag);
  217. options.Add("p","properties",Options.String);
  218. IF options.Parse(context.arg, context.error) & context.arg.GetString(filename) THEN
  219. IF ~options.GetInteger("width",width) THEN width := 0 END;
  220. IF ~options.GetInteger("height",height) THEN height := 0 END;
  221. IF ~options.GetInteger("x",x) THEN x := InvalidPosition END;
  222. IF ~options.GetInteger("y",y) THEN y := InvalidPosition END;
  223. IF options.GetFlag("fullscreen") THEN flags := {} ELSE flags := {WMWindowManager.FlagFrame, WMWindowManager.FlagClose, WMWindowManager.FlagMinimize} END;
  224. IF options.GetFlag("noFrame") THEN flags := {} ELSE flags := {WMWindowManager.FlagFrame, WMWindowManager.FlagClose, WMWindowManager.FlagMinimize} END;
  225. IF options.GetFlag("moveable") THEN flags := {FlagMoveable} ELSE flags := {WMWindowManager.FlagFrame, WMWindowManager.FlagClose, WMWindowManager.FlagMinimize} END;
  226. IF options.GetFlag("noFocus") THEN INCL(flags, WMWindowManager.FlagNoFocus) END;
  227. IF options.GetFlag("onTop") THEN INCL(flags, WMWindowManager.FlagStayOnTop) END;
  228. IF options.GetFlag("smoothGraphics") THEN INCL(flags,FlagSmoothGraphics); END;
  229. c := DoOpen(filename, context.error, x , y, width, height, options.GetFlag("client"), options.GetFlag("alpha"), options.GetFlag("fullscreen"), flags);
  230. IF options.GetString("properties",properties) & (c # NIL) THEN SetProperties(c,properties) END;
  231. ELSE
  232. context.error.String("Usage: ComponentViewer.Open [Options] <string> ~"); context.error.Ln;
  233. END;
  234. END Open;
  235. PROCEDURE Store*(context: Commands.Context);
  236. VAR
  237. filename, name, ext, formName : ARRAY 256 OF CHAR;
  238. form: WMComponents.Component;
  239. id: LONGINT; res: WORD;
  240. originator: WMComponents.Component;
  241. parent: XML.Element;
  242. BEGIN{EXCLUSIVE}
  243. context.arg.SkipWhitespace; context.arg.String(filename); D.String(filename); D.Ln;
  244. IF (context # NIL) & (context IS WMComponents.EventContext) THEN
  245. originator := context(WMComponents.EventContext).originator;
  246. parent := originator.GetParent();
  247. WHILE (parent # NIL) & (parent IS WMComponents.Component) & ~(parent IS WMComponents.Form) DO
  248. originator := parent(WMComponents.Component);
  249. parent := originator.GetParent();
  250. END;
  251. END;
  252. form := originator;
  253. (*form := GetForm(current);*)
  254. IF (form # NIL) & (filename # "") THEN
  255. Repositories.CreateRepository(filename,res);
  256. ASSERT(res = Repositories.Ok);
  257. Files.SplitExtension(filename, name, ext);
  258. id:= 1;
  259. COPY(form.GetName()^,formName);
  260. Repositories.PutComponent(form,name,form.GetName()^,id,res);
  261. ASSERT(res = Repositories.Ok);
  262. Repositories.StoreRepository(name,res);
  263. ASSERT(res = Repositories.Ok);
  264. Repositories.UnloadRepository(name,res);
  265. ASSERT(res = Repositories.Ok);
  266. context.out.String("stored component in repository "); context.out.String(filename); context.out.Ln;
  267. END;
  268. FINALLY
  269. END Store;
  270. PROCEDURE Restore*(context : WMRestorable.Context);
  271. VAR w : Window;
  272. BEGIN
  273. IF context # NIL THEN
  274. NEW(w, 100,100,FALSE);
  275. w.RestoreWindow(context);
  276. END;
  277. END Restore;
  278. PROCEDURE IncCount;
  279. BEGIN {EXCLUSIVE}
  280. INC(nofWindows)
  281. END IncCount;
  282. PROCEDURE DecCount;
  283. BEGIN {EXCLUSIVE}
  284. DEC(nofWindows)
  285. END DecCount;
  286. PROCEDURE Cleanup;
  287. VAR
  288. die : KillerMsg;
  289. msg : WMMessages.Message;
  290. m : WMWindowManager.WindowManager;
  291. BEGIN {EXCLUSIVE}
  292. NEW(die);
  293. msg.ext := die;
  294. msg.msgType := WMMessages.MsgExt;
  295. m := WMWindowManager.GetDefaultManager();
  296. m.Broadcast(msg);
  297. (*AWAIT(nofWindows = 0)*)
  298. END Cleanup;
  299. BEGIN
  300. nofWindows := 0;
  301. Modules.InstallTermHandler(Cleanup)
  302. END ComponentViewer.
  303. System.FreeDownTo ComponentViewer ~
  304. ComponentViewer.Open FractalDemo.XML ~
  305. ComponentViewer.Open ComponentHelper:Panel:1 ~
  306. ComponentViewer.Open --moveable --width=100 --height=100 WMStandardComponents.GenButton ~