tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / cui / source / dialogs / zoom.cxx
blob0fe135d3c49c5ad1840d71e17436a7c53d6e3def
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
31 const sal_uInt16 SPECIAL_FACTOR = 0xFFFF;
33 } // anonymous namespace
35 sal_uInt16 SvxZoomDialog::GetFactor() const
37 if (m_x100Btn->get_active())
38 return 100;
40 if (m_xUserBtn->get_active())
41 return static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
42 else
43 return SPECIAL_FACTOR;
46 void SvxZoomDialog::SetFactor(sal_uInt16 nNewFactor, ZoomButtonId nButtonId)
48 m_xUserEdit->set_sensitive(false);
50 if (nButtonId == ZoomButtonId::NONE)
52 if (nNewFactor == 100)
54 m_x100Btn->set_active(true);
55 m_x100Btn->grab_focus();
57 else
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();
65 else
67 m_xUserEdit->set_value(nNewFactor, FieldUnit::PERCENT);
68 switch (nButtonId)
70 case ZoomButtonId::OPTIMAL:
72 m_xOptimalBtn->set_active(true);
73 m_xOptimalBtn->grab_focus();
74 break;
76 case ZoomButtonId::PAGEWIDTH:
78 m_xPageWidthBtn->set_active(true);
79 m_xPageWidthBtn->grab_focus();
80 break;
82 case ZoomButtonId::WHOLEPAGE:
84 m_xWholePageBtn->set_active(true);
85 m_xWholePageBtn->grab_focus();
86 break;
88 default:
89 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 { return m_pOutSet.get(); }
123 SvxZoomDialog::SvxZoomDialog(weld::Window* pParent, const SfxItemSet& rCoreSet)
124 : SfxDialogController(pParent, u"cui/ui/zoomdialog.ui"_ustr, u"ZoomDialog"_ustr)
125 , m_rSet(rCoreSet)
126 , m_bModified(false)
127 , m_xOptimalBtn(m_xBuilder->weld_radio_button(u"optimal"_ustr))
128 , m_xWholePageBtn(m_xBuilder->weld_radio_button(u"fitwandh"_ustr))
129 , m_xPageWidthBtn(m_xBuilder->weld_radio_button(u"fitw"_ustr))
130 , m_x100Btn(m_xBuilder->weld_radio_button(u"100pc"_ustr))
131 , m_xUserBtn(m_xBuilder->weld_radio_button(u"variable"_ustr))
132 , m_xUserEdit(m_xBuilder->weld_metric_spin_button(u"zoomsb"_ustr, FieldUnit::PERCENT))
133 , m_xViewFrame(m_xBuilder->weld_widget(u"viewframe"_ustr))
134 , m_xAutomaticBtn(m_xBuilder->weld_radio_button(u"automatic"_ustr))
135 , m_xSingleBtn(m_xBuilder->weld_radio_button(u"singlepage"_ustr))
136 , m_xColumnsBtn(m_xBuilder->weld_radio_button(u"columns"_ustr))
137 , m_xColumnsEdit(m_xBuilder->weld_spin_button(u"columnssb"_ustr))
138 , m_xBookModeChk(m_xBuilder->weld_check_button(u"bookmode"_ustr))
139 , m_xOKBtn(m_xBuilder->weld_button(u"ok"_ustr))
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);
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);
153 Link<weld::SpinButton&, void> aViewLayoutSpinLink
154 = LINK(this, SvxZoomDialog, ViewLayoutSpinHdl);
155 m_xColumnsEdit->connect_value_changed(aViewLayoutSpinLink);
157 Link<weld::Toggleable&, void> aViewLayoutCheckLink
158 = LINK(this, SvxZoomDialog, ViewLayoutCheckHdl);
159 m_xBookModeChk->connect_toggled(aViewLayoutCheckLink);
161 m_xOKBtn->connect_clicked(LINK(this, SvxZoomDialog, OKHdl));
162 m_xUserEdit->connect_value_changed(LINK(this, SvxZoomDialog, SpinHdl));
164 // default values
165 sal_uInt16 nValue = 100;
166 sal_uInt16 nMin = 10;
167 sal_uInt16 nMax = 1000;
169 // maybe get the old value first
170 const SfxUInt16Item* pOldUserItem = nullptr;
171 if (SfxObjectShell* pShell = SfxObjectShell::Current())
172 pOldUserItem = pShell->GetItem(SID_ATTR_ZOOM_USER);
174 if (pOldUserItem)
175 nValue = pOldUserItem->GetValue();
177 // initialize UserEdit
178 if (nMin > nValue)
179 nMin = nValue;
180 if (nMax < nValue)
181 nMax = nValue;
183 SetLimits(nMin, nMax);
184 m_xUserEdit->set_value(nValue, FieldUnit::PERCENT);
186 const SfxPoolItem& rItem = m_rSet.Get(SID_ATTR_ZOOM);
188 if (auto pZoomItem = dynamic_cast<const SvxZoomItem*>(&rItem))
190 const sal_uInt16 nZoom = pZoomItem->GetValue();
191 const SvxZoomType eType = pZoomItem->GetType();
192 const SvxZoomEnableFlags nValSet = pZoomItem->GetValueSet();
193 ZoomButtonId nButtonId = ZoomButtonId::NONE;
195 switch (eType)
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;
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);
221 SetFactor(nZoom, nButtonId);
223 else
225 const sal_uInt16 nZoom = static_cast<const SfxUInt16Item&>(rItem).GetValue();
226 SetFactor(nZoom);
229 if (const SvxViewLayoutItem* pViewLayoutItem = m_rSet.GetItemIfSet(SID_ATTR_VIEWLAYOUT, false))
231 const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
232 const bool bBookMode = pViewLayoutItem->IsBookMode();
234 if (0 == nColumns)
236 m_xAutomaticBtn->set_active(true);
237 m_xColumnsEdit->set_value(2);
238 m_xColumnsEdit->set_sensitive(false);
239 m_xBookModeChk->set_sensitive(false);
241 else if (1 == nColumns)
243 m_xSingleBtn->set_active(true);
244 m_xColumnsEdit->set_value(2);
245 m_xColumnsEdit->set_sensitive(false);
246 m_xBookModeChk->set_sensitive(false);
248 else
250 m_xColumnsBtn->set_active(true);
251 if (!bBookMode)
253 m_xColumnsEdit->set_value(nColumns);
254 if (nColumns % 2 != 0)
255 m_xBookModeChk->set_sensitive(false);
257 else
259 m_xColumnsEdit->set_value(nColumns);
260 m_xBookModeChk->set_active(true);
264 else
266 // hide view layout related controls:
267 m_xViewFrame->set_visible(false);
271 IMPL_LINK_NOARG(SvxZoomDialog, UserHdl, weld::Toggleable&, void)
273 m_bModified = true;
275 if (m_xUserBtn->get_active())
277 m_xUserEdit->set_sensitive(true);
278 m_xUserEdit->grab_focus();
280 else
282 m_xUserEdit->set_sensitive(false);
286 IMPL_LINK_NOARG(SvxZoomDialog, SpinHdl, weld::MetricSpinButton&, void)
288 if (!m_xUserBtn->get_active())
289 return;
291 m_bModified = true;
294 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutUserHdl, weld::Toggleable&, void)
296 m_bModified = true;
298 if (m_xAutomaticBtn->get_active() || m_xSingleBtn->get_active())
300 m_xColumnsEdit->set_sensitive(false);
301 m_xBookModeChk->set_sensitive(false);
303 else if (m_xColumnsBtn->get_active())
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);
312 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutSpinHdl, weld::SpinButton&, void)
314 if (!m_xColumnsBtn->get_active())
315 return;
317 if (m_xColumnsEdit->get_value() % 2 == 0)
319 m_xBookModeChk->set_sensitive(true);
321 else
323 m_xBookModeChk->set_active(false);
324 m_xBookModeChk->set_sensitive(false);
327 m_bModified = true;
330 IMPL_LINK_NOARG(SvxZoomDialog, ViewLayoutCheckHdl, weld::Toggleable&, void)
332 if (!m_xColumnsBtn->get_active())
333 return;
335 m_bModified = true;
338 IMPL_LINK_NOARG(SvxZoomDialog, OKHdl, weld::Button&, void)
340 if (m_bModified)
342 SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, SID_ATTR_ZOOM);
343 SvxViewLayoutItem aViewLayoutItem(0, false, SID_ATTR_VIEWLAYOUT);
345 sal_uInt16 nFactor = GetFactor();
347 if (SPECIAL_FACTOR == nFactor)
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);
356 else
358 aZoomItem.SetValue(nFactor);
361 if (m_xAutomaticBtn->get_active())
363 aViewLayoutItem.SetValue(0);
364 aViewLayoutItem.SetBookMode(false);
366 if (m_xSingleBtn->get_active())
368 aViewLayoutItem.SetValue(1);
369 aViewLayoutItem.SetBookMode(false);
371 else if (m_xColumnsBtn->get_active())
373 aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_xColumnsEdit->get_value()));
374 aViewLayoutItem.SetBookMode(m_xBookModeChk->get_active());
377 m_pOutSet.reset(new SfxItemSet(m_rSet));
378 m_pOutSet->Put(aZoomItem);
380 // don't set attribute in case the whole viewlayout stuff is disabled:
381 if (m_xViewFrame->get_sensitive())
382 m_pOutSet->Put(aViewLayoutItem);
384 // memorize value from the UserEdit beyond the dialog
385 if (SfxObjectShell* pShell = SfxObjectShell::Current())
387 sal_uInt16 nZoomValue
388 = static_cast<sal_uInt16>(m_xUserEdit->get_value(FieldUnit::PERCENT));
389 pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
391 m_xDialog->response(RET_OK);
393 else
394 m_xDialog->response(RET_CANCEL);
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */