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
28 using System.Collections.Generic;
30 using System.ComponentModel;
31 using System.Runtime.InteropServices;
33 namespace System.Windows.Forms
35 [ComVisibleAttribute(true)]
36 [DefaultEvent ("RefreshItems")]
37 [DefaultProperty ("BindingSource")]
38 [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
39 [Designer ("System.Windows.Forms.Design.BindingNavigatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40 public class BindingNavigator : ToolStrip, ISupportInitialize
42 #region Private Fields
44 private ToolStripItem addNewItem = null;
45 private BindingSource bindingSource = null;
46 //private bool changingText = false;
47 private ToolStripItem countItem = null;
48 private string countItemFormat = Locale.GetText("of {0}");
49 private ToolStripItem deleteItem = null;
50 private bool initFlag = false;
51 private ToolStripItem moveFirstItem = null;
52 private ToolStripItem moveLastItem = null;
53 private ToolStripItem moveNextItem = null;
54 private ToolStripItem movePreviousItem = null;
55 private ToolStripItem positionItem = null;
57 #endregion Private Fields
60 #region Public Properties
62 [TypeConverter(typeof(ReferenceConverter))]
63 public ToolStripItem AddNewItem
65 get { return addNewItem; }
68 ReplaceItem(ref addNewItem, value, new EventHandler(OnAddNew));
74 [TypeConverter(typeof(ReferenceConverter))]
75 public BindingSource BindingSource
77 get { return bindingSource; }
80 AttachNewSource(value);
85 [TypeConverter(typeof(ReferenceConverter))]
86 public ToolStripItem CountItem
88 get { return countItem; }
96 public string CountItemFormat
98 get { return countItemFormat; }
101 countItemFormat = value;
106 [TypeConverter(typeof(ReferenceConverter))]
107 public ToolStripItem DeleteItem
109 get { return deleteItem; }
112 ReplaceItem(ref deleteItem, value, new EventHandler(OnDelete));
113 this.OnRefreshItems();
117 [TypeConverter(typeof(ReferenceConverter))]
118 public ToolStripItem MoveFirstItem
120 get { return moveFirstItem; }
123 ReplaceItem(ref moveFirstItem, value, new EventHandler(OnMoveFirst));
128 private void ReplaceItem(ref ToolStripItem existingItem, ToolStripItem newItem, EventHandler clickHandler)
130 if (existingItem != null)
131 existingItem.Click -= clickHandler;
133 newItem.Click += clickHandler;
135 existingItem = newItem;
139 [TypeConverter(typeof(ReferenceConverter))]
140 public ToolStripItem MoveLastItem
142 get { return moveLastItem; }
145 ReplaceItem(ref moveLastItem, value, new EventHandler(OnMoveLast));
150 [TypeConverter(typeof(ReferenceConverter))]
151 public ToolStripItem MoveNextItem
153 get { return moveNextItem; }
156 ReplaceItem(ref moveNextItem, value, new EventHandler(OnMoveNext));
161 [TypeConverter(typeof(ReferenceConverter))]
162 public ToolStripItem MovePreviousItem
164 get { return movePreviousItem; }
167 ReplaceItem(ref movePreviousItem, value, new EventHandler(OnMovePrevious));
172 [TypeConverter(typeof(ReferenceConverter))]
173 public ToolStripItem PositionItem
175 get { return positionItem; }
178 positionItem = value;
188 [EditorBrowsable (EditorBrowsableState.Never)]
189 public BindingNavigator ()
194 public BindingNavigator(BindingSource bindingSource)
197 AttachNewSource(bindingSource);
198 this.AddStandardItems();
202 public BindingNavigator(bool addStandardItems)
205 bindingSource = null;
206 if (addStandardItems)
207 this.AddStandardItems();
210 [EditorBrowsable (EditorBrowsableState.Never)]
211 public BindingNavigator(IContainer container)
214 bindingSource = null;
218 #endregion Constructors
221 #region Public Events
223 public event EventHandler RefreshItems;
228 #region Public And Protected Methods
230 public virtual void AddStandardItems()
234 MoveFirstItem = new ToolStripButton();
235 moveFirstItem.Image = ResourceImageLoader.Get("nav_first.png");
236 moveFirstItem.ToolTipText = Locale.GetText("Move first");
237 Items.Add(moveFirstItem);
239 MovePreviousItem = new ToolStripButton();
240 movePreviousItem.Image = ResourceImageLoader.Get("nav_previous.png");
241 movePreviousItem.ToolTipText = Locale.GetText("Move previous");
242 Items.Add(movePreviousItem);
244 Items.Add(new ToolStripSeparator());
246 PositionItem = new ToolStripTextBox();
247 positionItem.Width = 50;
248 positionItem.Text = (bindingSource == null ? 0 : 1).ToString();
249 positionItem.Width = 50;
250 positionItem.ToolTipText = Locale.GetText("Current position");
251 Items.Add(positionItem);
253 CountItem = new ToolStripLabel();
254 countItem.ToolTipText = Locale.GetText("Total number of items");
255 countItem.Text = Locale.GetText(countItemFormat, bindingSource == null ? 0 : bindingSource.Count);
256 Items.Add(countItem);
258 Items.Add(new ToolStripSeparator());
260 MoveNextItem = new ToolStripButton();
261 moveNextItem.Image = ResourceImageLoader.Get("nav_next.png");
262 moveNextItem.ToolTipText = Locale.GetText("Move next");
263 Items.Add(moveNextItem);
265 MoveLastItem = new ToolStripButton();
266 moveLastItem.Image = ResourceImageLoader.Get("nav_end.png");
267 moveLastItem.ToolTipText = Locale.GetText("Move last");
268 Items.Add(moveLastItem);
270 Items.Add(new ToolStripSeparator());
272 AddNewItem = new ToolStripButton();
273 addNewItem.Image = ResourceImageLoader.Get("nav_plus.png");
274 addNewItem.ToolTipText = Locale.GetText("Add new");
275 Items.Add(addNewItem);
277 DeleteItem = new ToolStripButton();
278 deleteItem.Image = ResourceImageLoader.Get("nav_delete.png");
279 deleteItem.ToolTipText = Locale.GetText("Delete");
280 Items.Add(deleteItem);
285 public void BeginInit()
290 protected override void Dispose(bool disposing)
292 base.Dispose(disposing);
295 public void EndInit()
301 protected virtual void OnRefreshItems()
306 if (this.RefreshItems != null)
307 this.RefreshItems(this, EventArgs.Empty);
309 this.RefreshItemsCore();
312 [EditorBrowsable (EditorBrowsableState.Advanced)]
313 protected virtual void RefreshItemsCore ()
317 bool is_source_available = bindingSource != null;
319 //changingText = true;
321 if (addNewItem != null)
322 addNewItem.Enabled = is_source_available && bindingSource.AllowNew;
324 if (moveFirstItem != null)
325 moveFirstItem.Enabled = is_source_available && bindingSource.Position > 0;
327 if (moveLastItem != null)
328 moveLastItem.Enabled = is_source_available && bindingSource.Position < (bindingSource.Count - 1);
330 if (moveNextItem != null)
331 moveNextItem.Enabled = is_source_available && bindingSource.Position < (bindingSource.Count - 1);
333 if (movePreviousItem != null)
334 movePreviousItem.Enabled = is_source_available && bindingSource.Position > 0;
336 if (deleteItem != null)
337 deleteItem.Enabled = is_source_available && (bindingSource.Count != 0 && bindingSource.AllowRemove);
339 if (countItem != null) {
340 countItem.Text = string.Format(countItemFormat, is_source_available ? bindingSource.Count : 0);
341 countItem.Enabled = is_source_available && bindingSource.Count > 0;
344 if (positionItem != null) {
345 positionItem.Text = string.Format("{0}", is_source_available? bindingSource.Position + 1 : 0);
346 positionItem.Enabled = is_source_available && bindingSource.Count > 0;
351 //changingText = false;
356 [MonoTODO ("Not implemented, will throw NotImplementedException")]
357 public bool Validate ()
359 throw new NotImplementedException();
365 #region Private Methode
367 private void AttachNewSource(BindingSource source)
369 if (bindingSource != null)
371 bindingSource.ListChanged -= new ListChangedEventHandler(OnListChanged);
372 bindingSource.PositionChanged -= new EventHandler(OnPositionChanged);
373 bindingSource.AddingNew -= new AddingNewEventHandler(OnAddingNew);
376 bindingSource = source;
378 if (bindingSource != null)
380 bindingSource.ListChanged += new ListChangedEventHandler(OnListChanged);
381 bindingSource.PositionChanged += new EventHandler(OnPositionChanged);
382 bindingSource.AddingNew += new AddingNewEventHandler(OnAddingNew);
386 private void OnAddNew(object sender, EventArgs e)
388 if (bindingSource != null)
389 bindingSource.AddNew();
394 private void OnAddingNew(object sender, AddingNewEventArgs e)
399 private void OnDelete(object sender, EventArgs e)
401 if (bindingSource != null)
402 bindingSource.RemoveCurrent();
407 private void OnListChanged(object sender, ListChangedEventArgs e)
412 private void OnMoveFirst(object sender, EventArgs e)
414 if (bindingSource != null)
415 bindingSource.MoveFirst();
420 private void OnMoveLast(object sender, EventArgs e)
422 if (bindingSource != null)
423 bindingSource.MoveLast();
428 private void OnMoveNext(object sender, EventArgs e)
430 if (bindingSource != null)
431 bindingSource.MoveNext();
436 private void OnMovePrevious(object sender, EventArgs e)
438 if (bindingSource != null)
439 bindingSource.MovePrevious();
444 private void OnPositionChanged(object sender, EventArgs e)
449 /*private void OnPositionTextChanged(object sender, EventArgs e)
459 ToolStripTextBox txt = sender as ToolStripTextBox;
464 if (!int.TryParse(txt.Text, out position))
466 txt.Text = (bindingSource == null ? 0 : bindingSource.Position).ToString();
473 if (position > bindingSource.Count)
474 position = bindingSource.Count;
476 bindingSource.Position = position;
481 changingText = false;