Update git submodules
[LibreOffice.git] / sd / source / ui / app / sdpopup.cxx
blob8c9607421be7cd8f88c17970d8cdbe399e445dab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <string_view>
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>
30 #include <strings.hrc>
31 #include <sdpopup.hxx>
32 #include <sdresid.hxx>
33 #include <sdmod.hxx>
34 #include <DrawDocShell.hxx>
37 * Popup menu for editing of field command
39 SdFieldPopup::SdFieldPopup(const SvxFieldData* pInField, LanguageType eLanguage)
40 : m_xBuilder(Application::CreateBuilder(nullptr, u"modules/simpress/ui/fieldmenu.ui"_ustr))
41 , m_xPopup(m_xBuilder->weld_menu(u"menu"_ustr))
42 , m_pField(pInField)
44 Fill(eLanguage);
47 SdFieldPopup::~SdFieldPopup()
51 void SdFieldPopup::Fill( LanguageType eLanguage )
53 sal_uInt16 nID = 1;
54 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FIX));
55 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_VAR));
56 m_xPopup->append_separator(u"separator1"_ustr);
58 if( auto pDateField = dynamic_cast< const SvxDateField *>( m_pField ) )
60 SvxDateField aDateField( *pDateField );
62 if (pDateField->GetType() == SvxDateType::Fix)
63 m_xPopup->set_active(u"1"_ustr, true);
64 else
65 m_xPopup->set_active(u"2"_ustr, true);
67 //SvxDateFormat::AppDefault, // is not used
68 //SvxDateFormat::System, // is not used
69 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_STANDARD_SMALL));
70 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_STANDARD_BIG));
72 SvNumberFormatter* pNumberFormatter = SdModule::get()->GetNumberFormatter();
73 aDateField.SetFormat( SvxDateFormat::A ); // 13.02.96
74 m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
75 aDateField.SetFormat( SvxDateFormat::B ); // 13.02.1996
76 m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
77 aDateField.SetFormat( SvxDateFormat::C ); // 13.Feb 1996
78 m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
80 aDateField.SetFormat( SvxDateFormat::D ); // 13.Februar 1996
81 m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
82 aDateField.SetFormat( SvxDateFormat::E ); // Die, 13.Februar 1996
83 m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
84 aDateField.SetFormat( SvxDateFormat::F ); // Dienstag, 13.Februar 1996
85 m_xPopup->append_radio(OUString::number(nID++), aDateField.GetFormatted(*pNumberFormatter, eLanguage));
87 m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pDateField->GetFormat() ) + 1), true); // - 2 + 3 !
89 else if( auto pTimeField = dynamic_cast< const SvxExtTimeField *>( m_pField ) )
91 SvxExtTimeField aTimeField( *pTimeField );
93 if( pTimeField->GetType() == SvxTimeType::Fix )
94 m_xPopup->set_active(u"1"_ustr, true);
95 else
96 m_xPopup->set_active(u"2"_ustr, true);
98 //SvxTimeFormat::AppDefault, // is not used
99 //SvxTimeFormat::System, // is not used
100 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_STANDARD_NORMAL));
102 SvNumberFormatter* pNumberFormatter = SdModule::get()->GetNumberFormatter();
103 aTimeField.SetFormat( SvxTimeFormat::HH24_MM ); // 13:49
104 m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
105 aTimeField.SetFormat( SvxTimeFormat::HH24_MM_SS ); // 13:49:38
106 m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
107 aTimeField.SetFormat( SvxTimeFormat::HH24_MM_SS_00 ); // 13:49:38.78
108 m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
110 aTimeField.SetFormat( SvxTimeFormat::HH12_MM ); // 01:49
111 m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
112 aTimeField.SetFormat( SvxTimeFormat::HH12_MM_SS ); // 01:49:38
113 m_xPopup->append_radio(OUString::number(nID++), aTimeField.GetFormatted(*pNumberFormatter, eLanguage));
114 aTimeField.SetFormat( SvxTimeFormat::HH12_MM_SS_00 ); // 01:49:38.78
115 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
120 m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pTimeField->GetFormat() ) + 1), true); // - 2 + 3 !
122 else if( auto pFileField = dynamic_cast< const SvxExtFileField *>( m_pField ) )
124 //SvxExtFileField aFileField( *pFileField );
126 if( pFileField->GetType() == SvxFileType::Fix )
127 m_xPopup->set_active(u"1"_ustr, true);
128 else
129 m_xPopup->set_active(u"2"_ustr, true);
131 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_NAME_EXT));
132 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_FULLPATH));
133 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_PATH));
134 m_xPopup->append_radio(OUString::number(nID++), SdResId(STR_FILEFORMAT_NAME));
136 m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pFileField->GetFormat() ) + 3), true);
138 else if( auto pAuthorField = dynamic_cast< const SvxAuthorField *>( m_pField ) )
140 SvxAuthorField aAuthorField( *pAuthorField );
142 if( pAuthorField->GetType() == SvxAuthorType::Fix )
143 m_xPopup->set_active(u"1"_ustr, true);
144 else
145 m_xPopup->set_active(u"2"_ustr, true);
147 for( sal_uInt16 i = 0; i < 4; i++ )
149 aAuthorField.SetFormat( static_cast<SvxAuthorFormat>(i) );
150 m_xPopup->append_radio(OUString::number(nID++), aAuthorField.GetFormatted());
152 m_xPopup->set_active(OUString::number(static_cast<sal_uInt16>( pAuthorField->GetFormat() ) + 3), true);
156 void SdFieldPopup::Execute(weld::Window* pParent, const tools::Rectangle& rRect)
158 OUString sIdent = m_xPopup->popup_at_rect(pParent, rRect);
159 if (sIdent.isEmpty())
160 return;
162 if (sIdent == "1" || sIdent == "2")
164 m_xPopup->set_active(u"1"_ustr, sIdent == "1");
165 m_xPopup->set_active(u"2"_ustr, sIdent == "2");
167 else
169 int nCount = m_xPopup->n_children();
170 for (int i = 3; i < nCount; i++)
171 m_xPopup->set_active(
172 OUString::number(i), sIdent == std::u16string_view(OUString::number(i)));
177 * Returns a new field, owned by caller.
178 * Returns NULL if nothing changed.
180 SvxFieldData* SdFieldPopup::GetField()
182 SvxFieldData* pNewField = nullptr;
184 sal_uInt16 nCount = m_xPopup->n_children();
186 if( auto pDateField = dynamic_cast< const SvxDateField *>( m_pField ) )
188 SvxDateType eType;
189 SvxDateFormat eFormat;
190 sal_uInt16 i;
192 if (m_xPopup->get_active(u"1"_ustr))
193 eType = SvxDateType::Fix;
194 else
195 eType = SvxDateType::Var;
197 for( i = 3; i < nCount; i++ )
199 if (m_xPopup->get_active(OUString::number(i)))
200 break;
202 eFormat = static_cast<SvxDateFormat>( i - 1 );
204 if( pDateField->GetFormat() != eFormat ||
205 pDateField->GetType() != eType )
207 pNewField = new SvxDateField( *pDateField );
208 static_cast<SvxDateField*>( pNewField )->SetType( eType );
209 static_cast<SvxDateField*>( pNewField )->SetFormat( eFormat );
211 if( (pDateField->GetType() == SvxDateType::Var) && (eType == SvxDateType::Fix) )
213 Date aDate( Date::SYSTEM );
214 static_cast<SvxDateField*>( pNewField )->SetFixDate( aDate );
218 else if( auto pTimeField = dynamic_cast< const SvxExtTimeField *>( m_pField ) )
220 SvxTimeType eType;
221 SvxTimeFormat eFormat;
222 sal_uInt16 i;
224 if (m_xPopup->get_active(u"1"_ustr))
225 eType = SvxTimeType::Fix;
226 else
227 eType = SvxTimeType::Var;
229 for( i = 3; i < nCount; i++ )
231 if (m_xPopup->get_active(OUString::number(i)))
232 break;
234 eFormat = static_cast<SvxTimeFormat>( i - 1 );
236 if( pTimeField->GetFormat() != eFormat ||
237 pTimeField->GetType() != eType )
239 pNewField = new SvxExtTimeField( *pTimeField );
240 static_cast<SvxExtTimeField*>( pNewField )->SetType( eType );
241 static_cast<SvxExtTimeField*>( pNewField )->SetFormat( eFormat );
243 if( (pTimeField->GetType() == SvxTimeType::Var) && (eType == SvxTimeType::Fix) )
245 tools::Time aTime( tools::Time::SYSTEM );
246 static_cast<SvxExtTimeField*>( pNewField )->SetFixTime( aTime );
251 else if( auto pFileField = dynamic_cast< const SvxExtFileField *>( m_pField ) )
253 SvxFileType eType;
254 SvxFileFormat eFormat;
255 sal_uInt16 i;
257 if (m_xPopup->get_active(u"1"_ustr))
258 eType = SvxFileType::Fix;
259 else
260 eType = SvxFileType::Var;
262 for( i = 3; i < nCount; i++ )
264 if (m_xPopup->get_active(OUString::number(i)))
265 break;
267 eFormat = static_cast<SvxFileFormat>( i - 3 );
269 if( pFileField->GetFormat() != eFormat ||
270 pFileField->GetType() != eType )
272 ::sd::DrawDocShell* pDocSh = dynamic_cast<::sd::DrawDocShell* >( SfxObjectShell::Current() );
274 if( pDocSh )
276 OUString aName;
277 if( pDocSh->HasName() )
278 aName = pDocSh->GetMedium()->GetName();
280 // Get current filename, not the one stored in the old field
281 pNewField = new SvxExtFileField( aName );
282 static_cast<SvxExtFileField*>( pNewField )->SetType( eType );
283 static_cast<SvxExtFileField*>( pNewField )->SetFormat( eFormat );
287 else if( auto pAuthorField = dynamic_cast< const SvxAuthorField *>( m_pField ) )
289 SvxAuthorType eType;
290 SvxAuthorFormat eFormat;
291 sal_uInt16 i;
293 if (m_xPopup->get_active(u"1"_ustr))
294 eType = SvxAuthorType::Fix;
295 else
296 eType = SvxAuthorType::Var;
298 for( i = 3; i < nCount; i++ )
300 if (m_xPopup->get_active(OUString::number(i)))
301 break;
303 eFormat = static_cast<SvxAuthorFormat>( i - 3 );
305 if( pAuthorField->GetFormat() != eFormat ||
306 pAuthorField->GetType() != eType )
308 // Get current state of address, not the old one
309 SvtUserOptions aUserOptions;
310 pNewField = new SvxAuthorField( aUserOptions.GetFirstName(), aUserOptions.GetLastName(), aUserOptions.GetID() );
311 static_cast<SvxAuthorField*>( pNewField )->SetType( eType );
312 static_cast<SvxAuthorField*>( pNewField )->SetFormat( eFormat );
315 return pNewField;
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */