Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/source/uibase/utlui/tmplctrl.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 <svl/style.hxx>
21
#include <svl/stritem.hxx>
22
#include <sfx2/dispatch.hxx>
23
#include <sfx2/viewfrm.hxx>
24
#include <vcl/commandevent.hxx>
25
#include <vcl/status.hxx>
26
#include <vcl/svapp.hxx>
27
#include <vcl/weld/Builder.hxx>
28
#include <vcl/weld/Menu.hxx>
29
#include <vcl/weld/Window.hxx>
30
#include <vcl/weld/weldutils.hxx>
31
32
#include <swtypes.hxx>
33
#include <strings.hrc>
34
35
#include <wrtsh.hxx>
36
#include <view.hxx>
37
#include <swmodule.hxx>
38
#include <cmdid.h>
39
#include <docsh.hxx>
40
#include <tmplctrl.hxx>
41
42
SFX_IMPL_STATUSBAR_CONTROL( SwTemplateControl, SfxStringItem );
43
44
SwTemplateControl::SwTemplateControl( sal_uInt16 _nSlotId,
45
                                      sal_uInt16 _nId,
46
                                      StatusBar& rStb ) :
47
0
    SfxStatusBarControl( _nSlotId, _nId, rStb )
48
0
{
49
0
}
50
51
SwTemplateControl::~SwTemplateControl()
52
0
{
53
0
}
54
55
void SwTemplateControl::StateChangedAtStatusBarControl(
56
    sal_uInt16 /*nSID*/, SfxItemState eState, const SfxPoolItem* pState )
57
0
{
58
0
    const SfxStringItem* pItem = nullptr;
59
0
    if (SfxItemState::DEFAULT == eState && (pItem = dynamic_cast<const SfxStringItem*>(pState)))
60
0
    {
61
0
        m_sTemplate = pItem->GetValue();
62
0
        GetStatusBar().SetItemText(GetId(), m_sTemplate);
63
0
        GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_TMPLCTRL_HINT));
64
0
    }
65
0
    else
66
0
    {
67
0
        GetStatusBar().SetItemText(GetId(), OUString());
68
0
        GetStatusBar().SetQuickHelpText(GetId(), u""_ustr);
69
0
    }
70
0
}
71
72
void SwTemplateControl::Paint( const UserDrawEvent&  )
73
0
{
74
0
}
75
76
void SwTemplateControl::Command( const CommandEvent& rCEvt )
77
0
{
78
0
    if ( rCEvt.GetCommand() != CommandEventId::ContextMenu ||
79
0
            GetStatusBar().GetItemText( GetId() ).isEmpty())
80
0
        return;
81
82
0
    {
83
0
        SwView* pView = ::GetActiveView();
84
0
        SwWrtShell *const pWrtShell(pView ? pView->GetWrtShellPtr() : nullptr);
85
0
        if (nullptr != pWrtShell &&
86
0
            !pWrtShell->SwCursorShell::HasSelection()&&
87
0
            !pWrtShell->IsSelFrameMode() &&
88
0
            !pWrtShell->GetSelectedObjCount())
89
0
        {
90
0
            SfxStyleSheetBasePool* pPool = pView->GetDocShell()->
91
0
                                                        GetStyleSheetPool();
92
0
            auto xIter = pPool->CreateIterator(SfxStyleFamily::Page);
93
0
            if (xIter->Count() > 1)
94
0
            {
95
0
                std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, u"modules/swriter/ui/pagestylemenu.ui"_ustr));
96
0
                std::unique_ptr<weld::Menu> xPopup(xBuilder->weld_menu(u"menu"_ustr));
97
98
0
                sal_uInt32 nCount = 0;
99
0
                SfxStyleSheetBase* pStyle = xIter->First();
100
0
                while( pStyle )
101
0
                {
102
0
                    xPopup->append(OUString::number(++nCount), pStyle->GetName());
103
0
                    pStyle = xIter->Next();
104
0
                }
105
106
0
                ::tools::Rectangle aRect(rCEvt.GetMousePosPixel(), Size(1, 1));
107
0
                weld::Window* pParent = weld::GetPopupParent(GetStatusBar(), aRect);
108
0
                OUString sResult = xPopup->popup_at_rect(pParent, aRect);
109
0
                sal_uInt32 nCurrId = sResult.toUInt32();
110
0
                if (nCurrId > 0)
111
0
                {
112
                    // looks a bit awkward, but another way is not possible
113
0
                    pStyle = xIter->operator[]( nCurrId - 1 );
114
0
                    SfxStringItem aStyle( FN_SET_PAGE_STYLE, pStyle->GetName() );
115
0
                    pWrtShell->GetView().GetViewFrame().GetDispatcher()->ExecuteList(
116
0
                                FN_SET_PAGE_STYLE,
117
0
                                SfxCallMode::SLOT|SfxCallMode::RECORD,
118
0
                                { &aStyle });
119
0
                }
120
0
            }
121
0
        }
122
0
    }
123
0
}
124
125
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */