Skip to content

Commit

Permalink
DriveSetup: improved size slider.
Browse files Browse the repository at this point in the history
* It uses size_for_string() to display all sizes which also fixes
  using MiB for one, and MB for the other size string.
* It is no longer limited to 1M granularity by itself, but can have
  arbitrary granularity as long as the number of possible values
  remain smaller than 2^31.
* This means it can also handle values beyond 4TB now.
* The sizes that are set programmatically retain their full resolution.
* CreateParametersPanel still uses 1M granularity for now, though,
  as we would otherwise need to be able to parse a size string with
  a unit.
  • Loading branch information
axeld committed Feb 4, 2013
1 parent 7ff0829 commit 0d7d495
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 43 deletions.
22 changes: 11 additions & 11 deletions src/apps/drivesetup/CreateParametersPanel.cpp
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 Haiku Inc. All rights reserved.
* Copyright 2008-2013 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
Expand Down Expand Up @@ -37,16 +37,14 @@ enum {
MSG_SIZE_TEXTCONTROL = 'stct'
};

static const uint32 kMegaByte = 0x100000;


CreateParametersPanel::CreateParametersPanel(BWindow* window,
BPartition* partition, off_t offset, off_t size)
:
AbstractParametersPanel(window)
{
// Scale offset, and size from bytes to megabytes (2^20)
// so that we do not run over a signed int32.
offset /= kMegaByte;
size /= kMegaByte;
_CreateViewControls(partition, offset, size);

Init(B_CREATE_PARAMETER_EDITOR, "", partition);
Expand All @@ -65,8 +63,8 @@ CreateParametersPanel::Go(off_t& offset, off_t& size, BString& name,
// The object will be deleted in Go(), so we need to get the values before

// Return the value back as bytes.
size = (off_t)fSizeSlider->Size() * kMegaByte;
offset = (off_t)fSizeSlider->Offset() * kMegaByte;
size = fSizeSlider->Size();
offset = fSizeSlider->Offset();

// get name
name.SetTo(fNameTextControl->Text());
Expand Down Expand Up @@ -102,9 +100,9 @@ CreateParametersPanel::MessageReceived(BMessage* message)

case MSG_SIZE_TEXTCONTROL:
{
int32 size = atoi(fSizeTextControl->Text());
off_t size = atoi(fSizeTextControl->Text()) * kMegaByte;
if (size >= 0 && size <= fSizeSlider->MaxPartitionSize())
fSizeSlider->SetValue(size + fSizeSlider->Offset());
fSizeSlider->SetSize(size);
else
_UpdateSizeTextControl();
break;
Expand Down Expand Up @@ -147,8 +145,10 @@ CreateParametersPanel::_CreateViewControls(BPartition* parent, off_t offset,
off_t size)
{
// Setup the controls
// TODO: use a lower granularity for smaller disks -- but this would
// require being able to parse arbitrary size strings with unit
fSizeSlider = new SizeSlider("Slider", B_TRANSLATE("Partition size"), NULL,
offset, offset + size);
offset, size, kMegaByte);
fSizeSlider->SetPosition(1.0);
fSizeSlider->SetModificationMessage(new BMessage(MSG_SIZE_SLIDER));

Expand Down Expand Up @@ -191,6 +191,6 @@ void
CreateParametersPanel::_UpdateSizeTextControl()
{
BString sizeString;
sizeString << fSizeSlider->Value() - fSizeSlider->Offset();
sizeString << fSizeSlider->Size() / kMegaByte;
fSizeTextControl->SetText(sizeString.String());
}
85 changes: 61 additions & 24 deletions src/apps/drivesetup/Support.cpp
@@ -1,12 +1,13 @@
/*
* Copyright 2002-2012 Haiku Inc. All rights reserved.
* Copyright 2002-2013 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
* Erik Jaesler <ejakowatz@users.sourceforge.net>
* Ithamar R. Adema <ithamar@unet.nl>
* Stephan Aßmus <superstippi@gmx.de>
* Axel Dörfler, axeld@pinc-software.de.
* Bryce Groff <bgroff@hawaii.edu>
* Erik Jaesler <ejakowatz@users.sourceforge.net>
*/


Expand All @@ -24,6 +25,10 @@
#define B_TRANSLATION_CONTEXT "Support"


static const int32 kMaxSliderLimit = 0x7fffff80;
// this is the maximum value that BSlider seem to work with fine


void
dump_partition_info(const BPartition* partition)
{
Expand Down Expand Up @@ -94,23 +99,36 @@ SpaceIDMap::SpaceIDFor(partition_id parentID, off_t spaceOffset)
}


// #pragma mark -


SizeSlider::SizeSlider(const char* name, const char* label,
BMessage* message, int32 minValue, int32 maxValue)
BMessage* message, off_t offset, off_t size, uint32 minGranularity)
:
BSlider(name, label, message, minValue, maxValue,
B_HORIZONTAL, B_TRIANGLE_THUMB),
fStartOffset(minValue),
fEndOffset(maxValue),
fMaxPartitionSize(maxValue - minValue)
BSlider(name, label, message, 0, kMaxSliderLimit, B_HORIZONTAL,
B_TRIANGLE_THUMB),
fStartOffset(offset),
fEndOffset(offset + size),
fMaxPartitionSize(size),
fGranularity(minGranularity)
{
rgb_color fillColor = ui_color(B_CONTROL_HIGHLIGHT_COLOR);
UseFillColor(true, &fillColor);

// Lazy loop to get a power of two granularity
while (size / fGranularity > kMaxSliderLimit)
fGranularity *= 2;

SetKeyIncrementValue(int32(1024 * 1024 * 1.0 * kMaxSliderLimit
/ ((MaxPartitionSize() - 1) / fGranularity) + 0.5));

char buffer[64];
char minString[64];
char maxString[64];
snprintf(minString, sizeof(minString), B_TRANSLATE("Offset: %ld MB"),
fStartOffset);
snprintf(maxString, sizeof(maxString), B_TRANSLATE("End: %ld MB"),
fEndOffset);
snprintf(minString, sizeof(minString), B_TRANSLATE("Offset: %s"),
string_for_size(fStartOffset, buffer, sizeof(buffer)));
snprintf(maxString, sizeof(maxString), B_TRANSLATE("End: %s"),
string_for_size(fEndOffset, buffer, sizeof(buffer)));
SetLimitLabels(minString, maxString);
}

Expand All @@ -120,35 +138,54 @@ SizeSlider::~SizeSlider()
}


void
SizeSlider::SetValue(int32 value)
{
BSlider::SetValue(value);

fSize = (off_t(1.0 * (MaxPartitionSize() - 1) * Value()
/ kMaxSliderLimit + 0.5) / fGranularity) * fGranularity;
}


const char*
SizeSlider::UpdateText() const
{
// TODO: Perhaps replace with string_for_size, but it looks like
// Value() and fStartOffset are always in MiB.
snprintf(fStatusLabel, sizeof(fStatusLabel), B_TRANSLATE("%ld MiB"),
Value() - fStartOffset);
return string_for_size(Size(), fStatusLabel, sizeof(fStatusLabel));
}

return fStatusLabel;

off_t
SizeSlider::Size() const
{
return fSize;
}


int32
SizeSlider::Size()
void
SizeSlider::SetSize(off_t size)
{
return Value() - fStartOffset;
if (size == fSize)
return;

SetValue(int32(1.0 * kMaxSliderLimit / fGranularity * size
/ ((MaxPartitionSize() - 1) / fGranularity) + 0.5));
fSize = size;
UpdateTextChanged();
}


int32
SizeSlider::Offset()
off_t
SizeSlider::Offset() const
{
// TODO: This should be the changed offset once a double
// headed slider is implemented.
return fStartOffset;
}

int32
SizeSlider::MaxPartitionSize()

off_t
SizeSlider::MaxPartitionSize() const
{
return fMaxPartitionSize;
}
22 changes: 14 additions & 8 deletions src/apps/drivesetup/Support.h
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 Haiku Inc. All rights reserved.
* Copyright 2002-2013 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license.
*/
#ifndef SUPPORT_H
Expand All @@ -22,8 +22,6 @@ void dump_partition_info(const BPartition* partition);
bool is_valid_partitionable_space(size_t size);


static const uint32 kMegaByte = 0x100000;

class SpaceIDMap : public HashMap<HashString, partition_id> {
public:
SpaceIDMap();
Expand All @@ -36,23 +34,31 @@ class SpaceIDMap : public HashMap<HashString, partition_id> {
partition_id fNextSpaceID;
};


class SizeSlider : public BSlider {
public:
SizeSlider(const char* name, const char* label,
BMessage* message, int32 minValue,
int32 maxValue);
BMessage* message, off_t offset,
off_t size, uint32 minGranularity);
virtual ~SizeSlider();

virtual void SetValue(int32);
virtual const char* UpdateText() const;
int32 Size();
int32 Offset();
int32 MaxPartitionSize();

off_t Size() const;
void SetSize(off_t size);

off_t Offset() const;
off_t MaxPartitionSize() const;

private:
off_t fStartOffset;
off_t fEndOffset;
off_t fSize;
off_t fMaxPartitionSize;
uint32 fGranularity;
mutable char fStatusLabel[64];
};


#endif // SUPPORT_H

0 comments on commit 0d7d495

Please sign in to comment.