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 <com/sun/star/frame/XFrame.hpp>
32 #include <com/sun/star/text/WritingMode.hpp>
33 #include <com/sun/star/frame/status/FontHeight.hpp>
34 #include <com/sun/star/frame/XDispatchProvider.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/drawing/DrawViewMode.hpp>
38 
39 #include <memory>
40 #include <boost/scoped_ptr.hpp>
41 
42 #include <vos/mutex.hxx>
43 
44 #include <vcl/svapp.hxx>
45 #include <vcl/toolbox.hxx>
46 
47 #include <svl/languageoptions.hxx>
48 
49 #include <svtools/ctrltool.hxx>
50 #include <svtools/ctrlbox.hxx>
51 #include <svtools/toolbarmenu.hxx>
52 #include <svtools/valueset.hxx>
53 
54 #include <toolkit/helper/vclunohelper.hxx>
55 
56 #include <sfx2/imagemgr.hxx>
57 
58 #include "app.hrc"
59 #include "glob.hrc"
60 #include "strings.hrc"
61 #include "res_bmp.hrc"
62 #include "sdresid.hxx"
63 #include "pres.hxx"
64 #include "slidelayoutcontroller.hxx"
65 
66 using rtl::OUString;
67 
68 using namespace ::com::sun::star;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::lang;
71 using namespace ::com::sun::star::text;
72 using namespace ::com::sun::star::frame;
73 using namespace ::com::sun::star::drawing;
74 using namespace ::com::sun::star::beans;
75 
76 namespace sd
77 {
78 
79 extern ::rtl::OUString ImplRetrieveLabelFromCommand( const Reference< XFrame >& xFrame, const OUString& aCmdURL );
80 
81 // -----------------------------------------------------------------------
82 
83 class LayoutToolbarMenu : public svtools::ToolbarMenu
84 {
85 public:
86     LayoutToolbarMenu( SlideLayoutController& rController, const Reference< XFrame >& xFrame, ::Window* pParent, const bool bInsertPage );
87     virtual ~LayoutToolbarMenu();
88 
89 protected:
90 	DECL_LINK( SelectHdl, void * );
91 
92 private:
93 	SlideLayoutController& mrController;
94     Reference< XFrame > mxFrame;
95 	bool mbInsertPage;
96 	ValueSet* mpLayoutSet1;
97 	ValueSet* mpLayoutSet2;
98 };
99 
100 // -----------------------------------------------------------------------
101 
102 struct snewfoil_value_info
103 {
104     sal_uInt16 mnBmpResId;
105     sal_uInt16 mnHCBmpResId;
106     sal_uInt16 mnStrResId;
107     WritingMode meWritingMode;
108     AutoLayout maAutoLayout;
109 };
110 
111 static snewfoil_value_info notes[] =
112 {
113     {BMP_FOILN_01, BMP_FOILN_01_H, STR_AUTOLAYOUT_NOTES, WritingMode_LR_TB,
114      AUTOLAYOUT_NOTES},
115     {0, 0, 0, WritingMode_LR_TB, AUTOLAYOUT_NONE},
116 };
117 
118 static snewfoil_value_info handout[] =
119 {
120     {BMP_FOILH_01, BMP_FOILH_01_H, STR_AUTOLAYOUT_HANDOUT1, WritingMode_LR_TB,
121      AUTOLAYOUT_HANDOUT1},
122     {BMP_FOILH_02, BMP_FOILH_02_H, STR_AUTOLAYOUT_HANDOUT2, WritingMode_LR_TB,
123      AUTOLAYOUT_HANDOUT2},
124     {BMP_FOILH_03, BMP_FOILH_03_H, STR_AUTOLAYOUT_HANDOUT3, WritingMode_LR_TB,
125      AUTOLAYOUT_HANDOUT3},
126     {BMP_FOILH_04, BMP_FOILH_04_H, STR_AUTOLAYOUT_HANDOUT4, WritingMode_LR_TB,
127      AUTOLAYOUT_HANDOUT4},
128     {BMP_FOILH_06, BMP_FOILH_06_H, STR_AUTOLAYOUT_HANDOUT6, WritingMode_LR_TB,
129      AUTOLAYOUT_HANDOUT6},
130     {BMP_FOILH_09, BMP_FOILH_09_H, STR_AUTOLAYOUT_HANDOUT9, WritingMode_LR_TB,
131      AUTOLAYOUT_HANDOUT9},
132     {0, 0, 0, WritingMode_LR_TB, AUTOLAYOUT_NONE},
133 };
134 
135 static snewfoil_value_info standard[] =
136 {
137     {BMP_LAYOUT_EMPTY, BMP_LAYOUT_EMPTY_H, STR_AUTOLAYOUT_NONE, WritingMode_LR_TB,        AUTOLAYOUT_NONE},
138 	{BMP_LAYOUT_HEAD03, BMP_LAYOUT_HEAD03_H, STR_AUTOLAYOUT_TITLE, WritingMode_LR_TB,       AUTOLAYOUT_TITLE},
139     {BMP_LAYOUT_HEAD02, BMP_LAYOUT_HEAD02_H, STR_AUTOLAYOUT_CONTENT, WritingMode_LR_TB,        AUTOLAYOUT_ENUM},
140 	{BMP_LAYOUT_HEAD02A, BMP_LAYOUT_HEAD02A_H, STR_AUTOLAYOUT_2CONTENT, WritingMode_LR_TB,       AUTOLAYOUT_2TEXT},
141 	{BMP_LAYOUT_HEAD01, BMP_LAYOUT_HEAD01_H, STR_AUTOLAYOUT_ONLY_TITLE, WritingMode_LR_TB,  AUTOLAYOUT_ONLY_TITLE},
142 	{BMP_LAYOUT_TEXTONLY, BMP_LAYOUT_TEXTONLY_H, STR_AUTOLAYOUT_ONLY_TEXT, WritingMode_LR_TB,   AUTOLAYOUT_ONLY_TEXT},
143     {BMP_LAYOUT_HEAD03B, BMP_LAYOUT_HEAD03B_H, STR_AUTOLAYOUT_2CONTENT_CONTENT, WritingMode_LR_TB,    AUTOLAYOUT_2OBJTEXT},
144 	{BMP_LAYOUT_HEAD03C, BMP_LAYOUT_HEAD03C_H, STR_AUTOLAYOUT_CONTENT_2CONTENT, WritingMode_LR_TB,    AUTOLAYOUT_TEXT2OBJ},
145 	{BMP_LAYOUT_HEAD03A, BMP_LAYOUT_HEAD03A_H, STR_AUTOLAYOUT_2CONTENT_OVER_CONTENT,WritingMode_LR_TB, AUTOLAYOUT_2OBJOVERTEXT},
146 	{BMP_LAYOUT_HEAD02B, BMP_LAYOUT_HEAD02B_H, STR_AUTOLAYOUT_CONTENT_OVER_CONTENT, WritingMode_LR_TB, AUTOLAYOUT_OBJOVERTEXT},
147     {BMP_LAYOUT_HEAD04, BMP_LAYOUT_HEAD04_H, STR_AUTOLAYOUT_4CONTENT, WritingMode_LR_TB,        AUTOLAYOUT_4OBJ},
148 	{BMP_LAYOUT_HEAD06, BMP_LAYOUT_HEAD06_H, STR_AUTOLAYOUT_6CONTENT, WritingMode_LR_TB,    AUTOLAYOUT_6CLIPART},
149 	{0, 0, 0, WritingMode_LR_TB, AUTOLAYOUT_NONE}
150 };
151 
152 static snewfoil_value_info v_standard[] =
153 {
154 	// vertical
155     {BMP_LAYOUT_VERTICAL02, BMP_LAYOUT_VERTICAL02_H, STR_AL_VERT_TITLE_TEXT_CHART, WritingMode_TB_RL,AUTOLAYOUT_VERTICAL_TITLE_TEXT_CHART},
156     {BMP_LAYOUT_VERTICAL01, BMP_LAYOUT_VERTICAL01_H, STR_AL_VERT_TITLE_VERT_OUTLINE, WritingMode_TB_RL, AUTOLAYOUT_VERTICAL_TITLE_VERTICAL_OUTLINE},
157     {BMP_LAYOUT_HEAD02, BMP_LAYOUT_HEAD02_H, STR_AL_TITLE_VERT_OUTLINE, WritingMode_TB_RL, AUTOLAYOUT_TITLE_VERTICAL_OUTLINE},
158     {BMP_LAYOUT_HEAD02A, BMP_LAYOUT_HEAD02A_H, STR_AL_TITLE_VERT_OUTLINE_CLIPART,   WritingMode_TB_RL, AUTOLAYOUT_TITLE_VERTICAL_OUTLINE_CLIPART},
159 	{0, 0, 0, WritingMode_LR_TB, AUTOLAYOUT_NONE}
160 };
161 
162 // -----------------------------------------------------------------------
163 
164 static void fillLayoutValueSet( ValueSet* pValue, snewfoil_value_info* pInfo, const bool bHighContrast )
165 {
166     Size aLayoutItemSize;
167 	for( ; pInfo->mnBmpResId; pInfo++ )
168 	{
169 		String aText( SdResId( pInfo->mnStrResId ) );
170 		BitmapEx aBmp( SdResId( (bHighContrast ? pInfo->mnHCBmpResId : pInfo->mnBmpResId) ) );
171 
172 		pValue->InsertItem( static_cast<sal_uInt16>(pInfo->maAutoLayout)+1, aBmp, aText );
173 
174 		aLayoutItemSize.Width() = std::max( aLayoutItemSize.Width(), aBmp.GetSizePixel().Width() );
175 		aLayoutItemSize.Height() = std::max( aLayoutItemSize.Height(), aBmp.GetSizePixel().Height() );
176 	}
177 
178 	aLayoutItemSize = pValue->CalcItemSizePixel( aLayoutItemSize );
179 	pValue->SetSizePixel( pValue->CalcWindowSizePixel( aLayoutItemSize ) );
180 }
181 
182 // -----------------------------------------------------------------------
183 
184 LayoutToolbarMenu::LayoutToolbarMenu( SlideLayoutController& rController, const Reference< XFrame >& xFrame, ::Window* pParent, const bool bInsertPage )
185 : svtools::ToolbarMenu(xFrame, pParent, WB_CLIPCHILDREN )
186 , mrController( rController )
187 , mxFrame(xFrame)
188 , mbInsertPage( bInsertPage )
189 , mpLayoutSet1( 0 )
190 , mpLayoutSet2( 0 )
191 {
192 	DrawViewMode eMode = DrawViewMode_DRAW;
193 
194 	// find out which view is running
195 	if( xFrame.is() ) try
196 	{
197 		Reference< XPropertySet > xControllerSet( xFrame->getController(), UNO_QUERY_THROW );
198 		xControllerSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "DrawViewMode" ) ) ) >>= eMode;
199 	}
200 	catch( Exception& e )
201 	{
202 		(void)e;
203 		OSL_ASSERT(false);
204 	}
205 
206 	const sal_Int32 LAYOUT_BORDER_PIX = 7;
207 
208 	String aTitle1( SdResId( STR_GLUE_ESCDIR_HORZ ) );
209 	String aTitle2( SdResId( STR_GLUE_ESCDIR_VERT ) );
210 
211 	const bool bHighContrast = GetSettings().GetStyleSettings().GetHighContrastMode();
212 	SvtLanguageOptions aLanguageOptions;
213     const bool bVerticalEnabled = aLanguageOptions.IsVerticalTextEnabled();
214 
215 	SetSelectHdl( LINK( this, LayoutToolbarMenu, SelectHdl ) );
216 
217 	mpLayoutSet1 = createEmptyValueSetControl();
218 	mpLayoutSet1->SetSelectHdl( LINK( this, LayoutToolbarMenu, SelectHdl ) );
219 
220 	snewfoil_value_info* pInfo = 0;
221 	sal_Int16 nColCount = 4;
222 	switch( eMode )
223 	{
224 	case DrawViewMode_DRAW: pInfo = &standard[0]; break;
225 	case DrawViewMode_HANDOUT: pInfo = &handout[0]; nColCount = 2; break;
226 	case DrawViewMode_NOTES: pInfo = &notes[0]; nColCount = 1; break;
227 	default: break;
228 	}
229 
230 	mpLayoutSet1->SetColCount( nColCount );
231 
232 	fillLayoutValueSet( mpLayoutSet1, pInfo, bHighContrast );
233 
234 	Size aSize( mpLayoutSet1->GetOutputSizePixel() );
235 	aSize.Width() += (mpLayoutSet1->GetColCount() + 1) * LAYOUT_BORDER_PIX;
236 	aSize.Height() += (mpLayoutSet1->GetLineCount() +1) * LAYOUT_BORDER_PIX;
237 	mpLayoutSet1->SetOutputSizePixel( aSize );
238 
239 	if( bVerticalEnabled && (eMode == DrawViewMode_DRAW) )
240 		appendEntry( -1, aTitle1 );
241 	appendEntry( 0, mpLayoutSet1 );
242 
243 	if( bVerticalEnabled && (eMode == DrawViewMode_DRAW) )
244 	{
245 		mpLayoutSet2 = new ValueSet( this, WB_TABSTOP | WB_MENUSTYLEVALUESET | WB_FLATVALUESET | WB_NOBORDER | WB_NO_DIRECTSELECT );
246 	//	mpLayoutSet2->SetHelpId( HID_VALUESET_EXTRUSION_LIGHTING );
247 
248 		mpLayoutSet2->SetSelectHdl( LINK( this, LayoutToolbarMenu, SelectHdl ) );
249 		mpLayoutSet2->SetColCount( 4 );
250 		mpLayoutSet2->EnableFullItemMode( sal_False );
251 		mpLayoutSet2->SetColor( GetControlBackground() );
252 
253 		fillLayoutValueSet( mpLayoutSet2, &v_standard[0], bHighContrast );
254 
255 		aSize = mpLayoutSet2->GetOutputSizePixel();
256 		aSize.Width() += (mpLayoutSet2->GetColCount() + 1) * LAYOUT_BORDER_PIX;
257 		aSize.Height() += (mpLayoutSet2->GetLineCount() + 1) * LAYOUT_BORDER_PIX;
258 		mpLayoutSet2->SetOutputSizePixel( aSize );
259 
260 		appendEntry( -1, aTitle2 );
261 		appendEntry( 1, mpLayoutSet2 );
262 	}
263 
264 	if( eMode == DrawViewMode_DRAW )
265 	{
266 		appendSeparator();
267 
268 		OUString sSlotStr;
269 		Image aSlotImage;
270 		if( mxFrame.is() )
271 		{
272 			if( bInsertPage )
273 				sSlotStr = OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:DuplicatePage" ) );
274 			else
275 				sSlotStr = OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:Undo" ) );
276 			aSlotImage = ::GetImage( mxFrame, sSlotStr, sal_False, bHighContrast );
277 
278 			String sSlotTitle;
279 			if( bInsertPage )
280 				sSlotTitle = ImplRetrieveLabelFromCommand( mxFrame, sSlotStr );
281 			else
282 				sSlotTitle = String( SdResId( STR_RESET_LAYOUT ) );
283 			appendEntry( 2, sSlotTitle, aSlotImage);
284 		}
285 	}
286 
287 	SetOutputSizePixel( getMenuSize() );
288 }
289 
290 // -----------------------------------------------------------------------
291 
292 LayoutToolbarMenu::~LayoutToolbarMenu()
293 {
294 }
295 
296 // -----------------------------------------------------------------------
297 
298 IMPL_LINK( LayoutToolbarMenu, SelectHdl, void *, pControl )
299 {
300 	if ( IsInPopupMode() )
301 		EndPopupMode();
302 
303 	Sequence< PropertyValue > aArgs;
304 
305 	AutoLayout eLayout = AUTOLAYOUT__END;
306 
307 	OUString sCommandURL( mrController.getCommandURL() );
308 
309     if( pControl == mpLayoutSet1 )
310     {
311 	    eLayout = static_cast< AutoLayout >(mpLayoutSet1->GetSelectItemId()-1);
312     }
313     else if( pControl == mpLayoutSet2 )
314     {
315 	    eLayout = static_cast< AutoLayout >(mpLayoutSet2->GetSelectItemId()-1);
316     }
317 
318 	if( eLayout != AUTOLAYOUT__END )
319 	{
320 		aArgs = Sequence< PropertyValue >(1);
321 		aArgs[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "WhatLayout" ) );
322 		aArgs[0].Value <<= (sal_Int32)eLayout;
323 	}
324 	else if( mbInsertPage )
325 	{
326 		sCommandURL = OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:DuplicatePage" ) );
327 	}
328 
329 	mrController.dispatchCommand( sCommandURL, aArgs );
330 
331 	return 0;
332 }
333 
334 // ====================================================================
335 
336 OUString SlideLayoutController_getImplementationName()
337 {
338 	return OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.sd.SlideLayoutController" ));
339 }
340 
341 // --------------------------------------------------------------------
342 
343 Sequence< OUString >  SlideLayoutController_getSupportedServiceNames() throw( RuntimeException )
344 {
345     Sequence< OUString > aSNS( 1 );
346     aSNS.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ToolbarController" ));
347     return aSNS;
348 }
349 
350 // --------------------------------------------------------------------
351 
352 Reference< XInterface > SAL_CALL SlideLayoutController_createInstance( const Reference< XMultiServiceFactory >& rSMgr ) throw( RuntimeException )
353 {
354 	return *new SlideLayoutController( rSMgr, OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:AssignLayout" )), false );
355 }
356 
357 // --------------------------------------------------------------------
358 
359 OUString InsertSlideController_getImplementationName()
360 {
361 	return OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.sd.InsertSlideController" ));
362 }
363 
364 // --------------------------------------------------------------------
365 
366 Sequence< OUString >  InsertSlideController_getSupportedServiceNames() throw( RuntimeException )
367 {
368     Sequence< OUString > aSNS( 1 );
369     aSNS.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ToolbarController" ));
370     return aSNS;
371 }
372 
373 // --------------------------------------------------------------------
374 
375 Reference< XInterface > SAL_CALL InsertSlideController_createInstance( const Reference< XMultiServiceFactory >& rSMgr ) throw( RuntimeException )
376 {
377 	return *new SlideLayoutController( rSMgr, OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:InsertPage" )), true );
378 }
379 
380 //========================================================================
381 // class SlideLayoutController
382 //========================================================================
383 
384 SlideLayoutController::SlideLayoutController( const Reference< lang::XMultiServiceFactory >& rServiceManager, const rtl::OUString& sCommandURL, bool bInsertPage )
385 : svt::PopupWindowController( rServiceManager, Reference< frame::XFrame >(), sCommandURL )
386 , mbInsertPage( bInsertPage )
387 {
388 }
389 
390 // --------------------------------------------------------------------
391 
392 ::Window* SlideLayoutController::createPopupWindow( ::Window* pParent )
393 {
394     return new sd::LayoutToolbarMenu( *this, m_xFrame, pParent, mbInsertPage );
395 }
396 
397 // --------------------------------------------------------------------
398 // XServiceInfo
399 // --------------------------------------------------------------------
400 
401 OUString SAL_CALL SlideLayoutController::getImplementationName() throw( RuntimeException )
402 {
403 	if( mbInsertPage )
404 		return InsertSlideController_getImplementationName();
405 	else
406 		return SlideLayoutController_getImplementationName();
407 }
408 
409 // --------------------------------------------------------------------
410 
411 Sequence< OUString > SAL_CALL SlideLayoutController::getSupportedServiceNames(  ) throw( RuntimeException )
412 {
413 	if( mbInsertPage )
414 		return InsertSlideController_getSupportedServiceNames();
415 	else
416 	    return SlideLayoutController_getSupportedServiceNames();
417 }
418 
419 }
420