2016-03-21 16 views
6

私はNSTextViewベースのコンポーネントを持っていて、そのシングルクリックを無効にしたいので、挿入ポイントはこれらの1回のクリックで影響を受けませんが、コピー&ペースト用のテキストを選択することができます:そこに挿入ポイントがあると:NSTextView:シングルクリックを無効にする方法はありますが、コピーアンドペーストの選択はできますか?

  1. シングルクリックは
  2. コピー&ペーストが可能であり、我々は、デフォルトのターミナルアプリを持っているまさにである私がしたい挿入ポイントに

には影響しません。何もしませんマウスのクリックで変更することはできませんが、コピー&ペースト用のテキストの選択は可能です。

私は- (void)mouseDown:(NSEvent *)theEventメソッドを見てみましたが、役に立たないものは見つかりませんでした。

+0

は編集= NO、選択= YES十分だろうか?あなたは何か違うものが必要ですか?あなたは明確にすることができますか? – Remizorrr

+0

'insertText:replacementRange:'は 'editable = NO'で動作しますが、テキストを追加するわけではありません。 –

+0

@StanislavPankevichは、貼り付け直前にYESに編集可能に設定してから、NOに戻すことができます。 – faviomob

答えて

1

私は、この種の動作を達成するためにハックの回避策を見つけました。私はdemo projectを作成しました。関連するクラスはTerminalLikeTextViewです。このソリューションは完璧に機能しますが、私はまだより良いソリューションを望んでいます.Hackyが少なく、NSTextViewの内部機構に依存しないようにしてください。

重要なステップは次のとおりです。

1)を設定しmouseDownFlagにYESにダウンマウスの前とNOの後:

@property (assign, nonatomic) BOOL mouseDownFlag; 

- (void)mouseDown:(NSEvent *)theEvent { 
    self.mouseDownFlag = YES; 

    [super mouseDown:theEvent]; 

    self.mouseDownFlag = NO; 
} 

2)updateInsertionPointStateAndRestartTimerメソッドから早期復帰を更新から挿入ポイントを防ぐために:

- (void)updateInsertionPointStateAndRestartTimer:(BOOL)flag { 
    if (self.mouseDownFlag) { 
     return; 
    } 

    [super updateInsertionPointStateAndRestartTimer:flag]; 
} 

3)最初の2つのステップではマウスで移動しない挿入ポイントが作成されますが、selectionRangeはまだ変更されますそれを追跡するために、EED:必要に応じて

static const NSUInteger kCursorLocationSnapshotNotExists = NSUIntegerMax; 
@property (assign, nonatomic) NSUInteger cursorLocationSnapshot; 

#pragma mark - <NSTextViewDelegate> 

- (NSRange)textView:(NSTextView *)textView willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange toCharacterRange:(NSRange)newSelectedCharRange { 

    if (self.mouseDownFlag && self.cursorLocationSnapshot == kCursorLocationSnapshotNotExists) { 
     self.cursorLocationSnapshot = oldSelectedCharRange.location; 
    } 

    return newSelectedCharRange; 
} 

4)キーを使用して印刷しようとすると、場所を復元します:

- (void)keyDown:(NSEvent *)event { 
    NSString *characters = event.characters; 

    [self insertTextToCurrentPosition:characters]; 
} 

- (void)insertTextToCurrentPosition:(NSString *)text { 
    if (self.cursorLocationSnapshot != kCursorLocationSnapshotNotExists) { 
     self.selectedRange = NSMakeRange(self.cursorLocationSnapshot, 0); 
     self.cursorLocationSnapshot = kCursorLocationSnapshotNotExists; 
    } 

    [self insertText:text replacementRange:NSMakeRange(self.selectedRange.location, 0)]; 
} 
関連する問題