1 /* 2 Copyright (c) 2013 Andrey Penechko 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license the "Software" to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module anchovy.gui.layouts.linearlayout; 30 31 import anchovy.gui; 32 33 public import anchovy.gui.interfaces.ilayout; 34 35 alias VerticalLayout = LinearLayout!true; 36 alias HorizontalLayout = LinearLayout!false; 37 38 //version = debug_linear; 39 40 class LinearLayout(bool vertical) : ILayout 41 { 42 43 override void minimize(Widget root) 44 { 45 Widget[] children = root.getPropertyAs!("children", Widget[]); 46 ivec2 rootSize = root.getPropertyAs!("size", ivec2); 47 int rootSpacing = root.coercePropertyAs!("spacing", int)(0); 48 int rootPadding = root.coercePropertyAs!("padding", int)(0); 49 50 int minRootWidth = int.min; // Will be max child width. Then padding will be added 51 int minRootLength = rootPadding * 2; 52 int childrenLength; 53 uint numExpandableChildren; 54 55 foreach(child; children) 56 { 57 ivec2 childSize = child.getPropertyAs!("prefSize", ivec2); 58 ivec2 childMinSize = child.getPropertyAs!("minSize", ivec2); 59 60 childrenLength += max(*sizeLength(childSize), *sizeLength(childMinSize)); 61 minRootWidth = max(max(*sizeWidth(childSize), minRootWidth), *sizeWidth(childMinSize)); 62 63 if (isExpandableLength(child)) ++numExpandableChildren; 64 } 65 66 minRootLength += (children.length-1) * rootSpacing; 67 minRootLength += childrenLength; 68 minRootWidth += rootPadding * 2; 69 70 version(debug_linear) 71 { 72 writeln("minRootLength ", minRootLength); 73 writeln("minRootWidth ", minRootWidth); 74 writeln("rootSize ", rootSize); 75 } 76 77 *sizeWidth(rootSize) = minRootWidth; 78 *sizeLength(rootSize) = minRootLength; 79 80 version(debug_linear) writeln("rootSize ", rootSize); 81 82 root.setProperty!("prefSize")(rootSize); 83 root.setProperty!("numExpandable")(numExpandableChildren); 84 85 version(debug_linear) writeln("linear minimize end\n"); 86 } 87 88 override void expand(Widget root) 89 { 90 Widget[] children = root.getPropertyAs!("children", Widget[]); 91 92 int rootSpacing = root.coercePropertyAs!("spacing", int)(0); 93 int rootPadding = root.coercePropertyAs!("padding", int)(0); 94 95 uint numExpandableChildren = root.getPropertyAs!("numExpandable", uint); 96 97 ivec2 rootSize = root.getPropertyAs!("size", ivec2); 98 ivec2 rootPrefSize = root.getPropertyAs!("prefSize", ivec2); 99 100 version(debug_linear) 101 { 102 writeln("Root: ", root["name"]); 103 writeln("rootSize ", rootSize); 104 writeln("rootPrefSize ", rootPrefSize); 105 } 106 107 int maxChildWidth = *sizeWidth(rootSize) - rootPadding * 2; 108 109 int extraLength = *sizeLength(rootSize) - *sizeLength(rootPrefSize); 110 int extraPerWidget = extraLength / cast(int)(numExpandableChildren > 0 ? numExpandableChildren : 1); 111 extraPerWidget = extraPerWidget > 0 ? extraPerWidget : 0; 112 113 version(debug_linear) 114 { 115 writeln("numChildren ", children.length); 116 writeln("numExpandableChildren ", numExpandableChildren); 117 writeln("extraPerWidget ", extraPerWidget); 118 writeln("extraLength ", extraLength); 119 writeln("rootPrefSize ", rootPrefSize); 120 } 121 122 int topOffset = rootPadding - rootSpacing; 123 124 125 foreach(child; children) 126 { 127 topOffset += rootSpacing; 128 static if(vertical) 129 child.setProperty!("position")(ivec2(rootPadding, topOffset)); 130 else 131 child.setProperty!("position")(ivec2(topOffset, rootPadding)); 132 133 ivec2 childSize = child.getPropertyAs!("prefSize", ivec2); 134 ivec2 childMinSize = child.getPropertyAs!("minSize", ivec2); 135 childSize = ivec2(max(childSize.x, childMinSize.x), max(childSize.y, childMinSize.y)); 136 137 if (isExpandableLength(child)) 138 { 139 version(debug_linear) writeln(" expandable ", child["type"]); 140 *sizeLength(childSize) += extraPerWidget; 141 } 142 143 if (isExpandableWidth(child)) 144 { 145 version(debug_linear) writeln("expandable width ", child["type"]); 146 *sizeWidth(childSize) = maxChildWidth; 147 } 148 149 topOffset += *sizeLength(childSize); // Offset for next child 150 151 child.setProperty!("size")(childSize); 152 } 153 154 version(debug_linear) writeln("linear expand end\n"); 155 } 156 157 override void onContainerResized(Widget root, ivec2 oldSize, ivec2 newSize) 158 { 159 version(debug_linear) writeln("onContainerResized"); 160 } 161 162 private: 163 164 static bool isExpandableWidth(Widget widget) 165 { 166 static if (vertical) 167 return widget.hasProperty!"hexpand"; 168 else 169 return widget.hasProperty!"vexpand"; 170 } 171 172 static bool isExpandableLength(Widget widget) 173 { 174 static if (vertical) 175 return widget.hasProperty!"vexpand"; 176 else 177 return widget.hasProperty!"hexpand"; 178 } 179 180 static pure int* sizeLength(ref ivec2 vector) 181 { 182 static if (vertical) 183 return &(vector.arrayof[1]); 184 else 185 return &(vector.arrayof[0]); 186 } 187 188 static pure int* sizeWidth(ref ivec2 vector) 189 { 190 static if (vertical) 191 return &(vector.arrayof[0]); 192 else 193 return &(vector.arrayof[1]); 194 } 195 196 static pure int max(int a, int b) 197 { 198 return a > b ? a : b; 199 } 200 }