New test.
[mono.git] / mcs / nunit20 / util / FormSettings.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright  2000-2002 Philip A. Craig
6 '
7 ' This software is provided 'as-is', without any express or implied warranty. In no 
8 ' event will the authors be held liable for any damages arising from the use of this 
9 ' software.
10
11 ' Permission is granted to anyone to use this software for any purpose, including 
12 ' commercial applications, and to alter it and redistribute it freely, subject to the 
13 ' following restrictions:
14 '
15 ' 1. The origin of this software must not be misrepresented; you must not claim that 
16 ' you wrote the original software. If you use this software in a product, an 
17 ' acknowledgment (see the following) in the product documentation is required.
18 '
19 ' Portions Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright  2000-2002 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 namespace NUnit.Util
31 {
32         using System;
33         using System.Drawing;
34
35         /// <summary>
36         /// FormSettings holds settings for NUnitForm
37         /// </summary>
38         public class FormSettings : SettingsGroup
39         {
40                 private static readonly string NAME = "Form";
41
42                 private static readonly string MAXIMIZED = "maximized";
43                 private static readonly string WIDTH = "width";
44                 private static readonly string HEIGHT = "height";
45                 private static readonly string XLOCATION = "x-location";
46                 private static readonly string YLOCATION = "y-location";
47                 private static readonly string TREE_SPLITTER_POSITION = "tree-splitter-position";
48                 private static readonly string TAB_SPLITTER_POSITION = "tab-splitter-position";
49
50                 public static readonly int DEFAULT_WIDTH = 756;
51                 public static readonly int MIN_WIDTH = 160;
52
53                 public static readonly int DEFAULT_HEIGHT = 512;
54                 public static readonly int MIN_HEIGHT = 32; 
55                 
56                 public static readonly int DEFAULT_XLOCATION = 10;
57                 
58                 public static readonly int DEFAULT_YLOCATION = 10;
59
60                 public static readonly int TREE_DEFAULT_POSITION = 300;
61                 public static readonly int TREE_MIN_POSITION = 240;
62                 
63                 public static readonly int TAB_DEFAULT_POSITION = 119;
64                 public static readonly int TAB_MIN_POSITION = 100;
65
66                 public FormSettings( ) : base( NAME, UserSettings.GetStorageImpl( NAME ) ) { }
67
68                 public FormSettings( SettingsStorage storage ) : base( NAME, storage ) { }
69
70                 public FormSettings( SettingsGroup parent ) : base( NAME, parent ) { }
71
72                 private Point location = Point.Empty;
73                 private Size size = Size.Empty;
74                 private int treeSplitterPosition = -1;
75                 private int tabSplitterPosition = -1;
76
77                 public bool IsMaximized
78                 {
79                         get
80                         {
81                                 return LoadIntSetting( MAXIMIZED, 0 ) == 1 ? true : false;
82                         }
83
84                         set
85                         {
86                                 SaveIntSetting( MAXIMIZED, value ? 1 : 0 );
87                         }
88                 }
89
90                 public Point Location
91                 {
92                         get 
93                         {
94                                 if ( location == Point.Empty )
95                                 {
96                                         int x = LoadIntSetting( XLOCATION, DEFAULT_XLOCATION );
97                                         int y = LoadIntSetting( YLOCATION, DEFAULT_YLOCATION );
98
99                                         location = new Point(x, y);
100
101                                         if ( !IsValidLocation( location ) )
102                                                 location = new Point( DEFAULT_XLOCATION, DEFAULT_YLOCATION );
103                                 }
104                                 
105                                 return location; 
106                         }
107                         set 
108                         { 
109                                 location = value;
110                                 SaveSetting( XLOCATION, location.X );
111                                 SaveSetting( YLOCATION, location.Y );
112                         }
113                 }
114
115                 private bool IsValidLocation( Point location )
116                 {
117                         Rectangle myArea = new Rectangle( location, this.Size );
118                         bool intersect = false;
119                         foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
120                         {
121                           intersect |= myArea.IntersectsWith(screen.WorkingArea);
122                         }
123                         return intersect;
124                 }
125
126                 public Size Size
127                 {
128                         get 
129                         { 
130                                 if ( size == Size.Empty )
131                                 {
132                                         int width = LoadIntSetting( WIDTH, DEFAULT_WIDTH );
133                                         if ( width < MIN_WIDTH ) width = MIN_WIDTH;
134                                         int height = LoadIntSetting( HEIGHT, DEFAULT_HEIGHT );
135                                         if ( height < MIN_HEIGHT ) height = MIN_HEIGHT;
136
137                                         size = new Size(width, height);
138                                 }
139
140                                 return size;
141                         }
142                         set
143                         { 
144                                 size = value;
145                                 SaveIntSetting( WIDTH, size.Width );
146                                 SaveIntSetting( HEIGHT, size.Height );
147                         }
148                 }
149
150                 public int TreeSplitterPosition
151                 {
152                         get 
153                         {
154                                 if ( treeSplitterPosition == -1 )
155                                 {
156                                         treeSplitterPosition = 
157                                                 LoadIntSetting( TREE_SPLITTER_POSITION, TREE_DEFAULT_POSITION );
158
159                                         if ( treeSplitterPosition < TREE_MIN_POSITION  || treeSplitterPosition > this.Size.Width )
160                                                 treeSplitterPosition = TREE_MIN_POSITION;
161                                 }
162                                 
163                                 return treeSplitterPosition; 
164                         }
165                         set 
166                         { 
167                                 treeSplitterPosition = value;
168                                 SaveSetting( TREE_SPLITTER_POSITION, treeSplitterPosition );
169                         }
170                 }
171
172                 public int TabSplitterPosition
173                 {
174                         get 
175                         {
176                                 if ( tabSplitterPosition == -1 )
177                                 {
178                                         tabSplitterPosition = 
179                                                 LoadIntSetting( TAB_SPLITTER_POSITION, TAB_DEFAULT_POSITION );
180                                         
181                                         if ( tabSplitterPosition < TAB_MIN_POSITION || tabSplitterPosition > this.Size.Height )
182                                                 tabSplitterPosition = TAB_MIN_POSITION;
183                                 }
184                                 
185                                 return tabSplitterPosition; 
186                         }
187                         set 
188                         { 
189                                 tabSplitterPosition = value;
190                                 SaveSetting( TAB_SPLITTER_POSITION, tabSplitterPosition );
191                         }
192                 }
193         }
194 }