jgridbaglayout.h 17 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
/***************************************************************************
 *   Copyright (C) 2005 by Jeff Ferr                                       *
 *   root@sat                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   aint64_t with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef J_GRIDBAGLAYOUT_H
#define J_GRIDBAGLAYOUT_H

#include "jlayout.h"
#include "jcomponent.h"

#include <iostream>
#include <cstdlib>
#include <map>

namespace jgui {

enum jgridbaglayout_constraints_t {
	// Specifies that this component is the next-to-last component in its 
	// column or row (<code>gridwidth</code>, <code>gridheight</code>), 
	// or that this component be placed next to the previously added 
	// component (<code>gridx</code>, <code>gridy</code>). 
	JGC_RELATIVE = -1,
	// Specifies that this component is the 
	// last component in its column or row. 
	JGC_REMAINDER = 0,
	// Do not resize the component. 
	JGC_NONE = 0,
	// Resize the component both horizontally and vertically. 
	JGC_BOTH = 1,
	// Resize the component horizontally but not vertically. 
	JGC_HORIZONTAL = 2,
	// Resize the component vertically but not horizontally. 
	JGC_VERTICAL = 3,
	// Put the component in the center of its display area.
	JGC_CENTER = 10,
	// Put the component at the top of its display area, centered horizontally. 
	JGC_NORTH = 11,
	// Put the component at the top-right corner of its display area. 
	JGC_NORTHEAST = 12,
	// Put the component on the right side of its display area, centered vertically.
	JGC_EAST = 13,
	// Put the component at the bottom-right corner of its display area. 
	JGC_SOUTHEAST = 14,
	// Put the component at the bottom of its display area, centered horizontally. 
	JGC_SOUTH = 15,
	// Put the component at the bottom-left corner of its display area. 
	JGC_SOUTHWEST = 16,
	// Put the component on the left side of its display area, centered vertically.
	JGC_WEST = 17,
	// Put the component at the top-left corner of its display area. 
	JGC_NORTHWEST = 18,
	// Place the component centered aint64_t the edge of its display area
	// associated with the start of a page for the current
	// <code>ComponentOrienation</code>.  Equal to NORTH for horizontal orientations. 
	JGC_PAGE_START = 19,
	// Place the component centered aint64_t the edge of its display area  
	// associated with the end of a page for the current
	// <code>ComponentOrienation</code>.  Equal to SOUTH for horizontal orientations.
	JGC_PAGE_END = 20,
	// Place the component centered aint64_t the edge of its display area where 
	// lines of text would normally begin for the current 
	// <code>ComponentOrienation</code>.  Equal to WEST for horizontal,
	// left-to-right orientations and EAST for horizontal, right-to-left orientations.
	JGC_LINE_START = 21,
	// Place the component centered aint64_t the edge of its display area where 
	// lines of text would normally end for the current 
	// <code>ComponentOrienation</code>.  Equal to EAST for horizontal,
	// left-to-right orientations and WEST for horizontal, right-to-left orientations.
	JGC_LINE_END = 22,
	// Place the component in the corner of its display area where 
	// the first line of text on a page would normally begin for the current 
	// <code>ComponentOrienation</code>.  Equal to NORTHWEST for horizontal,
	// left-to-right orientations and NORTHEAST for horizontal, right-to-left orientations.
	JGC_FIRST_LINE_START = 23,
	// Place the component in the corner of its display area where 
	// the first line of text on a page would normally end for the current 
	// <code>ComponentOrienation</code>.  Equal to NORTHEAST for horizontal,
	// left-to-right orientations and NORTHWEST for horizontal, right-to-left 
	// orientations.
	JGC_FIRST_LINE_END = 24,
	// Place the component in the corner of its display area where 
	// the last line of text on a page would normally start for the current 
	// <code>ComponentOrienation</code>.  Equal to SOUTHWEST for horizontal,
	// left-to-right orientations and SOUTHEAST for horizontal, right-to-left 
	// orientations.
	JGC_LAST_LINE_START = 25,
	// Place the component in the corner of its display area where 
	// the last line of text on a page would normally end for the current 
	// <code>ComponentOrienation</code>.  Equal to SOUTHEAST for horizontal,
	// left-to-right orientations and SOUTHWEST for horizontal, right-to-left 
	// orientations.
	JGC_LAST_LINE_END = 26,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally centered and
	// vertically aligned aint64_t the baseline of the prevailing row.
	// If the component does not have a baseline it will be vertically centered.
	JGC_BASELINE = 0x100,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally placed aint64_t the
	// leading edge.  For components with a left-to-right orientation,
	// the leading edge is the left edge.  Vertically the component is
	// aligned aint64_t the baseline of the prevailing row.  If the
	// component does not have a baseline it will be vertically centered.
	JGC_BASELINE_LEADING = 0x200,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally placed aint64_t the
	// trailing edge.  For components with a left-to-right
	// orientation, the trailing edge is the right edge.  Vertically
	// the component is aligned aint64_t the baseline of the prevailing
	// row.  If the component does not have a baseline it will be
	// vertically centered.
	JGC_BASELINE_TRAILING = 0x300,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally centered.  Vertically
	// the component is positioned so that its bottom edge touches
	// the baseline of the starting row.  If the starting row does not
	// have a baseline it will be vertically centered.
	JGC_ABOVE_BASELINE = 0x400,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally placed aint64_t the
	// leading edge.  For components with a left-to-right orientation,
	// the leading edge is the left edge.  Vertically the component is
	// positioned so that its bottom edge touches the baseline of the
	// starting row.  If the starting row does not have a baseline it
	// will be vertically centered.
	JGC_ABOVE_BASELINE_LEADING = 0x500,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally placed aint64_t the
	// trailing edge.  For components with a left-to-right
	// orientation, the trailing edge is the right edge.  Vertically
	// the component is positioned so that its bottom edge touches
	// the baseline of the starting row.  If the starting row does not
	// have a baseline it will be vertically centered.
	JGC_ABOVE_BASELINE_TRAILING = 0x600,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally centered.  Vertically
	// the component is positioned so that its top edge touches the
	// baseline of the starting row.  If the starting row does not
	// have a baseline it will be vertically centered.
	JGC_BELOW_BASELINE = 0x700,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally placed aint64_t the
	// leading edge.  For components with a left-to-right orientation,
	// the leading edge is the left edge.  Vertically the component is
	// positioned so that its top edge touches the baseline of the
	// starting row.  If the starting row does not have a baseline it
	// will be vertically centered.
	JGC_BELOW_BASELINE_LEADING = 0x800,
	// Possible value for the <code>anchor</code> field.  Specifies
	// that the component should be horizontally placed aint64_t the
	// trailing edge.  For components with a left-to-right
	// orientation, the trailing edge is the right edge.  Vertically
	// the component is positioned so that its top edge touches the
	// baseline of the starting row.  If the starting row does not
	// have a baseline it will be vertically centered.
	JGC_BELOW_BASELINE_TRAILING = 0x900
};

enum jgridbaglayout_style_t {
	JGS_EMPIRICMULTIPLIER = 2,
  // This field is no int64_ter used to reserve arrays and keeped for backward
  // compatibility. Previously, this was
  // the maximum number of grid positions (both horizontal and 
  // vertical) that could be laid out by the grid bag layout.
  // Current implementation doesn't impose any limits on the size of a grid. 
	JGS_MAXGRIDSIZE	= 512,
  // The smallest grid that can be laid out by the grid bag layout.
	JGS_MINSIZE	= 1,
  // The preferred grid size that can be laid out by the grid bag layout.
	JGS_PREFERREDSIZE	= 2
};

class Container;

/**
 * \brief
 *
 * \author Jeff Ferr
 */
