WMProgressComponents.Mod 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. MODULE WMProgressComponents; (** AUTHOR "staubesv"; PURPOSE "Progress indication components and windows"; *)
  2. IMPORT
  3. KernelLog, Strings, XML, Types, Models, WMGraphicUtilities, WMComponents, WMProperties, WMRectangles, WMGraphics;
  4. TYPE
  5. ProgressBar* = OBJECT(WMComponents.VisualComponent)
  6. VAR
  7. min-, max- : WMProperties.Int32Property;
  8. current : LONGINT;
  9. model- : WMProperties.ReferenceProperty;
  10. modelI : Models.Model;
  11. isVertical- : WMProperties.BooleanProperty;
  12. color-: WMProperties.ColorProperty;
  13. borderColor-: WMProperties.ColorProperty;
  14. textColor- : WMProperties.ColorProperty;
  15. showPercents- : WMProperties.BooleanProperty; (* Default: TRUE *)
  16. PROCEDURE &Init*;
  17. BEGIN
  18. Init^;
  19. takesFocus.Set(FALSE);
  20. NEW(isVertical, PrototypePbIsVertical, NIL, NIL); properties.Add(isVertical);
  21. NEW(color, PrototypePbColor, NIL, NIL); properties.Add(color);
  22. NEW(borderColor, PrototypePbBorderColor, NIL, NIL); properties.Add(borderColor);
  23. NEW(min, PrototypePbMin, NIL, NIL); properties.Add(min);
  24. NEW(max, PrototypePbMax, NIL, NIL); properties.Add(max);
  25. current := 0;
  26. NEW(model, PrototypePbModel, NIL, NIL); properties.Add(model);
  27. modelI := NIL;
  28. NEW(showPercents, PrototypePbShowPercents, NIL, NIL); properties.Add(showPercents);
  29. NEW(textColor, PrototypePbTextColor, NIL, NIL); properties.Add(textColor);
  30. SetNameAsString(StrProgressBar);
  31. SetGenerator("WMProgressComponents.GenProgressBar");
  32. END Init;
  33. PROCEDURE SetRange*(min, max : LONGINT);
  34. BEGIN
  35. Acquire;
  36. SELF.min.Set(min);
  37. SELF.max.Set(max);
  38. Release;
  39. Invalidate;
  40. END SetRange;
  41. PROCEDURE SetCurrent*(current : LONGINT);
  42. BEGIN
  43. SetInternal(current, TRUE);
  44. END SetCurrent;
  45. PROCEDURE SetInternal(current : LONGINT; updateModel : BOOLEAN);
  46. VAR changed : BOOLEAN; min, max : LONGINT;
  47. BEGIN
  48. min := SELF.min.Get(); max := SELF.max.Get();
  49. IF (current < min) THEN current := min; ELSIF (current > max) THEN current := max; END;
  50. Acquire;
  51. IF (current # SELF.current) THEN
  52. SELF.current := current; changed := TRUE;
  53. END;
  54. Release;
  55. IF changed THEN
  56. IF updateModel THEN UpdateModel(current); END;
  57. Invalidate;
  58. END;
  59. END SetInternal;
  60. PROCEDURE IncPos*;
  61. VAR max : LONGINT;
  62. BEGIN
  63. max := SELF.max.Get();
  64. Acquire;
  65. INC(current); IF current > max THEN current := max; END;
  66. Release;
  67. Invalidate;
  68. END IncPos;
  69. PROCEDURE RecacheProperties*;
  70. VAR any : ANY;
  71. BEGIN
  72. RecacheProperties^;
  73. any := model.Get();
  74. IF (any # NIL) & (any IS Models.Model) & (any # modelI) THEN
  75. modelI := any (Models.Model);
  76. modelI.onChanged.Add(ModelChanged);
  77. ModelChanged(NIL, NIL);
  78. ELSIF (modelI # NIL) THEN
  79. modelI.onChanged.Remove(ModelChanged);
  80. modelI := NIL;
  81. END;
  82. END RecacheProperties;
  83. PROCEDURE UpdateModel(value : LONGINT);
  84. VAR integer : Types.Integer; res : WORD; model : Models.Model;
  85. BEGIN
  86. model := modelI;
  87. IF (model # NIL) THEN
  88. integer.value := value;
  89. model.SetGeneric(integer, res); (* ignore res *)
  90. END;
  91. END UpdateModel;
  92. PROCEDURE ModelChanged(sender, data : ANY);
  93. VAR integer : Types.Integer; res : WORD;
  94. BEGIN
  95. Acquire;
  96. modelI.GetGeneric(integer, res);
  97. IF (res = Models.Ok) THEN
  98. SetInternal(integer.value, TRUE);
  99. ELSE
  100. KernelLog.String("WMProgressComponents.ModelChanged.res = "); KernelLog.Int(res, 0); KernelLog.Ln;
  101. END;
  102. Release;
  103. END ModelChanged;
  104. PROCEDURE PropertyChanged*(sender, property : ANY);
  105. BEGIN
  106. IF (property = min) OR (property = max) THEN
  107. Invalidate;
  108. ELSIF (property = color) OR (property = borderColor) OR (property = textColor) OR
  109. (property = showPercents) OR (property = isVertical) THEN
  110. Invalidate;
  111. ELSIF (property = model) THEN
  112. RecacheProperties;
  113. ELSE PropertyChanged^(sender, property)
  114. END;
  115. END PropertyChanged;
  116. PROCEDURE DrawBackground*(canvas: WMGraphics.Canvas);
  117. VAR
  118. rect: WMRectangles.Rectangle;
  119. width: LONGINT;
  120. string : ARRAY 32 OF CHAR;
  121. min, max, cur, percent : LONGINT; color: WMGraphics.Color;
  122. isVertical : BOOLEAN;
  123. BEGIN
  124. min := SELF.min.Get();
  125. max := SELF.max.Get();
  126. cur := current;
  127. IF (cur > max) THEN cur := max; ELSIF (cur < min) THEN cur := min; END;
  128. isVertical := SELF.isVertical.Get();
  129. DrawBackground^(canvas);
  130. IF max > min THEN
  131. IF ~isVertical THEN
  132. width := ENTIER((cur - min) / (max - min) * bounds.GetWidth());
  133. rect := WMRectangles.MakeRect(0, 0, width, bounds.GetHeight());
  134. ELSE
  135. width := ENTIER((cur - min) / (max - min) * bounds.GetHeight());
  136. rect := WMRectangles.MakeRect(0, bounds.GetHeight() - width, bounds.GetWidth(), bounds.GetHeight());
  137. END;
  138. canvas.Fill(rect, SELF.color.Get(), WMGraphics.ModeSrcOverDst);
  139. END;
  140. rect := GetClientRect();
  141. IF ~isVertical THEN
  142. rect.l := width;
  143. ELSE
  144. rect.t := rect.b + width;
  145. END;
  146. canvas.Fill(rect, fillColor.Get(), WMGraphics.ModeSrcOverDst);
  147. color := borderColor.Get();
  148. IF (color # 0) THEN
  149. WMGraphicUtilities.DrawRect(canvas, GetClientRect(), color, WMGraphics.ModeSrcOverDst);
  150. END;
  151. IF showPercents.Get() & (max - min > 0) THEN
  152. percent := ENTIER(100 * (cur - min) / (max - min)); Strings.IntToStr(percent, string); Strings.Append(string, "%");
  153. canvas.SetColor(textColor.Get());
  154. WMGraphics.DrawStringInRect(canvas, GetClientRect(), FALSE, WMGraphics.AlignCenter, WMGraphics.AlignCenter, string)
  155. END;
  156. END DrawBackground;
  157. END ProgressBar;
  158. VAR
  159. (** ProgressBar property prototypes *)
  160. PrototypePbMin*, PrototypePbMax*: WMProperties.Int32Property;
  161. PrototypePbModel* : WMProperties.ReferenceProperty;
  162. PrototypePbIsVertical* : WMProperties.BooleanProperty;
  163. PrototypePbColor*, PrototypePbBorderColor* : WMProperties.ColorProperty;
  164. PrototypePbShowPercents* : WMProperties.BooleanProperty;
  165. PrototypePbTextColor* : WMProperties.ColorProperty;
  166. StrProgressBar : Strings.String;
  167. PROCEDURE GenProgressBar*() : XML.Element;
  168. VAR pb : ProgressBar;
  169. BEGIN NEW(pb); RETURN pb
  170. END GenProgressBar;
  171. PROCEDURE InitPrototypes;
  172. VAR plProgressBar : WMProperties.PropertyList;
  173. BEGIN
  174. (* ProgressBar properties *)
  175. NEW(plProgressBar); WMComponents.propertyListList.Add("ProgressBar", plProgressBar);
  176. (* colors *)
  177. NEW(PrototypePbIsVertical, NIL, Strings.NewString("IsVertical"), Strings.NewString("Vertical progress bar?"));
  178. PrototypePbIsVertical.Set(FALSE);
  179. NEW(PrototypePbBorderColor, NIL, Strings.NewString("BorderColor"), Strings.NewString("Progressbar border color")); plProgressBar.Add(PrototypePbBorderColor);
  180. PrototypePbBorderColor.Set(WMGraphics.White);
  181. NEW(PrototypePbColor, NIL, Strings.NewString("Color"), Strings.NewString("Progressbar color")); plProgressBar.Add(PrototypePbColor);
  182. PrototypePbColor.Set(WMGraphics.Blue);
  183. NEW(PrototypePbTextColor, NIL, Strings.NewString("TextColor"), Strings.NewString("Progressbar text color")); plProgressBar.Add(PrototypePbTextColor);
  184. PrototypePbTextColor.Set(WMGraphics.White);
  185. (* position *)
  186. NEW(PrototypePbMin, NIL, Strings.NewString("Min"), Strings.NewString("Minimum position"));
  187. PrototypePbMin.Set(0);
  188. NEW(PrototypePbMax, NIL, Strings.NewString("Max"), Strings.NewString("Maximum position"));
  189. PrototypePbMax.Set(100);
  190. NEW(PrototypePbModel, NIL, Strings.NewString("Model"), Strings.NewString("Model")); PrototypePbModel.Set(NIL);
  191. (* other *)
  192. NEW(PrototypePbShowPercents, NIL, Strings.NewString("ShowPercents"), Strings.NewString("Should the progress also be displayed as text")); plProgressBar.Add(PrototypePbShowPercents);
  193. PrototypePbShowPercents.Set(TRUE);
  194. END InitPrototypes;
  195. BEGIN
  196. StrProgressBar := Strings.NewString("ProgressBar");
  197. InitPrototypes;
  198. END WMProgressComponents.