ScaleBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using System.Drawing.Design;
  6. namespace FastReport.Map
  7. {
  8. /// <summary>
  9. /// Specifies the position of a scale control inside the map.
  10. /// </summary>
  11. public enum ScaleDock
  12. {
  13. /// <summary>
  14. /// The scale is displayed at top left corner.
  15. /// </summary>
  16. TopLeft,
  17. /// <summary>
  18. /// The scale is displayed at top center side.
  19. /// </summary>
  20. TopCenter,
  21. /// <summary>
  22. /// The scale is displayed at top right corner.
  23. /// </summary>
  24. TopRight,
  25. /// <summary>
  26. /// The scale is displayed at middle left side.
  27. /// </summary>
  28. MiddleLeft,
  29. /// <summary>
  30. /// The scale is displayed at middle right side.
  31. /// </summary>
  32. MiddleRight,
  33. /// <summary>
  34. /// The scale is displayed at bottom left corner.
  35. /// </summary>
  36. BottomLeft,
  37. /// <summary>
  38. /// The scale is displayed at bottom center side.
  39. /// </summary>
  40. BottomCenter,
  41. /// <summary>
  42. /// The scale is displayed at bottom right corner.
  43. /// </summary>
  44. BottomRight
  45. }
  46. /// <summary>
  47. /// The base class for scale-type controls such as <see cref="DistanceScale"/> and <see cref="ColorScale"/>.
  48. /// </summary>
  49. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  50. public class ScaleBase
  51. {
  52. #region Fields
  53. private Border border;
  54. private FillBase fill;
  55. private Font titleFont;
  56. private Color titleColor;
  57. private string titleText;
  58. private Font font;
  59. private Color textColor;
  60. private Color borderColor;
  61. private ScaleDock dock;
  62. private bool visible;
  63. #endregion // Fields
  64. #region Properties
  65. /// <summary>
  66. /// Gets or sets the border.
  67. /// </summary>
  68. public Border Border
  69. {
  70. get { return border; }
  71. set
  72. {
  73. if (value == null)
  74. throw new ArgumentNullException("Border");
  75. border = value;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets or sets the fill.
  80. /// </summary>
  81. [Editor("FastReport.TypeEditors.FillEditor, FastReport", typeof(UITypeEditor))]
  82. public FillBase Fill
  83. {
  84. get { return fill; }
  85. set
  86. {
  87. if (value == null)
  88. throw new ArgumentNullException("Fill");
  89. fill = value;
  90. }
  91. }
  92. /// <summary>
  93. /// Gets or sets the title font.
  94. /// </summary>
  95. [Category("Appearance")]
  96. public Font TitleFont
  97. {
  98. get { return titleFont; }
  99. set
  100. {
  101. if (value == null)
  102. throw new ArgumentNullException("TitleFont");
  103. titleFont = value;
  104. }
  105. }
  106. /// <summary>
  107. /// Gets or sets the title text color.
  108. /// </summary>
  109. public Color TitleColor
  110. {
  111. get { return titleColor; }
  112. set { titleColor = value; }
  113. }
  114. /// <summary>
  115. /// Gets or sets the title text.
  116. /// </summary>
  117. public string TitleText
  118. {
  119. get { return titleText; }
  120. set { titleText = value; }
  121. }
  122. /// <summary>
  123. /// Gets or sets the font.
  124. /// </summary>
  125. [Category("Appearance")]
  126. public Font Font
  127. {
  128. get { return font; }
  129. set
  130. {
  131. if (value == null)
  132. throw new ArgumentNullException("Font");
  133. font = value;
  134. }
  135. }
  136. /// <summary>
  137. /// Gets or sets the text color.
  138. /// </summary>
  139. public Color TextColor
  140. {
  141. get { return textColor; }
  142. set { textColor = value; }
  143. }
  144. /// <summary>
  145. /// Gets or sets the border color.
  146. /// </summary>
  147. public Color BorderColor
  148. {
  149. get { return borderColor; }
  150. set { borderColor = value; }
  151. }
  152. /// <summary>
  153. /// Gets or sets the location of the scale.
  154. /// </summary>
  155. public ScaleDock Dock
  156. {
  157. get { return dock; }
  158. set { dock = value; }
  159. }
  160. /// <summary>
  161. /// Gets or sets the visibility of a scale.
  162. /// </summary>
  163. [DefaultValue(true)]
  164. public bool Visible
  165. {
  166. get { return visible; }
  167. set { visible = value; }
  168. }
  169. #endregion // Properties
  170. #region Public Methods
  171. /// <summary>
  172. /// Copies the contents of another ScaleBase.
  173. /// </summary>
  174. /// <param name="src">The ScaleBase instance to copy the contents from.</param>
  175. public virtual void Assign(ScaleBase src)
  176. {
  177. Border = src.Border.Clone();
  178. Fill = src.Fill.Clone();
  179. TitleFont = src.TitleFont;
  180. TitleColor = src.TitleColor;
  181. TitleText = src.TitleText;
  182. Font = src.Font;
  183. TextColor = src.TextColor;
  184. BorderColor = src.BorderColor;
  185. Dock = src.Dock;
  186. Visible = src.Visible;
  187. }
  188. /// <summary>
  189. /// Serializes the scale.
  190. /// </summary>
  191. /// <param name="writer">Writer object.</param>
  192. /// <param name="prefix">Scale property name.</param>
  193. /// <param name="diff">Another ScaleBase to compare with.</param>
  194. /// <remarks>
  195. /// This method is for internal use only.
  196. /// </remarks>
  197. public virtual void Serialize(FRWriter writer, string prefix, ScaleBase diff)
  198. {
  199. Border.Serialize(writer, prefix + ".Border", diff.Border);
  200. Fill.Serialize(writer, prefix + ".Fill", diff.Fill);
  201. if ((writer.SerializeTo != SerializeTo.Preview || !TitleFont.Equals(diff.TitleFont)) && writer.ItemName != "inherited")
  202. writer.WriteValue(prefix + ".TitleFont", TitleFont);
  203. if (TitleColor != diff.TitleColor)
  204. writer.WriteValue(prefix + ".TitleColor", TitleColor);
  205. if (TitleText != diff.TitleText)
  206. writer.WriteStr(prefix + ".TitleText", TitleText);
  207. if ((writer.SerializeTo != SerializeTo.Preview || !Font.Equals(diff.Font)) && writer.ItemName != "inherited")
  208. writer.WriteValue(prefix + ".Font", Font);
  209. if (TextColor != diff.TextColor)
  210. writer.WriteValue(prefix + ".TextColor", TextColor);
  211. if (BorderColor != diff.BorderColor)
  212. writer.WriteValue(prefix + ".BorderColor", BorderColor);
  213. if (Dock != diff.Dock)
  214. writer.WriteValue(prefix + ".Dock", Dock);
  215. if (Visible != diff.Visible)
  216. writer.WriteBool(prefix + ".Visible", Visible);
  217. }
  218. /// <summary>
  219. /// Gets the size of the scale, in pixels.
  220. /// </summary>
  221. /// <returns>The SizeF structure containing the size of the object.</returns>
  222. public virtual SizeF CalcSize()
  223. {
  224. SizeF size = new SizeF(100, 0);
  225. if (!String.IsNullOrEmpty(TitleText))
  226. size.Height += DrawUtils.MeasureString(TitleText, TitleFont).Height + 4;
  227. return size;
  228. }
  229. internal RectangleF GetDrawRect(MapObject parent)
  230. {
  231. float parentWidth = parent.Width - parent.Padding.Horizontal;
  232. float parentHeight = parent.Height - parent.Padding.Vertical;
  233. RectangleF drawRect = new RectangleF(
  234. new PointF(parent.AbsLeft + parent.Padding.Left, parent.AbsTop + parent.Padding.Top),
  235. CalcSize());
  236. switch (Dock)
  237. {
  238. case ScaleDock.TopLeft:
  239. break;
  240. case ScaleDock.TopCenter:
  241. drawRect.Offset((parentWidth - drawRect.Width) / 2, 0);
  242. break;
  243. case ScaleDock.TopRight:
  244. drawRect.Offset(parentWidth - drawRect.Width, 0);
  245. break;
  246. case ScaleDock.MiddleLeft:
  247. drawRect.Offset(0, (parentHeight - drawRect.Height) / 2);
  248. break;
  249. case ScaleDock.MiddleRight:
  250. drawRect.Offset(parentWidth - drawRect.Width, (parentHeight - drawRect.Height) / 2);
  251. break;
  252. case ScaleDock.BottomLeft:
  253. drawRect.Offset(0, parentHeight - drawRect.Height);
  254. break;
  255. case ScaleDock.BottomCenter:
  256. drawRect.Offset((parentWidth - drawRect.Width) / 2, parentHeight - drawRect.Height);
  257. break;
  258. case ScaleDock.BottomRight:
  259. drawRect.Offset(parentWidth - drawRect.Width, parentHeight - drawRect.Height);
  260. break;
  261. }
  262. return drawRect;
  263. }
  264. /// <summary>
  265. /// Draws the object.
  266. /// </summary>
  267. /// <param name="e">Draw parameters.</param>
  268. /// <param name="parent">Parent map object.</param>
  269. public virtual void Draw(FRPaintEventArgs e, MapObject parent)
  270. {
  271. RectangleF drawRect = GetDrawRect(parent);
  272. Fill.Draw(e, drawRect);
  273. Border.Draw(e, drawRect);
  274. if (!String.IsNullOrEmpty(TitleText))
  275. {
  276. Brush textBrush = e.Cache.GetBrush(TitleColor);
  277. Font font = e.Cache.GetFont(TitleFont.FontFamily,
  278. parent.IsPrinting ? TitleFont.Size : TitleFont.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi,
  279. TitleFont.Style);
  280. StringFormat format = e.Cache.GetStringFormat(StringAlignment.Center, StringAlignment.Near,
  281. StringTrimming.EllipsisCharacter, StringFormatFlags.NoWrap, 0, 0);
  282. RectangleF textRect = new RectangleF(drawRect.Left * e.ScaleX, (drawRect.Top + 3) * e.ScaleY,
  283. drawRect.Width * e.ScaleX, drawRect.Height * e.ScaleY);
  284. e.Graphics.DrawString(TitleText, font, textBrush, textRect, format);
  285. }
  286. }
  287. #endregion
  288. /// <summary>
  289. /// Initializes a new instance of the <see cref="ScaleBase"/> class.
  290. /// </summary>
  291. public ScaleBase()
  292. {
  293. border = new Border();
  294. border.Color = Color.Silver;
  295. border.Lines = BorderLines.All;
  296. fill = new SolidFill();
  297. (fill as SolidFill).Color = Color.White;
  298. borderColor = Color.DarkGray;
  299. titleFont = DrawUtils.DefaultFont;
  300. titleColor = Color.Black;
  301. titleText = "";
  302. font = DrawUtils.DefaultFont;
  303. textColor = Color.Black;
  304. visible = true;
  305. }
  306. }
  307. }