comparison dynamin/gui/layout.d @ 106:acdbb30fee7e

Port to D2. Most of the effort was dealing with immutable and const.
author Jordan Miner <jminer7@gmail.com>
date Mon, 17 Dec 2012 23:41:50 -0600
parents 5c8c1c2e12c0
children
comparison
equal deleted inserted replaced
105:97997a544ac0 106:acdbb30fee7e
32 H( ~ closeButton ) 32 H( ~ closeButton )
33 ) 33 )
34 */ 34 */
35 35
36 enum LayoutType { 36 enum LayoutType {
37 None, Table, Control, Filler, Spacer 37 Table, Control, Filler, Spacer
38 } 38 }
39 enum Elasticity { 39 enum Elasticity {
40 No, Semi, Yes 40 No, Semi, Yes
41 } 41 }
42 struct LayoutGroup { 42 struct LayoutGroup {
82 Size bestSize() { return cacheActive ? _bestSizeCache : _bestSize; } 82 Size bestSize() { return cacheActive ? _bestSizeCache : _bestSize; }
83 int baseline() { return cacheActive ? _baselineCache : _baseline; } 83 int baseline() { return cacheActive ? _baselineCache : _baseline; }
84 84
85 //{{{ _elasticX() 85 //{{{ _elasticX()
86 private Elasticity _elasticX() { 86 private Elasticity _elasticX() {
87 switch(type) { 87 final switch(type) {
88 case LayoutType.Control: 88 case LayoutType.Control:
89 return control.elasticX ? Elasticity.Yes : Elasticity.No; 89 return control.elasticX ? Elasticity.Yes : Elasticity.No;
90 case LayoutType.Table: 90 case LayoutType.Table:
91 auto e = Elasticity.No; 91 auto e = Elasticity.No;
92 foreach(layout; children) { 92 foreach(layout; children) {
106 } 106 }
107 } 107 }
108 //}}} 108 //}}}
109 //{{{ _elasticY() 109 //{{{ _elasticY()
110 private Elasticity _elasticY() { 110 private Elasticity _elasticY() {
111 switch(type) { 111 final switch(type) {
112 case LayoutType.Control: 112 case LayoutType.Control:
113 return control.elasticY ? Elasticity.Yes : Elasticity.No; 113 return control.elasticY ? Elasticity.Yes : Elasticity.No;
114 case LayoutType.Table: 114 case LayoutType.Table:
115 auto e = Elasticity.No; 115 auto e = Elasticity.No;
116 foreach(layout; children) { 116 foreach(layout; children) {
131 } 131 }
132 //}}} 132 //}}}
133 133
134 //{{{ _bestSize() 134 //{{{ _bestSize()
135 private Size _bestSize() { 135 private Size _bestSize() {
136 switch(type) { 136 final switch(type) {
137 case LayoutType.Control: 137 case LayoutType.Control:
138 return control.bestSize; 138 return control.bestSize;
139 case LayoutType.Table: 139 case LayoutType.Table:
140 scope colsInfo = new ColRowInfo[numColumns]; 140 scope colsInfo = new ColRowInfo[numColumns];
141 scope rowsInfo = new ColRowInfo[numRows]; 141 scope rowsInfo = new ColRowInfo[numRows];
148 } 148 }
149 } 149 }
150 //}}} 150 //}}}
151 //{{{ _baseline() 151 //{{{ _baseline()
152 private int _baseline() { 152 private int _baseline() {
153 switch(type) { 153 final switch(type) {
154 case LayoutType.Control: 154 case LayoutType.Control:
155 return control.baseline; 155 return control.baseline;
156 case LayoutType.Table: 156 case LayoutType.Table:
157 if(numRows != 1) 157 if(numRows != 1)
158 return 0; 158 return 0;
168 } 168 }
169 //}}} 169 //}}}
170 170
171 //{{{ layout() 171 //{{{ layout()
172 void layout(Rect rect) { 172 void layout(Rect rect) {
173 switch(type) { 173 final switch(type) {
174 case LayoutType.Control: 174 case LayoutType.Control:
175 control.location = Point(rect.x, rect.y); 175 control.location = Point(rect.x, rect.y);
176 control.size = Size(rect.width, rect.height); 176 control.size = Size(rect.width, rect.height);
177 return; 177 return;
178 case LayoutType.Table: 178 case LayoutType.Table:
377 * ----- 377 * -----
378 * Then the program will crash when compiled with the -release flag. (I am 378 * Then the program will crash when compiled with the -release flag. (I am
379 * pretty sure it is a DMD bug, but I don't have time to make a testcase 379 * pretty sure it is a DMD bug, but I don't have time to make a testcase
380 * for a bug that does not bother me.) This will work correctly: 380 * for a bug that does not bother me.) This will work correctly:
381 * ----- 381 * -----
382 * const char[] s = createLayout("V( b1 H(b2 b3) )"); 382 * enum char[] s = createLayout("V( b1 H(b2 b3) )");
383 * ----- 383 * -----
384 * Because then the function is interpreted at compile time with CTFE. 384 * Because then the function is interpreted at compile time with CTFE.
385 */ 385 */
386 string createLayout(string layout) { 386 string createLayout(string layout) {
387 string code = "delegate LayoutPanel() {\n"; 387 string code = "delegate LayoutPanel() {\n";
425 assert(0, "unknown character: " ~ str[0]); 425 assert(0, "unknown character: " ~ str[0]);
426 } 426 }
427 } 427 }
428 428
429 // {{{ copied from Phobos 429 // {{{ copied from Phobos
430 char[] ctfeUintToString(uint u) { 430 string ctfeUintToString(uint u) {
431 char[uint.sizeof * 3] buffer = void; 431 char[uint.sizeof * 3] buffer = void;
432 int ndigits; 432 int ndigits;
433 char[] result; 433 string result;
434 char[] digits = "0123456789"; 434 string digits = "0123456789";
435 435
436 ndigits = 0; 436 ndigits = 0;
437 if (u < 10) 437 if (u < 10)
438 // Avoid storage allocation for simple stuff 438 // Avoid storage allocation for simple stuff
439 result = digits[u .. u + 1]; 439 result = digits[u .. u + 1];
444 uint c = (u % 10) + '0'; 444 uint c = (u % 10) + '0';
445 u /= 10; 445 u /= 10;
446 ndigits++; 446 ndigits++;
447 buffer[buffer.length - ndigits] = cast(char)c; 447 buffer[buffer.length - ndigits] = cast(char)c;
448 } 448 }
449 result = new char[ndigits]; 449 result = buffer[$ - ndigits .. $].idup;
450 result[] = buffer[buffer.length - ndigits .. buffer.length];
451 } 450 }
452 return result; 451 return result;
453 } 452 }
454 uint ctfeStringToUint(char[] s) 453 uint ctfeStringToUint(string s)
455 { 454 {
456 int length = s.length; 455 int length = s.length;
457 456
458 if (!length) 457 if (!length)
459 return 0; 458 return 0;
566 565
567 //}}} 566 //}}}
568 567
569 unittest { 568 unittest {
570 class FakeButton : Control { 569 class FakeButton : Control {
571 Size bestSize() { return Size(80, 30); } 570 override Size bestSize() { return Size(80, 30); }
572 bool elasticX() { return false; } 571 override bool elasticX() { return false; }
573 bool elasticY() { return false; } 572 override bool elasticY() { return false; }
574 int baseline() { return 20; } 573 override int baseline() { return 20; }
575 } 574 }
576 class FakeTextBox : Control { 575 class FakeTextBox : Control {
577 Size bestSize() { return Size(100, 20); } 576 override Size bestSize() { return Size(100, 20); }
578 bool elasticX() { return true; } 577 override bool elasticX() { return true; }
579 bool elasticY() { return false; } 578 override bool elasticY() { return false; }
580 int baseline() { return 18; } 579 override int baseline() { return 18; }
581 } 580 }
582 class FakeListBox : Control { 581 class FakeListBox : Control {
583 Size bestSize() { return Size(100, 300); } 582 override Size bestSize() { return Size(100, 300); }
584 bool elasticX() { return false; } 583 override bool elasticX() { return false; }
585 bool elasticY() { return true; } 584 override bool elasticY() { return true; }
586 int baseline() { return 15; } 585 override int baseline() { return 15; }
587 } 586 }
588 class FakeLabel : Control { 587 class FakeLabel : Control {
589 Size bestSize() { return Size(70, 15); } 588 override Size bestSize() { return Size(70, 15); }
590 bool elasticX() { return false; } 589 override bool elasticX() { return false; }
591 bool elasticY() { return false; } 590 override bool elasticY() { return false; }
592 int baseline() { return 13; } 591 override int baseline() { return 13; }
593 } 592 }
594 auto button1 = new FakeButton(); 593 auto button1 = new FakeButton();
595 auto tb1 = new FakeTextBox(); 594 auto tb1 = new FakeTextBox();
596 auto tb2 = new FakeTextBox(); 595 auto tb2 = new FakeTextBox();
597 auto lb1 = new FakeListBox(); 596 auto lb1 = new FakeListBox();