class GridBagConstraints{

	private:

	public:
    jinsets_t insets;
    double weightx,
					 weighty;
		int anchor,
				fill,
				ipadx,
				ipady,
				tempX,
				tempY,
				tempWidth,
				tempHeight,
				minWidth,
				minHeight,
				ascent,
				descent,
				centerPadding,
				centerOffset,
				gridx,
				gridy,
				gridwidth,
				gridheight;
		jcomponent_behavior_t baselineResizeBehavior;

		GridBagConstraints() 
		{
			this->gridx = JGC_RELATIVE;
			this->gridy = JGC_RELATIVE;
			this->gridwidth = 1;
			this->gridheight = 1;

			this->weightx = 0;
			this->weighty = 0;
			this->anchor = JGC_CENTER;
			this->fill = JGC_NONE;

			this->ipadx = 0;
			this->ipady = 0;

			this->insets.left = 0;
			this->insets.right = 0;
			this->insets.top = 0;
			this->insets.bottom = 0;

			this->tempX = 0;
			this->tempY = 0;
			this->tempWidth = 0;
			this->tempHeight = 0;
			this->minWidth = 0;
			this->minHeight = 0;
			this->ascent = 0;
			this->descent = 0;
			this->centerPadding = 0;
			this->centerOffset = 0;
			this->baselineResizeBehavior = JCB_OTHER;
		}

		GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, jinsets_t insets, int ipadx, int ipady) 
		{
			this->gridx = gridx;
			this->gridy = gridy;
			this->gridwidth = gridwidth;
			this->gridheight = gridheight;
			this->fill = fill;
			this->ipadx = ipadx;
			this->ipady = ipady;
			this->insets = insets;
			this->anchor  = anchor;
			this->weightx = weightx;
			this->weighty = weighty;

			this->tempX = 0;
			this->tempY = 0;
			this->tempWidth = 0;
			this->tempHeight = 0;
			this->minWidth = 0;
			this->minHeight = 0;
			this->ascent = 0;
			this->descent = 0;
			this->centerPadding = 0;
			this->centerOffset = 0;
			this->baselineResizeBehavior = JCB_OTHER;
		}

		virtual ~GridBagConstraints()
		{
		}

		virtual bool IsVerticallyResizable() 
		{
			return (fill == JGC_BOTH || fill == JGC_VERTICAL);
		}
};

class GridBagLayoutInfo{

	public:
		int width, 
				height;							// number of  cells: horizontal and vertical
		int startx, 
				starty;							// starting point for layout
		int *minWidth,				// largest minWidth in each column
				*minHeight,											// largest minHeight in each row
				*maxAscent,											// Max ascent (baseline)
				*maxDescent;										// Max descent (height - baseline)
		double *weightX,			// largest weight in each column
					 *weightY;												// largest weight in each row
		bool hasBaseline;								// Whether or not baseline layout has been requested and one of the components has a valid baseline
		// These are only valid if hasBaseline is true and are indexed by row
		short *baselineType;	// The type of baseline for a particular row.  A mix of the BaselineResizeBehavior constants (1 << ordinal())
		int minWidthSize,
				minHeightSize,
				maxAscentSize,
				maxDescentSize,
				weightXSize,
				weightYSize,
				baselineTypeSize;

	public:
		GridBagLayoutInfo(int width, int height) 
		{
			this->width = width;
			this->height = height;
			this->startx = 0;
			this->starty = 0;	
			this->minWidth = NULL;
			this->minHeight = NULL;	
			this->maxAscent = NULL;	
			this->maxDescent = NULL;
			this->weightX = NULL;
			this->weightY = NULL;
			this->hasBaseline = false;
			this->baselineType = NULL;
			this->minWidthSize = 0;
			this->minHeightSize = 0;
			this->maxAscentSize = 0;
			this->maxDescentSize = 0;
			this->weightXSize = 0;
			this->weightYSize = 0;
			this->baselineTypeSize = 0;
		}

		virtual ~GridBagLayoutInfo() 
		{
			if (minWidth != NULL) {
				delete minWidth;
			}

			if (minHeight != NULL) {
				delete minHeight;
			}

			if (maxAscent != NULL) {
				delete maxAscent;
			}

			if (maxDescent != NULL) {
				delete maxDescent;
			}

			if (weightX != NULL) {
				delete weightX;
			}

			if (weightY != NULL) {
				delete weightY;
			}

			if (baselineType != NULL) {
				delete baselineType;
			}
		}

		virtual bool HasConstantDescent(int row) 
		{
			int ordinal = 0; // TODO:: Component.BaselineResizeBehavior.CONSTANT_DESCENT.ordinal();

			return ((baselineType[row] & (1 << ordinal)) != 0);
		}

		virtual bool HasBaseline(int row) 
		{
			return (hasBaseline && baselineType[row] != 0);
		}
};

class GridBagLayout : public Layout{

	private:
		std::map<Component *, GridBagConstraints *> comptable;
		GridBagConstraints *defaultConstraints;
		GridBagLayoutInfo *layoutInfo;
		Component *componentAdjusting;
		int columnWidthsSize,
				rowHeightsSize,
				columnWeightsSize,
				rowWeightsSize;
		bool rightToLeft;

	public:
		/**
		 * \brief
		 *
		 */
		GridBagLayout();
		
		/**
		 * \brief
		 *
		 */
		virtual ~GridBagLayout();

		/**
		 * \brief
		 *
		 */
		virtual void SetConstraints(Component *comp, GridBagConstraints *constraints);
		
		/**
		 * \brief
		 *
		 */
		virtual GridBagConstraints * GetConstraints(Component *comp);
		
		/**
		 * \brief
		 *
		 */
		virtual GridBagConstraints * LookupConstraints(Component *comp);
		
		/**
		 * \brief
		 *
		 */
		virtual void RemoveConstraints(Component *comp);
		
		/**
		 * \brief
		 *
		 */
		virtual jpoint_t GetLayoutOrigin();
		
		/**
		 * \brief
		 *
		 */
		virtual void AddLayoutComponent(Component *comp, GridBagConstraints *constraints);
		
		/**
		 * \brief
		 *
		 */
		virtual void RemoveLayoutComponent(Component *comp);
		
		/**
		 * \brief
		 *
		 */
		virtual GridBagLayoutInfo * GetLayoutInfo(Container *parent, int sizeflag);
		
		/**
		 * \brief
		 *
		 */
		virtual int64_t * PreInitMaximumArraySizes(Container *parent);
		
		/**
		 * \brief
		 *
		 */
		virtual bool CalculateBaseline(Component *c, GridBagConstraints *constraints, jsize_t size);
		
		/**
		 * \brief
		 *
		 */
		virtual void AdjustForGravity(GridBagConstraints *constraints, jregion_t *r);
		
		/**
		 * \brief
		 *
		 */
		virtual void AlignOnBaseline(GridBagConstraints *cons, jregion_t *r, int cellY, int cellHeight);
		
		/**
		 * \brief
		 *
		 */
		virtual void AlignAboveBaseline(GridBagConstraints *cons, jregion_t *r, int cellY, int cellHeight);
		
		/**
		 * \brief
		 *
		 */
		virtual void AlignBelowBaseline(GridBagConstraints *cons, jregion_t *r, int cellY, int cellHeight);
		
		/**
		 * \brief
		 *
		 */
		virtual void CenterVertically(GridBagConstraints *cons, jregion_t *r, int cellHeight);
		
		/**
		 * \brief
		 *
		 */
		virtual jsize_t GetMinSize(Container *parent, GridBagLayoutInfo *info);
		
		/**
		 * \brief
		 *
		 */
		virtual void ArrangeGrid(Container *parent);

		/**
		 * \brief
		 *
		 */
    virtual jsize_t GetMinimumLayoutSize(Container *parent);
		
		/**
		 * \brief
		 *
		 */
    virtual jsize_t GetMaximumLayoutSize(Container *parent);
		
		/**
		 * \brief
		 *
		 */
    virtual jsize_t GetPreferredLayoutSize(Container *parent);

		/**
		 * \brief
		 *
		 */
		virtual void DoLayout(Container *parent);

};

}

#endif