Splitter.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. namespace System.Windows.Forms {
  28. public class Splitter : Control {
  29. private int min_extra;
  30. private int min_size;
  31. private int move_start_x;
  32. private int move_start_y;
  33. private int thickness;
  34. private bool moving;
  35. private bool horz;
  36. private SplitterEventHandler on_splitter_moved;
  37. private SplitterEventHandler on_splitter_moving;
  38. private Control adjacent;
  39. public Splitter ()
  40. {
  41. SetStyle (ControlStyles.UserPaint, true);
  42. SetStyle (ControlStyles.StandardClick, true);
  43. SetStyle (ControlStyles.StandardDoubleClick, true);
  44. SetStyle (ControlStyles.AllPaintingInWmPaint, true);
  45. Dock = DockStyle.Left;
  46. }
  47. public override DockStyle Dock {
  48. get { return base.Dock; }
  49. set {
  50. if (value == base.Dock)
  51. return;
  52. switch (value) {
  53. case DockStyle.Bottom:
  54. case DockStyle.Top:
  55. horz = true;
  56. break;
  57. case DockStyle.Left:
  58. case DockStyle.Right:
  59. horz = false;
  60. break;
  61. default:
  62. throw new ArgumentException ("A splitter control must be docked left, right, top, or bottom.");
  63. }
  64. base.Dock = value;
  65. }
  66. }
  67. public int MinExtra {
  68. get { return min_extra; }
  69. set {
  70. if (value < 0)
  71. value = 0;
  72. min_extra = value;
  73. }
  74. }
  75. public int MinSize {
  76. get {
  77. return min_size;
  78. }
  79. set {
  80. if(value < 0)
  81. value = 0;
  82. min_size = value;
  83. }
  84. }
  85. public int SplitPosition {
  86. get {
  87. Control adjacent = FindAdjacentControl ();
  88. if (adjacent == null)
  89. return -1;
  90. if (horz)
  91. return adjacent.Width;
  92. return adjacent.Height;
  93. }
  94. set {
  95. adjacent = FindAdjacentControl ();
  96. if (adjacent == null)
  97. return;
  98. if (horz) {
  99. if (adjacent.Height == value)
  100. return;
  101. OnSplitterMoved (new SplitterEventArgs (Left, Top, Left, value));
  102. return;
  103. }
  104. if (adjacent.Width == value)
  105. return;
  106. OnSplitterMoved (new SplitterEventArgs (adjacent.Width / 2, adjacent.Height / 2, value, Top));
  107. adjacent = null;
  108. }
  109. }
  110. protected override Size DefaultSize {
  111. get {
  112. return new Size (3, 3);
  113. }
  114. }
  115. protected virtual void OnSplitterMoved (SplitterEventArgs e)
  116. {
  117. if (on_splitter_moved != null)
  118. on_splitter_moved (this, e);
  119. Move (e.SplitX, e.SplitY);
  120. }
  121. protected virtual void OnSplitterMoving (SplitterEventArgs e)
  122. {
  123. if (on_splitter_moving != null)
  124. on_splitter_moving (this, e);
  125. Move (e.SplitX, e.SplitY);
  126. }
  127. protected override void OnMouseDown (MouseEventArgs e)
  128. {
  129. base.OnMouseDown (e);
  130. if (!moving && e.Button == MouseButtons.Left) {
  131. adjacent = FindAdjacentControl ();
  132. move_start_x = e.X;
  133. move_start_y = e.Y;
  134. moving = true;
  135. Capture = true;
  136. }
  137. }
  138. protected override void OnMouseMove (MouseEventArgs e)
  139. {
  140. base.OnMouseMove (e);
  141. if (moving) {
  142. int x_move = e.X - move_start_x;
  143. int y_move = e.Y - move_start_y;
  144. move_start_x = e.X;
  145. move_start_y = e.Y;
  146. if (horz) {
  147. Top = Top + y_move;
  148. } else {
  149. Left = Left + x_move;
  150. }
  151. OnSplitterMoving (new SplitterEventArgs (e.X, e.Y, Left, Top));
  152. }
  153. }
  154. protected override void OnMouseUp (MouseEventArgs e)
  155. {
  156. base.OnMouseDown (e);
  157. moving = false;
  158. Capture = false;
  159. adjacent = null;
  160. }
  161. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  162. {
  163. if (horz) {
  164. if (height <= 0)
  165. thickness = 3;
  166. else
  167. thickness = height;
  168. } else {
  169. if (width <= 0)
  170. thickness = 3;
  171. else
  172. thickness = width;
  173. }
  174. base.SetBoundsCore (x, y, width, height, specified);
  175. }
  176. private void Draw ()
  177. {
  178. using (Graphics pdc = Parent.CreateGraphics ()) {
  179. pdc.FillRectangle (new SolidBrush (Color.Red), ClientRectangle);
  180. }
  181. }
  182. private void Move (int x, int y)
  183. {
  184. if (adjacent == null)
  185. return;
  186. if (horz) {
  187. if (adjacent.Height == y)
  188. return;
  189. adjacent.Height = y;
  190. return;
  191. }
  192. if (adjacent.Width == x)
  193. return;
  194. adjacent.Width = x;
  195. Draw ();
  196. }
  197. private Control FindAdjacentControl ()
  198. {
  199. if (Parent == null)
  200. return null;
  201. foreach (Control sibling in Parent.Controls) {
  202. if (!sibling.Visible)
  203. continue;
  204. switch (Dock) {
  205. case DockStyle.Left:
  206. if (sibling.Right == Left)
  207. return sibling;
  208. break;
  209. case DockStyle.Right:
  210. if (sibling.Left == Right)
  211. return sibling;
  212. break;
  213. case DockStyle.Top:
  214. if (sibling.Bottom == Top)
  215. return sibling;
  216. break;
  217. case DockStyle.Bottom:
  218. if (sibling.Top == Bottom)
  219. return sibling;
  220. break;
  221. }
  222. }
  223. return null;
  224. }
  225. public new event EventHandler BackgroundImageChanged {
  226. add { base.BackgroundImageChanged += value; }
  227. remove { base.BackgroundImageChanged -= value; }
  228. }
  229. public new event EventHandler Enter {
  230. add { base.Enter += value; }
  231. remove { base.Enter -= value; }
  232. }
  233. public new event EventHandler FontChanged {
  234. add { base.FontChanged += value; }
  235. remove { base.FontChanged -= value; }
  236. }
  237. public new event EventHandler ForeColorChanged {
  238. add { base.ForeColorChanged += value; }
  239. remove { base.ForeColorChanged -= value; }
  240. }
  241. public new event EventHandler ImeModeChanged {
  242. add { base.ImeModeChanged += value; }
  243. remove { base.ImeModeChanged -= value; }
  244. }
  245. public new event KeyEventHandler KeyDown {
  246. add { base.KeyDown += value; }
  247. remove { base.KeyDown -= value; }
  248. }
  249. public new event KeyPressEventHandler KeyPress {
  250. add { base.KeyPress += value; }
  251. remove { base.KeyPress -= value; }
  252. }
  253. public new event KeyEventHandler KeyUp {
  254. add { base.KeyUp += value; }
  255. remove { base.KeyUp -= value; }
  256. }
  257. public new event EventHandler Leave {
  258. add { base.Leave += value; }
  259. remove { base.Leave -= value; }
  260. }
  261. public new event EventHandler TabStopChanged {
  262. add { base.TabStopChanged += value; }
  263. remove { base.TabStopChanged -= value; }
  264. }
  265. public new event EventHandler TextChanged {
  266. add { base.TextChanged += value; }
  267. remove { base.TextChanged -= value; }
  268. }
  269. public event SplitterEventHandler SplitterMoved {
  270. add { on_splitter_moved += value; }
  271. remove { on_splitter_moved -= value; }
  272. }
  273. public event SplitterEventHandler SplitterMoving {
  274. add { on_splitter_moving += value; }
  275. remove { on_splitter_moving -= value; }
  276. }
  277. }
  278. }