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