WMPopups.Mod 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. MODULE WMPopups; (** AUTHOR "BF"; PURPOSE "Popup Windows"; *)
  2. IMPORT
  3. Strings, WMRectangles, WMGraphics, WMEvents, WMWindowManager, WMComponents, WMStandardComponents,
  4. Localization, Repositories;
  5. CONST
  6. LineHeight = 20;
  7. TYPE
  8. Entry = OBJECT
  9. VAR
  10. caption : Strings.String; (* {caption # NIL} *)
  11. onClickHandler : WMEvents.EventListener; (* {onClickHandler # NIL} *)
  12. parameter : ANY;
  13. next : Entry;
  14. PROCEDURE &Init(caption : Strings.String; onClickHandler : WMEvents.EventListener; parameter : ANY);
  15. BEGIN
  16. ASSERT((caption # NIL) & (onClickHandler # NIL));
  17. SELF.caption := caption;
  18. SELF.onClickHandler := onClickHandler;
  19. SELF.parameter := parameter;
  20. next := NIL;
  21. END Init;
  22. END Entry;
  23. TYPE
  24. PopupWindow = OBJECT(WMComponents.FormWindow)
  25. VAR
  26. isClosed : BOOLEAN;
  27. languages : Localization.Languages;
  28. PROCEDURE &New(entries : Entry);
  29. VAR vc : WMComponents.VisualComponent;
  30. BEGIN
  31. ASSERT(entries # NIL);
  32. vc := CreateForm(entries);
  33. Init(vc.bounds.GetWidth(), vc.bounds.GetHeight(), FALSE);
  34. SetContent(vc);
  35. isClosed := FALSE;
  36. END New;
  37. PROCEDURE Translate(value: Strings.String): Strings.String;
  38. VAR
  39. res : WORD;
  40. temp, word : Strings.String;
  41. dictionary : Repositories.Dictionary;
  42. BEGIN
  43. IF (value # NIL) & (LEN(value^) > 4) & (value^[0] = ':') & (value^[1] = ':') THEN
  44. (** If string needs translation. E.g. has prefix that points to repository and dictionary at least:
  45. ::<Repository name>:<Dictionary name>: **)
  46. Repositories.GetTranslationInfo(value^, dictionary, word, res);
  47. IF (dictionary # NIL) & (word # NIL) THEN
  48. temp := dictionary.Translate(word, languages);
  49. IF (temp # word) THEN
  50. RETURN temp
  51. END
  52. END
  53. END;
  54. RETURN NIL
  55. END Translate;
  56. PROCEDURE CreateForm(entries : Entry) : WMComponents.VisualComponent;
  57. VAR
  58. panel : WMStandardComponents.Panel;
  59. button : WMStandardComponents.Button;
  60. font : WMGraphics.Font;
  61. entry : Entry;
  62. width, height, w, h : LONGINT;
  63. temp : Strings.String;
  64. BEGIN
  65. NEW(panel);
  66. panel.fillColor.Set(WMGraphics.White);
  67. languages := Localization.GetLanguagePreferences();
  68. width := 100; height := 0;
  69. entry := entries;
  70. WHILE (entry # NIL) DO
  71. NEW(button);
  72. button.alignment.Set(WMComponents.AlignTop);
  73. button.bounds.SetExtents(width, LineHeight);
  74. button.caption.Set(entry.caption);
  75. button.onClick.Add(entry.onClickHandler);
  76. button.onClick.Add(Clicked);
  77. button.userData := entry.parameter;
  78. panel.AddInternalComponent(button);
  79. font := button.GetFont();
  80. temp := Translate(entry.caption);
  81. IF temp # NIL THEN
  82. font.GetStringSize(temp^, w, h);
  83. ELSE
  84. font.GetStringSize(entry.caption^, w, h);
  85. END;
  86. IF (w + 10 > width) THEN
  87. width := w + 10;
  88. END;
  89. height := height + LineHeight;
  90. entry := entry.next;
  91. END;
  92. width := MIN(width, 1024);
  93. panel.bounds.SetExtents(width, height);
  94. RETURN panel;
  95. END CreateForm;
  96. PROCEDURE Clicked(sender, data : ANY);
  97. BEGIN
  98. Close;
  99. END Clicked;
  100. PROCEDURE FocusLost*;
  101. BEGIN
  102. Close;
  103. END FocusLost;
  104. PROCEDURE Close*;
  105. BEGIN
  106. BEGIN {EXCLUSIVE}
  107. IF isClosed THEN RETURN; END;
  108. isClosed := TRUE;
  109. END;
  110. Close^;
  111. END Close;
  112. PROCEDURE FocusGot*;
  113. BEGIN
  114. manager.SetFocus(SELF)
  115. END FocusGot;
  116. END PopupWindow;
  117. (* Open a Popup *)
  118. Popup* = OBJECT
  119. VAR
  120. first, last : Entry;
  121. window : PopupWindow;
  122. PROCEDURE &New*;
  123. BEGIN
  124. first := NIL; last := NIL;
  125. window := NIL;
  126. END New;
  127. PROCEDURE Add*(CONST caption : ARRAY OF CHAR; onClickHandler : WMEvents.EventListener);
  128. BEGIN
  129. AddParButton(caption, onClickHandler, NIL);
  130. END Add;
  131. PROCEDURE AddParButton*(CONST caption : ARRAY OF CHAR; onClickHandler : WMEvents.EventListener; par : ANY);
  132. VAR entry : Entry;
  133. BEGIN {EXCLUSIVE}
  134. NEW(entry, Strings.NewString(caption), onClickHandler, par);
  135. IF (first = NIL) THEN
  136. first := entry; last := entry;
  137. ELSE
  138. last.next := entry; last := entry;
  139. END;
  140. END AddParButton;
  141. PROCEDURE Close*;
  142. BEGIN {EXCLUSIVE}
  143. IF (window # NIL) THEN
  144. window.Close;
  145. window := NIL;
  146. END;
  147. END Close;
  148. PROCEDURE Popup* (x, y : LONGINT);
  149. VAR manager : WMWindowManager.WindowManager;
  150. BEGIN {EXCLUSIVE}
  151. IF (first # NIL) THEN
  152. IF (window # NIL) THEN window.Close; END;
  153. NEW(window, first);
  154. manager := WMWindowManager.GetDefaultManager();
  155. manager.Add(x, y, window, {WMWindowManager.FlagStayOnTop, WMWindowManager.FlagHidden});
  156. manager.SetFocus(window);
  157. END;
  158. END Popup;
  159. END Popup;
  160. (** Open a color swatch dialog *)
  161. ColorSwatchPopup* = OBJECT (WMComponents.FormWindow)
  162. VAR colorPanel : ColorSwatchPanel;
  163. color- : WMGraphics.Color;
  164. onColorChosen* : PROCEDURE {DELEGATE} (color : WMGraphics.Color);
  165. PROCEDURE &New*;
  166. BEGIN
  167. color := 0H;
  168. CreatePopup;
  169. Init(colorPanel.bounds.GetWidth(), colorPanel.bounds.GetHeight(), FALSE);
  170. SetContent(colorPanel);
  171. END New;
  172. PROCEDURE CreatePopup;
  173. BEGIN
  174. NEW(colorPanel);
  175. colorPanel.ChosenColorProc := SetColor;
  176. END CreatePopup;
  177. PROCEDURE Popup*(x, y : LONGINT);
  178. BEGIN
  179. manager := WMWindowManager.GetDefaultManager();
  180. manager.Add(x, y, SELF, {WMWindowManager.FlagStayOnTop, WMWindowManager.FlagHidden});
  181. manager.SetFocus(SELF);
  182. END Popup;
  183. PROCEDURE Clicked(sender, data : ANY);
  184. BEGIN
  185. manager.Remove(SELF)
  186. END Clicked;
  187. PROCEDURE FocusLost*;
  188. BEGIN
  189. manager.Remove(SELF)
  190. END FocusLost;
  191. PROCEDURE FocusGot*;
  192. BEGIN
  193. manager.SetFocus(SELF)
  194. END FocusGot;
  195. PROCEDURE SetColor(color : WMGraphics.Color);
  196. BEGIN
  197. SELF.color := color;
  198. IF onColorChosen # NIL THEN onColorChosen(color) END;
  199. manager.Remove(SELF)
  200. END SetColor;
  201. END ColorSwatchPopup;
  202. (** Color Swatch Visual Component *)
  203. ColorSwatchPanel* = OBJECT(WMComponents.VisualComponent)
  204. VAR colors : ARRAY 19 OF LONGINT;
  205. ChosenColorProc* : PROCEDURE {DELEGATE} (color: WMGraphics.Color);
  206. (* CloseProc : PROCEDURE {DELEGATE}; *)
  207. PROCEDURE &Init*;
  208. BEGIN
  209. Init^;
  210. bounds.SetExtents(190, 70);
  211. BuildPalette;
  212. END Init;
  213. PROCEDURE PointerDown*(x, y : LONGINT; keys : SET);
  214. VAR r, g, b, a, i, j: LONGINT; cColor: WMGraphics.Color;
  215. BEGIN
  216. i := y DIV 10; j := x DIV 10;
  217. IF (i>= 0) & (i<=2) THEN
  218. WMGraphics.ColorToRGBA(colors[j], r, g, b, a);
  219. r := ENTIER((i+1)/4*r); g:= ENTIER((i+1)/4*g); b:= ENTIER((i+1)/4*b);
  220. cColor := WMGraphics.RGBAToColor(r, g, b, a);
  221. ELSIF (i= 3) THEN
  222. cColor := colors[j];
  223. ELSIF (i>=4) & (i<=6) THEN
  224. i := i - 4;
  225. WMGraphics.ColorToRGBA(colors[j], r, g, b, a);
  226. r := 255-ENTIER((3-i)/4*(255-r)); g:= 255-ENTIER((3-i)/4*(255-g)); b:= 255-ENTIER((3-i)/4*(255-b));
  227. cColor := WMGraphics.RGBAToColor(r, g, b, a);
  228. ELSE
  229. END;
  230. IF (y>0) & (y<bounds.GetHeight()) & (x>0) &(x<bounds.GetWidth())THEN
  231. ChosenColorProc(cColor);
  232. END;
  233. END PointerDown;
  234. PROCEDURE DrawBackground*(canvas: WMGraphics.Canvas);
  235. VAR r, g, b, a, i, j: LONGINT; color: WMGraphics.Color;
  236. BEGIN
  237. DrawBackground^(canvas);
  238. FOR i := 0 TO 2 DO
  239. FOR j := 0 TO 18 DO
  240. WMGraphics.ColorToRGBA(colors[j], r, g, b, a);
  241. r := ENTIER((i+1)/4*r); g:= ENTIER((i+1)/4*g); b:= ENTIER((i+1)/4*b);
  242. color := WMGraphics.RGBAToColor(r, g, b, a);
  243. canvas.Fill(WMRectangles.MakeRect(10*j,10*i,10*j+10,10*i+10),color , WMGraphics.ModeCopy);
  244. END;
  245. END;
  246. FOR j := 0 TO 18 DO
  247. color := colors[j];
  248. canvas.Fill(WMRectangles.MakeRect(10*j,30,10*j+10,10+30),color , WMGraphics.ModeCopy);
  249. END;
  250. FOR i := 0 TO 2 DO
  251. FOR j := 0 TO 18 DO
  252. WMGraphics.ColorToRGBA(colors[j], r, g, b, a);
  253. r := 255-ENTIER((3-i)/4*(255-r)); g:= 255-ENTIER((3-i)/4*(255-g)); b:= 255-ENTIER((3-i)/4*(255-b));
  254. color := WMGraphics.RGBAToColor(r, g, b, a);
  255. canvas.Fill(WMRectangles.MakeRect(10*j,10*i+40,10*j+10,10*i+10+40),color , WMGraphics.ModeCopy);
  256. END;
  257. END;
  258. END DrawBackground;
  259. PROCEDURE BuildPalette;
  260. BEGIN
  261. colors[0] := LONGINT(0FF0000FFH); (* red *)
  262. colors[1] := LONGINT(0FF5500FFH);
  263. colors[2] := LONGINT(0FFAA00FFH);
  264. colors[3] := LONGINT(0FFFF00FFH); (* yellow *)
  265. colors[4] := LONGINT(0AAFF00FFH);
  266. colors[5] := LONGINT(055FF00FFH);
  267. colors[6] := 000FF00FFH; (* green *)
  268. colors[7] := 000FF55FFH;
  269. colors[8] := 000FFAAFFH;
  270. colors[9] := 000FFFFFFH; (* cyan *)
  271. colors[10] := 000AAFFFFH;
  272. colors[11] := 00055FFFFH;
  273. colors[12] := 00000FFFFH; (* blue *)
  274. colors[13] := 05500FFFFH;
  275. colors[14] :=LONGINT( 0AA00FFFFH);
  276. colors[15] :=LONGINT( 0FF00FFFFH); (* magenta *)
  277. colors[16] :=LONGINT( 0FF00AAFFH);
  278. colors[17] :=LONGINT( 0FF0055FFH);
  279. colors[18] :=LONGINT( 0888888FFH); (* grey *)
  280. END BuildPalette;
  281. END ColorSwatchPanel;
  282. END WMPopups.
  283. -----------------------------------------------------
  284. System.Free WMPopups