1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Windows.Forms;
|
---|
4 | namespace Netron.Diagramming.Core {
|
---|
5 | /// <summary>
|
---|
6 | /// Clickable label shape material, i.e. an hyperlink.
|
---|
7 | /// </summary>
|
---|
8 | public partial class ClickableLabelMaterial :
|
---|
9 | LabelMaterial,
|
---|
10 | IMouseListener,
|
---|
11 | IHoverListener {
|
---|
12 |
|
---|
13 | // ------------------------------------------------------------------
|
---|
14 | /// <summary>
|
---|
15 | /// Occurs when the lable is clicked
|
---|
16 | /// </summary>
|
---|
17 | // ------------------------------------------------------------------
|
---|
18 | public event EventHandler OnClick;
|
---|
19 |
|
---|
20 | #region Fields
|
---|
21 |
|
---|
22 | // ------------------------------------------------------------------
|
---|
23 | /// <summary>
|
---|
24 | /// the url of this link
|
---|
25 | /// </summary>
|
---|
26 | // ------------------------------------------------------------------
|
---|
27 | private string mUrl = string.Empty;
|
---|
28 |
|
---|
29 | // ------------------------------------------------------------------
|
---|
30 | /// <summary>
|
---|
31 | /// Implementation of IVersion - the current version of
|
---|
32 | /// ClickableLabelMaterial.
|
---|
33 | /// </summary>
|
---|
34 | // ------------------------------------------------------------------
|
---|
35 | protected double clickableLabelMaterialVersion = 1.0;
|
---|
36 |
|
---|
37 | #endregion
|
---|
38 |
|
---|
39 | #region Properties
|
---|
40 |
|
---|
41 | // ------------------------------------------------------------------
|
---|
42 | /// <summary>
|
---|
43 | /// Gets the current version.
|
---|
44 | /// </summary>
|
---|
45 | // ------------------------------------------------------------------
|
---|
46 | public override double Version {
|
---|
47 | get {
|
---|
48 | return clickableLabelMaterialVersion;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | // ------------------------------------------------------------------
|
---|
53 | /// <summary>
|
---|
54 | /// Gets or sets the URL of this clickable material. Note that the
|
---|
55 | /// material can handle more than just Url's by overriding the mouse
|
---|
56 | /// event handlers. Settings this property is just a convenient way
|
---|
57 | /// to accelerate the development or customization of shapes.
|
---|
58 | /// </summary>
|
---|
59 | /// <value>The URL.</value>
|
---|
60 | // ------------------------------------------------------------------
|
---|
61 | public string Url {
|
---|
62 | get { return mUrl; }
|
---|
63 | set { mUrl = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | #endregion
|
---|
67 |
|
---|
68 | #region Constructor
|
---|
69 | /// <summary>
|
---|
70 | /// Initializes a new instance of the <see cref="T:ClickableLabelMaterial"/> class.
|
---|
71 | /// </summary>
|
---|
72 | public ClickableLabelMaterial()
|
---|
73 | : base() {
|
---|
74 | //Resizable = false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /// <summary>
|
---|
78 | /// Initializes a new instance of the <see cref="T:ClickableLabelMaterial"/> class.
|
---|
79 | /// </summary>
|
---|
80 | /// <param name="text">The text.</param>
|
---|
81 | public ClickableLabelMaterial(string text) : base(text) { }
|
---|
82 |
|
---|
83 | #endregion
|
---|
84 |
|
---|
85 | #region Methods
|
---|
86 |
|
---|
87 | private void RaiseOnClicked() {
|
---|
88 | if (OnClick != null)
|
---|
89 | OnClick(this, EventArgs.Empty);
|
---|
90 | }
|
---|
91 |
|
---|
92 | #region IMouseListener Members
|
---|
93 | /// <summary>
|
---|
94 | /// Gets the service object of the specified type.
|
---|
95 | /// </summary>
|
---|
96 | /// <param name="serviceType">An object that specifies the type of service object to get.</param>
|
---|
97 | /// <returns>
|
---|
98 | /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
|
---|
99 | /// </returns>
|
---|
100 | public override object GetService(Type serviceType) {
|
---|
101 | if (serviceType.Equals(typeof(IMouseListener)))
|
---|
102 | return this;
|
---|
103 | else if (serviceType.Equals(typeof(IHoverListener)))
|
---|
104 | return this;
|
---|
105 | else
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /// <summary>
|
---|
110 | /// Handles the mouse-down event
|
---|
111 | /// </summary>
|
---|
112 | /// <param name="e">The <see cref="T:MouseEventArgs"/> instance containing the event data.</param>
|
---|
113 | public virtual bool MouseDown(MouseEventArgs e) {
|
---|
114 | if (this.Rectangle.Contains(e.Location)) {
|
---|
115 | if (mUrl.Length > 0)
|
---|
116 | Process.Start(mUrl);
|
---|
117 | RaiseOnClicked();
|
---|
118 | return true;//let the rest of the loop go, the event was handled
|
---|
119 | }
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /// <summary>
|
---|
124 | /// Handles the mouse-move event
|
---|
125 | /// </summary>
|
---|
126 | /// <param name="e">The <see cref="T:MouseEventArgs"/> instance containing the event data.</param>
|
---|
127 | public void MouseMove(MouseEventArgs e) {
|
---|
128 | System.Diagnostics.Trace.WriteLine(e.Location.ToString());
|
---|
129 | }
|
---|
130 |
|
---|
131 | /// <summary>
|
---|
132 | /// Handles the mouse-up event
|
---|
133 | /// </summary>
|
---|
134 | /// <param name="e">The <see cref="T:MouseEventArgs"/> instance containing the event data.</param>
|
---|
135 | public virtual void MouseUp(MouseEventArgs e) {
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | #endregion
|
---|
140 |
|
---|
141 | #region IHoverListener Members
|
---|
142 | /// <summary>
|
---|
143 | /// Handles the <see cref="OnMouseHover"/> event.
|
---|
144 | /// </summary>
|
---|
145 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
146 | public void MouseHover(MouseEventArgs e) {
|
---|
147 |
|
---|
148 | }
|
---|
149 | private Cursor previousCursor;
|
---|
150 |
|
---|
151 | /// <summary>
|
---|
152 | /// Handles the OnMouseEnter event.
|
---|
153 | /// </summary>
|
---|
154 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
155 | public void MouseEnter(MouseEventArgs e) {
|
---|
156 | previousCursor = Cursor.Current;
|
---|
157 | this.Shape.Model.RaiseOnCursorChange(Cursors.Hand);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /// <summary>
|
---|
161 | /// Handles the OnMouseLeave event.
|
---|
162 | /// </summary>
|
---|
163 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
164 | public void MouseLeave(MouseEventArgs e) {
|
---|
165 | this.Shape.Model.RaiseOnCursorChange(previousCursor);
|
---|
166 | }
|
---|
167 |
|
---|
168 | //public override void Paint(Graphics g)
|
---|
169 | //{
|
---|
170 | // g.DrawRectangle(Pens.Violet, Rectangle);
|
---|
171 | // base.Paint(g);
|
---|
172 | //}
|
---|
173 | #endregion
|
---|
174 | #endregion
|
---|
175 | }
|
---|
176 | }
|
---|