xref: /aoo41x/main/sd/source/ui/dlg/LayerTabBar.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #include "LayerTabBar.hxx"
32 #include <svx/svdlayer.hxx>
33 #include <svx/svdpagv.hxx>
34 #include <vcl/msgbox.hxx>
35 #include <sfx2/dispatch.hxx>
36 
37 
38 #include "sdattr.hxx"
39 #include "app.hxx"
40 #include "helpids.h"
41 #include "app.hrc"
42 #include "glob.hrc"
43 #include "strings.hrc"
44 #include "res_bmp.hrc"
45 #include "DrawViewShell.hxx"
46 #include "Window.hxx"
47 #include "View.hxx"
48 #include "drawdoc.hxx"
49 #include "sdresid.hxx"
50 #include "DrawDocShell.hxx"
51 #include "drawview.hxx"
52 #include "undolayer.hxx"
53 
54 
55 namespace sd {
56 
57 #define SWITCH_TIMEOUT  20
58 
59 
60 /*************************************************************************
61 |*
62 |* Standard-Konstruktor
63 |*
64 \************************************************************************/
65 
66 LayerTabBar::LayerTabBar(DrawViewShell* pViewSh, Window* pParent)
67     : TabBar( pParent, WinBits( WB_BORDER | WB_3DLOOK | WB_SCROLL | WB_SIZEABLE ) ),
68 	DropTargetHelper( this ),
69 	pDrViewSh(pViewSh)
70 {
71 	EnableEditMode();
72 	SetSizePixel(Size(0, 0));
73 	SetMaxPageWidth( 150 );
74     SetHelpId( HID_SD_TABBAR_LAYERS );
75 }
76 
77 
78 LayerTabBar::LayerTabBar (
79     DrawViewShell* pViewSh,
80     Window* pParent,
81     const ResId& rResId)
82     : TabBar (pParent, rResId.GetWinBits()),
83 	DropTargetHelper( this ),
84 	pDrViewSh(pViewSh)
85 {
86 	EnableEditMode();
87 }
88 
89 /*************************************************************************
90 |*
91 |* Destruktor
92 |*
93 \************************************************************************/
94 
95 LayerTabBar::~LayerTabBar()
96 {
97 }
98 
99 /*************************************************************************
100 |*
101 \************************************************************************/
102 
103 void LayerTabBar::Select()
104 {
105 	SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
106 	pDispatcher->Execute(SID_SWITCHLAYER, SFX_CALLMODE_ASYNCHRON);
107 }
108 
109 /*************************************************************************
110 |*
111 \************************************************************************/
112 
113 void LayerTabBar::MouseButtonDown(const MouseEvent& rMEvt)
114 {
115 	sal_Bool bSetPageID=sal_False;
116 
117 	if (rMEvt.IsLeft() && !rMEvt.IsMod1() && !rMEvt.IsMod2())
118 	{
119 		Point aPosPixel = rMEvt.GetPosPixel();
120 		sal_uInt16 aLayerId = GetPageId( PixelToLogic(aPosPixel) );
121 
122 		if (aLayerId == 0)
123 		{
124 			SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
125 			pDispatcher->Execute(SID_INSERTLAYER, SFX_CALLMODE_SYNCHRON);
126 
127 			bSetPageID=sal_True;
128 		}
129 		else if (rMEvt.IsShift())
130 		{
131 			// Toggle zw. Layer sichtbar / unsichtbar
132 			String aName(GetPageText(aLayerId));
133 			SdrPageView* pPV = pDrViewSh->GetView()->GetSdrPageView();
134 			sal_Bool bVisible = pPV->IsLayerVisible(aName);
135 			pPV->SetLayerVisible(aName, !bVisible);
136 			pDrViewSh->ResetActualLayer();
137 		}
138 	}
139 
140 	// If you insert a new layer you must not call TabBar::MouseButtonDown(rMEvt);
141 	// because you want to activate the new layer
142 	if( !bSetPageID )
143 		TabBar::MouseButtonDown(rMEvt);
144 }
145 
146 /*************************************************************************
147 |*
148 \************************************************************************/
149 
150 void LayerTabBar::DoubleClick()
151 {
152 	if (GetCurPageId() != 0)
153 	{
154 		SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
155 		pDispatcher->Execute( SID_MODIFYLAYER, SFX_CALLMODE_SYNCHRON );
156 	}
157 }
158 
159 
160 /*************************************************************************
161 |*
162 |* AcceptDrop-Event
163 |*
164 \************************************************************************/
165 
166 sal_Int8 LayerTabBar::AcceptDrop( const AcceptDropEvent& rEvt )
167 {
168 	sal_Int8 nRet = DND_ACTION_NONE;
169 
170 	if( rEvt.mbLeaving )
171 		EndSwitchPage();
172 
173 	if( !pDrViewSh->GetDocSh()->IsReadOnly() )
174 	{
175 		sal_uInt16			nPageId = SDRPAGE_NOTFOUND;
176 		Point			aPos( PixelToLogic( rEvt.maPosPixel ) );
177 		sal_uInt16			nLayerId = pDrViewSh->GetView()->GetDoc()->GetLayerAdmin().GetLayerID( GetPageText( GetPageId( aPos ) ), sal_False );
178 
179 		nRet = pDrViewSh->AcceptDrop( rEvt, *this, NULL, nPageId, nLayerId );
180 
181 		SwitchPage( aPos );
182 	}
183 
184 	return nRet;
185 }
186 
187 /*************************************************************************
188 |*
189 |* ExecuteDrop-Event
190 |*
191 \************************************************************************/
192 
193 sal_Int8 LayerTabBar::ExecuteDrop( const ExecuteDropEvent& rEvt )
194 {
195 	sal_uInt16			nPageId = SDRPAGE_NOTFOUND;
196 	sal_uInt16			nLayerId = pDrViewSh->GetView()->GetDoc()->GetLayerAdmin().GetLayerID( GetPageText( GetPageId( PixelToLogic( rEvt.maPosPixel ) ) ), sal_False );
197 	sal_Int8		nRet = pDrViewSh->ExecuteDrop( rEvt, *this, NULL, nPageId, nLayerId );
198 
199 	EndSwitchPage();
200 
201 	return nRet;
202 
203 }
204 
205 /*************************************************************************
206 |*
207 \************************************************************************/
208 
209 void  LayerTabBar::Command(const CommandEvent& rCEvt)
210 {
211 	if ( rCEvt.GetCommand() == COMMAND_CONTEXTMENU )
212 	{
213 		SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
214 		pDispatcher->ExecutePopup(SdResId(RID_LAYERTAB_POPUP));
215 	}
216 }
217 
218 
219 /*************************************************************************
220 |*
221 \************************************************************************/
222 long LayerTabBar::StartRenaming()
223 {
224 	sal_Bool bOK = sal_True;
225 	String aLayerName = GetPageText( GetEditPageId() );
226 	String aLayoutLayer ( SdResId(STR_LAYER_LAYOUT) );
227 	String aControlsLayer ( SdResId(STR_LAYER_CONTROLS) );
228 	String aMeasureLinesLayer ( SdResId(STR_LAYER_MEASURELINES) );
229 	String aBackgroundLayer( SdResId(STR_LAYER_BCKGRND) );
230 	String aBackgroundObjLayer( SdResId(STR_LAYER_BCKGRNDOBJ) );
231 
232 	if ( aLayerName == aLayoutLayer       || aLayerName == aControlsLayer  ||
233 		 aLayerName == aMeasureLinesLayer ||
234 		 aLayerName == aBackgroundLayer   || aLayerName == aBackgroundObjLayer )
235 	{
236 		// Diese Namen duerfen nicht veraendert werden
237 		bOK = sal_False;
238 	}
239 	else
240 	{
241 		::sd::View* pView = pDrViewSh->GetView();
242 
243 		if ( pView->IsTextEdit() )
244 		{
245 			pView->SdrEndTextEdit();
246 		}
247 	}
248 
249 	return(bOK);
250 }
251 
252 /*************************************************************************
253 |*
254 \************************************************************************/
255 
256 long LayerTabBar::AllowRenaming()
257 {
258 	sal_Bool bOK = sal_True;
259 
260 	// Ueberpruefung auf schon vorhandene Namen
261 	::sd::View* pView = pDrViewSh->GetView();
262 	SdDrawDocument* pDoc = pView->GetDoc();
263 	String aLayerName = pView->GetActiveLayer();
264 	SdrLayerAdmin& rLayerAdmin = pDoc->GetLayerAdmin();
265 	String aNewName( GetEditText() );
266 
267 	if ( aNewName.Len() == 0 ||
268 		(rLayerAdmin.GetLayer( aNewName, sal_False ) && aLayerName != aNewName) )
269 	{
270 		// Name ist schon vorhanden
271 		WarningBox aWarningBox( &pDrViewSh->GetViewFrame()->GetWindow(), WinBits( WB_OK ),
272 								String(SdResId( STR_WARN_NAME_DUPLICATE ) ) );
273 		aWarningBox.Execute();
274 		bOK = sal_False;
275 	}
276 
277 	if (bOK)
278 	{
279 		String aLayoutLayer ( SdResId(STR_LAYER_LAYOUT) );
280 		String aControlsLayer ( SdResId(STR_LAYER_CONTROLS) );
281 		String aMeasureLinesLayer ( SdResId(STR_LAYER_MEASURELINES) );
282 		String aBackgroundLayer( SdResId(STR_LAYER_BCKGRND) );
283 		String aBackgroundObjLayer( SdResId(STR_LAYER_BCKGRNDOBJ) );
284 
285 		if ( aNewName == aLayoutLayer       || aNewName == aControlsLayer  ||
286 			 aNewName == aMeasureLinesLayer ||
287 			 aNewName == aBackgroundLayer   || aNewName == aBackgroundObjLayer )
288 		{
289 			// Diese Namen duerfen nicht vergeben werden
290 			bOK = sal_False;
291 		}
292 	}
293 
294 	return(bOK);
295 }
296 
297 /*************************************************************************
298 |*
299 \************************************************************************/
300 
301 void LayerTabBar::EndRenaming()
302 {
303 	if( !IsEditModeCanceled() )
304 	{
305 		::sd::View* pView = pDrViewSh->GetView();
306 		DrawView* pDrView = PTR_CAST( DrawView, pView );
307 
308 		SdDrawDocument* pDoc = pView->GetDoc();
309 		String aLayerName = pView->GetActiveLayer();
310 		SdrLayerAdmin& rLayerAdmin = pDoc->GetLayerAdmin();
311 		SdrLayer* pLayer = rLayerAdmin.GetLayer(aLayerName, sal_False);
312 
313 		if (pLayer)
314 		{
315 			String aNewName( GetEditText() );
316 
317 			DBG_ASSERT( pDrView, "Rename layer undo action is only working with a SdDrawView" );
318 			if( pDrView )
319 			{
320 				::svl::IUndoManager* pManager = pDoc->GetDocSh()->GetUndoManager();
321 				SdLayerModifyUndoAction* pAction = new SdLayerModifyUndoAction(
322 					pDoc,
323 					pLayer,
324 					aLayerName,
325 					pLayer->GetTitle(),
326 					pLayer->GetDescription(),
327 					pDrView->IsLayerVisible(aLayerName),
328 					pDrView->IsLayerLocked(aLayerName),
329 					pDrView->IsLayerPrintable(aLayerName),
330 					aNewName,
331 					pLayer->GetTitle(),
332 					pLayer->GetDescription(),
333 					pDrView->IsLayerVisible(aLayerName),
334 					pDrView->IsLayerLocked(aLayerName),
335 					pDrView->IsLayerPrintable(aLayerName)
336 					);
337 				pManager->AddUndoAction( pAction );
338 			}
339 
340 			// Zuerst View benachrichtigen, da innerhalb von SetName() schon
341 			// ResetActualLayer() gerufen wird und an der View der Layer dann
342 			// schon bekannt sein muss.
343 			pView->SetActiveLayer(aNewName);
344 			pLayer->SetName(aNewName);
345 			pDoc->SetChanged(sal_True);
346 		}
347 	}
348 }
349 
350 
351 /*************************************************************************
352 |*
353 \************************************************************************/
354 
355 void LayerTabBar::ActivatePage()
356 {
357 	if ( /*IsInSwitching*/ 1 && pDrViewSh!=NULL)
358 	{
359 
360         SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
361 		pDispatcher->Execute(SID_SWITCHLAYER, SFX_CALLMODE_ASYNCHRON);
362 	}
363 }
364 
365 
366 
367 
368 void LayerTabBar::SendActivatePageEvent (void)
369 {
370     CallEventListeners (VCLEVENT_TABBAR_PAGEACTIVATED,
371         reinterpret_cast<void*>(GetCurPageId()));
372 }
373 
374 
375 
376 
377 void LayerTabBar::SendDeactivatePageEvent (void)
378 {
379 	CallEventListeners (VCLEVENT_TABBAR_PAGEDEACTIVATED,
380         reinterpret_cast<void*>(GetCurPageId()));
381 }
382 
383 } // end of namespace sd
384