From 990e75bacfda7e70eb17d29e6437567657f154cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 12 Feb 2004 03:07:15 +0000 Subject: [PATCH] Started to implement the DataView class that will be the view to the editor. For your entertaining it already shows the first block of the file/device. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6567 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/diskprobe/DataView.cpp | 145 ++++++++++++++++++++++++++++++++ src/apps/diskprobe/DataView.h | 39 +++++++++ src/apps/diskprobe/Jamfile | 1 + 3 files changed, 185 insertions(+) create mode 100644 src/apps/diskprobe/DataView.cpp create mode 100644 src/apps/diskprobe/DataView.h diff --git a/src/apps/diskprobe/DataView.cpp b/src/apps/diskprobe/DataView.cpp new file mode 100644 index 0000000000..0645b590f8 --- /dev/null +++ b/src/apps/diskprobe/DataView.cpp @@ -0,0 +1,145 @@ +/* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the OpenBeOS License. +*/ + + +#include "DataView.h" +#include "DataEditor.h" + +#include + + +static const uint32 kBlockSize = 16; + + +DataView::DataView(BRect rect, DataEditor &editor) + : BView(rect, "dataView", B_FOLLOW_NONE, B_WILL_DRAW), + fEditor(editor) +{ + fPositionLength = 4; + fDataSize = fEditor.ViewSize(); + fData = (uint8 *)malloc(fDataSize); + fFontHeight = 15; + + UpdateFromEditor(); + + SetFont(be_fixed_font); +} + + +DataView::~DataView() +{ +} + + +void +DataView::DetachedFromWindow() +{ + fEditor.StopWatching(this); +} + + +void +DataView::AttachedToWindow() +{ + fEditor.StartWatching(this); +} + + +void +DataView::UpdateFromEditor(BMessage */*message*/) +{ + if (fData == NULL) + return; + + if (fEditor.Lock()) { + fOffset = fEditor.ViewOffset(); + + const uint8 *data; + if (fEditor.GetViewBuffer(&data) == B_OK) + memcpy(fData, data, fDataSize); + + fEditor.Unlock(); + } +} + + +void +DataView::MessageReceived(BMessage *message) +{ + switch (message->what) { + case B_OBSERVER_NOTICE_CHANGE: { + int32 what; + if (message->FindInt32(B_OBSERVE_WHAT_CHANGE, &what) != B_OK) + break; + + switch (what) { + case kMsgDataEditorUpdate: + UpdateFromEditor(message); + break; + } + break; + } + + default: + BView::MessageReceived(message); + } +} + + +void +DataView::ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size) +{ + line += sprintf(line, "%0*Lx: ", (int)fPositionLength, offset); + + for (uint32 i = 0; i < kBlockSize; i++) { + if (!(i % 4)) + *line++ = ' '; + + if (i >= size) { + strcpy(line, " "); + line += 2; + } else + line += sprintf(line, "%02x", *(unsigned char *)(buffer + i)); + } + + strcpy(line, " "); + line += 3; + + for (uint32 i = 0; i < kBlockSize; i++) { + if (i < size) { + char c = buffer[i]; + + if (c < 30) + *line++ = '.'; + else + *line++ = c; + } else + break; + } + + *line = '\0'; +} + + +void +DataView::Draw(BRect updateRect) +{ + if (fData == NULL) + return; + + // ToDo: take "updateRect" into account! + + char line[255]; + BPoint location(8, fFontHeight); + + for (uint32 i = 0; i < fDataSize; i += kBlockSize) { + ConvertLine(line, fOffset + i, fData + i, fDataSize - i); + DrawString(line, location); + + location.y += fFontHeight; + } + // ToDo: clear unused space! +} + diff --git a/src/apps/diskprobe/DataView.h b/src/apps/diskprobe/DataView.h new file mode 100644 index 0000000000..fb1448413d --- /dev/null +++ b/src/apps/diskprobe/DataView.h @@ -0,0 +1,39 @@ +/* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the OpenBeOS License. +*/ +#ifndef DATA_VIEW_H +#define DATA_VIEW_H + + +#include +#include +#include + + +class DataEditor; + + +class DataView : public BView { + public: + DataView(BRect rect, DataEditor &editor); + virtual ~DataView(); + + virtual void DetachedFromWindow(); + virtual void AttachedToWindow(); + virtual void MessageReceived(BMessage *message); + virtual void Draw(BRect updateRect); + + private: + void UpdateFromEditor(BMessage *message = NULL); + void ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size); + + DataEditor &fEditor; + int32 fPositionLength; + uint8 *fData; + uint32 fDataSize; + off_t fOffset; + float fFontHeight; +}; + +#endif /* DATA_VIEW_H */ diff --git a/src/apps/diskprobe/Jamfile b/src/apps/diskprobe/Jamfile index 47717d34b7..5861b55f5d 100644 --- a/src/apps/diskprobe/Jamfile +++ b/src/apps/diskprobe/Jamfile @@ -7,6 +7,7 @@ AddResources DiskProbe : DiskProbe.rdef ; App DiskProbe : DiskProbe.cpp DataEditor.cpp + DataView.cpp ProbeWindow.cpp ProbeView.cpp OpenWindow.cpp