StatusBarPanel.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // System.Windows.Forms.StatusBarPanel
  3. //
  4. // Author:
  5. // stubbed out by Richard Baumann ([email protected])
  6. // Dennis Hayes ([email protected])
  7. // Aleksey Ryabchuk ([email protected])
  8. //
  9. // (C) Ximian, Inc., 2002/3
  10. //
  11. using System;
  12. using System.ComponentModel;
  13. using System.Drawing;
  14. namespace System.Windows.Forms {
  15. /// <summary>
  16. /// Represents a panel in a StatusBar control.
  17. /// </summary>
  18. public class StatusBarPanel : Component, ISupportInitialize {
  19. //
  20. // --- Private Fields
  21. //
  22. private HorizontalAlignment alignment;
  23. private StatusBarPanelAutoSize autoSize;
  24. private StatusBarPanelBorderStyle borderStyle;
  25. private Icon icon;
  26. private int minWidth;
  27. private StatusBar parent;
  28. private StatusBarPanelStyle style;
  29. private string text;
  30. private string toolTipText;
  31. private int width;
  32. private bool suppressUpdates;
  33. //
  34. // --- Constructors/Destructors
  35. //
  36. public StatusBarPanel() : base()
  37. {
  38. alignment = HorizontalAlignment.Left;
  39. autoSize = StatusBarPanelAutoSize.None;
  40. borderStyle = StatusBarPanelBorderStyle.Sunken;
  41. minWidth = 10;
  42. style = StatusBarPanelStyle.Text;
  43. width = 100;
  44. suppressUpdates = false;
  45. }
  46. //
  47. // --- Public Methods
  48. //
  49. public void BeginInit()
  50. {
  51. suppressUpdates = true;
  52. }
  53. public void EndInit()
  54. {
  55. suppressUpdates = false;
  56. UpdateParent( true, true, null );
  57. }
  58. public override string ToString()
  59. {
  60. return "StatusBarPanel: {" + Text + "}";
  61. }
  62. //
  63. // --- Public Properties
  64. //
  65. public HorizontalAlignment Alignment {
  66. get { return alignment; }
  67. set {
  68. if ( !Enum.IsDefined ( typeof(HorizontalAlignment), value ) )
  69. throw new InvalidEnumArgumentException( "Alignment",
  70. (int)value,
  71. typeof(HorizontalAlignment));
  72. alignment = value;
  73. UpdateParent ( false, true, this );
  74. }
  75. }
  76. public StatusBarPanelAutoSize AutoSize {
  77. get { return autoSize; }
  78. set
  79. {
  80. if ( !Enum.IsDefined ( typeof(StatusBarPanelAutoSize), value ) )
  81. throw new InvalidEnumArgumentException( "AutoSize",
  82. (int)value,
  83. typeof(StatusBarPanelAutoSize));
  84. autoSize = value;
  85. UpdateParent ( true, false, null );
  86. }
  87. }
  88. public StatusBarPanelBorderStyle BorderStyle {
  89. get { return borderStyle; }
  90. set {
  91. if ( !Enum.IsDefined ( typeof(StatusBarPanelBorderStyle), value ) )
  92. throw new InvalidEnumArgumentException( "BorderStyle",
  93. (int)value,
  94. typeof(StatusBarPanelBorderStyle));
  95. borderStyle = value;
  96. UpdateParent ( false, true, this );
  97. }
  98. }
  99. public Icon Icon {
  100. get { return icon; }
  101. set {
  102. icon = value;
  103. UpdateParent ( true, false, null );
  104. }
  105. }
  106. public int MinWidth
  107. {
  108. get { return minWidth; }
  109. set {
  110. if ( value < 0 )
  111. throw new ArgumentException(
  112. string.Format("'{0}' is not a valid value for 'value'. 'value' must be greater than or equal to 0.",
  113. value ) ) ;
  114. minWidth = value;
  115. UpdateParent ( true, false, null );
  116. }
  117. }
  118. public StatusBar Parent {
  119. get { return parent; }
  120. }
  121. public StatusBarPanelStyle Style {
  122. get { return style; }
  123. set {
  124. if ( !Enum.IsDefined ( typeof(StatusBarPanelStyle), value ) )
  125. throw new InvalidEnumArgumentException( "Style",
  126. (int)value,
  127. typeof(StatusBarPanelStyle));
  128. style = value;
  129. UpdateParent ( false, true, this );
  130. }
  131. }
  132. public string Text {
  133. get { return text; }
  134. set {
  135. text = value;
  136. UpdateParent ( AutoSize == StatusBarPanelAutoSize.Contents, true, this );
  137. }
  138. }
  139. public string ToolTipText
  140. {
  141. get { return toolTipText; }
  142. set {
  143. toolTipText = value;
  144. UpdateTooltips ( this );
  145. }
  146. }
  147. public int Width {
  148. get { return width; }
  149. set {
  150. // According to MS documentation this method
  151. // should throw ArgumentException if value < MinWidth,
  152. // but it does not actually happens.
  153. if ( value < MinWidth )
  154. width = MinWidth;
  155. else
  156. width = value;
  157. UpdateParent ( true, false, null );
  158. }
  159. }
  160. public int GetContentWidth ( ) {
  161. if( Parent != null) {
  162. int cxsize = 0;
  163. if ( Text != null ) {}
  164. //cxsize = Win32.GetTextExtent( Parent.Handle, Text ).cx;
  165. return cxsize < MinWidth ? MinWidth : cxsize;
  166. }
  167. return Width;
  168. }
  169. private void UpdateParent ( bool UpdateParts, bool UpdateText, StatusBarPanel p ) {
  170. if ( Parent != null && suppressUpdates != true)
  171. Parent.UpdatePanels ( UpdateParts, UpdateText, p );
  172. }
  173. private void UpdateTooltips ( StatusBarPanel p ) {
  174. if ( Parent != null && suppressUpdates != true)
  175. Parent.UpdateToolTips ( p );
  176. }
  177. internal void SetParent ( StatusBar prnt ) {
  178. parent = prnt;
  179. }
  180. }
  181. }