Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/stbctrls/selctrl.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 <string_view>
23
24
#include <comphelper/propertyvalue.hxx>
25
#include <vcl/event.hxx>
26
#include <vcl/status.hxx>
27
#include <vcl/svapp.hxx>
28
#include <vcl/weld/Builder.hxx>
29
#include <vcl/weld/Menu.hxx>
30
#include <vcl/weld/Window.hxx>
31
#include <vcl/weld/weldutils.hxx>
32
#include <svl/intitem.hxx>
33
#include <tools/debug.hxx>
34
#include <tools/urlobj.hxx>
35
36
#include <svx/selctrl.hxx>
37
38
#include <bitmaps.hlst>
39
40
#include <com/sun/star/beans/PropertyValue.hpp>
41
#include <com/sun/star/frame/XFrame.hpp>
42
#include <com/sun/star/lang/XServiceInfo.hpp>
43
44
#include <svx/strings.hrc>
45
#include <svx/dialmgr.hxx>
46
47
SFX_IMPL_STATUSBAR_CONTROL(SvxSelectionModeControl, SfxUInt16Item);
48
49
namespace {
50
51
/// Popup menu to select the selection type
52
class SelectionTypePopup
53
{
54
    weld::Window* m_pPopupParent;
55
    std::unique_ptr<weld::Builder> m_xBuilder;
56
    std::unique_ptr<weld::Menu> m_xMenu;
57
    static OUString state_to_id(sal_uInt16 nState);
58
public:
59
    SelectionTypePopup(weld::Window* pPopupParent, sal_uInt16 nCurrent);
60
0
    OUString GetItemTextForState(sal_uInt16 nState) { return m_xMenu->get_label(state_to_id(nState)); }
61
    OUString popup_at_rect(const tools::Rectangle& rRect)
62
0
    {
63
0
        return m_xMenu->popup_at_rect(m_pPopupParent, rRect);
64
0
    }
65
    void HideSelectionType(const OUString& rIdent)
66
0
    {
67
0
        m_xMenu->remove(rIdent);
68
0
    }
69
    static sal_uInt16 id_to_state(std::u16string_view ident);
70
};
71
72
}
73
74
sal_uInt16 SelectionTypePopup::id_to_state(std::u16string_view ident)
75
0
{
76
0
    if (ident == u"block")
77
0
        return 3;
78
0
    else if (ident == u"adding")
79
0
        return 2;
80
0
    else if (ident == u"extending")
81
0
        return 1;
82
0
    else // fall through
83
0
        return 0;
84
0
}
85
86
OUString SelectionTypePopup::state_to_id(sal_uInt16 nState)
87
0
{
88
0
    switch (nState)
89
0
    {
90
0
        default: // fall through
91
0
        case 0: return u"standard"_ustr;
92
0
        case 1: return u"extending"_ustr;
93
0
        case 2: return u"adding"_ustr;
94
0
        case 3: return u"block"_ustr;
95
0
    }
96
0
}
97
98
SelectionTypePopup::SelectionTypePopup(weld::Window* pPopupParent, sal_uInt16 nCurrent)
99
0
    : m_pPopupParent(pPopupParent)
100
0
    , m_xBuilder(Application::CreateBuilder(m_pPopupParent, u"svx/ui/selectionmenu.ui"_ustr))
101
0
    , m_xMenu(m_xBuilder->weld_menu(u"menu"_ustr))
102
0
{
103
0
    m_xMenu->set_active(state_to_id(nCurrent), true);
104
0
}
105
106
SvxSelectionModeControl::SvxSelectionModeControl( sal_uInt16 _nSlotId,
107
                                                  sal_uInt16 _nId,
108
                                                  StatusBar& rStb ) :
109
0
    SfxStatusBarControl( _nSlotId, _nId, rStb ),
110
0
    mnState( 0 ),
111
0
    maImages{Image(StockImage::Yes, RID_SVXBMP_STANDARD_SELECTION),
112
0
             Image(StockImage::Yes, RID_SVXBMP_EXTENDING_SELECTION),
113
0
             Image(StockImage::Yes, RID_SVXBMP_ADDING_SELECTION),
114
0
             Image(StockImage::Yes, RID_SVXBMP_BLOCK_SELECTION)},
