**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / ColorDialog.cs
blob3fca8985a5b23cf7d09b31f247824e46f13c3e5a
1 //
2 // System.Windows.Forms.ColorDialog.cs
3 //
4 // Author:
5 // stubbed out by Jaak Simm (jaaksimm@firm.ee)
6 // Dennis Hayes (dennish@Raytek.com)
7 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
8 //
9 // (C) Ximian, Inc., 2002
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Drawing;
34 using System.Runtime.InteropServices;
36 namespace System.Windows.Forms {
38 /// <summary>
39 /// Represents a common dialog box that displays available colors along with controls that allow the user to define custom colors.
40 /// </summary>
42 public class ColorDialog : CommonDialog {
44 private bool allowFullOpen;
45 private bool anyColor;
46 private Color color;
47 private int[] customColors;
48 private bool fullOpen;
49 private bool showHelp;
50 private bool solidColorOnly;
52 public ColorDialog() : base()
54 Reset ( );
57 // these are show to be in the ms implmentation by winchurn
58 // but are not valid since they are not abstract or have any accessors.
59 //protected virtual IntPtr Instance { }
60 //protected virtual int Options { }
62 public virtual bool AllowFullOpen {
63 get {
64 return allowFullOpen;
66 set {
67 allowFullOpen = value;
71 public virtual bool AnyColor {
72 get {
73 return anyColor;
75 set {
76 anyColor = value;
80 public Color Color {
81 get {
82 return color;
84 set {
85 color = value;
89 public int[] CustomColors {
90 get {
91 return customColors;
93 set {
94 if ( value != null ) {
95 for ( int i = 0; i < Math.Min ( customColors.Length, value.Length ); i++ )
96 customColors [ i ] = value [ i ];
101 public virtual bool FullOpen {
102 get {
103 return fullOpen;
105 set {
106 fullOpen = value;
110 public virtual bool ShowHelp {
111 get {
112 return showHelp;
114 set {
115 showHelp = value;
119 public virtual bool SolidColorOnly {
120 get {
121 return solidColorOnly;
123 set {
124 solidColorOnly = value;
128 public override void Reset()
130 allowFullOpen = true;
131 anyColor = false;
132 color = Color.Black;
134 customColors = new int [ 16 ];
135 for ( int i = 0; i < customColors.Length; i++ )
136 customColors [ i ] = Win32.RGB ( Color.White );
138 fullOpen = false;
139 showHelp = false;
140 solidColorOnly = false;
143 protected override bool RunDialog( IntPtr hwndOwner )
145 CHOOSECOLOR cc = new CHOOSECOLOR ( );
146 cc.hwndOwner = hwndOwner;
147 cc.lStructSize = ( uint ) Marshal.SizeOf( cc );
148 cc.Flags = (int) ( ChooseColorFlags.CC_RGBINIT | ChooseColorFlags.CC_ENABLEHOOK );
150 cc.lpfnHook = new Win32.FnHookProc ( this.HookProc );
152 if ( AllowFullOpen ) {
153 if ( FullOpen )
154 cc.Flags |= (int) ChooseColorFlags.CC_FULLOPEN;
156 else cc.Flags |= (int) ChooseColorFlags.CC_PREVENTFULLOPEN;
158 if ( AnyColor )
159 cc.Flags |= (int) ChooseColorFlags.CC_ANYCOLOR;
161 if ( ShowHelp )
162 cc.Flags |= (int) ChooseColorFlags.CC_SHOWHELP;
164 if ( SolidColorOnly )
165 cc.Flags |= (int) ChooseColorFlags.CC_SOLIDCOLOR;
167 cc.rgbResult = Win32.RGB ( Color );
169 cc.lpCustColors = Marshal.AllocHGlobal ( Marshal.SizeOf( customColors[0] ) * customColors.Length );
170 Marshal.Copy ( customColors, 0, cc.lpCustColors, customColors.Length );
172 bool res = false;
173 try {
174 res = Win32.ChooseColor ( ref cc );
176 if ( res ) {
177 this.Color = Color.FromArgb ( cc.rgbResult );
178 Marshal.Copy ( cc.lpCustColors, customColors, 0, customColors.Length );
181 finally {
182 Marshal.FreeHGlobal ( cc.lpCustColors );
185 return res;
188 public override string ToString()
190 return GetType( ).FullName.ToString ( ) + ", Color: " + Color.ToString ( );