Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/appl/module.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 <sfx2/module.hxx>
21
#include <sfx2/app.hxx>
22
#include <sfx2/msgpool.hxx>
23
#include <sfx2/tbxctrl.hxx>
24
#include <sfx2/stbitem.hxx>
25
#include <sfx2/childwin.hxx>
26
#include <sfx2/docfac.hxx>
27
#include <sfx2/objface.hxx>
28
#include <sfx2/viewfrm.hxx>
29
#include <sfx2/tabdlg.hxx>
30
#include <sfx2/sfxsids.hrc>
31
#include <svl/intitem.hxx>
32
#include <comphelper/diagnose_ex.hxx>
33
#include <tools/fldunit.hxx>
34
#include <unotools/resmgr.hxx>
35
#include <sal/log.hxx>
36
#include <comphelper/lok.hxx>
37
#include <unotools/localedatawrapper.hxx>
38
39
#define ShellClass_SfxModule
40
#include <sfxslots.hxx>
41
#include <optional>
42
43
class SfxModule_Impl
44
{
45
public:
46
47
    std::optional<SfxSlotPool>              pSlotPool;
48
    std::vector<SfxTbxCtrlFactory>          maTbxCtrlFactories;
49
    std::vector<SfxStbCtrlFactory>          maStbCtrlFactories;
50
    std::vector<SfxChildWinFactory>         maFactories;
51
    OString                     maResName;
52
53
                                SfxModule_Impl();
54
                                ~SfxModule_Impl();
55
};
56
57
SfxModule_Impl::SfxModule_Impl()
58
27
{
59
27
}
60
61
SfxModule_Impl::~SfxModule_Impl()
62
0
{
63
0
    pSlotPool.reset();
64
0
    maTbxCtrlFactories.clear();
65
0
    maStbCtrlFactories.clear();
66
0
}
67
68
SFX_IMPL_SUPERCLASS_INTERFACE(SfxModule, SfxShell)
69
70
SfxModule::SfxModule(const OString& rResName, std::initializer_list<SfxObjectFactory*> pFactoryList)
71
27
    : pImpl(nullptr)
