android: Update app-specific/MIME type icons
[LibreOffice.git] / sd / source / ui / animations / CustomAnimationList.cxx
blob0128f210eb776c2ed1032d0e75e1f164c8979977
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/document/XActionLockable.hpp>
21 #include <com/sun/star/drawing/XDrawPage.hpp>
22 #include <com/sun/star/drawing/XShapes.hpp>
23 #include <com/sun/star/presentation/ShapeAnimationSubType.hpp>
24 #include <com/sun/star/presentation/EffectNodeType.hpp>
25 #include <com/sun/star/presentation/ParagraphTarget.hpp>
26 #include <com/sun/star/container/XEnumerationAccess.hpp>
27 #include <com/sun/star/presentation/EffectPresetClass.hpp>
28 #include <com/sun/star/presentation/EffectCommands.hpp>
29 #include <com/sun/star/text/XTextRange.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <comphelper/scopeguard.hxx>
32 #include <CustomAnimationList.hxx>
33 #include <CustomAnimationPreset.hxx>
34 #include <utility>
35 #include <vcl/commandevent.hxx>
36 #include <vcl/event.hxx>
37 #include <vcl/image.hxx>
38 #include <vcl/settings.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vcl/weldutils.hxx>
41 #include <tools/debug.hxx>
42 #include <tools/gen.hxx>
43 #include <comphelper/diagnose_ex.hxx>
45 #include <sdresid.hxx>
47 #include <strings.hrc>
48 #include <bitmaps.hlst>
50 #include <algorithm>
51 #include <memory>
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::presentation;
56 using ::com::sun::star::uno::UNO_QUERY;
57 using ::com::sun::star::uno::UNO_QUERY_THROW;
58 using ::com::sun::star::uno::Any;
59 using ::com::sun::star::uno::Reference;
60 using ::com::sun::star::uno::Exception;
61 using ::com::sun::star::uno::XInterface;
62 using ::com::sun::star::text::XTextRange;
63 using ::com::sun::star::drawing::XShape;
64 using ::com::sun::star::drawing::XShapes;
65 using ::com::sun::star::drawing::XDrawPage;
66 using ::com::sun::star::container::XChild;
67 using ::com::sun::star::container::XIndexAccess;
68 using ::com::sun::star::container::XEnumerationAccess;
69 using ::com::sun::star::container::XEnumeration;
70 using ::com::sun::star::beans::XPropertySet;
71 using ::com::sun::star::beans::XPropertySetInfo;
73 namespace sd {
75 // go recursively through all shapes in the given XShapes collection and return true as soon as the
76 // given shape is found. nIndex is incremented for each shape with the same shape type as the given
77 // shape is found until the given shape is found.
78 static bool getShapeIndex( const Reference< XShapes >& xShapes, const Reference< XShape >& xShape, sal_Int32& nIndex )
80 const sal_Int32 nCount = xShapes->getCount();
81 sal_Int32 n;
82 for( n = 0; n < nCount; n++ )
84 Reference< XShape > xChild;
85 xShapes->getByIndex( n ) >>= xChild;
86 if( xChild == xShape )
87 return true;
89 if( xChild->getShapeType() == xShape->getShapeType() )
90 nIndex++;
92 Reference< XShapes > xChildContainer( xChild, UNO_QUERY );
93 if( xChildContainer.is() )
95 if( getShapeIndex( xChildContainer, xShape, nIndex ) )
96 return true;
100 return false;
103 // returns the index of the shape type from the given shape
104 static sal_Int32 getShapeIndex( const Reference< XShape >& xShape )
106 Reference< XChild > xChild( xShape, UNO_QUERY );
107 Reference< XShapes > xPage;
109 while( xChild.is() && !xPage.is() )
111 Reference< XInterface > x( xChild->getParent() );
112 xChild.set( x, UNO_QUERY );
113 Reference< XDrawPage > xTestPage( x, UNO_QUERY );
114 if( xTestPage.is() )
115 xPage.set( x, UNO_QUERY );
118 sal_Int32 nIndex = 1;
120 if( xPage.is() && getShapeIndex( xPage, xShape, nIndex ) )
121 return nIndex;
122 else
123 return -1;
126 OUString getShapeDescription( const Reference< XShape >& xShape, bool bWithText )
128 OUString aDescription;
129 Reference< XPropertySet > xSet( xShape, UNO_QUERY );
130 bool bAppendIndex = true;
132 if(xSet.is()) try
134 Reference<XPropertySetInfo> xInfo(xSet->getPropertySetInfo());
135 if (xInfo.is())
137 static const OUStringLiteral aPropName1(u"Name");
138 if(xInfo->hasPropertyByName(aPropName1))
139 xSet->getPropertyValue(aPropName1) >>= aDescription;
141 bAppendIndex = aDescription.isEmpty();
143 static const OUStringLiteral aPropName2(u"UINameSingular");
144 if(xInfo->hasPropertyByName(aPropName2))
145 xSet->getPropertyValue(aPropName2) >>= aDescription;
148 catch( Exception& )
150 TOOLS_WARN_EXCEPTION( "sd", "sd::getShapeDescription()" );
153 if (bAppendIndex)
155 aDescription += " " + OUString::number(getShapeIndex(xShape));
158 if( bWithText )
160 Reference< XTextRange > xText( xShape, UNO_QUERY );
161 if( xText.is() )
163 OUString aText( xText->getString() );
164 if( !aText.isEmpty() )
166 aDescription += ": ";
168 aText = aText.replace( '\n', ' ' );
169 aText = aText.replace( '\r', ' ' );
171 aDescription += aText;
175 return aDescription;
178 static OUString getDescription( const Any& rTarget, bool bWithText )
180 OUString aDescription;
182 if( rTarget.getValueType() == ::cppu::UnoType<ParagraphTarget>::get() )
184 ParagraphTarget aParaTarget;
185 rTarget >>= aParaTarget;
187 css::uno::Reference<css::document::XActionLockable> xLockable(aParaTarget.Shape, css::uno::UNO_QUERY);
188 if (xLockable.is())
189 xLockable->addActionLock();
190 comphelper::ScopeGuard aGuard([&xLockable]()
192 if (xLockable.is())
193 xLockable->removeActionLock();
196 Reference< XEnumerationAccess > xText( aParaTarget.Shape, UNO_QUERY_THROW );
197 Reference< XEnumeration > xEnumeration( xText->createEnumeration(), css::uno::UNO_SET_THROW );
198 sal_Int32 nPara = aParaTarget.Paragraph;
200 while( xEnumeration->hasMoreElements() && nPara )
202 xEnumeration->nextElement();
203 nPara--;
206 DBG_ASSERT( xEnumeration->hasMoreElements(), "sd::CustomAnimationEffect::prepareText(), paragraph out of range!" );
208 if( xEnumeration->hasMoreElements() )
210 Reference< XTextRange > xParagraph;
211 xEnumeration->nextElement() >>= xParagraph;
213 if( xParagraph.is() )
214 aDescription = xParagraph->getString();
217 else
219 Reference< XShape > xShape;
220 rTarget >>= xShape;
221 if( xShape.is() )
222 aDescription = getShapeDescription( xShape, bWithText );
225 return aDescription;
228 class CustomAnimationListEntryItem
230 public:
231 CustomAnimationListEntryItem(OUString aDescription,
232 CustomAnimationEffectPtr pEffect);
233 const CustomAnimationEffectPtr& getEffect() const { return mpEffect; }
235 Size GetSize(const vcl::RenderContext& rRenderContext);
236 void Paint(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& rRect, bool bSelected);
237 void PaintEffect(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& rRect, bool bSelected);
238 void PaintTrigger(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& rRect);
240 private:
241 OUString msDescription;
242 OUString msEffectName;
243 CustomAnimationEffectPtr mpEffect;
245 public:
246 static const ::tools::Long nIconWidth = 19;
247 static const ::tools::Long nItemMinHeight = 38;
250 CustomAnimationListEntryItem::CustomAnimationListEntryItem(OUString aDescription, CustomAnimationEffectPtr pEffect)
251 : msDescription(std::move(aDescription))
252 , mpEffect(std::move(pEffect))
254 if (!mpEffect)
255 return;
256 switch (mpEffect->getPresetClass())
258 case EffectPresetClass::ENTRANCE:
259 msEffectName = SdResId(STR_CUSTOMANIMATION_ENTRANCE); break;
260 case EffectPresetClass::EXIT:
261 msEffectName = SdResId(STR_CUSTOMANIMATION_EXIT); break;
262 case EffectPresetClass::EMPHASIS:
263 msEffectName = SdResId(STR_CUSTOMANIMATION_EMPHASIS); break;
264 case EffectPresetClass::MOTIONPATH:
265 msEffectName = SdResId(STR_CUSTOMANIMATION_MOTION_PATHS); break;
266 default:
267 msEffectName = SdResId(STR_CUSTOMANIMATION_MISC); break;
269 msEffectName = msEffectName.replaceFirst( "%1" , CustomAnimationPresets::getCustomAnimationPresets().getUINameForPresetId(mpEffect->getPresetId()));
272 IMPL_STATIC_LINK(CustomAnimationList, CustomRenderHdl, weld::TreeView::render_args, aPayload, void)
274 vcl::RenderContext& rRenderContext = std::get<0>(aPayload);
275 const ::tools::Rectangle& rRect = std::get<1>(aPayload);
276 bool bSelected = std::get<2>(aPayload);
277 const OUString& rId = std::get<3>(aPayload);
279 CustomAnimationListEntryItem* pItem = weld::fromId<CustomAnimationListEntryItem*>(rId);
281 pItem->Paint(rRenderContext, rRect, bSelected);
284 IMPL_STATIC_LINK(CustomAnimationList, CustomGetSizeHdl, weld::TreeView::get_size_args, aPayload, Size)
286 vcl::RenderContext& rRenderContext = aPayload.first;
287 const OUString& rId = aPayload.second;
289 CustomAnimationListEntryItem* pItem = weld::fromId<CustomAnimationListEntryItem*>(rId);
290 if (!pItem)
291 return Size(CustomAnimationListEntryItem::nIconWidth, CustomAnimationListEntryItem::nItemMinHeight);
292 return pItem->GetSize(rRenderContext);
295 Size CustomAnimationListEntryItem::GetSize(const vcl::RenderContext& rRenderContext)
297 auto width = rRenderContext.GetTextWidth( msDescription ) + nIconWidth;
298 if (width < (rRenderContext.GetTextWidth( msEffectName ) + 2*nIconWidth))
299 width = rRenderContext.GetTextWidth( msEffectName ) + 2*nIconWidth;
301 Size aSize(width, rRenderContext.GetTextHeight());
302 if (aSize.Height() < nItemMinHeight)
303 aSize.setHeight(nItemMinHeight);
304 return aSize;
307 void CustomAnimationListEntryItem::PaintTrigger(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& rRect)
309 Size aSize(rRect.GetSize());
311 ::tools::Rectangle aOutRect(rRect);
313 // fill the background
314 Color aColor(rRenderContext.GetSettings().GetStyleSettings().GetDialogColor());
316 rRenderContext.Push();
317 rRenderContext.SetFillColor(aColor);
318 rRenderContext.SetLineColor();
319 rRenderContext.DrawRect(aOutRect);
321 // Erase the four corner pixels to make the rectangle appear rounded.
322 rRenderContext.SetLineColor(rRenderContext.GetSettings().GetStyleSettings().GetWindowColor());
323 rRenderContext.DrawPixel(aOutRect.TopLeft());
324 rRenderContext.DrawPixel(Point(aOutRect.Right(), aOutRect.Top()));
325 rRenderContext.DrawPixel(Point(aOutRect.Left(), aOutRect.Bottom()));
326 rRenderContext.DrawPixel(Point(aOutRect.Right(), aOutRect.Bottom()));
328 // draw the category title
330 int nVertBorder = ((aSize.Height() - rRenderContext.GetTextHeight()) >> 1);
331 int nHorzBorder = rRenderContext.LogicToPixel(Size(3, 3), MapMode(MapUnit::MapAppFont)).Width();
333 aOutRect.AdjustLeft(nHorzBorder );
334 aOutRect.AdjustRight( -nHorzBorder );
335 aOutRect.AdjustTop( nVertBorder );
336 aOutRect.AdjustBottom( -nVertBorder );
338 rRenderContext.DrawText(aOutRect, rRenderContext.GetEllipsisString(msDescription, aOutRect.GetWidth()));
339 rRenderContext.Pop();
342 void CustomAnimationListEntryItem::PaintEffect(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& rRect, bool bSelected)
344 rRenderContext.Push(vcl::PushFlags::TEXTCOLOR);
345 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
346 if (bSelected)
347 rRenderContext.SetTextColor(rStyleSettings.GetHighlightTextColor());
348 else
349 rRenderContext.SetTextColor(rStyleSettings.GetDialogTextColor());
351 Point aPos(rRect.TopLeft());
352 int nItemHeight = rRect.GetHeight();
354 sal_Int16 nNodeType = mpEffect->getNodeType();
355 if (nNodeType == EffectNodeType::ON_CLICK )
357 rRenderContext.DrawImage(aPos, Image(StockImage::Yes, BMP_CUSTOMANIMATION_ON_CLICK));
359 else if (nNodeType == EffectNodeType::AFTER_PREVIOUS)
361 rRenderContext.DrawImage(aPos, Image(StockImage::Yes, BMP_CUSTOMANIMATION_AFTER_PREVIOUS));
363 else if (nNodeType == EffectNodeType::WITH_PREVIOUS)
365 //FIXME With previous image not defined in CustomAnimation.src
368 aPos.AdjustX(nIconWidth);
370 //TODO, full width of widget ?
371 rRenderContext.DrawText(aPos, rRenderContext.GetEllipsisString(msDescription, rRect.GetWidth()));
373 aPos.AdjustY(nIconWidth);
375 OUString sImage;
376 switch (mpEffect->getPresetClass())
378 case EffectPresetClass::ENTRANCE:
379 sImage = BMP_CUSTOMANIMATION_ENTRANCE_EFFECT; break;
380 case EffectPresetClass::EXIT:
381 sImage = BMP_CUSTOMANIMATION_EXIT_EFFECT; break;
382 case EffectPresetClass::EMPHASIS:
383 sImage = BMP_CUSTOMANIMATION_EMPHASIS_EFFECT; break;
384 case EffectPresetClass::MOTIONPATH:
385 sImage = BMP_CUSTOMANIMATION_MOTION_PATH; break;
386 case EffectPresetClass::OLEACTION:
387 sImage = BMP_CUSTOMANIMATION_OLE; break;
388 case EffectPresetClass::MEDIACALL:
389 switch (mpEffect->getCommand())
391 case EffectCommands::TOGGLEPAUSE:
392 sImage = BMP_CUSTOMANIMATION_MEDIA_PAUSE; break;
393 case EffectCommands::STOP:
394 sImage = BMP_CUSTOMANIMATION_MEDIA_STOP; break;
395 case EffectCommands::PLAY:
396 default:
397 sImage = BMP_CUSTOMANIMATION_MEDIA_PLAY; break;
399 break;
400 default:
401 break;
404 if (!sImage.isEmpty())
406 Image aImage(StockImage::Yes, sImage);
407 Point aImagePos(aPos);
408 aImagePos.AdjustY((nItemHeight/2 - aImage.GetSizePixel().Height()) >> 1 );
409 rRenderContext.DrawImage(aImagePos, aImage);
412 aPos.AdjustX(nIconWidth );
413 aPos.AdjustY((nItemHeight/2 - rRenderContext.GetTextHeight()) >> 1 );
415 rRenderContext.DrawText(aPos, rRenderContext.GetEllipsisString(msEffectName, rRect.GetWidth()));
416 rRenderContext.Pop();
419 void CustomAnimationListEntryItem::Paint(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& rRect, bool bSelected)
421 if (mpEffect)
422 PaintEffect(rRenderContext, rRect, bSelected);
423 else
424 PaintTrigger(rRenderContext, rRect);
427 CustomAnimationList::CustomAnimationList(std::unique_ptr<weld::TreeView> xTreeView,
428 std::unique_ptr<weld::Label> xLabel,
429 std::unique_ptr<weld::Widget> xScrolledWindow)
430 : mxTreeView(std::move(xTreeView))
431 , maDropTargetHelper(*this)
432 , mxEmptyLabel(std::move(xLabel))
433 , mxEmptyLabelParent(std::move(xScrolledWindow))
434 , mbIgnorePaint(false)
435 , mpController(nullptr)
436 , mnLastGroupId(0)
437 , mnPostExpandEvent(nullptr)
438 , mnPostCollapseEvent(nullptr)
440 mxEmptyLabel->set_stack_background();
442 mxTreeView->set_selection_mode(SelectionMode::Multiple);
443 mxTreeView->connect_changed(LINK(this, CustomAnimationList, SelectHdl));
444 mxTreeView->connect_key_press(LINK(this, CustomAnimationList, KeyInputHdl));
445 mxTreeView->connect_popup_menu(LINK(this, CustomAnimationList, CommandHdl));
446 mxTreeView->connect_row_activated(LINK(this, CustomAnimationList, DoubleClickHdl));
447 mxTreeView->connect_expanding(LINK(this, CustomAnimationList, ExpandHdl));
448 mxTreeView->connect_collapsing(LINK(this, CustomAnimationList, CollapseHdl));
449 mxTreeView->connect_drag_begin(LINK(this, CustomAnimationList, DragBeginHdl));
450 mxTreeView->connect_custom_get_size(LINK(this, CustomAnimationList, CustomGetSizeHdl));
451 mxTreeView->connect_custom_render(LINK(this, CustomAnimationList, CustomRenderHdl));
452 mxTreeView->set_column_custom_renderer(0, true);
455 CustomAnimationListDropTarget::CustomAnimationListDropTarget(CustomAnimationList& rTreeView)
456 : DropTargetHelper(rTreeView.get_widget().get_drop_target())
457 , m_rTreeView(rTreeView)
461 sal_Int8 CustomAnimationListDropTarget::AcceptDrop(const AcceptDropEvent& rEvt)
463 sal_Int8 nAccept = m_rTreeView.AcceptDrop(rEvt);
465 if (nAccept != DND_ACTION_NONE)
467 // to enable the autoscroll when we're close to the edges
468 weld::TreeView& rWidget = m_rTreeView.get_widget();
469 rWidget.get_dest_row_at_pos(rEvt.maPosPixel, nullptr, true);
472 return nAccept;
475 sal_Int8 CustomAnimationListDropTarget::ExecuteDrop(const ExecuteDropEvent& rEvt)
477 return m_rTreeView.ExecuteDrop(rEvt);
480 // D'n'D #1: Record selected effects for drag'n'drop.
481 IMPL_LINK(CustomAnimationList, DragBeginHdl, bool&, rUnsetDragIcon, bool)
483 rUnsetDragIcon = false;
485 // Record which effects are selected:
486 // Since NextSelected(..) iterates through the selected items in the order they
487 // were selected, create a sorted list for simpler drag'n'drop algorithms.
488 mDndEffectsSelected.clear();
489 mxTreeView->selected_foreach([this](weld::TreeIter& rEntry){
490 mDndEffectsSelected.emplace_back(mxTreeView->make_iterator(&rEntry));
491 return false;
494 // Note: pEntry is the effect with focus (if multiple effects are selected)
495 mxDndEffectDragging = mxTreeView->make_iterator();
496 if (!mxTreeView->get_cursor(mxDndEffectDragging.get()))
497 mxDndEffectDragging.reset();
499 // Allow normal processing.
500 return false;
503 // D'n'D #3: Called each time mouse moves during drag
504 sal_Int8 CustomAnimationList::AcceptDrop( const AcceptDropEvent& rEvt )
506 sal_Int8 ret = DND_ACTION_NONE;
508 const bool bIsMove = DND_ACTION_MOVE == rEvt.mnAction;
509 if (mxDndEffectDragging && !rEvt.mbLeaving && bIsMove)
510 ret = DND_ACTION_MOVE;
511 return ret;
514 // D'n'D #5: Tell model to update effect order.
515 sal_Int8 CustomAnimationList::ExecuteDrop(const ExecuteDropEvent& rEvt)
517 std::unique_ptr<weld::TreeIter> xDndEffectInsertBefore(mxTreeView->make_iterator());
518 if (!mxTreeView->get_dest_row_at_pos(rEvt.maPosPixel, xDndEffectInsertBefore.get(), true))
519 xDndEffectInsertBefore.reset();
521 const bool bMovingEffect = ( mxDndEffectDragging != nullptr );
522 const bool bMoveNotSelf = !xDndEffectInsertBefore || (mxDndEffectDragging && mxTreeView->iter_compare(*xDndEffectInsertBefore, *mxDndEffectDragging) != 0);
523 const bool bHaveSequence(mpMainSequence);
525 if( bMovingEffect && bMoveNotSelf && bHaveSequence )
527 CustomAnimationListEntryItem* pTarget = xDndEffectInsertBefore ?
528 weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xDndEffectInsertBefore)) :
529 nullptr;
531 // Build list of effects
532 std::vector< CustomAnimationEffectPtr > aEffects;
533 for( const auto &pEntry : mDndEffectsSelected )
535 CustomAnimationListEntryItem* pCustomAnimationEffect = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*pEntry));
536 aEffects.push_back(pCustomAnimationEffect->getEffect());
539 // Callback to observer to have it update the model.
540 // If pTarget is null, pass nullptr to indicate end of list.
541 mpController->onDragNDropComplete(
542 std::move(aEffects),
543 pTarget ? pTarget->getEffect() : nullptr );
545 // Reset selection
546 mxTreeView->select(*mxDndEffectDragging);
547 Select();
550 // NOTE: Don't call default handler because all required
551 // move operations have been completed here to update the model.
552 return DND_ACTION_NONE;
555 CustomAnimationList::~CustomAnimationList()
557 if (mnPostExpandEvent)
559 Application::RemoveUserEvent(mnPostExpandEvent);
560 mnPostExpandEvent = nullptr;
563 if (mnPostCollapseEvent)
565 Application::RemoveUserEvent(mnPostCollapseEvent);
566 mnPostCollapseEvent = nullptr;
569 if( mpMainSequence )
570 mpMainSequence->removeListener( this );
572 clear();
575 IMPL_LINK(CustomAnimationList, KeyInputHdl, const KeyEvent&, rKEvt, bool)
577 const int nKeyCode = rKEvt.GetKeyCode().GetCode();
578 switch (nKeyCode)
580 case KEY_DELETE:
581 mpController->onContextMenu("remove");
582 return true;
583 case KEY_INSERT:
584 mpController->onContextMenu("create");
585 return true;
586 case KEY_SPACE:
588 std::unique_ptr<weld::TreeIter> xEntry = mxTreeView->make_iterator();
589 if (mxTreeView->get_cursor(xEntry.get()))
591 auto aRect = mxTreeView->get_row_area(*xEntry);
592 const Point aPos(aRect.getOpenWidth() / 2, aRect.getOpenHeight() / 2);
593 const CommandEvent aCEvt(aPos, CommandEventId::ContextMenu);
594 CommandHdl(aCEvt);
595 return true;
599 return false;
602 /** selects or deselects the given effect.
603 Selections of other effects are not changed */
604 void CustomAnimationList::select( const CustomAnimationEffectPtr& pEffect )
606 CustomAnimationListEntryItem* pEntry = nullptr;
608 std::unique_ptr<weld::TreeIter> xEntry = mxTreeView->make_iterator();
609 if (mxTreeView->get_iter_first(*xEntry))
613 CustomAnimationListEntryItem* pTestEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xEntry));
614 if (pTestEntry->getEffect() == pEffect)
616 mxTreeView->select(*xEntry);
617 mxTreeView->scroll_to_row(*xEntry);
618 pEntry = pTestEntry;
619 break;
621 } while (mxTreeView->iter_next(*xEntry));
624 if( !pEntry )
626 append( pEffect );
627 select( pEffect );
631 void CustomAnimationList::clear()
633 mxEntries.clear();
634 mxTreeView->clear();
636 mxEmptyLabelParent->show();
637 mxTreeView->hide();
639 mxLastParentEntry.reset();
640 mxLastTargetShape = nullptr;
643 void CustomAnimationList::update( const MainSequencePtr& pMainSequence )
645 if( mpMainSequence )
646 mpMainSequence->removeListener( this );
648 mpMainSequence = pMainSequence;
649 update();
651 if( mpMainSequence )
652 mpMainSequence->addListener( this );
655 struct stl_append_effect_func
657 explicit stl_append_effect_func( CustomAnimationList& rList ) : mrList( rList ) {}
658 void operator()(const CustomAnimationEffectPtr& pEffect);
659 CustomAnimationList& mrList;
662 void stl_append_effect_func::operator()(const CustomAnimationEffectPtr& pEffect)
664 mrList.append( pEffect );
667 void CustomAnimationList::update()
669 mbIgnorePaint = true;
671 std::vector< CustomAnimationEffectPtr > aVisible;
672 std::vector< CustomAnimationEffectPtr > aSelected;
673 CustomAnimationEffectPtr aCurrent;
675 CustomAnimationEffectPtr pFirstSelEffect;
676 CustomAnimationEffectPtr pLastSelEffect;
677 ::tools::Long nFirstVis = -1;
678 ::tools::Long nLastVis = -1;
679 ::tools::Long nFirstSelOld = -1;
680 ::tools::Long nLastSelOld = -1;
682 std::unique_ptr<weld::TreeIter> xEntry = mxTreeView->make_iterator();
684 if( mpMainSequence )
686 std::unique_ptr<weld::TreeIter> xLastSelectedEntry;
687 std::unique_ptr<weld::TreeIter> xLastVisibleEntry;
689 // save selection, current, and expand (visible) states
690 mxTreeView->all_foreach([this, &aVisible, &nFirstVis, &xLastVisibleEntry,
691 &aSelected, &nFirstSelOld, &pFirstSelEffect, &xLastSelectedEntry](weld::TreeIter& rEntry){
692 CustomAnimationListEntryItem* pEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(rEntry));
693 CustomAnimationEffectPtr pEffect(pEntry->getEffect());
694 if (pEffect)
696 if (weld::IsEntryVisible(*mxTreeView, rEntry))
698 aVisible.push_back(pEffect);
699 // save scroll position
700 if (nFirstVis == -1)
701 nFirstVis = weld::GetAbsPos(*mxTreeView, rEntry);
702 if (!xLastVisibleEntry)
703 xLastVisibleEntry = mxTreeView->make_iterator(&rEntry);
704 else
705 mxTreeView->copy_iterator(rEntry, *xLastVisibleEntry);
708 if (mxTreeView->is_selected(rEntry))
710 aSelected.push_back(pEffect);
711 if (nFirstSelOld == -1)
713 pFirstSelEffect = pEffect;
714 nFirstSelOld = weld::GetAbsPos(*mxTreeView, rEntry);
716 if (!xLastSelectedEntry)
717 xLastSelectedEntry = mxTreeView->make_iterator(&rEntry);
718 else
719 mxTreeView->copy_iterator(rEntry, *xLastSelectedEntry);
723 return false;
726 if (xLastSelectedEntry)
728 CustomAnimationListEntryItem* pEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xLastSelectedEntry));
729 pLastSelEffect = pEntry->getEffect();
730 nLastSelOld = weld::GetAbsPos(*mxTreeView, *xLastSelectedEntry);
733 if (xLastVisibleEntry)
734 nLastVis = weld::GetAbsPos(*mxTreeView, *xLastVisibleEntry);
736 if (mxTreeView->get_cursor(xEntry.get()))
738 CustomAnimationListEntryItem* pEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xEntry));
739 aCurrent = pEntry->getEffect();
743 // rebuild list
745 mxTreeView->freeze();
747 clear();
749 if (mpMainSequence)
751 std::for_each( mpMainSequence->getBegin(), mpMainSequence->getEnd(), stl_append_effect_func( *this ) );
752 mxLastParentEntry.reset();
754 auto rInteractiveSequenceVector = mpMainSequence->getInteractiveSequenceVector();
756 for (InteractiveSequencePtr const& pIS : rInteractiveSequenceVector)
758 Reference< XShape > xShape( pIS->getTriggerShape() );
759 if( xShape.is() )
761 OUString aDescription = SdResId(STR_CUSTOMANIMATION_TRIGGER) + ": " +
762 getShapeDescription( xShape, false );
764 mxEntries.emplace_back(std::make_unique<CustomAnimationListEntryItem>(aDescription, nullptr));
766 OUString sId(weld::toId(mxEntries.back().get()));
767 mxTreeView->insert(nullptr, -1, &aDescription, &sId, nullptr, nullptr, false, nullptr);
768 std::for_each( pIS->getBegin(), pIS->getEnd(), stl_append_effect_func( *this ) );
769 mxLastParentEntry.reset();
774 mxTreeView->thaw();
776 if (mxTreeView->n_children())
778 mxEmptyLabelParent->hide();
779 mxTreeView->show();
782 if (mpMainSequence)
784 ::tools::Long nFirstSelNew = -1;
785 ::tools::Long nLastSelNew = -1;
787 std::vector<std::unique_ptr<weld::TreeIter>> aNewSelection;
789 // restore selection state, expand state, and current-entry (under cursor)
790 if (mxTreeView->get_iter_first(*xEntry))
794 CustomAnimationListEntryItem* pEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xEntry));
796 CustomAnimationEffectPtr pEffect( pEntry->getEffect() );
797 if (pEffect)
799 // Any effects that were visible should still be visible, so expand their parents.
800 // (a previously expanded parent may have moved leaving a child to now be the new parent to expand)
801 if( std::find( aVisible.begin(), aVisible.end(), pEffect ) != aVisible.end() )
803 if (mxTreeView->get_iter_depth(*xEntry))
805 std::unique_ptr<weld::TreeIter> xParentEntry = mxTreeView->make_iterator(xEntry.get());
806 mxTreeView->iter_parent(*xParentEntry);
807 mxTreeView->expand_row(*xParentEntry);
811 if( std::find( aSelected.begin(), aSelected.end(), pEffect ) != aSelected.end() )
812 aNewSelection.emplace_back(mxTreeView->make_iterator(xEntry.get()));
814 // Restore the cursor, as it may deselect other effects wait until
815 // after the loop to reset the selection
816 if( pEffect == aCurrent )
817 mxTreeView->set_cursor(*xEntry);
819 if (pEffect == pFirstSelEffect)
820 nFirstSelNew = weld::GetAbsPos(*mxTreeView, *xEntry);
822 if (pEffect == pLastSelEffect)
823 nLastSelNew = weld::GetAbsPos(*mxTreeView, *xEntry);
825 } while (mxTreeView->iter_next(*xEntry));
828 // tdf#147032 unselect what previous set_cursor may have caused to get selected as a side-effect
829 mxTreeView->unselect_all();
830 for (const auto& rEntry : aNewSelection)
831 mxTreeView->select(*rEntry);
833 // Scroll to a selected entry, depending on where the selection moved.
834 const bool bMoved = nFirstSelNew != nFirstSelOld;
835 const bool bMovedUp = nFirstSelNew < nFirstSelOld;
836 const bool bMovedDown = nFirstSelNew > nFirstSelOld;
838 if( bMoved && nLastSelOld < nFirstVis && nLastSelNew < nFirstVis )
840 // The selection is above the visible area.
841 // Scroll up to show the last few selected entries.
842 if( nLastSelNew - (nLastVis - nFirstVis) > nFirstSelNew)
844 // The entries in the selection range can't fit in view.
845 // Scroll so the last selected entry is last in view.
846 mxTreeView->vadjustment_set_value(nLastSelNew - (nLastVis - nFirstVis));
848 else
849 mxTreeView->vadjustment_set_value(nFirstSelNew);
851 else if( bMoved && nFirstSelOld > nLastVis && nFirstSelNew > nLastVis )
853 // The selection is below the visible area.
854 // Scroll down to the first few selected entries.
855 mxTreeView->vadjustment_set_value(nFirstSelNew);
857 else if( bMovedUp && nFirstSelOld <= nFirstVis )
859 // A visible entry has moved up out of view; scroll up one.
860 mxTreeView->vadjustment_set_value(nFirstVis - 1);
862 else if( bMovedDown && nLastSelOld >= nLastVis )
864 // An entry has moved down out of view; scroll down one.
865 mxTreeView->vadjustment_set_value(nFirstVis + 1);
867 else if ( nFirstVis != -1 )
869 // The selection is still in view, or it hasn't moved.
870 mxTreeView->vadjustment_set_value(nFirstVis);
874 mbIgnorePaint = false;
876 Select();
879 void CustomAnimationList::append( CustomAnimationEffectPtr pEffect )
881 Any aTarget( pEffect->getTarget() );
882 if( !aTarget.hasValue() )
883 return;
887 // create a ui description
888 OUString aDescription = getDescription(aTarget, pEffect->getTargetSubItem() != ShapeAnimationSubType::ONLY_BACKGROUND);
890 std::unique_ptr<weld::TreeIter> xParentEntry;
892 Reference< XShape > xTargetShape( pEffect->getTargetShape() );
893 sal_Int32 nGroupId = pEffect->getGroupId();
895 // if this effect has the same target and group-id as the last root effect,
896 // the last root effect is also this effects parent
897 if (mxLastParentEntry && nGroupId != -1 && mxLastTargetShape == xTargetShape && mnLastGroupId == nGroupId)
898 xParentEntry = mxTreeView->make_iterator(mxLastParentEntry.get());
900 // create an entry for the effect
901 std::unique_ptr<weld::TreeIter> xEntry = mxTreeView->make_iterator();
903 mxEntries.emplace_back(std::make_unique<CustomAnimationListEntryItem>(aDescription, pEffect));
905 OUString sId(weld::toId(mxEntries.back().get()));
907 if (xParentEntry)
909 // add a subentry
910 mxTreeView->insert(xParentEntry.get(), -1, &aDescription, &sId, nullptr, nullptr, false, xEntry.get());
912 else
914 // add a root entry
915 mxTreeView->insert(nullptr, -1, &aDescription, &sId, nullptr, nullptr, false, xEntry.get());
917 // and the new root entry becomes the possible next group header
918 mxLastTargetShape = xTargetShape;
919 mnLastGroupId = nGroupId;
920 mxLastParentEntry = std::move(xEntry);
923 catch (const Exception&)
925 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationList::append()" );
929 static void selectShape(weld::TreeView* pTreeList, const Reference< XShape >& xShape )
931 std::unique_ptr<weld::TreeIter> xEntry = pTreeList->make_iterator();
932 if (!pTreeList->get_iter_first(*xEntry))
933 return;
935 bool bFirstEntry = true;
939 CustomAnimationListEntryItem* pEntry = weld::fromId<CustomAnimationListEntryItem*>(pTreeList->get_id(*xEntry));
940 CustomAnimationEffectPtr pEffect(pEntry->getEffect());
941 if (pEffect)
943 if (pEffect->getTarget() == xShape)
945 pTreeList->select(*xEntry);
946 if (bFirstEntry)
948 pTreeList->scroll_to_row(*xEntry);
949 bFirstEntry = false;
953 } while (pTreeList->iter_next(*xEntry));
956 void CustomAnimationList::onSelectionChanged(const Any& rSelection)
960 mxTreeView->unselect_all();
962 if (rSelection.hasValue())
964 Reference< XIndexAccess > xShapes(rSelection, UNO_QUERY);
965 if( xShapes.is() )
967 sal_Int32 nCount = xShapes->getCount();
968 sal_Int32 nIndex;
969 for( nIndex = 0; nIndex < nCount; nIndex++ )
971 Reference< XShape > xShape( xShapes->getByIndex( nIndex ), UNO_QUERY );
972 if( xShape.is() )
973 selectShape(mxTreeView.get(), xShape);
976 else
978 Reference< XShape > xShape(rSelection, UNO_QUERY);
979 if( xShape.is() )
980 selectShape(mxTreeView.get(), xShape);
984 Select();
986 catch( Exception& )
988 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationList::onSelectionChanged()" );
992 IMPL_LINK_NOARG(CustomAnimationList, SelectHdl, weld::TreeView&, void)
994 Select();
997 // Notify controller to refresh UI when we are notified of selection change from base class
998 void CustomAnimationList::Select()
1000 if( mbIgnorePaint )
1001 return;
1002 mpController->onSelect();
1005 IMPL_LINK_NOARG(CustomAnimationList, PostExpandHdl, void*, void)
1007 std::unique_ptr<weld::TreeIter> xEntry = mxTreeView->make_iterator();
1008 if (mxTreeView->get_selected(xEntry.get()))
1010 for (bool bChild = mxTreeView->iter_children(*xEntry); bChild; bChild = mxTreeView->iter_next_sibling(*xEntry))
1012 if (!mxTreeView->is_selected(*xEntry))
1013 mxTreeView->select(*xEntry);
1017 // Notify controller that selection has changed (it should update the UI)
1018 mpController->onSelect();
1020 mnPostExpandEvent = nullptr;
1023 IMPL_LINK(CustomAnimationList, ExpandHdl, const weld::TreeIter&, rParent, bool)
1025 // If expanded entry is selected, then select its children too afterwards.
1026 if (mxTreeView->is_selected(rParent) && !mnPostExpandEvent) {
1027 mnPostExpandEvent = Application::PostUserEvent(LINK(this, CustomAnimationList, PostExpandHdl));
1030 return true;
1033 IMPL_LINK_NOARG(CustomAnimationList, PostCollapseHdl, void*, void)
1035 // Deselect all entries as SvTreeListBox::Collapse selects the last
1036 // entry to have focus (or its parent), which is not desired
1037 mxTreeView->unselect_all();
1039 // Restore selection state for entries which are still visible
1040 for (const auto &pEntry : lastSelectedEntries)
1042 if (weld::IsEntryVisible(*mxTreeView, *pEntry))
1043 mxTreeView->select(*pEntry);
1046 lastSelectedEntries.clear();
1048 // Notify controller that selection has changed (it should update the UI)
1049 mpController->onSelect();
1051 mnPostCollapseEvent = nullptr;
1054 IMPL_LINK_NOARG(CustomAnimationList, CollapseHdl, const weld::TreeIter&, bool)
1056 if (!mnPostCollapseEvent)
1058 // weld::TreeView::collapse() discards multi-selection state
1059 // of list entries, so first save current selection state
1060 mxTreeView->selected_foreach([this](weld::TreeIter& rEntry){
1061 lastSelectedEntries.emplace_back(mxTreeView->make_iterator(&rEntry));
1062 return false;
1065 mnPostCollapseEvent = Application::PostUserEvent(LINK(this, CustomAnimationList, PostCollapseHdl));
1068 // Execute collapse on base class
1069 return true;
1072 bool CustomAnimationList::isExpanded( const CustomAnimationEffectPtr& pEffect ) const
1074 bool bExpanded = true; // we assume expanded by default
1076 std::unique_ptr<weld::TreeIter> xEntry = mxTreeView->make_iterator();
1077 if (mxTreeView->get_iter_first(*xEntry))
1081 CustomAnimationListEntryItem* pEntry =
1082 weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xEntry));
1083 if (pEntry->getEffect() == pEffect)
1085 if (mxTreeView->get_iter_depth(*xEntry)) // no parent, keep expanded default of true
1087 std::unique_ptr<weld::TreeIter> xParentEntry = mxTreeView->make_iterator(xEntry.get());
1088 if (mxTreeView->iter_parent(*xParentEntry))
1089 bExpanded = mxTreeView->get_row_expanded(*xParentEntry);
1091 break;
1093 } while (mxTreeView->iter_next(*xEntry));
1096 return bExpanded;
1099 bool CustomAnimationList::isVisible(const CustomAnimationEffectPtr& pEffect) const
1101 std::unique_ptr<weld::TreeIter> xEntry = mxTreeView->make_iterator();
1102 if (mxTreeView->get_iter_first(*xEntry))
1106 CustomAnimationListEntryItem* pTestEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xEntry));
1107 if (pTestEntry->getEffect() == pEffect)
1108 return weld::IsEntryVisible(*mxTreeView, *xEntry);
1109 } while (mxTreeView->iter_next(*xEntry));
1111 return true;
1114 EffectSequence CustomAnimationList::getSelection() const
1116 EffectSequence aSelection;
1118 mxTreeView->selected_foreach([this, &aSelection](weld::TreeIter& rEntry){
1119 CustomAnimationListEntryItem* pEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(rEntry));
1120 CustomAnimationEffectPtr pEffect(pEntry->getEffect());
1121 if (pEffect)
1122 aSelection.push_back(pEffect);
1124 // if the selected effect is not expanded and has children
1125 // we say that the children are automatically selected
1126 if (!mxTreeView->get_row_expanded(rEntry) && mxTreeView->iter_has_child(rEntry))
1128 std::unique_ptr<weld::TreeIter> xChild = mxTreeView->make_iterator(&rEntry);
1129 (void)mxTreeView->iter_children(*xChild);
1133 if (!mxTreeView->is_selected(*xChild))
1135 CustomAnimationListEntryItem* pChild = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(*xChild));
1136 const CustomAnimationEffectPtr& pChildEffect( pChild->getEffect() );
1137 if( pChildEffect )
1138 aSelection.push_back( pChildEffect );
1140 } while (mxTreeView->iter_next_sibling(*xChild));
1143 return false;
1146 return aSelection;
1149 IMPL_LINK_NOARG(CustomAnimationList, DoubleClickHdl, weld::TreeView&, bool)
1151 mpController->onDoubleClick();
1152 return false;
1155 IMPL_LINK(CustomAnimationList, CommandHdl, const CommandEvent&, rCEvt, bool)
1157 if (rCEvt.GetCommand() != CommandEventId::ContextMenu)
1158 return false;
1160 if (rCEvt.IsMouseEvent())
1162 ::Point aPos = rCEvt.GetMousePosPixel();
1163 std::unique_ptr<weld::TreeIter> xIter(mxTreeView->make_iterator());
1164 if (mxTreeView->get_dest_row_at_pos(aPos, xIter.get(), false) && !mxTreeView->is_selected(*xIter))
1166 mxTreeView->unselect_all();
1167 mxTreeView->set_cursor(*xIter);
1168 mxTreeView->select(*xIter);
1169 SelectHdl(*mxTreeView);
1173 if (!mxTreeView->get_selected(nullptr))
1174 return false;
1176 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(mxTreeView.get(), "modules/simpress/ui/effectmenu.ui"));
1177 std::unique_ptr<weld::Menu> xMenu = xBuilder->weld_menu("menu");
1179 sal_Int16 nNodeType = -1;
1180 sal_Int16 nEntries = 0;
1182 mxTreeView->selected_foreach([this, &nNodeType, &nEntries](weld::TreeIter& rEntry){
1183 CustomAnimationListEntryItem* pEntry = weld::fromId<CustomAnimationListEntryItem*>(mxTreeView->get_id(rEntry));
1184 CustomAnimationEffectPtr pEffect(pEntry->getEffect());
1186 nEntries++;
1187 if (pEffect)
1189 if( nNodeType == -1 )
1191 nNodeType = pEffect->getNodeType();
1193 else
1195 if( nNodeType != pEffect->getNodeType() )
1197 nNodeType = -1;
1198 return true;
1203 return false;
1206 xMenu->set_active("onclick", nNodeType == EffectNodeType::ON_CLICK);
1207 xMenu->set_active("withprev", nNodeType == EffectNodeType::WITH_PREVIOUS);
1208 xMenu->set_active("afterprev", nNodeType == EffectNodeType::AFTER_PREVIOUS);
1209 xMenu->set_sensitive("options", nEntries == 1);
1210 xMenu->set_sensitive("timing", nEntries == 1);
1212 OUString sCommand = xMenu->popup_at_rect(mxTreeView.get(), ::tools::Rectangle(rCEvt.GetMousePosPixel(), Size(1,1)));
1213 if (!sCommand.isEmpty())
1214 ExecuteContextMenuAction(sCommand);
1216 return true;
1219 void CustomAnimationList::ExecuteContextMenuAction(const OUString& rIdent)
1221 mpController->onContextMenu(rIdent);
1224 void CustomAnimationList::notify_change()
1226 update();
1227 mpController->onSelect();
1232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */