Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/stbctrls/pszctrl.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 <vcl/commandevent.hxx>
24
#include <vcl/event.hxx>
25
#include <vcl/fieldvalues.hxx>
26
#include <vcl/status.hxx>
27
#include <vcl/image.hxx>
28
#include <vcl/settings.hxx>
29
#include <vcl/svapp.hxx>
30
#include <vcl/weld.hxx>
31
#include <vcl/weldutils.hxx>
32
#include <svl/stritem.hxx>
33
#include <svl/ptitem.hxx>
34
#include <sfx2/module.hxx>
35
#include <svx/dialmgr.hxx>
36
#include <svx/statusitem.hxx>
37
#include <svx/strings.hrc>
38
#include <svl/intitem.hxx>
39
#include <sal/log.hxx>
40
41
#include <svx/pszctrl.hxx>
42
43
0
#define PAINT_OFFSET    5
44
45
#include <editeng/sizeitem.hxx>
46
#include "stbctrls.h"
47
48
#include <svx/svxids.hrc>
49
#include <bitmaps.hlst>
50
#include <unotools/localedatawrapper.hxx>
51
52
#include <com/sun/star/beans/PropertyValue.hpp>
53
54
/*  [Description]
55
56
    Function used to create a text representation of
57
    a metric value
58
59
    nVal is the metric value in the unit eUnit.
60
61
    [cross reference]
62
63
    <SvxPosSizeStatusBarControl::Paint(const UserDrawEvent&)>
64
*/
65
66
OUString SvxPosSizeStatusBarControl::GetMetricStr_Impl( tools::Long nVal ) const
67
0
{
68
    // deliver and set the Metric of the application
69
0
    FieldUnit eOutUnit = SfxModule::GetModuleFieldUnit( getFrameInterface() );
70
71
0
    OUString sMetric;
72
0
    const sal_Unicode cSep = Application::GetSettings().GetLocaleDataWrapper().getNumDecimalSep()[0];
73
0
    sal_Int64 nConvVal = vcl::ConvertValue( nVal * 100, 0, 0, FieldUnit::MM_100TH, eOutUnit );
74
75
0
    if ( nConvVal < 0 && ( nConvVal / 100 == 0 ) )
76
0
        sMetric += "-";
77
0
    sMetric += OUString::number(nConvVal / 100);
78
79
0
    if( FieldUnit::NONE != eOutUnit )
80
0
    {
81
0
        sMetric += OUStringChar(cSep);
82
0
        sal_Int64 nFract = nConvVal % 100;
83
84
0
        if ( nFract < 0 )
85
0
            nFract *= -1;
86
0
        if ( nFract < 10 )
87
0
            sMetric += "0";
88
0
        sMetric += OUString::number(nFract);
89
0
    }
90
91
0
    return sMetric;
92
0
}
93
94
95
SFX_IMPL_STATUSBAR_CONTROL(SvxPosSizeStatusBarControl, SvxSizeItem);
96
97
namespace {
98
99
class FunctionPopup_Impl
100
{
101
    std::unique_ptr<weld::Builder> m_xBuilder;
102
    std::unique_ptr<weld::Menu> m_xMenu;
103
    sal_uInt32        m_nSelected;
104
    static sal_uInt16 id_to_function(std::u16string_view rIdent);
105
    static OUString function_to_id(sal_uInt16 nFunc);
106
public:
107
    explicit FunctionPopup_Impl(sal_uInt32 nCheckEncoded);
108
    OUString Execute(weld::Window* pParent, const tools::Rectangle& rRect)
109
0
    {
110
0
        return m_xMenu->popup_at_rect(pParent, rRect);
111
0
    }
112
    sal_uInt32 GetSelected(std::u16string_view curident) const;
113
};
114
115
}
116
117
sal_uInt16 FunctionPopup_Impl::id_to_function(std::u16string_view rIdent)
118
0
{
119
0
    if (rIdent == u"avg")
120
0
        return PSZ_FUNC_AVG;
121
0
    else if (rIdent == u"counta")
122
0
        return PSZ_FUNC_COUNT2;
123
0
    else if (rIdent == u"count")
124
0
        return PSZ_FUNC_COUNT;
125
0
    else if (rIdent == u"max")
126
0
        return PSZ_FUNC_MAX;
127
0
    else if (rIdent == u"min")
128
0
        return PSZ_FUNC_MIN;
129
0
    else if (rIdent == u"sum")
130
0
        return PSZ_FUNC_SUM;
131
0
    else if (rIdent == u"selection")
132
0
        return PSZ_FUNC_SELECTION_COUNT;
133
0
    else if (rIdent == u"none")
134
0
        return PSZ_FUNC_NONE;
135
0
    return 0;
136
0
}
137
138
OUString FunctionPopup_Impl::function_to_id(sal_uInt16 nFunc)
139
0
{
140
0
    switch (nFunc)
141
0
    {
142
0
        case PSZ_FUNC_AVG:
143
0
            return u"avg"_ustr;
144
0
        case PSZ_FUNC_COUNT2:
145
0
            return u"counta"_ustr;
146
0
        case PSZ_FUNC_COUNT:
147
0
            return u"count"_ustr;
148
0
        case PSZ_FUNC_MAX:
149
0
            return u"max"_ustr;
150
0
        case PSZ_FUNC_MIN:
151
0
            return u"min"_ustr;
152
0
        case PSZ_FUNC_SUM:
153
0
            return u"sum"_ustr;
154
0
        case PSZ_FUNC_SELECTION_COUNT:
155
0
            return u"selection"_ustr;
156
0
        case PSZ_FUNC_NONE:
157
0
            return u"none"_ustr;
158
0
    }
159
0
    return {};
160
0
}
161
162
FunctionPopup_Impl::FunctionPopup_Impl(sal_uInt32 nCheckEncoded)
163
0
    : m_xBuilder(Application::CreateBuilder(nullptr, u"svx/ui/functionmenu.ui"_ustr))
164
0
    , m_xMenu(m_xBuilder->weld_menu(u"menu"_ustr))
165
0
    , m_nSelected(nCheckEncoded)
166
0
{
167
0
    for ( sal_uInt16 nCheck = 1; nCheck < 32; ++nCheck )
168
0
        if ( nCheckEncoded & (1u << nCheck) )
169
0
            m_xMenu->set_active(function_to_id(nCheck), true);
170
0
}
171
172
sal_uInt32 FunctionPopup_Impl::GetSelected(std::u16string_view curident) const
173
0
{
174
0
    sal_uInt32 nSelected = m_nSelected;
175
0
    sal_uInt16 nCurItemId = id_to_function(curident);
176
0
    if ( nCurItemId == PSZ_FUNC_NONE )
177
0
        nSelected = ( 1 << PSZ_FUNC_NONE );
178
0
    else
179
0
    {
180
0
        nSelected &= (~( 1u << PSZ_FUNC_NONE )); // Clear the "None" bit
181
0
        nSelected ^= ( 1u << nCurItemId ); // Toggle the bit corresponding to nCurItemId
182
0
        if ( !nSelected )
183
0
            nSelected = ( 1u << PSZ_FUNC_NONE );
184
0
    }
185
0
    return nSelected;
186
0
}
187
188
struct SvxPosSizeStatusBarControl_Impl
189
190
/*  [Description]
191
192
    This implementation-structure of the class SvxPosSizeStatusBarControl
193
    is done for the un-linking of the changes of the exported interface such as
194
    the toning down of symbols that are visible externally.
195
196
    One instance exists for each SvxPosSizeStatusBarControl-instance
197
    during its life time
198
*/
199
200
{
201
    Point     aPos;       // valid when a position is shown
202
    Size      aSize;      // valid when a size is shown
203
    OUString  aStr;       // valid when a text is shown
204
    bool      bPos;       // show position ?
205
    bool      bSize;      // set size ?
206
    bool      bTable;     // set table index ?
207
    bool      bHasMenu;   // set StarCalc popup menu ?
208
    sal_uInt32  nFunctionSet;  // the selected StarCalc functions encoded in 32 bits
209
    Image     aPosImage;  // Image to show the position
210
    Image     aSizeImage; // Image to show the size
211
};
212
213
/*  [Description]
214
215
    Ctor():
216
    Create an instance of the implementation class,
217
    load the images for the position and size
218
*/
219
220
#define STR_POSITION ".uno:Position"
221
#define STR_TABLECELL ".uno:StateTableCell"
222
#define STR_FUNC ".uno:StatusBarFunc"
223
224
SvxPosSizeStatusBarControl::SvxPosSizeStatusBarControl( sal_uInt16 _nSlotId,
225
                                                        sal_uInt16 _nId,
226
                                                        StatusBar& rStb ) :
227
0
    SfxStatusBarControl( _nSlotId, _nId, rStb ),
228
0
    pImpl( new SvxPosSizeStatusBarControl_Impl )
229
0
{
230
0
    pImpl->bPos = false;
231
0
    pImpl->bSize = false;
232
0
    pImpl->bTable = false;
233
0
    pImpl->bHasMenu = false;
234
0
    pImpl->nFunctionSet = 0;
235
0
    pImpl->aPosImage = Image(StockImage::Yes, RID_SVXBMP_POSITION);
236
0
    pImpl->aSizeImage = Image(StockImage::Yes, RID_SVXBMP_SIZE);
237
238
0
    addStatusListener( u"" STR_POSITION ""_ustr);         // SID_ATTR_POSITION
239
0
    addStatusListener( u"" STR_TABLECELL ""_ustr);   // SID_TABLE_CELL
240
0
    addStatusListener( u"" STR_FUNC ""_ustr);    // SID_PSZ_FUNCTION
241
0
    ImplUpdateItemText();
242
0
}
243
244
/*  [Description]
245
246
    Dtor():
247
    remove the pointer to the implementation class, so that the timer is stopped
248
249
*/
250
251
SvxPosSizeStatusBarControl::~SvxPosSizeStatusBarControl()
252
0
{
253
0
}
254
255
256
/*  [Description]
257
258
    SID_PSZ_FUNCTION activates the popup menu for Calc:
259
260
    Status overview
261
    Depending on the type of the item, a special setting is enabled, the others disabled.
262
263
                NULL/Void   SfxPointItem    SvxSizeItem     SfxStringItem
264
    ------------------------------------------------------------------------
265
    Position    sal_False                                       FALSE
266
    Size        FALSE                       TRUE            FALSE
267
    Text        sal_False                       sal_False           TRUE
268
269
*/
270
271
void SvxPosSizeStatusBarControl::StateChangedAtStatusBarControl( sal_uInt16 nSID, SfxItemState eState,
272
                                               const SfxPoolItem* pState )
273
0
{
274
    // Because the combi-controller, always sets the current Id as HelpId
275
    // first clean the cached HelpText
276
0
    GetStatusBar().SetHelpText( GetId(), u""_ustr );
277
278
0
    switch ( nSID )
279
0
    {
280
0
        case SID_ATTR_POSITION : GetStatusBar().SetHelpId( GetId(), u"" STR_POSITION ""_ustr ); break;
281
0
        case SID_TABLE_CELL: GetStatusBar().SetHelpId( GetId(), u"" STR_TABLECELL ""_ustr ); break;
282
0
        case SID_PSZ_FUNCTION: GetStatusBar().SetHelpId( GetId(), u"" STR_FUNC ""_ustr ); break;
283
0
        default: break;
284
0
    }
285
286
0
    if ( nSID == SID_PSZ_FUNCTION )
287
0
    {
288
0
        if ( eState == SfxItemState::DEFAULT )
289
0
        {
290
0
            pImpl->bHasMenu = true;
291
0
            if ( auto pUInt32Item = dynamic_cast< const SfxUInt32Item* >(pState) )
292
0
                pImpl->nFunctionSet = pUInt32Item->GetValue();
293
0
        }
294
0
        else
295
0
            pImpl->bHasMenu = false;
296
0
    }
297
0
    else if ( SfxItemState::DEFAULT != eState )
298
0
    {
299
        // don't switch to empty display before an empty state was
300
        // notified for all display types
301
302
0
        if ( nSID == SID_TABLE_CELL )
303
0
            pImpl->bTable = false;
304
0
        else if ( nSID == SID_ATTR_POSITION )
305
0
            pImpl->bPos = false;
306
0
        else if ( nSID == GetSlotId() )     // controller is registered for SID_ATTR_SIZE
307
0
            pImpl->bSize = false;
308
0
        else
309
0
        {
310
0
            SAL_WARN( "svx.stbcrtls","unknown slot id");
311
0
        }
312
0
    }
313
0
    else if ( auto pPointItem = dynamic_cast<const SfxPointItem*>( pState) )
314
0
    {
315
        // show position
316
0
        pImpl->aPos = pPointItem->GetValue();
317
0
        pImpl->bPos = true;
318
0
        pImpl->bTable = false;
319
0
    }
320
0
    else if ( auto pSizeItem = dynamic_cast<const SvxSizeItem*>( pState) )
321
0
    {
322
        // show size
323
0
        pImpl->aSize = pSizeItem->GetSize();
324
0
        pImpl->bSize = true;
325
0
        pImpl->bTable = false;
326
0
    }
327
0
    else if ( auto pStatusItem = dynamic_cast<const SvxStatusItem*>( pState) )
328
0
    {
329
        // show string (table cell or different)
330
0
        pImpl->aStr = pStatusItem->GetValue();
331
0
        pImpl->bTable = true;
332
0
        pImpl->bPos = false;
333
0
        pImpl->bSize = false;
334
0
        if (!pImpl->aStr.isEmpty())
335
0
        {
336
0
            OUString sTip;
337
0
            switch (pStatusItem->GetCategory())
338
0
            {
339
0
                case StatusCategory::TableCell:
340
0
                    sTip = SvxResId(RID_SVXSTR_TABLECELL_HINT);
341
0
                    break;
342
0
                case StatusCategory::Section:
343
0
                    sTip = SvxResId(RID_SVXSTR_SECTION_HINT);
344
0
                    break;
345
0
                case StatusCategory::TableOfContents:
346
0
                    sTip = SvxResId(RID_SVXSTR_TOC_HINT);
347
0
                    break;
348
0
                case StatusCategory::Numbering:
349
0
                    sTip = SvxResId(RID_SVXSTR_NUMBERING_HINT);
350
0
                    break;
351
0
                case StatusCategory::ListStyle:
352
0
                    sTip = SvxResId(RID_SVXSTR_LIST_STYLE_HINT);
353
0
                    break;
354
0
                case StatusCategory::Formula:
355
0
                    sTip = SvxResId(RID_SVXSTR_FORMULA_HINT);
356
0
                    break;
357
0
                case StatusCategory::RowColumn:
358
0
                    sTip = SvxResId(RID_SVXSTR_ROW_COLUMN_HINT);
359
0
                    break;
360
0
                case StatusCategory::NONE:
361
0
                    break;
362
0
            }
363
0
            GetStatusBar().SetQuickHelpText(GetId(), sTip);
364
0
        }
365
0
    }
366
0
    else if ( auto pStringItem = dynamic_cast<const SfxStringItem*>( pState) )
367
0
    {
368
0
        SAL_WARN( "svx.stbcrtls", "this should be a SvxStatusItem not a SfxStringItem" );
369
        // show string (table cell or different)
370
0
        pImpl->aStr = pStringItem->GetValue();
371
0
        pImpl->bTable = true;
372
0
        pImpl->bPos = false;
373
0
        pImpl->bSize = false;
374
0
    }
375
0
    else
376
0
    {
377
0
        SAL_WARN( "svx.stbcrtls", "invalid item type" );
378
0
        pImpl->bPos = false;
379
0
        pImpl->bSize = false;
380
0
        pImpl->bTable = false;
381
0
    }
382
383
0
    GetStatusBar().SetItemData( GetId(), nullptr );
384
385
0
    ImplUpdateItemText();
386
0
}
387
388
389
/*  [Description]
390
391
    execute popup menu, when the status enables this
392
*/
393
394
void SvxPosSizeStatusBarControl::Command( const CommandEvent& rCEvt )
395
0
{
396
0
    if ( rCEvt.GetCommand() == CommandEventId::ContextMenu && pImpl->bHasMenu )
397
0
    {
398
0
        sal_uInt32 nSelect = pImpl->nFunctionSet;
399
0
        if (!nSelect)
400
0
            nSelect = ( 1 << PSZ_FUNC_NONE );
401
0
        tools::Rectangle aRect(rCEvt.GetMousePosPixel(), Size(1,1));
402
0
        weld::Window* pParent = weld::GetPopupParent(GetStatusBar(), aRect);
403
0
        FunctionPopup_Impl aMenu(nSelect);
404
0
        OUString sIdent = aMenu.Execute(pParent, aRect);
405
0
        if (!sIdent.isEmpty())
406
0
        {
407
0
            nSelect = aMenu.GetSelected(sIdent);
408
0
            if (nSelect)
409
0
            {
410
0
                if (nSelect == (1 << PSZ_FUNC_NONE))
411
0
                    nSelect = 0;
412
413
0
                css::uno::Any a;
414
0
                SfxUInt32Item aItem( SID_PSZ_FUNCTION, nSelect );
415
0
                aItem.QueryValue( a );
416
0
                css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
417
0
                    u"StatusBarFunc"_ustr, a) };
418
0
                execute( u".uno:StatusBarFunc"_ustr, aArgs );
419
0
            }
420
0
        }
