comparison dwt/widgets/List.d @ 72:e08aaf7b6830

Ported dwt.widgets.List
author Jacob Carlborg <doob@me.com>
date Tue, 23 Dec 2008 23:51:30 +0100
parents 3607217555f2
children 8e3c85e1733d
comparison
equal deleted inserted replaced
71:3607217555f2 72:e08aaf7b6830
40 import dwt.internal.cocoa.NSTableColumn; 40 import dwt.internal.cocoa.NSTableColumn;
41 import dwt.internal.cocoa.NSTableView; 41 import dwt.internal.cocoa.NSTableView;
42 import dwt.internal.cocoa.OS; 42 import dwt.internal.cocoa.OS;
43 import dwt.internal.cocoa.SWTScrollView; 43 import dwt.internal.cocoa.SWTScrollView;
44 import dwt.internal.cocoa.SWTTableView; 44 import dwt.internal.cocoa.SWTTableView;
45 import dwt.internal.cocoa.id; 45 import cocoa = dwt.internal.cocoa.id;
46
47 import dwt.internal.c.Carbon;
48 import dwt.internal.objc.cocoa.Cocoa;
49 import objc = dwt.internal.objc.runtime;
50 import dwt.widgets.Composite;
51 import dwt.widgets.Scrollable;
52 import dwt.widgets.TypedListener;
46 53
47 /** 54 /**
48 * Instances of this class represent a selectable user interface 55 * Instances of this class represent a selectable user interface
49 * object that displays a list of strings and issues notification 56 * object that displays a list of strings and issues notification
50 * when a string is selected. A list may be single or multi select. 57 * when a string is selected. A list may be single or multi select.
102 */ 109 */
103 public this (Composite parent, int style) { 110 public this (Composite parent, int style) {
104 super (parent, checkStyle (style)); 111 super (parent, checkStyle (style));
105 } 112 }
106 113
107 int accessibilityAttributeValue (int /*long*/ id, int /*long*/ sel, int /*long*/ arg0) { 114 objc.id accessibilityAttributeValue (objc.id id, objc.SEL sel, objc.id arg0) {
108 115
109 if (accessible !is null) { 116 if (accessible !is null) {
110 NSString attribute = new NSString(arg0); 117 NSString attribute = new NSString(arg0);
111 id returnValue = accessible.internal_accessibilityAttributeValue(attribute, ACC.CHILDID_SELF); 118 cocoa.id returnValue = accessible.internal_accessibilityAttributeValue(attribute, ACC.CHILDID_SELF);
112 if (returnValue !is null) return returnValue.id; 119 if (returnValue !is null) return returnValue.id;
113 } 120 }
114 121
115 NSString attributeName = new NSString(arg0); 122 NSString attributeName = new NSString(arg0);
116 123
261 Rectangle rect = computeTrim (0, 0, width, height); 268 Rectangle rect = computeTrim (0, 0, width, height);
262 return new Point (rect.width, rect.height); 269 return new Point (rect.width, rect.height);
263 } 270 }
264 271
265 void createHandle () { 272 void createHandle () {
266 NSScrollView scrollWidget = cast(NSScrollView)new SWTScrollView().alloc(); 273 NSScrollView scrollWidget = cast(NSScrollView)(new SWTScrollView()).alloc();
267 scrollWidget.initWithFrame(new NSRect ()); 274 scrollWidget.initWithFrame(NSRect ());
268 if ((style & DWT.H_SCROLL) !is 0) scrollWidget.setHasHorizontalScroller(true); 275 if ((style & DWT.H_SCROLL) !is 0) scrollWidget.setHasHorizontalScroller(true);
269 if ((style & DWT.V_SCROLL) !is 0) scrollWidget.setHasVerticalScroller(true); 276 if ((style & DWT.V_SCROLL) !is 0) scrollWidget.setHasVerticalScroller(true);
270 scrollWidget.setAutohidesScrollers(true); 277 scrollWidget.setAutohidesScrollers(true);
271 scrollWidget.setBorderType((style & DWT.BORDER) !is 0 ? OS.NSBezelBorder : OS.NSNoBorder); 278 scrollWidget.setBorderType((style & DWT.BORDER) !is 0 ? OS.NSBezelBorder : OS.NSNoBorder);
272 279
273 NSTableView widget = cast(NSTableView)new SWTTableView().alloc(); 280 NSTableView widget = cast(NSTableView)(new SWTTableView()).alloc();
274 widget.initWithFrame(NSRect()); 281 widget.initWithFrame(NSRect());
275 widget.setAllowsMultipleSelection((style & DWT.MULTI) !is 0); 282 widget.setAllowsMultipleSelection((style & DWT.MULTI) !is 0);
276 widget.setDataSource(widget); 283 widget.setDataSource(widget);
277 widget.setHeaderView(null); 284 widget.setHeaderView(null);
278 widget.setDelegate(widget); 285 widget.setDelegate(widget);
279 widget.setDoubleAction(OS.sel_sendDoubleSelection); 286 widget.setDoubleAction(OS.sel_sendDoubleSelection);
280 if (!hasBorder()) widget.setFocusRingType(OS.NSFocusRingTypeNone); 287 if (!hasBorder()) widget.setFocusRingType(OS.NSFocusRingTypeNone);
281 288
282 column = cast(NSTableColumn)new NSTableColumn().alloc(); 289 column = cast(NSTableColumn)(new NSTableColumn()).alloc();
283 column.initWithIdentifier(NSString.stringWith("")); 290 column.initWithIdentifier(NSString.stringWith(""));
284 widget.addTableColumn (column); 291 widget.addTableColumn (column);
285 292
286 scrollView = scrollWidget; 293 scrollView = scrollWidget;
287 view = widget; 294 view = widget;
430 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 437 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
431 * </ul> 438 * </ul>
432 */ 439 */
433 public int getFocusIndex () { 440 public int getFocusIndex () {
434 checkWidget(); 441 checkWidget();
435 return (int)/*64*/((NSTableView)view).selectedRow(); 442 return cast(int)/*64*/(cast(NSTableView)view).selectedRow();
436 } 443 }
437 444
438 /** 445 /**
439 * Returns the item at the given, zero-relative index in the 446 * Returns the item at the given, zero-relative index in the
440 * receiver. Throws an exception if the index is out of range. 447 * receiver. Throws an exception if the index is out of range.
531 NSTableView widget = cast(NSTableView)view; 538 NSTableView widget = cast(NSTableView)view;
532 if (widget.numberOfSelectedRows() is 0) { 539 if (widget.numberOfSelectedRows() is 0) {
533 return new String [0]; 540 return new String [0];
534 } 541 }
535 NSIndexSet selection = widget.selectedRowIndexes(); 542 NSIndexSet selection = widget.selectedRowIndexes();
536 int count = (int)/*64*/selection.count(); 543 NSUInteger count = selection.count();
537 int /*long*/ [] indexBuffer = new int /*long*/ [count]; 544 NSUInteger [] indexBuffer = new NSUInteger [count];
538 selection.getIndexes(indexBuffer, count, 0); 545 selection.getIndexes(indexBuffer.ptr, count, 0);
539 String [] result = new String [count]; 546 String [] result = new String [count];
540 for (int i=0; i<count; i++) { 547 for (NSUInteger i=0; i<count; i++) {
541 result [i] = items [(int)/*64*/indexBuffer [i]]; 548 result [i] = items [indexBuffer [i]];
542 } 549 }
543 return result; 550 return result;
544 } 551 }
545 552
546 /** 553 /**
569 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 576 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
570 * </ul> 577 * </ul>
571 */ 578 */
572 public int getSelectionIndex () { 579 public int getSelectionIndex () {
573 checkWidget(); 580 checkWidget();
574 NSTableView widget = (NSTableView)view; 581 NSTableView widget = cast(NSTableView)view;
575 if (widget.numberOfSelectedRows() is 0) { 582 if (widget.numberOfSelectedRows() is 0) {
576 return -1; 583 return -1;
577 } 584 }
578 NSIndexSet selection = widget.selectedRowIndexes(); 585 NSIndexSet selection = widget.selectedRowIndexes();
579 int count = (int)/*64*/selection.count(); 586 NSUInteger count = selection.count();
580 int /*long*/ [] result = new int /*long*/ [count]; 587 NSUInteger [] result = NSUInteger [count];
581 selection.getIndexes(result, count, 0); 588 selection.getIndexes(result.ptr, count, 0);
582 return (int)/*64*/result [0]; 589 return cast(int) NSUInteger [0];
583 } 590 }
584 591
585 /** 592 /**
586 * Returns the zero-relative indices of the items which are currently 593 * Returns the zero-relative indices of the items which are currently
587 * selected in the receiver. The order of the indices is unspecified. 594 * selected in the receiver. The order of the indices is unspecified.
603 NSTableView widget = cast(NSTableView)view; 610 NSTableView widget = cast(NSTableView)view;
604 if (widget.numberOfSelectedRows() is 0) { 611 if (widget.numberOfSelectedRows() is 0) {
605 return new int [0]; 612 return new int [0];
606 } 613 }
607 NSIndexSet selection = widget.selectedRowIndexes(); 614 NSIndexSet selection = widget.selectedRowIndexes();
608 int count = (int)/*64*/selection.count(); 615 NSUInteger count = selection.count();
609 int /*long*/ [] indices = new int /*long*/ [count]; 616 NSUInteger [] indices = new NSUInteger [count];
610 selection.getIndexes(indices, count, 0); 617 selection.getIndexes(indices, count, 0);
611 int [] result = new int [count]; 618 NSUInteger [] result = new NSUInteger [count];
612 for (int i = 0; i < result.length; i++) { 619 for (NSUInteger i = 0; i < result.length; i++) {
613 result [i] = (int)/*64*/indices [i]; 620 result [i] = indices [i];
614 } 621 }
615 return result; 622 return cast(int[]) result;
616 } 623 }
617 624
618 /** 625 /**
619 * Returns the zero-relative index of the item which is currently 626 * Returns the zero-relative index of the item which is currently
620 * at the top of the receiver. This index can change when items are 627 * at the top of the receiver. This index can change when items are
632 //TODO - partial item at the top 639 //TODO - partial item at the top
633 NSRect rect = scrollView.documentVisibleRect(); 640 NSRect rect = scrollView.documentVisibleRect();
634 NSPoint point = NSPoint(); 641 NSPoint point = NSPoint();
635 point.x = rect.x; 642 point.x = rect.x;
636 point.y = rect.y; 643 point.y = rect.y;
637 int result = (int)/*64*/(cast(NSTableView)view).rowAtPoint(point); 644 int result = cast(int)/*64*/(cast(NSTableView)view).rowAtPoint(point);
638 if (result is -1) result = 0; 645 if (result is -1) result = 0;
639 return result; 646 return result;
640 } 647 }
641 648
642 /** 649 /**
712 checkWidget(); 719 checkWidget();
713 if (!(0 <= index && index < itemCount)) return false; 720 if (!(0 <= index && index < itemCount)) return false;
714 return (cast(NSTableView)view).isRowSelected(index); 721 return (cast(NSTableView)view).isRowSelected(index);
715 } 722 }
716 723
717 int /*long*/ numberOfRowsInTableView(int /*long*/ id, int /*long*/ sel, int /*long*/ aTableView) { 724 int numberOfRowsInTableView(objc.id id, objc.SEL sel, objc.id aTableView) {
718 return itemCount; 725 return itemCount;
719 } 726 }
720 727
721 void releaseHandle () { 728 void releaseHandle () {
722 super.releaseHandle (); 729 super.releaseHandle ();
892 * </ul> 899 * </ul>
893 */ 900 */
894 public void select (int index) { 901 public void select (int index) {
895 checkWidget(); 902 checkWidget();
896 if (0 <= index && index < itemCount) { 903 if (0 <= index && index < itemCount) {
897 NSIndexSet indexes = cast(NSIndexSet)new NSIndexSet().alloc(); 904 NSIndexSet indexes = cast(NSIndexSet)(new NSIndexSet()).alloc();
898 indexes.initWithIndex(index); 905 indexes.initWithIndex(index);
899 NSTableView widget = cast(NSTableView)view; 906 NSTableView widget = cast(NSTableView)view;
900 ignoreSelect = true; 907 ignoreSelect = true;
901 widget.selectRowIndexes(indexes, (style & DWT.MULTI) !is 0); 908 widget.selectRowIndexes(indexes, (style & DWT.MULTI) !is 0);
902 ignoreSelect = false; 909 ignoreSelect = false;
933 selectAll (); 940 selectAll ();
934 } else { 941 } else {
935 start = Math.max (0, start); 942 start = Math.max (0, start);
936 end = Math.min (end, itemCount - 1); 943 end = Math.min (end, itemCount - 1);
937 int length = end - start + 1; 944 int length = end - start + 1;
938 NSIndexSet indexes = cast(NSIndexSet)new NSIndexSet().alloc(); 945 NSIndexSet indexes = cast(NSIndexSet)(new NSIndexSet()).alloc();
939 NSRange range = NSRange(); 946 NSRange range = NSRange();
940 range.location = start; 947 range.location = start;
941 range.length = length; 948 range.length = length;
942 indexes.initWithIndexesInRange(range); 949 indexes.initWithIndexesInRange(range);
943 NSTableView widget = cast(NSTableView)view; 950 NSTableView widget = cast(NSTableView)view;
973 checkWidget (); 980 checkWidget ();
974 if (indices is null) error (DWT.ERROR_NULL_ARGUMENT); 981 if (indices is null) error (DWT.ERROR_NULL_ARGUMENT);
975 int length = indices.length; 982 int length = indices.length;
976 if (length is 0 || ((style & DWT.SINGLE) !is 0 && length > 1)) return; 983 if (length is 0 || ((style & DWT.SINGLE) !is 0 && length > 1)) return;
977 int count = 0; 984 int count = 0;
978 NSMutableIndexSet indexes = cast(NSMutableIndexSet)new NSMutableIndexSet().alloc().init(); 985 NSMutableIndexSet indexes = cast(NSMutableIndexSet)(new NSMutableIndexSet()).alloc().init();
979 for (int i=0; i<length; i++) { 986 for (int i=0; i<length; i++) {
980 int index = indices [i]; 987 int index = indices [i];
981 if (index >= 0 && index < itemCount) { 988 if (index >= 0 && index < itemCount) {
982 indexes.addIndex (indices [i]); 989 indexes.addIndex (indices [i]);
983 count++; 990 count++;
990 ignoreSelect = false; 997 ignoreSelect = false;
991 } 998 }
992 } 999 }
993 1000
994 void select (int [] ids, int count, bool clear) { 1001 void select (int [] ids, int count, bool clear) {
995 NSMutableIndexSet indexes = cast(NSMutableIndexSet)new NSMutableIndexSet().alloc().init(); 1002 NSMutableIndexSet indexes = cast(NSMutableIndexSet)(new NSMutableIndexSet()).alloc().init();
996 for (int i=0; i<count; i++) indexes.addIndex (ids [i] - 1); //WRONG -1 1003 for (int i=0; i<count; i++) indexes.addIndex (ids [i] - 1); //WRONG -1
997 NSTableView widget = cast(NSTableView)view; 1004 NSTableView widget = cast(NSTableView)view;
998 ignoreSelect = true; 1005 ignoreSelect = true;
999 widget.selectRowIndexes(indexes, !clear); 1006 widget.selectRowIndexes(indexes, !clear);
1000 ignoreSelect = false; 1007 ignoreSelect = false;
1036 } 1043 }
1037 } 1044 }
1038 return result; 1045 return result;
1039 } 1046 }
1040 1047
1041 void setBackground (float [] color) { 1048 void setBackground (CGFloat [] color) {
1042 super.setBackground (color); 1049 super.setBackground (color);
1043 NSColor nsColor; 1050 NSColor nsColor;
1044 if (color is null) { 1051 if (color is null) {
1045 nsColor = null; 1052 nsColor = null;
1046 } else { 1053 } else {
1047 nsColor = NSColor.colorWithDeviceRed (color [0], color [1], color [2], 1); 1054 nsColor = NSColor.colorWithDeviceRed (color [0], color [1], color [2], 1);
1048 } 1055 }
1049 ((NSTableView) view).setBackgroundColor (nsColor); 1056 (cast(NSTableView) view).setBackgroundColor (nsColor);
1050 } 1057 }
1051 1058
1052 void setFont (NSFont font) { 1059 void setFont (NSFont font) {
1053 super.setFont (font); 1060 super.setFont (font);
1054 float ascent = font.ascender (); 1061 CGFloat ascent = font.ascender ();
1055 float descent = -font.descender () + font.leading (); 1062 CGFloat descent = -font.descender () + font.leading ();
1056 ((NSTableView)view).setRowHeight ((int)Math.ceil (ascent + descent) + 1); 1063 (cast(NSTableView)view).setRowHeight (cast(int)Math.ceil (ascent + descent) + 1);
1057 } 1064 }
1058 1065
1059 /** 1066 /**
1060 * Sets the text of the item in the receiver's list at the given 1067 * Sets the text of the item in the receiver's list at the given
1061 * zero-relative index to the string argument. 1068 * zero-relative index to the string argument.
1072 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 1079 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1073 * </ul> 1080 * </ul>
1074 */ 1081 */
1075 public void setItem (int index, String string) { 1082 public void setItem (int index, String string) {
1076 checkWidget(); 1083 checkWidget();
1077 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); 1084 //if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
1078 if (!(0 <= index && index < itemCount)) error (DWT.ERROR_INVALID_RANGE); 1085 if (!(0 <= index && index < itemCount)) error (DWT.ERROR_INVALID_RANGE);
1079 items [index] = string; 1086 items [index] = string;
1080 (cast(NSTableView)view).reloadData(); 1087 (cast(NSTableView)view).reloadData();
1081 } 1088 }
1082 1089
1128 } 1135 }
1129 1136
1130 void setSelection (int index, bool notify) { 1137 void setSelection (int index, bool notify) {
1131 // checkWidget(); 1138 // checkWidget();
1132 if (0 <= index && index < itemCount) { 1139 if (0 <= index && index < itemCount) {
1133 int [] ids = new int [] {index + 1}; 1140 int [] ids = [index + 1];
1134 select (ids, ids.length, true); 1141 select (ids, ids.length, true);
1135 showIndex (index); 1142 showIndex (index);
1136 if (notify) postEvent (DWT.Selection); 1143 if (notify) postEvent (DWT.Selection);
1137 } 1144 }
1138 } 1145 }
1247 String string = items [length - i - 1]; 1254 String string = items [length - i - 1];
1248 if ((style & DWT.SINGLE) !is 0) { 1255 if ((style & DWT.SINGLE) !is 0) {
1249 int index = indexOf (string, 0); 1256 int index = indexOf (string, 0);
1250 if (index !is -1) { 1257 if (index !is -1) {
1251 count = 1; 1258 count = 1;
1252 ids = new int [] {index + 1}; 1259 ids = [index + 1];
1253 } 1260 }
1254 } else { 1261 } else {
1255 int index = 0; 1262 int index = 0;
1256 while ((index = indexOf (string, index)) !is -1) { 1263 while ((index = indexOf (string, index)) !is -1) {
1257 if (count is ids.length) { 1264 if (count is ids.length) {
1308 checkWidget(); 1315 checkWidget();
1309 int index = getSelectionIndex (); 1316 int index = getSelectionIndex ();
1310 if (index >= 0) showIndex (index); 1317 if (index >= 0) showIndex (index);
1311 } 1318 }
1312 1319
1313 void tableViewSelectionDidChange (int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) { 1320 void tableViewSelectionDidChange (objc.id id, objc.SEL sel, objc.id aNotification) {
1314 if (ignoreSelect) return; 1321 if (ignoreSelect) return;
1315 postEvent (DWT.Selection); 1322 postEvent (DWT.Selection);
1316 } 1323 }
1317 1324
1318 bool tableView_shouldEditTableColumn_row(int /*long*/ id, int /*long*/ sel, int /*long*/ aTableView, int /*long*/ aTableColumn, int /*long*/ rowIndex) { 1325 bool tableView_shouldEditTableColumn_row(objc.id id, objc.SEL sel, objc.id aTableView, objc.id aTableColumn, objc.id rowIndex) {
1319 return false; 1326 return false;
1320 } 1327 }
1321 1328
1322 int /*long*/ tableView_objectValueForTableColumn_row(int /*long*/ id, int /*long*/ sel, int /*long*/ aTableView, int /*long*/ aTableColumn, int /*long*/ rowIndex) { 1329 objc.id tableView_objectValueForTableColumn_row(objc.id id, objc.SEL sel, objc.id aTableView, objc.id aTableColumn, objc.id rowIndex) {
1323 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4); 1330 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4);
1324 if (foreground !is null) { 1331 if (foreground !is null) {
1325 NSColor color = NSColor.colorWithDeviceRed(foreground.handle[0], foreground.handle[1], foreground.handle[2], 1); 1332 NSColor color = NSColor.colorWithDeviceRed(foreground.handle[0], foreground.handle[1], foreground.handle[2], 1);
1326 dict.setObject(color, OS.NSForegroundColorAttributeName); 1333 dict.setObject(color, OS.NSForegroundColorAttributeName);
1327 } 1334 }
1328 if (font !is null) { 1335 if (font !is null) {
1329 dict.setObject(font.handle, OS.NSFontAttributeName); 1336 dict.setObject(font.handle, OS.NSFontAttributeName);
1330 } 1337 }
1331 String text = items[(int)/*64*/rowIndex]; 1338 String text = items[cast(size_t)rowIndex];
1332 int length = text.length(); 1339 int length_ = text.length();
1333 char[] chars = new char[length]; 1340 char[] chars = new char[length_];
1334 text.getChars(0, length, chars, 0); 1341 text.getChars(0, length, chars, 0);
1335 NSString str = NSString.stringWithCharacters(chars, length); 1342 NSString str = NSString.stringWithCharacters(chars, length);
1336 NSAttributedString attribStr = (cast(NSAttributedString)new NSAttributedString().alloc()).initWithString_(str, dict); 1343 NSAttributedString attribStr = (cast(NSAttributedString)(new NSAttributedString()).alloc()).initWithString_(str, dict);
1337 return attribStr.id; 1344 return attribStr.id;
1338 } 1345 }
1339 1346
1340 } 1347 }