| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2004 Novell, Inc.
- //
- // Authors:
- // Jordi Mas i Hernandez, [email protected]
- //
- // Based on work by:
- // Daniel Carrera, [email protected] (stubbed out)
- // Jaak Simm ([email protected]) (stubbed out)
- //
- // TODO:
- // - Change the cursor to a hand cursor when you are over a link (when cursors are available)
- //
- //
- // $Revision: 1.6 $
- // $Modtime: $
- // $Log: LinkLabel.cs,v $
- // Revision 1.6 2004/08/10 15:24:35 jackson
- // Let Control handle buffering.
- //
- // Revision 1.5 2004/08/08 17:52:12 jordi
- // *** empty log message ***
- //
- // Revision 1.4 2004/08/07 23:31:15 jordi
- // fixes label bug and draw method name
- //
- // Revision 1.3 2004/08/07 19:16:31 jordi
- // throw exceptions, fixes events, missing methods
- //
- // Revision 1.2 2004/07/22 15:22:19 jordi
- // link label: check link overlapping, implement events, and fixes
- //
- // Revision 1.1 2004/07/21 16:19:17 jordi
- // LinkLabel control implementation
- //
- //
- // INCOMPLETE
- using System.ComponentModel;
- using System.Collections;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- namespace System.Windows.Forms
- {
- public class LinkLabel : Label, IButtonControl
- {
- /* Encapsulates a piece of text (regular or link)*/
- internal class Piece
- {
- public string text;
- public int start;
- public int end;
- public LinkLabel.Link link; // Empty link indicates regular text
- public RectangleF rect;
- public Piece ()
- {
- start = end = 0;
- link = null;
- }
- }
- private Color active_link;
- private Color disabled_link;
- private Color link_color;
- private Color visited_color;
- private LinkArea link_area;
- private LinkBehavior link_behavior;
- private LinkCollection link_collection;
- private bool link_visited;
- private Font link_font;
- private bool link_click;
- private Piece[] pieces;
- #region Events
- public event LinkLabelLinkClickedEventHandler LinkClicked;
- #endregion // Events
- public LinkLabel ()
- {
- link_area = new LinkArea ();
- link_behavior = LinkBehavior.SystemDefault;
- link_collection = new LinkCollection (this);
- link_visited = false;
- link_click = false;
- pieces = null;
-
- ActiveLinkColor = Color.Red;
- DisabledLinkColor = ThemeEngine.Current.ColorGrayText;
- LinkColor = Color.FromArgb (255, 0, 0, 255);
- VisitedLinkColor = Color.FromArgb (255, 128, 0, 128);
- }
- #region Public Properties
- public Color ActiveLinkColor {
- get { return active_link;}
- set {
- if (active_link == value)
- return;
- active_link = value;
- Refresh ();
- }
- }
- public Color DisabledLinkColor {
- get { return disabled_link;}
- set {
- if (disabled_link == value)
- return;
- disabled_link = value;
- Refresh ();
- }
- }
- public Color LinkColor {
- get { return link_color;}
- set {
- if (link_color == value)
- return;
- link_color = value;
- Refresh ();
- }
- }
- public Color VisitedLinkColor {
- get { return visited_color;}
- set {
- if (visited_color == value)
- return;
- visited_color = value;
- Refresh ();
- }
- }
- public LinkArea LinkArea {
- get { return link_area;}
- set {
- if (value.Start <0 || value.Length >0)
- throw new ArgumentException();
-
- link_area = value;
- Refresh ();
- }
- }
- public LinkBehavior LinkBehavior {
- get { return link_behavior;}
- set {
- if (link_behavior == value)
- return;
- link_behavior = value;
- Refresh ();
- }
- }
- public LinkLabel.LinkCollection Links {
- get { return link_collection;}
- set { link_collection = value;}
- }
- public bool LinkVisited {
- get { return link_visited;}
- set {
- if (link_visited == value)
- return;
- link_visited = value;
- Refresh ();
- }
- }
- public override string Text {
- get { return base.Text; }
- set {
- if (base.Text == value)
- return;
- base.Text = value;
- Refresh ();
- }
- }
- #endregion // Public Properties
- [MonoTODO]
- DialogResult IButtonControl.DialogResult {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
- [MonoTODO]
- void IButtonControl.NotifyDefault (bool value)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- void IButtonControl.PerformClick ()
- {
- throw new NotImplementedException ();
- }
- //
- // --- Protected Properties
- //
- protected override ImeMode DefaultImeMode {
- get {return base.DefaultImeMode;}
- }
- //
- // --- Protected Methods
- //
- protected override AccessibleObject CreateAccessibilityInstance()
- {
- return base.CreateAccessibilityInstance();
- }
- protected override void CreateHandle ()
- {
- CreateLinkFont ();
- CreateLinkPieces ();
- base.CreateHandle();
- }
- protected override void OnEnabledChanged (EventArgs e)
- {
- base.OnEnabledChanged (e);
- }
- protected override void OnFontChanged (EventArgs e)
- {
- base.OnFontChanged (e);
- CreateLinkFont ();
- }
- protected override void OnGotFocus( EventArgs e)
- {
- base.OnGotFocus(e);
- }
- protected override void OnKeyDown (KeyEventArgs e)
- {
- base.OnKeyDown(e);
- }
- protected override void OnLostFocus (EventArgs e)
- {
- base.OnLostFocus (e);
- }
- protected override void OnMouseDown (MouseEventArgs e)
- {
- if (!Enabled) return;
- base.OnMouseDown(e);
- Point pnt = new Point (e.X, e.Y);
- if (Links.Count == 0) {
- if (paint_area.Contains (pnt)) {
- link_click = true;
- Refresh ();
- }
- }
- else {
- for (int i = 0; i < pieces.Length; i++) {
- if (pieces[i].rect.Contains (pnt)) {
- link_click = true;
- Refresh ();
- break;
- }
- }
- }
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- if (!Enabled) return;
- base.OnMouseLeave(e);
- }
- protected override void OnMouseMove (MouseEventArgs e)
- {
- base.OnMouseMove (e);
- }
- protected override void OnMouseUp (MouseEventArgs e)
- {
- if (!Enabled) return;
- base.OnMouseUp (e);
- Point pnt = new Point (e.X, e.Y);
- if (Links.Count == 0) {
- if (paint_area.Contains (pnt)) {
- link_click = false;
- if (LinkClicked != null)
- LinkClicked (this, new LinkLabelLinkClickedEventArgs (new Link ()));
- Refresh ();
- }
- }
- else {
- for (int i = 0; i < pieces.Length; i++) {
- if (pieces[i].rect.Contains (pnt)) {
- link_click = false;
- if ((LinkClicked != null) && (pieces[i].link != null))
- LinkClicked (this, new LinkLabelLinkClickedEventArgs (pieces[i].link));
- Refresh ();
- break;
- }
- }
- }
- if (paint_area.Contains (new Point (e.X, e.Y))) {
- link_click = false;
- Refresh ();
- }
- }
- protected override void OnPaint (PaintEventArgs e)
- {
- base.OnPaint (e);
- }
- protected override void OnPaintBackground(PaintEventArgs e)
- {
- base.OnPaint (e);
- }
- protected override void OnTextAlignChanged (EventArgs e)
- {
- base.OnTabIndexChanged(e);
- }
- protected override void OnTextChanged (EventArgs e)
- {
- base.OnTabIndexChanged(e);
- }
- internal void CreateLinkPieces ()
- {
- //Console.WriteLine ("CreateLinkPieces:" + Links.Count);
- if (Links.Count == 0)
- return;
- int num_pieces = (Links.Count * 2) + 1;
- pieces = new Piece [num_pieces];
- int cur_piece = 0;
- //Console.WriteLine ("pieces: " + num_pieces);
- pieces[cur_piece] = new Piece();
- pieces[cur_piece].start = 0;
- for (int i = 0; i < Text.Length; i++) { /* Every char on the text*/
- for (int l = 0; l < Links.Count; l++) { /* Every link that we know of*/
- if (Links[l].Start == i) {
- if (i > 0) {
- /*Push prev. regular text*/
- pieces[cur_piece].end = i;
- pieces[cur_piece].text = Text.Substring (pieces[cur_piece].start,
- pieces[cur_piece].end - pieces[cur_piece].start);
- cur_piece++;
- /* New link*/
- pieces[cur_piece] = new Piece();
- }
- pieces[cur_piece].start = Links[l].Start;
- pieces[cur_piece].end = Links[l].Start + Links[l].Length;
- pieces[cur_piece].link = Links[l];
- pieces[cur_piece].text = Text.Substring (pieces[cur_piece].start,
- pieces[cur_piece].end - pieces[cur_piece].start);
- cur_piece++; /* Push link*/
- pieces[cur_piece] = new Piece();
- i+= Links[l].Length;
- pieces[cur_piece].start = i;
- }
- }
- }
- if (pieces[cur_piece].end == 0) {
- pieces[cur_piece].end = Text.Length;
- pieces[cur_piece].text = Text.Substring (pieces[cur_piece].start, pieces[cur_piece].end - pieces[cur_piece].start);
- }
- CharacterRange[] charRanges = new CharacterRange [pieces.Length];
- for (int i = 0; i < pieces.Length; i++)
- charRanges[i] = new CharacterRange (pieces[i].start, pieces[i].end - pieces[i].start);
- Region[] charRegions = new Region [pieces.Length];
- string_format.SetMeasurableCharacterRanges (charRanges);
- charRegions = DeviceContext.MeasureCharacterRanges (Text, Font, paint_area, string_format);
- for (int i = 0; i < pieces.Length; i++)
- pieces[i].rect = charRegions[i].GetBounds (DeviceContext);
- if (Visible && IsHandleCreated)
- Refresh ();
- }
- /* Check if the links overlap */
- internal void CheckLinks ()
- {
- for (int i = 0; i < Links.Count; i++) {
- for (int l = 0; l < Links.Count; l++) {
- if (i==l) continue;
- if (((Links[i].Start + Links[i].Length) >= Links[l].Start &&
- Links[i].Start + Links[i].Length <= Links[l].Start + Links[l].Length) ||
- (Links[i].Start >= Links[l].Start &&
- Links[i].Start <= Links[l].Start + Links[l].Length))
- throw new InvalidOperationException ("Overlapping link regions.");
- }
- }
- }
- protected override void Draw ()
- {
- Color color;
- if (Visible == false) return;
- if (Enabled == false)
- color = DisabledLinkColor;
- else
- if (link_click == true)
- color = ActiveLinkColor;
- else
- if (LinkVisited == true)
- color = VisitedLinkColor;
- else
- color = LinkColor;
- if (Links.Count == 0 || pieces == null) {
- ThemeEngine.Current.DrawLabel (DeviceContext, paint_area, BorderStyle, Text,
- color, BackColor, link_font, string_format,
- true /* We paint ourselfs the disabled status*/);
- DrawImage (DeviceContext, Image, paint_area, image_align);
- return;
- }
- for (int i = 0; i < pieces.Length; i++) {
- if (pieces[i].link == null)
- DeviceContext.DrawString (pieces[i].text, Font, new SolidBrush (Color.Black),
- pieces[i].rect.X, pieces[i].rect.Y, string_format);
- else
- DeviceContext.DrawString (pieces[i].text, link_font, new SolidBrush (color),
- pieces[i].rect.X, pieces[i].rect.Y, string_format);
- }
- DrawImage (DeviceContext, Image, paint_area, image_align);
- }
- private void CreateLinkFont ()
- {
- link_font = new Font (Font.FontFamily, Font.Size, Font.Style | FontStyle.Underline,
- Font.Unit);
- }
- //
- // System.Windows.Forms.LinkLabel.Link
- //
- public class Link
- {
- private bool enabled;
- private int length;
- private object linkData;
- private int start;
- private bool visited;
- private LinkLabel owner;
- internal Link ()
- {
- enabled = true;
- visited = false;
- length = start = 0;
- linkData = null;
- owner = null;
- }
- internal Link (LinkLabel owner)
- {
- enabled = true;
- visited = false;
- length = start = 0;
- linkData = null;
- this.owner = owner;
- }
- public bool Enabled {
- get { return enabled; }
- set {
- if (enabled == value)
- return;
- enabled = value;
- if (owner != null)
- owner.CreateLinkPieces ();
- }
- }
- public int Length {
- get { return length; }
- set {
- if (length == value)
- return;
- length = value;
- if (owner != null)
- owner.CreateLinkPieces ();
- }
- }
- public object LinkData {
- get { return linkData; }
- set { linkData = value; }
- }
- public int Start {
- get { return start; }
- set {
- if (start == value)
- return;
- start = value;
- if (owner != null)
- owner.CreateLinkPieces ();
- }
- }
- public bool Visited {
- get { return visited; }
- set {
- if (visited == value)
- return;
- visited = value;
- if (owner != null)
- owner.CreateLinkPieces ();
- }
- }
- }
- //
- // System.Windows.Forms.LinkLabel.Link
- //
- public class LinkCollection : IList, ICollection, IEnumerable
- {
- private LinkLabel owner;
- private ArrayList collection = new ArrayList();
- public LinkCollection (LinkLabel owner)
- {
- this.owner = owner;
- }
- public int Count {
- get { return collection.Count; }
- }
- public bool IsReadOnly {
- get { return collection.IsReadOnly; }
- }
- public virtual LinkLabel.Link this[int index] {
- get {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException();
- return (LinkLabel.Link) collection[index];
- }
- set {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException();
- collection[index] = value;
- }
- }
- public Link Add (int start, int length)
- {
- Link link = new Link ();
- int idx;
- link.Length = length;
- link.Start = start;
- idx = collection.Add (link);
- owner.CheckLinks ();
- owner.CreateLinkPieces ();
- return (Link)collection[idx];
- }
- public Link Add (int start, int length, object o)
- {
- Link link = new Link ();
- int idx;
- link.Length = length;
- link.Start = start;
- link.LinkData = o;
- idx = collection.Add (link);
- owner.CheckLinks ();
- owner.CreateLinkPieces ();
-
- return (Link) collection[idx];
- }
- public virtual void Clear ()
- {
- collection.Clear();
- owner.CreateLinkPieces ();
- }
- public IEnumerator GetEnumerator ()
- {
- return collection.GetEnumerator ();
- }
- public int IndexOf (LinkLabel.Link link)
- {
- return collection.IndexOf (link);
- }
- public void Remove (LinkLabel.Link value)
- {
- collection.Remove (value);
- owner.CreateLinkPieces ();
- }
- public void RemoveAt (int index)
- {
- if (index >= Count)
- throw new ArgumentOutOfRangeException ("Invalid value for array index");
- collection.Remove (collection[index]);
- owner.CreateLinkPieces ();
- }
- bool IList.IsFixedSize {
- get {return collection.IsFixedSize;}
- }
- object IList.this[int index] {
- get { return collection[index]; }
- set { collection[index] = value; }
- }
- object ICollection.SyncRoot {
- [MonoTODO] get {
- throw new NotImplementedException ();
- }
- }
- bool ICollection.IsSynchronized {
- [MonoTODO] get {
- throw new NotImplementedException ();
- }
- }
- [MonoTODO]
- void ICollection.CopyTo (Array dest,int index)
- {
- throw new NotImplementedException ();
- }
- int IList.Add (object control)
- {
- return collection.Add (control);
- }
- bool IList.Contains (object control)
- {
- return collection.Contains (control);
- }
- int IList.IndexOf (object control)
- {
- return collection.IndexOf (control);
- }
- void IList.Insert (int index, object value)
- {
- collection.Insert (index, value);
- }
- void IList.Remove (object control)
- {
- collection.Remove (control);
- }
- }
- }
- }
|