Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/stbctrls/zoomctrl.cxx
Line
Count
Source
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 <sal/config.h>
21
22
#include <comphelper/propertyvalue.hxx>
23
#include <i18nutil/unicode.hxx>
24
#include <svl/voiditem.hxx>
25
#include <vcl/commandevent.hxx>
26
#include <vcl/event.hxx>
27
#include <vcl/svapp.hxx>
28
#include <vcl/status.hxx>
29
#include <vcl/weld/Builder.hxx>
30
#include <vcl/weld/Menu.hxx>
31
#include <vcl/weld/Window.hxx>
32
#include <vcl/weld/weldutils.hxx>
33
#include <vcl/settings.hxx>
34
#include <tools/urlobj.hxx>
35
#include <sal/log.hxx>
36
37
#include <svx/strings.hrc>
38
39
#include <svx/zoomctrl.hxx>
40
#include <sfx2/zoomitem.hxx>
41
#include <svx/dialmgr.hxx>
42
#include "modctrl_internal.hxx"
43
#include <bitmaps.hlst>
44
45
#include <com/sun/star/beans/PropertyValue.hpp>
46
#include <com/sun/star/frame/ModuleManager.hpp>
47
#include <com/sun/star/frame/XFrame.hpp>
48
49
SFX_IMPL_STATUSBAR_CONTROL(SvxZoomStatusBarControl,SvxZoomItem);
50
51
namespace {
52
53
class ZoomPopup_Impl
54
{
55
public:
56
    ZoomPopup_Impl(weld::Window* pPopupParent, sal_uInt16 nZ, SvxZoomEnableFlags nValueSet);
57
58
    sal_uInt16 GetZoom(std::u16string_view ident) const;
59
60
    OUString popup_at_rect(const tools::Rectangle& rRect)
61
0
    {
62
0
        return m_xMenu->popup_at_rect(m_pPopupParent, rRect);
63
0
    }
64
65
private:
66
    weld::Window* m_pPopupParent;
67
    std::unique_ptr<weld::Builder> m_xBuilder;
68
    std::unique_ptr<weld::Menu> m_xMenu;
69
    sal_uInt16 nZoom;
70
};
71
72
}
73
74
ZoomPopup_Impl::ZoomPopup_Impl(weld::Window* pPopupParent, sal_uInt16 nZ, SvxZoomEnableFlags nValueSet)
75
0
    : m_pPopupParent(pPopupParent)
76
0
    , m_xBuilder(Application::CreateBuilder(m_pPopupParent, u"svx/ui/zoommenu.ui"_ustr))
77
0
    , m_xMenu(m_xBuilder->weld_menu(u"menu"_ustr))
78
0
    , nZoom(nZ)
79
0
{
80
0
    if ( !(SvxZoomEnableFlags::N50 & nValueSet) )
81
0
        m_xMenu->set_sensitive(u"50"_ustr, false);
82
0
    if ( !(SvxZoomEnableFlags::N100 & nValueSet) )
83
0
        m_xMenu->set_sensitive(u"100"_ustr, false);
84
0
    if ( !(SvxZoomEnableFlags::N150 & nValueSet) )
85
0
        m_xMenu->set_sensitive(u"150"_ustr, false);
86
0
    if ( !(SvxZoomEnableFlags::N200 & nValueSet) )
87
0
        m_xMenu->set_sensitive(u"200"_ustr, false);
88
0
    if ( !(SvxZoomEnableFlags::OPTIMAL & nValueSet) )
89
0
        m_xMenu->set_sensitive(u"optimal"_ustr, false);
90
0
    if ( !(SvxZoomEnableFlags::WHOLEPAGE & nValueSet) )
91
0
        m_xMenu->set_sensitive(u"page"_ustr, false);
92
0
    if ( !(SvxZoomEnableFlags::PAGEWIDTH & nValueSet) )
93
0
        m_xMenu->set_sensitive(u"width"_ustr, false);
94
0
}
95
96
sal_uInt16 ZoomPopup_Impl::GetZoom(std::u16string_view ident) const
97
0
{
98
0
    sal_uInt16 nRet = nZoom;
99
100
0
    if (ident == u"200")
101
0
        nRet = 200;
102
0
    else if (ident == u"150")
103
0
        nRet = 150;
104
0
    else if (ident == u"100")
105
0
        nRet = 100;
106
0
    else if (ident == u"75")
107
0
        nRet =  75;
108
0
    else if (ident == u"50")
109
0
        nRet =  50;
110
0
    else if (ident == u"optimal" || ident == u"width" || ident == u"page")
111
0
        nRet = 0;
112
113
0
    return nRet;
114
0
}
115
116
SvxZoomStatusBarControl::SvxZoomStatusBarControl( sal_uInt16 _nSlotId,
117
                                                  sal_uInt16 _nId,
118
                                                  StatusBar& rStb ) :
119
120
0
    SfxStatusBarControl( _nSlotId, _nId, rStb ),
121
0
    nZoom( 100 ),
122
0
    nValueSet( SvxZoomEnableFlags::ALL )
123
0
{
124
0
    GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_ZOOMTOOL_HINT));
125
0
    ImplUpdateItemText();
126
0
}
127
128
void SvxZoomStatusBarControl::StateChangedAtStatusBarControl( sal_uInt16, SfxItemState eState,
129
                                            const SfxPoolItem* pState )
