bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / dialogs / zoom.cxx
blob85dc0d2881ec590bd5ca81fa53ce95ccbf11ed77
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 <svl/itemset.hxx>
21 #include <svl/itempool.hxx>
22 #include <sfx2/objsh.hxx>
23 #include <vcl/layout.hxx>
24 #include <vcl/msgbox.hxx>
26 #include <cuires.hrc>
28 #include "zoom.hxx"
29 #include <sfx2/zoomitem.hxx>
30 #include <svx/viewlayoutitem.hxx>
31 #include <dialmgr.hxx>
32 #include <svx/zoom_def.hxx>
34 namespace
37 const sal_uInt16 SPECIAL_FACTOR = 0xFFFF;
39 } // anonymous namespace
41 sal_uInt16 SvxZoomDialog::GetFactor() const
43 if (m_p100Btn->IsChecked())
44 return 100;
46 if (m_pUserBtn->IsChecked())
47 return static_cast<sal_uInt16>(m_pUserEdit->GetValue());
48 else
49 return SPECIAL_FACTOR;
52 void SvxZoomDialog::SetFactor(sal_uInt16 nNewFactor, ZoomButtonId nButtonId)
54 m_pUserEdit->Disable();
56 if (nButtonId == ZoomButtonId::NONE)
58 if ( nNewFactor == 100 )
60 m_p100Btn->Check();
61 m_p100Btn->GrabFocus();
63 else
65 m_pUserBtn->Check();
66 m_pUserEdit->Enable();
67 m_pUserEdit->SetValue(static_cast<long>(nNewFactor));
68 m_pUserEdit->GrabFocus();
71 else
73 m_pUserEdit->SetValue(static_cast<long>(nNewFactor));
74 switch(nButtonId)
76 case ZoomButtonId::OPTIMAL:
78 m_pOptimalBtn->Check();
79 m_pOptimalBtn->GrabFocus();
80 break;
82 case ZoomButtonId::PAGEWIDTH:
84 m_pPageWidthBtn->Check();
85 m_pPageWidthBtn->GrabFocus();
86 break;
88 case ZoomButtonId::WHOLEPAGE:
90 m_pWholePageBtn->Check();
91 m_pWholePageBtn->GrabFocus();
92 break;
94 default: break;
99 void SvxZoomDialog::HideButton(ZoomButtonId nButtonId)
101 switch (nButtonId)
103 case ZoomButtonId::OPTIMAL:
104 m_pOptimalBtn->Hide();
105 break;
107 case ZoomButtonId::PAGEWIDTH:
108 m_pPageWidthBtn->Hide();
109 break;
111 case ZoomButtonId::WHOLEPAGE:
112 m_pWholePageBtn->Hide();
113 break;
115 default:
116 OSL_FAIL("Wrong button number!" );
120 void SvxZoomDialog::SetLimits(sal_uInt16 nMin, sal_uInt16 nMax)
122 DBG_ASSERT(nMin < nMax, "invalid limits");
123 m_pUserEdit->SetMin(nMin);
124 m_pUserEdit->SetFirst(nMin);
125 m_pUserEdit->SetMax(nMax);
126 m_pUserEdit->SetLast(nMax);
129 const SfxItemSet* SvxZoomDialog::GetOutputItemSet() const
131 return mpOutSet.get();
134 SvxZoomDialog::SvxZoomDialog( vcl::Window* pParent, const SfxItemSet& rCoreSet )
135 : SfxModalDialog(pParent, "ZoomDialog", "cui/ui/zoomdialog.ui")
136 , mrSet(rCoreSet)
137 , mpOutSet()
138 , mbModified(false)
141 get(m_pOptimalBtn, "optimal");
142 get(m_pWholePageBtn, "fitwandh");
143 get(m_pPageWidthBtn, "fitw");
144 get(m_p100Btn, "100pc");
145 get(m_pUserBtn, "variable");
146 get(m_pUserEdit, "zoomsb");
147 get(m_pViewFrame, "viewframe");
148 get(m_pAutomaticBtn, "automatic");
149 get(m_pSingleBtn, "singlepage");
150 get(m_pColumnsBtn, "columns");
151 get(m_pColumnsEdit, "columnssb");
152 get(m_pBookModeChk, "bookmode");
153 get(m_pOKBtn, "ok");
154 Link<> aLink = LINK(this, SvxZoomDialog, UserHdl);
155 m_p100Btn->SetClickHdl(aLink);
156 m_pOptimalBtn->SetClickHdl(aLink);
157 m_pPageWidthBtn->SetClickHdl(aLink);
158 m_pWholePageBtn->SetClickHdl(aLink);
159 m_pUserBtn->SetClickHdl(aLink);
161 Link<> aViewLayoutLink = LINK(this, SvxZoomDialog, ViewLayoutUserHdl);
162 m_pAutomaticBtn->SetClickHdl(aViewLayoutLink);
163 m_pSingleBtn->SetClickHdl(aViewLayoutLink);
164 m_pColumnsBtn->SetClickHdl(aViewLayoutLink);
166 Link<> aViewLayoutSpinLink = LINK(this, SvxZoomDialog, ViewLayoutSpinHdl);
167 m_pColumnsEdit->SetModifyHdl(aViewLayoutSpinLink);
169 Link<> aViewLayoutCheckLink = LINK(this, SvxZoomDialog, ViewLayoutCheckHdl);
170 m_pBookModeChk->SetClickHdl(aViewLayoutCheckLink);
172 m_pOKBtn->SetClickHdl(LINK(this, SvxZoomDialog, OKHdl));
173 m_pUserEdit->SetModifyHdl(LINK(this, SvxZoomDialog, SpinHdl));
175 // default values
176 sal_uInt16 nValue = 100;
177 sal_uInt16 nMin = 10;
178 sal_uInt16 nMax = 1000;
180 // maybe get the old value first
181 const SfxUInt16Item* pOldUserItem = 0;
182 SfxObjectShell* pShell = SfxObjectShell::Current();
184 if (pShell)
185 pOldUserItem = static_cast<const SfxUInt16Item*>(pShell->GetItem(SID_ATTR_ZOOM_USER));
187 if (pOldUserItem)
188 nValue = pOldUserItem->GetValue();
190 // initialize UserEdit
191 if (nMin > nValue)
192 nMin = nValue;
193 if (nMax < nValue)
194 nMax = nValue;
196 SetLimits(nMin, nMax);
197 m_pUserEdit->SetValue(nValue);
199 m_pUserEdit->SetAccessibleName(m_pUserBtn->GetText());
200 m_pColumnsEdit->SetAccessibleName(m_pColumnsBtn->GetText());
201 m_pColumnsEdit->SetAccessibleRelationMemberOf(m_pColumnsBtn);
202 m_pBookModeChk->SetAccessibleRelationMemberOf(m_pColumnsBtn);
204 const SfxPoolItem& rItem = mrSet.Get(mrSet.GetPool()->GetWhich(SID_ATTR_ZOOM));
206 if (rItem.ISA(SvxZoomItem))
208 const SvxZoomItem& rZoomItem = static_cast<const SvxZoomItem&>(rItem);
209 const sal_uInt16 nZoom = rZoomItem.GetValue();
210 const SvxZoomType eType = rZoomItem.GetType();
211 const SvxZoomEnableFlags nValSet = rZoomItem.GetValueSet();
212 ZoomButtonId nButtonId = ZoomButtonId::NONE;
214 switch (eType)
216 case SvxZoomType::OPTIMAL:
217 nButtonId = ZoomButtonId::OPTIMAL;
218 break;
219 case SvxZoomType::PAGEWIDTH:
220 nButtonId = ZoomButtonId::PAGEWIDTH;
221 break;
222 case SvxZoomType::WHOLEPAGE:
223 nButtonId = ZoomButtonId::WHOLEPAGE;
224 break;
225 case SvxZoomType::PERCENT:
226 break;
227 case SvxZoomType::PAGEWIDTH_NOBORDER:
228 break;
231 if (!(SvxZoomEnableFlags::N100 & nValSet))
232 m_p100Btn->Disable();
233 if (!(SvxZoomEnableFlags::OPTIMAL & nValSet))
234 m_pOptimalBtn->Disable();
235 if (!(SvxZoomEnableFlags::PAGEWIDTH & nValSet))
236 m_pPageWidthBtn->Disable();
237 if (!(SvxZoomEnableFlags::WHOLEPAGE & nValSet))
238 m_pWholePageBtn->Disable();
240 SetFactor(nZoom, nButtonId);
242 else
244 const sal_uInt16 nZoom = static_cast<const SfxUInt16Item&>(rItem).GetValue();
245 SetFactor(nZoom);
248 const SfxPoolItem* pPoolViewLayoutItem = NULL;
249 if (SfxItemState::SET == mrSet.GetItemState(SID_ATTR_VIEWLAYOUT, false, &pPoolViewLayoutItem))
251 const SvxViewLayoutItem* pViewLayoutItem = static_cast<const SvxViewLayoutItem*>(pPoolViewLayoutItem);
252 const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
253 const bool bBookMode = pViewLayoutItem->IsBookMode();
255 if (0 == nColumns)
257 m_pAutomaticBtn->Check();
258 m_pColumnsEdit->SetValue(2);
259 m_pColumnsEdit->Disable();
260 m_pBookModeChk->Disable();
262 else if (1 == nColumns)
264 m_pSingleBtn->Check();
265 m_pColumnsEdit->SetValue(2);
266 m_pColumnsEdit->Disable();
267 m_pBookModeChk->Disable();
269 else
271 m_pColumnsBtn->Check();
272 if (!bBookMode)
274 m_pColumnsEdit->SetValue(nColumns);
275 if (nColumns % 2 != 0)
276 m_pBookModeChk->Disable();
278 else
280 m_pColumnsEdit->SetValue(nColumns);
281 m_pBookModeChk->Check();
285 else
287 // hide view layout related controls:
288 m_pViewFrame->Disable();
292 SvxZoomDialog::~SvxZoomDialog()
294 disposeOnce();
297 void SvxZoomDialog::dispose()
299 mpOutSet.reset();
300 m_pOptimalBtn.clear();
301 m_pWholePageBtn.clear();
302 m_pPageWidthBtn.clear();
303 m_p100Btn.clear();
304 m_pUserBtn.clear();
305 m_pUserEdit.clear();
306 m_pViewFrame.clear();
307 m_pAutomaticBtn.clear();
308 m_pSingleBtn.clear();
309 m_pColumnsBtn.clear();
310 m_pColumnsEdit.clear();
311 m_pBookModeChk.clear();
312 m_pOKBtn.clear();
313 SfxModalDialog::dispose();
316 IMPL_LINK(SvxZoomDialog, UserHdl, RadioButton *, pButton)
318 mbModified = true;
320 if (pButton == m_pUserBtn)
322 m_pUserEdit->Enable();
323 m_pUserEdit->GrabFocus();
325 else
327 m_pUserEdit->Disable();
329 return 0;
332 IMPL_LINK_NOARG(SvxZoomDialog, SpinHdl)
334 if (!m_pUserBtn->IsChecked())
335 return 0;
337 mbModified = true;
338 return 0;
341 IMPL_LINK(SvxZoomDialog, ViewLayoutUserHdl, RadioButton*, pButton)
343 mbModified = true;
345 if (pButton == m_pAutomaticBtn)
347 m_pColumnsEdit->Disable();
348 m_pBookModeChk->Disable();
350 else if (pButton == m_pSingleBtn)
352 m_pColumnsEdit->Disable();
353 m_pBookModeChk->Disable();
355 else if (pButton == m_pColumnsBtn)
357 m_pColumnsEdit->Enable();
358 m_pColumnsEdit->GrabFocus();
359 if (m_pColumnsEdit->GetValue() % 2 == 0)
360 m_pBookModeChk->Enable();
362 else
364 OSL_FAIL("Wrong Button");
365 return 0;
368 return 0;
371 IMPL_LINK(SvxZoomDialog, ViewLayoutSpinHdl, NumericField*, pEdit)
373 if (pEdit == m_pColumnsEdit && !m_pColumnsBtn->IsChecked())
374 return 0;
376 if (m_pColumnsEdit->GetValue() % 2 == 0)
378 m_pBookModeChk->Enable();
380 else
382 m_pBookModeChk->Check(false);
383 m_pBookModeChk->Disable();
386 mbModified = true;
388 return 0;
391 IMPL_LINK(SvxZoomDialog, ViewLayoutCheckHdl, CheckBox*, pCheckBox)
393 if (pCheckBox == m_pBookModeChk && !m_pColumnsBtn->IsChecked())
394 return 0;
396 mbModified = true;
398 return 0;
401 IMPL_LINK(SvxZoomDialog, OKHdl, Button*, pButton)
403 if (mbModified || m_pOKBtn != pButton)
405 SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, mrSet.GetPool()->GetWhich(SID_ATTR_ZOOM));
406 SvxViewLayoutItem aViewLayoutItem(0, false, mrSet.GetPool()->GetWhich(SID_ATTR_VIEWLAYOUT));
408 if (m_pOKBtn == pButton)
410 sal_uInt16 nFactor = GetFactor();
412 if (SPECIAL_FACTOR == nFactor)
414 if (m_pOptimalBtn->IsChecked())
415 aZoomItem.SetType(SvxZoomType::OPTIMAL);
416 else if (m_pPageWidthBtn->IsChecked())
417 aZoomItem.SetType(SvxZoomType::PAGEWIDTH);
418 else if (m_pWholePageBtn->IsChecked())
419 aZoomItem.SetType(SvxZoomType::WHOLEPAGE);
421 else
423 aZoomItem.SetValue(nFactor);
426 if (m_pAutomaticBtn->IsChecked())
428 aViewLayoutItem.SetValue(0);
429 aViewLayoutItem.SetBookMode(false);
431 if (m_pSingleBtn->IsChecked())
433 aViewLayoutItem.SetValue(1);
434 aViewLayoutItem.SetBookMode(false);
436 else if (m_pColumnsBtn->IsChecked())
438 aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_pColumnsEdit->GetValue()));
439 aViewLayoutItem.SetBookMode(m_pBookModeChk->IsChecked());
442 else
444 OSL_FAIL("Wrong Button");
445 return 0;
447 mpOutSet.reset(new SfxItemSet(mrSet));
448 mpOutSet->Put(aZoomItem);
450 // don't set attribute in case the whole viewlayout stuff is disabled:
451 if (m_pViewFrame->IsEnabled())
452 mpOutSet->Put(aViewLayoutItem);
454 // memorize value from the UserEdit beyond the dialog
455 SfxObjectShell* pShell = SfxObjectShell::Current();
457 if (pShell)
459 sal_uInt16 nZoomValue = static_cast<sal_uInt16>(m_pUserEdit->GetValue());
460 pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
462 EndDialog( RET_OK );
464 else
466 EndDialog( RET_CANCEL );
468 return 0;
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */