1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
31 #include <strings.hrc>
33 #include <DrawViewShell.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>
47 LayerTabBar::LayerTabBar(DrawViewShell
* pViewSh
, vcl::Window
* pParent
)
48 : TabBar( pParent
, WinBits( WB_BORDER
| WB_3DLOOK
| WB_SCROLL
) ),
49 DropTargetHelper( this ),
53 SetSizePixel(Size(0, 0));
54 SetMaxPageWidth( 150 );
55 SetHelpId( HID_SD_TABBAR_LAYERS
);
58 LayerTabBar::~LayerTabBar()
63 void LayerTabBar::dispose()
65 DropTargetHelper::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
);
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;
146 Point aPosPixel
= rMEvt
.GetPosPixel();
147 sal_uInt16 aTabId
= GetPageId( PixelToLogic(aPosPixel
) );
150 SfxDispatcher
* pDispatcher
= pDrViewSh
->GetViewFrame()->GetDispatcher();
151 pDispatcher
->Execute(SID_INSERTLAYER
, SfxCallMode::SYNCHRON
);
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();
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();
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
);
213 assert (pDrView
&& "Change layer attribute undo action is only working with a SdDrawView");
216 SfxUndoManager
* pManager
= rDoc
.GetDocSh()->GetUndoManager();
217 std::unique_ptr
<SdLayerModifyUndoAction
> pAction(new SdLayerModifyUndoAction(
222 pLayer
->GetDescription(),
228 pLayer
->GetDescription(),
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
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
);
262 sal_Int8
LayerTabBar::AcceptDrop( const AcceptDropEvent
& rEvt
)
264 sal_Int8 nRet
= DND_ACTION_NONE
;
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
);
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
);
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()
312 OUString aLayerName
= GetLayerName( GetEditPageId() );
314 if ( IsRealNameOfStandardLayer(aLayerName
))
316 // It is not allowed to change these names
321 ::sd::View
* pView
= pDrViewSh
->GetView();
323 if ( pView
->IsTextEdit() )
325 pView
->SdrEndTextEdit();
332 TabBarAllowRenamingReturnCode
LayerTabBar::AllowRenaming()
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
)));
356 if ( IsLocalizedNameOfStandardLayer(aNewName
) || IsRealNameOfStandardLayer(aNewName
) )
358 // Standard layer names may not be changed.
363 return bOK
? TABBAR_RENAMING_YES
: TABBAR_RENAMING_NO
;
366 void LayerTabBar::EndRenaming()
368 if( IsEditModeCanceled() )
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
);
382 OUString
aNewName( GetEditText() );
383 assert (pDrView
&& "Rename layer undo action is only working with a SdDrawView");
386 SfxUndoManager
* pManager
= rDoc
.GetDocSh()->GetUndoManager();
387 std::unique_ptr
<SdLayerModifyUndoAction
> pAction(new SdLayerModifyUndoAction(
392 pLayer
->GetDescription(),
393 pDrView
->IsLayerVisible(aLayerName
),
394 pDrView
->IsLayerLocked(aLayerName
),
395 pDrView
->IsLayerPrintable(aLayerName
),
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
);
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: */