1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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 <sfx2/dispatch.hxx>
29 #include <tools/urlobj.hxx>
30 #include <sal/log.hxx>
31
32 #include <svx/strings.hrc>
33
34 #include <svx/zoomctrl.hxx>
35 #include <svx/zoomslideritem.hxx>
36 #include <sfx2/zoomitem.hxx>
37 #include "stbctrls.h"
38 #include <svx/dialmgr.hxx>
39 #include "modctrl_internal.hxx"
40 #include <bitmaps.hlst>
41
42 #include <com/sun/star/beans/PropertyValue.hpp>
43
44 SFX_IMPL_STATUSBAR_CONTROL(SvxZoomStatusBarControl,SvxZoomItem);
45
46 class ZoomPopup_Impl
47 {
48 public:
49 ZoomPopup_Impl( sal_uInt16 nZ, SvxZoomEnableFlags nValueSet );
50
51 sal_uInt16 GetZoom();
GetCurItemIdent() const52 OString const & GetCurItemIdent() const { return m_xMenu->GetCurItemIdent(); }
53
Execute(vcl::Window * pWindow,const Point & rPopupPos)54 sal_uInt16 Execute(vcl::Window* pWindow, const Point& rPopupPos)
55 {
56 return m_xMenu->Execute(pWindow, rPopupPos);
57 }
58
59 private:
60 VclBuilder m_aBuilder;
61 VclPtr<PopupMenu> m_xMenu;
62 sal_uInt16 nZoom;
63 };
64
ZoomPopup_Impl(sal_uInt16 nZ,SvxZoomEnableFlags nValueSet)65 ZoomPopup_Impl::ZoomPopup_Impl( sal_uInt16 nZ, SvxZoomEnableFlags nValueSet )
66 : m_aBuilder(nullptr, VclBuilderContainer::getUIRootDir(), "svx/ui/zoommenu.ui", "")
67 , m_xMenu(m_aBuilder.get_menu("menu"))
68 , nZoom(nZ)
69 {
70 if ( !(SvxZoomEnableFlags::N50 & nValueSet) )
71 m_xMenu->EnableItem("50", false);
72 if ( !(SvxZoomEnableFlags::N100 & nValueSet) )
73 m_xMenu->EnableItem("100", false);
74 if ( !(SvxZoomEnableFlags::N150 & nValueSet) )
75 m_xMenu->EnableItem("150", false);
76 if ( !(SvxZoomEnableFlags::N200 & nValueSet) )
77 m_xMenu->EnableItem("200", false);
78 if ( !(SvxZoomEnableFlags::OPTIMAL & nValueSet) )
79 m_xMenu->EnableItem("optimal", false);
80 if ( !(SvxZoomEnableFlags::WHOLEPAGE & nValueSet) )
81 m_xMenu->EnableItem("page", false);
82 if ( !(SvxZoomEnableFlags::PAGEWIDTH & nValueSet) )
83 m_xMenu->EnableItem("width", false);
84 }
85
GetZoom()86 sal_uInt16 ZoomPopup_Impl::GetZoom()
87 {
88 OString sIdent = GetCurItemIdent();
89 if (sIdent == "200")
90 nZoom = 200;
91 else if (sIdent == "150")
92 nZoom = 150;
93 else if (sIdent == "100")
94 nZoom = 100;
95 else if (sIdent == "75")
96 nZoom = 75;
97 else if (sIdent == "50")
98 nZoom = 50;
99 else if (sIdent == "optimal" || sIdent == "width" || sIdent == "page")
100 nZoom = 0;
101
102 return nZoom;
103 }
104
SvxZoomStatusBarControl(sal_uInt16 _nSlotId,sal_uInt16 _nId,StatusBar & rStb)105 SvxZoomStatusBarControl::SvxZoomStatusBarControl( sal_uInt16 _nSlotId,
106 sal_uInt16 _nId,
107 StatusBar& rStb ) :
108
109 SfxStatusBarControl( _nSlotId, _nId, rStb ),
110 nZoom( 100 ),
111 nValueSet( SvxZoomEnableFlags::ALL )
112 {
113 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_ZOOMTOOL_HINT));
114 ImplUpdateItemText();
115 }
116
StateChanged(sal_uInt16,SfxItemState eState,const SfxPoolItem * pState)117 void SvxZoomStatusBarControl::StateChanged( sal_uInt16, SfxItemState eState,
118 const SfxPoolItem* pState )
119 {
120 if( SfxItemState::DEFAULT != eState )
121 {
122 GetStatusBar().SetItemText( GetId(), "" );
123 nValueSet = SvxZoomEnableFlags::NONE;
124 }
125 else if ( auto pItem = dynamic_cast< const SfxUInt16Item* >(pState) )
126 {
127 nZoom = pItem->GetValue();
128 ImplUpdateItemText();
129
130 if ( auto pZoomItem = dynamic_cast<const SvxZoomItem*>(pState) )
131 {
132 nValueSet = pZoomItem->GetValueSet();
133 }
134 else
135 {
136 SAL_INFO( "svx", "use SfxZoomItem for SID_ATTR_ZOOM" );
137 nValueSet = SvxZoomEnableFlags::ALL;
138 }
139 }
140 }
141
ImplUpdateItemText()142 void SvxZoomStatusBarControl::ImplUpdateItemText()
143 {
144 // workaround - don't bother updating when we don't have a real zoom value
145 if (nZoom)
146 {
147 OUString aStr(unicode::formatPercent(nZoom, Application::GetSettings().GetUILanguageTag()));
148 GetStatusBar().SetItemText( GetId(), aStr );
149 }
150 }
151
Paint(const UserDrawEvent &)152 void SvxZoomStatusBarControl::Paint( const UserDrawEvent& )
153 {
154 }
155
Command(const CommandEvent & rCEvt)156 void SvxZoomStatusBarControl::Command( const CommandEvent& rCEvt )
157 {
158 if ( CommandEventId::ContextMenu == rCEvt.GetCommand() && bool(nValueSet) )
159 {
160 ZoomPopup_Impl aPop(nZoom, nValueSet);
161 StatusBar& rStatusbar = GetStatusBar();
162
163 if (aPop.Execute(&rStatusbar, rCEvt.GetMousePosPixel()) && (nZoom != aPop.GetZoom() || !nZoom))
164 {
165 nZoom = aPop.GetZoom();
166 ImplUpdateItemText();
167 SvxZoomItem aZoom(SvxZoomType::PERCENT, nZoom, GetId());
168
169 OString sIdent = aPop.GetCurItemIdent();
170 if (sIdent == "optimal")
171 aZoom.SetType(SvxZoomType::OPTIMAL);
172 else if (sIdent == "width")
173 aZoom.SetType(SvxZoomType::PAGEWIDTH);
174 else if (sIdent == "page")
175 aZoom.SetType(SvxZoomType::WHOLEPAGE);
176
177 css::uno::Any a;
178 INetURLObject aObj( m_aCommandURL );
179
180 css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
181 aArgs[0].Name = aObj.GetURLPath();
182 aZoom.QueryValue( a );
183 aArgs[0].Value = a;
184
185 execute( aArgs );
186 }
187 }
188 else
189 SfxStatusBarControl::Command( rCEvt );
190 }
191
192 SFX_IMPL_STATUSBAR_CONTROL(SvxZoomPageStatusBarControl,SfxVoidItem);
193
SvxZoomPageStatusBarControl(sal_uInt16 _nSlotId,sal_uInt16 _nId,StatusBar & rStb)194 SvxZoomPageStatusBarControl::SvxZoomPageStatusBarControl(sal_uInt16 _nSlotId,
195 sal_uInt16 _nId, StatusBar& rStb)
196 : SfxStatusBarControl(_nSlotId, _nId, rStb)
197 , maImage(StockImage::Yes, RID_SVXBMP_ZOOM_PAGE)
198 {
199 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
200 }
201
Paint(const UserDrawEvent & rUsrEvt)202 void SvxZoomPageStatusBarControl::Paint(const UserDrawEvent& rUsrEvt)
203 {
204 vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
205 tools::Rectangle aRect = rUsrEvt.GetRect();
206 Point aPt = centerImage(aRect, maImage);
207 pDev->DrawImage(aPt, maImage);
208 }
209
MouseButtonDown(const MouseEvent &)210 bool SvxZoomPageStatusBarControl::MouseButtonDown(const MouseEvent&)
211 {
212 SvxZoomItem aZoom( SvxZoomType::WHOLEPAGE, 0, GetId() );
213
214 css::uno::Any a;
215 INetURLObject aObj( m_aCommandURL );
216
217 css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
218 aArgs[0].Name = aObj.GetURLPath();
219 aZoom.QueryValue( a );
220 aArgs[0].Value = a;
221
222 execute( aArgs );
223
224 return true;
225 }
226
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
228