LibreOffice Module cui (master) 1
zoom.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <osl/diagnose.h>
21#include <svl/itemset.hxx>
22#include <svl/itempool.hxx>
23#include <sfx2/objsh.hxx>
24#include <zoom.hxx>
25#include <sfx2/zoomitem.hxx>
27#include <svx/zoom_def.hxx>
28
29namespace
30{
31const sal_uInt16 SPECIAL_FACTOR = 0xFFFF;
32
33} // anonymous namespace
34
35sal_uInt16 SvxZoomDialog::GetFactor() const
36{
37 if (m_x100Btn->get_active())
38 return 100;
39
40 if (m_xUserBtn->get_active())
41 return static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
42 else
43 return SPECIAL_FACTOR;
44}
45
46void SvxZoomDialog::SetFactor(sal_uInt16 nNewFactor, ZoomButtonId nButtonId)
47{
48 m_xUserEdit->set_sensitive(false);
49
50 if (nButtonId == ZoomButtonId::NONE)
51 {
52 if (nNewFactor == 100)
53 {
54 m_x100Btn->set_active(true);
55 m_x100Btn->grab_focus();
56 }
57 else
58 {
59 m_xUserBtn->set_active(true);
60 m_xUserEdit->set_sensitive(true);
61 m_xUserEdit->set_value(nNewFactor, FieldUnit::PERCENT);
62 m_xUserEdit->grab_focus();
63 }
64 }
65 else
66 {
67 m_xUserEdit->set_value(nNewFactor, FieldUnit::PERCENT);
68 switch (nButtonId)
69 {
70 case ZoomButtonId::OPTIMAL:
71 {
72 m_xOptimalBtn->set_active(true);
73 m_xOptimalBtn->grab_focus();
74 break;
75 }
76 case ZoomButtonId::PAGEWIDTH:
77 {
78 m_xPageWidthBtn->set_active(true);
79 m_xPageWidthBtn->grab_focus();
80 break;
81 }
82 case ZoomButtonId::WHOLEPAGE:
83 {
84 m_xWholePageBtn->set_active(true);
85 m_xWholePageBtn->grab_focus();
86 break;
87 }
88 default:
89 break;
90 }
91 }
92}
93
95{
96 switch (nButtonId)
97 {
98 case ZoomButtonId::OPTIMAL:
99 m_xOptimalBtn->hide();
100 break;
101
102 case ZoomButtonId::PAGEWIDTH:
103 m_xPageWidthBtn->hide();
104 break;
105
106 case ZoomButtonId::WHOLEPAGE:
107 m_xWholePageBtn->hide();
108 break;
109
110 default:
111 OSL_FAIL("Wrong button number!");
112 }
113}
114
115void SvxZoomDialog::SetLimits(sal_uInt16 nMin, sal_uInt16 nMax)
116{
117 DBG_ASSERT(nMin < nMax, "invalid limits");
118 m_xUserEdit->set_range(nMin, nMax, FieldUnit::PERCENT);
119}
120
122
124 : SfxDialogController(pParent, "cui/ui/zoomdialog.ui", "ZoomDialog")
125 , m_rSet(rCoreSet)
126 , m_bModified(false)
127 , m_xOptimalBtn(m_xBuilder->weld_radio_button("optimal"))
128 , m_xWholePageBtn(m_xBuilder->weld_radio_button("fitwandh"))
129 , m_xPageWidthBtn(m_xBuilder->weld_radio_button("fitw"))
130 , m_x100Btn(m_xBuilder->weld_radio_button("100pc"))
131 , m_xUserBtn(m_xBuilder->weld_radio_button("variable"))
132 , m_xUserEdit(m_xBuilder->weld_metric_spin_button("zoomsb", FieldUnit::PERCENT))
133 , m_xViewFrame(m_xBuilder->weld_widget("viewframe"))
134 , m_xAutomaticBtn(m_xBuilder->weld_radio_button("automatic"))
135 , m_xSingleBtn(m_xBuilder->weld_radio_button("singlepage"))
136 , m_xColumnsBtn(m_xBuilder->weld_radio_button("columns"))
137 , m_xColumnsEdit(m_xBuilder->weld_spin_button("columnssb"))
138 , m_xBookModeChk(m_xBuilder->weld_check_button("bookmode"))
139 , m_xOKBtn(m_xBuilder->weld_button("ok"))
140{
141 Link<weld::Toggleable&, void> aLink = LINK(this, SvxZoomDialog, UserHdl);
142 m_x100Btn->connect_toggled(aLink);
143 m_xOptimalBtn->connect_toggled(aLink);
144 m_xPageWidthBtn->connect_toggled(aLink);
145 m_xWholePageBtn->connect_toggled(aLink);
146 m_xUserBtn->connect_toggled(aLink);
147
148 Link<weld::Toggleable&, void> aViewLayoutLink = LINK(this, SvxZoomDialog, ViewLayoutUserHdl);
149 m_xAutomaticBtn->connect_toggled(aViewLayoutLink);
150 m_xSingleBtn->connect_toggled(aViewLayoutLink);
151 m_xColumnsBtn->connect_toggled(aViewLayoutLink);
152
153 Link<weld::SpinButton&, void> aViewLayoutSpinLink
154 = LINK(this, SvxZoomDialog, ViewLayoutSpinHdl);
155 m_xColumnsEdit->connect_value_changed(aViewLayoutSpinLink);
156
157 Link<weld::Toggleable&, void> aViewLayoutCheckLink
158 = LINK(this, SvxZoomDialog, ViewLayoutCheckHdl);
159 m_xBookModeChk->connect_toggled(aViewLayoutCheckLink);
160
161 m_xOKBtn->connect_clicked(LINK(this, SvxZoomDialog, OKHdl));
162 m_xUserEdit->connect_value_changed(LINK(this, SvxZoomDialog, SpinHdl));
163
164 // default values
165 sal_uInt16 nValue = 100;
166 sal_uInt16 nMin = 10;
167 sal_uInt16 nMax = 1000;
168
169 // maybe get the old value first
170 const SfxUInt16Item* pOldUserItem = nullptr;
172 pOldUserItem = pShell->GetItem(SID_ATTR_ZOOM_USER);
173
174 if (pOldUserItem)
175 nValue = pOldUserItem->GetValue();
176
177 // initialize UserEdit
178 if (nMin > nValue)
179 nMin = nValue;
180 if (nMax < nValue)
181 nMax = nValue;
182
183 SetLimits(nMin, nMax);
184 m_xUserEdit->set_value(nValue, FieldUnit::PERCENT);
185
186 const SfxPoolItem& rItem = m_rSet.Get(SID_ATTR_ZOOM);
187
188 if (auto pZoomItem = dynamic_cast<const SvxZoomItem*>(&rItem))
189 {
190 const sal_uInt16 nZoom = pZoomItem->GetValue();
191 const SvxZoomType eType = pZoomItem->GetType();
192 const SvxZoomEnableFlags nValSet = pZoomItem->GetValueSet();
193 ZoomButtonId nButtonId = ZoomButtonId::NONE;
194
195 switch (eType)
196 {
197 case SvxZoomType::OPTIMAL:
198 nButtonId = ZoomButtonId::OPTIMAL;
199 break;
200 case SvxZoomType::PAGEWIDTH:
201 nButtonId = ZoomButtonId::PAGEWIDTH;
202 break;
203 case SvxZoomType::WHOLEPAGE:
204 nButtonId = ZoomButtonId::WHOLEPAGE;
205 break;
206 case SvxZoomType::PERCENT:
207 break;
208 case SvxZoomType::PAGEWIDTH_NOBORDER:
209 break;
210 }
211
212 if (!(SvxZoomEnableFlags::N100 & nValSet))
213 m_x100Btn->set_sensitive(false);
214 if (!(SvxZoomEnableFlags::OPTIMAL & nValSet))
215 m_xOptimalBtn->set_sensitive(false);
216 if (!(SvxZoomEnableFlags::PAGEWIDTH & nValSet))
217 m_xPageWidthBtn->set_sensitive(false);
218 if (!(SvxZoomEnableFlags::WHOLEPAGE & nValSet))
219 m_xWholePageBtn->set_sensitive(false);
220
221 SetFactor(nZoom, nButtonId);
222 }
223 else
224 {
225 const sal_uInt16 nZoom = static_cast<const SfxUInt16Item&>(rItem).GetValue();
226 SetFactor(nZoom);
227 }
228
229 if (const SvxViewLayoutItem* pViewLayoutItem = m_rSet.GetItemIfSet(SID_ATTR_VIEWLAYOUT, false))
230 {
231 const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
232 const bool bBookMode = pViewLayoutItem->IsBookMode();
233
234 if (0 == nColumns)
235 {
236 m_xAutomaticBtn->set_active(true);
237 m_xColumnsEdit->set_value(2);
238 m_xColumnsEdit->set_sensitive(false);
239 m_xBookModeChk->set_sensitive(false);
240 }
241 else if (1 == nColumns)
242 {
243 m_xSingleBtn->set_active(true);
244 m_xColumnsEdit->set_value(2);
245 m_xColumnsEdit->set_sensitive(false);
246 m_xBookModeChk->set_sensitive(false);
247 }
248 else
249 {
250 m_xColumnsBtn->set_active(true);
251 if (!bBookMode)
252 {
253 m_xColumnsEdit->set_value(nColumns);
254 if (nColumns % 2 != 0)
255 m_xBookModeChk->set_sensitive(false);
256 }
257 else
258 {
259 m_xColumnsEdit->set_value(nColumns);
260 m_xBookModeChk->set_active(true);
261 }
262 }
263 }
264 else
265 {
266 // hide view layout related controls:
267 m_xViewFrame->set_visible(false);
268 }
269}
270
272{
273 m_bModified = true;
274
275 if (m_xUserBtn->get_active())
276 {
277 m_xUserEdit->set_sensitive(true);
278 m_xUserEdit->grab_focus();
279 }
280 else
281 {
282 m_xUserEdit->set_sensitive(false);
283 }
284}
285
287{
288 if (!m_xUserBtn->get_active())
289 return;
290
291 m_bModified = true;
292}
293
295{
296 m_bModified = true;
297
298 if (m_xAutomaticBtn->get_active() || m_xSingleBtn->get_active())
299 {
300 m_xColumnsEdit->set_sensitive(false);
301 m_xBookModeChk->set_sensitive(false);
302 }
303 else if (m_xColumnsBtn->get_active())
304 {
305 m_xColumnsEdit->set_sensitive(true);
306 m_xColumnsEdit->grab_focus();
307 if (m_xColumnsEdit->get_value() % 2 == 0)
308 m_xBookModeChk->set_sensitive(true);
309 }
310}
311
313{
314 if (!m_xColumnsBtn->get_active())
315 return;
316
317 if (m_xColumnsEdit->get_value() % 2 == 0)
318 {
319 m_xBookModeChk->set_sensitive(true);
320 }
321 else
322 {
323 m_xBookModeChk->set_active(false);
324 m_xBookModeChk->set_sensitive(false);
325 }
326
327 m_bModified = true;
328}
329
331{
332 if (!m_xColumnsBtn->get_active())
333 return;
334
335 m_bModified = true;
336}
337
339{
340 if (m_bModified)
341 {
342 SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, SID_ATTR_ZOOM);
343 SvxViewLayoutItem aViewLayoutItem(0, false, SID_ATTR_VIEWLAYOUT);
344
345 sal_uInt16 nFactor = GetFactor();
346
347 if (SPECIAL_FACTOR == nFactor)
348 {
349 if (m_xOptimalBtn->get_active())
350 aZoomItem.SetType(SvxZoomType::OPTIMAL);
351 else if (m_xPageWidthBtn->get_active())
352 aZoomItem.SetType(SvxZoomType::PAGEWIDTH);
353 else if (m_xWholePageBtn->get_active())
354 aZoomItem.SetType(SvxZoomType::WHOLEPAGE);
355 }
356 else
357 {
358 aZoomItem.SetValue(nFactor);
359 }
360
361 if (m_xAutomaticBtn->get_active())
362 {
363 aViewLayoutItem.SetValue(0);
364 aViewLayoutItem.SetBookMode(false);
365 }
366 if (m_xSingleBtn->get_active())
367 {
368 aViewLayoutItem.SetValue(1);
369 aViewLayoutItem.SetBookMode(false);
370 }
371 else if (m_xColumnsBtn->get_active())
372 {
373 aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_xColumnsEdit->get_value()));
374 aViewLayoutItem.SetBookMode(m_xBookModeChk->get_active());
375 }
376
377 m_pOutSet.reset(new SfxItemSet(m_rSet));
378 m_pOutSet->Put(aZoomItem);
379
380 // don't set attribute in case the whole viewlayout stuff is disabled:
381 if (m_xViewFrame->get_sensitive())
382 m_pOutSet->Put(aViewLayoutItem);
383
384 // memorize value from the UserEdit beyond the dialog
386 {
387 sal_uInt16 nZoomValue
388 = static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
389 pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
390 }
391 m_xDialog->response(RET_OK);
392 }
393 else
394 m_xDialog->response(RET_CANCEL);
395}
396
397/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
sal_uInt16 GetValue() const
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
static SAL_WARN_UNUSED_RESULT SfxObjectShell * Current()
void SetBookMode(bool bNew)
const SfxItemSet * GetOutputItemSet() const
Definition: zoom.cxx:121
std::unique_ptr< weld::RadioButton > m_xOptimalBtn
Definition: zoom.hxx:34
std::unique_ptr< weld::RadioButton > m_xColumnsBtn
Definition: zoom.hxx:43
std::unique_ptr< weld::Widget > m_xViewFrame
Definition: zoom.hxx:40
std::unique_ptr< weld::RadioButton > m_xSingleBtn
Definition: zoom.hxx:42
void HideButton(ZoomButtonId nButtonId)
Definition: zoom.cxx:94
std::unique_ptr< weld::CheckButton > m_xBookModeChk
Definition: zoom.hxx:45
std::unique_ptr< weld::RadioButton > m_xUserBtn
Definition: zoom.hxx:38
SvxZoomDialog(weld::Window *pParent, const SfxItemSet &rCoreSet)
Definition: zoom.cxx:123
std::unique_ptr< SfxItemSet > m_pOutSet
Definition: zoom.hxx:31
void SetLimits(sal_uInt16 nMin, sal_uInt16 nMax)
Definition: zoom.cxx:115
void SetFactor(sal_uInt16 nNewFactor, ZoomButtonId nButtonId=ZoomButtonId::NONE)
Definition: zoom.cxx:46
std::unique_ptr< weld::MetricSpinButton > m_xUserEdit
Definition: zoom.hxx:39
std::unique_ptr< weld::Button > m_xOKBtn
Definition: zoom.hxx:46
std::unique_ptr< weld::RadioButton > m_xPageWidthBtn
Definition: zoom.hxx:36
std::unique_ptr< weld::SpinButton > m_xColumnsEdit
Definition: zoom.hxx:44
std::unique_ptr< weld::RadioButton > m_xAutomaticBtn
Definition: zoom.hxx:41
const SfxItemSet & m_rSet
Definition: zoom.hxx:30
sal_uInt16 GetFactor() const
Definition: zoom.cxx:35
std::unique_ptr< weld::RadioButton > m_xWholePageBtn
Definition: zoom.hxx:35
std::unique_ptr< weld::RadioButton > m_x100Btn
Definition: zoom.hxx:37
void SetType(SvxZoomType eNewType)
#define DBG_ASSERT(sCon, aError)
virtual tools::Long GetValue() const override
FieldUnit
DocumentType eType
sal_Int16 nValue
constexpr OUStringLiteral PERCENT(u"Percent")
RET_OK
RET_CANCEL
IMPL_LINK_NOARG(SvxZoomDialog, UserHdl, weld::Toggleable &, void)
Definition: zoom.cxx:271
ZoomButtonId
SvxZoomType
SvxZoomEnableFlags