Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / stbctrls / zoomctrl.cxx
blobd598822eda8141d47b6c6b8319e8e8d1f56b7a09
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 <i18nutil/unicode.hxx>
21 #include <vcl/builder.hxx>
22 #include <vcl/commandevent.hxx>
23 #include <vcl/event.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/status.hxx>
26 #include <vcl/menu.hxx>
27 #include <vcl/settings.hxx>
28 #include <tools/urlobj.hxx>
29 #include <sal/log.hxx>
31 #include <svx/strings.hrc>
33 #include <svx/zoomctrl.hxx>
34 #include <sfx2/zoomitem.hxx>
35 #include <svx/dialmgr.hxx>
36 #include "modctrl_internal.hxx"
37 #include <bitmaps.hlst>
39 #include <com/sun/star/beans/PropertyValue.hpp>
41 SFX_IMPL_STATUSBAR_CONTROL(SvxZoomStatusBarControl,SvxZoomItem);
43 namespace {
45 class ZoomPopup_Impl
47 public:
48 ZoomPopup_Impl( sal_uInt16 nZ, SvxZoomEnableFlags nValueSet );
50 sal_uInt16 GetZoom();
51 OString const & GetCurItemIdent() const { return m_xMenu->GetCurItemIdent(); }
53 sal_uInt16 Execute(vcl::Window* pWindow, const Point& rPopupPos)
55 return m_xMenu->Execute(pWindow, rPopupPos);
58 private:
59 VclBuilder m_aBuilder;
60 VclPtr<PopupMenu> m_xMenu;
61 sal_uInt16 nZoom;
66 ZoomPopup_Impl::ZoomPopup_Impl( sal_uInt16 nZ, SvxZoomEnableFlags nValueSet )
67 : m_aBuilder(nullptr, AllSettings::GetUIRootDir(), "svx/ui/zoommenu.ui", "")
68 , m_xMenu(m_aBuilder.get_menu("menu"))
69 , nZoom(nZ)
71 if ( !(SvxZoomEnableFlags::N50 & nValueSet) )
72 m_xMenu->EnableItem("50", false);
73 if ( !(SvxZoomEnableFlags::N100 & nValueSet) )
74 m_xMenu->EnableItem("100", false);
75 if ( !(SvxZoomEnableFlags::N150 & nValueSet) )
76 m_xMenu->EnableItem("150", false);
77 if ( !(SvxZoomEnableFlags::N200 & nValueSet) )
78 m_xMenu->EnableItem("200", false);
79 if ( !(SvxZoomEnableFlags::OPTIMAL & nValueSet) )
80 m_xMenu->EnableItem("optimal", false);
81 if ( !(SvxZoomEnableFlags::WHOLEPAGE & nValueSet) )
82 m_xMenu->EnableItem("page", false);
83 if ( !(SvxZoomEnableFlags::PAGEWIDTH & nValueSet) )
84 m_xMenu->EnableItem("width", false);
87 sal_uInt16 ZoomPopup_Impl::GetZoom()
89 OString sIdent = GetCurItemIdent();
90 if (sIdent == "200")
91 nZoom = 200;
92 else if (sIdent == "150")
93 nZoom = 150;
94 else if (sIdent == "100")
95 nZoom = 100;
96 else if (sIdent == "75")
97 nZoom = 75;
98 else if (sIdent == "50")
99 nZoom = 50;
100 else if (sIdent == "optimal" || sIdent == "width" || sIdent == "page")
101 nZoom = 0;
103 return nZoom;
106 SvxZoomStatusBarControl::SvxZoomStatusBarControl( sal_uInt16 _nSlotId,
107 sal_uInt16 _nId,
108 StatusBar& rStb ) :
110 SfxStatusBarControl( _nSlotId, _nId, rStb ),
111 nZoom( 100 ),
112 nValueSet( SvxZoomEnableFlags::ALL )
114 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_ZOOMTOOL_HINT));
115 ImplUpdateItemText();
118 void SvxZoomStatusBarControl::StateChanged( sal_uInt16, SfxItemState eState,
119 const SfxPoolItem* pState )
121 if( SfxItemState::DEFAULT != eState )
123 GetStatusBar().SetItemText( GetId(), "" );
124 nValueSet = SvxZoomEnableFlags::NONE;
126 else if ( auto pItem = dynamic_cast< const SfxUInt16Item* >(pState) )
128 nZoom = pItem->GetValue();
129 ImplUpdateItemText();
131 if ( auto pZoomItem = dynamic_cast<const SvxZoomItem*>(pState) )
133 nValueSet = pZoomItem->GetValueSet();
135 else
137 SAL_INFO( "svx", "use SfxZoomItem for SID_ATTR_ZOOM" );
138 nValueSet = SvxZoomEnableFlags::ALL;
143 void SvxZoomStatusBarControl::ImplUpdateItemText()
145 // workaround - don't bother updating when we don't have a real zoom value
146 if (nZoom)
148 OUString aStr(unicode::formatPercent(nZoom, Application::GetSettings().GetUILanguageTag()));
149 GetStatusBar().SetItemText( GetId(), aStr );
153 void SvxZoomStatusBarControl::Paint( const UserDrawEvent& )
157 void SvxZoomStatusBarControl::Command( const CommandEvent& rCEvt )
159 if ( CommandEventId::ContextMenu == rCEvt.GetCommand() && bool(nValueSet) )
161 ZoomPopup_Impl aPop(nZoom, nValueSet);
162 StatusBar& rStatusbar = GetStatusBar();
164 if (aPop.Execute(&rStatusbar, rCEvt.GetMousePosPixel()) && (nZoom != aPop.GetZoom() || !nZoom))
166 nZoom = aPop.GetZoom();
167 ImplUpdateItemText();
168 SvxZoomItem aZoom(SvxZoomType::PERCENT, nZoom, GetId());
170 OString sIdent = aPop.GetCurItemIdent();
171 if (sIdent == "optimal")
172 aZoom.SetType(SvxZoomType::OPTIMAL);
173 else if (sIdent == "width")
174 aZoom.SetType(SvxZoomType::PAGEWIDTH);
175 else if (sIdent == "page")
176 aZoom.SetType(SvxZoomType::WHOLEPAGE);
178 css::uno::Any a;
179 INetURLObject aObj( m_aCommandURL );
181 css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
182 aArgs[0].Name = aObj.GetURLPath();
183 aZoom.QueryValue( a );
184 aArgs[0].Value = a;
186 execute( aArgs );
189 else
190 SfxStatusBarControl::Command( rCEvt );
193 SFX_IMPL_STATUSBAR_CONTROL(SvxZoomPageStatusBarControl,SfxVoidItem);
195 SvxZoomPageStatusBarControl::SvxZoomPageStatusBarControl(sal_uInt16 _nSlotId,
196 sal_uInt16 _nId, StatusBar& rStb)
197 : SfxStatusBarControl(_nSlotId, _nId, rStb)
198 , maImage(StockImage::Yes, RID_SVXBMP_ZOOM_PAGE)
200 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
203 void SvxZoomPageStatusBarControl::Paint(const UserDrawEvent& rUsrEvt)
205 vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
206 tools::Rectangle aRect = rUsrEvt.GetRect();
207 Point aPt = centerImage(aRect, maImage);
208 pDev->DrawImage(aPt, maImage);
211 bool SvxZoomPageStatusBarControl::MouseButtonDown(const MouseEvent&)
213 SvxZoomItem aZoom( SvxZoomType::WHOLEPAGE, 0, GetId() );
215 css::uno::Any a;
216 INetURLObject aObj( m_aCommandURL );
218 css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
219 aArgs[0].Name = aObj.GetURLPath();
220 aZoom.QueryValue( a );
221 aArgs[0].Value = a;
223 execute( aArgs );
225 return true;
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */