2004-05-25 Sebastien Pouliot <sebastien@ximian.com>
[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 workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
118                         Rectangle myArea = new Rectangle( location, this.Size );
119                         return ( myArea.IntersectsWith( workingArea ) );
120                 }
121
122                 public Size Size
123                 {
124                         get 
125                         { 
126                                 if ( size == Size.Empty )
127                                 {
128                                         int width = LoadIntSetting( WIDTH, DEFAULT_WIDTH );
129                                         if ( width < MIN_WIDTH ) width = MIN_WIDTH;
130                                         int height = LoadIntSetting( HEIGHT, DEFAULT_HEIGHT );
131                                         if ( height < MIN_HEIGHT ) height = MIN_HEIGHT;
132
133                                         size = new Size(width, height);
134                                 }
135
136                                 return size;
137                         }
138                         set
139                         { 
140                                 size = value;
141                                 SaveIntSetting( WIDTH, size.Width );
142                                 SaveIntSetting( HEIGHT, size.Height );
143                         }
144                 }
145
146                 public int TreeSplitterPosition
147                 {
148                         get 
149                         {
150                                 if ( treeSplitterPosition == -1 )
151                                 {
152                                         treeSplitterPosition = 
153                                                 LoadIntSetting( TREE_SPLITTER_POSITION, TREE_DEFAULT_POSITION );
154
155                                         if ( treeSplitterPosition < TREE_MIN_POSITION  || treeSplitterPosition > this.Size.Width )
156                                                 treeSplitterPosition = TREE_MIN_POSITION;
157                                 }
158                                 
159                                 return treeSplitterPosition; 
160                         }
161                         set 
162                         { 
163                                 treeSplitterPosition = value;
164                                 SaveSetting( TREE_SPLITTER_POSITION, treeSplitterPosition );
165                         }
166                 }
167
168                 public int TabSplitterPosition
169                 {
170                         get 
171                         {
172                                 if ( tabSplitterPosition == -1 )
173                                 {
174                                         tabSplitterPosition = 
175                                                 LoadIntSetting( TAB_SPLITTER_POSITION, TAB_DEFAULT_POSITION );
176                                         
177                                         if ( tabSplitterPosition < TAB_MIN_POSITION || tabSplitterPosition > this.Size.Height )
178                                                 tabSplitterPosition = TAB_MIN_POSITION;
179                                 }
180                                 
181                                 return tabSplitterPosition; 
182                         }
183                         set 
184                         { 
185                                 tabSplitterPosition = value;
186                                 SaveSetting( TAB_SPLITTER_POSITION, tabSplitterPosition );
187                         }
188                 }
189         }
190 }