-
Notifications
You must be signed in to change notification settings - Fork 2
/
DecorationCollectionControl.cs
351 lines (312 loc) · 12.9 KB
/
DecorationCollectionControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// ********************************************************************************************************
// Product Name: DotSpatial.Symbology.Forms.dll
// Description: The Windows Forms user interface layer for the DotSpatial.Symbology library.
// ********************************************************************************************************
// The contents of this file are subject to the MIT License (MIT)
// you may not use this file except in compliance with the License. You may obtain a copy of the License at
// http://dotspatial.codeplex.com/license
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
// ANY KIND, either expressed or implied. See the License for the specific language governing rights and
// limitations under the License.
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 4/28/2009 4:23:05 PM
//
// Contributor(s): (Open source contributors should list themselves and their modifications here).
//
// ********************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DotSpatial.Data;
namespace DotSpatial.Symbology.Forms
{
/// <summary>
/// This is designed to automatically have add, subtract, up and down arrows for working with a simple collection of items.
/// </summary>
internal class DecorationCollectionControl : UserControl
{
#region Events
/// <summary>
/// Occurs when someone clicks the add button.
/// </summary>
public event EventHandler AddClicked;
/// <summary>
/// Occurs when someone selects one of the items in the list box
/// </summary>
public event EventHandler SelectedItemChanged;
/// <summary>
/// Occurs when either the Promote or Demote function has been used,
/// changing the order.
/// </summary>
public event EventHandler OrderChanged;
/// <summary>
/// Occurs when the remove button has been clicked
/// </summary>
public event EventHandler RemoveClicked;
/// <summary>
/// Occurs when the list has been added, removed, or re-ordered in any way.
/// </summary>
public event EventHandler ListChanged;
#endregion
#region Private Variables
private IList<ILineDecoration> _decorations;
private Button btnAdd;
private Button btnDown;
private Button btnRemove;
private Button btnUp;
private ListBox lbxItems;
private Panel panel1;
#endregion
#region Constructors
/// <summary>
/// Creates a new instance of the Collection Control
/// </summary>
public DecorationCollectionControl()
{
InitializeComponent();
_decorations = new List<ILineDecoration>();
lbxItems.DrawItem += lbxItems_DrawItem;
}
private void lbxItems_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle outer = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, outer);
}
else
{
Brush b = new SolidBrush(BackColor);
e.Graphics.FillRectangle(b, outer);
b.Dispose();
}
Rectangle inner = new Rectangle(e.Bounds.X + 5, e.Bounds.Y + 1, e.Bounds.Width - 10, e.Bounds.Height - 3);
e.Graphics.FillRectangle(Brushes.White, inner);
e.Graphics.DrawRectangle(Pens.Black, inner);
int index = (e.Index < 0) ? 0 : e.Index;
if (lbxItems.Items.Count == 0) return;
ILineDecoration lineDecoration = lbxItems.Items[index] as ILineDecoration;
if (lineDecoration == null) return;
GraphicsPath gp = new GraphicsPath();
gp.AddLine(new Point(e.Bounds.X + 10, e.Bounds.Y + e.Bounds.Height / 2), new Point(e.Bounds.Width - 10, e.Bounds.Y + e.Bounds.Height / 2));
lineDecoration.Draw(e.Graphics, gp, 1);
gp.Dispose();
}
#endregion
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DecorationCollectionControl));
this.lbxItems = new System.Windows.Forms.ListBox();
this.panel1 = new System.Windows.Forms.Panel();
this.btnDown = new System.Windows.Forms.Button();
this.btnUp = new System.Windows.Forms.Button();
this.btnRemove = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// lbxItems
//
resources.ApplyResources(this.lbxItems, "lbxItems");
this.lbxItems.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.lbxItems.FormattingEnabled = true;
this.lbxItems.Name = "lbxItems";
this.lbxItems.SelectedIndexChanged += new System.EventHandler(this.lbxItems_SelectedIndexChanged);
//
// panel1
//
this.panel1.Controls.Add(this.btnDown);
this.panel1.Controls.Add(this.btnUp);
this.panel1.Controls.Add(this.btnRemove);
this.panel1.Controls.Add(this.btnAdd);
resources.ApplyResources(this.panel1, "panel1");
this.panel1.Name = "panel1";
//
// btnDown
//
this.btnDown.Image = global::DotSpatial.Symbology.Forms.SymbologyFormsImages.down;
resources.ApplyResources(this.btnDown, "btnDown");
this.btnDown.Name = "btnDown";
this.btnDown.UseVisualStyleBackColor = true;
this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
//
// btnUp
//
this.btnUp.Image = global::DotSpatial.Symbology.Forms.SymbologyFormsImages.up;
resources.ApplyResources(this.btnUp, "btnUp");
this.btnUp.Name = "btnUp";
this.btnUp.UseVisualStyleBackColor = true;
this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
//
// btnRemove
//
this.btnRemove.Image = global::DotSpatial.Symbology.Forms.SymbologyFormsImages.mnuLayerClear;
resources.ApplyResources(this.btnRemove, "btnRemove");
this.btnRemove.Name = "btnRemove";
this.btnRemove.UseVisualStyleBackColor = true;
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// btnAdd
//
this.btnAdd.Image = global::DotSpatial.Symbology.Forms.SymbologyFormsImages.mnuLayerAdd;
resources.ApplyResources(this.btnAdd, "btnAdd");
this.btnAdd.Name = "btnAdd";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// DecorationCollectionControl
//
this.Controls.Add(this.lbxItems);
this.Controls.Add(this.panel1);
this.Name = "DecorationCollectionControl";
resources.ApplyResources(this, "$this");
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
private void lbxItems_SelectedIndexChanged(object sender, EventArgs e)
{
OnSelectedItemChanged();
}
/// <summary>
/// Fires the SelectedItemChanged event
/// </summary>
protected virtual void OnSelectedItemChanged()
{
if (SelectedItemChanged != null) SelectedItemChanged(this, EventArgs.Empty);
}
#endregion
#region Methods
/// <summary>
/// Refreshes the items in the list to accuratly reflect the current collection
/// </summary>
public void RefreshList()
{
lbxItems.SuspendLayout();
lbxItems.Items.Clear();
foreach (ILineDecoration lineDecoration in _decorations)
{
lbxItems.Items.Insert(0, lineDecoration);
}
lbxItems.ResumeLayout();
}
#endregion
#region Properties
/// <summary>
/// Gets the selected item cast as an object.
/// </summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ILineDecoration SelectedDecoration
{
get
{
if (lbxItems == null) return null;
if (lbxItems.SelectedItem == null) return null;
return lbxItems.SelectedItem as ILineDecoration;
}
set
{
if (lbxItems == null) return;
lbxItems.SelectedItem = value;
}
}
/// <summary>
/// Gets or sets the core list of lineDecorations that will be drawn here.
/// </summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IList<ILineDecoration> Decorations
{
get { return _decorations; }
set
{
_decorations = value;
RefreshList();
}
}
#endregion
#region Event Handlers
private void btnAdd_Click(object sender, EventArgs e)
{
OnAdd();
}
private void btnDown_Click(object sender, EventArgs e)
{
if (lbxItems.SelectedItem == null) return;
ILineDecoration lineDecoration = lbxItems.SelectedItem as ILineDecoration;
if (lineDecoration == null) return;
_decorations.DecreaseIndex(lineDecoration);
RefreshList();
lbxItems.SelectedItem = lineDecoration;
OnOrderChanged();
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (lbxItems.SelectedItem == null) return;
ILineDecoration lineDecoration = lbxItems.SelectedItem as ILineDecoration;
int index = _decorations.IndexOf(lineDecoration);
_decorations.Remove(lineDecoration);
RefreshList();
if (_decorations.Count == 0) return;
if (index >= _decorations.Count)
{
index -= 1;
}
lbxItems.SelectedIndex = index;
OnRemoveClick();
}
private void btnUp_Click(object sender, EventArgs e)
{
if (lbxItems.SelectedItem == null) return;
ILineDecoration lineDecoration = lbxItems.SelectedItem as ILineDecoration;
if (lineDecoration == null) return;
_decorations.IncreaseIndex(lineDecoration);
RefreshList();
lbxItems.SelectedItem = lineDecoration;
OnOrderChanged();
}
#endregion
#region Protected Methods
/// <summary>
/// Fires the AddClicked event
/// </summary>
protected virtual void OnAdd()
{
if (AddClicked != null) AddClicked(this, EventArgs.Empty);
OnListChanged();
}
/// <summary>
/// Fires the ListChanged event
/// </summary>
protected virtual void OnListChanged()
{
if (ListChanged != null) ListChanged(this, EventArgs.Empty);
}
/// <summary>
/// Fires the RemoveCLicked event
/// </summary>
protected virtual void OnRemoveClick()
{
if (RemoveClicked != null) RemoveClicked(this, EventArgs.Empty);
OnListChanged();
}
/// <summary>
/// Fires the OnOrderChanged event
/// </summary>
protected virtual void OnOrderChanged()
{
if (OrderChanged != null) OrderChanged(this, EventArgs.Empty);
OnListChanged();
}
#endregion
}
}