Double-click and drag word in NSTextView

  • Hello,

    I have a custom text view that overrides the copy and
    paste methods (-writeSelectionToPasteboard:type:) etc.
    I have to do this because my text can have custom
    attributes applied, and if I don't use my own
    pasteboard type these will get lost.

    However, one of my users has noticed that this causes
    an annoying "bug": the default behaviour of an
    NSTextView is that if you double-click on a word to
    select it and then drag and drop the word (or phrase)
    somewhere else, spaces will be inserted around it as
    necessary ("smartly"). But because I am overriding the
    methods that do all this, this behaviour is broken in
    my custom text view.

    So, my question is, how do I go about reimplementing
    this behaviour? ie. How do I detect whether the user
    has double-clicked the word to select it, and
    therefore know that I have to check the context of the
    word when copying and pasting? Is there any way of
    doing this, or any other solution?

    Many thanks,
    Keith

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • Sorry to reply to my own post, but I forgot to add
    that I have already tried:

    smartInsertForString:replacingRange:beforeString:afterString:

    This works great, except that I thought this would
    return nothing if the user hadn't double-clicked the
    word, which turns out not to be the case. If I
    incorporate this into my code, then smart-spaces get
    added even if the user dragged the mouse to select
    rather than double-clicked. So the problem remains
    that I need some way of knowing whether the the paste
    should inititate smart spaces or not (ie. I need to
    know if the word that is being pasted was selected by
    a double-click).

    Thanks in advance,
    Keith

    ----

    Hello,

    I have a custom text view that overrides the copy and
    paste methods (-writeSelectionToPasteboard:type:) etc.
    I have to do this because my text can have custom
    attributes applied, and if I don't use my own
    pasteboard type these will get lost.

    However, one of my users has noticed that this causes
    an annoying "bug": the default behaviour of an
    NSTextView is that if you double-click on a word to
    select it and then drag and drop the word (or phrase)
    somewhere else, spaces will be inserted around it as
    necessary ("smartly"). But because I am overriding the
    methods that do all this, this behaviour is broken in
    my custom text view.

    So, my question is, how do I go about reimplementing
    this behaviour? ie. How do I detect whether the user
    has double-clicked the word to select it, and
    therefore know that I have to check the context of the
    word when copying and pasting? Is there any way of
    doing this, or any other solution?

    Many thanks,
    Keith

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • Okay, now I'm **doubly** sorry to reply to my own post
    a second time... I just realised that NSTextView's
    -selectionGranularity tells me whether or not a word
    has been double-clicked for selection. The trouble is
    that the selection granularity is set when the word is
    selected and seems to be reset when it is dragged, so
    when I come to check whether or not I need to add
    extra whitespace in
    -readSelectionFromPasteboard:type:, the
    -selectionGranularity is no longer NSSelectByWord. My
    current solution to this problem is therefore to have
    a static variable to save the selection granularity:

    static NSSelectionGranularity copyPasteGranularity;

    I just set this to [self selectionGranularity] in
    -writeSelectionToPasteboard:type:. Then, when I come
    to read the copied string from the pasteboard in
    -readSelectionFromPasteboard:type:, I check to see if
    copyPasteGranularity == NSSelectByWord. If so, then I
    call
    -smartInsertForString:replacingRange:beforeString:afterString:
    and insert any extra whitespace returned from this
    method as necessary.

    This seems to work. If any text experts out there can
    tell me whether this is the best way around this
    problem or if there is a better solution, I would
    still be grateful. :)

    Many thanks again,
    Keith

    ---

    Hello,

    I have a custom text view that overrides the copy and
    paste methods (-writeSelectionToPasteboard:type:) etc.
    I have to do this because my text can have custom
    attributes applied, and if I don't use my own
    pasteboard type these will get lost.

    However, one of my users has noticed that this causes
    an annoying "bug": the default behaviour of an
    NSTextView is that if you double-click on a word to
    select it and then drag and drop the word (or phrase)
    somewhere else, spaces will be inserted around it as
    necessary ("smartly"). But because I am overriding the
    methods that do all this, this behaviour is broken in
    my custom text view.

    So, my question is, how do I go about reimplementing
    this behaviour? ie. How do I detect whether the user
    has double-clicked the word to select it, and
    therefore know that I have to check the context of the
    word when copying and pasting? Is there any way of
    doing this, or any other solution?

    Many thanks,
    Keith

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • > This seems to work. If any text experts out there can
    > tell me whether this is the best way around this
    > problem or if there is a better solution, I would
    > still be grateful. :)

    This constant is not in any public headers that I've seen, but check the pboard for @"NeXT smart paste pasteboard type". Cheers,

    ~Martin
previous month october 2006 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