72
27
{
73
27
    Construct_Impl(rResName);
74
27
    for (auto pFactory : pFactoryList)
75
51
    {
76
51
        if (pFactory)
77
45
            pFactory->SetModule_Impl( this );
78
51
    }
79
27
}
80
81
void SfxModule::Construct_Impl(const OString& rResName)
82
27
{
83
27
    SfxApplication *pApp = SfxApplication::GetOrCreate();
84
27
    pImpl = new SfxModule_Impl;
85
27
    pImpl->pSlotPool.emplace(&pApp->GetAppSlotPool_Impl());
86
27
    pImpl->maResName = rResName;
87
88
27
    SetPool( &pApp->GetPool() );
89
27
}
90
91
SfxModule::~SfxModule()
92
0
{
93
    //TODO how to silence useuniqueptr
94
0
    if (true)
95
0
    {
96
0
        delete pImpl;
97
0
    }
98
0
}
99
100
std::locale SfxModule::GetResLocale() const
101
13.7M
{
102
13.7M
    return Translate::Create(pImpl->maResName);
103
13.7M
}
104
105
SfxSlotPool* SfxModule::GetSlotPool() const
106
16.6k
{
107
16.6k
    return &*pImpl->pSlotPool;
108
16.6k
}
109
110
void SfxModule::RegisterChildWindow(const SfxChildWinFactory& rFact)
111
794
{
112
794
    assert(pImpl && "No real Module!");
113
114
15.1k
    for (size_t nFactory=0; nFactory<pImpl->maFactories.size(); ++nFactory)
115
14.3k
    {
116
14.3k
        if (rFact.nId == pImpl->maFactories[nFactory].nId)
117
0
        {
118
0
            pImpl->maFactories.erase( pImpl->maFactories.begin() + nFactory );
119
0
            SAL_WARN("sfx.appl", "ChildWindow registered multiple times!");
120
0
            return;
121
0
        }
122
14.3k
    }
123
124
794
    pImpl->maFactories.push_back( rFact );
125
794
}
126
127
void SfxModule::RegisterToolBoxControl( const SfxTbxCtrlFactory& rFact )
128
518
{
129
#ifdef DBG_UTIL
130
    for ( size_t n=0; n<pImpl->maTbxCtrlFactories.size(); n++ )
131
    {
132
        SfxTbxCtrlFactory *pF = &pImpl->maTbxCtrlFactories[n];
133
        if ( pF->nTypeId == rFact.nTypeId &&
134
            (pF->nSlotId == rFact.nSlotId || pF->nSlotId == 0) )
135
        {
136
            SAL_INFO("sfx.appl", "TbxController-Registering is not clearly defined!");
137
        }
138
    }
139
#endif
140
141
518
    pImpl->maTbxCtrlFactories.push_back( rFact );
142
518
}
143
144
145
void SfxModule::RegisterStatusBarControl( const SfxStbCtrlFactory& rFact )
146
249
{
147
#ifdef DBG_UTIL
148
    for ( size_t n=0; n<pImpl->maStbCtrlFactories.size(); n++ )
149
    {
150
        SfxStbCtrlFactory *pF = &pImpl->maStbCtrlFactories[n];
151
        if ( pF->nTypeId == rFact.nTypeId &&
152
            (pF->nSlotId == rFact.nSlotId || pF->nSlotId == 0) )
153
        {
154
            SAL_INFO("sfx.appl", "TbxController-Registering is not clearly defined!");
155
        }
156
    }
157
#endif
158
159
249
    pImpl->maStbCtrlFactories.push_back( rFact );
160
249
}
161
162
163
SfxTbxCtrlFactory* SfxModule::GetTbxCtrlFactory(const std::type_info& rSlotType, sal_uInt16 nSlotID) const
164
0
{
165
    // search for a factory with the given slot id
166
0
    for (auto& rFactory : pImpl->maTbxCtrlFactories)
167
0
        if( rFactory.nTypeId == rSlotType && rFactory.nSlotId == nSlotID )
168
0
            return &rFactory;
169
170
    // if no factory exists for the given slot id, see if we
171
    // have a generic factory with the correct slot type and slot id == 0
172
0
    for (auto& rFactory : pImpl->maTbxCtrlFactories)
173
0
        if( rFactory.nTypeId == rSlotType && rFactory.nSlotId == 0 )
174
0
            return &rFactory;
175
176
0
    return nullptr;
177
0
}
178
179
180
SfxStbCtrlFactory* SfxModule::GetStbCtrlFactory(const std::type_info& rSlotType, sal_uInt16 nSlotID) const
181
0
{
182
0
    for (auto& rFactory : pImpl->maStbCtrlFactories)
183
0
        if ( rFactory.nTypeId == rSlotType &&
184
0
             ( rFactory.nSlotId == 0 || rFactory.nSlotId == nSlotID ) )
185
0
            return &rFactory;
186
0
    return nullptr;
187
0
}
188
189
SfxChildWinFactory* SfxModule::GetChildWinFactoryById(sal_uInt16 nId) const
190
72.3k
{
191
72.3k
    for (auto& rFactory : pImpl->maFactories)
192
787k
        if (rFactory.nId == nId)
193
72.3k
            return &rFactory;
194
0
    return nullptr;
195
72.3k
}
196
197
std::unique_ptr<SfxTabPage> SfxModule::CreateTabPage(sal_uInt16, weld::Container*, weld::DialogController*, const SfxItemSet&)
198
0
{
199
0
    return nullptr;
200
0
}
201
202
void SfxModule::Invalidate( sal_uInt16 nId )
203
0
{
204
0
    for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
205
0
        if ( pFrame->GetObjectShell()->GetModule() == this )
206
0
            Invalidate_Impl( pFrame->GetBindings(), nId );
207
0
}
208
209
SfxModule* SfxModule::GetActiveModule( SfxViewFrame* pFrame )
210
634k
{
211
634k
    if ( !pFrame )
212
498k
        pFrame = SfxViewFrame::Current();
213
634k
    SfxObjectShell* pSh = nullptr;
214
634k
    if( pFrame )
215
136k
        pSh = pFrame->GetObjectShell();
216
634k
    return pSh ? pSh->GetModule() : nullptr;
217
634k
}
218
219
FieldUnit SfxModule::GetModuleFieldUnit( css::uno::Reference< css::frame::XFrame > const & i_frame )
220
0
{
221
0
    ENSURE_OR_RETURN( i_frame.is(), "SfxModule::GetModuleFieldUnit: invalid frame!", FieldUnit::MM_100TH );
222
223
    // find SfxViewFrame for the given XFrame
224
0
    SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
225
0
    while ( pViewFrame != nullptr )
226
0
    {
227
0
        if ( pViewFrame->GetFrame().GetFrameInterface() == i_frame )
228
0
            break;
229
0
        pViewFrame = SfxViewFrame::GetNext( *pViewFrame );
230
0
    }
231
0
    ENSURE_OR_RETURN(
232
0
        pViewFrame != nullptr,
233
0
        "SfxModule::GetModuleFieldUnit: unable to find an SfxViewFrame for the given XFrame",
234
0
        FieldUnit::MM_100TH);
235
236
    // find the module
237
0
    SfxModule const * pModule = GetActiveModule( pViewFrame );
238
0
    ENSURE_OR_RETURN(pModule != nullptr,
239
0
                     "SfxModule::GetModuleFieldUnit: no SfxModule for the given frame!",
240
0
                     FieldUnit::MM_100TH);
241
0
    return pModule->GetFieldUnit();
242
0
}
243
244
FieldUnit SfxModule::GetCurrentFieldUnit()
245
0
{
246
0
    FieldUnit eUnit = FieldUnit::INCH;
247
0
    SfxModule* pModule = GetActiveModule();
248
0
    if ( pModule )
249
0
        return pModule->GetFieldUnit();
250
0
    else
251
0
        SAL_WARN( "sfx.appl", "GetModuleFieldUnit(): no module found" );
252
0
    return eUnit;
253
0
}
254
255
FieldUnit SfxModule::GetFieldUnit() const
256
0
{
257
0
    if (comphelper::LibreOfficeKit::isActive())
258
0
    {
259
0
        MeasurementSystem eSystem
260
0
            = LocaleDataWrapper(comphelper::LibreOfficeKit::getLocale()).getMeasurementSystemEnum();
261
0
        return MeasurementSystem::Metric == eSystem ? FieldUnit::CM : FieldUnit::INCH;
262
0
    }
263
0
    FieldUnit eUnit = FieldUnit::INCH;
264
0
    const SfxUInt16Item* pItem = GetItem( SID_ATTR_METRIC );
265
0
    if ( pItem )
266
0
        eUnit = static_cast<FieldUnit>(pItem->GetValue());
267
0
    return eUnit;
268
0
}
269
270
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */