1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <stdio.h>
#include <tools/debug.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <unotools/localedatawrapper.hxx>
#include <vcl/svapp.hxx>
#include <svl/zformat.hxx>
#include <svtools/fmtfield.hxx>
#include <i18npool/mslangid.hxx>
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/util/SearchOptions.hpp>
#include <com/sun/star/util/SearchAlgorithms.hpp>
#include <com/sun/star/util/SearchResult.hpp>
#include <com/sun/star/util/SearchFlags.hpp>
#include <com/sun/star/lang/Locale.hpp>
#include <unotools/syslocale.hxx>
#ifndef REGEXP_SUPPORT
#include <map>
#endif
#if !defined INCLUDED_RTL_MATH_HXX
#include <rtl/math.hxx>
#endif
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::util;
#ifdef REGEXP_SUPPORT
//==============================================================================
// regular expression to validate complete numbers, plus every fragment which can occur during the input
// of a complete number
// [+/-][{digit}*.]*{digit}*[,{digit}*][e[+/-]{digit}*]
const char szNumericInput[] = "_[-+]?([0-9]*\\,)*[0-9]*(\\.[0-9]*)?(e[-+]?[0-9]*)?_";
// (the two _ are for normalizing it: With this, we can ensure that a to-be-checked text is always
// matched as a _whole_)
#else
// hmm. No support for regular expression. Well, I always (not really :) wanted to write a finite automat
// so here comes a finite automat ...
namespace validation
{
// the states of our automat.
enum State
{
START, // at the very start of the string
NUM_START, // the very start of the number
DIGIT_PRE_COMMA, // some pre-comma digits are read, perhaps including some thousand separators
DIGIT_POST_COMMA, // reading digits after the comma
EXPONENT_START, // at the very start of the exponent value
// (means: not including the "e" which denotes the exponent)
EXPONENT_DIGIT, // currently reading the digits of the exponent
END // reached the end of the string
};
// a row in the transition table (means the set of states to be reached from a given state)
typedef ::std::map< sal_Unicode, State > StateTransitions;
// a single transition
typedef StateTransitions::value_type Transition;
// the complete transition table
typedef ::std::map< State, StateTransitions > TransitionTable;
// the validator class
class NumberValidator
{
private:
TransitionTable m_aTransitions;
const sal_Unicode m_cThSep;
const sal_Unicode m_cDecSep;
public:
NumberValidator( const sal_Unicode _cThSep, const sal_Unicode _cDecSep );
sal_Bool isValidNumericFragment( const String& _rText );
private:
sal_Bool implValidateNormalized( const String& _rText );
};
//--------------------------------------------------------------------------
//..........................................................................
static void lcl_insertStopTransition( StateTransitions& _rRow )
{
_rRow.insert( Transition( '_', END ) );
}
//..........................................................................
static void lcl_insertStartExponentTransition( StateTransitions& _rRow )
{
_rRow.insert( Transition( 'e', EXPONENT_START ) );
}
//..........................................................................
static void lcl_insertSignTransitions( StateTransitions& _rRow, const State eNextState )
{
_rRow.insert( Transition( '-', eNextState ) );
_rRow.insert( Transition( '+', eNextState ) );
}
//..........................................................................
static void lcl_insertDigitTransitions( StateTransitions& _rRow, const State eNextState )
{
for ( sal_Unicode aChar = '0'; aChar <= '9'; ++aChar )
_rRow.insert( Transition( aChar, eNextState ) );
}
//..........................................................................
static void lcl_insertCommonPreCommaTransitions( StateTransitions& _rRow, const sal_Unicode _cThSep, const sal_Unicode _cDecSep )
{
// digits are allowed
lcl_insertDigitTransitions( _rRow, DIGIT_PRE_COMMA );
// the thousand separator is allowed
_rRow.insert( Transition( _cThSep, DIGIT_PRE_COMMA ) );
// a comma is allowed
_rRow.insert( Transition( _cDecSep, DIGIT_POST_COMMA ) );
}
//--------------------------------------------------------------------------
NumberValidator::NumberValidator( const sal_Unicode _cThSep, const sal_Unicode _cDecSep )
:m_cThSep( _cThSep )
,m_cDecSep( _cDecSep )
{
// build up our transition table
// how to procede from START
{
StateTransitions& rRow = m_aTransitions[ START ];
rRow.insert( Transition( '_', NUM_START ) );
// if we encounter the normalizing character, we want to procede with the number
}
// how to procede from NUM_START
{
StateTransitions& rRow = m_aTransitions[ NUM_START ];
// a sign is allowed
lcl_insertSignTransitions( rRow, DIGIT_PRE_COMMA );
// common transitions for the two pre-comma states
lcl_insertCommonPreCommaTransitions( rRow, m_cThSep, m_cDecSep );
// the exponent may start here
// (this would mean string like "_+e10_", but this is a valid fragment, though no valid number)
lcl_insertStartExponentTransition( rRow );
}
// how to procede from DIGIT_PRE_COMMA
{
StateTransitions& rRow = m_aTransitions[ DIGIT_PRE_COMMA ];
// common transitions for the two pre-comma states
lcl_insertCommonPreCommaTransitions( rRow, m_cThSep, m_cDecSep );
// the exponent may start here
lcl_insertStartExponentTransition( rRow );
// the final transition indicating the end of the string
// (if there is no comma and no post-comma, then the string may end here)
lcl_insertStopTransition( rRow );
}
// how to procede from DIGIT_POST_COMMA
{
StateTransitions& rRow = m_aTransitions[ DIGIT_POST_COMMA ];
// there might be digits, which would keep the state at DIGIT_POST_COMMA
lcl_insertDigitTransitions( rRow, DIGIT_POST_COMMA );
// the exponent may start here
lcl_insertStartExponentTransition( rRow );
// the string may end here
lcl_insertStopTransition( rRow );
}
// how to procede from EXPONENT_START
{
StateTransitions& rRow = m_aTransitions[ EXPONENT_START ];
// there may be a sign
lcl_insertSignTransitions( rRow, EXPONENT_DIGIT );
// there may be digits
lcl_insertDigitTransitions( rRow, EXPONENT_DIGIT );
// the string may end here
lcl_insertStopTransition( rRow );
}
// how to procede from EXPONENT_DIGIT
{
StateTransitions& rRow = m_aTransitions[ EXPONENT_DIGIT ];
// there may be digits
lcl_insertDigitTransitions( rRow, EXPONENT_DIGIT );
// the string may end here
lcl_insertStopTransition( rRow );
}
// how to procede from END
{
/*StateTransitions& rRow =*/ m_aTransitions[ EXPONENT_DIGIT ];
// no valid transition to leave this state
// (note that we, for consistency, nevertheless want to have a row in the table)
}
}
//--------------------------------------------------------------------------
sal_Bool NumberValidator::implValidateNormalized( const String& _rText )
{
const sal_Unicode* pCheckPos = _rText.GetBuffer();
State eCurrentState = START;
while ( END != eCurrentState )
{
// look up the transition row for the current state
TransitionTable::const_iterator aRow = m_aTransitions.find( eCurrentState );
DBG_ASSERT( m_aTransitions.end() != aRow,
"NumberValidator::implValidateNormalized: invalid transition table (row not found)!" );
if ( m_aTransitions.end() != aRow )
{
// look up the current character in this row
StateTransitions::const_iterator aTransition = aRow->second.find( *pCheckPos );
if ( aRow->second.end() != aTransition )
{
// there is a valid transition for this character
eCurrentState = aTransition->second;
++pCheckPos;
continue;
}
}
// if we're here, there is no valid transition
break;
}
DBG_ASSERT( ( END != eCurrentState ) || ( 0 == *pCheckPos ),
"NumberValidator::implValidateNormalized: inconsistency!" );
// if we're at END, then the string should be done, too - the string should be normalized, means ending
// a "_" and not containing any other "_" (except at the start), and "_" is the only possibility
// to reach the END state
// the string is valid if and only if we reached the final state
return ( END == eCurrentState );
}
//--------------------------------------------------------------------------
sal_Bool NumberValidator::isValidNumericFragment( const String& _rText )
{
if ( !_rText.Len() )
// empty strings are always allowed
return sal_True;
// normalize the string
String sNormalized( RTL_CONSTASCII_USTRINGPARAM("_") );
sNormalized.Append( _rText );
sNormalized.AppendAscii( "_" );
return implValidateNormalized( sNormalized );
}
}
#endif
//==============================================================================
SvNumberFormatter* FormattedField::StaticFormatter::s_cFormatter = NULL;
sal_uLong FormattedField::StaticFormatter::s_nReferences = 0;
//------------------------------------------------------------------------------
SvNumberFormatter* FormattedField::StaticFormatter::GetFormatter()
{
if (!s_cFormatter)
{
// get the Office's locale and translate
LanguageType eSysLanguage = MsLangId::convertLocaleToLanguage(
SvtSysLocale().GetLocaleData().getLocale() );
s_cFormatter = new SvNumberFormatter(
::comphelper::getProcessServiceFactory(),
eSysLanguage);
}
return s_cFormatter;
}
//------------------------------------------------------------------------------
FormattedField::StaticFormatter::StaticFormatter()
{
++s_nReferences;
}
//------------------------------------------------------------------------------
FormattedField::StaticFormatter::~StaticFormatter()
{
if (--s_nReferences == 0)
{
delete s_cFormatter;
s_cFormatter = NULL;
}
}
//==============================================================================
DBG_NAME(FormattedField);
#define INIT_MEMBERS() \
m_aLastSelection(0,0) \
,m_dMinValue(0) \
,m_dMaxValue(0) \
,m_bHasMin(sal_False) \
,m_bHasMax(sal_False) \
,m_bStrictFormat(sal_True) \
,m_bValueDirty(sal_True) \
,m_bEnableEmptyField(sal_True) \
,m_bAutoColor(sal_False) \
,m_bEnableNaN(sal_False) \
,m_dCurrentValue(0) \
,m_dDefaultValue(0) \
,m_nFormatKey(0) \
,m_pFormatter(NULL) \
,m_dSpinSize(1) \
,m_dSpinFirst(-1000000) \
,m_dSpinLast(1000000) \
,m_bTreatAsNumber(sal_True) \
,m_pLastOutputColor(NULL) \
,m_bUseInputStringForFormatting(false)
//------------------------------------------------------------------------------
FormattedField::FormattedField(Window* pParent, WinBits nStyle, SvNumberFormatter* pInitialFormatter, sal_Int32 nFormatKey)
:SpinField(pParent, nStyle)
,INIT_MEMBERS()
{
DBG_CTOR(FormattedField, NULL);
if (pInitialFormatter)
{
m_pFormatter = pInitialFormatter;
m_nFormatKey = nFormatKey;
}
}
//------------------------------------------------------------------------------
FormattedField::FormattedField(Window* pParent, const ResId& rResId, SvNumberFormatter* pInitialFormatter, sal_Int32 nFormatKey)
:SpinField(pParent, rResId)
,INIT_MEMBERS()
{
DBG_CTOR(FormattedField, NULL);
if (pInitialFormatter)
{
m_pFormatter = pInitialFormatter;
m_nFormatKey = nFormatKey;
}
}
//------------------------------------------------------------------------------
FormattedField::~FormattedField()
{
DBG_DTOR(FormattedField, NULL);
}
//------------------------------------------------------------------------------
void FormattedField::SetValidateText(const XubString& rText, const String* pErrorText)
{
DBG_CHKTHIS(FormattedField, NULL);
if (CheckText(rText))
SetText(rText);
else
if (pErrorText)
ImplSetTextImpl(*pErrorText, NULL);
else
ImplSetValue(m_dDefaultValue, sal_True);
}
//------------------------------------------------------------------------------
void FormattedField::SetText(const XubString& rStr)
{
DBG_CHKTHIS(FormattedField, NULL);
SpinField::SetText(rStr);
m_bValueDirty = sal_True;
}
//------------------------------------------------------------------------------
void FormattedField::SetText( const XubString& rStr, const Selection& rNewSelection )
{
DBG_CHKTHIS(FormattedField, NULL);
SpinField::SetText( rStr, rNewSelection );
m_bValueDirty = sal_True;
}
//------------------------------------------------------------------------------
void FormattedField::SetTextFormatted(const XubString& rStr)
{
DBG_CHKTHIS(FormattedField, NULL);
#if defined DBG_UTIL
if (ImplGetFormatter()->IsTextFormat(m_nFormatKey))
DBG_WARNING("FormattedField::SetTextFormatted : valid only with text formats !");
#endif
m_sCurrentTextValue = rStr;
String sFormatted;
double dNumber = 0.0;
// IsNumberFormat changes the format key parameter
sal_uInt32 nTempFormatKey = static_cast< sal_uInt32 >( m_nFormatKey );
if( IsUsingInputStringForFormatting() &&
ImplGetFormatter()->IsNumberFormat(m_sCurrentTextValue, nTempFormatKey, dNumber) )
ImplGetFormatter()->GetInputLineString(dNumber, m_nFormatKey, sFormatted);
else
ImplGetFormatter()->GetOutputString(m_sCurrentTextValue, m_nFormatKey, sFormatted, &m_pLastOutputColor);
// calculate the new selection
Selection aSel(GetSelection());
Selection aNewSel(aSel);
aNewSel.Justify();
sal_uInt16 nNewLen = sFormatted.Len();
sal_uInt16 nCurrentLen = GetText().Len();
if ((nNewLen > nCurrentLen) && (aNewSel.Max() == nCurrentLen))
{ // the new text is longer and the cursor was behind the last char (of the old text)
if (aNewSel.Min() == 0)
{ // the whole text was selected -> select the new text on the whole, too
aNewSel.Max() = nNewLen;
if (!nCurrentLen)
{ // there wasn't really a previous selection (as there was no previous text), we're setting a new one -> check the selection options
sal_uLong nSelOptions = GetSettings().GetStyleSettings().GetSelectionOptions();
if (nSelOptions & SELECTION_OPTION_SHOWFIRST)
{ // selection should be from right to left -> swap min and max
aNewSel.Min() = aNewSel.Max();
aNewSel.Max() = 0;
}
}
}
else if (aNewSel.Max() == aNewSel.Min())
{ // there was no selection -> set the cursor behind the new last char
aNewSel.Max() = nNewLen;
aNewSel.Min() = nNewLen;
}
}
else if (aNewSel.Max() > nNewLen)
aNewSel.Max() = nNewLen;
else
aNewSel = aSel; // don't use the justified version
SpinField::SetText(sFormatted, aNewSel);
m_bValueDirty = sal_False;
}
//------------------------------------------------------------------------------
String FormattedField::GetTextValue() const
{
if (m_bValueDirty)
{
((FormattedField*)this)->m_sCurrentTextValue = GetText();
((FormattedField*)this)->m_bValueDirty = sal_False;
}
return m_sCurrentTextValue;
}
//------------------------------------------------------------------------------
void FormattedField::EnableNotANumber( sal_Bool _bEnable )
{
if ( m_bEnableNaN == _bEnable )
return;
m_bEnableNaN = _bEnable;
}
//------------------------------------------------------------------------------
void FormattedField::SetAutoColor(sal_Bool _bAutomatic)
{
if (_bAutomatic == m_bAutoColor)
return;
m_bAutoColor = _bAutomatic;
if (m_bAutoColor)
{ // if auto color is switched on, adjust the current text color, too
if (m_pLastOutputColor)
SetControlForeground(*m_pLastOutputColor);
else
SetControlForeground();
}
}
//------------------------------------------------------------------------------
void FormattedField::Modify()
{
DBG_CHKTHIS(FormattedField, NULL);
if (!IsStrictFormat())
{
m_bValueDirty = sal_True;
SpinField::Modify();
return;
}
String sCheck = GetText();
if (CheckText(sCheck))
{
m_sLastValidText = sCheck;
m_aLastSelection = GetSelection();
m_bValueDirty = sal_True;
}
else
{
ImplSetTextImpl(m_sLastValidText, &m_aLastSelection);
}
SpinField::Modify();
}
//------------------------------------------------------------------------------
void FormattedField::ImplSetTextImpl(const XubString& rNew, Selection* pNewSel)
{
DBG_CHKTHIS(FormattedField, NULL);
if (m_bAutoColor)
{
if (m_pLastOutputColor)
SetControlForeground(*m_pLastOutputColor);
else
SetControlForeground();
}
if (pNewSel)
SpinField::SetText(rNew, *pNewSel);
else
{
Selection aSel(GetSelection());
aSel.Justify();
sal_uInt16 nNewLen = rNew.Len();
sal_uInt16 nCurrentLen = GetText().Len();
if ((nNewLen > nCurrentLen) && (aSel.Max() == nCurrentLen))
{ // new new text is longer and the cursor is behind the last char
if (aSel.Min() == 0)
{ // the whole text was selected -> select the new text on the whole, too
aSel.Max() = nNewLen;
if (!nCurrentLen)
{ // there wasn't really a previous selection (as there was no previous text), we're setting a new one -> check the selection options
sal_uLong nSelOptions = GetSettings().GetStyleSettings().GetSelectionOptions();
if (nSelOptions & SELECTION_OPTION_SHOWFIRST)
{ // selection should be from right to left -> swap min and max
aSel.Min() = aSel.Max();
aSel.Max() = 0;
}
}
}
else if (aSel.Max() == aSel.Min())
{ // there was no selection -> set the cursor behind the new last char
aSel.Max() = nNewLen;
aSel.Min() = nNewLen;
}
}
else if (aSel.Max() > nNewLen)
aSel.Max() = nNewLen;
SpinField::SetText(rNew, aSel);
}
m_bValueDirty = sal_True;
// muss nicht stimmen, aber sicherheitshalber ...
}
//------------------------------------------------------------------------------
long FormattedField::PreNotify(NotifyEvent& rNEvt)
{
DBG_CHKTHIS(FormattedField, NULL);
if (rNEvt.GetType() == EVENT_KEYINPUT)
m_aLastSelection = GetSelection();
return SpinField::PreNotify(rNEvt);
}
//------------------------------------------------------------------------------
void FormattedField::ImplSetFormatKey(sal_uLong nFormatKey)
{
DBG_CHKTHIS(FormattedField, NULL);
m_nFormatKey = nFormatKey;
sal_Bool bNeedFormatter = (m_pFormatter == NULL) && (nFormatKey != 0);
if (bNeedFormatter)
{
ImplGetFormatter(); // damit wird ein Standard-Formatter angelegt
m_nFormatKey = nFormatKey;
// kann sein, dass das in dem Standard-Formatter keinen Sinn macht, aber der nimmt dann ein Default-Format an.
// Auf diese Weise kann ich einfach einen der - formatteruebergreifended gleichen - Standard-Keys setzen.
DBG_ASSERT(m_pFormatter->GetEntry(nFormatKey) != NULL, "FormattedField::ImplSetFormatKey : invalid format key !");
// Wenn SetFormatKey aufgerufen wird, ohne dass ein Formatter existiert, muss der Key einer der Standard-Werte
// sein, der in allen Formattern (also auch in meinem neu angelegten) vorhanden ist.
}
}
//------------------------------------------------------------------------------
void FormattedField::SetFormatKey(sal_uLong nFormatKey)
{
DBG_CHKTHIS(FormattedField, NULL);
sal_Bool bNoFormatter = (m_pFormatter == NULL);
ImplSetFormatKey(nFormatKey);
FormatChanged((bNoFormatter && (m_pFormatter != NULL)) ? FCT_FORMATTER : FCT_KEYONLY);
}
//------------------------------------------------------------------------------
void FormattedField::SetFormatter(SvNumberFormatter* pFormatter, sal_Bool bResetFormat)
{
DBG_CHKTHIS(FormattedField, NULL);
if (bResetFormat)
{
m_pFormatter = pFormatter;
// calc the default format key from the Office's UI locale
if ( m_pFormatter )
{
// get the Office's locale and translate
LanguageType eSysLanguage = MsLangId::convertLocaleToLanguage(
SvtSysLocale().GetLocaleData().getLocale() );
// get the standard numeric format for this language
m_nFormatKey = m_pFormatter->GetStandardFormat( NUMBERFORMAT_NUMBER, eSysLanguage );
}
else
m_nFormatKey = 0;
}
else
{
XubString sOldFormat;
LanguageType aOldLang;
GetFormat(sOldFormat, aOldLang);
sal_uInt32 nDestKey = pFormatter->TestNewString(sOldFormat);
if (nDestKey == NUMBERFORMAT_ENTRY_NOT_FOUND)
{
// die Sprache des neuen Formatters
const SvNumberformat* pDefaultEntry = pFormatter->GetEntry(0);
LanguageType aNewLang = pDefaultEntry ? pDefaultEntry->GetLanguage() : LANGUAGE_DONTKNOW;
// den alten Format-String in die neue Sprache konvertieren
sal_uInt16 nCheckPos;
short nType;
pFormatter->PutandConvertEntry(sOldFormat, nCheckPos, nType, nDestKey, aOldLang, aNewLang);
m_nFormatKey = nDestKey;
}
m_pFormatter = pFormatter;
}
FormatChanged(FCT_FORMATTER);
}
//------------------------------------------------------------------------------
void FormattedField::GetFormat(XubString& rFormatString, LanguageType& eLang) const
{
DBG_CHKTHIS(FormattedField, NULL);
const SvNumberformat* pFormatEntry = ImplGetFormatter()->GetEntry(m_nFormatKey);
DBG_ASSERT(pFormatEntry != NULL, "FormattedField::GetFormat: no number format for the given format key.");
rFormatString = pFormatEntry ? pFormatEntry->GetFormatstring() : XubString();
eLang = pFormatEntry ? pFormatEntry->GetLanguage() : LANGUAGE_DONTKNOW;
}
//------------------------------------------------------------------------------
sal_Bool FormattedField::SetFormat(const XubString& rFormatString, LanguageType eLang)
{
DBG_CHKTHIS(FormattedField, NULL);
sal_uInt32 nNewKey = ImplGetFormatter()->TestNewString(rFormatString, eLang);
if (nNewKey == NUMBERFORMAT_ENTRY_NOT_FOUND)
{
sal_uInt16 nCheckPos;
short nType;
XubString rFormat(rFormatString);
if (!ImplGetFormatter()->PutEntry(rFormat, nCheckPos, nType, nNewKey, eLang))
return sal_False;
DBG_ASSERT(nNewKey != NUMBERFORMAT_ENTRY_NOT_FOUND, "FormattedField::SetFormatString : PutEntry returned an invalid key !");
}
if (nNewKey != m_nFormatKey)
SetFormatKey(nNewKey);
return sal_True;
}
//------------------------------------------------------------------------------
sal_Bool FormattedField::GetThousandsSep() const
{
DBG_ASSERT(!ImplGetFormatter()->IsTextFormat(m_nFormatKey),
"FormattedField::GetThousandsSep : your'e sure what your'e doing when setting the precision of a text format ?");
bool bThousand, IsRed;
sal_uInt16 nPrecision, nAnzLeading;
ImplGetFormatter()->GetFormatSpecialInfo(m_nFormatKey, bThousand, IsRed, nPrecision, nAnzLeading);
return bThousand;
}
//------------------------------------------------------------------------------
void FormattedField::SetThousandsSep(sal_Bool _bUseSeparator)
{
DBG_ASSERT(!ImplGetFormatter()->IsTextFormat(m_nFormatKey),
"FormattedField::SetThousandsSep : your'e sure what your'e doing when setting the precision of a text format ?");
// get the current settings
bool bThousand, IsRed;
sal_uInt16 nPrecision, nAnzLeading;
ImplGetFormatter()->GetFormatSpecialInfo(m_nFormatKey, bThousand, IsRed, nPrecision, nAnzLeading);
if (bThousand == (bool)_bUseSeparator)
return;
// we need the language for the following
LanguageType eLang;
String sFmtDescription;
GetFormat(sFmtDescription, eLang);
// generate a new format ...
ImplGetFormatter()->GenerateFormat(sFmtDescription, m_nFormatKey, eLang, _bUseSeparator, IsRed, nPrecision, nAnzLeading);
// ... and introduce it to the formatter
sal_uInt16 nCheckPos;
sal_uInt32 nNewKey;
short nType;
ImplGetFormatter()->PutEntry(sFmtDescription, nCheckPos, nType, nNewKey, eLang);
// set the new key
ImplSetFormatKey(nNewKey);
FormatChanged(FCT_THOUSANDSSEP);
}
//------------------------------------------------------------------------------
sal_uInt16 FormattedField::GetDecimalDigits() const
{
DBG_ASSERT(!ImplGetFormatter()->IsTextFormat(m_nFormatKey),
"FormattedField::GetDecimalDigits : your'e sure what your'e doing when setting the precision of a text format ?");
bool bThousand, IsRed;
sal_uInt16 nPrecision, nAnzLeading;
ImplGetFormatter()->GetFormatSpecialInfo(m_nFormatKey, bThousand, IsRed, nPrecision, nAnzLeading);
return nPrecision;
}
//------------------------------------------------------------------------------
void FormattedField::SetDecimalDigits(sal_uInt16 _nPrecision)
{
DBG_ASSERT(!ImplGetFormatter()->IsTextFormat(m_nFormatKey),
"FormattedField::SetDecimalDigits : your'e sure what your'e doing when setting the precision of a text format ?");
// get the current settings
bool bThousand, IsRed;
sal_uInt16 nPrecision, nAnzLeading;
ImplGetFormatter()->GetFormatSpecialInfo(m_nFormatKey, bThousand, IsRed, nPrecision, nAnzLeading);
if (nPrecision == _nPrecision)
return;
// we need the language for the following
LanguageType eLang;
String sFmtDescription;
GetFormat(sFmtDescription, eLang);
// generate a new format ...
ImplGetFormatter()->GenerateFormat(sFmtDescription, m_nFormatKey, eLang, bThousand, IsRed, _nPrecision, nAnzLeading);
// ... and introduce it to the formatter
sal_uInt16 nCheckPos;
sal_uInt32 nNewKey;
short nType;
ImplGetFormatter()->PutEntry(sFmtDescription, nCheckPos, nType, nNewKey, eLang);
// set the new key
ImplSetFormatKey(nNewKey);
FormatChanged(FCT_PRECISION);
}
//------------------------------------------------------------------------------
void FormattedField::FormatChanged( FORMAT_CHANGE_TYPE _nWhat )
{
DBG_CHKTHIS(FormattedField, NULL);
m_pLastOutputColor = NULL;
if ( ( 0 != ( _nWhat & FCT_FORMATTER ) ) && m_pFormatter )
m_pFormatter->SetEvalDateFormat( NF_EVALDATEFORMAT_INTL_FORMAT );
ReFormat();
}
//------------------------------------------------------------------------------
void FormattedField::Commit()
{
// remember the old text
String sOld( GetText() );
// do the reformat
ReFormat();
// did the text change?
if ( GetText() != sOld )
{ // consider the field as modified
Modify();
// but we have the most recent value now
m_bValueDirty = sal_False;
}
}
//------------------------------------------------------------------------------
void FormattedField::ReFormat()
{
if (!IsEmptyFieldEnabled() || GetText().Len())
{
if (TreatingAsNumber())
{
double dValue = GetValue();
if ( m_bEnableNaN && ::rtl::math::isNan( dValue ) )
return;
ImplSetValue( dValue, sal_True );
}
else
SetTextFormatted(GetTextValue());
}
}
//------------------------------------------------------------------------------
long FormattedField::Notify(NotifyEvent& rNEvt)
{
DBG_CHKTHIS(FormattedField, NULL);
if ((rNEvt.GetType() == EVENT_KEYINPUT) && !IsReadOnly())
{
const KeyEvent& rKEvt = *rNEvt.GetKeyEvent();
sal_uInt16 nMod = rKEvt.GetKeyCode().GetModifier();
switch ( rKEvt.GetKeyCode().GetCode() )
{
case KEY_UP:
case KEY_DOWN:
case KEY_PAGEUP:
case KEY_PAGEDOWN:
if (!nMod && ImplGetFormatter()->IsTextFormat(m_nFormatKey))
{
// the base class would translate this into calls to Up/Down/First/Last,
// but we don't want this if we are text-formatted
return 1;
}
}
}
if ((rNEvt.GetType() == EVENT_COMMAND) && !IsReadOnly())
{
const CommandEvent* pCommand = rNEvt.GetCommandEvent();
if (pCommand->GetCommand() == COMMAND_WHEEL)
{
const CommandWheelData* pData = rNEvt.GetCommandEvent()->GetWheelData();
if ((pData->GetMode() == COMMAND_WHEEL_SCROLL) && ImplGetFormatter()->IsTextFormat(m_nFormatKey))
{
// same as above : prevent the base class from doing Up/Down-calls
// (normally I should put this test into the Up/Down methods itself, shouldn't I ?)
// FS - 71553 - 19.01.00
return 1;
}
}
}
if (rNEvt.GetType() == EVENT_LOSEFOCUS)
{
// Sonderbehandlung fuer leere Texte
if (GetText().Len() == 0)
{
if (!IsEmptyFieldEnabled())
{
if (TreatingAsNumber())
{
ImplSetValue(m_dCurrentValue, sal_True);
Modify();
}
else
{
String sNew = GetTextValue();
if (sNew.Len())
SetTextFormatted(sNew);
else
SetTextFormatted(m_sDefaultText);
}
m_bValueDirty = sal_False;
}
}
else
{
Commit();
}
}
return SpinField::Notify( rNEvt );
}
//------------------------------------------------------------------------------
void FormattedField::SetMinValue(double dMin)
{
DBG_CHKTHIS(FormattedField, NULL);
DBG_ASSERT(m_bTreatAsNumber, "FormattedField::SetMinValue : only to be used in numeric mode !");
m_dMinValue = dMin;
m_bHasMin = sal_True;
// fuer die Ueberpruefung des aktuellen Wertes an der neuen Grenze -> ImplSetValue
ReFormat();
}
//------------------------------------------------------------------------------
void FormattedField::SetMaxValue(double dMax)
{
DBG_CHKTHIS(FormattedField, NULL);
DBG_ASSERT(m_bTreatAsNumber, "FormattedField::SetMaxValue : only to be used in numeric mode !");
m_dMaxValue = dMax;
m_bHasMax = sal_True;
// fuer die Ueberpruefung des aktuellen Wertes an der neuen Grenze -> ImplSetValue
ReFormat();
}
//------------------------------------------------------------------------------
void FormattedField::SetTextValue(const XubString& rText)
{
DBG_CHKTHIS(FormattedField, NULL);
SetText(rText);
ReFormat();
}
//------------------------------------------------------------------------------
void FormattedField::EnableEmptyField(sal_Bool bEnable)
{
DBG_CHKTHIS(FormattedField, NULL);
if (bEnable == m_bEnableEmptyField)
return;
m_bEnableEmptyField = bEnable;
if (!m_bEnableEmptyField && GetText().Len()==0)
ImplSetValue(m_dCurrentValue, sal_True);
}
//------------------------------------------------------------------------------
void FormattedField::ImplSetValue(double dVal, sal_Bool bForce)
{
DBG_CHKTHIS(FormattedField, NULL);
if (m_bHasMin && (dVal<m_dMinValue))
dVal = m_dMinValue;
if (m_bHasMax && (dVal>m_dMaxValue))
dVal = m_dMaxValue;
if (!bForce && (dVal == GetValue()))
return;
DBG_ASSERT(ImplGetFormatter() != NULL, "FormattedField::ImplSetValue : can't set a value without a formatter !");
m_bValueDirty = sal_False;
m_dCurrentValue = dVal;
String sNewText;
if (ImplGetFormatter()->IsTextFormat(m_nFormatKey))
{
// zuerst die Zahl als String im Standard-Format
String sTemp;
ImplGetFormatter()->GetOutputString(dVal, 0, sTemp, &m_pLastOutputColor);
// dann den String entsprechend dem Text-Format
ImplGetFormatter()->GetOutputString(sTemp, m_nFormatKey, sNewText, &m_pLastOutputColor);
}
else
{
if( IsUsingInputStringForFormatting())
ImplGetFormatter()->GetInputLineString(dVal, m_nFormatKey, sNewText);
else
ImplGetFormatter()->GetOutputString(dVal, m_nFormatKey, sNewText, &m_pLastOutputColor);
}
ImplSetTextImpl(sNewText, NULL);
m_bValueDirty = sal_False;
DBG_ASSERT(CheckText(sNewText), "FormattedField::ImplSetValue : formatted string doesn't match the criteria !");
}
//------------------------------------------------------------------------------
sal_Bool FormattedField::ImplGetValue(double& dNewVal)
{
DBG_CHKTHIS(FormattedField, NULL);
dNewVal = m_dCurrentValue;
if (!m_bValueDirty)
return sal_True;
dNewVal = m_dDefaultValue;
String sText(GetText());
if (!sText.Len())
return sal_True;
DBG_ASSERT(ImplGetFormatter() != NULL, "FormattedField::ImplGetValue : can't give you a current value without a formatter !");
sal_uInt32 nFormatKey = m_nFormatKey; // IsNumberFormat veraendert den FormatKey ...
if (ImplGetFormatter()->IsTextFormat(nFormatKey) && m_bTreatAsNumber)
// damit wir in einem als Text formatierten Feld trotzdem eine Eingabe wie '1,1' erkennen ...
nFormatKey = 0;
// Sonderbehandlung fuer %-Formatierung
if (ImplGetFormatter()->GetType(m_nFormatKey) == NUMBERFORMAT_PERCENT)
{
// the language of our format
LanguageType eLanguage = m_pFormatter->GetEntry(m_nFormatKey)->GetLanguage();
// the default number format for this language
sal_uLong nStandardNumericFormat = m_pFormatter->GetStandardFormat(NUMBERFORMAT_NUMBER, eLanguage);
sal_uInt32 nTempFormat = nStandardNumericFormat;
double dTemp;
if (m_pFormatter->IsNumberFormat(sText, nTempFormat, dTemp) &&
NUMBERFORMAT_NUMBER == m_pFormatter->GetType(nTempFormat))
// der String entspricht einer Number-Formatierung, hat also nur kein %
// -> append it
sText += '%';
// (with this, a input of '3' becomes '3%', which then by the formatter is translated
// into 0.03. Without this, the formatter would give us the double 3 for an input '3',
// which equals 300 percent.
}
if (!ImplGetFormatter()->IsNumberFormat(sText, nFormatKey, dNewVal))
return sal_False;
if (m_bHasMin && (dNewVal<m_dMinValue))
dNewVal = m_dMinValue;
if (m_bHasMax && (dNewVal>m_dMaxValue))
dNewVal = m_dMaxValue;
return sal_True;
}
//------------------------------------------------------------------------------
void FormattedField::SetValue(double dVal)
{
DBG_CHKTHIS(FormattedField, NULL);
ImplSetValue(dVal, m_bValueDirty);
}
//------------------------------------------------------------------------------
double FormattedField::GetValue()
{
DBG_CHKTHIS(FormattedField, NULL);
if ( !ImplGetValue( m_dCurrentValue ) )
{
if ( m_bEnableNaN )
::rtl::math::setNan( &m_dCurrentValue );
else
m_dCurrentValue = m_dDefaultValue;
}
m_bValueDirty = sal_False;
return m_dCurrentValue;
}
//------------------------------------------------------------------------------
void FormattedField::Up()
{
DBG_CHKTHIS(FormattedField, NULL);
SetValue(GetValue() + m_dSpinSize);
// das setValue handelt Bereichsueberschreitungen (min/max) automatisch
SetModifyFlag();
Modify();
SpinField::Up();
}
//------------------------------------------------------------------------------
void FormattedField::Down()
{
DBG_CHKTHIS(FormattedField, NULL);
SetValue(GetValue() - m_dSpinSize);
SetModifyFlag();
Modify();
SpinField::Down();
}
//------------------------------------------------------------------------------
void FormattedField::First()
{
DBG_CHKTHIS(FormattedField, NULL);
if (m_bHasMin)
{
SetValue(m_dMinValue);
SetModifyFlag();
Modify();
}
SpinField::First();
}
//------------------------------------------------------------------------------
void FormattedField::Last()
{
DBG_CHKTHIS(FormattedField, NULL);
if (m_bHasMax)
{
SetValue(m_dMaxValue);
SetModifyFlag();
Modify();
}
SpinField::Last();
}
//------------------------------------------------------------------------------
void FormattedField::UseInputStringForFormatting( bool bUseInputStr /* = true */ )
{
m_bUseInputStringForFormatting = bUseInputStr;
}
//------------------------------------------------------------------------------
bool FormattedField::IsUsingInputStringForFormatting() const
{
return m_bUseInputStringForFormatting;
}
//==============================================================================
//------------------------------------------------------------------------------
DoubleNumericField::~DoubleNumericField()
{
#ifdef REGEXP_SUPPORT
delete m_pConformanceTester;
#else
delete m_pNumberValidator;
#endif
}
//------------------------------------------------------------------------------
void DoubleNumericField::FormatChanged(FORMAT_CHANGE_TYPE nWhat)
{
ResetConformanceTester();
FormattedField::FormatChanged(nWhat);
}
//------------------------------------------------------------------------------
sal_Bool DoubleNumericField::CheckText(const XubString& sText) const
{
// We'd like to implement this using the NumberFormatter::IsNumberFormat, but unfortunately, this doesn't
// recognize fragments of numbers (like, for instance "1e", which happens during entering e.g. "1e10")
// Thus, the roundabout way via a regular expression
#ifdef REGEXP_SUPPORT
if (!sText.Len())
return sal_True;
String sForceComplete = '_';
sForceComplete += sText;
sForceComplete += '_';
sal_uInt16 nStart = 0, nEnd = sForceComplete.Len();
sal_Bool bFound = m_pConformanceTester->SearchFrwrd(sForceComplete, &nStart, &nEnd);
if (bFound && (nStart == 0) && (nEnd == sForceComplete.Len()))
return sal_True;
return sal_False;
#else
return m_pNumberValidator->isValidNumericFragment( sText );
#endif
}
//------------------------------------------------------------------------------
void DoubleNumericField::ResetConformanceTester()
{
// the thousands and the decimal separator are language dependent
const SvNumberformat* pFormatEntry = ImplGetFormatter()->GetEntry(m_nFormatKey);
sal_Unicode cSeparatorThousand = ',';
sal_Unicode cSeparatorDecimal = '.';
if (pFormatEntry)
{
Locale aLocale;
MsLangId::convertLanguageToLocale( pFormatEntry->GetLanguage(), aLocale );
LocaleDataWrapper aLocaleInfo(::comphelper::getProcessServiceFactory(), aLocale);
String sSeparator = aLocaleInfo.getNumThousandSep();
if (sSeparator.Len())
cSeparatorThousand = sSeparator.GetBuffer()[0];
sSeparator = aLocaleInfo.getNumDecimalSep();
if (sSeparator.Len())
cSeparatorDecimal = sSeparator.GetBuffer()[0];
}
#ifdef REGEXP_SUPPORT
String sDescription = String::CreateFromAscii(szNumericInput);
String sReplaceWith((sal_Unicode)'\\');
sReplaceWith += cSeparatorThousand;
sDescription.SearchAndReplaceAscii("\\,", sReplaceWith);
sReplaceWith = (sal_Unicode)'\\';
sReplaceWith += cSeparatorDecimal;
sDescription.SearchAndReplaceAscii("\\.", sReplaceWith);
delete m_pConformanceTester;
SearchOptions aParam;
aParam.algorithmType = SearchAlgorithms_REGEXP;
aParam.searchFlag = SearchFlags::ALL_IGNORE_CASE;
aParam.searchString = sDescription;
aParam.transliterateFlags = 0;
String sLanguage, sCountry;
ConvertLanguageToIsoNames( pFormatEntry ? pFormatEntry->GetLanguage() : LANGUAGE_ENGLISH_US, sLanguage, sCountry );
aParam.Locale.Language = sLanguage;
aParam.Locale.Country = sCountry;
m_pConformanceTester = new ::utl::TextSearch(aParam);
#else
delete m_pNumberValidator;
m_pNumberValidator = new validation::NumberValidator( cSeparatorThousand, cSeparatorDecimal );
#endif
}
//==============================================================================
//------------------------------------------------------------------------------
DoubleCurrencyField::DoubleCurrencyField(Window* pParent, WinBits nStyle)
:FormattedField(pParent, nStyle)
,m_bChangingFormat(sal_False)
{
m_bPrependCurrSym = sal_False;
// initialize with a system currency format
m_sCurrencySymbol = SvtSysLocale().GetLocaleData().getCurrSymbol();
UpdateCurrencyFormat();
}
//------------------------------------------------------------------------------
void DoubleCurrencyField::FormatChanged(FORMAT_CHANGE_TYPE nWhat)
{
if (m_bChangingFormat)
{
FormattedField::FormatChanged(nWhat);
return;
}
switch (nWhat)
{
case FCT_FORMATTER:
case FCT_PRECISION:
case FCT_THOUSANDSSEP:
// the aspects which changed don't take our currency settings into account (in fact, they most probably
// destroyed them)
UpdateCurrencyFormat();
break;
case FCT_KEYONLY:
OSL_FAIL("DoubleCurrencyField::FormatChanged : somebody modified my key !");
// We always build our own format from the settings we get via special methods (setCurrencySymbol etc.).
// Nobody but ourself should modifiy the format key directly !
break;
}
FormattedField::FormatChanged(nWhat);
}
//------------------------------------------------------------------------------
void DoubleCurrencyField::setCurrencySymbol(const String& _sSymbol)
{
if (m_sCurrencySymbol == _sSymbol)
return;
m_sCurrencySymbol = _sSymbol;
UpdateCurrencyFormat();
FormatChanged(FCT_CURRENCY_SYMBOL);
}
//------------------------------------------------------------------------------
void DoubleCurrencyField::setPrependCurrSym(sal_Bool _bPrepend)
{
if (m_bPrependCurrSym == _bPrepend)
return;
m_bPrependCurrSym = _bPrepend;
UpdateCurrencyFormat();
FormatChanged(FCT_CURRSYM_POSITION);
}
//------------------------------------------------------------------------------
void DoubleCurrencyField::UpdateCurrencyFormat()
{
// the old settings
XubString sOldFormat;
LanguageType eLanguage;
GetFormat(sOldFormat, eLanguage);
sal_Bool bThSep = GetThousandsSep();
sal_uInt16 nDigits = GetDecimalDigits();
// build a new format string with the base class' and my own settings
Locale aLocale;
MsLangId::convertLanguageToLocale( eLanguage, aLocale );
LocaleDataWrapper aLocaleInfo(::comphelper::getProcessServiceFactory(), aLocale);
XubString sNewFormat;
if (bThSep)
{
sNewFormat = '#';
sNewFormat += aLocaleInfo.getNumThousandSep();
sNewFormat.AppendAscii("##0");
}
else
sNewFormat = '0';
if (nDigits)
{
sNewFormat += aLocaleInfo.getNumDecimalSep();
XubString sTemp;
sTemp.Fill(nDigits, '0');
sNewFormat += sTemp;
}
if (getPrependCurrSym())
{
XubString sSymbol = getCurrencySymbol();
sSymbol = comphelper::string::stripStart(sSymbol, ' ');
sSymbol = comphelper::string::stripEnd(sSymbol, ' ');
XubString sTemp = String::CreateFromAscii("[$");
sTemp += sSymbol;
sTemp.AppendAscii("] ");
sTemp += sNewFormat;
// for negative values : $ -0.00, not -$ 0.00 ...
// (the real solution would be a possibility to choose a "positive currency format" and a "negative currency format" ...
// But not now ... (and hey, you could take a formatted field for this ....))
// FS - 31.03.00 74642
sTemp.AppendAscii(";[$");
sTemp += sSymbol;
sTemp.AppendAscii("] -");
sTemp += sNewFormat;
sNewFormat = sTemp;
}
else
{
XubString sTemp = getCurrencySymbol();
sTemp = comphelper::string::stripStart(sTemp, ' ');
sTemp = comphelper::string::stripEnd(sTemp, ' ');
sNewFormat += String::CreateFromAscii(" [$");
sNewFormat += sTemp;
sNewFormat += ']';
}
// set this new basic format
m_bChangingFormat = sal_True;
SetFormat(sNewFormat, eLanguage);
m_bChangingFormat = sal_False;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|