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 (nullptr != dynamic_cast<const SvxZoomItem*>(&rItem))
191  {
192  const SvxZoomItem& rZoomItem = static_cast<const SvxZoomItem&>(rItem);
193  const sal_uInt16 nZoom = rZoomItem.GetValue();
194  const SvxZoomType eType = rZoomItem.GetType();
195  const SvxZoomEnableFlags nValSet = rZoomItem.GetValueSet();
196  ZoomButtonId nButtonId = ZoomButtonId::NONE;
197 
198  switch (eType)
199  {
200  case SvxZoomType::OPTIMAL:
201  nButtonId = ZoomButtonId::OPTIMAL;
202  break;
203  case SvxZoomType::PAGEWIDTH:
204  nButtonId = ZoomButtonId::PAGEWIDTH;
205  break;
206  case SvxZoomType::WHOLEPAGE:
207  nButtonId = ZoomButtonId::WHOLEPAGE;
208  break;
209  case SvxZoomType::PERCENT:
210  break;
211  case SvxZoomType::PAGEWIDTH_NOBORDER:
212  break;
213  }
214 
215  if (!(SvxZoomEnableFlags::N100 & nValSet))
216  m_x100Btn->set_sensitive(false);
217  if (!(SvxZoomEnableFlags::OPTIMAL & nValSet))
218  m_xOptimalBtn->set_sensitive(false);
219  if (!(SvxZoomEnableFlags::PAGEWIDTH & nValSet))
220  m_xPageWidthBtn->set_sensitive(false);
221  if (!(SvxZoomEnableFlags::WHOLEPAGE & nValSet))
222  m_xWholePageBtn->set_sensitive(false);
223 
224  SetFactor(nZoom, nButtonId);
225  }
226  else
227  {
228  const sal_uInt16 nZoom = static_cast<const SfxUInt16Item&>(rItem).GetValue();
229  SetFactor(nZoom);
230  }
231 
232  const SfxPoolItem* pPoolViewLayoutItem = nullptr;
233  if (SfxItemState::SET == m_rSet.GetItemState(SID_ATTR_VIEWLAYOUT, false, &pPoolViewLayoutItem))
234  {
235  const SvxViewLayoutItem* pViewLayoutItem
236  = static_cast<const SvxViewLayoutItem*>(pPoolViewLayoutItem);
237  const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
238  const bool bBookMode = pViewLayoutItem->IsBookMode();
239 
240  if (0 == nColumns)
241  {
242  m_xAutomaticBtn->set_active(true);
243  m_xColumnsEdit->set_value(2);
244  m_xColumnsEdit->set_sensitive(false);
245  m_xBookModeChk->set_sensitive(false);
246  }
247  else if (1 == nColumns)
248  {
249  m_xSingleBtn->set_active(true);
250  m_xColumnsEdit->set_value(2);
251  m_xColumnsEdit->set_sensitive(false);
252  m_xBookModeChk->set_sensitive(false);
253  }
254  else
255  {
256  m_xColumnsBtn->set_active(true);
257  if (!bBookMode)
258  {
259  m_xColumnsEdit->set_value(nColumns);
260  if (nColumns % 2 != 0)
261  m_xBookModeChk->set_sensitive(false);
262  }
263  else
264  {
265  m_xColumnsEdit->set_value(nColumns);
266  m_xBookModeChk->set_active(true);
267  }
268  }
269  }
270  else
271  {
272  // hide view layout related controls:
273  m_xViewFrame->set_sensitive(false);
274  }
275 }
276 
278 {
279  m_bModified = true;
280 
281  if (m_xUserBtn->get_active())
282  {
283  m_xUserEdit->set_sensitive(true);
284  m_xUserEdit->grab_focus();
285  }
286  else
287  {
288  m_xUserEdit->set_sensitive(false);
289  }
290 }
291 
293 {
294  if (!m_xUserBtn->get_active())
295  return;
296 
297  m_bModified = true;
298 }
299 
301 {
302  m_bModified = true;
303 
304  if (m_xAutomaticBtn->get_active() || m_xSingleBtn->get_active())
305  {
306  m_xColumnsEdit->set_sensitive(false);
307  m_xBookModeChk->set_sensitive(false);
308  }
309  else if (m_xColumnsBtn->get_active())
310  {
311  m_xColumnsEdit->set_sensitive(true);
312  m_xColumnsEdit->grab_focus();
313  if (m_xColumnsEdit->get_value() % 2 == 0)
314  m_xBookModeChk->set_sensitive(true);
315  }
316 }
317 
318 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutSpinHdl, weld::SpinButton&, void)
319 {
320  if (!m_xColumnsBtn->get_active())
321  return;
322 
323  if (m_xColumnsEdit->get_value() % 2 == 0)
324  {
325  m_xBookModeChk->set_sensitive(true);
326  }
327  else
328  {
329  m_xBookModeChk->set_active(false);
330  m_xBookModeChk->set_sensitive(false);
331  }
332 
333  m_bModified = true;
334 }
335 
337 {
338  if (!m_xColumnsBtn->get_active())
339  return;
340 
341  m_bModified = true;
342 }
343 
345 {
346  if (m_bModified)
347  {
348  SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, m_rSet.GetPool()->GetWhich(SID_ATTR_ZOOM));
349  SvxViewLayoutItem aViewLayoutItem(0, false,
350  m_rSet.GetPool()->GetWhich(SID_ATTR_VIEWLAYOUT));
351 
352  sal_uInt16 nFactor = GetFactor();
353 
354  if (SPECIAL_FACTOR == nFactor)
355  {
356  if (m_xOptimalBtn->get_active())
357  aZoomItem.SetType(SvxZoomType::OPTIMAL);
358  else if (m_xPageWidthBtn->get_active())
359  aZoomItem.SetType(SvxZoomType::PAGEWIDTH);
360  else if (m_xWholePageBtn->get_active())
361  aZoomItem.SetType(SvxZoomType::WHOLEPAGE);
362  }
363  else
364  {
365  aZoomItem.SetValue(nFactor);
366  }
367 
368  if (m_xAutomaticBtn->get_active())
369  {
370  aViewLayoutItem.SetValue(0);
371  aViewLayoutItem.SetBookMode(false);
372  }
373  if (m_xSingleBtn->get_active())
374  {
375  aViewLayoutItem.SetValue(1);
376  aViewLayoutItem.SetBookMode(false);
377  }
378  else if (m_xColumnsBtn->get_active())
379  {
380  aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_xColumnsEdit->get_value()));
381  aViewLayoutItem.SetBookMode(m_xBookModeChk->get_active());
382  }
383 
384  m_pOutSet.reset(new SfxItemSet(m_rSet));
385  m_pOutSet->Put(aZoomItem);
386 
387  // don't set attribute in case the whole viewlayout stuff is disabled:
388  if (m_xViewFrame->get_sensitive())
389  m_pOutSet->Put(aViewLayoutItem);
390 
391  // memorize value from the UserEdit beyond the dialog
393 
394  if (pShell)
395  {
396  sal_uInt16 nZoomValue
397  = static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
398  pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
399  }
400  m_xDialog->response(RET_OK);
401  }
402  else
403  m_xDialog->response(RET_CANCEL);
404 }
405 
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< SfxItemSet > m_pOutSet
Definition: zoom.hxx:32
FieldUnit
const SfxItemSet * GetOutputItemSet() const
Definition: zoom.cxx:121
std::string GetValue
SvxZoomEnableFlags GetValueSet() const
sal_uInt16 GetValue() const
std::unique_ptr< weld::RadioButton > m_xAutomaticBtn
Definition: zoom.hxx:42
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:47
std::unique_ptr< weld::RadioButton > m_xPageWidthBtn
Definition: zoom.hxx:37
std::unique_ptr< weld::MetricSpinButton > m_xUserEdit
Definition: zoom.hxx:40
std::unique_ptr< weld::RadioButton > m_xOptimalBtn
Definition: zoom.hxx:35
std::unique_ptr< weld::Widget > m_xViewFrame
Definition: zoom.hxx:41
DocumentType eType
std::unique_ptr< weld::RadioButton > m_x100Btn
Definition: zoom.hxx:38
const SfxPoolItem * GetItem(sal_uInt16 nSlotId) const
std::unique_ptr< weld::RadioButton > m_xWholePageBtn
Definition: zoom.hxx:36
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:46
const SfxItemSet & m_rSet
Definition: zoom.hxx:31
std::unique_ptr< weld::RadioButton > m_xColumnsBtn
Definition: zoom.hxx:44
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:45
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
SvxZoomType GetType() const
sal_uInt16 GetFactor() const
Definition: zoom.cxx:35
RET_OK
Reference< XExecutableDialog > m_xDialog
const OUString PERCENT("Percent")
std::unique_ptr< weld::RadioButton > m_xUserBtn
Definition: zoom.hxx:39
std::unique_ptr< weld::RadioButton > m_xSingleBtn
Definition: zoom.hxx:43
SvxZoomType
IMPL_LINK_NOARG(SvxZoomDialog, UserHdl, weld::ToggleButton &, void)
Definition: zoom.cxx:277
sal_Int16 nValue