1 11 package org.eclipse.swt.layout; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.widgets.*; 16 17 43 public final class FillLayout extends Layout { 44 55 public int type = SWT.HORIZONTAL; 56 57 65 public int marginWidth = 0; 66 67 75 public int marginHeight = 0; 76 77 85 public int spacing = 0; 86 87 90 public FillLayout () { 91 } 92 93 100 public FillLayout (int type) { 101 this.type = type; 102 } 103 104 protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache) { 105 Control [] children = composite.getChildren (); 106 int count = children.length; 107 int maxWidth = 0, maxHeight = 0; 108 for (int i=0; i<count; i++) { 109 Control child = children [i]; 110 int w = wHint, h = hHint; 111 if (count > 0) { 112 if (type == SWT.HORIZONTAL && wHint != SWT.DEFAULT) { 113 w = Math.max (0, (wHint - (count - 1) * spacing) / count); 114 } 115 if (type == SWT.VERTICAL && hHint != SWT.DEFAULT) { 116 h = Math.max (0, (hHint - (count - 1) * spacing) / count); 117 } 118 } 119 Point size = computeChildSize (child, w, h, flushCache); 120 maxWidth = Math.max (maxWidth, size.x); 121 maxHeight = Math.max (maxHeight, size.y); 122 } 123 int width = 0, height = 0; 124 if (type == SWT.HORIZONTAL) { 125 width = count * maxWidth; 126 if (count != 0) width += (count - 1) * spacing; 127 height = maxHeight; 128 } else { 129 width = maxWidth; 130 height = count * maxHeight; 131 if (count != 0) height += (count - 1) * spacing; 132 } 133 width += marginWidth * 2; 134 height += marginHeight * 2; 135 if (wHint != SWT.DEFAULT) width = wHint; 136 if (hHint != SWT.DEFAULT) height = hHint; 137 return new Point (width, height); 138 } 139 140 Point computeChildSize (Control control, int wHint, int hHint, boolean flushCache) { 141 FillData data = (FillData)control.getLayoutData (); 142 if (data == null) { 143 data = new FillData (); 144 control.setLayoutData (data); 145 } 146 Point size = null; 147 if (wHint == SWT.DEFAULT && hHint == SWT.DEFAULT) { 148 size = data.computeSize (control, wHint, hHint, flushCache); 149 } else { 150 int trimX, trimY; 152 if (control instanceof Scrollable) { 153 Rectangle rect = ((Scrollable) control).computeTrim (0, 0, 0, 0); 154 trimX = rect.width; 155 trimY = rect.height; 156 } else { 157 trimX = trimY = control.getBorderWidth () * 2; 158 } 159 int w = wHint == SWT.DEFAULT ? wHint : Math.max (0, wHint - trimX); 160 int h = hHint == SWT.DEFAULT ? hHint : Math.max (0, hHint - trimY); 161 size = data.computeSize (control, w, h, flushCache); 162 } 163 return size; 164 } 165 166 protected boolean flushCache (Control control) { 167 Object data = control.getLayoutData(); 168 if (data != null) ((FillData)data).flushCache(); 169 return true; 170 } 171 172 String getName () { 173 String string = getClass ().getName (); 174 int index = string.lastIndexOf ('.'); 175 if (index == -1) return string; 176 return string.substring (index + 1, string.length ()); 177 } 178 179 protected void layout (Composite composite, boolean flushCache) { 180 Rectangle rect = composite.getClientArea (); 181 Control [] children = composite.getChildren (); 182 int count = children.length; 183 if (count == 0) return; 184 int width = rect.width - marginWidth * 2; 185 int height = rect.height - marginHeight * 2; 186 if (type == SWT.HORIZONTAL) { 187 width -= (count - 1) * spacing; 188 int x = rect.x + marginWidth, extra = width % count; 189 int y = rect.y + marginHeight, cellWidth = width / count; 190 for (int i=0; i<count; i++) { 191 Control child = children [i]; 192 int childWidth = cellWidth; 193 if (i == 0) { 194 childWidth += extra / 2; 195 } else { 196 if (i == count - 1) childWidth += (extra + 1) / 2; 197 } 198 child.setBounds (x, y, childWidth, height); 199 x += childWidth + spacing; 200 } 201 } else { 202 height -= (count - 1) * spacing; 203 int x = rect.x + marginWidth, cellHeight = height / count; 204 int y = rect.y + marginHeight, extra = height % count; 205 for (int i=0; i<count; i++) { 206 Control child = children [i]; 207 int childHeight = cellHeight; 208 if (i == 0) { 209 childHeight += extra / 2; 210 } else { 211 if (i == count - 1) childHeight += (extra + 1) / 2; 212 } 213 child.setBounds (x, y, width, childHeight); 214 y += childHeight + spacing; 215 } 216 } 217 } 218 219 225 public String toString () { 226 String string = getName ()+" {"; 227 string += "type="+((type == SWT.VERTICAL) ? "SWT.VERTICAL" : "SWT.HORIZONTAL")+" "; 228 if (marginWidth != 0) string += "marginWidth="+marginWidth+" "; 229 if (marginHeight != 0) string += "marginHeight="+marginHeight+" "; 230 if (spacing != 0) string += "spacing="+spacing+" "; 231 string = string.trim(); 232 string += "}"; 233 return string; 234 } 235 } 236 | Popular Tags |