WMWindowManager.Mod 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. MODULE WMWindowManager; (** AUTHOR "TF"; PURPOSE "Generic window manager"; *)
  2. IMPORT
  3. Modules, KernelLog, Plugins, Locks, Strings, Messages := WMMessages, Graphics := WMGraphics, Raster, Rectangles := WMRectangles;
  4. CONST
  5. FlagFrame* = 0; (** The window has a frame *)
  6. FlagClose* = 1; (** The window offers a close button; only frame windows *)
  7. FlagMinimize* = 2; (** The window offers a minimize button; only frame windows *)
  8. FlagStayOnTop* = 3; (** The window will always stay above all non stay on top windows *)
  9. FlagNonDispatched* = 4; (** The window has no message queue --> BE CAREFUL *)
  10. FlagNoFocus* = 5; (** The window can never get the keyboard focus *)
  11. FlagDecorWindow* = 6; (** The window is a decor window, associated to a master window *)
  12. FlagStayOnBottom* = 7; (** The window can not be moved up *)
  13. FlagNavigation* = 8; (** The window will always appear at the same position/size on the screen independent of the range of the viewport displaying it. *)
  14. FlagHidden* = 9; (** This flag indicates whether a window should be managed by window navigation tools. It does not influence the visibiliy or behaviour of the window *)
  15. FlagNoResizing* = 10; (** If set, window resizing is disabled *)
  16. FlagNoPointer*=11; (* if set, in window there will be no pointer visible --> touch screens *)
  17. FlagStorable*=12; (* window storable*)
  18. SizeMinHeight = 3; (* Minimum height of a window *)
  19. SizeMinWidth = 3; (* Minimum width of a window *)
  20. (* result codes for Window.OpenDocument *)
  21. Ok* = 0;
  22. Error* = 1;
  23. NotSupported* = 2;
  24. (* Window position for new windows added *)
  25. X0 = 30;
  26. Y0 = 80;
  27. TYPE
  28. Rectangle = Rectangles.Rectangle;
  29. String = Strings.String;
  30. Message = Messages.Message;
  31. RealRect* = RECORD l*, t*, r*, b* : REAL END;
  32. PointerInfo* = OBJECT
  33. VAR hotX*, hotY* : LONGINT; img* : Graphics.Image;
  34. END PointerInfo;
  35. (** All fields are read-only except for the module SkinEngine.Mod!
  36. Modifications of the instance that is owned by the window manager require
  37. the window manager write lock being held!
  38. Most of the information is used by WMDefaultWindows.Mod *)
  39. WindowStyle* = OBJECT
  40. VAR
  41. (** use bitmaps for frame windows? *)
  42. useBitmaps* : BOOLEAN;
  43. (** frame color if not using bitmaps (a = active, i = inactive) *)
  44. baCol*, biCol* : LONGINT;
  45. (** frame shading width if not using bitmaps (a = active, i = inactive) *)
  46. basw*, bisw* : LONGINT;
  47. (** height / width of frame windows th = top height, bh = bottom height, lw = left width, rw = right width *)
  48. th*, bh*, lw*, rw* : LONGINT;
  49. (** frame window bitmaps
  50. 1st letter: t = top, l = left, r = right, b = bottom decor window
  51. 2nd letter: a = active, i = inactive
  52. 3rd letter: a = left bitmap, b = middle bitmap (repeated), c = right bitmap
  53. *)
  54. taa*, tab*, tac*, tia*, tib*, tic*,
  55. laa*, lab*, lac*, lia*, lib*, lic*,
  56. raa*, rab*, rac*, ria*, rib*, ric*,
  57. baa*, bab*, bac*, bia*, bib*, bic* : Graphics.Image;
  58. (** Close and minimize button images (a = active, i = inactive) *)
  59. ca*, ci*, closeHover*,
  60. ma*, mi*, minimizeHover* : Graphics.Image;
  61. minimizeOffset* : LONGINT; (** offset correction for minimize button *)
  62. (** Window title position and color (a = active, i = inactive) *)
  63. atextX*, atextY*, atextColor*, itextX*, itextY*, itextColor* : LONGINT;
  64. bgColor*, fgColor*, selectCol*, desktopColor* : Graphics.Color;
  65. topFocusThreshold*, topThreshold*, bottomFocusThreshold*, bottomThreshold*,
  66. leftFocusThreshold*, leftThreshold*, rightFocusThreshold*, rightThreshold* : LONGINT;
  67. (* Initialize/reset to zero-style for windows *)
  68. PROCEDURE &Init*;
  69. BEGIN
  70. useBitmaps := FALSE;
  71. baCol := 0FFFFH; biCol := 0FF40H;
  72. basw := 4; bisw := 3;
  73. th := 20; bh := 3; lw := 3; rw := 3;
  74. (* images *)
  75. taa := NIL; tab := NIL; tac := NIL; tia := NIL; tib := NIL; tic := NIL;
  76. laa := NIL; lab := NIL; lac := NIL; lia := NIL; lib := NIL; lic := NIL;
  77. raa := NIL; rab := NIL; rac := NIL; ria := NIL; rib := NIL; ric := NIL;
  78. baa := NIL; bab := NIL; bac := NIL; bia := NIL; bib := NIL; bic := NIL;
  79. ca := Graphics.LoadImage("ZeroSkin.zip://aclose.png", TRUE);
  80. ci := Graphics.LoadImage("ZeroSkin.zip://iclose.png", TRUE);
  81. closeHover := NIL;
  82. ma := NIL; mi := NIL; minimizeHover := NIL;
  83. minimizeOffset := 0;
  84. (* window caption *)
  85. atextX := 5; atextY := 15; atextColor := LONGINT(0FFFF00FFH);
  86. itextX := 5; itextY := 15; itextColor := 04444FFH;
  87. (* desktop *)
  88. bgColor := LONGINT(08080FFFFH);
  89. fgColor := 0000000FFH;
  90. selectCol := 0FFFFH;
  91. desktopColor := LONGINT(08080FFFFH);
  92. topFocusThreshold := 0; topThreshold := 0;
  93. bottomFocusThreshold := 0; bottomThreshold := 0;
  94. leftFocusThreshold := 0; leftThreshold := 0;
  95. rightFocusThreshold := 0; rightThreshold := 0;
  96. END Init;
  97. (** calculate distances from images *)
  98. PROCEDURE Initialize*;
  99. BEGIN
  100. IF useBitmaps THEN
  101. IF tab # NIL THEN th := tab.height END;
  102. IF bab # NIL THEN bh := bab.height END;
  103. IF lab # NIL THEN lw := lab.width END;
  104. IF rab # NIL THEN rw := rab.width END;
  105. END
  106. END Initialize;
  107. END WindowStyle;
  108. DragInfo* = OBJECT
  109. VAR
  110. data*, sender* : ANY;
  111. onAccept*, onReject* : Messages.CompCommand;
  112. offsetX*, offsetY*: LONGINT;
  113. END DragInfo;
  114. (** List of decoration - windows to a master window *)
  115. DecorList* = OBJECT
  116. VAR next* : DecorList;
  117. w* : Window;
  118. END DecorList;
  119. (** A message preview procedure can set discard to TRUE to discard the message *)
  120. MessagePreviewProc* = PROCEDURE (VAR msg : Message; VAR discard : BOOLEAN);
  121. MessagePreviewList* = OBJECT
  122. VAR proc*: MessagePreviewProc;
  123. next*:MessagePreviewList;
  124. END MessagePreviewList;
  125. DocumentInfo* = RECORD
  126. id* : LONGINT;
  127. name* : ARRAY 32 OF CHAR; (* if name = "", this document info is not valid *)
  128. fullname* : ARRAY 256 OF CHAR;
  129. modified*, hasFocus* : BOOLEAN;
  130. END;
  131. VisualComponentInfo* = RECORD
  132. width*, height* : LONGINT; (* preferred width and height of visual component *)
  133. generator* : PROCEDURE {DELEGATE} () : ANY; (* NIL if no visual component available *)
  134. END;
  135. (** A window may provide information about currently opened documents. Additionally, it can provide a visual component that controls it. *)
  136. WindowInfo* = RECORD
  137. openDocuments* : ARRAY 16 OF DocumentInfo;
  138. handleDocumentInfo* : PROCEDURE {DELEGATE} (CONST info : DocumentInfo; new : BOOLEAN; VAR res : LONGINT);
  139. vc* : VisualComponentInfo;
  140. END;
  141. WindowInfoPtr = POINTER TO WindowInfo;
  142. Window* = OBJECT
  143. VAR
  144. id- : LONGINT;
  145. timestamp* : LONGINT; (* incremented at each call of procedure Draw *)
  146. (** Ranges in global coordinates *)
  147. bounds* : Rectangle; (** current range *)
  148. initialBounds* : Rectangle; (** range at window creation *)
  149. normalBounds* : Rectangle; (** range before toggling to fullscreen *)
  150. manager* : WindowManager;
  151. sequencer* : Messages.MsgSequencer;
  152. (** window state that may only be accessed by the window manager *)
  153. prev*, next* : Window; (** previous and next window in z order *)
  154. title : String; (* window title *)
  155. info* : WindowInfoPtr;
  156. master* : Window; (** is only set if the window is a decor window *)
  157. view* : ViewPort;
  158. decor* : DecorList;
  159. flags* : SET;
  160. icon* : Graphics.Image; (** Optional icon for Window *)
  161. topW*, bottomW*, leftW*, rightW* : Window; (** Optional decor windows *)
  162. useAlpha* : BOOLEAN;
  163. isVisible* : BOOLEAN;
  164. pointerInfo- : PointerInfo;
  165. acceptDrag : BOOLEAN;
  166. reduceQuality- : BOOLEAN;
  167. PROCEDURE &Init*(w, h : LONGINT; alpha : BOOLEAN);
  168. BEGIN
  169. id := GetId();
  170. timestamp := 0;
  171. bounds := Graphics.MakeRectangle(0, 0, w, h);
  172. initialBounds := bounds;
  173. normalBounds := bounds;
  174. manager := NIL; sequencer := NIL;
  175. prev := NIL; next := NIL;
  176. title := NIL;
  177. info := NIL;
  178. master := NIL; decor := NIL;
  179. view := NIL;
  180. flags := {};
  181. icon := NIL;
  182. topW := NIL; bottomW := NIL; leftW := NIL; rightW := NIL;
  183. useAlpha := alpha;
  184. isVisible := TRUE;
  185. pointerInfo := NIL;
  186. acceptDrag := FALSE;
  187. reduceQuality := FALSE;
  188. END Init;
  189. PROCEDURE IsCallFromSequencer*() : BOOLEAN;
  190. BEGIN
  191. RETURN (sequencer # NIL) & (sequencer.IsCallFromSequencer())
  192. END IsCallFromSequencer;
  193. (** Return the window manager that handles the window *)
  194. PROCEDURE GetManager*() : WindowManager;
  195. BEGIN
  196. RETURN manager
  197. END GetManager;
  198. (** Set the window title as UTF8 string. *)
  199. PROCEDURE SetTitle*(title : String);
  200. BEGIN
  201. IF manager # NIL THEN manager.SetWindowTitle(SELF, title) ELSE SELF.title := title END
  202. END SetTitle;
  203. (** Return the title as UTF8 string. Returns NIL if no title is set *)
  204. PROCEDURE GetTitle*() : String;
  205. BEGIN
  206. IF manager # NIL THEN RETURN manager.GetWindowTitle(SELF) ELSE RETURN title END
  207. END GetTitle;
  208. PROCEDURE SetIcon*(icon : Graphics.Image);
  209. BEGIN
  210. IF (manager # NIL) THEN manager.SetWindowIcon(SELF, icon); ELSE SELF.icon := icon; END;
  211. END SetIcon;
  212. (** Return the height in client space *) (* go via manager *)
  213. PROCEDURE GetHeight*() : LONGINT;
  214. BEGIN
  215. RETURN bounds.b - bounds.t
  216. END GetHeight;
  217. (** Return the width in client space *) (* go via manager *)
  218. PROCEDURE GetWidth*() : LONGINT;
  219. BEGIN
  220. RETURN bounds.r - bounds.l
  221. END GetWidth;
  222. PROCEDURE SetInfo*(CONST info : WindowInfo);
  223. BEGIN
  224. IF (manager # NIL) THEN
  225. manager.SetWindowInfo(SELF, info);
  226. ELSE
  227. IF (SELF.info = NIL) THEN NEW(SELF.info); END;
  228. SELF.info^ := info;
  229. END;
  230. END SetInfo;
  231. PROCEDURE GetInfo*(VAR info : WindowInfo) : BOOLEAN;
  232. VAR infoPtr : WindowInfoPtr;
  233. BEGIN
  234. IF (manager # NIL) THEN
  235. RETURN manager.GetWindowInfo(SELF, info);
  236. ELSE
  237. infoPtr := SELF.info;
  238. IF (infoPtr # NIL) THEN
  239. info := infoPtr^;
  240. END;
  241. RETURN (infoPtr # NIL);
  242. END;
  243. END GetInfo;
  244. (** Resize is called by the WM if it wants to resize the window.
  245. width and height contain the desired new size. The Window should set width and height to acceptable
  246. values or return the current size, if resize is not supported *)
  247. PROCEDURE Resizing*(VAR width, height : LONGINT);
  248. BEGIN
  249. IF FlagNoResizing IN flags THEN
  250. width := GetWidth(); height := GetHeight();
  251. ELSIF width < SizeMinWidth THEN
  252. width := GetWidth();
  253. ELSIF height < SizeMinHeight THEN
  254. height := GetHeight();
  255. END
  256. END Resizing;
  257. (** May replace the back-image, if needed. MUST check if requested size is reasonable (0 < x * y < memory) *)
  258. PROCEDURE Resized*(width, height : LONGINT);
  259. END Resized;
  260. (** Invalidate a rectangle in window coordinates*)
  261. PROCEDURE Invalidate*(rect : Rectangle);
  262. BEGIN
  263. Rectangles.MoveRel(rect, bounds.l, bounds.t);
  264. Rectangles.ClipRect(rect, bounds);
  265. IF manager # NIL THEN manager.AddVisibleDirty(SELF, rect) END
  266. END Invalidate;
  267. (** Message procedures *)
  268. (** Pointer Messages *)
  269. (** PointerDown is called via the generic message handler if the pointer (or a mouse button) is pressed down and
  270. a) the pointer is in the bounding box of the window AND IsHit returns TRUE for this position
  271. or
  272. b) another mouse button was pressed down on a position where a) was met and has not yet been released.
  273. x and y are in window coordinates but may lie out of the window boundaries in case b)
  274. keys is the set of buttons that are down
  275. *)
  276. PROCEDURE PointerDown*(x, y : LONGINT; keys : SET);
  277. END PointerDown;
  278. (** PointerMove is called via the generic message handler if the pointer (mouse) is moved and
  279. a) the pointer is in the bounding box of the window AND IsHit returns TRUE for this position
  280. or
  281. b) the pointer was pressed down on a position where a) was met and has not yet been released.
  282. x and y are in window coordinates but may lie out of the window boundaries.
  283. keys is the set of buttons that are down
  284. *)
  285. PROCEDURE PointerMove*(x, y : LONGINT; keys : SET);
  286. END PointerMove;
  287. PROCEDURE WheelMove*(dz : LONGINT);
  288. END WheelMove;
  289. (** PointerUp is called via the generic message handler if the pointer (or a mouse button) went up.
  290. x and y are in window coordinates but may lie out of the window boundaries.
  291. keys is the set of buttons that are STILL DOWN
  292. *)
  293. PROCEDURE PointerUp*(x, y : LONGINT; keys : SET);
  294. END PointerUp;
  295. (** PointerLeave is called via the generic message handler if the pointer has left the window with no button pressed. *)
  296. PROCEDURE PointerLeave*;
  297. END PointerLeave;
  298. (** DragOver is called via the message handler. *)
  299. PROCEDURE DragOver*(x, y: LONGINT; dragInfo : DragInfo);
  300. END DragOver;
  301. (** Dropped is called via the message handler to indicate an item has been dropped. *)
  302. PROCEDURE DragDropped*(x, y: LONGINT; dragInfo : DragInfo);
  303. END DragDropped;
  304. (** send the srcWindow a confirmation for the completed drag operation *)
  305. PROCEDURE ConfirmDrag*(accept : BOOLEAN; dragInfo : DragInfo);
  306. BEGIN
  307. IF dragInfo # NIL THEN
  308. IF accept THEN
  309. IF dragInfo.onAccept # NIL THEN dragInfo.onAccept(SELF, dragInfo) END
  310. ELSE
  311. IF dragInfo.onReject # NIL THEN dragInfo.onReject(SELF, dragInfo) END
  312. END
  313. END
  314. END ConfirmDrag;
  315. (** Start a drag operation. *)
  316. PROCEDURE StartDrag*(sender, data : ANY; img : Graphics.Image; offsetX, offsetY: LONGINT; onAccept, onReject : Messages.CompCommand) : BOOLEAN;
  317. BEGIN
  318. RETURN manager.StartDrag(SELF, sender, data, img, offsetX, offsetY, onAccept, onReject)
  319. END StartDrag;
  320. (** Keyboard message *)
  321. (** KeyEvent is called via the generic message handler to signal a keyboard event.
  322. The window can determine wheter the key was pressed or released by examining the
  323. Inputs.Release flag in flags. ucs contains the unicode equivalent of the key. Special input editors
  324. send the generated unicode characters via KeyEvent. *)
  325. PROCEDURE KeyEvent*(ucs : LONGINT; flags : SET; keysym : LONGINT);
  326. END KeyEvent;
  327. (** Focus messages *)
  328. (** FocusGot is called via the generic message handler if the keyboard focus is transfered to this window *)
  329. PROCEDURE FocusGot*;
  330. END FocusGot;
  331. (** FocusList is called via the generic message handler if the keyboard focus is transfered to some other window *)
  332. PROCEDURE FocusLost*;
  333. END FocusLost;
  334. (** Style *)
  335. (** StyleChanged is called via the generic message handler if a change in the global style occurs. The
  336. Window should read all the style information it relies on and redraw itself *)
  337. PROCEDURE StyleChanged*;
  338. END StyleChanged;
  339. (** Closing *)
  340. PROCEDURE CanClose*() : BOOLEAN;
  341. BEGIN
  342. RETURN TRUE
  343. END CanClose;
  344. (** Close is called via the generic message handler. *)
  345. PROCEDURE Close*;
  346. BEGIN
  347. IF manager # NIL THEN manager.Remove(SELF) END;
  348. END Close;
  349. (** Return true if the window is hit at the coordinates x and y (in window coordinates). Use
  350. this to generate non-rectangular windows.
  351. This Method will be called directly by the window manager. __> Don't block, don't crash !!
  352. *)
  353. PROCEDURE IsHit*(x, y : LONGINT) : BOOLEAN;
  354. BEGIN
  355. RETURN TRUE
  356. END IsHit;
  357. PROCEDURE SetPointerInfo*(pi : PointerInfo);
  358. BEGIN
  359. IF FlagNoPointer IN flags THEN pi := pointerNull END;
  360. IF pi # pointerInfo THEN
  361. pointerInfo := pi;
  362. IF manager # NIL THEN manager.CheckPointerImage END;
  363. END
  364. END SetPointerInfo;
  365. (** Generic message handler distributes messages to the different msg-handler methods *)
  366. PROCEDURE Handle*(VAR m : Message);
  367. BEGIN
  368. IF m.msgType = Messages.MsgKey THEN
  369. KeyEvent(m.x, m.flags, m.y)
  370. ELSIF m.msgType = Messages.MsgPointer THEN
  371. (* global to local transformation by sequencer thread: *)
  372. m.x := m.x-bounds.l;
  373. m.y := m.y-bounds.t;
  374. IF m.msgSubType = Messages.MsgSubPointerMove THEN
  375. IF (m.dz # 0) THEN WheelMove(m.dz) END;
  376. PointerMove(m.x, m.y, m.flags)
  377. ELSIF m.msgSubType = Messages.MsgSubPointerDown THEN PointerDown(m.x, m.y, m.flags)
  378. ELSIF m.msgSubType = Messages.MsgSubPointerUp THEN PointerUp(m.x, m.y, m.flags)
  379. ELSIF m.msgSubType = Messages.MsgSubPointerLeave THEN PointerLeave
  380. END
  381. ELSIF m.msgType = Messages.MsgDrag THEN
  382. IF m.msgSubType = Messages.MsgDragOver THEN
  383. IF (m.ext # NIL) THEN
  384. DragOver(m.x, m.y, m.ext(DragInfo))
  385. END
  386. ELSIF m.msgSubType = Messages.MsgDragDropped THEN
  387. IF (m.ext # NIL) THEN
  388. DragDropped(m.x, m.y, m.ext(DragInfo))
  389. END
  390. END
  391. ELSIF m.msgType = Messages.MsgClose THEN Close
  392. ELSIF m.msgType = Messages.MsgFocus THEN
  393. IF m.msgSubType = Messages.MsgSubFocusGot THEN FocusGot
  394. ELSIF m.msgSubType = Messages.MsgSubFocusLost THEN FocusLost
  395. END
  396. ELSIF m.msgType = Messages.MsgStyleChanged THEN StyleChanged
  397. ELSIF m.msgType = Messages.MsgResized THEN Resized(m.x, m.y)
  398. END;
  399. END Handle;
  400. (** Draw request form the window manager. The canvas becomes invalid when the method ends. The
  401. draw method may not modify window or WindowManager properties.
  402. w, h is the area in view coordinates, q is the Quality 0 lowest 1 mid 2 high. A window may ignore q *)
  403. PROCEDURE Draw*(canvas : Graphics.Canvas; w, h, q : LONGINT);
  404. END Draw;
  405. (** Is called by the windowmanager with reduce set, if the window is resized or moved on slow machines *)
  406. PROCEDURE HintReduceQuality*(reduce : BOOLEAN);
  407. BEGIN
  408. IF reduce # reduceQuality THEN
  409. reduceQuality := reduce;
  410. IF ~reduceQuality THEN
  411. IF manager # NIL THEN manager.AddVisibleDirty(SELF, bounds) END
  412. END
  413. END
  414. END HintReduceQuality;
  415. END Window;
  416. (** assumes the window is size agnostic, handles all the zooming issues directly *)
  417. BufferWindow* = OBJECT(Window)
  418. VAR
  419. img* : Graphics.Image;
  420. canvas* : Graphics.BufferCanvas;
  421. canvasGen-: Graphics.CanvasGenerator;
  422. pointerThreshold*,
  423. maxInterpolation* : LONGINT; (* allows limiting the interpolation degree on Draw *)
  424. PROCEDURE &Init*(w, h : LONGINT; alpha : BOOLEAN);
  425. BEGIN
  426. Init^(w, h, alpha);
  427. NEW(img);
  428. IF alpha THEN Raster.Create(img, w, h, Raster.BGRA8888) ELSE Raster.Create(img, w, h, format) END;
  429. SetCanvasGenerator(Graphics.GenCanvas);
  430. pointerThreshold := 1; (* invisible pixels are treated as invisible *)
  431. maxInterpolation := Graphics.ScaleBilinear;
  432. END Init;
  433. PROCEDURE SetCanvasGenerator*(canvasGen:Graphics.CanvasGenerator);
  434. BEGIN{EXCLUSIVE}
  435. SELF.canvasGen:=canvasGen; IF img # NIL THEN canvas:=canvasGen(img); END;
  436. IF manager # NIL THEN manager.AddVisibleDirty(SELF, bounds) END
  437. END SetCanvasGenerator;
  438. PROCEDURE IsHit(x, y : LONGINT) : BOOLEAN;
  439. VAR w, h : LONGINT; fx, fy : REAL;
  440. BEGIN
  441. w := GetWidth(); h := GetHeight();
  442. IF (w > 0) & (h > 0) & ((w # img.width) OR (h # img.height)) THEN
  443. fx := img.width / w; fy := img.height / h;
  444. RETURN Graphics.IsBitmapHit(ENTIER(x * fx), ENTIER(y * fy), pointerThreshold, img)
  445. ELSE RETURN Graphics.IsBitmapHit(x, y, pointerThreshold, img)
  446. END
  447. END IsHit;
  448. PROCEDURE Draw*(canvas : Graphics.Canvas; w, h, q : LONGINT);
  449. BEGIN
  450. IF reduceQuality THEN q := 0 END;
  451. IF img # NIL THEN
  452. IF (w = img.width) & (h = img.height) THEN
  453. IF useAlpha THEN canvas.DrawImage(0, 0, img, Graphics.ModeSrcOverDst)
  454. ELSE canvas.DrawImage(0, 0, img, Graphics.ModeCopy)
  455. END
  456. ELSE
  457. IF useAlpha THEN
  458. canvas.ScaleImage(img, Rectangles.MakeRect(0, 0, img.width, img.height),
  459. Rectangles.MakeRect(0, 0, w, h), Graphics.ModeSrcOverDst, MIN( q,maxInterpolation))
  460. ELSE
  461. canvas.ScaleImage(img, Rectangles.MakeRect(0, 0, img.width, img.height),
  462. Rectangles.MakeRect(0, 0, w, h), Graphics.ModeCopy, MIN(q,maxInterpolation))
  463. END
  464. END
  465. END;
  466. INC(timestamp);
  467. END Draw;
  468. PROCEDURE Invalidate*(rect : Rectangle);
  469. VAR w, h : LONGINT; fx, fy : REAL;
  470. BEGIN
  471. w := GetWidth(); h := GetHeight();
  472. IF (w > 0) & (h > 0) & ((w # img.width) OR (h # img.height)) THEN
  473. fx := w / img.width; fy := h / img.height;
  474. rect.l := ENTIER(rect.l * fx); rect.t := ENTIER(rect.t * fy);
  475. rect.r := ENTIER(rect.r * fx + 0.5); rect.b := ENTIER(rect.b * fy + 0.5)
  476. END;
  477. Invalidate^(rect)
  478. END Invalidate;
  479. PROCEDURE Handle*(VAR m : Message);
  480. VAR w, h : LONGINT; fx, fy : REAL;
  481. BEGIN
  482. w := GetWidth(); h := GetHeight();
  483. IF (w > 0) & (h > 0) & ((w # img.width) OR (h # img.height)) & (m.msgType = Messages.MsgPointer) THEN
  484. m.x := m.x-bounds.l; m.y := m.y-bounds.t;
  485. fx := img.width / w; fy := img.height / h; m.x := ENTIER(m.x * fx); m.y := ENTIER(m.y * fy);
  486. m.x := m.x + bounds.l; m.y := m.y+bounds.l;
  487. END;
  488. Handle^(m)
  489. END Handle;
  490. END BufferWindow;
  491. DoubleBufferWindow* = OBJECT(BufferWindow)
  492. VAR
  493. visibleCanvas : Graphics.BufferCanvas;
  494. backImg* : Graphics.Image;
  495. swapping, drawing : BOOLEAN;
  496. PROCEDURE &Init*(w, h: LONGINT; alpha : BOOLEAN);
  497. BEGIN
  498. NEW(backImg);
  499. IF alpha THEN Raster.Create(backImg, w, h, Raster.BGRA8888) ELSE Raster.Create(backImg, w, h, format) END;
  500. Init^(w, h, alpha);
  501. END Init;
  502. PROCEDURE ReInit*(w, h : LONGINT);
  503. BEGIN {EXCLUSIVE}
  504. AWAIT(~drawing);
  505. IF useAlpha THEN
  506. Raster.Create(img, w, h, Raster.BGRA8888);
  507. Raster.Create(backImg, w, h, Raster.BGRA8888)
  508. ELSE
  509. Raster.Create(img, w, h, format);
  510. Raster.Create(backImg, w, h, format)
  511. END;
  512. visibleCanvas:=canvasGen(img);
  513. canvas:=canvasGen(backImg);
  514. END ReInit;
  515. PROCEDURE SetCanvasGenerator*(canvasGen:Graphics.CanvasGenerator);
  516. BEGIN
  517. SELF.canvasGen:=canvasGen;
  518. IF img # NIL THEN visibleCanvas:=canvasGen(img); END;
  519. IF backImg # NIL THEN canvas:=canvasGen(backImg); END;
  520. IF manager # NIL THEN manager.AddVisibleDirty(SELF, bounds) END
  521. END SetCanvasGenerator;
  522. PROCEDURE Draw*(canvas : Graphics.Canvas; w, h, q : LONGINT);
  523. BEGIN
  524. BEGIN{EXCLUSIVE}
  525. AWAIT(~swapping); drawing := TRUE;
  526. END;
  527. IF reduceQuality THEN q := 0 END;
  528. IF img # NIL THEN
  529. IF (w = img.width) & (h = img.height) THEN
  530. IF useAlpha THEN canvas.DrawImage(0, 0, img, Graphics.ModeSrcOverDst)
  531. ELSE canvas.DrawImage(0, 0, img, Graphics.ModeCopy)
  532. END
  533. ELSE
  534. IF useAlpha THEN
  535. canvas.ScaleImage(img, Rectangles.MakeRect(0, 0, img.width, img.height),
  536. Rectangles.MakeRect(0, 0, w, h), Graphics.ModeSrcOverDst, MIN(q,maxInterpolation))
  537. ELSE
  538. canvas.ScaleImage(img, Rectangles.MakeRect(0, 0, img.width, img.height),
  539. Rectangles.MakeRect(0, 0, w, h), Graphics.ModeCopy, MIN(q,maxInterpolation))
  540. END
  541. END
  542. END;
  543. BEGIN{EXCLUSIVE}
  544. drawing := FALSE;
  545. END;
  546. INC(timestamp);
  547. END Draw;
  548. PROCEDURE CopyRect*(rect : Rectangle);
  549. BEGIN {EXCLUSIVE}
  550. swapping := TRUE;
  551. AWAIT(~drawing);
  552. visibleCanvas.SetClipRect(rect);
  553. visibleCanvas.DrawImage(0, 0, backImg, Graphics.ModeCopy);
  554. visibleCanvas.SetClipRect(visibleCanvas.limits);
  555. swapping := FALSE
  556. END CopyRect;
  557. PROCEDURE Swap*;
  558. VAR tmp : Graphics.Image; tmpc : Graphics.BufferCanvas;
  559. BEGIN {EXCLUSIVE}
  560. swapping := TRUE;
  561. AWAIT(~drawing);
  562. tmp := img; img := backImg; backImg := tmp;
  563. tmpc := canvas; canvas := visibleCanvas; visibleCanvas := tmpc;
  564. swapping := FALSE
  565. END Swap;
  566. END DoubleBufferWindow;
  567. (** A ViewPort observes the global coordinate space. The WindowManager calls the view on all changes that occur
  568. in the observed range. *)
  569. ViewPort* = OBJECT (Plugins.Plugin)
  570. VAR
  571. next* : ViewPort;
  572. manager* : WindowManager;
  573. range* : RealRect;
  574. (* Width and height of viewport at 1:1 zoom. Will be set once in the constructor of the ViewPort implementation *)
  575. width0*, height0* : LONGINT; (* read-only! *)
  576. (** The WindowManager calls the Update Procedure in locked state. *)
  577. PROCEDURE Update*(r : Rectangle; top : Window);
  578. END Update;
  579. (** The WindowManager calls the Update Procedure in locked state. *)
  580. PROCEDURE Refresh*(top : Window);
  581. END Refresh;
  582. (** Set the observed range *)
  583. PROCEDURE SetRange*(x, y, w, h : REAL; showTransition : BOOLEAN);
  584. END SetRange;
  585. (** Return the modifier keys that are pressed in the view *)
  586. PROCEDURE GetKeyState*(VAR state : SET);
  587. END GetKeyState;
  588. END ViewPort;
  589. Decorator* = PROCEDURE {DELEGATE} (w : Window);
  590. WindowManager* = OBJECT(Plugins.Plugin)
  591. VAR
  592. pointerNull*, pointerStandard*, pointerMove*, pointerText*, pointerCrosshair*,
  593. pointerLeftRight*, pointerUpDown*, pointerULDR*, pointerURDL*, pointerLink* : PointerInfo;
  594. decorate* : Decorator;
  595. viewRegistry- : Plugins.Registry;
  596. sequencer- : Messages.MsgSequencer; (** PROTECTED *)
  597. lock- : Locks.RWLock; (** PROTECTED *)
  598. messagePreviewList : MessagePreviewList;
  599. style : WindowStyle;
  600. PROCEDURE &Init*;
  601. BEGIN
  602. NEW(pointerNull);
  603. InitCursors;
  604. decorate := NIL;
  605. NEW(viewRegistry, "View#", "Views Port Window Manager");
  606. NEW(sequencer, Handle); lock := sequencer.lock;
  607. messagePreviewList := NIL;
  608. NEW(style);
  609. END Init;
  610. PROCEDURE InitCursors;
  611. BEGIN
  612. LoadCursor("ZeroSkin.zip://arrow.png", 0, 0, pointerStandard);
  613. LoadCursor("ZeroSkin.zip://move.png", 15, 15, pointerMove);
  614. LoadCursor("ZeroSkin.zip://text.png", 13, 12, pointerText);
  615. LoadCursor("ZeroSkin.zip://crosshair.png", 13, 12, pointerCrosshair);
  616. LoadCursor("ZeroSkin.zip://leftright.png", 13, 12, pointerLeftRight);
  617. LoadCursor("ZeroSkin.zip://updown.png", 13, 12, pointerUpDown);
  618. LoadCursor("ZeroSkin.zip://uldr.png", 13, 12, pointerULDR);
  619. LoadCursor("ZeroSkin.zip://urdl.png", 13, 12, pointerURDL);
  620. LoadCursor("ZeroSkin.zip://hand.png", 6, 0, pointerLink);
  621. END InitCursors;
  622. PROCEDURE ZeroSkin*;
  623. BEGIN
  624. lock.AcquireWrite;
  625. style.Init;
  626. SetStyle(style);
  627. InitCursors;
  628. lock.ReleaseWrite
  629. END ZeroSkin;
  630. PROCEDURE ShutDown*;
  631. BEGIN
  632. ASSERT(lock.HasWriteLock());
  633. Plugins.main.Remove(viewRegistry)
  634. END ShutDown;
  635. (** Window management *)
  636. (** Add adds a window at pos l, t with flags *)
  637. PROCEDURE Add*(l, t : LONGINT; item : Window; flags:SET);
  638. END Add;
  639. (** Remove removes a window *)
  640. PROCEDURE Remove*(item : Window);
  641. END Remove;
  642. (** Set the position of a window *)
  643. PROCEDURE SetWindowPos*(vs : Window; x, y : LONGINT);
  644. END SetWindowPos;
  645. (** Set the size of a window. Return the new size in width and height *)
  646. (** If the window contains left, top, right or bottom, SetWindowSize is called
  647. appropriately *)
  648. PROCEDURE SetWindowSize*(vs : Window; VAR width, height : LONGINT);
  649. END SetWindowSize;
  650. (** Add a region to be refreshed *)
  651. PROCEDURE AddDirty*(VAR rect:Rectangle);
  652. END AddDirty;
  653. (** Add a dirty region. The region is in window coordinates and will be clipped against non transparent
  654. windows above *)
  655. PROCEDURE AddVisibleDirty*(w : Window; rect : Rectangle);
  656. END AddVisibleDirty;
  657. (** Set the keyboard focus to the window w *)
  658. PROCEDURE SetFocus*(w : Window);
  659. END SetFocus;
  660. (** Add a decoration window w to window to. The window w must separately be added to the wm *)
  661. (** A window MUST NOT be added more than once *)
  662. (** MUST hold lock *)
  663. PROCEDURE AddDecorWindow*(to, decor : Window);
  664. VAR dl : DecorList;
  665. BEGIN
  666. lock.AcquireWrite;
  667. INCL(decor.flags, FlagDecorWindow);
  668. INCL(decor.flags, FlagHidden);
  669. decor.master := to;
  670. NEW(dl); dl.w := decor; dl.next := to.decor; to.decor := dl;
  671. lock.ReleaseWrite
  672. END AddDecorWindow;
  673. (** Remove a decoration window w from window from. The window w must separately be removed from the wm *)
  674. (** MUST hold lock *)
  675. PROCEDURE RemoveDecorWindow*(w, from : Window);
  676. VAR dl : DecorList;
  677. BEGIN
  678. lock.AcquireWrite;
  679. IF (from.decor # NIL) & (from.decor.w = w) THEN from.decor := from.decor.next
  680. ELSE
  681. dl := from.decor;
  682. WHILE (dl.next # NIL) & (dl.next.w # w) DO dl := dl.next END;
  683. IF dl.next # NIL THEN dl.next := dl.next.next END
  684. END;
  685. lock.ReleaseWrite
  686. END RemoveDecorWindow;
  687. PROCEDURE SetStyle*(x : WindowStyle);
  688. VAR m : Message;
  689. BEGIN
  690. ASSERT(style # NIL);
  691. style := x; m.msgType := Messages.MsgStyleChanged; m.ext := style;
  692. Broadcast(m)
  693. END SetStyle;
  694. PROCEDURE GetStyle*() : WindowStyle;
  695. BEGIN
  696. ASSERT(style # NIL);
  697. RETURN style
  698. END GetStyle;
  699. (** Move Window w to front. If FlagStayOnTop is not set in w.flags, w will stay behind all windows with this flag set *)
  700. PROCEDURE ToFront*(w : Window);
  701. END ToFront;
  702. (** Move Window w to the background. If FlagStayOnTop is not set in w.flags, w will stay behind all windows *)
  703. PROCEDURE ToBack*(w : Window);
  704. END ToBack;
  705. PROCEDURE SetIsVisible*(w : Window; isVisible : BOOLEAN);
  706. VAR d : DecorList;
  707. BEGIN
  708. ASSERT(w # NIL);
  709. lock.AcquireWrite;
  710. IF (w.isVisible # isVisible) THEN
  711. w.isVisible := isVisible;
  712. IF (w.leftW # NIL) THEN w.leftW.isVisible := isVisible; END;
  713. IF (w.rightW # NIL) THEN w.rightW.isVisible := isVisible; END;
  714. IF (w.topW # NIL) THEN w.topW.isVisible := isVisible; END;
  715. IF (w.bottomW # NIL) THEN w.bottomW.isVisible := isVisible; END;
  716. AddDirty(w.bounds);
  717. IF (w.decor # NIL) THEN
  718. d := w.decor;
  719. WHILE (d # NIL) & (d.w # NIL) DO
  720. AddDirty(d.w.bounds);
  721. d := d.next;
  722. END;
  723. END;
  724. IncOTimestamp;
  725. END;
  726. lock.ReleaseWrite;
  727. END SetIsVisible;
  728. (** Set icon for a given window. Icon may be set to NIL. *)
  729. PROCEDURE SetWindowIcon*(w : Window; icon : Graphics.Image);
  730. VAR tw : Window;
  731. BEGIN
  732. ASSERT(w # NIL);
  733. lock.AcquireWrite;
  734. w.icon := icon;
  735. tw := w.topW;
  736. IF tw # NIL THEN AddVisibleDirty(tw, tw.bounds) END;
  737. lock.ReleaseWrite;
  738. IncOTimestamp;
  739. END SetWindowIcon;
  740. (** Return the window at postition x, y in global space. *)
  741. (** Windows that have the FlagNavigate flag set will not be considered *)
  742. (** Must hold WM lock *)
  743. PROCEDURE GetPositionOwner*(x, y : LONGINT) : Window;
  744. END GetPositionOwner;
  745. PROCEDURE GetFocusOwner*() : Window;
  746. END GetFocusOwner;
  747. (** Set the title of a window as UTF-8 string *)
  748. PROCEDURE SetWindowTitle*(w : Window; title : String);
  749. VAR tw : Window;
  750. BEGIN
  751. lock.AcquireWrite;
  752. w.title := title;
  753. tw := w.topW;
  754. IF tw # NIL THEN AddVisibleDirty(tw, tw.bounds) END;
  755. lock.ReleaseWrite;
  756. IncWTimestamp; (* since navigation elements care about window title *)
  757. END SetWindowTitle;
  758. (** Get the title of a window as UTF-8 string *)
  759. PROCEDURE GetWindowTitle*(w : Window) : String;
  760. BEGIN
  761. RETURN w.title
  762. END GetWindowTitle;
  763. PROCEDURE SetWindowInfo*(w : Window; CONST info : WindowInfo);
  764. BEGIN
  765. ASSERT(w # NIL);
  766. lock.AcquireWrite;
  767. IF (w.info = NIL) THEN NEW(w.info); END;
  768. w.info^ := info;
  769. lock.ReleaseWrite;
  770. IncOTimestamp;
  771. END SetWindowInfo;
  772. PROCEDURE GetWindowInfo*(w : Window; VAR info : WindowInfo) : BOOLEAN;
  773. VAR infoPtr : WindowInfoPtr;
  774. BEGIN
  775. ASSERT(w # NIL);
  776. lock.AcquireRead;
  777. infoPtr := w.info;
  778. IF (infoPtr # NIL) THEN
  779. info := infoPtr^;
  780. END;
  781. lock.ReleaseRead;
  782. RETURN (infoPtr # NIL);
  783. END GetWindowInfo;
  784. (** Set or unset a window flag
  785. Note: Setting FlagStayOnTop unsets FlagStayOnBottom and vice versa *)
  786. PROCEDURE SetWindowFlag*(w : Window; flag : LONGINT; value : BOOLEAN);
  787. BEGIN
  788. ASSERT(w # NIL);
  789. ASSERT((flag = FlagFrame) OR (flag = FlagStayOnTop) OR (flag = FlagStayOnBottom) OR (flag = FlagHidden));
  790. END SetWindowFlag;
  791. (** Set if the window is willing to accept a dropped item *)
  792. PROCEDURE SetAcceptDrag*(w : Window; accept : BOOLEAN);
  793. BEGIN
  794. lock.AcquireWrite;
  795. w.acceptDrag := accept;
  796. lock.ReleaseWrite
  797. END SetAcceptDrag;
  798. PROCEDURE StartDrag*(w : Window; sender, data : ANY; img : Graphics.Image; offsetX, offsetY: LONGINT; onAccept, onReject : Messages.CompCommand) : BOOLEAN;
  799. END StartDrag;
  800. (** a pointer button must be pressed *)
  801. PROCEDURE TransferPointer*( to : Window) : BOOLEAN;
  802. END TransferPointer;
  803. (** Adjust pointer to new position / check picture *)
  804. PROCEDURE CheckPointerImage*;
  805. END CheckPointerImage;
  806. (** View management *)
  807. (** Add a view *)
  808. PROCEDURE AddView*(v : ViewPort);
  809. END AddView;
  810. (** Add the whole View.range as dirty and cause a redraw *)
  811. PROCEDURE RefreshView*(v : ViewPort);
  812. END RefreshView;
  813. (** RemoveView from windowmanager *)
  814. PROCEDURE RemoveView*(v : ViewPort);
  815. END RemoveView;
  816. (** Messages *)
  817. PROCEDURE Broadcast*(VAR m : Message);
  818. END Broadcast;
  819. PROCEDURE SendMessage*(dest : Window; VAR m : Message) : BOOLEAN;
  820. BEGIN
  821. IF dest.sequencer # NIL THEN RETURN dest.sequencer.Add(m)
  822. ELSE dest.Handle(m); RETURN TRUE
  823. END
  824. END SendMessage;
  825. (** Install a message preview procedure. The window manager calls the MessagePreviewProc for
  826. all external messages so that they can be recorded, changed or discarded *)
  827. PROCEDURE InstallMessagePreview*(x : MessagePreviewProc);
  828. VAR mpl : MessagePreviewList;
  829. BEGIN
  830. lock.AcquireWrite;
  831. NEW(mpl); mpl.next := messagePreviewList; mpl.proc := x; messagePreviewList := mpl;
  832. lock.ReleaseWrite
  833. END InstallMessagePreview;
  834. (** Remove a MessagePreviewProc *)
  835. PROCEDURE RemoveMessagePreview*(x : MessagePreviewProc);
  836. VAR cur : MessagePreviewList;
  837. BEGIN
  838. lock.AcquireWrite;
  839. IF (messagePreviewList # NIL) & (messagePreviewList.proc = x) THEN messagePreviewList := messagePreviewList.next
  840. ELSE
  841. cur := messagePreviewList;
  842. WHILE cur # NIL DO
  843. IF (cur.next # NIL) & (cur.next.proc = x) THEN cur.next := cur.next.next; lock.ReleaseWrite; RETURN
  844. ELSE cur := cur.next END
  845. END
  846. END;
  847. lock.ReleaseWrite
  848. END RemoveMessagePreview;
  849. (** Preview message to all installed message preview handlers. Only to be used by WindowManager itself *)
  850. PROCEDURE PreviewMessage*(VAR m : Message; VAR discard : BOOLEAN); (* protected *)
  851. VAR mpl : MessagePreviewList;
  852. BEGIN
  853. ASSERT(lock.HasReadLock());
  854. discard := FALSE;
  855. mpl := messagePreviewList;
  856. WHILE (mpl # NIL) & ~discard DO mpl.proc(m, discard); mpl := mpl.next END;
  857. END PreviewMessage;
  858. (** Enumeration *)
  859. (** Get the first "user" window --> May return NIL if only background and pointer window are installed *)
  860. (** Must hold lock *)
  861. PROCEDURE GetFirst*() : Window;
  862. END GetFirst;
  863. (** Get the window next "user" window on top of x *)
  864. PROCEDURE GetNext*(x : Window) : Window;
  865. END GetNext;
  866. (** Get the "user" window below x *)
  867. PROCEDURE GetPrev*(x : Window) : Window;
  868. END GetPrev;
  869. (** Replace the background window with w. Return the current background window *)
  870. PROCEDURE ReplaceBackground*(w : Window) : Window;
  871. END ReplaceBackground;
  872. (** Return the area that is actually occupied *)
  873. PROCEDURE GetPopulatedArea*(VAR r : Rectangle);
  874. END GetPopulatedArea;
  875. (** Internal handler for message that are directed to the window manager never call directly ! *)
  876. PROCEDURE HandleInternal*(VAR msg : Messages.Message); (** PROTECTED *)
  877. BEGIN
  878. ASSERT(sequencer.IsCallFromSequencer())
  879. END HandleInternal;
  880. (** All external events of the window manager are inserted here *)
  881. PROCEDURE Handle*(VAR msg : Messages.Message);
  882. VAR discard : BOOLEAN;
  883. BEGIN
  884. IF sequencer.IsCallFromSequencer() THEN
  885. PreviewMessage(msg, discard);
  886. IF ~discard THEN HandleInternal(msg) END
  887. ELSE
  888. IF ~sequencer.Add(msg) THEN
  889. KernelLog.String("A message sent to the WM could not be handled "); KernelLog.Ln
  890. END
  891. END
  892. END Handle;
  893. END WindowManager;
  894. VAR
  895. registry- : Plugins.Registry;
  896. pointerNull: PointerInfo;
  897. (* Changes whenever a Window is added or removed to/from a WindowManager *)
  898. wTimestamp- : LONGINT;
  899. (* Changes whenever the keyboard focus is given to another window or the visibility of a window changed *)
  900. oTimestamp- : LONGINT;
  901. (* Coordinate offsets use to determine position of a new window *)
  902. x1, y1 : LONGINT;
  903. format* : Raster.Format;
  904. nextId : LONGINT;
  905. standardCursorImage: Graphics.Image;
  906. (* Get a unique identifier *)
  907. PROCEDURE GetId() : LONGINT;
  908. BEGIN {EXCLUSIVE}
  909. INC(nextId);
  910. RETURN nextId;
  911. END GetId;
  912. PROCEDURE IncWTimestamp*;
  913. BEGIN {EXCLUSIVE}
  914. INC(wTimestamp);
  915. END IncWTimestamp;
  916. PROCEDURE IncOTimestamp*;
  917. BEGIN {EXCLUSIVE}
  918. INC(oTimestamp);
  919. END IncOTimestamp;
  920. (** Block until a window is added/removed, changes its visibility or the keyboard focus changes *)
  921. PROCEDURE AwaitChange*(wTs, oTs : LONGINT);
  922. BEGIN {EXCLUSIVE}
  923. AWAIT((wTimestamp # wTs) OR (oTimestamp # oTs));
  924. END AwaitChange;
  925. PROCEDURE ClearInfo*(VAR info : WindowInfo);
  926. VAR i : LONGINT;
  927. BEGIN
  928. FOR i := 0 TO LEN(info.openDocuments)-1 DO
  929. info.openDocuments[i].id := 0;
  930. info.openDocuments[i].name := "";
  931. info.openDocuments[i].fullname := "";
  932. info.openDocuments[i].modified := FALSE;
  933. info.openDocuments[i].hasFocus := FALSE;
  934. END;
  935. info.handleDocumentInfo := NIL;
  936. info.vc.width := 0;
  937. info.vc.height := 0;
  938. info.vc.generator := NIL;
  939. END ClearInfo;
  940. PROCEDURE NewString*(CONST x : ARRAY OF CHAR) : String;
  941. VAR t : String;
  942. BEGIN
  943. NEW(t, LEN(x)); COPY(x, t^); RETURN t
  944. END NewString;
  945. PROCEDURE LoadCursor*(CONST name : ARRAY OF CHAR; hx, hy : LONGINT; VAR pi : PointerInfo);
  946. BEGIN
  947. IF pi = NIL THEN NEW(pi) END;
  948. pi.img := Graphics.LoadImage(name, TRUE); pi.hotX := hx; pi.hotY := hy;
  949. IF pi.img = NIL THEN
  950. KernelLog.String("Picture not loaded : "); KernelLog.String(name); KernelLog.Ln;
  951. IF standardCursorImage = NIL THEN CreateStandardCursorImage END;
  952. pi.img := standardCursorImage; pi.hotX := 0; pi.hotY := 0;
  953. END
  954. END LoadCursor;
  955. PROCEDURE GetDefaultManager*() : WindowManager;
  956. VAR p : Plugins.Plugin;
  957. BEGIN
  958. p := registry.Await("");
  959. RETURN p(WindowManager)
  960. END GetDefaultManager;
  961. PROCEDURE GetDefaultView*() : ViewPort;
  962. VAR p : Plugins.Plugin; m : WindowManager;
  963. BEGIN
  964. m := GetDefaultManager();
  965. p := m.viewRegistry.Await("");
  966. RETURN p(ViewPort)
  967. END GetDefaultView;
  968. PROCEDURE ResetNextPosition*;
  969. BEGIN {EXCLUSIVE}
  970. x1 := 0; y1 := 0;
  971. END ResetNextPosition;
  972. PROCEDURE GetNextPosition*(window : Window; manager : WindowManager; view : ViewPort; VAR dx, dy : LONGINT);
  973. VAR style : WindowStyle; x, y : LONGINT;
  974. BEGIN {EXCLUSIVE}
  975. ASSERT((window # NIL) & (manager # NIL) & (view # NIL));
  976. style := manager.GetStyle();
  977. x := style.lw; y := style.th;
  978. dx := x + X0 + x1; dy := y + Y0 + y1;
  979. INC(x1, x); INC(y1, y);
  980. IF (x1 > ENTIER(0.3 * view.width0)) OR (y1 > ENTIER(0.3 * view.height0)) THEN
  981. x1 := 0; y1 := 0;
  982. END;
  983. END GetNextPosition;
  984. PROCEDURE DefaultAddWindow*(w : Window);
  985. VAR manager : WindowManager; view : ViewPort; dy, dx : LONGINT;
  986. BEGIN
  987. manager := GetDefaultManager();
  988. view := GetDefaultView();
  989. GetNextPosition(w, manager, view, dx, dy);
  990. manager.Add(ENTIER(view.range.l) + dx, ENTIER(view.range.t) + dy, w, {FlagFrame, FlagClose, FlagMinimize});
  991. manager.SetFocus(w)
  992. END DefaultAddWindow;
  993. PROCEDURE AddWindow*(w : Window; dx, dy : LONGINT);
  994. VAR manager : WindowManager; view : ViewPort;
  995. BEGIN
  996. manager := GetDefaultManager();
  997. view := GetDefaultView();
  998. manager.Add(ENTIER(view.range.l) + dx, ENTIER(view.range.t) + dy, w, {FlagFrame, FlagClose, FlagMinimize})
  999. END AddWindow;
  1000. PROCEDURE ExtAddWindow*(w : Window; dx, dy : LONGINT; flags : SET);
  1001. VAR manager : WindowManager; view : ViewPort;
  1002. BEGIN
  1003. manager := GetDefaultManager();
  1004. view := GetDefaultView();
  1005. manager.Add(ENTIER(view.range.l) + dx, ENTIER(view.range.t) + dy, w, flags)
  1006. END ExtAddWindow;
  1007. PROCEDURE ExtAddViewBoundWindow*(w : Window; dx, dy : LONGINT; view : ViewPort; flags : SET);
  1008. VAR manager : WindowManager;
  1009. BEGIN
  1010. flags := flags + {FlagNavigation};
  1011. manager := GetDefaultManager();
  1012. manager.Add(dx, dy, w, flags);
  1013. END ExtAddViewBoundWindow;
  1014. (** move a window to the default view *)
  1015. PROCEDURE DefaultBringToView*(w : Window; toFront : BOOLEAN);
  1016. VAR manager : WindowManager; view : ViewPort; dy, dx : LONGINT;
  1017. BEGIN
  1018. manager := GetDefaultManager();
  1019. view := GetDefaultView();
  1020. GetNextPosition(w, manager, view, dx, dy);
  1021. manager.SetWindowPos(w, ENTIER(view.range.l) + dx, ENTIER(view.range.t) + dy);
  1022. manager.SetFocus(w);
  1023. IF toFront THEN manager.ToFront(w) END
  1024. END DefaultBringToView;
  1025. PROCEDURE CleanUp;
  1026. BEGIN
  1027. Plugins.main.Remove(registry)
  1028. END CleanUp;
  1029. PROCEDURE CreateStandardCursorImage;
  1030. CONST Width = 32; Height = 32;
  1031. VAR canvas: Graphics.BufferCanvas; rect: Rectangles.Rectangle; points: ARRAY 3 OF Graphics.Point2d;
  1032. BEGIN
  1033. NEW(standardCursorImage);
  1034. Raster.Create(standardCursorImage, Width, Height, Raster.BGRA8888);
  1035. NEW(canvas, standardCursorImage);
  1036. Rectangles.SetRect (rect, 0, 0, Width, Height);
  1037. canvas.Fill(rect, 0H, Graphics.ModeCopy);
  1038. points[0].x := Width; points[0].y := Height DIV 2;
  1039. points[1].x := 0; points[1].y := 0;
  1040. points[2].x := Width DIV 2; points[2].y := Height;
  1041. canvas.FillPolygonFlat(points, 3, Graphics.White - 080H, Graphics.ModeCopy);
  1042. canvas.PolyLine(points, 3, FALSE, Graphics.Black, Graphics.ModeCopy);
  1043. END CreateStandardCursorImage;
  1044. BEGIN
  1045. Modules.InstallTermHandler(CleanUp);
  1046. NEW(registry, "WM#", "Window Managers");
  1047. nextId := 0; x1 := 0; y1 := 0;
  1048. wTimestamp := 0; oTimestamp := 0;
  1049. format := Raster.BGRA8888;
  1050. NEW(pointerNull);
  1051. standardCursorImage := NIL;
  1052. END WMWindowManager.