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
173 if (pShell)
174 pOldUserItem = pShell->GetItem(SID_ATTR_ZOOM_USER);
175
176 if (pOldUserItem)
177 nValue = pOldUserItem->GetValue();
178
179 // initialize UserEdit
180 if (nMin > nValue)
181 nMin = nValue;
182 if (nMax < nValue)
183 nMax = nValue;
184
185 SetLimits(nMin, nMax);
186 m_xUserEdit->set_value(nValue, FieldUnit::PERCENT);
187
188 const SfxPoolItem& rItem = m_rSet.Get(SID_ATTR_ZOOM);
189
190 if (auto pZoomItem = dynamic_cast<const SvxZoomItem*>(&rItem))
191 {
192 const sal_uInt16 nZoom = pZoomItem->GetValue();
193 const SvxZoomType eType = pZoomItem->GetType();
194 const SvxZoomEnableFlags nValSet = pZoomItem->GetValueSet();
195 ZoomButtonId nButtonId = ZoomButtonId::NONE;
196
197 switch (eType)
198 {
199 case SvxZoomType::OPTIMAL:
200 nButtonId = ZoomButtonId::OPTIMAL;
201 break;
202 case SvxZoomType::PAGEWIDTH:
203 nButtonId = ZoomButtonId::PAGEWIDTH;
204 break;
205 case SvxZoomType::WHOLEPAGE:
206 nButtonId = ZoomButtonId::WHOLEPAGE;
207 break;
208 case SvxZoomType::PERCENT:
209 break;
210 case SvxZoomType::PAGEWIDTH_NOBORDER:
211 break;
212 }
213
214 if (!(SvxZoomEnableFlags::N100 & nValSet))
215 m_x100Btn->set_sensitive(false);
216 if (!(SvxZoomEnableFlags::OPTIMAL & nValSet))
217 m_xOptimalBtn->set_sensitive(false);
218 if (!(SvxZoomEnableFlags::PAGEWIDTH & nValSet))
219 m_xPageWidthBtn->set_sensitive(false);
220 if (!(SvxZoomEnableFlags::WHOLEPAGE & nValSet))
221 m_xWholePageBtn->set_sensitive(false);
222
223 SetFactor(nZoom, nButtonId);
224 }
225 else
226 {
227 const sal_uInt16 nZoom = static_cast<const SfxUInt16Item&>(rItem).GetValue();
228 SetFactor(nZoom);
229 }
230
231 if (const SvxViewLayoutItem* pViewLayoutItem = m_rSet.GetItemIfSet(SID_ATTR_VIEWLAYOUT, false))
232 {
233 const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
234 const bool bBookMode = pViewLayoutItem->IsBookMode();
235
236 if (0 == nColumns)
237 {
238 m_xAutomaticBtn->set_active(true);
239 m_xColumnsEdit->set_value(2);
240 m_xColumnsEdit->set_sensitive(false);
241 m_xBookModeChk->set_sensitive(false);
242 }
243 else if (1 == nColumns)
244 {
245 m_xSingleBtn->set_active(true);
246 m_xColumnsEdit->set_value(2);
247 m_xColumnsEdit->set_sensitive(false);
248 m_xBookModeChk->set_sensitive(false);
249 }
250 else
251 {
252 m_xColumnsBtn->set_active(true);
253 if (!bBookMode)
254 {
255 m_xColumnsEdit->set_value(nColumns);
256 if (nColumns % 2 != 0)
257 m_xBookModeChk->set_sensitive(false);
258 }
259 else
260 {
261 m_xColumnsEdit->set_value(nColumns);
262 m_xBookModeChk->set_active(true);
263 }
264 }
265 }
266 else
267 {
268 // hide view layout related controls:
269 m_xViewFrame->set_sensitive(false);
270 }
271}
272
274{
275 m_bModified = true;
276
277 if (m_xUserBtn->get_active())
278 {
279 m_xUserEdit->set_sensitive(true);
280 m_xUserEdit->grab_focus();
281 }
282 else
283 {
284 m_xUserEdit->set_sensitive(false);
285 }
286}
287
289{
290 if (!m_xUserBtn->get_active())
291 return;
292
293 m_bModified = true;
294}
295
297{
298 m_bModified = true;
299
300 if (m_xAutomaticBtn->get_active() || m_xSingleBtn->get_active())
301 {
302 m_xColumnsEdit->set_sensitive(false);
303 m_xBookModeChk->set_sensitive(false);
304 }
305 else if (m_xColumnsBtn->get_active())
306 {
307 m_xColumnsEdit->set_sensitive(true);
308 m_xColumnsEdit->grab_focus();
309 if (m_xColumnsEdit->get_value() % 2 == 0)
310 m_xBookModeChk->set_sensitive(true);
311 }
312}
313
315{
316 if (!m_xColumnsBtn->get_active())
317 return;
318
319 if (m_xColumnsEdit->get_value() % 2 == 0)
320 {
321 m_xBookModeChk->set_sensitive(true);
322 }
323 else
324 {
325 m_xBookModeChk->set_active(false);
326 m_xBookModeChk->set_sensitive(false);
327 }
328
329 m_bModified = true;
330}
331
333{
334 if (!m_xColumnsBtn->get_active())
335 return;
336
337 m_bModified = true;
338}
339
341{
342 if (m_bModified)
343 {
344 SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, SID_ATTR_ZOOM);
345 SvxViewLayoutItem aViewLayoutItem(0, false, SID_ATTR_VIEWLAYOUT);
346
347 sal_uInt16 nFactor = GetFactor();
348
349 if (SPECIAL_FACTOR == nFactor)
350 {
351 if (m_xOptimalBtn->get_active())
352 aZoomItem.SetType(SvxZoomType::OPTIMAL);
353 else if (m_xPageWidthBtn->get_active())
354 aZoomItem.SetType(SvxZoomType::PAGEWIDTH);
355 else if (m_xWholePageBtn->get_active())
356 aZoomItem.SetType(SvxZoomType::WHOLEPAGE);
357 }
358 else
359 {
360 aZoomItem.SetValue(nFactor);
361 }
362
363 if (m_xAutomaticBtn->get_active())
364 {
365 aViewLayoutItem.SetValue(0);
366 aViewLayoutItem.SetBookMode(false);
367 }
368 if (m_xSingleBtn->get_active())
369 {
370 aViewLayoutItem.SetValue(1);
371 aViewLayoutItem.SetBookMode(false);
372 }
373 else if (m_xColumnsBtn->get_active())
374 {
375 aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_xColumnsEdit->get_value()));
376 aViewLayoutItem.SetBookMode(m_xBookModeChk->get_active());
377 }
378
379 m_pOutSet.reset(new SfxItemSet(m_rSet));
380 m_pOutSet->Put(aZoomItem);
381
382 // don't set attribute in case the whole viewlayout stuff is disabled:
383 if (m_xViewFrame->get_sensitive())
384 m_pOutSet->Put(aViewLayoutItem);
385
386 // memorize value from the UserEdit beyond the dialog
388
389 if (pShell)
390 {
391 sal_uInt16 nZoomValue
392 = static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
393 pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
394 }
395 m_xDialog->response(RET_OK);
396 }
397 else
398 m_xDialog->response(RET_CANCEL);
399}
400
401/* 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 SfxObjectShell * Current()
void PutItem(const SfxPoolItem &rItem)
const SfxPoolItem * GetItem(sal_uInt16 nSlotId) const
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:273
ZoomButtonId
SvxZoomType
SvxZoomEnableFlags