(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / StatusBar.cs
blobf9e939c48eb18ce3c39c394a2877b04c2389b5ca
1 //
2 // System.Windows.Forms.StatusBar.cs
3 //
4 // Author:
5 // stubbed out by Daniel Carrera (dcarrera@math.toronto.edu)
6 // stubbed out by Richard Baumann (biochem333@nyc.rr.com)
7 // Dennis Hayes (dennish@Raytek.com)
8 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
9 //
10 // (C) 2002/3 Ximian, Inc
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System;
35 using System.Collections;
36 using System.Drawing;
37 using System.ComponentModel;
38 using System.Runtime.InteropServices;
40 namespace System.Windows.Forms {
42 // <summary>
43 // Represents a Windows status bar control.
44 // </summary>
45 public class StatusBar : Control {
47 private bool sizingGrip;
48 private bool showPanels;
49 private StatusBarPanelCollection panels;
50 private string stext;
51 private const int GripSize = 16; // FIXME: get size from SystemMetrics
52 private const int PanelGap = 2; // FIXME: get size from StatusBar
53 private const int TextOffset = 3;
54 internal DockStyle dockstyle;
56 public StatusBar() : base()
58 Dock = DockStyle.Bottom;
59 showPanels = false;
60 sizingGrip = true;
61 base.TabStop = false;
64 public override string ToString()
66 string str = "System.Windows.Forms.StatusBar, Panels.Count: ";
67 str += Panels.Count;
68 for ( int i = 0; i < Panels.Count ; i++ ) {
70 str += ", Panels[" + i + "]: " + Panels[i].ToString ( );
72 return str;
75 protected override void CreateHandle()
77 initCommonControlsLibrary ( );
78 base.CreateHandle();
81 [MonoTODO]
82 protected override void Dispose(bool disposing) {
83 // FIXME:
84 base.Dispose(disposing);
87 protected virtual void OnDrawItem(StatusBarDrawItemEventArgs e)
89 if( DrawItem != null)
90 DrawItem ( this, e );
93 protected override void OnHandleCreated(EventArgs e)
95 base.OnHandleCreated(e);
96 SetPanelsImpl ( );
99 [MonoTODO]
100 protected override void OnHandleDestroyed(EventArgs e)
102 //FIXME:
103 base.OnHandleDestroyed(e);
106 [MonoTODO]
107 protected override void OnLayout(LayoutEventArgs e)
109 base.OnLayout(e);
110 if ( IsHandleCreated )
111 UpdateParts ( );
114 [MonoTODO]
115 protected override void OnMouseDown(MouseEventArgs e)
117 //FIXME:
118 base.OnMouseDown(e);
121 protected virtual void OnPanelClick(StatusBarPanelClickEventArgs e)
123 if ( PanelClick != null )
124 PanelClick ( this , e );
127 [MonoTODO]
128 protected override void OnResize(EventArgs e)
130 UpdatePanels( true, false, null );
131 base.OnResize(e);
134 [MonoTODO]
135 protected override void WndProc(ref Message m)
137 switch ((Msg) m.Msg ) {
138 case Msg.WM_DRAWITEM:
139 DRAWITEMSTRUCT dis = new DRAWITEMSTRUCT();
140 dis = (DRAWITEMSTRUCT)Marshal.PtrToStructure( m.LParam, dis.GetType() );
142 if ( dis.itemID < Panels.Count ) {
143 OnDrawItem (
144 new StatusBarDrawItemEventArgs (
145 Graphics.FromHdc ( dis.hDC ),
146 Font,
147 new Rectangle( dis.rcItem.left,
148 dis.rcItem.top,
149 dis.rcItem.right - dis.rcItem.left,
150 dis.rcItem.bottom - dis.rcItem.top),
151 dis.itemID,
152 (DrawItemState)dis.itemState,
153 Panels[dis.itemID] ) );
155 m.Result = (IntPtr)1;
156 break;
157 case Msg.WM_NOTIFY:
158 // FIXME
159 break;
160 default:
161 base.WndProc(ref m);
162 break;
166 public event StatusBarDrawItemEventHandler DrawItem;
167 public event StatusBarPanelClickEventHandler PanelClick;
169 public override Color BackColor {
170 get { return base.BackColor; }
171 set { base.BackColor = value; }
174 public override Image BackgroundImage {
175 get { return base.BackgroundImage; }
176 set { base.BackgroundImage = value; }
179 //FIXME:
180 [MonoTODO]
181 public override DockStyle Dock {
182 get {
183 return dockstyle;
185 set {
186 dockstyle = value;
190 public override Font Font {
192 get { return base.Font; }
193 set { base.Font = value; }
196 public override Color ForeColor {
198 get { return base.ForeColor; }
199 set { base.ForeColor = value; }
202 public new ImeMode ImeMode {
204 get { return DefaultImeMode; }
205 set { }
208 public StatusBar.StatusBarPanelCollection Panels {
209 get {
210 if( panels == null )
211 panels = new StatusBar.StatusBarPanelCollection( this );
212 return panels;
216 public bool ShowPanels {
217 get { return showPanels; }
218 set {
219 showPanels = value;
220 SetPanelsImpl ( );
224 [MonoTODO]
225 public bool SizingGrip
227 get { return sizingGrip; }
228 set {
229 // the only way to get rid of the grip dynamically
230 // is to recreate window
231 bool recreate = sizingGrip != value;
232 sizingGrip = value;
233 if ( IsHandleCreated && recreate )
234 RecreateHandle();
238 [MonoTODO]
239 public new bool TabStop {
240 get { return false; }
241 set { }
244 [MonoTODO]
245 public override string Text {
246 get { // should reuse base.Text ?
247 return stext;
249 set {
250 stext = value;
251 if ( IsHandleCreated )
252 UpdateStatusText ( );
257 // --- Protected Properties
259 [MonoTODO]
260 protected override CreateParams CreateParams {
261 get {
262 CreateParams createParams = base.CreateParams;
264 createParams.ClassName = "msctls_statusbar32";
266 createParams.Style = (int) (
267 WindowStyles.WS_CHILD |
268 WindowStyles.WS_VISIBLE |
269 WindowStyles.WS_OVERLAPPED |
270 WindowStyles.WS_CLIPCHILDREN |
271 WindowStyles.WS_CLIPCHILDREN ) |
272 (int)( CommonControlStyles.CCS_NORESIZE );
274 if( SizingGrip )
275 createParams.Style |= (int)StatusbarControlStyles.SBARS_SIZEGRIP;
277 createParams.Style |= (int)StatusbarControlStyles.SBT_TOOLTIPS;
279 return createParams;
283 protected override ImeMode DefaultImeMode {
284 get { return ImeMode.Disable; }
287 protected override Size DefaultSize {
288 get { return new Size ( 100, 22 ); }
291 internal void UpdateParts ( ) {
292 if ( Panels.Count > 0) {
293 int[] array = new int[ panels.Count ];
295 CalculatePanelWidths ( array );
296 int size = array.Length;
298 IntPtr buffer = Marshal.AllocHGlobal ( Marshal.SizeOf( size ) * size );
299 Marshal.Copy( array, 0, buffer, size );
300 Win32.SendMessage( Handle, (int)StatusbarMessages.SB_SETPARTS, size, buffer.ToInt32() );
301 Win32.SendMessage( Handle, (int)StatusbarMessages.SB_SIMPLE, 0, 0 );
302 Marshal.FreeHGlobal ( buffer );
304 else {
305 Win32.SendMessage( Handle, (int)StatusbarMessages.SB_SIMPLE, 1, 0 );
306 UpdateStatusText ( );
310 internal void UpdateText ( StatusBarPanel p ) {
311 // if p is not null then this call is request to
312 // update text in some specific panel
313 for (int i = 0; i < panels.Count; i++ ) {
314 if ( p != null && p != panels[i] )
315 continue;
317 int DrawStyle = i;
319 if ( panels[i].Style == StatusBarPanelStyle.OwnerDraw )
320 DrawStyle |= (int)StatusbarDrawType.SBT_OWNERDRAW;
322 switch ( panels[i].BorderStyle )
324 case StatusBarPanelBorderStyle.None:
325 DrawStyle |= (int)StatusbarDrawType.SBT_NOBORDERS;
326 break;
327 case StatusBarPanelBorderStyle.Raised:
328 DrawStyle |= (int)StatusbarDrawType.SBT_POPOUT;
329 break;
332 string TextToSet;
334 switch ( panels[i].Alignment ) {
335 case HorizontalAlignment.Center:
336 TextToSet = panels[i].Text.Insert( 0, "\t" );
337 break;
338 case HorizontalAlignment.Right:
339 TextToSet = panels[i].Text.Insert( 0, "\t\t" );
340 break;
341 default:
342 TextToSet = panels[i].Text;
343 break;
346 Win32.SendMessage( Handle, (int)StatusbarMessages.SB_SETTEXT, DrawStyle,
347 TextToSet );
351 internal void UpdateToolTips ( StatusBarPanel p ) {
352 // if p == null set tooltips for each panel
353 for (int i = 0; i < panels.Count; i++ ) {
354 if ( p != null && p != panels[i] )
355 continue;
357 Win32.SendMessage ( Handle, (int)StatusbarMessages.SB_SETTIPTEXT, i ,
358 panels[i].ToolTipText );
362 internal void UpdatePanels ( bool updateParts, bool updateText, StatusBarPanel p ) {
363 if ( IsHandleCreated ) {
364 if ( updateParts )
365 UpdateParts ( );
367 if ( updateText )
368 UpdateText( p );
370 Invalidate( );
374 protected void CalculatePanelWidths ( int[] array ) {
375 int[] WidthArray = new int[panels.Count];
377 int FixedWidth = ClientSize.Width - (SizingGrip == true ? GripSize : 0);
378 int NumSpringPanels = 0;
380 for (int i = 0; i < panels.Count; i++ ) {
381 switch ( panels[i].AutoSize ) {
382 case StatusBarPanelAutoSize.None:
383 WidthArray[i] = panels[i].Width + (PanelGap + TextOffset)*2;
384 break;
385 case StatusBarPanelAutoSize.Contents:
386 WidthArray[i] = panels[i].GetContentWidth( ) + (PanelGap + TextOffset)*2;
387 break;
388 default:
389 WidthArray[i] = 0;
390 NumSpringPanels++;
391 break;
393 FixedWidth -= WidthArray[i];
396 int SpringPanelLength = 0;
397 if ( NumSpringPanels > 0 && FixedWidth > 0)
398 SpringPanelLength = FixedWidth / NumSpringPanels;
400 for (int i = 0; i < panels.Count; i++ ) {
401 if ( panels[i].AutoSize == StatusBarPanelAutoSize.Spring)
402 WidthArray[i] = SpringPanelLength > panels[i].MinWidth ?
403 SpringPanelLength : panels[i].MinWidth;
406 for (int i = 0; i < panels.Count; i++ )
407 array[i] = WidthArray[i] + (i == 0 ? 0 : array[i - 1]);
410 internal void UpdateStatusText ( ){
411 Win32.SendMessage( Handle, (int)StatusbarMessages.SB_SETTEXT,
412 255 | (int)StatusbarDrawType.SBT_NOBORDERS, Text );
415 internal void SetPanelsImpl ( ) {
416 if( IsHandleCreated ) {
417 if ( base.Font.ToHfont ( ) != IntPtr.Zero )
418 Win32.SendMessage ( Handle, Msg.WM_SETFONT, base.Font.ToHfont().ToInt32(), 0 );
420 if( panels == null || panels.Count == 0 || showPanels == false) {
421 Win32.SendMessage( Handle, (int)StatusbarMessages.SB_SIMPLE, 1, 0 );
422 UpdateStatusText ( );
424 else {
425 UpdatePanels ( true, true, null );
426 UpdateToolTips ( null );
431 private void initCommonControlsLibrary ( ) {
432 if ( !RecreatingHandle ) {
433 INITCOMMONCONTROLSEX initEx = new INITCOMMONCONTROLSEX();
434 initEx.dwICC = CommonControlInitFlags.ICC_BAR_CLASSES;
435 Win32.InitCommonControlsEx(initEx);
440 // System.Windows.Forms.StatusBar.StatusBarPanelCollection
442 // Author:
443 // stubbed out by Richard Baumann (biochem333@nyc.rr.com)
444 // stub ammended by Jaak Simm (jaaksimm@firm.ee)
445 // Implemented by Richard Baumann <biochem333@nyc.rr.com>
446 // (C) Ximian, Inc., 2002
448 // <summary>
449 // Represents the collection of panels in a StatusBar control.
450 // </summary>
451 public class StatusBarPanelCollection : IList, ICollection, IEnumerable {
452 private ArrayList list;
453 private StatusBar owner;
455 public StatusBarPanelCollection( StatusBar owner ) : base() {
456 list = new ArrayList();
457 this.owner = owner;
460 public virtual int Add( StatusBarPanel value ) {
461 if (value == null)
462 throw new ArgumentNullException("value");
464 if (value.Parent != null)
465 throw new ArgumentException("Object already has a parent.", "value");
467 value.SetParent( owner );
468 int Index = list.Add( value );
470 owner.UpdatePanels ( true, true, null );
471 return Index;
474 public virtual StatusBarPanel Add( string text ) {
475 StatusBarPanel panel = new StatusBarPanel();
476 panel.Text = text;
477 this.Add ( panel );
478 return panel;
481 public virtual void AddRange(StatusBarPanel[] panels) {
482 if (panels == null)
483 throw new ArgumentNullException("panels");
485 // do we need to check for panel.Parent
486 // like it is done in Add(StatusBarPanel) ?
488 for (int i = 0; i < panels.Length; i++)
489 panels[i].SetParent( owner );
491 list.AddRange(panels);
492 owner.UpdatePanels ( true, true, null );
495 public virtual void Clear() {
496 for (int i = 0; i < list.Count; i++ )
497 ((StatusBarPanel)list[i]).SetParent ( null );
499 list.Clear();
500 owner.UpdatePanels ( true, true, null );
503 public bool Contains(StatusBarPanel panel) {
504 return list.Contains(panel);
507 public IEnumerator GetEnumerator() {
508 return list.GetEnumerator();
511 public int IndexOf(StatusBarPanel panel) {
512 return list.IndexOf(panel);
515 public virtual void Insert(int index, StatusBarPanel value) {
516 if (value == null)
517 throw new ArgumentNullException ( "value" );
519 if (value.Parent != null)
520 throw new ArgumentException ( "Object already has a parent.", "value" );
522 if (index < 0 || index > Count )
523 throw new ArgumentOutOfRangeException( "index" );
525 // very strange place to check autosize property :-))
526 if ( !Enum.IsDefined ( typeof(StatusBarPanelAutoSize), value.AutoSize ) )
527 throw new InvalidEnumArgumentException( "AutoSize",
528 (int)value.AutoSize,
529 typeof(StatusBarPanelAutoSize));
531 list.Insert(index, value);
532 value.SetParent ( owner );
533 owner.UpdatePanels ( true, true , null );
536 public virtual void Remove(StatusBarPanel value) {
537 if (value == null)
538 throw new ArgumentNullException( "value" );
540 list.Remove( value );
541 value.SetParent ( null );
544 public virtual void RemoveAt(int index) {
545 if (index < 0 || index > Count )
546 throw new ArgumentOutOfRangeException( "index" );
548 StatusBarPanel p = (StatusBarPanel)list[index];
549 list.RemoveAt(index);
550 p.SetParent ( null );
551 owner.UpdatePanels( true, true, null );
554 [MonoTODO]
555 // This member supports the .NET Framework
556 void ICollection.CopyTo(Array array, int index) {
557 if (array == null)
558 throw new ArgumentNullException ( "array" );
560 if (index < 0)
561 throw new ArgumentOutOfRangeException ( "index" );
563 if (array.Rank != 1 || index >= array.Length || Count+index >= array.Length)
564 throw new ArgumentException ( ); // FIXME: messages
566 // easier/quicker to let the runtime throw the invalid cast exception if necessary
567 for (int i = 0; index < array.Length; i++, index++)
568 array.SetValue(list[i], index);
571 [MonoTODO]
572 int IList.Add(object panel)
574 if (!(panel is StatusBarPanel))
575 throw new ArgumentException();//FIXME: message
576 return Add((StatusBarPanel) panel);
579 bool IList.Contains(object panel)
581 if (!(panel is StatusBarPanel))
582 return false;
583 return Contains((StatusBarPanel) panel);
586 int IList.IndexOf(object panel) {
587 if (!(panel is StatusBarPanel))
588 return -1;
589 return IndexOf((StatusBarPanel) panel);
592 [MonoTODO]
593 void IList.Insert(int index, object panel)
595 if (!(panel is StatusBarPanel))
596 throw new ArgumentException();//FIXME: message
598 Insert(index, (StatusBarPanel) panel);
601 [MonoTODO]
602 void IList.Remove(object panel)
604 if (!(panel is StatusBarPanel))
605 throw new ArgumentException(); //FIXME: message
607 Remove((StatusBarPanel) panel);
611 public int Count {
612 get { return list.Count; }
615 public bool IsReadOnly {
616 get { return false; }
619 object IList.this[int index] {
620 get { return this[index]; }
621 set { this[index]= (StatusBarPanel)value; }
624 public virtual StatusBarPanel this[int index] {
627 // The same checks are done by the list, so this is redundant
628 // This is left here in case you prefer better exception messages over performance
629 //string method_string = "get_Item(int) ";
630 //if (index < 0)
632 // throw new ArgumentOutOfRangeException(class_string + method_string + "index < 0");
634 //if (index >= Count)
636 // throw new ArgumentOutOfRangeException(class_string + method_string + "index >= Count");
638 return (StatusBarPanel)list[index];
642 // The same checks are done by the list, so this is redundant
643 // This is left here in case you prefer better exception messages over performance
644 //string method_string = "set_Item(int,StatusBarPanel) ";
645 //if (index < 0)
647 // throw new ArgumentOutOfRangeException(class_string + method_string + "index < 0");
649 //if (index >= Count)
651 // throw new ArgumentOutOfRangeException(class_string + method_string + "index >= Count");
653 //if (value == null)
655 // throw new ArgumentNullException(class_string + method_string + "panel == null");
657 list[index] = value;
661 bool IList.IsFixedSize {
662 [MonoTODO] get { throw new NotImplementedException (); }
665 object ICollection.SyncRoot {
667 [MonoTODO] get { throw new NotImplementedException (); }
670 bool ICollection.IsSynchronized {
672 [MonoTODO] get { throw new NotImplementedException (); }
675 private bool IsFixedSize { get { return false; } }