summaryrefslogtreecommitdiff
path: root/sd/source/ui/table/TableDesignPane.cxx
blob: 3e6b665b524587091bbc7dcaa2bcde5b0520a3d7 (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
/* -*- 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 <sfx2/viewfrm.hxx>
#include <vcl/virdev.hxx>
#include <vcl/layout.hxx>
#include <vcl/settings.hxx>
#include <vcl/builderfactory.hxx>

#include <tools/debug.hxx>
#include <svl/style.hxx>
#include <svl/stritem.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 <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 OUStringLiteral gPropNames[CB_COUNT] =
{
    "UseFirstRowStyle",
    "UseLastRowStyle",
    "UseBandingRowStyle",
    "UseFirstColumnStyle",
    "UseLastColumnStyle",
    "UseBandingColumnStyle"
};

TableDesignWidget::TableDesignWidget( VclBuilderContainer* pParent, ViewShellBase& rBase )
    : mrBase(rBase)
{
    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(false);
    m_pValueSet->SetColor();
    m_pValueSet->SetSelectHdl (LINK(this, TableDesignWidget, implValueSetHdl));

    for (sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i)
    {
        pParent->get(m_aCheckBoxes[i], OString(gPropNames[i].data, gPropNames[i].size));
        m_aCheckBoxes[i]->SetClickHdl( LINK( this, TableDesignWidget, implCheckBoxHdl ) );
    }

    // get current controller and initialize listeners
    try
    {
        mxView.set(mrBase.GetController(), UNO_QUERY);
        addListener();

        Reference< XController > xController( mrBase.GetController(), UNO_SET_THROW );
        Reference< XStyleFamiliesSupplier > xFamiliesSupp( xController->getModel(), UNO_QUERY_THROW );
        Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
        const OUString sFamilyName( "table" );
        mxTableFamily.set( 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 const & rBase )
{
    if( rBase.GetMainViewShell().get() && rBase.GetMainViewShell()->GetViewFrame() )
        return &rBase.GetMainViewShell()->GetViewFrame()->GetBindings();
    else
        return nullptr;
}

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

IMPL_LINK_NOARG(TableDesignWidget, implValueSetHdl, ValueSet*, void)
{
    ApplyStyle();
}

void TableDesignWidget::ApplyStyle()
{
    try
    {
        OUString sStyleName;
        sal_Int32 nIndex = static_cast< sal_Int32 >( m_pValueSet->GetSelectedItemId() ) - 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 ) );

                const 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->ExecuteList(SID_INSERT_TABLE, SfxCallMode::ASYNCHRON,
                    { &aArg });
        }
    }
    catch( Exception& )
    {
        OSL_FAIL("TableDesignWidget::implValueSetHdl(), exception caught!");
    }
}

IMPL_LINK_NOARG(TableDesignWidget, implCheckBoxHdl, Button*, void)
{
    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() )
        return;

    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 )
        return;

    const 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 );
        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.set( xDesc, UNO_QUERY );
        }
    }
    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 )
        return;

    Size aValueSetSize = GetSizePixel();

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

    aItemSize.AdjustHeight(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 (static_cast<sal_uInt16>(nColumnCount));
    SetLineCount (static_cast<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_FACTORY_CONSTRUCTOR(TableValueSet, WB_TABSTOP)

void TableDesignWidget::updateControls()
{
    static const bool gDefaults[CB_COUNT] = { true, false, true, false, false, false };

    const bool bHasTable = mxSelectedTable.is();

    for (sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i)
    {
        bool bUse = gDefaults[i];
        if( bHasTable ) try
        {
            mxSelectedTable->getPropertyValue( gPropNames[i] ) >>= 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 = static_cast<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 );
}

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

IMPL_LINK(TableDesignWidget,EventMultiplexerListener,
    tools::EventMultiplexerEvent&, rEvent, void)
{
    switch (rEvent.meEventId)
    {
        case EventMultiplexerEventId::CurrentPageChanged:
        case EventMultiplexerEventId::EditViewSelection:
            onSelectionChanged();
            break;

        case EventMultiplexerEventId::MainViewRemoved:
            mxView.clear();
            onSelectionChanged();
            break;

        case EventMultiplexerEventId::MainViewAdded:
            mxView.set( mrBase.GetController(), UNO_QUERY );
            onSelectionChanged();
            break;

        default: break;
    }
}

struct CellInfo
{
    Color maCellColor;
    Color maTextColor;
    std::shared_ptr<SvxBoxItem> maBorder;

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

CellInfo::CellInfo( const Reference< XStyle >& xStyle )
: maBorder(std::make_shared<SvxBoxItem>(SDRATTR_TABLE_BORDER))
{
    SfxStyleSheet* pStyleSheet = SfxUnoStyleSheet::getUnoStyleSheet( xStyle );
    if( !pStyleSheet )
        return;

    SfxItemSet& rSet = pStyleSheet->GetItemSet();

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

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

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

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() )
        return;

    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 * nPreviewColumns) + nRow] = xCellInfo;
        }
    }
}

static const BitmapEx 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

    ScopedVclPtr<VirtualDevice> pVirDev(VclPtr<VirtualDevice>::Create());
    Size aBmpSize(nBitmapWidth, nBitmapHeight);
    pVirDev->SetOutputSizePixel(aBmpSize);

    pVirDev->SetBackground( bIsPageDark ? COL_BLACK : COL_WHITE );
    pVirDev->Erase();

    // 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 * nPreviewColumns) + nRow]);

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

                if( xCellInfo->maCellColor != COL_TRANSPARENT )
                {
                    pVirDev->SetFillColor( xCellInfo->maCellColor );
                    pVirDev->DrawRect( aRect );
                }

                aTextColor = xCellInfo->maTextColor;
            }

            // draw text preview line
            if( aTextColor == COL_AUTO )
                aTextColor = bIsPageDark ? COL_WHITE : COL_BLACK;
            pVirDev->SetLineColor( aTextColor );
            const Point aPnt1( nX + 2, nY + ((nCellHeight - 1 ) >> 1) );
            const Point aPnt2( nX + nCellWidth - 3, aPnt1.Y() );
            pVirDev->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 * nPreviewColumns) + 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 * nPreviewColumns) + 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
                        }
                    }

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

    return pVirDev->GetBitmapEx(Point(0,0), aBmpSize);
}

void TableDesignWidget::FillDesignPreviewControl()
{
    sal_uInt16 nSelectedItem = m_pValueSet->GetSelectedItemId();
    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.AdjustWidth(10 * nCols);
        aSize.AdjustHeight(10 * nRows);
        m_pValueSet->set_width_request(aSize.Width());
        m_pValueSet->set_height_request(aSize.Height());
        m_pValueSet->Resize();
    }
    catch( Exception& )
    {
        OSL_FAIL("sd::TableDesignWidget::FillDesignPreviewControl(), exception caught!");
    }
    m_pValueSet->SelectItem(nSelectedItem);
}

VclPtr<vcl::Window> createTableDesignPanel( vcl::Window* pParent, ViewShellBase& rBase )
{
    VclPtr<TableDesignPane> pRet = nullptr;
    try
    {
        pRet = VclPtr<TableDesignPane>::Create( pParent, rBase );
    }
    catch (const uno::Exception&)
    {
    }
    return pRet;
}

}

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