Add a comment to clarify what kind of inputs the class handles
[LibreOffice.git] / sw / source / uibase / shells / grfsh.cxx
blobc508b0d4e86c16433188f82ab4c86e08ab1b1884
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 <cmdid.h>
21 #include <hintids.hxx>
22 #include <tools/urlobj.hxx>
23 #include <tools/UnitConversion.hxx>
24 #include <svl/stritem.hxx>
25 #include <svl/whiter.hxx>
26 #include <svl/urihelper.hxx>
27 #include <sfx2/bindings.hxx>
28 #include <sfx2/docfile.hxx>
29 #include <sfx2/objface.hxx>
30 #include <sfx2/viewfrm.hxx>
31 #include <editeng/sizeitem.hxx>
32 #include <sfx2/request.hxx>
33 #include <vcl/EnumContext.hxx>
34 #include <sfx2/htmlmode.hxx>
35 #include <svx/svdview.hxx>
36 #include <editeng/brushitem.hxx>
37 #include <svx/grfflt.hxx>
38 #include <svx/compressgraphicdialog.hxx>
39 #include <svx/tbxcolor.hxx>
40 #include <svx/sdangitm.hxx>
41 #include <osl/diagnose.h>
42 #include <drawdoc.hxx>
43 #include <view.hxx>
44 #include <wrtsh.hxx>
45 #include <viewopt.hxx>
46 #include <swmodule.hxx>
47 #include <swundo.hxx>
48 #include <uitool.hxx>
49 #include <docsh.hxx>
50 #include <grfsh.hxx>
51 #include <frmmgr.hxx>
52 #include <frmfmt.hxx>
53 #include <grfatr.hxx>
54 #include <swwait.hxx>
55 #include <svx/extedit.hxx>
56 #include <svx/graphichelper.hxx>
57 #include <doc.hxx>
58 #include <IDocumentDrawModelAccess.hxx>
59 #include <svx/drawitem.hxx>
60 #define ShellClass_SwGrfShell
62 #include <sfx2/msg.hxx>
63 #include <swslots.hxx>
64 #include <swabstdlg.hxx>
65 #include <unocrsr.hxx>
66 #include <flyfrm.hxx>
67 #include <memory>
69 constexpr OUString TOOLBOX_NAME = u"colorbar"_ustr;
71 class SwGrfShell::SwExternalToolEdit
72 : public ExternalToolEdit
74 private:
75 SwWrtShell *const m_pShell;
76 std::shared_ptr<SwUnoCursor> const m_pCursor;
78 public:
79 explicit SwExternalToolEdit(SwWrtShell *const pShell)
80 : m_pShell(pShell)
81 , m_pCursor( // need only Point, must point to SwGrfNode
82 pShell->GetDoc()->CreateUnoCursor(
83 *pShell->GetCurrentShellCursor().GetPoint()))
87 virtual void Update(Graphic & rGraphic) override
89 DBG_TESTSOLARMUTEX();
90 m_pShell->Push();
91 m_pShell->GetCurrentShellCursor().DeleteMark();
92 *m_pShell->GetCurrentShellCursor().GetPoint() = *m_pCursor->GetPoint();
93 m_pShell->ReRead(OUString(), OUString(), &rGraphic);
94 m_pShell->Pop();
98 SFX_IMPL_INTERFACE(SwGrfShell, SwBaseShell)
100 void SwGrfShell::InitInterface_Impl()
102 GetStaticInterface()->RegisterPopupMenu(u"graphic"_ustr);
104 GetStaticInterface()->RegisterObjectBar(SFX_OBJECTBAR_OBJECT, SfxVisibilityFlags::Invisible, ToolbarId::Grafik_Toolbox);
107 void SwGrfShell::Execute(SfxRequest &rReq)
109 SwWrtShell &rSh = GetShell();
111 sal_uInt16 nSlot = rReq.GetSlot();
112 switch(nSlot)
114 case SID_OBJECT_ROTATE:
116 // RotGrfFlyFrame: start rotation when possible
117 SdrView* pSdrView = rSh.GetDrawViewWithValidMarkList();
119 if(rSh.IsRotationOfSwGrfNodePossible() && pSdrView->IsRotateAllowed())
121 if(GetView().IsDrawRotate())
123 rSh.SetDragMode(SdrDragMode::Move);
125 else
127 rSh.SetDragMode(SdrDragMode::Rotate);
130 GetView().FlipDrawRotate();
133 break;
135 case SID_TWAIN_TRANSFER:
137 GetView().ExecuteScan( rReq );
138 break;
141 case SID_SAVE_GRAPHIC:
143 GraphicAttr aGraphicAttr;
144 rSh.GetGraphicAttr(aGraphicAttr);
146 short nState = RET_CANCEL;
147 if (aGraphicAttr != GraphicAttr()) // the image has been modified
149 weld::Window* pWin = GetView().GetFrameWeld();
150 if (pWin)
152 nState = GraphicHelper::HasToSaveTransformedImage(pWin);
155 else
157 nState = RET_NO;
160 if (nState == RET_YES)
162 const GraphicObject* pGraphicObj = rSh.GetGraphicObj();
163 if (pGraphicObj)
165 Graphic aGraphic = pGraphicObj->GetTransformedGraphic(pGraphicObj->GetPrefSize(), pGraphicObj->GetPrefMapMode(), aGraphicAttr);
166 OUString sGrfNm;
167 OUString sFilterNm;
168 rSh.GetGrfNms( &sGrfNm, &sFilterNm );
169 GraphicHelper::ExportGraphic(GetView().GetFrameWeld(), aGraphic, sGrfNm);
172 else if (nState == RET_NO)
174 const Graphic *pGraphic = rSh.GetGraphic();
175 if(nullptr != pGraphic)
177 OUString sGrfNm;
178 OUString sFilterNm;
179 rSh.GetGrfNms( &sGrfNm, &sFilterNm );
180 GraphicHelper::ExportGraphic(GetView().GetFrameWeld(), *pGraphic, sGrfNm);
184 break;
186 case SID_COMPRESS_GRAPHIC:
188 const Graphic* pGraphic = rSh.GetGraphic();
189 if( pGraphic )
191 Size aSize (
192 convertTwipToMm100(rSh.GetAnyCurRect(CurRectType::FlyEmbedded).SSize()));
194 SfxItemSetFixed<RES_GRFATR_MIRRORGRF, RES_GRFATR_CROPGRF> aSet( rSh.GetAttrPool() );
195 rSh.GetCurAttr( aSet );
196 SwMirrorGrf aMirror( aSet.Get(RES_GRFATR_MIRRORGRF) );
197 SwCropGrf aCrop( aSet.Get(RES_GRFATR_CROPGRF) );
199 tools::Rectangle aCropRectangle(
200 convertTwipToMm100(aCrop.GetLeft()),
201 convertTwipToMm100(aCrop.GetTop()),
202 convertTwipToMm100(aCrop.GetRight()),
203 convertTwipToMm100(aCrop.GetBottom()) );
205 Graphic aGraphic = *pGraphic;
207 CompressGraphicsDialog aDialog(GetView().GetFrameWeld(), std::move(aGraphic), aSize, aCropRectangle, GetView().GetViewFrame().GetBindings());
208 if (aDialog.run() == RET_OK)
210 rSh.StartAllAction();
211 rSh.StartUndo(SwUndoId::START);
212 tools::Rectangle aScaledCropedRectangle = aDialog.GetScaledCropRectangle();
214 aCrop.SetLeft( o3tl::toTwips( aScaledCropedRectangle.Left(), o3tl::Length::mm100 ));
215 aCrop.SetTop( o3tl::toTwips( aScaledCropedRectangle.Top(), o3tl::Length::mm100 ));
216 aCrop.SetRight( o3tl::toTwips( aScaledCropedRectangle.Right(), o3tl::Length::mm100 ));
217 aCrop.SetBottom( o3tl::toTwips( aScaledCropedRectangle.Bottom(), o3tl::Length::mm100 ));
219 Graphic aCompressedGraphic( aDialog.GetCompressedGraphic() );
220 rSh.ReRead(OUString(), OUString(), const_cast<const Graphic*>(&aCompressedGraphic));
222 rSh.SetAttrItem(aCrop);
223 rSh.SetAttrItem(aMirror);
225 rSh.EndUndo(SwUndoId::END);
226 rSh.EndAllAction();
230 break;
231 case SID_EXTERNAL_EDIT:
233 // When the graphic is selected to be opened via some external tool
234 // for advanced editing
235 GraphicObject const*const pGraphicObject(rSh.GetGraphicObj());
236 if(nullptr != pGraphicObject)
238 m_ExternalEdits.push_back(std::make_unique<SwExternalToolEdit>(
239 &rSh));
240 m_ExternalEdits.back()->Edit(pGraphicObject);
243 break;
244 case SID_CHANGE_PICTURE:
245 case SID_INSERT_GRAPHIC:
247 // #i123922# implement slot independent from the two below to
248 // bring up the insert graphic dialog and associated actions
249 SwView& rLclView = GetView();
250 rReq.SetReturnValue(SfxBoolItem(nSlot, rLclView.InsertGraphicDlg( rReq )));
251 break;
253 case FN_FORMAT_GRAFIC_DLG:
254 case FN_DRAW_WRAP_DLG:
256 SwFlyFrameAttrMgr aMgr( false, &rSh, rSh.IsFrameSelected() ?
257 Frmmgr_Type::NONE : Frmmgr_Type::GRF, nullptr);
258 const SwViewOption* pVOpt = rSh.GetViewOptions();
259 SwViewOption aUsrPref( *pVOpt );
261 SfxItemSetFixed<
262 RES_FRMATR_BEGIN, RES_GRFATR_CROPGRF,
263 // FillAttribute support:
264 XATTR_FILL_FIRST, XATTR_FILL_LAST,
265 SID_DOCFRAME, SID_DOCFRAME,
266 SID_REFERER, SID_REFERER,
267 SID_ATTR_BORDER_INNER, SID_ATTR_BORDER_INNER,
268 SID_ATTR_PAGE_SIZE, SID_ATTR_PAGE_SIZE, // 10051
269 // RotGrfFlyFrame: Need RotationAngle now
270 SID_ATTR_TRANSFORM_ANGLE, SID_ATTR_TRANSFORM_ANGLE, // 10095
271 // Items to hand over XPropertyList things like
272 // XColorList, XHatchList, XGradientList, and XBitmapList to
273 // the Area TabPage:
274 SID_COLOR_TABLE, SID_PATTERN_LIST, //10179
275 SID_HTML_MODE, SID_HTML_MODE, //10414
276 SID_ATTR_GRAF_KEEP_ZOOM, SID_ATTR_GRAF_KEEP_ZOOM, //10882
277 SID_ATTR_GRAF_FRMSIZE, SID_ATTR_GRAF_GRAPHIC, // 10884
278 // contains SID_ATTR_GRAF_FRMSIZE_PERCENT
279 FN_GET_PRINT_AREA, FN_GET_PRINT_AREA,
280 FN_PARAM_GRF_CONNECT, FN_PARAM_GRF_CONNECT,
281 FN_SET_FRM_NAME, FN_KEEP_ASPECT_RATIO,
282 FN_SET_FRM_ALT_NAME, FN_SET_FRM_ALT_NAME,
283 FN_UNO_DESCRIPTION, FN_UNO_DESCRIPTION> aSet( GetPool() );
285 // create needed items for XPropertyList entries from the DrawModel so that
286 // the Area TabPage can access them
287 const SwDrawModel* pDrawModel = rSh.GetView().GetDocShell()->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
289 aSet.Put(SvxColorListItem(pDrawModel->GetColorList(), SID_COLOR_TABLE));
290 aSet.Put(SvxGradientListItem(pDrawModel->GetGradientList(), SID_GRADIENT_LIST));
291 aSet.Put(SvxHatchListItem(pDrawModel->GetHatchList(), SID_HATCH_LIST));
292 aSet.Put(SvxBitmapListItem(pDrawModel->GetBitmapList(), SID_BITMAP_LIST));
293 aSet.Put(SvxPatternListItem(pDrawModel->GetPatternList(), SID_PATTERN_LIST));
295 sal_uInt16 nHtmlMode = ::GetHtmlMode(GetView().GetDocShell());
296 aSet.Put(SfxUInt16Item(SID_HTML_MODE, nHtmlMode));
297 FieldUnit eMetric = ::GetDfltMetric(0 != (nHtmlMode&HTMLMODE_ON));
298 SwModule* mod = SwModule::get();
299 mod->PutItem(SfxUInt16Item(SID_ATTR_METRIC, static_cast<sal_uInt16>(eMetric)));
301 const SwRect* pRect = &rSh.GetAnyCurRect(CurRectType::Page);
302 SwFormatFrameSize aFrameSize( SwFrameSize::Variable, pRect->Width(), pRect->Height());
303 aFrameSize.SetWhich( GetPool().GetWhichIDFromSlotID( SID_ATTR_PAGE_SIZE ) );
304 aSet.Put( aFrameSize );
306 aSet.Put(SfxStringItem(FN_SET_FRM_NAME, rSh.GetFlyName()));
307 aSet.Put(SfxStringItem(FN_UNO_DESCRIPTION, rSh.GetObjDescription()));
308 if ( nSlot == FN_FORMAT_GRAFIC_DLG )
310 // #i73249#
311 aSet.Put( SfxStringItem( FN_SET_FRM_ALT_NAME, rSh.GetObjTitle() ) );
314 pRect = &rSh.GetAnyCurRect(CurRectType::PagePrt);
315 aFrameSize.SetWidth( pRect->Width() );
316 aFrameSize.SetHeight( pRect->Height() );
317 aFrameSize.SetWhich( GetPool().GetWhichIDFromSlotID(FN_GET_PRINT_AREA) );
318 aSet.Put( aFrameSize );
320 aSet.Put( aMgr.GetAttrSet() );
321 SwFlyFrame* pFly = rSh.GetSelectedFlyFrame();
322 if (pFly)
324 // Work with the up to date layout size if possible.
325 SwFormatFrameSize aSize = aSet.Get(RES_FRM_SIZE);
326 aSize.SetWidth(pFly->getFrameArea().Width());
327 aSize.SetHeight(pFly->getFrameArea().Height());
328 aSet.Put(aSize);
330 aSet.SetParent( aMgr.GetAttrSet().GetParent() );
332 // At percentage values initialize size
333 SwFormatFrameSize aSizeCopy = aSet.Get(RES_FRM_SIZE);
334 if (aSizeCopy.GetWidthPercent() && aSizeCopy.GetWidthPercent() != SwFormatFrameSize::SYNCED)
335 aSizeCopy.SetWidth(rSh.GetAnyCurRect(CurRectType::FlyEmbedded).Width());
336 if (aSizeCopy.GetHeightPercent() && aSizeCopy.GetHeightPercent() != SwFormatFrameSize::SYNCED)
337 aSizeCopy.SetHeight(rSh.GetAnyCurRect(CurRectType::FlyEmbedded).Height());
338 // and now set the size for "external" tabpages
340 SvxSizeItem aSzItm( SID_ATTR_GRAF_FRMSIZE, aSizeCopy.GetSize() );
341 aSet.Put( aSzItm );
343 Size aSz( aSizeCopy.GetWidthPercent(), aSizeCopy.GetHeightPercent() );
344 if( SwFormatFrameSize::SYNCED == aSz.Width() ) aSz.setWidth( 0 );
345 if( SwFormatFrameSize::SYNCED == aSz.Height() ) aSz.setHeight( 0 );
347 aSzItm.SetSize( aSz );
348 aSzItm.SetWhich( SID_ATTR_GRAF_FRMSIZE_PERCENT );
349 aSet.Put( aSzItm );
352 OUString sGrfNm;
353 OUString sFilterNm;
354 rSh.GetGrfNms( &sGrfNm, &sFilterNm );
355 if( !sGrfNm.isEmpty() )
357 aSet.Put( SvxBrushItem( INetURLObject::decode( sGrfNm,
358 INetURLObject::DecodeMechanism::Unambiguous ),
359 sFilterNm, GPOS_LT,
360 SID_ATTR_GRAF_GRAPHIC ));
362 else
364 // #119353# - robust
365 const GraphicObject* pGrfObj = rSh.GetGraphicObj();
366 if ( pGrfObj )
368 aSet.Put( SvxBrushItem( *pGrfObj, GPOS_LT,
369 SID_ATTR_GRAF_GRAPHIC ) );
372 aSet.Put( SfxBoolItem( FN_PARAM_GRF_CONNECT, !sGrfNm.isEmpty() ) );
374 // get Mirror and Crop
376 SfxItemSetFixed<RES_GRFATR_MIRRORGRF, RES_GRFATR_CROPGRF> aTmpSet( rSh.GetAttrPool() );
378 rSh.GetCurAttr( aTmpSet );
379 aSet.Put( aTmpSet );
382 aSet.Put(SfxBoolItem(FN_KEEP_ASPECT_RATIO, aUsrPref.IsKeepRatio()));
383 aSet.Put(SfxBoolItem( SID_ATTR_GRAF_KEEP_ZOOM, aUsrPref.IsGrfKeepZoom()));
385 aSet.Put(SfxFrameItem( SID_DOCFRAME, &GetView().GetViewFrame().GetFrame()));
387 SfxObjectShell * sh = rSh.GetDoc()->GetPersist();
388 if (sh != nullptr && sh->HasName())
390 aSet.Put(
391 SfxStringItem(SID_REFERER, sh->GetMedium()->GetName()));
394 Size aUnrotatedSize;
395 Degree10 nCurrentRotation;
396 { // RotGrfFlyFrame: Add current RotationAngle value, convert from
397 // RES_GRFATR_ROTATION to SID_ATTR_TRANSFORM_ANGLE. Do not forget to
398 // convert from 10th degrees to 100th degrees
399 SfxItemSetFixed<RES_GRFATR_ROTATION, RES_GRFATR_ROTATION> aTmpSet( rSh.GetAttrPool() );
400 rSh.GetCurAttr( aTmpSet );
401 const SwRotationGrf& rRotation = aTmpSet.Get(RES_GRFATR_ROTATION);
402 nCurrentRotation = rRotation.GetValue();
403 aUnrotatedSize = rRotation.GetUnrotatedSize();
404 aSet.Put(SdrAngleItem(SID_ATTR_TRANSFORM_ANGLE, to<Degree100>(nCurrentRotation)));
407 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
408 ScopedVclPtr<SfxAbstractTabDialog> pDlg(pFact->CreateFrameTabDialog(u"PictureDialog"_ustr,
409 GetView().GetViewFrame(),
410 GetView().GetFrameWeld(),
411 aSet, false));
412 if (nSlot == FN_DRAW_WRAP_DLG)
413 pDlg->SetCurPageId(u"wrap"_ustr);
415 if (pDlg->Execute() == RET_OK)
417 rSh.StartAllAction();
418 rSh.StartUndo(SwUndoId::START);
419 SfxItemSet* pSet = const_cast<SfxItemSet*>(pDlg->GetOutputItemSet());
420 rReq.Done(*pSet);
421 // change the 2 frmsize SizeItems to the correct SwFrameSizeItem
422 if( const SvxSizeItem* pSizeItem = pSet->GetItemIfSet(
423 SID_ATTR_GRAF_FRMSIZE, false ))
425 SwFormatFrameSize aSize;
426 const Size& rSz = pSizeItem->GetSize();
427 aSize.SetWidth( rSz.Width() );
428 aSize.SetHeight( rSz.Height() );
430 pSizeItem = pSet->GetItemIfSet( SID_ATTR_GRAF_FRMSIZE_PERCENT, false );
431 if( pSizeItem )
433 const Size& rRelativeSize = pSizeItem->GetSize();
434 aSize.SetWidthPercent( static_cast< sal_uInt8 >( rRelativeSize.Width() ) );
435 aSize.SetHeightPercent( static_cast< sal_uInt8 >( rRelativeSize.Height() ) );
437 pSet->Put( aSize );
440 // Templates AutoUpdate
441 SwFrameFormat* pFormat = rSh.GetSelectedFrameFormat();
442 if(pFormat && pFormat->IsAutoUpdateOnDirectFormat())
444 pFormat->SetFormatAttr(*pSet);
445 SfxItemSetFixed<
446 RES_FRM_SIZE, RES_FRM_SIZE,
447 RES_SURROUND, RES_ANCHOR> aShellSet( GetPool() );
448 aShellSet.Put(*pSet);
449 aMgr.SetAttrSet(aShellSet);
451 else
453 aMgr.SetAttrSet(*pSet);
455 aMgr.UpdateFlyFrame();
457 bool bApplyUsrPref = false;
458 if (const SfxBoolItem* pRatioItem = pSet->GetItemIfSet(
459 FN_KEEP_ASPECT_RATIO ))
461 aUsrPref.SetKeepRatio( pRatioItem->GetValue() );
462 bApplyUsrPref = true;
464 if( const SfxBoolItem* pZoomItem = pSet->GetItemIfSet(
465 SID_ATTR_GRAF_KEEP_ZOOM ))
467 aUsrPref.SetGrfKeepZoom( pZoomItem->GetValue() );
468 bApplyUsrPref = true;
471 if( bApplyUsrPref )
472 mod->ApplyUsrPref(aUsrPref, &GetView());
474 // and now set all the graphic attributes and other stuff
475 if( const SvxBrushItem* pGraphicBrushItem = pSet->GetItemIfSet(
476 SID_ATTR_GRAF_GRAPHIC ))
478 if( !pGraphicBrushItem->GetGraphicLink().isEmpty() )
479 sGrfNm = pGraphicBrushItem->GetGraphicLink();
480 else
481 sGrfNm.clear();
483 if( !pGraphicBrushItem->GetGraphicFilter().isEmpty() )
484 sFilterNm = pGraphicBrushItem->GetGraphicFilter();
485 else
486 sFilterNm.clear();
488 if( !sGrfNm.isEmpty() )
490 SwDocShell* pDocSh = GetView().GetDocShell();
491 SwWait aWait( *pDocSh, true );
492 SfxMedium* pMedium = pDocSh->GetMedium();
493 INetURLObject aAbs;
494 if( pMedium )
495 aAbs = pMedium->GetURLObject();
496 rSh.ReRead( URIHelper::SmartRel2Abs(
497 aAbs, sGrfNm,
498 URIHelper::GetMaybeFileHdl() ),
499 sFilterNm );
502 if ( const SfxStringItem* pNameItem = pSet->GetItemIfSet(
503 FN_SET_FRM_ALT_NAME ))
505 // #i73249#
506 rSh.SetObjTitle( pNameItem->GetValue() );
509 if ( const SfxStringItem* pDescriptionItem = pSet->GetItemIfSet(
510 FN_UNO_DESCRIPTION ))
511 rSh.SetObjDescription( pDescriptionItem->GetValue() );
513 // RotGrfFlyFrame: Get and process evtl. changed RotationAngle
514 if ( const SdrAngleItem* pAngleItem = pSet->GetItemIfSet(SID_ATTR_TRANSFORM_ANGLE, false ))
516 const Degree10 aNewRotation = to<Degree10>(pAngleItem->GetValue() % 36000_deg100);
518 // RotGrfFlyFrame: Possible rotation change here, SwFlyFrameAttrMgr aMgr is available
519 aMgr.SetRotation(nCurrentRotation, aNewRotation, aUnrotatedSize);
522 SfxItemSetFixed<RES_GRFATR_BEGIN, RES_GRFATR_END-1> aGrfSet( rSh.GetAttrPool() );
523 aGrfSet.Put( *pSet );
524 if( aGrfSet.Count() )
525 rSh.SetAttrSet( aGrfSet );
527 rSh.EndUndo(SwUndoId::END);
528 rSh.EndAllAction();
531 break;
533 case FN_GRAPHIC_MIRROR_ON_EVEN_PAGES:
535 SfxItemSetFixed<RES_GRFATR_MIRRORGRF, RES_GRFATR_MIRRORGRF> aSet(rSh.GetAttrPool());
536 rSh.GetCurAttr( aSet );
537 SwMirrorGrf aGrf(aSet.Get(RES_GRFATR_MIRRORGRF));
538 aGrf.SetGrfToggle(!aGrf.IsGrfToggle());
539 rSh.SetAttrItem(aGrf);
541 break;
543 case SID_OBJECT_CROP:
545 GraphicObject const *pGraphicObject = rSh.GetGraphicObj();
546 if (nullptr != pGraphicObject && SdrDragMode::Crop != rSh.GetDragMode()) {
547 rSh.StartCropImage();
550 break;
552 default:
553 OSL_ENSURE(false, "wrong dispatcher");
554 return;
558 void SwGrfShell::ExecAttr( SfxRequest const &rReq )
560 GraphicType nGrfType = GraphicType::NONE;
561 if (CNT_GRF == GetShell().GetCntType())
562 nGrfType = GetShell().GetGraphicType();
563 if (GraphicType::Bitmap == nGrfType ||
564 GraphicType::GdiMetafile == nGrfType)
566 SfxItemSetFixed<RES_GRFATR_BEGIN, RES_GRFATR_END -1> aGrfSet( GetShell().GetAttrPool() );
567 const SfxItemSet *pArgs = rReq.GetArgs();
568 const SfxPoolItem* pItem;
569 sal_uInt16 nSlot = rReq.GetSlot();
570 if( !pArgs || SfxItemState::SET != pArgs->GetItemState( nSlot, false, &pItem ))
571 pItem = nullptr;
573 switch( nSlot )
575 case SID_FLIP_VERTICAL:
576 case SID_FLIP_HORIZONTAL:
578 GetShell().GetCurAttr( aGrfSet );
579 SwMirrorGrf aMirror( aGrfSet.Get( RES_GRFATR_MIRRORGRF ) );
580 MirrorGraph nMirror = aMirror.GetValue();
581 if ( nSlot==SID_FLIP_HORIZONTAL )
582 switch( nMirror )
584 case MirrorGraph::Dont: nMirror = MirrorGraph::Vertical;
585 break;
586 case MirrorGraph::Horizontal: nMirror = MirrorGraph::Both;
587 break;
588 case MirrorGraph::Vertical: nMirror = MirrorGraph::Dont;
589 break;
590 case MirrorGraph::Both: nMirror = MirrorGraph::Horizontal;
591 break;
593 else
594 switch( nMirror )
596 case MirrorGraph::Dont: nMirror = MirrorGraph::Horizontal;
597 break;
598 case MirrorGraph::Vertical: nMirror = MirrorGraph::Both;
599 break;
600 case MirrorGraph::Horizontal: nMirror = MirrorGraph::Dont;
601 break;
602 case MirrorGraph::Both: nMirror = MirrorGraph::Vertical;
603 break;
605 aMirror.SetValue( nMirror );
606 aGrfSet.ClearItem();
607 aGrfSet.Put( aMirror );
609 break;
611 case SID_ATTR_GRAF_LUMINANCE:
612 if( pItem )
613 aGrfSet.Put( SwLuminanceGrf(
614 static_cast<const SfxInt16Item*>(pItem)->GetValue() ));
615 break;
617 case SID_ATTR_GRAF_CONTRAST:
618 if( pItem )
619 aGrfSet.Put( SwContrastGrf(
620 static_cast<const SfxInt16Item*>(pItem)->GetValue() ));
621 break;
623 case SID_ATTR_GRAF_RED:
624 if( pItem )
625 aGrfSet.Put( SwChannelRGrf(
626 static_cast<const SfxInt16Item*>(pItem)->GetValue() ));
627 break;
629 case SID_ATTR_GRAF_GREEN:
630 if( pItem )
631 aGrfSet.Put( SwChannelGGrf(
632 static_cast<const SfxInt16Item*>(pItem)->GetValue() ));
633 break;
635 case SID_ATTR_GRAF_BLUE:
636 if( pItem )
637 aGrfSet.Put( SwChannelBGrf(
638 static_cast<const SfxInt16Item*>(pItem)->GetValue() ));
639 break;
641 case SID_ATTR_GRAF_GAMMA:
642 if( pItem )
644 double fVal = static_cast<const SfxUInt32Item*>(pItem)->GetValue();
645 aGrfSet.Put( SwGammaGrf(fVal/100. ));
647 break;
649 case SID_ATTR_GRAF_TRANSPARENCE:
650 if( pItem )
651 aGrfSet.Put( SwTransparencyGrf(
652 static_cast< sal_Int8 >( static_cast<const SfxUInt16Item*>(pItem )->GetValue() ) ) );
653 break;
655 case SID_ATTR_GRAF_INVERT:
656 if( pItem )
657 aGrfSet.Put( SwInvertGrf(
658 static_cast<const SfxBoolItem*>(pItem)->GetValue() ));
659 break;
661 case SID_ATTR_GRAF_MODE:
662 if( pItem )
663 aGrfSet.Put( SwDrawModeGrf(
664 static_cast<GraphicDrawMode>(static_cast<const SfxUInt16Item*>(pItem)->GetValue()) ));
665 break;
667 case SID_COLOR_SETTINGS:
669 svx::ToolboxAccess aToolboxAccess( TOOLBOX_NAME );
670 aToolboxAccess.toggleToolbox();
671 break;
674 case SID_GRFFILTER:
675 case SID_GRFFILTER_INVERT:
676 case SID_GRFFILTER_SMOOTH:
677 case SID_GRFFILTER_SHARPEN:
678 case SID_GRFFILTER_REMOVENOISE:
679 case SID_GRFFILTER_SOBEL:
680 case SID_GRFFILTER_MOSAIC:
681 case SID_GRFFILTER_EMBOSS:
682 case SID_GRFFILTER_POSTER:
683 case SID_GRFFILTER_POPART:
684 case SID_GRFFILTER_SEPIA:
685 case SID_GRFFILTER_SOLARIZE:
686 if( GraphicType::Bitmap == nGrfType )
688 // #119353# - robust
689 const GraphicObject* pFilterObj( GetShell().GetGraphicObj() );
690 if ( pFilterObj )
692 SvxGraphicFilter::ExecuteGrfFilterSlot( rReq, *pFilterObj,
693 [this] (GraphicObject aFilteredObject) -> void
695 GetShell().ReRead( OUString(), OUString(),
696 &aFilteredObject.GetGraphic() );
700 break;
702 default:
703 OSL_ENSURE(false, "wrong dispatcher");
706 if( aGrfSet.Count() )
707 GetShell().SetAttrSet( aGrfSet );
709 GetView().GetViewFrame().GetBindings().Invalidate(rReq.GetSlot());
712 void SwGrfShell::GetAttrState(SfxItemSet &rSet)
714 SwWrtShell &rSh = GetShell();
715 SfxItemSet aCoreSet( GetPool(), aNoTextNodeSetRange );
716 rSh.GetCurAttr( aCoreSet );
717 bool bParentCntProt = FlyProtectFlags::NONE != rSh.IsSelObjProtected( FlyProtectFlags::Content|FlyProtectFlags::Parent );
718 bool bIsGrfContent = CNT_GRF == GetShell().GetCntType();
720 SetGetStateSet( &rSet );
722 SfxWhichIter aIter( rSet );
723 sal_uInt16 nWhich = aIter.FirstWhich();
724 while( nWhich )
726 bool bDisable = bParentCntProt;
727 switch( nWhich )
729 case SID_OBJECT_ROTATE:
731 // RotGrfFlyFrame: steer rotation state
732 const bool bIsRotate(GetView().IsDrawRotate());
733 SdrView* pSdrView = rSh.GetDrawViewWithValidMarkList();
735 if(!bIsRotate && !pSdrView->IsRotateAllowed())
737 rSet.DisableItem(nWhich);
739 else
741 rSet.Put(SfxBoolItem(nWhich, bIsRotate));
744 break;
746 case SID_INSERT_GRAPHIC:
747 case FN_FORMAT_GRAFIC_DLG:
748 case SID_TWAIN_TRANSFER:
749 if( bParentCntProt || !bIsGrfContent )
750 bDisable = true;
751 else if ( nWhich == SID_INSERT_GRAPHIC
752 && rSh.CursorInsideInputField() )
754 bDisable = true;
756 break;
758 case SID_SAVE_GRAPHIC:
759 case SID_EXTERNAL_EDIT:
760 if( rSh.GetGraphicType() == GraphicType::NONE || GetObjectShell()->isExportLocked())
761 bDisable = true;
762 break;
764 case SID_COLOR_SETTINGS:
766 if ( bParentCntProt || !bIsGrfContent )
767 bDisable = true;
768 else
770 svx::ToolboxAccess aToolboxAccess( TOOLBOX_NAME );
771 rSet.Put( SfxBoolItem( nWhich, aToolboxAccess.isToolboxVisible() ) );
773 break;
776 case SID_FLIP_HORIZONTAL:
777 if( !bParentCntProt )
779 MirrorGraph nState = aCoreSet.Get(
780 RES_GRFATR_MIRRORGRF ).GetValue();
782 rSet.Put(SfxBoolItem( nWhich, nState == MirrorGraph::Vertical ||
783 nState == MirrorGraph::Both));
785 break;
787 case SID_FLIP_VERTICAL:
788 if( !bParentCntProt )
790 MirrorGraph nState = aCoreSet.GetItem<SwMirrorGrf>( RES_GRFATR_MIRRORGRF )->GetValue();
791 rSet.Put(SfxBoolItem( nWhich, nState == MirrorGraph::Horizontal ||
792 nState == MirrorGraph::Both));
794 break;
796 case SID_ATTR_GRAF_LUMINANCE:
797 if( !bParentCntProt )
798 rSet.Put( SfxInt16Item( nWhich,
799 aCoreSet.Get(RES_GRFATR_LUMINANCE).GetValue() ));
800 break;
802 case SID_ATTR_GRAF_CONTRAST:
803 if( !bParentCntProt )
804 rSet.Put( SfxInt16Item( nWhich,
805 aCoreSet.Get(RES_GRFATR_CONTRAST).GetValue() ));
806 break;
808 case SID_ATTR_GRAF_RED:
809 if( !bParentCntProt )
810 rSet.Put( SfxInt16Item( nWhich,
811 aCoreSet.Get(RES_GRFATR_CHANNELR).GetValue() ));
812 break;
814 case SID_ATTR_GRAF_GREEN:
815 if( !bParentCntProt )
816 rSet.Put( SfxInt16Item( nWhich,
817 aCoreSet.Get(RES_GRFATR_CHANNELG).GetValue() ));
818 break;
820 case SID_ATTR_GRAF_BLUE:
821 if( !bParentCntProt )
822 rSet.Put( SfxInt16Item( nWhich,
823 aCoreSet.Get(RES_GRFATR_CHANNELB).GetValue() ));
824 break;
826 case SID_ATTR_GRAF_GAMMA:
827 if( !bParentCntProt )
828 rSet.Put( SfxUInt32Item( nWhich, static_cast< sal_uInt32 >(
829 aCoreSet.Get( RES_GRFATR_GAMMA ).GetValue() * 100 ) ) );
830 break;
832 case SID_ATTR_GRAF_TRANSPARENCE:
833 if( !bParentCntProt )
835 // #119353# - robust
836 const GraphicObject* pGrafObj = rSh.GetGraphicObj();
837 if ( pGrafObj )
839 if( pGrafObj->IsAnimated() ||
840 GraphicType::GdiMetafile == pGrafObj->GetType() )
841 bDisable = true;
842 else
843 rSet.Put( SfxUInt16Item( nWhich,
844 aCoreSet.Get(RES_GRFATR_TRANSPARENCY).GetValue() ));
847 break;
849 case SID_ATTR_GRAF_INVERT:
850 if( !bParentCntProt )
851 rSet.Put( SfxBoolItem( nWhich,
852 aCoreSet.Get(RES_GRFATR_INVERT).GetValue() ));
853 break;
855 case SID_ATTR_GRAF_MODE:
856 if( !bParentCntProt )
857 rSet.Put( SfxUInt16Item( nWhich, static_cast<sal_uInt16>(aCoreSet.Get(RES_GRFATR_DRAWMODE).GetValue()) ));
858 break;
860 case SID_GRFFILTER:
861 case SID_GRFFILTER_INVERT:
862 case SID_GRFFILTER_SMOOTH:
863 case SID_GRFFILTER_SHARPEN:
864 case SID_GRFFILTER_REMOVENOISE:
865 case SID_GRFFILTER_SOBEL:
866 case SID_GRFFILTER_MOSAIC:
867 case SID_GRFFILTER_EMBOSS:
868 case SID_GRFFILTER_POSTER:
869 case SID_GRFFILTER_POPART:
870 case SID_GRFFILTER_SEPIA:
871 case SID_GRFFILTER_SOLARIZE:
873 if( bParentCntProt || !bIsGrfContent )
874 bDisable = true;
875 // #i59688# load graphic only if type is unknown
876 else
878 const GraphicType eGraphicType( rSh.GetGraphicType() );
879 if ( ( eGraphicType == GraphicType::NONE ||
880 eGraphicType == GraphicType::Default ) &&
881 rSh.IsLinkedGrfSwapOut() )
883 rSet.DisableItem( nWhich );
884 if( AddGrfUpdateSlot( nWhich ))
885 rSh.GetGraphic(false); // start the loading
887 else
889 bDisable = eGraphicType != GraphicType::Bitmap;
893 break;
895 case SID_OBJECT_CROP:
897 bDisable = FlyProtectFlags::NONE != rSh.IsSelObjProtected( FlyProtectFlags::Content|FlyProtectFlags::Parent );
898 if( rSh.GetGraphicType() == GraphicType::NONE )
899 bDisable = true;
901 break;
903 default:
904 bDisable = false;
907 if( bDisable )
908 rSet.DisableItem( nWhich );
909 nWhich = aIter.NextWhich();
911 SetGetStateSet( nullptr );
914 void SwGrfShell::ExecuteRotation(SfxRequest const &rReq)
916 // RotGrfFlyFrame: Modify rotation attribute instead of manipulating the graphic
917 Degree10 aRotation;
919 if (rReq.GetSlot() == SID_ROTATE_GRAPHIC_LEFT)
921 aRotation = 900_deg10;
923 else if (rReq.GetSlot() == SID_ROTATE_GRAPHIC_RIGHT)
925 aRotation = 2700_deg10;
927 else if (rReq.GetSlot() == SID_ROTATE_GRAPHIC_180)
929 aRotation = 1800_deg10;
932 if (rReq.GetSlot() != SID_ROTATE_GRAPHIC_RESET && 0_deg10 == aRotation)
933 return;
935 SwWrtShell& rShell = GetShell();
936 SfxItemSetFixed<RES_GRFATR_ROTATION, RES_GRFATR_ROTATION> aSet( rShell.GetAttrPool() );
937 rShell.GetCurAttr( aSet );
938 const SwRotationGrf& rRotation = aSet.Get(RES_GRFATR_ROTATION);
939 SwFlyFrameAttrMgr aMgr(false, &rShell, rShell.IsFrameSelected() ? Frmmgr_Type::NONE : Frmmgr_Type::GRF, nullptr);
941 // RotGrfFlyFrame: Possible rotation change here, SwFlyFrameAttrMgr aMgr is available
942 if (rReq.GetSlot() == SID_ROTATE_GRAPHIC_RESET)
944 aMgr.SetRotation(rRotation.GetValue(), 0_deg10, rRotation.GetUnrotatedSize());
946 else if(0_deg10 != aRotation)
948 const Degree10 aNewRotation((aRotation + rRotation.GetValue()) % 3600_deg10);
950 aMgr.SetRotation(rRotation.GetValue(), aNewRotation, rRotation.GetUnrotatedSize());
954 void SwGrfShell::GetAttrStateForRotation(SfxItemSet &rSet)
956 SwWrtShell& rShell = GetShell();
957 bool bIsParentContentProtected = FlyProtectFlags::NONE != rShell.IsSelObjProtected( FlyProtectFlags::Content|FlyProtectFlags::Parent );
959 SetGetStateSet( &rSet );
961 SfxWhichIter aIterator( rSet );
962 sal_uInt16 nWhich = aIterator.FirstWhich();
963 while( nWhich )
965 bool bDisable = bIsParentContentProtected;
966 switch( nWhich )
968 case SID_ROTATE_GRAPHIC_LEFT:
969 case SID_ROTATE_GRAPHIC_RIGHT:
970 case SID_ROTATE_GRAPHIC_180:
972 if( rShell.GetGraphicType() == GraphicType::NONE )
974 bDisable = true;
976 break;
978 case SID_ROTATE_GRAPHIC_RESET:
980 // RotGrfFlyFrame: disable when already no rotation
981 SfxItemSetFixed<RES_GRFATR_ROTATION, RES_GRFATR_ROTATION> aSet( rShell.GetAttrPool() );
982 rShell.GetCurAttr( aSet );
983 const SwRotationGrf& rRotation = aSet.Get(RES_GRFATR_ROTATION);
984 bDisable = (0_deg10 == rRotation.GetValue());
985 break;
987 case SID_ATTR_TRANSFORM_ANGLE:
989 // RotGrfFlyFrame: get rotation value from RES_GRFATR_ROTATION and copy to rSet as
990 // SID_ATTR_TRANSFORM_ANGLE, convert from 10th degrees to 100th degrees
991 SfxItemSetFixed<RES_GRFATR_ROTATION, RES_GRFATR_ROTATION> aSet( rShell.GetAttrPool() );
992 rShell.GetCurAttr( aSet );
993 const SwRotationGrf& rRotation = aSet.Get(RES_GRFATR_ROTATION);
994 rSet.Put(SdrAngleItem(SID_ATTR_TRANSFORM_ANGLE, to<Degree100>(rRotation.GetValue())));
995 break;
997 default:
998 bDisable = false;
1001 if( bDisable )
1002 rSet.DisableItem( nWhich );
1003 nWhich = aIterator.NextWhich();
1005 SetGetStateSet( nullptr );
1008 SwGrfShell::~SwGrfShell()
1012 SwGrfShell::SwGrfShell(SwView &_rView) :
1013 SwBaseShell(_rView)
1015 SetName(u"Graphic"_ustr);
1016 SfxShell::SetContextName(vcl::EnumContext::GetContextName(vcl::EnumContext::Context::Graphic));
1019 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */