Files
libreoffice/sw/source/core/edit/edtab.cxx

523 lines
17 KiB
C++
Raw Normal View History

/* -*- 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 .
*/
2000-09-18 23:08:29 +00:00
#include <fesh.hxx>
2000-09-18 23:08:29 +00:00
#include <hintids.hxx>
#include <hints.hxx>
2000-09-18 23:08:29 +00:00
#include <vcl/svapp.hxx>
#include <vcl/window.hxx>
#include <editeng/boxitem.hxx>
2000-09-18 23:08:29 +00:00
#include <swwait.hxx>
#include <fmtfsize.hxx>
#include <frmatr.hxx>
#include <editsh.hxx>
#include <doc.hxx>
#include <IDocumentUndoRedo.hxx>
#include <IDocumentChartDataProviderAccess.hxx>
#include <IDocumentFieldsAccess.hxx>
#include <IDocumentState.hxx>
2000-09-18 23:08:29 +00:00
#include <cntfrm.hxx>
#include <pam.hxx>
#include <ndtxt.hxx>
#include <fldbas.hxx>
#include <swtable.hxx>
#include <swundo.hxx>
#include <tblsel.hxx>
#include <edimp.hxx>
#include <tabfrm.hxx>
#include <cellfrm.hxx>
#include <cellatr.hxx>
#include <swtblfmt.hxx>
#include <swddetbl.hxx>
#include <mdiexp.hxx>
#include <unochart.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
2000-09-18 23:08:29 +00:00
//Added for bug #i119954# Application crashed if undo/redo covert nest table to text
bool ConvertTableToText( const SwTableNode *pTableNode, sal_Unicode cCh );
void ConvertNestedTablesToText( const SwTableLines &rTableLines, sal_Unicode cCh )
{
for (size_t n = 0; n < rTableLines.size(); ++n)
{
SwTableLine* pTableLine = rTableLines[ n ];
for (size_t i = 0; i < pTableLine->GetTabBoxes().size(); ++i)
{
SwTableBox* pTableBox = pTableLine->GetTabBoxes()[ i ];
if (pTableBox->GetTabLines().empty())
{
SwNodeIndex nodeIndex( *pTableBox->GetSttNd(), 1 );
SwNodeIndex endNodeIndex( *pTableBox->GetSttNd()->EndOfSectionNode() );
for( ; nodeIndex < endNodeIndex ; ++nodeIndex )
{
if ( SwTableNode* pTableNode = nodeIndex.GetNode().GetTableNode() )
ConvertTableToText( pTableNode, cCh );
}
}
else
{
ConvertNestedTablesToText( pTableBox->GetTabLines(), cCh );
}
}
}
}
bool ConvertTableToText( const SwTableNode *pConstTableNode, sal_Unicode cCh )
{
SwTableNode *pTableNode = const_cast< SwTableNode* >( pConstTableNode );
ConvertNestedTablesToText( pTableNode->GetTable().GetTabLines(), cCh );
return pTableNode->GetDoc()->TableToText( pTableNode, cCh );
}
//End for bug #i119954#
const SwTable& SwEditShell::InsertTable( const SwInsertTableOptions& rInsTableOpts,
sal_uInt16 nRows, sal_uInt16 nCols,
const SwTableAutoFormat* pTAFormat )
2000-09-18 23:08:29 +00:00
{
StartAllAction();
SwPosition* pPos = GetCursor()->GetPoint();
2000-09-18 23:08:29 +00:00
bool bEndUndo = 0 != pPos->nContent.GetIndex();
2000-09-18 23:08:29 +00:00
if( bEndUndo )
{
StartUndo( SwUndoId::START );
GetDoc()->getIDocumentContentOperations().SplitNode( *pPos, false );
2000-09-18 23:08:29 +00:00
}
// If called from a shell the adjust item is propagated
// from pPos to the new content nodes in the table.
const SwTable *pTable = GetDoc()->InsertTable( rInsTableOpts, *pPos,
nRows, nCols,
css::text::HoriOrientation::FULL, pTAFormat,
nullptr, true );
2000-09-18 23:08:29 +00:00
if( bEndUndo )
EndUndo( SwUndoId::END );
2000-09-18 23:08:29 +00:00
EndAllAction();
return *pTable;
}
bool SwEditShell::TextToTable( const SwInsertTableOptions& rInsTableOpts,
sal_Unicode cCh,
const SwTableAutoFormat* pTAFormat )
2000-09-18 23:08:29 +00:00
{
SwWait aWait( *GetDoc()->GetDocShell(), true );
bool bRet = false;
2000-09-18 23:08:29 +00:00
StartAllAction();
for(SwPaM& rPaM : GetCursor()->GetRingContainer())
{
if( rPaM.HasMark() )
bRet |= nullptr != GetDoc()->TextToTable( rInsTableOpts, rPaM, cCh,
css::text::HoriOrientation::FULL, pTAFormat );
}
2000-09-18 23:08:29 +00:00
EndAllAction();
return bRet;
}
bool SwEditShell::TableToText( sal_Unicode cCh )
2000-09-18 23:08:29 +00:00
{
SwWait aWait( *GetDoc()->GetDocShell(), true );
bool bRet = false;
SwPaM* pCursor = GetCursor();
const SwTableNode* pTableNd =
GetDoc()->IsIdxInTable( pCursor->GetPoint()->nNode );
2000-09-18 23:08:29 +00:00
if( IsTableMode() )
{
ClearMark();
pCursor = GetCursor();
2000-09-18 23:08:29 +00:00
}
else if( !pTableNd || pCursor->GetNext() != pCursor )
2000-09-18 23:08:29 +00:00
return bRet;
// TL_CHART2:
// tell the charts about the table to be deleted and have them use their own data
GetDoc()->getIDocumentChartDataProviderAccess().CreateChartInternalDataProviders( &pTableNd->GetTable() );
2000-09-18 23:08:29 +00:00
StartAllAction();
// move current Cursor out of the listing area
SwNodeIndex aTabIdx( *pTableNd );
pCursor->DeleteMark();
pCursor->GetPoint()->nNode = *pTableNd->EndOfSectionNode();
pCursor->GetPoint()->nContent.Assign( nullptr, 0 );
// move sPoint and Mark out of the area!
pCursor->SetMark();
pCursor->DeleteMark();
2000-09-18 23:08:29 +00:00
//Modified for bug #i119954# Application crashed if undo/redo covert nest table to text
StartUndo();
bRet = ConvertTableToText( pTableNd, cCh );
EndUndo();
//End for bug #i119954#
pCursor->GetPoint()->nNode = aTabIdx;
SwContentNode* pCNd = pCursor->GetContentNode();
2000-09-18 23:08:29 +00:00
if( !pCNd )
pCursor->Move( fnMoveForward, GoInContent );
2000-09-18 23:08:29 +00:00
else
pCursor->GetPoint()->nContent.Assign( pCNd, 0 );
2000-09-18 23:08:29 +00:00
EndAllAction();
return bRet;
}
bool SwEditShell::IsTextToTableAvailable() const
2000-09-18 23:08:29 +00:00
{
bool bOnlyText = false;
for(SwPaM& rPaM : GetCursor()->GetRingContainer())
{
if( rPaM.HasMark() && *rPaM.GetPoint() != *rPaM.GetMark() )
2000-09-18 23:08:29 +00:00
{
bOnlyText = true;
2000-09-18 23:08:29 +00:00
// check if selection is in listing
sal_uLong nStt = rPaM.GetMark()->nNode.GetIndex(),
nEnd = rPaM.GetPoint()->nNode.GetIndex();
if( nStt > nEnd ) { sal_uLong n = nStt; nStt = nEnd; nEnd = n; }
2000-09-18 23:08:29 +00:00
for( ; nStt <= nEnd; ++nStt )
if( !GetDoc()->GetNodes()[ nStt ]->IsTextNode() )
2000-09-18 23:08:29 +00:00
{
bOnlyText = false;
2000-09-18 23:08:29 +00:00
break;
}
if( !bOnlyText )
break;
}
}
2000-09-18 23:08:29 +00:00
return bOnlyText;
}
void SwEditShell::InsertDDETable( const SwInsertTableOptions& rInsTableOpts,
SwDDEFieldType* pDDEType,
sal_uInt16 nRows, sal_uInt16 nCols )
2000-09-18 23:08:29 +00:00
{
SwPosition* pPos = GetCursor()->GetPoint();
2000-09-18 23:08:29 +00:00
StartAllAction();
bool bEndUndo = 0 != pPos->nContent.GetIndex();
2000-09-18 23:08:29 +00:00
if( bEndUndo )
{
StartUndo( SwUndoId::START );
GetDoc()->getIDocumentContentOperations().SplitNode( *pPos, false );
2000-09-18 23:08:29 +00:00
}
const SwInsertTableOptions aInsTableOpts( rInsTableOpts.mnInsMode | tabopts::DEFAULT_BORDER,
rInsTableOpts.mnRowsToRepeat );
SwTable* pTable = const_cast<SwTable*>(GetDoc()->InsertTable( aInsTableOpts, *pPos,
nRows, nCols, css::text::HoriOrientation::FULL ));
SwTableNode* pTableNode = const_cast<SwTableNode*>(pTable->GetTabSortBoxes()[ 0 ]->
GetSttNd()->FindTableNode());
SwDDETable* pDDETable = new SwDDETable( *pTable, pDDEType );
pTableNode->SetNewTable( pDDETable ); // set the DDE table
2000-09-18 23:08:29 +00:00
if( bEndUndo )
EndUndo( SwUndoId::END );
2000-09-18 23:08:29 +00:00
EndAllAction();
}
/** update fields of a listing */
2000-09-18 23:08:29 +00:00
void SwEditShell::UpdateTable()
{
const SwTableNode* pTableNd = IsCursorInTable();
2000-09-18 23:08:29 +00:00
if( pTableNd )
2000-09-18 23:08:29 +00:00
{
StartAllAction();
if( DoesUndo() )
StartUndo();
EndAllTableBoxEdit();
SwTableFormulaUpdate aTableUpdate( &pTableNd->GetTable() );
GetDoc()->getIDocumentFieldsAccess().UpdateTableFields( &aTableUpdate );
if( DoesUndo() )
EndUndo();
2000-09-18 23:08:29 +00:00
EndAllAction();
}
}
// get/set Change Mode
TableChgMode SwEditShell::GetTableChgMode() const
2000-09-18 23:08:29 +00:00
{
TableChgMode eMode;
const SwTableNode* pTableNd = IsCursorInTable();
if( pTableNd )
eMode = pTableNd->GetTable().GetTableChgMode();
2000-09-18 23:08:29 +00:00
else
eMode = GetTableChgDefaultMode();
return eMode;
2000-09-18 23:08:29 +00:00
}
void SwEditShell::SetTableChgMode( TableChgMode eMode )
2000-09-18 23:08:29 +00:00
{
const SwTableNode* pTableNd = IsCursorInTable();
2000-09-18 23:08:29 +00:00
if( pTableNd )
2000-09-18 23:08:29 +00:00
{
const_cast<SwTable&>(pTableNd->GetTable()).SetTableChgMode( eMode );
if( !GetDoc()->getIDocumentState().IsModified() ) // Bug 57028
{
GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
}
GetDoc()->getIDocumentState().SetModified();
2000-09-18 23:08:29 +00:00
}
}
bool SwEditShell::GetTableBoxFormulaAttrs( SfxItemSet& rSet ) const
2000-09-18 23:08:29 +00:00
{
SwSelBoxes aBoxes;
if( IsTableMode() )
::GetTableSelCrs( *this, aBoxes );
2000-09-18 23:08:29 +00:00
else
{
do {
SwFrame *pFrame = GetCurrFrame();
2000-09-18 23:08:29 +00:00
do {
pFrame = pFrame->GetUpper();
} while ( pFrame && !pFrame->IsCellFrame() );
if ( pFrame )
2000-09-18 23:08:29 +00:00
{
SwTableBox *pBox = const_cast<SwTableBox*>(static_cast<SwCellFrame*>(pFrame)->GetTabBox());
aBoxes.insert( pBox );
2000-09-18 23:08:29 +00:00
}
} while( false );
2000-09-18 23:08:29 +00:00
}
for (size_t n = 0; n < aBoxes.size(); ++n)
2000-09-18 23:08:29 +00:00
{
const SwTableBox* pSelBox = aBoxes[ n ];
const SwTableBoxFormat* pTableFormat = static_cast<SwTableBoxFormat*>(pSelBox->GetFrameFormat());
if( !n )
2000-09-18 23:08:29 +00:00
{
// Convert formulae into external presentation
const SwTable& rTable = pSelBox->GetSttNd()->FindTableNode()->GetTable();
2000-09-18 23:08:29 +00:00
SwTableFormulaUpdate aTableUpdate( &rTable );
aTableUpdate.m_eFlags = TBL_BOXNAME;
GetDoc()->getIDocumentFieldsAccess().UpdateTableFields( &aTableUpdate );
2000-09-18 23:08:29 +00:00
rSet.Put( pTableFormat->GetAttrSet() );
2000-09-18 23:08:29 +00:00
}
else
rSet.MergeValues( pTableFormat->GetAttrSet() );
2000-09-18 23:08:29 +00:00
}
return 0 != rSet.Count();
}
void SwEditShell::SetTableBoxFormulaAttrs( const SfxItemSet& rSet )
2000-09-18 23:08:29 +00:00
{
SET_CURR_SHELL( this );
SwSelBoxes aBoxes;
if( IsTableMode() )
::GetTableSelCrs( *this, aBoxes );
2000-09-18 23:08:29 +00:00
else
{
do {
SwFrame *pFrame = GetCurrFrame();
2000-09-18 23:08:29 +00:00
do {
pFrame = pFrame->GetUpper();
} while ( pFrame && !pFrame->IsCellFrame() );
if ( pFrame )
2000-09-18 23:08:29 +00:00
{
SwTableBox *pBox = const_cast<SwTableBox*>(static_cast<SwCellFrame*>(pFrame)->GetTabBox());
aBoxes.insert( pBox );
2000-09-18 23:08:29 +00:00
}
} while( false );
2000-09-18 23:08:29 +00:00
}
// When setting a formula, do not check further!
if( SfxItemState::SET == rSet.GetItemState( RES_BOXATR_FORMULA ))
ClearTableBoxContent();
2000-09-18 23:08:29 +00:00
StartAllAction();
GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
for (size_t n = 0; n < aBoxes.size(); ++n)
{
GetDoc()->SetTableBoxFormulaAttrs( *aBoxes[ n ], rSet );
}
GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
2000-09-18 23:08:29 +00:00
EndAllAction();
}
bool SwEditShell::IsTableBoxTextFormat() const
2000-09-18 23:08:29 +00:00
{
if( IsTableMode() )
return false;
2000-09-18 23:08:29 +00:00
const SwTableBox *pBox = nullptr;
2000-09-18 23:08:29 +00:00
{
SwFrame *pFrame = GetCurrFrame();
2000-09-18 23:08:29 +00:00
do {
pFrame = pFrame->GetUpper();
} while ( pFrame && !pFrame->IsCellFrame() );
if ( pFrame )
pBox = static_cast<SwCellFrame*>(pFrame)->GetTabBox();
2000-09-18 23:08:29 +00:00
}
if( !pBox )
return false;
2000-09-18 23:08:29 +00:00
sal_uInt32 nFormat = 0;
2000-09-18 23:08:29 +00:00
const SfxPoolItem* pItem;
if( SfxItemState::SET == pBox->GetFrameFormat()->GetAttrSet().GetItemState(
RES_BOXATR_FORMAT, true, &pItem ))
2000-09-18 23:08:29 +00:00
{
nFormat = static_cast<const SwTableBoxNumFormat*>(pItem)->GetValue();
return GetDoc()->GetNumberFormatter()->IsTextFormat( nFormat ) ||
static_cast<sal_uInt32>(css::util::NumberFormat::TEXT) == nFormat;
2000-09-18 23:08:29 +00:00
}
sal_uLong nNd = pBox->IsValidNumTextNd();
2000-09-18 23:08:29 +00:00
if( ULONG_MAX == nNd )
return true;
2000-09-18 23:08:29 +00:00
const OUString& rText = GetDoc()->GetNodes()[ nNd ]->GetTextNode()->GetText();
if( rText.isEmpty() )
return false;
2000-09-18 23:08:29 +00:00
double fVal;
return !GetDoc()->IsNumberFormat( rText, nFormat, fVal );
2000-09-18 23:08:29 +00:00
}
OUString SwEditShell::GetTableBoxText() const
2001-03-12 17:48:10 +00:00
{
OUString sRet;
2001-03-12 17:48:10 +00:00
if( !IsTableMode() )
{
const SwTableBox *pBox = nullptr;
2001-03-12 17:48:10 +00:00
{
SwFrame *pFrame = GetCurrFrame();
2001-03-12 17:48:10 +00:00
do {
pFrame = pFrame->GetUpper();
} while ( pFrame && !pFrame->IsCellFrame() );
if ( pFrame )
pBox = static_cast<SwCellFrame*>(pFrame)->GetTabBox();
2001-03-12 17:48:10 +00:00
}
sal_uLong nNd;
if( pBox && ULONG_MAX != ( nNd = pBox->IsValidNumTextNd() ) )
sRet = GetDoc()->GetNodes()[ nNd ]->GetTextNode()->GetText();
2001-03-12 17:48:10 +00:00
}
return sRet;
}
2000-09-18 23:08:29 +00:00
bool SwEditShell::SplitTable( SplitTable_HeadlineOption eMode )
2000-09-18 23:08:29 +00:00
{
bool bRet = false;
SwPaM *pCursor = GetCursor();
if( pCursor->GetNode().FindTableNode() )
2000-09-18 23:08:29 +00:00
{
StartAllAction();
GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY, nullptr);
2000-09-18 23:08:29 +00:00
bRet = GetDoc()->SplitTable( *pCursor->GetPoint(), eMode, true );
2000-09-18 23:08:29 +00:00
GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
2000-09-18 23:08:29 +00:00
ClearFEShellTabCols();
EndAllAction();
}
return bRet;
}
bool SwEditShell::MergeTable( bool bWithPrev )
2000-09-18 23:08:29 +00:00
{
bool bRet = false;
SwPaM *pCursor = GetCursor();
if( pCursor->GetNode().FindTableNode() )
2000-09-18 23:08:29 +00:00
{
StartAllAction();
GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY, nullptr);
2000-09-18 23:08:29 +00:00
bRet = GetDoc()->MergeTable( *pCursor->GetPoint(), bWithPrev );
2000-09-18 23:08:29 +00:00
GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
2000-09-18 23:08:29 +00:00
ClearFEShellTabCols();
EndAllAction();
}
return bRet;
}
bool SwEditShell::CanMergeTable( bool bWithPrev, bool* pChkNxtPrv ) const
2000-09-18 23:08:29 +00:00
{
bool bRet = false;
const SwPaM *pCursor = GetCursor();
const SwTableNode* pTableNd = pCursor->GetNode().FindTableNode();
if( pTableNd && dynamic_cast< const SwDDETable* >(&pTableNd->GetTable()) == nullptr)
2000-09-18 23:08:29 +00:00
{
bool bNew = pTableNd->GetTable().IsNewModel();
2000-09-18 23:08:29 +00:00
const SwNodes& rNds = GetDoc()->GetNodes();
if( pChkNxtPrv )
{
const SwTableNode* pChkNd = rNds[ pTableNd->GetIndex() - 1 ]->FindTableNode();
if( pChkNd && dynamic_cast< const SwDDETable* >(&pChkNd->GetTable()) == nullptr &&
bNew == pChkNd->GetTable().IsNewModel() &&
2011-02-08 08:51:51 +01:00
// Consider table in table case
pChkNd->EndOfSectionIndex() == pTableNd->GetIndex() - 1 )
{
*pChkNxtPrv = true;
bRet = true; // using Prev is possible
}
2000-09-18 23:08:29 +00:00
else
{
pChkNd = rNds[ pTableNd->EndOfSectionIndex() + 1 ]->GetTableNode();
if( pChkNd && dynamic_cast< const SwDDETable* >(&pChkNd->GetTable()) == nullptr &&
bNew == pChkNd->GetTable().IsNewModel() )
{
*pChkNxtPrv = false;
bRet = true; // using Next is possible
}
2000-09-18 23:08:29 +00:00
}
}
else
{
const SwTableNode* pTmpTableNd = nullptr;
2000-09-18 23:08:29 +00:00
if( bWithPrev )
{
pTmpTableNd = rNds[ pTableNd->GetIndex() - 1 ]->FindTableNode();
2011-02-08 08:51:51 +01:00
// Consider table in table case
if ( pTmpTableNd && pTmpTableNd->EndOfSectionIndex() != pTableNd->GetIndex() - 1 )
pTmpTableNd = nullptr;
}
2000-09-18 23:08:29 +00:00
else
pTmpTableNd = rNds[ pTableNd->EndOfSectionIndex() + 1 ]->GetTableNode();
2000-09-18 23:08:29 +00:00
bRet = pTmpTableNd && dynamic_cast< const SwDDETable* >(&pTmpTableNd->GetTable()) == nullptr &&
bNew == pTmpTableNd->GetTable().IsNewModel();
2000-09-18 23:08:29 +00:00
}
}
return bRet;
}
/** create InsertDB as table Undo */
void SwEditShell::AppendUndoForInsertFromDB( bool bIsTable )
{
GetDoc()->AppendUndoForInsertFromDB( *GetCursor(), bIsTable );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */