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 "sddll.hxx"
32 
33 #include <com/sun/star/beans/XMultiPropertyStates.hpp>
34 #include <com/sun/star/frame/XController.hpp>
35 #include <com/sun/star/view/XSelectionSupplier.hpp>
36 #include <com/sun/star/style/XStyle.hpp>
37 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
38 
39 #include <comphelper/processfactory.hxx>
40 #include <sfx2/viewfrm.hxx>
41 #include <vcl/bmpacc.hxx>
42 #include <svl/style.hxx>
43 #include <sfx2/viewfrm.hxx>
44 #include <sfx2/bindings.hxx>
45 #include <sfx2/app.hxx>
46 #include <sfx2/request.hxx>
47 #include <sfx2/dispatch.hxx>
48 #include <svx/svxids.hrc>
49 #include <svx/svdetc.hxx>
50 #include <editeng/boxitem.hxx>
51 #include <editeng/borderline.hxx>
52 #include <editeng/colritem.hxx>
53 #include <editeng/eeitem.hxx>
54 #include <svx/sdr/table/tabledesign.hxx>
55 
56 #include "TableDesignPane.hxx"
57 
58 #include "DrawDocShell.hxx"
59 #include "ViewShellBase.hxx"
60 #include "DrawViewShell.hxx"
61 #include "DrawController.hxx"
62 #include "glob.hrc"
63 #include "sdresid.hxx"
64 #include "EventMultiplexer.hxx"
65 
66 #define C2U(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
67 using ::rtl::OUString;
68 using namespace ::com::sun::star;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::drawing;
71 using namespace ::com::sun::star::container;
72 using namespace ::com::sun::star::beans;
73 using namespace ::com::sun::star::view;
74 using namespace ::com::sun::star::style;
75 using namespace ::com::sun::star::frame;
76 using namespace ::com::sun::star::lang;
77 using namespace ::com::sun::star::ui;
78 
79 namespace sd {
80 
81 static const sal_Int32 nPreviewColumns = 5;
82 static const sal_Int32 nPreviewRows = 5;
83 
84 // --------------------------------------------------------------------
85 
86 static const OUString* getPropertyNames()
87 {
88 	static const OUString gPropNames[ CB_BANDED_COLUMNS-CB_HEADER_ROW+1 ] =
89 	{
90 
91 		C2U( "UseFirstRowStyle" ),
92 		C2U( "UseLastRowStyle" ),
93 		C2U( "UseBandingRowStyle" ),
94 		C2U( "UseFirstColumnStyle" ),
95 		C2U( "UseLastColumnStyle" ),
96 		C2U( "UseBandingColumnStyle" )
97 	};
98 	return &gPropNames[0];
99 }
100 // --------------------------------------------------------------------
101 
102 TableDesignPane::TableDesignPane( ::Window* pParent, ViewShellBase& rBase, bool bModal )
103 : Control( pParent, SdResId(DLG_TABLEDESIGNPANE) )
104 , mrBase( rBase )
105 , msTableTemplate( RTL_CONSTASCII_USTRINGPARAM( "TableTemplate" ) )
106 , mbModal( bModal )
107 , mbStyleSelected( false )
108 , mbOptionsChanged( false )
109 {
110 	Window* pControlParent = mbModal ? pParent : this;
111 
112 	mxControls[FL_TABLE_STYLES].reset( new FixedLine( pControlParent, SdResId( FL_TABLE_STYLES + 1 ) ) );
113 
114 	ValueSet* pValueSet = new ValueSet( pControlParent, SdResId( CT_TABLE_STYLES+1 ) );
115 	mxControls[CT_TABLE_STYLES].reset( pValueSet );
116 	if( !mbModal )
117 	{
118 		pValueSet->SetStyle( (pValueSet->GetStyle() & ~(WB_ITEMBORDER|WB_BORDER)) | WB_NO_DIRECTSELECT | WB_FLATVALUESET | WB_NOBORDER );
119 		pValueSet->SetColor();
120 		pValueSet->SetExtraSpacing(8);
121 	}
122 	else
123 	{
124 		pValueSet->SetColor( Color( COL_WHITE ) );
125 		pValueSet->SetBackground( Color( COL_WHITE ) );
126 	}
127 	pValueSet->SetSelectHdl (LINK(this, TableDesignPane, implValueSetHdl));
128 
129 	mxControls[FL_STYLE_OPTIONS].reset( new FixedLine( pControlParent, SdResId( FL_STYLE_OPTIONS + 1 ) ) );
130 	sal_uInt16 i;
131 	for( i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i )
132 	{
133 		CheckBox *pCheckBox = new CheckBox( pControlParent, SdResId( i+1 ) );
134 		mxControls[i].reset( pCheckBox );
135 		pCheckBox->SetClickHdl( LINK( this, TableDesignPane, implCheckBoxHdl ) );
136 	}
137 
138 	for( i = 0; i < DESIGNPANE_CONTROL_COUNT; i++ )
139 		mnOrgOffsetY[i] = mxControls[i]->GetPosPixel().Y();
140 
141 	// get current controller and initialize listeners
142 	try
143 	{
144 		mxView = Reference< XDrawView >::query(mrBase.GetController());
145 		addListener();
146 
147 		Reference< XController > xController( mrBase.GetController(), UNO_QUERY_THROW );
148 		Reference< XStyleFamiliesSupplier > xFamiliesSupp( xController->getModel(), UNO_QUERY_THROW );
149 		Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
150 		const OUString sFamilyName( RTL_CONSTASCII_USTRINGPARAM("table" ) );
151 		mxTableFamily = Reference< XIndexAccess >( xFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
152 
153 	}
154 	catch( Exception& e )
155 	{
156 		(void)e;
157 		DBG_ERROR( "sd::CustomAnimationPane::CustomAnimationPane(), Exception caught!" );
158 	}
159 
160 	onSelectionChanged();
161 	updateControls();
162 
163 	FreeResource();
164 }
165 
166 // --------------------------------------------------------------------
167 
168 TableDesignPane::~TableDesignPane()
169 {
170 	removeListener();
171 }
172 
173 // --------------------------------------------------------------------
174 
175 void TableDesignPane::DataChanged( const DataChangedEvent& /*rDCEvt*/ )
176 {
177 	updateLayout();
178 }
179 
180 // --------------------------------------------------------------------
181 
182 void TableDesignPane::Resize()
183 {
184 	updateLayout();
185 }
186 
187 // --------------------------------------------------------------------
188 
189 static SfxBindings* getBindings( ViewShellBase& rBase )
190 {
191 	if( rBase.GetMainViewShell().get() && rBase.GetMainViewShell()->GetViewFrame() )
192 		return &rBase.GetMainViewShell()->GetViewFrame()->GetBindings();
193 	else
194 		return 0;
195 }
196 
197 // --------------------------------------------------------------------
198 
199 static SfxDispatcher* getDispatcher( ViewShellBase& rBase )
200 {
201 	if( rBase.GetMainViewShell().get() && rBase.GetMainViewShell()->GetViewFrame() )
202 		return rBase.GetMainViewShell()->GetViewFrame()->GetDispatcher();
203 	else
204 		return 0;
205 }
206 
207 // --------------------------------------------------------------------
208 
209 IMPL_LINK( TableDesignPane, implValueSetHdl, Control*, EMPTYARG )
210 {
211 	mbStyleSelected = true;
212 	if( !mbModal )
213 		ApplyStyle();
214 	return 0;
215 }
216 
217 // --------------------------------------------------------------------
218 
219 void TableDesignPane::ApplyStyle()
220 {
221 	try
222 	{
223 		OUString sStyleName;
224 		ValueSet* pValueSet = static_cast< ValueSet* >( mxControls[CT_TABLE_STYLES].get() );
225 		sal_Int32 nIndex = static_cast< sal_Int32 >( pValueSet->GetSelectItemId() ) - 1;
226 
227 		if( (nIndex >= 0) && (nIndex < mxTableFamily->getCount()) )
228 		{
229 			Reference< XNameAccess > xNames( mxTableFamily, UNO_QUERY_THROW );
230 			sStyleName = xNames->getElementNames()[nIndex];
231 		}
232 
233 		if( sStyleName.getLength() == 0 )
234 			return;
235 
236 		SdrView* pView = mrBase.GetDrawView();
237 		if( mxSelectedTable.is() )
238 		{
239 			if( pView )
240 			{
241 				SfxRequest aReq( SID_TABLE_STYLE, SFX_CALLMODE_SYNCHRON, SFX_APP()->GetPool() );
242 				aReq.AppendItem( SfxStringItem( SID_TABLE_STYLE, sStyleName ) );
243 
244 				rtl::Reference< sdr::SelectionController > xController( pView->getSelectionController() );
245 				if( xController.is() )
246 					xController->Execute( aReq );
247 
248 				SfxBindings* pBindings = getBindings( mrBase );
249 				if( pBindings )
250 				{
251 					pBindings->Invalidate( SID_UNDO );
252 					pBindings->Invalidate( SID_REDO );
253 				}
254 			}
255 		}
256 		else
257 		{
258 			SfxDispatcher* pDispatcher = getDispatcher( mrBase );
259 			SfxStringItem aArg( SID_TABLE_STYLE, sStyleName );
260 			pDispatcher->Execute(SID_INSERT_TABLE, SFX_CALLMODE_ASYNCHRON, &aArg, 0 );
261 		}
262 	}
263 	catch( Exception& )
264 	{
265 		DBG_ERROR("TableDesignPane::implValueSetHdl(), exception caught!");
266 	}
267 }
268 
269 // --------------------------------------------------------------------
270 
271 IMPL_LINK( TableDesignPane, implCheckBoxHdl, Control*, EMPTYARG )
272 {
273 	mbOptionsChanged = true;
274 
275 	if( !mbModal )
276 		ApplyOptions();
277 
278 	FillDesignPreviewControl();
279 	return 0;
280 }
281 
282 // --------------------------------------------------------------------
283 
284 void TableDesignPane::ApplyOptions()
285 {
286 	static sal_uInt16 gParamIds[CB_BANDED_COLUMNS-CB_HEADER_ROW+1] =
287 	{
288 		ID_VAL_USEFIRSTROWSTYLE, ID_VAL_USELASTROWSTYLE, ID_VAL_USEBANDINGROWSTYLE,
289 		ID_VAL_USEFIRSTCOLUMNSTYLE, ID_VAL_USELASTCOLUMNSTYLE, ID_VAL_USEBANDINGCOLUMNSTYLE
290 	};
291 
292 	if( mxSelectedTable.is() )
293 	{
294 		SfxRequest aReq( SID_TABLE_STYLE_SETTINGS, SFX_CALLMODE_SYNCHRON, SFX_APP()->GetPool() );
295 
296 		for( sal_uInt16 i = 0; i < (CB_BANDED_COLUMNS-CB_HEADER_ROW+1); ++i )
297 		{
298 			aReq.AppendItem( SfxBoolItem( gParamIds[i], static_cast< CheckBox* >( mxControls[CB_HEADER_ROW+i].get() )->IsChecked() ) );
299 		}
300 
301 		SdrView* pView = mrBase.GetDrawView();
302 		if( pView )
303 		{
304 			rtl::Reference< sdr::SelectionController > xController( pView->getSelectionController() );
305 			if( xController.is() )
306 			{
307 				xController->Execute( aReq );
308 
309 				SfxBindings* pBindings = getBindings( mrBase );
310 				if( pBindings )
311 				{
312 					pBindings->Invalidate( SID_UNDO );
313 					pBindings->Invalidate( SID_REDO );
314 				}
315 			}
316 		}
317 	}
318 }
319 
320 // --------------------------------------------------------------------
321 
322 void TableDesignPane::onSelectionChanged()
323 {
324 	Reference< XPropertySet > xNewSelection;
325 
326 	if( mxView.is() ) try
327 	{
328 		Reference< XSelectionSupplier >  xSel( mxView, UNO_QUERY_THROW );
329 		if (xSel.is())
330 		{
331 			Any aSel( xSel->getSelection() );
332 			Sequence< XShape > xShapeSeq;
333 			if( aSel >>= xShapeSeq )
334 			{
335 				if( xShapeSeq.getLength() == 1 )
336 					aSel <<= xShapeSeq[0];
337 			}
338 			else
339 			{
340 				Reference< XShapes > xShapes( aSel, UNO_QUERY );
341 				if( xShapes.is() && (xShapes->getCount() == 1) )
342 					aSel <<= xShapes->getByIndex(0);
343 			}
344 
345 			Reference< XShapeDescriptor > xDesc( aSel, UNO_QUERY );
346 			if( xDesc.is() &&
347 				( xDesc->getShapeType().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "com.sun.star.drawing.TableShape" ) ) ||
348 				  xDesc->getShapeType().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "com.sun.star.presentation.TableShape" ) ) ) )
349 			{
350 				xNewSelection = Reference< XPropertySet >::query( xDesc );
351 			}
352 		}
353 	}
354 	catch( Exception& )
355 	{
356 		DBG_ERROR( "sd::TableDesignPane::onSelectionChanged(), Exception caught!" );
357 	}
358 
359 	if( mxSelectedTable != xNewSelection )
360 	{
361 		mxSelectedTable = xNewSelection;
362 		updateControls();
363 	}
364 }
365 
366 // --------------------------------------------------------------------
367 
368 void TableDesignPane::updateLayout()
369 {
370 	::Size aPaneSize( GetSizePixel() );
371     if(IsVisible() && aPaneSize.Width() > 0)
372     {
373 		Point aOffset( LogicToPixel( Point(3,3), MAP_APPFONT ) );
374 
375 		ValueSet* pValueSet = static_cast< ValueSet* >( mxControls[CT_TABLE_STYLES].get() );
376 
377 		Size aValueSetSize;
378 
379 		if( !mbModal )
380 		{
381 			const long nOptionsHeight = mnOrgOffsetY[CB_BANDED_COLUMNS] + mxControls[CB_BANDED_COLUMNS]->GetSizePixel().Height() + aOffset.Y();
382 
383 			const long nStylesHeight = aPaneSize.Height() - nOptionsHeight;
384 
385 			// set with of controls to size of pane
386 			for( sal_Int32 nId = 0; nId < DESIGNPANE_CONTROL_COUNT; ++nId )
387 			{
388 				Size aSize( mxControls[nId]->GetSizePixel() );
389 				aSize.Width() = aPaneSize.Width() - aOffset.X() - mxControls[nId]->GetPosPixel().X();
390 				mxControls[nId]->SetSizePixel( aSize );
391 				mxControls[nId]->SetPaintTransparent(sal_True);
392 				mxControls[nId]->SetBackground();
393 			}
394 			aValueSetSize = Size( pValueSet->GetSizePixel().Width(), nStylesHeight - mxControls[FL_TABLE_STYLES]->GetSizePixel().Height() - mnOrgOffsetY[FL_TABLE_STYLES]  );
395 		}
396 		else
397 		{
398 			aValueSetSize = pValueSet->GetSizePixel();
399 		}
400 
401 
402 		// Calculate the number of rows and columns.
403         if( pValueSet->GetItemCount() > 0 )
404         {
405             Image aImage = pValueSet->GetItemImage(pValueSet->GetItemId(0));
406             Size aItemSize = pValueSet->CalcItemSizePixel(aImage.GetSizePixel());
407 			pValueSet->SetItemWidth( aItemSize.Width() );
408 			pValueSet->SetItemHeight( aItemSize.Height() );
409 
410             aItemSize.Width() += 10;
411             aItemSize.Height() += 10;
412             int nColumnCount = (aValueSetSize.Width() - pValueSet->GetScrollWidth()) / aItemSize.Width();
413             if (nColumnCount < 1)
414                 nColumnCount = 1;
415 
416 			int nRowCount = (pValueSet->GetItemCount() + nColumnCount - 1) / nColumnCount;
417 	        if (nRowCount < 1)
418 		        nRowCount = 1;
419 
420 			int nVisibleRowCount = (aValueSetSize.Height()+2) / aItemSize.Height();
421 
422 			pValueSet->SetLineCount ( (nRowCount < nVisibleRowCount) ? (sal_uInt16)nRowCount : 0 );
423 
424 			pValueSet->SetColCount ((sal_uInt16)nColumnCount);
425             pValueSet->SetLineCount ((sal_uInt16)nRowCount);
426 
427 			if( !mbModal )
428 			{
429 				WinBits nStyle = pValueSet->GetStyle() & ~(WB_VSCROLL);
430 				if( nRowCount < nVisibleRowCount )
431 				{
432 					aValueSetSize.Height() = nRowCount * aItemSize.Height();
433 				}
434 				else if( nRowCount > nVisibleRowCount )
435 				{
436 					nStyle |= WB_VSCROLL;
437 				}
438 				pValueSet->SetStyle( nStyle );
439 			}
440        }
441 
442 		if( !mbModal )
443 		{
444 			pValueSet->SetSizePixel( aValueSetSize );
445 			pValueSet->SetBackground( GetSettings().GetStyleSettings().GetWindowColor() );
446 			pValueSet->SetColor( GetSettings().GetStyleSettings().GetWindowColor() );
447 
448 			Point aPos( pValueSet->GetPosPixel() );
449 
450             // The following line may look like a no-op but without it the
451             // control is placed off-screen when RTL is active.
452             pValueSet->SetPosPixel(pValueSet->GetPosPixel());
453 
454 			// shift show options section down
455 			const long nOptionsPos = aPos.Y() + aValueSetSize.Height();
456 			for( sal_Int32 nId = FL_STYLE_OPTIONS; nId <= CB_BANDED_COLUMNS; ++nId )
457 			{
458 				Point aCPos( mxControls[nId]->GetPosPixel() );
459 				aCPos.X() = ( nId == FL_STYLE_OPTIONS ?  1 : 2 ) * aOffset.X();
460 				aCPos.Y() = mnOrgOffsetY[nId] + nOptionsPos;
461 				mxControls[nId]->SetPosPixel( aCPos );
462 			}
463 		}
464     }
465 
466 	if( !mbModal )
467 		SetBackground( GetSettings().GetStyleSettings().GetWindowColor() );
468 }
469 
470 // --------------------------------------------------------------------
471 
472 void TableDesignPane::updateControls()
473 {
474 	static sal_Bool gDefaults[CB_BANDED_COLUMNS-CB_HEADER_ROW+1] = { sal_True, sal_False, sal_True, sal_False, sal_False, sal_False };
475 
476 	const bool bHasTable = mxSelectedTable.is();
477 	const OUString* pPropNames = getPropertyNames();
478 
479 	for( sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i )
480 	{
481 		sal_Bool bUse = gDefaults[i-CB_HEADER_ROW];
482 		if( bHasTable ) try
483 		{
484 			mxSelectedTable->getPropertyValue( *pPropNames++ ) >>= bUse;
485 		}
486 		catch( Exception& )
487 		{
488 			DBG_ERROR("sd::TableDesignPane::updateControls(), exception caught!");
489 		}
490 		static_cast< CheckBox* >( mxControls[i].get() )->Check( bUse ? sal_True : sal_False );
491 		mxControls[i]->Enable(bHasTable ? sal_True : sal_False );
492 	}
493 
494 	FillDesignPreviewControl();
495 	updateLayout();
496 
497 
498 	sal_uInt16 nSelection = 0;
499 	if( mxSelectedTable.is() )
500 	{
501 		Reference< XNamed > xNamed( mxSelectedTable->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "TableTemplate" ) ) ), UNO_QUERY );
502 		if( xNamed.is() )
503 		{
504 			const OUString sStyleName( xNamed->getName() );
505 
506 			Reference< XNameAccess > xNames( mxTableFamily, UNO_QUERY );
507 			if( xNames.is() )
508 			{
509 				Sequence< OUString > aNames( xNames->getElementNames() );
510 				for( sal_Int32 nIndex = 0; nIndex < aNames.getLength(); nIndex++ )
511 				{
512 					if( aNames[nIndex] == sStyleName )
513 					{
514 						nSelection = (sal_uInt16)nIndex+1;
515 						break;
516 					}
517 				}
518 			}
519 		}
520 	}
521 	ValueSet* pValueSet = static_cast< ValueSet* >( mxControls[CT_TABLE_STYLES].get() );
522 	pValueSet->SelectItem( nSelection );
523 }
524 
525 // --------------------------------------------------------------------
526 
527 void TableDesignPane::addListener()
528 {
529 	Link aLink( LINK(this,TableDesignPane,EventMultiplexerListener) );
530     mrBase.GetEventMultiplexer()->AddEventListener (
531         aLink,
532         tools::EventMultiplexerEvent::EID_EDIT_VIEW_SELECTION
533         | tools::EventMultiplexerEvent::EID_CURRENT_PAGE
534         | tools::EventMultiplexerEvent::EID_MAIN_VIEW_REMOVED
535         | tools::EventMultiplexerEvent::EID_MAIN_VIEW_ADDED
536         | tools::EventMultiplexerEvent::EID_DISPOSING);
537 }
538 
539 // --------------------------------------------------------------------
540 
541 void TableDesignPane::removeListener()
542 {
543 	Link aLink( LINK(this,TableDesignPane,EventMultiplexerListener) );
544     mrBase.GetEventMultiplexer()->RemoveEventListener( aLink );
545 }
546 
547 // --------------------------------------------------------------------
548 
549 IMPL_LINK(TableDesignPane,EventMultiplexerListener,
550     tools::EventMultiplexerEvent*,pEvent)
551 {
552     switch (pEvent->meEventId)
553     {
554         case tools::EventMultiplexerEvent::EID_CURRENT_PAGE:
555         case tools::EventMultiplexerEvent::EID_EDIT_VIEW_SELECTION:
556             onSelectionChanged();
557             break;
558 
559         case tools::EventMultiplexerEvent::EID_MAIN_VIEW_REMOVED:
560             mxView = Reference<XDrawView>();
561             onSelectionChanged();
562             break;
563 
564         case tools::EventMultiplexerEvent::EID_MAIN_VIEW_ADDED:
565             mxView = Reference<XDrawView>::query( mrBase.GetController() );
566             onSelectionChanged();
567             break;
568     }
569     return 0;
570 }
571 
572 // --------------------------------------------------------------------
573 
574 struct CellInfo
575 {
576 	Color maCellColor;
577 	Color maTextColor;
578 	SvxBoxItem maBorder;
579 
580 	explicit CellInfo( const Reference< XStyle >& xStyle );
581 };
582 
583 CellInfo::CellInfo( const Reference< XStyle >& xStyle )
584 : maBorder(SDRATTR_TABLE_BORDER)
585 {
586 	SfxStyleSheet* pStyleSheet = SfxUnoStyleSheet::getUnoStyleSheet( xStyle );
587 	if( pStyleSheet )
588 	{
589 		SfxItemSet& rSet = pStyleSheet->GetItemSet();
590 
591 		// get style fill color
592 		if( !GetDraftFillColor(rSet, maCellColor) )
593 			maCellColor.SetColor( COL_TRANSPARENT );
594 
595 		// get style text color
596 		const SvxColorItem* pTextColor = dynamic_cast<const SvxColorItem*>( rSet.GetItem(EE_CHAR_COLOR) );
597 		if( pTextColor )
598 			maTextColor = pTextColor->GetValue();
599 		else
600 			maTextColor.SetColor( COL_TRANSPARENT );
601 
602 		// get border
603 		const SvxBoxItem* pBoxItem = dynamic_cast<const SvxBoxItem*>(rSet.GetItem( SDRATTR_TABLE_BORDER ) );
604 		if( pBoxItem )
605 			maBorder = *pBoxItem;
606 	}
607 }
608 
609 // --------------------------------------------------------------------
610 
611 typedef std::vector< boost::shared_ptr< CellInfo > > CellInfoVector;
612 typedef boost::shared_ptr< CellInfo > CellInfoMatrix[nPreviewColumns][nPreviewRows];
613 
614 struct TableStyleSettings
615 {
616 	bool mbUseFirstRow;
617 	bool mbUseLastRow;
618 	bool mbUseFirstColumn;
619 	bool mbUseLastColumn;
620 	bool mbUseRowBanding;
621 	bool mbUseColumnBanding;
622 
623 	TableStyleSettings()
624 		: mbUseFirstRow(true)
625 		, mbUseLastRow(false)
626 		, mbUseFirstColumn(false)
627 		, mbUseLastColumn(false)
628 		, mbUseRowBanding(true)
629 		, mbUseColumnBanding(false) {}
630 };
631 
632 // --------------------------------------------------------------------
633 
634 static void FillCellInfoVector( const Reference< XIndexAccess >& xTableStyle, CellInfoVector& rVector )
635 {
636 	DBG_ASSERT( xTableStyle.is() && (xTableStyle->getCount() == sdr::table::style_count ), "sd::FillCellInfoVector(), inavlid table style!" );
637 	if( xTableStyle.is() ) try
638 	{
639 		rVector.resize( sdr::table::style_count );
640 
641 		for( sal_Int32 nStyle = 0; nStyle < sdr::table::style_count; ++nStyle )
642 		{
643 			Reference< XStyle > xStyle( xTableStyle->getByIndex( nStyle ), UNO_QUERY );
644 			if( xStyle.is() )
645 				rVector[nStyle].reset( new CellInfo( xStyle ) );
646 		}
647 	}
648 	catch(Exception&)
649 	{
650 		DBG_ERROR("sd::FillCellInfoVector(), exception caught!");
651 	}
652 }
653 
654 static void FillCellInfoMatrix( const CellInfoVector& rStyle, const TableStyleSettings& rSettings, CellInfoMatrix& rMatrix )
655 {
656 	for( sal_Int32 nRow = 0; nRow < nPreviewColumns; ++nRow )
657 	{
658 		const bool bFirstRow = rSettings.mbUseFirstRow && (nRow == 0);
659 		const bool bLastRow = rSettings.mbUseLastRow && (nRow == nPreviewColumns - 1);
660 
661 		for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol )
662 		{
663 			boost::shared_ptr< CellInfo > xCellInfo;
664 
665 			// first and last row win first, if used and available
666 			if( bFirstRow )
667 			{
668 				xCellInfo = rStyle[sdr::table::first_row_style];
669 			}
670 			else if( bLastRow )
671 			{
672 				xCellInfo = rStyle[sdr::table::last_row_style];
673 			}
674 
675 			if( !xCellInfo.get() )
676 			{
677 				// next come first and last column, if used and available
678 				if( rSettings.mbUseFirstColumn && (nCol == 0)  )
679 				{
680 					xCellInfo = rStyle[sdr::table::first_column_style];
681 				}
682 				else if( rSettings.mbUseLastColumn && (nCol == nPreviewColumns-1) )
683 				{
684 					xCellInfo = rStyle[sdr::table::last_column_style];
685 				}
686 			}
687 
688 			if( !xCellInfo.get() )
689 			{
690 				if( rSettings.mbUseRowBanding )
691 				{
692 					if( (nRow & 1) == 0 )
693 					{
694 						xCellInfo = rStyle[sdr::table::even_rows_style];
695 					}
696 					else
697 					{
698 						xCellInfo = rStyle[sdr::table::odd_rows_style];
699 					}
700 				}
701 			}
702 
703 			if( !xCellInfo.get() )
704 			{
705 				if( rSettings.mbUseColumnBanding )
706 				{
707 					if( (nCol & 1) == 0 )
708 					{
709 						xCellInfo = rStyle[sdr::table::even_columns_style];
710 					}
711 					else
712 					{
713 						xCellInfo = rStyle[sdr::table::odd_columns_style];
714 					}
715 				}
716 			}
717 
718 			if( !xCellInfo.get() )
719 			{
720 				// use default cell style if non found yet
721 				xCellInfo = rStyle[sdr::table::body_style];
722 			}
723 
724 			rMatrix[nCol][nRow] = xCellInfo;
725 		}
726 	}
727 }
728 
729 // --------------------------------------------------------------------
730 
731 const Bitmap CreateDesignPreview( const Reference< XIndexAccess >& xTableStyle, const TableStyleSettings& rSettings, bool bIsPageDark )
732 {
733 	CellInfoVector aCellInfoVector(sdr::table::style_count);
734 	FillCellInfoVector( xTableStyle, aCellInfoVector );
735 
736 	CellInfoMatrix aMatrix;
737 	FillCellInfoMatrix( aCellInfoVector, rSettings, aMatrix );
738 
739 // bbbbbbbbbbbb w = 12 pixel
740 // bccccccccccb h = 7 pixel
741 // bccccccccccb b = border color
742 // bcttttttttcb c = cell color
743 // bccccccccccb t = text color
744 // bccccccccccb
745 // bbbbbbbbbbbb
746 
747 
748 	const sal_Int32 nCellWidth = 12; // one pixel is shared with the next cell!
749 	const sal_Int32 nCellHeight = 7; // one pixel is shared with the next cell!
750 
751 	Bitmap aPreviewBmp( Size( (nCellWidth * nPreviewColumns) - (nPreviewColumns - 1), (nCellHeight * nPreviewRows) - (nPreviewRows - 1)), 24, NULL );
752     BitmapWriteAccess* pAccess = aPreviewBmp.AcquireWriteAccess();
753 	if( pAccess )
754 	{
755 		pAccess->Erase( Color( bIsPageDark ? COL_BLACK : COL_WHITE ) );
756 
757 		// first draw cell background and text line previews
758 		sal_Int32 nY = 0;
759 		sal_Int32 nRow;
760 		for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
761 		{
762 			sal_Int32 nX = 0;
763 			for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol, nX += nCellWidth-1 )
764 			{
765 				boost::shared_ptr< CellInfo > xCellInfo( aMatrix[nCol][nRow] );
766 
767 				Color aTextColor( COL_AUTO );
768 				if( xCellInfo.get() )
769 				{
770 					// fill cell background
771 					const Rectangle aRect( nX, nY, nX + nCellWidth - 1, nY + nCellHeight - 1 );
772 
773 					if( xCellInfo->maCellColor.GetColor() != COL_TRANSPARENT )
774 					{
775 						pAccess->SetFillColor( xCellInfo->maCellColor );
776 						pAccess->FillRect( aRect );
777 					}
778 
779 					aTextColor = xCellInfo->maTextColor;
780 				}
781 
782 				// draw text preview line
783 				if( aTextColor.GetColor() == COL_AUTO )
784 					aTextColor.SetColor( bIsPageDark ? COL_WHITE : COL_BLACK );
785 				pAccess->SetLineColor( aTextColor );
786 				const Point aPnt1( nX + 2, nY + ((nCellHeight - 1 ) >> 1) );
787 				const Point aPnt2( nX + nCellWidth - 3, aPnt1.Y() );
788 				pAccess->DrawLine( aPnt1, aPnt2 );
789 			}
790 		}
791 
792 		// second draw border lines
793 		nY = 0;
794 		for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
795 		{
796 			sal_Int32 nX = 0;
797 			for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol, nX += nCellWidth-1 )
798 			{
799 				boost::shared_ptr< CellInfo > xCellInfo( aMatrix[nCol][nRow] );
800 
801 				if( xCellInfo.get() )
802 				{
803 					const Point aPntTL( nX, nY );
804 					const Point aPntTR( nX + nCellWidth - 1, nY );
805 					const Point aPntBL( nX, nY + nCellHeight - 1 );
806 					const Point aPntBR( nX + nCellWidth - 1, nY + nCellHeight - 1 );
807 
808 					sal_Int32 border_diffs[8] = { 0,-1, 0,1, -1,0, 1,0 };
809 					sal_Int32* pDiff = &border_diffs[0];
810 
811 					// draw top border
812 					for( sal_uInt16 nLine = 0; nLine < 4; ++nLine )
813 					{
814 						const SvxBorderLine* pBorderLine = xCellInfo->maBorder.GetLine(nLine);
815 						if( !pBorderLine || ((pBorderLine->GetOutWidth() == 0) && (pBorderLine->GetInWidth()==0)) )
816 							continue;
817 
818 						sal_Int32 nBorderCol = nCol + *pDiff++;
819 						sal_Int32 nBorderRow = nRow + *pDiff++;
820 						if( (nBorderCol >= 0) && (nBorderCol < nPreviewColumns) && (nBorderRow >= 0) && (nBorderRow < nPreviewRows) )
821 						{
822 							// check border
823 							boost::shared_ptr< CellInfo > xBorderInfo( aMatrix[nBorderCol][nBorderRow] );
824 							if( xBorderInfo.get() )
825 							{
826 								const sal_uInt16 nOtherLine = nLine ^ 1;
827 								const SvxBorderLine* pBorderLine2 = xBorderInfo->maBorder.GetLine(nOtherLine^1);
828 								if( pBorderLine2 && pBorderLine2->HasPriority(*pBorderLine) )
829 									continue; // other border line wins
830 							}
831 						}
832 
833 						pAccess->SetLineColor( pBorderLine->GetColor() );
834 						switch( nLine )
835 						{
836 						case 0: pAccess->DrawLine( aPntTL, aPntTR ); break;
837 						case 1: pAccess->DrawLine( aPntBL, aPntBR ); break;
838 						case 2: pAccess->DrawLine( aPntTL, aPntBL ); break;
839 						case 3: pAccess->DrawLine( aPntTR, aPntBR ); break;
840 						}
841 					}
842 				}
843 			}
844 		}
845 
846 		aPreviewBmp.ReleaseAccess( pAccess );
847 	}
848 
849 	return aPreviewBmp;
850 }
851 
852 void TableDesignPane::FillDesignPreviewControl()
853 {
854 	ValueSet* pValueSet = static_cast< ValueSet* >( mxControls[CT_TABLE_STYLES].get() );
855 
856 	sal_uInt16 nSelectedItem = pValueSet->GetSelectItemId();
857 	pValueSet->Clear();
858 	try
859 	{
860 		TableStyleSettings aSettings;
861 		if( mxSelectedTable.is() )
862 		{
863 			aSettings.mbUseFirstRow = static_cast< CheckBox* >(mxControls[CB_HEADER_ROW].get())->IsChecked();
864 			aSettings.mbUseLastRow = static_cast< CheckBox* >(mxControls[CB_TOTAL_ROW].get())->IsChecked();
865 			aSettings.mbUseRowBanding = static_cast< CheckBox* >(mxControls[CB_BANDED_ROWS].get())->IsChecked();
866 			aSettings.mbUseFirstColumn = static_cast< CheckBox* >(mxControls[CB_FIRST_COLUMN].get())->IsChecked();
867 			aSettings.mbUseLastColumn = static_cast< CheckBox* >(mxControls[CB_LAST_COLUMN].get())->IsChecked();
868 			aSettings.mbUseColumnBanding = static_cast< CheckBox* >(mxControls[CB_BANDED_COLUMNS].get())->IsChecked();
869 		}
870 
871 		sal_Bool bIsPageDark = sal_False;
872 		if( mxView.is() )
873 		{
874 			Reference< XPropertySet > xPageSet( mxView->getCurrentPage(), UNO_QUERY );
875 			if( xPageSet.is() )
876 			{
877 				const OUString sIsBackgroundDark( RTL_CONSTASCII_USTRINGPARAM( "IsBackgroundDark" ) );
878 				xPageSet->getPropertyValue(sIsBackgroundDark) >>= bIsPageDark;
879 			}
880 		}
881 
882 		for( sal_Int32 nIndex = 0; nIndex < mxTableFamily->getCount(); nIndex++ ) try
883 		{
884 			Reference< XIndexAccess > xTableStyle( mxTableFamily->getByIndex( nIndex ), UNO_QUERY );
885 			if( xTableStyle.is() )
886 				pValueSet->InsertItem( sal::static_int_cast<sal_uInt16>( nIndex + 1 ), Image( CreateDesignPreview( xTableStyle, aSettings, bIsPageDark ) ) );
887 		}
888 		catch( Exception& )
889 		{
890 			DBG_ERROR("sd::TableDesignPane::FillDesignPreviewControl(), exception caught!");
891 		}
892 	}
893 	catch( Exception& )
894 	{
895 		DBG_ERROR("sd::TableDesignPane::FillDesignPreviewControl(), exception caught!");
896 	}
897 	pValueSet->SelectItem(nSelectedItem);
898 }
899 
900 // ====================================================================
901 
902 TableDesignDialog::TableDesignDialog(::Window* pParent, ViewShellBase& rBase )
903 : ModalDialog( pParent, SdResId( DLG_TABLEDESIGNPANE ))
904 , mrBase( rBase )
905 {
906 	mxFlSep1.reset( new FixedLine( this, SdResId( FL_SEP1 ) ) );
907 	mxFlSep2.reset( new FixedLine( this, SdResId( FL_SEP2 ) ) );
908 	mxHelpButton.reset( new HelpButton( this, SdResId( BTN_HELP ) ) );
909 	mxOkButton.reset( new OKButton( this, SdResId( BTN_OK ) ) );
910 	mxCancelButton.reset( new CancelButton( this, SdResId( BTN_CANCEL ) ) );
911 	FreeResource();
912 
913 	mpDesignPane.reset( new TableDesignPane( this, rBase, true ) );
914 	mpDesignPane->Hide();
915 }
916 
917 // --------------------------------------------------------------------
918 
919 short TableDesignDialog::Execute()
920 {
921 	if( ModalDialog::Execute() )
922 	{
923 		if( mpDesignPane->isStyleChanged() )
924 			mpDesignPane->ApplyStyle();
925 
926 		if( mpDesignPane->isOptionsChanged() )
927 			mpDesignPane->ApplyOptions();
928 		return sal_True;
929 	}
930 	return sal_False;
931 }
932 
933 // ====================================================================
934 
935 ::Window * createTableDesignPanel( ::Window* pParent, ViewShellBase& rBase )
936 {
937 	return new TableDesignPane( pParent, rBase, false );
938 }
939 
940 // ====================================================================
941 
942 void showTableDesignDialog( ::Window* pParent, ViewShellBase& rBase )
943 {
944 	boost::scoped_ptr< TableDesignDialog > xDialog( new TableDesignDialog( pParent, rBase ) );
945 	xDialog->Execute();
946 }
947 
948 
949 }
950 
951 
952