Re: Repositioning the Field Editor [solved]
Re: Repositioning the Field Editor [solved]
- Subject: Re: Repositioning the Field Editor [solved]
- From: "K. Darcy Otto" <email@hidden>
- Date: Thu, 30 Apr 2009 19:16:52 -0700
Thanks to this suggestion:
http://www.cocoabuilder.com/archive/message/cocoa/2009/4/30/235760
I have been able to solve this long-standing problem. The trick with
repositioning the field editor, at least within an NSTableView, is to
create an NSTextFieldCell subclass, and set the table cell in IB to be
an object of that sort. Then, implement the following delegates:
// NSTableView may call either selectWithFrame: or editWithFrame:,
depending on how editing is invoked. In ecah case, call
setFrame:inView:editor:delegate: in order to set the rect to avoid
scope lines (pushing it to the left, and squeezing it a comparable
amount from the right in order to still fit in the cell.
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
{
NSRect theRect = [self setFrame:aRect inView:controlView
editor:textObj delegate:anObject];
[super editWithFrame:theRect inView:controlView editor:textObj
delegate:anObject event:theEvent];
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject start:
(NSInteger)selStart length:(NSInteger)selLength
{
NSRect theRect = [self setFrame:aRect inView:controlView
editor:textObj delegate:anObject];
[super selectWithFrame:theRect inView:controlView editor:textObj
delegate:anObject start:selStart length:selLength];
}
All -setFrame:inView:editor:delegate does is return the desired rect,
in a new position.
On 29-Apr-09, at 9:58 PM, K. Darcy Otto wrote:
So, I have continued my work with a subclass of NSTextView for my
field editor, and here is another part of the puzzle: (i) if I
implement -drawRect: in the subclass, and ask it for the frame, the
frame is the same as the frame set in -viewWillDraw, but not acted
upon; (ii) if I implement -drawRect: in the subclass, but do not
call super, the field editor box is still drawn at the usual
location (the text is not drawn properly, etc., but the box is).
This leads me to think that -drawRect is not drawing the box for the
field editor. The question is, what object is drawing that box, and
is there a way to move it?
On 28-Apr-09, at 12:47 PM, K. Darcy Otto wrote:
Continuing on with the mystery, I have tried the following code in
my NSTableView subclass (below, not yet solved though). I have got
it so that the rect returned by [fieldEditor frame] is not empty,
but gives exactly the frame it should. Still though, modifying the
frame to something different makes no difference. Is there a way
to prompt the field editor to redraw itself in accordance with the
new frame?
In the documentation for -editColumn:row:withEvent:select:, I
noticed that it "sends -
selectWithFrame:inView:editor:delegate:start:length: and -
editWithFrame:inView:editor:delegate:event: to the field editor’s
NSCell object with the NSTableView as the text delegate. So my
call to super is doing that. Could it be that I need to intervene
somehow in these NSCell methods?
-(void)editColumn:(NSInteger)columnIndex row:(NSInteger)rowIndex
withEvent:(NSEvent *)theEvent select:(BOOL)flag
{
[super editColumn:columnIndex row:rowIndex withEvent:theEvent
select:flag];
NSTextView *fieldEditor = (NSTextView *)[[self window]
fieldEditor:YES forObject:nil];
// Change the text colour to red, just to confirm I've got ahold
of the actual field editor that is being used (I do)
[fieldEditor setTextColor:[NSColor redColor]];
// Get the field editor frame (it is the correct frame)
NSRect cellRect = [fieldEditor frame];
NSLog(@"1. fieldEditor x origin: %f",cellRect.origin.x); //
output: 127
// Change cellRect x origin, but don't extend the field width
cellRect.origin.x += 20;
cellRect.size.width -= 20;
// Set fieldEditor frame to new cellRect
[fieldEditor setFrame:cellRect];
[fieldEditor setNeedsDisplay:YES];
cellRect = [fieldEditor frame];
NSLog(@"2. fieldEditor x origin: %f",cellRect.origin.x); //
output: 147
}
On 27-Apr-09, at 7:19 PM, K. Darcy Otto wrote:
I have a field editor which I need to reposition in my tableView –
specifically, I need to move it a few pixels to the right. The
following post:
http://www.cocoabuilder.com/archive/message/cocoa/2009/3/16/232513
makes two suggestions: (i) implement the move in -viewWillDraw in
the field editor subclass, and (ii) reposition the field editor
after calling super in -editColumn:row:withEvent:select: in the
tableView subclass. Neither of these seem to make any difference
to where the field editor is drawn. Here is my code with respect
to (i), contained in the field editor subclass:
-(void)viewWillDraw
{
NSRect frame = [self frame];
NSLog(@"Old: %f",frame.origin.x);
frame.origin.x += 50.0;
[self setFrame:frame];
frame = [self frame];
NSLog(@"New: %f",frame.origin.x);
[super viewWillDraw];
}
Now, I can confirm this method is being called, and by means of
the NSLog statements, the frame is being changed prior to the call
to super. Unfortunately, the field editor is drawn where is
usually is drawn, with no shift in place.
With respect to (ii), contained in the tableView subclass:
-(void)editColumn:(NSInteger)columnIndex row:(NSInteger)rowIndex
withEvent:(NSEvent *)theEvent select:(BOOL)flag
{
[super editColumn:columnIndex row:rowIndex withEvent:theEvent
select:flag];
NSText *fieldEditor = [[self window] fieldEditor:YES
forObject:self];
NSRect fieldEditorFrame = [fieldEditor frame];
fieldEditorFrame.origin.x += 50.0;
[fieldEditor setFrame:fieldEditorFrame];
}
Here, it turns out that even though fieldEditor points to the
custom field editor object, fieldEditorFrame turns out to be
empty. And again, the field editor is drawn where it is usually
drawn, with no shift in place.
Any help would be greatly appreciated.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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