BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / translators / webp / ConfigView.cpp
blobf3328d10e7c7a67b197be43fbfa54bfafe102298
1 /*
2 * Copyright 2010-2011, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Philippe Houdoin
7 */
10 #include "ConfigView.h"
12 #include <stdio.h>
13 #include <string.h>
15 #include <Catalog.h>
16 #include <CheckBox.h>
17 #include <LayoutBuilder.h>
18 #include <MenuField.h>
19 #include <MenuItem.h>
20 #include <Message.h>
21 #include <PopUpMenu.h>
22 #include <Slider.h>
23 #include <StringView.h>
25 #include "webp/encode.h"
27 #include "TranslatorSettings.h"
28 #include "WebPTranslator.h"
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "ConfigView"
35 static const uint32 kMsgQuality = 'qlty';
36 static const uint32 kMsgPreset = 'prst';
37 static const uint32 kMsgMethod = 'metd';
38 static const uint32 kMsgPreprocessing = 'pprc';
40 static const struct preset_name {
41 const char* name;
42 WebPPreset id;
43 } kPresetNames[] = {
44 { B_TRANSLATE("Default"), WEBP_PRESET_DEFAULT },
45 { B_TRANSLATE("Picture"), WEBP_PRESET_PICTURE },
46 { B_TRANSLATE("Photo"), WEBP_PRESET_PHOTO },
47 { B_TRANSLATE("Drawing"), WEBP_PRESET_DRAWING },
48 { B_TRANSLATE("Icon"), WEBP_PRESET_ICON },
49 { B_TRANSLATE("Text"), WEBP_PRESET_TEXT },
50 { NULL },
54 ConfigView::ConfigView(TranslatorSettings* settings)
55 : BGroupView(B_TRANSLATE("WebPTranslator Settings"), B_VERTICAL),
56 fSettings(settings)
58 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
60 BStringView* title = new BStringView("title",
61 B_TRANSLATE("WebP image translator"));
62 title->SetFont(be_bold_font);
64 char versionString[256];
65 sprintf(versionString, "v%d.%d.%d, %s",
66 static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WEBP_TRANSLATOR_VERSION)),
67 static_cast<int>(B_TRANSLATION_MINOR_VERSION(WEBP_TRANSLATOR_VERSION)),
68 static_cast<int>(B_TRANSLATION_REVISION_VERSION(
69 WEBP_TRANSLATOR_VERSION)),
70 __DATE__);
72 BStringView* version = new BStringView("version", versionString);
74 BString copyrightsText;
75 BStringView *copyrightView = new BStringView("Copyright",
76 B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Haiku Inc."));
77 BStringView *copyright2View = new BStringView("Copyright2",
78 B_TRANSLATE("Based on libwebp v0.1,"));
79 BStringView *copyright3View = new BStringView("Copyright3",
80 B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Google Inc."));
82 // output parameters
84 fPresetsMenu = new BPopUpMenu(B_TRANSLATE("Preset"));
85 const struct preset_name* preset = kPresetNames;
86 while (preset->name != NULL) {
87 BMessage* msg = new BMessage(kMsgPreset);
88 msg->AddInt32("value", preset->id);
90 BMenuItem* item = new BMenuItem(preset->name, msg);
91 if (fSettings->SetGetInt32(WEBP_SETTING_PRESET) == preset->id)
92 item->SetMarked(true);
93 fPresetsMenu->AddItem(item);
95 preset++;
97 BMenuField* presetsField = new BMenuField(B_TRANSLATE("Output preset:"),
98 fPresetsMenu);
100 fQualitySlider = new BSlider("quality", B_TRANSLATE("Output quality:"),
101 new BMessage(kMsgQuality), 0, 100, B_HORIZONTAL, B_BLOCK_THUMB);
102 fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
103 fQualitySlider->SetHashMarkCount(10);
104 fQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
105 fQualitySlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_QUALITY));
107 fMethodSlider = new BSlider("method", B_TRANSLATE("Compression method:"),
108 new BMessage(kMsgMethod), 0, 6, B_HORIZONTAL, B_BLOCK_THUMB);
109 fMethodSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
110 fMethodSlider->SetHashMarkCount(7);
111 fMethodSlider->SetLimitLabels(B_TRANSLATE("Fast"),
112 B_TRANSLATE("Slower but better"));
113 fMethodSlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_METHOD));
115 fPreprocessingCheckBox = new BCheckBox("preprocessing",
116 B_TRANSLATE("Preprocessing filter"), new BMessage(kMsgPreprocessing));
117 if (fSettings->SetGetBool(WEBP_SETTING_PREPROCESSING))
118 fPreprocessingCheckBox->SetValue(B_CONTROL_ON);
120 // Build the layout
121 BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
122 .SetInsets(B_USE_DEFAULT_SPACING)
123 .Add(title)
124 .Add(version)
125 .Add(copyrightView)
126 .AddGlue()
127 .Add(presetsField)
128 .Add(fQualitySlider)
129 .Add(fMethodSlider)
130 .Add(fPreprocessingCheckBox)
131 .AddGlue()
132 .Add(copyright2View)
133 .Add(copyright3View);
135 BFont font;
136 GetFont(&font);
137 SetExplicitPreferredSize(BSize((font.Size() * 250)/12, (font.Size() * 350)/12));
142 ConfigView::~ConfigView()
144 fSettings->Release();
148 void
149 ConfigView::AttachedToWindow()
151 BGroupView::AttachedToWindow();
153 fPresetsMenu->SetTargetForItems(this);
155 fQualitySlider->SetTarget(this);
156 fMethodSlider->SetTarget(this);
157 fPreprocessingCheckBox->SetTarget(this);
159 if (Parent() == NULL && Window()->GetLayout() == NULL) {
160 Window()->SetLayout(new BGroupLayout(B_VERTICAL));
161 Window()->ResizeTo(PreferredSize().Width(), PreferredSize().Height());
166 void
167 ConfigView::MessageReceived(BMessage* message)
169 struct {
170 const char* name;
171 uint32 what;
172 TranSettingType type;
173 } maps[] = {
174 { WEBP_SETTING_PRESET, kMsgPreset, TRAN_SETTING_INT32 },
175 { WEBP_SETTING_QUALITY, kMsgQuality, TRAN_SETTING_INT32 },
176 { WEBP_SETTING_METHOD, kMsgMethod, TRAN_SETTING_INT32 },
177 { WEBP_SETTING_PREPROCESSING, kMsgPreprocessing, TRAN_SETTING_BOOL },
178 { NULL }
181 int i;
182 for (i = 0; maps[i].name != NULL; i++) {
183 if (maps[i].what == message->what)
184 break;
187 if (maps[i].name == NULL) {
188 BGroupView::MessageReceived(message);
189 return;
192 int32 value;
193 if (message->FindInt32("value", &value) == B_OK
194 || message->FindInt32("be:value", &value) == B_OK) {
195 switch(maps[i].type) {
196 case TRAN_SETTING_BOOL:
198 bool boolValue = value;
199 fSettings->SetGetBool(maps[i].name, &boolValue);
200 break;
202 case TRAN_SETTING_INT32:
203 fSettings->SetGetInt32(maps[i].name, &value);
204 break;
206 fSettings->SaveSettings();