Custom field editors

  • I'm looking at creating a custom field editor (so I can filter out
    keystrokes in various types of controls in a consistent way).
    The text editing programming guide has a section called "How to
    Substitute a Custom Field Editor", with an example snippet which seems
    nice and simple:

    > (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
    > {
    > if ([anObject isKindOfClass:[NSTextField class]])
    > {
    > return [[[myCustomFieldEditor alloc] init] autorelease];
    > }
    > return nil;
    > }

    (Admittedly, there are two problems in the code—no leading hyphen and a
    lowercase class name—but I'm going to let that slide for now :) )
    However, according to the docs for
    -windowWillReturnFieldEditor:toObject:, this code is actually broken:

    > Discussion
    > This method may be called multiple times while a control is first
    > responder. Therefore, you must return the same field editor object for
    > the control while the control is being edited.

    This seems to directly contradict the sample code above, so which is
    correct? I'll happily file it, but I just wanted to run this past
    someone who might have already been down this road before.
  • Yes, admittedly, the sample code is probably too simplified.

    Usually you should store your custom field editor instance somewhere
    and have mapping mechanism between the editor and window/object.

    Also, another issue with this sample is that it's returning the custom
    editor for all NSTextField subclasses and could cause issues if there
    is an NSTextField subclass that already requires its own NSTextView
    subclass.

    It should selectively return the custom field editor.

    I filed a bug 5670642 for requesting tech document improvement.

    Thanks,
    Aki

    On 2008/01/04, at 11:35, John Stiles wrote:

    > I'm looking at creating a custom field editor (so I can filter out
    > keystrokes in various types of controls in a consistent way).
    > The text editing programming guide has a section called "How to
    > Substitute a Custom Field Editor", with an example snippet which
    > seems nice and simple:
    >
    >> (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:
    >> (id)anObject
    >> {
    >> if ([anObject isKindOfClass:[NSTextField class]])
    >> {
    >> return [[[myCustomFieldEditor alloc] init] autorelease];
    >> }
    >> return nil;
    >> }
    >
    >
    > (Admittedly, there are two problems in the code$B!=(Bno leading
    > hyphen and a lowercase class name$B!=(Bbut I'm going to let that
    > slide for now :) )
    > However, according to the docs for -
    > windowWillReturnFieldEditor:toObject:, this code is actually broken:
    >
    >> Discussion
    >> This method may be called multiple times while a control is first
    >> responder. Therefore, you must return the same field editor object
    >> for the control while the control is being edited.
    >
    > This seems to directly contradict the sample code above, so which is
    > correct? I'll happily file it, but I just wanted to run this past
    > someone who might have already been down this road before.
  • Cool, thanks for filing it on my behalf :)
    I had been running under the assumption that the sample snippet was too
    simple, so fortunately the code I just wrote is not bogus… thanks!

    Aki Inoue wrote:
    > Yes, admittedly, the sample code is probably too simplified.
    >
    > Usually you should store your custom field editor instance somewhere
    > and have mapping mechanism between the editor and window/object.
    >
    > Also, another issue with this sample is that it's returning the custom
    > editor for all NSTextField subclasses and could cause issues if there
    > is an NSTextField subclass that already requires its own NSTextView
    > subclass.
    >
    > It should selectively return the custom field editor.
    >
    > I filed a bug 5670642 for requesting tech document improvement.
    >
    > Thanks,
    > Aki
    >
    > On 2008/01/04, at 11:35, John Stiles wrote:
    >
    >> I'm looking at creating a custom field editor (so I can filter out
    >> keystrokes in various types of controls in a consistent way).
    >> The text editing programming guide has a section called "How to
    >> Substitute a Custom Field Editor", with an example snippet which
    >> seems nice and simple:
    >>
    >>> (id)windowWillReturnFieldEditor:(NSWindow *)sender
    >>> toObject:(id)anObject
    >>> {
    >>> if ([anObject isKindOfClass:[NSTextField class]])
    >>> {
    >>> return [[[myCustomFieldEditor alloc] init] autorelease];
    >>> }
    >>> return nil;
    >>> }
    >>
    >>
    >> (Admittedly, there are two problems in the code—no leading hyphen and
    >> a lowercase class name—but I'm going to let that slide for now :) )
    >> However, according to the docs for
    >> -windowWillReturnFieldEditor:toObject:, this code is actually broken:
    >>
    >>> Discussion
    >>> This method may be called multiple times while a control is first
    >>> responder. Therefore, you must return the same field editor object
    >>> for the control while the control is being edited.
    >>
    >> This seems to directly contradict the sample code above, so which is
    >> correct? I'll happily file it, but I just wanted to run this past
    >> someone who might have already been down this road before.
    >
  • Actually, I'd like to point out what appears to be another, more subtle
    bug in this example code.

    It uses "-init" to initialize the custom field editor—however, custom
    field editors should almost certainly be subclasses of NSText, and so
    they should use the designated initializer "-initWithFrame:". Right?

    John Stiles wrote:
    > Cool, thanks for filing it on my behalf :)
    > I had been running under the assumption that the sample snippet was
    > too simple, so fortunately the code I just wrote is not bogus… thanks!
    >
    >
    > Aki Inoue wrote:
    >> Yes, admittedly, the sample code is probably too simplified.
    >>
    >> Usually you should store your custom field editor instance somewhere
    >> and have mapping mechanism between the editor and window/object.
    >>
    >> Also, another issue with this sample is that it's returning the
    >> custom editor for all NSTextField subclasses and could cause issues
    >> if there is an NSTextField subclass that already requires its own
    >> NSTextView subclass.
    >>
    >> It should selectively return the custom field editor.
    >>
    >> I filed a bug 5670642 for requesting tech document improvement.
    >>
    >> Thanks,
    >> Aki
    >>
    >> On 2008/01/04, at 11:35, John Stiles wrote:
    >>
    >>> I'm looking at creating a custom field editor (so I can filter out
    >>> keystrokes in various types of controls in a consistent way).
    >>> The text editing programming guide has a section called "How to
    >>> Substitute a Custom Field Editor", with an example snippet which
    >>> seems nice and simple:
    >>>
    >>>> (id)windowWillReturnFieldEditor:(NSWindow *)sender
    >>>> toObject:(id)anObject
    >>>> {
    >>>> if ([anObject isKindOfClass:[NSTextField class]])
    >>>> {
    >>>> return [[[myCustomFieldEditor alloc] init] autorelease];
    >>>> }
    >>>> return nil;
    >>>> }
    >>>
    >>>
    >>> (Admittedly, there are two problems in the code—no leading hyphen
    >>> and a lowercase class name—but I'm going to let that slide for now :) )
    >>> However, according to the docs for
    >>> -windowWillReturnFieldEditor:toObject:, this code is actually broken:
    >>>
    >>>> Discussion
    >>>> This method may be called multiple times while a control is first
    >>>> responder. Therefore, you must return the same field editor object
    >>>> for the control while the control is being edited.
    >>>
    >>> This seems to directly contradict the sample code above, so which is
    >>> correct? I'll happily file it, but I just wanted to run this past
    >>> someone who might have already been down this road before.
    >>
    >
  • No, it's always fine to call any init method unless specifically
    documented otherwise.

    Designated initializers are something that subclassers care about, not
    clients of a class.  The contract is that every init method of a class
    is guaranteed to funnel through one of the designated initializers.
    Thus, a subclasser (that needs to do initialization) must override all
    of the designated initializers of its superclass, but need not
    override any of the non-designated initializers.

    -Ken

    On 1/4/08, John Stiles <JStiles...> wrote:
    > Actually, I'd like to point out what appears to be another, more subtle
    > bug in this example code.
    >
    > It uses "-init" to initialize the custom field editor—however, custom
    > field editors should almost certainly be subclasses of NSText, and so
    > they should use the designated initializer "-initWithFrame:". Right?
    >
    >
    > John Stiles wrote:
    >> Cool, thanks for filing it on my behalf :)
    >> I had been running under the assumption that the sample snippet was
    >> too simple, so fortunately the code I just wrote is not bogus… thanks!
    >>
    >>
    >> Aki Inoue wrote:
    >>> Yes, admittedly, the sample code is probably too simplified.
    >>>
    >>> Usually you should store your custom field editor instance somewhere
    >>> and have mapping mechanism between the editor and window/object.
    >>>
    >>> Also, another issue with this sample is that it's returning the
    >>> custom editor for all NSTextField subclasses and could cause issues
    >>> if there is an NSTextField subclass that already requires its own
    >>> NSTextView subclass.
    >>>
    >>> It should selectively return the custom field editor.
    >>>
    >>> I filed a bug 5670642 for requesting tech document improvement.
    >>>
    >>> Thanks,
    >>> Aki
    >>>
    >>> On 2008/01/04, at 11:35, John Stiles wrote:
    >>>
    >>>> I'm looking at creating a custom field editor (so I can filter out
    >>>> keystrokes in various types of controls in a consistent way).
    >>>> The text editing programming guide has a section called "How to
    >>>> Substitute a Custom Field Editor", with an example snippet which
    >>>> seems nice and simple:
    >>>>
    >>>>> (id)windowWillReturnFieldEditor:(NSWindow *)sender
    >>>>> toObject:(id)anObject
    >>>>> {
    >>>>> if ([anObject isKindOfClass:[NSTextField class]])
    >>>>> {
    >>>>> return [[[myCustomFieldEditor alloc] init] autorelease];
    >>>>> }
    >>>>> return nil;
    >>>>> }
    >>>>
    >>>>
    >>>> (Admittedly, there are two problems in the code—no leading hyphen
    >>>> and a lowercase class name—but I'm going to let that slide for now :) )
    >>>> However, according to the docs for
    >>>> -windowWillReturnFieldEditor:toObject:, this code is actually broken:
    >>>>
    >>>>> Discussion
    >>>>> This method may be called multiple times while a control is first
    >>>>> responder. Therefore, you must return the same field editor object
    >>>>> for the control while the control is being edited.
    >>>>
    >>>> This seems to directly contradict the sample code above, so which is
    >>>> correct? I'll happily file it, but I just wanted to run this past
    >>>> someone who might have already been down this road before.
    >>>
    >>

    >
  • Well, true. In this case we know the client is a subclasser (that's what
    the example is about). But I didn't realize that's how designated
    initialization was meant to work. Thanks for the info.

    Ken Ferry wrote:
    > No, it's always fine to call any init method unless specifically
    > documented otherwise.
    >
    > Designated initializers are something that subclassers care about, not
    > clients of a class.  The contract is that every init method of a class
    > is guaranteed to funnel through one of the designated initializers.
    > Thus, a subclasser (that needs to do initialization) must override all
    > of the designated initializers of its superclass, but need not
    > override any of the non-designated initializers.
    >
    > -Ken
    >
    > On 1/4/08, John Stiles <JStiles...> wrote:
    >
    >> Actually, I'd like to point out what appears to be another, more subtle
    >> bug in this example code.
    >>
    >> It uses "-init" to initialize the custom field editor—however, custom
    >> field editors should almost certainly be subclasses of NSText, and so
    >> they should use the designated initializer "-initWithFrame:". Right?
    >>
    >>
    >> John Stiles wrote:
    >>
    >>> Cool, thanks for filing it on my behalf :)
    >>> I had been running under the assumption that the sample snippet was
    >>> too simple, so fortunately the code I just wrote is not bogus… thanks!
    >>>
    >>>
    >>> Aki Inoue wrote:
    >>>
    >>>> Yes, admittedly, the sample code is probably too simplified.
    >>>>
    >>>> Usually you should store your custom field editor instance somewhere
    >>>> and have mapping mechanism between the editor and window/object.
    >>>>
    >>>> Also, another issue with this sample is that it's returning the
    >>>> custom editor for all NSTextField subclasses and could cause issues
    >>>> if there is an NSTextField subclass that already requires its own
    >>>> NSTextView subclass.
    >>>>
    >>>> It should selectively return the custom field editor.
    >>>>
    >>>> I filed a bug 5670642 for requesting tech document improvement.
    >>>>
    >>>> Thanks,
    >>>> Aki
    >>>>
    >>>> On 2008/01/04, at 11:35, John Stiles wrote:
    >>>>
    >>>>
    >>>>> I'm looking at creating a custom field editor (so I can filter out
    >>>>> keystrokes in various types of controls in a consistent way).
    >>>>> The text editing programming guide has a section called "How to
    >>>>> Substitute a Custom Field Editor", with an example snippet which
    >>>>> seems nice and simple:
    >>>>>
    >>>>>
    >>>>>> (id)windowWillReturnFieldEditor:(NSWindow *)sender
    >>>>>> toObject:(id)anObject
    >>>>>> {
    >>>>>> if ([anObject isKindOfClass:[NSTextField class]])
    >>>>>> {
    >>>>>> return [[[myCustomFieldEditor alloc] init] autorelease];
    >>>>>> }
    >>>>>> return nil;
    >>>>>> }
    >>>>>>
    >>>>> (Admittedly, there are two problems in the code—no leading hyphen
    >>>>> and a lowercase class name—but I'm going to let that slide for now :) )
    >>>>> However, according to the docs for
    >>>>> -windowWillReturnFieldEditor:toObject:, this code is actually broken:
    >>>>>
    >>>>>
    >>>>>> Discussion
    >>>>>> This method may be called multiple times while a control is first
    >>>>>> responder. Therefore, you must return the same field editor object
    >>>>>> for the control while the control is being edited.
    >>>>>>
    >>>>> This seems to directly contradict the sample code above, so which is
    >>>>> correct? I'll happily file it, but I just wanted to run this past
    >>>>> someone who might have already been down this road before.
    >>>>>

    >>
    >>
previous month january 2008 next month
MTWTFSS
  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      
Go to today