comparison mde/gui/widget/layout.d @ 142:9dabcc44f515

Tightened rules for alignment sharing of grid layouts (avoids a bug and generally preferable).
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 08 Feb 2009 17:36:57 +0000
parents 6f69a9c111eb
children 66c58e5b0062
comparison
equal deleted inserted replaced
141:6f69a9c111eb 142:9dabcc44f515
164 * Derived constructors may also set initWidths to the array of column widths followed by 164 * Derived constructors may also set initWidths to the array of column widths followed by
165 * row heights used to initially set the row/column dimensions. */ 165 * row heights used to initially set the row/column dimensions. */
166 protected this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data) { 166 protected this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data) {
167 super (mgr, parent, id); 167 super (mgr, parent, id);
168 168
169 // Create cell aligners with appropriate col/row adjustment function 169 // Create cell aligners, potentially sharing
170 if (data.ints[1] & 1) 170 if (data.ints[1] & 1)
171 col = AlignColumns.getInstance (id, cols); 171 col = AlignColumns.getInstance (id, parent, cols, false);
172 else 172 else
173 col = (new AlignColumns (cols)); 173 col = (new AlignColumns (cols,null));
174 if (data.ints[1] & 2) 174 if (data.ints[1] & 2)
175 row = AlignColumns.getInstance (id~"R", rows); // id must be unique to that for cols! 175 row = AlignColumns.getInstance (id, parent, rows, true);
176 else 176 else
177 row = (new AlignColumns (rows)); 177 row = (new AlignColumns (rows,null));
178 178
179 AlignColumns.CallbackStruct cbS; 179 AlignColumns.CallbackStruct cbS;
180 cbS.setWidth = &setColWidth; 180 cbS.setWidth = &setColWidth;
181 cbS.sADD = &setupAlignDimData; 181 cbS.sADD = &setupAlignDimData;
182 cbS.newMW = &colNewMW; 182 cbS.newMW = &colNewMW;
458 458
459 /* All widgets in the grid, by row. Order: [ 0 1 ] 459 /* All widgets in the grid, by row. Order: [ 0 1 ]
460 * [ 2 3 ] */ 460 * [ 2 3 ] */
461 //IChildWidget[] subWidgets; (inherited from AParentWidget) 461 //IChildWidget[] subWidgets; (inherited from AParentWidget)
462 462
463 AlignColumns col, row; // aligners for cols and rows 463 package AlignColumns col, row; // aligners for cols and rows
464 // "rows" allocated in col and row; return value of *.addRows(): 464 // "rows" allocated in col and row; return value of *.addRows():
465 size_t colR = size_t.max, rowR = size_t.max; 465 size_t colR = size_t.max, rowR = size_t.max;
466 } 466 }
467 467
468 468
478 * Cells are not directly interacted with, but minimal widths for each column are passed, and 478 * Cells are not directly interacted with, but minimal widths for each column are passed, and
479 * callback functions are used to adjust the width of any column. 479 * callback functions are used to adjust the width of any column.
480 *************************************************************************************************/ 480 *************************************************************************************************/
481 package class AlignColumns 481 package class AlignColumns
482 { 482 {
483 /** Instance returned will be shared with any other widgets of same widgetID. 483 /** Get an aligner.
484 * 484 *
485 * Also ensures each widget sharing an instance expects the same number of columns. */ 485 * Will be shared with other layouts with the same id which have the same
486 static AlignColumns getInstance (widgetID id, size_t columns) { 486 * parent or if the parents share an aligner.
487 AlignColumns* p = id in instances; 487 *
488 * Also ensures each widget sharing an instance expects the same number of
489 * columns. */
490 static AlignColumns getInstance (widgetID id, IParentWidget parent, size_t columns, bool horiz) {
491 if (horiz)
492 id ~= "H"; // make the ID different for each aligner
493 AlignColumns[]* p = id in instances;
488 if (p) { 494 if (p) {
489 if (p.cols != columns) 495 foreach (ac; *p) {
490 throw new GuiException ("AlignColumns: no. of columns varies between sharing widgets (code error)"); 496 if (parent !is ac.parent) { // If parents are different
491 //logger.trace ("Shared alignment for: "~id); 497 GridWidget parGrid = cast(GridWidget) parent,
492 return *p; 498 acPGrid = cast(GridWidget) ac.parent;
493 } else { 499 // and either is not a GridWidget
494 auto a = new AlignColumns (columns); 500 // or their aligners are different
495 instances[id] = a; 501 if (parGrid is null || acPGrid is null ||
496 return a; 502 horiz ? parGrid.row !is acPGrid.row
497 } 503 : parGrid.col !is acPGrid.col) {
498 } 504 continue; // don't share with it
499 505 }
500 /** Create an instance. After creation, the number of columns can only be changed by calling 506 }
501 * reset. 507 if (ac.cols != columns)
508 continue; // Alignment sharing with ContentListWidgets??
509 return ac;
510 }
511 }
512 auto a = new AlignColumns (columns, parent);
513 if (p) *p ~= a;
514 else instances[id] = [a];
515 return a;
516 }
517
518 /** Create an aligner.
502 * 519 *
503 * After creation, minimal widths should be set for all columns (minWidth) and 520 * After creation, minimal widths should be set for all columns (minWidth) and
504 * setWidths must be called before other functions are used. */ 521 * setWidths must be called before other functions are used.
505 this (size_t columns) { 522 *
523 * Params:
524 * columns = Number of columns. Not expected to change.
525 * parent = The parent of the GridWidget using this aligner. (Used when sharing aligners.) */
526 this (size_t columns, IParentWidget parent) {
506 if (columns < 1) 527 if (columns < 1)
507 throw new GuiException("AlignColumns: created with <1 column (code error)"); 528 throw new GuiException("AlignColumns: created with <1 column (code error)");
508 minWidth.length = columns; 529 minWidth.length = columns;
509 sizable.length = columns; 530 sizable.length = columns;
510 static if (SIZABILITY & SIZABILITY_ENUM.START_TRUE) 531 static if (SIZABILITY & SIZABILITY_ENUM.START_TRUE)
511 sizable[] = true; 532 sizable[] = true;
512 cols = columns; 533 cols = columns;
534 this.parent = parent;
513 } 535 }
514 536
515 /** Like IChildWidget's setup; calls sADD delegates. */ 537 /** Like IChildWidget's setup; calls sADD delegates. */
516 void setup (uint n, uint flags) { 538 void setup (uint n, uint flags) {
517 if (n != setup_n) { 539 if (n != setup_n) {
731 753
732 if (nd != 0) { // needs enlarging or shrinking 754 if (nd != 0) { // needs enlarging or shrinking
733 width[col] = nmw; 755 width[col] = nmw;
734 foreach (cb; cbs) 756 foreach (cb; cbs)
735 cb.setWidth (col, nmw, -1); 757 cb.setWidth (col, nmw, -1);
736 if (lastSizable >= 0) { 758 if (lastSizable >= 0)
737 if (nd != adjustCellSizes (nd, lastSizable, -1)) 759 adjustCellSizes (nd, lastSizable, -1);
738 logger.error ("New minimal size not applied correctly (code error): minWidth: {}, nmw: {}, col: {}, nd: {}", minWidth, nmw, col, nd); 760 }
739 } 761
740 genPositions; 762 debug wdim ow = w;
741 } 763 genPositions;
764 debug if (w < ow)
765 logger.error ("newMinWidth: shrunk (code error); w={}, ow={}, nd={}", w,ow,nd);
742 766
743 //debug logger.trace ("newMW for col: minWidth: {}, nmw: {}, col: {}, nd: {}, mw: {}, w: {}", minWidth, nmw, col, nd, mw, w); 767 //debug logger.trace ("newMW for col: minWidth: {}, nmw: {}, col: {}, nd: {}, mw: {}, w: {}", minWidth, nmw, col, nd, mw, w);
744 foreach (cb; cbs) 768 foreach (cb; cbs)
745 cb.newMW (); 769 cb.newMW ();
746 return true; 770 return true;
860 // Callbacks used to actually adjust a column's width: 884 // Callbacks used to actually adjust a column's width:
861 885
862 uint setup_n = uint.max; // param n of last setup call 886 uint setup_n = uint.max; // param n of last setup call
863 bool setupWidths; // setWidths has been run 887 bool setupWidths; // setWidths has been run
864 888
865 static HashMap!(widgetID,AlignColumns) instances; 889 IParentWidget parent; // Used to determine when to share aligner
890
891 static HashMap!(widgetID,AlignColumns[]) instances;
866 static this () { 892 static this () {
867 instances = new HashMap!(widgetID,AlignColumns); 893 instances = new HashMap!(widgetID,AlignColumns[]);
868 } 894 }
869 895
870 alias IParentWidget.SIZABILITY SIZABILITY; 896 alias IParentWidget.SIZABILITY SIZABILITY;
871 alias IParentWidget.SIZABILITY_ENUM SIZABILITY_ENUM; 897 alias IParentWidget.SIZABILITY_ENUM SIZABILITY_ENUM;
872 898