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:
@@ -8,6 +8,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <Clipboard.h>
|
||||||
#include <Cursor.h>
|
#include <Cursor.h>
|
||||||
#include <ScrollBar.h>
|
#include <ScrollBar.h>
|
||||||
#include <Shape.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
|
void
|
||||||
TextDocumentView::Draw(BRect updateRect)
|
TextDocumentView::Draw(BRect updateRect)
|
||||||
{
|
{
|
||||||
@@ -54,13 +69,7 @@ TextDocumentView::Draw(BRect updateRect)
|
|||||||
|
|
||||||
int32 start;
|
int32 start;
|
||||||
int32 end;
|
int32 end;
|
||||||
if (fSelectionAnchorOffset <= fCaretOffset) {
|
GetSelection(start, end);
|
||||||
start = fSelectionAnchorOffset;
|
|
||||||
end = fCaretOffset;
|
|
||||||
} else {
|
|
||||||
start = fCaretOffset;
|
|
||||||
end = fSelectionAnchorOffset;
|
|
||||||
}
|
|
||||||
|
|
||||||
BShape shape;
|
BShape shape;
|
||||||
_GetSelectionShape(shape, start, end);
|
_GetSelectionShape(shape, start, end);
|
||||||
@@ -69,8 +78,10 @@ TextDocumentView::Draw(BRect updateRect)
|
|||||||
SetLineMode(B_ROUND_CAP, B_ROUND_JOIN);
|
SetLineMode(B_ROUND_CAP, B_ROUND_JOIN);
|
||||||
MovePenTo(fInsetLeft - 0.5f, fInsetTop - 0.5f);
|
MovePenTo(fInsetLeft - 0.5f, fInsetTop - 0.5f);
|
||||||
|
|
||||||
SetHighColor(30, 30, 30);
|
if (IsFocus() && Window() != NULL && Window()->IsActive()) {
|
||||||
FillShape(&shape);
|
SetHighColor(30, 30, 30);
|
||||||
|
FillShape(&shape);
|
||||||
|
}
|
||||||
|
|
||||||
SetHighColor(40, 40, 40);
|
SetHighColor(40, 40, 40);
|
||||||
StrokeShape(&shape);
|
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
|
void
|
||||||
TextDocumentView::MouseDown(BPoint where)
|
TextDocumentView::MouseDown(BPoint where)
|
||||||
{
|
{
|
||||||
|
MakeFocus();
|
||||||
|
|
||||||
int32 modifiers = 0;
|
int32 modifiers = 0;
|
||||||
if (Window() != NULL && Window()->CurrentMessage() != NULL)
|
if (Window() != NULL && Window()->CurrentMessage() != NULL)
|
||||||
Window()->CurrentMessage()->FindInt32("modifiers", &modifiers);
|
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
|
// #pragma mark - private
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,15 +12,22 @@
|
|||||||
#include "TextDocumentLayout.h"
|
#include "TextDocumentLayout.h"
|
||||||
|
|
||||||
|
|
||||||
|
class BClipboard;
|
||||||
|
|
||||||
|
|
||||||
class TextDocumentView : public BView {
|
class TextDocumentView : public BView {
|
||||||
public:
|
public:
|
||||||
TextDocumentView(const char* name = NULL);
|
TextDocumentView(const char* name = NULL);
|
||||||
virtual ~TextDocumentView();
|
virtual ~TextDocumentView();
|
||||||
|
|
||||||
|
virtual void MessageReceived(BMessage* message);
|
||||||
|
|
||||||
virtual void Draw(BRect updateRect);
|
virtual void Draw(BRect updateRect);
|
||||||
|
|
||||||
virtual void AttachedToWindow();
|
virtual void AttachedToWindow();
|
||||||
virtual void FrameResized(float width, float height);
|
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 MouseDown(BPoint where);
|
||||||
virtual void MouseUp(BPoint where);
|
virtual void MouseUp(BPoint where);
|
||||||
@@ -46,6 +53,11 @@ public:
|
|||||||
void SetCaret(const BPoint& where,
|
void SetCaret(const BPoint& where,
|
||||||
bool extendSelection);
|
bool extendSelection);
|
||||||
|
|
||||||
|
bool HasSelection() const;
|
||||||
|
void GetSelection(int32& start, int32& end) const;
|
||||||
|
|
||||||
|
void Copy(BClipboard* clipboard);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float _TextLayoutWidth(float viewWidth) const;
|
float _TextLayoutWidth(float viewWidth) const;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user