Exemplo n.º 1
0
		/// <summary>
		/// Restores the state of a control.
		/// </summary>
		/// <param name="c">The c.</param>
		/// <param name="info">The info.</param>
		public static void RestoreState(
			Control c,
			RestoreInformation info )
		{
			RestoreState( Storage, c, info );
		}
Exemplo n.º 2
0
		/// <summary>
		/// Restores the state of a control.
		/// Works for list views, too (column widths).
		/// </summary>
		/// <param name="storage">The storage.</param>
		/// <param name="c">The c.</param>
		/// <param name="info">The info.</param>
		public static void RestoreState(
			IPersistentPairStorage storage,
			Control c,
			RestoreInformation info )
		{
			string prefix = c.Name;

			object o = null;

			if ( c is Splitter )
			{
				o = RestoreValue( storage, prefix + @".SplitPosition" );
				if ( o != null )
				{
					((Splitter)c).SplitPosition = Convert.ToInt32( o );
				}
			}
			else if ( c is Form )
			{
				Form s = c as Form;
				RestoreState( storage, s, info );
			}
			else if ( c is SplitContainer )
			{
				SplitContainer s = c as SplitContainer;
				RestoreState( storage, s );
			}
			else if ( c is TabControl )
			{
				o = RestoreValue( storage, prefix + @".SelectedIndex" );
				if ( o != null )
				{
					((TabControl)c).SelectedIndex = Convert.ToInt32( o );
				}
			}
			else if ( c is ListView )
			{
				ListView listView = c as ListView;

				o = RestoreValue( storage, prefix + @".Columns.Count" );

				if ( o != null )
				{
					int count = Convert.ToInt32( o );
					for ( int i = 0; i < count; ++i )
					{
						o = RestoreValue( storage, prefix + @".Columns." + (i + 1) + @".Width" );

						if ( o != null && i < listView.Columns.Count )
						{
							listView.Columns[i].Width = Convert.ToInt32( o );
						}
					}
				}

				// --

				listView.SelectedItems.Clear();

				o = RestoreValue( storage, prefix + @".SelectedIndexes.Count" );

				if ( o != null )
				{
					int count = Convert.ToInt32( o );
					for ( int i = 0; i < count; ++i )
					{
						o = RestoreValue( storage, prefix + @".SelectedIndexes." + (i + 1) + @".Index" );

						if ( o != null )
						{
							int index = ConvertHelper.ToInt32( o );

							if ( index < listView.Items.Count )
							{
								listView.Items[index].Checked = true;
							}
						}
					}
				}
			}
			else if ( c is TextBox )
			{
				TextBox t = c as TextBox;

				o = RestoreValue( storage, prefix + @".Text" );
				if ( o != null )
				{
					t.Text = o as string;
				}
			}
			else if ( c is CheckBox )
			{
				CheckBox cb = c as CheckBox;

				o = RestoreValue( storage, prefix + @".CheckState" );
				if ( o != null )
				{
					cb.CheckState = (CheckState)Enum.Parse(
						typeof( CheckState ),
						o as string,
						true );
				}
			}
			else if ( c is RadioButton )
			{
				RadioButton rb = c as RadioButton;

				o = RestoreValue( storage, prefix + @".Checked" );
				if ( o != null )
				{
					rb.Checked = ConvertHelper.ToBoolean( o );
				}
			}
			else if ( c is DateTimePicker )
			{
				DateTimePicker rb = c as DateTimePicker;

				o = RestoreValue( storage, prefix + @".Value" );
				if ( o != null )
				{
					rb.Value = ConvertHelper.ToDateTime( o );
				}
				o = RestoreValue( storage, prefix + @".Checked" );
				if ( o != null )
				{
					rb.Checked = ConvertHelper.ToBoolean( o );
				}
			}
			else if ( c is CheckedListBox )
			{
				CheckedListBox clb = c as CheckedListBox;

				o = RestoreValue( storage, prefix + @".CheckedIndices" );
				if ( o is string )
				{
					o = StringHelper.DeserializeFromString( o as string );

					if ( o is int[] )
					{
						List<int> selectedIndices =
							new List<int>( o as int[] );

						for ( int i = 0; i < clb.Items.Count; i++ )
						{
							if ( selectedIndices.Contains( i ) )
							{
								clb.SetItemChecked( i, true );
							}
							else
							{
								clb.SetItemChecked( i, false );
							}
						}
					}
				}
			}
			else if ( c is ComboBox )
			{
				ComboBox rb = c as ComboBox;

				o = RestoreValue( storage, prefix + @".SelectedIndex" );
				if ( o != null )
				{
					int index = ConvertHelper.ToInt32( o );

					if ( index <= rb.Items.Count - 1 )
					{
						rb.SelectedIndex = index;
					}
				}

				if ( rb.DropDownStyle == ComboBoxStyle.DropDown )
				{
					rb.Text = RestoreValue( storage, prefix + @".Text" ) as string;
				}
			}
			else
			{
				Debug.Assert(
					false,
					string.Format(
					@"Trying to restore the state of an unknown control type: '{0}'.",
					c.GetType() ) );
			}

			// --

			if ( c is ISaveRestoreState )
			{
				ISaveRestoreState srs = c as ISaveRestoreState;

				if ( !protectRecursion.Contains( srs ) )
				{
					protectRecursion.Add( srs );
					try
					{
						srs.OnSaveState( storage, prefix );
					}
					finally
					{
						protectRecursion.Remove( srs );
					}
				}
			}
		}
