Files
libreoffice/vcl/source/control/field2.cxx

2714 lines
80 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
re-base on ALv2 code. Includes (at least) relevant parts of: linecap: Reintegrating finished LineCap feature Patch contributed by Regina Henschel http://svn.apache.org/viewvc?view=revision&revision=1232507 Patches contributed by Sven Jacobi impress212: #i81610# fixed animation export http://svn.apache.org/viewvc?view=revision&revision=1167620 impress212: drawinglayer gbuild environment changes http://svn.apache.org/viewvc?view=revision&revision=1167627 http://svn.apache.org/viewvc?view=revision&revision=1167628 impress212: DffPropSet -> minor code improvements, removing table http://svn.apache.org/viewvc?view=revision&revision=1167634 impress212: #158494# fixed excel import (text rotation) http://svn.apache.org/viewvc?view=revision&revision=1167638 Patches contributed by Armin Le Grand Svg: Reintegrated Svg replacement from /branches/alg/svgreplavement http://svn.apache.org/viewvc?view=revision&revision=1220836 #118728# changed indentifying definitions for Svg file detection http://svn.apache.org/viewvc?view=revision&revision=1229961 #118838# LineGeometry creation for complicated cases optimized to create single Polygons http://svn.apache.org/viewvc?view=revision&revision=1236232 #119176# corrected file type detection for SVG for svg files without xml header http://svn.apache.org/viewvc?view=revision&revision=1309445 #118728# Extended Svg file detection http://svn.apache.org/viewvc?view=revision&revision=1230531 #118529# solve break converters and convert commands for OLEs and images http://svn.apache.org/viewvc?view=revision&revision=1186168 svg: added WaE changes from branch svgreplacement to trunc http://svn.apache.org/viewvc?view=revision&revision=1222974 svg: corrected missing member initialization http://svn.apache.org/viewvc?view=revision&revision=1226134 fix for #118525#: Using primitives for chart sub-geometry visualisation http://svn.apache.org/viewvc?view=revision&revision=1226879 #118898# Adapted ImpGraphic::ImplGetBitmap to correctly convert metafiles to bitmapEx ... http://svn.apache.org/viewvc?view=revision&revision=1293316 fix for #118525#: removed no longer used variable maOriginalMapMode, one more exception eliminated http://svn.apache.org/viewvc?view=revision&revision=1227097 #16758# Added buffering to the VDev usages of the VclProcessor2D derivates... http://svn.apache.org/viewvc?view=revision&revision=1229521 #116758# Secured VDev buffer device to Vcl deinit http://svn.apache.org/viewvc?view=revision&revision=1230574 #116758# added remembering allocated VDevs for VDevBuffer to be able to also delete these when vcl goes down; it should never happen, but You never know http://svn.apache.org/viewvc?view=revision&revision=1230927 #118730# Changed SvgClipPathNode to use MaskPrimitive2D for primitive representation instead of TransparencePrimitive2D http://svn.apache.org/viewvc?view=revision&revision=1231198 #118822# secured 3D geometry creation (slices) by subdividing the 2D source polyPolygon early http://svn.apache.org/viewvc?view=revision&revision=1234749 #118829# enhanced Svg gradient quality, obstacles avoided http://svn.apache.org/viewvc?view=revision&revision=1235361 #118834# Unified usage of TextBreakupHelper as single tooling class for i18n text primitive breakup http://svn.apache.org/viewvc?view=revision&revision=1236110 #118853# added square pixel size limit to conversion of TransparencePrimitive2D to Metafile action http://svn.apache.org/viewvc?view=revision&revision=1237656 #118824# coreccted mirroring and boundrect when the graphicmanager is used for bitmap output http://svn.apache.org/viewvc?view=revision&revision=1240097 #115092# Corrected VclProcessor2D::RenderPolygonStrokePrimitive2D for various optimization scenarios http://svn.apache.org/viewvc?view=revision&revision=1241434 #118783# Corrected errors in ID strings, corrected Svg line/fill export, corrected polygon close state http://svn.apache.org/viewvc?view=revision&revision=1232006 #118796# corrected null-pointer usage in SVG text exporter http://svn.apache.org/viewvc?view=revision&revision=1240262 #118729# Use GraphicStreamUrl and GraphicUrl to allow multi image import with linked graphics, too http://svn.apache.org/viewvc?view=revision&revision=1229962 #118898# corrected error in GDIMetaFile::GetBoundRect in handling MetaFloatTransparentAction http://svn.apache.org/viewvc?view=revision&revision=1293349 #118855# Corrected handling of possibly created empty clipRegions after PolyPolygon clipping http://svn.apache.org/viewvc?view=revision&revision=1237725 #115962# Better (but not yet optimal, see comments in task) handling of MetaFloatTransparentAction in PDF export http://svn.apache.org/viewvc?view=revision&revision=1241078 IP clearance: #118466# This patch removes librsvg, libcroco, libgsf, ... http://svn.apache.org/viewvc?view=revision&revision=1200879 118779# Added svg content streaming in/out to ImpGraphic stream operators http://svn.apache.org/viewvc?view=revision&revision=1231908 linecap: correctons for WaE and mac drawing http://svn.apache.org/viewvc?view=revision&revision=1232793 svg: uses current system Dpi for Svg replacement image creation http://svn.apache.org/viewvc?view=revision&revision=1233948 Patches contributed by Mathias Bauer (and others) gnumake4 work variously http://svn.apache.org/viewvc?view=revision&revision=1394326 http://svn.apache.org/viewvc?view=revision&revision=1396797 http://svn.apache.org/viewvc?view=revision&revision=1397315 http://svn.apache.org/viewvc?view=revision&revision=1394326 Remove duplicate header includes. cws mba34issues01: #i117720#: convert assertion into warning http://svn.apache.org/viewvc?view=revision&revision=1172352 118485 - Styles for OLEs are not saved. Submitted by Armin Le Grand. http://svn.apache.org/viewvc?view=revision&revision=1182166 cws mba34issues01: #i117714#: remove assertion http://svn.apache.org/viewvc?view=revision&revision=1172357 Patch contributed by Jurgen Schmidt add some additional checks to ensure proper reading operations http://svn.apache.org/viewvc?view=revision&revision=1209022 mostly prefer our stream / bounds checking work. Patches contributed by Herbert Duerr #i118816# add clarifying comment regarding Font::*Color*() methods http://svn.apache.org/viewvc?view=revision&revision=1233833 extend macro->string handling for empty strings http://svn.apache.org/viewvc?view=revision&revision=1175801 avoid magic constants for SALCOLOR_NONE http://svn.apache.org/viewvc?view=revision&revision=1177543 initialize slant properly in ImplFontMetricData constructor (author=iorsh) http://svn.apache.org/viewvc?view=revision&revision=1177551 #i118675# make check for extension updates more stable http://svn.apache.org/viewvc?view=revision&revision=1214797 #a118617# remove VBasicEventListener.dll binary There are no known users depending on its CLSID http://svn.apache.org/viewvc?view=revision&revision=1203697 Patches contributed by Ariel Constenla-Haile Fix build breaker on Linux/gcc http://svn.apache.org/viewvc?view=revision&revision=1221104 Fix crash when trying to instantiate css.graphic.GraphicRasterizer_RSVG http://svn.apache.org/viewvc?view=revision&revision=1215559 Patches contributed by Oliver-Rainer Wittmann sw34bf06: #i117962# - method <SwFlyFrm::IsPaint(..)> - consider instances of <SwFlyDrawObj> http://svn.apache.org/viewvc?view=revision&revision=1172120 sw34bf06: #i117783# - Writer's implementation of XPagePrintable - apply print settings to new printing routines http://svn.apache.org/viewvc?view=revision&revision=1172115 gnumake4 work variously from Hans-Joachim Lankenau http://svn.apache.org/viewvc?view=revision&revision=1397315 http://svn.apache.org/viewvc?view=revision&revision=1396797 http://svn.apache.org/viewvc?view=revision&revision=1396782 http://svn.apache.org/viewvc?view=revision&revision=1394707 plus some amount of re-splitting of legacy headers. Patch contributed by Pavel Janik WaE: Remove unused variables. http://svn.apache.org/viewvc?view=revision&revision=1230697 Patches contributed by Takashi Ono mingwport35: i#117795: MinGW port fix for vcl2gnumake http://svn.apache.org/viewvc?view=revision&revision=1172091 mingwport35: i#117795: MinGW port fix for vcl2gnumake http://svn.apache.org/viewvc?view=revision&revision=1172091 Patch contributed by Christian Lippka impress212: #i98044# re enable Text menu for outline and title shapes http://svn.apache.org/viewvc?view=revision&revision=1167639 Patch contributed by Andre Fischer 118674: Made category B code optional and disabled by default. http://svn.apache.org/viewvc?view=revision&revision=1215131 118881: Ignore empty paragraphs after bullets. http://svn.apache.org/viewvc?view=revision&revision=1296205 Patches contributed by Philipp Lohmann ooo340fixes: #i117780# use rtl allocator http://svn.apache.org/viewvc?view=revision&revision=1172087 ooo34gsl02: #i117807# fix an off by one error (index actually inside the pfb section header) http://svn.apache.org/viewvc?view=revision&revision=1167576 various cleanups, related compilation fixes, warning cleanups, re-working of obsolete stl template pieces to use boost instead, changed string classes, re-adapt KDE about data, about dialog, fixing warnings, and other fixes & improvements. Disable svg import / render for about/ branding code-paths for now. Restore full icon theme set. Remove OS/2 conditionals and sources. Remove conflicting gtk/full-screen monitors support. Retain existing svg rasterizer files - temporarily disabled. Standardize stringificaiton and fixup dllpostfix issues. Rename SvgGradientHelper::== to equalTo to avoid overloading issues. Use the flat GdiPlus API for LineCaps calls.
2012-10-09 12:22:23 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 16:07:07 +00:00
#include "sal/config.h"
#include <algorithm>
#include <tools/diagnose_ex.h>
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <tools/rc.h>
#include <vcl/svapp.hxx>
#include <vcl/event.hxx>
#include <vcl/field.hxx>
#include <vcl/unohelp.hxx>
#include <vcl/settings.hxx>
2000-09-18 16:07:07 +00:00
#include <svdata.hxx>
#include <i18nlangtag/mslangid.hxx>
2000-09-18 16:07:07 +00:00
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/i18n/XCharacterClassification.hpp>
#include <com/sun/star/i18n/KCharacterType.hpp>
#include <unotools/localedatawrapper.hxx>
#include <unotools/calendarwrapper.hxx>
2002-09-02 12:41:36 +00:00
#include <unotools/charclass.hxx>
#include <unotools/misccfg.hxx>
2002-09-02 12:41:36 +00:00
2000-09-18 16:07:07 +00:00
using namespace ::com::sun::star;
using namespace ::comphelper;
2000-09-18 16:07:07 +00:00
#define EDITMASK_LITERAL 'L'
#define EDITMASK_ALPHA 'a'
#define EDITMASK_UPPERALPHA 'A'
#define EDITMASK_ALPHANUM 'c'
#define EDITMASK_UPPERALPHANUM 'C'
#define EDITMASK_NUM 'N'
#define EDITMASK_NUMSPACE 'n'
#define EDITMASK_ALLCHAR 'x'
#define EDITMASK_UPPERALLCHAR 'X'
uno::Reference< i18n::XCharacterClassification > ImplGetCharClass()
2000-09-18 16:07:07 +00:00
{
static uno::Reference< i18n::XCharacterClassification > xCharClass;
2000-09-18 16:07:07 +00:00
if ( !xCharClass.is() )
xCharClass = vcl::unohelper::CreateCharacterClassification();
return xCharClass;
}
static sal_Unicode* ImplAddString( sal_Unicode* pBuf, const OUString& rStr )
2001-11-05 12:06:21 +00:00
{
if ( rStr.getLength() == 1 )
*pBuf++ = rStr[0];
else if ( rStr.isEmpty() )
2001-11-05 12:06:21 +00:00
;
else
{
memcpy( pBuf, rStr.getStr(), rStr.getLength() * sizeof(sal_Unicode) );
pBuf += rStr.getLength();
2001-11-05 12:06:21 +00:00
}
return pBuf;
}
static sal_Unicode* ImplAddNum( sal_Unicode* pBuf, sal_uLong nNumber, int nMinLen )
2001-11-05 12:06:21 +00:00
{
// fill temp buffer with digits
sal_Unicode aTempBuf[30];
sal_Unicode* pTempBuf = aTempBuf;
do
{
*pTempBuf = (sal_Unicode)(nNumber % 10) + '0';
pTempBuf++;
nNumber /= 10;
if ( nMinLen )
nMinLen--;
}
while ( nNumber );
// fill with zeros up to the minimal length
while ( nMinLen > 0 )
{
*pBuf = '0';
pBuf++;
nMinLen--;
}
// copy temp buffer to real buffer
do
{
pTempBuf--;
*pBuf = *pTempBuf;
pBuf++;
}
while ( pTempBuf != aTempBuf );
return pBuf;
}
static sal_uInt16 ImplGetNum( const sal_Unicode*& rpBuf, bool& rbError )
2001-11-05 12:06:21 +00:00
{
if ( !*rpBuf )
{
rbError = true;
2001-11-05 12:06:21 +00:00
return 0;
}
sal_uInt16 nNumber = 0;
2001-11-05 12:06:21 +00:00
while( ( *rpBuf >= '0' ) && ( *rpBuf <= '9' ) )
{
nNumber *= 10;
nNumber += *rpBuf - '0';
rpBuf++;
}
return nNumber;
}
static void ImplSkipDelimiters( const sal_Unicode*& rpBuf )
{
while( ( *rpBuf == ',' ) || ( *rpBuf == '.' ) || ( *rpBuf == ';' ) ||
( *rpBuf == ':' ) || ( *rpBuf == '-' ) || ( *rpBuf == '/' ) )
{
rpBuf++;
}
}
2000-09-18 16:07:07 +00:00
static bool ImplIsPatternChar( sal_Unicode cChar, sal_Char cEditMask )
2000-09-18 16:07:07 +00:00
{
2002-09-02 12:41:36 +00:00
sal_Int32 nType = 0;
try
{
OUString aCharStr(cChar);
nType = ImplGetCharClass()->getStringType( aCharStr, 0, aCharStr.getLength(),
Application::GetSettings().GetLanguageTag().getLocale() );
2002-09-02 12:41:36 +00:00
}
catch (const ::com::sun::star::uno::Exception&)
2002-09-02 12:41:36 +00:00
{
SAL_WARN( "vcl.control", "ImplIsPatternChar: Exception caught!" );
DBG_UNHANDLED_EXCEPTION();
return false;
2002-09-02 12:41:36 +00:00
}
2000-09-18 16:07:07 +00:00
if ( (cEditMask == EDITMASK_ALPHA) || (cEditMask == EDITMASK_UPPERALPHA) )
{
2002-09-02 12:41:36 +00:00
if( !CharClass::isLetterType( nType ) )
return false;
2000-09-18 16:07:07 +00:00
}
2002-09-02 12:41:36 +00:00
else if ( cEditMask == EDITMASK_NUM )
2000-09-18 16:07:07 +00:00
{
2002-09-02 12:41:36 +00:00
if( !CharClass::isNumericType( nType ) )
return false;
2000-09-18 16:07:07 +00:00
}
2002-09-02 12:41:36 +00:00
else if ( (cEditMask == EDITMASK_ALPHANUM) || (cEditMask == EDITMASK_UPPERALPHANUM) )
2000-09-18 16:07:07 +00:00
{
2002-09-02 12:41:36 +00:00
if( !CharClass::isLetterNumericType( nType ) )
return false;
2000-09-18 16:07:07 +00:00
}
2002-09-02 12:41:36 +00:00
else if ( (cEditMask == EDITMASK_ALLCHAR) || (cEditMask == EDITMASK_UPPERALLCHAR) )
2000-09-18 16:07:07 +00:00
{
2002-09-02 12:41:36 +00:00
if ( cChar < 32 )
return false;
2000-09-18 16:07:07 +00:00
}
else if ( cEditMask == EDITMASK_NUMSPACE )
{
2002-09-02 12:41:36 +00:00
if ( !CharClass::isNumericType( nType ) && ( cChar != ' ' ) )
return false;
2000-09-18 16:07:07 +00:00
}
else
return false;
2000-09-18 16:07:07 +00:00
return true;
2000-09-18 16:07:07 +00:00
}
static sal_Unicode ImplPatternChar( sal_Unicode cChar, sal_Char cEditMask )
2000-09-18 16:07:07 +00:00
{
if ( ImplIsPatternChar( cChar, cEditMask ) )
{
if ( (cEditMask == EDITMASK_UPPERALPHA) ||
2002-09-02 12:41:36 +00:00
(cEditMask == EDITMASK_UPPERALPHANUM) ||
( cEditMask == EDITMASK_UPPERALLCHAR ) )
2000-09-18 16:07:07 +00:00
{
cChar = ImplGetCharClass()->toUpper(OUString(cChar), 0, 1,
Application::GetSettings().GetLanguageTag().getLocale())[0];
2000-09-18 16:07:07 +00:00
}
return cChar;
}
else
return 0;
}
static bool ImplCommaPointCharEqual( sal_Unicode c1, sal_Unicode c2 )
2000-09-18 16:07:07 +00:00
{
if ( c1 == c2 )
return true;
2000-09-18 16:07:07 +00:00
else if ( ((c1 == '.') || (c1 == ',')) &&
((c2 == '.') || (c2 == ',')) )
return true;
2000-09-18 16:07:07 +00:00
else
return false;
2000-09-18 16:07:07 +00:00
}
static OUString ImplPatternReformat( const OUString& rStr,
const OString& rEditMask,
const OUString& rLiteralMask,
sal_uInt16 nFormatFlags )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if (rEditMask.isEmpty())
2000-09-18 16:07:07 +00:00
return rStr;
OUString aStr = rStr;
OUStringBuffer aOutStr = OUString(rLiteralMask);
sal_Unicode cTempChar;
sal_Unicode cChar;
sal_Unicode cLiteral;
2000-09-18 16:07:07 +00:00
sal_Char cMask;
sal_Int32 nStrIndex = 0;
sal_Int32 i = 0;
sal_Int32 n;
2000-09-18 16:07:07 +00:00
2011-06-20 23:06:57 +01:00
while ( i < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
if ( nStrIndex >= aStr.getLength() )
2000-09-18 16:07:07 +00:00
break;
cChar = aStr[nStrIndex];
cLiteral = rLiteralMask[i];
2011-06-20 23:06:57 +01:00
cMask = rEditMask[i];
2000-09-18 16:07:07 +00:00
// current position is a literal
2000-09-18 16:07:07 +00:00
if ( cMask == EDITMASK_LITERAL )
{
// if it is a literal copy otherwise ignore because it might be the next valid
// character of the string
if ( ImplCommaPointCharEqual( cChar, cLiteral ) )
2000-09-18 16:07:07 +00:00
nStrIndex++;
else
{
// Otherwise we check if it is a invalid character. This is the case if it does not
// fit in the pattern of the next non-literal character.
2000-09-18 16:07:07 +00:00
n = i+1;
2011-06-20 23:06:57 +01:00
while ( n < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if ( rEditMask[n] != EDITMASK_LITERAL )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if ( !ImplIsPatternChar( cChar, rEditMask[n] ) )
2000-09-18 16:07:07 +00:00
nStrIndex++;
break;
}
n++;
}
}
}
else
{
// valid character at this position
2000-09-18 16:07:07 +00:00
cTempChar = ImplPatternChar( cChar, cMask );
if ( cTempChar )
{
// use this character
aOutStr[i] = cTempChar;
2000-09-18 16:07:07 +00:00
nStrIndex++;
}
else
{
// copy if it is a literal character
2000-09-18 16:07:07 +00:00
if ( cLiteral == cChar )
nStrIndex++;
else
{
// If the invalid character might be the next literal character then we jump
// ahead to it, otherwise we ignore it. Do only if empty literals are allowed.
2000-09-18 16:07:07 +00:00
if ( nFormatFlags & PATTERN_FORMAT_EMPTYLITERALS )
{
n = i;
2011-06-20 23:06:57 +01:00
while ( n < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if ( rEditMask[n] == EDITMASK_LITERAL )
2000-09-18 16:07:07 +00:00
{
if ( ImplCommaPointCharEqual( cChar, rLiteralMask[n] ) )
2000-09-18 16:07:07 +00:00
i = n+1;
break;
}
n++;
}
}
nStrIndex++;
continue;
}
}
}
i++;
}
return aOutStr.makeStringAndClear();
2000-09-18 16:07:07 +00:00
}
static void ImplPatternMaxPos( const OUString& rStr, const OString& rEditMask,
sal_uInt16 nFormatFlags, bool bSameMask,
sal_Int32 nCursorPos, sal_Int32& rPos )
2000-09-18 16:07:07 +00:00
{
// last position must not be longer than the contained string
sal_Int32 nMaxPos = rStr.getLength();
2000-09-18 16:07:07 +00:00
// if non empty literals are allowed ignore blanks at the end as well
2000-09-18 16:07:07 +00:00
if ( bSameMask && !(nFormatFlags & PATTERN_FORMAT_EMPTYLITERALS) )
{
while ( nMaxPos )
{
2011-06-20 23:06:57 +01:00
if ( (rEditMask[nMaxPos-1] != EDITMASK_LITERAL) &&
(rStr[nMaxPos-1] != ' ') )
2000-09-18 16:07:07 +00:00
break;
nMaxPos--;
}
// if we are in front of a literal, continue search until first character after the literal
sal_Int32 nTempPos = nMaxPos;
2011-06-20 23:06:57 +01:00
while ( nTempPos < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if ( rEditMask[nTempPos] != EDITMASK_LITERAL )
2000-09-18 16:07:07 +00:00
{
nMaxPos = nTempPos;
break;
}
nTempPos++;
}
}
if ( rPos > nMaxPos )
rPos = nMaxPos;
// character should not move left
2000-09-18 16:07:07 +00:00
if ( rPos < nCursorPos )
rPos = nCursorPos;
}
static void ImplPatternProcessStrictModify( Edit* pEdit,
const OString& rEditMask,
const OUString& rLiteralMask,
sal_uInt16 nFormatFlags, bool bSameMask )
2000-09-18 16:07:07 +00:00
{
OUString aText = pEdit->GetText();
2000-09-18 16:07:07 +00:00
// remove leading blanks
2000-09-18 16:07:07 +00:00
if ( bSameMask && !(nFormatFlags & PATTERN_FORMAT_EMPTYLITERALS) )
{
sal_Int32 i = 0;
sal_Int32 nMaxLen = aText.getLength();
2000-09-18 16:07:07 +00:00
while ( i < nMaxLen )
{
2011-06-20 23:06:57 +01:00
if ( (rEditMask[i] != EDITMASK_LITERAL) &&
(aText[i] != ' ') )
2000-09-18 16:07:07 +00:00
break;
i++;
}
// keep all literal characters
2011-06-20 23:06:57 +01:00
while ( i && (rEditMask[i] == EDITMASK_LITERAL) )
2000-09-18 16:07:07 +00:00
i--;
aText = aText.copy( i );
2000-09-18 16:07:07 +00:00
}
OUString aNewText = ImplPatternReformat( aText, rEditMask, rLiteralMask, nFormatFlags );
2000-09-18 16:07:07 +00:00
if ( aNewText != aText )
{
// adjust selection such that it remains at the end if it was there before
2000-09-18 16:07:07 +00:00
Selection aSel = pEdit->GetSelection();
sal_Int64 nMaxSel = std::max( aSel.Min(), aSel.Max() );
if ( nMaxSel >= aText.getLength() )
2000-09-18 16:07:07 +00:00
{
sal_Int32 nMaxPos = aNewText.getLength();
ImplPatternMaxPos( aNewText, rEditMask, nFormatFlags, bSameMask, nMaxSel, nMaxPos );
2000-09-18 16:07:07 +00:00
if ( aSel.Min() == aSel.Max() )
{
aSel.Min() = nMaxPos;
aSel.Max() = aSel.Min();
}
else if ( aSel.Min() > aSel.Max() )
aSel.Min() = nMaxPos;
else
aSel.Max() = nMaxPos;
}
pEdit->SetText( aNewText, aSel );
}
}
static sal_Int32 ImplPatternLeftPos(const OString& rEditMask, sal_Int32 nCursorPos)
2000-09-18 16:07:07 +00:00
{
// search non-literal predecessor
sal_Int32 nNewPos = nCursorPos;
sal_Int32 nTempPos = nNewPos;
2000-09-18 16:07:07 +00:00
while ( nTempPos )
{
2011-06-20 23:06:57 +01:00
if ( rEditMask[nTempPos-1] != EDITMASK_LITERAL )
2000-09-18 16:07:07 +00:00
{
nNewPos = nTempPos-1;
break;
}
nTempPos--;
}
return nNewPos;
}
static sal_Int32 ImplPatternRightPos( const OUString& rStr, const OString& rEditMask,
sal_uInt16 nFormatFlags, bool bSameMask,
sal_Int32 nCursorPos )
2000-09-18 16:07:07 +00:00
{
// search non-literal successor
sal_Int32 nNewPos = nCursorPos;
sal_Int32 nTempPos = nNewPos;
2011-06-20 23:06:57 +01:00
while ( nTempPos < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if ( rEditMask[nTempPos+1] != EDITMASK_LITERAL )
2000-09-18 16:07:07 +00:00
{
nNewPos = nTempPos+1;
break;
}
nTempPos++;
}
ImplPatternMaxPos( rStr, rEditMask, nFormatFlags, bSameMask, nCursorPos, nNewPos );
return nNewPos;
}
static bool ImplPatternProcessKeyInput( Edit* pEdit, const KeyEvent& rKEvt,
const OString& rEditMask,
const OUString& rLiteralMask,
bool bStrictFormat,
sal_uInt16 nFormatFlags,
bool bSameMask,
bool& rbInKeyInput )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if ( rEditMask.isEmpty() || !bStrictFormat )
return false;
2000-09-18 16:07:07 +00:00
Selection aOldSel = pEdit->GetSelection();
vcl::KeyCode aCode = rKEvt.GetKeyCode();
sal_Unicode cChar = rKEvt.GetCharCode();
sal_uInt16 nKeyCode = aCode.GetCode();
bool bShift = aCode.IsShift();
sal_Int32 nCursorPos = static_cast<sal_Int32>(aOldSel.Max());
sal_Int32 nNewPos;
sal_Int32 nTempPos;
2000-09-18 16:07:07 +00:00
if ( nKeyCode && !aCode.IsMod1() && !aCode.IsMod2() )
{
if ( nKeyCode == KEY_LEFT )
{
Selection aSel( ImplPatternLeftPos( rEditMask, nCursorPos ) );
if ( bShift )
aSel.Min() = aOldSel.Min();
pEdit->SetSelection( aSel );
return true;
2000-09-18 16:07:07 +00:00
}
else if ( nKeyCode == KEY_RIGHT )
{
// Use the start of selection as minimum; even a small position is allowed in case that
// all was selected by the focus
2000-09-18 16:07:07 +00:00
Selection aSel( aOldSel );
aSel.Justify();
nCursorPos = aSel.Min();
2000-09-18 16:07:07 +00:00
aSel.Max() = ImplPatternRightPos( pEdit->GetText(), rEditMask, nFormatFlags, bSameMask, nCursorPos );
if ( bShift )
aSel.Min() = aOldSel.Min();
else
aSel.Min() = aSel.Max();
pEdit->SetSelection( aSel );
return true;
2000-09-18 16:07:07 +00:00
}
else if ( nKeyCode == KEY_HOME )
{
// Home is the position of the first non-literal character
2000-09-18 16:07:07 +00:00
nNewPos = 0;
2011-06-20 23:06:57 +01:00
while ( (nNewPos < rEditMask.getLength()) &&
(rEditMask[nNewPos] == EDITMASK_LITERAL) )
2000-09-18 16:07:07 +00:00
nNewPos++;
// Home should not move to the right
2000-09-18 16:07:07 +00:00
if ( nCursorPos < nNewPos )
nNewPos = nCursorPos;
Selection aSel( nNewPos );
if ( bShift )
aSel.Min() = aOldSel.Min();
pEdit->SetSelection( aSel );
return true;
2000-09-18 16:07:07 +00:00
}
else if ( nKeyCode == KEY_END )
{
// End is position of last non-literal character
2011-06-20 23:06:57 +01:00
nNewPos = rEditMask.getLength();
2000-09-18 16:07:07 +00:00
while ( nNewPos &&
2011-06-20 23:06:57 +01:00
(rEditMask[nNewPos-1] == EDITMASK_LITERAL) )
2000-09-18 16:07:07 +00:00
nNewPos--;
// Use the start of selection as minimum; even a small position is allowed in case that
// all was selected by the focus
2000-09-18 16:07:07 +00:00
Selection aSel( aOldSel );
aSel.Justify();
nCursorPos = static_cast<sal_Int32>(aSel.Min());
2000-09-18 16:07:07 +00:00
ImplPatternMaxPos( pEdit->GetText(), rEditMask, nFormatFlags, bSameMask, nCursorPos, nNewPos );
aSel.Max() = nNewPos;
if ( bShift )
aSel.Min() = aOldSel.Min();
else
aSel.Min() = aSel.Max();
pEdit->SetSelection( aSel );
return true;
2000-09-18 16:07:07 +00:00
}
else if ( (nKeyCode == KEY_BACKSPACE) || (nKeyCode == KEY_DELETE) )
{
OUString aOldStr( pEdit->GetText() );
OUStringBuffer aStr( aOldStr );
2000-09-18 16:07:07 +00:00
Selection aSel = aOldSel;
aSel.Justify();
nNewPos = static_cast<sal_Int32>(aSel.Min());
2000-09-18 16:07:07 +00:00
// if selection then delete it
2000-09-18 16:07:07 +00:00
if ( aSel.Len() )
{
if ( bSameMask )
aStr.remove( static_cast<sal_Int32>(aSel.Min()), static_cast<sal_Int32>(aSel.Len()) );
2000-09-18 16:07:07 +00:00
else
{
OUString aRep = rLiteralMask.copy( static_cast<sal_Int32>(aSel.Min()), static_cast<sal_Int32>(aSel.Len()) );
aStr.remove( aSel.Min(), aRep.getLength() );
aStr.insert( aSel.Min(), aRep );
2000-09-18 16:07:07 +00:00
}
}
else
{
if ( nKeyCode == KEY_BACKSPACE )
{
nTempPos = nNewPos;
nNewPos = ImplPatternLeftPos( rEditMask, nTempPos );
}
else
nTempPos = ImplPatternRightPos( aStr.toString(), rEditMask, nFormatFlags, bSameMask, nNewPos );
2000-09-18 16:07:07 +00:00
if ( nNewPos != nTempPos )
{
if ( bSameMask )
{
2011-06-20 23:06:57 +01:00
if ( rEditMask[nNewPos] != EDITMASK_LITERAL )
aStr.remove( nNewPos, 1 );
2000-09-18 16:07:07 +00:00
}
else
{
aStr[nNewPos] = rLiteralMask[nNewPos];
2000-09-18 16:07:07 +00:00
}
}
}
if ( aOldStr != aStr.toString() )
2000-09-18 16:07:07 +00:00
{
if ( bSameMask )
aStr = ImplPatternReformat( aStr.toString(), rEditMask, rLiteralMask, nFormatFlags );
rbInKeyInput = true;
pEdit->SetText( aStr.toString(), Selection( nNewPos ) );
2000-09-18 16:07:07 +00:00
pEdit->SetModifyFlag();
pEdit->Modify();
rbInKeyInput = false;
2000-09-18 16:07:07 +00:00
}
else
pEdit->SetSelection( Selection( nNewPos ) );
return true;
2000-09-18 16:07:07 +00:00
}
else if ( nKeyCode == KEY_INSERT )
{
// you can only set InsertModus for a PatternField if the
// mask is equal at all input positions
2000-09-18 16:07:07 +00:00
if ( !bSameMask )
{
return true;
2000-09-18 16:07:07 +00:00
}
}
}
if ( rKEvt.GetKeyCode().IsMod2() || (cChar < 32) || (cChar == 127) )
return false;
2000-09-18 16:07:07 +00:00
Selection aSel = aOldSel;
aSel.Justify();
nNewPos = aSel.Min();
2000-09-18 16:07:07 +00:00
2011-06-20 23:06:57 +01:00
if ( nNewPos < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
sal_Unicode cPattChar = ImplPatternChar( cChar, rEditMask[nNewPos] );
2000-09-18 16:07:07 +00:00
if ( cPattChar )
cChar = cPattChar;
else
{
// If no valid character, check if the user wanted to jump to next literal. We do this
// only if we're after a character, so that literals that were skipped automatically
// do not influence the position anymore.
2000-09-18 16:07:07 +00:00
if ( nNewPos &&
2011-06-20 23:06:57 +01:00
(rEditMask[nNewPos-1] != EDITMASK_LITERAL) &&
2000-09-18 16:07:07 +00:00
!aSel.Len() )
{
// search for next character not being a literal
2000-09-18 16:07:07 +00:00
nTempPos = nNewPos;
2011-06-20 23:06:57 +01:00
while ( nTempPos < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
if ( rEditMask[nTempPos] == EDITMASK_LITERAL )
2000-09-18 16:07:07 +00:00
{
// only valid if no literal present
2011-06-20 23:06:57 +01:00
if ( (rEditMask[nTempPos+1] != EDITMASK_LITERAL ) &&
ImplCommaPointCharEqual( cChar, rLiteralMask[nTempPos] ) )
2000-09-18 16:07:07 +00:00
{
nTempPos++;
ImplPatternMaxPos( pEdit->GetText(), rEditMask, nFormatFlags, bSameMask, nNewPos, nTempPos );
if ( nTempPos > nNewPos )
{
pEdit->SetSelection( Selection( nTempPos ) );
return true;
2000-09-18 16:07:07 +00:00
}
}
break;
}
nTempPos++;
}
}
cChar = 0;
}
}
else
cChar = 0;
if ( cChar )
{
OUStringBuffer aStr = pEdit->GetText();
bool bError = false;
2000-09-18 16:07:07 +00:00
if ( bSameMask && pEdit->IsInsertMode() )
{
// crop spaces and literals at the end until current position
sal_Int32 n = aStr.getLength();
2000-09-18 16:07:07 +00:00
while ( n && (n > nNewPos) )
{
if ( (aStr[n-1] != ' ') &&
2011-06-20 23:06:57 +01:00
((n > rEditMask.getLength()) || (rEditMask[n-1] != EDITMASK_LITERAL)) )
2000-09-18 16:07:07 +00:00
break;
n--;
}
aStr.truncate( n );
2000-09-18 16:07:07 +00:00
if ( aSel.Len() )
aStr.remove( aSel.Min(), aSel.Len() );
2000-09-18 16:07:07 +00:00
if ( aStr.getLength() < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
// possibly extend string until cursor position
if ( aStr.getLength() < nNewPos )
aStr.append( rLiteralMask.copy( aStr.getLength(), nNewPos-aStr.getLength() ));
if ( nNewPos < aStr.getLength() )
aStr.insert( cChar, nNewPos );
2011-06-20 23:06:57 +01:00
else if ( nNewPos < rEditMask.getLength() )
aStr.append(cChar);
aStr = ImplPatternReformat( aStr.toString(), rEditMask, rLiteralMask, nFormatFlags );
2000-09-18 16:07:07 +00:00
}
else
bError = true;
2000-09-18 16:07:07 +00:00
}
else
{
if ( aSel.Len() )
{
// delete selection
OUString aRep = rLiteralMask.copy( aSel.Min(), aSel.Len() );
aStr.remove( aSel.Min(), aRep.getLength() );
aStr.insert( aSel.Min(), aRep );
2000-09-18 16:07:07 +00:00
}
if ( nNewPos < aStr.getLength() )
aStr[nNewPos] = cChar;
2011-06-20 23:06:57 +01:00
else if ( nNewPos < rEditMask.getLength() )
aStr.append(cChar);
2000-09-18 16:07:07 +00:00
}
if ( !bError )
2000-09-18 16:07:07 +00:00
{
rbInKeyInput = true;
Selection aNewSel( ImplPatternRightPos( aStr.toString(), rEditMask, nFormatFlags, bSameMask, nNewPos ) );
pEdit->SetText( aStr.toString(), aNewSel );
2000-09-18 16:07:07 +00:00
pEdit->SetModifyFlag();
pEdit->Modify();
rbInKeyInput = false;
2000-09-18 16:07:07 +00:00
}
}
return true;
2000-09-18 16:07:07 +00:00
}
void PatternFormatter::ImplSetMask(const OString& rEditMask, const OUString& rLiteralMask)
2000-09-18 16:07:07 +00:00
{
m_aEditMask = rEditMask;
2000-09-18 16:07:07 +00:00
maLiteralMask = rLiteralMask;
mbSameMask = true;
2000-09-18 16:07:07 +00:00
if ( m_aEditMask.getLength() != maLiteralMask.getLength() )
2000-09-18 16:07:07 +00:00
{
OUStringBuffer aBuf(maLiteralMask);
if (m_aEditMask.getLength() < aBuf.getLength())
aBuf.remove(m_aEditMask.getLength(), aBuf.getLength() - m_aEditMask.getLength());
2000-09-18 16:07:07 +00:00
else
comphelper::string::padToLength(aBuf, m_aEditMask.getLength(), ' ');
maLiteralMask = aBuf.makeStringAndClear();
2000-09-18 16:07:07 +00:00
}
// Strict mode allows only the input mode if only equal characters are allowed as mask and if
// only spaces are specified which are not allowed by the mask
sal_Int32 i = 0;
2000-09-18 16:07:07 +00:00
sal_Char c = 0;
2011-06-20 23:06:57 +01:00
while ( i < rEditMask.getLength() )
2000-09-18 16:07:07 +00:00
{
2011-06-20 23:06:57 +01:00
sal_Char cTemp = rEditMask[i];
2000-09-18 16:07:07 +00:00
if ( cTemp != EDITMASK_LITERAL )
{
if ( (cTemp == EDITMASK_ALLCHAR) ||
(cTemp == EDITMASK_UPPERALLCHAR) ||
(cTemp == EDITMASK_NUMSPACE) )
{
mbSameMask = false;
2000-09-18 16:07:07 +00:00
break;
}
if ( i < rLiteralMask.getLength() )
2000-09-18 16:07:07 +00:00
{
if ( rLiteralMask[i] != ' ' )
2000-09-18 16:07:07 +00:00
{
mbSameMask = false;
2000-09-18 16:07:07 +00:00
break;
}
}
if ( !c )
c = cTemp;
if ( cTemp != c )
{
mbSameMask = false;
2000-09-18 16:07:07 +00:00
break;
}
}
i++;
}
}
PatternFormatter::PatternFormatter()
{
mnFormatFlags = 0;
mbSameMask = true;
mbInPattKeyInput = false;
2000-09-18 16:07:07 +00:00
}
PatternFormatter::~PatternFormatter()
{
}
void PatternFormatter::SetMask( const OString& rEditMask,
const OUString& rLiteralMask )
2000-09-18 16:07:07 +00:00
{
ImplSetMask( rEditMask, rLiteralMask );
ReformatAll();
}
void PatternFormatter::SetString( const OUString& rStr )
2000-09-18 16:07:07 +00:00
{
maFieldString = rStr;
if ( GetField() )
{
GetField()->SetText( rStr );
MarkToBeReformatted( false );
2000-09-18 16:07:07 +00:00
}
}
OUString PatternFormatter::GetString() const
2000-09-18 16:07:07 +00:00
{
if ( !GetField() )
return OUString();
2000-09-18 16:07:07 +00:00
else
2011-06-20 23:06:57 +01:00
return ImplPatternReformat( GetField()->GetText(), m_aEditMask, maLiteralMask, mnFormatFlags );
2000-09-18 16:07:07 +00:00
}
void PatternFormatter::Reformat()
{
if ( GetField() )
{
2011-06-20 23:06:57 +01:00
ImplSetText( ImplPatternReformat( GetField()->GetText(), m_aEditMask, maLiteralMask, mnFormatFlags ) );
2000-09-18 16:07:07 +00:00
if ( !mbSameMask && IsStrictFormat() && !GetField()->IsReadOnly() )
GetField()->SetInsertMode( false );
2000-09-18 16:07:07 +00:00
}
}
PatternField::PatternField( vcl::Window* pParent, WinBits nWinStyle ) :
2000-09-18 16:07:07 +00:00
SpinField( pParent, nWinStyle )
{
SetField( this );
Reformat();
}
bool PatternField::PreNotify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2000-09-18 16:07:07 +00:00
{
if ( ImplPatternProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), GetEditMask(), GetLiteralMask(),
IsStrictFormat(), GetFormatFlags(),
ImplIsSameMask(), ImplGetInPattKeyInput() ) )
return true;
2000-09-18 16:07:07 +00:00
}
return SpinField::PreNotify( rNEvt );
}
bool PatternField::Notify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
MarkToBeReformatted( false );
else if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
2000-09-18 16:07:07 +00:00
{
if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
2000-09-18 16:07:07 +00:00
Reformat();
}
return SpinField::Notify( rNEvt );
}
void PatternField::Modify()
{
if ( !ImplGetInPattKeyInput() )
{
if ( IsStrictFormat() )
ImplPatternProcessStrictModify( GetField(), GetEditMask(), GetLiteralMask(), GetFormatFlags(), ImplIsSameMask() );
else
MarkToBeReformatted( true );
2000-09-18 16:07:07 +00:00
}
SpinField::Modify();
}
PatternBox::PatternBox( vcl::Window* pParent, WinBits nWinStyle ) :
2000-09-18 16:07:07 +00:00
ComboBox( pParent, nWinStyle )
{
SetField( this );
Reformat();
}
bool PatternBox::PreNotify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2000-09-18 16:07:07 +00:00
{
if ( ImplPatternProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), GetEditMask(), GetLiteralMask(),
IsStrictFormat(), GetFormatFlags(),
ImplIsSameMask(), ImplGetInPattKeyInput() ) )
return true;
2000-09-18 16:07:07 +00:00
}
return ComboBox::PreNotify( rNEvt );
}
bool PatternBox::Notify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
MarkToBeReformatted( false );
else if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
2000-09-18 16:07:07 +00:00
{
if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
2000-09-18 16:07:07 +00:00
Reformat();
}
return ComboBox::Notify( rNEvt );
}
void PatternBox::Modify()
{
if ( !ImplGetInPattKeyInput() )
{
if ( IsStrictFormat() )
ImplPatternProcessStrictModify( GetField(), GetEditMask(), GetLiteralMask(), GetFormatFlags(), ImplIsSameMask() );
else
MarkToBeReformatted( true );
2000-09-18 16:07:07 +00:00
}
ComboBox::Modify();
}
void PatternBox::ReformatAll()
{
OUString aStr;
SetUpdateMode( false );
sal_uInt16 nEntryCount = GetEntryCount();
for ( sal_uInt16 i=0; i < nEntryCount; i++ )
2000-09-18 16:07:07 +00:00
{
aStr = ImplPatternReformat( GetEntry( i ), GetEditMask(), GetLiteralMask(), GetFormatFlags() );
RemoveEntryAt(i);
2000-09-18 16:07:07 +00:00
InsertEntry( aStr, i );
}
PatternFormatter::Reformat();
SetUpdateMode( true );
2000-09-18 16:07:07 +00:00
}
2001-11-05 12:06:21 +00:00
static ExtDateFieldFormat ImplGetExtFormat( DateFormat eOld )
2000-09-18 16:07:07 +00:00
{
2001-11-05 12:06:21 +00:00
switch( eOld )
2000-09-18 16:07:07 +00:00
{
2001-11-05 12:06:21 +00:00
case DMY: return XTDATEF_SHORT_DDMMYY;
case MDY: return XTDATEF_SHORT_MMDDYY;
default: return XTDATEF_SHORT_YYMMDD;
2000-09-18 16:07:07 +00:00
}
}
static sal_uInt16 ImplCutNumberFromString( OUString& rStr )
{
sal_Int32 i1 = 0;
while (i1 != rStr.getLength() && !(rStr[i1] >= '0' && rStr[i1] <= '9')) {
++i1;
}
sal_Int32 i2 = i1;
while (i2 != rStr.getLength() && rStr[i2] >= '0' && rStr[i2] <= '9') {
++i2;
2000-09-18 16:07:07 +00:00
}
sal_Int32 nValue = rStr.copy(i1, i2-i1).toInt32();
rStr = rStr.copy(std::min(i2+1, rStr.getLength()));
return nValue;
2000-09-18 16:07:07 +00:00
}
static bool ImplCutMonthName( OUString& rStr, const OUString& _rLookupMonthName )
2000-09-18 16:07:07 +00:00
{
sal_Int32 index = 0;
rStr = rStr.replaceFirst(_rLookupMonthName, OUString(), &index);
return index >= 0;
}
static sal_uInt16 ImplCutMonthFromString( OUString& rStr, const CalendarWrapper& rCalendarWrapper )
{
// search for a month' name
for ( sal_uInt16 i=1; i <= 12; i++ )
{
OUString aMonthName = rCalendarWrapper.getMonths()[i-1].FullName;
// long month name?
if ( ImplCutMonthName( rStr, aMonthName ) )
return i;
// short month name?
OUString aAbbrevMonthName = rCalendarWrapper.getMonths()[i-1].AbbrevName;
if ( ImplCutMonthName( rStr, aAbbrevMonthName ) )
return i;
}
return ImplCutNumberFromString( rStr );
2000-09-18 16:07:07 +00:00
}
static OUString ImplGetDateSep( const LocaleDataWrapper& rLocaleDataWrapper, ExtDateFieldFormat eFormat )
2001-11-05 12:06:21 +00:00
{
if ( ( eFormat == XTDATEF_SHORT_YYMMDD_DIN5008 ) || ( eFormat == XTDATEF_SHORT_YYYYMMDD_DIN5008 ) )
return OUString("-");
else
return rLocaleDataWrapper.getDateSep();
2001-11-05 12:06:21 +00:00
}
static bool ImplDateProcessKeyInput( Edit*, const KeyEvent& rKEvt, ExtDateFieldFormat eFormat,
const LocaleDataWrapper& rLocaleDataWrapper )
2000-09-18 16:07:07 +00:00
{
sal_Unicode cChar = rKEvt.GetCharCode();
sal_uInt16 nGroup = rKEvt.GetKeyCode().GetGroup();
2001-11-05 12:06:21 +00:00
if ( (nGroup == KEYGROUP_FKEYS) || (nGroup == KEYGROUP_CURSOR) ||
(nGroup == KEYGROUP_MISC)||
((cChar >= '0') && (cChar <= '9')) ||
(cChar == ImplGetDateSep( rLocaleDataWrapper, eFormat )[0]) )
return false;
2000-09-18 16:07:07 +00:00
else
return true;
2000-09-18 16:07:07 +00:00
}
static bool ImplDateGetValue( const OUString& rStr, Date& rDate, ExtDateFieldFormat eDateFormat,
const LocaleDataWrapper& rLocaleDataWrapper, const CalendarWrapper& rCalendarWrapper,
const AllSettings& )
2000-09-18 16:07:07 +00:00
{
sal_uInt16 nDay = 0;
sal_uInt16 nMonth = 0;
sal_uInt16 nYear = 0;
bool bError = false;
OUString aStr( rStr );
2000-09-18 16:07:07 +00:00
2001-11-05 12:06:21 +00:00
if ( eDateFormat == XTDATEF_SYSTEM_LONG )
2000-09-18 16:07:07 +00:00
{
2001-11-05 12:06:21 +00:00
DateFormat eFormat = rLocaleDataWrapper.getLongDateFormat();
switch( eFormat )
2000-09-18 16:07:07 +00:00
{
case MDY:
2001-11-05 12:06:21 +00:00
nMonth = ImplCutMonthFromString( aStr, rCalendarWrapper );
nDay = ImplCutNumberFromString( aStr );
nYear = ImplCutNumberFromString( aStr );
2000-09-18 16:07:07 +00:00
break;
case DMY:
2001-11-05 12:06:21 +00:00
nDay = ImplCutNumberFromString( aStr );
nMonth = ImplCutMonthFromString( aStr, rCalendarWrapper );
nYear = ImplCutNumberFromString( aStr );
2000-09-18 16:07:07 +00:00
break;
case YMD:
default:
2001-11-05 12:06:21 +00:00
nYear = ImplCutNumberFromString( aStr );
nMonth = ImplCutMonthFromString( aStr, rCalendarWrapper );
nDay = ImplCutNumberFromString( aStr );
2000-09-18 16:07:07 +00:00
break;
}
}
else
{
bool bYear = true;
2001-11-05 12:06:21 +00:00
// Check if year is present:
OUString aDateSep = ImplGetDateSep( rLocaleDataWrapper, eDateFormat );
sal_Int32 nSepPos = aStr.indexOf( aDateSep );
if ( nSepPos < 0 )
return false;
nSepPos = aStr.indexOf( aDateSep, nSepPos+1 );
if ( ( nSepPos < 0 ) || ( nSepPos == (aStr.getLength()-1) ) )
2000-09-18 16:07:07 +00:00
{
bYear = false;
nYear = Date( Date::SYSTEM ).GetYear();
2000-09-18 16:07:07 +00:00
}
const sal_Unicode* pBuf = aStr.getStr();
2001-11-05 12:06:21 +00:00
ImplSkipDelimiters( pBuf );
2000-09-18 16:07:07 +00:00
2001-11-05 12:06:21 +00:00
switch ( eDateFormat )
2000-09-18 16:07:07 +00:00
{
2001-11-05 12:06:21 +00:00
case XTDATEF_SHORT_DDMMYY:
case XTDATEF_SHORT_DDMMYYYY:
2000-09-18 16:07:07 +00:00
{
2001-11-05 12:06:21 +00:00
nDay = ImplGetNum( pBuf, bError );
ImplSkipDelimiters( pBuf );
nMonth = ImplGetNum( pBuf, bError );
ImplSkipDelimiters( pBuf );
if ( bYear )
nYear = ImplGetNum( pBuf, bError );
2000-09-18 16:07:07 +00:00
}
break;
2001-11-05 12:06:21 +00:00
case XTDATEF_SHORT_MMDDYY:
case XTDATEF_SHORT_MMDDYYYY:
{
nMonth = ImplGetNum( pBuf, bError );
ImplSkipDelimiters( pBuf );
nDay = ImplGetNum( pBuf, bError );
ImplSkipDelimiters( pBuf );
if ( bYear )
nYear = ImplGetNum( pBuf, bError );
}
2000-09-18 16:07:07 +00:00
break;
2001-11-05 12:06:21 +00:00
case XTDATEF_SHORT_YYMMDD:
case XTDATEF_SHORT_YYYYMMDD:
case XTDATEF_SHORT_YYMMDD_DIN5008:
case XTDATEF_SHORT_YYYYMMDD_DIN5008:
{
if ( bYear )
nYear = ImplGetNum( pBuf, bError );
ImplSkipDelimiters( pBuf );
nMonth = ImplGetNum( pBuf, bError );
ImplSkipDelimiters( pBuf );
nDay = ImplGetNum( pBuf, bError );
}
2000-09-18 16:07:07 +00:00
break;
2001-11-05 12:06:21 +00:00
default:
{
2011-03-01 19:08:19 +01:00
OSL_FAIL( "DateFormat???" );
2001-11-05 12:06:21 +00:00
}
}
2000-09-18 16:07:07 +00:00
}
2001-11-05 12:06:21 +00:00
if ( bError || !nDay || !nMonth )
return false;
2000-09-18 16:07:07 +00:00
2001-11-05 12:06:21 +00:00
Date aNewDate( nDay, nMonth, nYear );
DateFormatter::ExpandCentury( aNewDate, utl::MiscCfg().GetYear2000() );
if ( aNewDate.IsValidDate() )
2001-11-05 12:06:21 +00:00
{
rDate = aNewDate;
return true;
2001-11-05 12:06:21 +00:00
}
return false;
2000-09-18 16:07:07 +00:00
}
bool DateFormatter::ImplDateReformat( const OUString& rStr, OUString& rOutStr, const AllSettings& rSettings )
2000-09-18 16:07:07 +00:00
{
Date aDate( 0, 0, 0 );
if ( !ImplDateGetValue( rStr, aDate, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper(), GetFieldSettings() ) )
return true;
2000-09-18 16:07:07 +00:00
Date aTempDate = aDate;
if ( aTempDate > GetMax() )
aTempDate = GetMax();
else if ( aTempDate < GetMin() )
aTempDate = GetMin();
if ( GetErrorHdl().IsSet() && (aDate != aTempDate) )
{
maCorrectedDate = aTempDate;
if( !GetErrorHdl().Call( this ) )
{
maCorrectedDate = Date( Date::SYSTEM );
return false;
2000-09-18 16:07:07 +00:00
}
else
maCorrectedDate = Date( Date::SYSTEM );
2000-09-18 16:07:07 +00:00
}
rOutStr = ImplGetDateAsText( aTempDate, rSettings );
return true;
2000-09-18 16:07:07 +00:00
}
OUString DateFormatter::ImplGetDateAsText( const Date& rDate,
const AllSettings& ) const
2000-09-18 16:07:07 +00:00
{
bool bShowCentury = false;
2001-11-05 12:06:21 +00:00
switch ( GetExtDateFormat() )
{
case XTDATEF_SYSTEM_SHORT_YYYY:
case XTDATEF_SYSTEM_LONG:
case XTDATEF_SHORT_DDMMYYYY:
case XTDATEF_SHORT_MMDDYYYY:
case XTDATEF_SHORT_YYYYMMDD:
case XTDATEF_SHORT_YYYYMMDD_DIN5008:
{
bShowCentury = true;
2001-11-05 12:06:21 +00:00
}
break;
default:
{
bShowCentury = false;
}
2001-11-05 12:06:21 +00:00
}
2001-11-05 12:06:21 +00:00
if ( !bShowCentury )
2000-09-18 16:07:07 +00:00
{
2001-11-05 12:06:21 +00:00
// Check if I have to use force showing the century
sal_uInt16 nTwoDigitYearStart = utl::MiscCfg().GetYear2000();
sal_uInt16 nYear = rDate.GetYear();
2001-11-05 12:06:21 +00:00
// If year is not in double digit range
2001-11-05 12:06:21 +00:00
if ( (nYear < nTwoDigitYearStart) || (nYear >= nTwoDigitYearStart+100) )
bShowCentury = true;
2000-09-18 16:07:07 +00:00
}
2001-11-05 12:06:21 +00:00
sal_Unicode aBuf[128];
sal_Unicode* pBuf = aBuf;
OUString aDateSep = ImplGetDateSep( ImplGetLocaleDataWrapper(), GetExtDateFormat( true ) );
sal_uInt16 nDay = rDate.GetDay();
sal_uInt16 nMonth = rDate.GetMonth();
sal_uInt16 nYear = rDate.GetYear();
sal_uInt16 nYearLen = bShowCentury ? 4 : 2;
2001-11-05 12:06:21 +00:00
if ( !bShowCentury )
nYear %= 100;
switch ( GetExtDateFormat( true ) )
2000-09-18 16:07:07 +00:00
{
2001-11-05 12:06:21 +00:00
case XTDATEF_SYSTEM_LONG:
{
return ImplGetLocaleDataWrapper().getLongDate( rDate, GetCalendarWrapper(), 1, false, 1, !bShowCentury );
2001-11-05 12:06:21 +00:00
}
case XTDATEF_SHORT_DDMMYY:
case XTDATEF_SHORT_DDMMYYYY:
{
pBuf = ImplAddNum( pBuf, nDay, 2 );
pBuf = ImplAddString( pBuf, aDateSep );
pBuf = ImplAddNum( pBuf, nMonth, 2 );
pBuf = ImplAddString( pBuf, aDateSep );
pBuf = ImplAddNum( pBuf, nYear, nYearLen );
}
break;
case XTDATEF_SHORT_MMDDYY:
case XTDATEF_SHORT_MMDDYYYY:
{
pBuf = ImplAddNum( pBuf, nMonth, 2 );
pBuf = ImplAddString( pBuf, aDateSep );
pBuf = ImplAddNum( pBuf, nDay, 2 );
pBuf = ImplAddString( pBuf, aDateSep );
pBuf = ImplAddNum( pBuf, nYear, nYearLen );
}
break;
case XTDATEF_SHORT_YYMMDD:
case XTDATEF_SHORT_YYYYMMDD:
case XTDATEF_SHORT_YYMMDD_DIN5008:
case XTDATEF_SHORT_YYYYMMDD_DIN5008:
{
pBuf = ImplAddNum( pBuf, nYear, nYearLen );
pBuf = ImplAddString( pBuf, aDateSep );
pBuf = ImplAddNum( pBuf, nMonth, 2 );
pBuf = ImplAddString( pBuf, aDateSep );
pBuf = ImplAddNum( pBuf, nDay, 2 );
}
break;
default:
{
2011-03-01 19:08:19 +01:00
OSL_FAIL( "DateFormat???" );
2001-11-05 12:06:21 +00:00
}
2000-09-18 16:07:07 +00:00
}
return OUString(aBuf, pBuf-aBuf);
2000-09-18 16:07:07 +00:00
}
static void ImplDateIncrementDay( Date& rDate, bool bUp )
2000-09-18 16:07:07 +00:00
{
DateFormatter::ExpandCentury( rDate );
if ( bUp )
{
if ( (rDate.GetDay() != 31) || (rDate.GetMonth() != 12) || (rDate.GetYear() != 9999) )
++rDate;
2000-09-18 16:07:07 +00:00
}
else
{
if ( (rDate.GetDay() != 1 ) || (rDate.GetMonth() != 1) || (rDate.GetYear() != 0) )
--rDate;
2000-09-18 16:07:07 +00:00
}
}
static void ImplDateIncrementMonth( Date& rDate, bool bUp )
2000-09-18 16:07:07 +00:00
{
DateFormatter::ExpandCentury( rDate );
sal_uInt16 nMonth = rDate.GetMonth();
sal_uInt16 nYear = rDate.GetYear();
2000-09-18 16:07:07 +00:00
if ( bUp )
{
if ( (nMonth == 12) && (nYear < 9999) )
{
rDate.SetMonth( 1 );
rDate.SetYear( nYear + 1 );
}
else
{
if ( nMonth < 12 )
rDate.SetMonth( nMonth + 1 );
}
}
else
{
if ( (nMonth == 1) && (nYear > 0) )
{
rDate.SetMonth( 12 );
rDate.SetYear( nYear - 1 );
}
else
{
if ( nMonth > 1 )
rDate.SetMonth( nMonth - 1 );
}
}
sal_uInt16 nDaysInMonth = Date::GetDaysInMonth( rDate.GetMonth(), rDate.GetYear());
2000-09-18 16:07:07 +00:00
if ( rDate.GetDay() > nDaysInMonth )
rDate.SetDay( nDaysInMonth );
}
static void ImplDateIncrementYear( Date& rDate, bool bUp )
2000-09-18 16:07:07 +00:00
{
DateFormatter::ExpandCentury( rDate );
sal_uInt16 nYear = rDate.GetYear();
sal_uInt16 nMonth = rDate.GetMonth();
2000-09-18 16:07:07 +00:00
if ( bUp )
{
if ( nYear < 9999 )
rDate.SetYear( nYear + 1 );
}
else
{
if ( nYear > 0 )
rDate.SetYear( nYear - 1 );
}
if (nMonth == 2)
{
// Handle February 29 from leap year to non-leap year.
sal_uInt16 nDay = rDate.GetDay();
if (nDay > 28)
{
// The check would not be necessary if it was guaranteed that the
// date was valid before and actually was a leap year,
// de-/incrementing a leap year with 29 always results in 28.
sal_uInt16 nDaysInMonth = Date::GetDaysInMonth( nMonth, rDate.GetYear());
if (nDay > nDaysInMonth)
rDate.SetDay( nDaysInMonth);
}
}
2000-09-18 16:07:07 +00:00
}
bool DateFormatter::ImplAllowMalformedInput() const
{
return !IsEnforceValidValue();
}
void DateField::ImplDateSpinArea( bool bUp )
2000-09-18 16:07:07 +00:00
{
// increment days if all is selected
2000-09-18 16:07:07 +00:00
if ( GetField() )
{
Date aDate( GetDate() );
Selection aSelection = GetField()->GetSelection();
aSelection.Justify();
OUString aText( GetText() );
if ( (sal_Int32)aSelection.Len() == aText.getLength() )
2000-09-18 16:07:07 +00:00
ImplDateIncrementDay( aDate, bUp );
else
{
sal_Int8 nDateArea = 0;
2000-09-18 16:07:07 +00:00
ExtDateFieldFormat eFormat = GetExtDateFormat( true );
2001-11-05 12:06:21 +00:00
if ( eFormat == XTDATEF_SYSTEM_LONG )
{
eFormat = ImplGetExtFormat( ImplGetLocaleDataWrapper().getLongDateFormat() );
nDateArea = 1;
}
2000-09-18 16:07:07 +00:00
else
{
// search area
sal_Int32 nPos = 0;
OUString aDateSep = ImplGetDateSep( ImplGetLocaleDataWrapper(), eFormat );
for ( sal_Int8 i = 1; i <= 3; i++ )
2000-09-18 16:07:07 +00:00
{
nPos = aText.indexOf( aDateSep, nPos );
if ( nPos >= (sal_Int32)aSelection.Max() )
2000-09-18 16:07:07 +00:00
{
nDateArea = i;
break;
}
else
nPos++;
}
}
2001-11-05 12:06:21 +00:00
2000-09-18 16:07:07 +00:00
switch( eFormat )
{
2001-11-05 12:06:21 +00:00
case XTDATEF_SHORT_MMDDYY:
case XTDATEF_SHORT_MMDDYYYY:
switch( nDateArea )
{
case 1: ImplDateIncrementMonth( aDate, bUp );
break;
case 2: ImplDateIncrementDay( aDate, bUp );
break;
case 3: ImplDateIncrementYear( aDate, bUp );
break;
}
break;
case XTDATEF_SHORT_DDMMYY:
case XTDATEF_SHORT_DDMMYYYY:
switch( nDateArea )
{
case 1: ImplDateIncrementDay( aDate, bUp );
break;
case 2: ImplDateIncrementMonth( aDate, bUp );
break;
case 3: ImplDateIncrementYear( aDate, bUp );
break;
}
break;
case XTDATEF_SHORT_YYMMDD:
case XTDATEF_SHORT_YYYYMMDD:
case XTDATEF_SHORT_YYMMDD_DIN5008:
case XTDATEF_SHORT_YYYYMMDD_DIN5008:
switch( nDateArea )
{
case 1: ImplDateIncrementYear( aDate, bUp );
break;
case 2: ImplDateIncrementMonth( aDate, bUp );
break;
case 3: ImplDateIncrementDay( aDate, bUp );
break;
}
break;
default:
2011-03-01 19:08:19 +01:00
OSL_FAIL( "invalid conversion" );
break;
2000-09-18 16:07:07 +00:00
}
}
ImplNewFieldValue( aDate );
}
}
void DateFormatter::ImplInit()
{
mbLongFormat = false;
mbShowDateCentury = true;
mpCalendarWrapper = NULL;
mnDateFormat = 0xFFFF;
2001-11-05 12:06:21 +00:00
mnExtDateFormat = XTDATEF_SYSTEM_SHORT;
2000-09-18 16:07:07 +00:00
}
DateFormatter::DateFormatter() :
maFieldDate( 0 ),
maLastDate( 0 ),
maMin( 1, 1, 1900 ),
maMax( 31, 12, 2200 ),
maCorrectedDate( Date::SYSTEM ),
mbEnforceValidValue( true )
2000-09-18 16:07:07 +00:00
{
ImplInit();
}
DateFormatter::~DateFormatter()
{
delete mpCalendarWrapper;
mpCalendarWrapper = NULL;
}
void DateFormatter::SetLocale( const ::com::sun::star::lang::Locale& rLocale )
{
delete mpCalendarWrapper;
mpCalendarWrapper = NULL;
FormatterBase::SetLocale( rLocale );
}
CalendarWrapper& DateFormatter::GetCalendarWrapper() const
{
if ( !mpCalendarWrapper )
{
const_cast<DateFormatter*>(this)->mpCalendarWrapper = new CalendarWrapper( comphelper::getProcessComponentContext() );
mpCalendarWrapper->loadDefaultCalendar( GetLocale() );
}
return *mpCalendarWrapper;
}
2001-11-05 12:06:21 +00:00
void DateFormatter::SetExtDateFormat( ExtDateFieldFormat eFormat )
{
mnExtDateFormat = eFormat;
ReformatAll();
}
ExtDateFieldFormat DateFormatter::GetExtDateFormat( bool bResolveSystemFormat ) const
2001-11-05 12:06:21 +00:00
{
ExtDateFieldFormat eDateFormat = (ExtDateFieldFormat)mnExtDateFormat;
if ( bResolveSystemFormat && ( eDateFormat <= XTDATEF_SYSTEM_SHORT_YYYY ) )
{
bool bShowCentury = (eDateFormat == XTDATEF_SYSTEM_SHORT_YYYY);
2001-11-05 12:06:21 +00:00
switch ( ImplGetLocaleDataWrapper().getDateFormat() )
{
case DMY: eDateFormat = bShowCentury ? XTDATEF_SHORT_DDMMYYYY : XTDATEF_SHORT_DDMMYY;
break;
case MDY: eDateFormat = bShowCentury ? XTDATEF_SHORT_MMDDYYYY : XTDATEF_SHORT_MMDDYY;
break;
default: eDateFormat = bShowCentury ? XTDATEF_SHORT_YYYYMMDD : XTDATEF_SHORT_YYMMDD;
}
}
return eDateFormat;
}
2000-09-18 16:07:07 +00:00
void DateFormatter::ReformatAll()
{
Reformat();
}
void DateFormatter::SetMin( const Date& rNewMin )
{
maMin = rNewMin;
if ( !IsEmptyFieldValue() )
ReformatAll();
}
void DateFormatter::SetMax( const Date& rNewMax )
{
maMax = rNewMax;
if ( !IsEmptyFieldValue() )
ReformatAll();
}
void DateFormatter::SetLongFormat( bool bLong )
2000-09-18 16:07:07 +00:00
{
mbLongFormat = bLong;
2001-11-05 12:06:21 +00:00
// #91913# Remove LongFormat and DateShowCentury - redundant
if ( bLong )
{
SetExtDateFormat( XTDATEF_SYSTEM_LONG );
}
else
{
if( mnExtDateFormat == XTDATEF_SYSTEM_LONG )
SetExtDateFormat( XTDATEF_SYSTEM_SHORT );
}
2000-09-18 16:07:07 +00:00
ReformatAll();
}
void DateFormatter::SetShowDateCentury( bool bShowDateCentury )
{
2001-11-05 12:06:21 +00:00
mbShowDateCentury = bShowDateCentury;
// #91913# Remove LongFormat and DateShowCentury - redundant
if ( bShowDateCentury )
2001-07-20 13:11:10 +00:00
{
2001-11-05 12:06:21 +00:00
switch ( GetExtDateFormat() )
{
case XTDATEF_SYSTEM_SHORT:
case XTDATEF_SYSTEM_SHORT_YY:
SetExtDateFormat( XTDATEF_SYSTEM_SHORT_YYYY ); break;
case XTDATEF_SHORT_DDMMYY:
SetExtDateFormat( XTDATEF_SHORT_DDMMYYYY ); break;
case XTDATEF_SHORT_MMDDYY:
SetExtDateFormat( XTDATEF_SHORT_MMDDYYYY ); break;
case XTDATEF_SHORT_YYMMDD:
SetExtDateFormat( XTDATEF_SHORT_YYYYMMDD ); break;
case XTDATEF_SHORT_YYMMDD_DIN5008:
SetExtDateFormat( XTDATEF_SHORT_YYYYMMDD_DIN5008 ); break;
default:
;
2001-11-05 12:06:21 +00:00
}
2001-07-20 13:11:10 +00:00
}
2001-11-05 12:06:21 +00:00
else
{
switch ( GetExtDateFormat() )
{
case XTDATEF_SYSTEM_SHORT:
case XTDATEF_SYSTEM_SHORT_YYYY:
SetExtDateFormat( XTDATEF_SYSTEM_SHORT_YY ); break;
case XTDATEF_SHORT_DDMMYYYY:
SetExtDateFormat( XTDATEF_SHORT_DDMMYY ); break;
case XTDATEF_SHORT_MMDDYYYY:
SetExtDateFormat( XTDATEF_SHORT_MMDDYY ); break;
case XTDATEF_SHORT_YYYYMMDD:
SetExtDateFormat( XTDATEF_SHORT_YYMMDD ); break;
case XTDATEF_SHORT_YYYYMMDD_DIN5008:
SetExtDateFormat( XTDATEF_SHORT_YYMMDD_DIN5008 ); break;
default:
;
2001-11-05 12:06:21 +00:00
}
}
ReformatAll();
}
2000-09-18 16:07:07 +00:00
void DateFormatter::SetDate( const Date& rNewDate )
{
SetUserDate( rNewDate );
maFieldDate = maLastDate;
maLastDate = GetDate();
}
void DateFormatter::SetUserDate( const Date& rNewDate )
{
ImplSetUserDate( rNewDate );
}
void DateFormatter::ImplSetUserDate( const Date& rNewDate, Selection* pNewSelection )
{
Date aNewDate = rNewDate;
if ( aNewDate > maMax )
aNewDate = maMax;
else if ( aNewDate < maMin )
aNewDate = maMin;
maLastDate = aNewDate;
if ( GetField() )
ImplSetText( ImplGetDateAsText( aNewDate, GetFieldSettings() ), pNewSelection );
}
void DateFormatter::ImplNewFieldValue( const Date& rDate )
{
if ( GetField() )
{
Selection aSelection = GetField()->GetSelection();
aSelection.Justify();
OUString aText = GetField()->GetText();
// If selected until the end then keep it that way
if ( (sal_Int32)aSelection.Max() == aText.getLength() )
2000-09-18 16:07:07 +00:00
{
if ( !aSelection.Len() )
aSelection.Min() = SELECTION_MAX;
aSelection.Max() = SELECTION_MAX;
}
Date aOldLastDate = maLastDate;
ImplSetUserDate( rDate, &aSelection );
maLastDate = aOldLastDate;
// Modify at Edit is only set at KeyInput
2000-09-18 16:07:07 +00:00
if ( GetField()->GetText() != aText )
{
GetField()->SetModifyFlag();
GetField()->Modify();
}
}
}
Date DateFormatter::GetDate() const
{
Date aDate( 0, 0, 0 );
if ( GetField() )
{
if ( ImplDateGetValue( GetField()->GetText(), aDate, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper(), GetFieldSettings() ) )
2000-09-18 16:07:07 +00:00
{
if ( aDate > maMax )
aDate = maMax;
else if ( aDate < maMin )
aDate = maMin;
}
else
{
// !!! We should find out why dates are treated differently than other fields (see
// also bug: 52384)
2000-09-18 16:07:07 +00:00
if ( !ImplAllowMalformedInput() )
{
if ( maLastDate.GetDate() )
aDate = maLastDate;
else if ( !IsEmptyFieldValueEnabled() )
aDate = Date( Date::SYSTEM );
}
else
aDate = GetInvalidDate();
2000-09-18 16:07:07 +00:00
}
}
return aDate;
}
void DateFormatter::SetEmptyDate()
{
FormatterBase::SetEmptyFieldValue();
}
bool DateFormatter::IsEmptyDate() const
2000-09-18 16:07:07 +00:00
{
bool bEmpty = FormatterBase::IsEmptyFieldValue();
2000-09-18 16:07:07 +00:00
if ( GetField() && MustBeReformatted() && IsEmptyFieldValueEnabled() )
{
if ( GetField()->GetText().isEmpty() )
2000-09-18 16:07:07 +00:00
{
bEmpty = true;
2000-09-18 16:07:07 +00:00
}
else if ( !maLastDate.GetDate() )
{
Date aDate( Date::EMPTY );
bEmpty = !ImplDateGetValue( GetField()->GetText(), aDate, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper(), GetFieldSettings() );
2000-09-18 16:07:07 +00:00
}
}
return bEmpty;
}
void DateFormatter::Reformat()
{
if ( !GetField() )
return;
if ( GetField()->GetText().isEmpty() && ImplGetEmptyFieldValue() )
2000-09-18 16:07:07 +00:00
return;
OUString aStr;
bool bOK = ImplDateReformat( GetField()->GetText(), aStr, GetFieldSettings() );
2000-09-18 16:07:07 +00:00
if( !bOK )
return;
if ( !aStr.isEmpty() )
2000-09-18 16:07:07 +00:00
{
ImplSetText( aStr );
ImplDateGetValue( aStr, maLastDate, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper(), GetFieldSettings() );
2000-09-18 16:07:07 +00:00
}
else
{
if ( maLastDate.GetDate() )
SetDate( maLastDate );
else if ( !IsEmptyFieldValueEnabled() )
SetDate( Date( Date::SYSTEM ) );
2000-09-18 16:07:07 +00:00
else
{
ImplSetText( OUString() );
SetEmptyFieldValueData( true );
2000-09-18 16:07:07 +00:00
}
}
}
void DateFormatter::ExpandCentury( Date& rDate )
{
ExpandCentury( rDate, utl::MiscCfg().GetYear2000() );
2000-09-18 16:07:07 +00:00
}
void DateFormatter::ExpandCentury( Date& rDate, sal_uInt16 nTwoDigitYearStart )
2000-09-18 16:07:07 +00:00
{
sal_uInt16 nDateYear = rDate.GetYear();
2000-09-18 16:07:07 +00:00
if ( nDateYear < 100 )
{
sal_uInt16 nCentury = nTwoDigitYearStart / 100;
2000-09-18 16:07:07 +00:00
if ( nDateYear < (nTwoDigitYearStart % 100) )
nCentury++;
rDate.SetYear( nDateYear + (nCentury*100) );
}
}
DateField::DateField( vcl::Window* pParent, WinBits nWinStyle ) :
2000-09-18 16:07:07 +00:00
SpinField( pParent, nWinStyle ),
maFirst( GetMin() ),
maLast( GetMax() )
{
SetField( this );
2001-07-20 12:19:24 +00:00
SetText( ImplGetLocaleDataWrapper().getDate( ImplGetFieldDate() ) );
2000-09-18 16:07:07 +00:00
Reformat();
ResetLastDate();
}
bool DateField::PreNotify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) && IsStrictFormat() &&
( GetExtDateFormat() != XTDATEF_SYSTEM_LONG ) &&
!rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2000-09-18 16:07:07 +00:00
{
if ( ImplDateProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), GetExtDateFormat( true ), ImplGetLocaleDataWrapper() ) )
return true;
2000-09-18 16:07:07 +00:00
}
return SpinField::PreNotify( rNEvt );
}
bool DateField::Notify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
MarkToBeReformatted( false );
else if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
2000-09-18 16:07:07 +00:00
{
if ( MustBeReformatted() )
{
// !!! We should find out why dates are treated differently than other fields (see
// also bug: 52384)
2000-09-18 16:07:07 +00:00
bool bTextLen = !GetText().isEmpty();
2000-09-18 16:07:07 +00:00
if ( bTextLen || !IsEmptyFieldValueEnabled() )
{
if ( !ImplAllowMalformedInput() )
Reformat();
else
{
Date aDate( 0, 0, 0 );
if ( ImplDateGetValue( GetText(), aDate, GetExtDateFormat(true), ImplGetLocaleDataWrapper(), GetCalendarWrapper(), GetFieldSettings() ) )
// even with strict text analysis, our text is a valid date -> do a complete
// reformat
Reformat();
}
}
2000-09-18 16:07:07 +00:00
else if ( !bTextLen && IsEmptyFieldValueEnabled() )
{
ResetLastDate();
SetEmptyFieldValueData( true );
2000-09-18 16:07:07 +00:00
}
}
}
return SpinField::Notify( rNEvt );
}
void DateField::DataChanged( const DataChangedEvent& rDCEvt )
{
SpinField::DataChanged( rDCEvt );
if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) && (rDCEvt.GetFlags() & (AllSettingsFlags::LOCALE|AllSettingsFlags::MISC)) )
{
if ( IsDefaultLocale() && ( rDCEvt.GetFlags() & AllSettingsFlags::LOCALE ) )
ImplGetLocaleDataWrapper().setLanguageTag( GetSettings().GetLanguageTag() );
2000-09-18 16:07:07 +00:00
ReformatAll();
}
2000-09-18 16:07:07 +00:00
}
void DateField::Modify()
{
MarkToBeReformatted( true );
2000-09-18 16:07:07 +00:00
SpinField::Modify();
}
void DateField::Up()
{
ImplDateSpinArea( true );
2000-09-18 16:07:07 +00:00
SpinField::Up();
}
void DateField::Down()
{
ImplDateSpinArea( false );
2000-09-18 16:07:07 +00:00
SpinField::Down();
}
void DateField::First()
{
ImplNewFieldValue( maFirst );
SpinField::First();
}
void DateField::Last()
{
ImplNewFieldValue( maLast );
SpinField::Last();
}
DateBox::DateBox( vcl::Window* pParent, WinBits nWinStyle ) :
2000-09-18 16:07:07 +00:00
ComboBox( pParent, nWinStyle )
{
SetField( this );
2001-07-20 12:19:24 +00:00
SetText( ImplGetLocaleDataWrapper().getDate( ImplGetFieldDate() ) );
2000-09-18 16:07:07 +00:00
Reformat();
}
bool DateBox::PreNotify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) && IsStrictFormat() &&
( GetExtDateFormat() != XTDATEF_SYSTEM_LONG ) &&
!rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2000-09-18 16:07:07 +00:00
{
if ( ImplDateProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), GetExtDateFormat( true ), ImplGetLocaleDataWrapper() ) )
return true;
2000-09-18 16:07:07 +00:00
}
return ComboBox::PreNotify( rNEvt );
}
void DateBox::DataChanged( const DataChangedEvent& rDCEvt )
{
ComboBox::DataChanged( rDCEvt );
if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) && (rDCEvt.GetFlags() & AllSettingsFlags::LOCALE) )
{
if ( IsDefaultLocale() )
ImplGetLocaleDataWrapper().setLanguageTag( GetSettings().GetLanguageTag() );
2000-09-18 16:07:07 +00:00
ReformatAll();
}
2000-09-18 16:07:07 +00:00
}
bool DateBox::Notify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
MarkToBeReformatted( false );
else if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
2000-09-18 16:07:07 +00:00
{
if ( MustBeReformatted() )
{
bool bTextLen = !GetText().isEmpty();
2000-09-18 16:07:07 +00:00
if ( bTextLen || !IsEmptyFieldValueEnabled() )
Reformat();
else if ( !bTextLen && IsEmptyFieldValueEnabled() )
{
ResetLastDate();
SetEmptyFieldValueData( true );
2000-09-18 16:07:07 +00:00
}
}
}
return ComboBox::Notify( rNEvt );
}
void DateBox::Modify()
{
MarkToBeReformatted( true );
2000-09-18 16:07:07 +00:00
ComboBox::Modify();
}
void DateBox::ReformatAll()
{
OUString aStr;
SetUpdateMode( false );
sal_uInt16 nEntryCount = GetEntryCount();
for ( sal_uInt16 i=0; i < nEntryCount; i++ )
2000-09-18 16:07:07 +00:00
{
ImplDateReformat( GetEntry( i ), aStr, GetFieldSettings() );
RemoveEntryAt(i);
2000-09-18 16:07:07 +00:00
InsertEntry( aStr, i );
}
DateFormatter::Reformat();
SetUpdateMode( true );
2000-09-18 16:07:07 +00:00
}
static bool ImplTimeProcessKeyInput( Edit*, const KeyEvent& rKEvt,
bool bStrictFormat, bool bDuration,
TimeFieldFormat eFormat,
const LocaleDataWrapper& rLocaleDataWrapper )
2000-09-18 16:07:07 +00:00
{
sal_Unicode cChar = rKEvt.GetCharCode();
2000-09-18 16:07:07 +00:00
if ( !bStrictFormat )
return false;
2000-09-18 16:07:07 +00:00
else
{
sal_uInt16 nGroup = rKEvt.GetKeyCode().GetGroup();
2000-09-18 16:07:07 +00:00
if ( (nGroup == KEYGROUP_FKEYS) || (nGroup == KEYGROUP_CURSOR) ||
(nGroup == KEYGROUP_MISC) ||
((cChar >= '0') && (cChar <= '9')) ||
string::equals(rLocaleDataWrapper.getTimeSep(), cChar) ||
(rLocaleDataWrapper.getTimeAM().indexOf(cChar) != -1) ||
(rLocaleDataWrapper.getTimePM().indexOf(cChar) != -1) ||
2002-09-02 11:27:42 +00:00
// Accept AM/PM:
(cChar == 'a') || (cChar == 'A') || (cChar == 'm') || (cChar == 'M') || (cChar == 'p') || (cChar == 'P') ||
((eFormat == TimeFieldFormat::F_100TH_SEC) && string::equals(rLocaleDataWrapper.getTime100SecSep(), cChar)) ||
((eFormat == TimeFieldFormat::F_SEC_CS) && string::equals(rLocaleDataWrapper.getTime100SecSep(), cChar)) ||
2000-09-18 16:07:07 +00:00
(bDuration && (cChar == '-')) )
return false;
2000-09-18 16:07:07 +00:00
else
return true;
2000-09-18 16:07:07 +00:00
}
}
static bool ImplIsOnlyDigits( const OUStringBuffer& _rStr )
{
const sal_Unicode* _pChr = _rStr.getStr();
for ( sal_Int32 i = 0; i < _rStr.getLength(); ++i, ++_pChr )
{
if ( *_pChr < '0' || *_pChr > '9' )
return false;
}
return true;
}
static bool ImplIsValidTimePortion( bool _bSkipInvalidCharacters, const OUStringBuffer& _rStr )
{
if ( !_bSkipInvalidCharacters )
{
if ( ( _rStr.getLength() > 2 ) || _rStr.isEmpty() || !ImplIsOnlyDigits( _rStr ) )
return false;
}
return true;
}
static bool ImplCutTimePortion( OUStringBuffer& _rStr, sal_Int32 _nSepPos, bool _bSkipInvalidCharacters, short* _pPortion )
{
OUString sPortion(_rStr.getStr(), _nSepPos );
_rStr = _nSepPos < _rStr.getLength()
? _rStr.copy( _nSepPos + 1 ) : OUStringBuffer();
if ( !ImplIsValidTimePortion( _bSkipInvalidCharacters, sPortion ) )
return false;
*_pPortion = (short)sPortion.toInt32();
return true;
}
static bool ImplTimeGetValue( const OUString& rStr, tools::Time& rTime,
TimeFieldFormat eFormat, bool bDuration,
const LocaleDataWrapper& rLocaleDataWrapper, bool _bSkipInvalidCharacters = true )
2000-09-18 16:07:07 +00:00
{
OUStringBuffer aStr = rStr;
2000-09-18 16:07:07 +00:00
short nHour = 0;
short nMinute = 0;
short nSecond = 0;
sal_Int64 nNanoSec = 0;
tools::Time aTime( 0, 0, 0 );
2000-09-18 16:07:07 +00:00
if ( rStr.isEmpty() )
return false;
2000-09-18 16:07:07 +00:00
// Search for separators
if (!rLocaleDataWrapper.getTimeSep().isEmpty())
2000-09-18 16:07:07 +00:00
{
OUStringBuffer aSepStr(",.;:/");
if ( !bDuration )
aSepStr.append('-');
// Replace characters above by the separator character
for (sal_Int32 i = 0; i < aSepStr.getLength(); ++i)
2000-09-18 16:07:07 +00:00
{
if (string::equals(rLocaleDataWrapper.getTimeSep(), aSepStr[i]))
continue;
for ( sal_Int32 j = 0; j < aStr.getLength(); j++ )
{
if (aStr[j] == aSepStr[i])
aStr[j] = rLocaleDataWrapper.getTimeSep()[0];
}
2000-09-18 16:07:07 +00:00
}
}
bool bNegative = false;
sal_Int32 nSepPos = aStr.indexOf( rLocaleDataWrapper.getTimeSep() );
if ( aStr[0] == '-' )
bNegative = true;
if ( eFormat != TimeFieldFormat::F_SEC_CS )
2000-09-18 16:07:07 +00:00
{
if ( nSepPos < 0 )
nSepPos = aStr.getLength();
if ( !ImplCutTimePortion( aStr, nSepPos, _bSkipInvalidCharacters, &nHour ) )
return false;
2000-09-18 16:07:07 +00:00
nSepPos = aStr.indexOf( rLocaleDataWrapper.getTimeSep() );
if ( !aStr.isEmpty() && aStr[0] == '-' )
bNegative = true;
if ( nSepPos >= 0 )
2000-09-18 16:07:07 +00:00
{
if ( !ImplCutTimePortion( aStr, nSepPos, _bSkipInvalidCharacters, &nMinute ) )
return false;
2000-09-18 16:07:07 +00:00
nSepPos = aStr.indexOf( rLocaleDataWrapper.getTimeSep() );
if ( !aStr.isEmpty() && aStr[0] == '-' )
bNegative = true;
if ( nSepPos >= 0 )
2000-09-18 16:07:07 +00:00
{
if ( !ImplCutTimePortion( aStr, nSepPos, _bSkipInvalidCharacters, &nSecond ) )
return false;
if ( !aStr.isEmpty() && aStr[0] == '-' )
bNegative = true;
nNanoSec = aStr.toString().toInt64();
2000-09-18 16:07:07 +00:00
}
else
nSecond = (short)aStr.toString().toInt32();
2000-09-18 16:07:07 +00:00
}
else
nMinute = (short)aStr.toString().toInt32();
2000-09-18 16:07:07 +00:00
}
else if ( nSepPos < 0 )
2000-09-18 16:07:07 +00:00
{
nSecond = (short)aStr.toString().toInt32();
2000-09-18 16:07:07 +00:00
nMinute += nSecond / 60;
nSecond %= 60;
nHour += nMinute / 60;
nMinute %= 60;
}
else
{
nSecond = (short)aStr.copy( 0, nSepPos ).toString().toInt32();
aStr.remove( 0, nSepPos+1 );
2000-09-18 16:07:07 +00:00
nSepPos = aStr.indexOf( rLocaleDataWrapper.getTimeSep() );
if ( !aStr.isEmpty() && aStr[0] == '-' )
bNegative = true;
if ( nSepPos >= 0 )
2000-09-18 16:07:07 +00:00
{
nMinute = nSecond;
nSecond = (short)aStr.copy( 0, nSepPos ).toString().toInt32();
aStr.remove( 0, nSepPos+1 );
2000-09-18 16:07:07 +00:00
nSepPos = aStr.indexOf( rLocaleDataWrapper.getTimeSep() );
if ( !aStr.isEmpty() && aStr[0] == '-' )
bNegative = true;
if ( nSepPos >= 0 )
2000-09-18 16:07:07 +00:00
{
nHour = nMinute;
nMinute = nSecond;
nSecond = (short)aStr.copy( 0, nSepPos ).toString().toInt32();
aStr.remove( 0, nSepPos+1 );
2000-09-18 16:07:07 +00:00
}
else
{
nHour += nMinute / 60;
nMinute %= 60;
}
}
else
{
nMinute += nSecond / 60;
nSecond %= 60;
nHour += nMinute / 60;
nMinute %= 60;
}
nNanoSec = aStr.toString().toInt64();
}
2000-09-18 16:07:07 +00:00
if ( nNanoSec )
{
assert(aStr.getLength() >= 1);
2000-09-18 16:07:07 +00:00
sal_Int32 nLen = 1; // at least one digit, otherwise nNanoSec==0
2000-09-18 16:07:07 +00:00
while ( aStr.getLength() > nLen && aStr[nLen] >= '0' && aStr[nLen] <= '9' )
nLen++;
while ( nLen < 9)
{
nNanoSec *= 10;
++nLen;
}
while ( nLen > 9 )
{
// round if negative?
nNanoSec = (nNanoSec + 5) / 10;
--nLen;
2000-09-18 16:07:07 +00:00
}
}
assert(nNanoSec > -1000000000 && nNanoSec < 1000000000);
if ( (nMinute > 59) || (nSecond > 59) || (nNanoSec > 1000000000) )
return false;
2000-09-18 16:07:07 +00:00
if ( eFormat == TimeFieldFormat::F_NONE )
nSecond = nNanoSec = 0;
else if ( eFormat == TimeFieldFormat::F_SEC )
nNanoSec = 0;
2000-09-18 16:07:07 +00:00
if ( !bDuration )
{
if ( bNegative || (nHour < 0) || (nMinute < 0) ||
(nSecond < 0) || (nNanoSec < 0) )
return false;
2000-09-18 16:07:07 +00:00
OUString aUpperCaseStr = aStr.toString().toAsciiUpperCase();
OUString aAM(rLocaleDataWrapper.getTimeAM().toAsciiUpperCase());
OUString aPM(rLocaleDataWrapper.getTimePM().toAsciiUpperCase());
OUString aAM2("AM"); // aAM is localized
OUString aPM2("PM"); // aPM is localized
2000-09-18 16:07:07 +00:00
if ( (nHour < 12) && ( ( aUpperCaseStr.indexOf( aPM ) >= 0 ) || ( aUpperCaseStr.indexOf( aPM2 ) >= 0 ) ) )
2000-09-18 16:07:07 +00:00
nHour += 12;
2002-09-02 11:27:42 +00:00
if ( (nHour == 12) && ( ( aUpperCaseStr.indexOf( aAM ) >= 0 ) || ( aUpperCaseStr.indexOf( aAM2 ) >= 0 ) ) )
2000-09-18 16:07:07 +00:00
nHour = 0;
aTime = tools::Time( (sal_uInt16)nHour, (sal_uInt16)nMinute, (sal_uInt16)nSecond,
(sal_uInt32)nNanoSec );
2000-09-18 16:07:07 +00:00
}
else
{
assert( !bNegative || (nHour < 0) || (nMinute < 0) ||
(nSecond < 0) || (nNanoSec < 0) );
2000-09-18 16:07:07 +00:00
if ( bNegative || (nHour < 0) || (nMinute < 0) ||
(nSecond < 0) || (nNanoSec < 0) )
2000-09-18 16:07:07 +00:00
{
// LEM TODO: this looks weird... I think buggy when parsing "05:-02:18"
bNegative = true;
2000-09-18 16:07:07 +00:00
nHour = nHour < 0 ? -nHour : nHour;
nMinute = nMinute < 0 ? -nMinute : nMinute;
nSecond = nSecond < 0 ? -nSecond : nSecond;
nNanoSec = nNanoSec < 0 ? -nNanoSec : nNanoSec;
2000-09-18 16:07:07 +00:00
}
aTime = tools::Time( (sal_uInt16)nHour, (sal_uInt16)nMinute, (sal_uInt16)nSecond,
(sal_uInt32)nNanoSec );
2000-09-18 16:07:07 +00:00
if ( bNegative )
aTime = -aTime;
}
rTime = aTime;
return true;
2000-09-18 16:07:07 +00:00
}
bool TimeFormatter::ImplTimeReformat( const OUString& rStr, OUString& rOutStr )
2000-09-18 16:07:07 +00:00
{
tools::Time aTime( 0, 0, 0 );
if ( !ImplTimeGetValue( rStr, aTime, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper() ) )
return true;
2000-09-18 16:07:07 +00:00
tools::Time aTempTime = aTime;
2000-09-18 16:07:07 +00:00
if ( aTempTime > GetMax() )
aTempTime = GetMax() ;
else if ( aTempTime < GetMin() )
aTempTime = GetMin();
if ( GetErrorHdl().IsSet() && (aTime != aTempTime) )
{
maCorrectedTime = aTempTime;
if ( !GetErrorHdl().Call( this ) )
{
maCorrectedTime = tools::Time( tools::Time::SYSTEM );
return false;
2000-09-18 16:07:07 +00:00
}
else
maCorrectedTime = tools::Time( tools::Time::SYSTEM );
2000-09-18 16:07:07 +00:00
}
bool bSecond = false;
bool b100Sec = false;
if ( meFormat != TimeFieldFormat::F_NONE )
bSecond = true;
if ( meFormat == TimeFieldFormat::F_100TH_SEC )
b100Sec = true;
2000-09-18 16:07:07 +00:00
if ( meFormat == TimeFieldFormat::F_SEC_CS )
2000-09-18 16:07:07 +00:00
{
sal_uLong n = aTempTime.GetHour() * 3600L;
2000-09-18 16:07:07 +00:00
n += aTempTime.GetMin() * 60L;
n += aTempTime.GetSec();
rOutStr = OUString::number( n );
2001-07-20 12:19:24 +00:00
rOutStr += ImplGetLocaleDataWrapper().getTime100SecSep();
std::ostringstream ostr;
ostr.fill('0');
ostr.width(9);
ostr << aTempTime.GetNanoSec();
rOutStr += OUString::createFromAscii(ostr.str().c_str());
2000-09-18 16:07:07 +00:00
}
else if ( mbDuration )
2001-07-20 12:19:24 +00:00
rOutStr = ImplGetLocaleDataWrapper().getDuration( aTempTime, bSecond, b100Sec );
2000-09-18 16:07:07 +00:00
else
2002-09-02 11:27:42 +00:00
{
2001-07-20 12:19:24 +00:00
rOutStr = ImplGetLocaleDataWrapper().getTime( aTempTime, bSecond, b100Sec );
2002-09-02 11:27:42 +00:00
if ( GetTimeFormat() == HOUR_12 )
{
if ( aTempTime.GetHour() > 12 )
{
tools::Time aT( aTempTime );
2002-09-02 11:27:42 +00:00
aT.SetHour( aT.GetHour() % 12 );
rOutStr = ImplGetLocaleDataWrapper().getTime( aT, bSecond, b100Sec );
}
// Don't use LocaleDataWrapper, we want AM/PM
if ( aTempTime.GetHour() < 12 )
rOutStr += "AM"; // ImplGetLocaleDataWrapper().getTimeAM();
2002-09-02 11:27:42 +00:00
else
rOutStr += "PM"; // ImplGetLocaleDataWrapper().getTimePM();
2002-09-02 11:27:42 +00:00
}
}
2000-09-18 16:07:07 +00:00
return true;
2000-09-18 16:07:07 +00:00
}
bool TimeFormatter::ImplAllowMalformedInput() const
{
return !IsEnforceValidValue();
}
void TimeField::ImplTimeSpinArea( bool bUp )
2000-09-18 16:07:07 +00:00
{
if ( GetField() )
{
sal_Int32 nTimeArea = 0;
tools::Time aTime( GetTime() );
OUString aText( GetText() );
2000-09-18 16:07:07 +00:00
Selection aSelection( GetField()->GetSelection() );
// Area search
if ( GetFormat() != TimeFieldFormat::F_SEC_CS )
2000-09-18 16:07:07 +00:00
{
//Which area is the cursor in of HH:MM:SS.TT
for ( sal_Int32 i = 1, nPos = 0; i <= 4; i++ )
2000-09-18 16:07:07 +00:00
{
sal_Int32 nPos1 = aText.indexOf( ImplGetLocaleDataWrapper().getTimeSep(), nPos );
sal_Int32 nPos2 = aText.indexOf( ImplGetLocaleDataWrapper().getTime100SecSep(), nPos );
//which ever comes first, bearing in mind that one might not be there
if (nPos1 >= 0 && nPos2 >= 0)
nPos = nPos1 < nPos2 ? nPos1 : nPos2;
else if (nPos1 >= 0)
nPos = nPos1;
else
nPos = nPos2;
if ( nPos < 0 || nPos >= aSelection.Max() )
2000-09-18 16:07:07 +00:00
{
nTimeArea = i;
break;
}
else
nPos++;
}
}
else
{
sal_Int32 nPos = aText.indexOf( ImplGetLocaleDataWrapper().getTime100SecSep() );
if ( nPos < 0 || nPos >= aSelection.Max() )
2000-09-18 16:07:07 +00:00
nTimeArea = 3;
else
nTimeArea = 4;
}
if ( nTimeArea )
{
tools::Time aAddTime( 0, 0, 0 );
2000-09-18 16:07:07 +00:00
if ( nTimeArea == 1 )
aAddTime = tools::Time( 1, 0 );
2000-09-18 16:07:07 +00:00
else if ( nTimeArea == 2 )
aAddTime = tools::Time( 0, 1 );
2000-09-18 16:07:07 +00:00
else if ( nTimeArea == 3 )
aAddTime = tools::Time( 0, 0, 1 );
2000-09-18 16:07:07 +00:00
else if ( nTimeArea == 4 )
aAddTime = tools::Time( 0, 0, 0, 1 );
2000-09-18 16:07:07 +00:00
if ( !bUp )
aAddTime = -aAddTime;
aTime += aAddTime;
2001-03-09 13:57:42 +00:00
if ( !IsDuration() )
2000-09-18 16:07:07 +00:00
{
tools::Time aAbsMaxTime( 23, 59, 59, 999999999 );
2000-09-18 16:07:07 +00:00
if ( aTime > aAbsMaxTime )
aTime = aAbsMaxTime;
tools::Time aAbsMinTime( 0, 0 );
2000-09-18 16:07:07 +00:00
if ( aTime < aAbsMinTime )
aTime = aAbsMinTime;
}
ImplNewFieldValue( aTime );
}
}
}
void TimeFormatter::ImplInit()
{
meFormat = TimeFieldFormat::F_NONE;
mbDuration = false;
2002-09-02 11:27:42 +00:00
mnTimeFormat = HOUR_24; // Should become a ExtTimeFieldFormat in next implementation, merge with mbDuration and meFormat
2000-09-18 16:07:07 +00:00
}
TimeFormatter::TimeFormatter() :
maLastTime( 0, 0 ),
2000-09-18 16:07:07 +00:00
maMin( 0, 0 ),
maMax( 23, 59, 59, 999999999 ),
maCorrectedTime( tools::Time::SYSTEM ),
mbEnforceValidValue( true ),
maFieldTime( 0, 0 )
2000-09-18 16:07:07 +00:00
{
ImplInit();
}
TimeFormatter::~TimeFormatter()
{
}
void TimeFormatter::ReformatAll()
{
Reformat();
}
void TimeFormatter::SetMin( const tools::Time& rNewMin )
2000-09-18 16:07:07 +00:00
{
maMin = rNewMin;
if ( !IsEmptyFieldValue() )
ReformatAll();
}
void TimeFormatter::SetMax( const tools::Time& rNewMax )
2000-09-18 16:07:07 +00:00
{
maMax = rNewMax;
if ( !IsEmptyFieldValue() )
ReformatAll();
}
void TimeFormatter::SetTimeFormat( TimeFormatter::TimeFormat eNewFormat )
{
mnTimeFormat = sal::static_int_cast<sal_uInt16>(eNewFormat);
}
2000-09-18 16:07:07 +00:00
void TimeFormatter::SetFormat( TimeFieldFormat eNewFormat )
{
meFormat = eNewFormat;
ReformatAll();
}
void TimeFormatter::SetDuration( bool bNewDuration )
2000-09-18 16:07:07 +00:00
{
mbDuration = bNewDuration;
ReformatAll();
}
void TimeFormatter::SetTime( const tools::Time& rNewTime )
2000-09-18 16:07:07 +00:00
{
SetUserTime( rNewTime );
maFieldTime = maLastTime;
SetEmptyFieldValueData( false );
2000-09-18 16:07:07 +00:00
}
void TimeFormatter::ImplNewFieldValue( const tools::Time& rTime )
2000-09-18 16:07:07 +00:00
{
if ( GetField() )
{
Selection aSelection = GetField()->GetSelection();
aSelection.Justify();
OUString aText = GetField()->GetText();
// If selected until the end then keep it that way
if ( (sal_Int32)aSelection.Max() == aText.getLength() )
2000-09-18 16:07:07 +00:00
{
if ( !aSelection.Len() )
aSelection.Min() = SELECTION_MAX;
aSelection.Max() = SELECTION_MAX;
}
tools::Time aOldLastTime = maLastTime;
2000-09-18 16:07:07 +00:00
ImplSetUserTime( rTime, &aSelection );
maLastTime = aOldLastTime;
// Modify at Edit is only set at KeyInput
2000-09-18 16:07:07 +00:00
if ( GetField()->GetText() != aText )
{
GetField()->SetModifyFlag();
GetField()->Modify();
}
}
}
void TimeFormatter::ImplSetUserTime( const tools::Time& rNewTime, Selection* pNewSelection )
2000-09-18 16:07:07 +00:00
{
tools::Time aNewTime = rNewTime;
2000-09-18 16:07:07 +00:00
if ( aNewTime > GetMax() )
aNewTime = GetMax();
else if ( aNewTime < GetMin() )
aNewTime = GetMin();
maLastTime = aNewTime;
if ( GetField() )
{
OUString aStr;
bool bSec = false;
bool b100Sec = false;
if ( meFormat != TimeFieldFormat::F_NONE )
bSec = true;
if ( meFormat == TimeFieldFormat::F_100TH_SEC || meFormat == TimeFieldFormat::F_SEC_CS )
b100Sec = true;
if ( meFormat == TimeFieldFormat::F_SEC_CS )
2000-09-18 16:07:07 +00:00
{
sal_uLong n = aNewTime.GetHour() * 3600L;
2000-09-18 16:07:07 +00:00
n += aNewTime.GetMin() * 60L;
n += aNewTime.GetSec();
aStr = OUString::number( n );
2001-07-20 12:19:24 +00:00
aStr += ImplGetLocaleDataWrapper().getTime100SecSep();
std::ostringstream ostr;
ostr.fill('0');
ostr.width(9);
ostr << aNewTime.GetNanoSec();
aStr += OUString::createFromAscii(ostr.str().c_str());
2000-09-18 16:07:07 +00:00
}
else if ( mbDuration )
2002-09-02 11:27:42 +00:00
{
2001-07-20 12:19:24 +00:00
aStr = ImplGetLocaleDataWrapper().getDuration( aNewTime, bSec, b100Sec );
2002-09-02 11:27:42 +00:00
}
2000-09-18 16:07:07 +00:00
else
2002-09-02 11:27:42 +00:00
{
2001-07-20 12:19:24 +00:00
aStr = ImplGetLocaleDataWrapper().getTime( aNewTime, bSec, b100Sec );
2002-09-02 11:27:42 +00:00
if ( GetTimeFormat() == HOUR_12 )
{
if ( aNewTime.GetHour() > 12 )
{
tools::Time aT( aNewTime );
2002-09-02 11:27:42 +00:00
aT.SetHour( aT.GetHour() % 12 );
aStr = ImplGetLocaleDataWrapper().getTime( aT, bSec, b100Sec );
}
// Don't use LocaleDataWrapper, we want AM/PM
if ( aNewTime.GetHour() < 12 )
aStr += "AM"; // ImplGetLocaleDataWrapper().getTimeAM();
2002-09-02 11:27:42 +00:00
else
aStr += "PM"; // ImplGetLocaleDataWrapper().getTimePM();
2002-09-02 11:27:42 +00:00
}
}
2000-09-18 16:07:07 +00:00
ImplSetText( aStr, pNewSelection );
}
}
void TimeFormatter::SetUserTime( const tools::Time& rNewTime )
2000-09-18 16:07:07 +00:00
{
ImplSetUserTime( rNewTime );
}
tools::Time TimeFormatter::GetTime() const
2000-09-18 16:07:07 +00:00
{
tools::Time aTime( 0, 0, 0 );
2000-09-18 16:07:07 +00:00
if ( GetField() )
{
bool bAllowMailformed = ImplAllowMalformedInput();
if ( ImplTimeGetValue( GetField()->GetText(), aTime, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper(), !bAllowMailformed ) )
2000-09-18 16:07:07 +00:00
{
if ( aTime > GetMax() )
aTime = GetMax();
else if ( aTime < GetMin() )
aTime = GetMin();
}
else
{
if ( bAllowMailformed )
aTime = GetInvalidTime();
else
aTime = maLastTime;
}
2000-09-18 16:07:07 +00:00
}
return aTime;
}
void TimeFormatter::Reformat()
{
if ( !GetField() )
return;
if ( GetField()->GetText().isEmpty() && ImplGetEmptyFieldValue() )
2000-09-18 16:07:07 +00:00
return;
OUString aStr;
bool bOK = ImplTimeReformat( GetField()->GetText(), aStr );
2000-09-18 16:07:07 +00:00
if ( !bOK )
return;
if ( !aStr.isEmpty() )
2000-09-18 16:07:07 +00:00
{
ImplSetText( aStr );
ImplTimeGetValue( aStr, maLastTime, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper() );
2000-09-18 16:07:07 +00:00
}
else
SetTime( maLastTime );
}
TimeField::TimeField( vcl::Window* pParent, WinBits nWinStyle ) :
2000-09-18 16:07:07 +00:00
SpinField( pParent, nWinStyle ),
maFirst( GetMin() ),
maLast( GetMax() )
{
SetField( this );
SetText( ImplGetLocaleDataWrapper().getTime( maFieldTime, false, false ) );
2000-09-18 16:07:07 +00:00
Reformat();
}
bool TimeField::PreNotify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2000-09-18 16:07:07 +00:00
{
if ( ImplTimeProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsDuration(), GetFormat(), ImplGetLocaleDataWrapper() ) )
return true;
2000-09-18 16:07:07 +00:00
}
return SpinField::PreNotify( rNEvt );
}
bool TimeField::Notify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
MarkToBeReformatted( false );
else if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
2000-09-18 16:07:07 +00:00
{
if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
{
if ( !ImplAllowMalformedInput() )
Reformat();
else
{
tools::Time aTime( 0, 0, 0 );
if ( ImplTimeGetValue( GetText(), aTime, GetFormat(), IsDuration(), ImplGetLocaleDataWrapper(), false ) )
// even with strict text analysis, our text is a valid time -> do a complete
// reformat
Reformat();
}
}
2000-09-18 16:07:07 +00:00
}
return SpinField::Notify( rNEvt );
}
void TimeField::DataChanged( const DataChangedEvent& rDCEvt )
{
SpinField::DataChanged( rDCEvt );
if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) && (rDCEvt.GetFlags() & AllSettingsFlags::LOCALE) )
{
if ( IsDefaultLocale() )
ImplGetLocaleDataWrapper().setLanguageTag( GetSettings().GetLanguageTag() );
2000-09-18 16:07:07 +00:00
ReformatAll();
}
2000-09-18 16:07:07 +00:00
}
void TimeField::Modify()
{
MarkToBeReformatted( true );
2000-09-18 16:07:07 +00:00
SpinField::Modify();
}
void TimeField::Up()
{
ImplTimeSpinArea( true );
2000-09-18 16:07:07 +00:00
SpinField::Up();
}
void TimeField::Down()
{
ImplTimeSpinArea( false );
2000-09-18 16:07:07 +00:00
SpinField::Down();
}
void TimeField::First()
{
ImplNewFieldValue( maFirst );
SpinField::First();
}
void TimeField::Last()
{
ImplNewFieldValue( maLast );
SpinField::Last();
}
void TimeField::SetExtFormat( ExtTimeFieldFormat eFormat )
{
switch ( eFormat )
{
case EXTTIMEF_24H_SHORT:
{
SetTimeFormat( HOUR_24 );
SetDuration( false );
SetFormat( TimeFieldFormat::F_NONE );
2000-09-18 16:07:07 +00:00
}
break;
case EXTTIMEF_24H_LONG:
{
SetTimeFormat( HOUR_24 );
SetDuration( false );
SetFormat( TimeFieldFormat::F_SEC );
2000-09-18 16:07:07 +00:00
}
break;
case EXTTIMEF_12H_SHORT:
{
SetTimeFormat( HOUR_12 );
SetDuration( false );
SetFormat( TimeFieldFormat::F_NONE );
2000-09-18 16:07:07 +00:00
}
break;
case EXTTIMEF_12H_LONG:
{
SetTimeFormat( HOUR_12 );
SetDuration( false );
SetFormat( TimeFieldFormat::F_SEC );
2000-09-18 16:07:07 +00:00
}
break;
case EXTTIMEF_DURATION_SHORT:
{
SetDuration( true );
SetFormat( TimeFieldFormat::F_NONE );
2000-09-18 16:07:07 +00:00
}
break;
case EXTTIMEF_DURATION_LONG:
{
SetDuration( true );
SetFormat( TimeFieldFormat::F_SEC );
2000-09-18 16:07:07 +00:00
}
break;
2011-03-01 19:08:19 +01:00
default: OSL_FAIL( "ExtTimeFieldFormat unknown!" );
2000-09-18 16:07:07 +00:00
}
if ( GetField() && !GetField()->GetText().isEmpty() )
SetUserTime( GetTime() );
ReformatAll();
2000-09-18 16:07:07 +00:00
}
TimeBox::TimeBox( vcl::Window* pParent, WinBits nWinStyle ) :
2000-09-18 16:07:07 +00:00
ComboBox( pParent, nWinStyle )
{
SetField( this );
SetText( ImplGetLocaleDataWrapper().getTime( maFieldTime, false, false ) );
2000-09-18 16:07:07 +00:00
Reformat();
}
bool TimeBox::PreNotify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2000-09-18 16:07:07 +00:00
{
if ( ImplTimeProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsDuration(), GetFormat(), ImplGetLocaleDataWrapper() ) )
return true;
2000-09-18 16:07:07 +00:00
}
return ComboBox::PreNotify( rNEvt );
}
bool TimeBox::Notify( NotifyEvent& rNEvt )
2000-09-18 16:07:07 +00:00
{
if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
MarkToBeReformatted( false );
else if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
2000-09-18 16:07:07 +00:00
{
if ( MustBeReformatted() && (!GetText().isEmpty() || !IsEmptyFieldValueEnabled()) )
2000-09-18 16:07:07 +00:00
Reformat();
}
return ComboBox::Notify( rNEvt );
}
void TimeBox::DataChanged( const DataChangedEvent& rDCEvt )
{
ComboBox::DataChanged( rDCEvt );
if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) && (rDCEvt.GetFlags() & AllSettingsFlags::LOCALE) )
{
if ( IsDefaultLocale() )
ImplGetLocaleDataWrapper().setLanguageTag( GetSettings().GetLanguageTag() );
2000-09-18 16:07:07 +00:00
ReformatAll();
}
2000-09-18 16:07:07 +00:00
}
void TimeBox::Modify()
{
MarkToBeReformatted( true );
2000-09-18 16:07:07 +00:00
ComboBox::Modify();
}
void TimeBox::ReformatAll()
{
OUString aStr;
SetUpdateMode( false );
sal_uInt16 nEntryCount = GetEntryCount();
for ( sal_uInt16 i=0; i < nEntryCount; i++ )
2000-09-18 16:07:07 +00:00
{
ImplTimeReformat( GetEntry( i ), aStr );
RemoveEntryAt(i);
2000-09-18 16:07:07 +00:00
InsertEntry( aStr, i );
}
TimeFormatter::Reformat();
SetUpdateMode( true );
2000-09-18 16:07:07 +00:00
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */