From 1aa7edf0c31cf8f0c0c657c8c98b9dec686df6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sat, 7 Dec 2013 22:51:08 +0100 Subject: [PATCH] TextDocumentView: Added plain-text copying to the clipboard * Make the focus view on MouseDown() * Also added reaction to focus and window activation changes. --- .../haiku-depot/textview/TextDocumentView.cpp | 100 ++++++++++++++++-- .../haiku-depot/textview/TextDocumentView.h | 12 +++ 2 files changed, 103 insertions(+), 9 deletions(-) diff --git a/src/apps/haiku-depot/textview/TextDocumentView.cpp b/src/apps/haiku-depot/textview/TextDocumentView.cpp index 727801e141..7ef8f99c65 100644 --- a/src/apps/haiku-depot/textview/TextDocumentView.cpp +++ b/src/apps/haiku-depot/textview/TextDocumentView.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -41,6 +42,20 @@ TextDocumentView::~TextDocumentView() } +void +TextDocumentView::MessageReceived(BMessage* message) +{ + switch (message->what) { + case B_COPY: + Copy(be_clipboard); + break; + + default: + BView::MessageReceived(message); + } +} + + void TextDocumentView::Draw(BRect updateRect) { @@ -54,13 +69,7 @@ TextDocumentView::Draw(BRect updateRect) int32 start; int32 end; - if (fSelectionAnchorOffset <= fCaretOffset) { - start = fSelectionAnchorOffset; - end = fCaretOffset; - } else { - start = fCaretOffset; - end = fSelectionAnchorOffset; - } + GetSelection(start, end); BShape shape; _GetSelectionShape(shape, start, end); @@ -69,8 +78,10 @@ TextDocumentView::Draw(BRect updateRect) SetLineMode(B_ROUND_CAP, B_ROUND_JOIN); MovePenTo(fInsetLeft - 0.5f, fInsetTop - 0.5f); - SetHighColor(30, 30, 30); - FillShape(&shape); + if (IsFocus() && Window() != NULL && Window()->IsActive()) { + SetHighColor(30, 30, 30); + FillShape(&shape); + } SetHighColor(40, 40, 40); StrokeShape(&shape); @@ -92,9 +103,27 @@ TextDocumentView::FrameResized(float width, float height) } +void +TextDocumentView::WindowActivated(bool active) +{ + Invalidate(); +} + + +void +TextDocumentView::MakeFocus(bool focus) +{ + if (focus != IsFocus()) + Invalidate(); + BView::MakeFocus(focus); +} + + void TextDocumentView::MouseDown(BPoint where) { + MakeFocus(); + int32 modifiers = 0; if (Window() != NULL && Window()->CurrentMessage() != NULL) Window()->CurrentMessage()->FindInt32("modifiers", &modifiers); @@ -237,6 +266,59 @@ TextDocumentView::SetCaret(const BPoint& location, bool extendSelection) } +bool +TextDocumentView::HasSelection() const +{ + return fSelectionAnchorOffset != fCaretOffset; +} + + +void +TextDocumentView::GetSelection(int32& start, int32& end) const +{ + if (fSelectionAnchorOffset <= fCaretOffset) { + start = fSelectionAnchorOffset; + end = fCaretOffset; + } else { + start = fCaretOffset; + end = fSelectionAnchorOffset; + } +} + + +void +TextDocumentView::Copy(BClipboard* clipboard) +{ + if (fSelectionAnchorOffset == fCaretOffset + || fTextDocument.Get() == NULL) { + // Nothing to copy, don't clear clipboard contents for now reason. + return; + } + + if (clipboard == NULL || !clipboard->Lock()) + return; + + clipboard->Clear(); + + BMessage* clip = clipboard->Data(); + if (clip != NULL) { + int32 start; + int32 end; + GetSelection(start, end); + + BString text = fTextDocument->GetText(start, end - start); + clip->AddData("text/plain", B_MIME_TYPE, text.String(), + text.Length()); + + // TODO: Support for "application/x-vnd.Be-text_run_array" + + clipboard->Commit(); + } + + clipboard->Unlock(); +} + + // #pragma mark - private diff --git a/src/apps/haiku-depot/textview/TextDocumentView.h b/src/apps/haiku-depot/textview/TextDocumentView.h index 9860284a58..7cf4fe6738 100644 --- a/src/apps/haiku-depot/textview/TextDocumentView.h +++ b/src/apps/haiku-depot/textview/TextDocumentView.h @@ -12,15 +12,22 @@ #include "TextDocumentLayout.h" +class BClipboard; + + class TextDocumentView : public BView { public: TextDocumentView(const char* name = NULL); virtual ~TextDocumentView(); + virtual void MessageReceived(BMessage* message); + virtual void Draw(BRect updateRect); virtual void AttachedToWindow(); virtual void FrameResized(float width, float height); + virtual void WindowActivated(bool active); + virtual void MakeFocus(bool focus = true); virtual void MouseDown(BPoint where); virtual void MouseUp(BPoint where); @@ -46,6 +53,11 @@ public: void SetCaret(const BPoint& where, bool extendSelection); + bool HasSelection() const; + void GetSelection(int32& start, int32& end) const; + + void Copy(BClipboard* clipboard); + private: float _TextLayoutWidth(float viewWidth) const;