115
0
    mbFeatureEnabled(false)
116
0
{
117
0
    GetStatusBar().SetQuickHelpText(GetId(), u""_ustr);
118
0
}
119
120
void SvxSelectionModeControl::StateChangedAtStatusBarControl( sal_uInt16, SfxItemState eState,
121
                                            const SfxPoolItem* pState )
122
0
{
123
0
    mbFeatureEnabled = SfxItemState::DEFAULT == eState;
124
0
    if (mbFeatureEnabled)
125
0
    {
126
0
        DBG_ASSERT( dynamic_cast< const SfxUInt16Item* >(pState) !=  nullptr, "invalid item type" );
127
0
        const SfxUInt16Item* pItem = static_cast<const SfxUInt16Item*>(pState);
128
0
        mnState = pItem->GetValue();
129
0
        SelectionTypePopup aPop(GetStatusBar().GetFrameWeld(), mnState);
130
0
        GetStatusBar().SetQuickHelpText(GetId(),
131
0
                                        SvxResId(RID_SVXSTR_SELECTIONMODE_HELPTEXT).
132
0
                                        replaceFirst("%1", aPop.GetItemTextForState(mnState)));
133
0
        GetStatusBar().Invalidate();
134
0
    }
135
0
}
136
137
bool SvxSelectionModeControl::MouseButtonDown( const MouseEvent& rEvt )
138
0
{
139
0
    if (!mbFeatureEnabled)
140
0
        return true;
141
142
0
    ::tools::Rectangle aRect(rEvt.GetPosPixel(), Size(1, 1));
143
0
    weld::Window* pPopupParent = weld::GetPopupParent(GetStatusBar(), aRect);
144
0
    SelectionTypePopup aPop(pPopupParent, mnState);
145
146
    // Check if Calc is opened; if true, hide block selection mode tdf#122280
147
0
    const css::uno::Reference < css::frame::XModel > xModel = m_xFrame->getController()->getModel();
148
0
    css::uno::Reference< css::lang::XServiceInfo > xServices( xModel, css::uno::UNO_QUERY );
149
0
    if ( xServices.is() )
150
0
    {
151
0
        bool bSpecModeCalc = xServices->supportsService(u"com.sun.star.sheet.SpreadsheetDocument"_ustr);
152
0
        if (bSpecModeCalc)
153
0
            aPop.HideSelectionType(u"block"_ustr);
154
0
    }
155
156
0
    OUString sIdent = aPop.popup_at_rect(aRect);
157
0
    if (!sIdent.isEmpty())
158
0
    {
159
0
        sal_uInt16 nNewState = SelectionTypePopup::id_to_state(sIdent);
160
0
        if ( nNewState != mnState )
161
0
        {
162
0
            mnState = nNewState;
163
164
0
            css::uno::Any a;
165
0
            SfxUInt16Item aState( GetSlotId(), mnState );
166
0
            aState.QueryValue( a );
167
0
            INetURLObject aObj( m_aCommandURL );
168
169
0
            css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
170
0
                aObj.GetURLPath(), a) };
171
0
            execute( aArgs );
172
0
        }
173
0
    }
174
175
0
    return true;
176
0
}
177
178
void SvxSelectionModeControl::Click()
179
0
{
180
0
}
181
182
void SvxSelectionModeControl::Paint( const UserDrawEvent& rUsrEvt )
183
0
{
184
0
    const tools::Rectangle aControlRect = getControlRect();
185
0
    vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
186
0
    tools::Rectangle aRect = rUsrEvt.GetRect();
187
188
0
    Size aImgSize( maImages[mnState].GetSizePixel() );
189
190
0
    Point aPos( aRect.Left() + ( aControlRect.GetWidth() - aImgSize.Width() ) / 2,
191
0
            aRect.Top() + ( aControlRect.GetHeight() - aImgSize.Height() ) / 2 );
192
193
0
    if (mbFeatureEnabled)
194
0
        pDev->DrawImage(aPos, maImages[mnState]);
195
0
    else
196
0
        pDev->DrawImage(aPos, Image());
197
0
}
198
199
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */