From 56904be5167c10e26398d0deec6ae849edb8e9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 16 Dec 2004 01:14:42 +0000 Subject: [PATCH] The DataView can now also accept drop messages from others and itself; the mouse is tracked when you're dragging over the view, and the affected area is selected. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10470 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/diskprobe/DataView.cpp | 62 +++++++++++++++++++++++++++++++-- src/apps/diskprobe/DataView.h | 3 ++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/apps/diskprobe/DataView.cpp b/src/apps/diskprobe/DataView.cpp index a094857fe9..03399fa611 100644 --- a/src/apps/diskprobe/DataView.cpp +++ b/src/apps/diskprobe/DataView.cpp @@ -60,7 +60,8 @@ DataView::DataView(BRect rect, DataEditor &editor) fMouseSelectionStart(-1), fKeySelectionStart(-1), fBitPosition(0), - fFitFontSize(false) + fFitFontSize(false), + fDragMessageSize(-1) { fPositionLength = 4; fStart = fEnd = 0; @@ -154,7 +155,16 @@ DataView::UpdateFromEditor(BMessage *message) } -void +bool +DataView::AcceptsDrop(const BMessage *message) +{ + return !fEditor.IsReadOnly() + && (message->HasData("text/plain", B_MIME_TYPE) + || message->HasData(B_FILE_MIME_TYPE, B_MIME_TYPE)); +} + + +void DataView::MessageReceived(BMessage *message) { switch (message->what) { @@ -224,6 +234,20 @@ DataView::MessageReceived(BMessage *message) fEditor.Redo(); break; + case B_MIME_DATA: + if (AcceptsDrop(message)) { + const void *data; + ssize_t size; + if (message->FindData("text/plain", B_MIME_TYPE, &data, &size) == B_OK + || message->FindData(B_FILE_MIME_TYPE, B_MIME_TYPE, &data, &size) == B_OK) { + if (fEditor.Replace(fOffset + fStart, (const uint8 *)data, size) != B_OK) + SetSelection(fStoredStart, fStoredEnd); + + fDragMessageSize = -1; + } + } + break; + default: BView::MessageReceived(message); } @@ -933,6 +957,10 @@ DataView::InitiateDrag(view_focus focus) frame = bounds & frame; DragMessage(drag, frame, NULL); + + fStoredStart = fStart; + fStoredEnd = fEnd; + fDragMessageSize = length; } @@ -982,8 +1010,36 @@ DataView::MouseDown(BPoint where) void -DataView::MouseMoved(BPoint where, uint32 transit, const BMessage */*dragMessage*/) +DataView::MouseMoved(BPoint where, uint32 transit, const BMessage *dragMessage) { + if (transit == B_EXITED_VIEW && fDragMessageSize > 0) { + SetSelection(fStoredStart, fStoredEnd); + fDragMessageSize = -1; + } + + if (dragMessage && AcceptsDrop(dragMessage)) { + // handle drag message and tracking + + if (transit == B_ENTERED_VIEW) { + fStoredStart = fStart; + fStoredEnd = fEnd; + + const void *data; + ssize_t size; + if (dragMessage->FindData("text/plain", B_MIME_TYPE, &data, &size) == B_OK + || dragMessage->FindData("text/plain", B_MIME_TYPE, &data, &size) == B_OK) + fDragMessageSize = size; + } else if (fDragMessageSize > 0) { + view_focus newFocus; + int32 start = PositionAt(kNoFocus, where, &newFocus); + int32 end = start + fDragMessageSize - 1; + + SetSelection(start, end); + MakeVisible(start); + } + return; + } + if (fMouseSelectionStart == -1) return; diff --git a/src/apps/diskprobe/DataView.h b/src/apps/diskprobe/DataView.h index 8f38393ae2..2a7b3babd3 100644 --- a/src/apps/diskprobe/DataView.h +++ b/src/apps/diskprobe/DataView.h @@ -77,6 +77,7 @@ class DataView : public BView { void UpdateFromEditor(BMessage *message = NULL); void ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size); + bool AcceptsDrop(const BMessage *message); void InitiateDrag(view_focus focus); void Copy(); void Paste(); @@ -99,6 +100,8 @@ class DataView : public BView { int32 fKeySelectionStart; int32 fBitPosition; bool fFitFontSize; + int32 fDragMessageSize; + int32 fStoredStart, fStoredEnd; }; static const uint32 kMsgBaseType = 'base';