diff --git a/src/apps/diskprobe/DataView.cpp b/src/apps/diskprobe/DataView.cpp index 0645b590f8..28088428d8 100644 --- a/src/apps/diskprobe/DataView.cpp +++ b/src/apps/diskprobe/DataView.cpp @@ -11,16 +11,19 @@ static const uint32 kBlockSize = 16; +static const uint32 kHorizontalSpace = 8; +static const uint32 kVerticalSpace = 4; DataView::DataView(BRect rect, DataEditor &editor) - : BView(rect, "dataView", B_FOLLOW_NONE, B_WILL_DRAW), + : BView(rect, "dataView", B_FOLLOW_ALL, B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS), fEditor(editor) { fPositionLength = 4; fDataSize = fEditor.ViewSize(); fData = (uint8 *)malloc(fDataSize); - fFontHeight = 15; + + SetFontSize(12.0); UpdateFromEditor(); @@ -94,14 +97,11 @@ 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)); + line += sprintf(line, "%02x ", *(unsigned char *)(buffer + i)); } strcpy(line, " "); @@ -111,7 +111,7 @@ DataView::ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size if (i < size) { char c = buffer[i]; - if (c < 30) + if (c < ' ' || c == 0x7f) *line++ = '.'; else *line++ = c; @@ -132,7 +132,7 @@ DataView::Draw(BRect updateRect) // ToDo: take "updateRect" into account! char line[255]; - BPoint location(8, fFontHeight); + BPoint location(kHorizontalSpace, kVerticalSpace + fAscent); for (uint32 i = 0; i < fDataSize; i += kBlockSize) { ConvertLine(line, fOffset + i, fData + i, fDataSize - i); @@ -143,3 +143,46 @@ DataView::Draw(BRect updateRect) // ToDo: clear unused space! } + +void +DataView::SetFont(const BFont *font, uint32 properties) +{ + if (!font->IsFixed()) + return; + + BView::SetFont(font, properties); + + font_height fontHeight; + font->GetHeight(&fontHeight); + + fFontHeight = int32(fontHeight.ascent + fontHeight.descent + fontHeight.leading); + fAscent = fontHeight.ascent; +} + + +void +DataView::SetFontSize(float point) +{ + BFont font = be_fixed_font; + font.SetSize(point); + + SetFont(&font); +} + + +void +DataView::GetPreferredSize(float *_width, float *_height) +{ + if (_width) { + BFont font; + GetFont(&font); + *_width = font.StringWidth("w") * (kBlockSize * 4 + fPositionLength + 6) + + 2 * kHorizontalSpace; + } + + if (_height) { + *_height = fFontHeight * ((fDataSize + kBlockSize - 1) / kBlockSize) + + 2 * kVerticalSpace; + } +} + diff --git a/src/apps/diskprobe/DataView.h b/src/apps/diskprobe/DataView.h index fb1448413d..0d7b70949c 100644 --- a/src/apps/diskprobe/DataView.h +++ b/src/apps/diskprobe/DataView.h @@ -24,6 +24,11 @@ class DataView : public BView { virtual void MessageReceived(BMessage *message); virtual void Draw(BRect updateRect); + virtual void SetFont(const BFont *font, uint32 properties = B_FONT_ALL); + virtual void GetPreferredSize(float *_width, float *_height); + + void SetFontSize(float point); + private: void UpdateFromEditor(BMessage *message = NULL); void ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size); @@ -33,7 +38,8 @@ class DataView : public BView { uint8 *fData; uint32 fDataSize; off_t fOffset; - float fFontHeight; + float fAscent; + int32 fFontHeight; }; #endif /* DATA_VIEW_H */ diff --git a/src/apps/diskprobe/ProbeView.cpp b/src/apps/diskprobe/ProbeView.cpp index 7c7095ff30..dea5371e8e 100644 --- a/src/apps/diskprobe/ProbeView.cpp +++ b/src/apps/diskprobe/ProbeView.cpp @@ -361,9 +361,9 @@ ProbeView::ProbeView(BRect rect, entry_ref *ref, const char *attribute) rect.top = rect.bottom + 3; rect.bottom = Bounds().bottom - B_H_SCROLL_BAR_HEIGHT; rect.right -= B_V_SCROLL_BAR_WIDTH; - BView *view = new DataView(rect, fEditor); + fDataView = new DataView(rect, fEditor); - fScrollView = new BScrollView("scroller", view, B_FOLLOW_ALL, B_WILL_DRAW, true, true); + fScrollView = new BScrollView("scroller", fDataView, B_FOLLOW_ALL, B_WILL_DRAW, true, true); AddChild(fScrollView); } @@ -373,6 +373,20 @@ ProbeView::~ProbeView() } +void +ProbeView::UpdateSizeLimits() +{ + if (Window() == NULL) + return; + + float width, height; + fDataView->GetPreferredSize(&width, &height); + + Window()->SetSizeLimits(200, width + B_V_SCROLL_BAR_WIDTH, + 200, height + fHeaderView->Frame().bottom + 1 + B_H_SCROLL_BAR_HEIGHT + Frame().top); +} + + void ProbeView::DetachedFromWindow() { diff --git a/src/apps/diskprobe/ProbeView.h b/src/apps/diskprobe/ProbeView.h index 70f674e631..bbf6208058 100644 --- a/src/apps/diskprobe/ProbeView.h +++ b/src/apps/diskprobe/ProbeView.h @@ -16,6 +16,7 @@ class BScrollView; class HeaderView; +class DataView; class ProbeView : public BView { @@ -29,9 +30,12 @@ class ProbeView : public BView { void AddFileMenuItems(BMenu *menu, int32 index); + void UpdateSizeLimits(); + private: DataEditor fEditor; HeaderView *fHeaderView; + DataView *fDataView; BScrollView *fScrollView; }; diff --git a/src/apps/diskprobe/ProbeWindow.cpp b/src/apps/diskprobe/ProbeWindow.cpp index 272d7a66c7..e4b9c807c2 100644 --- a/src/apps/diskprobe/ProbeWindow.cpp +++ b/src/apps/diskprobe/ProbeWindow.cpp @@ -70,6 +70,8 @@ ProbeWindow::ProbeWindow(BRect rect, entry_ref *ref, const char *attribute) ProbeView *probeView = new ProbeView(rect, ref, attribute); probeView->AddFileMenuItems(menu, menu->CountItems() - 4); AddChild(probeView); + + probeView->UpdateSizeLimits(); }