2004-04-13 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / ColorDialog.cs
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
10 //
11
12 using System.Drawing;
13 using System.Runtime.InteropServices;
14
15 namespace System.Windows.Forms {
16
17         /// <summary>
18         /// Represents a common dialog box that displays available colors along with controls that allow the user to define custom colors.
19         /// </summary>
20
21         public class ColorDialog : CommonDialog {
22
23                 private bool allowFullOpen;
24                 private bool anyColor;
25                 private Color color;
26                 private int[] customColors;
27                 private bool fullOpen;
28                 private bool showHelp;
29                 private bool solidColorOnly;
30
31                 public ColorDialog() : base() 
32                 {
33                         Reset ( );
34                 }
35
36                 // these are show to be in the ms implmentation by winchurn
37                 // but are not valid since they are not abstract or have any accessors.
38                 //protected virtual IntPtr Instance {           }
39                 //protected virtual int Options {               }
40                 
41                 public virtual bool AllowFullOpen {
42                         get {
43                                 return allowFullOpen;
44                         }
45                         set {
46                                 allowFullOpen = value;
47                         }
48                 }
49                 
50                 public virtual bool AnyColor {
51                         get {
52                                 return anyColor;
53                         }
54                         set {
55                                 anyColor = value;
56                         }
57                 }
58                 
59                 public Color Color {
60                         get {
61                                 return color;
62                         }
63                         set {
64                                 color = value;
65                         }
66                 }
67                 
68                 public int[] CustomColors {
69                         get {
70                                 return customColors;
71                         }
72                         set {
73                                 if ( value != null ) {
74                                         for ( int i = 0; i < Math.Min ( customColors.Length, value.Length ); i++ )
75                                                 customColors [ i ] = value [ i ];
76                                 }
77                         }
78                 }
79                 
80                 public virtual bool FullOpen {
81                         get {
82                                 return fullOpen;
83                         }
84                         set {
85                                 fullOpen = value;
86                         }
87                 }
88                 
89                 public virtual bool ShowHelp {
90                         get {
91                                 return showHelp;
92                         }
93                         set {
94                                 showHelp = value;
95                         }
96                 }
97                 
98                 public virtual bool SolidColorOnly {
99                         get {
100                                 return solidColorOnly;
101                         }
102                         set {
103                                 solidColorOnly = value;
104                         }
105                 }
106
107                 public override void Reset() 
108                 {
109                         allowFullOpen = true;
110                         anyColor = false;
111                         color = Color.Black;
112
113                         customColors = new int [ 16 ];
114                         for ( int i = 0; i < customColors.Length; i++ )
115                                 customColors [ i ] = Win32.RGB ( Color.White );
116
117                         fullOpen = false;
118                         showHelp = false;
119                         solidColorOnly = false;
120                 }
121                 
122                 protected override bool RunDialog( IntPtr hwndOwner ) 
123                 {
124                         CHOOSECOLOR cc = new CHOOSECOLOR (  );
125                         cc.hwndOwner = hwndOwner;
126                         cc.lStructSize  = ( uint ) Marshal.SizeOf( cc );
127                         cc.Flags = (int) ( ChooseColorFlags.CC_RGBINIT | ChooseColorFlags.CC_ENABLEHOOK );
128
129                         cc.lpfnHook = new Win32.FnHookProc ( this.HookProc );
130
131                         if ( AllowFullOpen ) {
132                                 if ( FullOpen )
133                                         cc.Flags |= (int) ChooseColorFlags.CC_FULLOPEN;
134                         }
135                         else cc.Flags |= (int) ChooseColorFlags.CC_PREVENTFULLOPEN;
136
137                         if ( AnyColor )
138                                 cc.Flags |= (int) ChooseColorFlags.CC_ANYCOLOR;
139
140                         if ( ShowHelp )
141                                 cc.Flags |= (int) ChooseColorFlags.CC_SHOWHELP;
142
143                         if ( SolidColorOnly )
144                                 cc.Flags |= (int) ChooseColorFlags.CC_SOLIDCOLOR;
145
146                         cc.rgbResult = Win32.RGB ( Color );
147
148                         cc.lpCustColors = Marshal.AllocHGlobal ( Marshal.SizeOf( customColors[0] ) * customColors.Length );
149                         Marshal.Copy ( customColors, 0, cc.lpCustColors, customColors.Length );
150                         
151                         bool res = false;
152                         try {
153                                 res = Win32_WineLess.ChooseColor ( ref cc );
154
155                                 if ( res ) {
156                                         this.Color = Color.FromArgb ( cc.rgbResult );
157                                         Marshal.Copy ( cc.lpCustColors, customColors, 0, customColors.Length );
158                                 }
159                         }
160                         finally {
161                                 Marshal.FreeHGlobal ( cc.lpCustColors );
162                         }
163
164                         return res;
165                 }
166                 
167                 public override string ToString() 
168                 {
169                         return GetType( ).FullName.ToString ( ) + ", Color: " + Color.ToString ( );
170                 }
171         }
172 }