Bump version to 6.4-15
[LibreOffice.git] / svx / source / form / navigatortree.cxx
blob6950633e383b2de7a04ec3c1b15ab055a523617e
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 <memory>
21 #include <svx/dialmgr.hxx>
22 #include <svx/fmshell.hxx>
23 #include <svx/fmmodel.hxx>
24 #include <svx/fmpage.hxx>
25 #include <svx/svdpagv.hxx>
26 #include <svx/svditer.hxx>
28 #include <helpids.h>
29 #include <fmexpl.hxx>
30 #include <fmshimp.hxx>
31 #include <fmservs.hxx>
32 #include <fmundo.hxx>
33 #include <fmpgeimp.hxx>
34 #include <fmobj.hxx>
35 #include <fmprop.hxx>
36 #include <sal/log.hxx>
37 #include <vcl/svapp.hxx>
38 #include <sfx2/viewsh.hxx>
39 #include <sfx2/dispatch.hxx>
40 #include <sfx2/viewfrm.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <comphelper/property.hxx>
43 #include <comphelper/types.hxx>
44 #include <com/sun/star/form/FormComponentType.hpp>
45 #include <com/sun/star/sdb/CommandType.hpp>
46 #include <com/sun/star/beans/PropertyAttribute.hpp>
47 #include <com/sun/star/script/XEventAttacherManager.hpp>
48 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
49 #include <com/sun/star/datatransfer/XTransferable.hpp>
50 #include <com/sun/star/uno/XComponentContext.hpp>
51 #include <svx/sdrpaintwindow.hxx>
53 #include <svx/svxdlg.hxx>
54 #include <svx/strings.hrc>
55 #include <bitmaps.hlst>
56 #include <vcl/treelistentry.hxx>
57 #include <vcl/commandevent.hxx>
59 namespace svxform
63 #define DROP_ACTION_TIMER_INITIAL_TICKS 10
64 // Time until scroll starts
65 #define DROP_ACTION_TIMER_SCROLL_TICKS 3
66 // Time to scroll one line
67 #define DROP_ACTION_TIMER_TICK_BASE 10
68 // factor for both declarations (in ms)
70 #define EXPLORER_SYNC_DELAY 200
71 // Time (in ms) until explorer synchronizes the view after select or deselect
73 using namespace ::com::sun::star::uno;
74 using namespace ::com::sun::star::lang;
75 using namespace ::com::sun::star::beans;
76 using namespace ::com::sun::star::form;
77 using namespace ::com::sun::star::awt;
78 using namespace ::com::sun::star::container;
79 using namespace ::com::sun::star::script;
80 using namespace ::com::sun::star::datatransfer;
81 using namespace ::com::sun::star::datatransfer::clipboard;
82 using namespace ::com::sun::star::sdb;
85 // helper
88 typedef ::std::map< Reference< XInterface >, SdrObject* > MapModelToShape;
91 static void collectShapeModelMapping( SdrPage const * _pPage, MapModelToShape& _rMapping )
93 OSL_ENSURE( _pPage, "collectShapeModelMapping: invalid arg!" );
95 _rMapping.clear();
97 SdrObjListIter aIter( _pPage );
98 while ( aIter.IsMore() )
100 SdrObject* pSdrObject = aIter.Next();
101 FmFormObj* pFormObject = FmFormObj::GetFormObject( pSdrObject );
102 if ( !pFormObject )
103 continue;
105 Reference< XInterface > xNormalizedModel( pFormObject->GetUnoControlModel(), UNO_QUERY );
106 // note that this is normalized (i.e. queried for XInterface explicitly)
108 ::std::pair< MapModelToShape::iterator, bool > aPos =
109 _rMapping.emplace( xNormalizedModel, pSdrObject );
110 DBG_ASSERT( aPos.second, "collectShapeModelMapping: model was already existent!" );
111 // if this asserts, this would mean we have 2 shapes pointing to the same model
115 NavigatorTree::NavigatorTree( vcl::Window* pParent )
116 :SvTreeListBox( pParent, WB_HASBUTTONS|WB_HASLINES|WB_BORDER|WB_HSCROLL ) // #100258# OJ WB_HSCROLL added
117 ,m_aControlExchange(this)
118 ,m_pRootEntry(nullptr)
119 ,m_pEditEntry(nullptr)
120 ,nEditEvent(nullptr)
121 ,m_sdiState(SDI_DIRTY)
122 ,m_aTimerTriggered(-1,-1)
123 ,m_aDropActionType( DA_SCROLLUP )
124 ,m_nSelectLock(0)
125 ,m_nFormsSelected(0)
126 ,m_nControlsSelected(0)
127 ,m_nHiddenControls(0)
128 ,m_aTimerCounter( DROP_ACTION_TIMER_INITIAL_TICKS )
129 ,m_bDragDataDirty(false)
130 ,m_bPrevSelectionMixed(false)
131 ,m_bRootSelected(false)
132 ,m_bInitialUpdate(true)
133 ,m_bKeyboardCut( false )
135 SetHelpId( HID_FORM_NAVIGATOR );
137 SetNodeBitmaps(
138 Image(StockImage::Yes, RID_SVXBMP_COLLAPSEDNODE),
139 Image(StockImage::Yes, RID_SVXBMP_EXPANDEDNODE)
142 SetDragDropMode(DragDropMode::ALL);
143 EnableInplaceEditing( true );
144 SetSelectionMode(SelectionMode::Multiple);
146 m_pNavModel.reset(new NavigatorTreeModel());
147 Clear();
149 StartListening( *m_pNavModel );
151 m_aDropActionTimer.SetInvokeHandler(LINK(this, NavigatorTree, OnDropActionTimer));
153 m_aSynchronizeTimer.SetInvokeHandler(LINK(this, NavigatorTree, OnSynchronizeTimer));
154 SetSelectHdl(LINK(this, NavigatorTree, OnEntrySelDesel));
155 SetDeselectHdl(LINK(this, NavigatorTree, OnEntrySelDesel));
159 NavigatorTree::~NavigatorTree()
161 disposeOnce();
164 void NavigatorTree::dispose()
166 if( nEditEvent )
167 Application::RemoveUserEvent( nEditEvent );
169 if (m_aSynchronizeTimer.IsActive())
170 m_aSynchronizeTimer.Stop();
172 DBG_ASSERT(GetNavModel() != nullptr, "NavigatorTree::~NavigatorTree : unexpected : no ExplorerModel");
173 EndListening( *m_pNavModel );
174 Clear();
175 m_pNavModel.reset();
176 SvTreeListBox::dispose();
180 void NavigatorTree::Clear()
182 m_pNavModel->Clear();
186 void NavigatorTree::UpdateContent( FmFormShell* pFormShell )
188 if (m_bInitialUpdate)
190 GrabFocus();
191 m_bInitialUpdate = false;
194 FmFormShell* pOldShell = GetNavModel()->GetFormShell();
195 FmFormPage* pOldPage = GetNavModel()->GetFormPage();
196 FmFormPage* pNewPage = pFormShell ? pFormShell->GetCurPage() : nullptr;
198 if ((pOldShell != pFormShell) || (pOldPage != pNewPage))
200 // new shell during editing
201 if (IsEditingActive())
202 CancelTextEditing();
204 m_bDragDataDirty = true; // as a precaution, although I don't drag
206 GetNavModel()->UpdateContent( pFormShell );
208 // if there is a form, expand root
209 if (m_pRootEntry && !IsExpanded(m_pRootEntry))
210 Expand(m_pRootEntry);
211 // if there is EXACTLY ONE form, expand it too
212 if (m_pRootEntry)
214 SvTreeListEntry* pFirst = FirstChild(m_pRootEntry);
215 if (pFirst && !pFirst->NextSibling())
216 Expand(pFirst);
221 bool NavigatorTree::implAllowExchange( sal_Int8 _nAction, bool* _pHasNonHidden )
223 SvTreeListEntry* pCurEntry = GetCurEntry();
224 if (!pCurEntry)
225 return false;
227 // Information for AcceptDrop and Execute Drop
228 CollectSelectionData(SDI_ALL);
229 if (m_arrCurrentSelection.empty())
230 // nothing to do
231 return false;
233 // check whether there are only hidden controls
234 // I may add a format to pCtrlExch
235 bool bHasNonHidden = std::any_of(m_arrCurrentSelection.begin(), m_arrCurrentSelection.end(),
236 [](const SvTreeListEntry* pEntry) {
237 FmEntryData* pCurrent = static_cast< FmEntryData* >( pEntry->GetUserData() );
238 return !IsHiddenControl( pCurrent );
241 if ( bHasNonHidden && ( 0 == ( _nAction & DND_ACTION_MOVE ) ) )
242 // non-hidden controls need to be moved
243 return false;
245 if ( _pHasNonHidden )
246 *_pHasNonHidden = bHasNonHidden;
248 return true;
252 bool NavigatorTree::implPrepareExchange( sal_Int8 _nAction )
254 EndSelection();
256 bool bHasNonHidden = false;
257 if ( !implAllowExchange( _nAction, &bHasNonHidden ) )
258 return false;
260 m_aControlExchange.prepareDrag();
261 m_aControlExchange->setFocusEntry( GetCurEntry() );
263 for (const auto& rpEntry : m_arrCurrentSelection)
264 m_aControlExchange->addSelectedEntry(rpEntry);
266 m_aControlExchange->setFormsRoot( GetNavModel()->GetFormPage()->GetForms() );
267 m_aControlExchange->buildPathFormat( this, m_pRootEntry );
269 if (!bHasNonHidden)
271 // create a sequence
272 Sequence< Reference< XInterface > > seqIFaces(m_arrCurrentSelection.size());
273 Reference< XInterface >* pArray = seqIFaces.getArray();
274 for (const auto& rpEntry : m_arrCurrentSelection)
276 *pArray = static_cast< FmEntryData* >( rpEntry->GetUserData() )->GetElement();
277 ++pArray;
279 // and the new format
280 m_aControlExchange->addHiddenControlsFormat(seqIFaces);
283 m_bDragDataDirty = false;
284 return true;
288 void NavigatorTree::StartDrag( sal_Int8 /*nAction*/, const ::Point& /*rPosPixel*/ )
290 EndSelection();
292 if ( !implPrepareExchange( DND_ACTION_COPYMOVE ) )
293 // nothing to do or something went wrong
294 return;
296 // collected all possible formats for current situation, we can start now
297 m_aControlExchange.startDrag( DND_ACTION_COPYMOVE );
301 void NavigatorTree::Command( const CommandEvent& rEvt )
303 bool bHandled = false;
304 switch( rEvt.GetCommand() )
306 case CommandEventId::ContextMenu:
308 // Position of click
309 ::Point ptWhere;
310 if (rEvt.IsMouseEvent())
312 ptWhere = rEvt.GetMousePosPixel();
313 SvTreeListEntry* ptClickedOn = GetEntry(ptWhere);
314 if (ptClickedOn == nullptr)
315 break;
316 if ( !IsSelected(ptClickedOn) )
318 SelectAll(false);
319 Select(ptClickedOn);
320 SetCurEntry(ptClickedOn);
323 else
325 if (m_arrCurrentSelection.empty()) // only happens with context menu via keyboard
326 break;
328 SvTreeListEntry* pCurrent = GetCurEntry();
329 if (!pCurrent)
330 break;
331 ptWhere = GetEntryPosition(pCurrent);
334 // update my selection data
335 CollectSelectionData(SDI_ALL);
337 // if there is at least one no-root-entry and the root selected, I deselect root
338 if ( (m_arrCurrentSelection.size() > 1) && m_bRootSelected )
340 Select( m_pRootEntry, false );
341 SetCursor( *m_arrCurrentSelection.begin(), true);
343 bool bSingleSelection = (m_arrCurrentSelection.size() == 1);
346 DBG_ASSERT( (!m_arrCurrentSelection.empty()) || m_bRootSelected, "no entries selected" );
347 // shouldn't happen, because I would have selected one during call to IsSelected,
348 // if there was none before
351 // create menu
352 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
353 FmFormModel* pFormModel = pFormShell ? pFormShell->GetFormModel() : nullptr;
354 if( pFormShell && pFormModel )
356 VclBuilder aBuilder(nullptr, VclBuilderContainer::getUIRootDir(), "svx/ui/formnavimenu.ui", "");
357 VclPtr<PopupMenu> aContextMenu(aBuilder.get_menu("menu"));
358 const sal_uInt16 nNewId = aContextMenu->GetItemId("new");
359 PopupMenu* pSubMenuNew = aContextMenu->GetPopupMenu(nNewId);
361 // menu 'New' only exists, if only the root or only one form is selected
362 aContextMenu->EnableItem(nNewId, bSingleSelection && (m_nFormsSelected || m_bRootSelected));
364 // 'New'\'Form' under the same terms
365 const sal_uInt16 nFormId = pSubMenuNew->GetItemId("form");
366 pSubMenuNew->EnableItem(nFormId, bSingleSelection && (m_nFormsSelected || m_bRootSelected));
367 pSubMenuNew->SetItemImage(nFormId, Image(StockImage::Yes, RID_SVXBMP_FORM));
369 // 'New'\'hidden...', if exactly one form is selected
370 const sal_uInt16 nHiddenId = pSubMenuNew->GetItemId("hidden");
371 pSubMenuNew->EnableItem(nHiddenId, bSingleSelection && m_nFormsSelected);
372 pSubMenuNew->SetItemImage(nHiddenId, Image(StockImage::Yes, RID_SVXBMP_HIDDEN));
374 // 'Delete': everything which is not root can be removed
375 aContextMenu->EnableItem(aContextMenu->GetItemId("delete"), !m_bRootSelected);
377 // 'Cut', 'Copy' and 'Paste'
378 aContextMenu->EnableItem(aContextMenu->GetItemId("cut"), !m_bRootSelected && implAllowExchange(DND_ACTION_MOVE));
379 aContextMenu->EnableItem(aContextMenu->GetItemId("copy"), !m_bRootSelected && implAllowExchange(DND_ACTION_COPY));
380 aContextMenu->EnableItem(aContextMenu->GetItemId("paste"), implAcceptPaste());
382 // TabDialog, if exactly one form
383 aContextMenu->EnableItem(aContextMenu->GetItemId("taborder"), bSingleSelection && m_nFormsSelected);
385 const sal_uInt16 nBrowserId = aContextMenu->GetItemId("props");
386 // in XML forms, we don't allow for the properties of a form
387 // #i36484#
388 if (pFormShell->GetImpl()->isEnhancedForm_Lock() && !m_nControlsSelected)
389 aContextMenu->RemoveItem(aContextMenu->GetItemPos(nBrowserId));
391 // if the property browser is already open, we don't allow for the properties, too
392 if (pFormShell->GetImpl()->IsPropBrwOpen_Lock())
393 aContextMenu->RemoveItem(aContextMenu->GetItemPos(nBrowserId));
394 // and finally, if there's a mixed selection of forms and controls, disable the entry, too
395 else
396 aContextMenu->EnableItem(nBrowserId,
397 (m_nControlsSelected && !m_nFormsSelected) || (!m_nControlsSelected && m_nFormsSelected) );
399 // rename, if one element and no root
400 aContextMenu->EnableItem(aContextMenu->GetItemId("rename"), bSingleSelection && !m_bRootSelected);
402 // Readonly-entry is only for root
403 aContextMenu->EnableItem(aContextMenu->GetItemId("designmode"), m_bRootSelected);
404 // the same for automatic control focus
405 aContextMenu->EnableItem(aContextMenu->GetItemId("controlfocus"), m_bRootSelected);
407 std::unique_ptr<VclBuilder> xBuilder;
408 VclPtr<PopupMenu> xConversionMenu;
409 // ConvertTo-Slots are enabled, if one control is selected
410 // the corresponding slot is disabled
411 const sal_Int16 nChangeId = aContextMenu->GetItemId("change");
412 if (!m_bRootSelected && !m_nFormsSelected && (m_nControlsSelected == 1))
414 xBuilder = FmXFormShell::GetConversionMenu_Lock();
415 xConversionMenu = xBuilder->get_menu("menu");
416 aContextMenu->SetPopupMenu(nChangeId, xConversionMenu);
417 #if OSL_DEBUG_LEVEL > 0
418 FmControlData* pCurrent = static_cast<FmControlData*>((*m_arrCurrentSelection.begin())->GetUserData());
419 OSL_ENSURE( pFormShell->GetImpl()->isSolelySelected_Lock( pCurrent->GetFormComponent() ),
420 "NavigatorTree::Command: inconsistency between the navigator selection, and the selection as the shell knows it!" );
421 #endif
423 pFormShell->GetImpl()->checkControlConversionSlotsForCurrentSelection_Lock(*aContextMenu->GetPopupMenu(nChangeId));
425 else
426 aContextMenu->EnableItem(nChangeId, false );
428 // remove all disabled entries
429 aContextMenu->RemoveDisabledEntries(true, true);
431 // set OpenReadOnly
433 aContextMenu->CheckItem("designmode", pFormModel->GetOpenInDesignMode());
434 aContextMenu->CheckItem("controlfocus", pFormModel->GetAutoControlFocus());
436 aContextMenu->Execute(this, ptWhere);
437 OString sIdent;
438 if (xConversionMenu)
439 sIdent = xConversionMenu->GetCurItemIdent();
440 if (sIdent.isEmpty())
441 sIdent = pSubMenuNew->GetCurItemIdent();
442 if (sIdent.isEmpty())
443 sIdent = aContextMenu->GetCurItemIdent();
444 if (sIdent == "form")
446 OUString aStr(SvxResId(RID_STR_FORM));
447 OUString aUndoStr = SvxResId(RID_STR_UNDO_CONTAINER_INSERT).replaceAll("#", aStr);
449 pFormModel->BegUndo(aUndoStr);
450 // slot was only available, if there is only one selected entry,
451 // which is a root or a form
452 NewForm( *m_arrCurrentSelection.begin() );
453 pFormModel->EndUndo();
455 else if (sIdent == "hidden")
457 OUString aStr(SvxResId(RID_STR_CONTROL));
458 OUString aUndoStr = SvxResId(RID_STR_UNDO_CONTAINER_INSERT).replaceAll("#", aStr);
460 pFormModel->BegUndo(aUndoStr);
461 // slot was valid for (exactly) one selected form
462 OUString fControlName = FM_COMPONENT_HIDDEN;
463 NewControl( fControlName, *m_arrCurrentSelection.begin(), true );
464 pFormModel->EndUndo();
466 else if (sIdent == "cut")
467 doCut();
468 else if (sIdent == "copy")
469 doCopy();
470 else if (sIdent == "paste")
471 doPaste();
472 else if (sIdent == "delete")
473 DeleteSelection();
474 else if (sIdent == "taborder")
476 // this slot was effective for exactly one selected form
477 SvTreeListEntry* pSelectedForm = *m_arrCurrentSelection.begin();
478 DBG_ASSERT( IsFormEntry(pSelectedForm), "NavigatorTree::Command: This entry must be a FormEntry." );
480 FmFormData* pFormData = static_cast<FmFormData*>(pSelectedForm->GetUserData());
481 const Reference< XForm >& xForm( pFormData->GetFormIface());
483 Reference< XTabControllerModel > xTabController(xForm, UNO_QUERY);
484 if( !xTabController.is() )
485 break;
486 GetNavModel()->GetFormShell()->GetImpl()->ExecuteTabOrderDialog_Lock(xTabController);
488 else if (sIdent == "props")
489 ShowSelectionProperties(true);
490 else if (sIdent == "rename")
492 // only allowed for one no-root-entry
493 EditEntry( *m_arrCurrentSelection.begin() );
495 else if (sIdent == "designmode")
497 pFormModel->SetOpenInDesignMode( !pFormModel->GetOpenInDesignMode() );
498 pFormShell->GetViewShell()->GetViewFrame()->GetBindings().Invalidate(SID_FM_OPEN_READONLY);
500 else if (sIdent == "controlfocus")
502 pFormModel->SetAutoControlFocus( !pFormModel->GetAutoControlFocus() );
503 pFormShell->GetViewShell()->GetViewFrame()->GetBindings().Invalidate(SID_FM_AUTOCONTROLFOCUS);
505 else if (FmXFormShell::isControlConversionSlot(sIdent))
507 FmControlData* pCurrent = static_cast<FmControlData*>((*m_arrCurrentSelection.begin())->GetUserData());
508 if (pFormShell->GetImpl()->executeControlConversionSlot_Lock(pCurrent->GetFormComponent(), sIdent))
509 ShowSelectionProperties();
512 bHandled = true;
514 break;
515 default: break;
518 if (!bHandled)
519 SvTreeListBox::Command( rEvt );
523 SvTreeListEntry* NavigatorTree::FindEntry( FmEntryData* pEntryData )
525 if( !pEntryData ) return nullptr;
526 SvTreeListEntry* pCurEntry = First();
527 while( pCurEntry )
529 FmEntryData* pCurEntryData = static_cast<FmEntryData*>(pCurEntry->GetUserData());
530 if( pCurEntryData && pCurEntryData->IsEqualWithoutChildren(pEntryData) )
531 return pCurEntry;
533 pCurEntry = Next( pCurEntry );
536 return nullptr;
540 void NavigatorTree::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
542 if( dynamic_cast<const FmNavRemovedHint*>(&rHint) )
544 const FmNavRemovedHint* pRemovedHint = static_cast<const FmNavRemovedHint*>(&rHint);
545 FmEntryData* pEntryData = pRemovedHint->GetEntryData();
546 Remove( pEntryData );
549 else if( dynamic_cast<const FmNavInsertedHint*>(&rHint) )
551 const FmNavInsertedHint* pInsertedHint = static_cast<const FmNavInsertedHint*>(&rHint);
552 FmEntryData* pEntryData = pInsertedHint->GetEntryData();
553 sal_uInt32 nRelPos = pInsertedHint->GetRelPos();
554 Insert( pEntryData, nRelPos );
557 else if( dynamic_cast<const FmNavModelReplacedHint*>(&rHint) )
559 FmEntryData* pData = static_cast<const FmNavModelReplacedHint*>(&rHint)->GetEntryData();
560 SvTreeListEntry* pEntry = FindEntry( pData );
561 if (pEntry)
562 { // reset image
563 SetCollapsedEntryBmp( pEntry, pData->GetNormalImage() );
564 SetExpandedEntryBmp( pEntry, pData->GetNormalImage() );
568 else if( dynamic_cast<const FmNavNameChangedHint*>(&rHint) )
570 const FmNavNameChangedHint* pNameChangedHint = static_cast<const FmNavNameChangedHint*>(&rHint);
571 SvTreeListEntry* pEntry = FindEntry( pNameChangedHint->GetEntryData() );
572 SetEntryText( pEntry, pNameChangedHint->GetNewName() );
575 else if( dynamic_cast<const FmNavClearedHint*>(&rHint) )
577 SvTreeListBox::Clear();
579 // default-entry "Forms"
580 Image aRootImage(StockImage::Yes, RID_SVXBMP_FORMS);
581 m_pRootEntry = InsertEntry( SvxResId(RID_STR_FORMS), aRootImage, aRootImage,
582 nullptr, false, 0 );
584 else if (dynamic_cast<const FmNavRequestSelectHint*>(&rHint))
586 FmNavRequestSelectHint* pershHint = const_cast<FmNavRequestSelectHint*>(static_cast<const FmNavRequestSelectHint*>(&rHint));
587 FmEntryDataArray& arredToSelect = pershHint->GetItems();
588 SynchronizeSelection(arredToSelect);
590 if (pershHint->IsMixedSelection())
591 // in this case I deselect all, although the view had a mixed selection
592 // during next selection, I must adapt the navigator to the view
593 m_bPrevSelectionMixed = true;
598 SvTreeListEntry* NavigatorTree::Insert( FmEntryData* pEntryData, sal_uLong nRelPos )
601 // insert current entry
602 SvTreeListEntry* pParentEntry = FindEntry( pEntryData->GetParent() );
603 SvTreeListEntry* pNewEntry;
605 if( !pParentEntry )
606 pNewEntry = InsertEntry( pEntryData->GetText(),
607 pEntryData->GetNormalImage(), pEntryData->GetNormalImage(),
608 m_pRootEntry, false, nRelPos, pEntryData );
610 else
611 pNewEntry = InsertEntry( pEntryData->GetText(),
612 pEntryData->GetNormalImage(), pEntryData->GetNormalImage(),
613 pParentEntry, false, nRelPos, pEntryData );
616 // If root-entry, expand root
617 if( !pParentEntry )
618 Expand( m_pRootEntry );
621 // insert children
622 FmEntryDataList* pChildList = pEntryData->GetChildList();
623 size_t nChildCount = pChildList->size();
624 for( size_t i = 0; i < nChildCount; i++ )
626 FmEntryData* pChildData = pChildList->at( i );
627 Insert( pChildData, TREELIST_APPEND );
630 return pNewEntry;
634 void NavigatorTree::Remove( FmEntryData* pEntryData )
636 if( !pEntryData )
637 return;
639 // entry for the data
640 SvTreeListEntry* pEntry = FindEntry( pEntryData );
641 if (!pEntry)
642 return;
644 // delete entry from TreeListBox
645 // I'm not allowed, to treat the selection, which I trigger:
646 // select changes the MarkList of the view, if somebody else does this at the same time
647 // and removes a selection, we get a problem
648 // e.g. Group controls with open navigator
649 LockSelectionHandling();
651 // little problem: I remember the selected data, but if somebody deletes one of these entries,
652 // I get inconsistent... this would be bad
653 Select(pEntry, false);
655 // selection can be modified during deletion,
656 // but because I disabled SelectionHandling, I have to do it later
657 sal_uIntPtr nExpectedSelectionCount = GetSelectionCount();
659 GetModel()->Remove(pEntry);
661 if (nExpectedSelectionCount != GetSelectionCount())
662 SynchronizeSelection();
664 // by default I treat the selection of course
665 UnlockSelectionHandling();
669 bool NavigatorTree::IsFormEntry( SvTreeListEntry const * pEntry )
671 FmEntryData* pEntryData = static_cast<FmEntryData*>(pEntry->GetUserData());
672 return !pEntryData || dynamic_cast<const FmFormData*>( pEntryData) != nullptr;
676 bool NavigatorTree::IsFormComponentEntry( SvTreeListEntry const * pEntry )
678 FmEntryData* pEntryData = static_cast<FmEntryData*>(pEntry->GetUserData());
679 return dynamic_cast<const FmControlData*>( pEntryData) != nullptr;
683 bool NavigatorTree::implAcceptPaste( )
685 SvTreeListEntry* pFirstSelected = FirstSelected();
686 if ( !pFirstSelected || NextSelected( pFirstSelected ) )
687 // no selected entry, or at least two selected entries
688 return false;
690 // get the clipboard
691 TransferableDataHelper aClipboardContent( TransferableDataHelper::CreateFromSystemClipboard( this ) );
693 sal_Int8 nAction = m_aControlExchange.isClipboardOwner() && doingKeyboardCut( ) ? DND_ACTION_MOVE : DND_ACTION_COPY;
694 return ( nAction == implAcceptDataTransfer( aClipboardContent.GetDataFlavorExVector(), nAction, pFirstSelected, false ) );
698 sal_Int8 NavigatorTree::implAcceptDataTransfer( const DataFlavorExVector& _rFlavors, sal_Int8 _nAction, SvTreeListEntry* _pTargetEntry, bool _bDnD )
700 // no target -> no drop
701 if (!_pTargetEntry)
702 return DND_ACTION_NONE;
704 // format check
705 bool bHasDefControlFormat = OControlExchange::hasFieldExchangeFormat( _rFlavors );
706 bool bHasControlPathFormat = OControlExchange::hasControlPathFormat( _rFlavors );
707 bool bHasHiddenControlsFormat = OControlExchange::hasHiddenControlModelsFormat( _rFlavors );
708 if (!bHasDefControlFormat && !bHasControlPathFormat && !bHasHiddenControlsFormat)
709 return DND_ACTION_NONE;
711 bool bSelfSource = _bDnD ? m_aControlExchange.isDragSource() : m_aControlExchange.isClipboardOwner();
713 if ( bHasHiddenControlsFormat )
714 { // bHasHiddenControlsFormat means that only hidden controls are part of the data
716 // hidden controls can be copied to a form only
717 if ((_pTargetEntry == m_pRootEntry) || !IsFormEntry(_pTargetEntry))
718 return DND_ACTION_NONE;
720 return bSelfSource ? ( DND_ACTION_COPYMOVE & _nAction ) : DND_ACTION_COPY;
723 if ( !bSelfSource )
725 // DnD or CnP crossing navigator boundaries
726 // The main problem here is that the current API does not allow us to sneak into the content which
727 // is to be inserted. So we have to allow it for the moment, but maybe reject later on (in the real drop).
729 // TODO: this smart behaviour later on ... at the moment, we disallow data transfer crossing navigator
730 // boundaries.
732 return DND_ACTION_NONE;
735 DBG_ASSERT( _bDnD ? m_aControlExchange.isDragSource() : m_aControlExchange.isClipboardOwner(),
736 "NavigatorTree::implAcceptDataTransfer: here only with source=dest!" );
737 // somebody changed the logic of this method ...
739 // from here on, I can work with m_aControlExchange instead of _rData!
741 bool bForeignCollection = m_aControlExchange->getFormsRoot().get() != GetNavModel()->GetFormPage()->GetForms().get();
742 if ( bForeignCollection )
744 // crossing shell/page boundaries, we can exchange hidden controls only
745 // But if we survived the checks above, we do not have hidden controls.
746 // -> no data transfer
747 DBG_ASSERT( !bHasHiddenControlsFormat, "NavigatorTree::implAcceptDataTransfer: still hidden controls format!" );
748 // somebody changed the logic of this method ...
750 return DND_ACTION_COPY;
753 if (DND_ACTION_MOVE != _nAction) // 'normal' controls within a shell are moved only (never copied)
754 return DND_ACTION_NONE;
756 if ( m_bDragDataDirty || !bHasDefControlFormat )
758 if (!bHasControlPathFormat)
759 // I am in the shell/page, which has the controls, but I have no format,
760 // which survived the shell change (SVX_FM_CONTROLS_AS_PATH)
761 return DND_ACTION_NONE;
763 // I must recreate the list of the ExchangeObjects, because the shell was changed during dragging
764 // (there are SvLBoxEntries in it, and we lost them during change)
765 m_aControlExchange->buildListFromPath(this, m_pRootEntry);
766 m_bDragDataDirty = false;
769 // List of dropped entries from DragServer
770 const ListBoxEntrySet& aDropped = m_aControlExchange->selected();
771 DBG_ASSERT(!aDropped.empty(), "NavigatorTree::implAcceptDataTransfer: no entries !");
773 bool bDropTargetIsComponent = IsFormComponentEntry( _pTargetEntry );
774 //SvTreeListEntry* pDropTargetParent = GetParent( _pTargetEntry );
776 // conditions to disallow the drop
777 // 0) the root entry is part of the list (can't DnD the root!)
778 // 1) one of the dragged entries is to be dropped onto its own parent
779 // 2) - " - is to be dropped onto itself
780 // 3) - " - is a Form and to be dropped onto one of its descendants
781 // 4) one of the entries is a control and to be dropped onto the root
782 // 5) a control or form will be dropped onto a control which is _not_ a sibling (dropping onto a sibling
783 // means moving the control)
785 // collect the ancestors of the drop target (speeds up 3)
786 SvLBoxEntrySortedArray arrDropAnchestors;
787 SvTreeListEntry* pLoop = _pTargetEntry;
788 while (pLoop)
790 arrDropAnchestors.insert(pLoop);
791 pLoop = GetParent(pLoop);
794 for (SvTreeListEntry* pCurrent : aDropped)
796 SvTreeListEntry* pCurrentParent = GetParent(pCurrent);
798 // test for 0)
799 if (pCurrent == m_pRootEntry)
800 return DND_ACTION_NONE;
802 // test for 1)
803 if ( _pTargetEntry == pCurrentParent )
804 return DND_ACTION_NONE;
806 // test for 2)
807 if (pCurrent == _pTargetEntry)
808 return DND_ACTION_NONE;
810 // test for 5)
811 // if ( bDropTargetIsComponent && (pDropTargetParent != pCurrentParent) )
812 if ( bDropTargetIsComponent ) // TODO : the line above can be inserted, if ExecuteDrop can handle inversion
813 return DND_ACTION_NONE;
815 // test for 3)
816 if ( IsFormEntry(pCurrent) )
818 if ( arrDropAnchestors.find(pCurrent) != arrDropAnchestors.end() )
819 return DND_ACTION_NONE;
820 } else if ( IsFormComponentEntry(pCurrent) )
822 // test for 4)
823 if (_pTargetEntry == m_pRootEntry)
824 return DND_ACTION_NONE;
828 return DND_ACTION_MOVE;
832 sal_Int8 NavigatorTree::AcceptDrop( const AcceptDropEvent& rEvt )
834 ::Point aDropPos = rEvt.maPosPixel;
836 // first handle possible DropActions (Scroll and swing open)
837 if (rEvt.mbLeaving)
839 if (m_aDropActionTimer.IsActive())
840 m_aDropActionTimer.Stop();
841 } else
843 bool bNeedTrigger = false;
844 // on the first entry ?
845 if ((aDropPos.Y() >= 0) && (aDropPos.Y() < GetEntryHeight()))
847 m_aDropActionType = DA_SCROLLUP;
848 bNeedTrigger = true;
849 } else
850 // on the last one (respectively the area, an entry would tale, if it flush with the bottom ?
851 if ((aDropPos.Y() < GetSizePixel().Height()) && (aDropPos.Y() >= GetSizePixel().Height() - GetEntryHeight()))
853 m_aDropActionType = DA_SCROLLDOWN;
854 bNeedTrigger = true;
855 } else
856 { // on an entry with children, not swang open
857 SvTreeListEntry* pDroppedOn = GetEntry(aDropPos);
858 if (pDroppedOn && (GetChildCount(pDroppedOn) > 0) && !IsExpanded(pDroppedOn))
860 // -> swing open
861 m_aDropActionType = DA_EXPANDNODE;
862 bNeedTrigger = true;
866 if (bNeedTrigger && (m_aTimerTriggered != aDropPos))
868 // restart counting
869 m_aTimerCounter = DROP_ACTION_TIMER_INITIAL_TICKS;
870 // remember pos, because I get AcceptDrops, although mouse hasn't moved
871 m_aTimerTriggered = aDropPos;
872 // start Timer
873 if (!m_aDropActionTimer.IsActive()) // exist Timer?
875 m_aDropActionTimer.SetTimeout(DROP_ACTION_TIMER_TICK_BASE);
876 m_aDropActionTimer.Start();
878 } else if (!bNeedTrigger)
879 m_aDropActionTimer.Stop();
882 return implAcceptDataTransfer( GetDataFlavorExVector(), rEvt.mnAction, GetEntry( aDropPos ), true );
886 sal_Int8 NavigatorTree::implExecuteDataTransfer( const OControlTransferData& _rData, sal_Int8 _nAction, const ::Point& _rDropPos, bool _bDnD )
888 return implExecuteDataTransfer( _rData, _nAction, GetEntry( _rDropPos ), _bDnD );
892 sal_Int8 NavigatorTree::implExecuteDataTransfer( const OControlTransferData& _rData, sal_Int8 _nAction, SvTreeListEntry* _pTargetEntry, bool _bDnD )
894 const DataFlavorExVector& rDataFlavors = _rData.GetDataFlavorExVector();
896 if ( DND_ACTION_NONE == implAcceptDataTransfer( rDataFlavors, _nAction, _pTargetEntry, _bDnD ) )
897 // under some platforms, it may happen that ExecuteDrop is called though AcceptDrop returned DND_ACTION_NONE
898 return DND_ACTION_NONE;
900 // would be bad, if we scroll after drop
901 if (m_aDropActionTimer.IsActive())
902 m_aDropActionTimer.Stop();
904 if (!_pTargetEntry)
905 // no target -> no drop
906 return DND_ACTION_NONE;
908 // format checks
909 #ifdef DBG_UTIL
910 bool bHasHiddenControlsFormat = OControlExchange::hasHiddenControlModelsFormat( rDataFlavors );
911 bool bForeignCollection = _rData.getFormsRoot().get() != GetNavModel()->GetFormPage()->GetForms().get();
912 DBG_ASSERT(!bForeignCollection || bHasHiddenControlsFormat, "NavigatorTree::implExecuteDataTransfer: invalid format (AcceptDrop shouldn't have let this pass) !");
913 DBG_ASSERT(bForeignCollection || !m_bDragDataDirty, "NavigatorTree::implExecuteDataTransfer: invalid state (shell changed since last exchange resync) !");
914 // this should be done in AcceptDrop: the list of controls is created in _rData
915 // and m_bDragDataDirty is reset
916 #endif
918 if ( DND_ACTION_COPY == _nAction )
919 { // bHasHiddenControlsFormat means that only hidden controls are part of the data
920 #ifdef DBG_UTIL
921 DBG_ASSERT( bHasHiddenControlsFormat, "NavigatorTree::implExecuteDataTransfer: copy allowed for hidden controls only!" );
922 #endif
923 DBG_ASSERT( _pTargetEntry && ( _pTargetEntry != m_pRootEntry ) && IsFormEntry( _pTargetEntry ),
924 "NavigatorTree::implExecuteDataTransfer: should not be here!" );
925 // implAcceptDataTransfer should have caught both cases
927 #ifdef DBG_UTIL
928 DBG_ASSERT(bHasHiddenControlsFormat, "NavigatorTree::implExecuteDataTransfer: only copying of hidden controls is supported !");
929 // should be caught by AcceptDrop
930 #endif
932 // because i want to select all targets (and only them)
933 SelectAll(false);
935 const Sequence< Reference< XInterface > >& aControls = _rData.hiddenControls();
936 sal_Int32 nCount = aControls.getLength();
937 const Reference< XInterface >* pControls = aControls.getConstArray();
939 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
940 FmFormModel* pFormModel = pFormShell ? pFormShell->GetFormModel() : nullptr;
942 // within undo
943 if (pFormModel)
945 OUString aStr(SvxResId(RID_STR_CONTROL));
946 OUString aUndoStr = SvxResId(RID_STR_UNDO_CONTAINER_INSERT).replaceAll("#", aStr);
947 pFormModel->BegUndo(aUndoStr);
950 // copy controls
951 for (sal_Int32 i=0; i<nCount; ++i)
953 // create new control
954 OUString fControlName = FM_COMPONENT_HIDDEN;
955 FmControlData* pNewControlData = NewControl( fControlName, _pTargetEntry, false);
956 Reference< XPropertySet > xNewPropSet( pNewControlData->GetPropertySet() );
958 // copy properties form old control to new one
959 Reference< XPropertySet > xCurrent(pControls[i], UNO_QUERY);
960 #if (OSL_DEBUG_LEVEL > 0)
961 // check whether it is a hidden control
962 sal_Int16 nClassId = ::comphelper::getINT16(xCurrent->getPropertyValue(FM_PROP_CLASSID));
963 OSL_ENSURE(nClassId == FormComponentType::HIDDENCONTROL, "NavigatorTree::implExecuteDataTransfer: invalid control in drop list !");
964 // if SVX_FM_HIDDEN_CONTROLS-format exists, the sequence
965 // should only contain hidden controls
966 #endif // (OSL_DEBUG_LEVEL > 0)
967 Reference< XPropertySetInfo > xPropInfo( xCurrent->getPropertySetInfo());
968 const Sequence< Property> seqAllCurrentProps = xPropInfo->getProperties();
969 for (Property const & currentProp : seqAllCurrentProps)
971 if (((currentProp.Attributes & PropertyAttribute::READONLY) == 0) && (currentProp.Name != FM_PROP_NAME))
972 { // (read-only attribs aren't set, ditto name,
973 // NewControl defined it uniquely
974 xNewPropSet->setPropertyValue(currentProp.Name, xCurrent->getPropertyValue(currentProp.Name));
978 SvTreeListEntry* pToSelect = FindEntry(pNewControlData);
979 Select(pToSelect);
980 if (i == 0)
981 SetCurEntry(pToSelect);
984 if (pFormModel)
985 pFormModel->EndUndo();
987 return _nAction;
990 if ( !OControlExchange::hasFieldExchangeFormat( _rData.GetDataFlavorExVector() ) )
992 // can't do anything without the internal format here ... usually happens when doing DnD or CnP
993 // over navigator boundaries
994 return DND_ACTION_NONE;
997 // some data for the target
998 bool bDropTargetIsForm = IsFormEntry(_pTargetEntry);
999 FmFormData* pTargetData = bDropTargetIsForm ? static_cast<FmFormData*>(_pTargetEntry->GetUserData()) : nullptr;
1001 DBG_ASSERT( DND_ACTION_COPY != _nAction, "NavigatorTree::implExecuteDataTransfer: somebody changed the logics!" );
1003 // list of dragged entries
1004 const ListBoxEntrySet aDropped = _rData.selected();
1005 DBG_ASSERT(!aDropped.empty(), "NavigatorTree::implExecuteDataTransfer: no entries!");
1007 // shell and model
1008 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
1009 FmFormModel* pFormModel = pFormShell ? pFormShell->GetFormModel() : nullptr;
1010 if (!pFormModel)
1011 return DND_ACTION_NONE;
1013 // for Undo
1014 const bool bUndo = pFormModel->IsUndoEnabled();
1016 if( bUndo )
1018 OUString strUndoDescription(SvxResId(RID_STR_UNDO_CONTAINER_REPLACE));
1019 pFormModel->BegUndo(strUndoDescription);
1022 // remove selection before adding an entry, so the mark doesn't flicker
1023 // -> lock action of selection
1024 LockSelectionHandling();
1026 // go through all dropped entries
1027 for ( ListBoxEntrySet::const_iterator dropped = aDropped.begin();
1028 dropped != aDropped.end();
1029 ++dropped
1032 // some data of the current element
1033 SvTreeListEntry* pCurrent = *dropped;
1034 DBG_ASSERT(pCurrent != nullptr, "NavigatorTree::implExecuteDataTransfer: invalid entry");
1035 DBG_ASSERT(GetParent(pCurrent) != nullptr, "NavigatorTree::implExecuteDataTransfer: invalid entry");
1036 // don't drag root
1038 FmEntryData* pCurrentUserData = static_cast<FmEntryData*>(pCurrent->GetUserData());
1040 Reference< XChild > xCurrentChild = pCurrentUserData->GetChildIFace();
1041 Reference< XIndexContainer > xContainer(xCurrentChild->getParent(), UNO_QUERY);
1043 FmFormData* pCurrentParentUserData = static_cast<FmFormData*>(pCurrentUserData->GetParent());
1044 DBG_ASSERT(pCurrentParentUserData == nullptr || dynamic_cast<const FmFormData*>(pCurrentUserData->GetParent()) != nullptr, "NavigatorTree::implExecuteDataTransfer: invalid parent");
1046 // remove from parent
1047 if (pCurrentParentUserData)
1048 pCurrentParentUserData->GetChildList()->removeNoDelete( pCurrentUserData );
1049 else
1050 GetNavModel()->GetRootList()->removeNoDelete( pCurrentUserData );
1052 // remove from container
1053 sal_Int32 nIndex = getElementPos(xContainer, xCurrentChild);
1054 GetNavModel()->m_pPropChangeList->Lock();
1055 // UndoAction for removal
1056 if ( bUndo && GetNavModel()->m_pPropChangeList->CanUndo())
1058 pFormModel->AddUndo(std::make_unique<FmUndoContainerAction>(*pFormModel, FmUndoContainerAction::Removed,
1059 xContainer, xCurrentChild, nIndex));
1061 else if( !GetNavModel()->m_pPropChangeList->CanUndo() )
1063 FmUndoContainerAction::DisposeElement( xCurrentChild );
1066 // copy events
1067 Reference< XEventAttacherManager > xManager(xContainer, UNO_QUERY);
1068 Sequence< ScriptEventDescriptor > aEvts;
1070 if (xManager.is() && nIndex >= 0)
1071 aEvts = xManager->getScriptEvents(nIndex);
1072 xContainer->removeByIndex(nIndex);
1074 // remove selection
1075 Select(pCurrent, false);
1076 // and delete it
1077 Remove(pCurrentUserData);
1079 // position in DropParents, where to insert dropped entries
1080 if (pTargetData)
1081 xContainer.set(pTargetData->GetElement(), UNO_QUERY);
1082 else
1083 xContainer = GetNavModel()->GetForms();
1085 // always insert at the end
1086 nIndex = xContainer->getCount();
1088 // UndoAction for insertion
1089 if ( bUndo && GetNavModel()->m_pPropChangeList->CanUndo())
1090 pFormModel->AddUndo(std::make_unique<FmUndoContainerAction>(*pFormModel, FmUndoContainerAction::Inserted,
1091 xContainer, xCurrentChild, nIndex));
1093 // insert in new container
1094 if (pTargetData)
1096 // insert in a form needs a FormComponent
1097 xContainer->insertByIndex( nIndex,
1098 makeAny( Reference< XFormComponent >( xCurrentChild, UNO_QUERY ) ) );
1100 else
1102 xContainer->insertByIndex( nIndex,
1103 makeAny( Reference< XForm >( xCurrentChild, UNO_QUERY ) ) );
1106 if (aEvts.hasElements())
1108 xManager.set(xContainer, UNO_QUERY);
1109 if (xManager.is())
1110 xManager->registerScriptEvents(nIndex, aEvts);
1113 GetNavModel()->m_pPropChangeList->UnLock();
1115 // give an entry the new parent
1116 pCurrentUserData->SetParent(pTargetData);
1118 // give parent the new child
1119 if (pTargetData)
1120 pTargetData->GetChildList()->insert( std::unique_ptr<FmEntryData>(pCurrentUserData), nIndex );
1121 else
1122 GetNavModel()->GetRootList()->insert( std::unique_ptr<FmEntryData>(pCurrentUserData), nIndex );
1124 // announce to myself and reselect
1125 SvTreeListEntry* pNew = Insert( pCurrentUserData, nIndex );
1126 if ( ( aDropped.begin() == dropped ) && pNew )
1128 SvTreeListEntry* pParent = GetParent( pNew );
1129 if ( pParent )
1130 Expand( pParent );
1134 UnlockSelectionHandling();
1136 if( bUndo )
1137 pFormModel->EndUndo();
1139 // During the move, the markings of the underlying view did not change (because the view is not affected by the logical
1140 // hierarchy of the form/control models. But my selection changed - which means I have to adjust it according to the
1141 // view marks, again.
1142 SynchronizeSelection();
1144 // in addition, with the move of controls such things as "the current form" may have changed - force the shell
1145 // to update itself accordingly
1146 if( pFormShell && pFormShell->GetImpl() && pFormShell->GetFormView() )
1147 pFormShell->GetImpl()->DetermineSelection_Lock( pFormShell->GetFormView()->GetMarkedObjectList() );
1149 if ( m_aControlExchange.isClipboardOwner() && ( DND_ACTION_MOVE == _nAction ) )
1150 m_aControlExchange->clear();
1152 return _nAction;
1156 sal_Int8 NavigatorTree::ExecuteDrop( const ExecuteDropEvent& rEvt )
1158 sal_Int8 nResult( DND_ACTION_NONE );
1160 if ( m_aControlExchange.isDragSource() )
1161 nResult = implExecuteDataTransfer( *m_aControlExchange, rEvt.mnAction, rEvt.maPosPixel, true );
1162 else
1164 OControlTransferData aDroppedData( rEvt.maDropEvent.Transferable );
1165 nResult = implExecuteDataTransfer( aDroppedData, rEvt.mnAction, rEvt.maPosPixel, true );
1168 return nResult;
1172 void NavigatorTree::doPaste()
1176 if ( m_aControlExchange.isClipboardOwner() )
1178 implExecuteDataTransfer( *m_aControlExchange, doingKeyboardCut( ) ? DND_ACTION_MOVE : DND_ACTION_COPY, FirstSelected(), false );
1180 else
1182 // the clipboard content
1183 Reference< XClipboard > xClipboard( GetClipboard() );
1184 Reference< XTransferable > xTransferable;
1185 if ( xClipboard.is() )
1186 xTransferable = xClipboard->getContents();
1188 OControlTransferData aClipboardContent( xTransferable );
1189 implExecuteDataTransfer( aClipboardContent, DND_ACTION_COPY, FirstSelected(), false );
1192 catch( const Exception& )
1194 OSL_FAIL( "NavigatorTree::doPaste: caught an exception!" );
1199 void NavigatorTree::doCopy()
1201 if ( implPrepareExchange( DND_ACTION_COPY ) )
1203 m_aControlExchange.setClipboardListener( LINK( this, NavigatorTree, OnClipboardAction ) );
1204 m_aControlExchange.copyToClipboard( );
1209 void NavigatorTree::ModelHasRemoved( SvTreeListEntry* _pEntry )
1211 SvTreeListEntry* pTypedEntry = _pEntry;
1212 if ( doingKeyboardCut() )
1213 m_aCutEntries.erase( pTypedEntry );
1215 if ( m_aControlExchange.isDataExchangeActive() )
1217 if ( 0 == m_aControlExchange->onEntryRemoved( pTypedEntry ) )
1219 // last of the entries which we put into the clipboard has been deleted from the tree.
1220 // Give up the clipboard ownership.
1221 m_aControlExchange.clear();
1227 void NavigatorTree::doCut()
1229 if ( implPrepareExchange( DND_ACTION_MOVE ) )
1231 m_aControlExchange.setClipboardListener( LINK( this, NavigatorTree, OnClipboardAction ) );
1232 m_aControlExchange.copyToClipboard( );
1233 m_bKeyboardCut = true;
1235 // mark all the entries we just "cut" into the clipboard as "nearly moved"
1236 for ( SvTreeListEntry* pEntry : m_arrCurrentSelection )
1238 if ( pEntry )
1240 m_aCutEntries.insert( pEntry );
1241 pEntry->SetFlags( pEntry->GetFlags() | SvTLEntryFlags::SEMITRANSPARENT );
1242 InvalidateEntry( pEntry );
1249 void NavigatorTree::KeyInput(const ::KeyEvent& rKEvt)
1251 const vcl::KeyCode& rCode = rKEvt.GetKeyCode();
1253 // delete?
1254 if (rKEvt.GetKeyCode().GetCode() == KEY_DELETE && !rKEvt.GetKeyCode().GetModifier())
1256 DeleteSelection();
1257 return;
1260 // copy'n'paste?
1261 switch ( rCode.GetFunction() )
1263 case KeyFuncType::CUT:
1264 doCut();
1265 break;
1267 case KeyFuncType::PASTE:
1268 if ( implAcceptPaste() )
1269 doPaste();
1270 break;
1272 case KeyFuncType::COPY:
1273 doCopy();
1274 break;
1276 default:
1277 break;
1280 SvTreeListBox::KeyInput(rKEvt);
1284 bool NavigatorTree::EditingEntry( SvTreeListEntry* pEntry, ::Selection& rSelection )
1286 if (!SvTreeListBox::EditingEntry( pEntry, rSelection ))
1287 return false;
1289 return (pEntry && (pEntry->GetUserData() != nullptr));
1290 // root, which isn't allowed to be renamed, has UserData=NULL
1294 void NavigatorTree::NewForm( SvTreeListEntry const * pParentEntry )
1297 // get ParentFormData
1298 if( !IsFormEntry(pParentEntry) )
1299 return;
1301 FmFormData* pParentFormData = static_cast<FmFormData*>(pParentEntry->GetUserData());
1304 // create new form
1305 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
1306 Reference< XForm > xNewForm(xContext->getServiceManager()->createInstanceWithContext(FM_SUN_COMPONENT_FORM, xContext), UNO_QUERY);
1307 if (!xNewForm.is())
1308 return;
1310 Reference< XPropertySet > xPropertySet(xNewForm, UNO_QUERY);
1311 if (!xPropertySet.is())
1312 return;
1314 FmFormData* pNewFormData = new FmFormData(xNewForm, pParentFormData);
1317 // set name
1318 OUString aName = GenerateName(pNewFormData);
1319 pNewFormData->SetText(aName);
1323 xPropertySet->setPropertyValue( FM_PROP_NAME, makeAny(aName) );
1324 // a form should always have the command type table as default
1325 xPropertySet->setPropertyValue( FM_PROP_COMMANDTYPE, makeAny(sal_Int32(CommandType::TABLE)));
1327 catch ( const Exception& )
1329 OSL_FAIL("NavigatorTree::NewForm : could not set essential properties!");
1333 // insert form
1334 GetNavModel()->Insert(pNewFormData, SAL_MAX_UINT32, true);
1337 // set new form as active
1338 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
1339 if( pFormShell )
1341 InterfaceBag aSelection;
1342 aSelection.insert( Reference<XInterface>( xNewForm, UNO_QUERY ) );
1343 pFormShell->GetImpl()->setCurrentSelection_Lock(aSelection);
1345 pFormShell->GetViewShell()->GetViewFrame()->GetBindings().Invalidate(SID_FM_PROPERTIES, true, true);
1347 GetNavModel()->SetModified();
1350 // switch to EditMode
1351 SvTreeListEntry* pNewEntry = FindEntry( pNewFormData );
1352 EditEntry( pNewEntry );
1356 FmControlData* NavigatorTree::NewControl( const OUString& rServiceName, SvTreeListEntry const * pParentEntry, bool bEditName )
1359 // get ParentForm
1360 if (!GetNavModel()->GetFormShell())
1361 return nullptr;
1362 if (!IsFormEntry(pParentEntry))
1363 return nullptr;
1365 FmFormData* pParentFormData = static_cast<FmFormData*>(pParentEntry->GetUserData());
1366 Reference< XForm > xParentForm( pParentFormData->GetFormIface());
1369 // create new component
1370 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
1371 Reference<XFormComponent> xNewComponent( xContext->getServiceManager()->createInstanceWithContext(rServiceName, xContext), UNO_QUERY);
1372 if (!xNewComponent.is())
1373 return nullptr;
1375 FmControlData* pNewFormControlData = new FmControlData(xNewComponent, pParentFormData);
1378 // set name
1379 OUString sName = FmFormPageImpl::setUniqueName( xNewComponent, xParentForm );
1381 pNewFormControlData->SetText( sName );
1384 // insert FormComponent
1385 GetNavModel()->Insert(pNewFormControlData, SAL_MAX_UINT32, true);
1386 GetNavModel()->SetModified();
1388 if (bEditName)
1391 // switch to EditMode
1392 SvTreeListEntry* pNewEntry = FindEntry( pNewFormControlData );
1393 Select( pNewEntry );
1394 EditEntry( pNewEntry );
1397 return pNewFormControlData;
1401 OUString NavigatorTree::GenerateName( FmEntryData const * pEntryData )
1403 const sal_uInt16 nMaxCount = 99;
1404 OUString aNewName;
1407 // create base name
1408 OUString aBaseName;
1409 if( dynamic_cast<const FmFormData*>( pEntryData) != nullptr )
1410 aBaseName = SvxResId( RID_STR_STDFORMNAME );
1411 else if( dynamic_cast<const FmControlData*>( pEntryData) != nullptr )
1412 aBaseName = SvxResId( RID_STR_CONTROL );
1415 // create new name
1416 FmFormData* pFormParentData = static_cast<FmFormData*>(pEntryData->GetParent());
1418 for( sal_Int32 i=0; i<nMaxCount; i++ )
1420 aNewName = aBaseName;
1421 if( i>0 )
1423 aNewName += " " + OUString::number(i);
1426 if( GetNavModel()->FindData(aNewName, pFormParentData,false) == nullptr )
1427 break;
1430 return aNewName;
1434 bool NavigatorTree::EditedEntry( SvTreeListEntry* pEntry, const OUString& rNewText )
1436 if (EditingCanceled())
1437 return true;
1439 GrabFocus();
1440 FmEntryData* pEntryData = static_cast<FmEntryData*>(pEntry->GetUserData());
1441 bool bRes = NavigatorTreeModel::Rename( pEntryData, rNewText);
1442 if( !bRes )
1444 m_pEditEntry = pEntry;
1445 nEditEvent = Application::PostUserEvent( LINK(this, NavigatorTree, OnEdit), nullptr, true );
1446 } else
1447 SetCursor(pEntry, true);
1449 return bRes;
1453 IMPL_LINK_NOARG(NavigatorTree, OnEdit, void*, void)
1455 nEditEvent = nullptr;
1456 EditEntry( m_pEditEntry );
1457 m_pEditEntry = nullptr;
1461 IMPL_LINK_NOARG(NavigatorTree, OnDropActionTimer, Timer *, void)
1463 if (--m_aTimerCounter > 0)
1464 return;
1466 switch ( m_aDropActionType )
1468 case DA_EXPANDNODE:
1470 SvTreeListEntry* pToExpand = GetEntry(m_aTimerTriggered);
1471 if (pToExpand && (GetChildCount(pToExpand) > 0) && !IsExpanded(pToExpand))
1472 // normally, we have to test, if the node is expanded,
1473 // but there is no method for this either in base class nor the model
1474 // the base class should tolerate it anyway
1475 Expand(pToExpand);
1477 // After expansion there is nothing to do like after scrolling
1478 m_aDropActionTimer.Stop();
1480 break;
1482 case DA_SCROLLUP :
1483 ScrollOutputArea( 1 );
1484 m_aTimerCounter = DROP_ACTION_TIMER_SCROLL_TICKS;
1485 break;
1487 case DA_SCROLLDOWN :
1488 ScrollOutputArea( -1 );
1489 m_aTimerCounter = DROP_ACTION_TIMER_SCROLL_TICKS;
1490 break;
1496 IMPL_LINK_NOARG(NavigatorTree, OnEntrySelDesel, SvTreeListBox*, void)
1498 m_sdiState = SDI_DIRTY;
1500 if (IsSelectionHandlingLocked())
1501 return;
1503 if (m_aSynchronizeTimer.IsActive())
1504 m_aSynchronizeTimer.Stop();
1506 m_aSynchronizeTimer.SetTimeout(EXPLORER_SYNC_DELAY);
1507 m_aSynchronizeTimer.Start();
1511 IMPL_LINK_NOARG(NavigatorTree, OnSynchronizeTimer, Timer *, void)
1513 SynchronizeMarkList();
1517 IMPL_LINK_NOARG(NavigatorTree, OnClipboardAction, OLocalExchange&, void)
1519 if ( !m_aControlExchange.isClipboardOwner() )
1521 if ( doingKeyboardCut() )
1523 for (SvTreeListEntry* pEntry : m_aCutEntries)
1525 if ( !pEntry )
1526 continue;
1528 pEntry->SetFlags( pEntry->GetFlags() & ~SvTLEntryFlags::SEMITRANSPARENT );
1529 InvalidateEntry( pEntry );
1531 ListBoxEntrySet aEmpty;
1532 m_aCutEntries.swap( aEmpty );
1534 m_bKeyboardCut = false;
1540 void NavigatorTree::ShowSelectionProperties(bool bForce)
1542 // at first i need the FormShell
1543 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
1544 if (!pFormShell)
1545 // no shell -> impossible to set curObject -> leave
1546 return;
1548 CollectSelectionData(SDI_ALL);
1549 SAL_WARN_IF(static_cast<size_t>(m_nFormsSelected + m_nControlsSelected
1550 + (m_bRootSelected ? 1 : 0)) != m_arrCurrentSelection.size(),
1551 "svx.form",
1552 "NavigatorTree::ShowSelectionProperties : selection meta data invalid !");
1555 InterfaceBag aSelection;
1556 bool bSetSelectionAsMarkList = false;
1558 if (m_bRootSelected)
1559 ; // no properties for the root, neither for single nor for multi selection
1560 else if ( m_nFormsSelected + m_nControlsSelected == 0 ) // none of the two should be less 0
1561 ; // no selection -> no properties
1562 else if ( m_nFormsSelected * m_nControlsSelected != 0 )
1563 ; // mixed selection -> no properties
1564 else
1565 { // either only forms, or only controls are selected
1566 if (m_arrCurrentSelection.size() == 1)
1568 if (m_nFormsSelected > 0)
1569 { // exactly one form is selected
1570 FmFormData* pFormData = static_cast<FmFormData*>((*m_arrCurrentSelection.begin())->GetUserData());
1571 aSelection.insert( Reference< XInterface >( pFormData->GetFormIface(), UNO_QUERY ) );
1573 else
1574 { // exactly one control is selected (whatever hidden or normal)
1575 FmEntryData* pEntryData = static_cast<FmEntryData*>((*m_arrCurrentSelection.begin())->GetUserData());
1577 aSelection.insert( Reference< XInterface >( pEntryData->GetElement(), UNO_QUERY ) );
1580 else
1581 { // it's a MultiSelection, so we must build a MultiSet
1582 if (m_nFormsSelected > 0)
1583 { // ... only forms
1584 // first of all collect PropertySet-Interfaces of the forms
1585 SvLBoxEntrySortedArray::const_iterator it = m_arrCurrentSelection.begin();
1586 for ( sal_Int32 i = 0; i < m_nFormsSelected; ++i )
1588 FmFormData* pFormData = static_cast<FmFormData*>((*it)->GetUserData());
1589 aSelection.insert( pFormData->GetPropertySet().get() );
1590 ++it;
1593 else
1594 { // ... only controls
1595 if (m_nHiddenControls == m_nControlsSelected)
1596 { // a MultiSet for properties of hidden controls
1597 SvLBoxEntrySortedArray::const_iterator it = m_arrCurrentSelection.begin();
1598 for ( sal_Int32 i = 0; i < m_nHiddenControls; ++i )
1600 FmEntryData* pEntryData = static_cast<FmEntryData*>((*it)->GetUserData());
1601 aSelection.insert( pEntryData->GetPropertySet().get() );
1602 ++it;
1605 else if (m_nHiddenControls == 0)
1606 { // only normal controls
1607 bSetSelectionAsMarkList = true;
1614 // and now my form and my SelObject
1615 if ( bSetSelectionAsMarkList )
1616 pFormShell->GetImpl()->setCurrentSelectionFromMark_Lock(pFormShell->GetFormView()->GetMarkedObjectList());
1617 else
1618 pFormShell->GetImpl()->setCurrentSelection_Lock(aSelection);
1620 if (pFormShell->GetImpl()->IsPropBrwOpen_Lock() || bForce)
1622 // and now deliver all to the PropertyBrowser
1623 pFormShell->GetViewShell()->GetViewFrame()->GetDispatcher()->Execute( SID_FM_SHOW_PROPERTY_BROWSER, SfxCallMode::ASYNCHRON );
1628 void NavigatorTree::DeleteSelection()
1630 // of course, i can't delete root
1631 bool bRootSelected = IsSelected(m_pRootEntry);
1632 sal_uIntPtr nSelectedEntries = GetSelectionCount();
1633 if (bRootSelected && (nSelectedEntries > 1)) // root and other elements ?
1634 Select(m_pRootEntry, false); // yes -> remove root from selection
1636 if ((nSelectedEntries == 0) || bRootSelected) // still root ?
1637 return; // -> only selected element -> leave
1639 DBG_ASSERT(!m_bPrevSelectionMixed, "NavigatorTree::DeleteSelection() : delete permitted if mark and selection are inconsistent");
1641 // i need the FormModel later
1642 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
1643 if (!pFormShell)
1644 return;
1645 FmFormModel* pFormModel = pFormShell->GetFormModel();
1646 if (!pFormModel)
1647 return;
1649 // now I have to safeguard the DeleteList: if you delete a form and a dependent element
1650 // - in this order - than the SvLBoxEntryPtr of the dependent element is already invalid,
1651 // when it should be deleted... you have to prohibit this GPF, that of course would happen,
1652 // so I take the 'normalized' list
1653 CollectSelectionData( SDI_NORMALIZED );
1655 // see below for why we need this mapping from models to shapes
1656 FmFormView* pFormView = pFormShell->GetFormView();
1657 SdrPageView* pPageView = pFormView ? pFormView->GetSdrPageView() : nullptr;
1658 SdrPage* pPage = pPageView ? pPageView->GetPage() : nullptr;
1659 DBG_ASSERT( pPage, "NavigatorTree::DeleteSelection: invalid form page!" );
1661 MapModelToShape aModelShapes;
1662 if ( pPage )
1663 collectShapeModelMapping( pPage, aModelShapes );
1665 // problem: we have to use ExplorerModel::Remove, since only this one properly deletes Form objects.
1666 // But, the controls themself must be deleted via DeleteMarked (else, the Writer has some problems
1667 // somewhere). In case I'd first delete the structure, then the controls, the UNDO would not work
1668 // (since UNDO then would mean to first restore the controls, then the structure, means their parent
1669 // form). The other way round, the EntryDatas would be invalid, if I'd first delete the controls and
1670 // then go on to the structure. This means I have to delete the forms *after* the normal controls, so
1671 // that during UNDO, they're restored in the proper order.
1672 pFormShell->GetImpl()->EnableTrackProperties_Lock(false);
1673 for (SvLBoxEntrySortedArray::reverse_iterator it = m_arrCurrentSelection.rbegin();
1674 it != m_arrCurrentSelection.rend(); )
1676 FmEntryData* pCurrent = static_cast<FmEntryData*>((*it)->GetUserData());
1678 // a form ?
1679 bool bIsForm = dynamic_cast<const FmFormData*>( pCurrent) != nullptr;
1681 // because deletion is done by the view, and i build on its MarkList,
1682 // but normally only direct controls, no indirect ones, are marked in a marked form,
1683 // I have to do it later
1684 if (bIsForm)
1685 MarkViewObj(static_cast<FmFormData*>(pCurrent), true/*deep*/);
1687 // a hidden control ?
1688 bool bIsHidden = IsHiddenControl(pCurrent);
1690 // keep forms and hidden controls, the rest not
1691 if (!bIsForm && !bIsHidden)
1693 // well, no form and no hidden control -> we can remove it from m_arrCurrentSelection, as it will
1694 // be deleted automatically. This is because for every model (except forms and hidden control models)
1695 // there exist a shape, which is marked _if_and_only_if_ the model is selected in our tree.
1696 if ( aModelShapes.find( pCurrent->GetElement() ) != aModelShapes.end() )
1698 // if there's a shape for the current entry, then either it is marked or it is in a
1699 // hidden layer (#i28502#), or something like this.
1700 // In the first case, it will be deleted below, in the second case, we currently don't
1701 // delete it, as there's no real (working!) API for this, neither in UNO nor in non-UNO.
1702 m_arrCurrentSelection.erase( --(it.base()) );
1704 else
1705 ++it;
1706 // In case there is no shape for the current entry, we keep the entry in m_arrCurrentSelection,
1707 // since then we can definitely remove it.
1709 else
1710 ++it;
1712 pFormShell->GetImpl()->EnableTrackProperties_Lock(true);
1714 // let the view delete the marked controls
1715 pFormShell->GetFormView()->DeleteMarked();
1717 // start UNDO at this point. Unfortunately, this results in 2 UNDO actions, since DeleteMarked is
1718 // creating an own one. However, if we'd move it before DeleteMarked, Writer does not really like
1719 // this ... :(
1720 // #i31038#
1723 // initialize UNDO
1724 OUString aUndoStr;
1725 if ( m_arrCurrentSelection.size() == 1 )
1727 aUndoStr = SvxResId(RID_STR_UNDO_CONTAINER_REMOVE);
1728 if (m_nFormsSelected)
1729 aUndoStr = aUndoStr.replaceFirst( "#", SvxResId( RID_STR_FORM ) );
1730 else
1731 // it must be a control (else the root would be selected, but it cannot be deleted)
1732 aUndoStr = aUndoStr.replaceFirst( "#", SvxResId( RID_STR_CONTROL ) );
1734 else
1736 aUndoStr = SvxResId(RID_STR_UNDO_CONTAINER_REMOVE_MULTIPLE);
1737 aUndoStr = aUndoStr.replaceFirst( "#", OUString::number( m_arrCurrentSelection.size() ) );
1739 pFormModel->BegUndo(aUndoStr);
1742 // remove remaining structure
1743 for (const auto& rpSelection : m_arrCurrentSelection)
1745 FmEntryData* pCurrent = static_cast<FmEntryData*>(rpSelection->GetUserData());
1747 // if the entry still has children, we skipped deletion of one of those children.
1748 // This may for instance be because the shape is in a hidden layer, where we're unable
1749 // to remove it
1750 if ( pCurrent->GetChildList()->size() )
1751 continue;
1753 // one remaining subtile problem, before deleting it : if it's a form and the shell
1754 // knows it as CurrentObject, I have to tell it something else
1755 if (dynamic_cast<const FmFormData*>( pCurrent) != nullptr)
1757 Reference< XForm > xCurrentForm( static_cast< FmFormData* >( pCurrent )->GetFormIface() );
1758 if (pFormShell->GetImpl()->getCurrentForm_Lock() == xCurrentForm) // shell knows form to be deleted ?
1759 pFormShell->GetImpl()->forgetCurrentForm_Lock(); // -> take away ...
1761 GetNavModel()->Remove(pCurrent, true);
1763 pFormModel->EndUndo();
1767 void NavigatorTree::CollectSelectionData(SELDATA_ITEMS sdiHow)
1769 DBG_ASSERT(sdiHow != SDI_DIRTY, "NavigatorTree::CollectSelectionData : ever thought about your parameter ? DIRTY ?");
1770 if (sdiHow == m_sdiState)
1771 return;
1773 m_arrCurrentSelection.clear();
1774 m_nFormsSelected = m_nControlsSelected = m_nHiddenControls = 0;
1775 m_bRootSelected = false;
1777 SvTreeListEntry* pSelectionLoop = FirstSelected();
1778 while (pSelectionLoop)
1780 // count different elements
1781 if (pSelectionLoop == m_pRootEntry)
1782 m_bRootSelected = true;
1783 else
1785 if (IsFormEntry(pSelectionLoop))
1786 ++m_nFormsSelected;
1787 else
1789 ++m_nControlsSelected;
1790 if (IsHiddenControl(static_cast<FmEntryData*>(pSelectionLoop->GetUserData())))
1791 ++m_nHiddenControls;
1795 if (sdiHow == SDI_NORMALIZED)
1797 // don't take something with a selected ancestor
1798 if (pSelectionLoop == m_pRootEntry)
1799 m_arrCurrentSelection.insert(pSelectionLoop);
1800 else
1802 SvTreeListEntry* pParentLoop = GetParent(pSelectionLoop);
1803 while (pParentLoop)
1805 // actually i would have to test, if parent is part of m_arr_CurrentSelection ...
1806 // but if it's selected, then it's in m_arrCurrentSelection
1807 // or one of its ancestors, which was selected earlier.
1808 // In both cases IsSelected is enough
1809 if (IsSelected(pParentLoop))
1810 break;
1811 else
1813 if (m_pRootEntry == pParentLoop)
1815 // until root (exclusive), there was no selected parent -> entry belongs to normalized list
1816 m_arrCurrentSelection.insert(pSelectionLoop);
1817 break;
1819 else
1820 pParentLoop = GetParent(pParentLoop);
1825 else if (sdiHow == SDI_NORMALIZED_FORMARK)
1827 SvTreeListEntry* pParent = GetParent(pSelectionLoop);
1828 if (!pParent || !IsSelected(pParent) || IsFormEntry(pSelectionLoop))
1829 m_arrCurrentSelection.insert(pSelectionLoop);
1831 else
1832 m_arrCurrentSelection.insert(pSelectionLoop);
1835 pSelectionLoop = NextSelected(pSelectionLoop);
1838 m_sdiState = sdiHow;
1842 void NavigatorTree::SynchronizeSelection(FmEntryDataArray& arredToSelect)
1844 LockSelectionHandling();
1845 if (arredToSelect.empty())
1847 SelectAll(false);
1849 else
1851 // compare current selection with requested SelectList
1852 SvTreeListEntry* pSelection = FirstSelected();
1853 while (pSelection)
1855 FmEntryData* pCurrent = static_cast<FmEntryData*>(pSelection->GetUserData());
1856 if (pCurrent != nullptr)
1858 FmEntryDataArray::iterator it = arredToSelect.find(pCurrent);
1859 if ( it != arredToSelect.end() )
1860 { // entry already selected, but also in SelectList
1861 // remove it from there
1862 arredToSelect.erase(it);
1863 } else
1864 { // entry selected, but not in SelectList -> remove selection
1865 Select(pSelection, false);
1866 // make it visible (maybe it's the only modification i do in this handler
1867 // so you should see it
1868 MakeVisible(pSelection);
1871 else
1872 Select(pSelection, false);
1874 pSelection = NextSelected(pSelection);
1877 // now SelectList contains only entries, which have to be selected
1878 // two possibilities : 1) run through SelectList, get SvTreeListEntry for every entry and select it (is more intuitive)
1879 // 2) run through my SvLBoxEntries and select those, i can find in the SelectList
1880 // 1) needs =(k*n) (k=length of SelectList, n=number of entries),
1881 // plus the fact, that FindEntry uses extensive IsEqualWithoutChilden instead of comparing pointer to UserData
1882 // 2) needs =(n*log k), duplicates some code from FindEntry
1883 // This may be a frequently used code ( at every change in mark of the view!),
1884 // so i use latter one
1885 SvTreeListEntry* pLoop = First();
1886 FmEntryDataArray::const_iterator aEnd = arredToSelect.end();
1887 while(pLoop)
1889 FmEntryData* pCurEntryData = static_cast<FmEntryData*>(pLoop->GetUserData());
1890 FmEntryDataArray::iterator it = arredToSelect.find(pCurEntryData);
1891 if (it != aEnd)
1893 Select(pLoop);
1894 MakeVisible(pLoop);
1895 SetCursor(pLoop, true);
1898 pLoop = Next(pLoop);
1901 UnlockSelectionHandling();
1905 void NavigatorTree::SynchronizeSelection()
1907 // shell and view
1908 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
1909 if(!pFormShell) return;
1911 FmFormView* pFormView = pFormShell->GetFormView();
1912 if (!pFormView) return;
1914 GetNavModel()->BroadcastMarkedObjects(pFormView->GetMarkedObjectList());
1918 void NavigatorTree::SynchronizeMarkList()
1920 // i'll need this shell
1921 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
1922 if (!pFormShell) return;
1924 CollectSelectionData(SDI_NORMALIZED_FORMARK);
1926 // the view shouldn't notify now if MarkList changed
1927 pFormShell->GetImpl()->EnableTrackProperties_Lock(false);
1929 UnmarkAllViewObj();
1931 for (SvTreeListEntry* pSelectionLoop : m_arrCurrentSelection)
1933 // When form selection, mark all controls of form
1934 if (IsFormEntry(pSelectionLoop) && (pSelectionLoop != m_pRootEntry))
1935 MarkViewObj(static_cast<FmFormData*>(pSelectionLoop->GetUserData()), false/*deep*/);
1937 // When control selection, mark Control-SdrObjects
1938 else if (IsFormComponentEntry(pSelectionLoop))
1940 FmControlData* pControlData = static_cast<FmControlData*>(pSelectionLoop->GetUserData());
1941 if (pControlData)
1944 // When HiddenControl no object can be selected
1945 Reference< XFormComponent > xFormComponent( pControlData->GetFormComponent());
1946 if (!xFormComponent.is())
1947 continue;
1948 Reference< XPropertySet > xSet(xFormComponent, UNO_QUERY);
1949 if (!xSet.is())
1950 continue;
1952 sal_uInt16 nClassId = ::comphelper::getINT16(xSet->getPropertyValue(FM_PROP_CLASSID));
1953 if (nClassId != FormComponentType::HIDDENCONTROL)
1954 MarkViewObj(pControlData);
1959 // if PropertyBrowser is open, I have to adopt it according to my selection
1960 // (Not as MarkList of view : if a form is selected, all belonging controls are selected in the view
1961 // but of course i want to see the form-properties
1962 ShowSelectionProperties();
1964 // reset flag at view
1965 pFormShell->GetImpl()->EnableTrackProperties_Lock(true);
1967 // if exactly one form is selected now, shell should notice it as CurrentForm
1968 // (if selection handling isn't locked, view cares about it in MarkListHasChanged
1969 // but mechanism doesn't work, if form is empty for example
1970 if ((m_arrCurrentSelection.size() == 1) && (m_nFormsSelected == 1))
1972 FmFormData* pSingleSelectionData = dynamic_cast<FmFormData*>( static_cast< FmEntryData* >( FirstSelected()->GetUserData() ) );
1973 DBG_ASSERT( pSingleSelectionData, "NavigatorTree::SynchronizeMarkList: invalid selected form!" );
1974 if ( pSingleSelectionData )
1976 InterfaceBag aSelection;
1977 aSelection.insert( Reference< XInterface >( pSingleSelectionData->GetFormIface(), UNO_QUERY ) );
1978 pFormShell->GetImpl()->setCurrentSelection_Lock(aSelection);
1984 bool NavigatorTree::IsHiddenControl(FmEntryData const * pEntryData)
1986 if (pEntryData == nullptr) return false;
1988 Reference< XPropertySet > xProperties( pEntryData->GetPropertySet() );
1989 if (::comphelper::hasProperty(FM_PROP_CLASSID, xProperties))
1991 Any aClassID = xProperties->getPropertyValue( FM_PROP_CLASSID );
1992 return (::comphelper::getINT16(aClassID) == FormComponentType::HIDDENCONTROL);
1994 return false;
1998 bool NavigatorTree::Select( SvTreeListEntry* pEntry, bool bSelect )
2000 if (bSelect == IsSelected(pEntry)) // this happens sometimes, maybe base class is to exact ;)
2001 return true;
2003 return SvTreeListBox::Select(pEntry, bSelect );
2007 void NavigatorTree::UnmarkAllViewObj()
2009 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
2010 if( !pFormShell )
2011 return;
2012 FmFormView* pFormView = pFormShell->GetFormView();
2013 pFormView->UnMarkAll();
2016 void NavigatorTree::MarkViewObj(FmFormData const * pFormData, bool bDeep )
2018 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
2019 if( !pFormShell )
2020 return;
2022 // first collect all sdrobjects
2023 ::std::set< Reference< XFormComponent > > aObjects;
2024 CollectObjects(pFormData,bDeep,aObjects);
2027 // find and select appropriate SdrObj in page
2028 FmFormView* pFormView = pFormShell->GetFormView();
2029 SdrPageView* pPageView = pFormView->GetSdrPageView();
2030 SdrPage* pPage = pPageView->GetPage();
2031 //FmFormPage* pFormPage = dynamic_cast< FmFormPage* >( pPage );
2033 SdrObjListIter aIter( pPage );
2034 while ( aIter.IsMore() )
2036 SdrObject* pSdrObject = aIter.Next();
2037 FmFormObj* pFormObject = FmFormObj::GetFormObject( pSdrObject );
2038 if ( !pFormObject )
2039 continue;
2041 Reference< XFormComponent > xControlModel( pFormObject->GetUnoControlModel(),UNO_QUERY );
2042 if ( xControlModel.is() && aObjects.find(xControlModel) != aObjects.end() && !pFormView->IsObjMarked( pSdrObject ) )
2044 // unfortunately, the writer doesn't like marking an already-marked object, again, so reset the mark first
2045 pFormView->MarkObj( pSdrObject, pPageView );
2047 } // while ( aIter.IsMore() )
2048 // make the mark visible
2049 ::tools::Rectangle aMarkRect( pFormView->GetAllMarkedRect());
2050 for ( sal_uInt32 i = 0; i < pFormView->PaintWindowCount(); ++i )
2052 SdrPaintWindow* pPaintWindow = pFormView->GetPaintWindow( i );
2053 OutputDevice& rOutDev = pPaintWindow->GetOutputDevice();
2054 if ( ( OUTDEV_WINDOW == rOutDev.GetOutDevType() ) && !aMarkRect.IsEmpty() )
2056 pFormView->MakeVisible( aMarkRect, static_cast<vcl::Window&>(rOutDev) );
2058 } // for ( sal_uInt32 i = 0; i < pFormView->PaintWindowCount(); ++i )
2061 void NavigatorTree::CollectObjects(FmFormData const * pFormData, bool bDeep, ::std::set< Reference< XFormComponent > >& _rObjects)
2063 FmEntryDataList* pChildList = pFormData->GetChildList();
2064 FmControlData* pControlData;
2065 for( size_t i = 0; i < pChildList->size(); ++i )
2067 FmEntryData* pEntryData = pChildList->at( i );
2068 if( dynamic_cast<const FmControlData*>( pEntryData) != nullptr )
2070 pControlData = static_cast<FmControlData*>(pEntryData);
2071 _rObjects.insert(pControlData->GetFormComponent());
2072 } // if( dynamic_cast<const FmControlData*>( pEntryData) != nullptr )
2073 else if (bDeep && (dynamic_cast<const FmFormData*>( pEntryData) != nullptr))
2074 CollectObjects(static_cast<FmFormData*>(pEntryData), bDeep, _rObjects);
2075 } // for( sal_uInt32 i=0; i<pChildList->Count(); i++ )
2078 void NavigatorTree::MarkViewObj( FmControlData const * pControlData)
2080 if( !pControlData )
2081 return;
2082 FmFormShell* pFormShell = GetNavModel()->GetFormShell();
2083 if( !pFormShell )
2084 return;
2087 // find and select appropriate SdrObj
2088 FmFormView* pFormView = pFormShell->GetFormView();
2089 Reference< XFormComponent > xFormComponent( pControlData->GetFormComponent());
2090 SdrPageView* pPageView = pFormView->GetSdrPageView();
2091 SdrPage* pPage = pPageView->GetPage();
2093 bool bPaint = false;
2094 SdrObjListIter aIter( pPage );
2095 while ( aIter.IsMore() )
2097 SdrObject* pSdrObject = aIter.Next();
2098 FmFormObj* pFormObject = FmFormObj::GetFormObject( pSdrObject );
2099 if ( !pFormObject )
2100 continue;
2102 Reference< XInterface > xControlModel( pFormObject->GetUnoControlModel() );
2103 if ( xControlModel != xFormComponent )
2104 continue;
2106 // mark the object
2107 if ( !pFormView->IsObjMarked( pSdrObject ) )
2108 // unfortunately, the writer doesn't like marking an already-marked object, again, so reset the mark first
2109 pFormView->MarkObj( pSdrObject, pPageView );
2111 bPaint = true;
2113 } // while ( aIter.IsMore() )
2114 if ( bPaint )
2116 // make the mark visible
2117 ::tools::Rectangle aMarkRect( pFormView->GetAllMarkedRect());
2118 for ( sal_uInt32 i = 0; i < pFormView->PaintWindowCount(); ++i )
2120 SdrPaintWindow* pPaintWindow = pFormView->GetPaintWindow( i );
2121 OutputDevice& rOutDev = pPaintWindow->GetOutputDevice();
2122 if ( OUTDEV_WINDOW == rOutDev.GetOutDevType() )
2124 pFormView->MakeVisible( aMarkRect, static_cast<vcl::Window&>(rOutDev) );
2126 } // for ( sal_uInt32 i = 0; i < pFormView->PaintWindowCount(); ++i )
2134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */