Re: NSTextField sendActionOn:

2010-01-17 Thread Brad Stone
I finally had a breakthrough!  I'm not sure it's the "best" solution but it 
works and hopefully will be instructive for others trying to do the same thing 
connection views.  There's a lot of steps (which is why I think it may not be 
the "best") so I'll try to be as clear as possible.

1) when a user clicks their mouse in my subclasses NSTextField I send this 
action:
 BOOL theResult = [NSApp sendAction:@selector(notifyViewOfMouseDown) to:nil 
from:nil];

2) my subclassed view has a method that executes when this action is sent:
- (void)notifyViewOfMouseDown {

NSDictionary *d = [NSDictionary dictionaryWithObject:self 
forKey:@"view"];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:SRVIsSelectedChanged object:self userInfo:d];

}
This allows me to package up the view and send it along in a notification.  
Packaging the view is **key** here because this is the view that needs to be 
selected.  My subclassed NSCollectionView is registered as an observer of this 
notification.  When it receives this notification I execute this code to update 
the array controller with a new selectedObject

- (void)handleViewSelections:(NSNotification *)note {

View *v = [[note userInfo] objectForKey:@"view"];


int limit = [[self content] count];

for (int i = 0; i < limit; i++) {
NSCollectionViewItem *item = [self itemAtIndex:i];
View *thisView = [item view];

if ([thisView isEqual:v]) {
[item setSelected:YES]; //my subclassed 
NSCollectionViewItem knows how to set itself as selected and update the array 
controller
}
}


}


Boy, I'm glad I figured that out!  I'm sure there are a lot more experienced 
programmers than me out there so if anyone can think of a way I can do this 
without so many steps I'd be glad to learn from them.

Jonathan, thanks for your suggestion.  My subclassed NSTextField didn't respond 
to superview.  I wish it did, it would have saved me a step.  To adapt your 
solution I'd need to register each newly created view in my NSCollectionView 
link that back to the NSCollectionViewItem that has the code to set itself as 
selected and to update the array controller.  I had to delete your reply from 
this message to make it thru the listserve size 
limit.___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTextField sendActionOn:

2010-01-17 Thread jonat...@mugginsoft.com

On 17 Jan 2010, at 16:30, Brad Stone wrote:

> Here's why I need this - I've been trying to solve this problem for 2 weeks!
> 
> This issue all revolves around a NSCollectionView.  Each CollectionViewItem 
> has a view containing a NSTextField, NSDatePicker, NSButton (checkbox), and 
> an NSLevelIndicator.  The selection index of my CollectionView is binded to 
> the selection index of an NSArrayController.  The problem I having is if the 
> user performs a mouseDown in the TextField I need to update the 
> selectionIndex of the array controller so the CollectionView will show the 
> appropriate view as selected. Without this, the wrong view is selected.  
> Here's a quick example:
> 
> 1) click the add button twice to create two items in my collectionView.  
> Items with index 0 and 1.  Since item 1 was the last one created, it is 
> selected (I have it showing a grey box).
> 2) click your mouse into the text field of the item at index 0 and start 
> typing
> 
> The user would expect item 0 to be the selected item but it's not.  The array 
> controller still thinks item 1 is selected.  It needs to be told otherwise.  
> If the user pressed the remove button item 1 would be removed.  
> 
> This is why I want to fire an action when the user inserts into the text 
> field (just like I do when the user clicks the checkbox).  I want to change 
> the selectedObject in the array controller.  The problem I'm having with 
> subclassing the NSTextField is I can't figure out how to get the 
> CollectionViewItem from the subclassed TextField.  
Does -superview not do the trick?

Sometimes when I need to activate views on mouse clicks I use the following 
approach.
Subclass NSWindow and register the views I need click detection in with 
NSWindow - addClickView:
In NSWindow -sendEvent: I check to see if we have a hit and dispatch a message 
on the view
Might help you out.

@interface MGSClickWindow : NSWindow {
NSHashTable *_clickViews;
}
- (void)addClickView:(NSView *)aView;
@end

@implementation MGSClickWindow
/*
 
 NSWindow designated initialiser
 
 */
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle 
backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
if ((self = [super initWithContentRect:contentRect 
styleMask:windowStyle backing:bufferingType defer:deferCreation])) {
_clickViews = [NSHashTable hashTableWithWeakObjects];
}

