1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <string_view>
25 #include <comphelper/diagnose_ex.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/string.hxx>
28 #include <o3tl/string_view.hxx>
29 #include <officecfg/Office/Common.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/event.hxx>
32 #include <vcl/toolkit/field.hxx>
33 #include <vcl/unohelp.hxx>
34 #include <vcl/settings.hxx>
35 #include <vcl/weldutils.hxx>
39 #include <com/sun/star/i18n/XCharacterClassification.hpp>
40 #include <com/sun/star/i18n/CalendarFieldIndex.hdl>
42 #include <unotools/localedatawrapper.hxx>
43 #include <unotools/calendarwrapper.hxx>
44 #include <unotools/charclass.hxx>
45 #include <svl/numformat.hxx>
46 #include <svl/zforlist.hxx>
48 using namespace ::com::sun::star
;
49 using namespace ::comphelper
;
51 #define EDITMASK_LITERAL 'L'
52 #define EDITMASK_ALPHA 'a'
53 #define EDITMASK_UPPERALPHA 'A'
54 #define EDITMASK_ALPHANUM 'c'
55 #define EDITMASK_UPPERALPHANUM 'C'
56 #define EDITMASK_NUM 'N'
57 #define EDITMASK_NUMSPACE 'n'
58 #define EDITMASK_ALLCHAR 'x'
59 #define EDITMASK_UPPERALLCHAR 'X'
61 uno::Reference
< i18n::XCharacterClassification
> const & ImplGetCharClass()
63 ImplSVData
*const pSVData
= ImplGetSVData();
66 if (!pSVData
->m_xCharClass
.is())
68 pSVData
->m_xCharClass
= vcl::unohelper::CreateCharacterClassification();
71 return pSVData
->m_xCharClass
;
74 static sal_Unicode
* ImplAddString( sal_Unicode
* pBuf
, const OUString
& rStr
)
76 memcpy( pBuf
, rStr
.getStr(), rStr
.getLength() * sizeof(sal_Unicode
) );
77 pBuf
+= rStr
.getLength();
81 static sal_Unicode
* ImplAddNum( sal_Unicode
* pBuf
, sal_uLong nNumber
, int nMinLen
)
83 // fill temp buffer with digits
84 sal_Unicode aTempBuf
[30];
85 sal_Unicode
* pTempBuf
= aTempBuf
;
88 *pTempBuf
= static_cast<sal_Unicode
>(nNumber
% 10) + '0';
96 // fill with zeros up to the minimal length
104 // copy temp buffer to real buffer
111 while ( pTempBuf
!= aTempBuf
);
116 static sal_Unicode
* ImplAddSNum( sal_Unicode
* pBuf
, sal_Int32 nNumber
, int nMinLen
)
123 return ImplAddNum( pBuf
, nNumber
, nMinLen
);
126 static sal_uInt16
ImplGetNum( const sal_Unicode
*& rpBuf
, bool& rbError
)
134 sal_uInt16 nNumber
= 0;
135 while( ( *rpBuf
>= '0' ) && ( *rpBuf
<= '9' ) )
138 nNumber
+= *rpBuf
- '0';
145 static void ImplSkipDelimiters( const sal_Unicode
*& rpBuf
)
147 while( ( *rpBuf
== ',' ) || ( *rpBuf
== '.' ) || ( *rpBuf
== ';' ) ||
148 ( *rpBuf
== ':' ) || ( *rpBuf
== '-' ) || ( *rpBuf
== '/' ) )
154 static bool ImplIsPatternChar( sal_Unicode cChar
, char cEditMask
)
160 OUString
aCharStr(cChar
);
161 nType
= ImplGetCharClass()->getCharacterType( aCharStr
, 0,
162 Application::GetSettings().GetLanguageTag().getLocale() );
164 catch (const css::uno::Exception
&)
166 DBG_UNHANDLED_EXCEPTION("vcl.control");
170 if ( (cEditMask
== EDITMASK_ALPHA
) || (cEditMask
== EDITMASK_UPPERALPHA
) )
172 if( !CharClass::isLetterType( nType
) )
175 else if ( cEditMask
== EDITMASK_NUM
)
177 if( !CharClass::isNumericType( nType
) )
180 else if ( (cEditMask
== EDITMASK_ALPHANUM
) || (cEditMask
== EDITMASK_UPPERALPHANUM
) )
182 if( !CharClass::isLetterNumericType( nType
) )
185 else if ( (cEditMask
== EDITMASK_ALLCHAR
) || (cEditMask
== EDITMASK_UPPERALLCHAR
) )
190 else if ( cEditMask
== EDITMASK_NUMSPACE
)
192 if ( !CharClass::isNumericType( nType
) && ( cChar
!= ' ' ) )
201 static sal_Unicode
ImplPatternChar( sal_Unicode cChar
, char cEditMask
)
203 if ( ImplIsPatternChar( cChar
, cEditMask
) )
205 if ( (cEditMask
== EDITMASK_UPPERALPHA
) ||
206 (cEditMask
== EDITMASK_UPPERALPHANUM
) ||
207 ( cEditMask
== EDITMASK_UPPERALLCHAR
) )
209 cChar
= ImplGetCharClass()->toUpper(OUString(cChar
), 0, 1,
210 Application::GetSettings().GetLanguageTag().getLocale())[0];
218 static bool ImplCommaPointCharEqual( sal_Unicode c1
, sal_Unicode c2
)
222 else if ( ((c1
== '.') || (c1
== ',')) &&
223 ((c2
== '.') || (c2
== ',')) )
229 static OUString
ImplPatternReformat( const OUString
& rStr
,
230 const OString
& rEditMask
,
231 std::u16string_view rLiteralMask
,
232 sal_uInt16 nFormatFlags
)
234 if (rEditMask
.isEmpty())
237 OUStringBuffer
aOutStr(rLiteralMask
);
238 sal_Unicode cTempChar
;
240 sal_Unicode cLiteral
;
242 sal_Int32 nStrIndex
= 0;
246 while ( i
< rEditMask
.getLength() )
248 if ( nStrIndex
>= rStr
.getLength() )
251 cChar
= rStr
[nStrIndex
];
252 cLiteral
= rLiteralMask
[i
];
253 cMask
= rEditMask
[i
];
255 // current position is a literal
256 if ( cMask
== EDITMASK_LITERAL
)
258 // if it is a literal copy otherwise ignore because it might be the next valid
259 // character of the string
260 if ( ImplCommaPointCharEqual( cChar
, cLiteral
) )
264 // Otherwise we check if it is an invalid character. This is the case if it does not
265 // fit in the pattern of the next non-literal character.
267 while ( n
< rEditMask
.getLength() )
269 if ( rEditMask
[n
] != EDITMASK_LITERAL
)
271 if ( !ImplIsPatternChar( cChar
, rEditMask
[n
] ) )
282 // valid character at this position
283 cTempChar
= ImplPatternChar( cChar
, cMask
);
286 // use this character
287 aOutStr
[i
] = cTempChar
;
292 // copy if it is a literal character
293 if ( cLiteral
== cChar
)
297 // If the invalid character might be the next literal character then we jump
298 // ahead to it, otherwise we ignore it. Do only if empty literals are allowed.
299 if ( nFormatFlags
& PATTERN_FORMAT_EMPTYLITERALS
)
302 while ( n
< rEditMask
.getLength() )
304 if ( rEditMask
[n
] == EDITMASK_LITERAL
)
306 if ( ImplCommaPointCharEqual( cChar
, rLiteralMask
[n
] ) )
325 return aOutStr
.makeStringAndClear();
328 static void ImplPatternMaxPos( std::u16string_view rStr
, const OString
& rEditMask
,
329 sal_uInt16 nFormatFlags
, bool bSameMask
,
330 sal_Int32 nCursorPos
, sal_Int32
& rPos
)
333 // last position must not be longer than the contained string
334 sal_Int32 nMaxPos
= rStr
.size();
336 // if non empty literals are allowed ignore blanks at the end as well
337 if ( bSameMask
&& !(nFormatFlags
& PATTERN_FORMAT_EMPTYLITERALS
) )
341 if ( (rEditMask
[nMaxPos
-1] != EDITMASK_LITERAL
) &&
342 (rStr
[nMaxPos
-1] != ' ') )
347 // if we are in front of a literal, continue search until first character after the literal
348 sal_Int32 nTempPos
= nMaxPos
;
349 while ( nTempPos
< rEditMask
.getLength() )
351 if ( rEditMask
[nTempPos
] != EDITMASK_LITERAL
)
360 if ( rPos
> nMaxPos
)
363 // character should not move left
364 if ( rPos
< nCursorPos
)
368 static OUString
ImplPatternProcessStrictModify(const OUString
& rText
,
369 const OString
& rEditMask
,
370 std::u16string_view rLiteralMask
,
373 OUString
aText(rText
);
375 // remove leading blanks
376 if (bSameMask
&& !rEditMask
.isEmpty())
379 sal_Int32 nMaxLen
= aText
.getLength();
380 while ( i
< nMaxLen
)
382 if ( (rEditMask
[i
] != EDITMASK_LITERAL
) &&
388 // keep all literal characters
389 while ( i
&& (rEditMask
[i
] == EDITMASK_LITERAL
) )
391 aText
= aText
.copy( i
);
394 return ImplPatternReformat(aText
, rEditMask
, rLiteralMask
, 0);
397 static void ImplPatternProcessStrictModify( Edit
* pEdit
,
398 const OString
& rEditMask
,
399 std::u16string_view rLiteralMask
,
402 OUString aText
= pEdit
->GetText();
403 OUString aNewText
= ImplPatternProcessStrictModify(aText
,
408 if ( aNewText
== aText
)
411 // adjust selection such that it remains at the end if it was there before
412 Selection aSel
= pEdit
->GetSelection();
413 sal_Int64 nMaxSel
= std::max( aSel
.Min(), aSel
.Max() );
414 if ( nMaxSel
>= aText
.getLength() )
416 sal_Int32 nMaxPos
= aNewText
.getLength();
417 ImplPatternMaxPos(aNewText
, rEditMask
, 0, bSameMask
, nMaxSel
, nMaxPos
);
418 if ( aSel
.Min() == aSel
.Max() )
420 aSel
.Min() = nMaxPos
;
421 aSel
.Max() = aSel
.Min();
423 else if ( aSel
.Min() > aSel
.Max() )
424 aSel
.Min() = nMaxPos
;
426 aSel
.Max() = nMaxPos
;
428 pEdit
->SetText( aNewText
, aSel
);
431 static void ImplPatternProcessStrictModify( weld::Entry
& rEntry
,
432 const OString
& rEditMask
,
433 std::u16string_view rLiteralMask
,
436 OUString aText
= rEntry
.get_text();
437 OUString aNewText
= ImplPatternProcessStrictModify(aText
,
442 if (aNewText
== aText
)
445 // adjust selection such that it remains at the end if it was there before
446 int nStartPos
, nEndPos
;
447 rEntry
.get_selection_bounds(nStartPos
, nEndPos
);
449 int nMaxSel
= std::max(nStartPos
, nEndPos
);
450 if (nMaxSel
>= aText
.getLength())
452 sal_Int32 nMaxPos
= aNewText
.getLength();
453 ImplPatternMaxPos(aNewText
, rEditMask
, 0, bSameMask
, nMaxSel
, nMaxPos
);
454 if (nStartPos
== nEndPos
)
459 else if (nStartPos
> nMaxPos
)
464 rEntry
.set_text(aNewText
);
465 rEntry
.select_region(nStartPos
, nEndPos
);
468 static sal_Int32
ImplPatternLeftPos(std::string_view rEditMask
, sal_Int32 nCursorPos
)
470 // search non-literal predecessor
471 sal_Int32 nNewPos
= nCursorPos
;
472 sal_Int32 nTempPos
= nNewPos
;
475 if ( rEditMask
[nTempPos
-1] != EDITMASK_LITERAL
)
477 nNewPos
= nTempPos
-1;
485 static sal_Int32
ImplPatternRightPos( std::u16string_view rStr
, const OString
& rEditMask
,
486 sal_uInt16 nFormatFlags
, bool bSameMask
,
487 sal_Int32 nCursorPos
)
489 // search non-literal successor
490 sal_Int32 nNewPos
= nCursorPos
;
492 for(sal_Int32 nTempPos
= nNewPos
+1; nTempPos
< rEditMask
.getLength(); ++nTempPos
)
494 if ( rEditMask
[nTempPos
] != EDITMASK_LITERAL
)
500 ImplPatternMaxPos( rStr
, rEditMask
, nFormatFlags
, bSameMask
, nCursorPos
, nNewPos
);
506 class IEditImplementation
509 virtual ~IEditImplementation() {}
511 virtual OUString
GetText() const = 0;
512 virtual void SetText(const OUString
& rStr
, const Selection
& rSelection
) = 0;
514 virtual Selection
GetSelection() const = 0;
515 virtual void SetSelection(const Selection
& rSelection
) = 0;
517 virtual bool IsInsertMode() const = 0;
519 virtual void SetModified() = 0;
523 static bool ImplPatternProcessKeyInput( IEditImplementation
& rEdit
, const KeyEvent
& rKEvt
,
524 const OString
& rEditMask
,
525 std::u16string_view rLiteralMask
,
530 if ( rEditMask
.isEmpty() || !bStrictFormat
)
533 sal_uInt16 nFormatFlags
= 0;
534 Selection aOldSel
= rEdit
.GetSelection();
535 vcl::KeyCode aCode
= rKEvt
.GetKeyCode();
536 sal_Unicode cChar
= rKEvt
.GetCharCode();
537 sal_uInt16 nKeyCode
= aCode
.GetCode();
538 bool bShift
= aCode
.IsShift();
539 sal_Int32 nCursorPos
= static_cast<sal_Int32
>(aOldSel
.Max());
543 if ( nKeyCode
&& !aCode
.IsMod1() && !aCode
.IsMod2() )
545 if ( nKeyCode
== KEY_LEFT
)
547 Selection
aSel( ImplPatternLeftPos( rEditMask
, nCursorPos
) );
549 aSel
.Min() = aOldSel
.Min();
550 rEdit
.SetSelection( aSel
);
553 else if ( nKeyCode
== KEY_RIGHT
)
555 // Use the start of selection as minimum; even a small position is allowed in case that
556 // all was selected by the focus
557 Selection
aSel( aOldSel
);
559 nCursorPos
= aSel
.Min();
560 aSel
.Max() = ImplPatternRightPos( rEdit
.GetText(), rEditMask
, nFormatFlags
, bSameMask
, nCursorPos
);
562 aSel
.Min() = aOldSel
.Min();
564 aSel
.Min() = aSel
.Max();
565 rEdit
.SetSelection( aSel
);
568 else if ( nKeyCode
== KEY_HOME
)
570 // Home is the position of the first non-literal character
572 while ( (nNewPos
< rEditMask
.getLength()) &&
573 (rEditMask
[nNewPos
] == EDITMASK_LITERAL
) )
576 // Home should not move to the right
577 if ( nCursorPos
< nNewPos
)
578 nNewPos
= nCursorPos
;
579 Selection
aSel( nNewPos
);
581 aSel
.Min() = aOldSel
.Min();
582 rEdit
.SetSelection( aSel
);
585 else if ( nKeyCode
== KEY_END
)
587 // End is position of last non-literal character
588 nNewPos
= rEditMask
.getLength();
590 (rEditMask
[nNewPos
-1] == EDITMASK_LITERAL
) )
592 // Use the start of selection as minimum; even a small position is allowed in case that
593 // all was selected by the focus
594 Selection
aSel( aOldSel
);
596 nCursorPos
= static_cast<sal_Int32
>(aSel
.Min());
597 ImplPatternMaxPos( rEdit
.GetText(), rEditMask
, nFormatFlags
, bSameMask
, nCursorPos
, nNewPos
);
598 aSel
.Max() = nNewPos
;
600 aSel
.Min() = aOldSel
.Min();
602 aSel
.Min() = aSel
.Max();
603 rEdit
.SetSelection( aSel
);
606 else if ( (nKeyCode
== KEY_BACKSPACE
) || (nKeyCode
== KEY_DELETE
) )
608 OUString
aOldStr( rEdit
.GetText() );
609 OUStringBuffer
aStr( aOldStr
);
610 Selection aSel
= aOldSel
;
613 nNewPos
= static_cast<sal_Int32
>(aSel
.Min());
615 // if selection then delete it
619 aStr
.remove( static_cast<sal_Int32
>(aSel
.Min()), static_cast<sal_Int32
>(aSel
.Len()) );
622 std::u16string_view aRep
= rLiteralMask
.substr( static_cast<sal_Int32
>(aSel
.Min()), static_cast<sal_Int32
>(aSel
.Len()) );
623 aStr
.remove( aSel
.Min(), aRep
.size() );
624 aStr
.insert( aSel
.Min(), aRep
);
629 if ( nKeyCode
== KEY_BACKSPACE
)
632 nNewPos
= ImplPatternLeftPos( rEditMask
, nTempPos
);
635 nTempPos
= ImplPatternRightPos( aStr
, rEditMask
, nFormatFlags
, bSameMask
, nNewPos
);
637 if ( nNewPos
!= nTempPos
)
641 if ( rEditMask
[nNewPos
] != EDITMASK_LITERAL
)
642 aStr
.remove( nNewPos
, 1 );
646 aStr
[nNewPos
] = rLiteralMask
[nNewPos
];
651 OUString sStr
= aStr
.makeStringAndClear();
652 if ( aOldStr
!= sStr
)
655 sStr
= ImplPatternReformat( sStr
, rEditMask
, rLiteralMask
, nFormatFlags
);
657 rEdit
.SetText( sStr
, Selection( nNewPos
) );
659 rbInKeyInput
= false;
662 rEdit
.SetSelection( Selection( nNewPos
) );
666 else if ( nKeyCode
== KEY_INSERT
)
668 // you can only set InsertMode for a PatternField if the
669 // mask is equal at all input positions
677 if ( rKEvt
.GetKeyCode().IsMod2() || (cChar
< 32) || (cChar
== 127) )
680 Selection aSel
= aOldSel
;
682 nNewPos
= aSel
.Min();
684 if ( nNewPos
< rEditMask
.getLength() )
686 sal_Unicode cPattChar
= ImplPatternChar( cChar
, rEditMask
[nNewPos
] );
691 // If no valid character, check if the user wanted to jump to next literal. We do this
692 // only if we're after a character, so that literals that were skipped automatically
693 // do not influence the position anymore.
695 (rEditMask
[nNewPos
-1] != EDITMASK_LITERAL
) &&
698 // search for next character not being a literal
700 while ( nTempPos
< rEditMask
.getLength() )
702 if ( rEditMask
[nTempPos
] == EDITMASK_LITERAL
)
704 // only valid if no literal present
705 if ( (rEditMask
[nTempPos
+1] != EDITMASK_LITERAL
) &&
706 ImplCommaPointCharEqual( cChar
, rLiteralMask
[nTempPos
] ) )
709 ImplPatternMaxPos( rEdit
.GetText(), rEditMask
, nFormatFlags
, bSameMask
, nNewPos
, nTempPos
);
710 if ( nTempPos
> nNewPos
)
712 rEdit
.SetSelection( Selection( nTempPos
) );
729 OUStringBuffer
aStr(rEdit
.GetText());
731 if ( bSameMask
&& rEdit
.IsInsertMode() )
733 // crop spaces and literals at the end until current position
734 sal_Int32 n
= aStr
.getLength();
735 while ( n
&& (n
> nNewPos
) )
737 if ( (aStr
[n
-1] != ' ') &&
738 ((n
> rEditMask
.getLength()) || (rEditMask
[n
-1] != EDITMASK_LITERAL
)) )
746 aStr
.remove( aSel
.Min(), aSel
.Len() );
748 if ( aStr
.getLength() < rEditMask
.getLength() )
750 // possibly extend string until cursor position
751 if ( aStr
.getLength() < nNewPos
)
752 aStr
.append( rLiteralMask
.substr(aStr
.getLength(), nNewPos
-aStr
.getLength()) );
753 if ( nNewPos
< aStr
.getLength() )
754 aStr
.insert( cChar
, nNewPos
);
755 else if ( nNewPos
< rEditMask
.getLength() )
757 aStr
= ImplPatternReformat( aStr
.toString(), rEditMask
, rLiteralMask
, nFormatFlags
);
767 std::u16string_view aRep
= rLiteralMask
.substr( aSel
.Min(), aSel
.Len() );
768 aStr
.remove( aSel
.Min(), aRep
.size() );
769 aStr
.insert( aSel
.Min(), aRep
);
772 if ( nNewPos
< aStr
.getLength() )
773 aStr
[nNewPos
] = cChar
;
774 else if ( nNewPos
< rEditMask
.getLength() )
781 const OUString sStr
= aStr
.makeStringAndClear();
782 Selection
aNewSel( ImplPatternRightPos( sStr
, rEditMask
, nFormatFlags
, bSameMask
, nNewPos
) );
783 rEdit
.SetText( sStr
, aNewSel
);
785 rbInKeyInput
= false;
794 bool ImplSetMask(const OString
& rEditMask
, OUString
& rLiteralMask
)
796 bool bSameMask
= true;
798 if (rEditMask
.getLength() != rLiteralMask
.getLength())
800 OUStringBuffer
aBuf(rLiteralMask
);
801 if (rEditMask
.getLength() < aBuf
.getLength())
802 aBuf
.setLength(rEditMask
.getLength());
804 comphelper::string::padToLength(aBuf
, rEditMask
.getLength(), ' ');
805 rLiteralMask
= aBuf
.makeStringAndClear();
808 // Strict mode allows only the input mode if only equal characters are allowed as mask and if
809 // only spaces are specified which are not allowed by the mask
812 while ( i
< rEditMask
.getLength() )
814 char cTemp
= rEditMask
[i
];
815 if ( cTemp
!= EDITMASK_LITERAL
)
817 if ( (cTemp
== EDITMASK_ALLCHAR
) ||
818 (cTemp
== EDITMASK_UPPERALLCHAR
) ||
819 (cTemp
== EDITMASK_NUMSPACE
) )
824 if ( i
< rLiteralMask
.getLength() )
826 if ( rLiteralMask
[i
] != ' ' )
847 PatternFormatter::PatternFormatter(Edit
* pEdit
)
848 : FormatterBase(pEdit
)
851 mbInPattKeyInput
= false;
854 PatternFormatter::~PatternFormatter()
858 void PatternFormatter::SetMask( const OString
& rEditMask
,
859 const OUString
& rLiteralMask
)
861 m_aEditMask
= rEditMask
;
862 maLiteralMask
= rLiteralMask
;
863 mbSameMask
= ImplSetMask(m_aEditMask
, maLiteralMask
);
869 class EntryImplementation
: public IEditImplementation
872 EntryImplementation(weld::PatternFormatter
& rFormatter
)
873 : m_rFormatter(rFormatter
)
874 , m_rEntry(rFormatter
.get_widget())
878 virtual OUString
GetText() const override
880 return m_rEntry
.get_text();
883 virtual void SetText(const OUString
& rStr
, const Selection
& rSelection
) override
885 m_rEntry
.set_text(rStr
);
886 SetSelection(rSelection
);
889 virtual Selection
GetSelection() const override
891 int nStartPos
, nEndPos
;
892 m_rEntry
.get_selection_bounds(nStartPos
, nEndPos
);
893 return Selection(nStartPos
, nEndPos
);
896 virtual void SetSelection(const Selection
& rSelection
) override
898 auto nMin
= rSelection
.Min();
899 auto nMax
= rSelection
.Max();
900 m_rEntry
.select_region(nMin
< 0 ? 0 : nMin
, nMax
== SELECTION_MAX
? -1 : nMax
);
903 virtual bool IsInsertMode() const override
905 return !m_rEntry
.get_overwrite_mode();
908 virtual void SetModified() override
910 m_rFormatter
.Modify();
914 weld::PatternFormatter
& m_rFormatter
;
915 weld::Entry
& m_rEntry
;
921 void PatternFormatter::SetStrictFormat(bool bStrict
)
923 if (bStrict
!= m_bStrictFormat
)
925 m_bStrictFormat
= bStrict
;
931 void PatternFormatter::SetMask(const OString
& rEditMask
,
932 const OUString
& rLiteralMask
)
934 m_aEditMask
= rEditMask
;
935 m_aLiteralMask
= rLiteralMask
;
936 m_bSameMask
= ImplSetMask(m_aEditMask
, m_aLiteralMask
);
940 void PatternFormatter::ReformatAll()
942 m_rEntry
.set_text(ImplPatternReformat(m_rEntry
.get_text(), m_aEditMask
, m_aLiteralMask
, 0/*nFormatFlags*/));
943 if (!m_bSameMask
&& m_bStrictFormat
&& m_rEntry
.get_editable())
944 m_rEntry
.set_overwrite_mode(true);
947 void PatternFormatter::EntryGainFocus()
952 void PatternFormatter::EntryLostFocus()
958 void PatternFormatter::Modify()
960 if (!m_bInPattKeyInput
)
963 ImplPatternProcessStrictModify(m_rEntry
, m_aEditMask
, m_aLiteralMask
, m_bSameMask
);
967 m_aModifyHdl
.Call(m_rEntry
);
970 IMPL_LINK(PatternFormatter
, KeyInputHdl
, const KeyEvent
&, rKEvt
, bool)
972 if (m_aKeyPressHdl
.Call(rKEvt
))
974 if (rKEvt
.GetKeyCode().IsMod2())
976 EntryImplementation
aAdapt(*this);
977 return ImplPatternProcessKeyInput(aAdapt
, rKEvt
, m_aEditMask
, m_aLiteralMask
,
979 m_bSameMask
, m_bInPattKeyInput
);
983 void PatternFormatter::SetString( const OUString
& rStr
)
987 GetField()->SetText( rStr
);
988 MarkToBeReformatted( false );
992 OUString
PatternFormatter::GetString() const
997 return ImplPatternReformat( GetField()->GetText(), m_aEditMask
, maLiteralMask
, 0/*nFormatFlags*/ );
1000 void PatternFormatter::Reformat()
1004 ImplSetText( ImplPatternReformat( GetField()->GetText(), m_aEditMask
, maLiteralMask
, 0/*nFormatFlags*/ ) );
1005 if ( !mbSameMask
&& IsStrictFormat() && !GetField()->IsReadOnly() )
1006 GetField()->SetInsertMode( false );
1010 PatternField::PatternField(vcl::Window
* pParent
, WinBits nWinStyle
)
1011 : SpinField(pParent
, nWinStyle
)
1012 , PatternFormatter(this)
1017 void PatternField::dispose()
1020 SpinField::dispose();
1025 class EditImplementation
: public IEditImplementation
1028 EditImplementation(Edit
& rEdit
)
1033 virtual OUString
GetText() const override
1035 return m_rEdit
.GetText();
1038 virtual void SetText(const OUString
& rStr
, const Selection
& rSelection
) override
1040 m_rEdit
.SetText(rStr
, rSelection
);
1043 virtual Selection
GetSelection() const override
1045 return m_rEdit
.GetSelection();
1048 virtual void SetSelection(const Selection
& rSelection
) override
1050 m_rEdit
.SetSelection(rSelection
);
1053 virtual bool IsInsertMode() const override
1055 return m_rEdit
.IsInsertMode();
1058 virtual void SetModified() override
1060 m_rEdit
.SetModifyFlag();
1069 bool PatternField::PreNotify( NotifyEvent
& rNEvt
)
1071 if ( (rNEvt
.GetType() == NotifyEventType::KEYINPUT
) && !rNEvt
.GetKeyEvent()->GetKeyCode().IsMod2() )
1073 EditImplementation
aAdapt(*GetField());
1074 if ( ImplPatternProcessKeyInput( aAdapt
, *rNEvt
.GetKeyEvent(), GetEditMask(), GetLiteralMask(),
1076 ImplIsSameMask(), ImplGetInPattKeyInput() ) )
1080 return SpinField::PreNotify( rNEvt
);
1083 bool PatternField::EventNotify( NotifyEvent
& rNEvt
)
1085 if ( rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
1086 MarkToBeReformatted( false );
1087 else if ( rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
1089 if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
1093 return SpinField::EventNotify( rNEvt
);
1096 void PatternField::Modify()
1098 if ( !ImplGetInPattKeyInput() )
1100 if ( IsStrictFormat() )
1101 ImplPatternProcessStrictModify( GetField(), GetEditMask(), GetLiteralMask(), ImplIsSameMask() );
1103 MarkToBeReformatted( true );
1106 SpinField::Modify();
1109 PatternBox::PatternBox(vcl::Window
* pParent
, WinBits nWinStyle
)
1110 : ComboBox( pParent
, nWinStyle
)
1111 , PatternFormatter(this)
1116 void PatternBox::dispose()
1119 ComboBox::dispose();
1122 bool PatternBox::PreNotify( NotifyEvent
& rNEvt
)
1124 if ( (rNEvt
.GetType() == NotifyEventType::KEYINPUT
) && !rNEvt
.GetKeyEvent()->GetKeyCode().IsMod2() )
1126 EditImplementation
aAdapt(*GetField());
1127 if ( ImplPatternProcessKeyInput( aAdapt
, *rNEvt
.GetKeyEvent(), GetEditMask(), GetLiteralMask(),
1129 ImplIsSameMask(), ImplGetInPattKeyInput() ) )
1133 return ComboBox::PreNotify( rNEvt
);
1136 bool PatternBox::EventNotify( NotifyEvent
& rNEvt
)
1138 if ( rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
1139 MarkToBeReformatted( false );
1140 else if ( rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
1142 if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
1146 return ComboBox::EventNotify( rNEvt
);
1149 void PatternBox::Modify()
1151 if ( !ImplGetInPattKeyInput() )
1153 if ( IsStrictFormat() )
1154 ImplPatternProcessStrictModify( GetField(), GetEditMask(), GetLiteralMask(), ImplIsSameMask() );
1156 MarkToBeReformatted( true );
1162 void PatternBox::ReformatAll()
1165 SetUpdateMode( false );
1166 const sal_Int32 nEntryCount
= GetEntryCount();
1167 for ( sal_Int32 i
=0; i
< nEntryCount
; ++i
)
1169 aStr
= ImplPatternReformat( GetEntry( i
), GetEditMask(), GetLiteralMask(), 0/*nFormatFlags*/ );
1171 InsertEntry( aStr
, i
);
1173 PatternFormatter::Reformat();
1174 SetUpdateMode( true );
1177 static ExtDateFieldFormat
ImplGetExtFormat( LongDateOrder eOld
)
1181 case LongDateOrder::YDM
:
1182 case LongDateOrder::DMY
: return ExtDateFieldFormat::ShortDDMMYY
;
1183 case LongDateOrder::MDY
: return ExtDateFieldFormat::ShortMMDDYY
;
1184 case LongDateOrder::YMD
:
1185 default: return ExtDateFieldFormat::ShortYYMMDD
;
1189 static sal_uInt16
ImplCutNumberFromString( OUString
& rStr
)
1192 while (i1
!= rStr
.getLength() && (rStr
[i1
] < '0' || rStr
[i1
] > '9')) {
1196 while (i2
!= rStr
.getLength() && rStr
[i2
] >= '0' && rStr
[i2
] <= '9') {
1199 sal_Int32 nValue
= o3tl::toInt32(rStr
.subView(i1
, i2
-i1
));
1200 rStr
= rStr
.copy(std::min(i2
+1, rStr
.getLength()));
1204 static bool ImplCutMonthName( OUString
& rStr
, std::u16string_view _rLookupMonthName
)
1206 sal_Int32 index
= 0;
1207 rStr
= rStr
.replaceFirst(_rLookupMonthName
, "", &index
);
1211 static sal_uInt16
ImplGetMonthFromCalendarItem( OUString
& rStr
, const uno::Sequence
< i18n::CalendarItem2
>& rMonths
)
1213 const sal_uInt16 nMonths
= rMonths
.getLength();
1214 for (sal_uInt16 i
=0; i
< nMonths
; ++i
)
1217 if ( ImplCutMonthName( rStr
, rMonths
[i
].FullName
) )
1220 // short month name?
1221 if ( ImplCutMonthName( rStr
, rMonths
[i
].AbbrevName
) )
1227 static sal_uInt16
ImplCutMonthFromString( OUString
& rStr
, OUString
& rCalendarName
,
1228 const LocaleDataWrapper
& rLocaleData
, const CalendarWrapper
& rCalendarWrapper
)
1230 const OUString
aDefaultCalendarName( rCalendarWrapper
.getUniqueID());
1231 rCalendarName
= aDefaultCalendarName
;
1233 // Search for a month name of the loaded default calendar.
1234 const uno::Sequence
< i18n::CalendarItem2
> aMonths
= rCalendarWrapper
.getMonths();
1235 sal_uInt16 nMonth
= ImplGetMonthFromCalendarItem( rStr
, aMonths
);
1239 // And also possessive genitive and partitive month names.
1240 const uno::Sequence
< i18n::CalendarItem2
> aGenitiveMonths
= rCalendarWrapper
.getGenitiveMonths();
1241 if (aGenitiveMonths
!= aMonths
)
1243 nMonth
= ImplGetMonthFromCalendarItem( rStr
, aGenitiveMonths
);
1247 const uno::Sequence
< i18n::CalendarItem2
> aPartitiveMonths
= rCalendarWrapper
.getPartitiveMonths();
1248 if (aPartitiveMonths
!= aMonths
)
1250 nMonth
= ImplGetMonthFromCalendarItem( rStr
, aPartitiveMonths
);
1255 // Check if there are more calendars and try them if so, as the long date
1256 // format is obtained from the number formatter this is possible (e.g.
1257 // ar_DZ "[~hijri] ...")
1258 const uno::Sequence
< i18n::Calendar2
> aCalendars
= rLocaleData
.getAllCalendars();
1259 if (aCalendars
.getLength() > 1)
1261 for (const auto& rCalendar
: aCalendars
)
1263 if (rCalendar
.Name
!= aDefaultCalendarName
)
1265 rCalendarName
= rCalendar
.Name
;
1267 nMonth
= ImplGetMonthFromCalendarItem( rStr
, rCalendar
.Months
);
1271 if (rCalendar
.Months
!= rCalendar
.GenitiveMonths
)
1273 nMonth
= ImplGetMonthFromCalendarItem( rStr
, rCalendar
.GenitiveMonths
);
1278 if (rCalendar
.Months
!= rCalendar
.PartitiveMonths
)
1280 nMonth
= ImplGetMonthFromCalendarItem( rStr
, rCalendar
.PartitiveMonths
);
1285 rCalendarName
= aDefaultCalendarName
;
1290 return ImplCutNumberFromString( rStr
);
1293 static OUString
ImplGetDateSep( const LocaleDataWrapper
& rLocaleDataWrapper
, ExtDateFieldFormat eFormat
)
1295 if ( ( eFormat
== ExtDateFieldFormat::ShortYYMMDD_DIN5008
) || ( eFormat
== ExtDateFieldFormat::ShortYYYYMMDD_DIN5008
) )
1298 return rLocaleDataWrapper
.getDateSep();
1301 static bool ImplDateProcessKeyInput( const KeyEvent
& rKEvt
, ExtDateFieldFormat eFormat
,
1302 const LocaleDataWrapper
& rLocaleDataWrapper
)
1304 sal_Unicode cChar
= rKEvt
.GetCharCode();
1305 sal_uInt16 nGroup
= rKEvt
.GetKeyCode().GetGroup();
1306 return !((nGroup
== KEYGROUP_FKEYS
) ||
1307 (nGroup
== KEYGROUP_CURSOR
) ||
1308 (nGroup
== KEYGROUP_MISC
)||
1309 ((cChar
>= '0') && (cChar
<= '9')) ||
1310 (cChar
== ImplGetDateSep( rLocaleDataWrapper
, eFormat
)[0]));
1313 bool DateFormatter::TextToDate(const OUString
& rStr
, Date
& rDate
, ExtDateFieldFormat eDateOrder
,
1314 const LocaleDataWrapper
& rLocaleDataWrapper
, const CalendarWrapper
& rCalendarWrapper
)
1316 sal_uInt16 nDay
= 0;
1317 sal_uInt16 nMonth
= 0;
1318 sal_uInt16 nYear
= 0;
1319 bool bError
= false;
1320 OUString
aStr( rStr
);
1322 if ( eDateOrder
== ExtDateFieldFormat::SystemLong
)
1324 OUString aCalendarName
;
1325 LongDateOrder eFormat
= rLocaleDataWrapper
.getLongDateOrder();
1328 case LongDateOrder::MDY
:
1329 nMonth
= ImplCutMonthFromString( aStr
, aCalendarName
, rLocaleDataWrapper
, rCalendarWrapper
);
1330 nDay
= ImplCutNumberFromString( aStr
);
1331 nYear
= ImplCutNumberFromString( aStr
);
1333 case LongDateOrder::DMY
:
1334 nDay
= ImplCutNumberFromString( aStr
);
1335 nMonth
= ImplCutMonthFromString( aStr
, aCalendarName
, rLocaleDataWrapper
, rCalendarWrapper
);
1336 nYear
= ImplCutNumberFromString( aStr
);
1338 case LongDateOrder::YDM
:
1339 nYear
= ImplCutNumberFromString( aStr
);
1340 nDay
= ImplCutNumberFromString( aStr
);
1341 nMonth
= ImplCutMonthFromString( aStr
, aCalendarName
, rLocaleDataWrapper
, rCalendarWrapper
);
1343 case LongDateOrder::YMD
:
1345 nYear
= ImplCutNumberFromString( aStr
);
1346 nMonth
= ImplCutMonthFromString( aStr
, aCalendarName
, rLocaleDataWrapper
, rCalendarWrapper
);
1347 nDay
= ImplCutNumberFromString( aStr
);
1350 if (aCalendarName
!= "gregorian")
1352 // Calendar widget is Gregorian, convert date.
1354 bError
= !nDay
|| !nMonth
|| !nYear
;
1357 CalendarWrapper
aCW( rLocaleDataWrapper
.getComponentContext());
1358 aCW
.loadCalendar( aCalendarName
, rLocaleDataWrapper
.getLoadedLanguageTag().getLocale());
1359 aCW
.setDateTime(0.5); // get rid of current time, set some day noon
1360 aCW
.setValue( i18n::CalendarFieldIndex::DAY_OF_MONTH
, nDay
);
1361 aCW
.setValue( i18n::CalendarFieldIndex::MONTH
, nMonth
- 1);
1362 aCW
.setValue( i18n::CalendarFieldIndex::YEAR
, nYear
);
1363 bError
= !aCW
.isValid();
1366 Date aDate
= aCW
.getEpochStart() + aCW
.getDateTime();
1367 nYear
= aDate
.GetYear();
1368 nMonth
= aDate
.GetMonth();
1369 nDay
= aDate
.GetDay();
1378 // Check if year is present:
1379 OUString aDateSep
= ImplGetDateSep( rLocaleDataWrapper
, eDateOrder
);
1380 sal_Int32 nSepPos
= aStr
.indexOf( aDateSep
);
1383 nSepPos
= aStr
.indexOf( aDateSep
, nSepPos
+1 );
1384 if ( ( nSepPos
< 0 ) || ( nSepPos
== (aStr
.getLength()-1) ) )
1387 nYear
= Date( Date::SYSTEM
).GetYearUnsigned();
1390 const sal_Unicode
* pBuf
= aStr
.getStr();
1391 ImplSkipDelimiters( pBuf
);
1393 switch ( eDateOrder
)
1395 case ExtDateFieldFormat::ShortDDMMYY
:
1396 case ExtDateFieldFormat::ShortDDMMYYYY
:
1398 nDay
= ImplGetNum( pBuf
, bError
);
1399 ImplSkipDelimiters( pBuf
);
1400 nMonth
= ImplGetNum( pBuf
, bError
);
1401 ImplSkipDelimiters( pBuf
);
1403 nYear
= ImplGetNum( pBuf
, bError
);
1406 case ExtDateFieldFormat::ShortMMDDYY
:
1407 case ExtDateFieldFormat::ShortMMDDYYYY
:
1409 nMonth
= ImplGetNum( pBuf
, bError
);
1410 ImplSkipDelimiters( pBuf
);
1411 nDay
= ImplGetNum( pBuf
, bError
);
1412 ImplSkipDelimiters( pBuf
);
1414 nYear
= ImplGetNum( pBuf
, bError
);
1417 case ExtDateFieldFormat::ShortYYMMDD
:
1418 case ExtDateFieldFormat::ShortYYYYMMDD
:
1419 case ExtDateFieldFormat::ShortYYMMDD_DIN5008
:
1420 case ExtDateFieldFormat::ShortYYYYMMDD_DIN5008
:
1423 nYear
= ImplGetNum( pBuf
, bError
);
1424 ImplSkipDelimiters( pBuf
);
1425 nMonth
= ImplGetNum( pBuf
, bError
);
1426 ImplSkipDelimiters( pBuf
);
1427 nDay
= ImplGetNum( pBuf
, bError
);
1433 OSL_FAIL( "DateOrder???" );
1438 if ( bError
|| !nDay
|| !nMonth
)
1441 Date
aNewDate( nDay
, nMonth
, nYear
);
1442 DateFormatter::ExpandCentury( aNewDate
, officecfg::Office::Common::DateFormat::TwoDigitYear::get() );
1443 if ( aNewDate
.IsValidDate() )
1451 void DateFormatter::ImplDateReformat( const OUString
& rStr
, OUString
& rOutStr
)
1453 Date
aDate( Date::EMPTY
);
1454 if (!TextToDate(rStr
, aDate
, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper()))
1457 Date aTempDate
= aDate
;
1458 if ( aTempDate
> GetMax() )
1459 aTempDate
= GetMax();
1460 else if ( aTempDate
< GetMin() )
1461 aTempDate
= GetMin();
1463 rOutStr
= ImplGetDateAsText( aTempDate
);
1468 ExtDateFieldFormat
ResolveSystemFormat(ExtDateFieldFormat eDateFormat
, const LocaleDataWrapper
& rLocaleData
)
1470 if (eDateFormat
<= ExtDateFieldFormat::SystemShortYYYY
)
1472 bool bShowCentury
= (eDateFormat
== ExtDateFieldFormat::SystemShortYYYY
);
1473 switch (rLocaleData
.getDateOrder())
1475 case DateOrder::DMY
:
1476 eDateFormat
= bShowCentury
? ExtDateFieldFormat::ShortDDMMYYYY
: ExtDateFieldFormat::ShortDDMMYY
;
1478 case DateOrder::MDY
:
1479 eDateFormat
= bShowCentury
? ExtDateFieldFormat::ShortMMDDYYYY
: ExtDateFieldFormat::ShortMMDDYY
;
1482 eDateFormat
= bShowCentury
? ExtDateFieldFormat::ShortYYYYMMDD
: ExtDateFieldFormat::ShortYYMMDD
;
1489 OUString
DateFormatter::FormatDate(const Date
& rDate
, ExtDateFieldFormat eExtFormat
,
1490 const LocaleDataWrapper
& rLocaleData
,
1491 const Formatter::StaticFormatter
& rStaticFormatter
)
1493 bool bShowCentury
= false;
1496 case ExtDateFieldFormat::SystemShortYYYY
:
1497 case ExtDateFieldFormat::SystemLong
:
1498 case ExtDateFieldFormat::ShortDDMMYYYY
:
1499 case ExtDateFieldFormat::ShortMMDDYYYY
:
1500 case ExtDateFieldFormat::ShortYYYYMMDD
:
1501 case ExtDateFieldFormat::ShortYYYYMMDD_DIN5008
:
1503 bShowCentury
= true;
1508 bShowCentury
= false;
1512 if ( !bShowCentury
)
1514 // Check if I have to use force showing the century
1515 sal_uInt16 nTwoDigitYearStart
= officecfg::Office::Common::DateFormat::TwoDigitYear::get();
1516 sal_uInt16 nYear
= rDate
.GetYearUnsigned();
1518 // If year is not in double digit range
1519 if ( (nYear
< nTwoDigitYearStart
) || (nYear
>= nTwoDigitYearStart
+100) )
1520 bShowCentury
= true;
1523 sal_Unicode aBuf
[128];
1524 sal_Unicode
* pBuf
= aBuf
;
1526 eExtFormat
= ResolveSystemFormat(eExtFormat
, rLocaleData
);
1528 OUString aDateSep
= ImplGetDateSep( rLocaleData
, eExtFormat
);
1529 sal_uInt16 nDay
= rDate
.GetDay();
1530 sal_uInt16 nMonth
= rDate
.GetMonth();
1531 sal_Int16 nYear
= rDate
.GetYear();
1532 sal_uInt16 nYearLen
= bShowCentury
? 4 : 2;
1534 if ( !bShowCentury
)
1539 case ExtDateFieldFormat::SystemLong
:
1541 SvNumberFormatter
* pFormatter
= rStaticFormatter
;
1542 const LanguageTag
aFormatterLang( pFormatter
->GetLanguageTag());
1543 const sal_uInt32 nIndex
= pFormatter
->GetFormatIndex( NF_DATE_SYSTEM_LONG
,
1544 rLocaleData
.getLanguageTag().getLanguageType(false));
1547 pFormatter
->GetOutputString( rDate
- pFormatter
->GetNullDate(), nIndex
, aStr
, &pCol
);
1548 // Reset to what other uses may expect.
1549 pFormatter
->ChangeIntl( aFormatterLang
.getLanguageType(false));
1552 case ExtDateFieldFormat::ShortDDMMYY
:
1553 case ExtDateFieldFormat::ShortDDMMYYYY
:
1555 pBuf
= ImplAddNum( pBuf
, nDay
, 2 );
1556 pBuf
= ImplAddString( pBuf
, aDateSep
);
1557 pBuf
= ImplAddNum( pBuf
, nMonth
, 2 );
1558 pBuf
= ImplAddString( pBuf
, aDateSep
);
1559 pBuf
= ImplAddSNum( pBuf
, nYear
, nYearLen
);
1562 case ExtDateFieldFormat::ShortMMDDYY
:
1563 case ExtDateFieldFormat::ShortMMDDYYYY
:
1565 pBuf
= ImplAddNum( pBuf
, nMonth
, 2 );
1566 pBuf
= ImplAddString( pBuf
, aDateSep
);
1567 pBuf
= ImplAddNum( pBuf
, nDay
, 2 );
1568 pBuf
= ImplAddString( pBuf
, aDateSep
);
1569 pBuf
= ImplAddSNum( pBuf
, nYear
, nYearLen
);
1572 case ExtDateFieldFormat::ShortYYMMDD
:
1573 case ExtDateFieldFormat::ShortYYYYMMDD
:
1574 case ExtDateFieldFormat::ShortYYMMDD_DIN5008
:
1575 case ExtDateFieldFormat::ShortYYYYMMDD_DIN5008
:
1577 pBuf
= ImplAddSNum( pBuf
, nYear
, nYearLen
);
1578 pBuf
= ImplAddString( pBuf
, aDateSep
);
1579 pBuf
= ImplAddNum( pBuf
, nMonth
, 2 );
1580 pBuf
= ImplAddString( pBuf
, aDateSep
);
1581 pBuf
= ImplAddNum( pBuf
, nDay
, 2 );
1586 OSL_FAIL( "DateOrder???" );
1590 return OUString(aBuf
, pBuf
-aBuf
);
1593 OUString
DateFormatter::ImplGetDateAsText( const Date
& rDate
) const
1595 return DateFormatter::FormatDate(rDate
, GetExtDateFormat(), ImplGetLocaleDataWrapper(), maStaticFormatter
);
1598 static void ImplDateIncrementDay( Date
& rDate
, bool bUp
)
1600 DateFormatter::ExpandCentury( rDate
);
1601 rDate
.AddDays( bUp
? 1 : -1 );
1604 static void ImplDateIncrementMonth( Date
& rDate
, bool bUp
)
1606 DateFormatter::ExpandCentury( rDate
);
1607 rDate
.AddMonths( bUp
? 1 : -1 );
1610 static void ImplDateIncrementYear( Date
& rDate
, bool bUp
)
1612 DateFormatter::ExpandCentury( rDate
);
1613 rDate
.AddYears( bUp
? 1 : -1 );
1616 bool DateFormatter::ImplAllowMalformedInput() const
1618 return !IsEnforceValidValue();
1621 int DateFormatter::GetDateArea(ExtDateFieldFormat
& eFormat
, std::u16string_view rText
, int nCursor
, const LocaleDataWrapper
& rLocaleDataWrapper
)
1623 sal_Int8 nDateArea
= 0;
1625 if ( eFormat
== ExtDateFieldFormat::SystemLong
)
1627 eFormat
= ImplGetExtFormat(rLocaleDataWrapper
.getLongDateOrder());
1634 OUString aDateSep
= ImplGetDateSep(rLocaleDataWrapper
, eFormat
);
1635 for ( sal_Int8 i
= 1; i
<= 3; i
++ )
1637 nPos
= rText
.find( aDateSep
, nPos
);
1638 if (nPos
== std::u16string_view::npos
|| static_cast<sal_Int32
>(nPos
) >= nCursor
)
1651 void DateField::ImplDateSpinArea( bool bUp
)
1653 // increment days if all is selected
1657 Date
aDate( GetDate() );
1658 Selection aSelection
= GetField()->GetSelection();
1659 aSelection
.Normalize();
1660 OUString
aText( GetText() );
1661 if ( static_cast<sal_Int32
>(aSelection
.Len()) == aText
.getLength() )
1662 ImplDateIncrementDay( aDate
, bUp
);
1665 ExtDateFieldFormat eFormat
= GetExtDateFormat( true );
1666 sal_Int8 nDateArea
= GetDateArea(eFormat
, aText
, aSelection
.Max(), ImplGetLocaleDataWrapper());
1670 case ExtDateFieldFormat::ShortMMDDYY
:
1671 case ExtDateFieldFormat::ShortMMDDYYYY
:
1674 case 1: ImplDateIncrementMonth( aDate
, bUp
);
1676 case 2: ImplDateIncrementDay( aDate
, bUp
);
1678 case 3: ImplDateIncrementYear( aDate
, bUp
);
1682 case ExtDateFieldFormat::ShortDDMMYY
:
1683 case ExtDateFieldFormat::ShortDDMMYYYY
:
1686 case 1: ImplDateIncrementDay( aDate
, bUp
);
1688 case 2: ImplDateIncrementMonth( aDate
, bUp
);
1690 case 3: ImplDateIncrementYear( aDate
, bUp
);
1694 case ExtDateFieldFormat::ShortYYMMDD
:
1695 case ExtDateFieldFormat::ShortYYYYMMDD
:
1696 case ExtDateFieldFormat::ShortYYMMDD_DIN5008
:
1697 case ExtDateFieldFormat::ShortYYYYMMDD_DIN5008
:
1700 case 1: ImplDateIncrementYear( aDate
, bUp
);
1702 case 2: ImplDateIncrementMonth( aDate
, bUp
);
1704 case 3: ImplDateIncrementDay( aDate
, bUp
);
1709 OSL_FAIL( "invalid conversion" );
1714 ImplNewFieldValue( aDate
);
1717 DateFormatter::DateFormatter(Edit
* pEdit
)
1718 : FormatterBase(pEdit
)
1722 , maMax(31, 12, 2200)
1723 , mbLongFormat(false)
1724 , mbShowDateCentury(true)
1725 , mnExtDateFormat(ExtDateFieldFormat::SystemShort
)
1726 , mbEnforceValidValue(true)
1730 DateFormatter::~DateFormatter()
1734 CalendarWrapper
& DateFormatter::GetCalendarWrapper() const
1736 if (!mxCalendarWrapper
)
1738 const_cast<DateFormatter
*>(this)->mxCalendarWrapper
.reset( new CalendarWrapper( comphelper::getProcessComponentContext() ) );
1739 mxCalendarWrapper
->loadDefaultCalendar( GetLocale() );
1742 return *mxCalendarWrapper
;
1745 void DateFormatter::SetExtDateFormat( ExtDateFieldFormat eFormat
)
1747 mnExtDateFormat
= eFormat
;
1751 ExtDateFieldFormat
DateFormatter::GetExtDateFormat( bool bResolveSystemFormat
) const
1753 ExtDateFieldFormat eDateFormat
= mnExtDateFormat
;
1755 if (bResolveSystemFormat
)
1756 eDateFormat
= ResolveSystemFormat(eDateFormat
, ImplGetLocaleDataWrapper());
1761 void DateFormatter::ReformatAll()
1766 void DateFormatter::SetMin( const Date
& rNewMin
)
1769 if ( !IsEmptyFieldValue() )
1773 void DateFormatter::SetMax( const Date
& rNewMax
)
1776 if ( !IsEmptyFieldValue() )
1780 void DateFormatter::SetLongFormat( bool bLong
)
1782 mbLongFormat
= bLong
;
1784 // #91913# Remove LongFormat and DateShowCentury - redundant
1787 SetExtDateFormat( ExtDateFieldFormat::SystemLong
);
1791 if( mnExtDateFormat
== ExtDateFieldFormat::SystemLong
)
1792 SetExtDateFormat( ExtDateFieldFormat::SystemShort
);
1800 ExtDateFieldFormat
ChangeDateCentury(ExtDateFieldFormat eExtDateFormat
, bool bShowDateCentury
)
1802 // #91913# Remove LongFormat and DateShowCentury - redundant
1803 if (bShowDateCentury
)
1805 switch (eExtDateFormat
)
1807 case ExtDateFieldFormat::SystemShort
:
1808 case ExtDateFieldFormat::SystemShortYY
:
1809 eExtDateFormat
= ExtDateFieldFormat::SystemShortYYYY
; break;
1810 case ExtDateFieldFormat::ShortDDMMYY
:
1811 eExtDateFormat
= ExtDateFieldFormat::ShortDDMMYYYY
; break;
1812 case ExtDateFieldFormat::ShortMMDDYY
:
1813 eExtDateFormat
= ExtDateFieldFormat::ShortMMDDYYYY
; break;
1814 case ExtDateFieldFormat::ShortYYMMDD
:
1815 eExtDateFormat
= ExtDateFieldFormat::ShortYYYYMMDD
; break;
1816 case ExtDateFieldFormat::ShortYYMMDD_DIN5008
:
1817 eExtDateFormat
= ExtDateFieldFormat::ShortYYYYMMDD_DIN5008
; break;
1824 switch (eExtDateFormat
)
1826 case ExtDateFieldFormat::SystemShort
:
1827 case ExtDateFieldFormat::SystemShortYYYY
:
1828 eExtDateFormat
= ExtDateFieldFormat::SystemShortYY
; break;
1829 case ExtDateFieldFormat::ShortDDMMYYYY
:
1830 eExtDateFormat
= ExtDateFieldFormat::ShortDDMMYY
; break;
1831 case ExtDateFieldFormat::ShortMMDDYYYY
:
1832 eExtDateFormat
= ExtDateFieldFormat::ShortMMDDYY
; break;
1833 case ExtDateFieldFormat::ShortYYYYMMDD
:
1834 eExtDateFormat
= ExtDateFieldFormat::ShortYYMMDD
; break;
1835 case ExtDateFieldFormat::ShortYYYYMMDD_DIN5008
:
1836 eExtDateFormat
= ExtDateFieldFormat::ShortYYMMDD_DIN5008
; break;
1842 return eExtDateFormat
;
1846 void DateFormatter::SetShowDateCentury( bool bShowDateCentury
)
1848 mbShowDateCentury
= bShowDateCentury
;
1850 SetExtDateFormat(ChangeDateCentury(GetExtDateFormat(), bShowDateCentury
));
1855 void DateFormatter::SetDate( const Date
& rNewDate
)
1857 ImplSetUserDate( rNewDate
);
1858 maFieldDate
= maLastDate
;
1859 maLastDate
= GetDate();
1862 void DateFormatter::ImplSetUserDate( const Date
& rNewDate
, Selection
const * pNewSelection
)
1864 Date aNewDate
= rNewDate
;
1865 if ( aNewDate
> maMax
)
1867 else if ( aNewDate
< maMin
)
1869 maLastDate
= aNewDate
;
1872 ImplSetText( ImplGetDateAsText( aNewDate
), pNewSelection
);
1875 void DateFormatter::ImplNewFieldValue( const Date
& rDate
)
1880 Selection aSelection
= GetField()->GetSelection();
1881 aSelection
.Normalize();
1882 OUString aText
= GetField()->GetText();
1884 // If selected until the end then keep it that way
1885 if ( static_cast<sal_Int32
>(aSelection
.Max()) == aText
.getLength() )
1887 if ( !aSelection
.Len() )
1888 aSelection
.Min() = SELECTION_MAX
;
1889 aSelection
.Max() = SELECTION_MAX
;
1892 Date aOldLastDate
= maLastDate
;
1893 ImplSetUserDate( rDate
, &aSelection
);
1894 maLastDate
= aOldLastDate
;
1896 // Modify at Edit is only set at KeyInput
1897 if ( GetField()->GetText() != aText
)
1899 GetField()->SetModifyFlag();
1900 GetField()->Modify();
1904 Date
DateFormatter::GetDate() const
1906 Date
aDate( Date::EMPTY
);
1910 if (TextToDate(GetField()->GetText(), aDate
, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper()))
1912 if ( aDate
> maMax
)
1914 else if ( aDate
< maMin
)
1919 // !!! We should find out why dates are treated differently than other fields (see
1922 if ( !ImplAllowMalformedInput() )
1924 if ( maLastDate
.GetDate() )
1926 else if ( !IsEmptyFieldValueEnabled() )
1927 aDate
= Date( Date::SYSTEM
);
1930 aDate
= Date( Date::EMPTY
); // set invalid date
1937 void DateFormatter::SetEmptyDate()
1939 FormatterBase::SetEmptyFieldValue();
1942 bool DateFormatter::IsEmptyDate() const
1944 bool bEmpty
= FormatterBase::IsEmptyFieldValue();
1946 if ( GetField() && MustBeReformatted() && IsEmptyFieldValueEnabled() )
1948 if ( GetField()->GetText().isEmpty() )
1952 else if ( !maLastDate
.GetDate() )
1954 Date
aDate( Date::EMPTY
);
1955 bEmpty
= !TextToDate(GetField()->GetText(), aDate
, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper());
1961 void DateFormatter::Reformat()
1966 if ( GetField()->GetText().isEmpty() && ImplGetEmptyFieldValue() )
1970 ImplDateReformat( GetField()->GetText(), aStr
);
1972 if ( !aStr
.isEmpty() )
1974 ImplSetText( aStr
);
1975 (void)TextToDate(aStr
, maLastDate
, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper());
1979 if ( maLastDate
.GetDate() )
1980 SetDate( maLastDate
);
1981 else if ( !IsEmptyFieldValueEnabled() )
1982 SetDate( Date( Date::SYSTEM
) );
1985 ImplSetText( OUString() );
1986 SetEmptyFieldValueData( true );
1991 void DateFormatter::ExpandCentury( Date
& rDate
)
1993 ExpandCentury(rDate
, officecfg::Office::Common::DateFormat::TwoDigitYear::get());
1996 void DateFormatter::ExpandCentury( Date
& rDate
, sal_uInt16 nTwoDigitYearStart
)
1998 sal_Int16 nDateYear
= rDate
.GetYear();
1999 if ( 0 <= nDateYear
&& nDateYear
< 100 )
2001 sal_uInt16 nCentury
= nTwoDigitYearStart
/ 100;
2002 if ( nDateYear
< (nTwoDigitYearStart
% 100) )
2004 rDate
.SetYear( nDateYear
+ (nCentury
*100) );
2008 DateField::DateField( vcl::Window
* pParent
, WinBits nWinStyle
) :
2009 SpinField( pParent
, nWinStyle
),
2010 DateFormatter(this),
2011 maFirst( GetMin() ),
2014 SetText( ImplGetLocaleDataWrapper().getDate( ImplGetFieldDate() ) );
2019 void DateField::dispose()
2022 SpinField::dispose();
2025 bool DateField::PreNotify( NotifyEvent
& rNEvt
)
2027 if ( (rNEvt
.GetType() == NotifyEventType::KEYINPUT
) && IsStrictFormat() &&
2028 ( GetExtDateFormat() != ExtDateFieldFormat::SystemLong
) &&
2029 !rNEvt
.GetKeyEvent()->GetKeyCode().IsMod2() )
2031 if ( ImplDateProcessKeyInput( *rNEvt
.GetKeyEvent(), GetExtDateFormat( true ), ImplGetLocaleDataWrapper() ) )
2035 return SpinField::PreNotify( rNEvt
);
2038 bool DateField::EventNotify( NotifyEvent
& rNEvt
)
2040 if ( rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
2041 MarkToBeReformatted( false );
2042 else if ( rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
2044 if ( MustBeReformatted() )
2046 // !!! We should find out why dates are treated differently than other fields (see
2049 bool bTextLen
= !GetText().isEmpty();
2050 if ( bTextLen
|| !IsEmptyFieldValueEnabled() )
2052 if ( !ImplAllowMalformedInput() )
2056 Date
aDate( 0, 0, 0 );
2057 if (TextToDate(GetText(), aDate
, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper()))
2058 // even with strict text analysis, our text is a valid date -> do a complete
2066 SetEmptyFieldValueData( true );
2071 return SpinField::EventNotify( rNEvt
);
2074 void DateField::DataChanged( const DataChangedEvent
& rDCEvt
)
2076 SpinField::DataChanged( rDCEvt
);
2078 if ( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) && (rDCEvt
.GetFlags() & (AllSettingsFlags::LOCALE
|AllSettingsFlags::MISC
)) )
2080 if (rDCEvt
.GetFlags() & AllSettingsFlags::LOCALE
)
2081 ImplResetLocaleDataWrapper();
2086 void DateField::Modify()
2088 MarkToBeReformatted( true );
2089 SpinField::Modify();
2092 void DateField::Up()
2094 ImplDateSpinArea( true );
2098 void DateField::Down()
2100 ImplDateSpinArea( false );
2104 void DateField::First()
2106 ImplNewFieldValue( maFirst
);
2110 void DateField::Last()
2112 ImplNewFieldValue( maLast
);
2116 DateBox::DateBox(vcl::Window
* pParent
, WinBits nWinStyle
)
2117 : ComboBox( pParent
, nWinStyle
)
2118 , DateFormatter(this)
2120 SetText( ImplGetLocaleDataWrapper().getDate( ImplGetFieldDate() ) );
2124 void DateBox::dispose()
2127 ComboBox::dispose();
2130 bool DateBox::PreNotify( NotifyEvent
& rNEvt
)
2132 if ( (rNEvt
.GetType() == NotifyEventType::KEYINPUT
) && IsStrictFormat() &&
2133 ( GetExtDateFormat() != ExtDateFieldFormat::SystemLong
) &&
2134 !rNEvt
.GetKeyEvent()->GetKeyCode().IsMod2() )
2136 if ( ImplDateProcessKeyInput( *rNEvt
.GetKeyEvent(), GetExtDateFormat( true ), ImplGetLocaleDataWrapper() ) )
2140 return ComboBox::PreNotify( rNEvt
);
2143 void DateBox::DataChanged( const DataChangedEvent
& rDCEvt
)
2145 ComboBox::DataChanged( rDCEvt
);
2147 if ( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) && (rDCEvt
.GetFlags() & AllSettingsFlags::LOCALE
) )
2149 ImplResetLocaleDataWrapper();
2154 bool DateBox::EventNotify( NotifyEvent
& rNEvt
)
2156 if ( rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
2157 MarkToBeReformatted( false );
2158 else if ( rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
2160 if ( MustBeReformatted() )
2162 bool bTextLen
= !GetText().isEmpty();
2163 if ( bTextLen
|| !IsEmptyFieldValueEnabled() )
2168 SetEmptyFieldValueData( true );
2173 return ComboBox::EventNotify( rNEvt
);
2176 void DateBox::Modify()
2178 MarkToBeReformatted( true );
2182 void DateBox::ReformatAll()
2185 SetUpdateMode( false );
2186 const sal_Int32 nEntryCount
= GetEntryCount();
2187 for ( sal_Int32 i
=0; i
< nEntryCount
; ++i
)
2189 ImplDateReformat( GetEntry( i
), aStr
);
2191 InsertEntry( aStr
, i
);
2193 DateFormatter::Reformat();
2194 SetUpdateMode( true );
2199 CalendarWrapper
& DateFormatter::GetCalendarWrapper() const
2201 if (!m_xCalendarWrapper
)
2203 m_xCalendarWrapper
.reset(new CalendarWrapper(comphelper::getProcessComponentContext()));
2204 m_xCalendarWrapper
->loadDefaultCalendar(Application::GetSettings().GetLanguageTag().getLocale());
2206 return *m_xCalendarWrapper
;
2209 void DateFormatter::SetShowDateCentury(bool bShowDateCentury
)
2211 m_eFormat
= ChangeDateCentury(m_eFormat
, bShowDateCentury
);
2216 void DateFormatter::SetDate(const Date
& rDate
)
2218 auto nDate
= rDate
.GetDate();
2219 bool bForceOutput
= GetEntryText().isEmpty() && rDate
== GetDate();
2222 ImplSetValue(nDate
, true);
2228 Date
DateFormatter::GetDate()
2230 return Date(GetValue());
2233 void DateFormatter::SetMin(const Date
& rNewMin
)
2235 SetMinValue(rNewMin
.GetDate());
2238 void DateFormatter::SetMax(const Date
& rNewMax
)
2240 SetMaxValue(rNewMax
.GetDate());
2243 OUString
DateFormatter::FormatNumber(int nValue
) const
2245 const LocaleDataWrapper
& rLocaleData
= Application::GetSettings().GetLocaleDataWrapper();
2246 return ::DateFormatter::FormatDate(Date(nValue
), m_eFormat
, rLocaleData
, m_aStaticFormatter
);
2249 IMPL_LINK_NOARG(DateFormatter
, FormatOutputHdl
, LinkParamNone
*, bool)
2251 OUString sText
= FormatNumber(GetValue());
2252 ImplSetTextImpl(sText
, nullptr);
2256 IMPL_LINK(DateFormatter
, ParseInputHdl
, sal_Int64
*, result
, TriState
)
2258 const LocaleDataWrapper
& rLocaleDataWrapper
= Application::GetSettings().GetLocaleDataWrapper();
2260 Date
aResult(Date::EMPTY
);
2261 bool bRet
= ::DateFormatter::TextToDate(GetEntryText(), aResult
, ResolveSystemFormat(m_eFormat
, rLocaleDataWrapper
),
2262 rLocaleDataWrapper
, GetCalendarWrapper());
2264 *result
= aResult
.GetDate();
2266 return bRet
? TRISTATE_TRUE
: TRISTATE_FALSE
;
2270 static bool ImplTimeProcessKeyInput( const KeyEvent
& rKEvt
,
2271 bool bStrictFormat
, bool bDuration
,
2272 TimeFieldFormat eFormat
,
2273 const LocaleDataWrapper
& rLocaleDataWrapper
)
2275 sal_Unicode cChar
= rKEvt
.GetCharCode();
2277 if ( !bStrictFormat
)
2281 sal_uInt16 nGroup
= rKEvt
.GetKeyCode().GetGroup();
2282 if ( (nGroup
== KEYGROUP_FKEYS
) || (nGroup
== KEYGROUP_CURSOR
) ||
2283 (nGroup
== KEYGROUP_MISC
) ||
2284 ((cChar
>= '0') && (cChar
<= '9')) ||
2285 rLocaleDataWrapper
.getTimeSep() == OUStringChar(cChar
) ||
2286 (rLocaleDataWrapper
.getTimeAM().indexOf(cChar
) != -1) ||
2287 (rLocaleDataWrapper
.getTimePM().indexOf(cChar
) != -1) ||
2289 (cChar
== 'a') || (cChar
== 'A') || (cChar
== 'm') || (cChar
== 'M') || (cChar
== 'p') || (cChar
== 'P') ||
2290 ((eFormat
== TimeFieldFormat::F_SEC_CS
) && rLocaleDataWrapper
.getTime100SecSep() == OUStringChar(cChar
)) ||
2291 (bDuration
&& (cChar
== '-')) )
2298 static bool ImplIsOnlyDigits( const OUString
& _rStr
)
2300 const sal_Unicode
* _pChr
= _rStr
.getStr();
2301 for ( sal_Int32 i
= 0; i
< _rStr
.getLength(); ++i
, ++_pChr
)
2303 if ( *_pChr
< '0' || *_pChr
> '9' )
2309 static bool ImplIsValidTimePortion( bool _bSkipInvalidCharacters
, const OUString
& _rStr
)
2311 if ( !_bSkipInvalidCharacters
)
2313 if ( ( _rStr
.getLength() > 2 ) || _rStr
.isEmpty() || !ImplIsOnlyDigits( _rStr
) )
2319 static bool ImplCutTimePortion( OUStringBuffer
& _rStr
, sal_Int32 _nSepPos
, bool _bSkipInvalidCharacters
, short* _pPortion
)
2321 OUString
sPortion(_rStr
.subView(0, _nSepPos
));
2323 if (_nSepPos
< _rStr
.getLength())
2324 _rStr
.remove(0, _nSepPos
+ 1);
2328 if ( !ImplIsValidTimePortion( _bSkipInvalidCharacters
, sPortion
) )
2330 *_pPortion
= static_cast<short>(sPortion
.toInt32());
2334 bool TimeFormatter::TextToTime(std::u16string_view rStr
, tools::Time
& rTime
,
2335 TimeFieldFormat eFormat
,
2336 bool bDuration
, const LocaleDataWrapper
& rLocaleDataWrapper
, bool _bSkipInvalidCharacters
)
2338 OUStringBuffer
aStr(rStr
);
2342 sal_Int64 nNanoSec
= 0;
2343 tools::Time
aTime( 0, 0, 0 );
2348 // Search for separators
2349 if (!rLocaleDataWrapper
.getTimeSep().isEmpty())
2351 OUStringBuffer
aSepStr(",.;:/");
2353 aSepStr
.append('-');
2355 // Replace characters above by the separator character
2356 for (sal_Int32 i
= 0; i
< aSepStr
.getLength(); ++i
)
2358 if (rLocaleDataWrapper
.getTimeSep() == OUStringChar(aSepStr
[i
]))
2360 for ( sal_Int32 j
= 0; j
< aStr
.getLength(); j
++ )
2362 if (aStr
[j
] == aSepStr
[i
])
2363 aStr
[j
] = rLocaleDataWrapper
.getTimeSep()[0];
2368 bool bNegative
= false;
2369 sal_Int32 nSepPos
= aStr
.indexOf( rLocaleDataWrapper
.getTimeSep() );
2370 if ( aStr
[0] == '-' )
2372 if ( eFormat
!= TimeFieldFormat::F_SEC_CS
)
2375 nSepPos
= aStr
.getLength();
2376 if ( !ImplCutTimePortion( aStr
, nSepPos
, _bSkipInvalidCharacters
, &nHour
) )
2379 nSepPos
= aStr
.indexOf( rLocaleDataWrapper
.getTimeSep() );
2380 if ( !aStr
.isEmpty() && aStr
[0] == '-' )
2384 if ( !ImplCutTimePortion( aStr
, nSepPos
, _bSkipInvalidCharacters
, &nMinute
) )
2387 nSepPos
= aStr
.indexOf( rLocaleDataWrapper
.getTimeSep() );
2388 if ( !aStr
.isEmpty() && aStr
[0] == '-' )
2392 if ( !ImplCutTimePortion( aStr
, nSepPos
, _bSkipInvalidCharacters
, &nSecond
) )
2394 if ( !aStr
.isEmpty() && aStr
[0] == '-' )
2396 nNanoSec
= o3tl::toInt64(aStr
);
2399 nSecond
= static_cast<short>(o3tl::toInt32(aStr
));
2402 nMinute
= static_cast<short>(o3tl::toInt32(aStr
));
2404 else if ( nSepPos
< 0 )
2406 nSecond
= static_cast<short>(o3tl::toInt32(aStr
));
2407 nMinute
+= nSecond
/ 60;
2409 nHour
+= nMinute
/ 60;
2414 nSecond
= static_cast<short>(o3tl::toInt32(aStr
.subView( 0, nSepPos
)));
2415 aStr
.remove( 0, nSepPos
+1 );
2417 nSepPos
= aStr
.indexOf( rLocaleDataWrapper
.getTimeSep() );
2418 if ( !aStr
.isEmpty() && aStr
[0] == '-' )
2423 nSecond
= static_cast<short>(o3tl::toInt32(aStr
.subView( 0, nSepPos
)));
2424 aStr
.remove( 0, nSepPos
+1 );
2426 nSepPos
= aStr
.indexOf( rLocaleDataWrapper
.getTimeSep() );
2427 if ( !aStr
.isEmpty() && aStr
[0] == '-' )
2433 nSecond
= static_cast<short>(o3tl::toInt32(aStr
.subView( 0, nSepPos
)));
2434 aStr
.remove( 0, nSepPos
+1 );
2438 nHour
+= nMinute
/ 60;
2444 nMinute
+= nSecond
/ 60;
2446 nHour
+= nMinute
/ 60;
2449 nNanoSec
= o3tl::toInt64(aStr
);
2454 assert(aStr
.getLength() >= 1);
2456 sal_Int32 nLen
= 1; // at least one digit, otherwise nNanoSec==0
2458 while ( aStr
.getLength() > nLen
&& aStr
[nLen
] >= '0' && aStr
[nLen
] <= '9' )
2468 // round if negative?
2469 nNanoSec
= (nNanoSec
+ 5) / 10;
2474 assert(nNanoSec
> -1000000000 && nNanoSec
< 1000000000);
2475 if ( (nMinute
> 59) || (nSecond
> 59) || (nNanoSec
> 1000000000) )
2478 if ( eFormat
== TimeFieldFormat::F_NONE
)
2479 nSecond
= nNanoSec
= 0;
2480 else if ( eFormat
== TimeFieldFormat::F_SEC
)
2485 if ( bNegative
|| (nHour
< 0) || (nMinute
< 0) ||
2486 (nSecond
< 0) || (nNanoSec
< 0) )
2489 OUString aUpperCaseStr
= aStr
.toString().toAsciiUpperCase();
2490 OUString
aAMlocalised(rLocaleDataWrapper
.getTimeAM().toAsciiUpperCase());
2491 OUString
aPMlocalised(rLocaleDataWrapper
.getTimePM().toAsciiUpperCase());
2493 if ( (nHour
< 12) && ( ( aUpperCaseStr
.indexOf( "PM" ) >= 0 ) || ( aUpperCaseStr
.indexOf( aPMlocalised
) >= 0 ) ) )
2496 if ( (nHour
== 12) && ( ( aUpperCaseStr
.indexOf( "AM" ) >= 0 ) || ( aUpperCaseStr
.indexOf( aAMlocalised
) >= 0 ) ) )
2499 aTime
= tools::Time( static_cast<sal_uInt16
>(nHour
), static_cast<sal_uInt16
>(nMinute
), static_cast<sal_uInt16
>(nSecond
),
2500 static_cast<sal_uInt32
>(nNanoSec
) );
2504 assert( !bNegative
|| (nHour
< 0) || (nMinute
< 0) ||
2505 (nSecond
< 0) || (nNanoSec
< 0) );
2506 if ( bNegative
|| (nHour
< 0) || (nMinute
< 0) ||
2507 (nSecond
< 0) || (nNanoSec
< 0) )
2509 // LEM TODO: this looks weird... I think buggy when parsing "05:-02:18"
2511 nHour
= nHour
< 0 ? -nHour
: nHour
;
2512 nMinute
= nMinute
< 0 ? -nMinute
: nMinute
;
2513 nSecond
= nSecond
< 0 ? -nSecond
: nSecond
;
2514 nNanoSec
= nNanoSec
< 0 ? -nNanoSec
: nNanoSec
;
2517 aTime
= tools::Time( static_cast<sal_uInt16
>(nHour
), static_cast<sal_uInt16
>(nMinute
), static_cast<sal_uInt16
>(nSecond
),
2518 static_cast<sal_uInt32
>(nNanoSec
) );
2528 void TimeFormatter::ImplTimeReformat( std::u16string_view rStr
, OUString
& rOutStr
)
2530 tools::Time
aTime( 0, 0, 0 );
2531 if ( !TextToTime( rStr
, aTime
, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper() ) )
2534 tools::Time aTempTime
= aTime
;
2535 if ( aTempTime
> GetMax() )
2536 aTempTime
= GetMax() ;
2537 else if ( aTempTime
< GetMin() )
2538 aTempTime
= GetMin();
2540 bool bSecond
= false;
2541 bool b100Sec
= false;
2542 if ( meFormat
!= TimeFieldFormat::F_NONE
)
2545 if ( meFormat
== TimeFieldFormat::F_SEC_CS
)
2547 sal_uLong n
= aTempTime
.GetHour() * 3600L;
2548 n
+= aTempTime
.GetMin() * 60L;
2549 n
+= aTempTime
.GetSec();
2550 rOutStr
= OUString::number( n
);
2551 rOutStr
+= ImplGetLocaleDataWrapper().getTime100SecSep();
2552 std::ostringstream ostr
;
2555 ostr
<< aTempTime
.GetNanoSec();
2556 rOutStr
+= OUString::createFromAscii(ostr
.str());
2558 else if ( mbDuration
)
2559 rOutStr
= ImplGetLocaleDataWrapper().getDuration( aTempTime
, bSecond
, b100Sec
);
2562 rOutStr
= ImplGetLocaleDataWrapper().getTime( aTempTime
, bSecond
, b100Sec
);
2563 if ( GetTimeFormat() == TimeFormat::Hour12
)
2565 if ( aTempTime
.GetHour() > 12 )
2567 tools::Time
aT( aTempTime
);
2568 aT
.SetHour( aT
.GetHour() % 12 );
2569 rOutStr
= ImplGetLocaleDataWrapper().getTime( aT
, bSecond
, b100Sec
);
2571 // Don't use LocaleDataWrapper, we want AM/PM
2572 if ( aTempTime
.GetHour() < 12 )
2573 rOutStr
+= "AM"; // ImplGetLocaleDataWrapper().getTimeAM();
2575 rOutStr
+= "PM"; // ImplGetLocaleDataWrapper().getTimePM();
2580 bool TimeFormatter::ImplAllowMalformedInput() const
2582 return !IsEnforceValidValue();
2585 int TimeFormatter::GetTimeArea(TimeFieldFormat eFormat
, std::u16string_view rText
, int nCursor
,
2586 const LocaleDataWrapper
& rLocaleDataWrapper
)
2591 if (eFormat
!= TimeFieldFormat::F_SEC_CS
)
2593 //Which area is the cursor in of HH:MM:SS.TT
2594 for ( size_t i
= 1, nPos
= 0; i
<= 4; i
++ )
2596 size_t nPos1
= rText
.find(rLocaleDataWrapper
.getTimeSep(), nPos
);
2597 size_t nPos2
= rText
.find(rLocaleDataWrapper
.getTime100SecSep(), nPos
);
2598 //which ever comes first, bearing in mind that one might not be there
2599 if (nPos1
!= std::u16string_view::npos
&& nPos2
!= std::u16string_view::npos
)
2600 nPos
= std::min(nPos1
, nPos2
);
2601 else if (nPos1
!= std::u16string_view::npos
)
2605 if (nPos
== std::u16string_view::npos
|| static_cast<sal_Int32
>(nPos
) >= nCursor
)
2616 size_t nPos
= rText
.find(rLocaleDataWrapper
.getTime100SecSep());
2617 if (nPos
== std::u16string_view::npos
|| static_cast<sal_Int32
>(nPos
) >= nCursor
)
2626 tools::Time
TimeFormatter::SpinTime(bool bUp
, const tools::Time
& rTime
, TimeFieldFormat eFormat
,
2627 bool bDuration
, std::u16string_view rText
, int nCursor
,
2628 const LocaleDataWrapper
& rLocaleDataWrapper
)
2630 tools::Time
aTime(rTime
);
2632 int nTimeArea
= GetTimeArea(eFormat
, rText
, nCursor
, rLocaleDataWrapper
);
2636 tools::Time
aAddTime( 0, 0, 0 );
2637 if ( nTimeArea
== 1 )
2638 aAddTime
= tools::Time( 1, 0 );
2639 else if ( nTimeArea
== 2 )
2640 aAddTime
= tools::Time( 0, 1 );
2641 else if ( nTimeArea
== 3 )
2642 aAddTime
= tools::Time( 0, 0, 1 );
2643 else if ( nTimeArea
== 4 )
2644 aAddTime
= tools::Time( 0, 0, 0, 1 );
2647 aAddTime
= -aAddTime
;
2652 tools::Time
aAbsMaxTime( 23, 59, 59, 999999999 );
2653 if ( aTime
> aAbsMaxTime
)
2654 aTime
= aAbsMaxTime
;
2655 tools::Time
aAbsMinTime( 0, 0 );
2656 if ( aTime
< aAbsMinTime
)
2657 aTime
= aAbsMinTime
;
2664 void TimeField::ImplTimeSpinArea( bool bUp
)
2668 tools::Time
aTime( GetTime() );
2669 OUString
aText( GetText() );
2670 Selection
aSelection( GetField()->GetSelection() );
2672 aTime
= TimeFormatter::SpinTime(bUp
, aTime
, GetFormat(), IsDuration(), aText
, aSelection
.Max(), ImplGetLocaleDataWrapper());
2674 ImplNewFieldValue( aTime
);
2678 TimeFormatter::TimeFormatter(Edit
* pEdit
)
2679 : FormatterBase(pEdit
)
2682 , maMax(23, 59, 59, 999999999)
2683 , meFormat(TimeFieldFormat::F_NONE
)
2684 , mnTimeFormat(TimeFormat::Hour24
) // Should become an ExtTimeFieldFormat in next implementation, merge with mbDuration and meFormat
2686 , mbEnforceValidValue(true)
2691 TimeFormatter::~TimeFormatter()
2695 void TimeFormatter::ReformatAll()
2700 void TimeFormatter::SetMin( const tools::Time
& rNewMin
)
2703 if ( !IsEmptyFieldValue() )
2707 void TimeFormatter::SetMax( const tools::Time
& rNewMax
)
2710 if ( !IsEmptyFieldValue() )
2714 void TimeFormatter::SetTimeFormat( TimeFormat eNewFormat
)
2716 mnTimeFormat
= eNewFormat
;
2720 void TimeFormatter::SetFormat( TimeFieldFormat eNewFormat
)
2722 meFormat
= eNewFormat
;
2726 void TimeFormatter::SetDuration( bool bNewDuration
)
2728 mbDuration
= bNewDuration
;
2732 void TimeFormatter::SetTime( const tools::Time
& rNewTime
)
2734 SetUserTime( rNewTime
);
2735 maFieldTime
= maLastTime
;
2736 SetEmptyFieldValueData( false );
2739 void TimeFormatter::ImplNewFieldValue( const tools::Time
& rTime
)
2744 Selection aSelection
= GetField()->GetSelection();
2745 aSelection
.Normalize();
2746 OUString aText
= GetField()->GetText();
2748 // If selected until the end then keep it that way
2749 if ( static_cast<sal_Int32
>(aSelection
.Max()) == aText
.getLength() )
2751 if ( !aSelection
.Len() )
2752 aSelection
.Min() = SELECTION_MAX
;
2753 aSelection
.Max() = SELECTION_MAX
;
2756 tools::Time aOldLastTime
= maLastTime
;
2757 ImplSetUserTime( rTime
, &aSelection
);
2758 maLastTime
= aOldLastTime
;
2760 // Modify at Edit is only set at KeyInput
2761 if ( GetField()->GetText() != aText
)
2763 GetField()->SetModifyFlag();
2764 GetField()->Modify();
2768 OUString
TimeFormatter::FormatTime(const tools::Time
& rNewTime
, TimeFieldFormat eFormat
, TimeFormat eHourFormat
, bool bDuration
, const LocaleDataWrapper
& rLocaleData
)
2772 bool b100Sec
= false;
2773 if ( eFormat
!= TimeFieldFormat::F_NONE
)
2775 if ( eFormat
== TimeFieldFormat::F_SEC_CS
)
2777 if ( eFormat
== TimeFieldFormat::F_SEC_CS
)
2779 sal_uLong n
= rNewTime
.GetHour() * 3600L;
2780 n
+= rNewTime
.GetMin() * 60L;
2781 n
+= rNewTime
.GetSec();
2782 aStr
= OUString::number( n
) + rLocaleData
.getTime100SecSep();
2783 std::ostringstream ostr
;
2786 ostr
<< rNewTime
.GetNanoSec();
2787 aStr
+= OUString::createFromAscii(ostr
.str());
2789 else if ( bDuration
)
2791 aStr
= rLocaleData
.getDuration( rNewTime
, bSec
, b100Sec
);
2795 aStr
= rLocaleData
.getTime( rNewTime
, bSec
, b100Sec
);
2796 if ( eHourFormat
== TimeFormat::Hour12
)
2798 if ( rNewTime
.GetHour() > 12 )
2800 tools::Time
aT( rNewTime
);
2801 aT
.SetHour( aT
.GetHour() % 12 );
2802 aStr
= rLocaleData
.getTime( aT
, bSec
, b100Sec
);
2804 // Don't use LocaleDataWrapper, we want AM/PM
2805 if ( rNewTime
.GetHour() < 12 )
2806 aStr
+= "AM"; // rLocaleData.getTimeAM();
2808 aStr
+= "PM"; // rLocaleData.getTimePM();
2815 void TimeFormatter::ImplSetUserTime( const tools::Time
& rNewTime
, Selection
const * pNewSelection
)
2817 tools::Time aNewTime
= rNewTime
;
2818 if ( aNewTime
> GetMax() )
2819 aNewTime
= GetMax();
2820 else if ( aNewTime
< GetMin() )
2821 aNewTime
= GetMin();
2822 maLastTime
= aNewTime
;
2826 OUString aStr
= TimeFormatter::FormatTime(aNewTime
, meFormat
, GetTimeFormat(), mbDuration
, ImplGetLocaleDataWrapper());
2827 ImplSetText( aStr
, pNewSelection
);
2831 void TimeFormatter::SetUserTime( const tools::Time
& rNewTime
)
2833 ImplSetUserTime( rNewTime
);
2836 tools::Time
TimeFormatter::GetTime() const
2838 tools::Time
aTime( 0, 0, 0 );
2842 bool bAllowMalformed
= ImplAllowMalformedInput();
2843 if ( TextToTime( GetField()->GetText(), aTime
, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper(), !bAllowMalformed
) )
2845 if ( aTime
> GetMax() )
2847 else if ( aTime
< GetMin() )
2852 if ( bAllowMalformed
)
2853 aTime
= tools::Time( 99, 99, 99 ); // set invalid time
2862 void TimeFormatter::Reformat()
2867 if ( GetField()->GetText().isEmpty() && ImplGetEmptyFieldValue() )
2871 ImplTimeReformat( GetField()->GetText(), aStr
);
2873 if ( !aStr
.isEmpty() )
2875 ImplSetText( aStr
);
2876 (void)TextToTime(aStr
, maLastTime
, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper());
2879 SetTime( maLastTime
);
2882 TimeField::TimeField( vcl::Window
* pParent
, WinBits nWinStyle
) :
2883 SpinField( pParent
, nWinStyle
),
2884 TimeFormatter(this),
2885 maFirst( GetMin() ),
2888 SetText( ImplGetLocaleDataWrapper().getTime( maFieldTime
, false ) );
2892 void TimeField::dispose()
2895 SpinField::dispose();
2898 bool TimeField::PreNotify( NotifyEvent
& rNEvt
)
2900 if ( (rNEvt
.GetType() == NotifyEventType::KEYINPUT
) && !rNEvt
.GetKeyEvent()->GetKeyCode().IsMod2() )
2902 if ( ImplTimeProcessKeyInput( *rNEvt
.GetKeyEvent(), IsStrictFormat(), IsDuration(), GetFormat(), ImplGetLocaleDataWrapper() ) )
2906 return SpinField::PreNotify( rNEvt
);
2909 bool TimeField::EventNotify( NotifyEvent
& rNEvt
)
2911 if ( rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
2912 MarkToBeReformatted( false );
2913 else if ( rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
2915 if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
2917 if ( !ImplAllowMalformedInput() )
2921 tools::Time
aTime( 0, 0, 0 );
2922 if ( TextToTime( GetText(), aTime
, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper(), false ) )
2923 // even with strict text analysis, our text is a valid time -> do a complete
2930 return SpinField::EventNotify( rNEvt
);
2933 void TimeField::DataChanged( const DataChangedEvent
& rDCEvt
)
2935 SpinField::DataChanged( rDCEvt
);
2937 if ( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) && (rDCEvt
.GetFlags() & AllSettingsFlags::LOCALE
) )
2939 ImplResetLocaleDataWrapper();
2944 void TimeField::Modify()
2946 MarkToBeReformatted( true );
2947 SpinField::Modify();
2950 void TimeField::Up()
2952 ImplTimeSpinArea( true );
2956 void TimeField::Down()
2958 ImplTimeSpinArea( false );
2962 void TimeField::First()
2964 ImplNewFieldValue( maFirst
);
2968 void TimeField::Last()
2970 ImplNewFieldValue( maLast
);
2974 void TimeField::SetExtFormat( ExtTimeFieldFormat eFormat
)
2978 case ExtTimeFieldFormat::Short24H
:
2980 SetTimeFormat( TimeFormat::Hour24
);
2981 SetDuration( false );
2982 SetFormat( TimeFieldFormat::F_NONE
);
2985 case ExtTimeFieldFormat::Long24H
:
2987 SetTimeFormat( TimeFormat::Hour24
);
2988 SetDuration( false );
2989 SetFormat( TimeFieldFormat::F_SEC
);
2992 case ExtTimeFieldFormat::Short12H
:
2994 SetTimeFormat( TimeFormat::Hour12
);
2995 SetDuration( false );
2996 SetFormat( TimeFieldFormat::F_NONE
);
2999 case ExtTimeFieldFormat::Long12H
:
3001 SetTimeFormat( TimeFormat::Hour12
);
3002 SetDuration( false );
3003 SetFormat( TimeFieldFormat::F_SEC
);
3006 case ExtTimeFieldFormat::ShortDuration
:
3008 SetDuration( true );
3009 SetFormat( TimeFieldFormat::F_NONE
);
3012 case ExtTimeFieldFormat::LongDuration
:
3014 SetDuration( true );
3015 SetFormat( TimeFieldFormat::F_SEC
);
3018 default: OSL_FAIL( "ExtTimeFieldFormat unknown!" );
3021 if ( GetField() && !GetField()->GetText().isEmpty() )
3022 SetUserTime( GetTime() );
3026 TimeBox::TimeBox(vcl::Window
* pParent
, WinBits nWinStyle
)
3027 : ComboBox(pParent
, nWinStyle
)
3028 , TimeFormatter(this)
3030 SetText( ImplGetLocaleDataWrapper().getTime( maFieldTime
, false ) );
3034 void TimeBox::dispose()
3037 ComboBox::dispose();
3040 bool TimeBox::PreNotify( NotifyEvent
& rNEvt
)
3042 if ( (rNEvt
.GetType() == NotifyEventType::KEYINPUT
) && !rNEvt
.GetKeyEvent()->GetKeyCode().IsMod2() )
3044 if ( ImplTimeProcessKeyInput( *rNEvt
.GetKeyEvent(), IsStrictFormat(), IsDuration(), GetFormat(), ImplGetLocaleDataWrapper() ) )
3048 return ComboBox::PreNotify( rNEvt
);
3051 bool TimeBox::EventNotify( NotifyEvent
& rNEvt
)
3053 if ( rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
3054 MarkToBeReformatted( false );
3055 else if ( rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
3057 if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
3061 return ComboBox::EventNotify( rNEvt
);
3064 void TimeBox::DataChanged( const DataChangedEvent
& rDCEvt
)
3066 ComboBox::DataChanged( rDCEvt
);
3068 if ( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) && (rDCEvt
.GetFlags() & AllSettingsFlags::LOCALE
) )
3070 ImplResetLocaleDataWrapper();
3075 void TimeBox::Modify()
3077 MarkToBeReformatted( true );
3081 void TimeBox::ReformatAll()
3084 SetUpdateMode( false );
3085 const sal_Int32 nEntryCount
= GetEntryCount();
3086 for ( sal_Int32 i
=0; i
< nEntryCount
; ++i
)
3088 ImplTimeReformat( GetEntry( i
), aStr
);
3090 InsertEntry( aStr
, i
);
3092 TimeFormatter::Reformat();
3093 SetUpdateMode( true );
3098 tools::Time
TimeFormatter::ConvertValue(int nValue
)
3100 tools::Time
aTime(0);
3101 aTime
.MakeTimeFromMS(nValue
);
3105 int TimeFormatter::ConvertValue(const tools::Time
& rTime
)
3107 return rTime
.GetMSFromTime();
3110 void TimeFormatter::SetTime(const tools::Time
& rTime
)
3112 auto nTime
= ConvertValue(rTime
);
3113 bool bForceOutput
= GetEntryText().isEmpty() && rTime
== GetTime();
3116 ImplSetValue(nTime
, true);
3122 tools::Time
TimeFormatter::GetTime()
3124 return ConvertValue(GetValue());
3127 void TimeFormatter::SetMin(const tools::Time
& rNewMin
)
3129 SetMinValue(ConvertValue(rNewMin
));
3132 void TimeFormatter::SetMax(const tools::Time
& rNewMax
)
3134 SetMaxValue(ConvertValue(rNewMax
));
3137 OUString
TimeFormatter::FormatNumber(int nValue
) const
3139 const LocaleDataWrapper
& rLocaleData
= Application::GetSettings().GetLocaleDataWrapper();
3140 return ::TimeFormatter::FormatTime(ConvertValue(nValue
), m_eFormat
, m_eTimeFormat
, m_bDuration
, rLocaleData
);
3143 IMPL_LINK_NOARG(TimeFormatter
, FormatOutputHdl
, LinkParamNone
*, bool)
3145 OUString sText
= FormatNumber(GetValue());
3146 ImplSetTextImpl(sText
, nullptr);
3150 IMPL_LINK(TimeFormatter
, ParseInputHdl
, sal_Int64
*, result
, TriState
)
3152 const LocaleDataWrapper
& rLocaleDataWrapper
= Application::GetSettings().GetLocaleDataWrapper();
3154 tools::Time
aResult(0);
3155 bool bRet
= ::TimeFormatter::TextToTime(GetEntryText(), aResult
, m_eFormat
, m_bDuration
, rLocaleDataWrapper
);
3157 *result
= ConvertValue(aResult
);
3159 return bRet
? TRISTATE_TRUE
: TRISTATE_FALSE
;
3162 IMPL_LINK(TimeFormatter
, CursorChangedHdl
, weld::Entry
&, rEntry
, void)
3164 int nStartPos
, nEndPos
;
3165 rEntry
.get_selection_bounds(nStartPos
, nEndPos
);
3167 const LocaleDataWrapper
& rLocaleData
= Application::GetSettings().GetLocaleDataWrapper();
3168 const int nTimeArea
= ::TimeFormatter::GetTimeArea(m_eFormat
, GetEntryText(), nEndPos
, rLocaleData
);
3170 int nIncrements
= 1;
3173 nIncrements
= 1000 * 60 * 60;
3174 else if (nTimeArea
== 2)
3175 nIncrements
= 1000 * 60;
3176 else if (nTimeArea
== 3)
3179 SetSpinSize(nIncrements
);
3183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */