Professional Web Applications Themes

Get notified on clicks - Mac Programming

Hi, In a Cocoa app, I'm trying to get notified when the user clicks outside of the current responder view. The purpose is to check the validity of input before performing the click. For example, a NSTextField is the current responder. If the user clicks on a NSButton, since the NSButton can't become responder, the NSTextField is not notified. I'm looking for a generic way to tell the NSTextField to check itself, and maybe filter the click. I was expecting textShouldEndEditing to be called, but it appears that it's called only when another responder is about to become the current ...

  1. #1

    Default Get notified on clicks

    Hi,

    In a Cocoa app, I'm trying to get notified when the user clicks outside of
    the current responder view.

    The purpose is to check the validity of input before performing the click.
    For example, a NSTextField is the current responder. If the user clicks on a
    NSButton, since the NSButton can't become responder, the NSTextField is not
    notified. I'm looking for a generic way to tell the NSTextField to check
    itself, and maybe filter the click.

    I was expecting textShouldEndEditing to be called, but it appears that it's
    called only when another responder is about to become the current responder.

    If possible, I'd rather avoid overriding NSApplication::sendEvent.

    Any idea ?

    Eric

    Eric Guest

  2. #2

    Default Re: Get notified on clicks

    In article <BBC784B1.14A63%fr>,
    Eric VERGNAUD <fr> wrote:
     

    Checking the validity of input is normally done by an NSFormatter, so
    you should write an NSFormatter subclass if you can.
     

    I believe this is due to strangeness with the field editor. If
    NSFormatter is not an option, then this is probably the area to
    investigate.
    Michael Guest

  3. #3

    Default Re: Get notified on clicks

    dans l'article mail-0B3F85.11080331102003localhost, Michael Ash à
    com a écrit le 31/10/03 11:08:
     
    >
    > Checking the validity of input is normally done by an NSFormatter, so
    > you should write an NSFormatter subclass if you can.

    >
    > I believe this is due to strangeness with the field editor. If
    > NSFormatter is not an option, then this is probably the area to
    > investigate.[/ref]

    Are you saying that textShouldEndEditing SHOULD be called when the user
    clicks on another control ?

    Eric Guest

  4. #4

    Default Re: Get notified on clicks

    In article <BBC853C4.14AE6%fr>,
    Eric VERGNAUD <fr> wrote:
     

    It should be called when a control ends editing. However, for every
    window there is exactly one field editor which actually has control
    whenever a text field is being edited, so if the user clicks on another
    text field, the field editor is still being edited and so this message
    is not sent. (This is my understanding, at least, it could very well be
    wrong.)
    Michael Guest

  5. #5

    Default Re: Get notified on clicks

    dans l'article mail-16ADCB.19503531102003localhost, Michael Ash à
    com a écrit le 31/10/03 19:50:
     
    >
    > It should be called when a control ends editing. However, for every
    > window there is exactly one field editor which actually has control
    > whenever a text field is being edited, so if the user clicks on another
    > text field, the field editor is still being edited and so this message
    > is not sent. (This is my understanding, at least, it could very well be
    > wrong.)[/ref]

    No, textShouldEndEditing is called properly when clicking in another text
    field. What I'm looking for is a way to get notified when the user clicks
    elsewhere.

    Something like textCouldEndEditing... Because another control might decide,
    for example, to close the window. I'm trying to implement it using
    sendEvent.

    On Windows, since every control can gain focus, this is trivial.

    Eric

    Eric Guest

  6. #6

    Default Re: Get notified on clicks

    In article <BBC8A89B.14B35%fr>,
    Eric VERGNAUD <fr> wrote:
     
    > >
    > > It should be called when a control ends editing. However, for every
    > > window there is exactly one field editor which actually has control
    > > whenever a text field is being edited, so if the user clicks on another
    > > text field, the field editor is still being edited and so this message
    > > is not sent. (This is my understanding, at least, it could very well be
    > > wrong.)[/ref]
    >
    > No, textShouldEndEditing is called properly when clicking in another text
    > field. What I'm looking for is a way to get notified when the user clicks
    > elsewhere.
    >
    > Something like textCouldEndEditing... Because another control might decide,
    > for example, to close the window. I'm trying to implement it using
    > sendEvent.[/ref]

    I see, I guess I misunderstood.

    Again, I think you should be using an NSFormatter; is there some reason
    this wouldn't work?
    Michael Guest

  7. #7

    Default Re: Get notified on clicks

    dans l'article mail-D0D054.20082101112003localhost, Michael Ash à
    com a écrit le 1/11/03 20:08:
     

    I'll try it, but I'm not very confident. It looks like NSFormatter has
    methods to check input each time the text changes and when the field resigns
    being the responder. The first is inadequate, because the input is not
    meaningful yet, and the latter probably doesn't happen when clicking
    elsewhere, otherwise textShouldEndEditing would be called, wouldn't it ?

    What Cocoa is missing is that if the user clicks elsewhere, it is ok to
    presume that the input is complete, except in special cases. So I think we
    should be notified for this.

    I'll try NSFormatter and let you know haw it goes.

    Eric

    Eric Guest

  8. #8

    Default Re: Get notified on clicks

    In article <BBC9F549.14B9D%fr>,
    Eric VERGNAUD <fr> wrote:
     
    >
    > I'll try it, but I'm not very confident. It looks like NSFormatter has
    > methods to check input each time the text changes and when the field resigns
    > being the responder. The first is inadequate, because the input is not
    > meaningful yet, and the latter probably doesn't happen when clicking
    > elsewhere, otherwise textShouldEndEditing would be called, wouldn't it ?
    >
    > What Cocoa is missing is that if the user clicks elsewhere, it is ok to
    > presume that the input is complete, except in special cases. So I think we
    > should be notified for this.
    >
    > I'll try NSFormatter and let you know haw it goes.
    >
    > Eric
    >[/ref]

    So you don't want the user to close this dialog by pushing the button if
    the text in the text field isn't formed properly?

    What you ought to do is deactivate any button which would close the
    dialog, then continually check the field in question (set your object as
    the delegate) until the input is in the proper format. When it is,
    re-enable the button so the user can click it.

    This is how I did a registration dialog in my app. I've seen it in
    several others' as well.

    You certainly don't want them to click on a button and nothing to
    happen, right?

    --
    |\/| /| |2 |<
    mehaase(at)sas(dot)upenn(dot)edu
    Mark Guest

  9. #9

    Default Re: Get notified on clicks

    dans l'article upenn.edu, Mark Haase à
    net a écrit le 2/11/03 6:02:
     
    >
    > So you don't want the user to close this dialog by pushing the button if
    > the text in the text field isn't formed properly?[/ref]
     

    That was just an example. Since I'm doing all my job through a C++
    multiplatform framework, what I'm looking for is a way to get the same
    atomic control that I have on Windows.

    Here is another example. Say you have a field in which to enter an url.
    You're obviously not going to check the url each time the user types a
    character. So a good time to check it is when the user has finished input.
    When is that ? When the user goes to another field ? Yes, but not only. It's
    also when the user clicks on a radio button, a check box, a popup etc...

    Obviously this kind of logic needs to be handled at the dialog/window level,
    or at the app level. So I'm looking for a way to get notified in such a
    situation.

    On Windows, I get a WM_LOSEFOCUS message, equivalent to the
    textShouldEndEditing message. This happens because on Windows, all controls
    accept keyboard input. I'm not expecting Cocoa controls to behave the same,
    however, I need the same low-level event control.

    This was easily managed in MacOS and Carbon. Having worked on multiplatform
    frameworks for more than 10 years, including platforms like PalmOS and
    WinCE, I've come to the conclusion that the concept of ending input is
    distinct from that of losing keyboard focus, even though it's the same on
    Windows.

    Eric


    Eric Guest

  10. #10

    Default Re: Get notified on clicks / solution / RFC

    Hi all,

    Finally, I've founhd a solution by overriding NSWindow:sendEvent.

    I'm giving it here because I thought it may be useful to someone else, and
    also because since I don't have a huge experience with Cocoa, maybe there
    are some side effects that I'm not aware of.

    Here's the code for MyNSWindow:sendEvent

    XCocoaFrame is a protocol which connects Cocoa UI objects to my C++ UI
    classes. canChangeFocus and onResignFocus are methods of this protocol.

    - (void)sendEvent:(NSEvent *)theEvent {
    if([theEvent type]==NSLeftMouseDown)
    {
    NSResponder* resp = [self firstResponder];
    if(resp && [resp isKindOfClass:[NSText class]])
    {
    NSText* text = (NSText*)resp;
    id obj = [text delegate];
    if(obj && [obj conformsToProtocol:protocol(XCocoaFrame)])
    {
    NSPoint clic = [theEvent locationInWindow];
    NSView* content = [self contentView];
    id hit = [content hitTest:clic];
    if(hit && hit!=obj)
    {
    bool canchangefocus = false;

    if([hit conformsToProtocol:protocol(XCocoaFrame)])
    canchangefocus = [hit canChangeFocus];
    else if([hit isKindOfClass:[NSControl class]])
    canchangefocus = true;
    else if([hit isKindOfClass:[NSResponder class]])
    canchangefocus = [hit acceptsFirstResponder];
    if(canchangefocus && ![obj onResignFocus])
    return; // filter the clic
    }
    }
    }
    }
    [super sendEvent:theEvent];
    }

    Any comment will be appreciated.

    Eric

    Eric Guest

Similar Threads

  1. Counting Clicks
    By Bart in forum ASP.NET Data Grid Control
    Replies: 6
    Last Post: January 12th, 07:05 PM
  2. Clicks and double-clicks
    By PontiMax in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: July 14th, 11:00 AM
  3. Images with clicks
    By Nrgo in forum Macromedia Director Basics
    Replies: 2
    Last Post: April 29th, 08:11 AM
  4. multiple mouse clicks
    By mrsr84 in forum Macromedia Director Lingo
    Replies: 6
    Last Post: September 1st, 07:19 PM
  5. Notified
    By Arizona 1991 in forum AIX
    Replies: 1
    Last Post: September 1st, 10:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139