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