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>
26 #include <svx/viewlayoutitem.hxx>
27 #include <svx/zoom_def.hxx>
28 
29 namespace
30 {
31 const sal_uInt16 SPECIAL_FACTOR = 0xFFFF;
32 
33 } // anonymous namespace
34 
35 sal_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 
46 void 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 
115 void 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 
121 const SfxItemSet* SvxZoomDialog::GetOutputItemSet() const { return m_pOutSet.get(); }
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::ToggleButton&, 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::ToggleButton&, 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::ToggleButton&, 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(m_rSet.GetPool()->GetWhich(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  const SfxPoolItem* pPoolViewLayoutItem = nullptr;
232  if (SfxItemState::SET == m_rSet.GetItemState(SID_ATTR_VIEWLAYOUT, false, &pPoolViewLayoutItem))
233  {
234  const SvxViewLayoutItem* pViewLayoutItem
235  = static_cast<const SvxViewLayoutItem*>(pPoolViewLayoutItem);
236  const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
237  const bool bBookMode = pViewLayoutItem->IsBookMode();
238 
239  if (0 == nColumns)
240  {
241  m_xAutomaticBtn->set_active(true);
242  m_xColumnsEdit->set_value(2);
243  m_xColumnsEdit->set_sensitive(false);
244  m_xBookModeChk->set_sensitive(false);
245  }
246  else if (1 == nColumns)
247  {
248  m_xSingleBtn->set_active(true);
249  m_xColumnsEdit->set_value(2);
250  m_xColumnsEdit->set_sensitive(false);
251  m_xBookModeChk->set_sensitive(false);
252  }
253  else
254  {
255  m_xColumnsBtn->set_active(true);
256  if (!bBookMode)
257  {
258  m_xColumnsEdit->set_value(nColumns);
259  if (nColumns % 2 != 0)
260  m_xBookModeChk->set_sensitive(false);
261  }
262  else
263  {
264  m_xColumnsEdit->set_value(nColumns);
265  m_xBookModeChk->set_active(true);
266  }
267  }
268  }
269  else
270  {
271  // hide view layout related controls:
272  m_xViewFrame->set_sensitive(false);
273  }
274 }
275 
277 {
278  m_bModified = true;
279 
280  if (m_xUserBtn->get_active())
281  {
282  m_xUserEdit->set_sensitive(true);
283  m_xUserEdit->grab_focus();
284  }
285  else
286  {
287  m_xUserEdit->set_sensitive(false);
288  }
289 }
290 
292 {
293  if (!m_xUserBtn->get_active())
294  return;
295 
296  m_bModified = true;
297 }
298 
300 {
301  m_bModified = true;
302 
303  if (m_xAutomaticBtn->get_active() || m_xSingleBtn->get_active())
304  {
305  m_xColumnsEdit->set_sensitive(false);
306  m_xBookModeChk->set_sensitive(false);
307  }
308  else if (m_xColumnsBtn->get_active())
309  {
310  m_xColumnsEdit->set_sensitive(true);
311  m_xColumnsEdit->grab_focus();
312  if (m_xColumnsEdit->get_value() % 2 == 0)
313  m_xBookModeChk->set_sensitive(true);
314  }
315 }
316 
317 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutSpinHdl, weld::SpinButton&, void)
318 {
319  if (!m_xColumnsBtn->get_active())
320  return;
321 
322  if (m_xColumnsEdit->get_value() % 2 == 0)
323  {
324  m_xBookModeChk->set_sensitive(true);
325  }
326  else
327  {
328  m_xBookModeChk->set_active(false);
329  m_xBookModeChk->set_sensitive(false);
330  }
331 
332  m_bModified = true;
333 }
334 
336 {
337  if (!m_xColumnsBtn->get_active())
338  return;
339 
340  m_bModified = true;
341 }
342 
344 {
345  if (m_bModified)
346  {
347  SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, m_rSet.GetPool()->GetWhich(SID_ATTR_ZOOM));
348  SvxViewLayoutItem aViewLayoutItem(0, false,
349  m_rSet.GetPool()->GetWhich(SID_ATTR_VIEWLAYOUT));
350 
351  sal_uInt16 nFactor = GetFactor();
352 
353  if (SPECIAL_FACTOR == nFactor)
354  {
355  if (m_xOptimalBtn->get_active())
356  aZoomItem.SetType(SvxZoomType::OPTIMAL);
357  else if (m_xPageWidthBtn->get_active())
358  aZoomItem.SetType(SvxZoomType::PAGEWIDTH);
359  else if (m_xWholePageBtn->get_active())
360  aZoomItem.SetType(SvxZoomType::WHOLEPAGE);
361  }
362  else
363  {
364  aZoomItem.SetValue(nFactor);
365  }
366 
367  if (m_xAutomaticBtn->get_active())
368  {
369  aViewLayoutItem.SetValue(0);
370  aViewLayoutItem.SetBookMode(false);
371  }
372  if (m_xSingleBtn->get_active())
373  {
374  aViewLayoutItem.SetValue(1);
375  aViewLayoutItem.SetBookMode(false);
376  }
377  else if (m_xColumnsBtn->get_active())
378  {
379  aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_xColumnsEdit->get_value()));
380  aViewLayoutItem.SetBookMode(m_xBookModeChk->get_active());
381  }
382 
383  m_pOutSet.reset(new SfxItemSet(m_rSet));
384  m_pOutSet->Put(aZoomItem);
385 
386  // don't set attribute in case the whole viewlayout stuff is disabled:
387  if (m_xViewFrame->get_sensitive())
388  m_pOutSet->Put(aViewLayoutItem);
389 
390  // memorize value from the UserEdit beyond the dialog
392 
393  if (pShell)
394  {
395  sal_uInt16 nZoomValue
396  = static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
397  pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
398  }
399  m_xDialog->response(RET_OK);
400  }
401  else
402  m_xDialog->response(RET_CANCEL);
403 }
404 
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< SfxItemSet > m_pOutSet
Definition: zoom.hxx:31
FieldUnit
const SfxItemSet * GetOutputItemSet() const
Definition: zoom.cxx:121
std::string GetValue
constexpr OUStringLiteral PERCENT(u"Percent")
sal_uInt16 GetValue() const
std::unique_ptr< weld::RadioButton > m_xAutomaticBtn
Definition: zoom.hxx:41
static SfxObjectShell * Current()
void PutItem(const SfxPoolItem &rItem)
RET_CANCEL
void SetFactor(sal_uInt16 nNewFactor, ZoomButtonId nButtonId=ZoomButtonId::NONE)
Definition: zoom.cxx:46
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::MetricSpinButton > m_xUserEdit
Definition: zoom.hxx:39
std::unique_ptr< weld::RadioButton > m_xOptimalBtn
Definition: zoom.hxx:34
std::unique_ptr< weld::Widget > m_xViewFrame
Definition: zoom.hxx:40
DocumentType eType
std::unique_ptr< weld::RadioButton > m_x100Btn
Definition: zoom.hxx:37
const SfxPoolItem * GetItem(sal_uInt16 nSlotId) const
std::unique_ptr< weld::RadioButton > m_xWholePageBtn
Definition: zoom.hxx:35
bool IsBookMode() const
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
#define DBG_ASSERT(sCon, aError)
void SetLimits(sal_uInt16 nMin, sal_uInt16 nMax)
Definition: zoom.cxx:115
std::unique_ptr< weld::CheckButton > m_xBookModeChk
Definition: zoom.hxx:45
const SfxItemSet & m_rSet
Definition: zoom.hxx:30
std::unique_ptr< weld::RadioButton > m_xColumnsBtn
Definition: zoom.hxx:43
SvxZoomDialog(weld::Window *pParent, const SfxItemSet &rCoreSet)
Definition: zoom.cxx:123
ZoomButtonId
SfxItemPool * GetPool() const
std::unique_ptr< weld::SpinButton > m_xColumnsEdit
Definition: zoom.hxx:44
void HideButton(ZoomButtonId nButtonId)
Definition: zoom.cxx:94
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
SvxZoomEnableFlags
sal_uInt16 GetWhich(sal_uInt16 nSlot, bool bDeep=true) const
sal_uInt16 GetFactor() const
Definition: zoom.cxx:35
RET_OK
Reference< XExecutableDialog > m_xDialog
std::unique_ptr< weld::RadioButton > m_xUserBtn
Definition: zoom.hxx:38
std::unique_ptr< weld::RadioButton > m_xSingleBtn
Definition: zoom.hxx:42
SvxZoomType
IMPL_LINK_NOARG(SvxZoomDialog, UserHdl, weld::ToggleButton &, void)
Definition: zoom.cxx:276
sal_Int16 nValue