Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / ui / drawfunc / graphsh.cxx
blobc3c5dd4b5f5ca3da8e8451c25845c6e09e5e3309
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 <sfx2/objface.hxx>
21 #include <vcl/EnumContext.hxx>
22 #include <sfx2/opengrf.hxx>
23 #include <svx/svdograf.hxx>
24 #include <svx/grfflt.hxx>
25 #include <svx/grafctrl.hxx>
26 #include <svx/compressgraphicdialog.hxx>
27 #include <svx/graphichelper.hxx>
28 #include <svx/svxids.hrc>
30 #include <graphsh.hxx>
31 #include <strings.hrc>
32 #include <viewdata.hxx>
33 #include <drawview.hxx>
34 #include <gridwin.hxx>
35 #include <scresid.hxx>
36 #include <svx/extedit.hxx>
38 #define ShellClass_ScGraphicShell
39 #include <scslots.hxx>
41 SFX_IMPL_INTERFACE(ScGraphicShell, ScDrawShell)
43 void ScGraphicShell::InitInterface_Impl()
45 GetStaticInterface()->RegisterObjectBar(SFX_OBJECTBAR_OBJECT,
46 SfxVisibilityFlags::Standard | SfxVisibilityFlags::Server,
47 ToolbarId::Graphic_Objectbar);
49 GetStaticInterface()->RegisterPopupMenu("graphic");
53 ScGraphicShell::ScGraphicShell(ScViewData* pData) :
54 ScDrawShell(pData)
56 SetName("GraphicObject");
57 SfxShell::SetContextName(vcl::EnumContext::GetContextName(vcl::EnumContext::Context::Graphic));
60 ScGraphicShell::~ScGraphicShell()
64 void ScGraphicShell::GetAttrState( SfxItemSet& rSet )
66 ScDrawView* pView = GetViewData()->GetScDrawView();
68 if( pView )
69 SvxGrafAttrHelper::GetGrafAttrState( rSet, *pView );
72 void ScGraphicShell::Execute( SfxRequest& rReq )
74 ScDrawView* pView = GetViewData()->GetScDrawView();
76 if( pView )
78 SvxGrafAttrHelper::ExecuteGrafAttr( rReq, *pView );
79 Invalidate();
83 void ScGraphicShell::GetFilterState( SfxItemSet& rSet )
85 ScDrawView* pView = GetViewData()->GetScDrawView();
86 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
87 bool bEnable = false;
89 if( rMarkList.GetMarkCount() == 1 )
91 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
93 if( dynamic_cast<const SdrGrafObj*>( pObj) && ( static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap ) )
94 bEnable = true;
97 if( !bEnable )
98 SvxGraphicFilter::DisableGraphicFilterSlots( rSet );
101 void ScGraphicShell::ExecuteFilter( const SfxRequest& rReq )
103 ScDrawView* pView = GetViewData()->GetScDrawView();
104 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
106 if( rMarkList.GetMarkCount() == 1 )
108 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
110 if( dynamic_cast<const SdrGrafObj*>( pObj) && static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap )
112 GraphicObject aFilterObj( static_cast<SdrGrafObj*>(pObj)->GetGraphicObject() );
114 if( SvxGraphicFilterResult::NONE ==
115 SvxGraphicFilter::ExecuteGrfFilterSlot( rReq, aFilterObj ) )
117 SdrPageView* pPageView = pView->GetSdrPageView();
119 if( pPageView )
121 SdrGrafObj* pFilteredObj(static_cast<SdrGrafObj*>(pObj->CloneSdrObject(pObj->getSdrModelFromSdrObject())));
122 OUString aStr = pView->GetDescriptionOfMarkedObjects() + " " + ScResId(SCSTR_UNDO_GRAFFILTER);
123 pView->BegUndo( aStr );
124 pFilteredObj->SetGraphicObject( aFilterObj );
125 pView->ReplaceObjectAtView( pObj, *pPageView, pFilteredObj );
126 pView->EndUndo();
132 Invalidate();
135 void ScGraphicShell::GetExternalEditState( SfxItemSet& rSet )
137 ScDrawView* pView = GetViewData()->GetScDrawView();
138 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
139 bool bEnable = false;
140 if( rMarkList.GetMarkCount() == 1 )
142 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
144 if( dynamic_cast<const SdrGrafObj*>( pObj) && ( static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap ) )
145 bEnable = true;
148 if (GetViewShell()->isExportLocked())
149 bEnable = false;
151 if( !bEnable )
152 rSet.DisableItem( SID_EXTERNAL_EDIT );
155 void ScGraphicShell::ExecuteExternalEdit( SAL_UNUSED_PARAMETER SfxRequest& )
157 ScDrawView* pView = GetViewData()->GetScDrawView();
158 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
160 if( rMarkList.GetMarkCount() == 1 )
162 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
164 if( dynamic_cast<const SdrGrafObj*>( pObj) && static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap )
166 GraphicObject aGraphicObject( static_cast<SdrGrafObj*>(pObj)->GetGraphicObject() );
167 m_ExternalEdits.push_back( std::make_unique<SdrExternalToolEdit>(
168 pView, pObj));
169 m_ExternalEdits.back()->Edit( &aGraphicObject );
173 Invalidate();
176 void ScGraphicShell::GetCompressGraphicState( SfxItemSet& rSet )
178 ScDrawView* pView = GetViewData()->GetScDrawView();
179 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
180 bool bEnable = false;
181 if( rMarkList.GetMarkCount() == 1 )
183 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
185 if( dynamic_cast<const SdrGrafObj*>( pObj) && ( static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap ) )
186 bEnable = true;
189 if( !bEnable )
190 rSet.DisableItem( SID_COMPRESS_GRAPHIC );
193 void ScGraphicShell::ExecuteCompressGraphic( SAL_UNUSED_PARAMETER SfxRequest& )
195 ScDrawView* pView = GetViewData()->GetScDrawView();
196 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
198 if( rMarkList.GetMarkCount() == 1 )
200 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
202 if( dynamic_cast<const SdrGrafObj*>( pObj) && static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap )
204 SdrGrafObj* pGraphicObj = static_cast<SdrGrafObj*>(pObj);
205 CompressGraphicsDialog dialog(GetViewData()->GetDialogParent(), pGraphicObj, GetViewData()->GetBindings());
206 if (dialog.run() == RET_OK)
208 SdrGrafObj* pNewObject = dialog.GetCompressedSdrGrafObj();
209 SdrPageView* pPageView = pView->GetSdrPageView();
210 OUString aUndoString = pView->GetDescriptionOfMarkedObjects() + " Compress";
211 pView->BegUndo( aUndoString );
212 pView->ReplaceObjectAtView( pObj, *pPageView, pNewObject );
213 pView->EndUndo();
218 Invalidate();
221 void ScGraphicShell::GetCropGraphicState( SfxItemSet& rSet )
223 ScDrawView* pView = GetViewData()->GetScDrawView();
224 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
225 bool bEnable = false;
226 if( rMarkList.GetMarkCount() == 1 )
228 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
230 if( dynamic_cast<const SdrGrafObj*>( pObj) && ( static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap ) )
231 bEnable = true;
234 if( !bEnable )
235 rSet.DisableItem( SID_OBJECT_CROP );
238 void ScGraphicShell::ExecuteCropGraphic( SAL_UNUSED_PARAMETER SfxRequest& )
240 ScDrawView* pView = GetViewData()->GetScDrawView();
241 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
243 if( rMarkList.GetMarkCount() == 1 )
245 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
247 if( dynamic_cast<const SdrGrafObj*>( pObj) && static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap )
249 pView->SetEditMode(SdrViewEditMode::Edit);
250 pView->SetDragMode(SdrDragMode::Crop);
254 Invalidate();
257 void ScGraphicShell::ExecuteSaveGraphic( SAL_UNUSED_PARAMETER SfxRequest& /*rReq*/)
259 ScDrawView* pView = GetViewData()->GetScDrawView();
260 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
261 if( rMarkList.GetMarkCount() == 1 )
263 const SdrGrafObj* pObj = dynamic_cast<const SdrGrafObj*>(rMarkList.GetMark( 0 )->GetMarkedSdrObj());
264 if( pObj && pObj->GetGraphicType() == GraphicType::Bitmap )
266 GraphicAttr aGraphicAttr = pObj->GetGraphicAttr();
267 short nState = RET_CANCEL;
268 vcl::Window* pWin = GetViewData()->GetActiveWin();
269 weld::Window* pWinFrame = pWin ? pWin->GetFrameWeld() : nullptr;
270 if (aGraphicAttr != GraphicAttr()) // the image has been modified
272 if (pWin)
274 nState = GraphicHelper::HasToSaveTransformedImage(pWinFrame);
277 else
279 nState = RET_NO;
282 if (nState == RET_YES)
284 GraphicHelper::ExportGraphic(pWinFrame, pObj->GetTransformedGraphic(), "");
286 else if (nState == RET_NO)
288 const GraphicObject& aGraphicObject(pObj->GetGraphicObject());
289 GraphicHelper::ExportGraphic(pWinFrame, aGraphicObject.GetGraphic(), "");
294 Invalidate();
297 void ScGraphicShell::GetSaveGraphicState(SfxItemSet &rSet)
299 ScDrawView* pView = GetViewData()->GetScDrawView();
300 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
301 bool bEnable = false;
302 if( rMarkList.GetMarkCount() == 1 )
304 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
306 if( dynamic_cast<const SdrGrafObj*>( pObj) && ( static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap ) )
307 bEnable = true;
310 if (GetViewShell()->isExportLocked())
311 bEnable = false;
313 if( !bEnable )
314 rSet.DisableItem( SID_SAVE_GRAPHIC );
317 void ScGraphicShell::ExecuteChangePicture( SAL_UNUSED_PARAMETER SfxRequest& /*rReq*/)
319 ScDrawView* pView = GetViewData()->GetScDrawView();
320 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
322 if( rMarkList.GetMarkCount() == 1 )
324 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
326 if( dynamic_cast<const SdrGrafObj*>( pObj) && static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap )
328 SdrGrafObj* pGraphicObj = static_cast<SdrGrafObj*>(pObj);
329 vcl::Window* pWin = GetViewData()->GetActiveWin();
330 SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC), pWin ? pWin->GetFrameWeld() : nullptr);
332 if( aDlg.Execute() == ERRCODE_NONE )
334 Graphic aGraphic;
335 ErrCode nError = aDlg.GetGraphic(aGraphic);
336 if( nError == ERRCODE_NONE )
338 SdrGrafObj* pNewObject(pGraphicObj->CloneSdrObject(pGraphicObj->getSdrModelFromSdrObject()));
339 pNewObject->SetGraphic( aGraphic );
340 SdrPageView* pPageView = pView->GetSdrPageView();
341 OUString aUndoString = pView->GetDescriptionOfMarkedObjects() + " Change";
342 pView->BegUndo( aUndoString );
343 pView->ReplaceObjectAtView( pObj, *pPageView, pNewObject );
344 pView->EndUndo();
350 Invalidate();
353 void ScGraphicShell::GetChangePictureState(SfxItemSet &rSet)
355 ScDrawView* pView = GetViewData()->GetScDrawView();
356 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
357 bool bEnable = false;
358 if( rMarkList.GetMarkCount() == 1 )
360 SdrObject* pObj = rMarkList.GetMark( 0 )->GetMarkedSdrObj();
362 if( dynamic_cast<const SdrGrafObj*>( pObj) && ( static_cast<SdrGrafObj*>(pObj)->GetGraphicType() == GraphicType::Bitmap ) )
363 bEnable = true;
366 if( !bEnable )
367 rSet.DisableItem( SID_CHANGE_PICTURE );
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */