PaneLayoutMgr.cs 20.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright (C) 2006  John Champion
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//=============================================================================

using System;
using System.Drawing;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace ZedGraph
{
	/// <summary>
	/// internal class to store pane layout details for the <see cref="MasterPane" />
	/// </summary>
	/// 
	/// <author> John Champion </author>
	/// <version> $Revision: 3.1 $ $Date: 2006/06/24 20:26:44 $ </version>
	[Serializable]
	public class PaneLayoutMgr : ICloneable, ISerializable
	{
		// =========== PANE LAYOUT STUFF ================

	#region Fields

		/// <summary>
		/// private field that saves the paneLayout format specified when
		/// <see cref="SetLayout(PaneLayout)"/> was called. This value will
		/// default to <see cref="MasterPane.Default.PaneLayout"/> if
		/// <see cref="SetLayout(PaneLayout)"/> (or an overload) was never called.
		/// </summary>
		internal PaneLayout _paneLayout;

		/// <summary>
		/// Private field that stores the boolean value that determines whether
		/// <see cref="_countList"/> is specifying rows or columns.
		/// </summary>
		internal bool _isColumnSpecified;
		/// <summary>
		/// private field that stores the row/column item count that was specified to the
		/// <see cref="SetLayout(bool,int[],float[])"/> method.  This values will be
		/// null if <see cref="SetLayout(bool,int[],float[])"/> was never called.
		/// </summary>
		internal int[] _countList;

		/// <summary>
		/// private field that stores the row/column size proportional values as specified
		/// to the <see cref="SetLayout(bool,int[],float[])"/> method.  This
		/// value will be null if <see cref="SetLayout(bool,int[],float[])"/>
		/// was never called.  
		/// </summary>
		internal float[] _prop;

	#endregion

	#region Constructors

		internal void Init()
		{
			_paneLayout = MasterPane.Default.PaneLayout;
			_countList = null;
			_isColumnSpecified = false;
			_prop = null;
		}

		internal PaneLayoutMgr()
		{
			Init();
		}

		internal PaneLayoutMgr( PaneLayoutMgr rhs )
		{
			_paneLayout = rhs._paneLayout;
			_countList = rhs._countList;
			_isColumnSpecified = rhs._isColumnSpecified;
			_prop = rhs._prop;
		}

		/// <summary>
		/// Implement the <see cref="ICloneable" /> interface in a typesafe manner by just
		/// calling the typed version of <see cref="Clone" />
		/// </summary>
		/// <returns>A deep copy of this object</returns>
		object ICloneable.Clone()
		{
			return this.Clone();
		}

		/// <summary>
		/// Typesafe, deep-copy clone method.
		/// </summary>
		/// <returns>A new, independent copy of this class</returns>
		public PaneLayoutMgr Clone()
		{
			return new PaneLayoutMgr( this );
		}

	#endregion

	#region Serialization

		/// <summary>
		/// Current schema value that defines the version of the serialized file
		/// </summary>
		internal const int schema = 10;

		/// <summary>
		/// Constructor for deserializing objects
		/// </summary>
		/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data
		/// </param>
		/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data
		/// </param>
		public PaneLayoutMgr( SerializationInfo info, StreamingContext context )
		{
			// The schema value is just a file version parameter.  You can use it to make future versions
			// backwards compatible as new member variables are added to classes
			int sch = info.GetInt32( "schema" );

			_paneLayout = (PaneLayout)info.GetValue( "paneLayout", typeof( PaneLayout ) );
			_countList = (int[])info.GetValue( "countList", typeof(int[]) );

			_isColumnSpecified = info.GetBoolean( "isColumnSpecified" );
			_prop = (float[]) info.GetValue( "prop", typeof( float[] ) );
		}

		/// <summary>
		/// Populates a <see cref="SerializationInfo"/> instance with the data needed to serialize the target object
		/// </summary>
		/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data</param>
		/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data</param>
		[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
		public virtual void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			info.AddValue( "schema", schema );

			info.AddValue( "paneLayout", _paneLayout );
			info.AddValue( "countList", _countList );
			info.AddValue( "isColumnSpecified", _isColumnSpecified );
			info.AddValue( "prop", _prop );
		}
	#endregion

	#region Methods

		/// <overloads>The SetLayout() methods setup the desired layout of the
		/// <see cref="GraphPane" /> objects within a <see cref="MasterPane" />.  These functions
		/// do not make any changes, they merely set the parameters so that future calls
		/// to <see cref="PaneBase.ReSize" /> or <see cref="DoLayout(Graphics,MasterPane)" />
		/// will use the desired layout.<br /><br />
		/// The layout options include a set of "canned" layouts provided by the
		/// <see cref="ZedGraph.PaneLayout" /> enumeration, options to just set a specific
		/// number of rows and columns of panes (and all pane sizes are the same), and more
		/// customized options of specifying the number or rows in each column or the number of
		/// columns in each row, along with proportional values that determine the size of each
		/// individual column or row.
		/// </overloads>
		/// <summary>
		/// Automatically set all of the <see cref="GraphPane"/> <see cref="PaneBase.Rect"/>'s in
		/// the list to a pre-defined layout configuration from a <see cref="PaneLayout" />
		/// enumeration.
		/// </summary>
		/// <remarks>This method uses a <see cref="PaneLayout"/> enumeration to describe the type of layout
		/// to be used.  Overloads are available that provide other layout options</remarks>
		/// <param name="paneLayout">A <see cref="PaneLayout"/> enumeration that describes how
		/// the panes should be laid out within the <see cref="PaneBase.Rect"/>.</param>
		/// <seealso cref="SetLayout(int,int)" />
		/// <seealso cref="SetLayout(bool,int[])" />
		/// <seealso cref="SetLayout(bool,int[],float[])" />
		public void SetLayout( PaneLayout paneLayout )
		{
			Init();

			_paneLayout = paneLayout;
		}

		/// <summary>
		/// Automatically set all of the <see cref="GraphPane"/> <see cref="PaneBase.Rect"/>'s in
		/// the list to a reasonable configuration.
		/// </summary>
		/// <remarks>This method explicitly specifies the number of rows and columns to use
		/// in the layout, and all <see cref="GraphPane" /> objects will have the same size.
		/// Overloads are available that provide other layout options</remarks>
		/// <param name="rows">The number of rows of <see cref="GraphPane"/> objects
		/// to include in the layout</param>
		/// <param name="columns">The number of columns of <see cref="GraphPane"/> objects
		/// to include in the layout</param>
		/// <seealso cref="SetLayout(PaneLayout)" />
		/// <seealso cref="SetLayout(bool,int[])" />
		/// <seealso cref="SetLayout(bool,int[],float[])" />
		public void SetLayout( int rows, int columns )
		{
			Init();

			if ( rows < 1 )
				rows = 1;
			if ( columns < 1 )
				columns = 1;

			int[] countList = new int[rows];

			for (int i=0; i<rows; i++ )
				countList[i] = columns;

			SetLayout( true, countList, null );
		}

		/// <summary>
		/// Automatically set all of the <see cref="GraphPane"/> <see cref="PaneBase.Rect"/>'s in
		/// the list to the specified configuration.
		/// </summary>
		/// <remarks>This method specifies the number of rows in each column, or the number of
		/// columns in each row, allowing for irregular layouts.  Overloads are available that
		/// provide other layout options.
		/// </remarks>
		/// <param name="isColumnSpecified">Specifies whether the number of columns in each row, or
		/// the number of rows in each column will be specified.  A value of true indicates the
		/// number of columns in each row are specified in <see paramref="countList"/>.</param>
		/// <param name="countList">An integer array specifying either the number of columns in
		/// each row or the number of rows in each column, depending on the value of
		/// <see paramref="isColumnSpecified"/>.</param>
		/// <seealso cref="SetLayout(PaneLayout)" />
		/// <seealso cref="SetLayout(int,int)" />
		/// <seealso cref="SetLayout(bool,int[],float[])" />
		public void SetLayout( bool isColumnSpecified, int[] countList )
		{
			SetLayout( isColumnSpecified, countList, null );
		}

		/// <summary>
		/// Automatically set all of the <see cref="GraphPane"/> <see cref="PaneBase.Rect"/>'s in
		/// the list to the specified configuration.
		/// </summary>
		/// <remarks>This method specifies the number of panes in each row or column, allowing for
		/// irregular layouts.</remarks>
		/// <remarks>This method specifies the number of rows in each column, or the number of
		/// columns in each row, allowing for irregular layouts.  Additionally, a
		/// <see paramref="proportion" /> parameter is provided that allows varying column or
		/// row sizes.  Overloads for SetLayout() are available that provide other layout options.
		/// </remarks>
		/// <param name="isColumnSpecified">Specifies whether the number of columns in each row, or
		/// the number of rows in each column will be specified.  A value of true indicates the
		/// number of columns in each row are specified in <see paramref="_countList"/>.</param>
		/// <param name="countList">An integer array specifying either the number of columns in
		/// each row or the number of rows in each column, depending on the value of
		/// <see paramref="isColumnSpecified"/>.</param>
		/// <param name="proportion">An array of float values specifying proportional sizes for each
		/// row or column.  Note that these proportions apply to the non-specified dimension -- that is,
		/// if <see paramref="isColumnSpecified"/> is true, then these proportions apply to the row
		/// heights, and if <see paramref="isColumnSpecified"/> is false, then these proportions apply
		/// to the column widths.  The values in this array are arbitrary floats -- the dimension of
		/// any given row or column is that particular proportional value divided by the sum of all
		/// the values.  For example, let <see paramref="isColumnSpecified"/> be true, and
		/// <see paramref="proportion"/> is an array with values of { 1.0, 2.0, 3.0 }.  The sum of
		/// those values is 6.0.  Therefore, the first row is 1/6th of the available height, the
		/// second row is 2/6th's of the available height, and the third row is 3/6th's of the
		/// available height.
		/// </param>
		/// <seealso cref="SetLayout(PaneLayout)" />
		/// <seealso cref="SetLayout(int,int)" />
		/// <seealso cref="SetLayout(bool,int[])" />
		public void SetLayout( bool isColumnSpecified, int[] countList, float[] proportion )
		{
			Init();

			// use defaults if the parameters are invalid
			if ( countList != null && countList.Length > 0 )
			{
				this._prop = new float[countList.Length];

				// Sum up the total proportional factors
				float sumProp = 0.0f;
				for ( int i = 0; i < countList.Length; i++ )
				{
					this._prop[i] = ( proportion == null || proportion.Length <= i || proportion[i] < 1e-10 ) ?
												1.0f : proportion[i];
					sumProp += this._prop[i];
				}

				// Make prop sum to 1.0
				for ( int i=0; i<countList.Length; i++ )
					this._prop[i] /= sumProp;

				_isColumnSpecified = isColumnSpecified;
				_countList = countList;
			}
		}

		/// <summary>
		/// Modify the <see cref="GraphPane" /> <see cref="PaneBase.Rect" /> sizes of each
		/// <see cref="GraphPane" /> such that they fit within the <see cref="MasterPane" />
		/// in a pre-configured layout.
		/// </summary>
		/// <remarks>The <see cref="SetLayout(PaneLayout)" /> method (and overloads) is
		/// used for setting the layout configuration.</remarks>
		/// <param name="g">A <see cref="Graphics" /> instance to be used for font sizing,
		/// etc. in determining the layout configuration.</param>
		/// <param name="master">The <see cref="MasterPane" /> instance which is to
		/// be resized.</param>
		/// <seealso cref="SetLayout(PaneLayout)" />
		/// <seealso cref="SetLayout(int,int)" />
		/// <seealso cref="SetLayout(bool,int[])" />
		/// <seealso cref="SetLayout(bool,int[],float[])" />
		public void DoLayout( Graphics g, MasterPane master )
		{
			if ( this._countList != null )
				DoLayout( g, master, this._isColumnSpecified, this._countList, this._prop );
			else
			{
				int count = master.PaneList.Count;
				if ( count == 0 )
					return;

				int rows,
						cols,
						root = (int)( Math.Sqrt( (double)count ) + 0.9999999 );

				//float[] widthList = new float[5];

				switch ( _paneLayout )
				{
					case PaneLayout.ForceSquare:
						rows = root;
						cols = root;
						DoLayout( g, master, rows, cols );
						break;
					case PaneLayout.SingleColumn:
						rows = count;
						cols = 1;
						DoLayout( g, master, rows, cols );
						break;
					case PaneLayout.SingleRow:
						rows = 1;
						cols = count;
						DoLayout( g, master, rows, cols );
						break;
					default:
					case PaneLayout.SquareColPreferred:
						rows = root;
						cols = root;
						if ( count <= root * ( root - 1 ) )
							rows--;
						DoLayout( g, master, rows, cols );
						break;
					case PaneLayout.SquareRowPreferred:
						rows = root;
						cols = root;
						if ( count <= root * ( root - 1 ) )
							cols--;
						DoLayout( g, master, rows, cols );
						break;
					case PaneLayout.ExplicitCol12:
						DoLayout( g, master, true, new int[2] { 1, 2 }, null );
						break;
					case PaneLayout.ExplicitCol21:
						DoLayout( g, master, true, new int[2] { 2, 1 }, null );
						break;
					case PaneLayout.ExplicitCol23:
						DoLayout( g, master, true, new int[2] { 2, 3 }, null );
						break;
					case PaneLayout.ExplicitCol32:
						DoLayout( g, master, true, new int[2] { 3, 2 }, null );
						break;
					case PaneLayout.ExplicitRow12:
						DoLayout( g, master, false, new int[2] { 1, 2 }, null );
						break;
					case PaneLayout.ExplicitRow21:
						DoLayout( g, master, false, new int[2] { 2, 1 }, null );
						break;
					case PaneLayout.ExplicitRow23:
						DoLayout( g, master, false, new int[2] { 2, 3 }, null );
						break;
					case PaneLayout.ExplicitRow32:
						DoLayout( g, master, false, new int[2] { 3, 2 }, null );
						break;
				}
			}
		}

		/// <summary>
		/// Internal method that applies a previously set layout with a specific
		/// row and column count.  This method is only called by
		/// <see cref="DoLayout(Graphics,MasterPane)" />.
		/// </summary>
		internal void DoLayout( Graphics g, MasterPane master, int rows, int columns )
		{
			if ( rows < 1 )
				rows = 1;
			if ( columns < 1 )
				columns = 1;

			int[] countList = new int[rows];

			for (int i=0; i<rows; i++ )
				countList[i] = columns;

			DoLayout( g, master, true, countList, null );
		}

		/// <summary>
		/// Internal method that applies a previously set layout with a rows per column or
		/// columns per row configuration.  This method is only called by
		/// <see cref="DoLayout(Graphics,MasterPane)" />.
		/// </summary>
		internal void DoLayout( Graphics g, MasterPane master, bool isColumnSpecified, int[] countList,
					float[] proportion )
		{

			// calculate scaleFactor on "normal" pane size (BaseDimension)
			float scaleFactor = master.CalcScaleFactor();

			// innerRect is the area for the GraphPane's
			RectangleF innerRect = master.CalcClientRect( g, scaleFactor );
			master.Legend.CalcRect( g, master, scaleFactor, ref innerRect );

			// scaled InnerGap is the area between the GraphPane.Rect's
			float scaledInnerGap = (float)( master._innerPaneGap * scaleFactor );

			int iPane = 0;

			if ( isColumnSpecified )
			{
				int rows = countList.Length;

				float y = 0.0f;

				for ( int rowNum = 0; rowNum < rows; rowNum++ )
				{
					float propFactor = _prop == null ? 1.0f / rows : _prop[rowNum];

					float height = ( innerRect.Height - (float)( rows - 1 ) * scaledInnerGap ) *
									propFactor;

					int columns = countList[rowNum];
					if ( columns <= 0 )
						columns = 1;
					float width = ( innerRect.Width - (float)( columns - 1 ) * scaledInnerGap ) /
									(float)columns;

					if ( iPane >= master._paneList.Count )
						return;

					for ( int colNum = 0; colNum < columns; colNum++ )
					{
						master[iPane].Rect = new RectangleF(
											innerRect.X + colNum * ( width + scaledInnerGap ),
											innerRect.Y + y,
											width,
											height );
						iPane++;
					}

					y += height + scaledInnerGap;
				}
			}
			else
			{
				int columns = countList.Length;

				float x = 0.0f;

				for ( int colNum = 0; colNum < columns; colNum++ )
				{
					float propFactor = _prop == null ? 1.0f / columns : _prop[colNum];

					float width = ( innerRect.Width - (float)( columns - 1 ) * scaledInnerGap ) *
									propFactor;

					int rows = countList[colNum];
					if ( rows <= 0 )
						rows = 1;
					float height = ( innerRect.Height - (float)( rows - 1 ) * scaledInnerGap ) / (float)rows;

					for ( int rowNum = 0; rowNum < rows; rowNum++ )
					{
						if ( iPane >= master._paneList.Count )
							return;

						master[iPane].Rect = new RectangleF(
											innerRect.X + x,
											innerRect.Y + rowNum * ( height + scaledInnerGap ),
											width,
											height );
						iPane++;
					}

					x += width + scaledInnerGap;
				}
			}
		}

		/*
		/// <summary>
		/// Automatically set all of the <see cref="GraphPane"/> <see cref="PaneBase.Rect"/>'s in
		/// the list to a reasonable configuration.
		/// </summary>
		/// <remarks>This method explicitly specifies the number of rows and columns to use in the layout.
		/// A more automatic overload, using a <see cref="PaneLayout"/> enumeration, is available.</remarks>
		/// <param name="g">
		/// A graphic device object to be drawn into.  This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// </param>
		/// <param name="rows">The number of rows of <see cref="GraphPane"/> objects
		/// to include in the layout</param>
		/// <param name="columns">The number of columns of <see cref="GraphPane"/> objects
		/// to include in the layout</param>
		public void DoPaneLayout( Graphics g, int rows, int columns )
		{
			// save the layout settings for future reference
			this._countList = null;
			this._rows = rows;
			this._columns = columns;

			// calculate scaleFactor on "normal" pane size (BaseDimension)
			float scaleFactor = this.CalcScaleFactor();

			// innerRect is the area for the GraphPane's
			RectangleF innerRect = CalcClientRect( g, scaleFactor );
			this._legend.CalcRect( g, this, scaleFactor, ref innerRect );

			// scaled InnerGap is the area between the GraphPane.Rect's
			float scaledInnerGap = (float)( this._innerPaneGap * scaleFactor );

			float width = ( innerRect.Width - (float)( columns - 1 ) * scaledInnerGap ) / (float)columns;
			float height = ( innerRect.Height - (float)( rows - 1 ) * scaledInnerGap ) / (float)rows;

			int i = 0;
			foreach ( GraphPane pane in this._paneList )
			{
				float rowNum = (float)( i / columns );
				float colNum = (float)( i % columns );

				pane.Rect = new RectangleF(
									innerRect.X + colNum * ( width + scaledInnerGap ),
									innerRect.Y + rowNum * ( height + scaledInnerGap ),
									width,
									height );

				i++;
			}
		}
		*/
	#endregion

	}
}