PictureBox.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper ([email protected])
  24. //
  25. using System;
  26. using System.Drawing;
  27. using System.Drawing.Imaging;
  28. namespace System.Windows.Forms {
  29. public class PictureBox : Control {
  30. private Image image;
  31. private PictureBoxSizeMode size_mode;
  32. private BorderStyle border_style;
  33. private bool redraw;
  34. private bool recalc;
  35. private EventHandler frame_handler;
  36. public PictureBox ()
  37. {
  38. redraw = true;
  39. recalc = true;
  40. }
  41. public PictureBoxSizeMode SizeMode {
  42. get { return size_mode; }
  43. set {
  44. if (size_mode == value)
  45. return;
  46. size_mode = value;
  47. UpdateSize ();
  48. Redraw (true);
  49. Invalidate ();
  50. }
  51. }
  52. public Image Image {
  53. get { return image; }
  54. set {
  55. StopAnimation ();
  56. image = value;
  57. UpdateSize ();
  58. if (image != null && ImageAnimator.CanAnimate (image)) {
  59. frame_handler = new EventHandler (OnAnimateImage);
  60. ImageAnimator.Animate (image, frame_handler);
  61. }
  62. Redraw (true);
  63. Invalidate ();
  64. }
  65. }
  66. public BorderStyle BorderStyle {
  67. get { return border_style; }
  68. set {
  69. border_style = value;
  70. Redraw (true);
  71. }
  72. }
  73. public new bool CausesValidation {
  74. get { return base.CausesValidation; }
  75. set { base.CausesValidation = value; }
  76. }
  77. public new ImeMode ImeMode {
  78. get { return base.ImeMode; }
  79. set { base.ImeMode = value; }
  80. }
  81. public override RightToLeft RightToLeft {
  82. get { return base.RightToLeft; }
  83. set { base.RightToLeft = value; }
  84. }
  85. public new int TabIndex {
  86. get { return base.TabIndex; }
  87. set { base.TabIndex = value; }
  88. }
  89. public new bool TabStop {
  90. get { return base.TabStop; }
  91. set { base.TabStop = value; }
  92. }
  93. public override string Text {
  94. get { return base.Text; }
  95. set { base.Text = value; }
  96. }
  97. protected override CreateParams CreateParams {
  98. get {
  99. return base.CreateParams;
  100. }
  101. }
  102. protected override ImeMode DefaultImeMode {
  103. get { return base.DefaultImeMode; }
  104. }
  105. public override Font Font {
  106. get { return base.Font; }
  107. set { base.Font = value; }
  108. }
  109. public override Color ForeColor {
  110. get { return base.ForeColor; }
  111. set { base.ForeColor = value; }
  112. }
  113. protected override Size DefaultSize {
  114. get { return ThemeEngine.Current.PictureBoxDefaultSize; }
  115. }
  116. protected override void Dispose (bool disposing)
  117. {
  118. if (image != null) {
  119. StopAnimation ();
  120. image.Dispose ();
  121. image = null;
  122. }
  123. base.Dispose (disposing);
  124. }
  125. protected override void OnPaint (PaintEventArgs pe)
  126. {
  127. if (this.Width <= 0 || this.Height <= 0 || this.Visible == false)
  128. return;
  129. Draw ();
  130. pe.Graphics.DrawImage (this.ImageBuffer, pe.ClipRectangle, pe.ClipRectangle, GraphicsUnit.Pixel);
  131. }
  132. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  133. {
  134. if (size_mode == PictureBoxSizeMode.AutoSize && image != null) {
  135. width = image.Width;
  136. height = image.Height;
  137. }
  138. base.SetBoundsCore (x, y, width, height, specified);
  139. }
  140. private void StopAnimation ()
  141. {
  142. if (frame_handler == null)
  143. return;
  144. ImageAnimator.StopAnimate (image, frame_handler);
  145. frame_handler = null;
  146. }
  147. private void UpdateSize ()
  148. {
  149. if (image == null)
  150. return;
  151. if (size_mode == PictureBoxSizeMode.AutoSize)
  152. ClientSize = image.Size;
  153. }
  154. private void Redraw (bool recalc)
  155. {
  156. redraw = true;
  157. this.recalc = recalc;
  158. }
  159. private void OnAnimateImage (object sender, EventArgs e)
  160. {
  161. // This is called from a worker thread,BeginInvoke is used
  162. // so the control is updated from the correct thread
  163. BeginInvoke (new EventHandler (UpdateAnimatedImage), new object [] { this, e });
  164. }
  165. private void UpdateAnimatedImage (object sender, EventArgs e)
  166. {
  167. ImageAnimator.UpdateFrames (image);
  168. Redraw (false);
  169. Refresh ();
  170. }
  171. [MonoTODO ("Borders and stuff, and move into the Theme")]
  172. private void Draw ()
  173. {
  174. if (redraw) {
  175. ThemeEngine.Current.DrawPictureBox (DeviceContext, this);
  176. }
  177. redraw = false;
  178. }
  179. }
  180. }