xref: /haiku/src/add-ons/translators/webp/ConfigView.cpp (revision 037bc75e7f1a6c54dc8c0123ef242c4181537ddf)
167a22493SPhilippe Houdoin /*
29f76097fSPhilippe Houdoin  * Copyright 2010-2011, Haiku, Inc. All rights reserved.
367a22493SPhilippe Houdoin  * Distributed under the terms of the MIT License.
467a22493SPhilippe Houdoin  *
567a22493SPhilippe Houdoin  * Authors:
667a22493SPhilippe Houdoin  *		Philippe Houdoin
767a22493SPhilippe Houdoin  */
867a22493SPhilippe Houdoin 
967a22493SPhilippe Houdoin 
1067a22493SPhilippe Houdoin #include "ConfigView.h"
1167a22493SPhilippe Houdoin 
1267a22493SPhilippe Houdoin #include <stdio.h>
1367a22493SPhilippe Houdoin #include <string.h>
1467a22493SPhilippe Houdoin 
159f76097fSPhilippe Houdoin #include <Catalog.h>
169f76097fSPhilippe Houdoin #include <CheckBox.h>
177d48219bSHannah Boneß #include <LayoutBuilder.h>
189f76097fSPhilippe Houdoin #include <MenuField.h>
199f76097fSPhilippe Houdoin #include <MenuItem.h>
209f76097fSPhilippe Houdoin #include <Message.h>
219f76097fSPhilippe Houdoin #include <PopUpMenu.h>
229f76097fSPhilippe Houdoin #include <Slider.h>
239f76097fSPhilippe Houdoin #include <StringView.h>
249f76097fSPhilippe Houdoin 
259f76097fSPhilippe Houdoin #include "webp/encode.h"
269f76097fSPhilippe Houdoin 
279f76097fSPhilippe Houdoin #include "TranslatorSettings.h"
289f76097fSPhilippe Houdoin #include "WebPTranslator.h"
299f76097fSPhilippe Houdoin 
309f76097fSPhilippe Houdoin 
31546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
32546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "ConfigView"
336537cf97SJérôme Duval 
3410dfe897SAxel Dörfler 
359f76097fSPhilippe Houdoin static const uint32 kMsgQuality	= 'qlty';
369f76097fSPhilippe Houdoin static const uint32 kMsgPreset	= 'prst';
379f76097fSPhilippe Houdoin static const uint32 kMsgMethod	= 'metd';
389f76097fSPhilippe Houdoin static const uint32 kMsgPreprocessing = 'pprc';
3967a22493SPhilippe Houdoin 
409f76097fSPhilippe Houdoin static const struct preset_name {
419f76097fSPhilippe Houdoin 	const char*	name;
429f76097fSPhilippe Houdoin 	WebPPreset	id;
439f76097fSPhilippe Houdoin } kPresetNames[] = {
449f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Default"), 	WEBP_PRESET_DEFAULT },
459f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Picture"), 	WEBP_PRESET_PICTURE },
469f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Photo"), 	WEBP_PRESET_PHOTO },
479f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Drawing"), 	WEBP_PRESET_DRAWING },
489f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Icon"), 		WEBP_PRESET_ICON },
499f76097fSPhilippe Houdoin 	{ B_TRANSLATE("Text"), 		WEBP_PRESET_TEXT },
509f76097fSPhilippe Houdoin 	{ NULL },
519f76097fSPhilippe Houdoin };
529f76097fSPhilippe Houdoin 
539f76097fSPhilippe Houdoin 
546195bee5SAdrien Destugues ConfigView::ConfigView(TranslatorSettings* settings)
556195bee5SAdrien Destugues 	: BGroupView(B_TRANSLATE("WebPTranslator Settings"), B_VERTICAL),
569f76097fSPhilippe Houdoin 	fSettings(settings)
5767a22493SPhilippe Houdoin {
5867a22493SPhilippe Houdoin 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
5967a22493SPhilippe Houdoin 
60468f8266SHumdinger 	BStringView* title = new BStringView("title",
61468f8266SHumdinger 		B_TRANSLATE("WebP image translator"));
6295d32039SPhilippe Houdoin 	title->SetFont(be_bold_font);
6367a22493SPhilippe Houdoin 
6495d32039SPhilippe Houdoin 	char versionString[256];
65*037bc75eSJanus 	sprintf(versionString, "v%d.%d.%d, %s",
666537cf97SJérôme Duval 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WEBP_TRANSLATOR_VERSION)),
676537cf97SJérôme Duval 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(WEBP_TRANSLATOR_VERSION)),
686537cf97SJérôme Duval 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
69*037bc75eSJanus 			WEBP_TRANSLATOR_VERSION)),
70*037bc75eSJanus 		__DATE__);
719f76097fSPhilippe Houdoin 
7295d32039SPhilippe Houdoin 	BStringView* version = new BStringView("version", versionString);
7367a22493SPhilippe Houdoin 
749f76097fSPhilippe Houdoin 	BString copyrightsText;
75*037bc75eSJanus 	BStringView *copyrightView = new BStringView("Copyright",
76*037bc75eSJanus 		B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Haiku Inc."));
77*037bc75eSJanus 	BStringView *copyright2View = new BStringView("Copyright2",
78*037bc75eSJanus 		B_TRANSLATE("Based on libwebp v0.1,"));
79*037bc75eSJanus 	BStringView *copyright3View = new BStringView("Copyright3",
80*037bc75eSJanus 		B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Google Inc."));
8167a22493SPhilippe Houdoin 
829f76097fSPhilippe Houdoin 	// output parameters
839f76097fSPhilippe Houdoin 
849f76097fSPhilippe Houdoin 	fPresetsMenu = new BPopUpMenu(B_TRANSLATE("Preset"));
859f76097fSPhilippe Houdoin 	const struct preset_name* preset = kPresetNames;
869f76097fSPhilippe Houdoin 	while (preset->name != NULL) {
879f76097fSPhilippe Houdoin 		BMessage* msg = new BMessage(kMsgPreset);
889f76097fSPhilippe Houdoin 		msg->AddInt32("value", preset->id);
899f76097fSPhilippe Houdoin 
909f76097fSPhilippe Houdoin 		BMenuItem* item = new BMenuItem(preset->name, msg);
919f76097fSPhilippe Houdoin 		if (fSettings->SetGetInt32(WEBP_SETTING_PRESET) == preset->id)
929f76097fSPhilippe Houdoin 			item->SetMarked(true);
939f76097fSPhilippe Houdoin 		fPresetsMenu->AddItem(item);
949f76097fSPhilippe Houdoin 
959f76097fSPhilippe Houdoin 		preset++;
969f76097fSPhilippe Houdoin 	}
979f76097fSPhilippe Houdoin 	BMenuField* presetsField = new BMenuField(B_TRANSLATE("Output preset:"),
9810dfe897SAxel Dörfler 		fPresetsMenu);
999f76097fSPhilippe Houdoin 
1009f76097fSPhilippe Houdoin 	fQualitySlider = new BSlider("quality", B_TRANSLATE("Output quality:"),
1019f76097fSPhilippe Houdoin 		new BMessage(kMsgQuality), 0, 100, B_HORIZONTAL, B_BLOCK_THUMB);
1029f76097fSPhilippe Houdoin 	fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
1039f76097fSPhilippe Houdoin 	fQualitySlider->SetHashMarkCount(10);
1049f76097fSPhilippe Houdoin 	fQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
1059f76097fSPhilippe Houdoin 	fQualitySlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_QUALITY));
1069f76097fSPhilippe Houdoin 
1079f76097fSPhilippe Houdoin 	fMethodSlider = new BSlider("method", B_TRANSLATE("Compression method:"),
1089f76097fSPhilippe Houdoin 		new BMessage(kMsgMethod), 0, 6, B_HORIZONTAL, B_BLOCK_THUMB);
1099f76097fSPhilippe Houdoin 	fMethodSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
1109f76097fSPhilippe Houdoin 	fMethodSlider->SetHashMarkCount(7);
1119f76097fSPhilippe Houdoin 	fMethodSlider->SetLimitLabels(B_TRANSLATE("Fast"),
1129f76097fSPhilippe Houdoin 		B_TRANSLATE("Slower but better"));
1139f76097fSPhilippe Houdoin 	fMethodSlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_METHOD));
1149f76097fSPhilippe Houdoin 
1159f76097fSPhilippe Houdoin 	fPreprocessingCheckBox = new BCheckBox("preprocessing",
1169f76097fSPhilippe Houdoin 		B_TRANSLATE("Preprocessing filter"), new BMessage(kMsgPreprocessing));
1179f76097fSPhilippe Houdoin 	if (fSettings->SetGetBool(WEBP_SETTING_PREPROCESSING))
1189f76097fSPhilippe Houdoin 		fPreprocessingCheckBox->SetValue(B_CONTROL_ON);
11967a22493SPhilippe Houdoin 
12067a22493SPhilippe Houdoin 	// Build the layout
121*037bc75eSJanus 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
122*037bc75eSJanus 		.SetInsets(B_USE_DEFAULT_SPACING)
12395d32039SPhilippe Houdoin 		.Add(title)
12495d32039SPhilippe Houdoin 		.Add(version)
125*037bc75eSJanus 		.Add(copyrightView)
12667a22493SPhilippe Houdoin 		.AddGlue()
1276195bee5SAdrien Destugues 		.Add(presetsField)
1289f76097fSPhilippe Houdoin 		.Add(fQualitySlider)
1299f76097fSPhilippe Houdoin 		.Add(fMethodSlider)
130*037bc75eSJanus 		.Add(fPreprocessingCheckBox)
131*037bc75eSJanus 		.AddGlue()
132*037bc75eSJanus 		.Add(copyright2View)
133*037bc75eSJanus 		.Add(copyright3View);
134*037bc75eSJanus 
135*037bc75eSJanus 	BFont font;
136*037bc75eSJanus 	GetFont(&font);
137*037bc75eSJanus 	SetExplicitPreferredSize(BSize((font.Size() * 1433)/12, (font.Size() * 200)/12));
1386195bee5SAdrien Destugues 
13967a22493SPhilippe Houdoin }
14067a22493SPhilippe Houdoin 
14167a22493SPhilippe Houdoin 
14267a22493SPhilippe Houdoin ConfigView::~ConfigView()
14367a22493SPhilippe Houdoin {
1449f76097fSPhilippe Houdoin 	fSettings->Release();
14567a22493SPhilippe Houdoin }
14667a22493SPhilippe Houdoin 
1479f76097fSPhilippe Houdoin 
1489f76097fSPhilippe Houdoin void
1499f76097fSPhilippe Houdoin ConfigView::AttachedToWindow()
1509f76097fSPhilippe Houdoin {
1516195bee5SAdrien Destugues 	BGroupView::AttachedToWindow();
1529f76097fSPhilippe Houdoin 
1539f76097fSPhilippe Houdoin 	fPresetsMenu->SetTargetForItems(this);
1549f76097fSPhilippe Houdoin 
1559f76097fSPhilippe Houdoin 	fQualitySlider->SetTarget(this);
1569f76097fSPhilippe Houdoin 	fMethodSlider->SetTarget(this);
1579f76097fSPhilippe Houdoin 	fPreprocessingCheckBox->SetTarget(this);
1586195bee5SAdrien Destugues 
1596195bee5SAdrien Destugues 	if (Parent() == NULL && Window()->GetLayout() == NULL) {
1606195bee5SAdrien Destugues 		Window()->SetLayout(new BGroupLayout(B_VERTICAL));
1616195bee5SAdrien Destugues 		Window()->ResizeTo(PreferredSize().Width(), PreferredSize().Height());
1626195bee5SAdrien Destugues 	}
1639f76097fSPhilippe Houdoin }
1649f76097fSPhilippe Houdoin 
1659f76097fSPhilippe Houdoin 
1669f76097fSPhilippe Houdoin void
1679f76097fSPhilippe Houdoin ConfigView::MessageReceived(BMessage* message)
1689f76097fSPhilippe Houdoin {
1699f76097fSPhilippe Houdoin 	struct {
1709f76097fSPhilippe Houdoin 		const char*		name;
1719f76097fSPhilippe Houdoin 		uint32			what;
1729f76097fSPhilippe Houdoin 		TranSettingType	type;
1739f76097fSPhilippe Houdoin 	} maps[] = {
1749f76097fSPhilippe Houdoin 		{ WEBP_SETTING_PRESET, kMsgPreset, TRAN_SETTING_INT32 },
1759f76097fSPhilippe Houdoin 		{ WEBP_SETTING_QUALITY, kMsgQuality, TRAN_SETTING_INT32 },
1769f76097fSPhilippe Houdoin 		{ WEBP_SETTING_METHOD, kMsgMethod, TRAN_SETTING_INT32 },
1779f76097fSPhilippe Houdoin 		{ WEBP_SETTING_PREPROCESSING, kMsgPreprocessing, TRAN_SETTING_BOOL },
1789f76097fSPhilippe Houdoin 		{ NULL }
1799f76097fSPhilippe Houdoin 	};
1809f76097fSPhilippe Houdoin 
1819f76097fSPhilippe Houdoin 	int i;
1829f76097fSPhilippe Houdoin 	for (i = 0; maps[i].name != NULL; i++) {
1839f76097fSPhilippe Houdoin 		if (maps[i].what == message->what)
1849f76097fSPhilippe Houdoin 			break;
1859f76097fSPhilippe Houdoin 	}
1869f76097fSPhilippe Houdoin 
1879f76097fSPhilippe Houdoin 	if (maps[i].name == NULL) {
1886195bee5SAdrien Destugues 		BGroupView::MessageReceived(message);
1899f76097fSPhilippe Houdoin 		return;
1909f76097fSPhilippe Houdoin 	}
1919f76097fSPhilippe Houdoin 
1929f76097fSPhilippe Houdoin 	int32 value;
1939f76097fSPhilippe Houdoin 	if (message->FindInt32("value", &value) == B_OK
1949f76097fSPhilippe Houdoin 		|| message->FindInt32("be:value", &value) == B_OK) {
1959f76097fSPhilippe Houdoin 		switch(maps[i].type) {
1969f76097fSPhilippe Houdoin 			case TRAN_SETTING_BOOL:
1979f76097fSPhilippe Houdoin 			{
1989f76097fSPhilippe Houdoin 				bool boolValue = value;
1999f76097fSPhilippe Houdoin 				fSettings->SetGetBool(maps[i].name, &boolValue);
2009f76097fSPhilippe Houdoin 				break;
2019f76097fSPhilippe Houdoin 			}
2029f76097fSPhilippe Houdoin 			case TRAN_SETTING_INT32:
2039f76097fSPhilippe Houdoin 				fSettings->SetGetInt32(maps[i].name, &value);
2049f76097fSPhilippe Houdoin 				break;
2059f76097fSPhilippe Houdoin 		}
2069f76097fSPhilippe Houdoin 		fSettings->SaveSettings();
2079f76097fSPhilippe Houdoin 	}
2089f76097fSPhilippe Houdoin }
209