2
0

LinkLabel.cs 16 KB

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