Coverage Report

Created: 2025-12-31 10:39

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