Skip to content

Commit

Permalink
DiskProbe: improved initial window size.
Browse files Browse the repository at this point in the history
* Also some automatic whitespace cleanup.
  • Loading branch information
axeld committed May 11, 2015
1 parent 748c0f3 commit d0f27e4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 42 deletions.
48 changes: 29 additions & 19 deletions src/apps/diskprobe/DataView.cpp
@@ -1,30 +1,29 @@
/*
* Copyright 2004-2009, Axel Dörfler, axeld@pinc-software.de.
* Copyright 2004-2015, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/


#include "DataView.h"
#include "DataEditor.h"

#include <stdio.h>
#include <stdlib.h>

#include <Window.h>
#include <ScrollBar.h>
#include <Autolock.h>
#include <Beep.h>
#include <Clipboard.h>
#include <Mime.h>
#include <Beep.h>
#include <ScrollBar.h>
#include <Window.h>

#include "DataEditor.h"

#ifndef __HAIKU__
typedef uint32 addr_t;
#endif

static const uint32 kBlockSize = 16;
// TODO: use variable spacing
static const uint32 kHorizontalSpace = 8;
static const uint32 kVerticalSpace = 4;
static const uint32 kPositionLength = 4;

static const uint32 kBlockSpace = 3;
static const uint32 kHexByteWidth = 3;
Expand Down Expand Up @@ -106,7 +105,6 @@ DataView::DataView(DataEditor &editor)
fFitFontSize(false),
fDragMessageSize(-1)
{
fPositionLength = 4;
fStart = fEnd = 0;

if (fEditor.Lock()) {
Expand Down Expand Up @@ -357,7 +355,7 @@ DataView::ConvertLine(char *line, off_t offset, const uint8 *buffer, size_t size
}

line += sprintf(line, fBase == kHexBase ? "%0*" B_PRIxOFF": " : "%0*"
B_PRIdOFF": ", (int)fPositionLength, offset);
B_PRIdOFF": ", (int)kPositionLength, offset);

for (uint32 i = 0; i < kBlockSize; i++) {
if (i >= size) {
Expand Down Expand Up @@ -412,7 +410,7 @@ BRect
DataView::DataBounds(bool inView) const
{
return BRect(0, 0,
fCharWidth * (kBlockSize * 4 + fPositionLength + 6) + 2 * kHorizontalSpace,
fCharWidth * (kBlockSize * 4 + kPositionLength + 6) + 2 * kHorizontalSpace,
fFontHeight * (((inView ? fSizeInView : fDataSize) + kBlockSize - 1) / kBlockSize)
+ 2 * kVerticalSpace);
}
Expand All @@ -434,7 +432,7 @@ DataView::PositionAt(view_focus focus, BPoint point, view_focus *_newFocus)
else if (point.y >= bounds.bottom - kVerticalSpace)
point.y = bounds.bottom - kVerticalSpace - 1;

float left = fCharWidth * (fPositionLength + kBlockSpace) + kHorizontalSpace;
float left = fCharWidth * (kPositionLength + kBlockSpace) + kHorizontalSpace;
float hexWidth = fCharWidth * kBlockSize * kHexByteWidth;
float width = fCharWidth;

Expand Down Expand Up @@ -478,11 +476,11 @@ DataView::SelectionFrame(view_focus which, int32 start, int32 end)

if (which == kHexFocus) {
spacing = fCharWidth / 2;
left = width * (fPositionLength + kBlockSpace);
left = width * (kPositionLength + kBlockSpace);
width *= kHexByteWidth;
byteWidth *= 2;
} else
left = width * (fPositionLength + 2 * kBlockSpace + kHexByteWidth * kBlockSize);
left = width * (kPositionLength + 2 * kBlockSpace + kHexByteWidth * kBlockSize);

left += kHorizontalSpace;
float startInLine = (start % kBlockSize) * width;
Expand Down Expand Up @@ -837,6 +835,18 @@ DataView::DataAt(int32 start)
}


/*static*/ int32
DataView::WidthForFontSize(float size)
{
BFont font = be_fixed_font;
font.SetSize(size);

float charWidth = font.StringWidth("w");
return (int32)ceilf(charWidth * (kBlockSize * 4 + kPositionLength + 6)
+ 2 * kHorizontalSpace);
}


void
DataView::SetBase(base_type type)
{
Expand Down Expand Up @@ -942,24 +952,24 @@ DataView::FrameResized(float width, float height)
if (fFitFontSize) {
// adapt the font size to fit in the view's bounds
float oldSize = FontSize();
BFont font = be_fixed_font;
float steps = 0.5f;

float size;
for (size = 1.f; size < 100; size += steps) {
font.SetSize(size);
float charWidth = font.StringWidth("w");
if (charWidth * (kBlockSize * 4 + fPositionLength + 6) + 2 * kHorizontalSpace > width)
int32 preferredWidth = WidthForFontSize(size);
if (preferredWidth > width)
break;

if (size > 6)
steps = 1.0f;
}
size -= steps;
font.SetSize(size);

if (oldSize != size) {
BFont font = be_fixed_font;
font.SetSize(size);
SetFont(&font);

Invalidate();
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/apps/diskprobe/DataView.h
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2009, Axel Dörfler, axeld@pinc-software.de.
* Copyright 2004-2015, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
#ifndef DATA_VIEW_H
Expand All @@ -13,17 +13,20 @@

class DataEditor;


enum base_type {
kHexBase = 16,
kDecimalBase = 10
};


enum view_focus {
kNoFocus,
kHexFocus,
kAsciiFocus
};


class DataView : public BView {
public:
DataView(DataEditor& editor);
Expand Down Expand Up @@ -65,6 +68,8 @@ class DataView : public BView {

const uint8* DataAt(int32 start);

static int32 WidthForFontSize(float size);

private:
BRect DataBounds(bool inView = false) const;
BRect SelectionFrame(view_focus which, int32 start,
Expand All @@ -90,7 +95,6 @@ class DataView : public BView {
void Paste();

DataEditor& fEditor;
int32 fPositionLength;
uint8* fData;
size_t fDataSize;
off_t fFileSize;
Expand Down
53 changes: 32 additions & 21 deletions src/apps/diskprobe/DiskProbe.cpp
@@ -1,16 +1,13 @@
/*
* Copyright 2004-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Copyright 2004-2015, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/


#include "DiskProbe.h"
#include "DataEditor.h"
#include "DataView.h"
#include "FileWindow.h"
#include "AttributeWindow.h"
#include "OpenWindow.h"
#include "FindWindow.h"

#include <stdio.h>
#include <string.h>

#include <AboutWindow.h>
#include <Alert.h>
Expand All @@ -21,17 +18,24 @@
#include <Entry.h>
#include <FilePanel.h>
#include <FindDirectory.h>
#include <LayoutUtils.h>
#include <Locale.h>
#include <Path.h>
#include <Screen.h>
#include <TextView.h>

#include <stdio.h>
#include <string.h>
#include "DataEditor.h"
#include "DataView.h"
#include "FileWindow.h"
#include "AttributeWindow.h"
#include "OpenWindow.h"
#include "FindWindow.h"


#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "DiskProbe"


const char *kSignature = "application/x-vnd.Haiku-DiskProbe";

static const uint32 kMsgDiskProbeSettings = 'DPst';
Expand All @@ -45,11 +49,13 @@ struct disk_probe_settings {
int32 flags;
};


enum disk_probe_flags {
kCaseSensitive = 0x01, // this flag alone is R5 DiskProbe settings compatible
kHexFindMode = 0x02,
};


class Settings {
public:
Settings();
Expand Down Expand Up @@ -100,11 +106,16 @@ Settings::Settings()
fMessage(kMsgDiskProbeSettings),
fUpdated(false)
{
float fontSize = be_plain_font->Size();
int32 windowWidth = DataView::WidthForFontSize(fontSize) + 20;
// TODO: make scrollbar width variable

BScreen screen;
fMessage.AddRect("window_frame", BRect(50, 50, screen.Frame().Width() - 50,
screen.Frame().Height() - 50));
fMessage.AddRect("window_frame", BLayoutUtils::AlignInFrame(screen.Frame(),
BSize(windowWidth, windowWidth),
BAlignment(B_ALIGN_HORIZONTAL_CENTER, B_ALIGN_VERTICAL_CENTER)));
fMessage.AddInt32("base_type", kHexBase);
fMessage.AddFloat("font_size", 12.0f);
fMessage.AddFloat("font_size", fontSize);
fMessage.AddBool("case_sensitive", true);
fMessage.AddInt8("find_mode", kAsciiMode);

Expand Down Expand Up @@ -135,17 +146,17 @@ Settings::Settings()
&& settings.window_frame.Height() < screen.Frame().Height())
fMessage.ReplaceRect("window_frame", settings.window_frame);

if (settings.base_type == kHexBase
if (settings.base_type == kHexBase
|| settings.base_type == kDecimalBase)
fMessage.ReplaceInt32("base_type",
fMessage.ReplaceInt32("base_type",
B_LENDIAN_TO_HOST_INT32(settings.base_type));
if (settings.font_size >= 0 && settings.font_size <= 72)
fMessage.ReplaceFloat("font_size",
fMessage.ReplaceFloat("font_size",
float(B_LENDIAN_TO_HOST_INT32(settings.font_size)));

fMessage.ReplaceBool("case_sensitive",
fMessage.ReplaceBool("case_sensitive",
settings.flags & kCaseSensitive);
fMessage.ReplaceInt8("find_mode",
fMessage.ReplaceInt8("find_mode",
settings.flags & kHexFindMode ? kHexMode : kAsciiMode);
}
}
Expand Down Expand Up @@ -188,7 +199,7 @@ Settings::~Settings()
}


status_t
status_t
Settings::Open(BFile *file, int32 mode)
{
BPath path;
Expand All @@ -201,7 +212,7 @@ Settings::Open(BFile *file, int32 mode)
}


void
void
Settings::UpdateFrom(BMessage *message)
{
BRect frame;
Expand Down Expand Up @@ -396,12 +407,12 @@ DiskProbe::ArgvReceived(int32 argc, char **argv)
path.SetTo(&currentDirectory, argv[i]);

status_t status;
entry_ref ref;
entry_ref ref;
BEntry entry;

if ((status = entry.SetTo(path.Path(), false)) != B_OK
|| (status = entry.GetRef(&ref)) != B_OK) {
fprintf(stderr, B_TRANSLATE("Could not open file \"%s\": %s\n"),
fprintf(stderr, B_TRANSLATE("Could not open file \"%s\": %s\n"),
path.Path(), strerror(status));
continue;
}
Expand Down

0 comments on commit d0f27e4

Please sign in to comment.