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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Olivier Dufour olivier.duff@free.fr
24 // Alan McGovern alan.mcgovern@gmail.com
30 using System.Collections.Generic;
32 using System.ComponentModel;
33 using System.Runtime.InteropServices;
35 namespace System.Windows.Forms
37 [ComVisibleAttribute(true)]
38 [DefaultEvent ("RefreshItems")]
39 [DefaultProperty ("BindingSource")]
40 [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
41 [Designer ("System.Windows.Forms.Design.BindingNavigatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42 public class BindingNavigator : ToolStrip, ISupportInitialize
44 #region Private Fields
46 private ToolStripItem addNewItem = null;
47 private BindingSource bindingSource = null;
48 //private bool changingText = false;
49 private ToolStripItem countItem = null;
50 private string countItemFormat = Locale.GetText("of {0}");
51 private ToolStripItem deleteItem = null;
52 private bool initFlag = false;
53 private ToolStripItem moveFirstItem = null;
54 private ToolStripItem moveLastItem = null;
55 private ToolStripItem moveNextItem = null;
56 private ToolStripItem movePreviousItem = null;
57 private ToolStripItem positionItem = null;
59 #endregion Private Fields
62 #region Public Properties
64 [TypeConverter(typeof(ReferenceConverter))]
65 public ToolStripItem AddNewItem
67 get { return addNewItem; }
70 ReplaceItem(ref addNewItem, value, new EventHandler(OnAddNew));
76 [TypeConverter(typeof(ReferenceConverter))]
77 public BindingSource BindingSource
79 get { return bindingSource; }
82 AttachNewSource(value);
87 [TypeConverter(typeof(ReferenceConverter))]
88 public ToolStripItem CountItem
90 get { return countItem; }
98 public string CountItemFormat
100 get { return countItemFormat; }
103 countItemFormat = value;
108 [TypeConverter(typeof(ReferenceConverter))]
109 public ToolStripItem DeleteItem
111 get { return deleteItem; }
114 ReplaceItem(ref deleteItem, value, new EventHandler(OnDelete));
115 this.OnRefreshItems();
119 [TypeConverter(typeof(ReferenceConverter))]
120 public ToolStripItem MoveFirstItem
122 get { return moveFirstItem; }
125 ReplaceItem(ref moveFirstItem, value, new EventHandler(OnMoveFirst));
130 private void ReplaceItem(ref ToolStripItem existingItem, ToolStripItem newItem, EventHandler clickHandler)
132 if (existingItem != null)
133 existingItem.Click -= clickHandler;
135 newItem.Click += clickHandler;
137 existingItem = newItem;
141 [TypeConverter(typeof(ReferenceConverter))]
142 public ToolStripItem MoveLastItem
144 get { return moveLastItem; }
147 ReplaceItem(ref moveLastItem, value, new EventHandler(OnMoveLast));
152 [TypeConverter(typeof(ReferenceConverter))]
153 public ToolStripItem MoveNextItem
155 get { return moveNextItem; }
158 ReplaceItem(ref moveNextItem, value, new EventHandler(OnMoveNext));
163 [TypeConverter(typeof(ReferenceConverter))]
164 public ToolStripItem MovePreviousItem
166 get { return movePreviousItem; }
169 ReplaceItem(ref movePreviousItem, value, new EventHandler(OnMovePrevious));
174 [TypeConverter(typeof(ReferenceConverter))]
175 public ToolStripItem PositionItem
177 get { return positionItem; }
180 positionItem = value;
190 [EditorBrowsable (EditorBrowsableState.Never)]
191 public BindingNavigator ()
196 public BindingNavigator(BindingSource bindingSource)
199 AttachNewSource(bindingSource);
200 this.AddStandardItems();
204 public BindingNavigator(bool addStandardItems)
207 bindingSource = null;
208 if (addStandardItems)
209 this.AddStandardItems();
212 [EditorBrowsable (EditorBrowsableState.Never)]
213 public BindingNavigator(IContainer container)
216 bindingSource = null;
220 #endregion Constructors
223 #region Public Events
225 public event EventHandler RefreshItems;
230 #region Public And Protected Methods
232 public virtual void AddStandardItems()
236 MoveFirstItem = new ToolStripButton();
237 moveFirstItem.Image = ResourceImageLoader.Get("nav_first.png");
238 moveFirstItem.ToolTipText = Locale.GetText("Move first");
239 Items.Add(moveFirstItem);
241 MovePreviousItem = new ToolStripButton();
242 movePreviousItem.Image = ResourceImageLoader.Get("nav_previous.png");
243 movePreviousItem.ToolTipText = Locale.GetText("Move previous");
244 Items.Add(movePreviousItem);
246 Items.Add(new ToolStripSeparator());
248 PositionItem = new ToolStripTextBox();
249 positionItem.Width = 50;
250 positionItem.Text = (bindingSource == null ? 0 : 1).ToString();
251 positionItem.Width = 50;
252 positionItem.ToolTipText = Locale.GetText("Current position");
253 Items.Add(positionItem);
255 CountItem = new ToolStripLabel();
256 countItem.ToolTipText = Locale.GetText("Total number of items");
257 countItem.Text = Locale.GetText(countItemFormat, bindingSource == null ? 0 : bindingSource.Count);
258 Items.Add(countItem);
260 Items.Add(new ToolStripSeparator());
262 MoveNextItem = new ToolStripButton();
263 moveNextItem.Image = ResourceImageLoader.Get("nav_next.png");
264 moveNextItem.ToolTipText = Locale.GetText("Move next");
265 Items.Add(moveNextItem);
267 MoveLastItem = new ToolStripButton();
268 moveLastItem.Image = ResourceImageLoader.Get("nav_end.png");
269 moveLastItem.ToolTipText = Locale.GetText("Move last");
270 Items.Add(moveLastItem);
272 Items.Add(new ToolStripSeparator());
274 AddNewItem = new ToolStripButton();
275 addNewItem.Image = ResourceImageLoader.Get("nav_plus.png");
276 addNewItem.ToolTipText = Locale.GetText("Add new");
277 Items.Add(addNewItem);
279 DeleteItem = new ToolStripButton();
280 deleteItem.Image = ResourceImageLoader.Get("nav_delete.png");
281 deleteItem.ToolTipText = Locale.GetText("Delete");
282 Items.Add(deleteItem);
287 public void BeginInit()
292 protected override void Dispose(bool disposing)
294 base.Dispose(disposing);
297 public void EndInit()
303 protected virtual void OnRefreshItems()
308 if (this.RefreshItems != null)
309 this.RefreshItems(this, EventArgs.Empty);
311 this.RefreshItemsCore();
314 [EditorBrowsable (EditorBrowsableState.Advanced)]
315 protected virtual void RefreshItemsCore ()
319 bool isNull = (bindingSource == null);
321 //changingText = true;
323 if (addNewItem != null)
324 addNewItem.Enabled = isNull ? addNewItem.Enabled : this.bindingSource.AllowNew;
326 if (moveFirstItem != null)
327 moveFirstItem.Enabled = isNull ? moveFirstItem.Enabled : bindingSource.Position > 0;
329 if (moveLastItem != null)
330 moveLastItem.Enabled = isNull ? moveLastItem.Enabled : bindingSource.Position < (bindingSource.Count - 1);
332 if (moveNextItem != null)
333 moveNextItem.Enabled = isNull ? moveNextItem.Enabled : bindingSource.Position < (bindingSource.Count - 1);
335 if (movePreviousItem != null)
336 movePreviousItem.Enabled = isNull ? movePreviousItem.Enabled : bindingSource.Position > 0;
338 if (deleteItem != null)
339 deleteItem.Enabled = isNull ? deleteItem.Enabled : (bindingSource.Count != 0 && bindingSource.AllowRemove);
341 if (countItem != null) {
342 countItem.Text = string.Format(countItemFormat, isNull ? 0 : bindingSource.Count);
343 countItem.Enabled = !isNull && bindingSource.Count > 0;
346 if (positionItem != null) {
347 positionItem.Text = string.Format("{0}", isNull ? 0 : bindingSource.Position + 1);
348 positionItem.Enabled = !isNull && bindingSource.Count > 0;
353 //changingText = false;
358 [MonoTODO("implement this")]
359 public bool Validate()
361 throw new NotImplementedException();
367 #region Private Methode
369 private void AttachNewSource(BindingSource source)
371 if (bindingSource != null)
373 bindingSource.ListChanged -= new ListChangedEventHandler(OnListChanged);
374 bindingSource.PositionChanged -= new EventHandler(OnPositionChanged);
375 bindingSource.AddingNew -= new AddingNewEventHandler(OnAddingNew);
378 bindingSource = source;
380 if (bindingSource != null)
382 bindingSource.ListChanged += new ListChangedEventHandler(OnListChanged);
383 bindingSource.PositionChanged += new EventHandler(OnPositionChanged);
384 bindingSource.AddingNew += new AddingNewEventHandler(OnAddingNew);
388 private void OnAddNew(object sender, EventArgs e)
390 if (bindingSource != null)
391 bindingSource.AddNew();
396 private void OnAddingNew(object sender, AddingNewEventArgs e)
401 private void OnDelete(object sender, EventArgs e)
403 if (bindingSource != null)
404 bindingSource.RemoveCurrent();
409 private void OnListChanged(object sender, ListChangedEventArgs e)
414 private void OnMoveFirst(object sender, EventArgs e)
416 if (bindingSource != null)
417 bindingSource.MoveFirst();
422 private void OnMoveLast(object sender, EventArgs e)
424 if (bindingSource != null)
425 bindingSource.MoveLast();
430 private void OnMoveNext(object sender, EventArgs e)
432 if (bindingSource != null)
433 bindingSource.MoveNext();
438 private void OnMovePrevious(object sender, EventArgs e)
440 if (bindingSource != null)
441 bindingSource.MovePrevious();
446 private void OnPositionChanged(object sender, EventArgs e)
451 /*private void OnPositionTextChanged(object sender, EventArgs e)
461 ToolStripTextBox txt = sender as ToolStripTextBox;
466 if (!int.TryParse(txt.Text, out position))
468 txt.Text = (bindingSource == null ? 0 : bindingSource.Position).ToString();
475 if (position > bindingSource.Count)
476 position = bindingSource.Count;
478 bindingSource.Position = position;
483 changingText = false;