2 * Copyright 2003-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Michael Pfeiffer, laplace@haiku-os.org
9 #include "PrintOptionsWindow.h"
11 #include <stdio.h> // for sprintf
12 #include <stdlib.h> // for atof
17 #include <ControlLook.h>
18 #include <GridLayoutBuilder.h>
19 #include <GroupLayoutBuilder.h>
20 #include <LayoutBuilder.h>
24 #include "ShowImageConstants.h"
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "PrintOptionsWindow"
31 PrintOptions::PrintOptions()
43 PrintOptions::SetBounds(BRect rect
)
50 PrintOptions::SetZoomFactor(float f
)
53 fDPI
= 72.0 / fZoomFactor
;
58 PrintOptions::SetDPI(float dpi
)
61 fZoomFactor
= 72.0 / dpi
;
66 PrintOptions::SetWidth(float w
)
69 fHeight
= (fBounds
.Height() + 1) * w
/ (fBounds
.Width() + 1);
74 PrintOptions::SetHeight(float h
)
76 fWidth
= (fBounds
.Width() + 1) * h
/ (fBounds
.Height() + 1);
81 PrintOptionsWindow::PrintOptionsWindow(BPoint at
, PrintOptions
* options
,
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
),
93 AddToSubset(listener
);
99 PrintOptionsWindow::~PrintOptionsWindow()
101 BMessage
msg(MSG_PRINT
);
102 msg
.AddInt32("status", fStatus
);
103 fListener
.SendMessage(&msg
);
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
);
119 PrintOptionsWindow::AddTextControl(const char* name
,
120 const char* label
, float value
, uint32 what
)
123 text
= new BTextControl(name
, label
, "", new BMessage(what
));
124 text
->SetModificationMessage(new BMessage(what
));
125 SetValue(text
, value
);
131 PrintOptionsWindow::Setup()
134 enum PrintOptions::Option op
= fCurrentOptions
.Option();
136 BRadioButton
* rbZoom
;
138 BRadioButton
* rbResize
;
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(),
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
,
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()
182 .Add(fZoomFactor
, 1, 1)
187 .AddGroup(B_HORIZONTAL
, spacing
)
191 .SetInsets(22, 0, 0, 0)
194 .AddGroup(B_HORIZONTAL
, 0)
197 .SetInsets(spacing
, spacing
, spacing
, spacing
)
202 enum PrintOptions::Option
203 PrintOptionsWindow::MsgToOption(uint32 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
;
216 PrintOptionsWindow::GetValue(BTextControl
* text
, float* value
)
218 *value
= atof(text
->Text());
224 PrintOptionsWindow::SetValue(BTextControl
* text
, float value
)
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
);
233 text
->SetModificationMessage(msg
);
238 PrintOptionsWindow::MessageReceived(BMessage
* msg
)
242 case kMsgFitToPageSelected
:
243 case kMsgZoomFactorSelected
:
244 case kMsgDPISelected
:
245 case kMsgWidthAndHeightSelected
:
246 fCurrentOptions
.SetOption(MsgToOption(msg
->what
));
249 case kMsgZoomFactorChanged
:
250 if (GetValue(fZoomFactor
, &value
)
251 && fCurrentOptions
.ZoomFactor() != value
) {
252 fCurrentOptions
.SetZoomFactor(value
/ 100);
253 SetValue(fDPI
, fCurrentOptions
.DPI());
257 if (GetValue(fDPI
, &value
) && fCurrentOptions
.DPI() != value
) {
258 fCurrentOptions
.SetDPI(value
);
259 SetValue(fZoomFactor
, 100 * fCurrentOptions
.ZoomFactor());
262 case kMsgWidthChanged
:
263 if (GetValue(fWidth
, &value
) && fCurrentOptions
.Width() != value
) {
264 fCurrentOptions
.SetWidth(value
);
265 SetValue(fHeight
, fCurrentOptions
.Height());
268 case kMsgHeightChanged
:
269 if (GetValue(fHeight
, &value
) && fCurrentOptions
.Height() != value
) {
270 fCurrentOptions
.SetHeight(value
);
271 SetValue(fWidth
, fCurrentOptions
.Width());
276 *fPrintOptions
= fCurrentOptions
;
278 PostMessage(B_QUIT_REQUESTED
);
282 BWindow::MessageReceived(msg
);