summaryrefslogtreecommitdiff
path: root/sd/source/ui/table/TableDesignPane.cxx
blob: c29e590c2ca4bb776d55f9607d4e3bf7aea8315f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "sddll.hxx"

#include <com/sun/star/beans/XMultiPropertyStates.hpp>
#include <com/sun/star/frame/XController.hpp>
#include <com/sun/star/view/XSelectionSupplier.hpp>
#include <com/sun/star/style/XStyle.hpp>
#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>

#include <comphelper/processfactory.hxx>
#include <sfx2/viewfrm.hxx>
#include <vcl/bmpacc.hxx>
#include <vcl/layout.hxx>
#include <vcl/settings.hxx>
#include <vcl/builderfactory.hxx>

#include <svl/style.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/app.hxx>
#include <sfx2/request.hxx>
#include <sfx2/dispatch.hxx>
#include <svx/svxids.hrc>
#include <svx/svdetc.hxx>
#include <editeng/boxitem.hxx>
#include <editeng/borderline.hxx>
#include <editeng/colritem.hxx>
#include <editeng/eeitem.hxx>
#include <svx/sdr/table/tabledesign.hxx>
#include <o3tl/enumrange.hxx>

#include "TableDesignPane.hxx"
#include "createtabledesignpanel.hxx"

#include "DrawDocShell.hxx"
#include "ViewShellBase.hxx"
#include "DrawViewShell.hxx"
#include "DrawController.hxx"
#include "glob.hrc"
#include "sdresid.hxx"
#include "EventMultiplexer.hxx"

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::drawing;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::view;
using namespace ::com::sun::star::style;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::ui;

