repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / showimage / PrintOptionsWindow.cpp
blob782e43e70c4e5394ab3597c12fdc7696e8354964
1 /*
2 * Copyright 2003-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Pfeiffer, laplace@haiku-os.org
7 */
9 #include "PrintOptionsWindow.h"
11 #include <stdio.h> // for sprintf
12 #include <stdlib.h> // for atof
14 #include <Box.h>
15 #include <Button.h>
16 #include <Catalog.h>
17 #include <ControlLook.h>
18 #include <GridLayoutBuilder.h>
19 #include <GroupLayoutBuilder.h>
20 #include <LayoutBuilder.h>
21 #include <Locale.h>
22 #include <String.h>
24 #include "ShowImageConstants.h"
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "PrintOptionsWindow"
31 PrintOptions::PrintOptions()
33 fOption(kFitToPage),
34 fZoomFactor(1.0),
35 fDPI(72.0),
36 fWidth(1024 / 72.0),
37 fHeight(768 / 72.0)
42 void
43 PrintOptions::SetBounds(BRect rect)
45 fBounds = rect;
49 void
50 PrintOptions::SetZoomFactor(float f)
52 fZoomFactor = f;
53 fDPI = 72.0 / fZoomFactor;
57 void
58 PrintOptions::SetDPI(float dpi)
60 fDPI = dpi;
61 fZoomFactor = 72.0 / dpi;
65 void
66 PrintOptions::SetWidth(float w)
68 fWidth = w;
69 fHeight = (fBounds.Height() + 1) * w / (fBounds.Width() + 1);
73 void
74 PrintOptions::SetHeight(float h)
76 fWidth = (fBounds.Width() + 1) * h / (fBounds.Height() + 1);
77 fHeight = h;
81 PrintOptionsWindow::PrintOptionsWindow(BPoint at, PrintOptions* options,
82 BWindow* listener)
84 BWindow(BRect(at.x, at.y, at.x + 300, at.y + 200),
85 B_TRANSLATE("Print options"),
86 B_TITLED_WINDOW_LOOK, B_MODAL_SUBSET_WINDOW_FEEL,
87 B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS),
88 fPrintOptions(options),
89 fCurrentOptions(*options),
90 fListener(listener),
91 fStatus(B_ERROR)
93 AddToSubset(listener);
94 Setup();
95 Show();
99 PrintOptionsWindow::~PrintOptionsWindow()
101 BMessage msg(MSG_PRINT);
102 msg.AddInt32("status", fStatus);
103 fListener.SendMessage(&msg);
107 BRadioButton*
108 PrintOptionsWindow::AddRadioButton(const char* name,
109 const char* label, uint32 what, bool selected)
111 BRadioButton* button;
112 button = new BRadioButton(name, label, new BMessage(what));
113 button->SetValue(selected ? B_CONTROL_ON : B_CONTROL_OFF);
114 return button;
118 BTextControl*
119 PrintOptionsWindow::AddTextControl(const char* name,
120 const char* label, float value, uint32 what)
122 BTextControl* text;
123 text = new BTextControl(name, label, "", new BMessage(what));
124 text->SetModificationMessage(new BMessage(what));
125 SetValue(text, value);
126 return text;
130 void
131 PrintOptionsWindow::Setup()
133 BString value;
134 enum PrintOptions::Option op = fCurrentOptions.Option();
135 BRadioButton* rbFit;
136 BRadioButton* rbZoom;
137 BRadioButton* rbDpi;
138 BRadioButton* rbResize;
139 BBox* line;
140 BButton* button;
142 rbFit = AddRadioButton("fit_to_page", B_TRANSLATE("Fit image to page"),
143 kMsgFitToPageSelected, op == PrintOptions::kFitToPage);
145 rbZoom = AddRadioButton("zoom_factor", B_TRANSLATE("Zoom factor in %:"),
146 kMsgZoomFactorSelected, op == PrintOptions::kZoomFactor);
148 fZoomFactor = AddTextControl("zoom_factor_text", "",
149 fCurrentOptions.ZoomFactor() * 100, kMsgZoomFactorChanged);
151 rbDpi = AddRadioButton("dpi", B_TRANSLATE("DPI:"), kMsgDPISelected,
152 op == PrintOptions::kDPI);
154 fDPI = AddTextControl("dpi_text", "", fCurrentOptions.DPI(),
155 kMsgDPIChanged);
157 rbResize = AddRadioButton("width_and_height",
158 B_TRANSLATE("Resize to (in 1/72 inches):"), kMsgWidthAndHeightSelected,
159 op == PrintOptions::kWidth || op == PrintOptions::kHeight);
161 fWidth = AddTextControl("width", B_TRANSLATE("Width:"),
162 fCurrentOptions.Width(), kMsgWidthChanged);
164 fHeight = AddTextControl("height", B_TRANSLATE("Height: "),
165 fCurrentOptions.Height(), kMsgHeightChanged);
167 line = new BBox(B_EMPTY_STRING, B_WILL_DRAW | B_FRAME_EVENTS,
168 B_FANCY_BORDER);
169 line->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
171 button = new BButton("job setup", B_TRANSLATE("Job setup"),
172 new BMessage(kMsgJobSetup));
173 SetDefaultButton(button);
175 const float spacing = be_control_look->DefaultItemSpacing();
177 SetLayout(new BGroupLayout(B_HORIZONTAL));
178 AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
179 .Add(BGridLayoutBuilder()
180 .Add(rbFit, 0, 0)
181 .Add(rbZoom, 0, 1)
182 .Add(fZoomFactor, 1, 1)
183 .Add(rbDpi, 0, 2)
184 .Add(fDPI, 1, 2)
185 .Add(rbResize, 0, 3)
187 .AddGroup(B_HORIZONTAL, spacing)
188 .Add(fWidth)
189 .Add(fHeight)
190 .AddGlue()
191 .SetInsets(22, 0, 0, 0)
192 .End()
193 .Add(line)
194 .AddGroup(B_HORIZONTAL, 0)
195 .Add(button)
196 .End()
197 .SetInsets(spacing, spacing, spacing, spacing)
202 enum PrintOptions::Option
203 PrintOptionsWindow::MsgToOption(uint32 what)
205 switch (what) {
206 case kMsgFitToPageSelected: return PrintOptions::kFitToPage;
207 case kMsgZoomFactorSelected: return PrintOptions::kZoomFactor;
208 case kMsgDPISelected: return PrintOptions::kDPI;
209 case kMsgWidthAndHeightSelected: return PrintOptions::kWidth;
211 return PrintOptions::kFitToPage;
215 bool
216 PrintOptionsWindow::GetValue(BTextControl* text, float* value)
218 *value = atof(text->Text());
219 return true;
223 void
224 PrintOptionsWindow::SetValue(BTextControl* text, float value)
226 BMessage* msg;
227 char s[80];
228 snprintf(s, sizeof(s), "%0.0f", value);
229 // prevent sending a notification when text is set
230 msg = new BMessage(*text->ModificationMessage());
231 text->SetModificationMessage(NULL);
232 text->SetText(s);
233 text->SetModificationMessage(msg);
237 void
238 PrintOptionsWindow::MessageReceived(BMessage* msg)
240 float value;
241 switch (msg->what) {
242 case kMsgFitToPageSelected:
243 case kMsgZoomFactorSelected:
244 case kMsgDPISelected:
245 case kMsgWidthAndHeightSelected:
246 fCurrentOptions.SetOption(MsgToOption(msg->what));
247 break;
249 case kMsgZoomFactorChanged:
250 if (GetValue(fZoomFactor, &value)
251 && fCurrentOptions.ZoomFactor() != value) {
252 fCurrentOptions.SetZoomFactor(value / 100);
253 SetValue(fDPI, fCurrentOptions.DPI());
255 break;
256 case kMsgDPIChanged:
257 if (GetValue(fDPI, &value) && fCurrentOptions.DPI() != value) {
258 fCurrentOptions.SetDPI(value);
259 SetValue(fZoomFactor, 100 * fCurrentOptions.ZoomFactor());
261 break;
262 case kMsgWidthChanged:
263 if (GetValue(fWidth, &value) && fCurrentOptions.Width() != value) {
264 fCurrentOptions.SetWidth(value);
265 SetValue(fHeight, fCurrentOptions.Height());
267 break;
268 case kMsgHeightChanged:
269 if (GetValue(fHeight, &value) && fCurrentOptions.Height() != value) {
270 fCurrentOptions.SetHeight(value);
271 SetValue(fWidth, fCurrentOptions.Width());
273 break;
275 case kMsgJobSetup:
276 *fPrintOptions = fCurrentOptions;
277 fStatus = B_OK;
278 PostMessage(B_QUIT_REQUESTED);
279 break;
281 default:
282 BWindow::MessageReceived(msg);