From 609442f8f8e23eb74c91a28122c47901318ea257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 26 Feb 2004 02:05:41 +0000 Subject: [PATCH] Moved the B_COPY code into a separate function. Implemented B_PASTE. Now also changes the selection to the cursor position in the hex editor when you change the first part of the byte. No need to hold the editor lock for Replace(). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6743 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/diskprobe/DataView.cpp | 92 ++++++++++++++++++++++----------- src/apps/diskprobe/DataView.h | 3 ++ 2 files changed, 65 insertions(+), 30 deletions(-) diff --git a/src/apps/diskprobe/DataView.cpp b/src/apps/diskprobe/DataView.cpp index 19c9206ee6..f0b6ad55ae 100644 --- a/src/apps/diskprobe/DataView.cpp +++ b/src/apps/diskprobe/DataView.cpp @@ -12,6 +12,7 @@ #include #include #include +#include typedef uint32 addr_t; @@ -151,6 +152,7 @@ DataView::MessageReceived(BMessage *message) break; case kMsgDataEditorOffsetChange: + fOffset = fEditor.ViewOffset(); SetSelection(0, 0); break; @@ -169,30 +171,11 @@ DataView::MessageReceived(BMessage *message) break; case B_COPY: - { - if (!be_clipboard->Lock()) - break; - - be_clipboard->Clear(); - - BMessage *clip; - if ((clip = be_clipboard->Data()) != NULL) { - uint8 *data = fData + fStart; - size_t length = fEnd + 1 - fStart; - - clip->AddData(B_FILE_MIME_TYPE, B_MIME_TYPE, data, length); - - if (is_valid_ascii(data, length)) - clip->AddData("text/plain", B_MIME_TYPE, data, length); - - be_clipboard->Commit(); - } - - be_clipboard->Unlock(); + Copy(); break; - } case B_PASTE: + Paste(); break; case B_UNDO: @@ -209,6 +192,57 @@ DataView::MessageReceived(BMessage *message) } +void +DataView::Copy() +{ + if (!be_clipboard->Lock()) + return; + + be_clipboard->Clear(); + + BMessage *clip; + if ((clip = be_clipboard->Data()) != NULL) { + uint8 *data = fData + fStart; + size_t length = fEnd + 1 - fStart; + + clip->AddData(B_FILE_MIME_TYPE, B_MIME_TYPE, data, length); + + if (is_valid_ascii(data, length)) + clip->AddData("text/plain", B_MIME_TYPE, data, length); + + be_clipboard->Commit(); + } + + be_clipboard->Unlock(); +} + + +void +DataView::Paste() +{ + if (!be_clipboard->Lock()) + return; + + const void *data; + ssize_t length; + BMessage *clip; + if ((clip = be_clipboard->Data()) != NULL + && (clip->FindData(B_FILE_MIME_TYPE, B_MIME_TYPE, &data, &length) == B_OK + || clip->FindData("text/plain", B_MIME_TYPE, &data, &length) == B_OK)) { + // we have valid data, but it could still be too + // large to to fit in the file + if (fOffset + fStart + length > fFileSize) + length = fFileSize - fOffset; + + if (fEditor.Replace(fOffset + fStart, (const uint8 *)data, length) == B_OK) + SetSelection(fStart + length, fStart + length); + } else + beep(); + + be_clipboard->Unlock(); +} + + void DataView::ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size) { @@ -918,15 +952,15 @@ DataView::KeyDown(const char *bytes, int32 numBytes) case B_BACKSPACE: if (fBitPosition == 0) SetSelection(fStart - 1, fStart - 1); - + if (fFocus == kHexFocus) fBitPosition = (fBitPosition + 4) % 8; // supposed to fall through case B_DELETE: - { - BAutolock locker(fEditor); - + SetSelection(fStart, fStart); + // to make sure only the cursor is selected + if (fFocus == kHexFocus) { const uint8 *data = DataAt(fStart); if (data == NULL) @@ -939,12 +973,8 @@ DataView::KeyDown(const char *bytes, int32 numBytes) } else fEditor.Replace(fOffset + fStart, (const uint8 *)"", 1); break; - } default: - { - BAutolock locker(fEditor); - if (fFocus == kHexFocus) { // only hexadecimal characters are allowed to be entered const uint8 *data = DataAt(fStart); @@ -956,6 +986,9 @@ DataView::KeyDown(const char *bytes, int32 numBytes) if (data == NULL || (number = (addr_t)strchr(hexNumbers, c)) == NULL) break; + SetSelection(fStart, fStart); + // to make sure only the cursor is selected + number -= (addr_t)hexNumbers; fBitPosition = (fBitPosition + 4) % 8; @@ -969,7 +1002,6 @@ DataView::KeyDown(const char *bytes, int32 numBytes) SetSelection(fStart + 1, fStart + 1); } break; - } } } diff --git a/src/apps/diskprobe/DataView.h b/src/apps/diskprobe/DataView.h index c673fcec6e..7743432c65 100644 --- a/src/apps/diskprobe/DataView.h +++ b/src/apps/diskprobe/DataView.h @@ -75,6 +75,9 @@ class DataView : public BView { void UpdateFromEditor(BMessage *message = NULL); void ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size); + void Copy(); + void Paste(); + DataEditor &fEditor; int32 fPositionLength; uint8 *fData;