StatusBar.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2004-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper ([email protected])
  24. //
  25. // TODO:
  26. // - Change cursor when mouse is over grip
  27. //
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.ComponentModel.Design;
  31. using System.Drawing;
  32. using System.Drawing.Text;
  33. using System.Drawing.Imaging;
  34. namespace System.Windows.Forms {
  35. [DefaultEvent("PanelClick")]
  36. [Designer("System.Windows.Forms.Design.StatusBarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  37. [DefaultProperty("Text")]
  38. public class StatusBar : Control {
  39. #region Fields
  40. private StatusBarPanelCollection panels;
  41. private bool show_panels = false;
  42. private bool sizing_grip = true;
  43. #endregion // Fields
  44. #region Public Constructors
  45. [MonoTODO("Change cursor when mouse is over grip")]
  46. public StatusBar ()
  47. {
  48. base.Dock = DockStyle.Bottom;
  49. Anchor = AnchorStyles.Top | AnchorStyles.Left;
  50. this.TabStop = false;
  51. this.SetStyle(ControlStyles.UserPaint | ControlStyles.Selectable, false);
  52. }
  53. #endregion // Public Constructors
  54. #region Public Instance Properties
  55. [Browsable(false)]
  56. [EditorBrowsable(EditorBrowsableState.Never)]
  57. public Color BackColor {
  58. get { return base.BackColor; }
  59. set {
  60. if (value == BackColor)
  61. return;
  62. base.BackColor = value;
  63. if (BackColorChanged != null)
  64. BackColorChanged (this, EventArgs.Empty);
  65. Refresh ();
  66. }
  67. }
  68. [Browsable(false)]
  69. [EditorBrowsable(EditorBrowsableState.Never)]
  70. public Image BackgroundImage {
  71. get { return base.BackgroundImage; }
  72. set {
  73. if (value == BackgroundImage)
  74. return;
  75. base.BackgroundImage = value;
  76. if (BackgroundImageChanged != null)
  77. BackgroundImageChanged (this, EventArgs.Empty);
  78. }
  79. }
  80. [Localizable(true)]
  81. [DefaultValue(DockStyle.Bottom)]
  82. public override DockStyle Dock {
  83. get { return base.Dock; }
  84. set {
  85. if (value == Dock)
  86. return;
  87. base.Dock = value;
  88. Refresh ();
  89. }
  90. }
  91. [Localizable(true)]
  92. public override Font Font {
  93. get { return base.Font; }
  94. set {
  95. if (value == Font)
  96. return;
  97. base.Font = value;
  98. Refresh ();
  99. }
  100. }
  101. [Browsable(false)]
  102. [EditorBrowsable(EditorBrowsableState.Never)]
  103. public Color ForeColor {
  104. get { return base.ForeColor; }
  105. set {
  106. if (value == ForeColor)
  107. return;
  108. if (ForeColorChanged != null)
  109. ForeColorChanged (this, EventArgs.Empty);
  110. Refresh ();
  111. }
  112. }
  113. [Browsable(false)]
  114. [EditorBrowsable(EditorBrowsableState.Never)]
  115. public new ImeMode ImeMode {
  116. get { return base.ImeMode; }
  117. set {
  118. if (value == ImeMode)
  119. return;
  120. base.ImeMode = value;
  121. if (ImeModeChanged != null)
  122. ImeModeChanged (this, EventArgs.Empty);
  123. }
  124. }
  125. [MergableProperty(false)]
  126. [Localizable(true)]
  127. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  128. public StatusBarPanelCollection Panels {
  129. get {
  130. if (panels == null)
  131. panels = new StatusBarPanelCollection (this);
  132. return panels;
  133. }
  134. }
  135. [DefaultValue(false)]
  136. public bool ShowPanels {
  137. get { return show_panels; }
  138. set {
  139. if (show_panels == value)
  140. return;
  141. show_panels = value;
  142. }
  143. }
  144. [DefaultValue(true)]
  145. public bool SizingGrip {
  146. get { return sizing_grip; }
  147. set {
  148. if (sizing_grip == value)
  149. return;
  150. sizing_grip = value;
  151. }
  152. }
  153. [DefaultValue(false)]
  154. public new bool TabStop {
  155. get { return base.TabStop; }
  156. set { base.TabStop = value; }
  157. }
  158. [Localizable(true)]
  159. public override string Text {
  160. get { return base.Text; }
  161. set {
  162. if (value == Text)
  163. return;
  164. base.Text = value;
  165. Refresh ();
  166. }
  167. }
  168. #endregion Public Instance Properties
  169. #region Protected Instance Properties
  170. protected override CreateParams CreateParams {
  171. get {
  172. return base.CreateParams;
  173. }
  174. }
  175. protected override ImeMode DefaultImeMode {
  176. get { return ImeMode.Disable; }
  177. }
  178. protected override Size DefaultSize {
  179. get { return ThemeEngine.Current.StatusBarDefaultSize; }
  180. }
  181. #endregion // Protected Instance Properties
  182. #region Public Instance Methods
  183. public override string ToString () {
  184. return base.ToString () + ", Panels.Count: " + Panels.Count +
  185. (Panels.Count > 0 ? ", Panels[0]: " + Panels [0] : String.Empty);
  186. }
  187. #endregion // Public Instance Methods
  188. #region Protected Instance Methods
  189. protected override void CreateHandle ()
  190. {
  191. base.CreateHandle ();
  192. }
  193. protected override void Dispose (bool disposing) {
  194. base.Dispose (disposing);
  195. }
  196. protected virtual void OnDrawItem (StatusBarDrawItemEventArgs e) {
  197. if (DrawItem != null)
  198. DrawItem (this, e);
  199. }
  200. protected override void OnHandleCreated (EventArgs e) {
  201. base.OnHandleCreated (e);
  202. }
  203. protected override void OnHandleDestroyed (EventArgs e) {
  204. base.OnHandleDestroyed (e);
  205. }
  206. protected override void OnLayout (LayoutEventArgs e) {
  207. base.OnLayout (e);
  208. }
  209. protected override void OnMouseDown (MouseEventArgs e) {
  210. if (panels == null)
  211. return;
  212. float prev_x = 0;
  213. float gap = ThemeEngine.Current.StatusBarHorzGapWidth;
  214. for (int i = 0; i < panels.Count; i++) {
  215. float x = panels [i].Width + prev_x + (i == panels.Count - 1 ? gap : gap / 2);
  216. if (e.X >= prev_x && e.X <= x) {
  217. OnPanelClick (new StatusBarPanelClickEventArgs (panels [i],
  218. e.Button, e.Clicks, e.X, e.Y));
  219. break;
  220. }
  221. prev_x = x;
  222. }
  223. base.OnMouseDown (e);
  224. }
  225. protected virtual void OnPanelClick (StatusBarPanelClickEventArgs e) {
  226. if (PanelClick != null)
  227. PanelClick (this, e);
  228. }
  229. protected override void OnResize (EventArgs e)
  230. {
  231. base.OnResize (e);
  232. if (Width <= 0 || Height <= 0)
  233. return;
  234. CalcPanelSizes ();
  235. }
  236. protected override void WndProc(ref Message m) {
  237. switch ((Msg) m.Msg) {
  238. case Msg.WM_PAINT: {
  239. PaintEventArgs paint_event;
  240. paint_event = XplatUI.PaintEventStart (Handle, true);
  241. DoPaint (paint_event);
  242. XplatUI.PaintEventEnd (Handle, true);
  243. return;
  244. }
  245. }
  246. base.WndProc (ref m);
  247. }
  248. #endregion // Methods
  249. #region Internal Methods
  250. internal void OnDrawItemInternal (StatusBarDrawItemEventArgs e)
  251. {
  252. OnDrawItem (e);
  253. }
  254. private void DoPaint (PaintEventArgs pevent)
  255. {
  256. if (Width <= 0 || Height <= 0 || Visible == false)
  257. return;
  258. CalcPanelSizes ();
  259. Draw (pevent.Graphics, pevent.ClipRectangle);
  260. }
  261. private void CalcPanelSizes ()
  262. {
  263. if (panels == null || !show_panels)
  264. return;
  265. if (Width == 0 || Height == 0)
  266. return;
  267. int gap = ThemeEngine.Current.StatusBarHorzGapWidth;
  268. int taken = 0;
  269. ArrayList springs = null;
  270. for (int i = 0; i < panels.Count; i++) {
  271. StatusBarPanel p = panels [i];
  272. if (p.AutoSize == StatusBarPanelAutoSize.None) {
  273. taken += p.Width;
  274. taken += gap;
  275. continue;
  276. }
  277. if (p.AutoSize == StatusBarPanelAutoSize.Contents) {
  278. int len = (int) (DeviceContext.MeasureString (p.Text, Font).Width + 0.5F);
  279. p.Width = (int) (len + 8);
  280. taken += p.Width;
  281. taken += gap;
  282. continue;
  283. }
  284. if (p.AutoSize == StatusBarPanelAutoSize.Spring) {
  285. if (springs == null)
  286. springs = new ArrayList ();
  287. springs.Add (p);
  288. taken += gap;
  289. continue;
  290. }
  291. }
  292. if (springs == null)
  293. return;
  294. int spring_total = springs.Count;
  295. int total_width = Width - taken - (SizingGrip ? ThemeEngine.Current.StatusBarSizeGripWidth : 0);
  296. for (int i = 0; i < spring_total; i++) {
  297. StatusBarPanel p = (StatusBarPanel) springs [i];
  298. int width = total_width / spring_total;
  299. p.Width = (width >= p.MinWidth ? width : p.MinWidth);
  300. }
  301. }
  302. private void Draw (Graphics dc, Rectangle clip)
  303. {
  304. ThemeEngine.Current.DrawStatusBar (dc, this.ClientRectangle, this);
  305. }
  306. #endregion // Internal Methods
  307. #region Events
  308. [Browsable(false)]
  309. [EditorBrowsable(EditorBrowsableState.Never)]
  310. public new event EventHandler BackColorChanged;
  311. [Browsable(false)]
  312. [EditorBrowsable(EditorBrowsableState.Never)]
  313. public new event EventHandler BackgroundImageChanged;
  314. [Browsable(false)]
  315. [EditorBrowsable(EditorBrowsableState.Never)]
  316. public new event EventHandler ForeColorChanged;
  317. [Browsable(false)]
  318. [EditorBrowsable(EditorBrowsableState.Never)]
  319. public new event EventHandler ImeModeChanged;
  320. public event StatusBarDrawItemEventHandler DrawItem;
  321. [Browsable(false)]
  322. [EditorBrowsable(EditorBrowsableState.Never)]
  323. public new event PaintEventHandler Paint;
  324. public event StatusBarPanelClickEventHandler PanelClick;
  325. #endregion // Events
  326. #region Subclass StatusBarPanelCollection
  327. public class StatusBarPanelCollection : IList, ICollection, IEnumerable {
  328. #region Fields
  329. private StatusBar owner;
  330. private ArrayList panels;
  331. #endregion // Fields
  332. #region Public Constructors
  333. public StatusBarPanelCollection (StatusBar owner)
  334. {
  335. this.owner = owner;
  336. }
  337. #endregion // Public Constructors
  338. #region Private & Internal Methods
  339. private int AddInternal (StatusBarPanel p, bool refresh) {
  340. if (p == null)
  341. throw new ArgumentNullException ("value");
  342. if (panels == null)
  343. panels = new ArrayList ();
  344. int res = panels.Add (p);
  345. p.SetParent (owner);
  346. if (refresh) {
  347. owner.CalcPanelSizes ();
  348. owner.Refresh ();
  349. }
  350. return res;
  351. }
  352. #endregion // Private & Internal Methods
  353. #region Public Instance Properties
  354. [Browsable(false)]
  355. [EditorBrowsable(EditorBrowsableState.Never)]
  356. public virtual int Count {
  357. get {
  358. if (panels == null)
  359. return 0;
  360. return panels.Count;
  361. }
  362. }
  363. public virtual bool IsReadOnly {
  364. get { return false; }
  365. }
  366. public virtual StatusBarPanel this [int index] {
  367. get {
  368. if (index < 0 || index >= Count)
  369. throw new ArgumentOutOfRangeException ("index");
  370. return (StatusBarPanel) panels [index];
  371. }
  372. set {
  373. if (value == null)
  374. throw new ArgumentNullException ("index");
  375. if (index < 0 || index >= Count)
  376. throw new ArgumentOutOfRangeException ("index");
  377. panels [index] = value;
  378. }
  379. }
  380. #endregion // Public Instance Properties
  381. #region Public Instance Methods
  382. public virtual int Add (StatusBarPanel p) {
  383. return AddInternal (p, true);
  384. }
  385. public virtual StatusBarPanel Add (string text) {
  386. StatusBarPanel res = new StatusBarPanel ();
  387. res.Text = text;
  388. Add (res);
  389. return res;
  390. }
  391. public virtual void AddRange (StatusBarPanel [] range) {
  392. if (range == null)
  393. throw new ArgumentNullException ("panels");
  394. if (range.Length == 0)
  395. return;
  396. if (panels == null)
  397. panels = new ArrayList (range.Length);
  398. for (int i = 0; i < range.Length; i++)
  399. AddInternal (range [i], false);
  400. owner.Refresh ();
  401. }
  402. public virtual void Clear () {
  403. panels.Clear ();
  404. owner.Refresh ();
  405. }
  406. public virtual bool Contains (StatusBarPanel panel) {
  407. return panels.Contains (panel);
  408. }
  409. public virtual IEnumerator GetEnumerator () {
  410. return panels.GetEnumerator ();
  411. }
  412. public virtual int IndexOf (StatusBarPanel panel) {
  413. return panels.IndexOf (panel);
  414. }
  415. public virtual void Insert (int index, StatusBarPanel value) {
  416. if (value == null)
  417. throw new ArgumentNullException ("value");
  418. if (index > Count)
  419. throw new ArgumentOutOfRangeException ("index");
  420. // TODO: InvalidArgumentException for bad AutoSize values
  421. // although it seems impossible to set it to a bad value
  422. value.SetParent (owner);
  423. panels [index] = value;
  424. owner.Refresh ();
  425. }
  426. public virtual void Remove (StatusBarPanel panel) {
  427. panels.Remove (panel);
  428. }
  429. public virtual void RemoveAt (int index) {
  430. panels.RemoveAt (index);
  431. }
  432. #endregion // Public Instance Methods
  433. #region IList & ICollection Interfaces
  434. bool ICollection.IsSynchronized {
  435. get { return panels.IsSynchronized; }
  436. }
  437. object ICollection.SyncRoot {
  438. get { return panels.SyncRoot; }
  439. }
  440. void ICollection.CopyTo (Array dest, int index)
  441. {
  442. panels.CopyTo (dest, index);
  443. }
  444. object IList.this [int index] {
  445. get { return panels [index]; }
  446. set { panels [index] = value; }
  447. }
  448. int IList.Add (object value) {
  449. return panels.Add (value);
  450. }
  451. bool IList.Contains (object panel) {
  452. return panels.Contains (panel);
  453. }
  454. int IList.IndexOf (object panel)
  455. {
  456. return panels.IndexOf (panel);
  457. }
  458. void IList.Insert (int index, object value)
  459. {
  460. panels.Insert (index, value);
  461. }
  462. bool IList.IsFixedSize {
  463. get { return false; }
  464. }
  465. void IList.Remove (object value)
  466. {
  467. panels.Remove (value);
  468. }
  469. #endregion // IList & ICollection Interfaces
  470. }
  471. #endregion // Subclass StatusBarPanelCollection
  472. }
  473. }