lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / table / tablecontroller.cxx
blob38a41b5ae04f031fbe95fcb121dea30b95b938f5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <algorithm>
22 #include <svx/sdr/table/tablecontroller.hxx>
23 #include <tablemodel.hxx>
25 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
26 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/table/XMergeableCellRange.hpp>
30 #include <com/sun/star/table/XMergeableCell.hpp>
32 #include <sal/config.h>
34 #include <vcl/svapp.hxx>
35 #include <vcl/settings.hxx>
37 #include <svl/whiter.hxx>
39 #include <sfx2/request.hxx>
41 #include <editeng/scripttypeitem.hxx>
42 #include <svx/svdotable.hxx>
43 #include <svx/sdr/overlay/overlayobjectcell.hxx>
44 #include <svx/sdr/overlay/overlaymanager.hxx>
45 #include <svx/svxids.hrc>
46 #include <editeng/outlobj.hxx>
47 #include <svx/svdoutl.hxx>
48 #include <svx/svdpagv.hxx>
49 #include <svx/svdetc.hxx>
50 #include <editeng/editobj.hxx>
51 #include "editeng/editstat.hxx"
52 #include "editeng/unolingu.hxx"
53 #include "svx/sdrpagewindow.hxx"
54 #include <svx/selectioncontroller.hxx>
55 #include <svx/svdmodel.hxx>
56 #include "svx/sdrpaintwindow.hxx"
57 #include <svx/svxdlg.hxx>
58 #include <editeng/boxitem.hxx>
59 #include "cell.hxx"
60 #include <editeng/borderline.hxx>
61 #include <editeng/colritem.hxx>
62 #include "editeng/lineitem.hxx"
63 #include "svx/svdstr.hrc"
64 #include "svdglob.hxx"
65 #include "svx/svdpage.hxx"
66 #include "tableundo.hxx"
67 #include "tablelayouter.hxx"
68 #include <vcl/msgbox.hxx>
69 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
70 #include <memory>
71 #include <o3tl/enumarray.hxx>
72 #include <o3tl/enumrange.hxx>
74 using ::editeng::SvxBorderLine;
75 using namespace sdr::table;
76 using namespace ::com::sun::star;
77 using namespace ::com::sun::star::uno;
78 using namespace ::com::sun::star::table;
79 using namespace ::com::sun::star::beans;
80 using namespace ::com::sun::star::container;
81 using namespace ::com::sun::star::text;
82 using namespace ::com::sun::star::style;
84 namespace sdr { namespace table {
86 class SvxTableControllerModifyListener : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XModifyListener >
88 public:
89 SvxTableControllerModifyListener( SvxTableController* pController )
90 : mpController( pController ) {}
92 // XModifyListener
93 virtual void SAL_CALL modified( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
95 // XEventListener
96 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
98 SvxTableController* mpController;
102 // XModifyListener
105 void SAL_CALL SvxTableControllerModifyListener::modified( const ::com::sun::star::lang::EventObject& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
107 if( mpController )
108 mpController->onTableModified();
112 // XEventListener
115 void SAL_CALL SvxTableControllerModifyListener::disposing( const ::com::sun::star::lang::EventObject& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
117 mpController = 0;
121 // class SvxTableController
124 rtl::Reference< sdr::SelectionController > CreateTableController( SdrObjEditView* pView, const SdrObject* pObj, const rtl::Reference< sdr::SelectionController >& xRefController )
126 return SvxTableController::create( pView, pObj, xRefController );
131 rtl::Reference< sdr::SelectionController > SvxTableController::create( SdrObjEditView* pView, const SdrObject* pObj, const rtl::Reference< sdr::SelectionController >& xRefController )
133 if( xRefController.is() )
135 SvxTableController* pController = dynamic_cast< SvxTableController* >( xRefController.get() );
136 if( pController && (pController->mxTableObj.get() == pObj) && (pController->mpView == pView) )
137 return xRefController;
139 return new SvxTableController( pView, pObj );
144 SvxTableController::SvxTableController( SdrObjEditView* pView, const SdrObject* pObj )
145 : mbCellSelectionMode(false)
146 , mbLeftButtonDown(false)
147 , mpSelectionOverlay(0)
148 , mpView( dynamic_cast< SdrView* >( pView ) )
149 , mxTableObj( dynamic_cast< SdrTableObj* >( const_cast< SdrObject* >( pObj ) ) )
150 , mpModel( 0 )
151 , mnUpdateEvent( 0 )
153 if( pObj )
155 mpModel = pObj->GetModel();
157 if( mxTableObj.is() )
159 static_cast< const SdrTableObj* >( pObj )->getActiveCellPos( maCursorFirstPos );
160 maCursorLastPos = maCursorFirstPos;
162 Reference< XTable > xTable( static_cast< const SdrTableObj* >( pObj )->getTable() );
163 if( xTable.is() )
165 mxModifyListener = new SvxTableControllerModifyListener( this );
166 xTable->addModifyListener( mxModifyListener );
168 mxTable.set( dynamic_cast< TableModel* >( xTable.get() ) );
174 SvxTableController::~SvxTableController()
176 if( mnUpdateEvent )
178 Application::RemoveUserEvent( mnUpdateEvent );
181 if( mxModifyListener.is() && mxTableObj.get() )
183 Reference< XTable > xTable( static_cast< SdrTableObj* >( mxTableObj.get() )->getTable() );
184 if( xTable.is() )
186 xTable->removeModifyListener( mxModifyListener );
187 mxModifyListener.clear();
194 const sal_uInt16 ACTION_NONE = 0;
195 const sal_uInt16 ACTION_GOTO_FIRST_CELL = 1;
196 const sal_uInt16 ACTION_GOTO_FIRST_COLUMN = 2;
197 const sal_uInt16 ACTION_GOTO_FIRST_ROW = 3;
198 const sal_uInt16 ACTION_GOTO_LEFT_CELL = 4;
199 const sal_uInt16 ACTION_GOTO_UP_CELL = 5;
200 const sal_uInt16 ACTION_GOTO_RIGHT_CELL = 6;
201 const sal_uInt16 ACTION_GOTO_DOWN_CELL = 7;
202 const sal_uInt16 ACTION_GOTO_LAST_CELL = 8;
203 const sal_uInt16 ACTION_GOTO_LAST_COLUMN = 9;
204 const sal_uInt16 ACTION_GOTO_LAST_ROW = 10;
205 const sal_uInt16 ACTION_EDIT_CELL = 11;
206 const sal_uInt16 ACTION_STOP_TEXT_EDIT = 12;
207 const sal_uInt16 ACTION_REMOVE_SELECTION = 13;
208 const sal_uInt16 ACTION_START_SELECTION = 14;
209 const sal_uInt16 ACTION_HANDLED_BY_VIEW = 15;
210 const sal_uInt16 ACTION_TAB = 18;
212 bool SvxTableController::onKeyInput(const KeyEvent& rKEvt, vcl::Window* pWindow )
214 if( !checkTableObject() )
215 return false;
217 // check if we are read only
218 if( mpModel && mpModel->IsReadOnly())
220 switch( rKEvt.GetKeyCode().GetCode() )
222 case awt::Key::DOWN:
223 case awt::Key::UP:
224 case awt::Key::LEFT:
225 case awt::Key::RIGHT:
226 case awt::Key::TAB:
227 case awt::Key::HOME:
228 case awt::Key::END:
229 case awt::Key::NUM2:
230 case awt::Key::NUM4:
231 case awt::Key::NUM6:
232 case awt::Key::NUM8:
233 case awt::Key::ESCAPE:
234 case awt::Key::F2:
235 break;
236 default:
237 // tell the view we eat the event, no further processing needed
238 return true;
242 sal_uInt16 nAction = getKeyboardAction( rKEvt, pWindow );
244 return executeAction( nAction, rKEvt.GetKeyCode().IsShift(), pWindow );
248 // ::com::sun::star::awt::XMouseClickHandler:
251 bool SvxTableController::onMouseButtonDown(const MouseEvent& rMEvt, vcl::Window* pWindow )
253 if (mxTableObj->GetModel()->isTiledRendering() && !pWindow)
255 // Tiled rendering: get the window that has the disabled map mode.
256 if (OutputDevice* pOutputDevice = mpView->GetFirstOutputDevice())
258 if (pOutputDevice->GetOutDevType() == OUTDEV_WINDOW)
259 pWindow = static_cast<vcl::Window*>(pOutputDevice);
263 if( !pWindow || !checkTableObject() )
264 return false;
266 SdrViewEvent aVEvt;
267 if( !rMEvt.IsRight() && mpView->PickAnything(rMEvt,SdrMouseEventKind::BUTTONDOWN, aVEvt) == SDRHIT_HANDLE )
268 return false;
270 TableHitKind eHit = static_cast< SdrTableObj* >(mxTableObj.get())->CheckTableHit( pWindow->PixelToLogic(rMEvt.GetPosPixel()), maMouseDownPos.mnCol, maMouseDownPos.mnRow, 0 );
272 mbLeftButtonDown = (rMEvt.GetClicks() == 1) && rMEvt.IsLeft();
274 if( eHit == SDRTABLEHIT_CELL )
276 StartSelection( maMouseDownPos );
277 return true;
280 if( rMEvt.IsRight() && eHit != SDRTABLEHIT_NONE )
281 return true; // right click will become context menu
283 // for cell selection with the mouse remember our first hit
284 if( mbLeftButtonDown )
286 RemoveSelection();
288 Point aPnt(rMEvt.GetPosPixel());
289 if (pWindow!=NULL)
290 aPnt=pWindow->PixelToLogic(aPnt);
292 SdrHdl* pHdl = mpView->PickHandle(aPnt);
294 if( pHdl )
296 mbLeftButtonDown = false;
298 else
300 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
302 if( !pWindow || !pTableObj || eHit == SDRTABLEHIT_NONE)
304 mbLeftButtonDown = false;
309 if (mxTableObj->GetModel()->isTiledRendering() && rMEvt.GetClicks() == 2 && rMEvt.IsLeft() && eHit == SDRTABLEHIT_CELLTEXTAREA)
311 bool bEmptyOutliner = false;
312 if (Outliner* pOutliner = mpView->GetTextEditOutliner())
314 if (pOutliner->GetParagraphCount() == 1)
316 if (Paragraph* pParagraph = pOutliner->GetParagraph(0))
317 bEmptyOutliner = pOutliner->GetText(pParagraph).isEmpty();
320 if (bEmptyOutliner)
322 // Tiled rendering: a left double-click in an empty cell: select it.
323 StartSelection(maMouseDownPos);
324 setSelectedCells(maMouseDownPos, maMouseDownPos);
325 // Update graphic selection, should be hidden now.
326 mpView->AdjustMarkHdl();
327 return true;
331 return false;
336 bool SvxTableController::onMouseButtonUp(const MouseEvent& rMEvt, vcl::Window* /*pWin*/)
338 if( !checkTableObject() )
339 return false;
341 mbLeftButtonDown = false;
343 if( rMEvt.GetClicks() == 2 )
344 return true;
346 return false;
351 bool SvxTableController::onMouseMove(const MouseEvent& rMEvt, vcl::Window* pWindow )
353 if( !checkTableObject() )
354 return false;
356 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
357 CellPos aPos;
358 if( mbLeftButtonDown && pTableObj && pTableObj->CheckTableHit( pWindow->PixelToLogic(rMEvt.GetPosPixel()), aPos.mnCol, aPos.mnRow, 0 ) != SDRTABLEHIT_NONE )
360 if(aPos != maMouseDownPos)
362 if( mbCellSelectionMode )
364 setSelectedCells( maMouseDownPos, aPos );
365 return true;
367 else
369 StartSelection( maMouseDownPos );
372 else if( mbCellSelectionMode )
374 UpdateSelection( aPos );
375 return true;
378 return false;
383 void SvxTableController::onSelectionHasChanged()
385 bool bSelected = false;
387 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
388 if( pTableObj && pTableObj->IsTextEditActive() )
390 pTableObj->getActiveCellPos( maCursorFirstPos );
391 maCursorLastPos = maCursorFirstPos;
392 mbCellSelectionMode = false;
394 else
396 const SdrMarkList& rMarkList= mpView->GetMarkedObjectList();
397 if( rMarkList.GetMarkCount() == 1 )
398 bSelected = mxTableObj.get() == rMarkList.GetMark(0)->GetMarkedSdrObj();
399 /* fdo#46186 Selecting the table means selecting the entire cells */
400 if (!hasSelectedCells() && pTableObj)
402 maCursorFirstPos = SdrTableObj::getFirstCell();
403 maCursorLastPos = pTableObj->getLastCell();
404 mbCellSelectionMode=true;
408 if( bSelected )
410 updateSelectionOverlay();
412 else
414 destroySelectionOverlay();
420 void SvxTableController::GetState( SfxItemSet& rSet )
422 if( !mxTable.is() || !mxTableObj.is() || !mxTableObj->GetModel() )
423 return;
425 std::unique_ptr<SfxItemSet> xSet;
427 bool bVertDone = false;
429 // Iterate over all requested items in the set.
430 SfxWhichIter aIter( rSet );
431 sal_uInt16 nWhich = aIter.FirstWhich();
432 while (nWhich)
434 switch (nWhich)
436 case SID_TABLE_VERT_BOTTOM:
437 case SID_TABLE_VERT_CENTER:
438 case SID_TABLE_VERT_NONE:
440 if( !mxTable.is() || !mxTableObj->GetModel() )
442 rSet.DisableItem(nWhich);
444 else if(!bVertDone)
446 if (!xSet)
448 xSet.reset(new SfxItemSet( mxTableObj->GetModel()->GetItemPool() ));
449 MergeAttrFromSelectedCells(*xSet, false);
452 SdrTextVertAdjust eAdj = SDRTEXTVERTADJUST_BLOCK;
454 if (xSet->GetItemState( SDRATTR_TEXT_VERTADJUST ) != SfxItemState::DONTCARE)
455 eAdj = static_cast<const SdrTextVertAdjustItem&>(xSet->Get(SDRATTR_TEXT_VERTADJUST)).GetValue();
457 rSet.Put(SfxBoolItem(SID_TABLE_VERT_BOTTOM, eAdj == SDRTEXTVERTADJUST_BOTTOM));
458 rSet.Put(SfxBoolItem(SID_TABLE_VERT_CENTER, eAdj == SDRTEXTVERTADJUST_CENTER));
459 rSet.Put(SfxBoolItem(SID_TABLE_VERT_NONE, eAdj == SDRTEXTVERTADJUST_TOP));
460 bVertDone = true;
462 break;
464 case SID_TABLE_DELETE_ROW:
465 if( !mxTable.is() || !hasSelectedCells() || (mxTable->getRowCount() <= 1) )
466 rSet.DisableItem(SID_TABLE_DELETE_ROW);
467 break;
468 case SID_TABLE_DELETE_COL:
469 if( !mxTable.is() || !hasSelectedCells() || (mxTable->getColumnCount() <= 1) )
470 rSet.DisableItem(SID_TABLE_DELETE_COL);
471 break;
472 case SID_TABLE_MERGE_CELLS:
473 if( !mxTable.is() || !hasSelectedCells() )
474 rSet.DisableItem(SID_TABLE_MERGE_CELLS);
475 break;
476 case SID_TABLE_SPLIT_CELLS:
477 if( !hasSelectedCells() || !mxTable.is() )
478 rSet.DisableItem(SID_TABLE_SPLIT_CELLS);
479 break;
481 case SID_OPTIMIZE_TABLE:
482 case SID_TABLE_DISTRIBUTE_COLUMNS:
483 case SID_TABLE_DISTRIBUTE_ROWS:
485 bool bDistributeColumns = false;
486 bool bDistributeRows = false;
487 if( mxTable.is() )
489 CellPos aStart, aEnd;
490 getSelectedCells( aStart, aEnd );
492 bDistributeColumns = aStart.mnCol != aEnd.mnCol;
493 bDistributeRows = aStart.mnRow != aEnd.mnRow;
495 if( !bDistributeColumns && !bDistributeRows )
496 rSet.DisableItem(SID_OPTIMIZE_TABLE);
497 if( !bDistributeColumns )
498 rSet.DisableItem(SID_TABLE_DISTRIBUTE_COLUMNS);
499 if( !bDistributeRows )
500 rSet.DisableItem(SID_TABLE_DISTRIBUTE_ROWS);
501 break;
504 case SID_AUTOFORMAT:
505 case SID_TABLE_SORT_DIALOG:
506 case SID_TABLE_AUTOSUM:
507 // if( !mxTable.is() )
508 // rSet.DisableItem( nWhich );
509 break;
510 default:
511 break;
513 nWhich = aIter.NextWhich();
519 void SvxTableController::onInsert( sal_uInt16 nSId, const SfxItemSet* pArgs )
521 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
522 if( !pTableObj )
523 return;
525 if( mxTable.is() ) try
528 bool bInsertAfter = true;
529 sal_uInt16 nCount = 0;
530 if( pArgs )
532 const SfxPoolItem* pItem = 0;
533 pArgs->GetItemState(nSId, false, &pItem);
534 if (pItem)
536 nCount = static_cast<const SfxInt16Item*>(pItem)->GetValue();
537 if(SfxItemState::SET == pArgs->GetItemState(SID_TABLE_PARAM_INSERT_AFTER, true, &pItem))
538 bInsertAfter = static_cast<const SfxBoolItem*>(pItem)->GetValue();
542 CellPos aStart, aEnd;
543 if( hasSelectedCells() )
545 getSelectedCells( aStart, aEnd );
547 else
549 if( bInsertAfter )
551 aStart.mnCol = mxTable->getColumnCount() - 1;
552 aStart.mnRow = mxTable->getRowCount() - 1;
553 aEnd = aStart;
557 if( pTableObj->IsTextEditActive() )
558 mpView->SdrEndTextEdit(true);
560 RemoveSelection();
562 const OUString sSize( "Size" );
564 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
566 switch( nSId )
568 case SID_TABLE_INSERT_COL:
570 TableModelNotifyGuard aGuard( mxTable.get() );
572 if( bUndo )
574 mpModel->BegUndo( ImpGetResStr(STR_TABLE_INSCOL) );
575 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
578 Reference< XTableColumns > xCols( mxTable->getColumns() );
579 const sal_Int32 nNewColumns = (nCount == 0) ? (aEnd.mnCol - aStart.mnCol + 1) : nCount;
580 const sal_Int32 nNewStartColumn = aEnd.mnCol + (bInsertAfter ? 1 : 0);
581 xCols->insertByIndex( nNewStartColumn, nNewColumns );
583 for( sal_Int32 nOffset = 0; nOffset < nNewColumns; nOffset++ )
585 // Resolves fdo#61540
586 // On Insert before, the reference column whose size is going to be
587 // used for newly created column(s) is wrong. As the new columns are
588 // inserted before the reference column, the reference column moved
589 // to the new position by no., of new columns i.e (earlier+newcolumns).
590 Reference< XPropertySet >(xCols->getByIndex(nNewStartColumn+nOffset), UNO_QUERY_THROW )->
591 setPropertyValue( sSize,
592 Reference< XPropertySet >(xCols->getByIndex( bInsertAfter?nNewStartColumn-1:nNewStartColumn+nNewColumns ), UNO_QUERY_THROW )->
593 getPropertyValue( sSize ) );
596 // Copy cell properties
597 sal_Int32 nPropSrcCol = (bInsertAfter ? aEnd.mnCol : aStart.mnCol + nNewColumns);
598 sal_Int32 nRowSpan = 0;
599 bool bNewSpan = false;
601 for( sal_Int32 nRow = 0; nRow < mxTable->getRowCount(); ++nRow )
603 CellRef xSourceCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nPropSrcCol, nRow ).get() ) );
605 // When we insert new COLUMNs, we want to copy ROW spans.
606 if (xSourceCell.is() && nRowSpan == 0)
608 // we are not in a span yet. Let's find out if the current cell is in a span.
609 sal_Int32 nColSpan = sal_Int32();
610 sal_Int32 nSpanInfoCol = sal_Int32();
612 if( xSourceCell->getRowSpan() > 1 )
614 // The current cell is the top-left cell in a span.
615 // Get the span info and propagate it to the target.
616 nRowSpan = xSourceCell->getRowSpan();
617 nColSpan = xSourceCell->getColumnSpan();
618 nSpanInfoCol = nPropSrcCol;
620 else if( xSourceCell->isMerged() )
622 // The current cell is a middle cell in a 2D span.
623 // Look for the top-left cell in the span.
624 for( nSpanInfoCol = nPropSrcCol - 1; nSpanInfoCol >= 0; --nSpanInfoCol )
626 CellRef xMergeInfoCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nSpanInfoCol, nRow ).get() ) );
627 if (xMergeInfoCell.is() && !xMergeInfoCell->isMerged())
629 nRowSpan = xMergeInfoCell->getRowSpan();
630 nColSpan = xMergeInfoCell->getColumnSpan();
631 break;
634 if( nRowSpan == 1 )
635 nRowSpan = 0;
638 // The target colomns are outside the span; Start a new span.
639 if( nRowSpan > 0 && ( nNewStartColumn < nSpanInfoCol || nSpanInfoCol + nColSpan <= nNewStartColumn ) )
640 bNewSpan = true;
643 // Now copy the properties from the source to the targets
644 for( sal_Int32 nOffset = 0; nOffset < nNewColumns; nOffset++ )
646 CellRef xTargetCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nNewStartColumn + nOffset, nRow ).get() ) );
647 if( xTargetCell.is() )
649 if( nRowSpan > 0 )
651 if( bNewSpan )
652 xTargetCell->merge( 1, nRowSpan );
653 else
654 xTargetCell->setMerged();
656 xTargetCell->copyFormatFrom( xSourceCell );
660 if( nRowSpan > 0 )
662 --nRowSpan;
663 bNewSpan = false;
667 if( bUndo )
668 mpModel->EndUndo();
670 aStart.mnCol = nNewStartColumn;
671 aStart.mnRow = 0;
672 aEnd.mnCol = aStart.mnCol + nNewColumns - 1;
673 aEnd.mnRow = mxTable->getRowCount() - 1;
674 break;
677 case SID_TABLE_INSERT_ROW:
679 TableModelNotifyGuard aGuard( mxTable.get() );
681 if( bUndo )
683 mpModel->BegUndo( ImpGetResStr(STR_TABLE_INSROW ) );
684 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
687 Reference< XTableRows > xRows( mxTable->getRows() );
688 const sal_Int32 nNewRows = (nCount == 0) ? (aEnd.mnRow - aStart.mnRow + 1) : nCount;
689 const sal_Int32 nNewRowStart = aEnd.mnRow + (bInsertAfter ? 1 : 0);
690 xRows->insertByIndex( nNewRowStart, nNewRows );
692 for( sal_Int32 nOffset = 0; nOffset < nNewRows; nOffset++ )
694 Reference< XPropertySet >( xRows->getByIndex( aEnd.mnRow + nOffset + 1 ), UNO_QUERY_THROW )->
695 setPropertyValue( sSize,
696 Reference< XPropertySet >( xRows->getByIndex( aStart.mnRow + nOffset ), UNO_QUERY_THROW )->
697 getPropertyValue( sSize ) );
700 // Copy the cell properties
701 sal_Int32 nPropSrcRow = (bInsertAfter ? aEnd.mnRow : aStart.mnRow + nNewRows);
702 sal_Int32 nColSpan = 0;
703 bool bNewSpan = false;
705 for( sal_Int32 nCol = 0; nCol < mxTable->getColumnCount(); ++nCol )
707 CellRef xSourceCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nPropSrcRow ).get() ) );
709 if (!xSourceCell.is())
710 continue;
712 // When we insert new ROWs, we want to copy COLUMN spans.
713 if( nColSpan == 0 )
715 // we are not in a span yet. Let's find out if the current cell is in a span.
716 sal_Int32 nRowSpan = sal_Int32();
717 sal_Int32 nSpanInfoRow = sal_Int32();
719 if( xSourceCell->getColumnSpan() > 1 )
721 // The current cell is the top-left cell in a span.
722 // Get the span info and propagate it to the target.
723 nColSpan = xSourceCell->getColumnSpan();
724 nRowSpan = xSourceCell->getRowSpan();
725 nSpanInfoRow = nPropSrcRow;
727 else if( xSourceCell->isMerged() )
729 // The current cell is a middle cell in a 2D span.
730 // Look for the top-left cell in the span.
731 for( nSpanInfoRow = nPropSrcRow - 1; nSpanInfoRow >= 0; --nSpanInfoRow )
733 CellRef xMergeInfoCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nSpanInfoRow ).get() ) );
734 if (xMergeInfoCell.is() && !xMergeInfoCell->isMerged())
736 nColSpan = xMergeInfoCell->getColumnSpan();
737 nRowSpan = xMergeInfoCell->getRowSpan();
738 break;
741 if( nColSpan == 1 )
742 nColSpan = 0;
745 // Inserted rows are outside the span; Start a new span.
746 if( nColSpan > 0 && ( nNewRowStart < nSpanInfoRow || nSpanInfoRow + nRowSpan <= nNewRowStart ) )
747 bNewSpan = true;
750 // Now copy the properties from the source to the targets
751 for( sal_Int32 nOffset = 0; nOffset < nNewRows; ++nOffset )
753 CellRef xTargetCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nNewRowStart + nOffset ).get() ) );
754 if( xTargetCell.is() )
756 if( nColSpan > 0 )
758 if( bNewSpan )
759 xTargetCell->merge( nColSpan, 1 );
760 else
761 xTargetCell->setMerged();
763 xTargetCell->copyFormatFrom( xSourceCell );
767 if( nColSpan > 0 )
769 --nColSpan;
770 bNewSpan = false;
774 if( bUndo )
775 mpModel->EndUndo();
777 aStart.mnCol = 0;
778 aStart.mnRow = nNewRowStart;
779 aEnd.mnCol = mxTable->getColumnCount() - 1;
780 aEnd.mnRow = aStart.mnRow + nNewRows - 1;
781 break;
785 StartSelection( aStart );
786 UpdateSelection( aEnd );
788 catch( Exception& )
790 OSL_FAIL("svx::SvxTableController::onInsert(), exception caught!");
796 void SvxTableController::onDelete( sal_uInt16 nSId )
798 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
799 if( !pTableObj )
800 return;
802 if( mxTable.is() && hasSelectedCells() )
804 CellPos aStart, aEnd;
805 getSelectedCells( aStart, aEnd );
807 if( pTableObj->IsTextEditActive() )
808 mpView->SdrEndTextEdit(true);
810 RemoveSelection();
812 bool bDeleteTable = false;
813 switch( nSId )
815 case SID_TABLE_DELETE_COL:
817 const sal_Int32 nRemovedColumns = aEnd.mnCol - aStart.mnCol + 1;
818 if( nRemovedColumns == mxTable->getColumnCount() )
820 bDeleteTable = true;
822 else
824 Reference< XTableColumns > xCols( mxTable->getColumns() );
825 xCols->removeByIndex( aStart.mnCol, nRemovedColumns );
827 break;
830 case SID_TABLE_DELETE_ROW:
832 const sal_Int32 nRemovedRows = aEnd.mnRow - aStart.mnRow + 1;
833 if( nRemovedRows == mxTable->getRowCount() )
835 bDeleteTable = true;
837 else
839 Reference< XTableRows > xRows( mxTable->getRows() );
840 xRows->removeByIndex( aStart.mnRow, nRemovedRows );
842 break;
846 if( bDeleteTable )
847 mpView->DeleteMarkedObj();
848 else
849 UpdateTableShape();
855 void SvxTableController::onSelect( sal_uInt16 nSId )
857 if( mxTable.is() )
859 const sal_Int32 nRowCount = mxTable->getRowCount();
860 const sal_Int32 nColCount = mxTable->getColumnCount();
861 if( nRowCount && nColCount )
863 CellPos aStart, aEnd;
864 getSelectedCells( aStart, aEnd );
866 switch( nSId )
868 case SID_TABLE_SELECT_ALL:
869 aEnd.mnCol = 0; aEnd.mnRow = 0;
870 aStart.mnCol = nColCount - 1; aStart.mnRow = nRowCount - 1;
871 break;
872 case SID_TABLE_SELECT_COL:
873 aEnd.mnRow = nRowCount - 1;
874 aStart.mnRow = 0;
875 break;
876 case SID_TABLE_SELECT_ROW:
877 aEnd.mnCol = nColCount - 1;
878 aStart.mnCol = 0;
879 break;
882 StartSelection( aEnd );
883 gotoCell( aStart, true, 0 );
888 namespace
890 SvxBoxItem mergeDrawinglayerTextDistancesAndSvxBoxItem(const SfxItemSet& rAttrSet)
892 // merge drawing layer text distance items into SvxBoxItem used by the dialog
893 SvxBoxItem aBoxItem( static_cast< const SvxBoxItem& >( rAttrSet.Get( SDRATTR_TABLE_BORDER ) ) );
894 aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( static_cast<const SdrMetricItem&>(rAttrSet.Get(SDRATTR_TEXT_LEFTDIST)).GetValue()), SvxBoxItemLine::LEFT );
895 aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( static_cast<const SdrMetricItem&>(rAttrSet.Get(SDRATTR_TEXT_RIGHTDIST)).GetValue()), SvxBoxItemLine::RIGHT );
896 aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( static_cast<const SdrMetricItem&>(rAttrSet.Get(SDRATTR_TEXT_UPPERDIST)).GetValue()), SvxBoxItemLine::TOP );
897 aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( static_cast<const SdrMetricItem&>(rAttrSet.Get(SDRATTR_TEXT_LOWERDIST)).GetValue()), SvxBoxItemLine::BOTTOM );
898 return aBoxItem;
902 void SvxTableController::onFormatTable( SfxRequest& rReq )
904 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
905 if( !pTableObj )
906 return;
908 const SfxItemSet* pArgs = rReq.GetArgs();
910 if( !pArgs && pTableObj->GetModel() )
912 SfxItemSet aNewAttr( pTableObj->GetModel()->GetItemPool() );
914 // merge drawing layer text distance items into SvxBoxItem used by the dialog
915 SvxBoxItem aBoxItem(mergeDrawinglayerTextDistancesAndSvxBoxItem(aNewAttr));
917 SvxBoxInfoItem aBoxInfoItem( static_cast< const SvxBoxInfoItem& >( aNewAttr.Get( SDRATTR_TABLE_BORDER_INNER ) ) );
919 MergeAttrFromSelectedCells(aNewAttr, false);
920 FillCommonBorderAttrFromSelectedCells( aBoxItem, aBoxInfoItem );
921 aNewAttr.Put( aBoxItem );
922 aNewAttr.Put( aBoxInfoItem );
924 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
925 std::unique_ptr< SfxAbstractTabDialog > xDlg( pFact ? pFact->CreateSvxFormatCellsDialog( NULL, &aNewAttr, pTableObj->GetModel(), pTableObj) : 0 );
926 // Even Cancel Button is returning positive(101) value,
927 if (xDlg.get() && xDlg->Execute() == RET_OK)
929 SfxItemSet aNewSet(*(xDlg->GetOutputItemSet()));
931 //Only properties that were unchanged by the dialog appear in this
932 //itemset. We had constructed these two properties from other
933 //ones, so if they were not changed, then forcible set them back to
934 //their originals in the new result set so we can decompose that
935 //unchanged state back to their input properties
936 if (aNewSet.GetItemState(SDRATTR_TABLE_BORDER, false) != SfxItemState::SET)
938 aNewSet.Put(aBoxItem);
940 if (aNewSet.GetItemState(SDRATTR_TABLE_BORDER_INNER, false) != SfxItemState::SET)
942 aNewSet.Put(aBoxInfoItem);
945 SvxBoxItem aNewBoxItem( static_cast< const SvxBoxItem& >( aNewSet.Get( SDRATTR_TABLE_BORDER ) ) );
947 if( aNewBoxItem.GetDistance( SvxBoxItemLine::LEFT ) != aBoxItem.GetDistance( SvxBoxItemLine::LEFT ) )
948 aNewSet.Put(makeSdrTextLeftDistItem( aNewBoxItem.GetDistance( SvxBoxItemLine::LEFT ) ) );
950 if( aNewBoxItem.GetDistance( SvxBoxItemLine::RIGHT ) != aBoxItem.GetDistance( SvxBoxItemLine::RIGHT ) )
951 aNewSet.Put(makeSdrTextRightDistItem( aNewBoxItem.GetDistance( SvxBoxItemLine::RIGHT ) ) );
953 if( aNewBoxItem.GetDistance( SvxBoxItemLine::TOP ) != aBoxItem.GetDistance( SvxBoxItemLine::TOP ) )
954 aNewSet.Put(makeSdrTextUpperDistItem( aNewBoxItem.GetDistance( SvxBoxItemLine::TOP ) ) );
956 if( aNewBoxItem.GetDistance( SvxBoxItemLine::BOTTOM ) != aBoxItem.GetDistance( SvxBoxItemLine::BOTTOM ) )
957 aNewSet.Put(makeSdrTextLowerDistItem( aNewBoxItem.GetDistance( SvxBoxItemLine::BOTTOM ) ) );
959 SetAttrToSelectedCells(aNewSet, false);
964 void SvxTableController::Execute( SfxRequest& rReq )
966 const sal_uInt16 nSId = rReq.GetSlot();
967 switch( nSId )
969 case SID_TABLE_INSERT_ROW:
970 case SID_TABLE_INSERT_COL:
971 onInsert( nSId, rReq.GetArgs() );
972 break;
973 case SID_TABLE_DELETE_ROW:
974 case SID_TABLE_DELETE_COL:
975 onDelete( nSId );
976 break;
977 case SID_TABLE_SELECT_ALL:
978 case SID_TABLE_SELECT_COL:
979 case SID_TABLE_SELECT_ROW:
980 onSelect( nSId );
981 break;
982 case SID_FORMAT_TABLE_DLG:
983 onFormatTable( rReq );
984 break;
986 case SID_FRAME_LINESTYLE:
987 case SID_FRAME_LINECOLOR:
988 case SID_ATTR_BORDER:
990 const SfxItemSet* pArgs = rReq.GetArgs();
991 if( pArgs )
992 ApplyBorderAttr( *pArgs );
994 break;
996 case SID_ATTR_FILL_STYLE:
998 const SfxItemSet* pArgs = rReq.GetArgs();
999 if( pArgs )
1000 SetAttributes( *pArgs, false );
1002 break;
1004 case SID_TABLE_MERGE_CELLS:
1005 MergeMarkedCells();
1006 break;
1008 case SID_TABLE_SPLIT_CELLS:
1009 SplitMarkedCells();
1010 break;
1012 case SID_TABLE_DISTRIBUTE_COLUMNS:
1013 DistributeColumns();
1014 break;
1016 case SID_TABLE_DISTRIBUTE_ROWS:
1017 DistributeRows();
1018 break;
1020 case SID_TABLE_VERT_BOTTOM:
1021 case SID_TABLE_VERT_CENTER:
1022 case SID_TABLE_VERT_NONE:
1023 SetVertical( nSId );
1024 break;
1026 case SID_AUTOFORMAT:
1027 case SID_TABLE_SORT_DIALOG:
1028 case SID_TABLE_AUTOSUM:
1029 default:
1030 break;
1032 case SID_TABLE_STYLE:
1033 SetTableStyle( rReq.GetArgs() );
1034 break;
1036 case SID_TABLE_STYLE_SETTINGS:
1037 SetTableStyleSettings( rReq.GetArgs() );
1038 break;
1042 void SvxTableController::SetTableStyle( const SfxItemSet* pArgs )
1044 SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
1045 SdrModel* pModel = pTableObj ? pTableObj->GetModel() : 0;
1047 if( !pTableObj || !pModel || !pArgs || (SfxItemState::SET != pArgs->GetItemState(SID_TABLE_STYLE, false)) )
1048 return;
1050 const SfxStringItem* pArg = dynamic_cast< const SfxStringItem* >( &pArgs->Get( SID_TABLE_STYLE ) );
1051 if( pArg && mxTable.is() ) try
1053 Reference< XStyleFamiliesSupplier > xSFS( pModel->getUnoModel(), UNO_QUERY_THROW );
1054 Reference< XNameAccess > xFamilyNameAccess( xSFS->getStyleFamilies(), UNO_QUERY_THROW );
1055 const OUString sFamilyName( "table" );
1056 Reference< XNameAccess > xTableFamilyAccess( xFamilyNameAccess->getByName( sFamilyName ), UNO_QUERY_THROW );
1058 if( xTableFamilyAccess->hasByName( pArg->GetValue() ) )
1060 // found table style with the same name
1061 Reference< XIndexAccess > xNewTableStyle( xTableFamilyAccess->getByName( pArg->GetValue() ), UNO_QUERY_THROW );
1063 const bool bUndo = pModel->IsUndoEnabled();
1065 if( bUndo )
1067 pModel->BegUndo( ImpGetResStr(STR_TABLE_STYLE) );
1068 pModel->AddUndo( new TableStyleUndo( *pTableObj ) );
1071 pTableObj->setTableStyle( xNewTableStyle );
1073 const sal_Int32 nRowCount = mxTable->getRowCount();
1074 const sal_Int32 nColCount = mxTable->getColumnCount();
1075 for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ )
1077 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
1079 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1080 if( xCell.is() )
1082 SfxItemSet aSet( xCell->GetItemSet() );
1083 bool bChanges = false;
1084 const SfxItemSet& rStyleAttribs = xCell->GetStyleSheet()->GetItemSet();
1086 for ( sal_uInt16 nWhich = SDRATTR_START; nWhich <= SDRATTR_TABLE_LAST; nWhich++ )
1088 if( (rStyleAttribs.GetItemState( nWhich ) == SfxItemState::SET) && (aSet.GetItemState( nWhich ) == SfxItemState::SET) )
1090 aSet.ClearItem( nWhich );
1091 bChanges = true;
1095 if( bChanges )
1097 if( bUndo )
1098 xCell->AddUndo();
1100 xCell->SetMergedItemSetAndBroadcast( aSet, true );
1104 catch( Exception& )
1106 OSL_FAIL( "svx::SvxTableController::SetTableStyle(), exception caught!" );
1110 if( bUndo )
1111 pModel->EndUndo();
1114 catch( Exception& )
1116 OSL_FAIL( "svx::SvxTableController::SetTableStyle(), exception caught!" );
1120 void SvxTableController::SetTableStyleSettings( const SfxItemSet* pArgs )
1122 SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
1123 SdrModel* pModel = pTableObj ? pTableObj->GetModel() : 0;
1125 if( !pTableObj || !pModel )
1126 return;
1128 TableStyleSettings aSettings( pTableObj->getTableStyleSettings() );
1130 const SfxPoolItem *pPoolItem=NULL;
1132 if( (SfxItemState::SET == pArgs->GetItemState(ID_VAL_USEFIRSTROWSTYLE, false,&pPoolItem)) )
1133 aSettings.mbUseFirstRow = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
1135 if( (SfxItemState::SET == pArgs->GetItemState(ID_VAL_USELASTROWSTYLE, false,&pPoolItem)) )
1136 aSettings.mbUseLastRow = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
1138 if( (SfxItemState::SET == pArgs->GetItemState(ID_VAL_USEBANDINGROWSTYLE, false,&pPoolItem)) )
1139 aSettings.mbUseRowBanding = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
1141 if( (SfxItemState::SET == pArgs->GetItemState(ID_VAL_USEFIRSTCOLUMNSTYLE, false,&pPoolItem)) )
1142 aSettings.mbUseFirstColumn = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
1144 if( (SfxItemState::SET == pArgs->GetItemState(ID_VAL_USELASTCOLUMNSTYLE, false,&pPoolItem)) )
1145 aSettings.mbUseLastColumn = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
1147 if( (SfxItemState::SET == pArgs->GetItemState(ID_VAL_USEBANDINGCOLUMNSTYLE, false,&pPoolItem)) )
1148 aSettings.mbUseColumnBanding = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
1150 if( aSettings == pTableObj->getTableStyleSettings() )
1151 return;
1153 const bool bUndo = pModel->IsUndoEnabled();
1155 if( bUndo )
1157 pModel->BegUndo( ImpGetResStr(STR_TABLE_STYLE_SETTINGS) );
1158 pModel->AddUndo( new TableStyleUndo( *pTableObj ) );
1161 pTableObj->setTableStyleSettings( aSettings );
1163 if( bUndo )
1164 pModel->EndUndo();
1167 void SvxTableController::SetVertical( sal_uInt16 nSId )
1169 SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
1170 if( mxTable.is() && pTableObj )
1172 TableModelNotifyGuard aGuard( mxTable.get() );
1174 CellPos aStart, aEnd;
1175 getSelectedCells( aStart, aEnd );
1177 SdrTextVertAdjust eAdj = SDRTEXTVERTADJUST_TOP;
1179 switch( nSId )
1181 case SID_TABLE_VERT_BOTTOM:
1182 eAdj = SDRTEXTVERTADJUST_BOTTOM;
1183 break;
1184 case SID_TABLE_VERT_CENTER:
1185 eAdj = SDRTEXTVERTADJUST_CENTER;
1186 break;
1187 //case SID_TABLE_VERT_NONE:
1188 default:
1189 break;
1192 SdrTextVertAdjustItem aItem( eAdj );
1194 for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
1196 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
1198 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1199 if( xCell.is() )
1200 xCell->SetMergedItem(aItem);
1204 UpdateTableShape();
1208 void SvxTableController::MergeMarkedCells()
1210 CellPos aStart, aEnd;
1211 getSelectedCells( aStart, aEnd );
1212 SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
1213 if( pTableObj )
1215 if( pTableObj->IsTextEditActive() )
1216 mpView->SdrEndTextEdit(true);
1218 TableModelNotifyGuard aGuard( mxTable.get() );
1219 MergeRange( aStart.mnCol, aStart.mnRow, aEnd.mnCol, aEnd.mnRow );
1223 void SvxTableController::SplitMarkedCells()
1225 if( mxTable.is() )
1227 CellPos aStart, aEnd;
1228 getSelectedCells( aStart, aEnd );
1230 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
1231 std::unique_ptr< SvxAbstractSplittTableDialog > xDlg( pFact ? pFact->CreateSvxSplittTableDialog( NULL, false, 99, 99 ) : 0 );
1232 if( xDlg.get() && xDlg->Execute() )
1234 const sal_Int32 nCount = xDlg->GetCount() - 1;
1235 if( nCount < 1 )
1236 return;
1238 getSelectedCells( aStart, aEnd );
1240 Reference< XMergeableCellRange > xRange( mxTable->createCursorByRange( mxTable->getCellRangeByPosition( aStart.mnCol, aStart.mnRow, aEnd.mnCol, aEnd.mnRow ) ), UNO_QUERY_THROW );
1242 const sal_Int32 nRowCount = mxTable->getRowCount();
1243 const sal_Int32 nColCount = mxTable->getColumnCount();
1246 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
1247 if( pTableObj )
1249 if( pTableObj->IsTextEditActive() )
1250 mpView->SdrEndTextEdit(true);
1252 TableModelNotifyGuard aGuard( mxTable.get() );
1254 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1255 if( bUndo )
1257 mpModel->BegUndo( ImpGetResStr(STR_TABLE_SPLIT) );
1258 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
1261 if( xDlg->IsHorizontal() )
1263 xRange->split( 0, nCount );
1265 else
1267 xRange->split( nCount, 0 );
1270 if( bUndo )
1271 mpModel->EndUndo();
1273 aEnd.mnRow += mxTable->getRowCount() - nRowCount;
1274 aEnd.mnCol += mxTable->getColumnCount() - nColCount;
1276 setSelectedCells( aStart, aEnd );
1281 void SvxTableController::DistributeColumns()
1283 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
1284 if( pTableObj )
1286 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1287 if( bUndo )
1289 mpModel->BegUndo( ImpGetResStr(STR_TABLE_DISTRIBUTE_COLUMNS) );
1290 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
1293 CellPos aStart, aEnd;
1294 getSelectedCells( aStart, aEnd );
1295 pTableObj->DistributeColumns( aStart.mnCol, aEnd.mnCol );
1297 if( bUndo )
1298 mpModel->EndUndo();
1302 void SvxTableController::DistributeRows()
1304 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
1305 if( pTableObj )
1307 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1308 if( bUndo )
1310 mpModel->BegUndo( ImpGetResStr(STR_TABLE_DISTRIBUTE_ROWS) );
1311 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
1314 CellPos aStart, aEnd;
1315 getSelectedCells( aStart, aEnd );
1316 pTableObj->DistributeRows( aStart.mnRow, aEnd.mnRow );
1318 if( bUndo )
1319 mpModel->EndUndo();
1323 bool SvxTableController::DeleteMarked()
1325 if( mbCellSelectionMode )
1327 if( mxTable.is() )
1329 CellPos aStart, aEnd;
1330 getSelectedCells( aStart, aEnd );
1331 for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
1333 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
1335 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1336 if( xCell.is() )
1337 xCell->SetOutlinerParaObject( 0 );
1341 UpdateTableShape();
1342 return true;
1346 return false;
1349 bool SvxTableController::GetStyleSheet( SfxStyleSheet*& rpStyleSheet ) const
1351 if( hasSelectedCells() )
1353 rpStyleSheet = 0;
1355 if( mxTable.is() )
1357 SfxStyleSheet* pRet=0;
1358 bool b1st=true;
1360 CellPos aStart, aEnd;
1361 const_cast<SvxTableController&>(*this).getSelectedCells( aStart, aEnd );
1363 for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
1365 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
1367 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1368 if( xCell.is() )
1370 SfxStyleSheet* pSS=xCell->GetStyleSheet();
1371 if(b1st)
1373 pRet=pSS;
1375 else if(pRet != pSS)
1377 return true;
1379 b1st=false;
1383 rpStyleSheet = pRet;
1384 return true;
1387 return false;
1390 bool SvxTableController::SetStyleSheet( SfxStyleSheet* pStyleSheet, bool bDontRemoveHardAttr )
1392 if( hasSelectedCells() && (!pStyleSheet || pStyleSheet->GetFamily() == SFX_STYLE_FAMILY_FRAME) )
1394 if( mxTable.is() )
1396 CellPos aStart, aEnd;
1397 getSelectedCells( aStart, aEnd );
1399 for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
1401 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
1403 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1404 if( xCell.is() )
1405 xCell->SetStyleSheet(pStyleSheet,bDontRemoveHardAttr);
1409 UpdateTableShape();
1410 return true;
1413 return false;
1417 // internals
1420 bool SvxTableController::checkTableObject()
1422 return mxTableObj.is();
1427 sal_uInt16 SvxTableController::getKeyboardAction( const KeyEvent& rKEvt, vcl::Window* /*pWindow*/ )
1429 const bool bMod1 = rKEvt.GetKeyCode().IsMod1(); // ctrl
1430 const bool bMod2 = rKEvt.GetKeyCode().IsMod2(); // Alt
1432 const bool bTextEdit = mpView->IsTextEdit();
1434 sal_uInt16 nAction = ACTION_HANDLED_BY_VIEW;
1436 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
1437 if( !pTableObj )
1438 return nAction;
1440 // handle special keys
1441 const sal_Int16 nCode = rKEvt.GetKeyCode().GetCode();
1442 switch( nCode )
1444 case awt::Key::ESCAPE: // handle escape
1446 if( bTextEdit )
1448 // escape during text edit ends text edit
1449 nAction = ACTION_STOP_TEXT_EDIT;
1451 if( mbCellSelectionMode )
1453 // escape with selected cells removes selection
1454 nAction = ACTION_REMOVE_SELECTION;
1456 break;
1458 case awt::Key::RETURN: // handle return
1460 if( !bMod1 && !bMod2 && !bTextEdit )
1462 // when not already editing, return starts text edit
1463 setSelectionStart( SdrTableObj::getFirstCell() );
1464 nAction = ACTION_EDIT_CELL;
1466 break;
1468 case awt::Key::F2: // f2 toggles text edit
1470 if( bMod1 || bMod2 ) // f2 with modifiers is handled by the view
1473 else if( bTextEdit )
1475 // f2 during text edit stops text edit
1476 nAction = ACTION_STOP_TEXT_EDIT;
1478 else if( mbCellSelectionMode )
1480 // f2 with selected cells removes selection
1481 nAction = ACTION_REMOVE_SELECTION;
1483 else
1485 // f2 with no selection and no text edit starts text edit
1486 setSelectionStart( SdrTableObj::getFirstCell() );
1487 nAction = ACTION_EDIT_CELL;
1489 break;
1491 case awt::Key::HOME:
1492 case awt::Key::NUM7:
1494 if( (bMod1 || bMod2) && (bTextEdit || mbCellSelectionMode) )
1496 if( bMod1 && !bMod2 )
1498 // ctrl + home jumps to first cell
1499 nAction = ACTION_GOTO_FIRST_CELL;
1501 else if( !bMod1 && bMod2 )
1503 // alt + home jumps to first column
1504 nAction = ACTION_GOTO_FIRST_COLUMN;
1507 break;
1509 case awt::Key::END:
1510 case awt::Key::NUM1:
1512 if( (bMod1 || bMod2) && (bTextEdit || mbCellSelectionMode) )
1514 if( bMod1 && !bMod2 )
1516 // ctrl + end jumps to last cell
1517 nAction = ACTION_GOTO_LAST_CELL;
1519 else if( !bMod1 && bMod2 )
1521 // alt + home jumps to last column
1522 nAction = ACTION_GOTO_LAST_COLUMN;
1525 break;
1528 case awt::Key::TAB:
1530 if( bTextEdit || mbCellSelectionMode )
1531 nAction = ACTION_TAB;
1532 break;
1535 case awt::Key::UP:
1536 case awt::Key::NUM8:
1537 case awt::Key::DOWN:
1538 case awt::Key::NUM2:
1539 case awt::Key::LEFT:
1540 case awt::Key::NUM4:
1541 case awt::Key::RIGHT:
1542 case awt::Key::NUM6:
1544 bool bTextMove = false;
1546 if( !bMod1 && bMod2 )
1548 if( (nCode == awt::Key::UP) || (nCode == awt::Key::NUM8) )
1550 nAction = ACTION_GOTO_LEFT_CELL;
1552 else if( (nCode == awt::Key::DOWN) || (nCode == awt::Key::NUM2) )
1554 nAction = ACTION_GOTO_RIGHT_CELL;
1556 break;
1559 if( !bTextMove )
1561 OutlinerView* pOLV = mpView->GetTextEditOutlinerView();
1562 if( pOLV )
1564 RemoveSelection();
1565 // during text edit, check if we navigate out of the cell
1566 ESelection aOldSelection = pOLV->GetSelection();
1567 pOLV->PostKeyEvent(rKEvt);
1568 bTextMove = pOLV && ( aOldSelection.IsEqual(pOLV->GetSelection()) );
1569 if( !bTextMove )
1571 nAction = ACTION_NONE;
1576 if( mbCellSelectionMode || bTextMove )
1578 // no text edit, navigate in cells if selection active
1579 switch( nCode )
1581 case awt::Key::LEFT:
1582 case awt::Key::NUM4:
1583 nAction = ACTION_GOTO_LEFT_CELL;
1584 break;
1585 case awt::Key::RIGHT:
1586 case awt::Key::NUM6:
1587 nAction = ACTION_GOTO_RIGHT_CELL;
1588 break;
1589 case awt::Key::DOWN:
1590 case awt::Key::NUM2:
1591 nAction = ACTION_GOTO_DOWN_CELL;
1592 break;
1593 case awt::Key::UP:
1594 case awt::Key::NUM8:
1595 nAction = ACTION_GOTO_UP_CELL;
1596 break;
1599 break;
1601 case awt::Key::PAGEUP:
1602 if( bMod2 )
1603 nAction = ACTION_GOTO_FIRST_ROW;
1604 break;
1606 case awt::Key::PAGEDOWN:
1607 if( bMod2 )
1608 nAction = ACTION_GOTO_LAST_ROW;
1609 break;
1611 return nAction;
1614 bool SvxTableController::executeAction( sal_uInt16 nAction, bool bSelect, vcl::Window* pWindow )
1616 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
1617 if( !pTableObj )
1618 return false;
1620 switch( nAction )
1622 case ACTION_GOTO_FIRST_CELL:
1624 gotoCell( SdrTableObj::getFirstCell(), bSelect, pWindow, nAction );
1625 break;
1628 case ACTION_GOTO_LEFT_CELL:
1630 gotoCell( pTableObj->getLeftCell( getSelectionEnd(), !bSelect ), bSelect, pWindow, nAction );
1631 break;
1634 case ACTION_GOTO_RIGHT_CELL:
1636 gotoCell( pTableObj->getRightCell( getSelectionEnd(), !bSelect ), bSelect, pWindow, nAction);
1637 break;
1640 case ACTION_GOTO_LAST_CELL:
1642 gotoCell( pTableObj->getLastCell(), bSelect, pWindow, nAction );
1643 break;
1646 case ACTION_GOTO_FIRST_COLUMN:
1648 CellPos aPos( SdrTableObj::getFirstCell().mnCol, getSelectionEnd().mnRow );
1649 gotoCell( aPos, bSelect, pWindow, nAction );
1650 break;
1653 case ACTION_GOTO_LAST_COLUMN:
1655 CellPos aPos( pTableObj->getLastCell().mnCol, getSelectionEnd().mnRow );
1656 gotoCell( aPos, bSelect, pWindow, nAction );
1657 break;
1660 case ACTION_GOTO_FIRST_ROW:
1662 CellPos aPos( getSelectionEnd().mnCol, SdrTableObj::getFirstCell().mnRow );
1663 gotoCell( aPos, bSelect, pWindow, nAction );
1664 break;
1667 case ACTION_GOTO_UP_CELL:
1669 gotoCell( pTableObj->getUpCell(getSelectionEnd(), !bSelect), bSelect, pWindow, nAction );
1670 break;
1673 case ACTION_GOTO_DOWN_CELL:
1675 gotoCell( pTableObj->getDownCell(getSelectionEnd(), !bSelect), bSelect, pWindow, nAction );
1676 break;
1679 case ACTION_GOTO_LAST_ROW:
1681 CellPos aPos( getSelectionEnd().mnCol, pTableObj->getLastCell().mnRow );
1682 gotoCell( aPos, bSelect, pWindow, nAction );
1683 break;
1686 case ACTION_EDIT_CELL:
1687 EditCell( getSelectionStart(), pWindow, 0, nAction );
1688 break;
1690 case ACTION_STOP_TEXT_EDIT:
1691 StopTextEdit();
1692 break;
1694 case ACTION_REMOVE_SELECTION:
1695 RemoveSelection();
1696 break;
1698 case ACTION_START_SELECTION:
1699 StartSelection( getSelectionStart() );
1700 break;
1702 case ACTION_TAB:
1704 if( bSelect )
1705 gotoCell( pTableObj->getPreviousCell( getSelectionEnd(), true ), false, pWindow, nAction );
1706 else
1708 CellPos aSelectionEnd( getSelectionEnd() );
1709 CellPos aNextCell( pTableObj->getNextCell( aSelectionEnd, true ) );
1710 if( aSelectionEnd == aNextCell )
1712 onInsert( SID_TABLE_INSERT_ROW, 0 );
1713 aNextCell = pTableObj->getNextCell( aSelectionEnd, true );
1715 gotoCell( aNextCell, false, pWindow, nAction );
1717 break;
1721 return nAction != ACTION_HANDLED_BY_VIEW;
1726 void SvxTableController::gotoCell( const CellPos& rPos, bool bSelect, vcl::Window* pWindow, sal_uInt16 nAction )
1728 if( mxTableObj.is() && static_cast<SdrTableObj*>(mxTableObj.get())->IsTextEditActive() )
1729 mpView->SdrEndTextEdit(true);
1731 if( bSelect )
1733 maCursorLastPos = rPos;
1734 if( mxTableObj.is() )
1735 static_cast< SdrTableObj* >( mxTableObj.get() )->setActiveCell( rPos );
1737 if( !mbCellSelectionMode )
1739 setSelectedCells( maCursorFirstPos, rPos );
1741 else
1743 UpdateSelection( rPos );
1746 else
1748 RemoveSelection();
1749 EditCell( rPos, pWindow, 0, nAction );
1755 const CellPos& SvxTableController::getSelectionStart()
1757 checkCell( maCursorFirstPos );
1758 return maCursorFirstPos;
1763 void SvxTableController::setSelectionStart( const CellPos& rPos )
1765 maCursorFirstPos = rPos;
1770 const CellPos& SvxTableController::getSelectionEnd()
1772 checkCell( maCursorLastPos );
1773 return maCursorLastPos;
1778 void SvxTableController::MergeRange( sal_Int32 nFirstCol, sal_Int32 nFirstRow, sal_Int32 nLastCol, sal_Int32 nLastRow )
1780 if( mxTable.is() ) try
1782 Reference< XMergeableCellRange > xRange( mxTable->createCursorByRange( mxTable->getCellRangeByPosition( nFirstCol, nFirstRow,nLastCol, nLastRow ) ), UNO_QUERY_THROW );
1783 if( xRange->isMergeable() )
1785 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1786 if( bUndo )
1788 mpModel->BegUndo( ImpGetResStr(STR_TABLE_MERGE) );
1789 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*mxTableObj.get()) );
1792 xRange->merge();
1794 if( bUndo )
1795 mpModel->EndUndo();
1798 catch( Exception& )
1800 DBG_ASSERT( false, "sdr::table::SvxTableController::MergeRange(), exception caught!" );
1808 void SvxTableController::checkCell( CellPos& rPos )
1810 if( mxTable.is() ) try
1812 if( rPos.mnCol >= mxTable->getColumnCount() )
1813 rPos.mnCol = mxTable->getColumnCount()-1;
1815 if( rPos.mnRow >= mxTable->getRowCount() )
1816 rPos.mnRow = mxTable->getRowCount()-1;
1818 catch( Exception& )
1820 OSL_FAIL("sdr::table::SvxTableController::checkCell(), exception caught!" );
1826 void SvxTableController::findMergeOrigin( CellPos& rPos )
1828 if( mxTable.is() ) try
1830 Reference< XMergeableCell > xCell( mxTable->getCellByPosition( rPos.mnCol, rPos.mnRow ), UNO_QUERY_THROW );
1831 if( xCell.is() && xCell->isMerged() )
1833 ::findMergeOrigin( mxTable, rPos.mnCol, rPos.mnRow, rPos.mnCol, rPos.mnRow );
1836 catch( Exception& )
1838 OSL_FAIL("sdr::table::SvxTableController::findMergeOrigin(), exception caught!" );
1844 void SvxTableController::EditCell( const CellPos& rPos, vcl::Window* pWindow, const awt::MouseEvent* pMouseEvent /*= 0*/, sal_uInt16 nAction /*= ACTION_NONE */ )
1846 SdrPageView* pPV = mpView->GetSdrPageView();
1848 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
1849 if( pTableObj && pTableObj->GetPage() == pPV->GetPage() )
1851 bool bEmptyOutliner = false;
1853 if(!pTableObj->GetOutlinerParaObject() && mpView->GetTextEditOutliner())
1855 ::Outliner* pOutl = mpView->GetTextEditOutliner();
1856 sal_Int32 nParaAnz = pOutl->GetParagraphCount();
1857 Paragraph* p1stPara = pOutl->GetParagraph( 0 );
1859 if(nParaAnz==1 && p1stPara)
1861 // Bei nur einem Pararaph
1862 if (pOutl->GetText(p1stPara).isEmpty())
1864 bEmptyOutliner = true;
1869 CellPos aPos( rPos );
1870 findMergeOrigin( aPos );
1872 if( pTableObj != mpView->GetTextEditObject() || bEmptyOutliner || !pTableObj->IsTextEditActive( aPos ) )
1874 if( pTableObj->IsTextEditActive() )
1875 mpView->SdrEndTextEdit(true);
1877 pTableObj->setActiveCell( aPos );
1879 // create new outliner, owner will be the SdrObjEditView
1880 SdrOutliner* pOutl = mpModel ? SdrMakeOutliner(OUTLINERMODE_OUTLINEOBJECT, *mpModel) : NULL;
1881 if (pOutl && pTableObj->IsVerticalWriting())
1882 pOutl->SetVertical( true );
1884 if (mpView->SdrBeginTextEdit(pTableObj, pPV, pWindow, true, pOutl))
1886 maCursorLastPos = maCursorFirstPos = rPos;
1888 OutlinerView* pOLV = mpView->GetTextEditOutlinerView();
1890 bool bNoSel = true;
1892 if( pMouseEvent )
1894 ::MouseEvent aMEvt( *pMouseEvent );
1896 SdrViewEvent aVEvt;
1897 SdrHitKind eHit = mpView->PickAnything(aMEvt, SdrMouseEventKind::BUTTONDOWN, aVEvt);
1899 if (eHit == SDRHIT_TEXTEDIT)
1901 // Text getroffen
1902 pOLV->MouseButtonDown(aMEvt);
1903 pOLV->MouseMove(aMEvt);
1904 pOLV->MouseButtonUp(aMEvt);
1905 // pOLV->MouseButtonDown(aMEvt);
1906 bNoSel = false;
1908 else
1910 nAction = ACTION_GOTO_LEFT_CELL;
1914 if( bNoSel )
1916 // Move cursor to end of text
1917 ESelection aNewSelection;
1919 const WritingMode eMode = pTableObj->GetWritingMode();
1920 if( ((nAction == ACTION_GOTO_LEFT_CELL) || (nAction == ACTION_GOTO_RIGHT_CELL)) && (eMode != WritingMode_TB_RL) )
1922 const bool bLast = ((nAction == ACTION_GOTO_LEFT_CELL) && (eMode == WritingMode_LR_TB)) ||
1923 ((nAction == ACTION_GOTO_RIGHT_CELL) && (eMode == WritingMode_RL_TB));
1925 if( bLast )
1926 aNewSelection = ESelection(EE_PARA_NOT_FOUND, EE_INDEX_NOT_FOUND, EE_PARA_NOT_FOUND, EE_INDEX_NOT_FOUND);
1928 pOLV->SetSelection(aNewSelection);
1937 bool SvxTableController::StopTextEdit()
1939 if(mpView->IsTextEdit())
1941 mpView->SdrEndTextEdit();
1942 mpView->SetCurrentObj(OBJ_TABLE);
1943 mpView->SetEditMode(SDREDITMODE_EDIT);
1944 return true;
1946 else
1948 return false;
1954 void SvxTableController::getSelectedCells( CellPos& rFirst, CellPos& rLast )
1956 if( mbCellSelectionMode )
1958 checkCell( maCursorFirstPos );
1959 checkCell( maCursorLastPos );
1961 rFirst.mnCol = std::min( maCursorFirstPos.mnCol, maCursorLastPos.mnCol );
1962 rFirst.mnRow = std::min( maCursorFirstPos.mnRow, maCursorLastPos.mnRow );
1963 rLast.mnCol = std::max( maCursorFirstPos.mnCol, maCursorLastPos.mnCol );
1964 rLast.mnRow = std::max( maCursorFirstPos.mnRow, maCursorLastPos.mnRow );
1966 bool bExt = false;
1967 if( mxTable.is() ) do
1969 bExt = false;
1970 for( sal_Int32 nRow = rFirst.mnRow; nRow <= rLast.mnRow && !bExt; nRow++ )
1972 for( sal_Int32 nCol = rFirst.mnCol; nCol <= rLast.mnCol && !bExt; nCol++ )
1974 Reference< XMergeableCell > xCell( mxTable->getCellByPosition( nCol, nRow ), UNO_QUERY );
1975 if( !xCell.is() )
1976 continue;
1978 if( xCell->isMerged() )
1980 CellPos aPos( nCol, nRow );
1981 findMergeOrigin( aPos );
1982 if( (aPos.mnCol < rFirst.mnCol) || (aPos.mnRow < rFirst.mnRow) )
1984 rFirst.mnCol = std::min( rFirst.mnCol, aPos.mnCol );
1985 rFirst.mnRow = std::min( rFirst.mnRow, aPos.mnRow );
1986 bExt = true;
1989 else
1991 if( ((nCol + xCell->getColumnSpan() - 1) > rLast.mnCol) || (nRow + xCell->getRowSpan() - 1 ) > rLast.mnRow )
1993 rLast.mnCol = std::max( rLast.mnCol, nCol + xCell->getColumnSpan() - 1 );
1994 rLast.mnRow = std::max( rLast.mnRow, nRow + xCell->getRowSpan() - 1 );
1995 bExt = true;
2001 while(bExt);
2003 else if( mpView && mpView->IsTextEdit() )
2005 rFirst = getSelectionStart();
2006 findMergeOrigin( rFirst );
2007 rLast = rFirst;
2009 if( mxTable.is() )
2011 Reference< XMergeableCell > xCell( mxTable->getCellByPosition( rLast.mnCol, rLast.mnRow ), UNO_QUERY );
2012 if( xCell.is() )
2014 rLast.mnCol += xCell->getColumnSpan() - 1;
2015 rLast.mnRow += xCell->getRowSpan() - 1;
2019 else
2021 rFirst.mnCol = 0;
2022 rFirst.mnRow = 0;
2023 if( mxTable.is() )
2025 rLast.mnRow = mxTable->getRowCount()-1;
2026 rLast.mnCol = mxTable->getColumnCount()-1;
2028 else
2030 rLast.mnRow = 0;
2031 rLast.mnCol = 0;
2038 void SvxTableController::StartSelection( const CellPos& rPos )
2040 StopTextEdit();
2041 mbCellSelectionMode = true;
2042 maCursorLastPos = maCursorFirstPos = rPos;
2043 mpView->MarkListHasChanged();
2048 void SvxTableController::setSelectedCells( const CellPos& rStart, const CellPos& rEnd )
2050 StopTextEdit();
2051 mbCellSelectionMode = true;
2052 maCursorFirstPos = rStart;
2053 UpdateSelection( rEnd );
2058 void SvxTableController::UpdateSelection( const CellPos& rPos )
2060 maCursorLastPos = rPos;
2061 mpView->MarkListHasChanged();
2066 void SvxTableController::clearSelection()
2068 RemoveSelection();
2073 void SvxTableController::selectAll()
2075 if( mxTable.is() )
2077 CellPos aPos1, aPos2( mxTable->getColumnCount()-1, mxTable->getRowCount()-1 );
2078 if( (aPos2.mnCol >= 0) && (aPos2.mnRow >= 0) )
2080 setSelectedCells( aPos1, aPos2 );
2087 void SvxTableController::RemoveSelection()
2089 if( mbCellSelectionMode )
2091 mbCellSelectionMode = false;
2092 mpView->MarkListHasChanged();
2098 void SvxTableController::onTableModified()
2100 if( mnUpdateEvent == 0 )
2101 mnUpdateEvent = Application::PostUserEvent( LINK( this, SvxTableController, UpdateHdl ) );
2105 void SvxTableController::updateSelectionOverlay()
2107 destroySelectionOverlay();
2108 if( mbCellSelectionMode )
2110 sdr::table::SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
2111 if( pTableObj )
2113 sdr::overlay::OverlayObjectCell::RangeVector aRanges;
2115 Rectangle aStartRect, aEndRect;
2116 CellPos aStart,aEnd;
2117 getSelectedCells( aStart, aEnd );
2118 pTableObj->getCellBounds( aStart, aStartRect );
2120 basegfx::B2DRange a2DRange( basegfx::B2DPoint(aStartRect.Left(), aStartRect.Top()) );
2121 a2DRange.expand( basegfx::B2DPoint(aStartRect.Right(), aStartRect.Bottom()) );
2123 findMergeOrigin( aEnd );
2124 pTableObj->getCellBounds( aEnd, aEndRect );
2125 a2DRange.expand( basegfx::B2DPoint(aEndRect.Left(), aEndRect.Top()) );
2126 a2DRange.expand( basegfx::B2DPoint(aEndRect.Right(), aEndRect.Bottom()) );
2127 aRanges.push_back( a2DRange );
2129 ::Color aHighlight( COL_BLUE );
2130 OutputDevice* pOutDev = mpView->GetFirstOutputDevice();
2131 if( pOutDev )
2132 aHighlight = pOutDev->GetSettings().GetStyleSettings().GetHighlightColor();
2134 const sal_uInt32 nCount = mpView->PaintWindowCount();
2135 for( sal_uInt32 nIndex = 0; nIndex < nCount; nIndex++ )
2137 SdrPaintWindow* pPaintWindow = mpView->GetPaintWindow(nIndex);
2138 if( pPaintWindow )
2140 rtl::Reference < sdr::overlay::OverlayManager > xOverlayManager = pPaintWindow->GetOverlayManager();
2141 if( xOverlayManager.is() )
2143 // sdr::overlay::CellOverlayType eType = sdr::overlay::CELL_OVERLAY_INVERT;
2144 sdr::overlay::CellOverlayType eType = sdr::overlay::CELL_OVERLAY_TRANSPARENT;
2146 sdr::overlay::OverlayObjectCell* pOverlay = new sdr::overlay::OverlayObjectCell( eType, aHighlight, aRanges );
2148 xOverlayManager->add(*pOverlay);
2149 mpSelectionOverlay = new sdr::overlay::OverlayObjectList;
2150 mpSelectionOverlay->append(*pOverlay);
2155 // If tiled rendering, emit callbacks for sdr table selection.
2156 if (pOutDev && pTableObj->GetModel()->isTiledRendering())
2158 // Left edge of aStartRect.
2159 Rectangle aSelectionStart(aStartRect.Left(), aStartRect.Top(), aStartRect.Left(), aStartRect.Bottom());
2160 // Right edge of aEndRect.
2161 Rectangle aSelectionEnd(aEndRect.Right(), aEndRect.Top(), aEndRect.Right(), aEndRect.Bottom());
2162 Rectangle aSelection(a2DRange.getMinX(), a2DRange.getMinY(), a2DRange.getMaxX(), a2DRange.getMaxY());
2164 if (pOutDev->GetMapMode().GetMapUnit() == MAP_100TH_MM)
2166 aSelectionStart = OutputDevice::LogicToLogic(aSelectionStart, MAP_100TH_MM, MAP_TWIP);
2167 aSelectionEnd = OutputDevice::LogicToLogic(aSelectionEnd, MAP_100TH_MM, MAP_TWIP);
2168 aSelection = OutputDevice::LogicToLogic(aSelection, MAP_100TH_MM, MAP_TWIP);
2171 pTableObj->GetModel()->libreOfficeKitCallback(LOK_CALLBACK_TEXT_SELECTION_START, aSelectionStart.toString().getStr());
2172 pTableObj->GetModel()->libreOfficeKitCallback(LOK_CALLBACK_TEXT_SELECTION_END, aSelectionEnd.toString().getStr());
2173 pTableObj->GetModel()->libreOfficeKitCallback(LOK_CALLBACK_TEXT_SELECTION, aSelection.toString().getStr());
2181 void SvxTableController::destroySelectionOverlay()
2183 if( mpSelectionOverlay )
2185 delete mpSelectionOverlay;
2186 mpSelectionOverlay = 0;
2188 if (mxTableObj->GetModel()->isTiledRendering())
2190 // Clear the LOK text selection so far provided by this table.
2191 mxTableObj->GetModel()->libreOfficeKitCallback(LOK_CALLBACK_TEXT_SELECTION_START, "EMPTY");
2192 mxTableObj->GetModel()->libreOfficeKitCallback(LOK_CALLBACK_TEXT_SELECTION_END, "EMPTY");
2193 mxTableObj->GetModel()->libreOfficeKitCallback(LOK_CALLBACK_TEXT_SELECTION, "EMPTY");
2200 void SvxTableController::MergeAttrFromSelectedCells(SfxItemSet& rAttr, bool bOnlyHardAttr) const
2202 if( mxTable.is() )
2204 CellPos aStart, aEnd;
2205 const_cast<SvxTableController&>(*this).getSelectedCells( aStart, aEnd );
2207 for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
2209 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
2211 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2212 if( xCell.is() && !xCell->isMerged() )
2214 const SfxItemSet& rSet = xCell->GetItemSet();
2215 SfxWhichIter aIter(rSet);
2216 sal_uInt16 nWhich(aIter.FirstWhich());
2217 while(nWhich)
2219 if(!bOnlyHardAttr)
2221 if(SfxItemState::DONTCARE == rSet.GetItemState(nWhich, false))
2222 rAttr.InvalidateItem(nWhich);
2223 else
2224 rAttr.MergeValue(rSet.Get(nWhich), true);
2226 else if(SfxItemState::SET == rSet.GetItemState(nWhich, false))
2228 const SfxPoolItem& rItem = rSet.Get(nWhich);
2229 rAttr.MergeValue(rItem, true);
2232 nWhich = aIter.NextWhich();
2242 const sal_uInt16 CELL_BEFORE = 0x0001;
2243 const sal_uInt16 CELL_LEFT = 0x0002;
2244 const sal_uInt16 CELL_RIGHT = 0x0004;
2245 const sal_uInt16 CELL_AFTER = 0x0008;
2247 const sal_uInt16 CELL_UPPER = 0x0010;
2248 const sal_uInt16 CELL_TOP = 0x0020;
2249 const sal_uInt16 CELL_BOTTOM = 0x0040;
2250 const sal_uInt16 CELL_LOWER = 0x0080;
2254 static void ImplSetLinePreserveColor( SvxBoxItem& rNewFrame, const SvxBorderLine* pNew, SvxBoxItemLine nLine )
2256 if( pNew )
2258 const SvxBorderLine* pOld = rNewFrame.GetLine(nLine);
2259 if( pOld )
2261 SvxBorderLine aNewLine( *pNew );
2262 aNewLine.SetColor( pOld->GetColor() );
2263 rNewFrame.SetLine( &aNewLine, nLine );
2264 return;
2267 rNewFrame.SetLine( pNew, nLine );
2272 static void ImplApplyBoxItem( sal_uInt16 nCellFlags, const SvxBoxItem* pBoxItem, const SvxBoxInfoItem* pBoxInfoItem, SvxBoxItem& rNewFrame )
2274 if( (nCellFlags & (CELL_BEFORE|CELL_AFTER|CELL_UPPER|CELL_LOWER)) != 0 )
2276 // current cell is outside the selection
2278 if( (nCellFlags & ( CELL_BEFORE|CELL_AFTER)) == 0 ) // check if its not nw or ne corner
2280 if( nCellFlags & CELL_UPPER )
2282 if( pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::TOP) )
2283 rNewFrame.SetLine(0, SvxBoxItemLine::BOTTOM );
2285 else if( nCellFlags & CELL_LOWER )
2287 if( pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::BOTTOM) )
2288 rNewFrame.SetLine( 0, SvxBoxItemLine::TOP );
2291 else if( (nCellFlags & ( CELL_UPPER|CELL_LOWER)) == 0 ) // check if its not sw or se corner
2293 if( nCellFlags & CELL_BEFORE )
2295 if( pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::LEFT) )
2296 rNewFrame.SetLine( 0, SvxBoxItemLine::RIGHT );
2298 else if( nCellFlags & CELL_AFTER )
2300 if( pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::RIGHT) )
2301 rNewFrame.SetLine( 0, SvxBoxItemLine::LEFT );
2305 else
2307 // current cell is inside the selection
2309 if( (nCellFlags & CELL_LEFT) ? pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::LEFT) : pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::VERT) )
2310 rNewFrame.SetLine( (nCellFlags & CELL_LEFT) ? pBoxItem->GetLeft() : pBoxInfoItem->GetVert(), SvxBoxItemLine::LEFT );
2312 if( (nCellFlags & CELL_RIGHT) ? pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::RIGHT) : pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::VERT) )
2313 rNewFrame.SetLine( (nCellFlags & CELL_RIGHT) ? pBoxItem->GetRight() : pBoxInfoItem->GetVert(), SvxBoxItemLine::RIGHT );
2315 if( (nCellFlags & CELL_TOP) ? pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::TOP) : pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::HORI) )
2316 rNewFrame.SetLine( (nCellFlags & CELL_TOP) ? pBoxItem->GetTop() : pBoxInfoItem->GetHori(), SvxBoxItemLine::TOP );
2318 if( (nCellFlags & CELL_BOTTOM) ? pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::BOTTOM) : pBoxInfoItem->IsValid(SvxBoxInfoItemValidFlags::HORI) )
2319 rNewFrame.SetLine( (nCellFlags & CELL_BOTTOM) ? pBoxItem->GetBottom() : pBoxInfoItem->GetHori(), SvxBoxItemLine::BOTTOM );
2321 // apply distance to borders
2322 if( pBoxInfoItem->IsValid( SvxBoxInfoItemValidFlags::DISTANCE ) )
2323 for( SvxBoxItemLine nLine : o3tl::enumrange<SvxBoxItemLine>() )
2324 rNewFrame.SetDistance( pBoxItem->GetDistance( nLine ), nLine );
2330 static void ImplSetLineColor( SvxBoxItem& rNewFrame, SvxBoxItemLine nLine, const Color& rColor )
2332 const SvxBorderLine* pSourceLine = rNewFrame.GetLine( nLine );
2333 if( pSourceLine )
2335 SvxBorderLine aLine( *pSourceLine );
2336 aLine.SetColor( rColor );
2337 rNewFrame.SetLine( &aLine, nLine );
2343 static void ImplApplyLineColorItem( sal_uInt16 nCellFlags, const SvxColorItem* pLineColorItem, SvxBoxItem& rNewFrame )
2345 const Color aColor( pLineColorItem->GetValue() );
2347 if( (nCellFlags & (CELL_LOWER|CELL_BEFORE|CELL_AFTER)) == 0 )
2348 ImplSetLineColor( rNewFrame, SvxBoxItemLine::BOTTOM, aColor );
2350 if( (nCellFlags & (CELL_UPPER|CELL_BEFORE|CELL_AFTER)) == 0 )
2351 ImplSetLineColor( rNewFrame, SvxBoxItemLine::TOP, aColor );
2353 if( (nCellFlags & (CELL_UPPER|CELL_LOWER|CELL_AFTER)) == 0 )
2354 ImplSetLineColor( rNewFrame, SvxBoxItemLine::RIGHT, aColor );
2356 if( (nCellFlags & (CELL_UPPER|CELL_LOWER|CELL_BEFORE)) == 0 )
2357 ImplSetLineColor( rNewFrame, SvxBoxItemLine::LEFT, aColor );
2362 static void ImplApplyBorderLineItem( sal_uInt16 nCellFlags, const SvxBorderLine* pBorderLineItem, SvxBoxItem& rNewFrame )
2364 if( (nCellFlags & ( CELL_BEFORE|CELL_AFTER|CELL_UPPER|CELL_LOWER)) != 0 )
2366 if( (nCellFlags & ( CELL_BEFORE|CELL_AFTER)) == 0 ) // check if its not nw or ne corner
2368 if( nCellFlags & CELL_UPPER )
2370 if( rNewFrame.GetBottom() )
2371 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::BOTTOM );
2373 else if( nCellFlags & CELL_LOWER )
2375 if( rNewFrame.GetTop() )
2376 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::TOP );
2379 else if( (nCellFlags & ( CELL_UPPER|CELL_LOWER)) == 0 ) // check if its not sw or se corner
2381 if( nCellFlags & CELL_BEFORE )
2383 if( rNewFrame.GetRight() )
2384 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::RIGHT );
2386 else if( nCellFlags & CELL_AFTER )
2388 if( rNewFrame.GetLeft() )
2389 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::LEFT );
2393 else
2395 if( rNewFrame.GetBottom() )
2396 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::BOTTOM );
2397 if( rNewFrame.GetTop() )
2398 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::TOP );
2399 if( rNewFrame.GetRight() )
2400 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::RIGHT );
2401 if( rNewFrame.GetLeft() )
2402 ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, SvxBoxItemLine::LEFT );
2408 void SvxTableController::ApplyBorderAttr( const SfxItemSet& rAttr )
2410 if( mxTable.is() )
2412 const sal_Int32 nRowCount = mxTable->getRowCount();
2413 const sal_Int32 nColCount = mxTable->getColumnCount();
2414 if( nRowCount && nColCount )
2416 const SvxBoxItem* pBoxItem = 0;
2417 if(SfxItemState::SET == rAttr.GetItemState(SDRATTR_TABLE_BORDER, false) )
2418 pBoxItem = dynamic_cast< const SvxBoxItem* >( &rAttr.Get( SDRATTR_TABLE_BORDER ) );
2420 const SvxBoxInfoItem* pBoxInfoItem = 0;
2421 if(SfxItemState::SET == rAttr.GetItemState(SDRATTR_TABLE_BORDER_INNER, false) )
2422 pBoxInfoItem = dynamic_cast< const SvxBoxInfoItem* >( &rAttr.Get( SDRATTR_TABLE_BORDER_INNER ) );
2424 const SvxColorItem* pLineColorItem = 0;
2425 if(SfxItemState::SET == rAttr.GetItemState(SID_FRAME_LINECOLOR, false) )
2426 pLineColorItem = dynamic_cast< const SvxColorItem* >( &rAttr.Get( SID_FRAME_LINECOLOR ) );
2428 const SvxBorderLine* pBorderLineItem = 0;
2429 if(SfxItemState::SET == rAttr.GetItemState(SID_FRAME_LINESTYLE, false) )
2430 pBorderLineItem = static_cast<const SvxLineItem&>(rAttr.Get( SID_FRAME_LINESTYLE )).GetLine();
2432 if( pBoxInfoItem && !pBoxItem )
2434 const static SvxBoxItem gaEmptyBoxItem( SDRATTR_TABLE_BORDER );
2435 pBoxItem = &gaEmptyBoxItem;
2437 else if( pBoxItem && !pBoxInfoItem )
2439 const static SvxBoxInfoItem gaEmptyBoxInfoItem( SDRATTR_TABLE_BORDER_INNER );
2440 pBoxInfoItem = &gaEmptyBoxInfoItem;
2443 CellPos aStart, aEnd;
2444 getSelectedCells( aStart, aEnd );
2446 const sal_Int32 nLastRow = std::min( aEnd.mnRow + 2, nRowCount );
2447 const sal_Int32 nLastCol = std::min( aEnd.mnCol + 2, nColCount );
2449 for( sal_Int32 nRow = std::max( aStart.mnRow - 1, (sal_Int32)0 ); nRow < nLastRow; nRow++ )
2451 sal_uInt16 nRowFlags = 0;
2452 nRowFlags |= (nRow == aStart.mnRow) ? CELL_TOP : 0;
2453 nRowFlags |= (nRow == aEnd.mnRow) ? CELL_BOTTOM : 0;
2454 nRowFlags |= (nRow < aStart.mnRow) ? CELL_UPPER : 0;
2455 nRowFlags |= (nRow > aEnd.mnRow) ? CELL_LOWER : 0;
2457 for( sal_Int32 nCol = std::max( aStart.mnCol - 1, (sal_Int32)0 ); nCol < nLastCol; nCol++ )
2459 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2460 if( !xCell.is() )
2461 continue;
2463 const SfxItemSet& rSet = xCell->GetItemSet();
2464 const SvxBoxItem* pOldOuter = static_cast<const SvxBoxItem*>(&rSet.Get( SDRATTR_TABLE_BORDER ));
2466 SvxBoxItem aNewFrame( *pOldOuter );
2468 sal_uInt16 nCellFlags = nRowFlags;
2469 nCellFlags |= (nCol == aStart.mnCol) ? CELL_LEFT : 0;
2470 nCellFlags |= (nCol == aEnd.mnCol) ? CELL_RIGHT : 0;
2471 nCellFlags |= (nCol < aStart.mnCol) ? CELL_BEFORE : 0;
2472 nCellFlags |= (nCol > aEnd.mnCol) ? CELL_AFTER : 0;
2474 if( pBoxItem && pBoxInfoItem )
2475 ImplApplyBoxItem( nCellFlags, pBoxItem, pBoxInfoItem, aNewFrame );
2477 if( pLineColorItem )
2478 ImplApplyLineColorItem( nCellFlags, pLineColorItem, aNewFrame );
2480 if( pBorderLineItem )
2481 ImplApplyBorderLineItem( nCellFlags, pBorderLineItem, aNewFrame );
2483 if (aNewFrame != *pOldOuter)
2485 SfxItemSet aAttr(*rSet.GetPool(), rSet.GetRanges());
2486 aAttr.Put(aNewFrame);
2487 xCell->SetMergedItemSetAndBroadcast( aAttr, false );
2497 void SvxTableController::UpdateTableShape()
2499 SdrObject* pTableObj = mxTableObj.get();
2500 if( pTableObj )
2502 pTableObj->ActionChanged();
2503 pTableObj->BroadcastObjectChange();
2505 updateSelectionOverlay();
2511 void SvxTableController::SetAttrToSelectedCells(const SfxItemSet& rAttr, bool bReplaceAll)
2513 if( mxTable.is() )
2515 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
2517 if( bUndo )
2518 mpModel->BegUndo( ImpGetResStr(STR_TABLE_NUMFORMAT) );
2520 CellPos aStart, aEnd;
2521 getSelectedCells( aStart, aEnd );
2523 SfxItemSet aAttr(*rAttr.GetPool(), rAttr.GetRanges());
2524 aAttr.Put(rAttr, true);
2526 const bool bFrame = (rAttr.GetItemState( SDRATTR_TABLE_BORDER ) == SfxItemState::SET) || (rAttr.GetItemState( SDRATTR_TABLE_BORDER_INNER ) == SfxItemState::SET);
2528 if( bFrame )
2530 aAttr.ClearItem( SDRATTR_TABLE_BORDER );
2531 aAttr.ClearItem( SDRATTR_TABLE_BORDER_INNER );
2534 for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
2536 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
2538 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2539 if( xCell.is() )
2541 if( bUndo )
2542 xCell->AddUndo();
2543 xCell->SetMergedItemSetAndBroadcast(aAttr, bReplaceAll);
2548 if( bFrame )
2550 ApplyBorderAttr( rAttr );
2553 UpdateTableShape();
2555 if( bUndo )
2556 mpModel->EndUndo();
2563 bool SvxTableController::GetAttributes(SfxItemSet& rTargetSet, bool bOnlyHardAttr) const
2565 if( mxTableObj.is() && hasSelectedCells() )
2567 MergeAttrFromSelectedCells( rTargetSet, bOnlyHardAttr );
2569 if( mpView->IsTextEdit() )
2571 if( mxTableObj->GetOutlinerParaObject() )
2572 rTargetSet.Put( SvxScriptTypeItem( mxTableObj->GetOutlinerParaObject()->GetTextObject().GetScriptType() ) );
2574 OutlinerView* pTextEditOutlinerView = mpView->GetTextEditOutlinerView();
2575 if(pTextEditOutlinerView)
2577 // FALSE= InvalidItems nicht al Default, sondern als "Loecher" betrachten
2578 rTargetSet.Put(pTextEditOutlinerView->GetAttribs(), false);
2579 rTargetSet.Put( SvxScriptTypeItem( pTextEditOutlinerView->GetSelectedScriptType() ) );
2583 return true;
2585 else
2587 return false;
2593 bool SvxTableController::SetAttributes(const SfxItemSet& rSet, bool bReplaceAll)
2595 if( mbCellSelectionMode || mpView->IsTextEdit() )
2597 SetAttrToSelectedCells( rSet, bReplaceAll );
2598 return true;
2600 return false;
2605 bool SvxTableController::GetMarkedObjModel( SdrPage* pNewPage )
2607 if( mxTableObj.is() && mbCellSelectionMode && pNewPage ) try
2609 sdr::table::SdrTableObj& rTableObj = *static_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
2611 CellPos aStart, aEnd;
2612 getSelectedCells( aStart, aEnd );
2614 SdrTableObj* pNewTableObj = rTableObj.CloneRange( aStart, aEnd );
2616 pNewTableObj->SetPage( pNewPage );
2617 pNewTableObj->SetModel( pNewPage->GetModel() );
2619 SdrInsertReason aReason(SDRREASON_VIEWCALL);
2620 pNewPage->InsertObject(pNewTableObj, SAL_MAX_SIZE, &aReason);
2622 return true;
2624 catch( Exception& )
2626 OSL_FAIL( "svx::SvxTableController::GetMarkedObjModel(), exception caught!" );
2628 return false;
2633 bool SvxTableController::PasteObjModel( const SdrModel& rModel )
2635 if( mxTableObj.is() && mpView && (rModel.GetPageCount() >= 1) )
2637 const SdrPage* pPastePage = rModel.GetPage(0);
2638 if( pPastePage && pPastePage->GetObjCount() == 1 )
2640 SdrTableObj* pPasteTableObj = dynamic_cast< SdrTableObj* >( pPastePage->GetObj(0) );
2641 if( pPasteTableObj )
2643 return PasteObject( pPasteTableObj );
2648 return false;
2653 bool SvxTableController::PasteObject( SdrTableObj* pPasteTableObj )
2655 if( !pPasteTableObj )
2656 return false;
2658 Reference< XTable > xPasteTable( pPasteTableObj->getTable() );
2659 if( !xPasteTable.is() )
2660 return false;
2662 if( !mxTable.is() )
2663 return false;
2665 sal_Int32 nPasteColumns = xPasteTable->getColumnCount();
2666 sal_Int32 nPasteRows = xPasteTable->getRowCount();
2668 CellPos aStart, aEnd;
2669 getSelectedCells( aStart, aEnd );
2671 if( mpView->IsTextEdit() )
2672 mpView->SdrEndTextEdit(true);
2674 sal_Int32 nColumns = mxTable->getColumnCount();
2675 sal_Int32 nRows = mxTable->getRowCount();
2677 const sal_Int32 nMissing = nPasteRows - ( nRows - aStart.mnRow );
2678 if( nMissing > 0 )
2680 Reference< XTableRows > xRows( mxTable->getRows() );
2681 xRows->insertByIndex( nRows, nMissing );
2682 nRows = mxTable->getRowCount();
2685 nPasteRows = std::min( nPasteRows, nRows - aStart.mnRow );
2686 nPasteColumns = std::min( nPasteColumns, nColumns - aStart.mnCol );
2688 // copy cell contents
2689 for( sal_Int32 nRow = 0; nRow < nPasteRows; ++nRow )
2691 for( sal_Int32 nCol = 0; nCol < nPasteColumns; ++nCol )
2693 CellRef xTargetCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( aStart.mnCol + nCol, aStart.mnRow + nRow ).get() ) );
2694 if( xTargetCell.is() && !xTargetCell->isMerged() )
2696 xTargetCell->AddUndo();
2697 xTargetCell->cloneFrom( dynamic_cast< Cell* >( xPasteTable->getCellByPosition( nCol, nRow ).get() ) );
2698 nCol += xTargetCell->getColumnSpan() - 1;
2703 UpdateTableShape();
2705 return true;
2708 bool SvxTableController::TakeFormatPaintBrush( std::shared_ptr< SfxItemSet >& /*rFormatSet*/ )
2710 // SdrView::TakeFormatPaintBrush() is enough
2711 return false;
2714 bool SvxTableController::ApplyFormatPaintBrush( SfxItemSet& rFormatSet, bool bNoCharacterFormats, bool bNoParagraphFormats )
2716 if( mbCellSelectionMode )
2718 SdrTextObj* pTableObj = dynamic_cast<SdrTextObj*>( mxTableObj.get() );
2719 if( !pTableObj )
2720 return false;
2722 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
2724 if( bUndo )
2725 mpModel->BegUndo( ImpGetResStr(STR_TABLE_NUMFORMAT) );
2727 CellPos aStart, aEnd;
2728 getSelectedCells( aStart, aEnd );
2730 SfxItemSet aAttr(*rFormatSet.GetPool(), rFormatSet.GetRanges());
2731 aAttr.Put(rFormatSet, true);
2733 const bool bFrame = (rFormatSet.GetItemState( SDRATTR_TABLE_BORDER ) == SfxItemState::SET) || (rFormatSet.GetItemState( SDRATTR_TABLE_BORDER_INNER ) == SfxItemState::SET);
2735 if( bFrame )
2737 aAttr.ClearItem( SDRATTR_TABLE_BORDER );
2738 aAttr.ClearItem( SDRATTR_TABLE_BORDER_INNER );
2741 const sal_uInt16* pRanges = rFormatSet.GetRanges();
2742 bool bTextOnly = true;
2744 while( *pRanges )
2746 if( (*pRanges != EE_PARA_START) && (*pRanges != EE_CHAR_START) )
2748 bTextOnly = false;
2749 break;
2751 pRanges += 2;
2754 const bool bReplaceAll = false;
2755 for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
2757 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
2759 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2760 if( xCell.is() )
2762 if( bUndo )
2763 xCell->AddUndo();
2764 if( !bTextOnly )
2765 xCell->SetMergedItemSetAndBroadcast(aAttr, bReplaceAll);
2767 SdrText* pText = static_cast< SdrText* >( xCell.get() );
2768 SdrObjEditView::ApplyFormatPaintBrushToText( rFormatSet, *pTableObj, pText, bNoCharacterFormats, bNoParagraphFormats );
2773 if( bFrame )
2775 ApplyBorderAttr( rFormatSet );
2778 UpdateTableShape();
2780 if( bUndo )
2781 mpModel->EndUndo();
2783 return true;
2786 return false;
2792 IMPL_LINK_NOARG(SvxTableController, UpdateHdl)
2794 mnUpdateEvent = 0;
2796 if( mbCellSelectionMode )
2798 CellPos aStart( maCursorFirstPos );
2799 CellPos aEnd( maCursorLastPos );
2800 checkCell(aStart);
2801 checkCell(aEnd);
2802 if( aStart != maCursorFirstPos || aEnd != maCursorLastPos )
2804 setSelectedCells( aStart, aEnd );
2807 updateSelectionOverlay();
2809 return 0;
2812 namespace
2815 struct LinesState
2817 LinesState(SvxBoxItem& rBoxItem_, SvxBoxInfoItem& rBoxInfoItem_)
2818 : rBoxItem(rBoxItem_)
2819 , rBoxInfoItem(rBoxInfoItem_)
2820 , bDistanceIndeterminate(false)
2822 aBorderSet.fill(false);
2823 aInnerLineSet.fill(false);
2824 aBorderIndeterminate.fill(false);
2825 aInnerLineIndeterminate.fill(false);
2826 aDistanceSet.fill(false);
2827 aDistance.fill(0);
2830 SvxBoxItem& rBoxItem;
2831 SvxBoxInfoItem& rBoxInfoItem;
2832 o3tl::enumarray<SvxBoxItemLine, bool> aBorderSet;
2833 o3tl::enumarray<SvxBoxInfoItemLine, bool> aInnerLineSet;
2834 o3tl::enumarray<SvxBoxItemLine, bool> aBorderIndeterminate;
2835 o3tl::enumarray<SvxBoxInfoItemLine, bool> aInnerLineIndeterminate;
2836 o3tl::enumarray<SvxBoxItemLine, bool> aDistanceSet;
2837 o3tl::enumarray<SvxBoxItemLine, sal_uInt16> aDistance;
2838 bool bDistanceIndeterminate;
2841 class BoxItemWrapper
2843 public:
2844 BoxItemWrapper(SvxBoxItem& rBoxItem, SvxBoxInfoItem& rBoxInfoItem, SvxBoxItemLine nBorderLine, SvxBoxInfoItemLine nInnerLine, bool bBorder);
2846 const SvxBorderLine* getLine() const;
2847 void setLine(const SvxBorderLine* pLine);
2849 private:
2850 SvxBoxItem& m_rBoxItem;
2851 SvxBoxInfoItem& m_rBoxInfoItem;
2852 const SvxBoxItemLine m_nBorderLine;
2853 const SvxBoxInfoItemLine m_nInnerLine;
2854 const bool m_bBorder;
2857 BoxItemWrapper::BoxItemWrapper(
2858 SvxBoxItem& rBoxItem, SvxBoxInfoItem& rBoxInfoItem,
2859 const SvxBoxItemLine nBorderLine, const SvxBoxInfoItemLine nInnerLine, const bool bBorder)
2860 : m_rBoxItem(rBoxItem)
2861 , m_rBoxInfoItem(rBoxInfoItem)
2862 , m_nBorderLine(nBorderLine)
2863 , m_nInnerLine(nInnerLine)
2864 , m_bBorder(bBorder)
2868 const SvxBorderLine* BoxItemWrapper::getLine() const
2870 if (m_bBorder)
2871 return m_rBoxItem.GetLine(m_nBorderLine);
2872 else
2873 return (m_nInnerLine == SvxBoxInfoItemLine::HORI) ? m_rBoxInfoItem.GetHori() : m_rBoxInfoItem.GetVert();
2876 void BoxItemWrapper::setLine(const SvxBorderLine* pLine)
2878 if (m_bBorder)
2879 m_rBoxItem.SetLine(pLine, m_nBorderLine);
2880 else
2881 m_rBoxInfoItem.SetLine(pLine, m_nInnerLine);
2884 void lcl_MergeBorderLine(
2885 LinesState& rLinesState, const SvxBorderLine* const pLine, const SvxBoxItemLine nLine,
2886 SvxBoxInfoItemValidFlags nValidFlag, const bool bBorder = true)
2888 const SvxBoxInfoItemLine nInnerLine(bBorder ? SvxBoxInfoItemLine::HORI : ((nValidFlag & SvxBoxInfoItemValidFlags::HORI) ? SvxBoxInfoItemLine::HORI : SvxBoxInfoItemLine::VERT));
2889 BoxItemWrapper aBoxItem(rLinesState.rBoxItem, rLinesState.rBoxInfoItem, nLine, nInnerLine, bBorder);
2890 bool& rbSet(bBorder ? rLinesState.aBorderSet[nLine] : rLinesState.aInnerLineSet[nInnerLine]);
2892 if (rbSet)
2894 bool& rbIndeterminate(bBorder ? rLinesState.aBorderIndeterminate[nLine] : rLinesState.aInnerLineIndeterminate[nInnerLine]);
2895 if (!rbIndeterminate)
2897 const SvxBorderLine* const pMergedLine(aBoxItem.getLine());
2898 if ((pLine && !pMergedLine) || (!pLine && pMergedLine) || (pLine && (*pLine != *pMergedLine)))
2900 aBoxItem.setLine(0);
2901 rbIndeterminate = true;
2905 else
2907 aBoxItem.setLine(pLine);
2908 rbSet = true;
2912 void lcl_MergeBorderOrInnerLine(
2913 LinesState& rLinesState, const SvxBorderLine* const pLine, const SvxBoxItemLine nLine,
2914 SvxBoxInfoItemValidFlags nValidFlag, const bool bBorder)
2916 if (bBorder)
2917 lcl_MergeBorderLine(rLinesState, pLine, nLine, nValidFlag);
2918 else
2920 const bool bVertical = (nLine == SvxBoxItemLine::LEFT) || (nLine == SvxBoxItemLine::RIGHT);
2921 lcl_MergeBorderLine(rLinesState, pLine, nLine, bVertical ? SvxBoxInfoItemValidFlags::VERT : SvxBoxInfoItemValidFlags::HORI, false);
2925 void lcl_MergeDistance(
2926 LinesState& rLinesState, const SvxBoxItemLine nIndex, const sal_uInt16 nDistance)
2928 if (rLinesState.aDistanceSet[nIndex])
2930 if (!rLinesState.bDistanceIndeterminate)
2931 rLinesState.bDistanceIndeterminate = nDistance != rLinesState.aDistance[nIndex];
2933 else
2935 rLinesState.aDistance[nIndex] = nDistance;
2936 rLinesState.aDistanceSet[nIndex] = true;
2940 void lcl_MergeCommonBorderAttr(LinesState& rLinesState, const SvxBoxItem& rCellBoxItem, const sal_Int32 nCellFlags)
2942 if( (nCellFlags & (CELL_BEFORE|CELL_AFTER|CELL_UPPER|CELL_LOWER)) != 0 )
2944 // current cell is outside the selection
2946 if( (nCellFlags & ( CELL_BEFORE|CELL_AFTER)) == 0 ) // check if its not nw or ne corner
2948 if( nCellFlags & CELL_UPPER )
2949 lcl_MergeBorderLine(rLinesState, rCellBoxItem.GetBottom(), SvxBoxItemLine::TOP, SvxBoxInfoItemValidFlags::TOP);
2950 else if( nCellFlags & CELL_LOWER )
2951 lcl_MergeBorderLine(rLinesState, rCellBoxItem.GetTop(), SvxBoxItemLine::BOTTOM, SvxBoxInfoItemValidFlags::BOTTOM);
2953 else if( (nCellFlags & ( CELL_UPPER|CELL_LOWER)) == 0 ) // check if its not sw or se corner
2955 if( nCellFlags & CELL_BEFORE )
2956 lcl_MergeBorderLine(rLinesState, rCellBoxItem.GetRight(), SvxBoxItemLine::LEFT, SvxBoxInfoItemValidFlags::LEFT);
2957 else if( nCellFlags & CELL_AFTER )
2958 lcl_MergeBorderLine(rLinesState, rCellBoxItem.GetLeft(), SvxBoxItemLine::RIGHT, SvxBoxInfoItemValidFlags::RIGHT);
2961 // NOTE: inner distances for cells outside the selected range
2962 // are not relevant -> we ignore them.
2964 else
2966 // current cell is inside the selection
2968 lcl_MergeBorderOrInnerLine(rLinesState, rCellBoxItem.GetTop(), SvxBoxItemLine::TOP, SvxBoxInfoItemValidFlags::TOP, (nCellFlags & CELL_TOP) != 0);
2969 lcl_MergeBorderOrInnerLine(rLinesState, rCellBoxItem.GetBottom(), SvxBoxItemLine::BOTTOM, SvxBoxInfoItemValidFlags::BOTTOM, (nCellFlags & CELL_BOTTOM) != 0);
2970 lcl_MergeBorderOrInnerLine(rLinesState, rCellBoxItem.GetLeft(), SvxBoxItemLine::LEFT, SvxBoxInfoItemValidFlags::LEFT, (nCellFlags & CELL_LEFT) != 0);
2971 lcl_MergeBorderOrInnerLine(rLinesState, rCellBoxItem.GetRight(), SvxBoxItemLine::RIGHT, SvxBoxInfoItemValidFlags::RIGHT, (nCellFlags & CELL_RIGHT) != 0);
2973 lcl_MergeDistance(rLinesState, SvxBoxItemLine::TOP, rCellBoxItem.GetDistance(SvxBoxItemLine::TOP));
2974 lcl_MergeDistance(rLinesState, SvxBoxItemLine::BOTTOM, rCellBoxItem.GetDistance(SvxBoxItemLine::BOTTOM));
2975 lcl_MergeDistance(rLinesState, SvxBoxItemLine::LEFT, rCellBoxItem.GetDistance(SvxBoxItemLine::LEFT));
2976 lcl_MergeDistance(rLinesState, SvxBoxItemLine::RIGHT, rCellBoxItem.GetDistance(SvxBoxItemLine::RIGHT));
2982 void SvxTableController::FillCommonBorderAttrFromSelectedCells( SvxBoxItem& rBoxItem, SvxBoxInfoItem& rBoxInfoItem ) const
2984 if( mxTable.is() )
2986 const sal_Int32 nRowCount = mxTable->getRowCount();
2987 const sal_Int32 nColCount = mxTable->getColumnCount();
2988 if( nRowCount && nColCount )
2990 CellPos aStart, aEnd;
2991 const_cast< SvxTableController* >( this )->getSelectedCells( aStart, aEnd );
2993 // We are adding one more row/column around the block of selected cells.
2994 // We will be checking the adjoining border of these too.
2995 const sal_Int32 nLastRow = std::min( aEnd.mnRow + 2, nRowCount );
2996 const sal_Int32 nLastCol = std::min( aEnd.mnCol + 2, nColCount );
2998 rBoxInfoItem.SetValid( SvxBoxInfoItemValidFlags::ALL, false );
2999 LinesState aLinesState( rBoxItem, rBoxInfoItem );
3001 /* Here we go through all the selected cells (enhanced by
3002 * the adjoining row/column on each side) and determine the
3003 * lines for presentation. The algorithm is simple:
3004 * 1. if a border or inner line is set (or unset) in all
3005 * cells to the same value, it will be used.
3006 * 2. if a border or inner line is set only in some cells,
3007 * it will be set to indeterminate state (SetValid() on
3008 * rBoxInfoItem).
3010 for( sal_Int32 nRow = std::max( aStart.mnRow - 1, (sal_Int32)0 ); nRow < nLastRow; nRow++ )
3012 sal_uInt16 nRowFlags = 0;
3013 nRowFlags |= (nRow == aStart.mnRow) ? CELL_TOP : 0;
3014 nRowFlags |= (nRow == aEnd.mnRow) ? CELL_BOTTOM : 0;
3015 nRowFlags |= (nRow < aStart.mnRow) ? CELL_UPPER : 0;
3016 nRowFlags |= (nRow > aEnd.mnRow) ? CELL_LOWER : 0;
3018 for( sal_Int32 nCol = std::max( aStart.mnCol - 1, (sal_Int32)0 ); nCol < nLastCol; nCol++ )
3020 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
3021 if( !xCell.is() )
3022 continue;
3024 sal_uInt16 nCellFlags = nRowFlags;
3025 nCellFlags |= (nCol == aStart.mnCol) ? CELL_LEFT : 0;
3026 nCellFlags |= (nCol == aEnd.mnCol) ? CELL_RIGHT : 0;
3027 nCellFlags |= (nCol < aStart.mnCol) ? CELL_BEFORE : 0;
3028 nCellFlags |= (nCol > aEnd.mnCol) ? CELL_AFTER : 0;
3030 const SfxItemSet& rSet = xCell->GetItemSet();
3031 SvxBoxItem aCellBoxItem(mergeDrawinglayerTextDistancesAndSvxBoxItem(rSet));
3032 lcl_MergeCommonBorderAttr( aLinesState, aCellBoxItem, nCellFlags );
3036 if (!aLinesState.aBorderIndeterminate[SvxBoxItemLine::TOP])
3037 aLinesState.rBoxInfoItem.SetValid(SvxBoxInfoItemValidFlags::TOP);
3038 if (!aLinesState.aBorderIndeterminate[SvxBoxItemLine::BOTTOM])
3039 aLinesState.rBoxInfoItem.SetValid(SvxBoxInfoItemValidFlags::BOTTOM);
3040 if (!aLinesState.aBorderIndeterminate[SvxBoxItemLine::LEFT])
3041 aLinesState.rBoxInfoItem.SetValid(SvxBoxInfoItemValidFlags::LEFT);
3042 if (!aLinesState.aBorderIndeterminate[SvxBoxItemLine::RIGHT])
3043 aLinesState.rBoxInfoItem.SetValid(SvxBoxInfoItemValidFlags::RIGHT);
3044 if (!aLinesState.aInnerLineIndeterminate[SvxBoxInfoItemLine::HORI])
3045 aLinesState.rBoxInfoItem.SetValid(SvxBoxInfoItemValidFlags::HORI);
3046 if (!aLinesState.aInnerLineIndeterminate[SvxBoxInfoItemLine::VERT])
3047 aLinesState.rBoxInfoItem.SetValid(SvxBoxInfoItemValidFlags::VERT);
3049 if (!aLinesState.bDistanceIndeterminate)
3051 if (aLinesState.aDistanceSet[SvxBoxItemLine::TOP])
3052 aLinesState.rBoxItem.SetDistance(aLinesState.aDistance[SvxBoxItemLine::TOP], SvxBoxItemLine::TOP);
3053 if (aLinesState.aDistanceSet[SvxBoxItemLine::BOTTOM])
3054 aLinesState.rBoxItem.SetDistance(aLinesState.aDistance[SvxBoxItemLine::BOTTOM], SvxBoxItemLine::BOTTOM);
3055 if (aLinesState.aDistanceSet[SvxBoxItemLine::LEFT])
3056 aLinesState.rBoxItem.SetDistance(aLinesState.aDistance[SvxBoxItemLine::LEFT], SvxBoxItemLine::LEFT);
3057 if (aLinesState.aDistanceSet[SvxBoxItemLine::RIGHT])
3058 aLinesState.rBoxItem.SetDistance(aLinesState.aDistance[SvxBoxItemLine::RIGHT], SvxBoxItemLine::RIGHT);
3059 aLinesState.rBoxInfoItem.SetValid(SvxBoxInfoItemValidFlags::DISTANCE);
3065 bool SvxTableController::selectRow( sal_Int32 row )
3067 if( !mxTable.is() )
3068 return false;
3069 CellPos aStart( 0, row ), aEnd( mxTable->getColumnCount() - 1, row );
3070 StartSelection( aEnd );
3071 gotoCell( aStart, true, 0 );
3072 return true;
3075 bool SvxTableController::selectColumn( sal_Int32 column )
3077 if( !mxTable.is() )
3078 return false;
3079 CellPos aStart( column, 0 ), aEnd( column, mxTable->getRowCount() - 1 );
3080 StartSelection( aEnd );
3081 gotoCell( aStart, true, 0 );
3082 return true;
3085 bool SvxTableController::deselectRow( sal_Int32 row )
3087 if( !mxTable.is() )
3088 return false;
3089 CellPos aStart( 0, row ), aEnd( mxTable->getColumnCount() - 1, row );
3090 StartSelection( aEnd );
3091 gotoCell( aStart, false, 0 );
3092 return true;
3095 bool SvxTableController::deselectColumn( sal_Int32 column )
3097 if( !mxTable.is() )
3098 return false;
3099 CellPos aStart( column, 0 ), aEnd( column, mxTable->getRowCount() - 1 );
3100 StartSelection( aEnd );
3101 gotoCell( aStart, false, 0 );
3102 return true;
3105 bool SvxTableController::isRowSelected( sal_Int32 nRow )
3107 if( hasSelectedCells() )
3109 CellPos aFirstPos, aLastPos;
3110 getSelectedCells( aFirstPos, aLastPos );
3111 if( (aFirstPos.mnCol == 0) && (nRow >= aFirstPos.mnRow && nRow <= aLastPos.mnRow) && (mxTable->getColumnCount() - 1 == aLastPos.mnCol) )
3112 return true;
3114 return false;
3117 bool SvxTableController::isColumnSelected( sal_Int32 nColumn )
3119 if( hasSelectedCells() )
3121 CellPos aFirstPos, aLastPos;
3122 getSelectedCells( aFirstPos, aLastPos );
3123 if( (aFirstPos.mnRow == 0) && (nColumn >= aFirstPos.mnCol && nColumn <= aLastPos.mnCol) && (mxTable->getRowCount() - 1 == aLastPos.mnRow) )
3124 return true;
3126 return false;
3129 bool SvxTableController::isRowHeader()
3131 SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
3132 SdrModel* pModel = pTableObj ? pTableObj->GetModel() : 0;
3134 if( !pTableObj || !pModel )
3135 return false;
3137 TableStyleSettings aSettings( pTableObj->getTableStyleSettings() );
3139 return aSettings.mbUseFirstRow;
3142 bool SvxTableController::isColumnHeader()
3144 SdrTableObj* pTableObj = dynamic_cast< sdr::table::SdrTableObj* >( mxTableObj.get() );
3145 SdrModel* pModel = pTableObj ? pTableObj->GetModel() : 0;
3147 if( !pTableObj || !pModel )
3148 return false;
3150 TableStyleSettings aSettings( pTableObj->getTableStyleSettings() );
3152 return aSettings.mbUseFirstColumn;
3155 bool SvxTableController::setCursorLogicPosition(const Point& rPosition, bool bPoint)
3157 if (mxTableObj->GetObjIdentifier() != OBJ_TABLE)
3158 return false;
3160 SdrTableObj* pTableObj = static_cast<SdrTableObj*>(mxTableObj.get());
3161 CellPos aCellPos;
3162 if (pTableObj->CheckTableHit(rPosition, aCellPos.mnCol, aCellPos.mnRow, 0) != SDRTABLEHIT_NONE)
3164 // Position is a table cell.
3165 if (mbCellSelectionMode)
3167 // We have a table selection already: adjust the point or the mark.
3168 if (bPoint)
3169 setSelectedCells(maCursorFirstPos, aCellPos);
3170 else
3171 setSelectedCells(aCellPos, maCursorLastPos);
3172 return true;
3174 else if (aCellPos != maMouseDownPos)
3176 // No selection, but rPosition is at an other cell: start table selection.
3177 StartSelection(maMouseDownPos);
3178 // Update graphic selection, should be hidden now.
3179 mpView->AdjustMarkHdl();
3183 return false;
3188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */