Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / drawfunc / fusel.cxx
blob9cb144bde0d43c50ddf3008c3de6e3b1df514e0c
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/EmbedStates.hpp>
22 #include <editeng/eeitem.hxx>
23 #include <editeng/flditem.hxx>
24 #include <svx/svddrgmt.hxx>
25 #include <svx/svdoole2.hxx>
26 #include <svx/svdotext.hxx>
27 #include <sfx2/dispatch.hxx>
28 #include <svtools/imapobj.hxx>
29 #include <svx/svdouno.hxx>
30 #include <svx/svdomedia.hxx>
31 #include <svx/svdpagv.hxx>
32 #include <editeng/outlobj.hxx>
33 #include <svx/svdocapt.hxx>
34 #include <sfx2/app.hxx>
36 #include "fusel.hxx"
37 #include "sc.hrc"
38 #include "fudraw.hxx"
39 #include "futext.hxx"
40 #include "drawview.hxx"
41 #include "tabvwsh.hxx"
42 #include "drawpage.hxx"
43 #include "globstr.hrc"
44 #include "drwlayer.hxx"
45 #include "userdat.hxx"
46 #include "scmod.hxx"
47 #include "client.hxx"
48 #include "charthelper.hxx"
49 #include "docuno.hxx"
50 #include "docsh.hxx"
52 // Maximal erlaubte Mausbewegung um noch Drag&Drop zu starten
53 //! fusel,fuconstr,futext - zusammenfassen!
54 #define SC_MAXDRAGMOVE 3
55 // Min necessary mouse motion for normal dragging
56 #define SC_MINDRAGMOVE 2
58 using namespace com::sun::star;
60 /*************************************************************************
62 |* Konstruktor
64 \************************************************************************/
66 FuSelection::FuSelection(ScTabViewShell* pViewSh, vcl::Window* pWin, ScDrawView* pViewP,
67 SdrModel* pDoc, SfxRequest& rReq ) :
68 FuDraw(pViewSh, pWin, pViewP, pDoc, rReq),
69 bVCAction(false)
73 /*************************************************************************
75 |* Destruktor
77 \************************************************************************/
79 FuSelection::~FuSelection()
83 sal_uInt8 FuSelection::Command(const CommandEvent& rCEvt)
85 return FuDraw::Command( rCEvt );
88 /*************************************************************************
90 |* MouseButtonDown-event
92 \************************************************************************/
94 bool FuSelection::MouseButtonDown(const MouseEvent& rMEvt)
96 // remember button state for creation of own MouseEvents
97 SetMouseButtonCode(rMEvt.GetButtons());
98 const bool bSelectionOnly = rMEvt.IsRight();
99 if ( pView->IsAction() )
101 if ( bSelectionOnly )
102 pView->BckAction();
103 return true;
106 bVCAction = false;
107 bIsInDragMode = false; // irgendwo muss es ja zurueckgesetzt werden (#50033#)
109 bool bReturn = FuDraw::MouseButtonDown(rMEvt);
111 aMDPos = pWindow->PixelToLogic( rMEvt.GetPosPixel() );
113 if ( rMEvt.IsLeft() )
115 SdrHdl* pHdl = pView->PickHandle(aMDPos);
117 if ( pHdl!=nullptr || pView->IsMarkedHit(aMDPos) )
119 // Determine if this is the tail of a SdrCaptionObj i.e.
120 // we need to disable the drag option on the tail of a note
121 // object. Also, disable the ability to use the circular
122 // drag of a note object.
123 bool bDrag = false;
124 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
125 if( rMarkList.GetMarkCount() == 1 )
127 SdrObject* pMarkedObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
128 if( ScDrawLayer::IsNoteCaption( pMarkedObj ) )
130 // move using the valid caption handles for note text box.
131 if(pHdl && (pHdl->GetKind() != HDL_POLY && pHdl->GetKind() != HDL_CIRC))
132 bDrag = true;
133 // move the complete note box.
134 else if(!pHdl)
135 bDrag = true;
137 else
138 bDrag = true; // different object
140 else
141 bDrag = true; // several objects
143 if ( bDrag )
145 aDragTimer.Start();
146 if (pView->BegDragObj(aMDPos, nullptr, pHdl))
147 pView->GetDragMethod()->SetShiftPressed( rMEvt.IsShift() );
148 bReturn = true;
151 else
153 SdrObject* pObj;
154 SdrPageView* pPV;
155 bool bAlt = rMEvt.IsMod2();
156 if ( !bAlt && pView->PickObj(aMDPos, pView->getHitTolLog(), pObj, pPV, SdrSearchOptions::PICKMACRO) )
158 pView->BegMacroObj(aMDPos, pObj, pPV, pWindow);
159 bReturn = true;
161 else
163 OUString sURL, sTarget;
164 if ( !bAlt && pView->PickObj(aMDPos, pView->getHitTolLog(), pObj, pPV, SdrSearchOptions::ALSOONMASTER))
166 // Support for imported Excel docs
167 // Excel is of course not consistent and allows
168 // a hyperlink to be assigned for an object group
169 // and even though the hyperlink is exported in the Escher layer
170 // its never used, when dealing with a group object the link
171 // associated with the clicked object is used only
173 // additionally you can also select a macro in Excel for a grouped
174 // objects and this *usually* results in the macro being set
175 // for the elements in the group and no macro is exported
176 // for the group itself ( this however is not always true )
177 // if a macro and hlink are defined favour the hlink
178 // If a group object has no hyperlink use the hyperlink of the
179 // object clicked
181 if ( pObj->IsGroupObject() )
183 ScMacroInfo* pTmpInfo = ScDrawLayer::GetMacroInfo( pObj );
184 if ( !pTmpInfo || pTmpInfo->GetMacro().isEmpty() )
186 SdrObject* pHit = nullptr;
187 if ( pView->PickObj(aMDPos, pView->getHitTolLog(), pHit, pPV, SdrSearchOptions::DEEP ) )
188 pObj = pHit;
192 ScMacroInfo* pInfo = ScDrawLayer::GetMacroInfo( pObj, true );
193 // For interoperability favour links over macros if both are defined
194 if ( !pInfo->GetHlink().isEmpty() )
196 OSL_TRACE("** Got URL");
197 sURL = pInfo->GetHlink();
199 else if ( !pInfo->GetMacro().isEmpty() )
201 SfxObjectShell* pObjSh = SfxObjectShell::Current();
202 if ( pObjSh && SfxApplication::IsXScriptURL( pInfo->GetMacro() ) )
204 uno::Reference< beans::XPropertySet > xProps( pObj->getUnoShape(), uno::UNO_QUERY );
205 uno::Any aCaller;
206 if ( xProps.is() )
210 aCaller = xProps->getPropertyValue("Name");
212 catch( uno::Exception& ) {}
214 uno::Any aRet;
215 uno::Sequence< sal_Int16 > aOutArgsIndex;
216 uno::Sequence< uno::Any > aOutArgs;
217 uno::Sequence< uno::Any > aInArgs;
218 pObjSh->CallXScript( pInfo->GetMacro(),
219 aInArgs, aRet, aOutArgsIndex, aOutArgs, true, &aCaller );
220 pViewShell->FakeButtonUp( pViewShell->GetViewData().GetActivePart() );
221 return true; // kein CaptureMouse etc.
226 // URL / ImageMap
228 SdrViewEvent aVEvt;
229 if ( !bAlt &&
230 pView->PickAnything( rMEvt, SdrMouseEventKind::BUTTONDOWN, aVEvt ) != SDRHIT_NONE &&
231 aVEvt.pObj != nullptr )
233 if ( ScDrawLayer::GetIMapInfo( aVEvt.pObj ) ) // ImageMap
235 const IMapObject* pIMapObj =
236 ScDrawLayer::GetHitIMapObject( aVEvt.pObj, aMDPos, *pWindow );
237 if ( pIMapObj && !pIMapObj->GetURL().isEmpty() )
239 sURL = pIMapObj->GetURL();
240 sTarget = pIMapObj->GetTarget();
243 if ( aVEvt.eEvent == SDREVENT_EXECUTEURL && aVEvt.pURLField ) // URL
245 sURL = aVEvt.pURLField->GetURL();
246 sTarget = aVEvt.pURLField->GetTargetFrame();
250 // open hyperlink, if found at object or in object's text
251 if ( !sURL.isEmpty() )
253 ScGlobal::OpenURL( sURL, sTarget );
254 pViewShell->FakeButtonUp( pViewShell->GetViewData().GetActivePart() );
255 return true; // kein CaptureMouse etc.
258 // Is another object being edited in this view?
259 // (Editing is ended in MarkListHasChanged - test before UnmarkAll)
260 SfxInPlaceClient* pClient = pViewShell->GetIPClient();
261 bool bWasOleActive = ( pClient && pClient->IsObjectInPlaceActive() );
263 // Markieren
265 // do not allow multiselection with note caption
266 bool bCaptionClicked = IsNoteCaptionClicked( aMDPos );
267 if ( !rMEvt.IsShift() || bCaptionClicked || IsNoteCaptionMarked() )
268 pView->UnmarkAll();
270 /* Unlock internal layer, if a note caption is clicked. The
271 layer will be relocked in ScDrawView::MarkListHasChanged(). */
272 if( bCaptionClicked )
273 pView->UnlockInternalLayer();
275 // try to select the clicked object
276 if ( pView->MarkObj( aMDPos, -2, false, rMEvt.IsMod1() ) )
279 //Objekt verschieben
281 if (pView->IsMarkedHit(aMDPos))
283 // Don't start drag timer if inplace editing of an OLE object
284 // was just ended with this mouse click - the view will be moved
285 // (different tool bars) and the object that was clicked on would
286 // be moved unintentionally.
287 if ( !bWasOleActive )
288 aDragTimer.Start();
290 pHdl=pView->PickHandle(aMDPos);
291 pView->BegDragObj(aMDPos, nullptr, pHdl);
292 bReturn = true;
294 else // Objekt am Rand getroffen
295 if (pViewShell->IsDrawSelMode())
296 bReturn = true;
298 else
300 // nichts getroffen
302 if (pViewShell->IsDrawSelMode())
305 //Objekt selektieren
307 pView->BegMarkObj(aMDPos);
308 bReturn = true;
316 if (!bIsInDragMode)
318 if (!bVCAction) // VC rufen selber CaptureMouse
319 pWindow->CaptureMouse();
320 ForcePointer(&rMEvt);
323 return bReturn;
326 /*************************************************************************
328 |* MouseMove-event
330 \************************************************************************/
332 bool FuSelection::MouseMove(const MouseEvent& rMEvt)
334 bool bReturn = FuDraw::MouseMove(rMEvt);
336 if (aDragTimer.IsActive() )
338 Point aOldPixel = pWindow->LogicToPixel( aMDPos );
339 Point aNewPixel = rMEvt.GetPosPixel();
340 if ( std::abs( aOldPixel.X() - aNewPixel.X() ) > SC_MAXDRAGMOVE ||
341 std::abs( aOldPixel.Y() - aNewPixel.Y() ) > SC_MAXDRAGMOVE )
342 aDragTimer.Stop();
345 if ( pView->IsAction() )
347 Point aPix(rMEvt.GetPosPixel());
348 Point aPnt(pWindow->PixelToLogic(aPix));
350 ForceScroll(aPix);
351 pView->MovAction(aPnt);
352 bReturn = true;
355 // Ein VCControl ist aktiv
356 // Event an den Manager weiterleiten
357 if( bVCAction )
359 bReturn = true;
362 ForcePointer(&rMEvt);
364 return bReturn;
367 /*************************************************************************
369 |* MouseButtonUp-event
371 \************************************************************************/
373 bool FuSelection::MouseButtonUp(const MouseEvent& rMEvt)
375 // remember button state for creation of own MouseEvents
376 SetMouseButtonCode(rMEvt.GetButtons());
378 bool bReturn = FuDraw::MouseButtonUp(rMEvt);
379 bool bOle = pViewShell && pViewShell->GetViewFrame()->GetFrame().IsInPlace();
381 SdrObject* pObj = nullptr;
382 if (aDragTimer.IsActive() )
384 aDragTimer.Stop();
387 sal_uInt16 nDrgLog = sal_uInt16 ( pWindow->PixelToLogic(Size(SC_MINDRAGMOVE,0)).Width() );
388 Point aPnt( pWindow->PixelToLogic( rMEvt.GetPosPixel() ) );
390 bool bCopy = false;
391 ScViewData* pViewData = ( pViewShell ? &pViewShell->GetViewData() : nullptr );
392 ScDocument* pDocument = ( pViewData ? pViewData->GetDocument() : nullptr );
393 SdrPageView* pPageView = ( pView ? pView->GetSdrPageView() : nullptr );
394 SdrPage* pPage = ( pPageView ? pPageView->GetPage() : nullptr );
395 ::std::vector< OUString > aExcludedChartNames;
396 ScRangeListVector aProtectedChartRangesVector;
398 if (pView && rMEvt.IsLeft())
400 if ( pView->IsDragObj() )
402 /******************************************************************
403 * Objekt wurde verschoben
404 ******************************************************************/
405 if ( rMEvt.IsMod1() )
407 if ( pPage )
409 ScChartHelper::GetChartNames( aExcludedChartNames, pPage );
411 if ( pView && pDocument )
413 const SdrMarkList& rSdrMarkList = pView->GetMarkedObjectList();
414 const size_t nMarkCount = rSdrMarkList.GetMarkCount();
415 for ( size_t i = 0; i < nMarkCount; ++i )
417 SdrMark* pMark = rSdrMarkList.GetMark( i );
418 pObj = ( pMark ? pMark->GetMarkedSdrObj() : nullptr );
419 if ( pObj )
421 ScChartHelper::AddRangesIfProtectedChart( aProtectedChartRangesVector, pDocument, pObj );
425 bCopy = true;
428 if (!rMEvt.IsShift() && !rMEvt.IsMod1() && !rMEvt.IsMod2() &&
429 std::abs(aPnt.X() - aMDPos.X()) < nDrgLog &&
430 std::abs(aPnt.Y() - aMDPos.Y()) < nDrgLog)
432 /*************************************************************
433 * If a user wants to click on an object in front of a marked
434 * one, he releases the mouse button immediately
435 **************************************************************/
436 SdrPageView* pPV = nullptr;
437 if (pView->PickObj(aMDPos, pView->getHitTolLog(), pObj, pPV, SdrSearchOptions::ALSOONMASTER | SdrSearchOptions::BEFOREMARK))
439 pView->UnmarkAllObj();
440 pView->MarkObj(pObj,pPV);
441 return true;
444 pView->EndDragObj( rMEvt.IsMod1() );
445 pView->ForceMarkedToAnotherPage();
447 bReturn = true;
449 else if (pView->IsAction() )
451 // unlock internal layer to include note captions
452 pView->UnlockInternalLayer();
453 pView->EndAction();
454 if ( pView->AreObjectsMarked() )
456 bReturn = true;
458 /* if multi-selection contains a note caption object, remove
459 all other objects from selection. */
460 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
461 const size_t nCount = rMarkList.GetMarkCount();
462 if( nCount > 1 )
464 bool bFound = false;
465 for( size_t nIdx = 0; !bFound && (nIdx < nCount); ++nIdx )
467 pObj = rMarkList.GetMark( nIdx )->GetMarkedSdrObj();
468 bFound = ScDrawLayer::IsNoteCaption( pObj );
469 if( bFound )
471 pView->UnMarkAll();
472 pView->MarkObj( pObj, pView->GetSdrPageView() );
480 /**************************************************************************
481 * Ggf. OLE-Objekt beruecksichtigen
482 **************************************************************************/
483 SfxInPlaceClient* pIPClient = pViewShell ? pViewShell->GetIPClient() : nullptr;
485 if (pIPClient)
487 ScModule* pScMod = SC_MOD();
488 bool bUnoRefDialog = pScMod->IsRefDialogOpen() && pScMod->GetCurRefDlgId() == WID_SIMPLE_REF;
490 if ( pIPClient->IsObjectInPlaceActive() && !bUnoRefDialog )
491 pIPClient->DeactivateObject();
494 sal_uInt16 nClicks = rMEvt.GetClicks();
495 if (pView && nClicks == 2 && rMEvt.IsLeft())
497 if ( pView->AreObjectsMarked() )
499 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
500 if (rMarkList.GetMarkCount() == 1)
502 SdrMark* pMark = rMarkList.GetMark(0);
503 pObj = pMark->GetMarkedSdrObj();
505 // aktivieren nur, wenn die Maus auch (noch) ueber dem
506 // selektierten Objekt steht
508 SdrViewEvent aVEvt;
509 SdrHitKind eHit = pView->PickAnything( rMEvt, SdrMouseEventKind::BUTTONDOWN, aVEvt );
510 if (eHit != SDRHIT_NONE && aVEvt.pObj == pObj && pViewShell)
512 sal_uInt16 nSdrObjKind = pObj->GetObjIdentifier();
514 // OLE: aktivieren
516 if (nSdrObjKind == OBJ_OLE2)
518 if (!bOle)
520 if (static_cast<SdrOle2Obj*>(pObj)->GetObjRef().is())
522 pViewShell->ActivateObject( static_cast<SdrOle2Obj*>(pObj), 0 );
527 // Edit text
528 // not in UNO controls
529 // #i32352# not in media objects
531 else if ( dynamic_cast<const SdrTextObj*>( pObj) != nullptr && dynamic_cast<const SdrUnoObj*>( pObj) == nullptr && dynamic_cast<const SdrMediaObj*>( pObj) == nullptr )
533 OutlinerParaObject* pOPO = pObj->GetOutlinerParaObject();
534 bool bVertical = ( pOPO && pOPO->IsVertical() );
535 sal_uInt16 nTextSlotId = bVertical ? SID_DRAW_TEXT_VERTICAL : SID_DRAW_TEXT;
537 pViewShell->GetViewData().GetDispatcher().
538 Execute(nTextSlotId, SfxCallMode::SYNCHRON | SfxCallMode::RECORD);
540 // jetzt den erzeugten FuText holen und in den EditModus setzen
541 FuPoor* pPoor = pViewShell->GetViewData().GetView()->GetDrawFuncPtr();
542 if ( pPoor && pPoor->GetSlotID() == nTextSlotId ) // hat keine RTTI
544 FuText* pText = static_cast<FuText*>(pPoor);
545 Point aMousePixel = rMEvt.GetPosPixel();
546 pText->SetInEditMode( pObj, &aMousePixel );
548 bReturn = true;
553 else if ( TestDetective( pView->GetSdrPageView(), aPnt ) )
554 bReturn = true;
557 // Ein VCControl ist aktiv
558 // Event an den Manager weiterleiten
559 if( bVCAction )
561 bVCAction = false;
562 bReturn = true;
565 ForcePointer(&rMEvt);
567 if (pWindow->IsMouseCaptured())
568 pWindow->ReleaseMouse();
570 // Command-Handler fuer Kontext-Menue kommt erst nach MouseButtonUp,
571 // darum hier die harte IsLeft-Abfrage
572 if ( !bReturn && rMEvt.IsLeft() )
573 if (pViewShell->IsDrawSelMode())
574 pViewShell->GetViewData().GetDispatcher().
575 Execute(SID_OBJECT_SELECT, SfxCallMode::SLOT | SfxCallMode::RECORD);
577 if ( bCopy && pViewData && pDocument && pPage )
579 ScDocShell* pDocShell = pViewData->GetDocShell();
580 ScModelObj* pModelObj = ( pDocShell ? ScModelObj::getImplementation( pDocShell->GetModel() ) : nullptr );
581 if ( pModelObj )
583 SCTAB nTab = pViewData->GetTabNo();
584 ScChartHelper::CreateProtectedChartListenersAndNotify( pDocument, pPage, pModelObj, nTab,
585 aProtectedChartRangesVector, aExcludedChartNames );
589 return bReturn;
592 /*************************************************************************
594 |* Tastaturereignisse bearbeiten
596 |* Wird ein KeyEvent bearbeitet, so ist der Return-Wert sal_True, andernfalls
597 |* FALSE.
599 \************************************************************************/
601 bool FuSelection::KeyInput(const KeyEvent& rKEvt)
603 return FuDraw::KeyInput(rKEvt);
606 /*************************************************************************
608 |* Function aktivieren
610 \************************************************************************/
612 void FuSelection::Activate()
614 FuDraw::Activate();
617 /*************************************************************************
619 |* Function deaktivieren
621 \************************************************************************/
623 void FuSelection::Deactivate()
625 /**************************************************************************
626 * Hide Cursor
627 **************************************************************************/
628 FuDraw::Deactivate();
631 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */