Mercurial > projects > dwt-mac
comparison dwt/widgets/Composite.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | 642f460a0908 |
children | 8615c76f10e4 |
comparison
equal
deleted
inserted
replaced
44:ca5e494f2bbf | 45:d8635bb48c7c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * | 10 * |
11 * Port to the D programming language: | 11 * Port to the D programming language: |
12 * Jacob Carlborg <jacob.carlborg@gmail.com> | 12 * Jacob Carlborg <doob@me.com> |
13 *******************************************************************************/ | 13 *******************************************************************************/ |
14 module dwt.widgets.Composite; | 14 module dwt.widgets.Composite; |
15 | 15 |
16 | 16 |
17 import dwt.DWT; | 17 import dwt.DWT; |
18 import dwt.DWTException; | 18 import dwt.DWTException; |
19 import dwt.accessibility.ACC; | |
19 import dwt.graphics.Point; | 20 import dwt.graphics.Point; |
20 import dwt.graphics.Rectangle; | 21 import dwt.graphics.Rectangle; |
21 import dwt.internal.cocoa.NSArray; | 22 import dwt.internal.cocoa.NSArray; |
22 import dwt.internal.cocoa.NSEvent; | 23 import dwt.internal.cocoa.NSEvent; |
24 import dwt.internal.cocoa.NSGraphicsContext; | |
23 import dwt.internal.cocoa.NSRect; | 25 import dwt.internal.cocoa.NSRect; |
26 import dwt.internal.cocoa.NSScrollView; | |
24 import dwt.internal.cocoa.NSView; | 27 import dwt.internal.cocoa.NSView; |
25 import dwt.internal.cocoa.OS; | 28 import dwt.internal.cocoa.OS; |
26 import dwt.internal.cocoa.SWTScrollView; | 29 import dwt.internal.cocoa.SWTScrollView; |
27 import dwt.internal.cocoa.SWTView; | 30 import dwt.internal.cocoa.SWTView; |
31 import dwt.internal.cocoa.id; | |
28 | 32 |
29 import dwt.dwthelper.utils; | 33 import dwt.dwthelper.utils; |
30 import dwt.internal.cocoa.NSInteger; | 34 import dwt.internal.cocoa.NSInteger; |
31 import dwt.widgets.Control; | 35 import dwt.widgets.Control; |
32 import dwt.widgets.Decorations; | 36 import dwt.widgets.Decorations; |
65 * who are building controls that are constructed from aggregates | 69 * who are building controls that are constructed from aggregates |
66 * of other controls. | 70 * of other controls. |
67 * </p> | 71 * </p> |
68 * | 72 * |
69 * @see Canvas | 73 * @see Canvas |
74 * @see <a href="http://www.eclipse.org/swt/snippets/#composite">Composite snippets</a> | |
75 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
70 */ | 76 */ |
71 public class Composite : Scrollable { | 77 public class Composite : Scrollable { |
72 Layout layout_; | 78 Layout layout_; |
73 Control[] tabList; | 79 Control[] tabList; |
74 int scrolledVisibleRgn, siblingsVisibleRgn; | |
75 int layoutCount, backgroundMode; | 80 int layoutCount, backgroundMode; |
76 | 81 |
77 this () { | 82 this () { |
78 /* Do nothing */ | 83 /* Do nothing */ |
79 } | 84 } |
112 super (parent, style); | 117 super (parent, style); |
113 } | 118 } |
114 | 119 |
115 Control [] _getChildren () { | 120 Control [] _getChildren () { |
116 NSArray views = contentView().subviews(); | 121 NSArray views = contentView().subviews(); |
117 NSUInteger count = views.count(); | 122 int count = cast(int)/*64*/views.count(); |
118 Control [] children = new Control [count]; | 123 Control [] children = new Control [count]; |
124 if (count is 0) return children; | |
119 int j = 0; | 125 int j = 0; |
120 for (int i=0; i<count; i++){ | 126 for (int i=0; i<count; i++){ |
121 NSInteger tag = (new NSView(views.objectAtIndex(i))).tag(); | 127 Widget widget = display.getWidget (views.objectAtIndex (count - i - 1).id); |
122 if (tag !is 0 && tag !is -1) { | 128 if (widget !is null && widget !is this && widget instanceof Control) { |
123 Object widget = OS.JNIGetObject(tag); | 129 children [j++] = cast(Control) widget; |
124 if (widget !is null && widget !is this) { | |
125 if ( null !is cast(Control)widget ) { | |
126 children [j++] = cast(Control) widget; | |
127 } | |
128 } | |
129 } | 130 } |
130 } | 131 } |
131 if (j is count) return children; | 132 if (j is count) return children; |
132 Control [] newChildren = new Control [j]; | 133 Control [] newChildren = new Control [j]; |
133 System.arraycopy (children, 0, newChildren, 0, j); | 134 System.arraycopy (children, 0, newChildren, 0, j); |
150 } | 151 } |
151 tabList = newList; | 152 tabList = newList; |
152 return tabList; | 153 return tabList; |
153 } | 154 } |
154 | 155 |
155 bool acceptsFirstResponder () { | 156 bool acceptsFirstResponder (int /*long*/ id, int /*long*/ sel) { |
156 if ((state & CANVAS) !is 0) { | 157 if ((state & CANVAS) !is 0) { |
157 return ((style & DWT.NO_FOCUS) is 0); | 158 return ((style & DWT.NO_FOCUS) is 0); |
158 } | 159 } |
159 return super.acceptsFirstResponder (); | 160 return super.acceptsFirstResponder (id, sel); |
160 } | 161 } |
161 | 162 |
163 int accessibilityAttributeNames(int /*long*/ id, int /*long*/ sel) { | |
164 | |
165 if (id is view.id) { | |
166 if (accessible !is null) { | |
167 // If there is an accessible, it may provide its own list of attributes if it's a lightweight control. | |
168 // If not, let Cocoa handle it for this view. | |
169 id returnObject = accessible.internal_accessibilityAttributeNames(ACC.CHILDID_SELF); | |
170 if (returnObject !is null) return returnObject.id; | |
171 } | |
172 } | |
173 | |
174 return super.accessibilityAttributeNames(id, sel); | |
175 } | |
176 | |
177 bool accessibilityIsIgnored(int /*long*/ id, int /*long*/ sel) { | |
178 // If we have an accessible and it represents a valid accessible role, this view is not ignored. | |
179 if (view !is null && id is view.id) { | |
180 if (accessible !is null) { | |
181 id role = accessible.internal_accessibilityAttributeValue(OS.NSAccessibilityRoleAttribute, ACC.CHILDID_SELF); | |
182 if (role !is null) return false; | |
183 } | |
184 } | |
185 | |
186 return super.accessibilityIsIgnored(id, sel); | |
187 } | |
162 | 188 |
163 /** | 189 /** |
164 * Clears any data that has been cached by a Layout for all widgets that | 190 * Clears any data that has been cached by a Layout for all widgets that |
165 * are in the parent hierarchy of the changed control up to and including the | 191 * are in the parent hierarchy of the changed control up to and including the |
166 * receiver. If an ancestor does not have a layout, it is skipped. | 192 * receiver. If an ancestor does not have a layout, it is skipped. |
248 } | 274 } |
249 } | 275 } |
250 return result; | 276 return result; |
251 } | 277 } |
252 | 278 |
253 NSView contentView () { | |
254 return view; | |
255 } | |
256 | |
257 void createHandle () { | 279 void createHandle () { |
258 createHandle (parent.contentView()); | |
259 } | |
260 | |
261 void createHandle (NSView parent) { | |
262 state |= CANVAS; | 280 state |= CANVAS; |
263 NSRect rect = NSRect(); | 281 NSRect rect = NSRect(); |
264 if ((style & (DWT.V_SCROLL | DWT.H_SCROLL)) !is 0 || hasBorder ()) { | 282 if ((style & (DWT.V_SCROLL | DWT.H_SCROLL)) !is 0 || hasBorder ()) { |
265 SWTScrollView scrollWidget = cast(SWTScrollView)(new SWTScrollView()).alloc(); | 283 NSScrollView scrollWidget = cast(NSScrollView)(new SWTScrollView()).alloc(); |
266 scrollWidget.initWithFrame (rect); | 284 scrollWidget.initWithFrame (rect); |
267 scrollWidget.setDrawsBackground(false); | 285 scrollWidget.setDrawsBackground(false); |
268 if ((style & DWT.H_SCROLL) !is 0) scrollWidget.setHasHorizontalScroller(true); | 286 if ((style & DWT.H_SCROLL) !is 0) scrollWidget.setHasHorizontalScroller(true); |
269 if ((style & DWT.V_SCROLL) !is 0) scrollWidget.setHasVerticalScroller(true); | 287 if ((style & DWT.V_SCROLL) !is 0) scrollWidget.setHasVerticalScroller(true); |
270 scrollWidget.setBorderType(hasBorder() ? NSBezelBorder : NSNoBorder); | 288 scrollWidget.setBorderType(hasBorder() ? NSBezelBorder : NSNoBorder); |
271 scrollWidget.setTag(jniRef); | |
272 scrollView = scrollWidget; | 289 scrollView = scrollWidget; |
273 rect.width = rect.height = 100000; | 290 rect.width = rect.height = 100000; |
274 } | 291 } |
275 SWTView widget = cast(SWTView)(new SWTView()).alloc(); | 292 NSView widget = cast(NSView)(new SWTView()).alloc(); |
276 widget.initWithFrame (rect); | 293 widget.initWithFrame (rect); |
277 // widget.setFocusRingType(OS.NSFocusRingTypeExterior); | 294 // widget.setFocusRingType(OS.NSFocusRingTypeExterior); |
278 widget.setTag(jniRef); | |
279 view = widget; | 295 view = widget; |
280 if (scrollView !is null) { | 296 } |
281 // view.setAutoresizingMask (OS.NSViewWidthSizable | OS.NSViewHeightSizable); | 297 |
282 scrollView.setDocumentView(view); | 298 void drawWidget (int /*long*/ id, NSRect rect) { |
283 if (parent !is null) parent.addSubview_(scrollView); | 299 if ((state & CANVAS) !is 0) { |
284 } else { | 300 if ((style & DWT.NO_BACKGROUND) is 0) { |
285 if (parent !is null) parent.addSubview_(view); | 301 NSGraphicsContext context = NSGraphicsContext.currentContext(); |
286 } | 302 fillBackground (view, context, rect); |
303 } | |
304 } | |
305 super.drawWidget (id, rect); | |
287 } | 306 } |
288 | 307 |
289 Composite findDeferredControl () { | 308 Composite findDeferredControl () { |
290 return layoutCount > 0 ? this : parent.findDeferredControl (); | 309 return layoutCount > 0 ? this : parent.findDeferredControl (); |
291 } | 310 } |
490 public bool isLayoutDeferred () { | 509 public bool isLayoutDeferred () { |
491 checkWidget (); | 510 checkWidget (); |
492 return findDeferredControl () !is null; | 511 return findDeferredControl () !is null; |
493 } | 512 } |
494 | 513 |
514 bool isOpaque (int /*long*/ id, int /*long*/ sel) { | |
515 if ((state & CANVAS) !is 0) { | |
516 if (id is view.id) { | |
517 if (region is null && background !is null && background.handle[3] is 1) { | |
518 return true; | |
519 } | |
520 } | |
521 } | |
522 return super.isOpaque (id, sel); | |
523 } | |
524 | |
495 bool isTabGroup () { | 525 bool isTabGroup () { |
496 if ((state & CANVAS) !is 0) return true; | 526 if ((state & CANVAS) !is 0) return true; |
497 return super.isTabGroup (); | 527 return super.isTabGroup (); |
528 } | |
529 | |
530 void keyDown (int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { | |
531 if ((state & CANVAS) !is 0) { | |
532 NSArray array = NSArray.arrayWithObject (new NSEvent (theEvent)); | |
533 view.interpretKeyEvents (array); | |
534 return; | |
535 } | |
536 super.keyDown (id, sel, theEvent); | |
498 } | 537 } |
499 | 538 |
500 /** | 539 /** |
501 * If the receiver has a layout, asks the layout to <em>lay out</em> | 540 * If the receiver has a layout, asks the layout to <em>lay out</em> |
502 * (that is, set the size and location of) the receiver's children. | 541 * (that is, set the size and location of) the receiver's children. |
692 height = Math.max (height, rect.y + rect.height); | 731 height = Math.max (height, rect.y + rect.height); |
693 } | 732 } |
694 return new Point (width, height); | 733 return new Point (width, height); |
695 } | 734 } |
696 | 735 |
736 void pageDown(int /*long*/ id, int /*long*/ sel, int /*long*/ sender) { | |
737 if ((state & CANVAS) !is 0) return; | |
738 super.pageDown(id, sel, sender); | |
739 } | |
740 | |
741 void pageUp(int /*long*/ id, int /*long*/ sel, int /*long*/ sender) { | |
742 if ((state & CANVAS) !is 0) return; | |
743 super.pageUp(id, sel, sender); | |
744 } | |
745 | |
746 void reflectScrolledClipView (int /*long*/ id, int /*long*/ sel, int /*long*/ aClipView) { | |
747 if ((state & CANVAS) !is 0) return; | |
748 super.reflectScrolledClipView (id, sel, aClipView); | |
749 } | |
750 | |
697 void releaseChildren (bool destroy) { | 751 void releaseChildren (bool destroy) { |
698 Control [] children = _getChildren (); | 752 Control [] children = _getChildren (); |
699 for (int i=0; i<children.length; i++) { | 753 for (int i=0; i<children.length; i++) { |
700 Control child = children [i]; | 754 Control child = children [i]; |
701 if (child !is null && !child.isDisposed ()) { | 755 if (child !is null && !child.isDisposed ()) { |
713 | 767 |
714 void removeControl (Control control) { | 768 void removeControl (Control control) { |
715 fixTabList (control); | 769 fixTabList (control); |
716 } | 770 } |
717 | 771 |
772 void resized () { | |
773 super.resized (); | |
774 if (layout !is null) { | |
775 markLayout (false, false); | |
776 updateLayout (false); | |
777 } | |
778 } | |
779 | |
780 void scrollWheel (int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { | |
781 if ((state & CANVAS) !is 0) { | |
782 NSView view = scrollView !is null ? scrollView : this.view; | |
783 if (id is view.id) { | |
784 NSEvent nsEvent = new NSEvent(theEvent); | |
785 float /*double*/ delta = nsEvent.deltaY(); | |
786 if (delta !is 0) { | |
787 if (hooks (DWT.MouseWheel) || filters (DWT.MouseWheel)) { | |
788 if (!sendMouseEvent(nsEvent, DWT.MouseWheel, true)) { | |
789 return; | |
790 } | |
791 } | |
792 } | |
793 bool handled = false; | |
794 ScrollBar bar = verticalBar; | |
795 if (delta !is 0 && bar !is null && bar.getEnabled ()) { | |
796 int selection = Math.max (0, (int)(0.5f + bar.getSelection () - bar.getIncrement () * delta)); | |
797 bar.setSelection (selection); | |
798 Event event = new Event (); | |
799 event.detail = delta > 0 ? DWT.PAGE_UP : DWT.PAGE_DOWN; | |
800 bar.sendEvent (DWT.Selection, event); | |
801 handled = true; | |
802 } | |
803 bar = horizontalBar; | |
804 delta = nsEvent.deltaX (); | |
805 if (delta !is 0 && bar !is null && bar.getEnabled ()) { | |
806 int selection = Math.max (0, (int)(0.5f + bar.getSelection () - bar.getIncrement () * delta)); | |
807 bar.setSelection (selection); | |
808 Event event = new Event (); | |
809 event.detail = delta > 0 ? DWT.PAGE_UP : DWT.PAGE_DOWN; | |
810 bar.sendEvent (DWT.Selection, event); | |
811 handled = true; | |
812 } | |
813 if (!handled) view.superview().scrollWheel(nsEvent); | |
814 return; | |
815 } | |
816 callSuper(id, sel, theEvent); | |
817 return; | |
818 } | |
819 super.scrollWheel (id, sel, theEvent); | |
820 } | |
821 | |
718 /** | 822 /** |
719 * Sets the background drawing mode to the argument which should | 823 * Sets the background drawing mode to the argument which should |
720 * be one of the following constants defined in class <code>DWT</code>: | 824 * be one of the following constants defined in class <code>DWT</code>: |
721 * <code>INHERIT_NONE</code>, <code>INHERIT_DEFAULT</code>, | 825 * <code>INHERIT_NONE</code>, <code>INHERIT_DEFAULT</code>, |
722 * <code>INHERIT_FORCE</code>. | 826 * <code>INHERIT_FORCE</code>. |
737 backgroundMode = mode; | 841 backgroundMode = mode; |
738 Control [] children = _getChildren (); | 842 Control [] children = _getChildren (); |
739 for (int i = 0; i < children.length; i++) { | 843 for (int i = 0; i < children.length; i++) { |
740 children [i].updateBackgroundMode (); | 844 children [i].updateBackgroundMode (); |
741 } | 845 } |
742 } | |
743 | |
744 int setBounds (int x, int y, int width, int height, bool move, bool resize) { | |
745 int result = super.setBounds (x, y, width, height, move, resize); | |
746 if (layout_ !is null && (result & RESIZED) !is 0) { | |
747 markLayout (false, false); | |
748 updateLayout (false); | |
749 } | |
750 return result; | |
751 } | 846 } |
752 | 847 |
753 public bool setFocus () { | 848 public bool setFocus () { |
754 checkWidget (); | 849 checkWidget (); |
755 Control [] children = _getChildren (); | 850 Control [] children = _getChildren (); |