LinkLabel.cs 16 KB

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