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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 .
*/
#include <unotools/charclass.hxx>
#include <editsh.hxx>
#include <fldbas.hxx>
#include <doc.hxx>
#include <docary.hxx>
#include <fmtfld.hxx>
#include <txtfld.hxx>
#include <edimp.hxx>
#include <dbfld.hxx>
#include <expfld.hxx>
#include <flddat.hxx>
#include <swundo.hxx>
#include <dbmgr.hxx>
#include <swddetbl.hxx>
#include <hints.hxx>
#include <switerator.hxx>
#include <fieldhint.hxx>
/// count field types with a ResId, if 0 count all
sal_uInt16 SwEditShell::GetFldTypeCount(sal_uInt16 nResId, sal_Bool bUsed ) const
{
const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
const sal_uInt16 nSize = pFldTypes->size();
if(nResId == USHRT_MAX)
{
if(!bUsed)
return nSize;
else
{
sal_uInt16 nUsed = 0;
for ( sal_uInt16 i = 0; i < nSize; i++ )
{
if(IsUsed(*(*pFldTypes)[i]))
nUsed++;
}
return nUsed;
}
}
// all types with the same ResId
sal_uInt16 nIdx = 0;
for(sal_uInt16 i = 0; i < nSize; ++i)
{ // same ResId -> increment index
SwFieldType& rFldType = *((*pFldTypes)[i]);
if(rFldType.Which() == nResId)
nIdx++;
}
return nIdx;
}
/// get field types with a ResId, if 0 get all
SwFieldType* SwEditShell::GetFldType(sal_uInt16 nFld, sal_uInt16 nResId, sal_Bool bUsed ) const
{
const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
const sal_uInt16 nSize = pFldTypes->size();
if(nResId == USHRT_MAX && nFld < nSize)
{
if(!bUsed)
return (*pFldTypes)[nFld];
else
{
sal_uInt16 i, nUsed = 0;
for ( i = 0; i < nSize; i++ )
{
if(IsUsed(*(*pFldTypes)[i]))
{
if(nUsed == nFld)
break;
nUsed++;
}
}
return i < nSize ? (*pFldTypes)[i] : 0;
}
}
sal_uInt16 nIdx = 0;
for(sal_uInt16 i = 0; i < nSize; ++i)
{ // same ResId -> increment index
SwFieldType* pFldType = (*pFldTypes)[i];
if(pFldType->Which() == nResId)
{
if (!bUsed || IsUsed(*pFldType))
{
if(nIdx == nFld)
return pFldType;
nIdx++;
}
}
}
return 0;
}
/// get first type with given ResId and name
SwFieldType* SwEditShell::GetFldType(sal_uInt16 nResId, const OUString& rName) const
{
return GetDoc()->GetFldType( nResId, rName, false );
}
/// delete field type
void SwEditShell::RemoveFldType(sal_uInt16 nFld, sal_uInt16 nResId)
{
if( USHRT_MAX == nResId )
{
GetDoc()->RemoveFldType(nFld);
return;
}
const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
const sal_uInt16 nSize = pFldTypes->size();
sal_uInt16 nIdx = 0;
for( sal_uInt16 i = 0; i < nSize; ++i )
// Gleiche ResId -> Index erhoehen
if( (*pFldTypes)[i]->Which() == nResId &&
nIdx++ == nFld )
{
GetDoc()->RemoveFldType( i );
return;
}
}
/// delete field type based on its name
void SwEditShell::RemoveFldType(sal_uInt16 nResId, const OUString& rStr)
{
const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
const sal_uInt16 nSize = pFldTypes->size();
const CharClass& rCC = GetAppCharClass();
OUString aTmp( rCC.lowercase( rStr ));
for(sal_uInt16 i = 0; i < nSize; ++i)
{
// same ResId -> increment index
SwFieldType* pFldType = (*pFldTypes)[i];
if( pFldType->Which() == nResId )
{
if( aTmp == rCC.lowercase( pFldType->GetName() ) )
{
GetDoc()->RemoveFldType(i);
return;
}
}
}
}
void SwEditShell::FieldToText( SwFieldType* pType )
{
if( !pType->GetDepends() )
return;
SET_CURR_SHELL( this );
StartAllAction();
StartUndo( UNDO_DELETE );
Push();
SwPaM* pPaM = GetCrsr();
SwFieldHint aHint( pPaM );
SwClientIter aIter( *pType );
for ( SwClient* pClient = aIter.GoStart(); pClient; pClient = ++aIter )
{
pPaM->DeleteMark();
pClient->SwClientNotifyCall( *pType, aHint );
}
Pop( sal_False );
EndAllAction();
EndUndo( UNDO_DELETE );
}
/// add a field at the cursor position
void SwEditShell::Insert2(SwField& rFld, const bool bForceExpandHints)
{
SET_CURR_SHELL( this );
StartAllAction();
SwFmtFld aFld( rFld );
const SetAttrMode nInsertFlags = (bForceExpandHints)
? nsSetAttrMode::SETATTR_FORCEHINTEXPAND
: nsSetAttrMode::SETATTR_DEFAULT;
FOREACHPAM_START(GetCrsr()) // for each PaM
const bool bSuccess(GetDoc()->InsertPoolItem(*PCURCRSR, aFld, nInsertFlags));
OSL_ENSURE( bSuccess, "Doc->Insert(Field) failed");
(void) bSuccess;
FOREACHPAM_END()
EndAllAction();
}
/// Are the PaMs positioned on fields?
static SwTxtFld* lcl_FindInputFld( SwDoc* pDoc, SwField& rFld )
{
// Search field via its address. For input fields this needs to be done in protected fields.
SwTxtFld* pTFld = 0;
if( RES_INPUTFLD == rFld.Which() )
{
const SfxPoolItem* pItem = NULL;
const sal_uInt32 nMaxItems =
pDoc->GetAttrPool().GetItemCount2( RES_TXTATR_INPUTFIELD );
for( sal_uInt32 n = 0; n < nMaxItems; ++n )
if( 0 != (pItem = pDoc->GetAttrPool().GetItem2( RES_TXTATR_INPUTFIELD, n ) )
&& ((SwFmtFld*)pItem)->GetField() == &rFld )
{
pTFld = ((SwFmtFld*)pItem)->GetTxtFld();
break;
}
}
else if( RES_SETEXPFLD == rFld.Which()
&& ((SwSetExpField&)rFld).GetInputFlag() )
{
const SfxPoolItem* pItem = NULL;
const sal_uInt32 nMaxItems =
pDoc->GetAttrPool().GetItemCount2( RES_TXTATR_FIELD );
for( sal_uInt32 n = 0; n < nMaxItems; ++n )
if( 0 != (pItem = pDoc->GetAttrPool().GetItem2( RES_TXTATR_FIELD, n ) )
&& ((SwFmtFld*)pItem)->GetField() == &rFld )
{
pTFld = ((SwFmtFld*)pItem)->GetTxtFld();
break;
}
}
return pTFld;
}
void SwEditShell::UpdateFlds( SwField &rFld )
{
SET_CURR_SHELL( this );
StartAllAction();
{
// // If there are no selections so take the value of the current cursor position.
SwMsgPoolItem* pMsgHnt = 0;
SwRefMarkFldUpdate aRefMkHt( GetOut() );
sal_uInt16 nFldWhich = rFld.GetTyp()->Which();
if( RES_GETREFFLD == nFldWhich )
pMsgHnt = &aRefMkHt;
SwPaM* pCrsr = GetCrsr();
SwTxtFld *pTxtFld;
SwFmtFld *pFmtFld;
if ( pCrsr->GetNext() == pCrsr && !pCrsr->HasMark())
{
pTxtFld = GetTxtFldAtPos( pCrsr->Start(), true );
if (!pTxtFld) // #i30221#
pTxtFld = lcl_FindInputFld( GetDoc(), rFld);
if (pTxtFld != 0)
GetDoc()->UpdateFld(pTxtFld, rFld, pMsgHnt, sal_True);
}
// bOkay (instead of return because of EndAllAction) becomes false,
// 1) if only one PaM has more than one field or
// 2) if there are mixed field types
bool bOkay = true;
sal_Bool bTblSelBreak = sal_False;
SwMsgPoolItem aFldHint( RES_TXTATR_FIELD ); // Search-Hint
SwMsgPoolItem aInputFldHint( RES_TXTATR_INPUTFIELD );
FOREACHPAM_START(GetCrsr()) // for each PaM
if( PCURCRSR->HasMark() && bOkay ) // ... with selection
{
// copy of the PaM
SwPaM aCurPam( *PCURCRSR->GetMark(), *PCURCRSR->GetPoint() );
SwPaM aPam( *PCURCRSR->GetPoint() );
SwPosition *pCurStt = aCurPam.Start(), *pCurEnd =
aCurPam.End();
/*
* In case that there are two contiguous fields in a PaM, the aPam goes step by step
* to the end. aCurPam is reduced in each loop. If aCurPam was searched completely,
* the loop terminates because Start = End.
*/
// Search for SwTxtFld ...
while( bOkay
&& pCurStt->nContent != pCurEnd->nContent
&& ( aPam.Find( aFldHint, sal_False, fnMoveForward, &aCurPam )
|| aPam.Find( aInputFldHint, sal_False, fnMoveForward, &aCurPam ) ) )
{
// if only one PaM has more than one field ...
if( aPam.Start()->nContent != pCurStt->nContent )
bOkay = false;
if( 0 != (pTxtFld = GetTxtFldAtPos( pCurStt, true )) )
{
pFmtFld = (SwFmtFld*)&pTxtFld->GetFmtFld();
SwField *pCurFld = pFmtFld->GetField();
// if there are mixed field types
if( pCurFld->GetTyp()->Which() !=
rFld.GetTyp()->Which() )
bOkay = false;
bTblSelBreak = GetDoc()->UpdateFld(pTxtFld, rFld,
pMsgHnt, sal_False);
}
// The search area is reduced by the found area:
pCurStt->nContent++;
}
}
if( bTblSelBreak ) // If table section and table formula are updated -> finish
break;
FOREACHPAM_END()
}
GetDoc()->SetModified();
EndAllAction();
}
SwDBData SwEditShell::GetDBData() const
{
return GetDoc()->GetDBData();
}
const SwDBData& SwEditShell::GetDBDesc() const
{
return GetDoc()->GetDBDesc();
}
void SwEditShell::ChgDBData(const SwDBData& rNewData)
{
GetDoc()->ChgDBData(rNewData);
}
void SwEditShell::GetAllUsedDB( std::vector<OUString>& rDBNameList,
std::vector<OUString>* pAllDBNames )
{
GetDoc()->GetAllUsedDB( rDBNameList, pAllDBNames );
}
void SwEditShell::ChangeDBFields( const std::vector<OUString>& rOldNames,
const OUString& rNewName )
{
GetDoc()->ChangeDBFields( rOldNames, rNewName );
}
/// Update all expression fields
void SwEditShell::UpdateExpFlds(sal_Bool bCloseDB)
{
SET_CURR_SHELL( this );
StartAllAction();
GetDoc()->UpdateExpFlds(NULL, true);
if (bCloseDB)
GetDoc()->GetNewDBMgr()->CloseAll(); // close all database connections
EndAllAction();
}
SwNewDBMgr* SwEditShell::GetNewDBMgr() const
{
return GetDoc()->GetNewDBMgr();
}
/// insert field type
SwFieldType* SwEditShell::InsertFldType(const SwFieldType& rFldType)
{
return GetDoc()->InsertFldType(rFldType);
}
void SwEditShell::LockExpFlds()
{
GetDoc()->LockExpFlds();
}
void SwEditShell::UnlockExpFlds()
{
GetDoc()->UnlockExpFlds();
}
void SwEditShell::SetFldUpdateFlags( SwFldUpdateFlags eFlags )
{
getIDocumentSettingAccess()->setFieldUpdateFlags( eFlags );
}
SwFldUpdateFlags SwEditShell::GetFldUpdateFlags(sal_Bool bDocSettings) const
{
return getIDocumentSettingAccess()->getFieldUpdateFlags( !bDocSettings );
}
void SwEditShell::SetLabelDoc( sal_Bool bFlag )
{
GetDoc()->set(IDocumentSettingAccess::LABEL_DOCUMENT, bFlag );
}
sal_Bool SwEditShell::IsLabelDoc() const
{
return getIDocumentSettingAccess()->get(IDocumentSettingAccess::LABEL_DOCUMENT);
}
void SwEditShell::ChangeAuthorityData(const SwAuthEntry* pNewData)
{
GetDoc()->ChangeAuthorityData(pNewData);
}
sal_Bool SwEditShell::IsAnyDatabaseFieldInDoc()const
{
const SwFldTypes * pFldTypes = GetDoc()->GetFldTypes();
const sal_uInt16 nSize = pFldTypes->size();
for(sal_uInt16 i = 0; i < nSize; ++i)
{
SwFieldType& rFldType = *((*pFldTypes)[i]);
sal_uInt16 nWhich = rFldType.Which();
if(IsUsed(rFldType))
{
switch(nWhich)
{
case RES_DBFLD:
case RES_DBNEXTSETFLD:
case RES_DBNUMSETFLD:
case RES_DBSETNUMBERFLD:
{
SwIterator<SwFmtFld,SwFieldType> aIter( rFldType );
SwFmtFld* pFld = aIter.First();
while(pFld)
{
if(pFld->IsFldInDoc())
return sal_True;
pFld = aIter.Next();
}
}
break;
}
}
}
return sal_False;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|