namespace sd {

static const sal_Int32 nPreviewColumns = 5;
static const sal_Int32 nPreviewRows = 5;
static const sal_Int32 nCellWidth = 12; // one pixel is shared with the next cell!
static const sal_Int32 nCellHeight = 7; // one pixel is shared with the next cell!
static const sal_Int32 nBitmapWidth = (nCellWidth * nPreviewColumns) - (nPreviewColumns - 1);
static const sal_Int32 nBitmapHeight = (nCellHeight * nPreviewRows) - (nPreviewRows - 1);

static const OUString* getPropertyNames()
{
    static const OUString gPropNames[ CB_COUNT ] =
    {
        OUString("UseFirstRowStyle") ,
        OUString("UseLastRowStyle") ,
        OUString("UseBandingRowStyle") ,
        OUString("UseFirstColumnStyle") ,
        OUString("UseLastColumnStyle") ,
        OUString("UseBandingColumnStyle")
    };
    return &gPropNames[0];
}

TableDesignWidget::TableDesignWidget( VclBuilderContainer* pParent, ViewShellBase& rBase, bool bModal )
    : mrBase(rBase)
    , mbModal(bModal)
    , mbStyleSelected(false)
    , mbOptionsChanged(false)
{
    pParent->get(m_pValueSet, "previews");
    m_pValueSet->SetStyle(m_pValueSet->GetStyle() | WB_NO_DIRECTSELECT | WB_FLATVALUESET | WB_ITEMBORDER);
    m_pValueSet->SetExtraSpacing(8);
    m_pValueSet->setModal(mbModal);
    if( !mbModal )
    {
        m_pValueSet->SetColor();
    }
    else
    {
        m_pValueSet->SetColor( Color( COL_WHITE ) );
        m_pValueSet->SetBackground( Color( COL_WHITE ) );
    }
    m_pValueSet->SetSelectHdl (LINK(this, TableDesignWidget, implValueSetHdl));

    const OUString* pPropNames = getPropertyNames();
    for (sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i)
    {
        pParent->get(m_aCheckBoxes[i], OUStringToOString(pPropNames[i], RTL_TEXTENCODING_UTF8));
        m_aCheckBoxes[i]->SetClickHdl( LINK( this, TableDesignWidget, implCheckBoxHdl ) );
    }

    // get current controller and initialize listeners
    try
    {
        mxView = Reference< XDrawView >::query(mrBase.GetController());
        addListener();

        Reference< XController > xController( mrBase.GetController(), UNO_QUERY_THROW );
        Reference< XStyleFamiliesSupplier > xFamiliesSupp( xController->getModel(), UNO_QUERY_THROW );
        Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
        const OUString sFamilyName( "table" );
        mxTableFamily = Reference< XIndexAccess >( xFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
    }
    catch (const Exception&)
    {
        OSL_FAIL( "sd::CustomAnimationPane::CustomAnimationPane(), Exception caught!" );
    }

    onSelectionChanged();
    updateControls();
}

TableDesignWidget::~TableDesignWidget()
{
    removeListener();
}

static SfxBindings* getBindings( ViewShellBase& rBase )
{
    if( rBase.GetMainViewShell().get() && rBase.GetMainViewShell()->GetViewFrame() )
        return &rBase.GetMainViewShell()->GetViewFrame()->GetBindings();
    else
        return 0;
}

static SfxDispatcher* getDispatcher( ViewShellBase& rBase )
{
    if( rBase.GetMainViewShell().get() && rBase.GetMainViewShell()->GetViewFrame() )
        return rBase.GetMainViewShell()->GetViewFrame()->GetDispatcher();
    else
        return 0;
}

IMPL_LINK_NOARG_TYPED(TableDesignWidget, implValueSetHdl, ValueSet*, void)
{
    mbStyleSelected = true;
    if( !mbModal )
        ApplyStyle();
}

void TableDesignWidget::ApplyStyle()
{
    try
    {
        OUString sStyleName;
        sal_Int32 nIndex = static_cast< sal_Int32 >( m_pValueSet->GetSelectItemId() ) - 1;

        if( (nIndex >= 0) && (nIndex < mxTableFamily->getCount()) )
        {
            Reference< XNameAccess > xNames( mxTableFamily, UNO_QUERY_THROW );
            sStyleName = xNames->getElementNames()[nIndex];
        }

        if( sStyleName.isEmpty() )
            return;

        SdrView* pView = mrBase.GetDrawView();
        if( mxSelectedTable.is() )
        {
            if( pView )
            {
                SfxRequest aReq( SID_TABLE_STYLE, SfxCallMode::SYNCHRON, SfxGetpApp()->GetPool() );
                aReq.AppendItem( SfxStringItem( SID_TABLE_STYLE, sStyleName ) );

                rtl::Reference< sdr::SelectionController > xController( pView->getSelectionController() );
                if( xController.is() )
                    xController->Execute( aReq );

                SfxBindings* pBindings = getBindings( mrBase );
                if( pBindings )
                {
                    pBindings->Invalidate( SID_UNDO );
                    pBindings->Invalidate( SID_REDO );
                }
            }
        }
        else
        {
            SfxDispatcher* pDispatcher = getDispatcher( mrBase );
            SfxStringItem aArg( SID_TABLE_STYLE, sStyleName );
            pDispatcher->Execute(SID_INSERT_TABLE, SfxCallMode::ASYNCHRON, &aArg, 0 );
        }
    }
    catch( Exception& )
    {
        OSL_FAIL("TableDesignWidget::implValueSetHdl(), exception caught!");
    }
}

IMPL_LINK_NOARG_TYPED(TableDesignWidget, implCheckBoxHdl, Button*, void)
{
    mbOptionsChanged = true;

    if( !mbModal )
        ApplyOptions();

    FillDesignPreviewControl();
}

void TableDesignWidget::ApplyOptions()
{
    static const sal_uInt16 gParamIds[CB_COUNT] =
    {
        ID_VAL_USEFIRSTROWSTYLE, ID_VAL_USELASTROWSTYLE, ID_VAL_USEBANDINGROWSTYLE,
        ID_VAL_USEFIRSTCOLUMNSTYLE, ID_VAL_USELASTCOLUMNSTYLE, ID_VAL_USEBANDINGCOLUMNSTYLE
    };

    if( mxSelectedTable.is() )
    {
        SfxRequest aReq( SID_TABLE_STYLE_SETTINGS, SfxCallMode::SYNCHRON, SfxGetpApp()->GetPool() );

        for( sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i )
        {
            aReq.AppendItem( SfxBoolItem( gParamIds[i], m_aCheckBoxes[i]->IsChecked() ) );
        }

        SdrView* pView = mrBase.GetDrawView();
        if( pView )
        {
            rtl::Reference< sdr::SelectionController > xController( pView->getSelectionController() );
            if( xController.is() )
            {
                xController->Execute( aReq );

                SfxBindings* pBindings = getBindings( mrBase );
                if( pBindings )
                {
                    pBindings->Invalidate( SID_UNDO );
                    pBindings->Invalidate( SID_REDO );
                }
            }
        }
    }
}

void TableDesignWidget::onSelectionChanged()
{
    Reference< XPropertySet > xNewSelection;

    if( mxView.is() ) try
    {
        Reference< XSelectionSupplier >  xSel( mxView, UNO_QUERY_THROW );
        if (xSel.is())
        {
            Any aSel( xSel->getSelection() );
            Sequence< XShape > xShapeSeq;
            if( aSel >>= xShapeSeq )
            {
                if( xShapeSeq.getLength() == 1 )
                    aSel <<= xShapeSeq[0];
            }
            else
            {
                Reference< XShapes > xShapes( aSel, UNO_QUERY );
                if( xShapes.is() && (xShapes->getCount() == 1) )
                    aSel <<= xShapes->getByIndex(0);
            }

            Reference< XShapeDescriptor > xDesc( aSel, UNO_QUERY );
            if( xDesc.is() && ( xDesc->getShapeType() == "com.sun.star.drawing.TableShape" || xDesc->getShapeType() == "com.sun.star.presentation.TableShape" ) )
            {
                xNewSelection = Reference< XPropertySet >::query( xDesc );
            }
        }
    }
    catch( Exception& )
    {
        OSL_FAIL( "sd::TableDesignWidget::onSelectionChanged(), Exception caught!" );
    }

    if( mxSelectedTable != xNewSelection )
    {
        mxSelectedTable = xNewSelection;
        updateControls();
    }
}

void TableValueSet::Resize()
{
    ValueSet::Resize();
    // Calculate the number of rows and columns.
    if( GetItemCount() > 0 )
    {
        Size aValueSetSize = GetSizePixel();

        Image aImage = GetItemImage(GetItemId(0));
        Size aItemSize = aImage.GetSizePixel();

        aItemSize.Width() += 10;
        aItemSize.Height() += 10;
        int nColumnCount = (aValueSetSize.Width() - GetScrollWidth()) / aItemSize.Width();
        if (nColumnCount < 1)
            nColumnCount = 1;

        int nRowCount = (GetItemCount() + nColumnCount - 1) / nColumnCount;
        if (nRowCount < 1)
            nRowCount = 1;

        int nVisibleRowCount = (aValueSetSize.Height()+2) / aItemSize.Height();

        SetColCount ((sal_uInt16)nColumnCount);
        SetLineCount ((sal_uInt16)nRowCount);

        if( !m_bModal )
        {
            WinBits nStyle = GetStyle() & ~(WB_VSCROLL);
            if( nRowCount > nVisibleRowCount )
            {
                nStyle |= WB_VSCROLL;
            }
            SetStyle( nStyle );
        }
    }
}

TableValueSet::TableValueSet(Window *pParent, WinBits nStyle)
    : ValueSet(pParent, nStyle)
    , m_bModal(false)
{
}

void TableValueSet::DataChanged( const DataChangedEvent& /*rDCEvt*/ )
{
    updateSettings();
}

void TableValueSet::updateSettings()
{
    if( !m_bModal )
    {
        SetBackground( GetSettings().GetStyleSettings().GetWindowColor() );
        SetColor( GetSettings().GetStyleSettings().GetWindowColor() );
        SetExtraSpacing(8);
    }
}

VCL_BUILDER_DECL_FACTORY(TableValueSet)
{
    WinBits nWinStyle = WB_TABSTOP;
    OString sBorder = VclBuilder::extractCustomProperty(rMap);
    if (!sBorder.isEmpty())
        nWinStyle |= WB_BORDER;
    rRet = VclPtr<TableValueSet>::Create(pParent, nWinStyle);
}

void TableDesignWidget::updateControls()
{
    static const sal_Bool gDefaults[CB_COUNT] = { sal_True, sal_False, sal_True, sal_False, sal_False, sal_False };

    const bool bHasTable = mxSelectedTable.is();
    const OUString* pPropNames = getPropertyNames();

    for (sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i)
    {
        bool bUse = gDefaults[i];
        if( bHasTable ) try
        {
            mxSelectedTable->getPropertyValue( *pPropNames++ ) >>= bUse;
        }
        catch( Exception& )
        {
            OSL_FAIL("sd::TableDesignWidget::updateControls(), exception caught!");
        }
        m_aCheckBoxes[i]->Check(bUse);
        m_aCheckBoxes[i]->Enable(bHasTable);
    }

    FillDesignPreviewControl();
    m_pValueSet->updateSettings();
    m_pValueSet->Resize();

    sal_uInt16 nSelection = 0;
    if( mxSelectedTable.is() )
    {
        Reference< XNamed > xNamed( mxSelectedTable->getPropertyValue( "TableTemplate" ), UNO_QUERY );
        if( xNamed.is() )
        {
            const OUString sStyleName( xNamed->getName() );

            Reference< XNameAccess > xNames( mxTableFamily, UNO_QUERY );
            if( xNames.is() )
            {
                Sequence< OUString > aNames( xNames->getElementNames() );
                for( sal_Int32 nIndex = 0; nIndex < aNames.getLength(); nIndex++ )
                {
                    if( aNames[nIndex] == sStyleName )
                    {
                        nSelection = (sal_uInt16)nIndex+1;
                        break;
                    }
                }
            }
        }
    }
    m_pValueSet->SelectItem( nSelection );
}

void TableDesignWidget::addListener()
{
    Link<tools::EventMultiplexerEvent&,void> aLink( LINK(this,TableDesignWidget,EventMultiplexerListener) );
    mrBase.GetEventMultiplexer()->AddEventListener (
        aLink,
        tools::EventMultiplexerEvent::EID_EDIT_VIEW_SELECTION
        | tools::EventMultiplexerEvent::EID_CURRENT_PAGE
        | tools::EventMultiplexerEvent::EID_MAIN_VIEW_REMOVED
        | tools::EventMultiplexerEvent::EID_MAIN_VIEW_ADDED
        | tools::EventMultiplexerEvent::EID_DISPOSING);
}

void TableDesignWidget::removeListener()
{
    Link<tools::EventMultiplexerEvent&,void> aLink( LINK(this,TableDesignWidget,EventMultiplexerListener) );
    mrBase.GetEventMultiplexer()->RemoveEventListener( aLink );
}

IMPL_LINK_TYPED(TableDesignWidget,EventMultiplexerListener,
    tools::EventMultiplexerEvent&, rEvent, void)
{
    switch (rEvent.meEventId)
    {
        case tools::EventMultiplexerEvent::EID_CURRENT_PAGE:
        case tools::EventMultiplexerEvent::EID_EDIT_VIEW_SELECTION:
            onSelectionChanged();
            break;

        case tools::EventMultiplexerEvent::EID_MAIN_VIEW_REMOVED:
            mxView.clear();
            onSelectionChanged();
            break;

        case tools::EventMultiplexerEvent::EID_MAIN_VIEW_ADDED:
            mxView = Reference<XDrawView>::query( mrBase.GetController() );
            onSelectionChanged();
            break;
    }
}

struct CellInfo
{
    Color maCellColor;
    Color maTextColor;
    SvxBoxItem maBorder;

    explicit CellInfo( const Reference< XStyle >& xStyle );
};

CellInfo::CellInfo( const Reference< XStyle >& xStyle )
: maBorder(SDRATTR_TABLE_BORDER)
{
    SfxStyleSheet* pStyleSheet = SfxUnoStyleSheet::getUnoStyleSheet( xStyle );
    if( pStyleSheet )
    {
        SfxItemSet& rSet = pStyleSheet->GetItemSet();

        // get style fill color
        if( !GetDraftFillColor(rSet, maCellColor) )
            maCellColor.SetColor( COL_TRANSPARENT );

        // get style text color
        const SvxColorItem* pTextColor = dynamic_cast<const SvxColorItem*>( rSet.GetItem(EE_CHAR_COLOR) );
        if( pTextColor )
            maTextColor = pTextColor->GetValue();
        else
            maTextColor.SetColor( COL_TRANSPARENT );

        // get border
        const SvxBoxItem* pBoxItem = dynamic_cast<const SvxBoxItem*>(rSet.GetItem( SDRATTR_TABLE_BORDER ) );
        if( pBoxItem )
            maBorder = *pBoxItem;
    }
}

typedef std::vector< std::shared_ptr< CellInfo > > CellInfoVector;
typedef std::shared_ptr< CellInfo > CellInfoMatrix[nPreviewColumns][nPreviewRows];

struct TableStyleSettings
{
    bool mbUseFirstRow;
    bool mbUseLastRow;
    bool mbUseFirstColumn;
    bool mbUseLastColumn;
    bool mbUseRowBanding;
    bool mbUseColumnBanding;

    TableStyleSettings()
        : mbUseFirstRow(true)
        , mbUseLastRow(false)
        , mbUseFirstColumn(false)
        , mbUseLastColumn(false)
        , mbUseRowBanding(true)
        , mbUseColumnBanding(false) {}
};

static void FillCellInfoVector( const Reference< XIndexAccess >& xTableStyle, CellInfoVector& rVector )
{
    DBG_ASSERT( xTableStyle.is() && (xTableStyle->getCount() == sdr::table::style_count ), "sd::FillCellInfoVector(), invalid table style!" );
    if( xTableStyle.is() ) try
    {
        rVector.resize( sdr::table::style_count );

        for( sal_Int32 nStyle = 0; nStyle < sdr::table::style_count; ++nStyle )
        {
            Reference< XStyle > xStyle( xTableStyle->getByIndex( nStyle ), UNO_QUERY );
            if( xStyle.is() )
                rVector[nStyle].reset( new CellInfo( xStyle ) );
        }
    }
    catch(Exception&)
    {
        OSL_FAIL("sd::FillCellInfoVector(), exception caught!");
    }
}

static void FillCellInfoMatrix( const CellInfoVector& rStyle, const TableStyleSettings& rSettings, CellInfoMatrix& rMatrix )
{
    for( sal_Int32 nRow = 0; nRow < nPreviewColumns; ++nRow )
    {
        const bool bFirstRow = rSettings.mbUseFirstRow && (nRow == 0);
        const bool bLastRow = rSettings.mbUseLastRow && (nRow == nPreviewColumns - 1);

        for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol )
        {
            std::shared_ptr< CellInfo > xCellInfo;

            // first and last row win first, if used and available
            if( bFirstRow )
            {
                xCellInfo = rStyle[sdr::table::first_row_style];
            }
            else if( bLastRow )
            {
                xCellInfo = rStyle[sdr::table::last_row_style];
            }

            if( !xCellInfo.get() )
            {
                // next come first and last column, if used and available
                if( rSettings.mbUseFirstColumn && (nCol == 0)  )
                {
                    xCellInfo = rStyle[sdr::table::first_column_style];
                }
                else if( rSettings.mbUseLastColumn && (nCol == nPreviewColumns-1) )
                {
                    xCellInfo = rStyle[sdr::table::last_column_style];
                }
            }

            if( !xCellInfo.get() )
            {
                if( rSettings.mbUseRowBanding )
                {
                    if( (nRow & 1) == 0 )
                    {
                        xCellInfo = rStyle[sdr::table::even_rows_style];
                    }
                    else
                    {
                        xCellInfo = rStyle[sdr::table::odd_rows_style];
                    }
                }
            }

            if( !xCellInfo.get() )
            {
                if( rSettings.mbUseColumnBanding )
                {
                    if( (nCol & 1) == 0 )
                    {
                        xCellInfo = rStyle[sdr::table::even_columns_style];
                    }
                    else
                    {
                        xCellInfo = rStyle[sdr::table::odd_columns_style];
                    }
                }
            }

            if( !xCellInfo.get() )
            {
                // use default cell style if non found yet
                xCellInfo = rStyle[sdr::table::body_style];
            }

            rMatrix[nCol][nRow] = xCellInfo;
        }
    }
}

const Bitmap CreateDesignPreview( const Reference< XIndexAccess >& xTableStyle, const TableStyleSettings& rSettings, bool bIsPageDark )
{
    CellInfoVector aCellInfoVector(sdr::table::style_count);
    FillCellInfoVector( xTableStyle, aCellInfoVector );

    CellInfoMatrix aMatrix;
    FillCellInfoMatrix( aCellInfoVector, rSettings, aMatrix );

// bbbbbbbbbbbb w = 12 pixel
// bccccccccccb h = 7 pixel
// bccccccccccb b = border color
// bcttttttttcb c = cell color
// bccccccccccb t = text color
// bccccccccccb
// bbbbbbbbbbbb

    Bitmap aPreviewBmp( Size( nBitmapWidth, nBitmapHeight), 24, NULL );
    BitmapWriteAccess* pAccess = aPreviewBmp.AcquireWriteAccess();
    if( pAccess )
    {
        pAccess->Erase( Color( bIsPageDark ? COL_BLACK : COL_WHITE ) );

        // first draw cell background and text line previews
        sal_Int32 nY = 0;
        sal_Int32 nRow;
        for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
        {
            sal_Int32 nX = 0;
            for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol, nX += nCellWidth-1 )
            {
                std::shared_ptr< CellInfo > xCellInfo( aMatrix[nCol][nRow] );

                Color aTextColor( COL_AUTO );
                if( xCellInfo.get() )
                {
                    // fill cell background
                    const Rectangle aRect( nX, nY, nX + nCellWidth - 1, nY + nCellHeight - 1 );

                    if( xCellInfo->maCellColor.GetColor() != COL_TRANSPARENT )
                    {
                        pAccess->SetFillColor( xCellInfo->maCellColor );
                        pAccess->FillRect( aRect );
                    }

                    aTextColor = xCellInfo->maTextColor;
                }

                // draw text preview line
                if( aTextColor.GetColor() == COL_AUTO )
                    aTextColor.SetColor( bIsPageDark ? COL_WHITE : COL_BLACK );
                pAccess->SetLineColor( aTextColor );
                const Point aPnt1( nX + 2, nY + ((nCellHeight - 1 ) >> 1) );
                const Point aPnt2( nX + nCellWidth - 3, aPnt1.Y() );
                pAccess->DrawLine( aPnt1, aPnt2 );
            }
        }

        // second draw border lines
        nY = 0;
        for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
        {
            sal_Int32 nX = 0;
            for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol, nX += nCellWidth-1 )
            {
                std::shared_ptr< CellInfo > xCellInfo( aMatrix[nCol][nRow] );

                if( xCellInfo.get() )
                {
                    const Point aPntTL( nX, nY );
                    const Point aPntTR( nX + nCellWidth - 1, nY );
                    const Point aPntBL( nX, nY + nCellHeight - 1 );
                    const Point aPntBR( nX + nCellWidth - 1, nY + nCellHeight - 1 );

                    sal_Int32 border_diffs[8] = { 0,-1, 0,1, -1,0, 1,0 };
                    sal_Int32* pDiff = &border_diffs[0];

                    // draw top border
                    for( SvxBoxItemLine nLine : o3tl::enumrange<SvxBoxItemLine>() )
                    {
                        const ::editeng::SvxBorderLine* pBorderLine = xCellInfo->maBorder.GetLine(nLine);
                        if( !pBorderLine || ((pBorderLine->GetOutWidth() == 0) && (pBorderLine->GetInWidth()==0)) )
                            continue;

                        sal_Int32 nBorderCol = nCol + *pDiff++;
                        sal_Int32 nBorderRow = nRow + *pDiff++;
                        if( (nBorderCol >= 0) && (nBorderCol < nPreviewColumns) && (nBorderRow >= 0) && (nBorderRow < nPreviewRows) )
                        {
                            // check border
                            std::shared_ptr< CellInfo > xBorderInfo( aMatrix[nBorderCol][nBorderRow] );
                            if( xBorderInfo.get() )
                            {
                                const ::editeng::SvxBorderLine* pBorderLine2 = xBorderInfo->maBorder.GetLine(static_cast<SvxBoxItemLine>(static_cast<int>(nLine)^1));
                                if( pBorderLine2 && pBorderLine2->HasPriority(*pBorderLine) )
                                    continue; // other border line wins
                            }
                        }

                        pAccess->SetLineColor( pBorderLine->GetColor() );
                        switch( nLine )
                        {
                        case SvxBoxItemLine::TOP: pAccess->DrawLine( aPntTL, aPntTR ); break;
                        case SvxBoxItemLine::BOTTOM: pAccess->DrawLine( aPntBL, aPntBR ); break;
                        case SvxBoxItemLine::LEFT: pAccess->DrawLine( aPntTL, aPntBL ); break;
                        case SvxBoxItemLine::RIGHT: pAccess->DrawLine( aPntTR, aPntBR ); break;
                        }
                    }
                }
            }
        }

        Bitmap::ReleaseAccess( pAccess );
    }

    return aPreviewBmp;
}

void TableDesignWidget::FillDesignPreviewControl()
{
    sal_uInt16 nSelectedItem = m_pValueSet->GetSelectItemId();
    m_pValueSet->Clear();
    try
    {
        TableStyleSettings aSettings;
        if( mxSelectedTable.is() )
        {
            aSettings.mbUseFirstRow = m_aCheckBoxes[CB_HEADER_ROW]->IsChecked();
            aSettings.mbUseLastRow = m_aCheckBoxes[CB_TOTAL_ROW]->IsChecked();
            aSettings.mbUseRowBanding = m_aCheckBoxes[CB_BANDED_ROWS]->IsChecked();
            aSettings.mbUseFirstColumn = m_aCheckBoxes[CB_FIRST_COLUMN]->IsChecked();
            aSettings.mbUseLastColumn = m_aCheckBoxes[CB_LAST_COLUMN]->IsChecked();
            aSettings.mbUseColumnBanding = m_aCheckBoxes[CB_BANDED_COLUMNS]->IsChecked();
        }

        bool bIsPageDark = false;
        if( mxView.is() )
        {
            Reference< XPropertySet > xPageSet( mxView->getCurrentPage(), UNO_QUERY );
            if( xPageSet.is() )
            {
                const OUString sIsBackgroundDark( "IsBackgroundDark" );
                xPageSet->getPropertyValue(sIsBackgroundDark) >>= bIsPageDark;
            }
        }

        sal_Int32 nCount = mxTableFamily->getCount();
        for( sal_Int32 nIndex = 0; nIndex < nCount; ++nIndex ) try
        {
            Reference< XIndexAccess > xTableStyle( mxTableFamily->getByIndex( nIndex ), UNO_QUERY );
            if( xTableStyle.is() )
                m_pValueSet->InsertItem( sal::static_int_cast<sal_uInt16>( nIndex + 1 ), Image( CreateDesignPreview( xTableStyle, aSettings, bIsPageDark ) ) );
        }
        catch( Exception& )
        {
            OSL_FAIL("sd::TableDesignWidget::FillDesignPreviewControl(), exception caught!");
        }
        sal_Int32 nCols = 3;
        sal_Int32 nRows = (nCount+2)/3;
        m_pValueSet->SetColCount(nCols);
        m_pValueSet->SetLineCount(nRows);
        WinBits nStyle = m_pValueSet->GetStyle() & ~(WB_VSCROLL);
        m_pValueSet->SetStyle(nStyle);
        Size aSize(m_pValueSet->GetOptimalSize());
        aSize.Width() += (10 * nCols);
        aSize.Height() += (10 * nRows);
        m_pValueSet->set_width_request(aSize.Width());
        m_pValueSet->set_height_request(aSize.Height());
    }
    catch( Exception& )
    {
        OSL_FAIL("sd::TableDesignWidget::FillDesignPreviewControl(), exception caught!");
    }
    m_pValueSet->SelectItem(nSelectedItem);
}

short TableDesignDialog::Execute()
{
    if( ModalDialog::Execute() )
    {
        if( aImpl.isStyleChanged() )
            aImpl.ApplyStyle();

        if( aImpl.isOptionsChanged() )
            aImpl.ApplyOptions();
        return RET_OK;
    }
    return RET_CANCEL;
}

VclPtr<vcl::Window> createTableDesignPanel( vcl::Window* pParent, ViewShellBase& rBase )
{
    return VclPtr<TableDesignPane>::Create( pParent, rBase );
}

void showTableDesignDialog( vcl::Window* pParent, ViewShellBase& rBase )
{
    ScopedVclPtrInstance< TableDesignDialog > xDialog( pParent, rBase );
    xDialog->Execute();
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */