comparison mde/gui/widget/layout.d @ 140:c94ec5594449

Fixed a bug with changing the minimal size and optimised it slightly.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 08 Feb 2009 15:20:11 +0000
parents 29a524e7c858
children 6f69a9c111eb
comparison
equal deleted inserted replaced
139:29a524e7c858 140:c94ec5594449
260 260
261 // Unlike for most widgets, these actually resize self and sub-widgets, since the parent 261 // Unlike for most widgets, these actually resize self and sub-widgets, since the parent
262 // simply calling setWidth/setHeight wouldn't work. 262 // simply calling setWidth/setHeight wouldn't work.
263 override void minWChange (IChildWidget widg, wdim nmw) { 263 override void minWChange (IChildWidget widg, wdim nmw) {
264 size_t i = getWidgetIndex(widg); 264 size_t i = getWidgetIndex(widg);
265 col.newMinWidth (i%cols, i/cols + colR, nmw); 265 if (!col.newMinWidth (i%cols, i/cols + colR, nmw)) {
266 // callbacks to all sharing layouts do the rest 266 // don't propegate call to parent; set position as required
267 widg.setPosition (x + col.pos[i % cols], y + row.pos[i / cols]);
268 mgr.requestRedraw;
269 } // else callbacks to all sharing layouts do the rest
267 } 270 }
268 override void minHChange (IChildWidget widg, wdim nmh) { 271 override void minHChange (IChildWidget widg, wdim nmh) {
269 size_t i = getWidgetIndex(widg); 272 size_t i = getWidgetIndex(widg);
270 row.newMinWidth (i/cols, i%cols + rowR, nmh); 273 if (!row.newMinWidth (i/cols, i%cols + rowR, nmh)) {
274 widg.setPosition (x + col.pos[i % cols], y + row.pos[i / cols]);
275 mgr.requestRedraw;
276 }
271 } 277 }
272 //END Size & position 278 //END Size & position
273 279
274 280
275 // Find the relevant widget. 281 // Find the relevant widget.
406 for (size_t i = 0; i < cols; ++i) { 412 for (size_t i = 0; i < cols; ++i) {
407 subWidgets[i + cols*j].setHeight (h, dir); 413 subWidgets[i + cols*j].setHeight (h, dir);
408 } 414 }
409 } 415 }
410 416
411 void colNewMW (bool mwChange) { 417 void colNewMW () {
412 if (mwChange) { 418 w = col.w;
413 w = col.w; 419 parent.minWChange (this, col.mw);
414 parent.minWChange (this, col.mw); 420 }
415 } else { // don't propegate call to parent 421 void rowNewMW () {
416 setPosition (x,y); 422 h = row.w;
417 mgr.requestRedraw; 423 parent.minHChange (this, row.mw);
418 }
419 }
420 void rowNewMW (bool mwChange) {
421 if (mwChange) {
422 h = row.w;
423 parent.minHChange (this, row.mw);
424 } else { // don't propegate call to parent
425 setPosition (x,y);
426 mgr.requestRedraw;
427 }
428 } 424 }
429 425
430 426
431 //BEGIN Col/row resizing callback 427 //BEGIN Col/row resizing callback
432 override void resizeCallback (wdim cx, wdim cy) { 428 override void resizeCallback (wdim cx, wdim cy) {
698 genPositions; 694 genPositions;
699 } 695 }
700 696
701 /** Called when one of the cells in column col now has minimal width nmw. 697 /** Called when one of the cells in column col now has minimal width nmw.
702 * 698 *
703 * Enlarges column minimal width if necessary; tries to keep total width the same. */ 699 * Enlarges column minimal width if necessary; tries to keep total width
704 void newMinWidth (size_t col, size_t row, wdim nmw) { 700 * the same.
701 *
702 * Returns: true if min-width changes (callbacks do necessary work),
703 * false if no change to mw (position should still be reset). */
704 bool newMinWidth (size_t col, size_t row, wdim nmw) {
705 minCellWidths[col + row*cols] = nmw; 705 minCellWidths[col + row*cols] = nmw;
706 wdim nd = 0; // negative diff to keep overall size constant if possible 706 wdim nd = 0; // negative diff to keep overall size constant if possible
707 wdim omw = minWidth[col]; // to check if mw actually changes
708 if (minWidth[col] < nmw) { // increase minimal 707 if (minWidth[col] < nmw) { // increase minimal
709 minWidth[col] = nmw; 708 minWidth[col] = nmw;
710 nd = width[col] - nmw; // negative diff 709 nd = width[col] - nmw; // negative diff
711 if (nd > 0) 710 if (nd > 0)
712 nd = 0; // don't decrease our width! 711 nd = 0; // Don't decrease if already larger (mustn't shrink self)
713 } else if (minWidth[col] > nmw) { // potentially decrease minimal 712 } else if (minWidth[col] > nmw) { // potentially decrease minimal
714 nmw = 0; // set nmw to max of all cell min widths 713 // set nmw to max of all cell min widths in col:
715 for (size_t r = 0; r < rows; ++r) { 714 for (size_t r = 0; r < rows; ++r) {
716 wdim mcw = minCellWidths[col+r*cols]; 715 wdim mcw = minCellWidths[col+r*cols];
717 if (nmw < mcw) 716 if (nmw < mcw)
718 nmw = mcw; 717 nmw = mcw;
719 } 718 }
719 if (minWidth[col] == nmw) // no change
720 return false;
720 minWidth[col] = nmw; 721 minWidth[col] = nmw;
721 if (!sizable[col] && lastSizable >= 0) 722 if (!sizable[col] && lastSizable >= 0)
722 nd = width[col] - nmw; // Not resizable but another column is 723 nd = width[col] - nmw; // Not resizable but another column is
724 // Else leave larger; mustn't shrink ourself
723 } else 725 } else
724 return false; 726 return false;
725 727
726 mw = spacing * cast(wdim)(cols - 1); 728 mw = spacing * cast(wdim)(cols - 1);
727 foreach (imw; minWidth) 729 foreach (imw; minWidth)
735 if (nd != adjustCellSizes (nd, lastSizable, -1)) 737 if (nd != adjustCellSizes (nd, lastSizable, -1))
736 logger.error ("New minimal size not applied correctly (code error): minWidth: {}, nmw: {}, col: {}, nd: {}", minWidth, nmw, col, nd); 738 logger.error ("New minimal size not applied correctly (code error): minWidth: {}, nmw: {}, col: {}, nd: {}", minWidth, nmw, col, nd);
737 } 739 }
738 genPositions; 740 genPositions;
739 } 741 }
740 742
741 bool mwChange = nmw != omw; // size only changes if true, presuming old size is valid 743 debug logger.trace ("newMW for col: minWidth: {}, nmw: {}, col: {}, nd: {}, mw: {}, w: {}", minWidth, nmw, col, nd, mw, w);
742 foreach (cb; cbs) 744 foreach (cb; cbs)
743 cb.newMW (mwChange); 745 cb.newMW ();
746 return true;
744 } 747 }
745 748
746 /* Generate position infomation for each column and set w. */ 749 /* Generate position infomation for each column and set w. */
747 private void genPositions () { 750 private void genPositions () {
748 pos.length = cols; 751 pos.length = cols;
836 wdim w,mw; // current & minimal widths 839 wdim w,mw; // current & minimal widths
837 840
838 package struct CallbackStruct { 841 package struct CallbackStruct {
839 void delegate (size_t,wdim,int) setWidth; // set width of a column, with resize direction 842 void delegate (size_t,wdim,int) setWidth; // set width of a column, with resize direction
840 void delegate (uint,uint) sADD; // setupAlignDimData dlgs 843 void delegate (uint,uint) sADD; // setupAlignDimData dlgs
841 void delegate (bool) newMW; // propegate or finalize minimal width change 844 void delegate () newMW; // propegate or finalize minimal width change
842 } 845 }
843 CallbackStruct cbs[]; 846 CallbackStruct cbs[];
844 847
845 protected: 848 protected:
846 /* Minimal width for each column. 849 /* Minimal width for each column.