Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sd/source/ui/app/sdpopup.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 <editeng/flditem.hxx>
25
#include <sfx2/objsh.hxx>
26
#include <sfx2/docfile.hxx>
27
#include <unotools/useroptions.hxx>
28
#include <vcl/svapp.hxx>
29
#include <vcl/weld/Menu.hxx>
30
31
#include <strings.hrc>
32
#include <sdpopup.hxx>
33
#include <sdresid.hxx>
34
#include <sdmod.hxx>
35
#include <DrawDocShell.hxx>
36
37
/*
38
 * Popup menu for editing of field command
39
 */
40
SdFieldPopup::SdFieldPopup(const SvxFieldData* pInField, LanguageType eLanguage)
41
0
    : m_xBuilder(Application::CreateBuilder(nullptr, u"modules/simpress/ui/fieldmenu.ui"_ustr))
42
0
    , m_xPopup(m_xBuilder->weld_menu(u"menu"_ustr))
43
0
    , m_pField(pInField)
44
0
{
45
0
    Fill(eLanguage);
46
0
}
47
48
SdFieldPopup::~SdFieldPopup()
49
0
{
50
0
}
51
52
void SdFieldPopup::Fill( LanguageType eLanguage )
53
0
{
54
0
    sal_uInt16 nID = 1;
55
0
    m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FIX));
56
0
    m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_VAR));
57
0
    m_xPopup->append_separator(u"separator1"_ustr);
58
59
0
    if( auto pDateField = dynamic_cast< const SvxDateField *>( m_pField ) )
60
0
    {
61
0
        SvxDateField aDateField( *pDateField );
62
63
0
        if (pDateField->GetType() == SvxDateType::Fix)
64
0
            m_xPopup->set_active(u"1"_ustr, true);
65
0
        else
66
0
            m_xPopup->set_active(u"2"_ustr, true);
67
68
        //SvxDateFormat::AppDefault,     // is not used
69
        //SvxDateFormat::System,         // is not used
70
0
        m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_STANDARD_SMALL));
71
0
        m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_STANDARD_BIG));
72
73
0
        SvNumberFormatter* pNumberFormatter = SdModule::get()->GetNumberFormatter();
74
0
        aDateField.SetFormat( SvxDateFormat::A );    // 13.02.96
75
0
        m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
76
0
        aDateField.SetFormat( SvxDateFormat::B );    // 13.02.1996
77
0
        m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
78
0
        aDateField.SetFormat( SvxDateFormat::C );    // 13.Feb 1996
79
0
        m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
80
81
0
        aDateField.SetFormat( SvxDateFormat::D );    // 13.Februar 1996
82
0
        m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
83
0
        aDateField.SetFormat( SvxDateFormat::E );    // Die, 13.Februar 1996
84
0
        m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
85
0
        aDateField.SetFormat( SvxDateFormat::F );    // Dienstag, 13.Februar 1996
86
0
        m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
87
88
0
        m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pDateField->GetFormat() ) + 1), true); // - 2 + 3 !
89
0
    }
90
0
    else if( auto pTimeField = dynamic_cast< const SvxExtTimeField *>( m_pField ) )
91
0
    {
92
0
        SvxExtTimeField aTimeField( *pTimeField );
93
94
0
        if( pTimeField->GetType() == SvxTimeType::Fix )
95
0
            m_xPopup->set_active(u"1"_ustr, true);
96
0
        else
97
0
            m_xPopup->set_active(u"2"_ustr, true);
98
99
        //SvxTimeFormat::AppDefault,     // is not used
100
        //SvxTimeFormat::System,         // is not used
101
0
        m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_STANDARD_NORMAL));
102
103
0
        SvNumberFormatter* pNumberFormatter = SdModule::get()->GetNumberFormatter();
104
0
        aTimeField.SetFormat( SvxTimeFormat::HH24_MM );    // 13:49
105
0
        m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
106
0
        aTimeField.SetFormat( SvxTimeFormat::HH24_MM_SS );   // 13:49:38
107
0
        m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
108
0
        aTimeField.SetFormat( SvxTimeFormat::HH24_MM_SS_00 );  // 13:49:38.78
109
0
        m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
110
111
0
        aTimeField.SetFormat( SvxTimeFormat::HH12_MM );    // 01:49
112
0
        m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
113
0
        aTimeField.SetFormat( SvxTimeFormat::HH12_MM_SS );   // 01:49:38
114
0
        m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
115
0
        aTimeField.SetFormat( SvxTimeFormat::HH12_MM_SS_00 );  // 01:49:38.78
