Ruler.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. using System;
  2. using System.Collections;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.ComponentModel;
  7. using FastReport.Utils;
  8. using FastReport.Editor;
  9. using System.Collections.Generic;
  10. using FastReport.TypeConverters;
  11. namespace FastReport.Design.PageDesigners.Page
  12. {
  13. #if !DEBUG
  14. [DesignTimeVisible(false)]
  15. #endif
  16. internal class RulerBase : Control
  17. {
  18. public bool allowPaint = true;
  19. protected ToolTip toolTip;
  20. private ReportPageDesigner pageDesigner;
  21. private float offset;
  22. public float Offset
  23. {
  24. get { return offset; }
  25. set { offset = value; }
  26. }
  27. public ReportWorkspace Workspace
  28. {
  29. get { return pageDesigner.Workspace; }
  30. }
  31. public ReportPageDesigner PageDesigner
  32. {
  33. get { return pageDesigner; }
  34. }
  35. public Designer Designer
  36. {
  37. get { return pageDesigner.Designer; }
  38. }
  39. protected bool CheckGridStep(ref float kx, ref float ky)
  40. {
  41. bool al = ReportWorkspace.SnapToGrid;
  42. if (ModifierKeys == Keys.Alt)
  43. al = !al;
  44. bool result = true;
  45. float grid = ReportWorkspace.Grid.SnapSize;
  46. if (al)
  47. {
  48. result = kx >= grid || kx <= -grid || ky >= grid || ky <= -grid;
  49. if (result)
  50. {
  51. kx = (int)(kx / grid) * grid;
  52. ky = (int)(ky / grid) * grid;
  53. }
  54. }
  55. return result;
  56. }
  57. #if !MONO
  58. private const int WM_PAINT = 0x000F;
  59. protected override void WndProc(ref Message m)
  60. {
  61. if ((m.Msg != WM_PAINT) || (allowPaint && m.Msg == WM_PAINT))
  62. {
  63. base.WndProc(ref m);
  64. }
  65. }
  66. #endif
  67. public RulerBase(ReportPageDesigner pd) : base()
  68. {
  69. pageDesigner = pd;
  70. toolTip = new ToolTip();
  71. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  72. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  73. }
  74. }
  75. #if !DEBUG
  76. [DesignTimeVisible(false)]
  77. #endif
  78. internal class HorzRuler : RulerBase
  79. {
  80. private PointF lastMousePoint;
  81. private bool mouseDown;
  82. private bool mouseMoved;
  83. private int activeGuide;
  84. private bool saveAutoGuides;
  85. private void DrawRuler(Graphics g, float start, float size)
  86. {
  87. Font font = new Font("Tahoma", 6f * this.FontDpiMultiplier());
  88. Brush brush = SystemBrushes.WindowText;
  89. Pen pen = SystemPens.WindowText;
  90. int w5 = 5;
  91. int w10 = 10;
  92. float dx = (ReportWorkspace.Grid.GridUnits == PageUnits.Millimeters || ReportWorkspace.Grid.GridUnits == PageUnits.Centimeters ?
  93. Units.Millimeters : Units.TenthsOfInch) * Designer.ZoomDpi;
  94. string s = (((int)Math.Round(size / dx)) / 10).ToString();
  95. float maxw = g.MeasureString(s, font).Width;
  96. float i = start;
  97. int i1 = 0;
  98. while (i < start + size)
  99. {
  100. int h = 0;
  101. if (i1 == 0)
  102. h = 0;
  103. else if (i1 % w10 == 0)
  104. h = 6;
  105. else if (i1 % w5 == 0)
  106. h = 4;
  107. else
  108. h = 2;
  109. if (h == 2 && dx * w10 < this.LogicalToDevice(41))
  110. h = 0;
  111. if (h == 4 && dx * w10 < this.LogicalToDevice(21))
  112. h = 0;
  113. int w = 0;
  114. if (h == 6)
  115. {
  116. if (maxw > dx * w10 * 1.5f)
  117. w = w10 * 4;
  118. else if (maxw > dx * w10 * 0.7f)
  119. w = w10 * 2;
  120. else
  121. w = w10;
  122. }
  123. if (w != 0 && i1 % w == 0)
  124. {
  125. if (ReportWorkspace.Grid.GridUnits == PageUnits.Millimeters)
  126. s = (i1).ToString();
  127. else if (ReportWorkspace.Grid.GridUnits == PageUnits.HundrethsOfInch)
  128. s = (i1 * 10).ToString();
  129. else
  130. s = (i1 / 10).ToString();
  131. SizeF sz = g.MeasureString(s, font);
  132. g.DrawString(s, font, brush, new PointF(Offset + i - sz.Width / 2 + 1, this.LogicalToDevice(7)));
  133. }
  134. else if (h != 0)
  135. {
  136. g.DrawLine(pen,
  137. Offset + i, this.LogicalToDevice(6 + (13 - h) / 2),
  138. Offset + i, this.LogicalToDevice(6 + (13 - h) / 2 + h - 1));
  139. }
  140. i += dx;
  141. i1++;
  142. }
  143. }
  144. private void DrawRulerRtl(Graphics g, float start, float size)
  145. {
  146. Font font = new Font("Tahoma", 6f * this.FontDpiMultiplier());
  147. Brush brush = SystemBrushes.WindowText;
  148. Pen pen = SystemPens.WindowText;
  149. int w5 = 5;
  150. int w10 = 10;
  151. float dx = (ReportWorkspace.Grid.GridUnits == PageUnits.Millimeters || ReportWorkspace.Grid.GridUnits == PageUnits.Centimeters ?
  152. Units.Millimeters : Units.TenthsOfInch) * Designer.ZoomDpi;
  153. string s = (((int)Math.Round(size / dx)) / 10).ToString();
  154. float maxw = g.MeasureString(s, font).Width;
  155. float i = start + size;
  156. int i1 = 0;
  157. while (i > start)
  158. {
  159. int h = 0;
  160. if (i1 == 0)
  161. h = 0;
  162. else if (i1 % w10 == 0)
  163. h = 6;
  164. else if (i1 % w5 == 0)
  165. h = 4;
  166. else
  167. h = 2;
  168. if (h == 2 && dx * w10 < this.LogicalToDevice(41))
  169. h = 0;
  170. if (h == 4 && dx * w10 < this.LogicalToDevice(21))
  171. h = 0;
  172. int w = 0;
  173. if (h == 6)
  174. {
  175. if (maxw > dx * w10 * 1.5f)
  176. w = w10 * 4;
  177. else if (maxw > dx * w10 * 0.7f)
  178. w = w10 * 2;
  179. else
  180. w = w10;
  181. }
  182. if (w != 0 && i1 % w == 0)
  183. {
  184. if (ReportWorkspace.Grid.GridUnits == PageUnits.Millimeters)
  185. s = (i1).ToString();
  186. else if (ReportWorkspace.Grid.GridUnits == PageUnits.HundrethsOfInch)
  187. s = (i1 * 10).ToString();
  188. else
  189. s = (i1 / 10).ToString();
  190. SizeF sz = g.MeasureString(s, font);
  191. g.DrawString(s, font, brush, new PointF(Offset + i - sz.Width / 2 + 1, this.LogicalToDevice(7)));
  192. }
  193. else if (h != 0)
  194. {
  195. g.DrawLine(pen,
  196. Offset + i, this.LogicalToDevice(6 + (13 - h) / 2),
  197. Offset + i, this.LogicalToDevice(6 + (13 - h) / 2 + h - 1));
  198. }
  199. i -= dx;
  200. i1++;
  201. }
  202. }
  203. private void DrawGuides(Graphics g)
  204. {
  205. FloatCollection guides = Workspace.Page.Guides;
  206. if (guides == null)
  207. return;
  208. if (guides.Count > activeGuide && activeGuide != -1)
  209. {
  210. toolTip.Show(Converter.ToString((Workspace.Page.LeftMargin * Units.Millimeters + guides[activeGuide]) / ReportWorkspace.Grid.SnapSize * 10 / 4, typeof(PaperConverter))
  211. + " | " +
  212. Converter.ToString((Workspace.Page.PaperWidth * Units.Millimeters - Workspace.Page.LeftMargin * Units.Millimeters - guides[activeGuide]) / ReportWorkspace.Grid.SnapSize * 10 / 4, typeof(PaperConverter)),
  213. this, (int)Math.Round(Offset + guides[activeGuide] * Designer.ZoomDpi - 4), 32
  214. );
  215. }
  216. if (activeGuide == -1)
  217. toolTip.Hide(this);
  218. for (int i = 0; i < guides.Count; i++)
  219. {
  220. Image b = this.GetImage(i == activeGuide ? 174 : 74);
  221. g.DrawImage(b, (int)Math.Round(Offset + guides[i] * Designer.ZoomDpi - 4), 16);
  222. }
  223. }
  224. private void MoveGuide(float kx)
  225. {
  226. Workspace.Guides.MoveVGuide(activeGuide, kx);
  227. float f = Workspace.Page.Guides[activeGuide];
  228. f += kx;
  229. Workspace.Page.Guides[activeGuide] = Converter.DecreasePrecision(f, 2);
  230. Workspace.Refresh();
  231. Refresh();
  232. }
  233. private void FixGuide(bool remove)
  234. {
  235. float f = Workspace.Page.Guides[activeGuide];
  236. if (remove || f < 0 || f > Workspace.Width / Designer.ZoomDpi)
  237. Workspace.Page.Guides.RemoveAt(activeGuide);
  238. activeGuide = -1;
  239. Refresh();
  240. }
  241. protected override void OnPaint(PaintEventArgs e)
  242. {
  243. if (Workspace.Locked)
  244. return;
  245. Graphics g = e.Graphics;
  246. g.SetClip(new RectangleF(Height, 0, Width - Height, Height));
  247. g.FillRectangle(SystemBrushes.Window, new RectangleF(
  248. Offset, this.LogicalToDevice(5),
  249. Workspace.Width, Height - this.LogicalToDevice(10)));
  250. if (Config.RightToLeft)
  251. {
  252. DrawRulerRtl(g, 0, Workspace.Width);
  253. }
  254. else
  255. {
  256. DrawRuler(g, 0, Workspace.Width);
  257. }
  258. DrawGuides(g);
  259. }
  260. protected override void OnMouseDown(MouseEventArgs e)
  261. {
  262. base.OnMouseDown(e);
  263. if (Workspace.Locked)
  264. return;
  265. float scale = Designer.ZoomDpi;
  266. lastMousePoint = new PointF(e.X / scale, e.Y / scale);
  267. mouseDown = true;
  268. mouseMoved = false;
  269. if (activeGuide != -1)
  270. Workspace.Guides.BeforeMoveVGuide(activeGuide);
  271. saveAutoGuides = ReportWorkspace.AutoGuides;
  272. ReportWorkspace.AutoGuides = false;
  273. }
  274. protected override void OnMouseMove(MouseEventArgs e)
  275. {
  276. base.OnMouseMove(e);
  277. if (Workspace.Locked)
  278. return;
  279. float scale = Designer.ZoomDpi;
  280. if (e.Button == MouseButtons.None)
  281. {
  282. // find guide
  283. FloatCollection guides = Workspace.Page.Guides;
  284. if (guides == null)
  285. return;
  286. float x = (e.X - Offset) / scale;
  287. activeGuide = -1;
  288. for (int i = 0; i < guides.Count; i++)
  289. {
  290. if (x > guides[i] - 5 && x < guides[i] + 5)
  291. {
  292. activeGuide = i;
  293. break;
  294. }
  295. }
  296. Refresh();
  297. }
  298. else if (e.Button == MouseButtons.Left)
  299. {
  300. if (activeGuide == -1)
  301. return;
  302. float kx = e.X / scale - lastMousePoint.X;
  303. float ky = e.Y / scale - lastMousePoint.Y;
  304. if (!CheckGridStep(ref kx, ref ky))
  305. return;
  306. mouseMoved = true;
  307. MoveGuide(kx);
  308. lastMousePoint.X += kx;
  309. lastMousePoint.Y += ky;
  310. }
  311. }
  312. protected override void OnMouseUp(MouseEventArgs e)
  313. {
  314. base.OnMouseUp(e);
  315. if (Workspace.Locked || !mouseDown)
  316. return;
  317. ReportWorkspace.AutoGuides = saveAutoGuides;
  318. mouseDown = false;
  319. if (mouseMoved)
  320. {
  321. FixGuide(e.Y < -5 || e.Y > Height + 5);
  322. Workspace.Designer.SetModified(null, "MoveGuide");
  323. }
  324. else
  325. {
  326. // create new guide
  327. float x = (e.X - Offset) / Designer.ZoomDpi;
  328. if (x < Workspace.Width && e.Y > 5 && e.Y < 20)
  329. {
  330. if (ReportWorkspace.SnapToGrid)
  331. x = (int)(x / ReportWorkspace.Grid.SnapSize) * ReportWorkspace.Grid.SnapSize;
  332. if (Workspace.Page.Guides == null)
  333. Workspace.Page.Guides = new FloatCollection();
  334. Workspace.Page.Guides.Add(x);
  335. Workspace.Designer.SetModified(null, "AddGuide");
  336. }
  337. }
  338. }
  339. protected override void OnMouseLeave(EventArgs e)
  340. {
  341. base.OnMouseLeave(e);
  342. if (Workspace.Locked)
  343. return;
  344. if (!mouseDown && activeGuide != -1)
  345. {
  346. activeGuide = -1;
  347. Refresh();
  348. }
  349. }
  350. public HorzRuler(ReportPageDesigner pd) : base(pd)
  351. {
  352. activeGuide = -1;
  353. }
  354. }
  355. #if !DEBUG
  356. [DesignTimeVisible(false)]
  357. #endif
  358. internal class VertRuler : RulerBase
  359. {
  360. private PointF lastMousePoint;
  361. private bool mouseDown;
  362. private bool mouseMoved;
  363. private int activeGuide;
  364. private BandBase activeBand;
  365. private bool resizing;
  366. private bool saveAutoGuides;
  367. private void DrawRuler(Graphics g, float start, float size)
  368. {
  369. Font font = new Font("Tahoma", 6f * this.FontDpiMultiplier());
  370. Brush brush = SystemBrushes.WindowText;
  371. Pen pen = SystemPens.WindowText;
  372. int w5 = 5;
  373. int w10 = 10;
  374. float dx = (ReportWorkspace.Grid.GridUnits == PageUnits.Millimeters || ReportWorkspace.Grid.GridUnits == PageUnits.Centimeters ?
  375. Units.Millimeters : Units.TenthsOfInch) * Designer.ZoomDpi;
  376. string s = (((int)Math.Round(size / dx)) / 10).ToString();
  377. float maxw = g.MeasureString(s, font).Width;
  378. float i = start;
  379. int i1 = 0;
  380. while (i < start + size)
  381. {
  382. int h = 0;
  383. if (i1 == 0)
  384. h = 0;
  385. else if (i1 % w10 == 0)
  386. h = 6;
  387. else if (i1 % w5 == 0)
  388. h = 4;
  389. else
  390. h = 2;
  391. if (h == 2 && dx * w10 < this.LogicalToDevice(41))
  392. h = 0;
  393. if (h == 4 && dx * w10 < this.LogicalToDevice(21))
  394. h = 0;
  395. int w = 0;
  396. if (h == 6)
  397. {
  398. if (maxw > dx * w10 * 1.5f)
  399. w = w10 * 4;
  400. else if (maxw > dx * w10 * 0.7f)
  401. w = w10 * 2;
  402. else
  403. w = w10;
  404. }
  405. if (w != 0 && i1 % w == 0)
  406. {
  407. if (ReportWorkspace.Grid.GridUnits == PageUnits.Millimeters)
  408. s = (i1).ToString();
  409. else if (ReportWorkspace.Grid.GridUnits == PageUnits.HundrethsOfInch)
  410. s = (i1 * 10).ToString();
  411. else
  412. s = (i1 / 10).ToString();
  413. SizeF sz = g.MeasureString(s, font);
  414. PointF p = new PointF(Width - sz.Height - this.LogicalToDevice(7), Offset + i + sz.Width / 2);
  415. GraphicsState state = g.Save();
  416. g.TranslateTransform(p.X + sz.Height / 2, p.Y + sz.Width / 2);
  417. g.RotateTransform(-90);
  418. p.X /= 2;
  419. p.Y = -sz.Height / 2;
  420. g.DrawString(s, font, brush, p);
  421. g.Restore(state);
  422. }
  423. else if (h != 0)
  424. {
  425. g.DrawLine(pen,
  426. this.LogicalToDevice(6 + (13 - h) / 2), Offset + i,
  427. this.LogicalToDevice(6 + (13 - h) / 2 + h - 1), Offset + i);
  428. }
  429. i += dx;
  430. i1++;
  431. }
  432. }
  433. private void DrawGuides(Graphics g, BandBase band)
  434. {
  435. FloatCollection guides = band.Guides;
  436. if (guides == null)
  437. return;
  438. if (band == activeBand && guides.Count > activeGuide && activeGuide != -1)
  439. {
  440. toolTip.Show(Converter.ToString((Workspace.Page.TopMargin * Units.Millimeters + (band.Top + guides[activeGuide])) / ReportWorkspace.Grid.SnapSize * 10 / 4, typeof(PaperConverter))
  441. + " | "
  442. + Converter.ToString((Workspace.Page.PaperHeight * Units.Millimeters - Workspace.Page.TopMargin * Units.Millimeters - (band.Top + guides[activeGuide])) / ReportWorkspace.Grid.SnapSize * 10 / 4, typeof(PaperConverter)),
  443. this, 32, (int)Math.Round(Offset + (band.Top + guides[activeGuide]) * Designer.ZoomDpi + 10)
  444. );
  445. }
  446. if (activeGuide == -1)
  447. toolTip.Hide(this);
  448. for (int i = 0; i < guides.Count; i++)
  449. {
  450. Image b = this.GetImage(band == activeBand && i == activeGuide ? 173 : 73);
  451. g.DrawImage(b, 16, (int)Math.Round(Offset + (band.Top + guides[i]) * Designer.ZoomDpi - 4));
  452. }
  453. }
  454. private BandCollection GetBands()
  455. {
  456. BandCollection bands = new BandCollection();
  457. ObjectCollection objects = PageDesigner.Page.AllObjects;
  458. foreach (Base c in objects)
  459. {
  460. if (c is BandBase)
  461. bands.Add(c as BandBase);
  462. }
  463. return bands;
  464. }
  465. private BandBase BandAt(float y)
  466. {
  467. BandCollection bands = GetBands();
  468. foreach (BandBase b in bands)
  469. {
  470. if (y >= b.Top && y <= b.Bottom + (ReportWorkspace.ClassicView ? BandBase.HeaderSize : 4))
  471. return b;
  472. }
  473. return null;
  474. }
  475. private void MoveGuide(float ky)
  476. {
  477. Workspace.Guides.MoveHGuide(activeBand, activeGuide, ky);
  478. float f = activeBand.Guides[activeGuide];
  479. f += ky;
  480. activeBand.Guides[activeGuide] = Converter.DecreasePrecision(f, 2);
  481. Workspace.Refresh();
  482. Refresh();
  483. }
  484. private void ResizeBand(float ky)
  485. {
  486. activeBand.Height += ky;
  487. activeBand.FixHeight();
  488. Workspace.UpdateBands();
  489. }
  490. private void FixGuide(bool remove)
  491. {
  492. float f = activeBand.Guides[activeGuide];
  493. if (remove || f < 0 || f > activeBand.Height)
  494. activeBand.Guides.RemoveAt(activeGuide);
  495. activeGuide = -1;
  496. Refresh();
  497. }
  498. protected override void OnPaint(PaintEventArgs e)
  499. {
  500. if (Workspace.Locked)
  501. return;
  502. Graphics g = e.Graphics;
  503. // highlight bands list
  504. Hashtable bandsToHighlight = new Hashtable();
  505. if (ReportWorkspace.EnableBacklight)
  506. {
  507. foreach (Base obj in Designer.SelectedObjects)
  508. {
  509. BandBase band = null;
  510. if (obj is BandBase)
  511. band = obj as BandBase;
  512. else if (obj is ReportComponentBase)
  513. band = (obj as ReportComponentBase).Band;
  514. if (band != null)
  515. bandsToHighlight[band] = 1;
  516. }
  517. }
  518. float scale = Designer.ZoomDpi;
  519. BandCollection bands = GetBands();
  520. foreach (BandBase b in bands)
  521. {
  522. Brush brush = bandsToHighlight.ContainsKey(b) ? Brushes.Gainsboro : SystemBrushes.Window;
  523. g.FillRectangle(brush, new RectangleF(
  524. this.LogicalToDevice(5), Offset + b.Top * scale,
  525. Width - this.LogicalToDevice(10), b.Height * scale + 1));
  526. DrawRuler(g, b.Top * scale, b.Height * scale);
  527. DrawGuides(g, b);
  528. if (ReportWorkspace.ClassicView)
  529. {
  530. RectangleF fillRect = new RectangleF(
  531. this.LogicalToDevice(5), Offset + (b.Top - (BandBase.HeaderSize - 1)) * scale,
  532. Width - this.LogicalToDevice(10), (BandBase.HeaderSize - 1) * scale);
  533. if (b.Top == BandBase.HeaderSize)
  534. {
  535. fillRect.Y = 0;
  536. fillRect.Height += scale;
  537. }
  538. b.DrawBandHeader(g, fillRect, true);
  539. if (b.Top > BandBase.HeaderSize)
  540. {
  541. // draw splitter lines
  542. float lineY = fillRect.Top + fillRect.Height / 2 - this.LogicalToDevice(2);
  543. for (int i = 0; i < 6; i += 2)
  544. {
  545. g.DrawLine(SystemPens.ControlDarkDark,
  546. this.LogicalToDevice(9), lineY + this.LogicalToDevice(i),
  547. Width - this.LogicalToDevice(10), lineY + this.LogicalToDevice(i));
  548. }
  549. }
  550. }
  551. }
  552. }
  553. protected override void OnMouseDown(MouseEventArgs e)
  554. {
  555. base.OnMouseDown(e);
  556. if (Workspace.Locked)
  557. return;
  558. float scale = Designer.ZoomDpi;
  559. lastMousePoint = new PointF(e.X / scale, e.Y / scale);
  560. mouseDown = true;
  561. mouseMoved = false;
  562. if (activeBand != null && activeGuide != -1)
  563. Workspace.Guides.BeforeMoveHGuide(activeBand, activeGuide);
  564. saveAutoGuides = ReportWorkspace.AutoGuides;
  565. ReportWorkspace.AutoGuides = false;
  566. }
  567. protected override void OnMouseMove(MouseEventArgs e)
  568. {
  569. base.OnMouseMove(e);
  570. if (Workspace.Locked)
  571. return;
  572. float scale = Designer.ZoomDpi;
  573. if (e.Button == MouseButtons.None)
  574. {
  575. // find band
  576. float y = (e.Y - Offset) / scale;
  577. Cursor = Cursors.Default;
  578. resizing = false;
  579. activeGuide = -1;
  580. activeBand = BandAt(y);
  581. if (activeBand != null)
  582. {
  583. // check band resize
  584. if (y > activeBand.Bottom - 1 &&
  585. y < activeBand.Bottom + (ReportWorkspace.ClassicView ? BandBase.HeaderSize : 4))
  586. {
  587. resizing = true;
  588. Cursor = Cursors.HSplit;
  589. }
  590. else
  591. {
  592. // check guides
  593. FloatCollection guides = activeBand.Guides;
  594. if (guides != null)
  595. {
  596. for (int i = 0; i < guides.Count; i++)
  597. {
  598. if (y > activeBand.Top + guides[i] - 5 &&
  599. y < activeBand.Top + guides[i] + 5)
  600. {
  601. activeGuide = i;
  602. break;
  603. }
  604. }
  605. }
  606. }
  607. }
  608. Refresh();
  609. }
  610. else if (e.Button == MouseButtons.Left)
  611. {
  612. float kx = e.X / scale - lastMousePoint.X;
  613. float ky = e.Y / scale - lastMousePoint.Y;
  614. if (!CheckGridStep(ref kx, ref ky))
  615. return;
  616. if (activeBand != null)
  617. {
  618. if (resizing)
  619. {
  620. mouseMoved = true;
  621. ResizeBand(ky);
  622. }
  623. else if (activeGuide != -1)
  624. {
  625. mouseMoved = true;
  626. MoveGuide(ky);
  627. }
  628. }
  629. lastMousePoint.X += kx;
  630. lastMousePoint.Y += ky;
  631. }
  632. }
  633. protected override void OnMouseUp(MouseEventArgs e)
  634. {
  635. base.OnMouseUp(e);
  636. if (Workspace.Locked)
  637. return;
  638. ReportWorkspace.AutoGuides = saveAutoGuides;
  639. mouseDown = false;
  640. if (mouseMoved)
  641. {
  642. if (resizing)
  643. activeBand.FixHeight();
  644. else
  645. FixGuide(e.X < -this.LogicalToDevice(5) || e.X > Width + this.LogicalToDevice(5));
  646. Workspace.Designer.SetModified(null, resizing ? "ResizeBand" : "MoveGuide");
  647. }
  648. else
  649. {
  650. // create new guide
  651. if (e.X > this.LogicalToDevice(5) && e.X < this.LogicalToDevice(20))
  652. {
  653. float y = (e.Y - Offset) / Designer.ZoomDpi;
  654. BandBase band = BandAt(y);
  655. if (band != null)
  656. {
  657. y = y - band.Top;
  658. if (ReportWorkspace.SnapToGrid)
  659. y = (int)(y / ReportWorkspace.Grid.SnapSize) * ReportWorkspace.Grid.SnapSize;
  660. if (band.Guides == null)
  661. band.Guides = new FloatCollection();
  662. band.Guides.Add(y);
  663. Workspace.Designer.SetModified(null, "AddGuide");
  664. }
  665. }
  666. }
  667. }
  668. protected override void OnMouseLeave(EventArgs e)
  669. {
  670. base.OnMouseLeave(e);
  671. if (Workspace.Locked)
  672. return;
  673. if (!mouseDown && activeGuide != -1)
  674. {
  675. activeGuide = -1;
  676. Refresh();
  677. }
  678. }
  679. public VertRuler(ReportPageDesigner pd) : base(pd)
  680. {
  681. activeGuide = -1;
  682. }
  683. }
  684. #if !MONO
  685. /// <summary>
  686. /// Represent ruler with guides for forms of editors
  687. /// </summary>
  688. public class RulerWithGuides : EditRuler
  689. {
  690. private bool mouseDown = false;
  691. private Pen pen;
  692. private List<RulerElement> rulerElements;
  693. private RulerElement activElement;
  694. private int countTabPos;
  695. private bool leftIndentIsActive = false;
  696. private bool rightIndentIsActive = false;
  697. #region Properties
  698. /// <summary>
  699. /// Get or set left indent position
  700. /// </summary>
  701. public int LeftIndent
  702. {
  703. get
  704. {
  705. return leftIndent.Indent;
  706. }
  707. set
  708. {
  709. leftIndent.Indent = value;
  710. Invalidate();
  711. }
  712. }
  713. /// <summary>
  714. /// Get or set right indent position
  715. /// </summary>
  716. public int RightIndent
  717. {
  718. get
  719. {
  720. return rightIndent.Indent;
  721. }
  722. set
  723. {
  724. rightIndent.Indent = value;
  725. Invalidate();
  726. }
  727. }
  728. /// <summary>
  729. /// Gets or sets tab positiions
  730. /// </summary>
  731. public int[] TabPositions
  732. {
  733. get
  734. {
  735. List<int> result = new List<int>();
  736. foreach (var elem in rulerElements)
  737. {
  738. if(elem is TabPosition)
  739. {
  740. result.Add(elem.Bounds.X + 4);
  741. }
  742. }
  743. result.Sort();
  744. return result.ToArray();
  745. }
  746. set
  747. {
  748. for (int i = 0; i < rulerElements.Count; i++)
  749. {
  750. if (rulerElements[i] is TabPosition)
  751. {
  752. rulerElements.Remove(rulerElements[i]);
  753. i--;
  754. }
  755. }
  756. for (int i = 0; i < value.Length; i++)
  757. {
  758. rulerElements.Add(new TabPosition(value[i]));
  759. }
  760. Invalidate();
  761. }
  762. }
  763. #endregion
  764. #region Protected method
  765. /// <inheritdoc/>
  766. protected override void OnDoubleClick(EventArgs e)
  767. {
  768. base.OnDoubleClick(e);
  769. MouseEventArgs me = e as MouseEventArgs;
  770. if (me.X > RulerStart && me.X < RulerWidth && me.Button == MouseButtons.Left && !TabPosExist(me.X) &&
  771. countTabPos + 1 <= 32 && !leftIndentIsActive && !rightIndentIsActive && activElement == null)
  772. {
  773. rulerElements.Add(new TabPosition(me.X));
  774. countTabPos++;
  775. OnChange(this);
  776. }
  777. }
  778. /// <inheritdoc/>
  779. protected override Rectangle GetIndentHitRect(RulerIndent Indent)
  780. {
  781. Rectangle result = Rectangle.Empty;
  782. if (Vertical)
  783. {
  784. //if (Indent.Orientation == IndentOrientation.Near)
  785. // result = new Rectangle(cRulerTop, RulerStart + Indent.Indent - EditConsts.DefaultRulerHitWidth, cRulerHeight, EditConsts.DefaultRulerHitWidth * 2);
  786. //else
  787. // result = new Rectangle(cRulerTop, PageStart + PageWidth - EditConsts.DefaultRulerHitWidth, cRulerHeight, EditConsts.DefaultRulerHitWidth * 2);
  788. }
  789. else
  790. {
  791. if (Indent.Orientation == IndentOrientation.Near)
  792. result = new Rectangle(RulerStart + Indent.Indent - EditConsts.DefaultRulerHitWidth, cRulerTop, EditConsts.DefaultRulerHitWidth * 2, cRulerHeight);
  793. else
  794. result = new Rectangle(RulerStart + RulerWidth - EditConsts.DefaultRulerHitWidth - Indent.Indent, cRulerTop, EditConsts.DefaultRulerHitWidth * 2, cRulerHeight);
  795. }
  796. return result;
  797. }
  798. /// <inheritdoc/>
  799. protected override Rectangle GetIndentRect(RulerIndent indent)
  800. {
  801. Rectangle result = Rectangle.Empty;
  802. if (Vertical)
  803. {
  804. //if (indent.Orientation == IndentOrientation.Near)
  805. // result = new Rectangle(cRulerTop, RulerStart, cRulerHeight, indent.Indent);
  806. //else
  807. // result = new Rectangle(cRulerTop, PageWidth + PageStart, cRulerHeight, indent.Indent);
  808. }
  809. else
  810. {
  811. if (indent.Orientation == IndentOrientation.Near)
  812. result = new Rectangle(RulerStart, cRulerTop, indent.Indent, cRulerHeight);
  813. else
  814. result = new Rectangle(RulerWidth + RulerStart - indent.Indent, cRulerTop, indent.Indent, cRulerHeight);
  815. }
  816. return result;
  817. }
  818. /// <inheritdoc/>
  819. protected override void OnPaint(PaintEventArgs pe)
  820. {
  821. base.OnPaint(pe);
  822. foreach(var elem in rulerElements)
  823. {
  824. elem.Draw(pe.Graphics);
  825. }
  826. }
  827. /// <inheritdoc/>
  828. protected override void OnMouseDown(MouseEventArgs e)
  829. {
  830. base.OnMouseDown(e);
  831. mouseDown = true;
  832. if (RulerWidth - RightIndent + RulerStart + 2 >= e.X && RulerWidth - RightIndent + RulerStart - 4 <= e.X && e.Button != MouseButtons.Right)
  833. rightIndentIsActive = true;
  834. else if (LeftIndent + RulerStart - 4 <= e.X && LeftIndent + RulerStart + 2 >= e.X && e.Button != MouseButtons.Right)
  835. leftIndentIsActive = true;
  836. else
  837. foreach (var elem in rulerElements)
  838. {
  839. if (elem.Bounds.Contains(e.Location))
  840. {
  841. if (e.Button == MouseButtons.Right && elem is TabPosition)
  842. {
  843. rulerElements.Remove(elem);
  844. countTabPos--;
  845. }
  846. else
  847. {
  848. activElement = elem;
  849. activElement.IsActive = true;
  850. if (e.X > RulerWidth + RulerStart - RightIndent)
  851. activElement.Move(new Point(RulerWidth + RulerStart - RightIndent, 0));
  852. else if(e.X < RulerStart + LeftIndent)
  853. activElement.Move(new Point(RulerStart + LeftIndent, 0));
  854. }
  855. break;
  856. }
  857. }
  858. OnChange(this);
  859. }
  860. /// <inheritdoc/>
  861. protected override void OnMouseMove(MouseEventArgs e)
  862. {
  863. int oldLeftIndent = LeftIndent;
  864. base.OnMouseMove(e);
  865. if (mouseDown && activElement != null && e.X < RulerWidth + RulerStart - RightIndent && e.X > RulerStart + LeftIndent)
  866. {
  867. activElement.Move(e.Location);
  868. OnChange(this);
  869. }
  870. else if(mouseDown && leftIndentIsActive)
  871. {
  872. int dx = LeftIndent - oldLeftIndent;
  873. //if (dx != 0)
  874. dx += 4;
  875. foreach (var elem in rulerElements)
  876. {
  877. elem.Move(new Point(elem.Bounds.X + dx, 0));
  878. }
  879. }
  880. }
  881. /// <inheritdoc/>
  882. protected override void OnChange(object sender)
  883. {
  884. base.OnChange(sender);
  885. Invalidate();
  886. }
  887. /// <inheritdoc/>
  888. protected override void OnMouseCaptureChanged(EventArgs e)
  889. {
  890. base.OnMouseCaptureChanged(e);
  891. mouseDown = false;
  892. if (activElement != null)
  893. activElement.IsActive = false;
  894. activElement = null;
  895. leftIndentIsActive = false;
  896. rightIndentIsActive = false;
  897. OnChange(this);
  898. }
  899. /// <inheritdoc/>
  900. protected override void OnMouseUp(MouseEventArgs e)
  901. {
  902. base.OnMouseUp(e);
  903. mouseDown = false;
  904. if(activElement != null)
  905. activElement.IsActive = false;
  906. activElement = null;
  907. leftIndentIsActive = false;
  908. rightIndentIsActive = false;
  909. OnChange(this);
  910. }
  911. /// <inheritdoc/>
  912. protected override void InitLayout()
  913. {
  914. base.InitLayout();
  915. pen = new Pen(Color.Black, 1);
  916. pen.DashStyle = DashStyle.Dash;
  917. pen.DashPattern = new float[] { 4, 4 };
  918. rulerElements = new List<RulerElement>();
  919. }
  920. /// <inheritdoc/>
  921. protected override void OnHandleDestroyed(EventArgs e)
  922. {
  923. pen.Dispose();
  924. base.OnHandleDestroyed(e);
  925. }
  926. #endregion
  927. private bool TabPosExist(int x)
  928. {
  929. foreach (var elem in rulerElements)
  930. {
  931. if (elem.Bounds.Contains(x, elem.Bounds.Y) && elem is TabPosition)
  932. {
  933. return true;
  934. }
  935. }
  936. return false;
  937. }
  938. /// <summary>
  939. /// Method for drawing guides on element of form.
  940. /// </summary>
  941. /// <param name="sender"></param>
  942. /// <param name="e"></param>
  943. public void DrawGuides(object sender, PaintEventArgs e)
  944. {
  945. if (mouseDown)
  946. {
  947. if(activElement != null)
  948. e.Graphics.DrawLine(pen, new Point(activElement.Bounds.X + 4, e.ClipRectangle.Top), new Point(activElement.Bounds.X + 4, e.ClipRectangle.Bottom));
  949. else if(leftIndentIsActive)
  950. e.Graphics.DrawLine(pen, new Point(LeftIndent + RulerStart, e.ClipRectangle.Top), new Point(LeftIndent + RulerStart, e.ClipRectangle.Bottom));
  951. else if (rightIndentIsActive)
  952. e.Graphics.DrawLine(pen, new Point(RulerWidth - RightIndent + RulerStart + 2, e.ClipRectangle.Top), new Point(RulerWidth - RightIndent + RulerStart + 2, e.ClipRectangle.Bottom));
  953. }
  954. }
  955. /// <summary>
  956. /// Base class for elements of RulerWithGuides
  957. /// </summary>
  958. public class RulerElement
  959. {
  960. private bool isActive;
  961. private Rectangle bounds;
  962. /// <summary>
  963. /// Get or set bounds of object
  964. /// </summary>
  965. public Rectangle Bounds
  966. {
  967. get { return bounds; }
  968. set { bounds = value; }
  969. }
  970. /// <summary>
  971. /// Get or set state of object
  972. /// </summary>
  973. public bool IsActive
  974. {
  975. get { return isActive; }
  976. set { isActive = value; }
  977. }
  978. /// <summary>
  979. /// Draw element on graphics
  980. /// </summary>
  981. /// <param name="g"></param>
  982. public virtual void Draw(Graphics g) { }
  983. /// <summary>
  984. /// Method for moving object by means of chenging bounds
  985. /// </summary>
  986. /// <param name="location"></param>
  987. public virtual void Move(Point location) { }
  988. }
  989. /// <summary>
  990. /// Element of RulerWithGuides presenting position of tabs
  991. /// </summary>
  992. public class TabPosition : RulerElement
  993. {
  994. /// <summary>
  995. /// Constructor of class TabPosition
  996. /// </summary>
  997. /// <param name="x"></param>
  998. public TabPosition(int x)
  999. {
  1000. IsActive = false;
  1001. Bounds = new Rectangle(x - 4, 16, 14, 14);
  1002. }
  1003. /// <inheritdoc/>
  1004. public override void Draw(Graphics g)
  1005. {
  1006. base.Draw(g);
  1007. if(IsActive)
  1008. g.DrawImage(Res.GetImage(174, 96), Bounds);
  1009. else
  1010. g.DrawImage(Res.GetImage(74, 96), Bounds);
  1011. }
  1012. /// <inheritdoc/>
  1013. public override void Move(Point location)
  1014. {
  1015. base.Move(location);
  1016. Bounds = new Rectangle(location.X - 4, 16, 14, 14);
  1017. }
  1018. }
  1019. }
  1020. #endif
  1021. }