421
0
    }
422
0
    else
423
0
        SfxStatusBarControl::Command( rCEvt );
424
0
}
425
426
427
/*  [Description]
428
429
    Depending on the type to be shown, the value us shown. First the
430
    rectangle is repainted (removed).
431
*/
432
433
void SvxPosSizeStatusBarControl::Paint( const UserDrawEvent& rUsrEvt )
434
0
{
435
0
    vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
436
437
0
    const tools::Rectangle& rRect = rUsrEvt.GetRect();
438
0
    StatusBar& rBar = GetStatusBar();
439
0
    Point aItemPos = rBar.GetItemTextPos( GetId() );
440
0
    auto popIt = pDev->ScopedPush(vcl::PushFlags::LINECOLOR | vcl::PushFlags::FILLCOLOR);
441
0
    pDev->SetLineColor();
442
0
    pDev->SetFillColor( pDev->GetBackground().GetColor() );
443
444
0
    if ( pImpl->bPos || pImpl->bSize )
445
0
    {
446
        // count the position for showing the size
447
0
        tools::Long nSizePosX =
448
0
            rRect.Left() + rRect.GetWidth() / 2 + PAINT_OFFSET;
449
        // draw position
450
0
        Point aPnt = rRect.TopLeft();
451
0
        aPnt.AdjustX(PAINT_OFFSET );
452
        // vertically centered
453
0
        const tools::Long nSizePosY =
454
0
            (rRect.GetHeight() - pImpl->aPosImage.GetSizePixel().Height()) / 2;
455
0
        aPnt.AdjustY( nSizePosY );
456
457
0
        pDev->DrawImage( aPnt, pImpl->aPosImage );
458
0
        aPnt.AdjustX(pImpl->aPosImage.GetSizePixel().Width() );
459
0
        aPnt.AdjustX(PAINT_OFFSET );
460
0
        OUString aStr = GetMetricStr_Impl( pImpl->aPos.X()) + " / " +
461
0
            GetMetricStr_Impl( pImpl->aPos.Y());
462
0
        tools::Rectangle aRect(aPnt, Point(nSizePosX, rRect.Bottom()));
463
0
        pDev->DrawRect(aRect);
464
0
        vcl::Region aOrigRegion(pDev->GetClipRegion());
465
0
        pDev->SetClipRegion(vcl::Region(aRect));
466
0
        pDev->DrawText(aPnt, aStr);
467
0
        pDev->SetClipRegion(aOrigRegion);
468
469
        // draw the size, when available
470
0
        aPnt.setX( nSizePosX );
471
472
0
        if ( pImpl->bSize )
473
0
        {
474
0
            pDev->DrawImage( aPnt, pImpl->aSizeImage );
475
0
            aPnt.AdjustX(pImpl->aSizeImage.GetSizePixel().Width() );
476
0
            Point aDrwPnt = aPnt;
477
0
            aPnt.AdjustX(PAINT_OFFSET );
478
0
            aStr = GetMetricStr_Impl( pImpl->aSize.Width() ) + " x " +
479
0
                GetMetricStr_Impl( pImpl->aSize.Height() );
480
0
            aRect = tools::Rectangle(aDrwPnt, rRect.BottomRight());
481
0
            pDev->DrawRect(aRect);
482
0
            aOrigRegion = pDev->GetClipRegion();
483
0
            pDev->SetClipRegion(vcl::Region(aRect));
484
0
            pDev->DrawText(aPnt, aStr);
485
0
            pDev->SetClipRegion(aOrigRegion);
486
0
        }
487
0
        else
488
0
            pDev->DrawRect( tools::Rectangle( aPnt, rRect.BottomRight() ) );
489
0
    }
490
0
    else if ( pImpl->bTable )
491
0
    {
492
0
        pDev->DrawRect( rRect );
493
0
        pDev->DrawText( Point(
494
0
            rRect.Left() + rRect.GetWidth() / 2 - pDev->GetTextWidth( pImpl->aStr ) / 2,
495
0
            aItemPos.Y() ), pImpl->aStr );
496
0
    }
497
0
    else
498
0
    {
499
        // Empty display if neither size nor table position are available.
500
        // Date/Time are no longer used (#65302#).
501
0
        pDev->DrawRect( rRect );
502
0
    }
503
0
}
504
505
void SvxPosSizeStatusBarControl::ImplUpdateItemText()
506
0
{
507
    //  set only strings as text at the statusBar, so that the Help-Tips
508
    //  can work with the text, when it is too long for the statusBar
509
0
    OUString aText;
510
0
    int nCharsWidth = -1;
511
0
    if ( pImpl->bPos || pImpl->bSize )
512
0
    {
513
0
        aText = GetMetricStr_Impl( pImpl->aPos.X()) + " / " +
514
0
            GetMetricStr_Impl( pImpl->aPos.Y());
515
        // widest X/Y string looks like "-999,99"
516
0
        nCharsWidth = 1 + 6 + 3 + 6; // icon + x + slash + y
517
0
        if ( pImpl->bSize )
518
0
        {
519
0
            aText += " " + GetMetricStr_Impl( pImpl->aSize.Width() ) + " x " +
520
0
                GetMetricStr_Impl( pImpl->aSize.Height() );
521
0
            nCharsWidth += 1 + 1 + 4 + 3 + 4; // icon + space + w + x + h
522
0
        }
523
0
    }
524
0
    else if ( pImpl->bTable )
525
0
       aText = pImpl->aStr;
526
527
0
    GetStatusBar().SetItemText( GetId(), aText, nCharsWidth );
528
0
}
529
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */