SelectedTextObjects.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using FastReport.Design;
  6. using FastReport.Format;
  7. using FastReport.Forms;
  8. using System.Windows.Forms;
  9. namespace FastReport
  10. {
  11. /// <summary>
  12. /// Holds the list of <see cref="TextObject"/> objects currently selected in the designer.
  13. /// </summary>
  14. /// <remarks>
  15. /// This class is used by the "Text" toolbar. Use methods of this class to perform some
  16. /// operations on the selected objects.
  17. /// <para/>Note: after calling any method in this class, call the
  18. /// <see cref="Designer.SetModified()">Designer.SetModified</see> method to reflect changes.
  19. /// <para/>Note: this list contains only objects of <see cref="TextObject"/> type. If you want to access all
  20. /// selected objects, use the <see cref="Designer.SelectedObjects"/> property.
  21. /// </remarks>
  22. public class SelectedTextObjects
  23. {
  24. private List<TextObject> list;
  25. private Designer designer;
  26. /// <summary>
  27. /// Gets the first selected object.
  28. /// </summary>
  29. public TextObject First
  30. {
  31. get { return list.Count > 0 ? list[0] : null; }
  32. }
  33. /// <summary>
  34. /// Gets the number of selected objects.
  35. /// </summary>
  36. public int Count
  37. {
  38. get { return list.Count; }
  39. }
  40. /// <summary>
  41. /// Gets a value indicating whether the operations are enabled.
  42. /// </summary>
  43. public bool Enabled
  44. {
  45. get
  46. {
  47. return Count > 1 || (Count == 1 && CanModify(First));
  48. }
  49. }
  50. private List<TextObject> ModifyList
  51. {
  52. get { return list.FindAll(CanModify); }
  53. }
  54. private bool CanModify(TextObject c)
  55. {
  56. return !c.HasRestriction(Restrictions.DontModify);
  57. }
  58. internal void Update()
  59. {
  60. list.Clear();
  61. if (designer.SelectedObjects != null)
  62. {
  63. foreach (Base c in designer.SelectedObjects)
  64. {
  65. if (c is TextObject)
  66. list.Add(c as TextObject);
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// Sets the font name for the selected objects.
  72. /// </summary>
  73. /// <param name="name">Font name.</param>
  74. public void SetFontName(string name)
  75. {
  76. foreach (TextObject text in ModifyList)
  77. {
  78. text.Font = new Font(name, text.Font.Size, text.Font.Style);
  79. }
  80. designer.LastFormatting.Font = First.Font;
  81. designer.SetModified();
  82. }
  83. /// <summary>
  84. /// Sets the font size for the selected objects.
  85. /// </summary>
  86. /// <param name="size">Font size.</param>
  87. public void SetFontSize(float size)
  88. {
  89. foreach (TextObject text in ModifyList)
  90. {
  91. text.Font = new Font(text.Font.Name, size, text.Font.Style);
  92. }
  93. designer.LastFormatting.Font = First.Font;
  94. designer.SetModified();
  95. }
  96. /// <summary>
  97. /// Toggles the specified font style for the selected objects.
  98. /// </summary>
  99. /// <param name="style">Font style.</param>
  100. /// <param name="toggle">Toggle value.</param>
  101. public void ToggleFontStyle(FontStyle style, bool toggle)
  102. {
  103. foreach (TextObject text in ModifyList)
  104. {
  105. FontStyle newStyle = text.Font.Style;
  106. if (toggle)
  107. newStyle |= style;
  108. else
  109. newStyle &= ~style;
  110. // some fonts do not support particular styles
  111. try
  112. {
  113. text.Font = new Font(text.Font, newStyle);
  114. }
  115. catch
  116. {
  117. }
  118. }
  119. designer.LastFormatting.Font = First.Font;
  120. designer.SetModified();
  121. }
  122. /// <summary>
  123. /// Sets the horizontal text alignment for tthe selected objects.
  124. /// </summary>
  125. /// <param name="align">Alignment to set.</param>
  126. public void SetHAlign(HorzAlign align)
  127. {
  128. foreach (TextObject text in ModifyList)
  129. {
  130. text.HorzAlign = align;
  131. }
  132. designer.LastFormatting.HorzAlign = align;
  133. designer.SetModified();
  134. }
  135. /// <summary>
  136. /// Sets the vertical text alignment for tthe selected objects.
  137. /// </summary>
  138. /// <param name="align">Alignment to set.</param>
  139. public void SetVAlign(VertAlign align)
  140. {
  141. foreach (TextObject text in ModifyList)
  142. {
  143. text.VertAlign = align;
  144. }
  145. designer.LastFormatting.VertAlign = align;
  146. designer.SetModified();
  147. }
  148. /// <summary>
  149. /// Sets the text color for the selected objects.
  150. /// </summary>
  151. /// <param name="color">Text color.</param>
  152. public void SetTextColor(Color color)
  153. {
  154. foreach (TextObject text in ModifyList)
  155. {
  156. text.TextFill = new SolidFill(color);
  157. }
  158. designer.LastFormatting.TextFill = new SolidFill(color);
  159. designer.SetModified();
  160. }
  161. /// <summary>
  162. /// Sets the angle for the selected objects.
  163. /// </summary>
  164. /// <param name="angle">Angle to set.</param>
  165. public void SetAngle(int angle)
  166. {
  167. foreach (TextObject text in ModifyList)
  168. {
  169. text.Angle = angle;
  170. }
  171. designer.LastFormatting.Angle = angle;
  172. designer.SetModified();
  173. }
  174. /// <summary>
  175. /// Sets the AutoWidth property value for the selected objects.
  176. /// </summary>
  177. /// <param name="value">Value to set.</param>
  178. public void SetAutoWidth(bool value)
  179. {
  180. foreach (TextObject text in ModifyList)
  181. {
  182. text.AutoWidth = value;
  183. }
  184. designer.SetModified();
  185. }
  186. /// <summary>
  187. /// Sets the WordWrap property value for the selected objects.
  188. /// </summary>
  189. /// <param name="value">Value to set.</param>
  190. public void SetWordWrap(bool value)
  191. {
  192. foreach (TextObject text in ModifyList)
  193. {
  194. text.WordWrap = value;
  195. }
  196. designer.SetModified();
  197. }
  198. /// <summary>
  199. /// Sets the highlight conditions for the selected objects.
  200. /// </summary>
  201. /// <param name="value">Highlight conditions.</param>
  202. public void SetConditions(ConditionCollection value)
  203. {
  204. foreach (TextObject text in ModifyList)
  205. {
  206. text.Highlight.Assign(value);
  207. }
  208. designer.SetModified();
  209. }
  210. /// <summary>
  211. /// Clears the text of the selected objects.
  212. /// </summary>
  213. public void ClearText()
  214. {
  215. foreach (TextObject text in ModifyList)
  216. {
  217. text.Text = "";
  218. }
  219. designer.SetModified();
  220. }
  221. /// <summary>
  222. /// Invokes the highlight editor for the selected objects.
  223. /// </summary>
  224. /// <returns><b>true</b> if editor was closed with the OK button.</returns>
  225. public bool InvokeHighlightEditor()
  226. {
  227. using (HighlightEditorForm form = new HighlightEditorForm(designer.ActiveReport))
  228. {
  229. form.Conditions = First.Highlight;
  230. if (form.ShowDialog() == DialogResult.OK)
  231. {
  232. SetConditions(form.Conditions);
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. internal SelectedTextObjects(Designer designer)
  239. {
  240. this.designer = designer;
  241. list = new List<TextObject>();
  242. }
  243. }
  244. }