return self;
}
/*
 
 add a click view
 
 click view must be a sub view of the NSWindow contentView
 
 */
- (void)addClickView:(NSView *)aView
{
if ([aView isDescendantOf:[self contentView]] && [aView 
respondsToSelector:@selector(subviewClicked:)]) {

// _clickViews will maintain a weak ref to aView so we don't 
need
// to remove it
[_clickViews addObject:aView];
}
}

/*
 
 send event
 
 This action method dispatches mouse and keyboard events sent to the window by 
the NSApplication object.
 
 */
- (void)sendEvent:(NSEvent *)event
{

// look for mouse down
if ([event type] == NSLeftMouseDown) {

// look for deepest subview
NSView *deepView = [[self contentView] hitTest:[event 
locationInWindow]];
if (deepView) {
for (NSView *aClickView in [_clickViews allObjects]) {
if ([deepView isDescendantOf:aClickView]) {
[(id)aClickView 
subviewClicked:deepView];
break;
}
}
}   
}

[super sendEvent:event];

}

@end



> If I could I could then execute my method to update the ArrayController.  I 
> tried creating an IBOutlet to the CollectionView, the ArrayController and the 
> CollectionViewItem but they all come back as nil.  I think I read here that 
> IBOutlets don't work in this instance.  I also tried setting up my own 
> Notification but the CollectionViewItem never receives it (other objects do).
> 
> This is tricky, any help you may have would be appreciated.
> 
> Brad
> 
> 
> 
> On Jan 17, 2010, at 6:12 AM, Graham Cox wrote:
> 
>> 
>> On 17/01/2010, at 3:56 PM, Brad Stone wrote:
>> 
>>> I was able to capture the mouseDown event in the field but only in a 
>>> subclass which is causing me problems elsewhere.
>> 
>> 
>> Indeed, a mouse click is not the only reason a field might become focused - 
>> the user could tab into it as well.
>> 
>> Overriding -becomeFirstResponder should do it.
>> 
>> Taking a step back though, WHY do you need to get notified here? What are 
>> you trying to do? There might be a better way.
>> 
>> --Graham
>> 
>> 
>> 
>> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admi

Re: NSTextField sendActionOn:

2010-01-17 Thread Brad Stone
Here's why I need this - I've been trying to solve this problem for 2 weeks!

This issue all revolves around a NSCollectionView.  Each CollectionViewItem has 
a view containing a NSTextField, NSDatePicker, NSButton (checkbox), and an 
NSLevelIndicator.  The selection index of my CollectionView is binded to the 
selection index of an NSArrayController.  The problem I having is if the user 
performs a mouseDown in the TextField I need to update the selectionIndex of 
the array controller so the CollectionView will show the appropriate view as 
selected. Without this, the wrong view is selected.  Here's a quick example:

1) click the add button twice to create two items in my collectionView.  Items 
with index 0 and 1.  Since item 1 was the last one created, it is selected (I 
have it showing a grey box).
2) click your mouse into the text field of the item at index 0 and start typing

The user would expect item 0 to be the selected item but it's not.  The array 
controller still thinks item 1 is selected.  It needs to be told otherwise.  If 
the user pressed the remove button item 1 would be removed.  

This is why I want to fire an action when the user inserts into the text field 
(just like I do when the user clicks the checkbox).  I want to change the 
selectedObject in the array controller.  The problem I'm having with 
subclassing the NSTextField is I can't figure out how to get the 
CollectionViewItem from the subclassed TextField.  If I could I could then 
execute my method to update the ArrayController.  I tried creating an IBOutlet 
to the CollectionView, the ArrayController and the CollectionViewItem but they 
all come back as nil.  I think I read here that IBOutlets don't work in this 
instance.  I also tried setting up my own Notification but the 
CollectionViewItem never receives it (other objects do).

This is tricky, any help you may have would be appreciated.

Brad



On Jan 17, 2010, at 6:12 AM, Graham Cox wrote:

> 
> On 17/01/2010, at 3:56 PM, Brad Stone wrote:
> 
>> I was able to capture the mouseDown event in the field but only in a 
>> subclass which is causing me problems elsewhere.
> 
> 
> Indeed, a mouse click is not the only reason a field might become focused - 
> the user could tab into it as well.
> 
> Overriding -becomeFirstResponder should do it.
> 
> Taking a step back though, WHY do you need to get notified here? What are you 
> trying to do? There might be a better way.
> 
> --Graham
> 
> 
> 
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTextField sendActionOn:

