comparison dwt/widgets/Table.d @ 90:c7f7f4d7091a

All widgets are ported
author Jacob Carlborg <doob@me.com>
date Tue, 30 Dec 2008 18:54:31 +0100
parents b24476d6dedf
children 63a09873578e
comparison
equal deleted inserted replaced
89:8e3c85e1733d 90:c7f7f4d7091a
58 import dwt.internal.cocoa.SWTTableHeaderCell; 58 import dwt.internal.cocoa.SWTTableHeaderCell;
59 import dwt.internal.cocoa.SWTTableHeaderView; 59 import dwt.internal.cocoa.SWTTableHeaderView;
60 import dwt.internal.cocoa.SWTTableView; 60 import dwt.internal.cocoa.SWTTableView;
61 import cocoa = dwt.internal.cocoa.id; 61 import cocoa = dwt.internal.cocoa.id;
62 62
63 import dwt.internal.c.Carbon; 63 import Carbon = dwt.internal.c.Carbon;
64 import dwt.internal.objc.cocoa.Cocoa; 64 import dwt.internal.objc.cocoa.Cocoa;
65 import objc = dwt.internal.objc.runtime; 65 import objc = dwt.internal.objc.runtime;
66 import dwt.widgets.Composite; 66 import dwt.widgets.Composite;
67 import dwt.widgets.Event; 67 import dwt.widgets.Event;
68 import dwt.widgets.TableColumn; 68 import dwt.widgets.TableColumn;
808 } 808 }
809 } 809 }
810 810
811 void drawInteriorWithFrame_inView (objc.id id, objc.SEL sel, objc.id cellFrame, objc.id view) { 811 void drawInteriorWithFrame_inView (objc.id id, objc.SEL sel, objc.id cellFrame, objc.id view) {
812 NSRect rect = NSRect (); 812 NSRect rect = NSRect ();
813 OS.memmove (rect, cellFrame, NSRect.sizeof); 813 OS.memmove (&rect, cellFrame, NSRect.sizeof);
814 814
815 NSTableView tableView = cast(NSTableView)this.view; 815 NSTableView tableView = cast(NSTableView)this.view;
816 NSBrowserCell cell = new NSBrowserCell (id); 816 NSBrowserCell cell = new NSBrowserCell (id);
817 NSRange rowsRange = tableView.rowsInRect (rect); 817 NSRange rowsRange = tableView.rowsInRect (rect);
818 NSUInteger rowIndex = rowsRange.location; 818 NSUInteger rowIndex = rowsRange.location;
821 NSUInteger nsColumnIndex = 0; 821 NSUInteger nsColumnIndex = 0;
822 if (columnCount !is 0) { 822 if (columnCount !is 0) {
823 NSIndexSet columnsSet = tableView.columnIndexesInRect (rect); 823 NSIndexSet columnsSet = tableView.columnIndexesInRect (rect);
824 nsColumnIndex = columnsSet.firstIndex (); 824 nsColumnIndex = columnsSet.firstIndex ();
825 NSArray nsColumns = tableView.tableColumns (); 825 NSArray nsColumns = tableView.tableColumns ();
826 id nsColumn = nsColumns.objectAtIndex (nsColumnIndex); 826 cocoa.id nsColumn = nsColumns.objectAtIndex (nsColumnIndex);
827 for (int i = 0; i < columnCount; i++) { 827 for (int i = 0; i < columnCount; i++) {
828 if (columns[i].nsColumn.id is nsColumn.id) { 828 if (columns[i].nsColumn.id is nsColumn.id) {
829 columnIndex = indexOf (columns[i]); 829 columnIndex = indexOf (columns[i]);
830 break; 830 break;
831 } 831 }
865 NSSize spacing = tableView.intercellSpacing (); 865 NSSize spacing = tableView.intercellSpacing ();
866 fullRect.width = rect.width + spacing.width; 866 fullRect.width = rect.width + spacing.width;
867 } 867 }
868 868
869 if (hooks (DWT.EraseItem)) { 869 if (hooks (DWT.EraseItem)) {
870 NSRect eraseItemRect = null; 870 NSRect eraseItemRect;
871 // TODO how to handle rearranged columns? The third clause below ensures that 871 // TODO how to handle rearranged columns? The third clause below ensures that
872 // there are either 0 columns or that column 0 is still the first physical column. 872 // there are either 0 columns or that column 0 is still the first physical column.
873 if (columnIndex is 0 && (style & DWT.CHECK) !is 0 && (columnCount is 0 || tableView.columnWithIdentifier (columns[0].nsColumn) is 1)) { 873 if (columnIndex is 0 && (style & DWT.CHECK) !is 0 && (columnCount is 0 || tableView.columnWithIdentifier (columns[0].nsColumn) is 1)) {
874 eraseItemRect = NSRect (); 874 eraseItemRect = NSRect ();
875 eraseItemRect.y = fullRect.y; 875 eraseItemRect.y = fullRect.y;
877 eraseItemRect.height = fullRect.height; 877 eraseItemRect.height = fullRect.height;
878 } else { 878 } else {
879 eraseItemRect = fullRect; 879 eraseItemRect = fullRect;
880 } 880 }
881 GCData data = new GCData (); 881 GCData data = new GCData ();
882 data.paintRect = eraseItemRect; 882 data.paintRectStruct = eraseItemRect;
883 data.paintRect = &data.paintRectStruct;
883 GC gc = GC.cocoa_new (this, data); 884 GC gc = GC.cocoa_new (this, data);
884 gc.setFont (item.getFont (columnIndex)); 885 gc.setFont (item.getFont (columnIndex));
885 if (isSelected) { 886 if (isSelected) {
886 CGFloat[] components = [nsSelectionForeground.numberOfComponents ()]; 887 Carbon.CGFloat[] components = [nsSelectionForeground.numberOfComponents ()];
887 nsSelectionForeground.getComponents (components); 888 nsSelectionForeground.getComponents (components.ptr);
888 Color selectionForeground = Color.cocoa_new (display, components); 889 Color selectionForeground = Color.cocoa_new (display, components);
889 gc.setForeground (selectionForeground); 890 gc.setForeground (selectionForeground);
890 components = [nsSelectionBackground.numberOfComponents ()]; 891 components = [nsSelectionBackground.numberOfComponents ()];
891 nsSelectionBackground.getComponents (components); 892 nsSelectionBackground.getComponents (components.ptr);
892 Color selectionBackground = Color.cocoa_new (display, components); 893 Color selectionBackground = Color.cocoa_new (display, components);
893 gc.setBackground (selectionBackground); 894 gc.setBackground (selectionBackground);
894 } else { 895 } else {
895 gc.setForeground (item.getForeground (columnIndex)); 896 gc.setForeground (item.getForeground (columnIndex));
896 gc.setBackground (item.getBackground (columnIndex)); 897 gc.setBackground (item.getBackground (columnIndex));
926 selectionRect.x = columnRect.x; selectionRect.width = columnRect.width; 927 selectionRect.x = columnRect.x; selectionRect.width = columnRect.width;
927 } else { 928 } else {
928 NSRect rowRect = tableView.rectOfRow (rowIndex); 929 NSRect rowRect = tableView.rectOfRow (rowIndex);
929 if ((style & DWT.CHECK) !is 0) { 930 if ((style & DWT.CHECK) !is 0) {
930 /* highlighting at this stage draws over the checkbox, so don't include its column */ 931 /* highlighting at this stage draws over the checkbox, so don't include its column */
931 CGFloat checkWidth = checkColumn.width (); 932 Carbon.CGFloat checkWidth = checkColumn.width ();
932 selectionRect.x = checkWidth; 933 selectionRect.x = checkWidth;
933 selectionRect.width = rowRect.width - checkWidth; 934 selectionRect.width = rowRect.width - checkWidth;
934 } else { 935 } else {
935 selectionRect.width = rowRect.width; 936 selectionRect.width = rowRect.width;
936 } 937 }
940 } 941 }
941 942
942 if (drawBackground && !drawSelection) { 943 if (drawBackground && !drawSelection) {
943 NSGraphicsContext context = NSGraphicsContext.currentContext (); 944 NSGraphicsContext context = NSGraphicsContext.currentContext ();
944 context.saveGraphicsState (); 945 context.saveGraphicsState ();
945 CGFloat[] colorRGB = background.handle; 946 Carbon.CGFloat[] colorRGB = background.handle;
946 NSColor color = NSColor.colorWithDeviceRed (colorRGB[0], colorRGB[1], colorRGB[2], 1f); 947 NSColor color = NSColor.colorWithDeviceRed (colorRGB[0], colorRGB[1], colorRGB[2], 1f);
947 color.setFill (); 948 color.setFill ();
948 NSBezierPath.fillRect (fullRect); 949 NSBezierPath.fillRect (fullRect);
949 context.restoreGraphicsState (); 950 context.restoreGraphicsState ();
950 } 951 }
964 if (columnIndex is 0 && (style & DWT.CHECK) !is 0 && (columnCount is 0 || tableView.columnWithIdentifier (columns[0].nsColumn) is 1)) { 965 if (columnIndex is 0 && (style & DWT.CHECK) !is 0 && (columnCount is 0 || tableView.columnWithIdentifier (columns[0].nsColumn) is 1)) {
965 NSRect gcRect = NSRect (); 966 NSRect gcRect = NSRect ();
966 gcRect.y = fullRect.y; 967 gcRect.y = fullRect.y;
967 gcRect.width = fullRect.x + fullRect.width; 968 gcRect.width = fullRect.x + fullRect.width;
968 gcRect.height = fullRect.height; 969 gcRect.height = fullRect.height;
969 data.paintRect = gcRect; 970 data.paintRectStruct = gcRect;
971 data.paintRect = &data.paintRectStruct;
970 } else { 972 } else {
971 data.paintRect = fullRect; 973 data.paintRectStruct = fullRect;
974 data.paintRect = &data.paintRectStruct;
972 } 975 }
973 GC gc = GC.cocoa_new (this, data); 976 GC gc = GC.cocoa_new (this, data);
974 gc.setFont (item.getFont (columnIndex)); 977 gc.setFont (item.getFont (columnIndex));
975 if (isSelected) { 978 if (isSelected) {
976 CGFloat[] components = [nsSelectionForeground.numberOfComponents ()]; 979 Carbon.CGFloat[] components = [nsSelectionForeground.numberOfComponents ()];
977 nsSelectionForeground.getComponents (components); 980 nsSelectionForeground.getComponents (components.ptr);
978 Color selectionForeground = Color.cocoa_new (display, components); 981 Color selectionForeground = Color.cocoa_new (display, components);
979 gc.setForeground (selectionForeground); 982 gc.setForeground (selectionForeground);
980 components = [nsSelectionBackground.numberOfComponents ()]; 983 components = [nsSelectionBackground.numberOfComponents ()];
981 nsSelectionBackground.getComponents (components); 984 nsSelectionBackground.getComponents (components.ptr);
982 Color selectionBackground = Color.cocoa_new (display, components); 985 Color selectionBackground = Color.cocoa_new (display, components);
983 gc.setBackground (selectionBackground); 986 gc.setBackground (selectionBackground);
984 gc.setBackground (display.getSystemColor (DWT.COLOR_GREEN)); 987 gc.setBackground (display.getSystemColor (DWT.COLOR_GREEN));
985 } else { 988 } else {
986 gc.setForeground (item.getForeground (columnIndex)); 989 gc.setForeground (item.getForeground (columnIndex));
1453 return new int [0]; 1456 return new int [0];
1454 } 1457 }
1455 NSIndexSet selection = widget.selectedRowIndexes(); 1458 NSIndexSet selection = widget.selectedRowIndexes();
1456 NSUInteger count = selection.count(); 1459 NSUInteger count = selection.count();
1457 NSUInteger [] indices = new NSUInteger [count]; 1460 NSUInteger [] indices = new NSUInteger [count];
1458 selection.getIndexes(indices, count, 0); 1461 selection.getIndexes(indices.ptr, count, null);
1459 int [] result = new int [count]; 1462 int [] result = new int [count];
1460 for (int i = 0; i < indices.length; i++) { 1463 for (int i = 0; i < indices.length; i++) {
1461 result [i] = cast(int)/*64*/indices [i]; 1464 result [i] = cast(int)/*64*/indices [i];
1462 } 1465 }
1463 return result; 1466 return result;
1952 ignoreSelect = true; 1955 ignoreSelect = true;
1953 widget.selectAll(null); 1956 widget.selectAll(null);
1954 ignoreSelect = false; 1957 ignoreSelect = false;
1955 } 1958 }
1956 1959
1957 void setBackground (CGFloat [] color) { 1960 void setBackground (Carbon.CGFloat [] color) {
1958 super.setBackground (color); 1961 super.setBackground (color);
1959 NSColor nsColor; 1962 NSColor nsColor;
1960 if (color is null) { 1963 if (color is null) {
1961 nsColor = null; 1964 nsColor = null;
1962 } else { 1965 } else {
2345 public void setSelection (TableItem [] items) { 2348 public void setSelection (TableItem [] items) {
2346 checkWidget (); 2349 checkWidget ();
2347 if (items is null) error (DWT.ERROR_NULL_ARGUMENT); 2350 if (items is null) error (DWT.ERROR_NULL_ARGUMENT);
2348 //TODO - optimize to use expand flag 2351 //TODO - optimize to use expand flag
2349 deselectAll (); 2352 deselectAll ();
2350 int length = items.length; 2353 int length_ = items.length;
2351 if (length is 0 || ((style & DWT.SINGLE) !is 0 && length > 1)) return; 2354 if (length_ is 0 || ((style & DWT.SINGLE) !is 0 && length_ > 1)) return;
2352 int [] indices = new int [length]; 2355 int [] indices = new int [length_];
2353 int count = 0; 2356 int count = 0;
2354 for (int i=0; i<length; i++) { 2357 for (int i=0; i<length_; i++) {
2355 int index = indexOf (items [length - i - 1]); 2358 int index = indexOf (items [length_ - i - 1]);
2356 if (index !is -1) { 2359 if (index !is -1) {
2357 indices [count++] = index; 2360 indices [count++] = index;
2358 } 2361 }
2359 } 2362 }
2360 if (count > 0) { 2363 if (count > 0) {
2549 } 2552 }
2550 2553
2551 void tableViewColumnDidMove (objc.id id, objc.SEL sel, objc.id aNotification) { 2554 void tableViewColumnDidMove (objc.id id, objc.SEL sel, objc.id aNotification) {
2552 NSNotification notification = new NSNotification (aNotification); 2555 NSNotification notification = new NSNotification (aNotification);
2553 NSDictionary userInfo = notification.userInfo (); 2556 NSDictionary userInfo = notification.userInfo ();
2554 id nsOldIndex = userInfo.valueForKey (NSString.stringWith ("NSOldColumn")); //$NON-NLS-1$ 2557 cocoa.id nsOldIndex = userInfo.valueForKey (NSString.stringWith ("NSOldColumn")); //$NON-NLS-1$
2555 id nsNewIndex = userInfo.valueForKey (NSString.stringWith ("NSNewColumn")); //$NON-NLS-1$ 2558 cocoa.id nsNewIndex = userInfo.valueForKey (NSString.stringWith ("NSNewColumn")); //$NON-NLS-1$
2556 int oldIndex = (new NSNumber (nsOldIndex)).intValue (); 2559 int oldIndex = (new NSNumber (nsOldIndex)).intValue ();
2557 int newIndex = (new NSNumber (nsNewIndex)).intValue (); 2560 int newIndex = (new NSNumber (nsNewIndex)).intValue ();
2558 int startIndex = Math.min (oldIndex, newIndex); 2561 int startIndex = Math.min (oldIndex, newIndex);
2559 int endIndex = Math.max (oldIndex, newIndex); 2562 int endIndex = Math.max (oldIndex, newIndex);
2560 NSTableView tableView = cast(NSTableView)view; 2563 NSTableView tableView = cast(NSTableView)view;
2570 } 2573 }
2571 2574
2572 void tableViewColumnDidResize (objc.id id, objc.SEL sel, objc.id aNotification) { 2575 void tableViewColumnDidResize (objc.id id, objc.SEL sel, objc.id aNotification) {
2573 NSNotification notification = new NSNotification (aNotification); 2576 NSNotification notification = new NSNotification (aNotification);
2574 NSDictionary userInfo = notification.userInfo (); 2577 NSDictionary userInfo = notification.userInfo ();
2575 id columnId = userInfo.valueForKey (NSString.stringWith ("NSTableColumn")); //$NON-NLS-1$ 2578 cocoa.id columnId = userInfo.valueForKey (NSString.stringWith ("NSTableColumn")); //$NON-NLS-1$
2576 TableColumn column = getColumn (columnId); 2579 TableColumn column = getColumn (columnId);
2577 if (column is null) return; /* either CHECK column or firstColumn in 0-column Table */ 2580 if (column is null) return; /* either CHECK column or firstColumn in 0-column Table */
2578 2581
2579 column.sendEvent (DWT.Resize); 2582 column.sendEvent (DWT.Resize);
2580 if (isDisposed ()) return; 2583 if (isDisposed ()) return;
2609 postEvent (DWT.Selection, event); 2612 postEvent (DWT.Selection, event);
2610 } 2613 }
2611 } 2614 }
2612 2615
2613 void tableView_didClickTableColumn (objc.id id, objc.SEL sel, objc.id tableView, objc.id tableColumn) { 2616 void tableView_didClickTableColumn (objc.id id, objc.SEL sel, objc.id tableView, objc.id tableColumn) {
2614 TableColumn column = getColumn (new id (tableColumn)); 2617 TableColumn column = getColumn (new cocoa.id (tableColumn));
2615 column.postEvent (DWT.Selection); 2618 column.postEvent (DWT.Selection);
2616 } 2619 }
2617 2620
2618 objc.id tableView_objectValueForTableColumn_row (objc.id id, objc.SEL sel, objc.id aTableView, objc.id aTableColumn, objc.id rowIndex) { 2621 objc.id tableView_objectValueForTableColumn_row (objc.id id, objc.SEL sel, objc.id aTableView, objc.id aTableColumn, objc.id rowIndex) {
2619 TableItem item = _getItem (cast(int)/*64*/rowIndex); 2622 TableItem item = _getItem (cast(int)/*64*/rowIndex);
2642 event.detail = DWT.CHECK; 2645 event.detail = DWT.CHECK;
2643 event.item = item; 2646 event.item = item;
2644 event.index = cast(int)/*64*/rowIndex; 2647 event.index = cast(int)/*64*/rowIndex;
2645 postEvent (DWT.Selection, event); 2648 postEvent (DWT.Selection, event);
2646 NSTableView tableView = cast(NSTableView)view; 2649 NSTableView tableView = cast(NSTableView)view;
2647 NSRect rect = tableView.rectOfRow (rowIndex); 2650 NSRect rect = tableView.rectOfRow (cast(NSInteger)rowIndex);
2648 tableView.setNeedsDisplayInRect (rect); 2651 tableView.setNeedsDisplayInRect (rect);
2649 } 2652 }
2650 } 2653 }
2651 2654
2652 bool tableView_shouldEditTableColumn_row (objc.id id, objc.SEL sel, objc.id aTableView, objc.id aTableColumn, objc.id rowIndex) { 2655 bool tableView_shouldEditTableColumn_row (objc.id id, objc.SEL sel, objc.id aTableView, objc.id aTableColumn, objc.id rowIndex) {
2670 cell.setLeaf (true); 2673 cell.setLeaf (true);
2671 2674
2672 if (hooks (DWT.MeasureItem)) { 2675 if (hooks (DWT.MeasureItem)) {
2673 NSTableView tableView = cast(NSTableView)this.view; 2676 NSTableView tableView = cast(NSTableView)this.view;
2674 NSInteger nsColumnIndex = tableView.columnWithIdentifier (new cocoa.id (aTableColumn)); 2677 NSInteger nsColumnIndex = tableView.columnWithIdentifier (new cocoa.id (aTableColumn));
2675 NSRect rect = tableView.frameOfCellAtColumn (nsColumnIndex, rowIndex); 2678 NSRect rect = tableView.frameOfCellAtColumn (nsColumnIndex, cast(NSInteger) rowIndex);
2676 NSRect contentRect = cell.titleRectForBounds (rect); 2679 NSRect contentRect = cell.titleRectForBounds (rect);
2677 NSSize contentSize = cell.cellSizeForBounds (rect); 2680 NSSize contentSize = cell.cellSizeForBounds (rect);
2678 2681
2679 GCData data = new GCData (); 2682 GCData data = new GCData ();
2680 data.paintRect = tableView.frame (); 2683 data.paintRectStruct = tableView.frame ();
2684 data.paintRect = &data.paintRectStruct;
2681 GC gc = GC.cocoa_new (this, data); 2685 GC gc = GC.cocoa_new (this, data);
2682 gc.setFont (item.getFont (columnIndex)); 2686 gc.setFont (item.getFont (columnIndex));
2683 int rowHeight = cast(int)tableView.rowHeight (); 2687 int rowHeight = cast(int)tableView.rowHeight ();
2684 Event event = new Event (); 2688 Event event = new Event ();
2685 event.item = item; 2689 event.item = item;