Applied the logic in r31871 to jpeg2000 translator too. Fixes bug #4139.

Also cleaned up.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32166 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2009-08-06 17:05:42 +00:00
parent 5a9c3ff54c
commit bdaaeb0c37
2 changed files with 66 additions and 251 deletions
@@ -81,8 +81,6 @@ translation_format outputFormats[] = {
{}, {},
}; };
bool gAreSettingsRunning = false;
//! Make settings to defaults //! Make settings to defaults
void void
@@ -569,12 +567,9 @@ jas_stream_positionIOopen(BPositionIO *positionIO)
// #pragma mark - SView // #pragma mark - SView
SView::SView(const char *name, float x, float y) SView::SView(BRect frame, const char *name)
: BView(BRect(x, y, x, y), name, B_FOLLOW_NONE, B_WILL_DRAW) : BView(frame, name, B_FOLLOW_NONE, B_WILL_DRAW)
{ {
fPreferredWidth = 0;
fPreferredHeight = 0;
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetLowColor(ViewColor()); SetLowColor(ViewColor());
@@ -583,41 +578,10 @@ SView::SView(const char *name, float x, float y)
void void
SView::GetPreferredSize(float* _width, float* _height) SView::AttachedToWindow()
{ {
if (_width) BView::AttachedToWindow();
*_width = fPreferredWidth; ResizeTo(Parent()->Bounds().Width(), Parent()->Bounds().Height());
if (_height)
*_height = fPreferredHeight;
}
void
SView::ResizeToPreferred()
{
ResizeTo(fPreferredWidth, fPreferredHeight);
}
void
SView::ResizePreferredBy(float width, float height)
{
fPreferredWidth += width;
fPreferredHeight += height;
}
void
SView::AddChild(BView *child, BView *before)
{
BView::AddChild(child, before);
child->ResizeToPreferred();
BRect frame = child->Frame();
if (frame.right > fPreferredWidth)
fPreferredWidth = frame.right;
if (frame.bottom > fPreferredHeight)
fPreferredHeight = frame.bottom;
} }
@@ -662,27 +626,26 @@ SSlider::ResizeToPreferred()
// #pragma mark - // #pragma mark -
TranslatorReadView::TranslatorReadView(const char *name, jpeg_settings *settings, TranslatorReadView::TranslatorReadView(BRect frame, const char *name,
float x, float y) jpeg_settings *settings)
: SView(name, x, y), : SView(frame, name),
fSettings(settings) fSettings(settings)
{ {
fGrayAsRGB32 = new BCheckBox(BRect(10, GetPreferredHeight(), 10, fGrayAsRGB32 = new BCheckBox(BRect(10, 10, 40, 40), "grayasrgb32",
GetPreferredHeight()), "grayasrgb32", VIEW_LABEL_GRAYASRGB32, VIEW_LABEL_GRAYASRGB32, new BMessage(VIEW_MSG_SET_GRAYASRGB32));
new BMessage(VIEW_MSG_SET_GRAYASRGB32));
fGrayAsRGB32->SetFont(be_plain_font); fGrayAsRGB32->SetFont(be_plain_font);
if (fSettings->B_GRAY8_as_B_RGB32) if (fSettings->B_GRAY8_as_B_RGB32)
fGrayAsRGB32->SetValue(1); fGrayAsRGB32->SetValue(1);
AddChild(fGrayAsRGB32); AddChild(fGrayAsRGB32);
fGrayAsRGB32->ResizeToPreferred();
ResizeToPreferred();
} }
void void
TranslatorReadView::AttachedToWindow() TranslatorReadView::AttachedToWindow()
{ {
SView::AttachedToWindow();
fGrayAsRGB32->SetTarget(this); fGrayAsRGB32->SetTarget(this);
} }
@@ -710,46 +673,53 @@ TranslatorReadView::MessageReceived(BMessage *message)
// #pragma mark - TranslatorWriteView // #pragma mark - TranslatorWriteView
TranslatorWriteView::TranslatorWriteView(const char *name, jpeg_settings *settings, TranslatorWriteView::TranslatorWriteView(BRect frame, const char *name,
float x, float y) jpeg_settings *settings)
: SView(name, x, y), : SView(frame, name),
fSettings(settings) fSettings(settings)
{ {
fQualitySlider = new SSlider(BRect(10, GetPreferredHeight(), 10, BRect rect(10, 10, 10, 40);
GetPreferredHeight()), "quality", VIEW_LABEL_QUALITY, fQualitySlider = new SSlider(rect, "quality",
new BMessage(VIEW_MSG_SET_QUALITY), 0, 100); VIEW_LABEL_QUALITY, new BMessage(VIEW_MSG_SET_QUALITY), 0, 100);
fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM); fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
fQualitySlider->SetHashMarkCount(10); fQualitySlider->SetHashMarkCount(10);
fQualitySlider->SetLimitLabels("Low", "High"); fQualitySlider->SetLimitLabels("Low", "High");
fQualitySlider->SetFont(be_plain_font); fQualitySlider->SetFont(be_plain_font);
fQualitySlider->SetValue(fSettings->Quality); fQualitySlider->SetValue(fSettings->Quality);
AddChild(fQualitySlider); AddChild(fQualitySlider);
fQualitySlider->ResizeToPreferred();
fGrayAsRGB24 = new BCheckBox(BRect(10, GetPreferredHeight() + 5, 25, rect.OffsetBy(0, fQualitySlider->Bounds().Height() + 10);
GetPreferredHeight() + 5), "gray1asrgb24", VIEW_LABEL_GRAY1ASRGB24,
fGrayAsRGB24 = new BCheckBox(rect, "gray1asrgb24",
VIEW_LABEL_GRAY1ASRGB24,
new BMessage(VIEW_MSG_SET_GRAY1ASRGB24)); new BMessage(VIEW_MSG_SET_GRAY1ASRGB24));
fGrayAsRGB24->SetFont(be_plain_font); fGrayAsRGB24->SetFont(be_plain_font);
if (fSettings->B_GRAY1_as_B_RGB24) if (fSettings->B_GRAY1_as_B_RGB24)
fGrayAsRGB24->SetValue(1); fGrayAsRGB24->SetValue(1);
AddChild(fGrayAsRGB24); AddChild(fGrayAsRGB24);
fGrayAsRGB24->ResizeToPreferred();
fCodeStreamOnly = new BCheckBox(BRect(10, GetPreferredHeight() + 5, 10, rect.OffsetBy(0, fGrayAsRGB24->Bounds().Height() + 10);
GetPreferredHeight()), "codestreamonly", VIEW_LABEL_JPC,
new BMessage(VIEW_MSG_SET_JPC)); fCodeStreamOnly = new BCheckBox(rect, "codestreamonly",
VIEW_LABEL_JPC, new BMessage(VIEW_MSG_SET_JPC));
fCodeStreamOnly->SetFont(be_plain_font); fCodeStreamOnly->SetFont(be_plain_font);
if (fSettings->JPC) if (fSettings->JPC)
fCodeStreamOnly->SetValue(1); fCodeStreamOnly->SetValue(1);
AddChild(fCodeStreamOnly); AddChild(fCodeStreamOnly);
ResizeToPreferred(); fCodeStreamOnly->ResizeToPreferred();
} }
void void
TranslatorWriteView::AttachedToWindow() TranslatorWriteView::AttachedToWindow()
{ {
SView::AttachedToWindow();
fQualitySlider->SetTarget(this); fQualitySlider->SetTarget(this);
fGrayAsRGB24->SetTarget(this); fGrayAsRGB24->SetTarget(this);
fCodeStreamOnly->SetTarget(this); fCodeStreamOnly->SetTarget(this);
@@ -797,14 +767,15 @@ TranslatorWriteView::MessageReceived(BMessage *message)
// #pragma mark - // #pragma mark -
TranslatorAboutView::TranslatorAboutView(const char *name, float x, float y) TranslatorAboutView::TranslatorAboutView(BRect frame, const char *name)
: SView(name, x, y) : SView(frame, name)
{ {
BStringView *title = new BStringView(BRect(10, 0, 10, 0), "Title", BStringView *title = new BStringView(BRect(10, 0, 10, 0), "Title",
translatorName); translatorName);
title->SetFont(be_bold_font); title->SetFont(be_bold_font);
AddChild(title); AddChild(title);
title->ResizeToPreferred();
BRect rect = title->Bounds(); BRect rect = title->Bounds();
float space = title->StringWidth(" "); float space = title->StringWidth(" ");
@@ -822,7 +793,10 @@ TranslatorAboutView::TranslatorAboutView(const char *name, float x, float y)
version->MoveBy(0, rect.bottom-version->Frame().bottom); version->MoveBy(0, rect.bottom-version->Frame().bottom);
AddChild(version); AddChild(version);
version->ResizeToPreferred();
BRect stringFrame = title->Frame();
// Now for each line in translatorInfo add a BStringView // Now for each line in translatorInfo add a BStringView
char* current = translatorInfo; char* current = translatorInfo;
int32 index = 1; int32 index = 1;
@@ -837,71 +811,40 @@ TranslatorAboutView::TranslatorAboutView(const char *name, float x, float y)
newLine + 1 - current)); newLine + 1 - current));
current = newLine + 1; current = newLine + 1;
} }
BStringView* string = new BStringView(BRect(10, GetPreferredHeight(), stringFrame.OffsetBy(0, stringFrame.Height() + 2);
10, GetPreferredHeight()), "copyright", text); BStringView* string = new BStringView(stringFrame, "copyright",
text);
if (index > 3) if (index > 3)
string->SetFontSize(9); string->SetFontSize(9);
AddChild(string); AddChild(string);
string->ResizeToPreferred();
index++; index++;
} }
ResizeToPreferred();
} }
// #pragma mark - // #pragma mark -
TranslatorView::TranslatorView(const char *name) TranslatorView::TranslatorView(BRect frame, const char *name)
: SView(name), : BTabView(frame, name)
fTabWidth(30),
fActiveChild(0)
{ {
// Set global var to true
gAreSettingsRunning = true;
// Load settings to global settings struct // Load settings to global settings struct
LoadSettings(&fSettings); LoadSettings(&fSettings);
font_height fontHeight; BRect contentSize = ContainerView()->Bounds();
GetFontHeight(&fontHeight); SView *view = new TranslatorWriteView(contentSize, "Write",
fTabHeight = (int32)ceilf(fontHeight.ascent + fontHeight.descent &fSettings);
+ fontHeight.leading) + 7; AddTab(view);
// Add left and top margins view = new TranslatorReadView(contentSize, "Read", &fSettings);
float top = fTabHeight + 20; AddTab(view);
float left = 0; view = new TranslatorAboutView(contentSize, "About");
AddTab(view);
// This will remember longest string width
int32 nameWidth = 0;
SView *view = new TranslatorWriteView("Write", &fSettings, left, top);
AddChild(view);
nameWidth = (int32)StringWidth(view->Name());
fTabs.AddItem(new BTab(view));
view = new TranslatorReadView("Read", &fSettings, left, top);
AddChild(view);
if (nameWidth < StringWidth(view->Name()))
nameWidth = (int32)StringWidth(view->Name());
fTabs.AddItem(new BTab(view));
view = new TranslatorAboutView("About", left, top);
AddChild(view);
if (nameWidth < StringWidth(view->Name()))
nameWidth = (int32)StringWidth(view->Name());
fTabs.AddItem(new BTab(view));
fTabWidth += nameWidth;
if (fTabWidth * CountChildren() > GetPreferredWidth())
ResizePreferredBy((fTabWidth * CountChildren()) - GetPreferredWidth(), 0);
// Add right and bottom margins
ResizePreferredBy(10, 15);
ResizeToPreferred(); ResizeToPreferred();
// Make TranslatorView resize itself with parent // Make TranslatorView resize itself with parent
SetFlags(Flags() | B_FOLLOW_ALL); SetFlags(Flags() | B_FOLLOW_ALL);
} }
@@ -909,109 +852,6 @@ TranslatorView::TranslatorView(const char *name)
TranslatorView::~TranslatorView() TranslatorView::~TranslatorView()
{ {
gAreSettingsRunning = false;
BTab* tab;
while ((tab = (BTab*)fTabs.RemoveItem((int32)0)) != NULL) {
delete tab;
}
}
//! Attached to window - resize parent to preferred
void
TranslatorView::AttachedToWindow()
{
// Hide all children except first one
BView *child;
int32 index = 1;
while ((child = ChildAt(index++)) != NULL)
child->Hide();
}
BRect
TranslatorView::_TabFrame(int32 index) const
{
return BRect(index * fTabWidth, 10, (index + 1) * fTabWidth, 10 + fTabHeight);
}
void
TranslatorView::Draw(BRect updateRect)
{
// This is needed because DataTranslations app hides children
// after user changes translator
if (ChildAt(fActiveChild)->IsHidden())
ChildAt(fActiveChild)->Show();
// Clear
SetHighColor(ViewColor());
BRect frame = _TabFrame(0);
FillRect(BRect(frame.left, frame.top, Bounds().right, frame.bottom - 1));
int32 index = 0;
BTab* tab;
while ((tab = (BTab*)fTabs.ItemAt(index)) != NULL) {
tab_position position;
if (fActiveChild == index)
position = B_TAB_FRONT;
else if (index == 0)
position = B_TAB_FIRST;
else
position = B_TAB_ANY;
tab->DrawTab(this, _TabFrame(index), position, index + 1 != fActiveChild);
index++;
}
// Draw bottom edge
SetHighColor(tint_color(ViewColor(), B_LIGHTEN_MAX_TINT));
BRect selectedFrame = _TabFrame(fActiveChild);
float offset = ceilf(frame.Height() / 2.0);
if (selectedFrame.left > frame.left) {
StrokeLine(BPoint(frame.left, frame.bottom),
BPoint(selectedFrame.left, frame.bottom));
}
if (selectedFrame.right + offset < Bounds().right) {
StrokeLine(BPoint(selectedFrame.right + offset, frame.bottom),
BPoint(Bounds().right, frame.bottom));
}
}
//! MouseDown, check if on tab, if so change tab if needed
void
TranslatorView::MouseDown(BPoint where)
{
BRect frame = _TabFrame(fTabs.CountItems() - 1);
frame.left = 0;
if (!frame.Contains(where))
return;
for (int32 index = fTabs.CountItems(); index-- > 0;) {
if (!_TabFrame(index).Contains(where))
continue;
if (fActiveChild != index) {
// Hide current visible child
ChildAt(fActiveChild)->Hide();
// This loop is needed because it looks like in DataTranslations
// view gets hidden more than one time when user changes translator
while (ChildAt(index)->IsHidden()) {
ChildAt(index)->Show();
}
// Remember which one is currently visible
fActiveChild = index;
Invalidate(frame);
break;
}
}
} }
@@ -1042,7 +882,7 @@ TranslatorWindow::TranslatorWindow(bool quitOnClose)
status_t status_t
MakeConfig(BMessage *ioExtension, BView **outView, BRect *outExtent) MakeConfig(BMessage *ioExtension, BView **outView, BRect *outExtent)
{ {
*outView = new TranslatorView("TranslatorView"); *outView = new TranslatorView(BRect(0, 0, 300, 250), "TranslatorView");
*outExtent = (*outView)->Frame(); *outExtent = (*outView)->Frame();
return B_OK; return B_OK;
} }
@@ -40,6 +40,7 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <Path.h> #include <Path.h>
#include <Slider.h> #include <Slider.h>
#include <StringView.h> #include <StringView.h>
#include <TabView.h>
#include <TranslationKit.h> #include <TranslationKit.h>
#include <TranslatorAddOn.h> #include <TranslatorAddOn.h>
@@ -99,29 +100,14 @@ class SSlider : public BSlider {
//! Basic view class with resizing to needed size //! Basic view class with resizing to needed size
class SView : public BView { class SView : public BView {
public: public:
SView(const char* name, float x = 0, float y = 0); SView(BRect rect, const char* name);
virtual void AttachedToWindow();
virtual void GetPreferredSize(float* _width, float* _height);
virtual void ResizeToPreferred();
void AddChild(BView* child, BView* before = NULL);
float GetPreferredWidth()
{ return fPreferredWidth; }
float GetPreferredHeight()
{ return fPreferredHeight; }
void ResizePreferredBy(float width, float height);
private:
float fPreferredWidth;
float fPreferredHeight;
}; };
//! Configuration view for reading settings //! Configuration view for reading settings
class TranslatorReadView : public SView { class TranslatorReadView : public SView {
public: public:
TranslatorReadView(const char* name, jpeg_settings* settings, TranslatorReadView(BRect rect, const char* name, jpeg_settings* settings);
float x = 0, float y = 0);
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
@@ -134,8 +120,7 @@ class TranslatorReadView : public SView {
//! Configuration view for writing settings //! Configuration view for writing settings
class TranslatorWriteView : public SView { class TranslatorWriteView : public SView {
public: public:
TranslatorWriteView(const char* name, jpeg_settings* settings, TranslatorWriteView(BRect rect, const char* name, jpeg_settings* settings);
float x = 0, float y = 0);
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
@@ -149,27 +134,17 @@ class TranslatorWriteView : public SView {
class TranslatorAboutView : public SView { class TranslatorAboutView : public SView {
public: public:
TranslatorAboutView(const char* name, float x = 0, float y = 0); TranslatorAboutView(BRect rect, const char* name);
}; };
//! Configuration view //! Configuration view
class TranslatorView : public SView { class TranslatorView : public BTabView {
public: public:
TranslatorView(const char *name); TranslatorView(BRect rect, const char *name);
virtual ~TranslatorView(); virtual ~TranslatorView();
virtual void AttachedToWindow();
virtual void Draw(BRect updateRect);
virtual void MouseDown(BPoint where);
private: private:
BRect _TabFrame(int32 index) const;
jpeg_settings fSettings; jpeg_settings fSettings;
BList fTabs;
int32 fTabWidth;
int32 fTabHeight;
int32 fActiveChild;
}; };
//! Window used for configuration //! Window used for configuration