LinkLabel.cs 18 KB

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