1 // Written in the D programming language. 2 /++ 3 + Authors: KanzakiKino 4 + Copyright: KanzakiKino 2018 5 + License: LGPL-3.0 6 ++/ 7 module w4d.widget.panel; 8 import w4d.layout.lineup, 9 w4d.widget.base, 10 w4d.exception; 11 import gl3n.linalg; 12 import std.algorithm; 13 14 /// A widget of panel. 15 /// PanelWidget can have children. 16 class PanelWidget : Widget 17 { 18 protected Widget[] _children; 19 /// 20 override @property Widget[] children () 21 { 22 return _children.dup; 23 } 24 25 /// 26 this () 27 { 28 super(); 29 setLayout!VerticalLineupLayout; 30 } 31 32 /// Adds the child. 33 Widget addChild ( Widget child ) 34 { 35 enforce( child, "Append child is null." ); 36 _children ~= child; 37 requestLayout(); 38 return child; 39 } 40 /// Swaps the children. 41 void swapChild ( Widget c1, Widget c2 ) 42 { 43 auto i1 = _children.countUntil!"a is b"(c1); 44 auto i2 = _children.countUntil!"a is b"(c2); 45 enforce( i1 >= 0 && i2 >= 0, "The children are unknown." ); 46 _children.swapAt( i1, i2 ); 47 requestLayout(); 48 } 49 /// Removes all children. 50 void removeAllChildren () 51 { 52 _children.each!( x => _context.forget(x) ); 53 _children = []; 54 requestLayout(); 55 } 56 /// Removes the child. 57 void removeChild ( Widget child ) 58 { 59 _context.forget( child ); 60 _children = _children.remove!( x => x is child ); 61 requestLayout(); 62 } 63 64 protected static template DisableModifyChildren () 65 { 66 import w4d.widget.base: Widget; 67 import w4d.exception: W4dException; 68 69 enum DisableModifyChildren_ErrorMes = "Modifying children is not allowed."; 70 71 override Widget addChild ( Widget ) 72 { 73 throw new W4dException( DisableModifyChildren_ErrorMes ); 74 } 75 override void swapChild ( Widget, Widget ) 76 { 77 throw new W4dException( DisableModifyChildren_ErrorMes ); 78 } 79 override void removeChild ( Widget ) 80 { 81 throw new W4dException( DisableModifyChildren_ErrorMes ); 82 } 83 override void removeAllChildren () 84 { 85 throw new W4dException( DisableModifyChildren_ErrorMes ); 86 } 87 } 88 89 /// 90 override @property bool trackable () { return false; } 91 /// 92 override @property bool focusable () { return false; } 93 }