Re: [Moderator] Drawing from lower left, why?
Re: [Moderator] Drawing from lower left, why?
- Subject: Re: [Moderator] Drawing from lower left, why?
- From: Ken Tozier <email@hidden>
- Date: Fri, 15 Jun 2007 20:06:45 -0400
On Jun 15, 2007, at 7:36 PM, cocoa-dev-admins wrote:
Please stay on topic.
Coordinate system "design" is not specific to Cocoa.
If you are having issues that are directly relevant to Cocoa
application development, then please describe the problems and
solicit solutions. Posting a rant is not constructive and makes it
more difficult for others to identify your problem and offer
solutions.
Per the List Guidelines:
When you ask a question, whenever possible please:
1. Describe what your high-level goal is
High level, I wrote a custom matrix class with dynamically resizable
page thumbnail cells (similar to iPhoto's thumbnail view) The matrix
has an optional title which classifies what types of pages a user is
looking at (unstarted with ads, started, sent to prepress etc). The
cells draw from the upper left and flow down the page wrapping to
another line the same way text in a text view does.
2. Include any relevant code
Here are the cell origin calculation/drawing methods. I'm not
entirely sure which part or parts are interacting to produce the
vertical placement errors but I suspect one major part is in the "y"
calculation of the "rectForCell" method.
- (BOOL) isFlipped
{
return YES;
}
- (void) drawRect:(NSRect) rect
{
NSGraphicsContext *context = [NSGraphicsContext currentContext];
[context saveGraphicsState];
[[NSColor greenColor] set];
NSRectFillUsingOperation(rect, NSCompositeSourceOver);
titleRect.size.width = rect.size.width;
if (title != nil)
[self drawTitle];
[self drawCells];
[context restoreGraphicsState];
}
- (void) drawTitle
{
if (title != nil)
{
attrSize = [title size];
if (attrSize.width >= titleRect.size.width)
{
attrSize.width = titleRect.size.width - 20;
attrSize.height += psize.height;
}
attrSize.height = (attrSize.height > titleRect.size.height) ?
titleRect.size.height : attrSize.height ;
titleTextRect.origin.x = (titleRect.size.width - attrSize.width) / 2;
titleTextRect.origin.y = titleRect.origin.y + attrSize.height / 9;
titleTextRect.size.width = attrSize.width;
titleTextRect.size.height = attrSize.height;
// draw text into context
[[NSColor redColor] set]; // <- for testing
NSRectFillUsingOperation(titleRect, NSCompositeSourceOver); // <-
for testing
[title drawInRect: titleTextRect];
}
}
- (void) drawCells
{
if ([cells count] > 0)
{
NSEnumerator *enumerator = [cells objectEnumerator];
id cell;
while (cell = [enumerator nextObject])
{
[backColor set];
[self drawCell: cell];
}
}
}
- (void) drawCell:(XTPageView *) inCell
{
NSRect cellFrame = [self rectForCell: inCell];
[inCell drawAtPoint: cellFrame.origin
scale: drawScale];
}
// This method Calculates the rectangle for a given cell
- (NSRect) rectForCell:(id) inCell
{
float vOffset = titleRect.origin.y + titleRect.size.height +
verticalSpacing; // <- adding this to y kicks it too far below the
title bar
int cellIndex = [cells indexOfObject: inCell],
row = cellIndex / cellsPerRow,
column = cellIndex - row * cellsPerRow, // NOTE: this doesn't
always equal zero because it's mod division
x = margin + column * cellWidth + horizontalSpacing * column,
// Next line is where I suspect the main problem is
// This places the cells in the right place but I think it's just
a luck coincidence because
// it doesn't account for the depth of the title. I add 1 to "row"
index because otherwise
// cellHeight * (row + 1) would be zero for the entire top row of
cells. What I want is to rework
// this calculation so I don't need to do the (row + 1) and
account for the title depth
y = cellHeight * (row + 1) + verticalSpacing;
return NSMakeRect(x, y, cellWidth, cellHeight);
}
3. If appropriate, include URLs to screenshots
For more details, see:
<http://www.catb.org/~esr/faqs/smart-questions.html>
and
<http://www.mikeash.com/getting_answers.html>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden