BandStructure.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.ComponentModel;
  6. using FastReport.Utils;
  7. using FastReport.Forms;
  8. namespace FastReport.Design.PageDesigners.Page
  9. {
  10. #if !DEBUG
  11. [DesignTimeVisible(false)]
  12. #endif
  13. internal class BandStructure : Control
  14. {
  15. private BandStructureNode root;
  16. private ReportPageDesigner pageDesigner;
  17. private int offset;
  18. public Button btnConfigure;
  19. public bool allowPaint = true;
  20. public ReportPage Page
  21. {
  22. get { return pageDesigner.Page as ReportPage; }
  23. }
  24. public Designer Designer
  25. {
  26. get { return pageDesigner.Designer; }
  27. }
  28. public int Offset
  29. {
  30. get { return offset; }
  31. set { offset = value; }
  32. }
  33. private void btnConfigure_Click(object sender, EventArgs e)
  34. {
  35. using (ConfigureBandsForm form = new ConfigureBandsForm(Designer))
  36. {
  37. form.Page = Page;
  38. form.ShowDialog();
  39. }
  40. }
  41. private void AddBand(BandBase band, BandStructureNode root)
  42. {
  43. if (band == null)
  44. return;
  45. BandStructureNode node = root.Add();
  46. node.AddBand(band);
  47. }
  48. private void EnumDataBand(DataBand band, BandStructureNode root)
  49. {
  50. if (band == null)
  51. return;
  52. BandStructureNode node = root.Add();
  53. node.AddBand(band.Header);
  54. node.AddBand(band);
  55. foreach (BandBase b in band.Bands)
  56. {
  57. EnumBand(b, node);
  58. }
  59. node.AddBand(band.Footer);
  60. }
  61. private void EnumGroupHeaderBand(GroupHeaderBand band, BandStructureNode root)
  62. {
  63. if (band == null)
  64. return;
  65. BandStructureNode node = root.Add();
  66. node.AddBand(band.Header);
  67. node.AddBand(band);
  68. EnumGroupHeaderBand(band.NestedGroup, node);
  69. EnumDataBand(band.Data, node);
  70. node.AddBand(band.GroupFooter);
  71. node.AddBand(band.Footer);
  72. }
  73. private void EnumBand(BandBase band, BandStructureNode root)
  74. {
  75. if (band is DataBand)
  76. EnumDataBand(band as DataBand, root);
  77. else if (band is GroupHeaderBand)
  78. EnumGroupHeaderBand(band as GroupHeaderBand, root);
  79. }
  80. private void EnumPageBands()
  81. {
  82. if (Page == null)
  83. return;
  84. if (Page.TitleBeforeHeader)
  85. {
  86. AddBand(Page.ReportTitle, root);
  87. AddBand(Page.PageHeader, root);
  88. }
  89. else
  90. {
  91. AddBand(Page.PageHeader, root);
  92. AddBand(Page.ReportTitle, root);
  93. }
  94. AddBand(Page.ColumnHeader, root);
  95. foreach (BandBase b in Page.Bands)
  96. {
  97. EnumBand(b, root);
  98. }
  99. AddBand(Page.ColumnFooter, root);
  100. AddBand(Page.ReportSummary, root);
  101. AddBand(Page.PageFooter, root);
  102. AddBand(Page.Overlay, root);
  103. }
  104. private void EnumNodes(List<BandStructureNode> list, BandStructureNode root)
  105. {
  106. if (root != this.root)
  107. list.Add(root);
  108. foreach (BandStructureNode node in root.childNodes)
  109. {
  110. EnumNodes(list, node);
  111. }
  112. }
  113. private void DrawItems(Graphics g)
  114. {
  115. List<BandStructureNode> list = new List<BandStructureNode>();
  116. EnumNodes(list, root);
  117. foreach (BandStructureNode node in list)
  118. {
  119. int offs = this.LogicalToDevice(24) + Offset - 2;
  120. RectangleF fillRect = new RectangleF(node.left, node.top + offs, Width - node.left - 1, node.height);
  121. BandBase bandFill = null;
  122. foreach (BandBase band in node.bands)
  123. {
  124. bandFill = band;
  125. if (band is GroupHeaderBand || band is DataBand)
  126. break;
  127. }
  128. // fill node area
  129. bandFill.DrawBandHeader(g, fillRect, false);
  130. g.DrawRectangle(SystemPens.ControlDark, node.left, node.top + offs, Width - node.left - 1, node.height);
  131. // draw band title
  132. float scale = Designer.ZoomDpi;
  133. using (Font f = Designer.LogicalToDevice(DrawUtils.DefaultFont))
  134. {
  135. foreach (BandBase band in node.bands)
  136. {
  137. g.DrawLine(SystemPens.ControlDark, node.left + 8 * Designer.DpiMultiplier(), band.Top * scale + offs, Width, band.Top * scale + offs);
  138. Rectangle textRect = new Rectangle((int)(node.left + 4), (int)(band.Top * scale + offs + 4),
  139. (int)(Width - (node.left + 4) - 4), (int)(band.Height * scale - 4));
  140. ObjectInfo info = RegisteredObjects.FindObject(band);
  141. string text = Res.Get(info.Text);
  142. if (band.GetInfoText() != "")
  143. text += ": " + band.GetInfoText();
  144. int textHeight = TextRenderer.MeasureText(text, f, textRect.Size).Height;
  145. TextFormatFlags flags = textHeight > textRect.Height ?
  146. TextFormatFlags.WordBreak : TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
  147. TextRenderer.DrawText(g, text, f, textRect, SystemColors.WindowText, flags);
  148. if (band.IsAncestor)
  149. g.DrawImage(this.GetImage(99), (int)(node.left + Width - 10), (int)(band.Top * scale + offs + 3));
  150. }
  151. }
  152. }
  153. }
  154. private bool PointInRect(Point point, Rectangle rect)
  155. {
  156. return point.X >= rect.Left && point.X <= rect.Right && point.Y >= rect.Top && point.Y <= rect.Bottom;
  157. }
  158. protected override void OnPaint(PaintEventArgs e)
  159. {
  160. if (pageDesigner.Locked)
  161. return;
  162. root.Clear();
  163. EnumPageBands();
  164. DrawItems(e.Graphics);
  165. }
  166. protected override void OnMouseDown(MouseEventArgs e)
  167. {
  168. base.OnMouseDown(e);
  169. if (pageDesigner.Locked)
  170. return;
  171. List<BandStructureNode> list = new List<BandStructureNode>();
  172. EnumNodes(list, root);
  173. float scale = Designer.ZoomDpi;
  174. int offs = 24 + Offset;
  175. foreach (BandStructureNode node in list)
  176. {
  177. foreach (BandBase band in node.bands)
  178. {
  179. if (PointInRect(e.Location, new Rectangle((int)node.left, (int)(band.Top * scale) + offs,
  180. Width - (int)node.left, (int)(band.Height * scale))))
  181. {
  182. pageDesigner.Workspace.Focus();
  183. Designer.CancelPaste();
  184. Designer.SelectedObjects.Clear();
  185. Designer.SelectedObjects.Add(band);
  186. Designer.SelectionChanged(null);
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. protected override void OnMouseUp(MouseEventArgs e)
  193. {
  194. base.OnMouseUp(e);
  195. if (pageDesigner.Locked)
  196. return;
  197. if (e.Button == MouseButtons.Right && Designer.SelectedObjects.Count > 0
  198. && Designer.SelectedObjects[0] is BandBase)
  199. {
  200. ContextMenuBase menu = Designer.SelectedObjects[0].GetContextMenu();
  201. if (menu != null)
  202. {
  203. menu.Show(this, e.Location);
  204. }
  205. }
  206. }
  207. protected override void OnDoubleClick(EventArgs e)
  208. {
  209. base.OnDoubleClick(e);
  210. if (Designer.SelectedObjects.Count == 1 && Designer.SelectedObjects[0] is BandBase)
  211. {
  212. BandBase band = Designer.SelectedObjects[0] as BandBase;
  213. if (!band.HasRestriction(Restrictions.DontEdit))
  214. band.HandleDoubleClick();
  215. }
  216. }
  217. #if !MONO
  218. private const int WM_PAINT = 0x000F;
  219. protected override void WndProc(ref Message m)
  220. {
  221. if ((m.Msg != WM_PAINT) || (allowPaint && m.Msg == WM_PAINT))
  222. {
  223. base.WndProc(ref m);
  224. }
  225. }
  226. #endif
  227. public void Localize()
  228. {
  229. btnConfigure.Text = Res.Get("Designer,Workspace,Report,ConfigureBands");
  230. }
  231. public void UpdateDpiDependencies()
  232. {
  233. btnConfigure.Font = this.LogicalToDevice(DrawUtils.DefaultFont);
  234. btnConfigure.Height = this.LogicalToDevice(24);
  235. }
  236. public BandStructure(ReportPageDesigner pd)
  237. {
  238. pageDesigner = pd;
  239. root = new BandStructureNode(Designer);
  240. btnConfigure = new Button();
  241. btnConfigure.Dock = DockStyle.Top;
  242. btnConfigure.FlatStyle = FlatStyle.Flat;
  243. btnConfigure.FlatAppearance.BorderColor = SystemColors.ButtonFace;
  244. btnConfigure.FlatAppearance.BorderSize = 0;
  245. btnConfigure.TextAlign = ContentAlignment.MiddleLeft;
  246. btnConfigure.Cursor = Cursors.Hand;
  247. btnConfigure.Click += new EventHandler(btnConfigure_Click);
  248. Controls.Add(btnConfigure);
  249. Localize();
  250. UpdateDpiDependencies();
  251. #if !MONO
  252. SetStyle(ControlStyles.UserPaint, true);
  253. #endif
  254. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  255. SetStyle(ControlStyles.DoubleBuffer, true);
  256. SetStyle(ControlStyles.ResizeRedraw, true);
  257. }
  258. private class BandStructureNode
  259. {
  260. private Designer designer;
  261. private int level;
  262. public float left { get { return level * 8 * designer.DpiMultiplier() - 1; } }
  263. public float top;
  264. public float height;
  265. public List<BandBase> bands;
  266. public List<BandStructureNode> childNodes;
  267. public BandStructureNode parent;
  268. public void AdjustHeight(float height)
  269. {
  270. this.height += height;
  271. if (parent != null)
  272. parent.AdjustHeight(height);
  273. }
  274. public BandStructureNode Add()
  275. {
  276. BandStructureNode result = new BandStructureNode(designer);
  277. childNodes.Add(result);
  278. result.parent = this;
  279. result.level = level + 1;
  280. return result;
  281. }
  282. public void AddBand(BandBase band)
  283. {
  284. if (band != null)
  285. {
  286. if (band.Child != null && band.Child.FillUnusedSpace)
  287. {
  288. AddBand(band.Child);
  289. AddBandInternal(band);
  290. }
  291. else
  292. {
  293. AddBandInternal(band);
  294. AddBand(band.Child);
  295. }
  296. }
  297. }
  298. private void AddBandInternal(BandBase band)
  299. {
  300. if (band != null)
  301. {
  302. bands.Add(band);
  303. float scale = designer.ZoomDpi;
  304. if (bands.Count == 1)
  305. top = band.Top * scale;
  306. float height = band.Height * scale + 4;
  307. AdjustHeight(height);
  308. }
  309. }
  310. public void Clear()
  311. {
  312. bands.Clear();
  313. while (childNodes.Count > 0)
  314. {
  315. childNodes[0].Clear();
  316. childNodes.RemoveAt(0);
  317. }
  318. }
  319. public BandStructureNode(Designer designer)
  320. {
  321. this.designer = designer;
  322. bands = new List<BandBase>();
  323. childNodes = new List<BandStructureNode>();
  324. level = -1;
  325. }
  326. }
  327. }
  328. }