xref: /trunk/main/svx/source/table/tablecontroller.cxx (revision f6e50924346d0b8c0b07c91832a97665dd718b0c)
1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "tablecontroller.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
30cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
33cdf0e10cSrcweir #include <com/sun/star/table/XMergeableCellRange.hpp>
34cdf0e10cSrcweir #include <com/sun/star/table/XMergeableCell.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <sal/config.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <vcl/svapp.hxx>
39cdf0e10cSrcweir #include <svl/whiter.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <sfx2/request.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <editeng/scripttypeitem.hxx>
44cdf0e10cSrcweir #include <svx/svdotable.hxx>
45cdf0e10cSrcweir #include <svx/sdr/overlay/overlayobjectcell.hxx>
46cdf0e10cSrcweir #include <svx/sdr/overlay/overlaymanager.hxx>
47cdf0e10cSrcweir #include <svx/svxids.hrc>
48cdf0e10cSrcweir #include <editeng/outlobj.hxx>
49cdf0e10cSrcweir #include <svx/svdoutl.hxx>
50cdf0e10cSrcweir #include <svx/svdpagv.hxx>
51cdf0e10cSrcweir #include <svx/svdetc.hxx>
52cdf0e10cSrcweir #include <editeng/editobj.hxx>
53cdf0e10cSrcweir #include "editeng/editstat.hxx"
54cdf0e10cSrcweir #include "editeng/unolingu.hxx"
55cdf0e10cSrcweir #include "svx/sdrpagewindow.hxx"
56cdf0e10cSrcweir #include <svx/selectioncontroller.hxx>
57cdf0e10cSrcweir #include <svx/svdmodel.hxx>
58cdf0e10cSrcweir #include "svx/sdrpaintwindow.hxx"
59cdf0e10cSrcweir #include <svx/svxdlg.hxx>
60cdf0e10cSrcweir #include <editeng/boxitem.hxx>
61cdf0e10cSrcweir #include "cell.hxx"
62cdf0e10cSrcweir #include <editeng/borderline.hxx>
63cdf0e10cSrcweir #include <editeng/colritem.hxx>
64cdf0e10cSrcweir #include "editeng/bolnitem.hxx"
65cdf0e10cSrcweir #include "svx/svdstr.hrc"
66cdf0e10cSrcweir #include "svx/svdglob.hxx"
67cdf0e10cSrcweir #include "svx/svdpage.hxx"
68cdf0e10cSrcweir #include "tableundo.hxx"
69cdf0e10cSrcweir #include "tablelayouter.hxx"
70cdf0e10cSrcweir 
71cdf0e10cSrcweir using ::rtl::OUString;
72cdf0e10cSrcweir using namespace ::sdr::table;
73cdf0e10cSrcweir using namespace ::com::sun::star;
74cdf0e10cSrcweir using namespace ::com::sun::star::uno;
75cdf0e10cSrcweir using namespace ::com::sun::star::table;
76cdf0e10cSrcweir using namespace ::com::sun::star::beans;
77cdf0e10cSrcweir using namespace ::com::sun::star::container;
78cdf0e10cSrcweir using namespace ::com::sun::star::text;
79cdf0e10cSrcweir using namespace ::com::sun::star::style;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir namespace sdr { namespace table {
82cdf0e10cSrcweir 
83cdf0e10cSrcweir // --------------------------------------------------------------------
84cdf0e10cSrcweir // class SvxTableControllerModifyListener
85cdf0e10cSrcweir // --------------------------------------------------------------------
86cdf0e10cSrcweir 
87cdf0e10cSrcweir class SvxTableControllerModifyListener : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XModifyListener >
88cdf0e10cSrcweir {
89cdf0e10cSrcweir public:
90cdf0e10cSrcweir     SvxTableControllerModifyListener( SvxTableController* pController )
91cdf0e10cSrcweir         : mpController( pController ) {}
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     // XModifyListener
94cdf0e10cSrcweir     virtual void SAL_CALL modified( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::RuntimeException);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     // XEventListener
97cdf0e10cSrcweir     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     SvxTableController* mpController;
100cdf0e10cSrcweir };
101cdf0e10cSrcweir 
102cdf0e10cSrcweir // --------------------------------------------------------------------
103cdf0e10cSrcweir // XModifyListener
104cdf0e10cSrcweir // --------------------------------------------------------------------
105cdf0e10cSrcweir 
106cdf0e10cSrcweir void SAL_CALL SvxTableControllerModifyListener::modified( const ::com::sun::star::lang::EventObject&  ) throw (::com::sun::star::uno::RuntimeException)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     if( mpController )
109cdf0e10cSrcweir         mpController->onTableModified();
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir // --------------------------------------------------------------------
113cdf0e10cSrcweir // XEventListener
114cdf0e10cSrcweir // --------------------------------------------------------------------
115cdf0e10cSrcweir 
116cdf0e10cSrcweir void SAL_CALL SvxTableControllerModifyListener::disposing( const ::com::sun::star::lang::EventObject&  ) throw (::com::sun::star::uno::RuntimeException)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     mpController = 0;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir // --------------------------------------------------------------------
122cdf0e10cSrcweir // class SvxTableController
123cdf0e10cSrcweir // --------------------------------------------------------------------
124cdf0e10cSrcweir 
125cdf0e10cSrcweir rtl::Reference< sdr::SelectionController > CreateTableController( SdrObjEditView* pView, const SdrObject* pObj, const rtl::Reference< sdr::SelectionController >& xRefController )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     return SvxTableController::create( pView, pObj, xRefController );
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir // --------------------------------------------------------------------
131cdf0e10cSrcweir 
132cdf0e10cSrcweir rtl::Reference< sdr::SelectionController > SvxTableController::create( SdrObjEditView* pView, const SdrObject* pObj, const rtl::Reference< sdr::SelectionController >& xRefController )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     if( xRefController.is() )
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         SvxTableController* pController = dynamic_cast< SvxTableController* >( xRefController.get() );
137cdf0e10cSrcweir         if( pController && (pController->mxTableObj.get() == pObj) && (pController->mpView == pView)  )
138cdf0e10cSrcweir             return xRefController;
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir     return new SvxTableController( pView, pObj );
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir // --------------------------------------------------------------------
144cdf0e10cSrcweir 
145cdf0e10cSrcweir SvxTableController::SvxTableController( SdrObjEditView* pView, const SdrObject* pObj )
146cdf0e10cSrcweir : mbCellSelectionMode(false)
147cdf0e10cSrcweir , mbLeftButtonDown(false)
148cdf0e10cSrcweir , mpSelectionOverlay(0)
149cdf0e10cSrcweir , mpView( dynamic_cast< SdrView* >( pView ) )
150cdf0e10cSrcweir , mxTableObj( dynamic_cast< SdrTableObj* >( const_cast< SdrObject* >( pObj ) ) )
151cdf0e10cSrcweir , mpModel( 0 )
152cdf0e10cSrcweir , mnUpdateEvent( 0 )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     if( pObj )
155cdf0e10cSrcweir         mpModel = pObj->GetModel();
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     if( mxTableObj.is() )
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir         static_cast< const SdrTableObj* >( pObj )->getActiveCellPos( maCursorFirstPos );
160cdf0e10cSrcweir         maCursorLastPos = maCursorFirstPos;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         Reference< XTable > xTable( static_cast< const SdrTableObj* >( pObj )->getTable() );
163cdf0e10cSrcweir         if( xTable.is() )
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             mxModifyListener = new SvxTableControllerModifyListener( this );
166cdf0e10cSrcweir             xTable->addModifyListener( mxModifyListener );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir             mxTable.set( dynamic_cast< TableModel* >( xTable.get() ) );
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir // --------------------------------------------------------------------
174cdf0e10cSrcweir 
175cdf0e10cSrcweir SvxTableController::~SvxTableController()
176cdf0e10cSrcweir {
177cdf0e10cSrcweir     if( mnUpdateEvent )
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         Application::RemoveUserEvent( mnUpdateEvent );
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     if( mxModifyListener.is() && mxTableObj.get() )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         Reference< XTable > xTable( static_cast< SdrTableObj* >( mxTableObj.get() )->getTable() );
185cdf0e10cSrcweir         if( xTable.is() )
186cdf0e10cSrcweir         {
187cdf0e10cSrcweir             xTable->removeModifyListener( mxModifyListener );
188cdf0e10cSrcweir             mxModifyListener.clear();
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir // --------------------------------------------------------------------
194cdf0e10cSrcweir 
195cdf0e10cSrcweir const sal_uInt16 ACTION_NONE = 0;
196cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_FIRST_CELL = 1;
197cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_FIRST_COLUMN = 2;
198cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_FIRST_ROW = 3;
199cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_LEFT_CELL = 4;
200cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_UP_CELL = 5;
201cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_RIGHT_CELL = 6;
202cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_DOWN_CELL = 7;
203cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_LAST_CELL = 8;
204cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_LAST_COLUMN = 9;
205cdf0e10cSrcweir const sal_uInt16 ACTION_GOTO_LAST_ROW = 10;
206cdf0e10cSrcweir const sal_uInt16 ACTION_EDIT_CELL = 11;
207cdf0e10cSrcweir const sal_uInt16 ACTION_STOP_TEXT_EDIT = 12;
208cdf0e10cSrcweir const sal_uInt16 ACTION_REMOVE_SELECTION = 13;
209cdf0e10cSrcweir const sal_uInt16 ACTION_START_SELECTION = 14;
210cdf0e10cSrcweir const sal_uInt16 ACTION_HANDLED_BY_VIEW = 15;
211cdf0e10cSrcweir const sal_uInt16 ACTION_TAB = 18;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir bool SvxTableController::onKeyInput(const KeyEvent& rKEvt, Window* pWindow )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir     if( !checkTableObject() )
216cdf0e10cSrcweir         return false;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     // check if we are read only
219cdf0e10cSrcweir     if( mpModel && mpModel->IsReadOnly())
220cdf0e10cSrcweir     {
221cdf0e10cSrcweir         switch( rKEvt.GetKeyCode().GetCode() )
222cdf0e10cSrcweir         {
223cdf0e10cSrcweir         case awt::Key::DOWN:
224cdf0e10cSrcweir         case awt::Key::UP:
225cdf0e10cSrcweir         case awt::Key::LEFT:
226cdf0e10cSrcweir         case awt::Key::RIGHT:
227cdf0e10cSrcweir         case awt::Key::TAB:
228cdf0e10cSrcweir         case awt::Key::HOME:
229cdf0e10cSrcweir         case awt::Key::END:
230cdf0e10cSrcweir         case awt::Key::NUM2:
231cdf0e10cSrcweir         case awt::Key::NUM4:
232cdf0e10cSrcweir         case awt::Key::NUM6:
233cdf0e10cSrcweir         case awt::Key::NUM8:
234cdf0e10cSrcweir         case awt::Key::ESCAPE:
235cdf0e10cSrcweir         case awt::Key::F2:
236cdf0e10cSrcweir             break;
237cdf0e10cSrcweir         default:
238cdf0e10cSrcweir             // tell the view we eat the event, no further processing needed
239cdf0e10cSrcweir             return true;
240cdf0e10cSrcweir         }
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     sal_uInt16 nAction = getKeyboardAction( rKEvt, pWindow );
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     return executeAction( nAction, ( rKEvt.GetKeyCode().IsShift() ) ? sal_True : sal_False, pWindow );
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir // --------------------------------------------------------------------
249cdf0e10cSrcweir // ::com::sun::star::awt::XMouseClickHandler:
250cdf0e10cSrcweir // --------------------------------------------------------------------
251cdf0e10cSrcweir 
252cdf0e10cSrcweir bool SvxTableController::onMouseButtonDown(const MouseEvent& rMEvt, Window* pWindow )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     if( !pWindow || !checkTableObject() )
255cdf0e10cSrcweir         return false;
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     SdrViewEvent aVEvt;
258cdf0e10cSrcweir     if( !rMEvt.IsRight() && mpView->PickAnything(rMEvt,SDRMOUSEBUTTONDOWN, aVEvt) == SDRHIT_HANDLE )
259cdf0e10cSrcweir         return false;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir     TableHitKind eHit = static_cast< SdrTableObj* >(mxTableObj.get())->CheckTableHit( pWindow->PixelToLogic(rMEvt.GetPosPixel()), maMouseDownPos.mnCol, maMouseDownPos.mnRow, 0 );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     mbLeftButtonDown = (rMEvt.GetClicks() == 1) && rMEvt.IsLeft();
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     if( eHit == SDRTABLEHIT_CELL )
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir         StartSelection( maMouseDownPos );
268cdf0e10cSrcweir         return true;
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     if( rMEvt.IsRight() && eHit != SDRTABLEHIT_NONE )
272cdf0e10cSrcweir         return true; // right click will become context menu
273cdf0e10cSrcweir 
274cdf0e10cSrcweir     // for cell selektion with the mouse remember our first hit
275cdf0e10cSrcweir     if( mbLeftButtonDown )
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         RemoveSelection();
278cdf0e10cSrcweir 
279cdf0e10cSrcweir         Point aPnt(rMEvt.GetPosPixel());
280cdf0e10cSrcweir         if (pWindow!=NULL)
281cdf0e10cSrcweir             aPnt=pWindow->PixelToLogic(aPnt);
282cdf0e10cSrcweir 
283cdf0e10cSrcweir         SdrHdl* pHdl = mpView->PickHandle(aPnt);
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         if( pHdl )
286cdf0e10cSrcweir         {
287cdf0e10cSrcweir             mbLeftButtonDown = false;
288cdf0e10cSrcweir         }
289cdf0e10cSrcweir         else
290cdf0e10cSrcweir         {
291cdf0e10cSrcweir             ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
292cdf0e10cSrcweir 
293cdf0e10cSrcweir             if( !pWindow || !pTableObj || eHit  == SDRTABLEHIT_NONE)
294cdf0e10cSrcweir             {
295cdf0e10cSrcweir                 mbLeftButtonDown = false;
296cdf0e10cSrcweir             }
297cdf0e10cSrcweir         }
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     return false;
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
303cdf0e10cSrcweir // --------------------------------------------------------------------
304cdf0e10cSrcweir 
305cdf0e10cSrcweir bool SvxTableController::onMouseButtonUp(const MouseEvent& rMEvt, Window* /*pWin*/)
306cdf0e10cSrcweir {
307cdf0e10cSrcweir     if( !checkTableObject() )
308cdf0e10cSrcweir         return false;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     mbLeftButtonDown = false;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir     if( rMEvt.GetClicks() == 2 )
313cdf0e10cSrcweir         return true;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir     return false;
316cdf0e10cSrcweir }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir // --------------------------------------------------------------------
319cdf0e10cSrcweir 
320cdf0e10cSrcweir bool SvxTableController::onMouseMove(const MouseEvent& rMEvt, Window* pWindow )
321cdf0e10cSrcweir {
322cdf0e10cSrcweir     if( !checkTableObject() )
323cdf0e10cSrcweir         return false;
324cdf0e10cSrcweir 
325cdf0e10cSrcweir     if( rMEvt.IsLeft() )
326cdf0e10cSrcweir     {
327cdf0e10cSrcweir         int i = 0;
328cdf0e10cSrcweir         i++;
329cdf0e10cSrcweir     }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
332cdf0e10cSrcweir     CellPos aPos;
333cdf0e10cSrcweir     if( mbLeftButtonDown && pTableObj && pTableObj->CheckTableHit( pWindow->PixelToLogic(rMEvt.GetPosPixel()), aPos.mnCol, aPos.mnRow, 0 ) != SDRTABLEHIT_NONE )
334cdf0e10cSrcweir     {
335cdf0e10cSrcweir         if(aPos != maMouseDownPos)
336cdf0e10cSrcweir         {
337cdf0e10cSrcweir             if( mbCellSelectionMode )
338cdf0e10cSrcweir             {
339cdf0e10cSrcweir                 setSelectedCells( maMouseDownPos, aPos );
340cdf0e10cSrcweir                 return true;
341cdf0e10cSrcweir             }
342cdf0e10cSrcweir             else
343cdf0e10cSrcweir             {
344cdf0e10cSrcweir                 StartSelection( maMouseDownPos );
345cdf0e10cSrcweir             }
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir         else if( mbCellSelectionMode )
348cdf0e10cSrcweir         {
349cdf0e10cSrcweir             UpdateSelection( aPos );
350cdf0e10cSrcweir             return true;
351cdf0e10cSrcweir         }
352cdf0e10cSrcweir     }
353cdf0e10cSrcweir     return false;
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir // --------------------------------------------------------------------
357cdf0e10cSrcweir 
358cdf0e10cSrcweir void SvxTableController::onSelectionHasChanged()
359cdf0e10cSrcweir {
360cdf0e10cSrcweir     bool bSelected = false;
361cdf0e10cSrcweir 
362cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
363cdf0e10cSrcweir     if( pTableObj && pTableObj->IsTextEditActive() )
364cdf0e10cSrcweir     {
365cdf0e10cSrcweir         pTableObj->getActiveCellPos( maCursorFirstPos );
366cdf0e10cSrcweir         maCursorLastPos = maCursorFirstPos;
367cdf0e10cSrcweir         mbCellSelectionMode = false;
368cdf0e10cSrcweir     }
369cdf0e10cSrcweir     else
370cdf0e10cSrcweir     {
371cdf0e10cSrcweir         const SdrMarkList& rMarkList= mpView->GetMarkedObjectList();
372cdf0e10cSrcweir         if( rMarkList.GetMarkCount() == 1 )
373cdf0e10cSrcweir             bSelected = mxTableObj.get() == rMarkList.GetMark(0)->GetMarkedSdrObj();
374cdf0e10cSrcweir     }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir     if( bSelected )
377cdf0e10cSrcweir     {
378cdf0e10cSrcweir         updateSelectionOverlay();
379cdf0e10cSrcweir     }
380cdf0e10cSrcweir     else
381cdf0e10cSrcweir     {
382cdf0e10cSrcweir         destroySelectionOverlay();
383cdf0e10cSrcweir     }
384cdf0e10cSrcweir }
385cdf0e10cSrcweir 
386cdf0e10cSrcweir // --------------------------------------------------------------------
387cdf0e10cSrcweir 
388cdf0e10cSrcweir void SvxTableController::GetState( SfxItemSet& rSet )
389cdf0e10cSrcweir {
390cdf0e10cSrcweir     if( !mxTable.is() || !mxTableObj.is() || !mxTableObj->GetModel() )
391cdf0e10cSrcweir         return;
392cdf0e10cSrcweir 
393cdf0e10cSrcweir     SfxItemSet* pSet = 0;
394cdf0e10cSrcweir 
395cdf0e10cSrcweir     bool bVertDone = false;
396cdf0e10cSrcweir 
397cdf0e10cSrcweir     // Iterate over all requested items in the set.
398cdf0e10cSrcweir     SfxWhichIter aIter( rSet );
399cdf0e10cSrcweir     sal_uInt16 nWhich = aIter.FirstWhich();
400cdf0e10cSrcweir     while (nWhich)
401cdf0e10cSrcweir     {
402cdf0e10cSrcweir         switch (nWhich)
403cdf0e10cSrcweir         {
404cdf0e10cSrcweir             case SID_TABLE_VERT_BOTTOM:
405cdf0e10cSrcweir             case SID_TABLE_VERT_CENTER:
406cdf0e10cSrcweir             case SID_TABLE_VERT_NONE:
407cdf0e10cSrcweir                 {
408cdf0e10cSrcweir                     if( !mxTable.is() || !mxTableObj->GetModel() )
409cdf0e10cSrcweir                     {
410cdf0e10cSrcweir                         rSet.DisableItem(nWhich);
411cdf0e10cSrcweir                     }
412cdf0e10cSrcweir                     else if(!bVertDone)
413cdf0e10cSrcweir                     {
414cdf0e10cSrcweir                         if( !pSet )
415cdf0e10cSrcweir                         {
416cdf0e10cSrcweir                             pSet = new SfxItemSet( mxTableObj->GetModel()->GetItemPool() );
417cdf0e10cSrcweir                             MergeAttrFromSelectedCells(*pSet, sal_False);
418cdf0e10cSrcweir                         }
419cdf0e10cSrcweir 
420cdf0e10cSrcweir                         SdrTextVertAdjust eAdj = SDRTEXTVERTADJUST_BLOCK;
421cdf0e10cSrcweir 
422cdf0e10cSrcweir                         if( pSet->GetItemState( SDRATTR_TEXT_VERTADJUST ) != SFX_ITEM_DONTCARE )
423cdf0e10cSrcweir                             eAdj = ((SdrTextVertAdjustItem&)(pSet->Get(SDRATTR_TEXT_VERTADJUST))).GetValue();
424cdf0e10cSrcweir 
425cdf0e10cSrcweir                         rSet.Put(SfxBoolItem(SID_TABLE_VERT_BOTTOM, eAdj == SDRTEXTVERTADJUST_BOTTOM));
426cdf0e10cSrcweir                         rSet.Put(SfxBoolItem(SID_TABLE_VERT_CENTER, eAdj == SDRTEXTVERTADJUST_CENTER));
427cdf0e10cSrcweir                         rSet.Put(SfxBoolItem(SID_TABLE_VERT_NONE, eAdj == SDRTEXTVERTADJUST_TOP));
428cdf0e10cSrcweir                         bVertDone = true;
429cdf0e10cSrcweir                     }
430cdf0e10cSrcweir                     break;
431cdf0e10cSrcweir                 }
432cdf0e10cSrcweir             case SID_TABLE_DELETE_ROW:
433cdf0e10cSrcweir                 if( !mxTable.is() || !hasSelectedCells() || (mxTable->getRowCount() <= 1) )
434cdf0e10cSrcweir                     rSet.DisableItem(SID_TABLE_DELETE_ROW);
435cdf0e10cSrcweir                 break;
436cdf0e10cSrcweir             case SID_TABLE_DELETE_COL:
437cdf0e10cSrcweir                 if( !mxTable.is() || !hasSelectedCells() || (mxTable->getColumnCount() <= 1) )
438cdf0e10cSrcweir                     rSet.DisableItem(SID_TABLE_DELETE_COL);
439cdf0e10cSrcweir                 break;
440cdf0e10cSrcweir             case SID_TABLE_MERGE_CELLS:
441cdf0e10cSrcweir                 if( !mxTable.is() || !hasSelectedCells() )
442cdf0e10cSrcweir                     rSet.DisableItem(SID_TABLE_MERGE_CELLS);
443cdf0e10cSrcweir                 break;
444cdf0e10cSrcweir             case SID_TABLE_SPLIT_CELLS:
445cdf0e10cSrcweir                 if( !hasSelectedCells() || !mxTable.is() )
446cdf0e10cSrcweir                     rSet.DisableItem(SID_TABLE_SPLIT_CELLS);
447cdf0e10cSrcweir                 break;
448cdf0e10cSrcweir 
449cdf0e10cSrcweir             case SID_OPTIMIZE_TABLE:
450cdf0e10cSrcweir             case SID_TABLE_DISTRIBUTE_COLUMNS:
451cdf0e10cSrcweir             case SID_TABLE_DISTRIBUTE_ROWS:
452cdf0e10cSrcweir             {
453cdf0e10cSrcweir                 bool bDistributeColumns = false;
454cdf0e10cSrcweir                 bool bDistributeRows = false;
455cdf0e10cSrcweir                 if( mxTable.is() )
456cdf0e10cSrcweir                 {
457cdf0e10cSrcweir                     CellPos aStart, aEnd;
458cdf0e10cSrcweir                     getSelectedCells( aStart, aEnd );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir                     bDistributeColumns = aStart.mnCol != aEnd.mnCol;
461cdf0e10cSrcweir                     bDistributeRows = aStart.mnRow != aEnd.mnRow;
462cdf0e10cSrcweir                 }
463cdf0e10cSrcweir                 if( !bDistributeColumns && !bDistributeRows )
464cdf0e10cSrcweir                     rSet.DisableItem(SID_OPTIMIZE_TABLE);
465cdf0e10cSrcweir                 if( !bDistributeColumns )
466cdf0e10cSrcweir                     rSet.DisableItem(SID_TABLE_DISTRIBUTE_COLUMNS);
467cdf0e10cSrcweir                 if( !bDistributeRows )
468cdf0e10cSrcweir                     rSet.DisableItem(SID_TABLE_DISTRIBUTE_ROWS);
469cdf0e10cSrcweir                 break;
470cdf0e10cSrcweir             }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir             case SID_AUTOFORMAT:
473cdf0e10cSrcweir             case SID_TABLE_SORT_DIALOG:
474cdf0e10cSrcweir             case SID_TABLE_AUTOSUM:
475cdf0e10cSrcweir //              if( !mxTable.is() )
476cdf0e10cSrcweir //                  rSet.DisableItem( nWhich );
477cdf0e10cSrcweir                 break;
478cdf0e10cSrcweir             default:
479cdf0e10cSrcweir                 break;
480cdf0e10cSrcweir         }
481cdf0e10cSrcweir         nWhich = aIter.NextWhich();
482cdf0e10cSrcweir     }
483cdf0e10cSrcweir     if( pSet )
484cdf0e10cSrcweir         delete pSet;
485cdf0e10cSrcweir }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir // --------------------------------------------------------------------
488cdf0e10cSrcweir 
489cdf0e10cSrcweir void SvxTableController::onInsert( sal_uInt16 nSId, const SfxItemSet* pArgs )
490cdf0e10cSrcweir {
491cdf0e10cSrcweir     ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
492cdf0e10cSrcweir     if( !pTableObj )
493cdf0e10cSrcweir         return;
494cdf0e10cSrcweir 
495cdf0e10cSrcweir     if( mxTable.is() ) try
496cdf0e10cSrcweir     {
497cdf0e10cSrcweir         //
498cdf0e10cSrcweir         bool bInsertAfter = true;
499cdf0e10cSrcweir         sal_uInt16 nCount = 0;
500cdf0e10cSrcweir         if( pArgs )
501cdf0e10cSrcweir         {
502cdf0e10cSrcweir             const SfxPoolItem* pItem = 0;
503cdf0e10cSrcweir             pArgs->GetItemState(nSId, sal_False, &pItem);
504cdf0e10cSrcweir             if (pItem)
505cdf0e10cSrcweir             {
506cdf0e10cSrcweir                 nCount = ((const SfxInt16Item* )pItem)->GetValue();
507cdf0e10cSrcweir                 if(SFX_ITEM_SET == pArgs->GetItemState(SID_TABLE_PARAM_INSERT_AFTER, sal_True, &pItem))
508cdf0e10cSrcweir                     bInsertAfter = ((const SfxBoolItem* )pItem)->GetValue();
509cdf0e10cSrcweir             }
510cdf0e10cSrcweir         }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir         CellPos aStart, aEnd;
513cdf0e10cSrcweir         if( hasSelectedCells() )
514cdf0e10cSrcweir         {
515cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
516cdf0e10cSrcweir         }
517cdf0e10cSrcweir         else
518cdf0e10cSrcweir         {
519cdf0e10cSrcweir             if( bInsertAfter )
520cdf0e10cSrcweir             {
521cdf0e10cSrcweir                 aStart.mnCol = mxTable->getColumnCount() - 1;
522cdf0e10cSrcweir                 aStart.mnRow = mxTable->getRowCount() - 1;
523cdf0e10cSrcweir                 aEnd = aStart;
524cdf0e10cSrcweir             }
525cdf0e10cSrcweir         }
526cdf0e10cSrcweir 
527cdf0e10cSrcweir         if( pTableObj->IsTextEditActive() )
528cdf0e10cSrcweir             mpView->SdrEndTextEdit(sal_True);
529cdf0e10cSrcweir 
530cdf0e10cSrcweir         RemoveSelection();
531cdf0e10cSrcweir 
532cdf0e10cSrcweir         const OUString sSize( RTL_CONSTASCII_USTRINGPARAM( "Size" ) );
533cdf0e10cSrcweir 
534cdf0e10cSrcweir         const bool bUndo = mpModel && mpModel->IsUndoEnabled();
535cdf0e10cSrcweir 
536cdf0e10cSrcweir         switch( nSId )
537cdf0e10cSrcweir         {
538cdf0e10cSrcweir         case SID_TABLE_INSERT_COL:
539cdf0e10cSrcweir         {
540cdf0e10cSrcweir             TableModelNotifyGuard aGuard( mxTable.get() );
541cdf0e10cSrcweir 
542cdf0e10cSrcweir             if( bUndo )
543cdf0e10cSrcweir             {
544cdf0e10cSrcweir                 mpModel->BegUndo( ImpGetResStr(STR_TABLE_INSCOL) );
545cdf0e10cSrcweir                 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
546cdf0e10cSrcweir             }
547cdf0e10cSrcweir 
548cdf0e10cSrcweir             Reference< XTableColumns > xCols( mxTable->getColumns() );
549cdf0e10cSrcweir             const sal_Int32 nNewColumns = (nCount == 0) ? (aEnd.mnCol - aStart.mnCol + 1) : nCount;
550cdf0e10cSrcweir             const sal_Int32 nNewStartColumn = aEnd.mnCol + (bInsertAfter ? 1 : 0);
551cdf0e10cSrcweir             xCols->insertByIndex( nNewStartColumn, nNewColumns );
552cdf0e10cSrcweir 
553cdf0e10cSrcweir             for( sal_Int32 nOffset = 0; nOffset < nNewColumns; nOffset++ )
554cdf0e10cSrcweir             {
555cdf0e10cSrcweir                 Reference< XPropertySet >( xCols->getByIndex( aEnd.mnCol + nOffset + 1 ), UNO_QUERY_THROW )->
556cdf0e10cSrcweir                     setPropertyValue( sSize,
557cdf0e10cSrcweir                         Reference< XPropertySet >( xCols->getByIndex( aStart.mnCol + nOffset ), UNO_QUERY_THROW )->
558cdf0e10cSrcweir                             getPropertyValue( sSize ) );
559cdf0e10cSrcweir             }
560cdf0e10cSrcweir 
561cdf0e10cSrcweir             if( bUndo )
562cdf0e10cSrcweir                 mpModel->EndUndo();
563cdf0e10cSrcweir 
564cdf0e10cSrcweir             aStart.mnCol = nNewStartColumn;
565cdf0e10cSrcweir             aStart.mnRow = 0;
566cdf0e10cSrcweir             aEnd.mnCol = aStart.mnCol + nNewColumns - 1;
567cdf0e10cSrcweir             aEnd.mnRow = mxTable->getRowCount() - 1;
568cdf0e10cSrcweir             break;
569cdf0e10cSrcweir         }
570cdf0e10cSrcweir 
571cdf0e10cSrcweir         case SID_TABLE_INSERT_ROW:
572cdf0e10cSrcweir         {
573cdf0e10cSrcweir             TableModelNotifyGuard aGuard( mxTable.get() );
574cdf0e10cSrcweir 
575cdf0e10cSrcweir             if( bUndo )
576cdf0e10cSrcweir             {
577cdf0e10cSrcweir                 mpModel->BegUndo( ImpGetResStr(STR_TABLE_INSROW ) );
578cdf0e10cSrcweir                 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
579cdf0e10cSrcweir             }
580cdf0e10cSrcweir 
581cdf0e10cSrcweir             Reference< XTableRows > xRows( mxTable->getRows() );
582cdf0e10cSrcweir             const sal_Int32 nNewRows = (nCount == 0) ? (aEnd.mnRow - aStart.mnRow + 1) : nCount;
583cdf0e10cSrcweir             const sal_Int32 nNewRowStart = aEnd.mnRow + (bInsertAfter ? 1 : 0);
584cdf0e10cSrcweir             xRows->insertByIndex( nNewRowStart, nNewRows );
585cdf0e10cSrcweir 
586cdf0e10cSrcweir             for( sal_Int32 nOffset = 0; nOffset < nNewRows; nOffset++ )
587cdf0e10cSrcweir             {
588cdf0e10cSrcweir                 Reference< XPropertySet >( xRows->getByIndex( aEnd.mnRow + nOffset + 1 ), UNO_QUERY_THROW )->
589cdf0e10cSrcweir                     setPropertyValue( sSize,
590cdf0e10cSrcweir                         Reference< XPropertySet >( xRows->getByIndex( aStart.mnRow + nOffset ), UNO_QUERY_THROW )->
591cdf0e10cSrcweir                             getPropertyValue( sSize ) );
592cdf0e10cSrcweir             }
593cdf0e10cSrcweir 
594cdf0e10cSrcweir             if( bUndo )
595cdf0e10cSrcweir                 mpModel->EndUndo();
596cdf0e10cSrcweir 
597cdf0e10cSrcweir             aStart.mnCol = 0;
598cdf0e10cSrcweir             aStart.mnRow = nNewRowStart;
599cdf0e10cSrcweir             aEnd.mnCol = mxTable->getColumnCount() - 1;
600cdf0e10cSrcweir             aEnd.mnRow = aStart.mnRow + nNewRows - 1;
601cdf0e10cSrcweir             break;
602cdf0e10cSrcweir         }
603cdf0e10cSrcweir         }
604cdf0e10cSrcweir 
605cdf0e10cSrcweir         StartSelection( aStart );
606cdf0e10cSrcweir         UpdateSelection( aEnd );
607cdf0e10cSrcweir     }
608cdf0e10cSrcweir     catch( Exception& e )
609cdf0e10cSrcweir     {
610cdf0e10cSrcweir         (void)e;
611cdf0e10cSrcweir         DBG_ERROR("svx::SvxTableController::onInsert(), exception caught!");
612cdf0e10cSrcweir     }
613cdf0e10cSrcweir }
614cdf0e10cSrcweir 
615cdf0e10cSrcweir // --------------------------------------------------------------------
616cdf0e10cSrcweir 
617cdf0e10cSrcweir void SvxTableController::onDelete( sal_uInt16 nSId )
618cdf0e10cSrcweir {
619cdf0e10cSrcweir     ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
620cdf0e10cSrcweir     if( !pTableObj )
621cdf0e10cSrcweir         return;
622cdf0e10cSrcweir 
623cdf0e10cSrcweir     if( mxTable.is() && hasSelectedCells() )
624cdf0e10cSrcweir     {
625cdf0e10cSrcweir         CellPos aStart, aEnd;
626cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
627cdf0e10cSrcweir 
628cdf0e10cSrcweir         if( pTableObj->IsTextEditActive() )
629cdf0e10cSrcweir             mpView->SdrEndTextEdit(sal_True);
630cdf0e10cSrcweir 
631cdf0e10cSrcweir         RemoveSelection();
632cdf0e10cSrcweir 
633cdf0e10cSrcweir         bool bDeleteTable = false;
634cdf0e10cSrcweir         switch( nSId )
635cdf0e10cSrcweir         {
636cdf0e10cSrcweir         case SID_TABLE_DELETE_COL:
637cdf0e10cSrcweir         {
638cdf0e10cSrcweir             const sal_Int32 nRemovedColumns = aEnd.mnCol - aStart.mnCol + 1;
639cdf0e10cSrcweir             if( nRemovedColumns == mxTable->getColumnCount() )
640cdf0e10cSrcweir             {
641cdf0e10cSrcweir                 bDeleteTable = true;
642cdf0e10cSrcweir             }
643cdf0e10cSrcweir             else
644cdf0e10cSrcweir             {
645cdf0e10cSrcweir                 Reference< XTableColumns > xCols( mxTable->getColumns() );
646cdf0e10cSrcweir                 xCols->removeByIndex( aStart.mnCol, nRemovedColumns );
647cdf0e10cSrcweir             }
648cdf0e10cSrcweir             break;
649cdf0e10cSrcweir         }
650cdf0e10cSrcweir 
651cdf0e10cSrcweir         case SID_TABLE_DELETE_ROW:
652cdf0e10cSrcweir         {
653cdf0e10cSrcweir             const sal_Int32 nRemovedRows = aEnd.mnRow - aStart.mnRow + 1;
654cdf0e10cSrcweir             if( nRemovedRows == mxTable->getRowCount() )
655cdf0e10cSrcweir             {
656cdf0e10cSrcweir                 bDeleteTable = true;
657cdf0e10cSrcweir             }
658cdf0e10cSrcweir             else
659cdf0e10cSrcweir             {
660cdf0e10cSrcweir                 Reference< XTableRows > xRows( mxTable->getRows() );
661cdf0e10cSrcweir                 xRows->removeByIndex( aStart.mnRow, nRemovedRows );
662cdf0e10cSrcweir             }
663cdf0e10cSrcweir             break;
664cdf0e10cSrcweir         }
665cdf0e10cSrcweir         }
666cdf0e10cSrcweir 
667cdf0e10cSrcweir         if( bDeleteTable )
668cdf0e10cSrcweir             mpView->DeleteMarkedObj();
669cdf0e10cSrcweir         else
670cdf0e10cSrcweir             UpdateTableShape();
671cdf0e10cSrcweir     }
672cdf0e10cSrcweir }
673cdf0e10cSrcweir 
674cdf0e10cSrcweir // --------------------------------------------------------------------
675cdf0e10cSrcweir 
676cdf0e10cSrcweir void SvxTableController::onSelect( sal_uInt16 nSId )
677cdf0e10cSrcweir {
678cdf0e10cSrcweir     if( mxTable.is() )
679cdf0e10cSrcweir     {
680cdf0e10cSrcweir         const sal_Int32 nRowCount = mxTable->getRowCount();
681cdf0e10cSrcweir         const sal_Int32 nColCount = mxTable->getColumnCount();
682cdf0e10cSrcweir         if( nRowCount && nColCount )
683cdf0e10cSrcweir         {
684cdf0e10cSrcweir             CellPos aStart, aEnd;
685cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
686cdf0e10cSrcweir 
687cdf0e10cSrcweir             switch( nSId )
688cdf0e10cSrcweir             {
689cdf0e10cSrcweir             case SID_TABLE_SELECT_ALL:
690cdf0e10cSrcweir                 aEnd.mnCol = 0; aEnd.mnRow = 0;
691cdf0e10cSrcweir                 aStart.mnCol = nColCount - 1; aStart.mnRow = nRowCount - 1;
692cdf0e10cSrcweir                 break;
693cdf0e10cSrcweir             case SID_TABLE_SELECT_COL:
694cdf0e10cSrcweir                 aEnd.mnRow = nRowCount - 1;
695cdf0e10cSrcweir                 aStart.mnRow = 0;
696cdf0e10cSrcweir                 break;
697cdf0e10cSrcweir             case SID_TABLE_SELECT_ROW:
698cdf0e10cSrcweir                 aEnd.mnCol = nColCount - 1;
699cdf0e10cSrcweir                 aStart.mnCol = 0;
700cdf0e10cSrcweir                 break;
701cdf0e10cSrcweir             }
702cdf0e10cSrcweir 
703cdf0e10cSrcweir             StartSelection( aEnd );
704cdf0e10cSrcweir             gotoCell( aStart, true, 0 );
705cdf0e10cSrcweir         }
706cdf0e10cSrcweir     }
707cdf0e10cSrcweir }
708cdf0e10cSrcweir 
709cdf0e10cSrcweir // --------------------------------------------------------------------
710cdf0e10cSrcweir void SvxTableController::onFormatTable( SfxRequest& rReq )
711cdf0e10cSrcweir {
712cdf0e10cSrcweir     ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
713cdf0e10cSrcweir     if( !pTableObj )
714cdf0e10cSrcweir         return;
715cdf0e10cSrcweir 
716cdf0e10cSrcweir     const SfxItemSet* pArgs = rReq.GetArgs();
717cdf0e10cSrcweir 
718cdf0e10cSrcweir     if( !pArgs && pTableObj->GetModel() )
719cdf0e10cSrcweir     {
720cdf0e10cSrcweir         SfxItemSet aNewAttr( pTableObj->GetModel()->GetItemPool() );
721cdf0e10cSrcweir         MergeAttrFromSelectedCells(aNewAttr, sal_False);
722cdf0e10cSrcweir 
723cdf0e10cSrcweir         // merge drawing layer text distance items into SvxBoxItem used by the dialog
724cdf0e10cSrcweir         SvxBoxItem aBoxItem( static_cast< const SvxBoxItem& >( aNewAttr.Get( SDRATTR_TABLE_BORDER ) ) );
725cdf0e10cSrcweir         aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( ((SdrTextLeftDistItem&)(aNewAttr.Get(SDRATTR_TEXT_LEFTDIST))).GetValue()), BOX_LINE_LEFT );
726cdf0e10cSrcweir         aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( ((SdrTextRightDistItem&)(aNewAttr.Get(SDRATTR_TEXT_RIGHTDIST))).GetValue()), BOX_LINE_RIGHT );
727cdf0e10cSrcweir         aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( ((SdrTextUpperDistItem&)(aNewAttr.Get(SDRATTR_TEXT_UPPERDIST))).GetValue()), BOX_LINE_TOP );
728cdf0e10cSrcweir         aBoxItem.SetDistance( sal::static_int_cast< sal_uInt16 >( ((SdrTextLowerDistItem&)(aNewAttr.Get(SDRATTR_TEXT_LOWERDIST))).GetValue()), BOX_LINE_BOTTOM );
729cdf0e10cSrcweir         aNewAttr.Put( aBoxItem );
730cdf0e10cSrcweir 
731cdf0e10cSrcweir         SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
732cdf0e10cSrcweir         std::auto_ptr< SfxAbstractTabDialog > pDlg( pFact ? pFact->CreateSvxFormatCellsDialog( NULL, &aNewAttr, pTableObj->GetModel(), pTableObj) : 0 );
733cdf0e10cSrcweir         if( pDlg.get() && pDlg->Execute() )
734cdf0e10cSrcweir         {
735cdf0e10cSrcweir             SfxItemSet aNewSet( *(pDlg->GetOutputItemSet ()) );
736cdf0e10cSrcweir 
737cdf0e10cSrcweir             SvxBoxItem aNewBoxItem( static_cast< const SvxBoxItem& >( aNewSet.Get( SDRATTR_TABLE_BORDER ) ) );
738cdf0e10cSrcweir 
739cdf0e10cSrcweir             if( aNewBoxItem.GetDistance( BOX_LINE_LEFT ) != aBoxItem.GetDistance( BOX_LINE_LEFT ) )
740cdf0e10cSrcweir                 aNewSet.Put(SdrTextLeftDistItem( aNewBoxItem.GetDistance( BOX_LINE_LEFT ) ) );
741cdf0e10cSrcweir 
742cdf0e10cSrcweir             if( aNewBoxItem.GetDistance( BOX_LINE_RIGHT ) != aBoxItem.GetDistance( BOX_LINE_RIGHT ) )
743cdf0e10cSrcweir                 aNewSet.Put(SdrTextRightDistItem( aNewBoxItem.GetDistance( BOX_LINE_RIGHT ) ) );
744cdf0e10cSrcweir 
745cdf0e10cSrcweir             if( aNewBoxItem.GetDistance( BOX_LINE_TOP ) != aBoxItem.GetDistance( BOX_LINE_TOP ) )
746cdf0e10cSrcweir                 aNewSet.Put(SdrTextUpperDistItem( aNewBoxItem.GetDistance( BOX_LINE_TOP ) ) );
747cdf0e10cSrcweir 
748cdf0e10cSrcweir             if( aNewBoxItem.GetDistance( BOX_LINE_BOTTOM ) != aBoxItem.GetDistance( BOX_LINE_BOTTOM ) )
749cdf0e10cSrcweir                 aNewSet.Put(SdrTextLowerDistItem( aNewBoxItem.GetDistance( BOX_LINE_BOTTOM ) ) );
750cdf0e10cSrcweir 
751cdf0e10cSrcweir             SetAttrToSelectedCells(aNewSet, sal_False);
752cdf0e10cSrcweir         }
753cdf0e10cSrcweir         UpdateTableShape();
754cdf0e10cSrcweir     }
755cdf0e10cSrcweir }
756cdf0e10cSrcweir 
757cdf0e10cSrcweir // --------------------------------------------------------------------
758cdf0e10cSrcweir 
759cdf0e10cSrcweir void SvxTableController::Execute( SfxRequest& rReq )
760cdf0e10cSrcweir {
761cdf0e10cSrcweir     const sal_uInt16 nSId = rReq.GetSlot();
762cdf0e10cSrcweir     switch( nSId )
763cdf0e10cSrcweir     {
764cdf0e10cSrcweir     case SID_TABLE_INSERT_ROW:
765cdf0e10cSrcweir     case SID_TABLE_INSERT_COL:
766cdf0e10cSrcweir         onInsert( nSId, rReq.GetArgs() );
767cdf0e10cSrcweir         break;
768cdf0e10cSrcweir     case SID_TABLE_DELETE_ROW:
769cdf0e10cSrcweir     case SID_TABLE_DELETE_COL:
770cdf0e10cSrcweir         onDelete( nSId );
771cdf0e10cSrcweir         break;
772cdf0e10cSrcweir     case SID_TABLE_SELECT_ALL:
773cdf0e10cSrcweir     case SID_TABLE_SELECT_COL:
774cdf0e10cSrcweir     case SID_TABLE_SELECT_ROW:
775cdf0e10cSrcweir         onSelect( nSId );
776cdf0e10cSrcweir         break;
777cdf0e10cSrcweir     case SID_FORMAT_TABLE_DLG:
778cdf0e10cSrcweir         onFormatTable( rReq );
779cdf0e10cSrcweir         break;
780cdf0e10cSrcweir 
781cdf0e10cSrcweir     case SID_FRAME_LINESTYLE:
782cdf0e10cSrcweir     case SID_FRAME_LINECOLOR:
783cdf0e10cSrcweir     case SID_ATTR_BORDER:
784cdf0e10cSrcweir         {
785cdf0e10cSrcweir             const SfxItemSet* pArgs = rReq.GetArgs();
786cdf0e10cSrcweir             if( pArgs )
787cdf0e10cSrcweir                 ApplyBorderAttr( *pArgs );
788cdf0e10cSrcweir         }
789cdf0e10cSrcweir         break;
790cdf0e10cSrcweir 
791cdf0e10cSrcweir     case SID_ATTR_FILL_STYLE:
792cdf0e10cSrcweir         {
793cdf0e10cSrcweir             const SfxItemSet* pArgs = rReq.GetArgs();
794cdf0e10cSrcweir             if( pArgs )
795cdf0e10cSrcweir                 SetAttributes( *pArgs, false );
796cdf0e10cSrcweir         }
797cdf0e10cSrcweir         break;
798cdf0e10cSrcweir 
799cdf0e10cSrcweir     case SID_TABLE_MERGE_CELLS:
800cdf0e10cSrcweir         MergeMarkedCells();
801cdf0e10cSrcweir         break;
802cdf0e10cSrcweir 
803cdf0e10cSrcweir     case SID_TABLE_SPLIT_CELLS:
804cdf0e10cSrcweir         SplitMarkedCells();
805cdf0e10cSrcweir         break;
806cdf0e10cSrcweir 
807cdf0e10cSrcweir     case SID_TABLE_DISTRIBUTE_COLUMNS:
808cdf0e10cSrcweir         DistributeColumns();
809cdf0e10cSrcweir         break;
810cdf0e10cSrcweir 
811cdf0e10cSrcweir     case SID_TABLE_DISTRIBUTE_ROWS:
812cdf0e10cSrcweir         DistributeRows();
813cdf0e10cSrcweir         break;
814cdf0e10cSrcweir 
815cdf0e10cSrcweir     case SID_TABLE_VERT_BOTTOM:
816cdf0e10cSrcweir     case SID_TABLE_VERT_CENTER:
817cdf0e10cSrcweir     case SID_TABLE_VERT_NONE:
818cdf0e10cSrcweir         SetVertical( nSId );
819cdf0e10cSrcweir         break;
820cdf0e10cSrcweir 
821cdf0e10cSrcweir     case SID_AUTOFORMAT:
822cdf0e10cSrcweir     case SID_TABLE_SORT_DIALOG:
823cdf0e10cSrcweir     case SID_TABLE_AUTOSUM:
824cdf0e10cSrcweir     default:
825cdf0e10cSrcweir         break;
826cdf0e10cSrcweir 
827cdf0e10cSrcweir     case SID_TABLE_STYLE:
828cdf0e10cSrcweir         SetTableStyle( rReq.GetArgs() );
829cdf0e10cSrcweir         break;
830cdf0e10cSrcweir 
831cdf0e10cSrcweir     case SID_TABLE_STYLE_SETTINGS:
832cdf0e10cSrcweir         SetTableStyleSettings( rReq.GetArgs() );
833cdf0e10cSrcweir         break;
834cdf0e10cSrcweir     }
835cdf0e10cSrcweir }
836cdf0e10cSrcweir 
837cdf0e10cSrcweir void SvxTableController::SetTableStyle( const SfxItemSet* pArgs )
838cdf0e10cSrcweir {
839cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
840cdf0e10cSrcweir     SdrModel* pModel = pTableObj ? pTableObj->GetModel() : 0;
841cdf0e10cSrcweir 
842cdf0e10cSrcweir     if( !pTableObj || !pModel || !pArgs || (SFX_ITEM_SET != pArgs->GetItemState(SID_TABLE_STYLE, sal_False)) )
843cdf0e10cSrcweir         return;
844cdf0e10cSrcweir 
845cdf0e10cSrcweir     const SfxStringItem* pArg = dynamic_cast< const SfxStringItem* >( &pArgs->Get( SID_TABLE_STYLE ) );
846cdf0e10cSrcweir     if( pArg && mxTable.is() ) try
847cdf0e10cSrcweir     {
848cdf0e10cSrcweir         Reference< XStyleFamiliesSupplier > xSFS( pModel->getUnoModel(), UNO_QUERY_THROW );
849cdf0e10cSrcweir         Reference< XNameAccess > xFamilyNameAccess( xSFS->getStyleFamilies(), UNO_QUERY_THROW );
850cdf0e10cSrcweir         const OUString sFamilyName( RTL_CONSTASCII_USTRINGPARAM( "table" ) );
851cdf0e10cSrcweir         Reference< XNameAccess > xTableFamilyAccess( xFamilyNameAccess->getByName( sFamilyName ), UNO_QUERY_THROW );
852cdf0e10cSrcweir 
853cdf0e10cSrcweir         if( xTableFamilyAccess->hasByName( pArg->GetValue() ) )
854cdf0e10cSrcweir         {
855cdf0e10cSrcweir             // found table style with the same name
856cdf0e10cSrcweir             Reference< XIndexAccess > xNewTableStyle( xTableFamilyAccess->getByName( pArg->GetValue() ), UNO_QUERY_THROW );
857cdf0e10cSrcweir 
858cdf0e10cSrcweir             const bool bUndo = pModel->IsUndoEnabled();
859cdf0e10cSrcweir 
860cdf0e10cSrcweir             if( bUndo )
861cdf0e10cSrcweir             {
862cdf0e10cSrcweir                 pModel->BegUndo( ImpGetResStr(STR_TABLE_STYLE) );
863cdf0e10cSrcweir                 pModel->AddUndo( new TableStyleUndo( *pTableObj ) );
864cdf0e10cSrcweir             }
865cdf0e10cSrcweir 
866cdf0e10cSrcweir             pTableObj->setTableStyle( xNewTableStyle );
867cdf0e10cSrcweir 
868cdf0e10cSrcweir             const sal_Int32 nRowCount = mxTable->getRowCount();
869cdf0e10cSrcweir             const sal_Int32 nColCount = mxTable->getColumnCount();
870cdf0e10cSrcweir             for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ )
871cdf0e10cSrcweir             {
872cdf0e10cSrcweir                 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
873cdf0e10cSrcweir                 {
874cdf0e10cSrcweir                     CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
875cdf0e10cSrcweir                     if( xCell.is() )
876cdf0e10cSrcweir                     {
877cdf0e10cSrcweir                         SfxItemSet aSet( xCell->GetItemSet() );
878cdf0e10cSrcweir                         bool bChanges = false;
879cdf0e10cSrcweir                         const SfxItemSet& rStyleAttribs = xCell->GetStyleSheet()->GetItemSet();
880cdf0e10cSrcweir 
881cdf0e10cSrcweir                         for ( sal_uInt16 nWhich = SDRATTR_START; nWhich <= SDRATTR_TABLE_LAST; nWhich++ )
882cdf0e10cSrcweir                         {
883cdf0e10cSrcweir                             if( (rStyleAttribs.GetItemState( nWhich ) == SFX_ITEM_ON) && (aSet.GetItemState( nWhich ) == SFX_ITEM_ON) )
884cdf0e10cSrcweir                             {
885cdf0e10cSrcweir                                 aSet.ClearItem( nWhich );
886cdf0e10cSrcweir                                 bChanges = true;
887cdf0e10cSrcweir                             }
888cdf0e10cSrcweir                         }
889cdf0e10cSrcweir 
890cdf0e10cSrcweir                         if( bChanges )
891cdf0e10cSrcweir                         {
892cdf0e10cSrcweir                             if( bUndo )
893cdf0e10cSrcweir                                 xCell->AddUndo();
894cdf0e10cSrcweir 
895cdf0e10cSrcweir                             xCell->SetMergedItemSetAndBroadcast( aSet, sal_True );
896cdf0e10cSrcweir                         }
897cdf0e10cSrcweir                     }
898cdf0e10cSrcweir                 }
899cdf0e10cSrcweir                 catch( Exception& e )
900cdf0e10cSrcweir                 {
901cdf0e10cSrcweir                     (void)e;
902cdf0e10cSrcweir                     DBG_ERROR( "svx::SvxTableController::SetTableStyle(), exception caught!" );
903cdf0e10cSrcweir                 }
904cdf0e10cSrcweir             }
905cdf0e10cSrcweir 
906cdf0e10cSrcweir             if( bUndo )
907cdf0e10cSrcweir                 pModel->EndUndo();
908cdf0e10cSrcweir         }
909cdf0e10cSrcweir     }
910cdf0e10cSrcweir     catch( Exception& e )
911cdf0e10cSrcweir     {
912cdf0e10cSrcweir         (void)e;
913cdf0e10cSrcweir         DBG_ERROR( "svx::SvxTableController::SetTableStyle(), exception caught!" );
914cdf0e10cSrcweir     }
915cdf0e10cSrcweir }
916cdf0e10cSrcweir 
917cdf0e10cSrcweir void SvxTableController::SetTableStyleSettings( const SfxItemSet* pArgs )
918cdf0e10cSrcweir {
919cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
920cdf0e10cSrcweir     SdrModel* pModel = pTableObj ? pTableObj->GetModel() : 0;
921cdf0e10cSrcweir 
922cdf0e10cSrcweir     if( !pTableObj || !pModel )
923cdf0e10cSrcweir         return;
924cdf0e10cSrcweir 
925cdf0e10cSrcweir     TableStyleSettings aSettings( pTableObj->getTableStyleSettings() );
926cdf0e10cSrcweir 
927cdf0e10cSrcweir     const SfxPoolItem *pPoolItem=NULL;
928cdf0e10cSrcweir 
929cdf0e10cSrcweir     if( (SFX_ITEM_SET == pArgs->GetItemState(ID_VAL_USEFIRSTROWSTYLE, sal_False,&pPoolItem)) )
930cdf0e10cSrcweir         aSettings.mbUseFirstRow = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
931cdf0e10cSrcweir 
932cdf0e10cSrcweir     if( (SFX_ITEM_SET == pArgs->GetItemState(ID_VAL_USELASTROWSTYLE, sal_False,&pPoolItem)) )
933cdf0e10cSrcweir         aSettings.mbUseLastRow = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
934cdf0e10cSrcweir 
935cdf0e10cSrcweir     if( (SFX_ITEM_SET == pArgs->GetItemState(ID_VAL_USEBANDINGROWSTYLE, sal_False,&pPoolItem)) )
936cdf0e10cSrcweir         aSettings.mbUseRowBanding = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
937cdf0e10cSrcweir 
938cdf0e10cSrcweir     if( (SFX_ITEM_SET == pArgs->GetItemState(ID_VAL_USEFIRSTCOLUMNSTYLE, sal_False,&pPoolItem)) )
939cdf0e10cSrcweir         aSettings.mbUseFirstColumn = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
940cdf0e10cSrcweir 
941cdf0e10cSrcweir     if( (SFX_ITEM_SET == pArgs->GetItemState(ID_VAL_USELASTCOLUMNSTYLE, sal_False,&pPoolItem)) )
942cdf0e10cSrcweir         aSettings.mbUseLastColumn = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
943cdf0e10cSrcweir 
944cdf0e10cSrcweir     if( (SFX_ITEM_SET == pArgs->GetItemState(ID_VAL_USEBANDINGCOLUMNSTYLE, sal_False,&pPoolItem)) )
945cdf0e10cSrcweir         aSettings.mbUseColumnBanding = static_cast< const SfxBoolItem* >(pPoolItem)->GetValue();
946cdf0e10cSrcweir 
947cdf0e10cSrcweir     if( aSettings == pTableObj->getTableStyleSettings() )
948cdf0e10cSrcweir         return;
949cdf0e10cSrcweir 
950cdf0e10cSrcweir     const bool bUndo = pModel->IsUndoEnabled();
951cdf0e10cSrcweir 
952cdf0e10cSrcweir     if( bUndo )
953cdf0e10cSrcweir     {
954cdf0e10cSrcweir         pModel->BegUndo( ImpGetResStr(STR_TABLE_STYLE_SETTINGS) );
955cdf0e10cSrcweir         pModel->AddUndo( new TableStyleUndo( *pTableObj ) );
956cdf0e10cSrcweir     }
957cdf0e10cSrcweir 
958cdf0e10cSrcweir     pTableObj->setTableStyleSettings( aSettings );
959cdf0e10cSrcweir 
960cdf0e10cSrcweir     if( bUndo )
961cdf0e10cSrcweir         pModel->EndUndo();
962cdf0e10cSrcweir }
963cdf0e10cSrcweir 
964cdf0e10cSrcweir void SvxTableController::SetVertical( sal_uInt16 nSId )
965cdf0e10cSrcweir {
966cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
967cdf0e10cSrcweir     if( mxTable.is() && pTableObj )
968cdf0e10cSrcweir     {
969cdf0e10cSrcweir         TableModelNotifyGuard aGuard( mxTable.get() );
970cdf0e10cSrcweir 
971cdf0e10cSrcweir         CellPos aStart, aEnd;
972cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
973cdf0e10cSrcweir 
974cdf0e10cSrcweir         SdrTextVertAdjust eAdj = SDRTEXTVERTADJUST_TOP;
975cdf0e10cSrcweir 
976cdf0e10cSrcweir         switch( nSId )
977cdf0e10cSrcweir         {
978cdf0e10cSrcweir             case SID_TABLE_VERT_BOTTOM:
979cdf0e10cSrcweir                 eAdj = SDRTEXTVERTADJUST_BOTTOM;
980cdf0e10cSrcweir                 break;
981cdf0e10cSrcweir             case SID_TABLE_VERT_CENTER:
982cdf0e10cSrcweir                 eAdj = SDRTEXTVERTADJUST_CENTER;
983cdf0e10cSrcweir                 break;
984cdf0e10cSrcweir             //case SID_TABLE_VERT_NONE:
985cdf0e10cSrcweir             default:
986cdf0e10cSrcweir                 break;
987cdf0e10cSrcweir         }
988cdf0e10cSrcweir 
989cdf0e10cSrcweir         SdrTextVertAdjustItem aItem( eAdj );
990cdf0e10cSrcweir 
991cdf0e10cSrcweir         for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
992cdf0e10cSrcweir         {
993cdf0e10cSrcweir             for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
994cdf0e10cSrcweir             {
995cdf0e10cSrcweir                 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
996cdf0e10cSrcweir                 if( xCell.is() )
997cdf0e10cSrcweir                     xCell->SetMergedItem(aItem);
998cdf0e10cSrcweir             }
999cdf0e10cSrcweir         }
1000cdf0e10cSrcweir 
1001cdf0e10cSrcweir         UpdateTableShape();
1002cdf0e10cSrcweir     }
1003cdf0e10cSrcweir }
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir void SvxTableController::MergeMarkedCells()
1006cdf0e10cSrcweir {
1007cdf0e10cSrcweir     CellPos aStart, aEnd;
1008cdf0e10cSrcweir     getSelectedCells( aStart, aEnd );
1009cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
1010cdf0e10cSrcweir     if( pTableObj )
1011cdf0e10cSrcweir     {
1012cdf0e10cSrcweir         if( pTableObj->IsTextEditActive() )
1013cdf0e10cSrcweir             mpView->SdrEndTextEdit(sal_True);
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir         TableModelNotifyGuard aGuard( mxTable.get() );
1016cdf0e10cSrcweir         MergeRange( aStart.mnCol, aStart.mnRow, aEnd.mnCol, aEnd.mnRow );
1017cdf0e10cSrcweir     }
1018cdf0e10cSrcweir }
1019cdf0e10cSrcweir 
1020cdf0e10cSrcweir void SvxTableController::SplitMarkedCells()
1021cdf0e10cSrcweir {
1022cdf0e10cSrcweir     if( mxTable.is() )
1023cdf0e10cSrcweir     {
1024cdf0e10cSrcweir         CellPos aStart, aEnd;
1025cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
1026cdf0e10cSrcweir 
1027cdf0e10cSrcweir         SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
1028cdf0e10cSrcweir         std::auto_ptr< SvxAbstractSplittTableDialog > xDlg( pFact ? pFact->CreateSvxSplittTableDialog( NULL, false, 99, 99 ) : 0 );
1029cdf0e10cSrcweir         if( xDlg.get() && xDlg->Execute() )
1030cdf0e10cSrcweir         {
1031cdf0e10cSrcweir             const sal_Int32 nCount = xDlg->GetCount() - 1;
1032cdf0e10cSrcweir             if( nCount < 1 )
1033cdf0e10cSrcweir                 return;
1034cdf0e10cSrcweir 
1035cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
1036cdf0e10cSrcweir 
1037cdf0e10cSrcweir             Reference< XMergeableCellRange > xRange( mxTable->createCursorByRange( mxTable->getCellRangeByPosition( aStart.mnCol, aStart.mnRow, aEnd.mnCol, aEnd.mnRow ) ), UNO_QUERY_THROW );
1038cdf0e10cSrcweir 
1039cdf0e10cSrcweir             const sal_Int32 nRowCount = mxTable->getRowCount();
1040cdf0e10cSrcweir             const sal_Int32 nColCount = mxTable->getColumnCount();
1041cdf0e10cSrcweir 
1042cdf0e10cSrcweir 
1043cdf0e10cSrcweir             SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
1044cdf0e10cSrcweir             if( pTableObj )
1045cdf0e10cSrcweir             {
1046cdf0e10cSrcweir                 if( pTableObj->IsTextEditActive() )
1047cdf0e10cSrcweir                     mpView->SdrEndTextEdit(sal_True);
1048cdf0e10cSrcweir 
1049cdf0e10cSrcweir                 TableModelNotifyGuard aGuard( mxTable.get() );
1050cdf0e10cSrcweir 
1051cdf0e10cSrcweir                 const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1052cdf0e10cSrcweir                 if( bUndo )
1053cdf0e10cSrcweir                 {
1054cdf0e10cSrcweir                     mpModel->BegUndo( ImpGetResStr(STR_TABLE_SPLIT) );
1055cdf0e10cSrcweir                     mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
1056cdf0e10cSrcweir                 }
1057cdf0e10cSrcweir 
1058cdf0e10cSrcweir                 if( xDlg->IsHorizontal() )
1059cdf0e10cSrcweir                 {
1060cdf0e10cSrcweir                     xRange->split( 0, nCount );
1061cdf0e10cSrcweir                 }
1062cdf0e10cSrcweir                 else
1063cdf0e10cSrcweir                 {
1064cdf0e10cSrcweir                     xRange->split( nCount, 0 );
1065cdf0e10cSrcweir                 }
1066cdf0e10cSrcweir 
1067cdf0e10cSrcweir                 if( bUndo )
1068cdf0e10cSrcweir                     mpModel->EndUndo();
1069cdf0e10cSrcweir             }
1070cdf0e10cSrcweir             aEnd.mnRow += mxTable->getRowCount() - nRowCount;
1071cdf0e10cSrcweir             aEnd.mnCol += mxTable->getColumnCount() - nColCount;
1072cdf0e10cSrcweir 
1073cdf0e10cSrcweir             setSelectedCells( aStart, aEnd );
1074cdf0e10cSrcweir         }
1075cdf0e10cSrcweir     }
1076cdf0e10cSrcweir }
1077cdf0e10cSrcweir 
1078cdf0e10cSrcweir void SvxTableController::DistributeColumns()
1079cdf0e10cSrcweir {
1080cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
1081cdf0e10cSrcweir     if( pTableObj )
1082cdf0e10cSrcweir     {
1083cdf0e10cSrcweir         const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1084cdf0e10cSrcweir         if( bUndo )
1085cdf0e10cSrcweir         {
1086cdf0e10cSrcweir             mpModel->BegUndo( ImpGetResStr(STR_TABLE_DISTRIBUTE_COLUMNS) );
1087cdf0e10cSrcweir             mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
1088cdf0e10cSrcweir         }
1089cdf0e10cSrcweir 
1090cdf0e10cSrcweir         CellPos aStart, aEnd;
1091cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
1092cdf0e10cSrcweir         pTableObj->DistributeColumns( aStart.mnCol, aEnd.mnCol );
1093cdf0e10cSrcweir 
1094cdf0e10cSrcweir         if( bUndo )
1095cdf0e10cSrcweir             mpModel->EndUndo();
1096cdf0e10cSrcweir     }
1097cdf0e10cSrcweir }
1098cdf0e10cSrcweir 
1099cdf0e10cSrcweir void SvxTableController::DistributeRows()
1100cdf0e10cSrcweir {
1101cdf0e10cSrcweir     SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxTableObj.get() );
1102cdf0e10cSrcweir     if( pTableObj )
1103cdf0e10cSrcweir     {
1104cdf0e10cSrcweir         const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1105cdf0e10cSrcweir         if( bUndo )
1106cdf0e10cSrcweir         {
1107cdf0e10cSrcweir             mpModel->BegUndo( ImpGetResStr(STR_TABLE_DISTRIBUTE_ROWS) );
1108cdf0e10cSrcweir             mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*pTableObj) );
1109cdf0e10cSrcweir         }
1110cdf0e10cSrcweir 
1111cdf0e10cSrcweir         CellPos aStart, aEnd;
1112cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
1113cdf0e10cSrcweir         pTableObj->DistributeRows( aStart.mnRow, aEnd.mnRow );
1114cdf0e10cSrcweir 
1115cdf0e10cSrcweir         if( bUndo )
1116cdf0e10cSrcweir             mpModel->EndUndo();
1117cdf0e10cSrcweir     }
1118cdf0e10cSrcweir }
1119cdf0e10cSrcweir 
1120cdf0e10cSrcweir bool SvxTableController::DeleteMarked()
1121cdf0e10cSrcweir {
1122cdf0e10cSrcweir     if( mbCellSelectionMode )
1123cdf0e10cSrcweir     {
1124cdf0e10cSrcweir         if( mxTable.is() )
1125cdf0e10cSrcweir         {
1126cdf0e10cSrcweir             CellPos aStart, aEnd;
1127cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
1128cdf0e10cSrcweir             for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
1129cdf0e10cSrcweir             {
1130cdf0e10cSrcweir                 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
1131cdf0e10cSrcweir                 {
1132cdf0e10cSrcweir                     CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1133cdf0e10cSrcweir                     if( xCell.is() )
1134cdf0e10cSrcweir                         xCell->SetOutlinerParaObject( 0 );
1135cdf0e10cSrcweir                 }
1136cdf0e10cSrcweir             }
1137cdf0e10cSrcweir 
1138cdf0e10cSrcweir             UpdateTableShape();
1139cdf0e10cSrcweir             return true;
1140cdf0e10cSrcweir         }
1141cdf0e10cSrcweir     }
1142cdf0e10cSrcweir 
1143cdf0e10cSrcweir     return false;
1144cdf0e10cSrcweir }
1145cdf0e10cSrcweir 
1146cdf0e10cSrcweir bool SvxTableController::GetStyleSheet( SfxStyleSheet*& rpStyleSheet ) const
1147cdf0e10cSrcweir {
1148cdf0e10cSrcweir     if( hasSelectedCells() )
1149cdf0e10cSrcweir     {
1150cdf0e10cSrcweir         rpStyleSheet = 0;
1151cdf0e10cSrcweir 
1152cdf0e10cSrcweir         if( mxTable.is() )
1153cdf0e10cSrcweir         {
1154cdf0e10cSrcweir             SfxStyleSheet* pRet=0;
1155cdf0e10cSrcweir             bool b1st=true;
1156cdf0e10cSrcweir 
1157cdf0e10cSrcweir             CellPos aStart, aEnd;
1158cdf0e10cSrcweir             const_cast<SvxTableController&>(*this).getSelectedCells( aStart, aEnd );
1159cdf0e10cSrcweir 
1160cdf0e10cSrcweir             for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
1161cdf0e10cSrcweir             {
1162cdf0e10cSrcweir                 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
1163cdf0e10cSrcweir                 {
1164cdf0e10cSrcweir                     CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1165cdf0e10cSrcweir                     if( xCell.is() )
1166cdf0e10cSrcweir                     {
1167cdf0e10cSrcweir                         SfxStyleSheet* pSS=xCell->GetStyleSheet();
1168cdf0e10cSrcweir                         if(b1st)
1169cdf0e10cSrcweir                         {
1170cdf0e10cSrcweir                             pRet=pSS;
1171cdf0e10cSrcweir                         }
1172cdf0e10cSrcweir                         else if(pRet != pSS)
1173cdf0e10cSrcweir                         {
1174cdf0e10cSrcweir                             return true;
1175cdf0e10cSrcweir                         }
1176cdf0e10cSrcweir                         b1st=false;
1177cdf0e10cSrcweir                     }
1178cdf0e10cSrcweir                 }
1179cdf0e10cSrcweir             }
1180cdf0e10cSrcweir             rpStyleSheet = pRet;
1181cdf0e10cSrcweir             return true;
1182cdf0e10cSrcweir         }
1183cdf0e10cSrcweir     }
1184cdf0e10cSrcweir     return false;
1185cdf0e10cSrcweir }
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir bool SvxTableController::SetStyleSheet( SfxStyleSheet* pStyleSheet, bool bDontRemoveHardAttr )
1188cdf0e10cSrcweir {
1189cdf0e10cSrcweir     if( hasSelectedCells() && (!pStyleSheet || pStyleSheet->GetFamily() == SFX_STYLE_FAMILY_FRAME) )
1190cdf0e10cSrcweir     {
1191cdf0e10cSrcweir         if( mxTable.is() )
1192cdf0e10cSrcweir         {
1193cdf0e10cSrcweir             CellPos aStart, aEnd;
1194cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
1195cdf0e10cSrcweir 
1196cdf0e10cSrcweir             for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
1197cdf0e10cSrcweir             {
1198cdf0e10cSrcweir                 for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
1199cdf0e10cSrcweir                 {
1200cdf0e10cSrcweir                     CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
1201cdf0e10cSrcweir                     if( xCell.is() )
1202cdf0e10cSrcweir                         xCell->SetStyleSheet(pStyleSheet,bDontRemoveHardAttr);
1203cdf0e10cSrcweir                 }
1204cdf0e10cSrcweir             }
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir             UpdateTableShape();
1207cdf0e10cSrcweir             return true;
1208cdf0e10cSrcweir         }
1209cdf0e10cSrcweir     }
1210cdf0e10cSrcweir     return false;
1211cdf0e10cSrcweir }
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir // --------------------------------------------------------------------
1214cdf0e10cSrcweir // internals
1215cdf0e10cSrcweir // --------------------------------------------------------------------
1216cdf0e10cSrcweir 
1217cdf0e10cSrcweir bool SvxTableController::checkTableObject()
1218cdf0e10cSrcweir {
1219cdf0e10cSrcweir     return mxTableObj.is();
1220cdf0e10cSrcweir }
1221cdf0e10cSrcweir 
1222cdf0e10cSrcweir // --------------------------------------------------------------------
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir sal_uInt16 SvxTableController::getKeyboardAction( const KeyEvent& rKEvt, Window* /*pWindow*/ )
1225cdf0e10cSrcweir {
1226cdf0e10cSrcweir     const bool bMod1 = rKEvt.GetKeyCode().IsMod1(); // ctrl
1227cdf0e10cSrcweir     const bool bMod2 = rKEvt.GetKeyCode().IsMod2() != 0; // Alt
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir     const bool bTextEdit = mpView->IsTextEdit();
1230cdf0e10cSrcweir 
1231cdf0e10cSrcweir     sal_uInt16 nAction = ACTION_HANDLED_BY_VIEW;
1232cdf0e10cSrcweir 
1233cdf0e10cSrcweir     ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
1234cdf0e10cSrcweir     if( !pTableObj )
1235cdf0e10cSrcweir         return nAction;
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir     // handle special keys
1238cdf0e10cSrcweir     const sal_Int16 nCode = rKEvt.GetKeyCode().GetCode();
1239cdf0e10cSrcweir     switch( nCode )
1240cdf0e10cSrcweir     {
1241cdf0e10cSrcweir     case awt::Key::ESCAPE:          // handle escape
1242cdf0e10cSrcweir     {
1243cdf0e10cSrcweir         if( bTextEdit )
1244cdf0e10cSrcweir         {
1245cdf0e10cSrcweir             // escape during text edit ends text edit
1246cdf0e10cSrcweir             nAction = ACTION_STOP_TEXT_EDIT;
1247cdf0e10cSrcweir         }
1248cdf0e10cSrcweir         if( mbCellSelectionMode )
1249cdf0e10cSrcweir         {
1250cdf0e10cSrcweir             // escape with selected cells removes selection
1251cdf0e10cSrcweir             nAction = ACTION_REMOVE_SELECTION;
1252cdf0e10cSrcweir         }
1253cdf0e10cSrcweir         break;
1254cdf0e10cSrcweir     }
1255cdf0e10cSrcweir     case awt::Key::RETURN:      // handle return
1256cdf0e10cSrcweir     {
1257cdf0e10cSrcweir         if( !bMod1 && !bMod2 && !bTextEdit )
1258cdf0e10cSrcweir         {
1259cdf0e10cSrcweir             // when not already editing, return starts text edit
1260cdf0e10cSrcweir             setSelectionStart( pTableObj->getFirstCell() );
1261cdf0e10cSrcweir             nAction = ACTION_EDIT_CELL;
1262cdf0e10cSrcweir         }
1263cdf0e10cSrcweir         break;
1264cdf0e10cSrcweir     }
1265cdf0e10cSrcweir     case awt::Key::F2:          // f2 toggles text edit
1266cdf0e10cSrcweir     {
1267cdf0e10cSrcweir         if( bMod1 || bMod2 )    // f2 with modifiers is handled by the view
1268cdf0e10cSrcweir         {
1269cdf0e10cSrcweir         }
1270cdf0e10cSrcweir         else if( bTextEdit )
1271cdf0e10cSrcweir         {
1272cdf0e10cSrcweir             // f2 during text edit stops text edit
1273cdf0e10cSrcweir             nAction = ACTION_STOP_TEXT_EDIT;
1274cdf0e10cSrcweir         }
1275cdf0e10cSrcweir         else if( mbCellSelectionMode )
1276cdf0e10cSrcweir         {
1277cdf0e10cSrcweir             // f2 with selected cells removes selection
1278cdf0e10cSrcweir             nAction = ACTION_REMOVE_SELECTION;
1279cdf0e10cSrcweir         }
1280cdf0e10cSrcweir         else
1281cdf0e10cSrcweir         {
1282cdf0e10cSrcweir             // f2 with no selection and no text edit starts text edit
1283cdf0e10cSrcweir             setSelectionStart( pTableObj->getFirstCell() );
1284cdf0e10cSrcweir             nAction = ACTION_EDIT_CELL;
1285cdf0e10cSrcweir         }
1286cdf0e10cSrcweir         break;
1287cdf0e10cSrcweir     }
1288cdf0e10cSrcweir     case awt::Key::HOME:
1289cdf0e10cSrcweir     case awt::Key::NUM7:
1290cdf0e10cSrcweir     {
1291cdf0e10cSrcweir         if( (bMod1 ||  bMod2) && (bTextEdit || mbCellSelectionMode) )
1292cdf0e10cSrcweir         {
1293cdf0e10cSrcweir             if( bMod1 && !bMod2 )
1294cdf0e10cSrcweir             {
1295cdf0e10cSrcweir                 // strg + home jumps to first cell
1296cdf0e10cSrcweir                 nAction = ACTION_GOTO_FIRST_CELL;
1297cdf0e10cSrcweir             }
1298cdf0e10cSrcweir             else if( !bMod1 && bMod2 )
1299cdf0e10cSrcweir             {
1300cdf0e10cSrcweir                 // alt + home jumps to first column
1301cdf0e10cSrcweir                 nAction = ACTION_GOTO_FIRST_COLUMN;
1302cdf0e10cSrcweir             }
1303cdf0e10cSrcweir         }
1304cdf0e10cSrcweir         break;
1305cdf0e10cSrcweir     }
1306cdf0e10cSrcweir     case awt::Key::END:
1307cdf0e10cSrcweir     case awt::Key::NUM1:
1308cdf0e10cSrcweir     {
1309cdf0e10cSrcweir         if( (bMod1 ||  bMod2) && (bTextEdit || mbCellSelectionMode) )
1310cdf0e10cSrcweir         {
1311cdf0e10cSrcweir             if( bMod1 && !bMod2 )
1312cdf0e10cSrcweir             {
1313cdf0e10cSrcweir                 // strg + end jumps to last cell
1314cdf0e10cSrcweir                 nAction = ACTION_GOTO_LAST_CELL;
1315cdf0e10cSrcweir             }
1316cdf0e10cSrcweir             else if( !bMod1 && bMod2 )
1317cdf0e10cSrcweir             {
1318cdf0e10cSrcweir                 // alt + home jumps to last column
1319cdf0e10cSrcweir                 nAction = ACTION_GOTO_LAST_COLUMN;
1320cdf0e10cSrcweir             }
1321cdf0e10cSrcweir         }
1322cdf0e10cSrcweir         break;
1323cdf0e10cSrcweir     }
1324cdf0e10cSrcweir 
1325cdf0e10cSrcweir     case awt::Key::TAB:
1326cdf0e10cSrcweir     {
1327cdf0e10cSrcweir         if( bTextEdit || mbCellSelectionMode )
1328cdf0e10cSrcweir             nAction = ACTION_TAB;
1329cdf0e10cSrcweir         break;
1330cdf0e10cSrcweir     }
1331cdf0e10cSrcweir 
1332cdf0e10cSrcweir     case awt::Key::UP:
1333cdf0e10cSrcweir     case awt::Key::NUM8:
1334cdf0e10cSrcweir     case awt::Key::DOWN:
1335cdf0e10cSrcweir     case awt::Key::NUM2:
1336cdf0e10cSrcweir     case awt::Key::LEFT:
1337cdf0e10cSrcweir     case awt::Key::NUM4:
1338cdf0e10cSrcweir     case awt::Key::RIGHT:
1339cdf0e10cSrcweir     case awt::Key::NUM6:
1340cdf0e10cSrcweir     {
1341cdf0e10cSrcweir         bool bTextMove = false;
1342cdf0e10cSrcweir 
1343cdf0e10cSrcweir         if( !bMod1 && bMod2 )
1344cdf0e10cSrcweir         {
1345cdf0e10cSrcweir             if( (nCode == awt::Key::UP) || (nCode == awt::Key::NUM8) )
1346cdf0e10cSrcweir             {
1347cdf0e10cSrcweir                 nAction = ACTION_GOTO_LEFT_CELL;
1348cdf0e10cSrcweir             }
1349cdf0e10cSrcweir             else if( (nCode == awt::Key::DOWN) || (nCode == awt::Key::NUM2) )
1350cdf0e10cSrcweir             {
1351cdf0e10cSrcweir                 nAction = ACTION_GOTO_RIGHT_CELL;
1352cdf0e10cSrcweir             }
1353cdf0e10cSrcweir             break;
1354cdf0e10cSrcweir         }
1355cdf0e10cSrcweir 
1356cdf0e10cSrcweir         if( !bTextMove )
1357cdf0e10cSrcweir         {
1358cdf0e10cSrcweir             OutlinerView* pOLV = mpView->GetTextEditOutlinerView();
1359cdf0e10cSrcweir             if( pOLV )
1360cdf0e10cSrcweir             {
1361cdf0e10cSrcweir                 RemoveSelection();
1362cdf0e10cSrcweir                 // during text edit, check if we navigate out of the cell
1363cdf0e10cSrcweir                 ESelection aOldSelection = pOLV->GetSelection();
1364cdf0e10cSrcweir                 pOLV->PostKeyEvent(rKEvt);
1365cdf0e10cSrcweir                 bTextMove = pOLV && ( aOldSelection.IsEqual(pOLV->GetSelection()) );
1366cdf0e10cSrcweir                 if( !bTextMove )
1367cdf0e10cSrcweir                 {
1368cdf0e10cSrcweir                     nAction = ACTION_NONE;
1369cdf0e10cSrcweir                 }
1370cdf0e10cSrcweir             }
1371cdf0e10cSrcweir         }
1372cdf0e10cSrcweir 
1373cdf0e10cSrcweir         if( mbCellSelectionMode || bTextMove )
1374cdf0e10cSrcweir         {
1375cdf0e10cSrcweir             // no text edit, navigate in cells if selection active
1376cdf0e10cSrcweir             switch( nCode )
1377cdf0e10cSrcweir             {
1378cdf0e10cSrcweir             case awt::Key::LEFT:
1379cdf0e10cSrcweir             case awt::Key::NUM4:
1380cdf0e10cSrcweir                 nAction = ACTION_GOTO_LEFT_CELL;
1381cdf0e10cSrcweir                 break;
1382cdf0e10cSrcweir             case awt::Key::RIGHT:
1383cdf0e10cSrcweir             case awt::Key::NUM6:
1384cdf0e10cSrcweir                 nAction = ACTION_GOTO_RIGHT_CELL;
1385cdf0e10cSrcweir                 break;
1386cdf0e10cSrcweir             case awt::Key::DOWN:
1387cdf0e10cSrcweir             case awt::Key::NUM2:
1388cdf0e10cSrcweir                 nAction = ACTION_GOTO_DOWN_CELL;
1389cdf0e10cSrcweir                 break;
1390cdf0e10cSrcweir             case awt::Key::UP:
1391cdf0e10cSrcweir             case awt::Key::NUM8:
1392cdf0e10cSrcweir                 nAction = ACTION_GOTO_UP_CELL;
1393cdf0e10cSrcweir                 break;
1394cdf0e10cSrcweir             }
1395cdf0e10cSrcweir         }
1396cdf0e10cSrcweir         break;
1397cdf0e10cSrcweir     }
1398cdf0e10cSrcweir     case awt::Key::PAGEUP:
1399cdf0e10cSrcweir         if( bMod2 )
1400cdf0e10cSrcweir             nAction = ACTION_GOTO_FIRST_ROW;
1401cdf0e10cSrcweir         break;
1402cdf0e10cSrcweir 
1403cdf0e10cSrcweir     case awt::Key::PAGEDOWN:
1404cdf0e10cSrcweir         if( bMod2 )
1405cdf0e10cSrcweir             nAction = ACTION_GOTO_LAST_ROW;
1406cdf0e10cSrcweir         break;
1407cdf0e10cSrcweir     }
1408cdf0e10cSrcweir     return nAction;
1409cdf0e10cSrcweir }
1410cdf0e10cSrcweir 
1411cdf0e10cSrcweir bool SvxTableController::executeAction( sal_uInt16 nAction, bool bSelect, Window* pWindow )
1412cdf0e10cSrcweir {
1413cdf0e10cSrcweir     ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
1414cdf0e10cSrcweir     if( !pTableObj )
1415cdf0e10cSrcweir         return false;
1416cdf0e10cSrcweir 
1417cdf0e10cSrcweir     switch( nAction )
1418cdf0e10cSrcweir     {
1419cdf0e10cSrcweir     case ACTION_GOTO_FIRST_CELL:
1420cdf0e10cSrcweir     {
1421cdf0e10cSrcweir         gotoCell( pTableObj->getFirstCell(), bSelect, pWindow, nAction );
1422cdf0e10cSrcweir         break;
1423cdf0e10cSrcweir     }
1424cdf0e10cSrcweir 
1425cdf0e10cSrcweir     case ACTION_GOTO_LEFT_CELL:
1426cdf0e10cSrcweir     {
1427cdf0e10cSrcweir         gotoCell( pTableObj->getLeftCell( getSelectionEnd(), !bSelect ), bSelect, pWindow, nAction );
1428cdf0e10cSrcweir         break;
1429cdf0e10cSrcweir     }
1430cdf0e10cSrcweir 
1431cdf0e10cSrcweir     case ACTION_GOTO_RIGHT_CELL:
1432cdf0e10cSrcweir     {
1433cdf0e10cSrcweir         gotoCell( pTableObj->getRightCell( getSelectionEnd(), !bSelect ), bSelect, pWindow, nAction);
1434cdf0e10cSrcweir         break;
1435cdf0e10cSrcweir     }
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir     case ACTION_GOTO_LAST_CELL:
1438cdf0e10cSrcweir     {
1439cdf0e10cSrcweir         gotoCell( pTableObj->getLastCell(), bSelect, pWindow, nAction );
1440cdf0e10cSrcweir         break;
1441cdf0e10cSrcweir     }
1442cdf0e10cSrcweir 
1443cdf0e10cSrcweir     case ACTION_GOTO_FIRST_COLUMN:
1444cdf0e10cSrcweir     {
1445cdf0e10cSrcweir         CellPos aPos( pTableObj->getFirstCell().mnCol, getSelectionEnd().mnRow );
1446cdf0e10cSrcweir         gotoCell( aPos, bSelect, pWindow, nAction );
1447cdf0e10cSrcweir         break;
1448cdf0e10cSrcweir     }
1449cdf0e10cSrcweir 
1450cdf0e10cSrcweir     case ACTION_GOTO_LAST_COLUMN:
1451cdf0e10cSrcweir     {
1452cdf0e10cSrcweir         CellPos aPos( pTableObj->getLastCell().mnCol, getSelectionEnd().mnRow );
1453cdf0e10cSrcweir         gotoCell( aPos, bSelect, pWindow, nAction );
1454cdf0e10cSrcweir         break;
1455cdf0e10cSrcweir     }
1456cdf0e10cSrcweir 
1457cdf0e10cSrcweir     case ACTION_GOTO_FIRST_ROW:
1458cdf0e10cSrcweir     {
1459cdf0e10cSrcweir         CellPos aPos( getSelectionEnd().mnCol, pTableObj->getFirstCell().mnRow );
1460cdf0e10cSrcweir         gotoCell( aPos, bSelect, pWindow, nAction );
1461cdf0e10cSrcweir         break;
1462cdf0e10cSrcweir     }
1463cdf0e10cSrcweir 
1464cdf0e10cSrcweir     case ACTION_GOTO_UP_CELL:
1465cdf0e10cSrcweir     {
1466cdf0e10cSrcweir         gotoCell( pTableObj->getUpCell(getSelectionEnd(), !bSelect), bSelect, pWindow, nAction );
1467cdf0e10cSrcweir         break;
1468cdf0e10cSrcweir     }
1469cdf0e10cSrcweir 
1470cdf0e10cSrcweir     case ACTION_GOTO_DOWN_CELL:
1471cdf0e10cSrcweir     {
1472cdf0e10cSrcweir         gotoCell( pTableObj->getDownCell(getSelectionEnd(), !bSelect), bSelect, pWindow, nAction );
1473cdf0e10cSrcweir         break;
1474cdf0e10cSrcweir     }
1475cdf0e10cSrcweir 
1476cdf0e10cSrcweir     case ACTION_GOTO_LAST_ROW:
1477cdf0e10cSrcweir     {
1478cdf0e10cSrcweir         CellPos aPos( getSelectionEnd().mnCol, pTableObj->getLastCell().mnRow );
1479cdf0e10cSrcweir         gotoCell( aPos, bSelect, pWindow, nAction );
1480cdf0e10cSrcweir         break;
1481cdf0e10cSrcweir     }
1482cdf0e10cSrcweir 
1483cdf0e10cSrcweir     case ACTION_EDIT_CELL:
1484cdf0e10cSrcweir         EditCell( getSelectionStart(), pWindow, 0, nAction );
1485cdf0e10cSrcweir         break;
1486cdf0e10cSrcweir 
1487cdf0e10cSrcweir     case ACTION_STOP_TEXT_EDIT:
1488cdf0e10cSrcweir         StopTextEdit();
1489cdf0e10cSrcweir         break;
1490cdf0e10cSrcweir 
1491cdf0e10cSrcweir     case ACTION_REMOVE_SELECTION:
1492cdf0e10cSrcweir         RemoveSelection();
1493cdf0e10cSrcweir         break;
1494cdf0e10cSrcweir 
1495cdf0e10cSrcweir     case ACTION_START_SELECTION:
1496cdf0e10cSrcweir         StartSelection( getSelectionStart() );
1497cdf0e10cSrcweir         break;
1498cdf0e10cSrcweir 
1499cdf0e10cSrcweir     case ACTION_TAB:
1500cdf0e10cSrcweir     {
1501cdf0e10cSrcweir         if( bSelect )
1502cdf0e10cSrcweir             gotoCell( pTableObj->getPreviousCell( getSelectionEnd(), true ), false, pWindow, nAction );
1503cdf0e10cSrcweir         else
1504cdf0e10cSrcweir         {
1505cdf0e10cSrcweir             CellPos aSelectionEnd( getSelectionEnd() );
1506cdf0e10cSrcweir             CellPos aNextCell( pTableObj->getNextCell( aSelectionEnd, true ) );
1507cdf0e10cSrcweir             if( aSelectionEnd == aNextCell )
1508cdf0e10cSrcweir             {
1509cdf0e10cSrcweir                 onInsert( SID_TABLE_INSERT_ROW, 0 );
1510cdf0e10cSrcweir                 aNextCell = pTableObj->getNextCell( aSelectionEnd, true );
1511cdf0e10cSrcweir             }
1512cdf0e10cSrcweir             gotoCell( aNextCell, false, pWindow, nAction );
1513cdf0e10cSrcweir         }
1514cdf0e10cSrcweir         break;
1515cdf0e10cSrcweir     }
1516cdf0e10cSrcweir     }
1517cdf0e10cSrcweir 
1518cdf0e10cSrcweir     return nAction != ACTION_HANDLED_BY_VIEW;
1519cdf0e10cSrcweir }
1520cdf0e10cSrcweir 
1521cdf0e10cSrcweir // --------------------------------------------------------------------
1522cdf0e10cSrcweir 
1523cdf0e10cSrcweir void SvxTableController::gotoCell( const CellPos& rPos, bool bSelect, Window* pWindow, sal_uInt16 nAction )
1524cdf0e10cSrcweir {
1525cdf0e10cSrcweir     if( mxTableObj.is() && static_cast<SdrTableObj*>(mxTableObj.get())->IsTextEditActive() )
1526cdf0e10cSrcweir         mpView->SdrEndTextEdit(sal_True);
1527cdf0e10cSrcweir 
1528cdf0e10cSrcweir     if( bSelect )
1529cdf0e10cSrcweir     {
1530cdf0e10cSrcweir         maCursorLastPos = rPos;
1531cdf0e10cSrcweir         if( mxTableObj.is() )
1532cdf0e10cSrcweir             static_cast< SdrTableObj* >( mxTableObj.get() )->setActiveCell( rPos );
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir         if( !mbCellSelectionMode )
1535cdf0e10cSrcweir         {
1536cdf0e10cSrcweir             setSelectedCells( maCursorFirstPos, rPos );
1537cdf0e10cSrcweir         }
1538cdf0e10cSrcweir         else
1539cdf0e10cSrcweir         {
1540cdf0e10cSrcweir             UpdateSelection( rPos );
1541cdf0e10cSrcweir         }
1542cdf0e10cSrcweir     }
1543cdf0e10cSrcweir     else
1544cdf0e10cSrcweir     {
1545cdf0e10cSrcweir         RemoveSelection();
1546cdf0e10cSrcweir         EditCell( rPos, pWindow, 0, nAction );
1547cdf0e10cSrcweir     }
1548cdf0e10cSrcweir }
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir // --------------------------------------------------------------------
1551cdf0e10cSrcweir 
1552cdf0e10cSrcweir const CellPos& SvxTableController::getSelectionStart()
1553cdf0e10cSrcweir {
1554cdf0e10cSrcweir     checkCell( maCursorFirstPos );
1555cdf0e10cSrcweir     return maCursorFirstPos;
1556cdf0e10cSrcweir }
1557cdf0e10cSrcweir 
1558cdf0e10cSrcweir // --------------------------------------------------------------------
1559cdf0e10cSrcweir 
1560cdf0e10cSrcweir void SvxTableController::setSelectionStart( const CellPos& rPos )
1561cdf0e10cSrcweir {
1562cdf0e10cSrcweir     maCursorFirstPos = rPos;
1563cdf0e10cSrcweir }
1564cdf0e10cSrcweir 
1565cdf0e10cSrcweir // --------------------------------------------------------------------
1566cdf0e10cSrcweir 
1567cdf0e10cSrcweir const CellPos& SvxTableController::getSelectionEnd()
1568cdf0e10cSrcweir {
1569cdf0e10cSrcweir     checkCell( maCursorLastPos );
1570cdf0e10cSrcweir     return maCursorLastPos;
1571cdf0e10cSrcweir }
1572cdf0e10cSrcweir 
1573cdf0e10cSrcweir // --------------------------------------------------------------------
1574cdf0e10cSrcweir 
1575cdf0e10cSrcweir Reference< XCellCursor > SvxTableController::getSelectionCursor()
1576cdf0e10cSrcweir {
1577cdf0e10cSrcweir     Reference< XCellCursor > xCursor;
1578cdf0e10cSrcweir 
1579cdf0e10cSrcweir     if( mxTable.is() )
1580cdf0e10cSrcweir     {
1581cdf0e10cSrcweir         if( hasSelectedCells() )
1582cdf0e10cSrcweir         {
1583cdf0e10cSrcweir             CellPos aStart, aEnd;
1584cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
1585cdf0e10cSrcweir             xCursor = mxTable->createCursorByRange( mxTable->getCellRangeByPosition( aStart.mnCol, aStart.mnRow, aEnd.mnCol, aEnd.mnRow ) );
1586cdf0e10cSrcweir         }
1587cdf0e10cSrcweir         else
1588cdf0e10cSrcweir         {
1589cdf0e10cSrcweir             xCursor = mxTable->createCursor();
1590cdf0e10cSrcweir         }
1591cdf0e10cSrcweir     }
1592cdf0e10cSrcweir 
1593cdf0e10cSrcweir     return xCursor;
1594cdf0e10cSrcweir }
1595cdf0e10cSrcweir 
1596cdf0e10cSrcweir void SvxTableController::MergeRange( sal_Int32 nFirstCol, sal_Int32 nFirstRow, sal_Int32 nLastCol, sal_Int32 nLastRow )
1597cdf0e10cSrcweir {
1598cdf0e10cSrcweir     if( mxTable.is() ) try
1599cdf0e10cSrcweir     {
1600cdf0e10cSrcweir         Reference< XMergeableCellRange > xRange( mxTable->createCursorByRange( mxTable->getCellRangeByPosition( nFirstCol, nFirstRow,nLastCol, nLastRow ) ), UNO_QUERY_THROW );
1601cdf0e10cSrcweir         if( xRange->isMergeable() )
1602cdf0e10cSrcweir         {
1603cdf0e10cSrcweir             const bool bUndo = mpModel && mpModel->IsUndoEnabled();
1604cdf0e10cSrcweir             if( bUndo )
1605cdf0e10cSrcweir             {
1606cdf0e10cSrcweir                 mpModel->BegUndo( ImpGetResStr(STR_TABLE_MERGE) );
1607cdf0e10cSrcweir                 mpModel->AddUndo( mpModel->GetSdrUndoFactory().CreateUndoGeoObject(*mxTableObj.get()) );
1608cdf0e10cSrcweir             }
1609cdf0e10cSrcweir 
1610cdf0e10cSrcweir             xRange->merge();
1611cdf0e10cSrcweir 
1612cdf0e10cSrcweir             if( bUndo )
1613cdf0e10cSrcweir                 mpModel->EndUndo();
1614cdf0e10cSrcweir         }
1615cdf0e10cSrcweir     }
1616cdf0e10cSrcweir     catch( Exception& )
1617cdf0e10cSrcweir     {
1618cdf0e10cSrcweir         DBG_ASSERT( false, "sdr::table::SvxTableController::MergeRange(), exception caught!" );
1619cdf0e10cSrcweir     }
1620cdf0e10cSrcweir }
1621cdf0e10cSrcweir 
1622cdf0e10cSrcweir 
1623cdf0e10cSrcweir 
1624cdf0e10cSrcweir // --------------------------------------------------------------------
1625cdf0e10cSrcweir 
1626cdf0e10cSrcweir void SvxTableController::checkCell( CellPos& rPos )
1627cdf0e10cSrcweir {
1628cdf0e10cSrcweir     if( mxTable.is() ) try
1629cdf0e10cSrcweir     {
1630cdf0e10cSrcweir         if( rPos.mnCol >= mxTable->getColumnCount() )
1631cdf0e10cSrcweir             rPos.mnCol = mxTable->getColumnCount()-1;
1632cdf0e10cSrcweir 
1633cdf0e10cSrcweir         if( rPos.mnRow >= mxTable->getRowCount() )
1634cdf0e10cSrcweir             rPos.mnRow = mxTable->getRowCount()-1;
1635cdf0e10cSrcweir     }
1636cdf0e10cSrcweir     catch( Exception& e )
1637cdf0e10cSrcweir     {
1638cdf0e10cSrcweir         (void)e;
1639cdf0e10cSrcweir         DBG_ERROR("sdr::table::SvxTableController::checkCell(), exception caught!" );
1640cdf0e10cSrcweir     }
1641cdf0e10cSrcweir }
1642cdf0e10cSrcweir 
1643cdf0e10cSrcweir // --------------------------------------------------------------------
1644cdf0e10cSrcweir 
1645cdf0e10cSrcweir void SvxTableController::findMergeOrigin( CellPos& rPos )
1646cdf0e10cSrcweir {
1647cdf0e10cSrcweir     if( mxTable.is() ) try
1648cdf0e10cSrcweir     {
1649cdf0e10cSrcweir         Reference< XMergeableCell > xCell( mxTable->getCellByPosition( rPos.mnCol, rPos.mnRow ), UNO_QUERY_THROW );
1650cdf0e10cSrcweir         if( xCell.is() && xCell->isMerged() )
1651cdf0e10cSrcweir         {
1652cdf0e10cSrcweir             ::findMergeOrigin( mxTable, rPos.mnCol, rPos.mnRow, rPos.mnCol, rPos.mnRow );
1653cdf0e10cSrcweir         }
1654cdf0e10cSrcweir     }
1655cdf0e10cSrcweir     catch( Exception& e )
1656cdf0e10cSrcweir     {
1657cdf0e10cSrcweir         (void)e;
1658cdf0e10cSrcweir         DBG_ERROR("sdr::table::SvxTableController::findMergeOrigin(), exception caught!" );
1659cdf0e10cSrcweir     }
1660cdf0e10cSrcweir }
1661cdf0e10cSrcweir 
1662cdf0e10cSrcweir // --------------------------------------------------------------------
1663cdf0e10cSrcweir 
1664cdf0e10cSrcweir void SvxTableController::EditCell( const CellPos& rPos, ::Window* pWindow, const awt::MouseEvent* pMouseEvent /*= 0*/, sal_uInt16 nAction /*= ACTION_NONE */ )
1665cdf0e10cSrcweir {
1666cdf0e10cSrcweir     SdrPageView* pPV = mpView->GetSdrPageView();
1667cdf0e10cSrcweir 
1668cdf0e10cSrcweir     ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
1669cdf0e10cSrcweir     if( pTableObj && pTableObj->GetPage() == pPV->GetPage() )
1670cdf0e10cSrcweir     {
1671cdf0e10cSrcweir         bool bEmptyOutliner = false;
1672cdf0e10cSrcweir 
1673cdf0e10cSrcweir         if(!pTableObj->GetOutlinerParaObject() && mpView->GetTextEditOutliner())
1674cdf0e10cSrcweir         {
1675cdf0e10cSrcweir             ::Outliner* pOutl = mpView->GetTextEditOutliner();
1676cdf0e10cSrcweir             sal_uIntPtr nParaAnz = pOutl->GetParagraphCount();
1677cdf0e10cSrcweir             Paragraph* p1stPara = pOutl->GetParagraph( 0 );
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir             if(nParaAnz==1 && p1stPara)
1680cdf0e10cSrcweir             {
1681cdf0e10cSrcweir                 // Bei nur einem Pararaph
1682cdf0e10cSrcweir                 if (pOutl->GetText(p1stPara).Len() == 0)
1683cdf0e10cSrcweir                 {
1684cdf0e10cSrcweir                     bEmptyOutliner = true;
1685cdf0e10cSrcweir                 }
1686cdf0e10cSrcweir             }
1687cdf0e10cSrcweir         }
1688cdf0e10cSrcweir 
1689cdf0e10cSrcweir         CellPos aPos( rPos );
1690cdf0e10cSrcweir         findMergeOrigin( aPos );
1691cdf0e10cSrcweir 
1692cdf0e10cSrcweir         if( pTableObj != mpView->GetTextEditObject() || bEmptyOutliner || !pTableObj->IsTextEditActive( aPos ) )
1693cdf0e10cSrcweir         {
1694cdf0e10cSrcweir             if( pTableObj->IsTextEditActive() )
1695cdf0e10cSrcweir                 mpView->SdrEndTextEdit(sal_True);
1696cdf0e10cSrcweir 
1697cdf0e10cSrcweir             pTableObj->setActiveCell( aPos );
1698cdf0e10cSrcweir 
1699cdf0e10cSrcweir             // create new outliner, owner will be the SdrObjEditView
1700cdf0e10cSrcweir             SdrOutliner* pOutl = SdrMakeOutliner( OUTLINERMODE_OUTLINEOBJECT, mpModel );
1701cdf0e10cSrcweir             if( pTableObj->IsVerticalWriting() )
1702cdf0e10cSrcweir                 pOutl->SetVertical( sal_True );
1703cdf0e10cSrcweir 
1704cdf0e10cSrcweir             if(mpView->SdrBeginTextEdit(pTableObj, pPV, pWindow, sal_True, pOutl))
1705cdf0e10cSrcweir             {
1706cdf0e10cSrcweir                 maCursorLastPos = maCursorFirstPos = rPos;
1707cdf0e10cSrcweir 
1708cdf0e10cSrcweir                 OutlinerView* pOLV = mpView->GetTextEditOutlinerView();
1709cdf0e10cSrcweir 
1710cdf0e10cSrcweir                 bool bNoSel = true;
1711cdf0e10cSrcweir 
1712cdf0e10cSrcweir                 if( pMouseEvent )
1713cdf0e10cSrcweir                 {
1714cdf0e10cSrcweir                     ::MouseEvent aMEvt( *pMouseEvent );
1715cdf0e10cSrcweir 
1716cdf0e10cSrcweir                     SdrViewEvent aVEvt;
1717cdf0e10cSrcweir                     SdrHitKind eHit = mpView->PickAnything(aMEvt, SDRMOUSEBUTTONDOWN, aVEvt);
1718cdf0e10cSrcweir 
1719cdf0e10cSrcweir                     if (eHit == SDRHIT_TEXTEDIT)
1720cdf0e10cSrcweir                     {
1721cdf0e10cSrcweir                         // Text getroffen
1722cdf0e10cSrcweir                         pOLV->MouseButtonDown(aMEvt);
1723cdf0e10cSrcweir                         pOLV->MouseMove(aMEvt);
1724cdf0e10cSrcweir                         pOLV->MouseButtonUp(aMEvt);
1725cdf0e10cSrcweir //                      pOLV->MouseButtonDown(aMEvt);
1726cdf0e10cSrcweir                         bNoSel = false;
1727cdf0e10cSrcweir                     }
1728cdf0e10cSrcweir                     else
1729cdf0e10cSrcweir                     {
1730cdf0e10cSrcweir                         nAction = ACTION_GOTO_LEFT_CELL;
1731cdf0e10cSrcweir                     }
1732cdf0e10cSrcweir                 }
1733cdf0e10cSrcweir 
1734cdf0e10cSrcweir                 if( bNoSel )
1735cdf0e10cSrcweir                 {
1736cdf0e10cSrcweir                     // Move cursor to end of text
1737cdf0e10cSrcweir                     ESelection aNewSelection;
1738cdf0e10cSrcweir 
1739cdf0e10cSrcweir                     const WritingMode eMode = pTableObj->GetWritingMode();
1740cdf0e10cSrcweir                     if( ((nAction == ACTION_GOTO_LEFT_CELL) || (nAction == ACTION_GOTO_RIGHT_CELL)) && (eMode != WritingMode_TB_RL) )
1741cdf0e10cSrcweir                     {
1742cdf0e10cSrcweir                         const bool bLast = ((nAction == ACTION_GOTO_LEFT_CELL) && (eMode == WritingMode_LR_TB)) ||
1743cdf0e10cSrcweir                                              ((nAction == ACTION_GOTO_RIGHT_CELL) && (eMode == WritingMode_RL_TB));
1744cdf0e10cSrcweir 
1745cdf0e10cSrcweir                         if( bLast )
1746cdf0e10cSrcweir                             aNewSelection = ESelection(EE_PARA_NOT_FOUND, EE_INDEX_NOT_FOUND, EE_PARA_NOT_FOUND, EE_INDEX_NOT_FOUND);
1747cdf0e10cSrcweir                     }
1748cdf0e10cSrcweir                     pOLV->SetSelection(aNewSelection);
1749cdf0e10cSrcweir                 }
1750cdf0e10cSrcweir             }
1751cdf0e10cSrcweir         }
1752cdf0e10cSrcweir     }
1753cdf0e10cSrcweir }
1754cdf0e10cSrcweir 
1755cdf0e10cSrcweir // --------------------------------------------------------------------
1756cdf0e10cSrcweir 
1757cdf0e10cSrcweir bool SvxTableController::StopTextEdit()
1758cdf0e10cSrcweir {
1759cdf0e10cSrcweir     if(mpView->IsTextEdit())
1760cdf0e10cSrcweir     {
1761cdf0e10cSrcweir         mpView->SdrEndTextEdit();
1762cdf0e10cSrcweir         mpView->SetCurrentObj(OBJ_TABLE);
1763cdf0e10cSrcweir         mpView->SetEditMode(SDREDITMODE_EDIT);
1764cdf0e10cSrcweir         return true;
1765cdf0e10cSrcweir     }
1766cdf0e10cSrcweir     else
1767cdf0e10cSrcweir     {
1768cdf0e10cSrcweir         return false;
1769cdf0e10cSrcweir     }
1770cdf0e10cSrcweir }
1771cdf0e10cSrcweir 
1772cdf0e10cSrcweir // --------------------------------------------------------------------
1773cdf0e10cSrcweir 
1774cdf0e10cSrcweir void SvxTableController::DeleteTable()
1775cdf0e10cSrcweir {
1776cdf0e10cSrcweir     //
1777cdf0e10cSrcweir }
1778cdf0e10cSrcweir 
1779cdf0e10cSrcweir // --------------------------------------------------------------------
1780cdf0e10cSrcweir 
1781cdf0e10cSrcweir void SvxTableController::getSelectedCells( CellPos& rFirst, CellPos& rLast )
1782cdf0e10cSrcweir {
1783cdf0e10cSrcweir     if( mbCellSelectionMode )
1784cdf0e10cSrcweir     {
1785cdf0e10cSrcweir         checkCell( maCursorFirstPos );
1786cdf0e10cSrcweir         checkCell( maCursorLastPos );
1787cdf0e10cSrcweir 
1788cdf0e10cSrcweir         rFirst.mnCol = std::min( maCursorFirstPos.mnCol, maCursorLastPos.mnCol );
1789cdf0e10cSrcweir         rFirst.mnRow = std::min( maCursorFirstPos.mnRow, maCursorLastPos.mnRow );
1790cdf0e10cSrcweir         rLast.mnCol = std::max( maCursorFirstPos.mnCol, maCursorLastPos.mnCol );
1791cdf0e10cSrcweir         rLast.mnRow = std::max( maCursorFirstPos.mnRow, maCursorLastPos.mnRow );
1792cdf0e10cSrcweir 
1793cdf0e10cSrcweir         bool bExt = false;
1794cdf0e10cSrcweir         if( mxTable.is() ) do
1795cdf0e10cSrcweir         {
1796cdf0e10cSrcweir             bExt = false;
1797cdf0e10cSrcweir             for( sal_Int32 nRow = rFirst.mnRow; nRow <= rLast.mnRow && !bExt; nRow++ )
1798cdf0e10cSrcweir             {
1799cdf0e10cSrcweir                 for( sal_Int32 nCol = rFirst.mnCol; nCol <= rLast.mnCol && !bExt; nCol++ )
1800cdf0e10cSrcweir                 {
1801cdf0e10cSrcweir                     Reference< XMergeableCell > xCell( mxTable->getCellByPosition( nCol, nRow ), UNO_QUERY );
1802cdf0e10cSrcweir                     if( !xCell.is() )
1803cdf0e10cSrcweir                         continue;
1804cdf0e10cSrcweir 
1805cdf0e10cSrcweir                     if( xCell->isMerged() )
1806cdf0e10cSrcweir                     {
1807cdf0e10cSrcweir                         CellPos aPos( nCol, nRow );
1808cdf0e10cSrcweir                         findMergeOrigin( aPos );
1809cdf0e10cSrcweir                         if( (aPos.mnCol < rFirst.mnCol) || (aPos.mnRow < rFirst.mnRow) )
1810cdf0e10cSrcweir                         {
1811cdf0e10cSrcweir                             rFirst.mnCol = std::min( rFirst.mnCol, aPos.mnCol );
1812cdf0e10cSrcweir                             rFirst.mnRow = std::min( rFirst.mnRow, aPos.mnRow );
1813cdf0e10cSrcweir                             bExt = true;
1814cdf0e10cSrcweir                         }
1815cdf0e10cSrcweir                     }
1816cdf0e10cSrcweir                     else
1817cdf0e10cSrcweir                     {
1818cdf0e10cSrcweir                         if( ((nCol + xCell->getColumnSpan() - 1) > rLast.mnCol) || (nRow + xCell->getRowSpan() - 1 ) > rLast.mnRow )
1819cdf0e10cSrcweir                         {
1820cdf0e10cSrcweir                             rLast.mnCol = std::max( rLast.mnCol, nCol + xCell->getColumnSpan() - 1 );
1821cdf0e10cSrcweir                             rLast.mnRow = std::max( rLast.mnRow, nRow + xCell->getRowSpan() - 1 );
1822cdf0e10cSrcweir                             bExt = true;
1823cdf0e10cSrcweir                         }
1824cdf0e10cSrcweir                     }
1825cdf0e10cSrcweir                 }
1826cdf0e10cSrcweir             }
1827cdf0e10cSrcweir         }
1828cdf0e10cSrcweir         while(bExt);
1829cdf0e10cSrcweir     }
1830cdf0e10cSrcweir     else if( mpView && mpView->IsTextEdit() )
1831cdf0e10cSrcweir     {
1832cdf0e10cSrcweir         rFirst = getSelectionStart();
1833cdf0e10cSrcweir         findMergeOrigin( rFirst );
1834cdf0e10cSrcweir         rLast = rFirst;
1835cdf0e10cSrcweir 
1836cdf0e10cSrcweir         if( mxTable.is() )
1837cdf0e10cSrcweir         {
1838cdf0e10cSrcweir             Reference< XMergeableCell > xCell( mxTable->getCellByPosition( rLast.mnCol, rLast.mnRow ), UNO_QUERY );
1839cdf0e10cSrcweir             if( xCell.is() )
1840cdf0e10cSrcweir             {
1841cdf0e10cSrcweir                 rLast.mnCol += xCell->getColumnSpan() - 1;
1842cdf0e10cSrcweir                 rLast.mnRow += xCell->getRowSpan() - 1;
1843cdf0e10cSrcweir             }
1844cdf0e10cSrcweir         }
1845cdf0e10cSrcweir     }
1846cdf0e10cSrcweir     else
1847cdf0e10cSrcweir     {
1848cdf0e10cSrcweir         rFirst.mnCol = 0;
1849cdf0e10cSrcweir         rFirst.mnRow = 0;
1850cdf0e10cSrcweir         if( mxTable.is() )
1851cdf0e10cSrcweir         {
1852cdf0e10cSrcweir             rLast.mnRow = mxTable->getRowCount()-1;
1853cdf0e10cSrcweir             rLast.mnCol = mxTable->getColumnCount()-1;
1854cdf0e10cSrcweir         }
1855cdf0e10cSrcweir         else
1856cdf0e10cSrcweir         {
1857cdf0e10cSrcweir             rLast.mnRow = 0;
1858cdf0e10cSrcweir             rLast.mnCol = 0;
1859cdf0e10cSrcweir         }
1860cdf0e10cSrcweir     }
1861cdf0e10cSrcweir }
1862cdf0e10cSrcweir 
1863cdf0e10cSrcweir // --------------------------------------------------------------------
1864cdf0e10cSrcweir 
1865cdf0e10cSrcweir void SvxTableController::StartSelection( const CellPos& rPos )
1866cdf0e10cSrcweir {
1867cdf0e10cSrcweir     StopTextEdit();
1868cdf0e10cSrcweir     mbCellSelectionMode = true;
1869cdf0e10cSrcweir     maCursorLastPos = maCursorFirstPos = rPos;
1870cdf0e10cSrcweir     mpView->MarkListHasChanged();
1871cdf0e10cSrcweir }
1872cdf0e10cSrcweir 
1873cdf0e10cSrcweir // --------------------------------------------------------------------
1874cdf0e10cSrcweir 
1875cdf0e10cSrcweir void SvxTableController::setSelectedCells( const CellPos& rStart, const CellPos& rEnd )
1876cdf0e10cSrcweir {
1877cdf0e10cSrcweir     StopTextEdit();
1878cdf0e10cSrcweir     mbCellSelectionMode = true;
1879cdf0e10cSrcweir     maCursorFirstPos = rStart;
1880cdf0e10cSrcweir     UpdateSelection( rEnd );
1881cdf0e10cSrcweir }
1882cdf0e10cSrcweir 
1883cdf0e10cSrcweir // --------------------------------------------------------------------
1884cdf0e10cSrcweir 
1885cdf0e10cSrcweir void SvxTableController::UpdateSelection( const CellPos& rPos )
1886cdf0e10cSrcweir {
1887cdf0e10cSrcweir     maCursorLastPos = rPos;
1888cdf0e10cSrcweir     mpView->MarkListHasChanged();
1889cdf0e10cSrcweir }
1890cdf0e10cSrcweir 
1891cdf0e10cSrcweir // --------------------------------------------------------------------
1892cdf0e10cSrcweir 
1893cdf0e10cSrcweir void SvxTableController::clearSelection()
1894cdf0e10cSrcweir {
1895cdf0e10cSrcweir     RemoveSelection();
1896cdf0e10cSrcweir }
1897cdf0e10cSrcweir 
1898cdf0e10cSrcweir // --------------------------------------------------------------------
1899cdf0e10cSrcweir 
1900cdf0e10cSrcweir void SvxTableController::selectAll()
1901cdf0e10cSrcweir {
1902cdf0e10cSrcweir     if( mxTable.is() )
1903cdf0e10cSrcweir     {
1904cdf0e10cSrcweir         CellPos aPos1, aPos2( mxTable->getColumnCount()-1, mxTable->getRowCount()-1 );
1905cdf0e10cSrcweir         if( (aPos2.mnCol >= 0) && (aPos2.mnRow >= 0) )
1906cdf0e10cSrcweir         {
1907cdf0e10cSrcweir             setSelectedCells( aPos1, aPos2 );
1908cdf0e10cSrcweir         }
1909cdf0e10cSrcweir     }
1910cdf0e10cSrcweir }
1911cdf0e10cSrcweir 
1912cdf0e10cSrcweir // --------------------------------------------------------------------
1913cdf0e10cSrcweir 
1914cdf0e10cSrcweir void SvxTableController::RemoveSelection()
1915cdf0e10cSrcweir {
1916cdf0e10cSrcweir     if( mbCellSelectionMode )
1917cdf0e10cSrcweir     {
1918cdf0e10cSrcweir         mbCellSelectionMode = false;
1919cdf0e10cSrcweir         mpView->MarkListHasChanged();
1920cdf0e10cSrcweir     }
1921cdf0e10cSrcweir }
1922cdf0e10cSrcweir 
1923cdf0e10cSrcweir // --------------------------------------------------------------------
1924cdf0e10cSrcweir 
1925cdf0e10cSrcweir void SvxTableController::onTableModified()
1926cdf0e10cSrcweir {
1927cdf0e10cSrcweir     if( mnUpdateEvent == 0 )
1928cdf0e10cSrcweir         mnUpdateEvent = Application::PostUserEvent( LINK( this, SvxTableController, UpdateHdl ) );
1929cdf0e10cSrcweir }
1930cdf0e10cSrcweir // --------------------------------------------------------------------
1931cdf0e10cSrcweir 
1932cdf0e10cSrcweir void SvxTableController::updateSelectionOverlay()
1933cdf0e10cSrcweir {
1934cdf0e10cSrcweir     destroySelectionOverlay();
1935cdf0e10cSrcweir     if( mbCellSelectionMode )
1936cdf0e10cSrcweir     {
1937cdf0e10cSrcweir         ::sdr::table::SdrTableObj* pTableObj = dynamic_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
1938cdf0e10cSrcweir         if( pTableObj )
1939cdf0e10cSrcweir         {
1940cdf0e10cSrcweir             sdr::overlay::OverlayObjectCell::RangeVector aRanges;
1941cdf0e10cSrcweir 
1942cdf0e10cSrcweir             Rectangle aRect;
1943cdf0e10cSrcweir             CellPos aStart,aEnd;
1944cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
1945cdf0e10cSrcweir             pTableObj->getCellBounds( aStart, aRect );
1946cdf0e10cSrcweir 
1947cdf0e10cSrcweir             basegfx::B2DRange a2DRange( basegfx::B2DPoint(aRect.Left(), aRect.Top()) );
1948cdf0e10cSrcweir             a2DRange.expand( basegfx::B2DPoint(aRect.Right(), aRect.Bottom()) );
1949cdf0e10cSrcweir 
1950cdf0e10cSrcweir             findMergeOrigin( aEnd );
1951cdf0e10cSrcweir             pTableObj->getCellBounds( aEnd, aRect );
1952cdf0e10cSrcweir             a2DRange.expand( basegfx::B2DPoint(aRect.Left(), aRect.Top()) );
1953cdf0e10cSrcweir             a2DRange.expand( basegfx::B2DPoint(aRect.Right(), aRect.Bottom()) );
1954cdf0e10cSrcweir             aRanges.push_back( a2DRange );
1955cdf0e10cSrcweir 
1956cdf0e10cSrcweir             ::Color aHighlight( COL_BLUE );
1957cdf0e10cSrcweir             OutputDevice* pOutDev = mpView->GetFirstOutputDevice();
1958cdf0e10cSrcweir             if( pOutDev )
1959cdf0e10cSrcweir                 aHighlight = pOutDev->GetSettings().GetStyleSettings().GetHighlightColor();
1960cdf0e10cSrcweir 
1961cdf0e10cSrcweir             const sal_uInt32 nCount = mpView->PaintWindowCount();
1962cdf0e10cSrcweir             for( sal_uInt32 nIndex = 0; nIndex < nCount; nIndex++ )
1963cdf0e10cSrcweir             {
1964cdf0e10cSrcweir                 SdrPaintWindow* pPaintWindow = mpView->GetPaintWindow(nIndex);
1965cdf0e10cSrcweir                 if( pPaintWindow )
1966cdf0e10cSrcweir                 {
1967cdf0e10cSrcweir                     ::sdr::overlay::OverlayManager* pOverlayManager = pPaintWindow->GetOverlayManager();
1968cdf0e10cSrcweir                     if( pOverlayManager )
1969cdf0e10cSrcweir                     {
1970cdf0e10cSrcweir                         // sdr::overlay::CellOverlayType eType = sdr::overlay::CELL_OVERLAY_INVERT;
1971cdf0e10cSrcweir                         sdr::overlay::CellOverlayType eType = sdr::overlay::CELL_OVERLAY_TRANSPARENT;
1972cdf0e10cSrcweir 
1973cdf0e10cSrcweir                         sdr::overlay::OverlayObjectCell* pOverlay = new sdr::overlay::OverlayObjectCell( eType, aHighlight, aRanges );
1974cdf0e10cSrcweir 
1975cdf0e10cSrcweir                         pOverlayManager->add(*pOverlay);
1976cdf0e10cSrcweir                         mpSelectionOverlay = new ::sdr::overlay::OverlayObjectList;
1977cdf0e10cSrcweir                         mpSelectionOverlay->append(*pOverlay);
1978cdf0e10cSrcweir                     }
1979cdf0e10cSrcweir                 }
1980cdf0e10cSrcweir             }
1981cdf0e10cSrcweir         }
1982cdf0e10cSrcweir     }
1983cdf0e10cSrcweir }
1984cdf0e10cSrcweir 
1985cdf0e10cSrcweir // --------------------------------------------------------------------
1986cdf0e10cSrcweir 
1987cdf0e10cSrcweir void SvxTableController::destroySelectionOverlay()
1988cdf0e10cSrcweir {
1989cdf0e10cSrcweir     if( mpSelectionOverlay )
1990cdf0e10cSrcweir     {
1991cdf0e10cSrcweir         delete mpSelectionOverlay;
1992cdf0e10cSrcweir         mpSelectionOverlay = 0;
1993cdf0e10cSrcweir     }
1994cdf0e10cSrcweir }
1995cdf0e10cSrcweir 
1996cdf0e10cSrcweir // --------------------------------------------------------------------
1997cdf0e10cSrcweir 
1998cdf0e10cSrcweir void SvxTableController::MergeAttrFromSelectedCells(SfxItemSet& rAttr, bool bOnlyHardAttr) const
1999cdf0e10cSrcweir {
2000cdf0e10cSrcweir     if( mxTable.is() )
2001cdf0e10cSrcweir     {
2002cdf0e10cSrcweir         CellPos aStart, aEnd;
2003cdf0e10cSrcweir         const_cast<SvxTableController&>(*this).getSelectedCells( aStart, aEnd );
2004cdf0e10cSrcweir 
2005cdf0e10cSrcweir         for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
2006cdf0e10cSrcweir         {
2007cdf0e10cSrcweir             for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
2008cdf0e10cSrcweir             {
2009cdf0e10cSrcweir                 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2010cdf0e10cSrcweir                 if( xCell.is() && !xCell->isMerged() )
2011cdf0e10cSrcweir                 {
2012cdf0e10cSrcweir                     const SfxItemSet& rSet = xCell->GetItemSet();
2013cdf0e10cSrcweir                     SfxWhichIter aIter(rSet);
2014cdf0e10cSrcweir                     sal_uInt16 nWhich(aIter.FirstWhich());
2015cdf0e10cSrcweir                     while(nWhich)
2016cdf0e10cSrcweir                     {
2017cdf0e10cSrcweir                         if(!bOnlyHardAttr)
2018cdf0e10cSrcweir                         {
2019cdf0e10cSrcweir                             if(SFX_ITEM_DONTCARE == rSet.GetItemState(nWhich, sal_False))
2020cdf0e10cSrcweir                                 rAttr.InvalidateItem(nWhich);
2021cdf0e10cSrcweir                             else
2022cdf0e10cSrcweir                                 rAttr.MergeValue(rSet.Get(nWhich), sal_True);
2023cdf0e10cSrcweir                         }
2024cdf0e10cSrcweir                         else if(SFX_ITEM_SET == rSet.GetItemState(nWhich, sal_False))
2025cdf0e10cSrcweir                         {
2026cdf0e10cSrcweir                             const SfxPoolItem& rItem = rSet.Get(nWhich);
2027cdf0e10cSrcweir                             rAttr.MergeValue(rItem, sal_True);
2028cdf0e10cSrcweir                         }
2029cdf0e10cSrcweir 
2030cdf0e10cSrcweir                         nWhich = aIter.NextWhich();
2031cdf0e10cSrcweir                     }
2032cdf0e10cSrcweir                 }
2033cdf0e10cSrcweir             }
2034cdf0e10cSrcweir         }
2035cdf0e10cSrcweir     }
2036cdf0e10cSrcweir 
2037cdf0e10cSrcweir     if( mpView->IsTextEdit() )
2038cdf0e10cSrcweir     {
2039cdf0e10cSrcweir     }
2040cdf0e10cSrcweir }
2041cdf0e10cSrcweir 
2042cdf0e10cSrcweir // --------------------------------------------------------------------
2043cdf0e10cSrcweir 
2044cdf0e10cSrcweir const sal_uInt16 CELL_BEFORE = 0x0001;
2045cdf0e10cSrcweir const sal_uInt16 CELL_LEFT   = 0x0002;
2046cdf0e10cSrcweir const sal_uInt16 CELL_RIGHT  = 0x0004;
2047cdf0e10cSrcweir const sal_uInt16 CELL_AFTER  = 0x0008;
2048cdf0e10cSrcweir 
2049cdf0e10cSrcweir const sal_uInt16 CELL_UPPER  = 0x0010;
2050cdf0e10cSrcweir const sal_uInt16 CELL_TOP    = 0x0020;
2051cdf0e10cSrcweir const sal_uInt16 CELL_BOTTOM = 0x0040;
2052cdf0e10cSrcweir const sal_uInt16 CELL_LOWER  = 0x0080;
2053cdf0e10cSrcweir 
2054cdf0e10cSrcweir // --------------------------------------------------------------------
2055cdf0e10cSrcweir 
2056cdf0e10cSrcweir static void ImplSetLinePreserveColor( SvxBoxItem& rNewFrame, const SvxBorderLine* pNew, sal_uInt16 nLine )
2057cdf0e10cSrcweir {
2058cdf0e10cSrcweir     if( pNew )
2059cdf0e10cSrcweir     {
2060cdf0e10cSrcweir         const SvxBorderLine* pOld = rNewFrame.GetLine(nLine);
2061cdf0e10cSrcweir         if( pOld )
2062cdf0e10cSrcweir         {
2063cdf0e10cSrcweir             SvxBorderLine aNewLine( *pNew );
2064cdf0e10cSrcweir             aNewLine.SetColor( pOld->GetColor() );
2065cdf0e10cSrcweir             rNewFrame.SetLine( &aNewLine, nLine );
2066cdf0e10cSrcweir             return;
2067cdf0e10cSrcweir         }
2068cdf0e10cSrcweir     }
2069cdf0e10cSrcweir     rNewFrame.SetLine( pNew, nLine );
2070cdf0e10cSrcweir }
2071cdf0e10cSrcweir 
2072cdf0e10cSrcweir // --------------------------------------------------------------------
2073cdf0e10cSrcweir 
2074cdf0e10cSrcweir static void ImplApplyBoxItem( sal_uInt16 nCellFlags, const SvxBoxItem* pBoxItem, const SvxBoxInfoItem* pBoxInfoItem, SvxBoxItem& rNewFrame )
2075cdf0e10cSrcweir {
2076cdf0e10cSrcweir     if( (nCellFlags & (CELL_BEFORE|CELL_AFTER|CELL_UPPER|CELL_LOWER)) != 0 )
2077cdf0e10cSrcweir     {
2078cdf0e10cSrcweir         // current cell is outside the selection
2079cdf0e10cSrcweir 
2080cdf0e10cSrcweir         if( (nCellFlags & ( CELL_BEFORE|CELL_AFTER)) == 0 ) // check if its not nw or ne corner
2081cdf0e10cSrcweir         {
2082cdf0e10cSrcweir             if( nCellFlags & CELL_UPPER )
2083cdf0e10cSrcweir             {
2084cdf0e10cSrcweir                 if( pBoxInfoItem->IsValid(VALID_TOP) )
2085cdf0e10cSrcweir                     rNewFrame.SetLine(0, BOX_LINE_BOTTOM );
2086cdf0e10cSrcweir             }
2087cdf0e10cSrcweir             else if( nCellFlags & CELL_LOWER )
2088cdf0e10cSrcweir             {
2089cdf0e10cSrcweir                 if( pBoxInfoItem->IsValid(VALID_BOTTOM) )
2090cdf0e10cSrcweir                     rNewFrame.SetLine( 0, BOX_LINE_TOP );
2091cdf0e10cSrcweir             }
2092cdf0e10cSrcweir         }
2093cdf0e10cSrcweir         else if( (nCellFlags & ( CELL_UPPER|CELL_LOWER)) == 0 ) // check if its not sw or se corner
2094cdf0e10cSrcweir         {
2095cdf0e10cSrcweir             if( nCellFlags & CELL_BEFORE )
2096cdf0e10cSrcweir             {
2097cdf0e10cSrcweir                 if( pBoxInfoItem->IsValid(VALID_LEFT) )
2098cdf0e10cSrcweir                     rNewFrame.SetLine( 0, BOX_LINE_RIGHT );
2099cdf0e10cSrcweir             }
2100cdf0e10cSrcweir             else if( nCellFlags & CELL_AFTER )
2101cdf0e10cSrcweir             {
2102cdf0e10cSrcweir                 if( pBoxInfoItem->IsValid(VALID_RIGHT) )
2103cdf0e10cSrcweir                     rNewFrame.SetLine( 0, BOX_LINE_LEFT );
2104cdf0e10cSrcweir             }
2105cdf0e10cSrcweir         }
2106cdf0e10cSrcweir     }
2107cdf0e10cSrcweir     else
2108cdf0e10cSrcweir     {
2109cdf0e10cSrcweir         // current cell is inside the selection
2110cdf0e10cSrcweir 
2111cdf0e10cSrcweir         if( (nCellFlags & CELL_LEFT) ? pBoxInfoItem->IsValid(VALID_LEFT) : pBoxInfoItem->IsValid(VALID_VERT) )
2112cdf0e10cSrcweir             rNewFrame.SetLine( (nCellFlags & CELL_LEFT) ? pBoxItem->GetLeft() : pBoxInfoItem->GetVert(), BOX_LINE_LEFT );
2113cdf0e10cSrcweir 
2114cdf0e10cSrcweir         if( (nCellFlags & CELL_RIGHT) ? pBoxInfoItem->IsValid(VALID_RIGHT) : pBoxInfoItem->IsValid(VALID_VERT) )
2115cdf0e10cSrcweir             rNewFrame.SetLine( (nCellFlags & CELL_RIGHT) ? pBoxItem->GetRight() : pBoxInfoItem->GetVert(), BOX_LINE_RIGHT );
2116cdf0e10cSrcweir 
2117cdf0e10cSrcweir         if( (nCellFlags & CELL_TOP) ? pBoxInfoItem->IsValid(VALID_TOP) : pBoxInfoItem->IsValid(VALID_HORI) )
2118cdf0e10cSrcweir             rNewFrame.SetLine( (nCellFlags & CELL_TOP) ? pBoxItem->GetTop() : pBoxInfoItem->GetHori(), BOX_LINE_TOP );
2119cdf0e10cSrcweir 
2120cdf0e10cSrcweir         if( (nCellFlags & CELL_BOTTOM) ? pBoxInfoItem->IsValid(VALID_BOTTOM) : pBoxInfoItem->IsValid(VALID_HORI) )
2121cdf0e10cSrcweir             rNewFrame.SetLine( (nCellFlags & CELL_BOTTOM) ? pBoxItem->GetBottom() : pBoxInfoItem->GetHori(), BOX_LINE_BOTTOM );
2122cdf0e10cSrcweir 
2123cdf0e10cSrcweir         // apply distance to borders
2124cdf0e10cSrcweir         if( pBoxInfoItem->IsValid( VALID_DISTANCE ) )
2125cdf0e10cSrcweir             for( sal_uInt16 nLine = 0; nLine < 4; ++nLine )
2126cdf0e10cSrcweir                 rNewFrame.SetDistance( pBoxItem->GetDistance( nLine ), nLine );
2127cdf0e10cSrcweir     }
2128cdf0e10cSrcweir }
2129cdf0e10cSrcweir 
2130cdf0e10cSrcweir // --------------------------------------------------------------------
2131cdf0e10cSrcweir 
2132cdf0e10cSrcweir static void ImplSetLineColor( SvxBoxItem& rNewFrame, sal_uInt16 nLine, const Color& rColor )
2133cdf0e10cSrcweir {
2134cdf0e10cSrcweir     const SvxBorderLine* pSourceLine = rNewFrame.GetLine( nLine );
2135cdf0e10cSrcweir     if( pSourceLine )
2136cdf0e10cSrcweir     {
2137cdf0e10cSrcweir         SvxBorderLine aLine( *pSourceLine );
2138cdf0e10cSrcweir         aLine.SetColor( rColor );
2139cdf0e10cSrcweir         rNewFrame.SetLine( &aLine, nLine );
2140cdf0e10cSrcweir     }
2141cdf0e10cSrcweir }
2142cdf0e10cSrcweir 
2143cdf0e10cSrcweir // --------------------------------------------------------------------
2144cdf0e10cSrcweir 
2145cdf0e10cSrcweir static void ImplApplyLineColorItem( sal_uInt16 nCellFlags, const SvxColorItem* pLineColorItem, SvxBoxItem& rNewFrame )
2146cdf0e10cSrcweir {
2147cdf0e10cSrcweir     const Color aColor( pLineColorItem->GetValue() );
2148cdf0e10cSrcweir 
2149cdf0e10cSrcweir     if( (nCellFlags & (CELL_LOWER|CELL_BEFORE|CELL_AFTER)) == 0 )
2150cdf0e10cSrcweir         ImplSetLineColor( rNewFrame, BOX_LINE_BOTTOM, aColor );
2151cdf0e10cSrcweir 
2152cdf0e10cSrcweir     if( (nCellFlags & (CELL_UPPER|CELL_BEFORE|CELL_AFTER)) == 0 )
2153cdf0e10cSrcweir         ImplSetLineColor( rNewFrame, BOX_LINE_TOP, aColor );
2154cdf0e10cSrcweir 
2155cdf0e10cSrcweir     if( (nCellFlags & (CELL_UPPER|CELL_LOWER|CELL_AFTER)) == 0 )
2156cdf0e10cSrcweir         ImplSetLineColor( rNewFrame, BOX_LINE_RIGHT, aColor );
2157cdf0e10cSrcweir 
2158cdf0e10cSrcweir     if( (nCellFlags & (CELL_UPPER|CELL_LOWER|CELL_BEFORE)) == 0 )
2159cdf0e10cSrcweir         ImplSetLineColor( rNewFrame, BOX_LINE_LEFT, aColor );
2160cdf0e10cSrcweir }
2161cdf0e10cSrcweir 
2162cdf0e10cSrcweir // --------------------------------------------------------------------
2163cdf0e10cSrcweir 
2164cdf0e10cSrcweir static void ImplApplyBorderLineItem( sal_uInt16 nCellFlags, const SvxBorderLine* pBorderLineItem, SvxBoxItem& rNewFrame )
2165cdf0e10cSrcweir {
2166cdf0e10cSrcweir     if( (nCellFlags & ( CELL_BEFORE|CELL_AFTER|CELL_UPPER|CELL_LOWER)) != 0 )
2167cdf0e10cSrcweir     {
2168cdf0e10cSrcweir         if( (nCellFlags & ( CELL_BEFORE|CELL_AFTER)) == 0 ) // check if its not nw or ne corner
2169cdf0e10cSrcweir         {
2170cdf0e10cSrcweir             if( nCellFlags & CELL_UPPER )
2171cdf0e10cSrcweir             {
2172cdf0e10cSrcweir                 if( rNewFrame.GetBottom() )
2173cdf0e10cSrcweir                     ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_BOTTOM );
2174cdf0e10cSrcweir             }
2175cdf0e10cSrcweir             else if( nCellFlags & CELL_LOWER )
2176cdf0e10cSrcweir             {
2177cdf0e10cSrcweir                 if( rNewFrame.GetTop() )
2178cdf0e10cSrcweir                     ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_TOP );
2179cdf0e10cSrcweir             }
2180cdf0e10cSrcweir         }
2181cdf0e10cSrcweir         else if( (nCellFlags & ( CELL_UPPER|CELL_LOWER)) == 0 ) // check if its not sw or se corner
2182cdf0e10cSrcweir         {
2183cdf0e10cSrcweir             if( nCellFlags & CELL_BEFORE )
2184cdf0e10cSrcweir             {
2185cdf0e10cSrcweir                 if( rNewFrame.GetRight() )
2186cdf0e10cSrcweir                     ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_RIGHT );
2187cdf0e10cSrcweir             }
2188cdf0e10cSrcweir             else if( nCellFlags & CELL_AFTER )
2189cdf0e10cSrcweir             {
2190cdf0e10cSrcweir                 if( rNewFrame.GetLeft() )
2191cdf0e10cSrcweir                     ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_LEFT );
2192cdf0e10cSrcweir             }
2193cdf0e10cSrcweir         }
2194cdf0e10cSrcweir     }
2195cdf0e10cSrcweir     else
2196cdf0e10cSrcweir     {
2197cdf0e10cSrcweir         if( rNewFrame.GetBottom() )
2198cdf0e10cSrcweir             ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_BOTTOM );
2199cdf0e10cSrcweir         if( rNewFrame.GetTop() )
2200cdf0e10cSrcweir             ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_TOP );
2201cdf0e10cSrcweir         if( rNewFrame.GetRight() )
2202cdf0e10cSrcweir             ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_RIGHT );
2203cdf0e10cSrcweir         if( rNewFrame.GetLeft() )
2204cdf0e10cSrcweir             ImplSetLinePreserveColor( rNewFrame, pBorderLineItem, BOX_LINE_LEFT );
2205cdf0e10cSrcweir     }
2206cdf0e10cSrcweir }
2207cdf0e10cSrcweir 
2208cdf0e10cSrcweir // --------------------------------------------------------------------
2209cdf0e10cSrcweir 
2210cdf0e10cSrcweir void SvxTableController::ApplyBorderAttr( const SfxItemSet& rAttr )
2211cdf0e10cSrcweir {
2212cdf0e10cSrcweir     if( mxTable.is() )
2213cdf0e10cSrcweir     {
2214cdf0e10cSrcweir         const sal_Int32 nRowCount = mxTable->getRowCount();
2215cdf0e10cSrcweir         const sal_Int32 nColCount = mxTable->getColumnCount();
2216cdf0e10cSrcweir         if( nRowCount && nColCount )
2217cdf0e10cSrcweir         {
2218cdf0e10cSrcweir             const SvxBoxItem* pBoxItem = 0;
2219cdf0e10cSrcweir             if(SFX_ITEM_SET == rAttr.GetItemState(SDRATTR_TABLE_BORDER, sal_False) )
2220cdf0e10cSrcweir                 pBoxItem = dynamic_cast< const SvxBoxItem* >( &rAttr.Get( SDRATTR_TABLE_BORDER ) );
2221cdf0e10cSrcweir 
2222cdf0e10cSrcweir             const SvxBoxInfoItem* pBoxInfoItem = 0;
2223cdf0e10cSrcweir             if(SFX_ITEM_SET == rAttr.GetItemState(SDRATTR_TABLE_BORDER_INNER, sal_False) )
2224cdf0e10cSrcweir                 pBoxInfoItem = dynamic_cast< const SvxBoxInfoItem* >( &rAttr.Get( SDRATTR_TABLE_BORDER_INNER ) );
2225cdf0e10cSrcweir 
2226cdf0e10cSrcweir             const SvxColorItem* pLineColorItem = 0;
2227cdf0e10cSrcweir             if(SFX_ITEM_SET == rAttr.GetItemState(SID_FRAME_LINECOLOR, sal_False) )
2228cdf0e10cSrcweir                 pLineColorItem = dynamic_cast< const SvxColorItem* >( &rAttr.Get( SID_FRAME_LINECOLOR ) );
2229cdf0e10cSrcweir 
2230cdf0e10cSrcweir             const SvxBorderLine* pBorderLineItem = 0;
2231cdf0e10cSrcweir             if(SFX_ITEM_SET == rAttr.GetItemState(SID_FRAME_LINESTYLE, sal_False) )
2232cdf0e10cSrcweir                 pBorderLineItem = ((const SvxLineItem&)rAttr.Get( SID_FRAME_LINESTYLE )).GetLine();
2233cdf0e10cSrcweir 
2234cdf0e10cSrcweir             if( pBoxInfoItem && !pBoxItem )
2235cdf0e10cSrcweir             {
2236cdf0e10cSrcweir                 const static SvxBoxItem gaEmptyBoxItem( SDRATTR_TABLE_BORDER );
2237cdf0e10cSrcweir                 pBoxItem = &gaEmptyBoxItem;
2238cdf0e10cSrcweir             }
2239cdf0e10cSrcweir             else if( pBoxItem && !pBoxInfoItem )
2240cdf0e10cSrcweir             {
2241cdf0e10cSrcweir                 const static SvxBoxInfoItem gaEmptyBoxInfoItem( SDRATTR_TABLE_BORDER_INNER );
2242cdf0e10cSrcweir                 pBoxInfoItem = &gaEmptyBoxInfoItem;
2243cdf0e10cSrcweir             }
2244cdf0e10cSrcweir 
2245cdf0e10cSrcweir             CellPos aStart, aEnd;
2246cdf0e10cSrcweir             getSelectedCells( aStart, aEnd );
2247cdf0e10cSrcweir 
2248cdf0e10cSrcweir             const sal_Int32 nLastRow = std::min( aEnd.mnRow + 2, nRowCount );
2249cdf0e10cSrcweir             const sal_Int32 nLastCol = std::min( aEnd.mnCol + 2, nColCount );
2250cdf0e10cSrcweir 
2251cdf0e10cSrcweir             for( sal_Int32 nRow = std::max( aStart.mnRow - 1, (sal_Int32)0 ); nRow < nLastRow; nRow++ )
2252cdf0e10cSrcweir             {
2253cdf0e10cSrcweir                 sal_uInt16 nRowFlags = 0;
2254cdf0e10cSrcweir                 nRowFlags |= (nRow == aStart.mnRow) ? CELL_TOP : 0;
2255cdf0e10cSrcweir                 nRowFlags |= (nRow == aEnd.mnRow)   ? CELL_BOTTOM : 0;
2256cdf0e10cSrcweir                 nRowFlags |= (nRow < aStart.mnRow)  ? CELL_UPPER : 0;
2257cdf0e10cSrcweir                 nRowFlags |= (nRow > aEnd.mnRow)    ? CELL_LOWER : 0;
2258cdf0e10cSrcweir 
2259cdf0e10cSrcweir                 for( sal_Int32 nCol = std::max( aStart.mnCol - 1, (sal_Int32)0 ); nCol < nLastCol; nCol++ )
2260cdf0e10cSrcweir                 {
2261cdf0e10cSrcweir                     CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2262cdf0e10cSrcweir                     if( !xCell.is() )
2263cdf0e10cSrcweir                         continue;
2264cdf0e10cSrcweir 
2265cdf0e10cSrcweir                     const SfxItemSet& rSet = xCell->GetItemSet();
2266cdf0e10cSrcweir                     const SvxBoxItem* pOldOuter = (const SvxBoxItem*)     &rSet.Get( SDRATTR_TABLE_BORDER );
2267cdf0e10cSrcweir 
2268cdf0e10cSrcweir                     SvxBoxItem aNewFrame( *pOldOuter );
2269cdf0e10cSrcweir 
2270cdf0e10cSrcweir                     sal_uInt16 nCellFlags = nRowFlags;
2271cdf0e10cSrcweir                     nCellFlags |= (nCol == aStart.mnCol) ? CELL_LEFT : 0;
2272cdf0e10cSrcweir                     nCellFlags |= (nCol == aEnd.mnCol)   ? CELL_RIGHT : 0;
2273cdf0e10cSrcweir                     nCellFlags |= (nCol < aStart.mnCol)  ? CELL_BEFORE : 0;
2274cdf0e10cSrcweir                     nCellFlags |= (nCol > aEnd.mnCol)    ? CELL_AFTER : 0;
2275cdf0e10cSrcweir 
2276cdf0e10cSrcweir                     if( pBoxItem && pBoxInfoItem )
2277cdf0e10cSrcweir                         ImplApplyBoxItem( nCellFlags, pBoxItem, pBoxInfoItem, aNewFrame );
2278cdf0e10cSrcweir 
2279cdf0e10cSrcweir                     if( pLineColorItem )
2280cdf0e10cSrcweir                         ImplApplyLineColorItem( nCellFlags, pLineColorItem, aNewFrame );
2281cdf0e10cSrcweir 
2282cdf0e10cSrcweir                     if( pBorderLineItem )
2283cdf0e10cSrcweir                         ImplApplyBorderLineItem( nCellFlags, pBorderLineItem, aNewFrame );
2284cdf0e10cSrcweir 
2285cdf0e10cSrcweir                     if (aNewFrame != *pOldOuter)
2286cdf0e10cSrcweir                     {
2287cdf0e10cSrcweir                         SfxItemSet aAttr(*rSet.GetPool(), rSet.GetRanges());
2288cdf0e10cSrcweir                         aAttr.Put(aNewFrame);
2289cdf0e10cSrcweir                         xCell->SetMergedItemSetAndBroadcast( aAttr, false );
2290cdf0e10cSrcweir                     }
2291cdf0e10cSrcweir                 }
2292cdf0e10cSrcweir             }
2293cdf0e10cSrcweir         }
2294cdf0e10cSrcweir     }
2295cdf0e10cSrcweir }
2296cdf0e10cSrcweir 
2297cdf0e10cSrcweir // --------------------------------------------------------------------
2298cdf0e10cSrcweir 
2299cdf0e10cSrcweir void SvxTableController::UpdateTableShape()
2300cdf0e10cSrcweir {
2301cdf0e10cSrcweir     SdrObject* pTableObj = mxTableObj.get();
2302cdf0e10cSrcweir     if( pTableObj )
2303cdf0e10cSrcweir     {
2304cdf0e10cSrcweir         pTableObj->ActionChanged();
2305cdf0e10cSrcweir         pTableObj->BroadcastObjectChange();
2306cdf0e10cSrcweir     }
2307cdf0e10cSrcweir     updateSelectionOverlay();
2308cdf0e10cSrcweir }
2309cdf0e10cSrcweir 
2310cdf0e10cSrcweir 
2311cdf0e10cSrcweir // --------------------------------------------------------------------
2312cdf0e10cSrcweir 
2313cdf0e10cSrcweir void SvxTableController::SetAttrToSelectedCells(const SfxItemSet& rAttr, bool bReplaceAll)
2314cdf0e10cSrcweir {
2315cdf0e10cSrcweir     if( mxTable.is() )
2316cdf0e10cSrcweir     {
2317cdf0e10cSrcweir         const bool bUndo = mpModel && mpModel->IsUndoEnabled();
2318cdf0e10cSrcweir 
2319cdf0e10cSrcweir         if( bUndo )
2320cdf0e10cSrcweir             mpModel->BegUndo( ImpGetResStr(STR_TABLE_NUMFORMAT) );
2321cdf0e10cSrcweir 
2322cdf0e10cSrcweir         CellPos aStart, aEnd;
2323cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
2324cdf0e10cSrcweir 
2325cdf0e10cSrcweir         SfxItemSet aAttr(*rAttr.GetPool(), rAttr.GetRanges());
2326cdf0e10cSrcweir         aAttr.Put(rAttr, sal_True);
2327cdf0e10cSrcweir 
2328cdf0e10cSrcweir         const bool bFrame = (rAttr.GetItemState( SDRATTR_TABLE_BORDER ) == SFX_ITEM_SET) || (rAttr.GetItemState( SDRATTR_TABLE_BORDER_INNER ) == SFX_ITEM_SET);
2329cdf0e10cSrcweir 
2330cdf0e10cSrcweir         if( bFrame )
2331cdf0e10cSrcweir         {
2332cdf0e10cSrcweir             aAttr.ClearItem( SDRATTR_TABLE_BORDER );
2333cdf0e10cSrcweir             aAttr.ClearItem( SDRATTR_TABLE_BORDER_INNER );
2334cdf0e10cSrcweir         }
2335cdf0e10cSrcweir 
2336cdf0e10cSrcweir         for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
2337cdf0e10cSrcweir         {
2338cdf0e10cSrcweir             for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
2339cdf0e10cSrcweir             {
2340cdf0e10cSrcweir                 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2341cdf0e10cSrcweir                 if( xCell.is() )
2342cdf0e10cSrcweir                 {
2343cdf0e10cSrcweir                     if( bUndo )
2344cdf0e10cSrcweir                         xCell->AddUndo();
2345cdf0e10cSrcweir                     xCell->SetMergedItemSetAndBroadcast(aAttr, bReplaceAll);
2346cdf0e10cSrcweir                 }
2347cdf0e10cSrcweir             }
2348cdf0e10cSrcweir         }
2349cdf0e10cSrcweir 
2350cdf0e10cSrcweir         if( bFrame )
2351cdf0e10cSrcweir         {
2352cdf0e10cSrcweir             ApplyBorderAttr( rAttr );
2353cdf0e10cSrcweir         }
2354cdf0e10cSrcweir 
2355cdf0e10cSrcweir         UpdateTableShape();
2356cdf0e10cSrcweir 
2357cdf0e10cSrcweir         if( bUndo )
2358cdf0e10cSrcweir             mpModel->EndUndo();
2359cdf0e10cSrcweir 
2360cdf0e10cSrcweir     }
2361cdf0e10cSrcweir }
2362cdf0e10cSrcweir 
2363cdf0e10cSrcweir // --------------------------------------------------------------------
2364cdf0e10cSrcweir 
2365cdf0e10cSrcweir bool SvxTableController::GetAttributes(SfxItemSet& rTargetSet, bool bOnlyHardAttr) const
2366cdf0e10cSrcweir {
2367cdf0e10cSrcweir     if( mxTableObj.is() && hasSelectedCells() )
2368cdf0e10cSrcweir     {
2369cdf0e10cSrcweir         MergeAttrFromSelectedCells( rTargetSet, bOnlyHardAttr );
2370cdf0e10cSrcweir 
2371cdf0e10cSrcweir         if( mpView->IsTextEdit() )
2372cdf0e10cSrcweir         {
2373cdf0e10cSrcweir             if( mxTableObj->GetOutlinerParaObject() )
2374cdf0e10cSrcweir                 rTargetSet.Put( SvxScriptTypeItem( mxTableObj->GetOutlinerParaObject()->GetTextObject().GetScriptType() ) );
2375cdf0e10cSrcweir 
2376cdf0e10cSrcweir             OutlinerView* pTextEditOutlinerView = mpView->GetTextEditOutlinerView();
2377cdf0e10cSrcweir             if(pTextEditOutlinerView)
2378cdf0e10cSrcweir             {
2379cdf0e10cSrcweir                 // FALSE= InvalidItems nicht al Default, sondern als "Loecher" betrachten
2380cdf0e10cSrcweir                 rTargetSet.Put(pTextEditOutlinerView->GetAttribs(), sal_False);
2381cdf0e10cSrcweir                 rTargetSet.Put( SvxScriptTypeItem( pTextEditOutlinerView->GetSelectedScriptType() ), sal_False );
2382cdf0e10cSrcweir             }
2383cdf0e10cSrcweir         }
2384cdf0e10cSrcweir 
2385cdf0e10cSrcweir         return true;
2386cdf0e10cSrcweir     }
2387cdf0e10cSrcweir     else
2388cdf0e10cSrcweir     {
2389cdf0e10cSrcweir         return false;
2390cdf0e10cSrcweir     }
2391cdf0e10cSrcweir }
2392cdf0e10cSrcweir 
2393cdf0e10cSrcweir // --------------------------------------------------------------------
2394cdf0e10cSrcweir 
2395cdf0e10cSrcweir bool SvxTableController::SetAttributes(const SfxItemSet& rSet, bool bReplaceAll)
2396cdf0e10cSrcweir {
2397cdf0e10cSrcweir     if( mbCellSelectionMode || mpView->IsTextEdit()  )
2398cdf0e10cSrcweir     {
2399cdf0e10cSrcweir         SetAttrToSelectedCells( rSet, bReplaceAll );
2400cdf0e10cSrcweir         return true;
2401cdf0e10cSrcweir     }
2402cdf0e10cSrcweir     return false;
2403cdf0e10cSrcweir }
2404cdf0e10cSrcweir 
2405cdf0e10cSrcweir // --------------------------------------------------------------------
2406cdf0e10cSrcweir 
2407cdf0e10cSrcweir bool SvxTableController::GetMarkedObjModel( SdrPage* pNewPage )
2408cdf0e10cSrcweir {
2409cdf0e10cSrcweir     if( mxTableObj.is() && mbCellSelectionMode && pNewPage ) try
2410cdf0e10cSrcweir     {
2411cdf0e10cSrcweir         ::sdr::table::SdrTableObj& rTableObj = *static_cast< ::sdr::table::SdrTableObj* >( mxTableObj.get() );
2412cdf0e10cSrcweir 
2413cdf0e10cSrcweir         CellPos aStart, aEnd;
2414cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
2415cdf0e10cSrcweir 
2416cdf0e10cSrcweir         SdrTableObj* pNewTableObj = rTableObj.CloneRange( aStart, aEnd );
2417cdf0e10cSrcweir 
2418cdf0e10cSrcweir         pNewTableObj->SetPage( pNewPage );
2419cdf0e10cSrcweir         pNewTableObj->SetModel( pNewPage->GetModel() );
2420cdf0e10cSrcweir 
2421cdf0e10cSrcweir         SdrInsertReason aReason(SDRREASON_VIEWCALL);
2422cdf0e10cSrcweir         pNewPage->InsertObject(pNewTableObj,CONTAINER_APPEND,&aReason);
2423cdf0e10cSrcweir 
2424cdf0e10cSrcweir         return true;
2425cdf0e10cSrcweir     }
2426cdf0e10cSrcweir     catch( Exception& )
2427cdf0e10cSrcweir     {
2428cdf0e10cSrcweir         DBG_ERROR( "svx::SvxTableController::GetMarkedObjModel(), exception caught!" );
2429cdf0e10cSrcweir     }
2430cdf0e10cSrcweir     return false;
2431cdf0e10cSrcweir }
2432cdf0e10cSrcweir 
2433cdf0e10cSrcweir // --------------------------------------------------------------------
2434cdf0e10cSrcweir 
2435cdf0e10cSrcweir bool SvxTableController::PasteObjModel( const SdrModel& rModel )
2436cdf0e10cSrcweir {
2437cdf0e10cSrcweir     if( mxTableObj.is() && mpView && (rModel.GetPageCount() >= 1) )
2438cdf0e10cSrcweir     {
2439cdf0e10cSrcweir         const SdrPage* pPastePage = rModel.GetPage(0);
2440cdf0e10cSrcweir         if( pPastePage && pPastePage->GetObjCount() == 1 )
2441cdf0e10cSrcweir         {
2442cdf0e10cSrcweir             SdrTableObj* pPasteTableObj = dynamic_cast< SdrTableObj* >( pPastePage->GetObj(0) );
2443cdf0e10cSrcweir             if( pPasteTableObj )
2444cdf0e10cSrcweir             {
2445cdf0e10cSrcweir                 return PasteObject( pPasteTableObj );
2446cdf0e10cSrcweir             }
2447cdf0e10cSrcweir         }
2448cdf0e10cSrcweir     }
2449cdf0e10cSrcweir 
2450cdf0e10cSrcweir     return false;
2451cdf0e10cSrcweir }
2452cdf0e10cSrcweir 
2453cdf0e10cSrcweir // --------------------------------------------------------------------
2454cdf0e10cSrcweir 
2455cdf0e10cSrcweir bool SvxTableController::PasteObject( SdrTableObj* pPasteTableObj )
2456cdf0e10cSrcweir {
2457cdf0e10cSrcweir     if( !pPasteTableObj )
2458cdf0e10cSrcweir         return false;
2459cdf0e10cSrcweir 
2460cdf0e10cSrcweir     Reference< XTable > xPasteTable( pPasteTableObj->getTable() );
2461cdf0e10cSrcweir     if( !xPasteTable.is() )
2462cdf0e10cSrcweir         return false;
2463cdf0e10cSrcweir 
2464cdf0e10cSrcweir     if( !mxTable.is() )
2465cdf0e10cSrcweir         return false;
2466cdf0e10cSrcweir 
2467cdf0e10cSrcweir     sal_Int32 nPasteColumns = xPasteTable->getColumnCount();
2468cdf0e10cSrcweir     sal_Int32 nPasteRows = xPasteTable->getRowCount();
2469cdf0e10cSrcweir 
2470cdf0e10cSrcweir     CellPos aStart, aEnd;
2471cdf0e10cSrcweir     getSelectedCells( aStart, aEnd );
2472cdf0e10cSrcweir 
2473cdf0e10cSrcweir     if( mpView->IsTextEdit() )
2474cdf0e10cSrcweir         mpView->SdrEndTextEdit(sal_True);
2475cdf0e10cSrcweir 
2476cdf0e10cSrcweir     sal_Int32 nColumns = mxTable->getColumnCount();
2477cdf0e10cSrcweir     sal_Int32 nRows = mxTable->getRowCount();
2478cdf0e10cSrcweir 
2479cdf0e10cSrcweir     const sal_Int32 nMissing = nPasteRows - ( nRows - aStart.mnRow );
2480cdf0e10cSrcweir     if( nMissing > 0 )
2481cdf0e10cSrcweir     {
2482cdf0e10cSrcweir         Reference< XTableRows > xRows( mxTable->getRows() );
2483cdf0e10cSrcweir         xRows->insertByIndex( nRows, nMissing );
2484cdf0e10cSrcweir         nRows = mxTable->getRowCount();
2485cdf0e10cSrcweir     }
2486cdf0e10cSrcweir 
2487cdf0e10cSrcweir     nPasteRows = std::min( nPasteRows, nRows - aStart.mnRow );
2488cdf0e10cSrcweir     nPasteColumns = std::min( nPasteColumns, nColumns - aStart.mnCol );
2489cdf0e10cSrcweir 
2490cdf0e10cSrcweir     // copy cell contents
2491cdf0e10cSrcweir     for( sal_Int32 nRow = 0; nRow < nPasteRows; ++nRow )
2492cdf0e10cSrcweir     {
2493cdf0e10cSrcweir         for( sal_Int32 nCol = 0; nCol < nPasteColumns; ++nCol )
2494cdf0e10cSrcweir         {
2495cdf0e10cSrcweir             CellRef xTargetCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( aStart.mnCol + nCol, aStart.mnRow + nRow ).get() ) );
2496cdf0e10cSrcweir             if( xTargetCell.is() && !xTargetCell->isMerged() )
2497cdf0e10cSrcweir             {
2498cdf0e10cSrcweir                 xTargetCell->AddUndo();
2499cdf0e10cSrcweir                 xTargetCell->cloneFrom( dynamic_cast< Cell* >( xPasteTable->getCellByPosition( nCol, nRow ).get() ) );
2500cdf0e10cSrcweir                 nCol += xTargetCell->getColumnSpan() - 1;
2501cdf0e10cSrcweir             }
2502cdf0e10cSrcweir         }
2503cdf0e10cSrcweir     }
2504cdf0e10cSrcweir 
2505cdf0e10cSrcweir     UpdateTableShape();
2506cdf0e10cSrcweir 
2507cdf0e10cSrcweir     return true;
2508cdf0e10cSrcweir }
2509cdf0e10cSrcweir 
2510cdf0e10cSrcweir bool SvxTableController::TakeFormatPaintBrush( boost::shared_ptr< SfxItemSet >& /*rFormatSet*/  )
2511cdf0e10cSrcweir {
2512cdf0e10cSrcweir     // SdrView::TakeFormatPaintBrush() is enough
2513cdf0e10cSrcweir     return false;
2514cdf0e10cSrcweir }
2515cdf0e10cSrcweir 
2516cdf0e10cSrcweir bool SvxTableController::ApplyFormatPaintBrush( SfxItemSet& rFormatSet, bool bNoCharacterFormats, bool bNoParagraphFormats )
2517cdf0e10cSrcweir {
2518cdf0e10cSrcweir     if( mbCellSelectionMode )
2519cdf0e10cSrcweir     {
2520cdf0e10cSrcweir         SdrTextObj* pTableObj = dynamic_cast<SdrTextObj*>( mxTableObj.get() );
2521cdf0e10cSrcweir         if( !pTableObj )
2522cdf0e10cSrcweir             return false;
2523cdf0e10cSrcweir 
2524cdf0e10cSrcweir         const bool bUndo = mpModel && mpModel->IsUndoEnabled();
2525cdf0e10cSrcweir 
2526cdf0e10cSrcweir         if( bUndo )
2527cdf0e10cSrcweir             mpModel->BegUndo( ImpGetResStr(STR_TABLE_NUMFORMAT) );
2528cdf0e10cSrcweir 
2529cdf0e10cSrcweir         CellPos aStart, aEnd;
2530cdf0e10cSrcweir         getSelectedCells( aStart, aEnd );
2531cdf0e10cSrcweir 
2532cdf0e10cSrcweir         SfxItemSet aAttr(*rFormatSet.GetPool(), rFormatSet.GetRanges());
2533cdf0e10cSrcweir         aAttr.Put(rFormatSet, sal_True);
2534cdf0e10cSrcweir 
2535cdf0e10cSrcweir         const bool bFrame = (rFormatSet.GetItemState( SDRATTR_TABLE_BORDER ) == SFX_ITEM_SET) || (rFormatSet.GetItemState( SDRATTR_TABLE_BORDER_INNER ) == SFX_ITEM_SET);
2536cdf0e10cSrcweir 
2537cdf0e10cSrcweir         if( bFrame )
2538cdf0e10cSrcweir         {
2539cdf0e10cSrcweir             aAttr.ClearItem( SDRATTR_TABLE_BORDER );
2540cdf0e10cSrcweir             aAttr.ClearItem( SDRATTR_TABLE_BORDER_INNER );
2541cdf0e10cSrcweir         }
2542cdf0e10cSrcweir 
2543cdf0e10cSrcweir         const sal_uInt16* pRanges = rFormatSet.GetRanges();
2544cdf0e10cSrcweir         bool bTextOnly = true;
2545cdf0e10cSrcweir 
2546cdf0e10cSrcweir         while( *pRanges )
2547cdf0e10cSrcweir         {
2548cdf0e10cSrcweir             if( (*pRanges != EE_PARA_START) && (*pRanges != EE_CHAR_START) )
2549cdf0e10cSrcweir             {
2550cdf0e10cSrcweir                 bTextOnly = true;
2551cdf0e10cSrcweir                 break;
2552cdf0e10cSrcweir             }
2553cdf0e10cSrcweir             pRanges += 2;
2554cdf0e10cSrcweir         }
2555cdf0e10cSrcweir 
2556cdf0e10cSrcweir         const bool bReplaceAll = false;
2557cdf0e10cSrcweir         for( sal_Int32 nRow = aStart.mnRow; nRow <= aEnd.mnRow; nRow++ )
2558cdf0e10cSrcweir         {
2559cdf0e10cSrcweir             for( sal_Int32 nCol = aStart.mnCol; nCol <= aEnd.mnCol; nCol++ )
2560cdf0e10cSrcweir             {
2561cdf0e10cSrcweir                 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
2562cdf0e10cSrcweir                 if( xCell.is() )
2563cdf0e10cSrcweir                 {
2564cdf0e10cSrcweir                     if( bUndo )
2565cdf0e10cSrcweir                         xCell->AddUndo();
2566cdf0e10cSrcweir                     if( !bTextOnly )
2567cdf0e10cSrcweir                         xCell->SetMergedItemSetAndBroadcast(aAttr, bReplaceAll);
2568cdf0e10cSrcweir 
2569cdf0e10cSrcweir                     SdrText* pText = static_cast< SdrText* >( xCell.get() );
2570cdf0e10cSrcweir                     mpView->ApplyFormatPaintBrushToText( rFormatSet, *pTableObj, pText, bNoCharacterFormats, bNoParagraphFormats );
2571cdf0e10cSrcweir                 }
2572cdf0e10cSrcweir             }
2573cdf0e10cSrcweir         }
2574cdf0e10cSrcweir 
2575cdf0e10cSrcweir         if( bFrame )
2576cdf0e10cSrcweir         {
2577cdf0e10cSrcweir             ApplyBorderAttr( rFormatSet );
2578cdf0e10cSrcweir         }
2579cdf0e10cSrcweir 
2580cdf0e10cSrcweir         UpdateTableShape();
2581cdf0e10cSrcweir 
2582cdf0e10cSrcweir         if( bUndo )
2583cdf0e10cSrcweir             mpModel->EndUndo();
2584cdf0e10cSrcweir 
2585cdf0e10cSrcweir         return true;
2586cdf0e10cSrcweir 
2587cdf0e10cSrcweir     }
2588cdf0e10cSrcweir     return false;
2589cdf0e10cSrcweir }
2590cdf0e10cSrcweir 
2591cdf0e10cSrcweir 
2592cdf0e10cSrcweir // --------------------------------------------------------------------
2593cdf0e10cSrcweir 
2594cdf0e10cSrcweir IMPL_LINK( SvxTableController, UpdateHdl, void *, EMPTYARG )
2595cdf0e10cSrcweir {
2596cdf0e10cSrcweir     mnUpdateEvent = 0;
2597cdf0e10cSrcweir 
2598cdf0e10cSrcweir     if( mbCellSelectionMode )
2599cdf0e10cSrcweir     {
2600cdf0e10cSrcweir         CellPos aStart( maCursorFirstPos );
2601cdf0e10cSrcweir         CellPos aEnd( maCursorLastPos );
2602cdf0e10cSrcweir         checkCell(aStart);
2603cdf0e10cSrcweir         checkCell(aEnd);
2604cdf0e10cSrcweir         if( aStart != maCursorFirstPos  || aEnd != maCursorLastPos )
2605cdf0e10cSrcweir         {
2606cdf0e10cSrcweir             setSelectedCells( aStart, aEnd );
2607cdf0e10cSrcweir         }
2608cdf0e10cSrcweir     }
2609cdf0e10cSrcweir     updateSelectionOverlay();
2610cdf0e10cSrcweir 
2611cdf0e10cSrcweir     return 0;
2612cdf0e10cSrcweir }
2613cdf0e10cSrcweir 
2614cdf0e10cSrcweir } }
2615