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 <editeng/eeitem.hxx>
21 #include <editeng/flditem.hxx>
22 #include <sfx2/objsh.hxx>
23 #include <sfx2/docfile.hxx>
24 #include <svl/itemset.hxx>
25 #include <svx/langbox.hxx>
26 #include <editeng/langitem.hxx>
27 #include <unotools/useroptions.hxx>
28 
29 #include <strings.hrc>
30 #include <sdresid.hxx>
31 #include <sdmod.hxx>
32 #include <dlgfield.hxx>
33 #include <DrawDocShell.hxx>
34 
35 /**
36  * dialog to edit field commands
37  */
SdModifyFieldDlg(weld::Window * pWindow,const SvxFieldData * pInField,const SfxItemSet & rSet)38 SdModifyFieldDlg::SdModifyFieldDlg(weld::Window* pWindow, const SvxFieldData* pInField, const SfxItemSet& rSet)
39     : GenericDialogController(pWindow, "modules/simpress/ui/dlgfield.ui", "EditFieldsDialog")
40     , m_aInputSet(rSet)
41     , m_pField(pInField)
42     , m_xRbtFix(m_xBuilder->weld_radio_button("fixedRB"))
43     , m_xRbtVar(m_xBuilder->weld_radio_button("varRB"))
44     , m_xLbLanguage(new SvxLanguageBox(m_xBuilder->weld_combo_box("languageLB")))
45     , m_xLbFormat(m_xBuilder->weld_combo_box("formatLB"))
46 {
47     m_xLbLanguage->SetLanguageList( SvxLanguageListFlags::ALL|SvxLanguageListFlags::ONLY_KNOWN, false );
48     m_xLbLanguage->connect_changed(LINK(this, SdModifyFieldDlg, LanguageChangeHdl));
49     FillControls();
50 }
51 
~SdModifyFieldDlg()52 SdModifyFieldDlg::~SdModifyFieldDlg()
53 {
54 }
55 
56 /**
57  * Returns the new field, owned by caller.
58  * Returns NULL if nothing has changed.
59  */
GetField()60 SvxFieldData* SdModifyFieldDlg::GetField()
61 {
62     SvxFieldData* pNewField = nullptr;
63 
64     if( m_xRbtFix->get_state_changed_from_saved() ||
65         m_xRbtVar->get_state_changed_from_saved() ||
66         m_xLbFormat->get_value_changed_from_saved() )
67     {
68         if( auto pDateField = dynamic_cast< const SvxDateField *>( m_pField ) )
69         {
70             SvxDateType   eType;
71             SvxDateFormat eFormat;
72 
73             if( m_xRbtFix->get_active() )
74                 eType = SvxDateType::Fix;
75             else
76                 eType = SvxDateType::Var;
77 
78             eFormat = static_cast<SvxDateFormat>( m_xLbFormat->get_active() + 2 );
79 
80             pNewField = new SvxDateField( *pDateField );
81             static_cast<SvxDateField*>( pNewField )->SetType( eType );
82             static_cast<SvxDateField*>( pNewField )->SetFormat( eFormat );
83         }
84         else if( auto pTimeField = dynamic_cast< const SvxExtTimeField *>( m_pField ) )
85         {
86             SvxTimeType   eType;
87             SvxTimeFormat eFormat;
88 
89             if( m_xRbtFix->get_active() )
90                 eType = SvxTimeType::Fix;
91             else
92                 eType = SvxTimeType::Var;
93 
94             eFormat = static_cast<SvxTimeFormat>( m_xLbFormat->get_active() + 2 );
95 
96             pNewField = new SvxExtTimeField( *pTimeField );
97             static_cast<SvxExtTimeField*>( pNewField )->SetType( eType );
98             static_cast<SvxExtTimeField*>( pNewField )->SetFormat( eFormat );
99         }
100         else if( dynamic_cast< const SvxExtFileField *>( m_pField ) !=  nullptr )
101         {
102             SvxFileType   eType;
103             SvxFileFormat eFormat;
104 
105             if( m_xRbtFix->get_active() )
106                 eType = SvxFileType::Fix;
107             else
108                 eType = SvxFileType::Var;
109 
110             eFormat = static_cast<SvxFileFormat>( m_xLbFormat->get_active() );
111 
112             ::sd::DrawDocShell* pDocSh = dynamic_cast< ::sd::DrawDocShell* >(SfxObjectShell::Current() );
113 
114             if( pDocSh )
115             {
116                 OUString aName;
117                 if( pDocSh->HasName() )
118                     aName = pDocSh->GetMedium()->GetName();
119 
120                 // Get current filename, not the one stored in the old field
121                 pNewField = new SvxExtFileField( aName );
122                 static_cast<SvxExtFileField*>( pNewField )->SetType( eType );
123                 static_cast<SvxExtFileField*>( pNewField )->SetFormat( eFormat );
124             }
125         }
126         else if( dynamic_cast< const SvxAuthorField *>( m_pField ) !=  nullptr )
127         {
128             SvxAuthorType   eType;
129             SvxAuthorFormat eFormat;
130 
131             if( m_xRbtFix->get_active() )
132                 eType = SvxAuthorType::Fix;
133             else
134                 eType = SvxAuthorType::Var;
135 
136             eFormat = static_cast<SvxAuthorFormat>( m_xLbFormat->get_active() );
137 
138             // Get current state of address, not the old one
139             SvtUserOptions aUserOptions;
140             pNewField = new SvxAuthorField( aUserOptions.GetFirstName(), aUserOptions.GetLastName(), aUserOptions.GetID() );
141             static_cast<SvxAuthorField*>( pNewField )->SetType( eType );
142             static_cast<SvxAuthorField*>( pNewField )->SetFormat( eFormat );
143         }
144     }
145 
146     return pNewField;
147 }
148 
FillFormatList()149 void SdModifyFieldDlg::FillFormatList()
150 {
151     LanguageType eLangType = m_xLbLanguage->get_active_id();
152 
153     m_xLbFormat->clear();
154 
155     if( auto pDateField = dynamic_cast< const SvxDateField *>( m_pField ) )
156     {
157         SvxDateField aDateField( *pDateField );
158 
159         //SvxDateFormat::AppDefault,     // not used
160         //SvxDateFormat::System,         // not used
161         m_xLbFormat->append_text( SdResId( STR_STANDARD_SMALL ) );
162         m_xLbFormat->append_text( SdResId( STR_STANDARD_BIG ) );
163 
164         SvNumberFormatter* pNumberFormatter = SD_MOD()->GetNumberFormatter();
165         aDateField.SetFormat( SvxDateFormat::A );    // 13.02.96
166         m_xLbFormat->append_text( aDateField.GetFormatted( *pNumberFormatter, eLangType ) );
167         aDateField.SetFormat( SvxDateFormat::B );    // 13.02.1996
168         m_xLbFormat->append_text( aDateField.GetFormatted( *pNumberFormatter, eLangType ) );
169         aDateField.SetFormat( SvxDateFormat::C );    // 13.Feb 1996
170         m_xLbFormat->append_text( aDateField.GetFormatted( *pNumberFormatter, eLangType ) );
171         aDateField.SetFormat( SvxDateFormat::D );    // 13.Februar 1996
172         m_xLbFormat->append_text( aDateField.GetFormatted( *pNumberFormatter, eLangType ) );
173         aDateField.SetFormat( SvxDateFormat::E );    // Die, 13.Februar 1996
174         m_xLbFormat->append_text( aDateField.GetFormatted( *pNumberFormatter, eLangType ) );
175         aDateField.SetFormat( SvxDateFormat::F );    // Dienstag, 13.Februar 1996
176         m_xLbFormat->append_text( aDateField.GetFormatted( *pNumberFormatter, eLangType ) );
177 
178         m_xLbFormat->set_active( static_cast<sal_uInt16>(pDateField->GetFormat()) - 2 );
179     }
180     else if( auto pTimeField = dynamic_cast< const SvxExtTimeField *>( m_pField ) )
181     {
182         SvxExtTimeField aTimeField( *pTimeField );
183 
184         //SvxTimeFormat::AppDefault,     // not used
185         //SvxTimeFormat::System,         // not used
186         m_xLbFormat->append_text( SdResId( STR_STANDARD_NORMAL ) );
187 
188         SvNumberFormatter* pNumberFormatter = SD_MOD()->GetNumberFormatter();
189         aTimeField.SetFormat( SvxTimeFormat::HH24_MM );    // 13:49
190         m_xLbFormat->append_text( aTimeField.GetFormatted( *pNumberFormatter, eLangType ) );
191         aTimeField.SetFormat( SvxTimeFormat::HH24_MM_SS );   // 13:49:38
192         m_xLbFormat->append_text( aTimeField.GetFormatted( *pNumberFormatter, eLangType ) );
193         aTimeField.SetFormat( SvxTimeFormat::HH24_MM_SS_00 );  // 13:49:38.78
194         m_xLbFormat->append_text( aTimeField.GetFormatted( *pNumberFormatter, eLangType ) );
195         aTimeField.SetFormat( SvxTimeFormat::HH12_MM );    // 01:49
196         m_xLbFormat->append_text( aTimeField.GetFormatted( *pNumberFormatter, eLangType ) );
197         aTimeField.SetFormat( SvxTimeFormat::HH12_MM_SS );   // 01:49:38
198         m_xLbFormat->append_text( aTimeField.GetFormatted( *pNumberFormatter, eLangType ) );
199         aTimeField.SetFormat( SvxTimeFormat::HH12_MM_SS_00 );  // 01:49:38.78
200         m_xLbFormat->append_text( aTimeField.GetFormatted( *pNumberFormatter, eLangType ) );
201         //SvxTimeFormat::HH12_MM_AMPM,  // 01:49 PM
202         //SvxTimeFormat::HH12_MM_SS_AMPM, // 01:49:38 PM
203         //SvxTimeFormat::HH12_MM_SS_00_AMPM // 01:49:38.78 PM
204 
205         m_xLbFormat->set_active( static_cast<sal_uInt16>(pTimeField->GetFormat()) - 2 );
206     }
207     else if( auto pFileField = dynamic_cast< const SvxExtFileField *>( m_pField ) )
208     {
209         m_xLbFormat->append_text( SdResId( STR_FILEFORMAT_NAME_EXT ) );
210         m_xLbFormat->append_text( SdResId( STR_FILEFORMAT_FULLPATH ) );
211         m_xLbFormat->append_text( SdResId( STR_FILEFORMAT_PATH ) );
212         m_xLbFormat->append_text( SdResId( STR_FILEFORMAT_NAME ) );
213 
214         m_xLbFormat->set_active( static_cast<sal_uInt16>( pFileField->GetFormat() ) );
215     }
216     else if( auto pAuthorField = dynamic_cast< const SvxAuthorField *>( m_pField ) )
217     {
218         SvxAuthorField aAuthorField( *pAuthorField );
219 
220         for( sal_uInt16 i = 0; i < 4; i++ )
221         {
222             aAuthorField.SetFormat( static_cast<SvxAuthorFormat>(i) );
223             m_xLbFormat->append_text( aAuthorField.GetFormatted() );
224         }
225 
226         m_xLbFormat->set_active( static_cast<sal_uInt16>( pAuthorField->GetFormat() ) );
227 
228     }
229 
230 }
231 
FillControls()232 void SdModifyFieldDlg::FillControls()
233 {
234     m_xLbFormat->clear();
235 
236     if( auto pDateField = dynamic_cast< const SvxDateField *>( m_pField ) )
237     {
238         if( pDateField->GetType() == SvxDateType::Fix )
239             m_xRbtFix->set_active(true);
240         else
241             m_xRbtVar->set_active(true);
242     }
243     else if( auto pTimeField = dynamic_cast< const SvxExtTimeField *>( m_pField ) )
244     {
245         if( pTimeField->GetType() == SvxTimeType::Fix )
246             m_xRbtFix->set_active(true);
247         else
248             m_xRbtVar->set_active(true);
249     }
250     else if( auto pFileField = dynamic_cast< const SvxExtFileField *>( m_pField ) )
251     {
252         if( pFileField->GetType() == SvxFileType::Fix )
253             m_xRbtFix->set_active(true);
254         else
255             m_xRbtVar->set_active(true);
256     }
257     else if( auto pAuthorField = dynamic_cast< const SvxAuthorField *>( m_pField ) )
258     {
259         if( pAuthorField->GetType() == SvxAuthorType::Fix )
260             m_xRbtFix->set_active(true);
261         else
262             m_xRbtVar->set_active(true);
263     }
264     m_xRbtFix->save_state();
265     m_xRbtVar->save_state();
266 
267     const SfxPoolItem* pItem;
268     if( SfxItemState::SET == m_aInputSet.GetItemState(EE_CHAR_LANGUAGE, true, &pItem ) )
269         m_xLbLanguage->set_active_id(static_cast<const SvxLanguageItem*>(pItem)->GetLanguage());
270 
271     m_xLbLanguage->save_active_id();
272 
273     FillFormatList();
274     m_xLbFormat->save_value();
275 }
276 
IMPL_LINK_NOARG(SdModifyFieldDlg,LanguageChangeHdl,weld::ComboBox &,void)277 IMPL_LINK_NOARG(SdModifyFieldDlg, LanguageChangeHdl, weld::ComboBox&, void)
278 {
279     FillFormatList();
280 }
281 
GetItemSet() const282 SfxItemSet SdModifyFieldDlg::GetItemSet() const
283 {
284     SfxItemSet aOutput( *m_aInputSet.GetPool(), svl::Items<EE_CHAR_LANGUAGE, EE_CHAR_LANGUAGE_CTL>{} );
285 
286     if (m_xLbLanguage->get_active_id_changed_from_saved())
287     {
288         LanguageType eLangType = m_xLbLanguage->get_active_id();
289         SvxLanguageItem aItem( eLangType, EE_CHAR_LANGUAGE );
290         aOutput.Put( aItem );
291 
292         SvxLanguageItem aItemCJK( eLangType, EE_CHAR_LANGUAGE_CJK );
293         aOutput.Put( aItemCJK );
294 
295         SvxLanguageItem aItemCTL( eLangType, EE_CHAR_LANGUAGE_CTL );
296         aOutput.Put( aItemCTL );
297     }
298 
299     return aOutput;
300 }
301 
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
303