comparison dwt/widgets/TreeColumn.d @ 88:6c56264306a6

Ported dwt.widgets.TreeColumn
author Jacob Carlborg <doob@me.com>
date Tue, 30 Dec 2008 16:44:02 +0100
parents d8635bb48c7c
children 8e3c85e1733d
comparison
equal deleted inserted replaced
87:4e8317766ba0 88:6c56264306a6
33 import dwt.internal.cocoa.NSTableHeaderCell; 33 import dwt.internal.cocoa.NSTableHeaderCell;
34 import dwt.internal.cocoa.NSTableHeaderView; 34 import dwt.internal.cocoa.NSTableHeaderView;
35 import dwt.internal.cocoa.NSView; 35 import dwt.internal.cocoa.NSView;
36 import dwt.internal.cocoa.OS; 36 import dwt.internal.cocoa.OS;
37 37
38 import dwt.internal.objc.cocoa.Cocoa;
39 import objc = dwt.internal.objc.runtime;
40 import dwt.widgets.Item;
41 import dwt.widgets.Tree;
42 import dwt.widgets.TypedListener;
43
38 /** 44 /**
39 * Instances of this class represent a column in a tree widget. 45 * Instances of this class represent a column in a tree widget.
40 * <p><dl> 46 * <p><dl>
41 * <dt><b>Styles:</b></dt> 47 * <dt><b>Styles:</b></dt>
42 * <dd>LEFT, RIGHT, CENTER</dd> 48 * <dd>LEFT, RIGHT, CENTER</dd>
57 public class TreeColumn : Item { 63 public class TreeColumn : Item {
58 NSTableColumn nsColumn; 64 NSTableColumn nsColumn;
59 Tree parent; 65 Tree parent;
60 String toolTipText, displayText; 66 String toolTipText, displayText;
61 67
62 static final int MARGIN = 2; 68 static const int MARGIN = 2;
63 69
64 /** 70 /**
65 * Constructs a new instance of this class given its parent 71 * Constructs a new instance of this class given its parent
66 * (which must be a <code>Tree</code>) and a style value 72 * (which must be a <code>Tree</code>) and a style value
67 * describing its behavior and appearance. The item is added 73 * describing its behavior and appearance. The item is added
217 void destroyWidget () { 223 void destroyWidget () {
218 parent.destroyItem (this); 224 parent.destroyItem (this);
219 releaseHandle (); 225 releaseHandle ();
220 } 226 }
221 227
222 void drawInteriorWithFrame_inView (int /*long*/ id, int /*long*/ sel, int /*long*/ cellFrame, int /*long*/ view) { 228 void drawInteriorWithFrame_inView (objc.id id, objc.SEL sel, objc.id cellFrame, objc.id view) {
223 NSRect cellRect = new NSRect (); 229 NSRect cellRect = NSRect ();
224 OS.memmove (cellRect, cellFrame, NSRect.sizeof); 230 OS.memmove (&cellRect, cellFrame, NSRect.sizeof);
225 231
226 /* 232 /*
227 * Feature in Cocoa. When the last column in a tree does not reach the 233 * Feature in Cocoa. When the last column in a tree does not reach the
228 * rightmost edge of the tree view, the cell that draws the rightmost- 234 * rightmost edge of the tree view, the cell that draws the rightmost-
229 * column's header is also invoked to draw the header space between its 235 * column's header is also invoked to draw the header space between its
230 * right edge and the tree's right edge. If this case is detected then 236 * right edge and the tree's right edge. If this case is detected then
231 * nothing should be drawn. 237 * nothing should be drawn.
232 */ 238 */
233 NSOutlineView outlineView = (NSOutlineView)parent.view; 239 NSOutlineView outlineView = cast(NSOutlineView)parent.view;
234 int columnIndex = (int)/*64*/outlineView.columnWithIdentifier (nsColumn); 240 NSInteger columnIndex = outlineView.columnWithIdentifier (nsColumn);
235 NSRect headerRect = parent.headerView.headerRectOfColumn (columnIndex); 241 NSRect headerRect = parent.headerView.headerRectOfColumn (columnIndex);
236 if (headerRect.x !is cellRect.x || headerRect.width !is cellRect.width) return; 242 if (headerRect.x !is cellRect.x || headerRect.width !is cellRect.width) return;
237 243
238 NSGraphicsContext context = NSGraphicsContext.currentContext (); 244 NSGraphicsContext context = NSGraphicsContext.currentContext ();
239 context.saveGraphicsState (); 245 context.saveGraphicsState ();
240 246
241 int contentWidth = 0; 247 int contentWidth = 0;
242 NSSize stringSize = null, imageSize = null; 248 NSSize stringSize, imageSize;
243 NSAttributedString attrString = null; 249 NSAttributedString attrString = null;
244 NSTableHeaderCell headerCell = nsColumn.headerCell (); 250 NSTableHeaderCell headerCell = nsColumn.headerCell ();
245 if (displayText !is null) { 251 if (displayText !is null) {
246 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4); 252 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4);
247 dict.setObject (headerCell.font (), OS.NSFontAttributeName); 253 dict.setObject (headerCell.font (), OS.NSFontAttributeName);
248 NSMutableParagraphStyle paragraphStyle = (NSMutableParagraphStyle)new NSMutableParagraphStyle ().alloc ().init (); 254 NSMutableParagraphStyle paragraphStyle = cast(NSMutableParagraphStyle)(new NSMutableParagraphStyle ()).alloc ().init ();
249 paragraphStyle.autorelease (); 255 paragraphStyle.autorelease ();
250 paragraphStyle.setLineBreakMode (OS.NSLineBreakByClipping); 256 paragraphStyle.setLineBreakMode (OS.NSLineBreakByClipping);
251 dict.setObject (paragraphStyle, OS.NSParagraphStyleAttributeName); 257 dict.setObject (paragraphStyle, OS.NSParagraphStyleAttributeName);
252 NSString string = NSString.stringWith (displayText); 258 NSString string = NSString.stringWith (displayText);
253 attrString = ((NSAttributedString)new NSAttributedString ().alloc ()).initWithString (string, dict); 259 attrString = (cast(NSAttributedString)(new NSAttributedString ()).alloc ()).initWithString (string, dict);
254 stringSize = attrString.size (); 260 stringSize = attrString.size ();
255 contentWidth += Math.ceil (stringSize.width); 261 contentWidth += Math.ceil (stringSize.width);
256 if (image !is null) contentWidth += MARGIN; /* space between image and text */ 262 if (image !is null) contentWidth += MARGIN; /* space between image and text */
257 } 263 }
258 if (image !is null) { 264 if (image !is null) {
268 cellRect.width = Math.max (0, sortRect.x - cellRect.x); 274 cellRect.width = Math.max (0, sortRect.x - cellRect.x);
269 } 275 }
270 276
271 int drawX = 0; 277 int drawX = 0;
272 if ((style & DWT.CENTER) !is 0) { 278 if ((style & DWT.CENTER) !is 0) {
273 drawX = (int)(cellRect.x + Math.max (MARGIN, ((cellRect.width - contentWidth) / 2))); 279 drawX = cast(int)(cellRect.x + Math.max (MARGIN, ((cellRect.width - contentWidth) / 2)));
274 } else if ((style & DWT.RIGHT) !is 0) { 280 } else if ((style & DWT.RIGHT) !is 0) {
275 drawX = (int)(cellRect.x + Math.max (MARGIN, cellRect.width - contentWidth - MARGIN)); 281 drawX = cast(int)(cellRect.x + Math.max (MARGIN, cellRect.width - contentWidth - MARGIN));
276 } else { 282 } else {
277 drawX = (int)cellRect.x + MARGIN; 283 drawX = cast(int)cellRect.x + MARGIN;
278 } 284 }
279 285
280 if (image !is null) { 286 if (image !is null) {
281 NSRect destRect = new NSRect (); 287 NSRect destRect = NSRect ();
282 destRect.x = drawX; 288 destRect.x = drawX;
283 destRect.y = cellRect.y; 289 destRect.y = cellRect.y;
284 destRect.width = Math.min (imageSize.width, cellRect.width - 2 * MARGIN); 290 destRect.width = Math.min (imageSize.width, cellRect.width - 2 * MARGIN);
285 destRect.height = Math.min (imageSize.height, cellRect.height); 291 destRect.height = Math.min (imageSize.height, cellRect.height);
286 bool isFlipped = new NSView (view).isFlipped(); 292 bool isFlipped = (new NSView (view)).isFlipped();
287 if (isFlipped) { 293 if (isFlipped) {
288 context.saveGraphicsState (); 294 context.saveGraphicsState ();
289 NSAffineTransform transform = NSAffineTransform.transform (); 295 NSAffineTransform transform = NSAffineTransform.transform ();
290 transform.scaleXBy (1, -1); 296 transform.scaleXBy (1, -1);
291 transform.translateXBy (0, -(destRect.height + 2 * destRect.y)); 297 transform.translateXBy (0, -(destRect.height + 2 * destRect.y));
292 transform.concat (); 298 transform.concat ();
293 } 299 }
294 NSRect sourceRect = new NSRect (); 300 NSRect sourceRect = NSRect ();
295 sourceRect.width = destRect.width; 301 sourceRect.width = destRect.width;
296 sourceRect.height = destRect.height; 302 sourceRect.height = destRect.height;
297 image.handle.drawInRect (destRect, sourceRect, OS.NSCompositeSourceOver, 1f); 303 image.handle.drawInRect (destRect, sourceRect, OS.NSCompositeSourceOver, 1f);
298 if (isFlipped) context.restoreGraphicsState (); 304 if (isFlipped) context.restoreGraphicsState ();
299 drawX += destRect.width; 305 drawX += destRect.width;
300 } 306 }
301 307
302 if (displayText !is null && displayText.length () > 0) { 308 if (displayText !is null && displayText.length () > 0) {
303 if (image !is null) drawX += MARGIN; /* space between image and text */ 309 if (image !is null) drawX += MARGIN; /* space between image and text */
304 NSRect destRect = new NSRect (); 310 NSRect destRect = NSRect ();
305 destRect.x = drawX; 311 destRect.x = drawX;
306 destRect.y = cellRect.y; 312 destRect.y = cellRect.y;
307 destRect.width = Math.min (stringSize.width, cellRect.x + cellRect.width - MARGIN - drawX); 313 destRect.width = Math.min (stringSize.width, cellRect.x + cellRect.width - MARGIN - drawX);
308 destRect.height = Math.min (stringSize.height, cellRect.height); 314 destRect.height = Math.min (stringSize.height, cellRect.height);
309 attrString.drawInRect (destRect); 315 attrString.drawInRect (destRect);
450 if (displayText !is null) { 456 if (displayText !is null) {
451 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity (4); 457 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity (4);
452 NSTableHeaderCell headerCell = nsColumn.headerCell (); 458 NSTableHeaderCell headerCell = nsColumn.headerCell ();
453 dict.setObject (headerCell.font (), OS.NSFontAttributeName); 459 dict.setObject (headerCell.font (), OS.NSFontAttributeName);
454 NSString string = NSString.stringWith (displayText); 460 NSString string = NSString.stringWith (displayText);
455 NSAttributedString attrString = ((NSAttributedString)new NSAttributedString ().alloc ()).initWithString (string, dict); 461 NSAttributedString attrString = (cast(NSAttributedString)(new NSAttributedString ()).alloc ()).initWithString (string, dict);
456 NSSize stringSize = attrString.size (); 462 NSSize stringSize = attrString.size ();
457 attrString.release (); 463 attrString.release ();
458 width += Math.ceil (stringSize.width); 464 width += Math.ceil (stringSize.width);
459 if (image !is null) width += MARGIN; /* space between image and text */ 465 if (image !is null) width += MARGIN; /* space between image and text */
460 } 466 }
462 NSSize imageSize = image.handle.size (); 468 NSSize imageSize = image.handle.size ();
463 width += Math.ceil (imageSize.width); 469 width += Math.ceil (imageSize.width);
464 } 470 }
465 if (parent.sortColumn is this && parent.sortDirection !is DWT.NONE) { 471 if (parent.sortColumn is this && parent.sortDirection !is DWT.NONE) {
466 NSTableHeaderCell headerCell = nsColumn.headerCell (); 472 NSTableHeaderCell headerCell = nsColumn.headerCell ();
467 NSRect rect = new NSRect (); 473 NSRect rect = NSRect ();
468 rect.width = rect.height = Float.MAX_VALUE; 474 rect.width = rect.height = Float.MAX_VALUE;
469 NSSize cellSize = headerCell.cellSizeForBounds (rect); 475 NSSize cellSize = headerCell.cellSizeForBounds (rect);
470 rect.height = cellSize.height; 476 rect.height = cellSize.height;
471 NSRect sortRect = headerCell.sortIndicatorRectForBounds (rect); 477 NSRect sortRect = headerCell.sortIndicatorRectForBounds (rect);
472 width += Math.ceil (sortRect.width); 478 width += Math.ceil (sortRect.width);
577 if ((alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER)) is 0) return; 583 if ((alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER)) is 0) return;
578 int index = parent.indexOf (this); 584 int index = parent.indexOf (this);
579 if (index is -1 || index is 0) return; 585 if (index is -1 || index is 0) return;
580 style &= ~(DWT.LEFT | DWT.RIGHT | DWT.CENTER); 586 style &= ~(DWT.LEFT | DWT.RIGHT | DWT.CENTER);
581 style |= alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER); 587 style |= alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER);
582 NSOutlineView outlineView = ((NSOutlineView) parent.view); 588 NSOutlineView outlineView = (cast(NSOutlineView) parent.view);
583 NSTableHeaderView headerView = outlineView.headerView (); 589 NSTableHeaderView headerView = outlineView.headerView ();
584 if (headerView is null) return; 590 if (headerView is null) return;
585 index = (int)/*64*/outlineView.columnWithIdentifier (nsColumn); 591 NSInteger = outlineView.columnWithIdentifier (nsColumn);
586 NSRect rect = headerView.headerRectOfColumn (index); 592 NSRect rect = headerView.headerRectOfColumn (index);
587 headerView.setNeedsDisplayInRect (rect); 593 headerView.setNeedsDisplayInRect (rect);
588 rect = outlineView.rectOfColumn (index); 594 rect = outlineView.rectOfColumn (index);
589 parent.view.setNeedsDisplayInRect (rect); 595 parent.view.setNeedsDisplayInRect (rect);
590 } 596 }
593 checkWidget(); 599 checkWidget();
594 if (image !is null && image.isDisposed ()) { 600 if (image !is null && image.isDisposed ()) {
595 error (DWT.ERROR_INVALID_ARGUMENT); 601 error (DWT.ERROR_INVALID_ARGUMENT);
596 } 602 }
597 super.setImage (image); 603 super.setImage (image);
598 NSTableHeaderView headerView = ((NSOutlineView) parent.view).headerView (); 604 NSTableHeaderView headerView = (cast(NSOutlineView) parent.view).headerView ();
599 if (headerView is null) return; 605 if (headerView is null) return;
600 int /*long*/ index = ((NSOutlineView)parent.view).columnWithIdentifier (nsColumn); 606 NSInteger index = (cast(NSOutlineView)parent.view).columnWithIdentifier (nsColumn);
601 NSRect rect = headerView.headerRectOfColumn (index); 607 NSRect rect = headerView.headerRectOfColumn (index);
602 headerView.setNeedsDisplayInRect (rect); 608 headerView.setNeedsDisplayInRect (rect);
603 } 609 }
604 610
605 /** 611 /**
625 */ 631 */
626 public void setMoveable (bool moveable) { 632 public void setMoveable (bool moveable) {
627 checkWidget (); 633 checkWidget ();
628 // TODO how to make only some columns movable? And handle moveable is false. 634 // TODO how to make only some columns movable? And handle moveable is false.
629 if (moveable) { 635 if (moveable) {
630 ((NSOutlineView)parent.view).setAllowsColumnReordering (true); 636 (cast(NSOutlineView)parent.view).setAllowsColumnReordering (true);
631 } 637 }
632 // int [] flags = new int [1]; 638 // int [] flags = new int [1];
633 // OS.GetDataBrowserPropertyFlags (parent.handle, id, flags); 639 // OS.GetDataBrowserPropertyFlags (parent.handle, id, flags);
634 // if (moveable) { 640 // if (moveable) {
635 // flags [0] |= OS.kDataBrowserListViewMovableColumn; 641 // flags [0] |= OS.kDataBrowserListViewMovableColumn;
661 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); 667 if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
662 super.setText (string); 668 super.setText (string);
663 char [] buffer = new char [text.length ()]; 669 char [] buffer = new char [text.length ()];
664 text.getChars (0, buffer.length, buffer, 0); 670 text.getChars (0, buffer.length, buffer, 0);
665 int length = fixMnemonic (buffer); 671 int length = fixMnemonic (buffer);
666 displayText = new String (buffer, 0, length); 672 displayText = new_String (buffer, 0, length);
667 NSString title = NSString.stringWith (displayText); 673 NSString title = NSString.stringWith (displayText);
668 nsColumn.headerCell ().setTitle (title); 674 nsColumn.headerCell ().setTitle (title);
669 NSTableHeaderView headerView = ((NSOutlineView) parent.view).headerView (); 675 NSTableHeaderView headerView = (cast(NSOutlineView) parent.view).headerView ();
670 if (headerView is null) return; 676 if (headerView is null) return;
671 int /*long*/ index = ((NSOutlineView)parent.view).columnWithIdentifier (nsColumn); 677 NSInteger index = (cast(NSOutlineView)parent.view).columnWithIdentifier (nsColumn);
672 NSRect rect = headerView.headerRectOfColumn (index); 678 NSRect rect = headerView.headerRectOfColumn (index);
673 headerView.setNeedsDisplayInRect (rect); 679 headerView.setNeedsDisplayInRect (rect);
674 } 680 }
675 681
676 /** 682 /**