|
Lines 47-54
Link Here
|
| 47 |
public class ToolBar extends Composite { |
47 |
public class ToolBar extends Composite { |
| 48 |
int itemCount; |
48 |
int itemCount; |
| 49 |
ToolItem [] items; |
49 |
ToolItem [] items; |
|
|
50 |
NSToolbar nsToolbar; |
| 50 |
NSArray accessibilityAttributes = null; |
51 |
NSArray accessibilityAttributes = null; |
| 51 |
ToolItem lastFocus; |
52 |
ToolItem lastFocus; |
|
|
53 |
static int NEXT_ID; |
| 52 |
|
54 |
|
| 53 |
/** |
55 |
/** |
| 54 |
* Constructs a new instance of this class given its parent |
56 |
* Constructs a new instance of this class given its parent |
|
Lines 84-90
Link Here
|
| 84 |
* @see Widget#getStyle() |
86 |
* @see Widget#getStyle() |
| 85 |
*/ |
87 |
*/ |
| 86 |
public ToolBar (Composite parent, int style) { |
88 |
public ToolBar (Composite parent, int style) { |
| 87 |
super (parent, checkStyle (style)); |
89 |
this(parent, style, false); |
|
|
90 |
} |
| 91 |
|
| 92 |
ToolBar(Composite parent, int style, boolean internal) { |
| 93 |
super (parent, checkStyle (parent, style, internal)); |
| 88 |
|
94 |
|
| 89 |
/* |
95 |
/* |
| 90 |
* Ensure that either of HORIZONTAL or VERTICAL is set. |
96 |
* Ensure that either of HORIZONTAL or VERTICAL is set. |
|
Lines 141-147
Link Here
|
| 141 |
return super.accessibilityIsIgnored(id, sel); |
147 |
return super.accessibilityIsIgnored(id, sel); |
| 142 |
} |
148 |
} |
| 143 |
|
149 |
|
| 144 |
static int checkStyle (int style) { |
150 |
static int checkStyle (Composite parent, int style, boolean internal) { |
| 145 |
/* |
151 |
/* |
| 146 |
* Even though it is legal to create this widget |
152 |
* Even though it is legal to create this widget |
| 147 |
* with scroll bars, they serve no useful purpose |
153 |
* with scroll bars, they serve no useful purpose |
|
Lines 149-155
Link Here
|
| 149 |
* widget's client area. The fix is to clear |
155 |
* widget's client area. The fix is to clear |
| 150 |
* the SWT style. |
156 |
* the SWT style. |
| 151 |
*/ |
157 |
*/ |
| 152 |
return style & ~(SWT.H_SCROLL | SWT.V_SCROLL); |
158 |
int newStyle = style & ~(SWT.H_SCROLL | SWT.V_SCROLL); |
|
|
159 |
|
| 160 |
/* |
| 161 |
* Only internal clients can create an NSToolbar-based ToolBar. |
| 162 |
*/ |
| 163 |
if (!internal && (newStyle & SWT.SMOOTH) != 0) { |
| 164 |
newStyle &= ~SWT.SMOOTH; |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
* A unified toolbar can only be parented to a Shell, and |
| 169 |
* there can only be one unified toolbar per shell. If neither of these |
| 170 |
* conditions hold, turn off the SMOOTH flag. |
| 171 |
*/ |
| 172 |
if ((style & SWT.SMOOTH) != 0) { |
| 173 |
if (parent instanceof Shell) { |
| 174 |
Shell s = (Shell)parent; |
| 175 |
if (s.window.toolbar() != null) newStyle &= ~SWT.SMOOTH; |
| 176 |
} else { |
| 177 |
newStyle &= ~SWT.SMOOTH; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Unified toolbar only supports a horizontal layout, and doesn't wrap. |
| 183 |
*/ |
| 184 |
if ((newStyle & SWT.SMOOTH) != 0) { |
| 185 |
newStyle &= ~(SWT.VERTICAL | SWT.WRAP); |
| 186 |
newStyle |= SWT.HORIZONTAL; |
| 187 |
} |
| 188 |
|
| 189 |
return newStyle; |
| 153 |
} |
190 |
} |
| 154 |
|
191 |
|
| 155 |
protected void checkSubclass () { |
192 |
protected void checkSubclass () { |
|
Lines 186-205
Link Here
|
| 186 |
} |
223 |
} |
| 187 |
|
224 |
|
| 188 |
void createHandle () { |
225 |
void createHandle () { |
| 189 |
state |= THEME_BACKGROUND; |
226 |
if ((style & SWT.SMOOTH) != 0) { |
| 190 |
if (hasBorder()) { |
227 |
nsToolbar = ((NSToolbar)new SWTToolbar().alloc()).initWithIdentifier(NSString.stringWith(String.valueOf(NEXT_ID++))); |
| 191 |
NSRect rect = new NSRect(); |
228 |
nsToolbar.setDelegate(nsToolbar); |
| 192 |
NSScrollView scrollWidget = (NSScrollView)new SWTScrollView().alloc(); |
229 |
nsToolbar.setDisplayMode(OS.NSToolbarDisplayModeIconOnly); |
| 193 |
scrollWidget.initWithFrame (rect); |
230 |
NSWindow window = parent.view.window(); |
| 194 |
scrollWidget.setDrawsBackground(false); |
231 |
window.setToolbar(nsToolbar); |
| 195 |
scrollWidget.setBorderType(OS.NSBezelBorder); |
232 |
nsToolbar.setVisible(true); |
| 196 |
scrollView = scrollWidget; |
233 |
NSArray views = window.contentView().superview().subviews(); |
| 197 |
} |
234 |
for (int i = 0; i < views.count(); i++) { |
| 198 |
NSView widget = (NSView)new SWTView().alloc(); |
235 |
id id = views.objectAtIndex(i); |
| 199 |
widget.init(); |
236 |
if (new NSObject(id).className().getString().equals("NSToolbarView")) { |
| 200 |
// widget.setDrawsBackground(false); |
237 |
view = new NSView(id); |
| 201 |
view = widget; |
238 |
OS.object_setClass(view.id, OS.objc_getClass("SWTToolbarView")); |
| 202 |
if (scrollView != null) view.setAutoresizingMask(OS.NSViewHeightSizable | OS.NSViewWidthSizable); |
239 |
view.retain(); |
|
|
240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
} else { |
| 244 |
state |= THEME_BACKGROUND; |
| 245 |
if (hasBorder()) { |
| 246 |
NSRect rect = new NSRect(); |
| 247 |
NSScrollView scrollWidget = (NSScrollView)new SWTScrollView().alloc(); |
| 248 |
scrollWidget.initWithFrame (rect); |
| 249 |
scrollWidget.setDrawsBackground(false); |
| 250 |
scrollWidget.setBorderType(OS.NSBezelBorder); |
| 251 |
scrollView = scrollWidget; |
| 252 |
} |
| 253 |
NSView widget = (NSView)new SWTView().alloc(); |
| 254 |
widget.init(); |
| 255 |
view = widget; |
| 256 |
if (scrollView != null) view.setAutoresizingMask(OS.NSViewHeightSizable | OS.NSViewWidthSizable); |
| 257 |
} |
| 203 |
} |
258 |
} |
| 204 |
|
259 |
|
| 205 |
void createItem (ToolItem item, int index) { |
260 |
void createItem (ToolItem item, int index) { |
|
Lines 209-218
Link Here
|
| 209 |
System.arraycopy (items, 0, newItems, 0, items.length); |
264 |
System.arraycopy (items, 0, newItems, 0, items.length); |
| 210 |
items = newItems; |
265 |
items = newItems; |
| 211 |
} |
266 |
} |
| 212 |
item.createWidget(); |
267 |
if ((style & SWT.SMOOTH) != 0) { |
| 213 |
view.addSubview(item.view); |
268 |
item.style |= SWT.SMOOTH; |
|
|
269 |
} |
| 270 |
item.createWidget(); |
| 214 |
System.arraycopy (items, index, items, index + 1, itemCount++ - index); |
271 |
System.arraycopy (items, index, items, index + 1, itemCount++ - index); |
| 215 |
items [index] = item; |
272 |
items [index] = item; |
|
|
273 |
if ((style & SWT.SMOOTH) != 0) { |
| 274 |
nsToolbar.insertItemWithItemIdentifier(item.getItemID(), index); |
| 275 |
} else { |
| 276 |
view.addSubview(item.view); |
| 277 |
} |
| 216 |
relayout (); |
278 |
relayout (); |
| 217 |
} |
279 |
} |
| 218 |
|
280 |
|
|
Lines 222-227
Link Here
|
| 222 |
itemCount = 0; |
284 |
itemCount = 0; |
| 223 |
} |
285 |
} |
| 224 |
|
286 |
|
|
|
287 |
NSFont defaultNSFont() { |
| 288 |
return NSFont.systemFontOfSize(11.0f); |
| 289 |
} |
| 290 |
|
| 291 |
void deregister () { |
| 292 |
super.deregister (); |
| 293 |
if (nsToolbar != null) display.removeWidget (nsToolbar); |
| 294 |
} |
| 295 |
|
| 225 |
void destroyItem (ToolItem item) { |
296 |
void destroyItem (ToolItem item) { |
| 226 |
int index = 0; |
297 |
int index = 0; |
| 227 |
while (index < itemCount) { |
298 |
while (index < itemCount) { |
|
Lines 232-238
Link Here
|
| 232 |
if (item == lastFocus) lastFocus = null; |
303 |
if (item == lastFocus) lastFocus = null; |
| 233 |
System.arraycopy (items, index + 1, items, index, --itemCount - index); |
304 |
System.arraycopy (items, index + 1, items, index, --itemCount - index); |
| 234 |
items [itemCount] = null; |
305 |
items [itemCount] = null; |
| 235 |
item.view.removeFromSuperview(); |
306 |
if ((style & SWT.SMOOTH) != 0) { |
|
|
307 |
nsToolbar.removeItemAtIndex(index); |
| 308 |
} else { |
| 309 |
item.view.removeFromSuperview(); |
| 310 |
} |
| 236 |
relayout (); |
311 |
relayout (); |
| 237 |
} |
312 |
} |
| 238 |
|
313 |
|
|
Lines 262-267
Link Here
|
| 262 |
return super.findTooltip (pt); |
337 |
return super.findTooltip (pt); |
| 263 |
} |
338 |
} |
| 264 |
|
339 |
|
|
|
340 |
void setZOrder() { |
| 341 |
if ((style & SWT.SMOOTH) != 0) return; |
| 342 |
super.setZOrder(); |
| 343 |
} |
| 344 |
|
| 345 |
public Rectangle getBounds () { |
| 346 |
checkWidget(); |
| 347 |
|
| 348 |
if ((style & SWT.SMOOTH) != 0) { |
| 349 |
// The NSToolbar's view will always be a child of the Shell, so we can just |
| 350 |
// convert the frame to window-relative coordinates. |
| 351 |
NSRect rect = view.frame(); |
| 352 |
rect = view.convertRect_toView_(rect, null); |
| 353 |
return new Rectangle((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height); |
| 354 |
} |
| 355 |
|
| 356 |
return super.getBounds(); |
| 357 |
} |
| 358 |
|
| 265 |
boolean forceFocus (NSView focusView) { |
359 |
boolean forceFocus (NSView focusView) { |
| 266 |
if (lastFocus != null && lastFocus.setFocus ()) return true; |
360 |
if (lastFocus != null && lastFocus.setFocus ()) return true; |
| 267 |
ToolItem [] items = getItems (); |
361 |
ToolItem [] items = getItems (); |
|
Lines 448-453
Link Here
|
| 448 |
return new int [] {rows, maxX, y + itemHeight}; |
542 |
return new int [] {rows, maxX, y + itemHeight}; |
| 449 |
} |
543 |
} |
| 450 |
|
544 |
|
|
|
545 |
int [] layoutUnified (int width, int height, boolean resize) { |
| 546 |
int x = 0, y = 0; |
| 547 |
int itemHeight = 0, maxX = 0; |
| 548 |
Point [] sizes = new Point [itemCount]; |
| 549 |
NSRect [] containerRects = new NSRect[itemCount]; |
| 550 |
// This next line relies on the observation that all of the toolbar item views are children of the first |
| 551 |
// subview of the NSToolbarView. |
| 552 |
NSArray itemViewers = new NSView(view.subviews().objectAtIndex(0)).subviews(); |
| 553 |
for (int i=0; i<itemCount; i++) { |
| 554 |
Point size = sizes [i] = items [i].computeSize (); |
| 555 |
containerRects [i] = new NSView(itemViewers.objectAtIndex(i)).frame(); |
| 556 |
if (i == 0) x = (int) containerRects[0].x; |
| 557 |
itemHeight = Math.max (itemHeight, size.y); |
| 558 |
} |
| 559 |
for (int i=0; i<itemCount; i++) { |
| 560 |
ToolItem item = items [i]; |
| 561 |
Point size = sizes [i]; |
| 562 |
if (resize) { |
| 563 |
item.setBounds (0, 0, size.x, itemHeight); |
| 564 |
Control control = item.control; |
| 565 |
if (control != null) { |
| 566 |
control.setBounds (x, y, size.x, itemHeight); |
| 567 |
} |
| 568 |
} |
| 569 |
x += containerRects[i].width; |
| 570 |
maxX = Math.max (maxX, x); |
| 571 |
} |
| 572 |
|
| 573 |
return new int [] {1, maxX, itemHeight}; |
| 574 |
} |
| 575 |
|
| 451 |
int [] layoutVertical (int width, int height, boolean resize) { |
576 |
int [] layoutVertical (int width, int height, boolean resize) { |
| 452 |
int xSpacing = 2, ySpacing = 0; |
577 |
int xSpacing = 2, ySpacing = 0; |
| 453 |
int marginWidth = 0, marginHeight = 0; |
578 |
int marginWidth = 0, marginHeight = 0; |
|
Lines 485-490
Link Here
|
| 485 |
} |
610 |
} |
| 486 |
|
611 |
|
| 487 |
int [] layout (int nWidth, int nHeight, boolean resize) { |
612 |
int [] layout (int nWidth, int nHeight, boolean resize) { |
|
|
613 |
if ((style & SWT.SMOOTH) != 0) { |
| 614 |
return layoutUnified(nWidth, nHeight, resize); |
| 615 |
} |
| 616 |
|
| 488 |
if ((style & SWT.VERTICAL) != 0) { |
617 |
if ((style & SWT.VERTICAL) != 0) { |
| 489 |
return layoutVertical (nWidth, nHeight, resize); |
618 |
return layoutVertical (nWidth, nHeight, resize); |
| 490 |
} else { |
619 |
} else { |
|
Lines 492-497
Link Here
|
| 492 |
} |
621 |
} |
| 493 |
} |
622 |
} |
| 494 |
|
623 |
|
|
|
624 |
void register() { |
| 625 |
super.register(); |
| 626 |
if (nsToolbar != null) display.addWidget (nsToolbar, this); |
| 627 |
} |
| 628 |
|
| 495 |
void relayout () { |
629 |
void relayout () { |
| 496 |
if (!getDrawing()) return; |
630 |
if (!getDrawing()) return; |
| 497 |
Rectangle rect = getClientArea (); |
631 |
Rectangle rect = getClientArea (); |
|
Lines 513-519
Link Here
|
| 513 |
} |
647 |
} |
| 514 |
|
648 |
|
| 515 |
void releaseHandle () { |
649 |
void releaseHandle () { |
| 516 |
super.releaseHandle (); |
650 |
super.releaseHandle (); |
|
|
651 |
|
| 652 |
if (nsToolbar != null) { |
| 653 |
nsToolbar.release(); |
| 654 |
nsToolbar = null; |
| 655 |
} |
| 656 |
|
| 517 |
if (accessibilityAttributes != null) accessibilityAttributes.release(); |
657 |
if (accessibilityAttributes != null) accessibilityAttributes.release(); |
| 518 |
accessibilityAttributes = null; |
658 |
accessibilityAttributes = null; |
| 519 |
} |
659 |
} |
|
Lines 547-560
Link Here
|
| 547 |
case SWT.MouseMove: |
687 |
case SWT.MouseMove: |
| 548 |
// Start with the global mouse location, as the MouseEnter may occur due to |
688 |
// Start with the global mouse location, as the MouseEnter may occur due to |
| 549 |
// an application-activated event, which isn't associated with a window. |
689 |
// an application-activated event, which isn't associated with a window. |
| 550 |
NSPoint toolbarPoint = NSEvent.mouseLocation(); |
690 |
NSPoint windowPoint = NSEvent.mouseLocation(); |
| 551 |
toolbarPoint = view.window().convertScreenToBase(toolbarPoint); |
691 |
windowPoint = view.window().convertScreenToBase(windowPoint); |
| 552 |
toolbarPoint = view.convertPoint_fromView_(toolbarPoint, null); |
|
|
| 553 |
for (int i = 0; i < itemCount; i++) { |
692 |
for (int i = 0; i < itemCount; i++) { |
| 554 |
ToolItem item = items [i]; |
693 |
ToolItem item = items [i]; |
| 555 |
int currState = item.state; |
694 |
int currState = item.state; |
| 556 |
|
695 |
NSPoint viewPoint = item.view.convertPoint_fromView_(windowPoint, null); |
| 557 |
if (OS.NSPointInRect(toolbarPoint, item.view.frame())) { |
696 |
if (item.view.mouse(viewPoint, item.view.bounds())) { |
| 558 |
item.state |= Widget.HOT; |
697 |
item.state |= Widget.HOT; |
| 559 |
} else { |
698 |
} else { |
| 560 |
item.state &= ~Widget.HOT; |
699 |
item.state &= ~Widget.HOT; |
|
Lines 576-581
Link Here
|
| 576 |
return super.sendMouseEvent(nsEvent, type, send); |
715 |
return super.sendMouseEvent(nsEvent, type, send); |
| 577 |
} |
716 |
} |
| 578 |
|
717 |
|
|
|
718 |
void setBounds (int x, int y, int width, int height, boolean move, boolean resize) { |
| 719 |
// In the unified toolbar case, the toolbar view size and position is completely controlled |
| 720 |
// by the window, so don't change its bounds or location. |
| 721 |
if ((style & SWT.SMOOTH) != 0) return; |
| 722 |
super.setBounds(x, y, width, height, move, resize); |
| 723 |
} |
| 724 |
|
| 579 |
void setFont(NSFont font) { |
725 |
void setFont(NSFont font) { |
| 580 |
for (int i = 0; i < itemCount; i++) { |
726 |
for (int i = 0; i < itemCount; i++) { |
| 581 |
ToolItem item = items[i]; |
727 |
ToolItem item = items[i]; |
|
Lines 589-594
Link Here
|
| 589 |
if (redraw && drawCount == 0) relayout(); |
735 |
if (redraw && drawCount == 0) relayout(); |
| 590 |
} |
736 |
} |
| 591 |
|
737 |
|
|
|
738 |
public void setVisible(boolean visible) { |
| 739 |
if (nsToolbar != null) nsToolbar.setVisible(visible); |
| 740 |
super.setVisible(visible); |
| 741 |
} |
| 742 |
|
| 743 |
int /*long*/ toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(int /*long*/ id, int /*long*/ sel, int /*long*/ toolbar, int /*long*/ itemIdentifier, boolean flag) { |
| 744 |
NSString itemID = new NSString(itemIdentifier); |
| 745 |
for (int j = 0; j < itemCount; j++) { |
| 746 |
ToolItem item = items[j]; |
| 747 |
if (itemID.isEqual(item.nsItem.itemIdentifier())) { |
| 748 |
return item.nsItem.id; |
| 749 |
} |
| 750 |
} |
| 751 |
return 0; |
| 752 |
} |
| 753 |
|
| 754 |
/* |
| 755 |
* Returns an array of all toolbar item IDs allowed to be in the toolbar. Since the ToolBar created all of the ToolItems |
| 756 |
* return all of the item IDs. |
| 757 |
*/ |
| 758 |
int /*long*/ toolbarAllowedItemIdentifiers(int /*long*/ id, int /*long*/ sel, int /*long*/ toolbar) { |
| 759 |
NSMutableArray array = NSMutableArray.arrayWithCapacity(itemCount); |
| 760 |
for (int i = 0; i < itemCount; i++) { |
| 761 |
array.addObject(items[i].nsItem.itemIdentifier()); |
| 762 |
} |
| 763 |
return array.id; |
| 764 |
} |
| 765 |
|
| 766 |
/* |
| 767 |
* This delegate method isn't really needed because ToolBars aren't customizable, but it's required according to the documentation. |
| 768 |
*/ |
| 769 |
int /*long*/ toolbarDefaultItemIdentifiers(int /*long*/ id, int /*long*/ sel, int /*long*/ toolbar) { |
| 770 |
return toolbarAllowedItemIdentifiers(id, sel, toolbar); |
| 771 |
} |
| 772 |
|
| 773 |
/* |
| 774 |
* toolbarSelectableItemIdentifiers returns an array of all items that can be the selected item, as determined |
| 775 |
* by setSelectedItemIdentifier. |
| 776 |
*/ |
| 777 |
int /*long*/ toolbarSelectableItemIdentifiers(int /*long*/ id, int /*long*/ sel, int /*long*/ toolbar) { |
| 778 |
NSMutableArray array = NSMutableArray.arrayWithCapacity(itemCount); |
| 779 |
for (int i = 0; i < itemCount; i++) { |
| 780 |
if ((items[i].style & SWT.RADIO) != 0) array.addObject(items[i].nsItem.itemIdentifier()); |
| 781 |
} |
| 782 |
return array.id; |
| 783 |
} |
| 784 |
|
| 592 |
boolean translateTraversal (int key, NSEvent theEvent, boolean[] consume) { |
785 |
boolean translateTraversal (int key, NSEvent theEvent, boolean[] consume) { |
| 593 |
boolean result = super.translateTraversal (key, theEvent, consume); |
786 |
boolean result = super.translateTraversal (key, theEvent, consume); |
| 594 |
if (result) return result; |
787 |
if (result) return result; |