GroupExpertForm.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using FastReport.Utils;
  9. using FastReport.Design;
  10. using FastReport.Design.PageDesigners.Page;
  11. namespace FastReport.Forms
  12. {
  13. internal partial class GroupExpertForm : BaseDialogForm
  14. {
  15. private Designer designer;
  16. private Report report;
  17. private ReportPage page;
  18. private void FillGroups(GroupHeaderBand group)
  19. {
  20. if (group == null)
  21. return;
  22. lbGroups.Items.Add(group);
  23. FillGroups(group.NestedGroup);
  24. }
  25. private void UpdateButtonsState()
  26. {
  27. int itemIndex = lbGroups.SelectedIndex;
  28. btnEdit.Enabled = itemIndex != -1;
  29. btnDelete.Enabled = itemIndex != -1;
  30. btnUp.Enabled = itemIndex > 0;
  31. btnDown.Enabled = itemIndex >= 0 && itemIndex < lbGroups.Items.Count - 1;
  32. }
  33. private void lbGroups_DrawItem(object sender, DrawItemEventArgs e)
  34. {
  35. e.DrawBackground();
  36. if (e.Index >= 0)
  37. {
  38. GroupHeaderBand c = lbGroups.Items[e.Index] as GroupHeaderBand;
  39. this.DrawImageAndText(e, GetImage(RegisteredObjects.FindObject(c).ImageIndex), c.GetInfoText());
  40. }
  41. /*e.Graphics.FillRectangle((e.State & DrawItemState.Selected) != 0 ? SystemBrushes.Highlight : SystemBrushes.Window,
  42. new Rectangle(e.Bounds.X + 22, e.Bounds.Y, e.Bounds.Width - 22, e.Bounds.Height));
  43. if (e.Index < 0)
  44. return;
  45. GroupHeaderBand c = lbGroups.Items[e.Index] as GroupHeaderBand;
  46. Image img = RegisteredObjects.FindObject(c).Image;
  47. e.Graphics.DrawImage(img, e.Bounds.X + 2, e.Bounds.Y);
  48. TextRenderer.DrawText(e.Graphics, c.GetInfoText(), e.Font,
  49. new Point(e.Bounds.X + 24, e.Bounds.Y + (e.Bounds.Height - lbGroups.ItemHeight) / 2), e.ForeColor);*/
  50. }
  51. private void btnAdd_Click(object sender, EventArgs e)
  52. {
  53. if (String.IsNullOrEmpty(cbxDataColumn.Text))
  54. return;
  55. GroupHeaderBand group = new GroupHeaderBand();
  56. group.Condition = cbxDataColumn.Text;
  57. group.SetReport(report);
  58. lbGroups.Items.Add(group);
  59. lbGroups.SelectedItem = group;
  60. }
  61. private void lbGroups_SelectedIndexChanged(object sender, EventArgs e)
  62. {
  63. UpdateButtonsState();
  64. }
  65. private void btnEdit_Click(object sender, EventArgs e)
  66. {
  67. GroupHeaderBand group = lbGroups.SelectedItem as GroupHeaderBand;
  68. group.InvokeEditor();
  69. }
  70. private void btnDelete_Click(object sender, EventArgs e)
  71. {
  72. int index = lbGroups.SelectedIndex;
  73. lbGroups.Items.RemoveAt(index);
  74. if (index >= lbGroups.Items.Count)
  75. index--;
  76. if (index < 0)
  77. index = 0;
  78. if (index < lbGroups.Items.Count)
  79. lbGroups.SelectedIndex = index;
  80. }
  81. private void btnUp_Click(object sender, EventArgs e)
  82. {
  83. object item = lbGroups.SelectedItem;
  84. int index = lbGroups.SelectedIndex;
  85. lbGroups.Items.Remove(item);
  86. lbGroups.Items.Insert(index - 1, item);
  87. lbGroups.SelectedItem = item;
  88. }
  89. private void btnDown_Click(object sender, EventArgs e)
  90. {
  91. object item = lbGroups.SelectedItem;
  92. int index = lbGroups.SelectedIndex;
  93. lbGroups.Items.Remove(item);
  94. lbGroups.Items.Insert(index + 1, item);
  95. lbGroups.SelectedItem = item;
  96. }
  97. private void GroupExpertForm_Shown(object sender, EventArgs e)
  98. {
  99. lblHint.Width = gbGroupCondition.Width - lblHint.Left * 2;
  100. lbGroups.Height = gbGroups.Height - lbGroups.Top - 12;
  101. }
  102. private void GroupExpertForm_FormClosed(object sender, FormClosedEventArgs e)
  103. {
  104. Done();
  105. }
  106. private void Init()
  107. {
  108. cbxDataColumn.Report = report;
  109. // fill existing groups
  110. foreach (BandBase band in page.Bands)
  111. {
  112. if (band is GroupHeaderBand)
  113. {
  114. FillGroups(band as GroupHeaderBand);
  115. break;
  116. }
  117. }
  118. UpdateButtonsState();
  119. }
  120. private void Done()
  121. {
  122. if (DialogResult == DialogResult.OK)
  123. {
  124. float defaultHeight = Units.Millimeters * 10;
  125. if (ReportWorkspace.Grid.GridUnits == PageUnits.Inches ||
  126. ReportWorkspace.Grid.GridUnits == PageUnits.HundrethsOfInch)
  127. defaultHeight = Units.Inches * 0.4f;
  128. GroupHeaderBand initialGroup = null;
  129. DataBand data = null;
  130. int childIndex = -1;
  131. foreach (BandBase band in page.Bands)
  132. {
  133. if (band is GroupHeaderBand)
  134. {
  135. childIndex = band.ZOrder;
  136. initialGroup = band as GroupHeaderBand;
  137. data = (band as GroupHeaderBand).GroupDataBand;
  138. break;
  139. }
  140. else if (band is DataBand)
  141. {
  142. childIndex = band.ZOrder;
  143. data = band as DataBand;
  144. break;
  145. }
  146. }
  147. // report has no groups nor databands, create a databand
  148. if (childIndex == -1)
  149. {
  150. data = new DataBand();
  151. data.Height = defaultHeight;
  152. childIndex = 0;
  153. }
  154. // connect groups to each other
  155. data.Parent = null;
  156. Base parent = null;
  157. for (int i = 0; i < lbGroups.Items.Count; i++)
  158. {
  159. GroupHeaderBand group = lbGroups.Items[i] as GroupHeaderBand;
  160. group.Parent = parent;
  161. group.Data = i < lbGroups.Items.Count - 1 ? null : data;
  162. parent = group;
  163. }
  164. // insert a group to the report page
  165. if (lbGroups.Items.Count > 0)
  166. {
  167. GroupHeaderBand firstGroup = lbGroups.Items[0] as GroupHeaderBand;
  168. page.Bands.Insert(childIndex, firstGroup);
  169. // create unique names
  170. if (String.IsNullOrEmpty(firstGroup.GroupDataBand.Name))
  171. firstGroup.GroupDataBand.CreateUniqueName();
  172. for (int i = 0; i < lbGroups.Items.Count; i++)
  173. {
  174. GroupHeaderBand group = lbGroups.Items[i] as GroupHeaderBand;
  175. if (String.IsNullOrEmpty(group.Name))
  176. {
  177. group.Height = defaultHeight;
  178. group.CreateUniqueName();
  179. // create text object with group name
  180. TextObject text = new TextObject();
  181. text.Parent = group;
  182. text.CreateUniqueName();
  183. text.Bounds = new RectangleF(new PointF(0, 0), text.GetPreferredSize());
  184. text.Text = "[" + group.Condition + "]";
  185. // create group footer
  186. group.GroupFooter = new GroupFooterBand();
  187. group.GroupFooter.Height = defaultHeight;
  188. group.GroupFooter.CreateUniqueName();
  189. }
  190. }
  191. }
  192. else
  193. {
  194. if (!String.IsNullOrEmpty(data.Name))
  195. page.Bands.Insert(childIndex, data);
  196. }
  197. // delete initial group if it was deleted in the expert
  198. if (initialGroup != null && !lbGroups.Items.Contains(initialGroup))
  199. initialGroup.Dispose();
  200. }
  201. }
  202. public override void Localize()
  203. {
  204. base.Localize();
  205. MyRes res = new MyRes("Forms,GroupExpert");
  206. Text = res.Get("");
  207. gbGroupCondition.Text = res.Get("Condition");
  208. lblHint.Text = res.Get("Hint");
  209. btnAdd.Text = res.Get("Add");
  210. gbGroups.Text = res.Get("Groups");
  211. btnEdit.Text = res.Get("Edit");
  212. btnDelete.Text = res.Get("Delete");
  213. }
  214. public override void UpdateDpiDependencies()
  215. {
  216. base.UpdateDpiDependencies();
  217. btnUp.Image = GetImage(208);
  218. btnDown.Image = GetImage(209);
  219. lbGroups.ItemHeight = this.LogicalToDevice(17);
  220. }
  221. public GroupExpertForm(Designer designer)
  222. {
  223. this.designer = designer;
  224. report = designer.ActiveReport;
  225. page = designer.ActiveReportTab.ActivePage as ReportPage;
  226. InitializeComponent();
  227. Init();
  228. Localize();
  229. UIUtils.CheckRTL(this);
  230. UpdateDpiDependencies();
  231. }
  232. }
  233. }