LibreOffice Module svx (master)  1
zoomctrl.cxx
Go to the documentation of this file.
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 
45 
46 namespace {
47 
48 class ZoomPopup_Impl
49 {
50 public:
51  ZoomPopup_Impl( sal_uInt16 nZ, SvxZoomEnableFlags nValueSet );
52 
53  sal_uInt16 GetZoom();
54  OString const & GetCurItemIdent() const { return m_xMenu->GetCurItemIdent(); }
55 
56  sal_uInt16 Execute(vcl::Window* pWindow, const Point& rPopupPos)
57  {
58  return m_xMenu->Execute(pWindow, rPopupPos);
59  }
60 
61 private:
62  VclBuilder m_aBuilder;
63  VclPtr<PopupMenu> m_xMenu;
64  sal_uInt16 nZoom;
65 };
66 
67 }
68 
69 ZoomPopup_Impl::ZoomPopup_Impl( sal_uInt16 nZ, SvxZoomEnableFlags nValueSet )
70  : m_aBuilder(nullptr, VclBuilderContainer::getUIRootDir(), "svx/ui/zoommenu.ui", "")
71  , m_xMenu(m_aBuilder.get_menu("menu"))
72  , nZoom(nZ)
73 {
74  if ( !(SvxZoomEnableFlags::N50 & nValueSet) )
75  m_xMenu->EnableItem("50", false);
76  if ( !(SvxZoomEnableFlags::N100 & nValueSet) )
77  m_xMenu->EnableItem("100", false);
78  if ( !(SvxZoomEnableFlags::N150 & nValueSet) )
79  m_xMenu->EnableItem("150", false);
80  if ( !(SvxZoomEnableFlags::N200 & nValueSet) )
81  m_xMenu->EnableItem("200", false);
82  if ( !(SvxZoomEnableFlags::OPTIMAL & nValueSet) )
83  m_xMenu->EnableItem("optimal", false);
84  if ( !(SvxZoomEnableFlags::WHOLEPAGE & nValueSet) )
85  m_xMenu->EnableItem("page", false);
86  if ( !(SvxZoomEnableFlags::PAGEWIDTH & nValueSet) )
87  m_xMenu->EnableItem("width", false);
88 }
89 
90 sal_uInt16 ZoomPopup_Impl::GetZoom()
91 {
92  OString sIdent = GetCurItemIdent();
93  if (sIdent == "200")
94  nZoom = 200;
95  else if (sIdent == "150")
96  nZoom = 150;
97  else if (sIdent == "100")
98  nZoom = 100;
99  else if (sIdent == "75")
100  nZoom = 75;
101  else if (sIdent == "50")
102  nZoom = 50;
103  else if (sIdent == "optimal" || sIdent == "width" || sIdent == "page")
104  nZoom = 0;
105 
106  return nZoom;
107 }
108 
110  sal_uInt16 _nId,
111  StatusBar& rStb ) :
112 
113  SfxStatusBarControl( _nSlotId, _nId, rStb ),
114  nZoom( 100 ),
115  nValueSet( SvxZoomEnableFlags::ALL )
116 {
117  GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_ZOOMTOOL_HINT));
119 }
120 
122  const SfxPoolItem* pState )
123 {
124  if( SfxItemState::DEFAULT != eState )
125  {
126  GetStatusBar().SetItemText( GetId(), "" );
127  nValueSet = SvxZoomEnableFlags::NONE;
128  }
129  else if ( auto pItem = dynamic_cast< const SfxUInt16Item* >(pState) )
130  {
131  nZoom = pItem->GetValue();
133 
134  if ( auto pZoomItem = dynamic_cast<const SvxZoomItem*>(pState) )
135  {
136  nValueSet = pZoomItem->GetValueSet();
137  }
138  else
139  {
140  SAL_INFO( "svx", "use SfxZoomItem for SID_ATTR_ZOOM" );
141  nValueSet = SvxZoomEnableFlags::ALL;
142  }
143  }
144 }
145 
147 {
148  // workaround - don't bother updating when we don't have a real zoom value
149  if (nZoom)
150  {
151  OUString aStr(unicode::formatPercent(nZoom, Application::GetSettings().GetUILanguageTag()));
152  GetStatusBar().SetItemText( GetId(), aStr );
153  }
154 }
155 
157 {
158 }
159 
161 {
162  if ( CommandEventId::ContextMenu == rCEvt.GetCommand() && bool(nValueSet) )
163  {
164  ZoomPopup_Impl aPop(nZoom, nValueSet);
165  StatusBar& rStatusbar = GetStatusBar();
166 
167  if (aPop.Execute(&rStatusbar, rCEvt.GetMousePosPixel()) && (nZoom != aPop.GetZoom() || !nZoom))
168  {
169  nZoom = aPop.GetZoom();
171  SvxZoomItem aZoom(SvxZoomType::PERCENT, nZoom, GetId());
172 
173  OString sIdent = aPop.GetCurItemIdent();
174  if (sIdent == "optimal")
175  aZoom.SetType(SvxZoomType::OPTIMAL);
176  else if (sIdent == "width")
177  aZoom.SetType(SvxZoomType::PAGEWIDTH);
178  else if (sIdent == "page")
179  aZoom.SetType(SvxZoomType::WHOLEPAGE);
180 
181  css::uno::Any a;
182  INetURLObject aObj( m_aCommandURL );
183 
184  css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
185  aArgs[0].Name = aObj.GetURLPath();
186  aZoom.QueryValue( a );
187  aArgs[0].Value = a;
188 
189  execute( aArgs );
190  }
191  }
192  else
194 }
195 
197 
199  sal_uInt16 _nId, StatusBar& rStb)
200  : SfxStatusBarControl(_nSlotId, _nId, rStb)
201  , maImage(StockImage::Yes, RID_SVXBMP_ZOOM_PAGE)
202 {
203  GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
204 }
205 
207 {
208  vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
209  tools::Rectangle aRect = rUsrEvt.GetRect();
210  Point aPt = centerImage(aRect, maImage);
211  pDev->DrawImage(aPt, maImage);
212 }
213 
215 {
216  SvxZoomItem aZoom( SvxZoomType::WHOLEPAGE, 0, GetId() );
217 
218  css::uno::Any a;
219  INetURLObject aObj( m_aCommandURL );
220 
221  css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
222  aArgs[0].Name = aObj.GetURLPath();
223  aZoom.QueryValue( a );
224  aArgs[0].Value = a;
225 
226  execute( aArgs );
227 
228  return true;
229 }
230 
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
virtual void Paint(const UserDrawEvent &rEvt) override
Definition: zoomctrl.cxx:206
const tools::Rectangle & GetRect() const
void SetType(SvxZoomType eNewType)
OUString GetURLPath(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
virtual bool MouseButtonDown(const MouseEvent &rEvt) override
Definition: zoomctrl.cxx:214
static const AllSettings & GetSettings()
void SetItemText(sal_uInt16 nItemId, const OUString &rText, int nCharsWidth=-1)
SFX_IMPL_STATUSBAR_CONTROL(SvxZoomStatusBarControl, SvxZoomItem)
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
virtual void Command(const CommandEvent &rCEvt) override
Definition: zoomctrl.cxx:160
sal_uInt16 GetId() const
virtual void Paint(const UserDrawEvent &rEvt) override
Definition: zoomctrl.cxx:156
vcl::RenderContext * GetRenderContext() const
uno_Any a
CommandEventId GetCommand() const
virtual void StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: zoomctrl.cxx:121
const Point & GetMousePosPixel() const
StockImage
SvxZoomStatusBarControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: zoomctrl.cxx:109
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
static OUString formatPercent(double dNumber, const LanguageTag &rLangTag)
SfxItemState
SvxZoomEnableFlags
virtual void Command(const CommandEvent &rCEvt)
StatusBar & GetStatusBar() const
#define SAL_INFO(area, stream)
Point centerImage(const tools::Rectangle &rBoundingRect, const Image &rImg)
Given a bounding rectangle and an image, determine the top-left position of the image so that the ima...
Definition: modctrl.cxx:129
aStr
SvxZoomPageStatusBarControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: zoomctrl.cxx:198