130
0
{
131
0
    if( SfxItemState::DEFAULT != eState )
132
0
    {
133
0
        GetStatusBar().SetItemText( GetId(), u""_ustr );
134
0
        nValueSet = SvxZoomEnableFlags::NONE;
135
0
    }
136
0
    else if ( auto pItem = dynamic_cast< const SfxUInt16Item* >(pState) )
137
0
    {
138
0
        nZoom = pItem->GetValue();
139
0
        ImplUpdateItemText();
140
141
0
        if ( auto pZoomItem = dynamic_cast<const SvxZoomItem*>(pState) )
142
0
        {
143
0
            nValueSet = pZoomItem->GetValueSet();
144
0
        }
145
0
        else
146
0
        {
147
0
            SAL_INFO( "svx", "use SfxZoomItem for SID_ATTR_ZOOM" );
148
0
            nValueSet = SvxZoomEnableFlags::ALL;
149
0
        }
150
0
    }
151
0
}
152
153
void SvxZoomStatusBarControl::ImplUpdateItemText()
154
0
{
155
    // workaround - don't bother updating when we don't have a real zoom value
156
0
    if (nZoom)
157
0
    {
158
0
        OUString aStr(unicode::formatPercent(nZoom, Application::GetSettings().GetUILanguageTag()));
159
0
        GetStatusBar().SetItemText( GetId(), aStr );
160
0
    }
161
0
}
162
163
void SvxZoomStatusBarControl::Paint( const UserDrawEvent& )
164
0
{
165
0
}
166
167
void SvxZoomStatusBarControl::Command( const CommandEvent& rCEvt )
168
0
{
169
0
    if ( CommandEventId::ContextMenu == rCEvt.GetCommand() && bool(nValueSet) )
170
0
    {
171
0
        ::tools::Rectangle aRect(rCEvt.GetMousePosPixel(), Size(1, 1));
172
0
        weld::Window* pPopupParent = weld::GetPopupParent(GetStatusBar(), aRect);
173
0
        ZoomPopup_Impl aPop(pPopupParent, nZoom, nValueSet);
174
175
0
        OUString sIdent = aPop.popup_at_rect(aRect);
176
0
        if (!sIdent.isEmpty() && (nZoom != aPop.GetZoom(sIdent) || !nZoom))
177
0
        {
178
0
            nZoom = aPop.GetZoom(sIdent);
179
0
            ImplUpdateItemText();
180
0
            SvxZoomItem aZoom(SvxZoomType::PERCENT, nZoom, TypedWhichId<SvxZoomItem>(GetId()));
181
182
0
            if (sIdent == "optimal")
183
0
                aZoom.SetType(SvxZoomType::OPTIMAL);
184
0
            else if (sIdent == "width")
185
0
                aZoom.SetType(SvxZoomType::PAGEWIDTH);
186
0
            else if (sIdent == "page")
187
0
                aZoom.SetType(SvxZoomType::WHOLEPAGE);
188
189
0
            css::uno::Any a;
190
0
            aZoom.QueryValue( a );
191
0
            INetURLObject aObj( m_aCommandURL );
192
193
0
            css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
194
0
                aObj.GetURLPath(), a) };
195
0
            execute( aArgs );
196
0
        }
197
0
    }
198
0
    else
199
0
        SfxStatusBarControl::Command( rCEvt );
200
0
}
201
202
SFX_IMPL_STATUSBAR_CONTROL(SvxZoomPageStatusBarControl,SfxVoidItem);
203
204
SvxZoomPageStatusBarControl::SvxZoomPageStatusBarControl(sal_uInt16 _nSlotId,
205
    sal_uInt16 _nId, StatusBar& rStb)
206
0
    : SfxStatusBarControl(_nSlotId, _nId, rStb)
207
0
    , maImage(StockImage::Yes, RID_SVXBMP_ZOOM_PAGE)
208
0
{
209
0
    GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
210
0
}
211
212
void SAL_CALL SvxZoomPageStatusBarControl::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
213
0
{
214
    // Call inherited initialize
215
0
    StatusbarController::initialize(aArguments);
216
217
    // Get document type
218
0
    css::uno::Reference< css::frame::XModuleManager2 > xModuleManager = css::frame::ModuleManager::create( m_xContext );
219
0
    OUString aModuleIdentifier = xModuleManager->identify( css::uno::Reference<XInterface>( m_xFrame, css::uno::UnoReference_Query::UNO_QUERY ) );
220
221
    // Decide what to show in zoom bar
222
0
    if ( aModuleIdentifier == "com.sun.star.drawing.DrawingDocument" )
223
0
    {
224
0
        GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_PAGE));
225
0
    }
226
0
    else if ( aModuleIdentifier == "com.sun.star.presentation.PresentationDocument" )
227
0
    {
228
0
        GetStatusBar().SetQuickHelpText(GetId(), SvxResId(RID_SVXSTR_FIT_SLIDE));
229
0
    }
230
0
}
231
232
void SvxZoomPageStatusBarControl::Paint(const UserDrawEvent& rUsrEvt)
233
0
{
234
0
    vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
235
0
    tools::Rectangle aRect = rUsrEvt.GetRect();
236
0
    Point aPt = centerImage(aRect, maImage);
237
0
    pDev->DrawImage(aPt, maImage);
238
0
}
239
240
bool SvxZoomPageStatusBarControl::MouseButtonDown(const MouseEvent&)
241
0
{
242
0
    SvxZoomItem aZoom( SvxZoomType::WHOLEPAGE, 0, TypedWhichId<SvxZoomItem>(GetId()) );
243
244
0
    css::uno::Any a;
245
0
    aZoom.QueryValue( a );
246
0
    INetURLObject aObj( m_aCommandURL );
247
248
0
    css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
249
0
        aObj.GetURLPath(), a) };
250
0
    execute( aArgs );
251
252
0
    return true;
253
0
}
254
255
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */