Merge pull request #1516 from DavidKarlas/getThreadsHang
[mono.git] / mcs / class / System.Design / System.Windows.Forms.Design / AnchorEditor.cs
1 //
2 // System.Windows.Forms.Design.AnchorEditor.cs
3 //
4 //
5 // For creating type editors see the *great* tutorial:
6 //  "Walkthrough: implement a UI Type Editor"
7 //
8 // Author:
9 //   Dennis Hayes 
10 //   Miguel de Icaza (miguel@novell.com)
11 // (C) 2006 Novell, Inc.  http://www.ximian.com
12 //
13 //
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.
32 //
33
34 using System;
35 using System.Drawing;
36 using System.ComponentModel;
37 using System.Drawing.Design;
38 using System.Windows.Forms;
39
40 namespace System.Windows.Forms.Design
41 {
42         public sealed class AnchorEditor : UITypeEditor
43         {
44                 #region Public Instance Constructors
45
46                 public AnchorEditor()
47                 {
48                 }
49
50                 #endregion Public Instance Constructors
51
52                 #region Override implementation of UITypeEditor
53
54                 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, Object value)
55                 {
56                         IWindowsFormsEditorService editor_service = null;
57                                 
58                         if (provider != null){
59                                 editor_service = provider.GetService (typeof (IWindowsFormsEditorService))
60                                         as IWindowsFormsEditorService;
61                         }
62
63                         if (editor_service != null){
64                                 AnchorSelector anchor_selector = new AnchorSelector (editor_service, (AnchorStyles) value);
65                                 editor_service.DropDownControl (anchor_selector);
66
67                                 value = anchor_selector.AnchorStyles;
68                         }
69
70                         return value;
71                 }
72
73                 public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
74                 {
75                         return UITypeEditorEditStyle.DropDown;
76                 }
77
78                 #endregion Override implementation of UITypeEditor
79         }
80
81         internal class AnchorSelector : UserControl
82         {
83                 /// <summary> 
84                 /// Required designer variable.
85                 /// </summary>
86                 private System.ComponentModel.IContainer components = null;
87         
88                 protected override void Dispose(bool disposing)
89                 {
90                         if (disposing && (components != null)) {
91                                 components.Dispose();
92                         }
93                         base.Dispose(disposing);
94                 }
95         
96                 private void InitializeComponent()
97                 {
98                     this.SuspendLayout();
99                     this.Name = "AnchorSelector";
100                     this.Size = new System.Drawing.Size (150, 120);
101                     this.ResumeLayout(false);
102                 }
103
104                 public AnchorStyles AnchorStyles {
105                         get {
106                                 return styles;
107                         }
108                 }
109                 
110                 AnchorStyles styles;
111         
112                 public AnchorSelector (IWindowsFormsEditorService editor_service, AnchorStyles startup)
113                 {
114                         styles = startup;
115                         
116                         InitializeComponent();
117                         BackColor = Color.White;
118                 }
119         
120                 void PaintState(Graphics g, int x1, int y1, int x2, int y2, AnchorStyles v)
121                 {
122                         if ((styles & v) != 0)
123                                 g.DrawLine(SystemPens.MenuText, x1, y1, x2, y2);
124                         else {
125                                 int xf = (x1 == x2) ? 10 : 0;
126                                 int yf = (y1 == y2) ? 10 : 0;
127                                 
128                                 g.DrawBezier(SystemPens.MenuText,
129                                              new Point(x1, y1),
130                                              new Point((x1+x2)/2+xf, (y1+y2)/2-yf),
131                                              new Point((x1+x2)/2-xf, (y1+y2)/2+yf),
132                                              new Point(x2, y2));
133                         }
134                 }
135         
136                 protected override void OnPaint (PaintEventArgs e)
137                 {
138                         Graphics g = e.Graphics;
139                         int w3 = Width / 3;
140                         int h3 = Height / 3;
141                         int w2 = Width/2;
142                         int h2 = Height/2;
143                         
144                         g.FillRectangle(Brushes.Black, new Rectangle(w3, h3, w3, h3));
145                         
146                         PaintState (g, 0, h2, w3, h2, AnchorStyles.Left);
147                         PaintState (g, w3 * 2, h2, Width, h2, AnchorStyles.Right);
148                         PaintState (g, w2, 0, w2, h3, AnchorStyles.Top);
149                         PaintState (g, w2, h3 * 2, w2, Height, AnchorStyles.Bottom);
150                 }
151                 
152                 protected override void OnClick (EventArgs ee)
153                 {
154                         Point e = PointToClient (MousePosition);
155                         int w3 = Width / 3;
156                         int h3 = Height / 3;
157                         
158                         if (e.X <= w3 && e.Y > h3 && e.Y < h3 * 2)
159                                 styles = styles ^ AnchorStyles.Left;
160                         else if (e.Y < h3 && e.X > w3 && e.X < w3 * 2)
161                                 styles = styles ^ AnchorStyles.Top;
162                         else if (e.X > w3 * 2 && e.Y > h3 && e.Y < h3 * 2)
163                                 styles = styles ^ AnchorStyles.Right;
164                         else if (e.Y > h3 * 2 && e.X > w3 && e.X < w3 * 2)
165                                 styles = styles ^ AnchorStyles.Bottom;
166                         else
167                                 base.OnClick(ee);
168                         Invalidate();
169                 }
170
171                 protected override void OnDoubleClick (EventArgs ee)
172                 {
173                         OnClick (ee);
174                 }
175
176         }
177         
178 }