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/commandevent.hxx>
22 #include <vcl/event.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/status.hxx>
25 #include <vcl/weldutils.hxx>
26 #include <vcl/settings.hxx>
27 #include <tools/urlobj.hxx>
28 #include <sal/log.hxx>
29 
30 #include <svx/strings.hrc>
31 
32 #include <svx/zoomctrl.hxx>
33 #include <sfx2/zoomitem.hxx>
34 #include <svx/dialmgr.hxx>
35 #include "modctrl_internal.hxx"
36 #include <bitmaps.hlst>
37 
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 
41 
42 namespace {
43 
44 class ZoomPopup_Impl
45 {
46 public:
47  ZoomPopup_Impl(weld::Window* pPopupParent, sal_uInt16 nZ, SvxZoomEnableFlags nValueSet);
48 
49  sal_uInt16 GetZoom(std::string_view ident);
50 
51  OString popup_at_rect(const tools::Rectangle& rRect)
52  {
53  return m_xMenu->popup_at_rect(m_pPopupParent, rRect);
54  }
55 
56 private:
57  weld::Window* m_pPopupParent;
58  std::unique_ptr<weld::Builder> m_xBuilder;
59  std::unique_ptr<weld::Menu> m_xMenu;
60  sal_uInt16 nZoom;
61 };
62 
63 }
64 
65 ZoomPopup_Impl::ZoomPopup_Impl(weld::Window* pPopupParent, sal_uInt16 nZ, SvxZoomEnableFlags nValueSet)
66  : m_pPopupParent(pPopupParent)
67  , m_xBuilder(Application::CreateBuilder(m_pPopupParent, "svx/ui/zoommenu.ui"))
68  , m_xMenu(m_xBuilder->weld_menu("menu"))
69  , nZoom(nZ)
70 {
71  if ( !(SvxZoomEnableFlags::N50 & nValueSet) )
72  m_xMenu->set_sensitive("50", false);
73  if ( !(SvxZoomEnableFlags::N100 & nValueSet) )
74  m_xMenu->set_sensitive("100", false);
75  if ( !(SvxZoomEnableFlags::N150 & nValueSet) )
76  m_xMenu->set_sensitive("150", false);
77  if ( !(SvxZoomEnableFlags::N200 & nValueSet) )
78  m_xMenu->set_sensitive("200", false);
79  if ( !(SvxZoomEnableFlags::OPTIMAL & nValueSet) )
80  m_xMenu->set_sensitive("optimal", false);
81  if ( !(SvxZoomEnableFlags::WHOLEPAGE & nValueSet) )
82  m_xMenu->set_sensitive("page", false);
83  if ( !(SvxZoomEnableFlags::PAGEWIDTH & nValueSet) )
84  m_xMenu->set_sensitive("width", false);
85 }
86 
87 sal_uInt16 ZoomPopup_Impl::GetZoom(std::string_view ident)
88 {
89  sal_uInt16 nRet = nZoom;
90 
91  if (ident == "200")
92  nRet = 200;
93  else if (ident == "150")
94  nRet = 150;
95  else if (ident == "100")
96  nRet = 100;
97  else if (ident == "75")
98  nRet = 75;
99  else if (ident == "50")
100  nRet = 50;
101  else if (ident == "optimal" || ident == "width" || ident == "page")
102  nRet = 0;
103 
104  return nRet;
105 }
106 
108  sal_uInt16 _nId,
109  StatusBar& rStb ) :
110 
111  SfxStatusBarControl( _nSlotId, _nId, rStb ),
112  nZoom( 100 ),
113  nValueSet( SvxZoomEnableFlags::ALL )
114 {
115  GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_ZOOMTOOL_HINT));
117 }
118 
120  const SfxPoolItem* pState )
121 {
122  if( SfxItemState::DEFAULT != eState )
123  {
124  GetStatusBar().SetItemText( GetId(), "" );
125  nValueSet = SvxZoomEnableFlags::NONE;
126  }
127  else if ( auto pItem = dynamic_cast< const SfxUInt16Item* >(pState) )
128  {
129  nZoom = pItem->GetValue();
131 
132  if ( auto pZoomItem = dynamic_cast<const SvxZoomItem*>(pState) )
133  {
134  nValueSet = pZoomItem->GetValueSet();
135  }
136  else
137  {
138  SAL_INFO( "svx", "use SfxZoomItem for SID_ATTR_ZOOM" );
139  nValueSet = SvxZoomEnableFlags::ALL;
140  }
141  }
142 }
143 
145 {
146  // workaround - don't bother updating when we don't have a real zoom value
147  if (nZoom)
148  {
149  OUString aStr(unicode::formatPercent(nZoom, Application::GetSettings().GetUILanguageTag()));
150  GetStatusBar().SetItemText( GetId(), aStr );
151  }
152 }
153 
155 {
156 }
157 
159 {
160  if ( CommandEventId::ContextMenu == rCEvt.GetCommand() && bool(nValueSet) )
161  {
162  ::tools::Rectangle aRect(rCEvt.GetMousePosPixel(), Size(1, 1));
163  weld::Window* pPopupParent = weld::GetPopupParent(GetStatusBar(), aRect);
164  ZoomPopup_Impl aPop(pPopupParent, nZoom, nValueSet);
165 
166  OString sIdent = aPop.popup_at_rect(aRect);
167  if (!sIdent.isEmpty() && (nZoom != aPop.GetZoom(sIdent) || !nZoom))
168  {
169  nZoom = aPop.GetZoom(sIdent);
171  SvxZoomItem aZoom(SvxZoomType::PERCENT, nZoom, GetId());
172 
173  if (sIdent == "optimal")
174  aZoom.SetType(SvxZoomType::OPTIMAL);
175  else if (sIdent == "width")
176  aZoom.SetType(SvxZoomType::PAGEWIDTH);
177  else if (sIdent == "page")
178  aZoom.SetType(SvxZoomType::WHOLEPAGE);
179 
180  css::uno::Any a;
181  INetURLObject aObj( m_aCommandURL );
182 
183  css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
184  aArgs[0].Name = aObj.GetURLPath();
185  aZoom.QueryValue( a );
186  aArgs[0].Value = a;
187 
188  execute( aArgs );
189  }
190  }
191  else
193 }
194 
196 
198  sal_uInt16 _nId, StatusBar& rStb)
199  : SfxStatusBarControl(_nSlotId, _nId, rStb)
200  , maImage(StockImage::Yes, RID_SVXBMP_ZOOM_PAGE)
201 {
202  GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
203 }
204 
206 {
207  vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
208  tools::Rectangle aRect = rUsrEvt.GetRect();
209  Point aPt = centerImage(aRect, maImage);
210  pDev->DrawImage(aPt, maImage);
211 }
212 
214 {
215  SvxZoomItem aZoom( SvxZoomType::WHOLEPAGE, 0, GetId() );
216 
217  css::uno::Any a;
218  INetURLObject aObj( m_aCommandURL );
219 
220  css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
221  aArgs[0].Name = aObj.GetURLPath();
222  aZoom.QueryValue( a );
223  aArgs[0].Value = a;
224 
225  execute( aArgs );
226 
227  return true;
228 }
229 
230 /* 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:205
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:213
static const AllSettings & GetSettings()
void SetItemText(sal_uInt16 nItemId, const OUString &rText, int nCharsWidth=-1)
SFX_IMPL_STATUSBAR_CONTROL(SvxZoomStatusBarControl, SvxZoomItem)
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
virtual void Command(const CommandEvent &rCEvt) override
Definition: zoomctrl.cxx:158
sal_uInt16 GetId() const
virtual void Paint(const UserDrawEvent &rEvt) override
Definition: zoomctrl.cxx:154
vcl::RenderContext * GetRenderContext() const
uno_Any a
weld::Window * GetPopupParent(vcl::Window &rOutWin, tools::Rectangle &rRect)
CommandEventId GetCommand() const
const Point & GetMousePosPixel() const
StockImage
SvxZoomStatusBarControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: zoomctrl.cxx:107
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:128
virtual void StateChangedAtStatusBarControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: zoomctrl.cxx:119
aStr
SvxZoomPageStatusBarControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: zoomctrl.cxx:197