116
0
        m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
117
        //SvxTimeFormat::HH12_MM_AMPM,  // 01:49 PM
118
        //SvxTimeFormat::HH12_MM_SS_AMPM, // 01:49:38 PM
119
        //SvxTimeFormat::HH12_MM_SS_00_AMPM // 01:49:38.78 PM
120
121
0
        m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pTimeField->GetFormat() ) + 1), true); // - 2 + 3 !
122
0
    }
123
0
    else if( auto pFileField = dynamic_cast< const SvxExtFileField *>( m_pField ) )
124
0
    {
125
        //SvxExtFileField aFileField( *pFileField );
126
127
0
        if( pFileField->GetType() == SvxFileType::Fix )
128
0
            m_xPopup->set_active(u"1"_ustr, true);
129
0
        else
130
0
            m_xPopup->set_active(u"2"_ustr, true);
131
132
0
        m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_NAME_EXT));
133
0
        m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_FULLPATH));
134
0
        m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_PATH));
135
0
        m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_NAME));
136
137
0
        m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pFileField->GetFormat() ) + 3), true);
138
0
    }
139
0
    else if( auto pAuthorField = dynamic_cast< const SvxAuthorField *>( m_pField ) )
140
0
    {
141
0
        SvxAuthorField aAuthorField( *pAuthorField );
142
143
0
        if( pAuthorField->GetType() == SvxAuthorType::Fix )
144
0
            m_xPopup->set_active(u"1"_ustr, true);
145
0
        else
146
0
            m_xPopup->set_active(u"2"_ustr, true);
147
148
0
        for( sal_uInt16 i = 0; i < 4; i++ )
149
0
        {
150
0
            aAuthorField.SetFormat( static_cast<SvxAuthorFormat>(i) );
151
0
            m_xPopup->append_radio(OUString::number(nID++), aAuthorField.GetFormatted());
152
0
        }
153
0
        m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pAuthorField->GetFormat() ) + 3), true);
154
0
    }
155
0
}
156
157
void SdFieldPopup::Execute(weld::Window* pParent, const tools::Rectangle& rRect)
158
0
{
159
0
    OUString sIdent = m_xPopup->popup_at_rect(pParent, rRect);
160
0
    if (sIdent.isEmpty())
161
0
        return;
162
163
0
    if (sIdent == "1" || sIdent == "2")
164
0
    {
165
0
        m_xPopup->set_active(u"1"_ustr, sIdent == "1");
166
0
        m_xPopup->set_active(u"2"_ustr, sIdent == "2");
167
0
    }
168
0
    else
169
0
    {
170
0
        int nCount = m_xPopup->n_children();
171
0
        for (int i = 3; i < nCount; i++)
172
0
            m_xPopup->set_active(
173
0
                OUString::number(i), sIdent == std::u16string_view(OUString::number(i)));
174
0
    }
175
0
}
176
177
/**
178
 * Returns a new field, owned by caller.
179
 * Returns NULL if nothing changed.
180
 */
181
SvxFieldData* SdFieldPopup::GetField()
182
0
{
183
0
    SvxFieldData* pNewField = nullptr;
184
185
0
    sal_uInt16 nCount = m_xPopup->n_children();
186
187
0
    if( auto pDateField = dynamic_cast< const SvxDateField *>( m_pField ) )
188
0
    {
189
0
        SvxDateType   eType;
190
0
        SvxDateFormat eFormat;
191
0
        sal_uInt16 i;
192
193
0
        if (m_xPopup->get_active(u"1"_ustr))
194
0
            eType = SvxDateType::Fix;
195
0
        else
196
0
            eType = SvxDateType::Var;
197
198
0
        for( i = 3; i < nCount; i++ )
199
0
        {
200
0
            if (m_xPopup->get_active(OUString::number(i)))
201
0
                break;
202
0
        }
203
0
        eFormat = static_cast<SvxDateFormat>( i - 1 );
204
205
0
        if( pDateField->GetFormat() != eFormat ||
206
0
            pDateField->GetType() != eType )
207
0
        {
208
0
            pNewField = new SvxDateField( *pDateField );
209
0
            static_cast<SvxDateField*>( pNewField )->SetType( eType );
210
0
            static_cast<SvxDateField*>( pNewField )->SetFormat( eFormat );
211
212
0
            if( (pDateField->GetType() == SvxDateType::Var) && (eType == SvxDateType::Fix) )
213
0
            {
214
0
                Date aDate( Date::SYSTEM );
215
0
                static_cast<SvxDateField*>( pNewField )->SetFixDate( aDate );
216
0
            }
217
0
        }
218
0
    }
219
0
    else if( auto pTimeField = dynamic_cast< const SvxExtTimeField *>( m_pField ) )
220
0
    {
221
0
        SvxTimeType   eType;
222
0
        SvxTimeFormat eFormat;
223
0
        sal_uInt16 i;
224
225
0
        if (m_xPopup->get_active(u"1"_ustr))
226
0
            eType = SvxTimeType::Fix;
227
0
        else
228
0
            eType = SvxTimeType::Var;
229
230
0
        for( i = 3; i < nCount; i++ )
231
0
        {
232
0
            if (m_xPopup->get_active(OUString::number(i)))
233
0
                break;
234
0
        }
235
0
        eFormat = static_cast<SvxTimeFormat>( i - 1 );
236
237
0
        if( pTimeField->GetFormat() != eFormat ||
238
0
            pTimeField->GetType() != eType )
239
0
        {
240
0
            pNewField = new SvxExtTimeField( *pTimeField );
241
0
            static_cast<SvxExtTimeField*>( pNewField )->SetType( eType );
242
0
            static_cast<SvxExtTimeField*>( pNewField )->SetFormat( eFormat );
243
244
0
            if( (pTimeField->GetType() == SvxTimeType::Var) && (eType == SvxTimeType::Fix) )
245
0
            {
246
0
                tools::Time aTime( tools::Time::SYSTEM );
247
0
                static_cast<SvxExtTimeField*>( pNewField )->SetFixTime( aTime );
248
0
            }
249
250
0
        }
251
0
    }
252
0
    else if( auto pFileField = dynamic_cast< const SvxExtFileField *>( m_pField ) )
253
0
    {
254
0
        SvxFileType   eType;
255
0
        SvxFileFormat eFormat;
256
0
        sal_uInt16 i;
257
258
0
        if (m_xPopup->get_active(u"1"_ustr))
259
0
            eType = SvxFileType::Fix;
260
0
        else
261
0
            eType = SvxFileType::Var;
262
263
0
        for( i = 3; i < nCount; i++ )
264
0
        {
265
0
            if (m_xPopup->get_active(OUString::number(i)))
266
0
                break;
267
0
        }
268
0
        eFormat = static_cast<SvxFileFormat>( i - 3 );
269
270
0
        if( pFileField->GetFormat() != eFormat ||
271
0
            pFileField->GetType() != eType )
272
0
        {
273
0
            ::sd::DrawDocShell* pDocSh = dynamic_cast<::sd::DrawDocShell* >( SfxObjectShell::Current() );
274
275
0
            if( pDocSh )
276
0
            {
277
0
                OUString aName;
278
0
                if( pDocSh->HasName() )
279
0
                    aName = pDocSh->GetMedium()->GetName();
280
281
                // Get current filename, not the one stored in the old field
282
0
                pNewField = new SvxExtFileField( aName );
283
0
                static_cast<SvxExtFileField*>( pNewField )->SetType( eType );
284
0
                static_cast<SvxExtFileField*>( pNewField )->SetFormat( eFormat );
285
0
            }
286
0
        }
287
0
    }
288
0
    else if( auto pAuthorField = dynamic_cast< const SvxAuthorField *>( m_pField ) )
289
0
    {
290
0
        SvxAuthorType   eType;
291
0
        SvxAuthorFormat eFormat;
292
0
        sal_uInt16 i;
293
294
0
        if (m_xPopup->get_active(u"1"_ustr))
295
0
            eType = SvxAuthorType::Fix;
296
0
        else
297
0
            eType = SvxAuthorType::Var;
298
299
0
        for( i = 3; i < nCount; i++ )
300
0
        {
301
0
            if (m_xPopup->get_active(OUString::number(i)))
302
0
                break;
303
0
        }
304
0
        eFormat = static_cast<SvxAuthorFormat>( i - 3 );
305
306
0
        if( pAuthorField->GetFormat() != eFormat ||
307
0
            pAuthorField->GetType() != eType )
308
0
        {
309
            // Get current state of address, not the old one
310
0
            SvtUserOptions aUserOptions;
311
0
            pNewField = new SvxAuthorField( aUserOptions.GetFirstName(), aUserOptions.GetLastName(), aUserOptions.GetID() );
312
0
            static_cast<SvxAuthorField*>( pNewField )->SetType( eType );
313
0
            static_cast<SvxAuthorField*>( pNewField )->SetFormat( eFormat );
314
0
        }
315
0
    }
316
0
    return pNewField;
317
0
}
318
319
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */