LinkLabel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. // Based on work by:
  26. // Daniel Carrera, [email protected] (stubbed out)
  27. // Jaak Simm ([email protected]) (stubbed out)
  28. //
  29. // TODO:
  30. // - Change the cursor to a hand cursor when you are over a link (when cursors are available)
  31. // - Focus handeling
  32. //
  33. // $Revision: 1.8 $
  34. // $Modtime: $
  35. // $Log: LinkLabel.cs,v $
  36. // Revision 1.8 2004/09/07 09:40:15 jordi
  37. // LinkLabel fixes, methods, multiple links
  38. //
  39. // Revision 1.7 2004/08/21 22:32:14 pbartok
  40. // - Signature Fixes
  41. //
  42. // Revision 1.6 2004/08/10 15:24:35 jackson
  43. // Let Control handle buffering.
  44. //
  45. // Revision 1.5 2004/08/08 17:52:12 jordi
  46. // *** empty log message ***
  47. //
  48. // Revision 1.4 2004/08/07 23:31:15 jordi
  49. // fixes label bug and draw method name
  50. //
  51. // Revision 1.3 2004/08/07 19:16:31 jordi
  52. // throw exceptions, fixes events, missing methods
  53. //
  54. // Revision 1.2 2004/07/22 15:22:19 jordi
  55. // link label: check link overlapping, implement events, and fixes
  56. //
  57. // Revision 1.1 2004/07/21 16:19:17 jordi
  58. // LinkLabel control implementation
  59. //
  60. //
  61. // INCOMPLETE
  62. using System.ComponentModel;
  63. using System.Collections;
  64. using System.Drawing;
  65. using System.Drawing.Drawing2D;
  66. namespace System.Windows.Forms
  67. {
  68. public class LinkLabel : Label, IButtonControl
  69. {
  70. /* Encapsulates a piece of text (regular or link)*/
  71. internal class Piece
  72. {
  73. public string text;
  74. public int start;
  75. public int end;
  76. public LinkLabel.Link link; // Empty link indicates regular text
  77. public RectangleF rect;
  78. public bool clicked;
  79. public Piece ()
  80. {
  81. start = end = 0;
  82. link = null;
  83. clicked = false;
  84. }
  85. }
  86. private Color active_link;
  87. private Color disabled_link;
  88. private Color link_color;
  89. private Color visited_color;
  90. private LinkArea link_area;
  91. private LinkBehavior link_behavior;
  92. private LinkCollection link_collection;
  93. private bool link_visited;
  94. private Font link_font;
  95. private bool link_click;
  96. private Piece[] pieces;
  97. private Cursor override_cursor;
  98. private DialogResult dialog_result;
  99. #region Events
  100. public event LinkLabelLinkClickedEventHandler LinkClicked;
  101. #endregion // Events
  102. public LinkLabel ()
  103. {
  104. link_collection = new LinkCollection (this);
  105. LinkArea = new LinkArea (0, -1);
  106. link_behavior = LinkBehavior.SystemDefault;
  107. link_visited = false;
  108. link_click = false;
  109. pieces = null;
  110. ActiveLinkColor = Color.Red;
  111. DisabledLinkColor = ThemeEngine.Current.ColorGrayText;
  112. LinkColor = Color.FromArgb (255, 0, 0, 255);
  113. VisitedLinkColor = Color.FromArgb (255, 128, 0, 128);
  114. }
  115. #region Public Properties
  116. public Color ActiveLinkColor {
  117. get { return active_link;}
  118. set {
  119. if (active_link == value)
  120. return;
  121. active_link = value;
  122. Refresh ();
  123. }
  124. }
  125. public Color DisabledLinkColor {
  126. get { return disabled_link;}
  127. set {
  128. if (disabled_link == value)
  129. return;
  130. disabled_link = value;
  131. Refresh ();
  132. }
  133. }
  134. public Color LinkColor {
  135. get { return link_color;}
  136. set {
  137. if (link_color == value)
  138. return;
  139. link_color = value;
  140. Refresh ();
  141. }
  142. }
  143. public Color VisitedLinkColor {
  144. get { return visited_color;}
  145. set {
  146. if (visited_color == value)
  147. return;
  148. visited_color = value;
  149. Refresh ();
  150. }
  151. }
  152. public LinkArea LinkArea {
  153. get { return link_area;}
  154. set {
  155. if (value.Start <0 || value.Length > 0)
  156. throw new ArgumentException ();
  157. if (!value.IsEmpty)
  158. Links.Add (value.Start, value.Length);
  159. link_area = value;
  160. Refresh ();
  161. }
  162. }
  163. public LinkBehavior LinkBehavior {
  164. get { return link_behavior;}
  165. set {
  166. if (link_behavior == value)
  167. return;
  168. link_behavior = value;
  169. Refresh ();
  170. }
  171. }
  172. public LinkLabel.LinkCollection Links {
  173. get { return link_collection;}
  174. }
  175. public bool LinkVisited {
  176. get { return link_visited;}
  177. set {
  178. if (link_visited == value)
  179. return;
  180. link_visited = value;
  181. Refresh ();
  182. }
  183. }
  184. protected Cursor OverrideCursor {
  185. get { return override_cursor;}
  186. set { override_cursor = value;}
  187. }
  188. public override string Text {
  189. get { return base.Text; }
  190. set {
  191. if (base.Text == value)
  192. return;
  193. base.Text = value;
  194. Refresh ();
  195. }
  196. }
  197. #endregion // Public Properties
  198. DialogResult IButtonControl.DialogResult {
  199. get { return dialog_result; }
  200. set { dialog_result = value; }
  201. }
  202. void IButtonControl.NotifyDefault (bool value)
  203. {
  204. }
  205. void IButtonControl.PerformClick ()
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. #region Public Methods
  210. protected override AccessibleObject CreateAccessibilityInstance()
  211. {
  212. return base.CreateAccessibilityInstance();
  213. }
  214. protected override void CreateHandle ()
  215. {
  216. CreateLinkFont ();
  217. CreateLinkPieces ();
  218. base.CreateHandle();
  219. }
  220. protected override void OnEnabledChanged (EventArgs e)
  221. {
  222. base.OnEnabledChanged (e);
  223. Refresh ();
  224. }
  225. protected override void OnFontChanged (EventArgs e)
  226. {
  227. base.OnFontChanged (e);
  228. CreateLinkFont ();
  229. Refresh ();
  230. }
  231. protected override void OnGotFocus (EventArgs e)
  232. {
  233. base.OnGotFocus(e);
  234. }
  235. protected override void OnKeyDown (KeyEventArgs e)
  236. {
  237. base.OnKeyDown(e);
  238. }
  239. protected virtual void OnLinkClicked (LinkLabelLinkClickedEventArgs e)
  240. {
  241. if (LinkClicked != null)
  242. LinkClicked (this, e);
  243. }
  244. protected override void OnLostFocus (EventArgs e)
  245. {
  246. base.OnLostFocus (e);
  247. }
  248. protected override void OnMouseDown (MouseEventArgs e)
  249. {
  250. if (!Enabled) return;
  251. base.OnMouseDown(e);
  252. this.Capture = true;
  253. for (int i = 0; i < pieces.Length; i++) {
  254. if (pieces[i].rect.Contains (e.X, e.Y)) {
  255. if (pieces[i].link!= null) {
  256. pieces[i].clicked = true;
  257. Refresh ();
  258. }
  259. break;
  260. }
  261. }
  262. }
  263. protected override void OnMouseLeave(EventArgs e)
  264. {
  265. if (!Enabled) return;
  266. base.OnMouseLeave(e);
  267. }
  268. protected override void OnMouseMove (MouseEventArgs e)
  269. {
  270. base.OnMouseMove (e);
  271. }
  272. protected override void OnMouseUp (MouseEventArgs e)
  273. {
  274. if (!Enabled) return;
  275. base.OnMouseUp (e);
  276. this.Capture = false;
  277. for (int i = 0; i < pieces.Length; i++) {
  278. if (pieces[i].link!= null && pieces[i].clicked == true) {
  279. if (LinkClicked != null)
  280. LinkClicked (this, new LinkLabelLinkClickedEventArgs (pieces[i].link));
  281. pieces[i].clicked = false;
  282. Refresh ();
  283. }
  284. }
  285. }
  286. protected override void OnPaint (PaintEventArgs pevent)
  287. {
  288. if (Width <= 0 || Height <= 0 || Visible == false)
  289. return;
  290. Draw ();
  291. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  292. }
  293. protected override void OnPaintBackground(PaintEventArgs e)
  294. {
  295. }
  296. protected override void OnTextAlignChanged (EventArgs e)
  297. {
  298. base.OnTextAlignChanged (e);
  299. Refresh ();
  300. }
  301. protected override void OnTextChanged (EventArgs e)
  302. {
  303. base.OnTextChanged (e);
  304. Refresh ();
  305. }
  306. protected Link PointInLink (int x, int y)
  307. {
  308. for (int i = 0; i < pieces.Length; i++) {
  309. if (pieces[i].rect.Contains (x,y) && pieces[i].link != null)
  310. return pieces[i].link;
  311. }
  312. return null;
  313. }
  314. protected override bool ProcessDialogKey (Keys keyData)
  315. {
  316. return base.ProcessDialogKey (keyData);
  317. }
  318. protected override void Select (bool directed, bool forward)
  319. {
  320. base.Select (directed, forward);
  321. }
  322. public void Select ()
  323. {
  324. base.Select ();
  325. }
  326. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  327. {
  328. base.SetBoundsCore (x, y, width, height, specified);
  329. Refresh ();
  330. }
  331. protected override void WndProc (ref Message m)
  332. {
  333. base.WndProc (ref m);
  334. }
  335. #endregion //Public Methods
  336. #region Private Methods
  337. internal void CreateLinkPieces ()
  338. {
  339. if (Links.Count == 0)
  340. return;
  341. int cur_piece = 0;
  342. if (Links.Count == 1 && Links[0].Start == 0 && Links[0].Length == -1) {
  343. pieces = new Piece [1];
  344. pieces[cur_piece] = new Piece();
  345. pieces[cur_piece].start = 0;
  346. pieces[cur_piece].end = Text.Length;
  347. pieces[cur_piece].link = Links[0];
  348. pieces[cur_piece].text = Text;
  349. pieces[cur_piece].rect = ClientRectangle;
  350. return;
  351. }
  352. pieces = new Piece [(Links.Count * 2) + 1];
  353. pieces[cur_piece] = new Piece();
  354. pieces[cur_piece].start = 0;
  355. for (int i = 0; i < Text.Length; i++) { /* Every char on the text*/
  356. for (int l = 0; l < Links.Count; l++) { /* Every link that we know of*/
  357. if (Links[l].Start == i) {
  358. if (i > 0) {
  359. /*Push prev. regular text*/
  360. pieces[cur_piece].end = i;
  361. pieces[cur_piece].text = Text.Substring (pieces[cur_piece].start,
  362. pieces[cur_piece].end - pieces[cur_piece].start);
  363. cur_piece++;
  364. /* New link*/
  365. pieces[cur_piece] = new Piece ();
  366. }
  367. pieces[cur_piece].start = Links[l].Start;
  368. pieces[cur_piece].end = Links[l].Start + Links[l].Length;
  369. pieces[cur_piece].link = Links[l];
  370. pieces[cur_piece].text = Text.Substring (pieces[cur_piece].start,
  371. pieces[cur_piece].end - pieces[cur_piece].start);
  372. cur_piece++; /* Push link*/
  373. pieces[cur_piece] = new Piece();
  374. i+= Links[l].Length;
  375. pieces[cur_piece].start = i;
  376. }
  377. }
  378. }
  379. if (pieces[cur_piece].end == 0) {
  380. pieces[cur_piece].end = Text.Length;
  381. pieces[cur_piece].text = Text.Substring (pieces[cur_piece].start, pieces[cur_piece].end - pieces[cur_piece].start);
  382. }
  383. CharacterRange[] charRanges = new CharacterRange [pieces.Length];
  384. for (int i = 0; i < pieces.Length; i++)
  385. charRanges[i] = new CharacterRange (pieces[i].start, pieces[i].end - pieces[i].start);
  386. Region[] charRegions = new Region [pieces.Length];
  387. string_format.SetMeasurableCharacterRanges (charRanges);
  388. charRegions = DeviceContext.MeasureCharacterRanges (Text, Font, ClientRectangle, string_format);
  389. for (int i = 0; i < pieces.Length; i++) {
  390. //RectangleF[] f = charRegions[i].GetRegionScans (new Matrix());
  391. pieces[i].rect = charRegions[i].GetBounds (DeviceContext);
  392. Console.WriteLine (pieces[i].rect);
  393. }
  394. if (Visible && IsHandleCreated)
  395. Refresh ();
  396. }
  397. /* Check if the links overlap */
  398. internal void CheckLinks ()
  399. {
  400. for (int i = 0; i < Links.Count; i++) {
  401. for (int l = 0; l < Links.Count; l++) {
  402. if (i==l) continue;
  403. if (((Links[i].Start + Links[i].Length) >= Links[l].Start &&
  404. Links[i].Start + Links[i].Length <= Links[l].Start + Links[l].Length) ||
  405. (Links[i].Start >= Links[l].Start &&
  406. Links[i].Start <= Links[l].Start + Links[l].Length))
  407. throw new InvalidOperationException ("Overlapping link regions.");
  408. }
  409. }
  410. }
  411. private Color GetLinkColor (Piece piece, int i)
  412. {
  413. Color color;
  414. if (Enabled == false ||
  415. (piece.link != null && piece.link.Enabled == false))
  416. color = DisabledLinkColor;
  417. else
  418. if (piece.clicked == true)
  419. color = ActiveLinkColor;
  420. else
  421. if ((LinkVisited == true && i == 0) ||
  422. (piece.link != null && piece.link.Visited == true))
  423. color = VisitedLinkColor;
  424. else
  425. color = LinkColor;
  426. return color;
  427. }
  428. internal void Draw ()
  429. {
  430. Color color;
  431. //dc.FillRectangle (label_br_back_color, area);
  432. ThemeEngine.Current.DrawBorderStyle (DeviceContext, ClientRectangle, BorderStyle);
  433. if (Links.Count == 1 && Links[0].Start == 0 && Links[0].Length == -1) {
  434. color = GetLinkColor (pieces[0], 0);
  435. DeviceContext.DrawString (Text, Font, new SolidBrush (color),
  436. ClientRectangle, string_format);
  437. return;
  438. }
  439. for (int i = 0; i < pieces.Length; i++) {
  440. color = GetLinkColor (pieces[i], i);
  441. if (pieces[i].link == null)
  442. DeviceContext.DrawString (pieces[i].text, Font, new SolidBrush (Color.Black),
  443. pieces[i].rect.X, pieces[i].rect.Y, string_format);
  444. else
  445. DeviceContext.DrawString (pieces[i].text, link_font, new SolidBrush (color),
  446. pieces[i].rect.X, pieces[i].rect.Y, string_format);
  447. }
  448. DrawImage (DeviceContext, Image, ClientRectangle, image_align);
  449. }
  450. private void CreateLinkFont ()
  451. {
  452. link_font = new Font (Font.FontFamily, Font.Size, Font.Style | FontStyle.Underline,
  453. Font.Unit);
  454. }
  455. #endregion // Private Methods
  456. //
  457. // System.Windows.Forms.LinkLabel.Link
  458. //
  459. public class Link
  460. {
  461. private bool enabled;
  462. private int length;
  463. private object linkData;
  464. private int start;
  465. private bool visited;
  466. private LinkLabel owner;
  467. internal Link ()
  468. {
  469. enabled = true;
  470. visited = false;
  471. length = start = 0;
  472. linkData = null;
  473. owner = null;
  474. }
  475. internal Link (LinkLabel owner)
  476. {
  477. enabled = true;
  478. visited = false;
  479. length = start = 0;
  480. linkData = null;
  481. this.owner = owner;
  482. }
  483. public bool Enabled {
  484. get { return enabled; }
  485. set {
  486. if (enabled == value)
  487. return;
  488. enabled = value;
  489. if (owner != null)
  490. owner.CreateLinkPieces ();
  491. }
  492. }
  493. public int Length {
  494. get { return length; }
  495. set {
  496. if (length == value)
  497. return;
  498. length = value;
  499. if (owner != null)
  500. owner.CreateLinkPieces ();
  501. }
  502. }
  503. public object LinkData {
  504. get { return linkData; }
  505. set { linkData = value; }
  506. }
  507. public int Start {
  508. get { return start; }
  509. set {
  510. if (start == value)
  511. return;
  512. start = value;
  513. if (owner != null)
  514. owner.CreateLinkPieces ();
  515. }
  516. }
  517. public bool Visited {
  518. get { return visited; }
  519. set {
  520. if (visited == value)
  521. return;
  522. visited = value;
  523. if (owner != null)
  524. owner.CreateLinkPieces ();
  525. }
  526. }
  527. }
  528. //
  529. // System.Windows.Forms.LinkLabel.Link
  530. //
  531. public class LinkCollection : IList, ICollection, IEnumerable
  532. {
  533. private LinkLabel owner;
  534. private ArrayList collection = new ArrayList();
  535. public LinkCollection (LinkLabel owner)
  536. {
  537. if (owner==null)
  538. throw new ArgumentNullException ();
  539. this.owner = owner;
  540. }
  541. public int Count {
  542. get { return collection.Count; }
  543. }
  544. public bool IsReadOnly {
  545. get { return false; }
  546. }
  547. public virtual LinkLabel.Link this[int index] {
  548. get {
  549. if (index < 0 || index >= Count)
  550. throw new ArgumentOutOfRangeException();
  551. return (LinkLabel.Link) collection[index];
  552. }
  553. set {
  554. if (index < 0 || index >= Count)
  555. throw new ArgumentOutOfRangeException();
  556. collection[index] = value;
  557. }
  558. }
  559. public Link Add (int start, int length)
  560. {
  561. return Add (start, length, null);
  562. }
  563. public Link Add (int start, int length, object o)
  564. {
  565. Link link = new Link ();
  566. int idx;
  567. if (Count == 1 && this[0].Start == 0
  568. && this[0].Length == -1) {
  569. Console.WriteLine ("Clear list");
  570. Clear ();
  571. }
  572. link.Length = length;
  573. link.Start = start;
  574. link.LinkData = o;
  575. idx = collection.Add (link);
  576. owner.CheckLinks ();
  577. owner.CreateLinkPieces ();
  578. return (Link) collection[idx];
  579. }
  580. public virtual void Clear ()
  581. {
  582. collection.Clear();
  583. owner.CreateLinkPieces ();
  584. }
  585. public bool Contains (LinkLabel.Link link)
  586. {
  587. return collection.Contains (link);
  588. }
  589. public IEnumerator GetEnumerator ()
  590. {
  591. return collection.GetEnumerator ();
  592. }
  593. public int IndexOf (LinkLabel.Link link)
  594. {
  595. return collection.IndexOf (link);
  596. }
  597. public void Remove (LinkLabel.Link value)
  598. {
  599. collection.Remove (value);
  600. owner.CreateLinkPieces ();
  601. }
  602. public void RemoveAt (int index)
  603. {
  604. if (index >= Count)
  605. throw new ArgumentOutOfRangeException ("Invalid value for array index");
  606. collection.Remove (collection[index]);
  607. owner.CreateLinkPieces ();
  608. }
  609. bool IList.IsFixedSize {
  610. get {return false;}
  611. }
  612. object IList.this[int index] {
  613. get { return collection[index]; }
  614. set { collection[index] = value; }
  615. }
  616. object ICollection.SyncRoot {
  617. get {return this;}
  618. }
  619. bool ICollection.IsSynchronized {
  620. get {return false;}
  621. }
  622. void ICollection.CopyTo (Array dest, int index)
  623. {
  624. collection.CopyTo (dest, index);
  625. }
  626. int IList.Add (object control)
  627. {
  628. return collection.Add (control);
  629. }
  630. bool IList.Contains (object control)
  631. {
  632. return collection.Contains (control);
  633. }
  634. int IList.IndexOf (object control)
  635. {
  636. return collection.IndexOf (control);
  637. }
  638. void IList.Insert (int index, object value)
  639. {
  640. collection.Insert (index, value);
  641. }
  642. void IList.Remove (object control)
  643. {
  644. collection.Remove (control);
  645. }
  646. }
  647. }
  648. }