Exemplo n.º 3
0
		/// <summary>
		/// Restores the state of a control.
		/// </summary>
		/// <param name="storage">The storage.</param>
		/// <param name="c">The c.</param>
		/// <param name="info">The info.</param>
		private static void RestoreState(
			IPersistentPairStorage storage,
			Form c,
			RestoreInformation info )
		{
			// If child, center to parent.
			bool centerParent = !c.TopLevel || c.ParentForm != null;

			bool hasAnythingRestored = false;

			string prefix = c.Name;

			object o = null;
			if ( !centerParent )
			{
				o = RestoreValue( storage, prefix + @".Left" );
				if ( o != null )
				{
					hasAnythingRestored = true;
					c.Left = Convert.ToInt32( o );
				}
				o = RestoreValue( storage, prefix + @".Top" );
				if ( o != null )
				{
					hasAnythingRestored = true;
					c.Top = Convert.ToInt32( o );
				}
			}
			o = RestoreValue( storage, prefix + @".Width" );
			if ( o != null )
			{
				hasAnythingRestored = true;
				c.Width = Convert.ToInt32( o );
			}
			o = RestoreValue( storage, prefix + @".Height" );
			if ( o != null )
			{
				hasAnythingRestored = true;
				c.Height = Convert.ToInt32( o );
			}

			o = RestoreValue( storage, prefix + @".WindowState" );
			if ( o != null )
			{
				hasAnythingRestored = true;
				FormWindowState state = (FormWindowState)Convert.ToInt32( o );

				// Don't allow to start minimized.
				if ( state != c.WindowState &&
					state != FormWindowState.Minimized )
				{
					if ( c.StartPosition != FormStartPosition.Manual )
					{
						c.StartPosition = FormStartPosition.Manual;
					}
					c.WindowState = state;
				}

				if ( state == FormWindowState.Minimized )
				{
					c.WindowState = FormWindowState.Maximized;
				}
			}

			// --

			// 2007-01-28: Zoom if desired.
			if ( !hasAnythingRestored &&
				info != null &&
				info.SuggestZoomPercent > 0 )
			{
				double zoom = (double)info.SuggestZoomPercent / 100.0;

				Size screenSize = SystemInformation.WorkingArea.Size;
				Size windowSize = c.Size;

				int width;
				int height;

				if ( (double)screenSize.Width / (double)windowSize.Width <
					(double)screenSize.Height / (double)windowSize.Height )
				{
					// Scale in X.
					width = (int)((double)screenSize.Width * zoom);
					double fac = (double)width / (double)c.Width;
					height = (int)(fac * (double)c.Height);
				}
				else
				{
					// Scale in Y.
					height = (int)((double)screenSize.Height * zoom);
					double fac = (double)height / (double)c.Height;
					width = (int)(fac * (double)c.Width);
				}

				// Only apply if getting larger.
				if ( width > c.Width && height > c.Height )
				{
					c.Width = width;
					c.Height = height;
				}
			}

			// --

			// If child, center to parent.
			if ( centerParent )
			{
				c.StartPosition = FormStartPosition.CenterParent;
			}
		}