TextDocumentView: Added plain-text copying to the clipboard

* Make the focus view on MouseDown()
 * Also added reaction to focus and window activation changes.
This commit is contained in:
Stephan Aßmus
2013-12-08 14:17:05 +01:00
parent a9fcf3d6b5
commit 1aa7edf0c3
2 changed files with 103 additions and 9 deletions
@@ -8,6 +8,7 @@
#include <algorithm>
#include <stdio.h>
#include <Clipboard.h>
#include <Cursor.h>
#include <ScrollBar.h>
#include <Shape.h>
@@ -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
@@ -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;