Question

So if I have an NSView based tableview and inside the views are NSTextViews which are non-editable but selectable...

how can I get that nice functionality of command-A selects all the text? I don't mean row selection. I have row selection disabled for the tableview. I mean highlighting the text in blue so you can copy it to your clipboard. But not just 1 NSTextView's text from one row, all of them from all the rows.

And in addition to command-A click and drag should do this too. But out of the box it seems I can only select one row's text. Here is video showing problem:

https://dl.dropboxusercontent.com/u/2510380/table.mov (i keep clicking and dragging but can't highlight text on the next row)

here are two mac apps (skype and gabble) that do this:

https://dl.dropboxusercontent.com/u/2510380/skype.mov

and

https://dl.dropboxusercontent.com/u/2510380/gabble.mov

Assuming they are NOT using WebViews with just HTML inside, how do you get this control over the clipboard? i.e. in Skype you select the text and only the conversation is highlighted, not the timestamp of each message. Also the text copied to the clipboard is formatted very nicely. Can you point me in the right direction to reverse engineer skype?

Was it helpful?

Solution

Unfortunately there's no way to do this easily. This is because only ONE control can be the first responder at a time. This means that, though you can have selection in multiple text views, there are several problems:

  1. Only one text view's text will actually be highlighted with the "live" highlight color; the others will have the gray highlight of non-focused controls.

  2. Copy commands will only apply to the first responder text view.

  3. Drag session starts will be initiated from the control the mouse was actually pointing at (irrespective of first responder) and will only drag that control's text.

  4. In a view-based table view, the controls may not even "exist" for a row not currently displayed, so it'll never get the message unless you forcibly create each row, which could be costly for a large table.

Knowing all this, you might be able to "fake it" by having your controller be complicit in a text view and table view subclass's special handling of a select-all message when it's first responder. On receiving this message, the text view subclass can call super then notify the controller (to get its default behavior AND to let you know it happened), at which point the controller can turn around and send the command to all (existing) text views. Highlighting can be spoofed by overriding the text view's drawing and a drag initiation could defer to a delegate (the controller), which would handle writing ALL the strings from your model to the pasteboard (not even touching the text views in possibly-nonexistent row views). The table view subclass would simply pass the same select-all message to the controller without calling super (and even forcibly making sure nothing is selected before returning for good measure).

I hope this helps. If I've forgotten any of your requirements, let me know.

OTHER TIPS

Try like this:-

First create button programatically then write this code after you create button and also write this code in your load method or awakefromnib method.

   NSButton *Buttn=// alloc initwithframe;

  [Buttn setKeyEquivalentModifierMask: 
NSCommandKeyMask];
[Buttn setKeyEquivalent:@"A"];
[Buttn     
setAction:@selector(yourmeth:)];
 [Buttn setTarget:self];

 // now when you press cmd a write 
 below code in action method
- (void)selectRowIndexes:(NSIndexSet
 *)indexes byExtendingSelection:
   (BOOL)extend
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top