1 11 package org.eclipse.swt.custom; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.widgets.*; 16 import org.eclipse.swt.graphics.*; 17 18 31 public class SashForm extends Composite { 32 33 public int SASH_WIDTH = 3; 34 35 int sashStyle; 36 Sash[] sashes = new Sash[0]; 37 Color background = null; 42 Color foreground = null; 43 Control[] controls = new Control[0]; 44 Control maxControl = null; 45 Listener sashListener; 46 static final int DRAG_MINIMUM = 20; 47 48 75 public SashForm(Composite parent, int style) { 76 super(parent, checkStyle(style)); 77 super.setLayout(new SashFormLayout()); 78 sashStyle = ((style & SWT.VERTICAL) != 0) ? SWT.HORIZONTAL : SWT.VERTICAL; 79 if ((style & SWT.BORDER) != 0) sashStyle |= SWT.BORDER; 80 if ((style & SWT.SMOOTH) != 0) sashStyle |= SWT.SMOOTH; 81 sashListener = new Listener() { 82 public void handleEvent(Event e) { 83 onDragSash(e); 84 } 85 }; 86 } 87 static int checkStyle (int style) { 88 int mask = SWT.BORDER | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 89 return style & mask; 90 } 91 97 public int getOrientation() { 98 return (sashStyle & SWT.VERTICAL) != 0 ? SWT.HORIZONTAL : SWT.VERTICAL; 100 } 101 public int getStyle() { 102 int style = super.getStyle(); 103 style |= getOrientation() == SWT.VERTICAL ? SWT.VERTICAL : SWT.HORIZONTAL; 104 if ((sashStyle & SWT.SMOOTH) != 0) style |= SWT.SMOOTH; 105 return style; 106 } 107 113 public Control getMaximizedControl(){ 114 return this.maxControl; 116 } 117 131 132 public int[] getWeights() { 133 checkWidget(); 134 Control[] cArray = getControls(false); 135 int[] ratios = new int[cArray.length]; 136 for (int i = 0; i < cArray.length; i++) { 137 Object data = cArray[i].getLayoutData(); 138 if (data != null && data instanceof SashFormData) { 139 ratios[i] = (int)(((SashFormData)data).weight * 1000 >> 16); 140 } else { 141 ratios[i] = 200; 142 } 143 } 144 return ratios; 145 } 146 Control[] getControls(boolean onlyVisible) { 147 Control[] children = getChildren(); 148 Control[] result = new Control[0]; 149 for (int i = 0; i < children.length; i++) { 150 if (children[i] instanceof Sash) continue; 151 if (onlyVisible && !children[i].getVisible()) continue; 152 153 Control[] newResult = new Control[result.length + 1]; 154 System.arraycopy(result, 0, newResult, 0, result.length); 155 newResult[result.length] = children[i]; 156 result = newResult; 157 } 158 return result; 159 } 160 void onDragSash(Event event) { 161 Sash sash = (Sash)event.widget; 162 int sashIndex = -1; 163 for (int i= 0; i < sashes.length; i++) { 164 if (sashes[i] == sash) { 165 sashIndex = i; 166 break; 167 } 168 } 169 if (sashIndex == -1) return; 170 171 Control c1 = controls[sashIndex]; 172 Control c2 = controls[sashIndex + 1]; 173 Rectangle b1 = c1.getBounds(); 174 Rectangle b2 = c2.getBounds(); 175 176 Rectangle sashBounds = sash.getBounds(); 177 Rectangle area = getClientArea(); 178 boolean correction = false; 179 if (getOrientation() == SWT.HORIZONTAL) { 180 correction = b1.width < DRAG_MINIMUM || b2.width < DRAG_MINIMUM; 181 int totalWidth = b2.x + b2.width - b1.x; 182 int shift = event.x - sashBounds.x; 183 b1.width += shift; 184 b2.x += shift; 185 b2.width -= shift; 186 if (b1.width < DRAG_MINIMUM) { 187 b1.width = DRAG_MINIMUM; 188 b2.x = b1.x + b1.width + sashBounds.width; 189 b2.width = totalWidth - b2.x; 190 event.x = b1.x + b1.width; 191 event.doit = false; 192 } 193 if (b2.width < DRAG_MINIMUM) { 194 b1.width = totalWidth - DRAG_MINIMUM - sashBounds.width; 195 b2.x = b1.x + b1.width + sashBounds.width; 196 b2.width = DRAG_MINIMUM; 197 event.x = b1.x + b1.width; 198 event.doit = false; 199 } 200 Object data1 = c1.getLayoutData(); 201 if (data1 == null || !(data1 instanceof SashFormData)) { 202 data1 = new SashFormData(); 203 c1.setLayoutData(data1); 204 } 205 Object data2 = c2.getLayoutData(); 206 if (data2 == null || !(data2 instanceof SashFormData)) { 207 data2 = new SashFormData(); 208 c2.setLayoutData(data2); 209 } 210 ((SashFormData)data1).weight = (((long)b1.width << 16) + area.width - 1) / area.width; 211 ((SashFormData)data2).weight = (((long)b2.width << 16) + area.width - 1) / area.width; 212 } else { 213 correction = b1.height < DRAG_MINIMUM || b2.height < DRAG_MINIMUM; 214 int totalHeight = b2.y + b2.height - b1.y; 215 int shift = event.y - sashBounds.y; 216 b1.height += shift; 217 b2.y += shift; 218 b2.height -= shift; 219 if (b1.height < DRAG_MINIMUM) { 220 b1.height = DRAG_MINIMUM; 221 b2.y = b1.y + b1.height + sashBounds.height; 222 b2.height = totalHeight - b2.y; 223 event.y = b1.y + b1.height; 224 event.doit = false; 225 } 226 if (b2.height < DRAG_MINIMUM) { 227 b1.height = totalHeight - DRAG_MINIMUM - sashBounds.height; 228 b2.y = b1.y + b1.height + sashBounds.height; 229 b2.height = DRAG_MINIMUM; 230 event.y = b1.y + b1.height; 231 event.doit = false; 232 } 233 Object data1 = c1.getLayoutData(); 234 if (data1 == null || !(data1 instanceof SashFormData)) { 235 data1 = new SashFormData(); 236 c1.setLayoutData(data1); 237 } 238 Object data2 = c2.getLayoutData(); 239 if (data2 == null || !(data2 instanceof SashFormData)) { 240 data2 = new SashFormData(); 241 c2.setLayoutData(data2); 242 } 243 ((SashFormData)data1).weight = (((long)b1.height << 16) + area.height - 1) / area.height; 244 ((SashFormData)data2).weight = (((long)b2.height << 16) + area.height - 1) / area.height; 245 } 246 if (correction || (event.doit && event.detail != SWT.DRAG)) { 247 c1.setBounds(b1); 248 sash.setBounds(event.x, event.y, event.width, event.height); 249 c2.setBounds(b2); 250 } 251 } 252 265 public void setOrientation(int orientation) { 266 checkWidget(); 267 if (getOrientation() == orientation) return; 268 if (orientation != SWT.HORIZONTAL && orientation != SWT.VERTICAL) { 269 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 270 } 271 sashStyle &= ~(SWT.HORIZONTAL | SWT.VERTICAL); 272 sashStyle |= orientation == SWT.VERTICAL ? SWT.HORIZONTAL : SWT.VERTICAL; 273 for (int i = 0; i < sashes.length; i++) { 274 sashes[i].dispose(); 275 sashes[i] = new Sash(this, sashStyle); 276 sashes[i].setBackground(background); 277 sashes[i].setForeground(foreground); 278 sashes[i].addListener(SWT.Selection, sashListener); 279 } 280 layout(false); 281 } 282 public void setBackground (Color color) { 283 super.setBackground(color); 284 background = color; 285 for (int i = 0; i < sashes.length; i++) { 286 sashes[i].setBackground(background); 287 } 288 } 289 public void setForeground (Color color) { 290 super.setForeground(color); 291 foreground = color; 292 for (int i = 0; i < sashes.length; i++) { 293 sashes[i].setForeground(foreground); 294 } 295 } 296 311 public void setLayout (Layout layout) { 312 checkWidget(); 313 return; 314 } 315 329 public void setMaximizedControl(Control control){ 330 checkWidget(); 331 if (control == null) { 332 if (maxControl != null) { 333 this.maxControl = null; 334 layout(false); 335 for (int i= 0; i < sashes.length; i++){ 336 sashes[i].setVisible(true); 337 } 338 } 339 return; 340 } 341 342 for (int i= 0; i < sashes.length; i++){ 343 sashes[i].setVisible(false); 344 } 345 maxControl = control; 346 layout(false); 347 } 348 349 364 public void setWeights(int[] weights) { 365 checkWidget(); 366 Control[] cArray = getControls(false); 367 if (weights == null || weights.length != cArray.length) { 368 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 369 } 370 371 int total = 0; 372 for (int i = 0; i < weights.length; i++) { 373 if (weights[i] < 0) { 374 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 375 } 376 total += weights[i]; 377 } 378 if (total == 0) { 379 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 380 } 381 for (int i = 0; i < cArray.length; i++) { 382 Object data = cArray[i].getLayoutData(); 383 if (data == null || !(data instanceof SashFormData)) { 384 data = new SashFormData(); 385 cArray[i].setLayoutData(data); 386 } 387 ((SashFormData)data).weight = (((long)weights[i] << 16) + total - 1) / total; 388 } 389 390 layout(false); 391 } 392 } 393 | Popular Tags |