2010-01-17 Thread Graham Cox

On 17/01/2010, at 3:56 PM, Brad Stone wrote:

> I was able to capture the mouseDown event in the field but only in a subclass 
> which is causing me problems elsewhere.


Indeed, a mouse click is not the only reason a field might become focused - the 
user could tab into it as well.

Overriding -becomeFirstResponder should do it.

Taking a step back though, WHY do you need to get notified here? What are you 
trying to do? There might be a better way.

--Graham




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTextField sendActionOn:

2010-01-17 Thread jonat...@mugginsoft.com
On 17 Jan 2010, at 04:56, Brad Stone wrote:

> I tried that (textDidBeginEditing) but it fires only after the user hits a 
> key to begin typing, not when they first enter the field (i.e. the action 
> that makes the focus ring show up).  I want to be notified as soon as the 
> user clicks in the field to get the cursor in there.  I was able to capture 
> the mouseDown event in the field but only in a subclass which is causing me 
> problems elsewhere.
> 
If subclassing is out (for whatever) and your OS X target is 10.6 then you 
could try observing NSWindow firstResponder.
Subclassing is the way to go though IMHO.

Regards

Jonathan Mitchell

Developer
http://www.mugginsoft.com
> On Jan 16, 2010, at 3:08 PM, Matthew Lindfield Seager wrote:
> 
>> On Sunday, January 17, 2010, Brad Stone  wrote:
>>> The best I can do for the NSTextField is bind an action and in, IB, in the 
>>> TextFieldAttributes set "Action" to "Sent on End Editing"  which sends the 
>>> action after the first character is type.  I want the action sent as soon 
>>> as the field gains focus.
>> 
>> Googling "nstextfield begin editing notification" without the quotes
>> takes one straight to the fine manual. See textDidBeginEditing in
>> particular.
>> 
>> Hope that is what you are after.
>> 
>> Regards,
>> Matt
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/jonathan%40mugginsoft.com
> 
> This email sent to jonat...@mugginsoft.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTextField sendActionOn:

2010-01-16 Thread Brad Stone
I tried that (textDidBeginEditing) but it fires only after the user hits a key 
to begin typing, not when they first enter the field (i.e. the action that 
makes the focus ring show up).  I want to be notified as soon as the user 
clicks in the field to get the cursor in there.  I was able to capture the 
mouseDown event in the field but only in a subclass which is causing me 
problems elsewhere.

On Jan 16, 2010, at 3:08 PM, Matthew Lindfield Seager wrote:

> On Sunday, January 17, 2010, Brad Stone  wrote:
>> The best I can do for the NSTextField is bind an action and in, IB, in the 
>> TextFieldAttributes set "Action" to "Sent on End Editing"  which sends the 
>> action after the first character is type.  I want the action sent as soon as 
>> the field gains focus.
> 
> Googling "nstextfield begin editing notification" without the quotes
> takes one straight to the fine manual. See textDidBeginEditing in
> particular.
> 
> Hope that is what you are after.
> 
> Regards,
> Matt

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTextField sendActionOn:

2010-01-16 Thread Matthew Lindfield Seager
On Sunday, January 17, 2010, Brad Stone  wrote:
> The best I can do for the NSTextField is bind an action and in, IB, in the 
> TextFieldAttributes set "Action" to "Sent on End Editing"  which sends the 
> action after the first character is type.  I want the action sent as soon as 
> the field gains focus.

Googling "nstextfield begin editing notification" without the quotes
takes one straight to the fine manual. See textDidBeginEditing in
particular.

Hope that is what you are after.

Regards,
Matt
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


NSTextField sendActionOn:

2010-01-16 Thread Brad Stone
I have an NSTextField and an NSDatePicker on a view.  As soon as the user 
clicks into either on (and before they type anything) I want to send an action. 
 I thought I'd use sendActionOn: but I can't get it to work.

The best I can do for the NSTextField is bind an action and in, IB, in the 
TextFieldAttributes set "Action" to "Sent on End Editing"  which sends the 
action after the first character is type.  I want the action sent as soon as 
the field gains focus.

Thanks___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com