2009-12-01 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Design / System.Windows.Forms.Design / AnchorEditor.cs
blob380e30360707a1c7a44fa217e15693927cb6f705
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
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.
34 using System;
35 using System.Drawing;
36 using System.ComponentModel;
37 using System.Drawing.Design;
38 using System.Windows.Forms;
40 namespace System.Windows.Forms.Design
42 public sealed class AnchorEditor : UITypeEditor
44 #region Public Instance Constructors
46 public AnchorEditor()
50 #endregion Public Instance Constructors
52 #region Override implementation of UITypeEditor
54 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, Object value)
56 IWindowsFormsEditorService editor_service = null;
58 if (provider != null){
59 editor_service = provider.GetService (typeof (IWindowsFormsEditorService))
60 as IWindowsFormsEditorService;
63 if (editor_service != null){
64 AnchorSelector anchor_selector = new AnchorSelector (editor_service, (AnchorStyles) value);
65 editor_service.DropDownControl (anchor_selector);
67 value = anchor_selector.AnchorStyles;
70 return value;
73 public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
75 return UITypeEditorEditStyle.DropDown;
78 #endregion Override implementation of UITypeEditor
81 internal class AnchorSelector : UserControl
83 /// <summary>
84 /// Required designer variable.
85 /// </summary>
86 private System.ComponentModel.IContainer components = null;
88 protected override void Dispose(bool disposing)
90 if (disposing && (components != null)) {
91 components.Dispose();
93 base.Dispose(disposing);
96 private void InitializeComponent()
98 this.SuspendLayout();
99 this.Name = "AnchorSelector";
100 this.Size = new System.Drawing.Size (150, 120);
101 this.ResumeLayout(false);
104 public AnchorStyles AnchorStyles {
105 get {
106 return styles;
110 AnchorStyles styles;
112 public AnchorSelector (IWindowsFormsEditorService editor_service, AnchorStyles startup)
114 styles = startup;
116 InitializeComponent();
117 BackColor = Color.White;
120 void PaintState(Graphics g, int x1, int y1, int x2, int y2, AnchorStyles v)
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;
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));
136 protected override void OnPaint (PaintEventArgs e)
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;
144 g.FillRectangle(Brushes.Black, new Rectangle(w3, h3, w3, h3));
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);
152 protected override void OnClick (EventArgs ee)
154 Point e = PointToClient (MousePosition);
155 int w3 = Width / 3;
156 int h3 = Height / 3;
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();
171 protected override void OnDoubleClick (EventArgs ee)
173 OnClick (ee);