bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / dlg / LayerTabBar.cxx
blobc37aa9637885cfb4aedf0cdf4ac51bc4737b43a3
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 <LayerTabBar.hxx>
21 #include <svx/svdlayer.hxx>
22 #include <svx/svdpagv.hxx>
23 #include <vcl/commandevent.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/weld.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <sfx2/viewfrm.hxx>
29 #include <helpids.h>
30 #include <app.hrc>
31 #include <strings.hrc>
33 #include <DrawViewShell.hxx>
34 #include <View.hxx>
35 #include <drawdoc.hxx>
36 #include <sdresid.hxx>
37 #include <unokywds.hxx>
38 #include <DrawDocShell.hxx>
39 #include <drawview.hxx>
40 #include <undolayer.hxx>
42 namespace sd {
44 /**
45 * default constructor
47 LayerTabBar::LayerTabBar(DrawViewShell* pViewSh, vcl::Window* pParent)
48 : TabBar( pParent, WinBits( WB_BORDER | WB_3DLOOK | WB_SCROLL ) ),
49 DropTargetHelper( this ),
50 pDrViewSh(pViewSh)
52 EnableEditMode();
53 SetSizePixel(Size(0, 0));
54 SetMaxPageWidth( 150 );
55 SetHelpId( HID_SD_TABBAR_LAYERS );
58 LayerTabBar::~LayerTabBar()
60 disposeOnce();
63 void LayerTabBar::dispose()
65 DropTargetHelper::dispose();
66 TabBar::dispose();
69 OUString LayerTabBar::convertToLocalizedName(const OUString& rName)
71 if ( rName == sUNO_LayerName_background )
72 return SdResId( STR_LAYER_BCKGRND );
74 if ( rName == sUNO_LayerName_background_objects )
75 return SdResId( STR_LAYER_BCKGRNDOBJ );
77 if ( rName == sUNO_LayerName_layout )
78 return SdResId( STR_LAYER_LAYOUT );
80 if ( rName == sUNO_LayerName_controls )
81 return SdResId( STR_LAYER_CONTROLS );
83 if ( rName == sUNO_LayerName_measurelines )
84 return SdResId( STR_LAYER_MEASURELINES );
86 return rName;
89 // Use a method name, that is specific to LayerTabBar to make code better readable
90 OUString LayerTabBar::GetLayerName(sal_uInt16 nPageId) const
92 return GetAuxiliaryText(nPageId);
95 void LayerTabBar::SetLayerName( sal_uInt16 nPageId, const OUString& rText )
97 SetAuxiliaryText(nPageId, rText);
100 // Here "Page" is a tab in the LayerTabBar.
101 void LayerTabBar::InsertPage( sal_uInt16 nPageId, const OUString& rText,
102 TabBarPageBits nBits, sal_uInt16 nPos)
104 OUString sLocalizedName(convertToLocalizedName(rText));
105 TabBar::InsertPage(nPageId, sLocalizedName, nBits, nPos );
106 SetLayerName(nPageId, rText);
109 void LayerTabBar::SetPageText( sal_uInt16 nPageId, const OUString& rText )
111 OUString sLocalizedName(convertToLocalizedName(rText));
112 SetLayerName(nPageId, rText);
113 TabBar::SetPageText(nPageId, sLocalizedName);
116 bool LayerTabBar::IsLocalizedNameOfStandardLayer(const OUString& rName)
118 return ( rName == SdResId(STR_LAYER_LAYOUT)
119 || rName == SdResId(STR_LAYER_CONTROLS)
120 || rName == SdResId(STR_LAYER_MEASURELINES)
121 || rName == SdResId(STR_LAYER_BCKGRND)
122 || rName == SdResId(STR_LAYER_BCKGRNDOBJ) );
125 bool LayerTabBar::IsRealNameOfStandardLayer(const OUString& rName)
127 return ( rName == sUNO_LayerName_layout
128 || rName == sUNO_LayerName_controls
129 || rName == sUNO_LayerName_measurelines
130 || rName == sUNO_LayerName_background
131 || rName == sUNO_LayerName_background_objects );
134 void LayerTabBar::Select()
136 SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
137 pDispatcher->Execute(SID_SWITCHLAYER, SfxCallMode::ASYNCHRON);
140 void LayerTabBar::MouseButtonDown(const MouseEvent& rMEvt)
142 bool bSetPageID=false;
144 if (rMEvt.IsLeft())
146 Point aPosPixel = rMEvt.GetPosPixel();
147 sal_uInt16 aTabId = GetPageId( PixelToLogic(aPosPixel) );
148 if (aTabId == 0)
150 SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
151 pDispatcher->Execute(SID_INSERTLAYER, SfxCallMode::SYNCHRON);
153 bSetPageID=true;
155 else if (rMEvt.IsMod2())
157 // direct editing of tab text
158 // make sure the clicked tab is the current tab otherwise Edit() acts on the wrong tab
159 if ( aTabId != GetCurPageId())
161 MouseEvent aSyntheticEvent (rMEvt.GetPosPixel(), 1, MouseEventModifiers::SYNTHETIC, MOUSE_LEFT, 0);
162 TabBar::MouseButtonDown(aSyntheticEvent);
165 else if (rMEvt.IsMod1() || rMEvt.IsShift())
167 // keyboard Shortcuts to change layer attributes
169 OUString aName(GetLayerName(aTabId));
170 SdrPageView* pPV = pDrViewSh->GetView()->GetSdrPageView();
172 // Save old state
174 bool bOldPrintable = pPV->IsLayerPrintable(aName);
175 bool bOldVisible = pPV->IsLayerVisible(aName);
176 bool bOldLocked = pPV->IsLayerLocked(aName);
178 bool bNewPrintable = bOldPrintable;
179 bool bNewVisible = bOldVisible;
180 bool bNewLocked = bOldLocked;
182 if (rMEvt.IsMod1() && rMEvt.IsShift())
184 // Shift+Ctrl: Toggle between layer printable / not printable
185 bNewPrintable = !bOldPrintable;
186 pPV->SetLayerPrintable(aName, bNewPrintable);
188 else if (rMEvt.IsShift())
190 // Shift: Toggle between layer visible / hidden
191 bNewVisible = !bOldVisible;
192 pPV->SetLayerVisible(aName, bNewVisible);
194 else // if (rMEvt.IsMod1())
196 // Ctrl: Toggle between layer locked / unlocked
197 bNewLocked = !bOldLocked;
198 pPV->SetLayerLocked(aName, bNewLocked);
201 pDrViewSh->ResetActualLayer();
203 // Add Undo action
205 ::sd::View* pView = pDrViewSh->GetView();
206 DrawView* pDrView = dynamic_cast<DrawView*>(pView);
208 SdDrawDocument& rDoc = pView->GetDoc();
209 SdrLayer* pLayer = rDoc.GetLayerAdmin().GetLayer(aName);
211 if (pLayer)
213 assert (pDrView && "Change layer attribute undo action is only working with a SdDrawView");
214 if(pDrView)
216 SfxUndoManager* pManager = rDoc.GetDocSh()->GetUndoManager();
217 std::unique_ptr<SdLayerModifyUndoAction> pAction(new SdLayerModifyUndoAction(
218 &rDoc,
219 pLayer,
220 aName,
221 pLayer->GetTitle(),
222 pLayer->GetDescription(),
223 bOldVisible,
224 bOldLocked,
225 bOldPrintable,
226 aName,
227 pLayer->GetTitle(),
228 pLayer->GetDescription(),
229 bNewVisible,
230 bNewLocked,
231 bNewPrintable
233 pManager->AddUndoAction(std::move(pAction));
237 // Mark document changed
239 pView->GetDoc().SetChanged();
243 // If you insert a new layer you must not call TabBar::MouseButtonDown(rMEvt);
244 // because you want to activate the new layer
245 if( !bSetPageID )
246 TabBar::MouseButtonDown(rMEvt);
249 void LayerTabBar::DoubleClick()
251 if (GetCurPageId() != 0)
253 SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
254 pDispatcher->Execute( SID_MODIFYLAYER, SfxCallMode::SYNCHRON );
259 * AcceptDrop-Event
262 sal_Int8 LayerTabBar::AcceptDrop( const AcceptDropEvent& rEvt )
264 sal_Int8 nRet = DND_ACTION_NONE;
266 if( rEvt.mbLeaving )
267 EndSwitchPage();
269 if( !pDrViewSh->GetDocSh()->IsReadOnly() )
271 Point aPos( PixelToLogic( rEvt.maPosPixel ) );
272 OUString sLayerName( GetLayerName(GetPageId(aPos)) );
273 SdrLayerID nLayerId = pDrViewSh->GetView()->GetDoc().GetLayerAdmin().GetLayerID(sLayerName);
275 nRet = pDrViewSh->AcceptDrop( rEvt, *this, nullptr, SDRPAGE_NOTFOUND, nLayerId );
277 SwitchPage( aPos );
280 return nRet;
284 * ExecuteDrop-Event
286 sal_Int8 LayerTabBar::ExecuteDrop( const ExecuteDropEvent& rEvt )
288 Point aPos( PixelToLogic(rEvt.maPosPixel) );
289 OUString sLayerName( GetLayerName(GetPageId(aPos)) );
290 SdrLayerID nLayerId = pDrViewSh->GetView()->GetDoc().GetLayerAdmin().GetLayerID(sLayerName);
292 sal_Int8 nRet = pDrViewSh->ExecuteDrop( rEvt, *this, nullptr, SDRPAGE_NOTFOUND, nLayerId );
294 EndSwitchPage();
296 return nRet;
300 void LayerTabBar::Command(const CommandEvent& rCEvt)
302 if ( rCEvt.GetCommand() == CommandEventId::ContextMenu )
304 SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
305 pDispatcher->ExecutePopup("layertab");
309 bool LayerTabBar::StartRenaming()
311 bool bOK = true;
312 OUString aLayerName = GetLayerName( GetEditPageId() );
314 if ( IsRealNameOfStandardLayer(aLayerName))
316 // It is not allowed to change these names
317 bOK = false;
319 else
321 ::sd::View* pView = pDrViewSh->GetView();
323 if ( pView->IsTextEdit() )
325 pView->SdrEndTextEdit();
329 return bOK;
332 TabBarAllowRenamingReturnCode LayerTabBar::AllowRenaming()
334 bool bOK = true;
336 // Check if names already exists
337 ::sd::View* pView = pDrViewSh->GetView();
338 SdDrawDocument& rDoc = pView->GetDoc();
339 OUString aLayerName = pView->GetActiveLayer();
340 SdrLayerAdmin& rLayerAdmin = rDoc.GetLayerAdmin();
341 OUString aNewName( GetEditText() );
343 if (aNewName.isEmpty() ||
344 (rLayerAdmin.GetLayer( aNewName ) && aLayerName != aNewName) )
346 // Name already exists.
347 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(pDrViewSh->GetViewFrame()->GetWindow().GetFrameWeld(),
348 VclMessageType::Warning, VclButtonsType::Ok,
349 SdResId(STR_WARN_NAME_DUPLICATE)));
350 xWarn->run();
351 bOK = false;
354 if (bOK)
356 if ( IsLocalizedNameOfStandardLayer(aNewName) || IsRealNameOfStandardLayer(aNewName) )
358 // Standard layer names may not be changed.
359 bOK = false;
363 return bOK ? TABBAR_RENAMING_YES : TABBAR_RENAMING_NO;
366 void LayerTabBar::EndRenaming()
368 if( IsEditModeCanceled() )
369 return;
371 ::sd::View* pView = pDrViewSh->GetView();
372 DrawView* pDrView = dynamic_cast<DrawView*>( pView );
374 SdDrawDocument& rDoc = pView->GetDoc();
375 OUString aLayerName = pView->GetActiveLayer();
376 SdrLayerAdmin& rLayerAdmin = rDoc.GetLayerAdmin();
377 SdrLayer* pLayer = rLayerAdmin.GetLayer(aLayerName);
379 if (!pLayer)
380 return;
382 OUString aNewName( GetEditText() );
383 assert (pDrView && "Rename layer undo action is only working with a SdDrawView");
384 if( pDrView )
386 SfxUndoManager* pManager = rDoc.GetDocSh()->GetUndoManager();
387 std::unique_ptr<SdLayerModifyUndoAction> pAction(new SdLayerModifyUndoAction(
388 &rDoc,
389 pLayer,
390 aLayerName,
391 pLayer->GetTitle(),
392 pLayer->GetDescription(),
393 pDrView->IsLayerVisible(aLayerName),
394 pDrView->IsLayerLocked(aLayerName),
395 pDrView->IsLayerPrintable(aLayerName),
396 aNewName,
397 pLayer->GetTitle(),
398 pLayer->GetDescription(),
399 pDrView->IsLayerVisible(aLayerName),
400 pDrView->IsLayerLocked(aLayerName),
401 pDrView->IsLayerPrintable(aLayerName)
403 pManager->AddUndoAction( std::move(pAction) );
406 // First notify View since SetName() calls ResetActualLayer() and
407 // the View then already has to know the Layer
408 pView->SetActiveLayer(aNewName);
409 pLayer->SetName(aNewName);
410 rDoc.SetChanged();
413 void LayerTabBar::ActivatePage()
415 if (pDrViewSh!=nullptr)
418 SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
419 pDispatcher->Execute(SID_SWITCHLAYER, SfxCallMode::ASYNCHRON);
423 void LayerTabBar::SendActivatePageEvent()
425 CallEventListeners (VclEventId::TabbarPageActivated,
426 reinterpret_cast<void*>(GetCurPageId()));
429 void LayerTabBar::SendDeactivatePageEvent()
431 CallEventListeners (VclEventId::TabbarPageDeactivated,
432 reinterpret_cast<void*>(GetCurPageId()));
435 } // end of namespace sd
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */