LinkLabel.cs 16 KB

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