1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/drawing/XDrawView.hpp>
26 #include <com/sun/star/frame/XController.hpp>
27 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28 #include <com/sun/star/util/XModifiable.hpp>
29 #include <com/sun/star/view/XSelectionSupplier.hpp>
30 #include <com/sun/star/style/XStyle.hpp>
31 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
33 #include <comphelper/sequence.hxx>
34 #include <sfx2/viewfrm.hxx>
35 #include <vcl/commandevent.hxx>
36 #include <vcl/image.hxx>
37 #include <vcl/settings.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/virdev.hxx>
41 #include <tools/debug.hxx>
42 #include <comphelper/diagnose_ex.hxx>
43 #include <svl/style.hxx>
44 #include <svl/stritem.hxx>
45 #include <sfx2/bindings.hxx>
46 #include <sfx2/app.hxx>
47 #include <sfx2/request.hxx>
48 #include <sfx2/dispatch.hxx>
49 #include <svx/svxids.hrc>
50 #include <svx/svdetc.hxx>
51 #include <svx/svxdlg.hxx>
52 #include <editeng/boxitem.hxx>
53 #include <editeng/borderline.hxx>
54 #include <editeng/colritem.hxx>
55 #include <editeng/eeitem.hxx>
56 #include <svx/sdr/table/tabledesign.hxx>
57 #include <svx/sdr/table/tablecontroller.hxx>
58 #include <o3tl/enumrange.hxx>
60 #include <TableDesignPane.hxx>
62 #include <stlsheet.hxx>
63 #include <strings.hrc>
64 #include <sdresid.hxx>
65 #include <bitmaps.hlst>
66 #include <ViewShell.hxx>
67 #include <ViewShellBase.hxx>
68 #include <EventMultiplexer.hxx>
70 using namespace ::com::sun::star
;
71 using namespace ::com::sun::star::uno
;
72 using namespace ::com::sun::star::drawing
;
73 using namespace ::com::sun::star::container
;
74 using namespace ::com::sun::star::beans
;
75 using namespace ::com::sun::star::view
;
76 using namespace ::com::sun::star::style
;
77 using namespace ::com::sun::star::frame
;
78 using namespace ::com::sun::star::lang
;
79 using namespace ::com::sun::star::ui
;
83 const sal_Int32 nPreviewColumns
= 5;
84 const sal_Int32 nPreviewRows
= 5;
85 const sal_Int32 nCellWidth
= 12; // one pixel is shared with the next cell!
86 const sal_Int32 nCellHeight
= 7; // one pixel is shared with the next cell!
87 const sal_Int32 nBitmapWidth
= (nCellWidth
* nPreviewColumns
) - (nPreviewColumns
- 1);
88 const sal_Int32 nBitmapHeight
= (nCellHeight
* nPreviewRows
) - (nPreviewRows
- 1);
90 const std::u16string_view gPropNames
[CB_COUNT
] =
94 u
"UseBandingRowStyle",
95 u
"UseFirstColumnStyle",
96 u
"UseLastColumnStyle",
97 u
"UseBandingColumnStyle"
100 constexpr std::u16string_view aTableStyleBaseName
= u
"table";
102 TableDesignWidget::TableDesignWidget(weld::Builder
& rBuilder
, ViewShellBase
& rBase
)
104 , m_xMenu(rBuilder
.weld_menu("menu"))
105 , m_xValueSet(new TableValueSet(rBuilder
.weld_scrolled_window("previewswin", true)))
106 , m_xValueSetWin(new weld::CustomWeld(rBuilder
, "previews", *m_xValueSet
))
108 m_xValueSet
->SetStyle(m_xValueSet
->GetStyle() | WB_NO_DIRECTSELECT
| WB_FLATVALUESET
| WB_ITEMBORDER
);
109 m_xValueSet
->SetExtraSpacing(8);
110 m_xValueSet
->setModal(false);
111 m_xValueSet
->SetColor();
112 m_xValueSet
->SetSelectHdl(LINK(this, TableDesignWidget
, implValueSetHdl
));
113 m_xValueSet
->SetContextMenuHandler(LINK(this, TableDesignWidget
, implContextMenuHandler
));
115 for (sal_uInt16 i
= CB_HEADER_ROW
; i
<= CB_BANDED_COLUMNS
; ++i
)
117 m_aCheckBoxes
[i
] = rBuilder
.weld_check_button(OUString(gPropNames
[i
]));
118 m_aCheckBoxes
[i
]->connect_toggled(LINK(this, TableDesignWidget
, implCheckBoxHdl
));
121 // get current controller and initialize listeners
124 mxView
.set(mrBase
.GetController(), UNO_QUERY
);
127 Reference
< XController
> xController( mrBase
.GetController(), UNO_SET_THROW
);
128 Reference
< XStyleFamiliesSupplier
> xFamiliesSupp( xController
->getModel(), UNO_QUERY_THROW
);
129 Reference
< XNameAccess
> xFamilies( xFamiliesSupp
->getStyleFamilies() );
130 mxTableFamily
.set( xFamilies
->getByName( "table" ), UNO_QUERY_THROW
);
131 mxCellFamily
.set( xFamilies
->getByName( "cell" ), UNO_QUERY_THROW
);
133 catch (const Exception
&)
135 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPane::CustomAnimationPane()" );
138 onSelectionChanged();
142 TableDesignWidget::~TableDesignWidget()
147 void TableDesignWidget::setDocumentModified()
151 Reference
<XController
> xController(mrBase
.GetController(), UNO_SET_THROW
);
152 Reference
<util::XModifiable
> xModifiable(xController
->getModel(), UNO_QUERY_THROW
);
153 xModifiable
->setModified(true);
157 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::setDocumentModified()");
161 IMPL_LINK(TableDesignWidget
, implContextMenuHandler
, const Point
*, pPoint
, void)
163 auto nClickedItemId
= pPoint
? m_xValueSet
->GetItemId(*pPoint
) : m_xValueSet
->GetSelectedItemId();
167 if (nClickedItemId
> mxTableFamily
->getCount())
172 Reference
<XStyle
> xStyle(mxTableFamily
->getByIndex(nClickedItemId
- 1), UNO_QUERY_THROW
);
174 m_xMenu
->set_visible("clone", true);
175 m_xMenu
->set_visible("format", true);
176 m_xMenu
->set_visible("delete", xStyle
->isUserDefined());
177 m_xMenu
->set_visible("reset", !xStyle
->isUserDefined());
178 m_xMenu
->set_sensitive("reset", Reference
<util::XModifiable
>(xStyle
, UNO_QUERY_THROW
)->isModified());
182 m_xMenu
->set_visible("clone", false);
183 m_xMenu
->set_visible("format", false);
184 m_xMenu
->set_visible("delete", false);
185 m_xMenu
->set_visible("reset", false);
190 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::implContextMenuHandler()");
193 m_xValueSet
->SelectItem(nClickedItemId
);
195 Point aPosition
= pPoint
? *pPoint
: m_xValueSet
->GetItemRect(nClickedItemId
).Center();
196 OUString aCommand
= m_xMenu
->popup_at_rect(m_xValueSet
->GetDrawingArea(), ::tools::Rectangle(aPosition
, Size(1,1)));
198 if (aCommand
== "new")
200 else if (aCommand
== "clone")
202 else if (aCommand
== "delete")
204 else if (aCommand
== "reset")
206 else if (!aCommand
.isEmpty())
212 OUString
getNewStyleName(const Reference
<XNameContainer
>& rFamily
, std::u16string_view rBaseName
)
214 OUString
aName(rBaseName
);
215 sal_Int32 nIndex
= 1;
216 while(rFamily
->hasByName(aName
))
218 aName
= rBaseName
+ OUString::number(nIndex
++);
225 void TableDesignWidget::InsertStyle()
229 Reference
<XSingleServiceFactory
> xFactory(mxTableFamily
, UNO_QUERY_THROW
);
230 Reference
<XNameContainer
> xTableFamily(mxTableFamily
, UNO_QUERY_THROW
);
231 Reference
<XNameReplace
> xTableStyle(xFactory
->createInstance(), UNO_QUERY_THROW
);
232 const OUString
aName(getNewStyleName(xTableFamily
, aTableStyleBaseName
));
233 xTableFamily
->insertByName(aName
, Any(xTableStyle
));
235 Reference
<XStyle
> xCellStyle(mxCellFamily
->getByName("default"), UNO_QUERY_THROW
);
237 xTableStyle
->replaceByName("body", Any(xCellStyle
));
238 xTableStyle
->replaceByName("odd-rows" , Any(xCellStyle
));
239 xTableStyle
->replaceByName("odd-columns" , Any(xCellStyle
));
240 xTableStyle
->replaceByName("first-row" , Any(xCellStyle
));
241 xTableStyle
->replaceByName("first-column" , Any(xCellStyle
));
242 xTableStyle
->replaceByName("last-row" , Any(xCellStyle
));
243 xTableStyle
->replaceByName("last-column" , Any(xCellStyle
));
247 setDocumentModified();
251 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::InsertStyle()");
255 void TableDesignWidget::CloneStyle()
259 Reference
<XNameAccess
> xSrcTableStyle(mxTableFamily
->getByIndex(m_xValueSet
->GetSelectedItemId() - 1), UNO_QUERY_THROW
);
261 Reference
<XSingleServiceFactory
> xFactory(mxTableFamily
, UNO_QUERY_THROW
);
262 Reference
<XNameContainer
> xTableFamily(mxTableFamily
, UNO_QUERY_THROW
);
263 Reference
<XNameReplace
> xDestTableStyle(xFactory
->createInstance(), UNO_QUERY_THROW
);
264 const OUString
aName(getNewStyleName(xTableFamily
, aTableStyleBaseName
));
265 xTableFamily
->insertByName(aName
, Any(xDestTableStyle
));
267 auto aNames(xSrcTableStyle
->getElementNames());
268 for (const auto& name
: aNames
)
270 Reference
<XStyle
> xSrcCellStyle(xSrcTableStyle
->getByName(name
), UNO_QUERY
);
271 if (xSrcCellStyle
&& xSrcCellStyle
->isUserDefined())
273 Reference
<XSingleServiceFactory
> xCellFactory(mxCellFamily
, UNO_QUERY_THROW
);
274 Reference
<XStyle
> xDestCellStyle(xCellFactory
->createInstance(), UNO_QUERY_THROW
);
275 xDestCellStyle
->setParentStyle(xSrcCellStyle
->getParentStyle());
276 const OUString
aStyleName(getNewStyleName(mxCellFamily
, Concat2View(aName
+ "-" + name
)));
277 mxCellFamily
->insertByName(aStyleName
, Any(xDestCellStyle
));
279 rtl::Reference xSrcStyleSheet
= static_cast<SdStyleSheet
*>(xSrcCellStyle
.get());
280 rtl::Reference xDestStyleSheet
= static_cast<SdStyleSheet
*>(xDestCellStyle
.get());
282 xDestStyleSheet
->GetItemSet().Put(xSrcStyleSheet
->GetItemSet());
284 xDestTableStyle
->replaceByName(name
, Any(xDestCellStyle
));
287 xDestTableStyle
->replaceByName(name
, Any(xSrcCellStyle
));
292 setDocumentModified();
296 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::CloneStyle()");
300 void TableDesignWidget::ResetStyle()
304 Reference
<XIndexReplace
> xTableStyle(mxTableFamily
->getByIndex(m_xValueSet
->GetSelectedItemId() - 1), UNO_QUERY_THROW
);
306 for (sal_Int32 i
= 0; i
< xTableStyle
->getCount(); ++i
)
308 Reference
<XStyle
> xCellStyle(xTableStyle
->getByIndex(i
), UNO_QUERY
);
309 while (xCellStyle
&& xCellStyle
->isUserDefined() && !xCellStyle
->getParentStyle().isEmpty())
310 xCellStyle
.set(mxCellFamily
->getByName(xCellStyle
->getParentStyle()), UNO_QUERY
);
312 xTableStyle
->replaceByIndex(i
, Any(xCellStyle
));
315 endTextEditForStyle(xTableStyle
);
316 Reference
<util::XModifiable
>(xTableStyle
, UNO_QUERY_THROW
)->setModified(false);
319 setDocumentModified();
323 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::ResetStyle()");
327 void TableDesignWidget::DeleteStyle()
331 Reference
<XStyle
> xTableStyle(mxTableFamily
->getByIndex(m_xValueSet
->GetSelectedItemId() - 1), UNO_QUERY_THROW
);
333 if (xTableStyle
->isInUse())
335 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
336 m_xValueSet
->GetDrawingArea(), VclMessageType::Question
, VclButtonsType::YesNo
, SdResId(STR_REMOVE_TABLESTYLE
)));
338 if (xBox
->run() != RET_YES
)
341 endTextEditForStyle(xTableStyle
);
344 Reference
<XNameContainer
>(mxTableFamily
, UNO_QUERY_THROW
)->removeByName(xTableStyle
->getName());
347 setDocumentModified();
351 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::DeleteStyle()");
355 void TableDesignWidget::EditStyle(const OUString
& rCommand
)
359 Reference
<XNameReplace
> xTableStyle(mxTableFamily
->getByIndex(m_xValueSet
->GetSelectedItemId() - 1), UNO_QUERY_THROW
);
360 Reference
<XStyle
> xCellStyle(xTableStyle
->getByName(rCommand
), UNO_QUERY_THROW
);
361 rtl::Reference xStyleSheet
= static_cast<SdStyleSheet
*>(xCellStyle
.get());
363 bool bUserDefined
= xStyleSheet
->IsEditable();
366 Reference
<XSingleServiceFactory
> xFactory(mxCellFamily
, UNO_QUERY_THROW
);
367 xCellStyle
.set(xFactory
->createInstance(), UNO_QUERY_THROW
);
368 xCellStyle
->setParentStyle(xStyleSheet
->getName());
369 xStyleSheet
= static_cast<SdStyleSheet
*>(xCellStyle
.get());
372 SfxItemSet
aNewAttr(xStyleSheet
->GetItemSet());
374 // merge drawing layer text distance items into SvxBoxItem used by the dialog
375 SvxBoxItem
aBoxItem(sdr::table::SvxTableController::TextDistancesToSvxBoxItem(aNewAttr
));
376 aNewAttr
.Put(aBoxItem
);
378 // inner borders do not apply to a cell style
379 SvxBoxInfoItem
aBoxInfoItem(aNewAttr
.Get(SDRATTR_TABLE_BORDER_INNER
));
380 aBoxInfoItem
.SetTable(false);
381 aNewAttr
.Put(aBoxInfoItem
);
383 SvxAbstractDialogFactory
* pFact
= SvxAbstractDialogFactory::Create();
384 ScopedVclPtr
<SfxAbstractTabDialog
> pDlg(pFact
? pFact
->CreateSvxFormatCellsDialog(
385 mrBase
.GetFrameWeld(), &aNewAttr
, mrBase
.GetDrawView()->GetModel(), true) : nullptr);
386 if (pDlg
&& pDlg
->Execute() == RET_OK
)
388 endTextEditForStyle(xTableStyle
);
392 Reference
<XNamed
> xNamed(xTableStyle
, UNO_QUERY_THROW
);
393 const OUString
aStyleName(getNewStyleName(mxCellFamily
, Concat2View(xNamed
->getName() + "-" + rCommand
)));
394 mxCellFamily
->insertByName(aStyleName
, Any(xCellStyle
));
395 xTableStyle
->replaceByName(rCommand
, Any(xCellStyle
));
398 SfxItemSet
aNewSet(*pDlg
->GetOutputItemSet());
399 sdr::table::SvxTableController::SvxBoxItemToTextDistances(aBoxItem
, aNewSet
);
400 sdr::properties::CleanupFillProperties(aNewSet
);
401 xStyleSheet
->GetItemSet().Put(aNewSet
);
402 xStyleSheet
->Broadcast(SfxHint(SfxHintId::DataChanged
));
405 setDocumentModified();
410 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::EditStyle()");
414 static SfxBindings
* getBindings( ViewShellBase
const & rBase
)
416 if( rBase
.GetMainViewShell() && rBase
.GetMainViewShell()->GetViewFrame() )
417 return &rBase
.GetMainViewShell()->GetViewFrame()->GetBindings();
422 static SfxDispatcher
* getDispatcher( ViewShellBase
const & rBase
)
424 if( rBase
.GetMainViewShell() && rBase
.GetMainViewShell()->GetViewFrame() )
425 return rBase
.GetMainViewShell()->GetViewFrame()->GetDispatcher();
430 IMPL_LINK_NOARG(TableDesignWidget
, implValueSetHdl
, ValueSet
*, void)
435 void TableDesignWidget::ApplyStyle()
440 sal_Int32 nIndex
= static_cast< sal_Int32
>( m_xValueSet
->GetSelectedItemId() ) - 1;
442 if( (nIndex
>= 0) && (nIndex
< mxTableFamily
->getCount()) )
444 Reference
< XNameAccess
> xNames( mxTableFamily
, UNO_QUERY_THROW
);
445 sStyleName
= xNames
->getElementNames()[nIndex
];
447 else if (nIndex
== mxTableFamily
->getCount())
453 if( sStyleName
.isEmpty() )
456 SdrView
* pView
= mrBase
.GetDrawView();
457 if( mxSelectedTable
.is() )
461 if (pView
->IsTextEdit())
462 pView
->SdrEndTextEdit();
464 SfxRequest
aReq( SID_TABLE_STYLE
, SfxCallMode::SYNCHRON
, SfxGetpApp()->GetPool() );
465 aReq
.AppendItem( SfxStringItem( SID_TABLE_STYLE
, sStyleName
) );
467 const rtl::Reference
< sdr::SelectionController
>& xController( pView
->getSelectionController() );
468 if( xController
.is() )
469 xController
->Execute( aReq
);
471 SfxBindings
* pBindings
= getBindings( mrBase
);
474 pBindings
->Invalidate( SID_UNDO
);
475 pBindings
->Invalidate( SID_REDO
);
478 setDocumentModified();
482 SfxDispatcher
* pDispatcher
= getDispatcher( mrBase
);
483 SfxStringItem
aArg( SID_TABLE_STYLE
, sStyleName
);
484 pDispatcher
->ExecuteList(SID_INSERT_TABLE
, SfxCallMode::ASYNCHRON
,
490 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::implValueSetHdl()");
494 IMPL_LINK_NOARG(TableDesignWidget
, implCheckBoxHdl
, weld::Toggleable
&, void)
497 FillDesignPreviewControl();
500 void TableDesignWidget::ApplyOptions()
502 static const sal_uInt16 gParamIds
[CB_COUNT
] =
504 ID_VAL_USEFIRSTROWSTYLE
, ID_VAL_USELASTROWSTYLE
, ID_VAL_USEBANDINGROWSTYLE
,
505 ID_VAL_USEFIRSTCOLUMNSTYLE
, ID_VAL_USELASTCOLUMNSTYLE
, ID_VAL_USEBANDINGCOLUMNSTYLE
508 if( !mxSelectedTable
.is() )
511 SfxRequest
aReq( SID_TABLE_STYLE_SETTINGS
, SfxCallMode::SYNCHRON
, SfxGetpApp()->GetPool() );
513 for( sal_uInt16 i
= CB_HEADER_ROW
; i
<= CB_BANDED_COLUMNS
; ++i
)
515 aReq
.AppendItem( SfxBoolItem( gParamIds
[i
], m_aCheckBoxes
[i
]->get_active() ) );
518 SdrView
* pView
= mrBase
.GetDrawView();
522 const rtl::Reference
< sdr::SelectionController
>& xController( pView
->getSelectionController() );
523 if( xController
.is() )
525 xController
->Execute( aReq
);
527 SfxBindings
* pBindings
= getBindings( mrBase
);
530 pBindings
->Invalidate( SID_UNDO
);
531 pBindings
->Invalidate( SID_REDO
);
534 setDocumentModified();
537 void TableDesignWidget::onSelectionChanged()
539 Reference
< XPropertySet
> xNewSelection
;
541 if( mxView
.is() ) try
543 Reference
< XSelectionSupplier
> xSel( mxView
, UNO_QUERY_THROW
);
544 Any
aSel( xSel
->getSelection() );
545 Sequence
< XShape
> xShapeSeq
;
546 if( aSel
>>= xShapeSeq
)
548 if( xShapeSeq
.getLength() == 1 )
549 aSel
<<= xShapeSeq
[0];
553 Reference
< XShapes
> xShapes( aSel
, UNO_QUERY
);
554 if( xShapes
.is() && (xShapes
->getCount() == 1) )
555 aSel
= xShapes
->getByIndex(0);
558 Reference
< XShapeDescriptor
> xDesc( aSel
, UNO_QUERY
);
559 if( xDesc
.is() && ( xDesc
->getShapeType() == "com.sun.star.drawing.TableShape" || xDesc
->getShapeType() == "com.sun.star.presentation.TableShape" ) )
561 xNewSelection
.set( xDesc
, UNO_QUERY
);
566 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::onSelectionChanged()" );
569 if( mxSelectedTable
!= xNewSelection
)
571 mxSelectedTable
= xNewSelection
;
576 bool TableValueSet::Command(const CommandEvent
& rEvent
)
578 if (rEvent
.GetCommand() != CommandEventId::ContextMenu
)
579 return ValueSet::Command(rEvent
);
581 maContextMenuHandler
.Call(rEvent
.IsMouseEvent() ? &rEvent
.GetMousePosPixel() : nullptr);
585 void TableValueSet::Resize()
588 // Calculate the number of rows and columns.
589 if( GetItemCount() <= 0 )
592 Size aValueSetSize
= GetOutputSizePixel();
594 Image aImage
= GetItemImage(GetItemId(0));
595 Size aItemSize
= aImage
.GetSizePixel();
597 aItemSize
.AdjustHeight(10 );
598 int nColumnCount
= (aValueSetSize
.Width() - GetScrollWidth()) / aItemSize
.Width();
599 if (nColumnCount
< 1)
602 int nRowCount
= (GetItemCount() + nColumnCount
- 1) / nColumnCount
;
606 int nVisibleRowCount
= std::min(nRowCount
, getMaxRowCount());
608 SetColCount (static_cast<sal_uInt16
>(nColumnCount
));
609 SetLineCount (static_cast<sal_uInt16
>(nVisibleRowCount
));
613 WinBits nStyle
= GetStyle() & ~WB_VSCROLL
;
614 if( nRowCount
> nVisibleRowCount
)
616 nStyle
|= WB_VSCROLL
;
622 TableValueSet::TableValueSet(std::unique_ptr
<weld::ScrolledWindow
> pScrolledWindow
)
623 : ValueSet(std::move(pScrolledWindow
))
628 void TableValueSet::StyleUpdated()
633 void TableValueSet::updateSettings()
637 Color aColor
= Application::GetSettings().GetStyleSettings().GetWindowColor();
643 void TableDesignWidget::updateControls()
645 static const bool gDefaults
[CB_COUNT
] = { true, false, true, false, false, false };
647 const bool bHasTable
= mxSelectedTable
.is();
649 for (sal_uInt16 i
= CB_HEADER_ROW
; i
<= CB_BANDED_COLUMNS
; ++i
)
651 bool bUse
= gDefaults
[i
];
654 mxSelectedTable
->getPropertyValue( OUString(gPropNames
[i
]) ) >>= bUse
;
658 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::updateControls()");
660 m_aCheckBoxes
[i
]->set_active(bUse
);
661 m_aCheckBoxes
[i
]->set_sensitive(bHasTable
);
664 FillDesignPreviewControl();
665 m_xValueSet
->updateSettings();
666 m_xValueSet
->Resize();
668 if( mxSelectedTable
.is() )
670 Reference
< XNamed
> xNamed( mxSelectedTable
->getPropertyValue( "TableTemplate" ), UNO_QUERY
);
672 selectStyle(xNamed
->getName());
676 void TableDesignWidget::selectStyle(std::u16string_view rStyle
)
678 Reference
< XNameAccess
> xNames( mxTableFamily
, UNO_QUERY
);
681 Sequence
< OUString
> aNames( xNames
->getElementNames() );
682 sal_Int32 nIndex
= comphelper::findValue(aNames
, rStyle
);
684 m_xValueSet
->SelectItem(static_cast<sal_uInt16
>(nIndex
) + 1);
688 void TableDesignWidget::endTextEditForStyle(const Reference
<XInterface
>& rStyle
)
690 if (!mxSelectedTable
)
693 Reference
<XInterface
> xTableStyle(mxSelectedTable
->getPropertyValue("TableTemplate"), UNO_QUERY
);
694 if (xTableStyle
!= rStyle
)
697 if (mrBase
.GetDrawView()->IsTextEdit())
698 mrBase
.GetDrawView()->SdrEndTextEdit();
701 void TableDesignWidget::addListener()
703 Link
<tools::EventMultiplexerEvent
&,void> aLink( LINK(this,TableDesignWidget
,EventMultiplexerListener
) );
704 mrBase
.GetEventMultiplexer()->AddEventListener( aLink
);
707 void TableDesignWidget::removeListener()
709 Link
<tools::EventMultiplexerEvent
&,void> aLink( LINK(this,TableDesignWidget
,EventMultiplexerListener
) );
710 mrBase
.GetEventMultiplexer()->RemoveEventListener( aLink
);
713 IMPL_LINK(TableDesignWidget
,EventMultiplexerListener
,
714 tools::EventMultiplexerEvent
&, rEvent
, void)
716 switch (rEvent
.meEventId
)
718 case EventMultiplexerEventId::CurrentPageChanged
:
719 case EventMultiplexerEventId::EditViewSelection
:
720 onSelectionChanged();
723 case EventMultiplexerEventId::MainViewRemoved
:
725 onSelectionChanged();
728 case EventMultiplexerEventId::MainViewAdded
:
729 mxView
.set( mrBase
.GetController(), UNO_QUERY
);
730 onSelectionChanged();
743 std::shared_ptr
<SvxBoxItem
> maBorder
;
745 explicit CellInfo( const Reference
< XStyle
>& xStyle
);
750 CellInfo::CellInfo( const Reference
< XStyle
>& xStyle
)
751 : maBorder(std::make_shared
<SvxBoxItem
>(SDRATTR_TABLE_BORDER
))
753 SfxStyleSheet
* pStyleSheet
= SfxUnoStyleSheet::getUnoStyleSheet( xStyle
);
757 SfxItemSet
& rSet
= pStyleSheet
->GetItemSet();
759 // get style fill color
760 if( !GetDraftFillColor(rSet
, maCellColor
) )
761 maCellColor
= COL_TRANSPARENT
;
763 // get style text color
764 const SvxColorItem
* pTextColor
= rSet
.GetItem(EE_CHAR_COLOR
);
766 maTextColor
= pTextColor
->GetValue();
768 maTextColor
= COL_TRANSPARENT
;
771 const SvxBoxItem
* pBoxItem
= rSet
.GetItem( SDRATTR_TABLE_BORDER
);
773 maBorder
.reset(pBoxItem
->Clone());
776 typedef std::vector
< std::shared_ptr
< CellInfo
> > CellInfoVector
;
777 typedef std::shared_ptr
< CellInfo
> CellInfoMatrix
[nPreviewColumns
* nPreviewRows
];
781 struct TableStyleSettings
785 bool mbUseFirstColumn
;
786 bool mbUseLastColumn
;
787 bool mbUseRowBanding
;
788 bool mbUseColumnBanding
;
791 : mbUseFirstRow(true)
792 , mbUseLastRow(false)
793 , mbUseFirstColumn(false)
794 , mbUseLastColumn(false)
795 , mbUseRowBanding(true)
796 , mbUseColumnBanding(false) {}
801 static void FillCellInfoVector( const Reference
< XIndexAccess
>& xTableStyle
, CellInfoVector
& rVector
)
803 DBG_ASSERT( xTableStyle
.is() && (xTableStyle
->getCount() == sdr::table::style_count
), "sd::FillCellInfoVector(), invalid table style!" );
804 if( !xTableStyle
.is() )
809 rVector
.resize( sdr::table::style_count
);
811 for( sal_Int32 nStyle
= 0; nStyle
< sdr::table::style_count
; ++nStyle
)
813 Reference
< XStyle
> xStyle( xTableStyle
->getByIndex( nStyle
), UNO_QUERY
);
815 rVector
[nStyle
] = std::make_shared
<CellInfo
>( xStyle
);
820 TOOLS_WARN_EXCEPTION( "sd", "sd::FillCellInfoVector()");
824 static void FillCellInfoMatrix( const CellInfoVector
& rStyle
, const TableStyleSettings
& rSettings
, CellInfoMatrix
& rMatrix
)
826 for( sal_Int32 nRow
= 0; nRow
< nPreviewColumns
; ++nRow
)
828 const bool bFirstRow
= rSettings
.mbUseFirstRow
&& (nRow
== 0);
829 const bool bLastRow
= rSettings
.mbUseLastRow
&& (nRow
== nPreviewColumns
- 1);
831 for( sal_Int32 nCol
= 0; nCol
< nPreviewColumns
; ++nCol
)
833 std::shared_ptr
< CellInfo
> xCellInfo
;
835 // first and last row win first, if used and available
838 xCellInfo
= rStyle
[sdr::table::first_row_style
];
842 xCellInfo
= rStyle
[sdr::table::last_row_style
];
847 // next come first and last column, if used and available
848 if( rSettings
.mbUseFirstColumn
&& (nCol
== 0) )
850 xCellInfo
= rStyle
[sdr::table::first_column_style
];
852 else if( rSettings
.mbUseLastColumn
&& (nCol
== nPreviewColumns
-1) )
854 xCellInfo
= rStyle
[sdr::table::last_column_style
];
860 if( rSettings
.mbUseRowBanding
)
862 if( (nRow
& 1) == 0 )
864 xCellInfo
= rStyle
[sdr::table::even_rows_style
];
868 xCellInfo
= rStyle
[sdr::table::odd_rows_style
];
875 if( rSettings
.mbUseColumnBanding
)
877 if( (nCol
& 1) == 0 )
879 xCellInfo
= rStyle
[sdr::table::even_columns_style
];
883 xCellInfo
= rStyle
[sdr::table::odd_columns_style
];
890 // use default cell style if non found yet
891 xCellInfo
= rStyle
[sdr::table::body_style
];
894 rMatrix
[(nCol
* nPreviewColumns
) + nRow
] = xCellInfo
;
899 static BitmapEx
CreateDesignPreview( const Reference
< XIndexAccess
>& xTableStyle
, const TableStyleSettings
& rSettings
, bool bIsPageDark
)
901 CellInfoVector
aCellInfoVector(sdr::table::style_count
);
902 FillCellInfoVector( xTableStyle
, aCellInfoVector
);
904 CellInfoMatrix aMatrix
;
905 FillCellInfoMatrix( aCellInfoVector
, rSettings
, aMatrix
);
907 // bbbbbbbbbbbb w = 12 pixel
908 // bccccccccccb h = 7 pixel
909 // bccccccccccb b = border color
910 // bcttttttttcb c = cell color
911 // bccccccccccb t = text color
915 ScopedVclPtr
<VirtualDevice
> pVirDev(VclPtr
<VirtualDevice
>::Create());
916 Size
aBmpSize(nBitmapWidth
, nBitmapHeight
);
917 pVirDev
->SetOutputSizePixel(aBmpSize
);
919 pVirDev
->SetBackground( bIsPageDark
? COL_BLACK
: COL_WHITE
);
922 // first draw cell background and text line previews
925 for( nRow
= 0; nRow
< nPreviewRows
; ++nRow
, nY
+= nCellHeight
-1 )
928 for( sal_Int32 nCol
= 0; nCol
< nPreviewColumns
; ++nCol
, nX
+= nCellWidth
-1 )
930 std::shared_ptr
< CellInfo
> xCellInfo(aMatrix
[(nCol
* nPreviewColumns
) + nRow
]);
932 Color
aTextColor( COL_AUTO
);
935 // fill cell background
936 const ::tools::Rectangle
aRect( nX
, nY
, nX
+ nCellWidth
- 1, nY
+ nCellHeight
- 1 );
938 if( xCellInfo
->maCellColor
!= COL_TRANSPARENT
)
940 pVirDev
->SetFillColor( xCellInfo
->maCellColor
);
941 pVirDev
->DrawRect( aRect
);
944 aTextColor
= xCellInfo
->maTextColor
;
947 // draw text preview line
948 if( aTextColor
== COL_AUTO
)
949 aTextColor
= bIsPageDark
? COL_WHITE
: COL_BLACK
;
950 pVirDev
->SetLineColor( aTextColor
);
951 const Point
aPnt1( nX
+ 2, nY
+ ((nCellHeight
- 1 ) >> 1) );
952 const Point
aPnt2( nX
+ nCellWidth
- 3, aPnt1
.Y() );
953 pVirDev
->DrawLine( aPnt1
, aPnt2
);
957 // second draw border lines
959 for( nRow
= 0; nRow
< nPreviewRows
; ++nRow
, nY
+= nCellHeight
-1 )
962 for( sal_Int32 nCol
= 0; nCol
< nPreviewColumns
; ++nCol
, nX
+= nCellWidth
-1 )
964 std::shared_ptr
< CellInfo
> xCellInfo(aMatrix
[(nCol
* nPreviewColumns
) + nRow
]);
968 const Point
aPntTL( nX
, nY
);
969 const Point
aPntTR( nX
+ nCellWidth
- 1, nY
);
970 const Point
aPntBL( nX
, nY
+ nCellHeight
- 1 );
971 const Point
aPntBR( nX
+ nCellWidth
- 1, nY
+ nCellHeight
- 1 );
973 sal_Int32 border_diffs
[8] = { 0,-1, 0,1, -1,0, 1,0 };
974 sal_Int32
* pDiff
= &border_diffs
[0];
977 for( SvxBoxItemLine nLine
: o3tl::enumrange
<SvxBoxItemLine
>() )
979 const ::editeng::SvxBorderLine
* pBorderLine
= xCellInfo
->maBorder
->GetLine(nLine
);
980 if( !pBorderLine
|| ((pBorderLine
->GetOutWidth() == 0) && (pBorderLine
->GetInWidth()==0)) )
983 sal_Int32 nBorderCol
= nCol
+ *pDiff
++;
984 sal_Int32 nBorderRow
= nRow
+ *pDiff
++;
985 if( (nBorderCol
>= 0) && (nBorderCol
< nPreviewColumns
) && (nBorderRow
>= 0) && (nBorderRow
< nPreviewRows
) )
988 std::shared_ptr
< CellInfo
> xBorderInfo(aMatrix
[(nBorderCol
* nPreviewColumns
) + nBorderRow
]);
991 const ::editeng::SvxBorderLine
* pBorderLine2
= xBorderInfo
->maBorder
->GetLine(static_cast<SvxBoxItemLine
>(static_cast<int>(nLine
)^1));
992 if( pBorderLine2
&& pBorderLine2
->HasPriority(*pBorderLine
) )
993 continue; // other border line wins
997 pVirDev
->SetLineColor( pBorderLine
->GetColor() );
1000 case SvxBoxItemLine::TOP
: pVirDev
->DrawLine( aPntTL
, aPntTR
); break;
1001 case SvxBoxItemLine::BOTTOM
: pVirDev
->DrawLine( aPntBL
, aPntBR
); break;
1002 case SvxBoxItemLine::LEFT
: pVirDev
->DrawLine( aPntTL
, aPntBL
); break;
1003 case SvxBoxItemLine::RIGHT
: pVirDev
->DrawLine( aPntTR
, aPntBR
); break;
1010 return pVirDev
->GetBitmapEx(Point(0,0), aBmpSize
);
1013 void TableDesignWidget::FillDesignPreviewControl()
1015 sal_uInt16 nSelectedItem
= m_xValueSet
->GetSelectedItemId();
1016 m_xValueSet
->Clear();
1019 TableStyleSettings aSettings
;
1020 if( mxSelectedTable
.is() )
1022 aSettings
.mbUseFirstRow
= m_aCheckBoxes
[CB_HEADER_ROW
]->get_active();
1023 aSettings
.mbUseLastRow
= m_aCheckBoxes
[CB_TOTAL_ROW
]->get_active();
1024 aSettings
.mbUseRowBanding
= m_aCheckBoxes
[CB_BANDED_ROWS
]->get_active();
1025 aSettings
.mbUseFirstColumn
= m_aCheckBoxes
[CB_FIRST_COLUMN
]->get_active();
1026 aSettings
.mbUseLastColumn
= m_aCheckBoxes
[CB_LAST_COLUMN
]->get_active();
1027 aSettings
.mbUseColumnBanding
= m_aCheckBoxes
[CB_BANDED_COLUMNS
]->get_active();
1030 bool bIsPageDark
= false;
1033 Reference
< XPropertySet
> xPageSet( mxView
->getCurrentPage(), UNO_QUERY
);
1036 xPageSet
->getPropertyValue("IsBackgroundDark") >>= bIsPageDark
;
1040 sal_Int32 nCount
= mxTableFamily
->getCount();
1041 for( sal_Int32 nIndex
= 0; nIndex
< nCount
; ++nIndex
) try
1043 Reference
< XIndexAccess
> xTableStyle( mxTableFamily
->getByIndex( nIndex
), UNO_QUERY
);
1044 if( xTableStyle
.is() )
1045 m_xValueSet
->InsertItem( sal::static_int_cast
<sal_uInt16
>( nIndex
+ 1 ), Image( CreateDesignPreview( xTableStyle
, aSettings
, bIsPageDark
) ) );
1049 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::FillDesignPreviewControl()");
1051 m_xValueSet
->InsertItem(++nCount
, Image(StockImage::Yes
, BMP_INSERT_TABLESTYLE
), SdResId(STR_INSERT_TABLESTYLE
));
1053 sal_Int32 nCols
= 3;
1054 sal_Int32 nRows
= std::min
<sal_Int32
>((nCount
+2)/3, TableValueSet::getMaxRowCount());
1055 m_xValueSet
->SetColCount(nCols
);
1056 m_xValueSet
->SetLineCount(nRows
);
1057 WinBits nStyle
= m_xValueSet
->GetStyle() & ~WB_VSCROLL
;
1058 m_xValueSet
->SetStyle(nStyle
);
1060 m_xValueSet
->SetOptimalSize();
1061 weld::DrawingArea
* pDrawingArea
= m_xValueSet
->GetDrawingArea();
1062 Size aSize
= pDrawingArea
->get_preferred_size();
1063 aSize
.AdjustWidth(10 * nCols
);
1064 aSize
.AdjustHeight(10 * nRows
);
1065 pDrawingArea
->set_size_request(aSize
.Width(), aSize
.Height());
1067 m_xValueSet
->Resize();
1071 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::FillDesignPreviewControl()");
1073 m_xValueSet
->SelectItem(nSelectedItem
);
1078 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */