Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / sc / source / ui / view / tabvwshb.cxx
blob6f4f963763b0a7614eac39e023ffd879275bb3d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
21 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
23 #ifdef _MSC_VER
24 #pragma optimize ("", off)
25 #endif
27 #include <com/sun/star/embed/EmbedMisc.hpp>
28 #include <com/sun/star/embed/EmbedStates.hpp>
29 #include <sfx2/app.hxx>
30 #include <toolkit/helper/vclunohelper.hxx>
31 #include <svx/svxdlg.hxx>
32 #include <svx/dataaccessdescriptor.hxx>
33 #include <svx/pfiledlg.hxx>
34 #include <svx/svditer.hxx>
35 #include <svx/svdmark.hxx>
36 #include <svx/svdograf.hxx>
37 #include <svx/svdogrp.hxx>
38 #include <svx/svdoole2.hxx>
39 #include <svx/svdouno.hxx>
40 #include <svx/svdview.hxx>
41 #include <sfx2/linkmgr.hxx>
42 #include <svx/fontworkbar.hxx>
43 #include <sfx2/bindings.hxx>
44 #include <sfx2/dispatch.hxx>
45 #include <sfx2/viewfrm.hxx>
46 #include <svtools/soerr.hxx>
47 #include <svl/rectitem.hxx>
48 #include <svl/slstitm.hxx>
49 #include <svl/whiter.hxx>
50 #include <unotools/moduleoptions.hxx>
51 #include <sot/exchange.hxx>
52 #include <tools/diagnose_ex.h>
54 #include "tabvwsh.hxx"
55 #include "globstr.hrc"
56 #include "scmod.hxx"
57 #include "document.hxx"
58 #include "sc.hrc"
59 #include "client.hxx"
60 #include "fuinsert.hxx"
61 #include "docsh.hxx"
62 #include "chartarr.hxx"
63 #include "drawview.hxx"
64 #include "ChartRangeSelectionListener.hxx"
66 using namespace com::sun::star;
68 // STATIC DATA -----------------------------------------------------------
70 void ScTabViewShell::ConnectObject( SdrOle2Obj* pObj )
72 // wird aus dem Paint gerufen
74 uno::Reference < embed::XEmbeddedObject > xObj = pObj->GetObjRef();
75 Window* pWin = GetActiveWin();
77 // wenn schon connected ist, nicht nochmal SetObjArea/SetSizeScale
79 SfxInPlaceClient* pClient = FindIPClient( xObj, pWin );
80 if ( !pClient )
82 pClient = new ScClient( this, pWin, GetSdrView()->GetModel(), pObj );
83 Rectangle aRect = pObj->GetLogicRect();
84 Size aDrawSize = aRect.GetSize();
86 Size aOleSize = pObj->GetOrigObjSize();
88 Fraction aScaleWidth (aDrawSize.Width(), aOleSize.Width() );
89 Fraction aScaleHeight(aDrawSize.Height(), aOleSize.Height() );
90 aScaleWidth.ReduceInaccurate(10); // kompatibel zum SdrOle2Obj
91 aScaleHeight.ReduceInaccurate(10);
92 pClient->SetSizeScale(aScaleWidth,aScaleHeight);
94 // sichtbarer Ausschnitt wird nur inplace veraendert!
95 // the object area must be set after the scaling since it triggers the resizing
96 aRect.SetSize( aOleSize );
97 pClient->SetObjArea( aRect );
99 ((ScClient*)pClient)->SetGrafEdit( NULL );
103 sal_Bool ScTabViewShell::ActivateObject( SdrOle2Obj* pObj, long nVerb )
105 // Gueltigkeits-Hinweisfenster nicht ueber dem Objekt stehenlassen
106 RemoveHintWindow();
108 uno::Reference < embed::XEmbeddedObject > xObj = pObj->GetObjRef();
109 Window* pWin = GetActiveWin();
110 ErrCode nErr = ERRCODE_NONE;
111 sal_Bool bErrorShown = false;
114 SfxInPlaceClient* pClient = FindIPClient( xObj, pWin );
115 if ( !pClient )
116 pClient = new ScClient( this, pWin, GetSdrView()->GetModel(), pObj );
118 if ( !(nErr & ERRCODE_ERROR_MASK) && xObj.is() )
120 Rectangle aRect = pObj->GetLogicRect();
123 // #i118485# center on BoundRect for activation,
124 // OLE may be sheared/rotated now
125 const Rectangle& rBoundRect = pObj->GetCurrentBoundRect();
126 const Point aDelta(rBoundRect.Center() - aRect.Center());
127 aRect.Move(aDelta.X(), aDelta.Y());
130 Size aDrawSize = aRect.GetSize();
132 MapMode aMapMode( MAP_100TH_MM );
133 Size aOleSize = pObj->GetOrigObjSize( &aMapMode );
135 if ( pClient->GetAspect() != embed::Aspects::MSOLE_ICON
136 && ( xObj->getStatus( pClient->GetAspect() ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE ) )
138 // scale must always be 1 - change VisArea if different from client size
140 if ( aDrawSize != aOleSize )
142 MapUnit aUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( pClient->GetAspect() ) );
143 aOleSize = OutputDevice::LogicToLogic( aDrawSize,
144 MAP_100TH_MM, aUnit );
145 awt::Size aSz( aOleSize.Width(), aOleSize.Height() );
146 xObj->setVisualAreaSize( pClient->GetAspect(), aSz );
148 Fraction aOne( 1, 1 );
149 pClient->SetSizeScale( aOne, aOne );
151 else
153 // calculate scale from client and VisArea size
155 Fraction aScaleWidth (aDrawSize.Width(), aOleSize.Width() );
156 Fraction aScaleHeight(aDrawSize.Height(), aOleSize.Height() );
157 aScaleWidth.ReduceInaccurate(10); // kompatibel zum SdrOle2Obj
158 aScaleHeight.ReduceInaccurate(10);
159 pClient->SetSizeScale(aScaleWidth,aScaleHeight);
162 // sichtbarer Ausschnitt wird nur inplace veraendert!
163 // the object area must be set after the scaling since it triggers the resizing
164 aRect.SetSize( aOleSize );
165 pClient->SetObjArea( aRect );
167 ((ScClient*)pClient)->SetGrafEdit( NULL );
169 nErr = pClient->DoVerb( nVerb );
170 bErrorShown = sal_True;
171 // SfxViewShell::DoVerb zeigt seine Fehlermeldungen selber an
173 // attach listener to selection changes in chart that affect cell
174 // ranges, so those can be highlighted
175 // note: do that after DoVerb, so that the chart controller exists
176 if ( SvtModuleOptions().IsChart() )
178 SvGlobalName aObjClsId ( xObj->getClassID() );
179 if (SotExchange::IsChart( aObjClsId ))
183 uno::Reference < embed::XComponentSupplier > xSup( xObj, uno::UNO_QUERY_THROW );
184 uno::Reference< chart2::data::XDataReceiver > xDataReceiver(
185 xSup->getComponent(), uno::UNO_QUERY_THROW );
186 uno::Reference< chart2::data::XRangeHighlighter > xRangeHightlighter(
187 xDataReceiver->getRangeHighlighter());
188 if( xRangeHightlighter.is())
190 uno::Reference< view::XSelectionChangeListener > xListener(
191 new ScChartRangeSelectionListener( this ));
192 xRangeHightlighter->addSelectionChangeListener( xListener );
195 catch( const uno::Exception & )
197 OSL_FAIL( "Exception caught while querying chart" );
203 if (nErr != ERRCODE_NONE && !bErrorShown)
204 ErrorHandler::HandleError(nErr);
206 // #i118524# refresh handles to suppress for activated OLE
207 if(GetSdrView())
209 GetSdrView()->AdjustMarkHdl();
211 //! SetDocumentName sollte schon im Sfx passieren ???
212 //TODO/LATER: how "SetDocumentName"?
213 //xIPObj->SetDocumentName( GetViewData()->GetDocShell()->GetTitle() );
215 return ( !(nErr & ERRCODE_ERROR_MASK) );
218 ErrCode ScTabViewShell::DoVerb(long nVerb)
220 SdrView* pView = GetSdrView();
221 if (!pView)
222 return ERRCODE_SO_NOTIMPL; // soll nicht sein
224 SdrOle2Obj* pOle2Obj = NULL;
225 SdrObject* pObj = NULL;
226 ErrCode nErr = ERRCODE_NONE;
228 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
229 if (rMarkList.GetMarkCount() == 1)
231 pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
232 if (pObj->GetObjIdentifier() == OBJ_OLE2)
233 pOle2Obj = (SdrOle2Obj*) pObj;
236 if (pOle2Obj)
238 ActivateObject( pOle2Obj, nVerb );
240 else
242 OSL_FAIL("kein Objekt fuer Verb gefunden");
245 return nErr;
248 void ScTabViewShell::DeactivateOle()
250 // deactivate inplace editing if currently active
252 ScModule* pScMod = SC_MOD();
253 bool bUnoRefDialog = pScMod->IsRefDialogOpen() && pScMod->GetCurRefDlgId() == WID_SIMPLE_REF;
255 ScClient* pClient = (ScClient*) GetIPClient();
256 if ( pClient && pClient->IsObjectInPlaceActive() && !bUnoRefDialog )
257 pClient->DeactivateObject();
260 void ScTabViewShell::ExecDrawIns(SfxRequest& rReq)
262 sal_uInt16 nSlot = rReq.GetSlot();
263 if (nSlot != SID_OBJECTRESIZE )
265 SC_MOD()->InputEnterHandler();
266 UpdateInputHandler();
269 // Rahmen fuer Chart einfuegen wird abgebrochen:
270 FuPoor* pPoor = GetDrawFuncPtr();
271 if ( pPoor && pPoor->GetSlotID() == SID_DRAW_CHART )
272 GetViewData()->GetDispatcher().Execute(SID_DRAW_CHART, SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD);
274 MakeDrawLayer();
276 SfxBindings& rBindings = GetViewFrame()->GetBindings();
277 ScTabView* pTabView = GetViewData()->GetView();
278 Window* pWin = pTabView->GetActiveWin();
279 ScDrawView* pView = pTabView->GetScDrawView();
280 ScDocShell* pDocSh = GetViewData()->GetDocShell();
281 ScDocument* pDoc = pDocSh->GetDocument();
282 SdrModel* pDrModel = pView->GetModel();
284 switch ( nSlot )
286 case SID_INSERT_GRAPHIC:
287 FuInsertGraphic(this, pWin, pView, pDrModel, rReq);
288 // shell is set in MarkListHasChanged
289 break;
291 case SID_INSERT_AVMEDIA:
292 FuInsertMedia(this, pWin, pView, pDrModel, rReq);
293 // shell is set in MarkListHasChanged
294 break;
296 case SID_INSERT_DIAGRAM:
297 FuInsertChart(this, pWin, pView, pDrModel, rReq);
298 break;
300 case SID_INSERT_OBJECT:
301 case SID_INSERT_PLUGIN:
302 case SID_INSERT_SOUND:
303 case SID_INSERT_VIDEO:
304 case SID_INSERT_SMATH:
305 case SID_INSERT_FLOATINGFRAME:
306 FuInsertOLE(this, pWin, pView, pDrModel, rReq);
307 break;
309 case SID_OBJECTRESIZE:
311 // Der Server moechte die Clientgrosse verandern
313 SfxInPlaceClient* pClient = GetIPClient();
315 if ( pClient && pClient->IsObjectInPlaceActive() )
317 const SfxRectangleItem& rRect =
318 (SfxRectangleItem&)rReq.GetArgs()->Get(SID_OBJECTRESIZE);
319 Rectangle aRect( pWin->PixelToLogic( rRect.GetValue() ) );
321 if ( pView->AreObjectsMarked() )
323 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
325 if (rMarkList.GetMarkCount() == 1)
327 SdrMark* pMark = rMarkList.GetMark(0);
328 SdrObject* pObj = pMark->GetMarkedSdrObj();
330 sal_uInt16 nSdrObjKind = pObj->GetObjIdentifier();
332 if (nSdrObjKind == OBJ_OLE2)
334 if ( ( (SdrOle2Obj*) pObj)->GetObjRef().is() )
336 pObj->SetLogicRect(aRect);
343 break;
345 case SID_LINKS:
347 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
348 SfxAbstractLinksDialog* pDlg = pFact->CreateLinksDialog( pWin, pDoc->GetLinkManager() );
349 if ( pDlg )
351 pDlg->Execute();
352 rBindings.Invalidate( nSlot );
353 SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_AREALINKS_CHANGED ) ); // Navigator
354 rReq.Done();
357 break;
359 // #98721#
360 case SID_FM_CREATE_FIELDCONTROL:
362 SFX_REQUEST_ARG( rReq, pDescriptorItem, SfxUnoAnyItem, SID_FM_DATACCESS_DESCRIPTOR, false );
363 OSL_ENSURE( pDescriptorItem, "SID_FM_CREATE_FIELDCONTROL: invalid request args!" );
365 if(pDescriptorItem)
367 //! merge with ScViewFunc::PasteDataFormat (SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE)?
369 ScDrawView* pDrView = GetScDrawView();
370 SdrPageView* pPageView = pDrView ? pDrView->GetSdrPageView() : NULL;
371 if(pPageView)
373 ::svx::ODataAccessDescriptor aDescriptor(pDescriptorItem->GetValue());
374 SdrObject* pNewDBField = pDrView->CreateFieldControl(aDescriptor);
376 if(pNewDBField)
378 Rectangle aVisArea = pWin->PixelToLogic(Rectangle(Point(0,0), pWin->GetOutputSizePixel()));
379 Point aObjPos(aVisArea.Center());
380 Size aObjSize(pNewDBField->GetLogicRect().GetSize());
381 aObjPos.X() -= aObjSize.Width() / 2;
382 aObjPos.Y() -= aObjSize.Height() / 2;
383 Rectangle aNewObjectRectangle(aObjPos, aObjSize);
385 pNewDBField->SetLogicRect(aNewObjectRectangle);
387 // controls must be on control layer, groups on front layer
388 if ( pNewDBField->ISA(SdrUnoObj) )
389 pNewDBField->NbcSetLayer(SC_LAYER_CONTROLS);
390 else
391 pNewDBField->NbcSetLayer(SC_LAYER_FRONT);
392 if (pNewDBField->ISA(SdrObjGroup))
394 SdrObjListIter aIter( *pNewDBField, IM_DEEPWITHGROUPS );
395 SdrObject* pSubObj = aIter.Next();
396 while (pSubObj)
398 if ( pSubObj->ISA(SdrUnoObj) )
399 pSubObj->NbcSetLayer(SC_LAYER_CONTROLS);
400 else
401 pSubObj->NbcSetLayer(SC_LAYER_FRONT);
402 pSubObj = aIter.Next();
406 pView->InsertObjectAtView(pNewDBField, *pPageView);
410 rReq.Done();
412 break;
414 case SID_FONTWORK_GALLERY_FLOATER:
415 svx::FontworkBar::execute( pView, rReq, GetViewFrame()->GetBindings() );
416 rReq.Ignore();
417 break;
421 void ScTabViewShell::GetDrawInsState(SfxItemSet &rSet)
423 sal_Bool bOle = GetViewFrame()->GetFrame().IsInPlace();
424 sal_Bool bTabProt = GetViewData()->GetDocument()->IsTabProtected(GetViewData()->GetTabNo());
425 ScDocShell* pDocShell = ( GetViewData() ? GetViewData()->GetDocShell() : NULL );
426 bool bShared = ( pDocShell ? pDocShell->IsDocShared() : false );
428 SfxWhichIter aIter(rSet);
429 sal_uInt16 nWhich = aIter.FirstWhich();
430 while ( nWhich )
432 switch ( nWhich )
434 case SID_INSERT_DIAGRAM:
435 if ( bOle || bTabProt || !SvtModuleOptions().IsChart() || bShared )
436 rSet.DisableItem( nWhich );
437 break;
439 case SID_INSERT_SMATH:
440 if ( bOle || bTabProt || !SvtModuleOptions().IsMath() || bShared )
441 rSet.DisableItem( nWhich );
442 break;
444 case SID_INSERT_OBJECT:
445 case SID_INSERT_PLUGIN:
446 case SID_INSERT_FLOATINGFRAME:
447 if ( bOle || bTabProt || bShared )
448 rSet.DisableItem( nWhich );
449 break;
451 case SID_INSERT_SOUND:
452 case SID_INSERT_VIDEO:
453 /* #i102735# discussed with NN: removed for performance reasons
454 || !SvxPluginFileDlg::IsAvailable(nWhich)
456 if ( bOle || bTabProt || bShared )
457 rSet.DisableItem( nWhich );
458 break;
460 case SID_INSERT_GRAPHIC:
461 case SID_INSERT_AVMEDIA:
462 case SID_FONTWORK_GALLERY_FLOATER:
463 if ( bTabProt || bShared )
464 rSet.DisableItem( nWhich );
465 break;
467 case SID_LINKS:
469 if (GetViewData()->GetDocument()->GetLinkManager()->GetLinks().empty())
470 rSet.DisableItem( SID_LINKS );
472 break;
474 nWhich = aIter.NextWhich();
479 //------------------------------------------------------------------
481 void ScTabViewShell::ExecuteUndo(SfxRequest& rReq)
483 SfxShell* pSh = GetViewData()->GetDispatcher().GetShell(0);
484 ::svl::IUndoManager* pUndoManager = pSh->GetUndoManager();
486 const SfxItemSet* pReqArgs = rReq.GetArgs();
487 ScDocShell* pDocSh = GetViewData()->GetDocShell();
489 sal_uInt16 nSlot = rReq.GetSlot();
490 switch ( nSlot )
492 case SID_UNDO:
493 case SID_REDO:
494 if ( pUndoManager )
496 sal_Bool bIsUndo = ( nSlot == SID_UNDO );
498 sal_uInt16 nCount = 1;
499 const SfxPoolItem* pItem;
500 if ( pReqArgs && pReqArgs->GetItemState( nSlot, sal_True, &pItem ) == SFX_ITEM_SET )
501 nCount = ((const SfxUInt16Item*)pItem)->GetValue();
503 // lock paint for more than one cell undo action (not for editing within a cell)
504 sal_Bool bLockPaint = ( nCount > 1 && pUndoManager == GetUndoManager() );
505 if ( bLockPaint )
506 pDocSh->LockPaint();
510 for (sal_uInt16 i=0; i<nCount; i++)
512 if ( bIsUndo )
513 pUndoManager->Undo();
514 else
515 pUndoManager->Redo();
518 catch ( const uno::Exception& )
520 // no need to handle. By definition, the UndoManager handled this by clearing the
521 // Undo/Redo stacks
524 if ( bLockPaint )
525 pDocSh->UnlockPaint();
527 GetViewFrame()->GetBindings().InvalidateAll(false);
529 break;
530 // default:
531 // GetViewFrame()->ExecuteSlot( rReq );
535 void ScTabViewShell::GetUndoState(SfxItemSet &rSet)
537 SfxShell* pSh = GetViewData()->GetDispatcher().GetShell(0);
538 ::svl::IUndoManager* pUndoManager = pSh->GetUndoManager();
540 SfxWhichIter aIter(rSet);
541 sal_uInt16 nWhich = aIter.FirstWhich();
542 while ( nWhich )
544 switch (nWhich)
546 case SID_GETUNDOSTRINGS:
547 case SID_GETREDOSTRINGS:
549 SfxStringListItem aStrLst( nWhich );
550 if ( pUndoManager )
552 std::vector<String> &aList = aStrLst.GetList();
553 sal_Bool bIsUndo = ( nWhich == SID_GETUNDOSTRINGS );
554 size_t nCount = bIsUndo ? pUndoManager->GetUndoActionCount() : pUndoManager->GetRedoActionCount();
555 for (size_t i=0; i<nCount; ++i)
557 aList.push_back( rtl::OUString( bIsUndo ? pUndoManager->GetUndoActionComment(i) :
558 pUndoManager->GetRedoActionComment(i) ) );
561 rSet.Put( aStrLst );
563 break;
564 default:
565 // get state from sfx view frame
566 GetViewFrame()->GetSlotState( nWhich, NULL, &rSet );
569 nWhich = aIter.NextWhich();
575 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */