1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
//
// System.Windows.Forms.Design.AnchorEditor.cs
//
//
// For creating type editors see the *great* tutorial:
// "Walkthrough: implement a UI Type Editor"
//
// Author:
// Dennis Hayes
// Miguel de Icaza (miguel@novell.com)
// (C) 2006 Novell, Inc. http://www.ximian.com
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
namespace System.Windows.Forms.Design
{
public sealed class AnchorEditor : UITypeEditor
{
#region Public Instance Constructors
public AnchorEditor()
{
}
#endregion Public Instance Constructors
#region Override implementation of UITypeEditor
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, Object value)
{
IWindowsFormsEditorService editor_service = null;
if (provider != null){
editor_service = provider.GetService (typeof (IWindowsFormsEditorService))
as IWindowsFormsEditorService;
}
if (editor_service != null){
AnchorSelector anchor_selector = new AnchorSelector (editor_service, (AnchorStyles) value);
editor_service.DropDownControl (anchor_selector);
value = anchor_selector.AnchorStyles;
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
#endregion Override implementation of UITypeEditor
}
internal class AnchorSelector : UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Name = "AnchorSelector";
this.Size = new System.Drawing.Size (150, 120);
this.ResumeLayout(false);
}
public AnchorStyles AnchorStyles {
get {
return styles;
}
}
AnchorStyles styles;
public AnchorSelector (IWindowsFormsEditorService editor_service, AnchorStyles startup)
{
styles = startup;
InitializeComponent();
BackColor = Color.White;
}
void PaintState(Graphics g, int x1, int y1, int x2, int y2, AnchorStyles v)
{
if ((styles & v) != 0)
g.DrawLine(SystemPens.MenuText, x1, y1, x2, y2);
else {
int xf = (x1 == x2) ? 10 : 0;
int yf = (y1 == y2) ? 10 : 0;
g.DrawBezier(SystemPens.MenuText,
new Point(x1, y1),
new Point((x1+x2)/2+xf, (y1+y2)/2-yf),
new Point((x1+x2)/2-xf, (y1+y2)/2+yf),
new Point(x2, y2));
}
}
protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.Graphics;
int w3 = Width / 3;
int h3 = Height / 3;
int w2 = Width/2;
int h2 = Height/2;
g.FillRectangle(Brushes.Black, new Rectangle(w3, h3, w3, h3));
PaintState (g, 0, h2, w3, h2, AnchorStyles.Left);
PaintState (g, w3 * 2, h2, Width, h2, AnchorStyles.Right);
PaintState (g, w2, 0, w2, h3, AnchorStyles.Top);
PaintState (g, w2, h3 * 2, w2, Height, AnchorStyles.Bottom);
}
protected override void OnClick (EventArgs ee)
{
Point e = PointToClient (MousePosition);
int w3 = Width / 3;
int h3 = Height / 3;
if (e.X <= w3 && e.Y > h3 && e.Y < h3 * 2)
styles = styles ^ AnchorStyles.Left;
else if (e.Y < h3 && e.X > w3 && e.X < w3 * 2)
styles = styles ^ AnchorStyles.Top;
else if (e.X > w3 * 2 && e.Y > h3 && e.Y < h3 * 2)
styles = styles ^ AnchorStyles.Right;
else if (e.Y > h3 * 2 && e.X > w3 && e.X < w3 * 2)
styles = styles ^ AnchorStyles.Bottom;
else
base.OnClick(ee);
Invalidate();
}
protected override void OnDoubleClick (EventArgs ee)
{
OnClick (ee);
}
}
}
|