|
Lines 39-44
Link Here
|
| 39 |
public class ToolItem extends Item { |
39 |
public class ToolItem extends Item { |
| 40 |
NSView view; |
40 |
NSView view; |
| 41 |
NSButton button; |
41 |
NSButton button; |
|
|
42 |
NSToolbarItem nsItem; |
| 43 |
NSString id; |
| 42 |
int width = DEFAULT_SEPARATOR_WIDTH; |
44 |
int width = DEFAULT_SEPARATOR_WIDTH; |
| 43 |
ToolBar parent; |
45 |
ToolBar parent; |
| 44 |
Image hotImage, disabledImage; |
46 |
Image hotImage, disabledImage; |
|
Lines 245-279
Link Here
|
| 245 |
} |
247 |
} |
| 246 |
|
248 |
|
| 247 |
void createHandle () { |
249 |
void createHandle () { |
| 248 |
if ((style & SWT.SEPARATOR) != 0) { |
250 |
if (parent.toolbar == null) { |
| 249 |
NSBox widget = (NSBox)new SWTBox().alloc(); |
251 |
if ((style & SWT.SEPARATOR) != 0) { |
| 250 |
widget.init(); |
252 |
NSBox widget = (NSBox)new SWTBox().alloc(); |
| 251 |
widget.setBoxType(OS.NSBoxSeparator); |
253 |
widget.init(); |
| 252 |
widget.setBorderWidth(0); |
254 |
widget.setBoxType(OS.NSBoxSeparator); |
| 253 |
view = widget; |
255 |
widget.setBorderWidth(0); |
|
|
256 |
view = widget; |
| 257 |
} else { |
| 258 |
NSView widget = (NSView)new SWTView().alloc(); |
| 259 |
widget.init(); |
| 260 |
button = (NSButton)new SWTButton().alloc(); |
| 261 |
button.init(); |
| 262 |
/* |
| 263 |
* Feature in Cocoa. NSButtons without borders do not leave any margin |
| 264 |
* between their edge and their image. The workaround is to provide a |
| 265 |
* custom cell that displays the image in a better position. |
| 266 |
*/ |
| 267 |
NSButtonCell cell = (NSButtonCell)new SWTButtonCell ().alloc ().init (); |
| 268 |
button.setCell (cell); |
| 269 |
cell.release(); |
| 270 |
button.setBordered(false); |
| 271 |
button.setAction(OS.sel_sendSelection); |
| 272 |
button.setTarget(button); |
| 273 |
Font font = parent.font != null ? parent.font : parent.defaultFont (); |
| 274 |
button.setFont(font.handle); |
| 275 |
button.setImagePosition(OS.NSImageOverlaps); |
| 276 |
button.setTitle(NSString.string()); |
| 277 |
button.setEnabled(parent.getEnabled()); |
| 278 |
widget.addSubview(button); |
| 279 |
view = widget; |
| 280 |
} |
| 254 |
} else { |
281 |
} else { |
| 255 |
NSView widget = (NSView)new SWTView().alloc(); |
282 |
id = NSString.stringWith(String.valueOf(++ToolBar.NEXT_ID)); |
| 256 |
widget.init(); |
283 |
id.retain(); |
| 257 |
button = (NSButton)new SWTButton().alloc(); |
284 |
nsItem = ((NSToolbarItem)new NSToolbarItem().alloc()).initWithItemIdentifier(id); |
| 258 |
button.init(); |
285 |
nsItem.setTarget(parent.toolbar); |
| 259 |
/* |
286 |
nsItem.setAction(OS.sel_sendSelection_); |
| 260 |
* Feature in Cocoa. NSButtons without borders do not leave any margin |
|
|
| 261 |
* between their edge and their image. The workaround is to provide a |
| 262 |
* custom cell that displays the image in a better position. |
| 263 |
*/ |
| 264 |
NSButtonCell cell = (NSButtonCell)new SWTButtonCell ().alloc ().init (); |
| 265 |
button.setCell (cell); |
| 266 |
cell.release(); |
| 267 |
button.setBordered(false); |
| 268 |
button.setAction(OS.sel_sendSelection); |
| 269 |
button.setTarget(button); |
| 270 |
Font font = parent.font != null ? parent.font : parent.defaultFont (); |
| 271 |
button.setFont(font.handle); |
| 272 |
button.setImagePosition(OS.NSImageOverlaps); |
| 273 |
button.setTitle(NSString.string()); |
| 274 |
button.setEnabled(parent.getEnabled()); |
| 275 |
widget.addSubview(button); |
| 276 |
view = widget; |
| 277 |
} |
287 |
} |
| 278 |
} |
288 |
} |
| 279 |
|
289 |
|
|
Lines 348-355
Link Here
|
| 348 |
} |
358 |
} |
| 349 |
|
359 |
|
| 350 |
void enableWidget(boolean enabled) { |
360 |
void enableWidget(boolean enabled) { |
| 351 |
if ((style & SWT.SEPARATOR) == 0) { |
361 |
if (nsItem == null) { |
| 352 |
((NSButton)button).setEnabled(enabled); |
362 |
if ((style & SWT.SEPARATOR) == 0) { |
|
|
363 |
((NSButton)button).setEnabled(enabled); |
| 364 |
} |
| 365 |
} else { |
| 366 |
nsItem.setEnabled(enabled); |
| 353 |
} |
367 |
} |
| 354 |
} |
368 |
} |
| 355 |
|
369 |
|
|
Lines 650-655
Link Here
|
| 650 |
if (view != null) view.release (); |
664 |
if (view != null) view.release (); |
| 651 |
if (button != null) button.release (); |
665 |
if (button != null) button.release (); |
| 652 |
view = button = null; |
666 |
view = button = null; |
|
|
667 |
if (nsItem != null) nsItem.release(); |
| 668 |
nsItem = null; |
| 669 |
if (id != null) id.release(); |
| 670 |
id = null; |
| 653 |
parent = null; |
671 |
parent = null; |
| 654 |
} |
672 |
} |
| 655 |
|
673 |
|
|
Lines 682-687
Link Here
|
| 682 |
} |
700 |
} |
| 683 |
|
701 |
|
| 684 |
void setBounds (int x, int y, int width, int height) { |
702 |
void setBounds (int x, int y, int width, int height) { |
|
|
703 |
if (view == null) return; |
| 685 |
NSRect rect = new NSRect(); |
704 |
NSRect rect = new NSRect(); |
| 686 |
rect.x = x; |
705 |
rect.x = x; |
| 687 |
rect.y = y; |
706 |
rect.y = y; |
|
Lines 721-736
Link Here
|
| 721 |
} |
740 |
} |
| 722 |
if ((style & SWT.SEPARATOR) == 0) return; |
741 |
if ((style & SWT.SEPARATOR) == 0) return; |
| 723 |
if (this.control == control) return; |
742 |
if (this.control == control) return; |
| 724 |
NSBox widget = (NSBox)view; |
743 |
Control oldControl = this.control; |
| 725 |
if (control == null) { |
|
|
| 726 |
widget.setBoxType(OS.NSBoxSeparator); |
| 727 |
} else { |
| 728 |
widget.setBoxType(OS.NSBoxCustom); |
| 729 |
} |
| 730 |
this.control = control; |
744 |
this.control = control; |
| 731 |
view.setHidden(control != null); |
745 |
if (nsItem == null) { |
| 732 |
if (control != null && !control.isDisposed ()) { |
746 |
NSBox widget = (NSBox)view; |
| 733 |
control.moveAbove (null); |
747 |
if (control == null) { |
|
|
748 |
widget.setBoxType(OS.NSBoxSeparator); |
| 749 |
} else { |
| 750 |
widget.setBoxType(OS.NSBoxCustom); |
| 751 |
} |
| 752 |
view.setHidden(control != null); |
| 753 |
if (control != null && !control.isDisposed ()) { |
| 754 |
control.moveAbove (null); |
| 755 |
} |
| 756 |
} else { |
| 757 |
NSToolbar toolbar = parent.toolbar; |
| 758 |
int index = parent.indexOf(this); |
| 759 |
toolbar.removeItemAtIndex(index); |
| 760 |
toolbar.insertItemWithItemIdentifier(control != null ? id : OS.NSToolbarSeparatorItemIdentifier, index); |
| 761 |
nsItem.setView(control != null ? control.topView() : null); |
| 762 |
if (control != null) { |
| 763 |
NSSize size = new NSSize(); |
| 764 |
//TODO should not computeSize |
| 765 |
size.height = control.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; |
| 766 |
size.width = width; |
| 767 |
nsItem.setMinSize(size); |
| 768 |
nsItem.setMaxSize(size); |
| 769 |
} |
| 770 |
if (oldControl != null && !oldControl.isDisposed()) { |
| 771 |
parent.contentView().addSubview(oldControl.topView(), OS.NSWindowBelow, null); |
| 772 |
oldControl.setBounds(0,0,0,0); |
| 773 |
} |
| 734 |
} |
774 |
} |
| 735 |
parent.relayout (); |
775 |
parent.relayout (); |
| 736 |
} |
776 |
} |
|
Lines 881-896
Link Here
|
| 881 |
if (string == null) error (SWT.ERROR_NULL_ARGUMENT); |
921 |
if (string == null) error (SWT.ERROR_NULL_ARGUMENT); |
| 882 |
if ((style & SWT.SEPARATOR) != 0) return; |
922 |
if ((style & SWT.SEPARATOR) != 0) return; |
| 883 |
super.setText (string); |
923 |
super.setText (string); |
| 884 |
NSButton widget = (NSButton)button; |
924 |
if (nsItem == null) { |
| 885 |
widget.setAttributedTitle(createString()); |
925 |
NSButton widget = (NSButton)button; |
| 886 |
if (text.length() != 0 && image != null) { |
926 |
widget.setAttributedTitle(createString()); |
| 887 |
if ((parent.style & SWT.RIGHT) != 0) { |
927 |
if (text.length() != 0 && image != null) { |
| 888 |
widget.setImagePosition(OS.NSImageLeft); |
928 |
if ((parent.style & SWT.RIGHT) != 0) { |
|
|
929 |
widget.setImagePosition(OS.NSImageLeft); |
| 930 |
} else { |
| 931 |
widget.setImagePosition(OS.NSImageAbove); |
| 932 |
} |
| 889 |
} else { |
933 |
} else { |
| 890 |
widget.setImagePosition(OS.NSImageAbove); |
934 |
widget.setImagePosition(text.length() != 0 ? OS.NSNoImage : OS.NSImageOnly); |
| 891 |
} |
935 |
} |
| 892 |
} else { |
936 |
} else { |
| 893 |
widget.setImagePosition(text.length() != 0 ? OS.NSNoImage : OS.NSImageOnly); |
937 |
char [] chars = new char [text.length ()]; |
|
|
938 |
text.getChars (0, chars.length, chars, 0); |
| 939 |
int length = fixMnemonic (chars); |
| 940 |
nsItem.setLabel(NSString.stringWithCharacters(chars, length)); |
| 894 |
} |
941 |
} |
| 895 |
parent.relayout (); |
942 |
parent.relayout (); |
| 896 |
} |
943 |
} |
|
Lines 917-924
Link Here
|
| 917 |
*/ |
964 |
*/ |
| 918 |
public void setToolTipText (String string) { |
965 |
public void setToolTipText (String string) { |
| 919 |
checkWidget(); |
966 |
checkWidget(); |
| 920 |
toolTipText = string; |
967 |
if (nsItem == null) { |
| 921 |
parent.checkToolTip (this); |
968 |
toolTipText = string; |
|
|
969 |
parent.checkToolTip (this); |
| 970 |
} else { |
| 971 |
char[] chars = new char [string.length ()]; |
| 972 |
string.getChars (0, chars.length, chars, 0); |
| 973 |
int length = fixMnemonic (chars); |
| 974 |
nsItem.setToolTip(NSString.stringWithCharacters (chars, length)); |
| 975 |
} |
| 922 |
} |
976 |
} |
| 923 |
|
977 |
|
| 924 |
void setVisible (boolean visible) { |
978 |
void setVisible (boolean visible) { |
|
Lines 947-952
Link Here
|
| 947 |
if ((style & SWT.SEPARATOR) == 0) return; |
1001 |
if ((style & SWT.SEPARATOR) == 0) return; |
| 948 |
if (width < 0 || this.width == width) return; |
1002 |
if (width < 0 || this.width == width) return; |
| 949 |
this.width = width; |
1003 |
this.width = width; |
|
|
1004 |
if (nsItem != null) { |
| 1005 |
NSSize size = new NSSize(); |
| 1006 |
//TODO should not computeSize |
| 1007 |
size.height = control != null ? control.computeSize(SWT.DEFAULT, SWT.DEFAULT).y : 0; |
| 1008 |
size.width = width; |
| 1009 |
nsItem.setMinSize(size); |
| 1010 |
nsItem.setMaxSize(size); |
| 1011 |
} |
| 950 |
parent.relayout(); |
1012 |
parent.relayout(); |
| 951 |
} |
1013 |
} |
| 952 |
|
1014 |
|
|
Lines 966-989
Link Here
|
| 966 |
image = disabledImage; |
1028 |
image = disabledImage; |
| 967 |
} |
1029 |
} |
| 968 |
} |
1030 |
} |
| 969 |
NSButton widget = (NSButton)button; |
1031 |
if (nsItem == null) { |
| 970 |
/* |
1032 |
NSButton widget = (NSButton)button; |
| 971 |
* Feature in Cocoa. If the NSImage object being set into the button is |
1033 |
/* |
| 972 |
* the same NSImage object that is already there then the button does not |
1034 |
* Feature in Cocoa. If the NSImage object being set into the button is |
| 973 |
* redraw itself. This results in the button's image not visually updating |
1035 |
* the same NSImage object that is already there then the button does not |
| 974 |
* if the NSImage object's content has changed since it was last set |
1036 |
* redraw itself. This results in the button's image not visually updating |
| 975 |
* into the button. The workaround is to explicitly redraw the button. |
1037 |
* if the NSImage object's content has changed since it was last set |
| 976 |
*/ |
1038 |
* into the button. The workaround is to explicitly redraw the button. |
| 977 |
widget.setImage(image != null ? image.handle : null); |
1039 |
*/ |
| 978 |
widget.setNeedsDisplay(true); |
1040 |
widget.setImage(image != null ? image.handle : null); |
| 979 |
if (text.length() != 0 && image != null) { |
1041 |
widget.setNeedsDisplay(true); |
| 980 |
if ((parent.style & SWT.RIGHT) != 0) { |
1042 |
if (text.length() != 0 && image != null) { |
| 981 |
widget.setImagePosition(OS.NSImageLeft); |
1043 |
if ((parent.style & SWT.RIGHT) != 0) { |
| 982 |
} else { |
1044 |
widget.setImagePosition(OS.NSImageLeft); |
| 983 |
((NSButton)button).setImagePosition(OS.NSImageAbove); |
1045 |
} else { |
|
|
1046 |
((NSButton)button).setImagePosition(OS.NSImageAbove); |
| 1047 |
} |
| 1048 |
} else { |
| 1049 |
widget.setImagePosition(text.length() != 0 ? OS.NSNoImage : OS.NSImageOnly); |
| 984 |
} |
1050 |
} |
| 985 |
} else { |
1051 |
} else { |
| 986 |
widget.setImagePosition(text.length() != 0 ? OS.NSNoImage : OS.NSImageOnly); |
1052 |
nsItem.setImage(image != null ? image.handle : null); |
| 987 |
} |
1053 |
} |
| 988 |
parent.relayout(); |
1054 |
parent.relayout(); |
| 989 |
} |
1055 |
} |