Re: How to implement iChat's smiley popup in the text entry field
Re: How to implement iChat's smiley popup in the text entry field
- Subject: Re: How to implement iChat's smiley popup in the text entry field
- From: Ryan Stevens <email@hidden>
- Date: Sat, 13 Nov 2004 18:16:50 -0800
On Nov 13, 2004, at 11:53 AM, Larry Gerndt wrote:
I would like to implement a popup menu located in the right end of an
NSTextField just like iChat has for picking a smiley face. Since I'm new
to
Cocoa, I'm not sure how to go about this, and would appreciate a pointer on
the right way to do this.
If you want it to look exactly the same, you have some work cut out for
you...
The way I would approach this functionality is with a borderless pulldown
NSPopUpButton that uses the TextAndImageCell class from
"/Developer/Examples/AppKit/DragNDropOutlineView/" for its menu items. I
think menus use cells, but I'm not certain; someone else can correct me on
this one.
Otherwise, you're going to have to create a bunch of custom classes to deal
with the presentation of the popup view, mouse-overs, selection bezels...
ugh. Apple probably has their own APIs for some of this stuff, so it's
easier for them, but it's a lot of work for the rest of us.
Fortunately, I don't have to implement the smiley's menu, my menu will be
just
a regular menu of text strings (predefined chat messages), so I imagine it's
quite a bit easier. I just want to know how to get it located properly in
the
text field such that it looks like it's embedded there and the text field
doesn't show text over the icon.
Oh, then you want NSTextAttachment.
A brief scan of the documentation for NSTextAttachment didn't reveal to me
how this is what I want. Do you happen to have a pointer to some example
where it's used to accomplish what I want?
I'd try something like this..
// Std. written in Mail
- (void)setString:(NSString *)string
{
NSFileWrapper *wrapper = [[[NSFileWrapper alloc] initWithPath:pathToImage] autorelease];
NSTextAttachment *attachment = [[[NSTextAttachment alloc] initWithFileWrapper:wrapper] autorelease];
NSMutableAttribuedString *mAttedString = [NSMutableAttributedString attributedStringWithAttachment:attachment];
{ // give the attachment a right alignment..
NSMutableParagraphStyle *style = [[[NSMutableParagraphStyle alloc] init] autorelease];
NSDictionary *attsDict;
[style setAlignment:NSRightTextAlignment];
attsDict = [NSDictionary dictionaryWithObject:style
forKey:NSParagraphStyleAttributeName];
[mAttedString addAttributes:attsDict range:NSMakeRange(0,1)];
}
NSAttributedString *myString = [[[NSAttributedString alloc] initWithString:string] autorelease];
[mAttedString insertAttributedString:myString atIndex:0];
[self setAttributedString:mAttedString]; // self being an NSTextField subclass
}
And if that doesn't work then something like this will for sure (which I wrote and ripped out of Charla <http://sourceforge.net/projects/charla/>)...
// This is required, as you'll see later.
@implementation PoserTextView
NSRange myPrvtSelectedRange;
- (void)setSelectedRange:(NSRange)aRange
{
myPrvtSelectedRange = aRange;
[super setSelectedRange:aRange];
}
- (NSRange)_prvtSelectedRange
{
return myPrvtSelectedRange;
}
@end
#import "RSTextFieldCell.h"
#import "PoserTextView.h"
@implementation RSTextFieldCell
- (id)initTextCell:(NSString *)txt
{
[super initTextCell:txt];
imgCell = [[NSImageCell alloc] initImageCell:[NSImage imageNamed:@"shades.tiff"]];
return self;
}
- (void)dealloc
{
[imgCell release];
[super dealloc];
}
- (NSRect)drawingRectForBounds:(NSRect)theRect
{
NSRect rect = [super drawingRectForBounds:theRect];
rect.origin.x += 18;
rect.size.width -= 18;
return rect;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[super drawWithFrame:cellFrame inView:controlView];
[imgCell drawWithFrame:NSMakeRect(18) inView:controlView];
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(id)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
{
NSPoint mouseLoc=[[controlView window] convertScreenToBase:[NSEvent mouseLocation]];
NSRect rect = [controlView frame];
rect.size.width = 18;
// We should be able to just return from here so the selection isn't modified but nooo.
if (NSPointInRect(mouseLoc, rect)) {
// Get the selectedRange but for some reason textObj always returns (0, 32000).
// We posed as NSTextView as a work-around...
NSRange selectedRange = [(PoserTextView *)textObj _prvtSelectedRange];
selStart = selectedRange.location;
selLength = selectedRange.length;
}
[super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden