Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / stbctrls / zoomctrl.cxx
blob77bb47fce38f709f60feb401e4bb66e120ec8fa3
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 <sal/config.h>
22 #include <comphelper/propertyvalue.hxx>
23 #include <i18nutil/unicode.hxx>
24 #include <vcl/commandevent.hxx>
25 #include <vcl/event.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/status.hxx>
28 #include <vcl/weldutils.hxx>
29 #include <vcl/settings.hxx>
30 #include <tools/urlobj.hxx>
31 #include <sal/log.hxx>
33 #include <svx/strings.hrc>
35 #include <svx/zoomctrl.hxx>
36 #include <sfx2/zoomitem.hxx>
37 #include <svx/dialmgr.hxx>
38 #include "modctrl_internal.hxx"
39 #include <bitmaps.hlst>
41 #include <com/sun/star/beans/PropertyValue.hpp>
42 #include <com/sun/star/frame/ModuleManager.hpp>
44 SFX_IMPL_STATUSBAR_CONTROL(SvxZoomStatusBarControl,SvxZoomItem);
46 namespace {
48 class ZoomPopup_Impl
50 public:
51 ZoomPopup_Impl(weld::Window* pPopupParent, sal_uInt16 nZ, SvxZoomEnableFlags nValueSet);
53 sal_uInt16 GetZoom(std::u16string_view ident) const;
55 OUString popup_at_rect(const tools::Rectangle& rRect)
57 return m_xMenu->popup_at_rect(m_pPopupParent, rRect);
60 private:
61 weld::Window* m_pPopupParent;
62 std::unique_ptr<weld::Builder> m_xBuilder;
63 std::unique_ptr<weld::Menu> m_xMenu;
64 sal_uInt16 nZoom;
69 ZoomPopup_Impl::ZoomPopup_Impl(weld::Window* pPopupParent, sal_uInt16 nZ, SvxZoomEnableFlags nValueSet)
70 : m_pPopupParent(pPopupParent)
71 , m_xBuilder(Application::CreateBuilder(m_pPopupParent, "svx/ui/zoommenu.ui"))
72 , m_xMenu(m_xBuilder->weld_menu("menu"))
73 , nZoom(nZ)
75 if ( !(SvxZoomEnableFlags::N50 & nValueSet) )
76 m_xMenu->set_sensitive("50", false);
77 if ( !(SvxZoomEnableFlags::N100 & nValueSet) )
78 m_xMenu->set_sensitive("100", false);
79 if ( !(SvxZoomEnableFlags::N150 & nValueSet) )
80 m_xMenu->set_sensitive("150", false);
81 if ( !(SvxZoomEnableFlags::N200 & nValueSet) )
82 m_xMenu->set_sensitive("200", false);
83 if ( !(SvxZoomEnableFlags::OPTIMAL & nValueSet) )
84 m_xMenu->set_sensitive("optimal", false);
85 if ( !(SvxZoomEnableFlags::WHOLEPAGE & nValueSet) )
86 m_xMenu->set_sensitive("page", false);
87 if ( !(SvxZoomEnableFlags::PAGEWIDTH & nValueSet) )
88 m_xMenu->set_sensitive("width", false);
91 sal_uInt16 ZoomPopup_Impl::GetZoom(std::u16string_view ident) const
93 sal_uInt16 nRet = nZoom;
95 if (ident == u"200")
96 nRet = 200;
97 else if (ident == u"150")
98 nRet = 150;
99 else if (ident == u"100")
100 nRet = 100;
101 else if (ident == u"75")
102 nRet = 75;
103 else if (ident == u"50")
104 nRet = 50;
105 else if (ident == u"optimal" || ident == u"width" || ident == u"page")
106 nRet = 0;
108 return nRet;
111 SvxZoomStatusBarControl::SvxZoomStatusBarControl( sal_uInt16 _nSlotId,
112 sal_uInt16 _nId,
113 StatusBar& rStb ) :
115 SfxStatusBarControl( _nSlotId, _nId, rStb ),
116 nZoom( 100 ),
117 nValueSet( SvxZoomEnableFlags::ALL )
119 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_ZOOMTOOL_HINT));
120 ImplUpdateItemText();
123 void SvxZoomStatusBarControl::StateChangedAtStatusBarControl( sal_uInt16, SfxItemState eState,
124 const SfxPoolItem* pState )
126 if( SfxItemState::DEFAULT != eState )
128 GetStatusBar().SetItemText( GetId(), "" );
129 nValueSet = SvxZoomEnableFlags::NONE;
131 else if ( auto pItem = dynamic_cast< const SfxUInt16Item* >(pState) )
133 nZoom = pItem->GetValue();
134 ImplUpdateItemText();
136 if ( auto pZoomItem = dynamic_cast<const SvxZoomItem*>(pState) )
138 nValueSet = pZoomItem->GetValueSet();
140 else
142 SAL_INFO( "svx", "use SfxZoomItem for SID_ATTR_ZOOM" );
143 nValueSet = SvxZoomEnableFlags::ALL;
148 void SvxZoomStatusBarControl::ImplUpdateItemText()
150 // workaround - don't bother updating when we don't have a real zoom value
151 if (nZoom)
153 OUString aStr(unicode::formatPercent(nZoom, Application::GetSettings().GetUILanguageTag()));
154 GetStatusBar().SetItemText( GetId(), aStr );
158 void SvxZoomStatusBarControl::Paint( const UserDrawEvent& )
162 void SvxZoomStatusBarControl::Command( const CommandEvent& rCEvt )
164 if ( CommandEventId::ContextMenu == rCEvt.GetCommand() && bool(nValueSet) )
166 ::tools::Rectangle aRect(rCEvt.GetMousePosPixel(), Size(1, 1));
167 weld::Window* pPopupParent = weld::GetPopupParent(GetStatusBar(), aRect);
168 ZoomPopup_Impl aPop(pPopupParent, nZoom, nValueSet);
170 OUString sIdent = aPop.popup_at_rect(aRect);
171 if (!sIdent.isEmpty() && (nZoom != aPop.GetZoom(sIdent) || !nZoom))
173 nZoom = aPop.GetZoom(sIdent);
174 ImplUpdateItemText();
175 SvxZoomItem aZoom(SvxZoomType::PERCENT, nZoom, TypedWhichId<SvxZoomItem>(GetId()));
177 if (sIdent == "optimal")
178 aZoom.SetType(SvxZoomType::OPTIMAL);
179 else if (sIdent == "width")
180 aZoom.SetType(SvxZoomType::PAGEWIDTH);
181 else if (sIdent == "page")
182 aZoom.SetType(SvxZoomType::WHOLEPAGE);
184 css::uno::Any a;
185 aZoom.QueryValue( a );
186 INetURLObject aObj( m_aCommandURL );
188 css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
189 aObj.GetURLPath(), a) };
190 execute( aArgs );
193 else
194 SfxStatusBarControl::Command( rCEvt );
197 SFX_IMPL_STATUSBAR_CONTROL(SvxZoomPageStatusBarControl,SfxVoidItem);
199 SvxZoomPageStatusBarControl::SvxZoomPageStatusBarControl(sal_uInt16 _nSlotId,
200 sal_uInt16 _nId, StatusBar& rStb)
201 : SfxStatusBarControl(_nSlotId, _nId, rStb)
202 , maImage(StockImage::Yes, RID_SVXBMP_ZOOM_PAGE)
204 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
207 void SAL_CALL SvxZoomPageStatusBarControl::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
209 // Call inherited initialize
210 StatusbarController::initialize(aArguments);
212 // Get document type
213 css::uno::Reference< css::frame::XModuleManager2 > xModuleManager = css::frame::ModuleManager::create( m_xContext );
214 OUString aModuleIdentifier = xModuleManager->identify( css::uno::Reference<XInterface>( m_xFrame, css::uno::UnoReference_Query::UNO_QUERY ) );
216 // Decide what to show in zoom bar
217 if ( aModuleIdentifier == "com.sun.star.drawing.DrawingDocument" )
219 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_PAGE));
221 else if ( aModuleIdentifier == "com.sun.star.presentation.PresentationDocument" )
223 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
227 void SvxZoomPageStatusBarControl::Paint(const UserDrawEvent& rUsrEvt)
229 vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
230 tools::Rectangle aRect = rUsrEvt.GetRect();
231 Point aPt = centerImage(aRect, maImage);
232 pDev->DrawImage(aPt, maImage);
235 bool SvxZoomPageStatusBarControl::MouseButtonDown(const MouseEvent&)
237 SvxZoomItem aZoom( SvxZoomType::WHOLEPAGE, 0, TypedWhichId<SvxZoomItem>(GetId()) );
239 css::uno::Any a;
240 aZoom.QueryValue( a );
241 INetURLObject aObj( m_aCommandURL );
243 css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
244 aObj.GetURLPath(), a) };
245 execute( aArgs );
247 return true;
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */