bump product version to 6.4.0.3
[LibreOffice.git] / cui / source / dialogs / zoom.cxx
blob380ad1404da40bf9b0e59e4bb7a0bb0bf5afca22
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
29 namespace
32 const sal_uInt16 SPECIAL_FACTOR = 0xFFFF;
34 } // anonymous namespace
36 sal_uInt16 SvxZoomDialog::GetFactor() const
38 if (m_x100Btn->get_active())
39 return 100;
41 if (m_xUserBtn->get_active())
42 return static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
43 else
44 return SPECIAL_FACTOR;
47 void SvxZoomDialog::SetFactor(sal_uInt16 nNewFactor, ZoomButtonId nButtonId)
49 m_xUserEdit->set_sensitive(false);
51 if (nButtonId == ZoomButtonId::NONE)
53 if ( nNewFactor == 100 )
55 m_x100Btn->set_active(true);
56 m_x100Btn->grab_focus();
58 else
60 m_xUserBtn->set_active(true);
61 m_xUserEdit->set_sensitive(true);
62 m_xUserEdit->set_value(nNewFactor, FieldUnit::PERCENT);
63 m_xUserEdit->grab_focus();
66 else
68 m_xUserEdit->set_value(nNewFactor, FieldUnit::PERCENT);
69 switch(nButtonId)
71 case ZoomButtonId::OPTIMAL:
73 m_xOptimalBtn->set_active(true);
74 m_xOptimalBtn->grab_focus();
75 break;
77 case ZoomButtonId::PAGEWIDTH:
79 m_xPageWidthBtn->set_active(true);
80 m_xPageWidthBtn->grab_focus();
81 break;
83 case ZoomButtonId::WHOLEPAGE:
85 m_xWholePageBtn->set_active(true);
86 m_xWholePageBtn->grab_focus();
87 break;
89 default: break;
94 void SvxZoomDialog::HideButton(ZoomButtonId nButtonId)
96 switch (nButtonId)
98 case ZoomButtonId::OPTIMAL:
99 m_xOptimalBtn->hide();
100 break;
102 case ZoomButtonId::PAGEWIDTH:
103 m_xPageWidthBtn->hide();
104 break;
106 case ZoomButtonId::WHOLEPAGE:
107 m_xWholePageBtn->hide();
108 break;
110 default:
111 OSL_FAIL("Wrong button number!" );
115 void SvxZoomDialog::SetLimits(sal_uInt16 nMin, sal_uInt16 nMax)
117 DBG_ASSERT(nMin < nMax, "invalid limits");
118 m_xUserEdit->set_range(nMin, nMax, FieldUnit::PERCENT);
121 const SfxItemSet* SvxZoomDialog::GetOutputItemSet() const
123 return m_pOutSet.get();
126 SvxZoomDialog::SvxZoomDialog(weld::Window* pParent, const SfxItemSet& rCoreSet)
127 : SfxDialogController(pParent, "cui/ui/zoomdialog.ui", "ZoomDialog")
128 , m_rSet(rCoreSet)
129 , m_bModified(false)
130 , m_xOptimalBtn(m_xBuilder->weld_radio_button("optimal"))
131 , m_xWholePageBtn(m_xBuilder->weld_radio_button("fitwandh"))
132 , m_xPageWidthBtn(m_xBuilder->weld_radio_button("fitw"))
133 , m_x100Btn(m_xBuilder->weld_radio_button("100pc"))
134 , m_xUserBtn(m_xBuilder->weld_radio_button("variable"))
135 , m_xUserEdit(m_xBuilder->weld_metric_spin_button("zoomsb", FieldUnit::PERCENT))
136 , m_xViewFrame(m_xBuilder->weld_widget("viewframe"))
137 , m_xAutomaticBtn(m_xBuilder->weld_radio_button("automatic"))
138 , m_xSingleBtn(m_xBuilder->weld_radio_button("singlepage"))
139 , m_xColumnsBtn(m_xBuilder->weld_radio_button("columns"))
140 , m_xColumnsEdit(m_xBuilder->weld_spin_button("columnssb"))
141 , m_xBookModeChk(m_xBuilder->weld_check_button("bookmode"))
142 , m_xOKBtn(m_xBuilder->weld_button("ok"))
144 Link<weld::ToggleButton&,void> aLink = LINK(this, SvxZoomDialog, UserHdl);
145 m_x100Btn->connect_toggled(aLink);
146 m_xOptimalBtn->connect_toggled(aLink);
147 m_xPageWidthBtn->connect_toggled(aLink);
148 m_xWholePageBtn->connect_toggled(aLink);
149 m_xUserBtn->connect_toggled(aLink);
151 Link<weld::ToggleButton&,void> aViewLayoutLink = LINK(this, SvxZoomDialog, ViewLayoutUserHdl);
152 m_xAutomaticBtn->connect_toggled(aViewLayoutLink);
153 m_xSingleBtn->connect_toggled(aViewLayoutLink);
154 m_xColumnsBtn->connect_toggled(aViewLayoutLink);
156 Link<weld::SpinButton&,void> aViewLayoutSpinLink = LINK(this, SvxZoomDialog, ViewLayoutSpinHdl);
157 m_xColumnsEdit->connect_value_changed(aViewLayoutSpinLink);
159 Link<weld::ToggleButton&,void> aViewLayoutCheckLink = LINK(this, SvxZoomDialog, ViewLayoutCheckHdl);
160 m_xBookModeChk->connect_toggled(aViewLayoutCheckLink);
162 m_xOKBtn->connect_clicked(LINK(this, SvxZoomDialog, OKHdl));
163 m_xUserEdit->connect_value_changed(LINK(this, SvxZoomDialog, SpinHdl));
165 // default values
166 sal_uInt16 nValue = 100;
167 sal_uInt16 nMin = 10;
168 sal_uInt16 nMax = 1000;
170 // maybe get the old value first
171 const SfxUInt16Item* pOldUserItem = nullptr;
172 SfxObjectShell* pShell = SfxObjectShell::Current();
174 if (pShell)
175 pOldUserItem = pShell->GetItem(SID_ATTR_ZOOM_USER);
177 if (pOldUserItem)
178 nValue = pOldUserItem->GetValue();
180 // initialize UserEdit
181 if (nMin > nValue)
182 nMin = nValue;
183 if (nMax < nValue)
184 nMax = nValue;
186 SetLimits(nMin, nMax);
187 m_xUserEdit->set_value(nValue, FieldUnit::PERCENT);
189 const SfxPoolItem& rItem = m_rSet.Get(m_rSet.GetPool()->GetWhich(SID_ATTR_ZOOM));
191 if (nullptr != dynamic_cast<const SvxZoomItem*>( &rItem))
193 const SvxZoomItem& rZoomItem = static_cast<const SvxZoomItem&>(rItem);
194 const sal_uInt16 nZoom = rZoomItem.GetValue();
195 const SvxZoomType eType = rZoomItem.GetType();
196 const SvxZoomEnableFlags nValSet = rZoomItem.GetValueSet();
197 ZoomButtonId nButtonId = ZoomButtonId::NONE;
199 switch (eType)
201 case SvxZoomType::OPTIMAL:
202 nButtonId = ZoomButtonId::OPTIMAL;
203 break;
204 case SvxZoomType::PAGEWIDTH:
205 nButtonId = ZoomButtonId::PAGEWIDTH;
206 break;
207 case SvxZoomType::WHOLEPAGE:
208 nButtonId = ZoomButtonId::WHOLEPAGE;
209 break;
210 case SvxZoomType::PERCENT:
211 break;
212 case SvxZoomType::PAGEWIDTH_NOBORDER:
213 break;
216 if (!(SvxZoomEnableFlags::N100 & nValSet))
217 m_x100Btn->set_sensitive(false);
218 if (!(SvxZoomEnableFlags::OPTIMAL & nValSet))
219 m_xOptimalBtn->set_sensitive(false);
220 if (!(SvxZoomEnableFlags::PAGEWIDTH & nValSet))
221 m_xPageWidthBtn->set_sensitive(false);
222 if (!(SvxZoomEnableFlags::WHOLEPAGE & nValSet))
223 m_xWholePageBtn->set_sensitive(false);
225 SetFactor(nZoom, nButtonId);
227 else
229 const sal_uInt16 nZoom = static_cast<const SfxUInt16Item&>(rItem).GetValue();
230 SetFactor(nZoom);
233 const SfxPoolItem* pPoolViewLayoutItem = nullptr;
234 if (SfxItemState::SET == m_rSet.GetItemState(SID_ATTR_VIEWLAYOUT, false, &pPoolViewLayoutItem))
236 const SvxViewLayoutItem* pViewLayoutItem = static_cast<const SvxViewLayoutItem*>(pPoolViewLayoutItem);
237 const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
238 const bool bBookMode = pViewLayoutItem->IsBookMode();
240 if (0 == nColumns)
242 m_xAutomaticBtn->set_active(true);
243 m_xColumnsEdit->set_value(2);
244 m_xColumnsEdit->set_sensitive(false);
245 m_xBookModeChk->set_sensitive(false);
247 else if (1 == nColumns)
249 m_xSingleBtn->set_active(true);
250 m_xColumnsEdit->set_value(2);
251 m_xColumnsEdit->set_sensitive(false);
252 m_xBookModeChk->set_sensitive(false);
254 else
256 m_xColumnsBtn->set_active(true);
257 if (!bBookMode)
259 m_xColumnsEdit->set_value(nColumns);
260 if (nColumns % 2 != 0)
261 m_xBookModeChk->set_sensitive(false);
263 else
265 m_xColumnsEdit->set_value(nColumns);
266 m_xBookModeChk->set_active(true);
270 else
272 // hide view layout related controls:
273 m_xViewFrame->set_sensitive(false);
277 IMPL_LINK_NOARG(SvxZoomDialog, UserHdl, weld::ToggleButton&, void)
279 m_bModified = true;
281 if (m_xUserBtn->get_active())
283 m_xUserEdit->set_sensitive(true);
284 m_xUserEdit->grab_focus();
286 else
288 m_xUserEdit->set_sensitive(false);
292 IMPL_LINK_NOARG(SvxZoomDialog, SpinHdl, weld::MetricSpinButton&, void)
294 if (!m_xUserBtn->get_active())
295 return;
297 m_bModified = true;
300 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutUserHdl, weld::ToggleButton&, void)
302 m_bModified = true;
304 if (m_xAutomaticBtn->get_active() || m_xSingleBtn->get_active())
306 m_xColumnsEdit->set_sensitive(false);
307 m_xBookModeChk->set_sensitive(false);
309 else if (m_xColumnsBtn->get_active())
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);
318 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutSpinHdl, weld::SpinButton&, void)
320 if (!m_xColumnsBtn->get_active())
321 return;
323 if (m_xColumnsEdit->get_value() % 2 == 0)
325 m_xBookModeChk->set_sensitive(true);
327 else
329 m_xBookModeChk->set_active(false);
330 m_xBookModeChk->set_sensitive(false);
333 m_bModified = true;
336 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutCheckHdl, weld::ToggleButton&, void)
338 if (!m_xColumnsBtn->get_active())
339 return;
341 m_bModified = true;
344 IMPL_LINK_NOARG(SvxZoomDialog, OKHdl, weld::Button&, void)
346 if (m_bModified)
348 SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, m_rSet.GetPool()->GetWhich(SID_ATTR_ZOOM));
349 SvxViewLayoutItem aViewLayoutItem(0, false, m_rSet.GetPool()->GetWhich(SID_ATTR_VIEWLAYOUT));
351 sal_uInt16 nFactor = GetFactor();
353 if (SPECIAL_FACTOR == nFactor)
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);
362 else
364 aZoomItem.SetValue(nFactor);
367 if (m_xAutomaticBtn->get_active())
369 aViewLayoutItem.SetValue(0);
370 aViewLayoutItem.SetBookMode(false);
372 if (m_xSingleBtn->get_active())
374 aViewLayoutItem.SetValue(1);
375 aViewLayoutItem.SetBookMode(false);
377 else if (m_xColumnsBtn->get_active())
379 aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_xColumnsEdit->get_value()));
380 aViewLayoutItem.SetBookMode(m_xBookModeChk->get_active());
383 m_pOutSet.reset(new SfxItemSet(m_rSet));
384 m_pOutSet->Put(aZoomItem);
386 // don't set attribute in case the whole viewlayout stuff is disabled:
387 if (m_xViewFrame->get_sensitive())
388 m_pOutSet->Put(aViewLayoutItem);
390 // memorize value from the UserEdit beyond the dialog
391 SfxObjectShell* pShell = SfxObjectShell::Current();
393 if (pShell)
395 sal_uInt16 nZoomValue = static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
396 pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
398 m_xDialog->response(RET_OK);
400 else
401 m_xDialog->response(RET_CANCEL);
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */