You need to log in to create posts and topics.
TextFieldClick
1,678 Posts
#1 · October 6, 2012, 12:00 pm
Quote from Forum Archives on October 6, 2012, 12:00 pmPosted by: svanvoorst <svanvoorst@...>
Remember how easy it was to use _efClick in FB? This is what you have to do in order to accomplish the same thing in Cocoa: first subclass NSTextField and then use the mouseDown: event. If anyone knows an easier or more direct method, please post.'------ start --------/*Demonstrates trapping clicks on NSTextFields.Text fields are subclassed and use a mouseDown: eventto trap clicks.Written by S.Van Voorst {20121006}*/BeginCDeclaration@interface TextField : NSTextField{BOOL isInside;}//methods automatically called@end //interfaceEndCBeginCFunction@implementation TextField : NSTextField;- (id)initWithFrame:(NSRect)frameRect{if ((self = [super initWithFrame:frameRect]) != nil){self = [super initWithFrame:frameRect];// Add initialization code here}return self;}- (void) mouseDown:(NSEvent *)event{NSPoint wndPt = [event locationInWindow];NSPoint pt = [self convertPoint:wndPt fromView:nil];// ***** Corrects for Embedder View being flipped ***** //isInside = [self mouse:pt inRect:[self bounds]];if (isInside == YES) NSBeep();}@end //implementationEndCBeginCDeclaration@interface EmbedderView : NSView//methods automatically called@end //interfaceEndCBeginCFunction@implementation EmbedderView : NSView;- (id)initWithFrame:(NSRect)frameRect{if ((self = [super initWithFrame:frameRect]) != nil){self = [super initWithFrame:frameRect];// Add initialization code here}return self;}- (void)drawRect:(NSRect)rect{// ****** Background ***** //[[NSColor whiteColor] set];[NSBezierPath fillRect:rect];}// ----- Use this if you want 0,0 (origin) to be top, left ---- //// ----- Otherwise origin will be at bottom, left (Unflipped) ----- //-(BOOL)isFlipped{return YES;}@end //implementationEndCBeginCDeclaration@interface MyDelegate : NSObject{NSWindow *window;}- (void) createMenu;- (void) createWindow;@endEndCBeginCFunction@implementation MyDelegate : NSObject- (void) dealloc{[window release];[super dealloc];}- (void) createMenu{// ******** Menu and menubar ********//id menubar = [[NSMenu new] autorelease];id appMenuItem = [[NSMenuItem new] autorelease];[menubar addItem:appMenuItem];[NSApp setMainMenu:menubar];id appMenu = [[NSMenu new] autorelease];id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"action:@selector(terminate:) keyEquivalent:@"q"] autorelease];[appMenu addItem:quitMenuItem];[appMenuItem setSubmenu:appMenu];}- (void) createWindow{// ***** window ***** //#define _wndW 600#define _wndH 350window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMaskbacking: NSBackingStoreBuffereddefer: NO];[window center];[window setTitle: @"Text Field Click"];// ***** Create an instance of EmbedderView (NSView) ***** //EmbedderView *baseView = [[EmbedderView alloc]initWithFrame:NSMakeRect( 20, 80, _wndW - 40, _wndH - 100 )];[[window contentView] addSubview:baseView];[baseView release];// **** Prevent focus on first text field upon opening ***** //[window setInitialFirstResponder:baseView];// ***** Subclassed Text Fields **** //TextField *txtFld[3];NSRect efR[3];efR[0] = NSMakeRect( 30, 80, 140, 30 );txtFld[0] = [[TextField alloc]initWithFrame:efR[0]];[baseView addSubview:txtFld[0]];[txtFld[0] release];efR[1] = NSOffsetRect( efR[0], 160, 0);txtFld[1] = [[TextField alloc]initWithFrame:efR[1]];[baseView addSubview:txtFld[1]];[txtFld[1] release];efR[2] = NSOffsetRect( efR[1], 160, 0);txtFld[2] = [[TextField alloc]initWithFrame:efR[2]];[baseView addSubview:txtFld[2]];[txtFld[2] release];// ***** Quit btn ***** //NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 130, 30, 95, 30 )];[quitBtn setBezelStyle:NSRoundedBezelStyle ];[quitBtn setTitle: @"Quit" ];[quitBtn setAction:@selector(terminate:)];[[window contentView] addSubview: quitBtn];[quitBtn release];}- (void) applicationWillFinishLaunching: (NSNotification *)not{[self createMenu];[self createWindow];}- (void) applicationDidFinishLaunching: (NSNotification *)not;{[window makeKeyAndOrderFront: nil];}@endEndCBeginCCodeNSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];[NSApplication sharedApplication];[NSApp setDelegate: [MyDelegate new]];[NSApp run];[pool drain];EndC
'------ end -------Steve Van Voorst
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
Posted by: svanvoorst <svanvoorst@...>
Remember how easy it was to use _efClick in FB? This is what you have to do in order to accomplish the same thing in Cocoa: first subclass NSTextField and then use the mouseDown: event. If anyone knows an easier or more direct method, please post.
'------ start --------
/*
Demonstrates trapping clicks on NSTextFields.
Text fields are subclassed and use a mouseDown: event
to trap clicks.
Written by S.Van Voorst {20121006}
*/
BeginCDeclaration
@interface TextField : NSTextField
{
BOOL isInside;
}
//methods automatically called
@end //interface
EndC
BeginCFunction
@implementation TextField : NSTextField;
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil)
{
self = [super initWithFrame:frameRect];
// Add initialization code here
}
return self;
}
- (void) mouseDown:(NSEvent *)event
{
NSPoint wndPt = [event locationInWindow];
NSPoint pt = [self convertPoint:wndPt fromView:nil];
// ***** Corrects for Embedder View being flipped ***** //
isInside = [self mouse:pt inRect:[self bounds]];
if (isInside == YES) NSBeep();
}
@end //implementation
EndC
BeginCDeclaration
@interface EmbedderView : NSView
//methods automatically called
@end //interface
EndC
BeginCFunction
@implementation EmbedderView : NSView;
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil)
{
self = [super initWithFrame:frameRect];
// Add initialization code here
}
return self;
}
- (void)drawRect:(NSRect)rect
{
// ****** Background ***** //
[[NSColor whiteColor] set];
[NSBezierPath fillRect:rect];
}
// ----- Use this if you want 0,0 (origin) to be top, left ---- //
// ----- Otherwise origin will be at bottom, left (Unflipped) ----- //
-(BOOL)isFlipped
{
return YES;
}
@end //implementation
EndC
BeginCDeclaration
@interface MyDelegate : NSObject
{
NSWindow *window;
}
- (void) createMenu;
- (void) createWindow;
@end
EndC
BeginCFunction
@implementation MyDelegate : NSObject
- (void) dealloc
{
[window release];
[super dealloc];
}
- (void) createMenu
{
// ******** Menu and menubar ********//
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"
action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
}
- (void) createWindow
{
// ***** window ***** //
#define _wndW 600
#define _wndH 350
window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[window center];
[window setTitle: @"Text Field Click"];
// ***** Create an instance of EmbedderView (NSView) ***** //
EmbedderView *baseView = [[EmbedderView alloc]initWithFrame:NSMakeRect( 20, 80, _wndW - 40, _wndH - 100 )];
[[window contentView] addSubview:baseView];
[baseView release];
// **** Prevent focus on first text field upon opening ***** //
[window setInitialFirstResponder:baseView];
// ***** Subclassed Text Fields **** //
TextField *txtFld[3];
NSRect efR[3];
efR[0] = NSMakeRect( 30, 80, 140, 30 );
txtFld[0] = [[TextField alloc]initWithFrame:efR[0]];
[baseView addSubview:txtFld[0]];
[txtFld[0] release];
efR[1] = NSOffsetRect( efR[0], 160, 0);
txtFld[1] = [[TextField alloc]initWithFrame:efR[1]];
[baseView addSubview:txtFld[1]];
[txtFld[1] release];
efR[2] = NSOffsetRect( efR[1], 160, 0);
txtFld[2] = [[TextField alloc]initWithFrame:efR[2]];
[baseView addSubview:txtFld[2]];
[txtFld[2] release];
// ***** Quit btn ***** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 130, 30, 95, 30 )];
[quitBtn setBezelStyle:NSRoundedBezelStyle ];
[quitBtn setTitle: @"Quit" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
[quitBtn release];
}
- (void) applicationWillFinishLaunching: (NSNotification *)not
{
[self createMenu];
[self createWindow];
}
- (void) applicationDidFinishLaunching: (NSNotification *)not;
{
[window makeKeyAndOrderFront: nil];
}
@end
EndC
BeginCCode
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[NSApplication sharedApplication];
[NSApp setDelegate: [MyDelegate new]];
[NSApp run];
[pool drain];
EndC
'------ end -------
Steve Van Voorst
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
Click for thumbs down.0Click for thumbs up.0