• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: field editor oddness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: field editor oddness


  • Subject: Re: field editor oddness
  • From: Tom Waters <email@hidden>
  • Date: Wed, 27 Jun 2001 11:27:14 -0700

I've asked for help on field editors on this list a couple of times without any response... I thought if i distilled the issue down to a small test case and provided complete source, then someone might be kind enough to take a look at it for me...

Run this app, and you can click and drag around the text and double-click it to start editing.

Also, while writing this small example for y'all I ran across another thing that seems like a bug.

It sure seems like drawAtPoint: doesn't pay attention to negative coordinates... if you run this example and drag around the "Hello World" text, you'll see that when it overlaps with the left or top edge, the text refuses to draw anywhere but at zero, even though you can see the coords of where i'm drawing it in the lower left.

Plus, as the editor resizes itself (incorrectly i might add), it seems like it never causes MyView to repaint behind it...




Make a new cocoa project.
Edit the window to have a custom view.
Make a subclass of NSView called MyView.
Set the view's custom class to MyView.
in PB, but the following code in MyView.m

#import <Cocoa/Cocoa.h>

@interface MyView : NSView
{
NSRect itemRect;
NSString *itemTitle;
NSSize maxSize;
id editor;
}
@end

@implementation MyView

- (BOOL)isFlipped
{
return YES;
}

- (BOOL)isOpaque
{
return NO;
}

- (void)setString:(NSString *)s
{
[itemTitle release];
itemTitle = [s retain];
itemRect.size = [itemTitle sizeWithAttributes: nil];
}

- (void)awakeFromNib
{
itemRect.origin = NSMakePoint(100, 100);
[self setString: @"Hello World"];
maxSize = NSMakeSize(128, 300);
}

-(void)drawRect:(NSRect)r
{
[[NSColor windowBackgroundColor] set];
NSRectFill(r);

[[NSColor selectedTextBackgroundColor] set];
NSRectFill(itemRect);

[[NSColor textColor] set];
[itemTitle drawAtPoint: itemRect.origin withAttributes: nil];

[[NSString stringWithFormat: @"%g,%g", itemRect.origin.x, itemRect.origin.y] drawAtPoint: NSZeroPoint withAttributes: nil];

}

- (void)textDidEndEditing:(NSNotification *)aNotification
{
[self setNeedsDisplayInRect: itemRect];
[self setString: [[aNotification object] string]];
[self setNeedsDisplayInRect: itemRect];
[[self window] makeFirstResponder: nil];
[editor removeFromSuperview];
}

- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint p = [self convertPoint: [theEvent locationInWindow] fromView: nil];
if (editor != nil) {
[self setNeedsDisplayInRect: itemRect];
[[self window] makeFirstResponder: nil];
[editor removeFromSuperview];
editor = nil;
}
if (NSPointInRect(p, itemRect)) {
if ([theEvent clickCount] > 1) {
editor = [[self window] fieldEditor: YES forObject: self];
[editor setString: itemTitle];
[editor setFrame: itemRect];
[editor setDelegate: self];
[editor setMaxSize: maxSize];
[editor setHorizontallyResizable: YES];
[editor setBackgroundColor: [NSColor redColor]];
[editor setDrawsBackground: YES];
[editor setFieldEditor: YES];
[self addSubview: editor];
[[self window] makeFirstResponder: editor];
} else {
NSSize offset = NSMakeSize(p.x - itemRect.origin.x, p.y - itemRect.origin.y);
while (theEvent = [[self window] nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask]) {
if ([theEvent type] == NSLeftMouseDragged) {
NSPoint p = [self convertPoint: [theEvent locationInWindow] fromView: nil];
[self setNeedsDisplayInRect: itemRect];
itemRect.origin.x = p.x - offset.width;
itemRect.origin.y = p.y - offset.height;
[self setNeedsDisplayInRect: itemRect];
[self setNeedsDisplayInRect: NSMakeRect(0,0,100,20)];

}
else break;
}
}
}
}

@end





On Monday, June 25, 2001, at 06:59 PM, Tom Waters wrote:

I am re-attempting to use the built-in field editor in NSWindow.

In an earlier attempt, I subclassed NSTextView and did it myself, because I was frustrated with attempts to make the standard one work right. But I would prefer to do things the "correct" way, as opposed to simply making it work.

Here's a simple example of what I am seeing that seems odd/wrong.

I have a custom view, full of custom cells.

when I get a double-click in my view I do the following:

- (void)mouseDown:(NSEvent *)theEvent
if ([theEvent clickCount] > 1) {
id editor = [[self window] fieldEditor: YES forObject: cell];
[editor setFont: [cell font]];
[editor setString: [cell title]];
[editor setFrame: [cell textRect]];
[editor setDelegate: self];
[editor setMaxSize: NSMakeSize(128, 10000)];
[editor setHorizontallyResizable: YES];
[editor setBackgroundColor: [NSColor redColor]];
[editor setDrawsBackground: YES];
[editor setFieldEditor: YES];
[self addSubview: editor];
[[self window] makeFirstResponder: editor];
}

My cell's textRect method returns the exact bounding box of the text to be edited, computed via NSStringAdditions sizeWithAttributes:

Given this setup, I would expect that the editor would be in the exact right place, and then resize accordingly up to the max width, then wrap to a second line, etc. This was my intent by setting setHorizontallyResizable to YES.

The problem I'm seeing is that the text editor ALWAYS wraps the last character of my title to a second line, even if I use the editor to delete the last few characters, it insists on keeping things on two lines...

"Mac OS X" looks like this, as you hit backspace from the end.

Mac OS
X

Mac O
S

Mac

Ma
c

Attempts to use sizeToFit have made things worse!

Anyone see what I'm doing wrong?

Also, whose responsibility is it to paint the border that one sees around most edited text? ala TableView and OutlineView... the method I'm using above has no border at all...


  • Prev by Date: Re: Drag and drop problems!
  • Next by Date: Re: autoreleasing NSBezierPath object
  • Previous by thread: field editor oddness
  • Next by thread: Re: Communicate with pipe and AuthorizationExecuteWithPrivileges
  • Index(es):
    • Date
    • Thread