tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / table / TableDesignPane.cxx
blob3ed644ce75f3ea2ba377946c2f5d4eaeb253ec74
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
69 #include <DrawController.hxx>
71 using namespace ::com::sun::star;
72 using namespace ::com::sun::star::uno;
73 using namespace ::com::sun::star::drawing;
74 using namespace ::com::sun::star::container;
75 using namespace ::com::sun::star::beans;
76 using namespace ::com::sun::star::view;
77 using namespace ::com::sun::star::style;
78 using namespace ::com::sun::star::frame;
79 using namespace ::com::sun::star::lang;
81 namespace sd {
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] =
92 u"UseFirstRowStyle",
93 u"UseLastRowStyle",
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)
103 : mrBase(rBase)
104 , m_xMenu(rBuilder.weld_menu(u"menu"_ustr))
105 , m_xValueSet(new TableValueSet(rBuilder.weld_scrolled_window(u"previewswin"_ustr, true)))
106 , m_xValueSetWin(new weld::CustomWeld(rBuilder, u"previews"_ustr, *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 = mrBase.GetDrawController();
125 addListener();
127 DrawController* pController = mrBase.GetDrawController();
128 if (pController)
130 Reference< XStyleFamiliesSupplier > xFamiliesSupp( pController->getModel(), UNO_QUERY_THROW );
131 Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
132 mxTableFamily.set( xFamilies->getByName( u"table"_ustr ), UNO_QUERY_THROW );
133 mxCellFamily.set( xFamilies->getByName( u"cell"_ustr ), UNO_QUERY_THROW );
136 catch (const Exception&)
138 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPane::CustomAnimationPane()" );
141 onSelectionChanged();
142 updateControls();
145 TableDesignWidget::~TableDesignWidget()
147 removeListener();
150 void TableDesignWidget::setDocumentModified()
154 Reference<XController> xController(mrBase.GetController(), UNO_SET_THROW);
155 Reference<util::XModifiable> xModifiable(xController->getModel(), UNO_QUERY_THROW);
156 xModifiable->setModified(true);
158 catch (Exception&)
160 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::setDocumentModified()");
164 IMPL_LINK(TableDesignWidget, implContextMenuHandler, const Point*, pPoint, void)
166 auto nClickedItemId = pPoint ? m_xValueSet->GetItemId(*pPoint) : m_xValueSet->GetSelectedItemId();
170 if (nClickedItemId > mxTableFamily->getCount())
171 return;
173 if (nClickedItemId)
175 Reference<XStyle> xStyle(mxTableFamily->getByIndex(nClickedItemId - 1), UNO_QUERY_THROW);
177 m_xMenu->set_visible(u"clone"_ustr, true);
178 m_xMenu->set_visible(u"format"_ustr, true);
179 m_xMenu->set_visible(u"delete"_ustr, xStyle->isUserDefined());
180 m_xMenu->set_visible(u"reset"_ustr, !xStyle->isUserDefined());
181 m_xMenu->set_sensitive(u"reset"_ustr, Reference<util::XModifiable>(xStyle, UNO_QUERY_THROW)->isModified());
183 else
185 m_xMenu->set_visible(u"clone"_ustr, false);
186 m_xMenu->set_visible(u"format"_ustr, false);
187 m_xMenu->set_visible(u"delete"_ustr, false);
188 m_xMenu->set_visible(u"reset"_ustr, false);
191 catch (Exception&)
193 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::implContextMenuHandler()");
196 m_xValueSet->SelectItem(nClickedItemId);
198 Point aPosition = pPoint ? *pPoint : m_xValueSet->GetItemRect(nClickedItemId).Center();
199 OUString aCommand = m_xMenu->popup_at_rect(m_xValueSet->GetDrawingArea(), ::tools::Rectangle(aPosition, Size(1,1)));
201 if (aCommand == "new")
202 InsertStyle();
203 else if (aCommand == "clone")
204 CloneStyle();
205 else if (aCommand == "delete")
206 DeleteStyle();
207 else if (aCommand == "reset")
208 ResetStyle();
209 else if (!aCommand.isEmpty())
210 EditStyle(aCommand);
213 namespace
215 OUString getNewStyleName(const Reference<XNameContainer>& rFamily, std::u16string_view rBaseName)
217 OUString aName(rBaseName);
218 sal_Int32 nIndex = 1;
219 while(rFamily->hasByName(aName))
221 aName = rBaseName + OUString::number(nIndex++);
224 return aName;
228 void TableDesignWidget::InsertStyle()
232 Reference<XSingleServiceFactory> xFactory(mxTableFamily, UNO_QUERY_THROW);
233 Reference<XNameContainer> xTableFamily(mxTableFamily, UNO_QUERY_THROW);
234 Reference<XNameReplace> xTableStyle(xFactory->createInstance(), UNO_QUERY_THROW);
235 const OUString aName(getNewStyleName(xTableFamily, aTableStyleBaseName));
236 xTableFamily->insertByName(aName, Any(xTableStyle));
238 Reference<XStyle> xCellStyle(mxCellFamily->getByName(u"default"_ustr), UNO_QUERY_THROW);
240 xTableStyle->replaceByName(u"body"_ustr, Any(xCellStyle));
241 xTableStyle->replaceByName(u"odd-rows"_ustr , Any(xCellStyle));
242 xTableStyle->replaceByName(u"odd-columns"_ustr , Any(xCellStyle));
243 xTableStyle->replaceByName(u"first-row"_ustr , Any(xCellStyle));
244 xTableStyle->replaceByName(u"first-column"_ustr , Any(xCellStyle));
245 xTableStyle->replaceByName(u"last-row"_ustr , Any(xCellStyle));
246 xTableStyle->replaceByName(u"last-column"_ustr , Any(xCellStyle));
248 updateControls();
249 selectStyle(aName);
250 setDocumentModified();
252 catch (Exception&)
254 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::InsertStyle()");
258 void TableDesignWidget::CloneStyle()
262 Reference<XNameAccess> xSrcTableStyle(mxTableFamily->getByIndex(m_xValueSet->GetSelectedItemId() - 1), UNO_QUERY_THROW);
264 Reference<XSingleServiceFactory> xFactory(mxTableFamily, UNO_QUERY_THROW);
265 Reference<XNameContainer> xTableFamily(mxTableFamily, UNO_QUERY_THROW);
266 Reference<XNameReplace> xDestTableStyle(xFactory->createInstance(), UNO_QUERY_THROW);
267 const OUString aName(getNewStyleName(xTableFamily, aTableStyleBaseName));
268 xTableFamily->insertByName(aName, Any(xDestTableStyle));
270 auto aNames(xSrcTableStyle->getElementNames());
271 for (const auto& name : aNames)
273 Reference<XStyle> xSrcCellStyle(xSrcTableStyle->getByName(name), UNO_QUERY);
274 if (xSrcCellStyle && xSrcCellStyle->isUserDefined())
276 Reference<XSingleServiceFactory> xCellFactory(mxCellFamily, UNO_QUERY_THROW);
277 Reference<XStyle> xDestCellStyle(xCellFactory->createInstance(), UNO_QUERY_THROW);
278 xDestCellStyle->setParentStyle(xSrcCellStyle->getParentStyle());
279 const OUString aStyleName(getNewStyleName(mxCellFamily, Concat2View(aName + "-" + name)));
280 mxCellFamily->insertByName(aStyleName, Any(xDestCellStyle));
282 rtl::Reference xSrcStyleSheet = static_cast<SdStyleSheet*>(xSrcCellStyle.get());
283 rtl::Reference xDestStyleSheet = static_cast<SdStyleSheet*>(xDestCellStyle.get());
285 xDestStyleSheet->GetItemSet().Put(xSrcStyleSheet->GetItemSet());
287 xDestTableStyle->replaceByName(name, Any(xDestCellStyle));
289 else
290 xDestTableStyle->replaceByName(name, Any(xSrcCellStyle));
293 updateControls();
294 selectStyle(aName);
295 setDocumentModified();
297 catch (Exception&)
299 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::CloneStyle()");
303 void TableDesignWidget::ResetStyle()
307 Reference<XIndexReplace> xTableStyle(mxTableFamily->getByIndex(m_xValueSet->GetSelectedItemId() - 1), UNO_QUERY_THROW);
309 for (sal_Int32 i = 0; i < xTableStyle->getCount(); ++i)
311 Reference<XStyle> xCellStyle(xTableStyle->getByIndex(i), UNO_QUERY);
312 while (xCellStyle && xCellStyle->isUserDefined() && !xCellStyle->getParentStyle().isEmpty())
313 xCellStyle.set(mxCellFamily->getByName(xCellStyle->getParentStyle()), UNO_QUERY);
315 xTableStyle->replaceByIndex(i, Any(xCellStyle));
318 endTextEditForStyle(xTableStyle);
319 Reference<util::XModifiable>(xTableStyle, UNO_QUERY_THROW)->setModified(false);
321 updateControls();
322 setDocumentModified();
324 catch (Exception&)
326 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::ResetStyle()");
330 void TableDesignWidget::DeleteStyle()
334 Reference<XStyle> xTableStyle(mxTableFamily->getByIndex(m_xValueSet->GetSelectedItemId() - 1), UNO_QUERY_THROW);
336 if (xTableStyle->isInUse())
338 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
339 m_xValueSet->GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo, SdResId(STR_REMOVE_TABLESTYLE)));
341 if (xBox->run() != RET_YES)
342 return;
344 endTextEditForStyle(xTableStyle);
347 Reference<XNameContainer>(mxTableFamily, UNO_QUERY_THROW)->removeByName(xTableStyle->getName());
349 updateControls();
350 setDocumentModified();
352 catch (Exception&)
354 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::DeleteStyle()");
358 void TableDesignWidget::EditStyle(const OUString& rCommand)
362 Reference<XNameReplace> xTableStyle(mxTableFamily->getByIndex(m_xValueSet->GetSelectedItemId() - 1), UNO_QUERY_THROW);
363 Reference<XStyle> xCellStyle(xTableStyle->getByName(rCommand), UNO_QUERY_THROW);
364 rtl::Reference xStyleSheet = static_cast<SdStyleSheet*>(xCellStyle.get());
366 bool bUserDefined = xStyleSheet->IsEditable();
367 if (!bUserDefined)
369 Reference<XSingleServiceFactory> xFactory(mxCellFamily, UNO_QUERY_THROW);
370 xCellStyle.set(xFactory->createInstance(), UNO_QUERY_THROW);
371 xCellStyle->setParentStyle(xStyleSheet->getName());
372 xStyleSheet = static_cast<SdStyleSheet*>(xCellStyle.get());
375 SfxItemSet aNewAttr(xStyleSheet->GetItemSet());
377 // merge drawing layer text distance items into SvxBoxItem used by the dialog
378 SvxBoxItem aBoxItem(sdr::table::SvxTableController::TextDistancesToSvxBoxItem(aNewAttr));
379 aNewAttr.Put(aBoxItem);
381 // inner borders do not apply to a cell style
382 SvxBoxInfoItem aBoxInfoItem(aNewAttr.Get(SDRATTR_TABLE_BORDER_INNER));
383 aBoxInfoItem.SetTable(false);
384 aNewAttr.Put(aBoxInfoItem);
386 SdrView* pDrawView = mrBase.GetDrawView();
387 if (!pDrawView)
388 return;
390 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
391 ScopedVclPtr<SfxAbstractTabDialog> pDlg(pFact ? pFact->CreateSvxFormatCellsDialog(
392 mrBase.GetFrameWeld(), aNewAttr, pDrawView->GetModel(), true) : nullptr);
393 if (pDlg && pDlg->Execute() == RET_OK)
395 endTextEditForStyle(xTableStyle);
397 if (!bUserDefined)
399 Reference<XNamed> xNamed(xTableStyle, UNO_QUERY_THROW);
400 const OUString aStyleName(getNewStyleName(mxCellFamily, Concat2View(xNamed->getName() + "-" + rCommand)));
401 mxCellFamily->insertByName(aStyleName, Any(xCellStyle));
402 xTableStyle->replaceByName(rCommand, Any(xCellStyle));
405 SfxItemSet aNewSet(*pDlg->GetOutputItemSet());
406 sdr::table::SvxTableController::SvxBoxItemToTextDistances(aBoxItem, aNewSet);
407 sdr::properties::CleanupFillProperties(aNewSet);
408 xStyleSheet->GetItemSet().Put(aNewSet);
409 xStyleSheet->Broadcast(SfxHint(SfxHintId::DataChanged));
411 updateControls();
412 setDocumentModified();
415 catch (Exception&)
417 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::EditStyle()");
421 static SfxBindings* getBindings( ViewShellBase const & rBase )
423 auto pViewShell = rBase.GetMainViewShell().get();
424 if( !pViewShell )
425 return nullptr;
426 auto pViewFrame = pViewShell->GetViewFrame();
427 if( !pViewFrame )
428 return nullptr;
429 return &pViewFrame->GetBindings();
432 static SfxDispatcher* getDispatcher( ViewShellBase const & rBase )
434 auto pViewShell = rBase.GetMainViewShell().get();
435 if( !pViewShell )
436 return nullptr;
437 auto pViewFrame = pViewShell->GetViewFrame();
438 if( !pViewFrame )
439 return nullptr;
440 return pViewFrame->GetDispatcher();
443 IMPL_LINK_NOARG(TableDesignWidget, implValueSetHdl, ValueSet*, void)
445 ApplyStyle();
448 void TableDesignWidget::ApplyStyle()
452 OUString sStyleName;
453 sal_Int32 nIndex = static_cast< sal_Int32 >( m_xValueSet->GetSelectedItemId() ) - 1;
455 if( (nIndex >= 0) && (nIndex < mxTableFamily->getCount()) )
457 Reference< XNameAccess > xNames( mxTableFamily, UNO_QUERY_THROW );
458 sStyleName = xNames->getElementNames()[nIndex];
460 else if (nIndex == mxTableFamily->getCount())
462 InsertStyle();
463 return;
466 if( sStyleName.isEmpty() )
467 return;
469 if( mxSelectedTable.is() )
471 if (SdrView* pView = mrBase.GetDrawView())
473 if (pView->IsTextEdit())
474 pView->SdrEndTextEdit();
476 SfxRequest aReq( SID_TABLE_STYLE, SfxCallMode::SYNCHRON, SfxGetpApp()->GetPool() );
477 aReq.AppendItem( SfxStringItem( SID_TABLE_STYLE, sStyleName ) );
479 const rtl::Reference< sdr::SelectionController >& xController( pView->getSelectionController() );
480 if( xController.is() )
481 xController->Execute( aReq );
483 SfxBindings* pBindings = getBindings( mrBase );
484 if( pBindings )
486 pBindings->Invalidate( SID_UNDO );
487 pBindings->Invalidate( SID_REDO );
490 setDocumentModified();
492 else
494 SfxDispatcher* pDispatcher = getDispatcher( mrBase );
495 SfxStringItem aArg( SID_TABLE_STYLE, sStyleName );
496 pDispatcher->ExecuteList(SID_INSERT_TABLE, SfxCallMode::ASYNCHRON,
497 { &aArg });
500 catch( Exception& )
502 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::implValueSetHdl()");
506 IMPL_LINK_NOARG(TableDesignWidget, implCheckBoxHdl, weld::Toggleable&, void)
508 ApplyOptions();
509 FillDesignPreviewControl();
512 void TableDesignWidget::ApplyOptions()
514 static const sal_uInt16 gParamIds[CB_COUNT] =
516 ID_VAL_USEFIRSTROWSTYLE, ID_VAL_USELASTROWSTYLE, ID_VAL_USEBANDINGROWSTYLE,
517 ID_VAL_USEFIRSTCOLUMNSTYLE, ID_VAL_USELASTCOLUMNSTYLE, ID_VAL_USEBANDINGCOLUMNSTYLE
520 if( !mxSelectedTable.is() )
521 return;
523 SfxRequest aReq( SID_TABLE_STYLE_SETTINGS, SfxCallMode::SYNCHRON, SfxGetpApp()->GetPool() );
525 for( sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i )
527 aReq.AppendItem( SfxBoolItem( gParamIds[i], m_aCheckBoxes[i]->get_active() ) );
530 SdrView* pView = mrBase.GetDrawView();
531 if( !pView )
532 return;
534 const rtl::Reference< sdr::SelectionController >& xController( pView->getSelectionController() );
535 if( xController.is() )
537 xController->Execute( aReq );
539 SfxBindings* pBindings = getBindings( mrBase );
540 if( pBindings )
542 pBindings->Invalidate( SID_UNDO );
543 pBindings->Invalidate( SID_REDO );
546 setDocumentModified();
549 void TableDesignWidget::onSelectionChanged()
551 Reference< XPropertySet > xNewSelection;
553 if( mxView.is() ) try
555 Any aSel( mxView->getSelection() );
556 Sequence< XShape > xShapeSeq;
557 if( aSel >>= xShapeSeq )
559 if( xShapeSeq.getLength() == 1 )
560 aSel <<= xShapeSeq[0];
562 else
564 Reference< XShapes > xShapes( aSel, UNO_QUERY );
565 if( xShapes.is() && (xShapes->getCount() == 1) )
566 aSel = xShapes->getByIndex(0);
569 Reference< XShapeDescriptor > xDesc( aSel, UNO_QUERY );
570 if( xDesc.is() && ( xDesc->getShapeType() == "com.sun.star.drawing.TableShape" || xDesc->getShapeType() == "com.sun.star.presentation.TableShape" ) )
572 xNewSelection.set( xDesc, UNO_QUERY );
575 catch( Exception& )
577 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::onSelectionChanged()" );
580 if( mxSelectedTable != xNewSelection )
582 mxSelectedTable = std::move(xNewSelection);
583 updateControls();
587 bool TableValueSet::Command(const CommandEvent& rEvent)
589 if (rEvent.GetCommand() != CommandEventId::ContextMenu)
590 return ValueSet::Command(rEvent);
592 maContextMenuHandler.Call(rEvent.IsMouseEvent() ? &rEvent.GetMousePosPixel() : nullptr);
593 return true;
596 void TableValueSet::Resize()
598 ValueSet::Resize();
599 // Calculate the number of rows and columns.
600 if( GetItemCount() <= 0 )
601 return;
603 Size aValueSetSize = GetOutputSizePixel();
605 Image aImage = GetItemImage(GetItemId(0));
606 Size aItemSize = aImage.GetSizePixel();
608 aItemSize.AdjustHeight(10 );
609 int nColumnCount = (aValueSetSize.Width() - GetScrollWidth()) / aItemSize.Width();
610 if (nColumnCount < 1)
611 nColumnCount = 1;
613 int nRowCount = (GetItemCount() + nColumnCount - 1) / nColumnCount;
614 if (nRowCount < 1)
615 nRowCount = 1;
617 int nVisibleRowCount = std::min(nRowCount, getMaxRowCount());
619 SetColCount (static_cast<sal_uInt16>(nColumnCount));
620 SetLineCount (static_cast<sal_uInt16>(nVisibleRowCount));
622 if( !m_bModal )
624 WinBits nStyle = GetStyle() & ~WB_VSCROLL;
625 if( nRowCount > nVisibleRowCount )
627 nStyle |= WB_VSCROLL;
629 SetStyle( nStyle );
633 TableValueSet::TableValueSet(std::unique_ptr<weld::ScrolledWindow> pScrolledWindow)
634 : ValueSet(std::move(pScrolledWindow))
635 , m_bModal(false)
639 void TableValueSet::StyleUpdated()
641 updateSettings();
644 void TableValueSet::updateSettings()
646 if( !m_bModal )
648 Color aColor = Application::GetSettings().GetStyleSettings().GetWindowColor();
649 SetColor(aColor);
650 SetExtraSpacing(8);
654 void TableDesignWidget::updateControls()
656 static const bool gDefaults[CB_COUNT] = { true, false, true, false, false, false };
658 const bool bHasTable = mxSelectedTable.is();
660 for (sal_uInt16 i = CB_HEADER_ROW; i <= CB_BANDED_COLUMNS; ++i)
662 bool bUse = gDefaults[i];
663 if( bHasTable ) try
665 mxSelectedTable->getPropertyValue( OUString(gPropNames[i]) ) >>= bUse;
667 catch( Exception& )
669 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::updateControls()");
671 m_aCheckBoxes[i]->set_active(bUse);
672 m_aCheckBoxes[i]->set_sensitive(bHasTable);
675 FillDesignPreviewControl();
676 m_xValueSet->updateSettings();
677 m_xValueSet->Resize();
679 if( mxSelectedTable.is() )
681 Reference< XNamed > xNamed( mxSelectedTable->getPropertyValue( u"TableTemplate"_ustr ), UNO_QUERY );
682 if( xNamed.is() )
683 selectStyle(xNamed->getName());
687 void TableDesignWidget::selectStyle(std::u16string_view rStyle)
689 Reference< XNameAccess > xNames( mxTableFamily, UNO_QUERY );
690 if( xNames.is() )
692 Sequence< OUString > aNames( xNames->getElementNames() );
693 sal_Int32 nIndex = comphelper::findValue(aNames, rStyle);
694 if (nIndex != -1)
695 m_xValueSet->SelectItem(static_cast<sal_uInt16>(nIndex) + 1);
699 void TableDesignWidget::endTextEditForStyle(const Reference<XInterface>& rStyle)
701 if (!mxSelectedTable)
702 return;
704 Reference<XInterface> xTableStyle(mxSelectedTable->getPropertyValue(u"TableTemplate"_ustr), UNO_QUERY);
705 if (xTableStyle != rStyle)
706 return;
708 SdrView* pDrawView = mrBase.GetDrawView();
709 if (pDrawView && pDrawView->IsTextEdit())
710 pDrawView->SdrEndTextEdit();
713 void TableDesignWidget::addListener()
715 Link<tools::EventMultiplexerEvent&,void> aLink( LINK(this,TableDesignWidget,EventMultiplexerListener) );
716 mrBase.GetEventMultiplexer()->AddEventListener( aLink );
719 void TableDesignWidget::removeListener()
721 Link<tools::EventMultiplexerEvent&,void> aLink( LINK(this,TableDesignWidget,EventMultiplexerListener) );
722 mrBase.GetEventMultiplexer()->RemoveEventListener( aLink );
725 IMPL_LINK(TableDesignWidget,EventMultiplexerListener,
726 tools::EventMultiplexerEvent&, rEvent, void)
728 switch (rEvent.meEventId)
730 case EventMultiplexerEventId::CurrentPageChanged:
731 case EventMultiplexerEventId::EditViewSelection:
732 onSelectionChanged();
733 break;
735 case EventMultiplexerEventId::MainViewRemoved:
736 mxView.clear();
737 onSelectionChanged();
738 break;
740 case EventMultiplexerEventId::MainViewAdded:
741 mxView = mrBase.GetDrawController();
742 onSelectionChanged();
743 break;
745 default: break;
749 namespace {
751 struct CellInfo
753 Color maCellColor;
754 Color maTextColor;
755 std::shared_ptr<SvxBoxItem> maBorder;
757 explicit CellInfo( const Reference< XStyle >& xStyle );
762 CellInfo::CellInfo( const Reference< XStyle >& xStyle )
763 : maBorder(std::make_shared<SvxBoxItem>(SDRATTR_TABLE_BORDER))
765 SfxStyleSheet* pStyleSheet = SfxUnoStyleSheet::getUnoStyleSheet( xStyle );
766 if( !pStyleSheet )
767 return;
769 SfxItemSet& rSet = pStyleSheet->GetItemSet();
771 // get style fill color
772 maCellColor = GetDraftFillColor(rSet).value_or(COL_TRANSPARENT);
774 // get style text color
775 const SvxColorItem* pTextColor = rSet.GetItem(EE_CHAR_COLOR);
776 if( pTextColor )
777 maTextColor = pTextColor->GetValue();
778 else
779 maTextColor = COL_TRANSPARENT;
781 // get border
782 const SvxBoxItem* pBoxItem = rSet.GetItem( SDRATTR_TABLE_BORDER );
783 if( pBoxItem )
784 maBorder.reset(pBoxItem->Clone());
787 typedef std::vector< std::shared_ptr< CellInfo > > CellInfoVector;
788 typedef std::shared_ptr< CellInfo > CellInfoMatrix[nPreviewColumns * nPreviewRows];
790 namespace {
792 struct TableStyleSettings
794 bool mbUseFirstRow;
795 bool mbUseLastRow;
796 bool mbUseFirstColumn;
797 bool mbUseLastColumn;
798 bool mbUseRowBanding;
799 bool mbUseColumnBanding;
801 TableStyleSettings()
802 : mbUseFirstRow(true)
803 , mbUseLastRow(false)
804 , mbUseFirstColumn(false)
805 , mbUseLastColumn(false)
806 , mbUseRowBanding(true)
807 , mbUseColumnBanding(false) {}
812 static void FillCellInfoVector( const Reference< XIndexAccess >& xTableStyle, CellInfoVector& rVector )
814 DBG_ASSERT( xTableStyle.is() && (xTableStyle->getCount() == sdr::table::style_count ), "sd::FillCellInfoVector(), invalid table style!" );
815 if( !xTableStyle.is() )
816 return;
820 rVector.resize( sdr::table::style_count );
822 for( sal_Int32 nStyle = 0; nStyle < sdr::table::style_count; ++nStyle )
824 Reference< XStyle > xStyle( xTableStyle->getByIndex( nStyle ), UNO_QUERY );
825 if( xStyle.is() )
826 rVector[nStyle] = std::make_shared<CellInfo>( xStyle );
829 catch(Exception&)
831 TOOLS_WARN_EXCEPTION( "sd", "sd::FillCellInfoVector()");
835 static void FillCellInfoMatrix( const CellInfoVector& rStyle, const TableStyleSettings& rSettings, CellInfoMatrix& rMatrix )
837 for( sal_Int32 nRow = 0; nRow < nPreviewColumns; ++nRow )
839 const bool bFirstRow = rSettings.mbUseFirstRow && (nRow == 0);
840 const bool bLastRow = rSettings.mbUseLastRow && (nRow == nPreviewColumns - 1);
842 for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol )
844 std::shared_ptr< CellInfo > xCellInfo;
846 // first and last row win first, if used and available
847 if( bFirstRow )
849 xCellInfo = rStyle[sdr::table::first_row_style];
851 else if( bLastRow )
853 xCellInfo = rStyle[sdr::table::last_row_style];
856 if( !xCellInfo )
858 // next come first and last column, if used and available
859 if( rSettings.mbUseFirstColumn && (nCol == 0) )
861 xCellInfo = rStyle[sdr::table::first_column_style];
863 else if( rSettings.mbUseLastColumn && (nCol == nPreviewColumns-1) )
865 xCellInfo = rStyle[sdr::table::last_column_style];
869 if( !xCellInfo )
871 if( rSettings.mbUseRowBanding )
873 if( (nRow & 1) == 0 )
875 xCellInfo = rStyle[sdr::table::even_rows_style];
877 else
879 xCellInfo = rStyle[sdr::table::odd_rows_style];
884 if( !xCellInfo )
886 if( rSettings.mbUseColumnBanding )
888 if( (nCol & 1) == 0 )
890 xCellInfo = rStyle[sdr::table::even_columns_style];
892 else
894 xCellInfo = rStyle[sdr::table::odd_columns_style];
899 if( !xCellInfo )
901 // use default cell style if non found yet
902 xCellInfo = rStyle[sdr::table::body_style];
905 rMatrix[(nCol * nPreviewColumns) + nRow] = std::move(xCellInfo);
910 static BitmapEx CreateDesignPreview( const Reference< XIndexAccess >& xTableStyle, const TableStyleSettings& rSettings, bool bIsPageDark )
912 CellInfoVector aCellInfoVector(sdr::table::style_count);
913 FillCellInfoVector( xTableStyle, aCellInfoVector );
915 CellInfoMatrix aMatrix;
916 FillCellInfoMatrix( aCellInfoVector, rSettings, aMatrix );
918 // bbbbbbbbbbbb w = 12 pixel
919 // bccccccccccb h = 7 pixel
920 // bccccccccccb b = border color
921 // bcttttttttcb c = cell color
922 // bccccccccccb t = text color
923 // bccccccccccb
924 // bbbbbbbbbbbb
926 ScopedVclPtr<VirtualDevice> pVirDev(VclPtr<VirtualDevice>::Create());
927 Size aBmpSize(nBitmapWidth, nBitmapHeight);
928 pVirDev->SetOutputSizePixel(aBmpSize);
930 pVirDev->SetBackground( bIsPageDark ? COL_BLACK : COL_WHITE );
931 pVirDev->Erase();
933 // first draw cell background and text line previews
934 sal_Int32 nY = 0;
935 sal_Int32 nRow;
936 for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
938 sal_Int32 nX = 0;
939 for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol, nX += nCellWidth-1 )
941 std::shared_ptr< CellInfo > xCellInfo(aMatrix[(nCol * nPreviewColumns) + nRow]);
943 Color aTextColor( COL_AUTO );
944 if( xCellInfo )
946 // fill cell background
947 const ::tools::Rectangle aRect( nX, nY, nX + nCellWidth - 1, nY + nCellHeight - 1 );
949 if( xCellInfo->maCellColor != COL_TRANSPARENT )
951 pVirDev->SetFillColor( xCellInfo->maCellColor );
952 pVirDev->DrawRect( aRect );
955 aTextColor = xCellInfo->maTextColor;
958 // draw text preview line
959 if( aTextColor == COL_AUTO )
960 aTextColor = bIsPageDark ? COL_WHITE : COL_BLACK;
961 pVirDev->SetLineColor( aTextColor );
962 const Point aPnt1( nX + 2, nY + ((nCellHeight - 1 ) >> 1) );
963 const Point aPnt2( nX + nCellWidth - 3, aPnt1.Y() );
964 pVirDev->DrawLine( aPnt1, aPnt2 );
968 // second draw border lines
969 nY = 0;
970 for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
972 sal_Int32 nX = 0;
973 for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol, nX += nCellWidth-1 )
975 std::shared_ptr< CellInfo > xCellInfo(aMatrix[(nCol * nPreviewColumns) + nRow]);
977 if( xCellInfo )
979 const Point aPntTL( nX, nY );
980 const Point aPntTR( nX + nCellWidth - 1, nY );
981 const Point aPntBL( nX, nY + nCellHeight - 1 );
982 const Point aPntBR( nX + nCellWidth - 1, nY + nCellHeight - 1 );
984 sal_Int32 border_diffs[8] = { 0,-1, 0,1, -1,0, 1,0 };
985 sal_Int32* pDiff = &border_diffs[0];
987 // draw top border
988 for( SvxBoxItemLine nLine : o3tl::enumrange<SvxBoxItemLine>() )
990 const ::editeng::SvxBorderLine* pBorderLine = xCellInfo->maBorder->GetLine(nLine);
991 if( !pBorderLine || ((pBorderLine->GetOutWidth() == 0) && (pBorderLine->GetInWidth()==0)) )
992 continue;
994 sal_Int32 nBorderCol = nCol + *pDiff++;
995 sal_Int32 nBorderRow = nRow + *pDiff++;
996 if( (nBorderCol >= 0) && (nBorderCol < nPreviewColumns) && (nBorderRow >= 0) && (nBorderRow < nPreviewRows) )
998 // check border
999 std::shared_ptr< CellInfo > xBorderInfo(aMatrix[(nBorderCol * nPreviewColumns) + nBorderRow]);
1000 if( xBorderInfo )
1002 const ::editeng::SvxBorderLine* pBorderLine2 = xBorderInfo->maBorder->GetLine(static_cast<SvxBoxItemLine>(static_cast<int>(nLine)^1));
1003 if( pBorderLine2 && pBorderLine2->HasPriority(*pBorderLine) )
1004 continue; // other border line wins
1008 pVirDev->SetLineColor( pBorderLine->GetColor() );
1009 switch( nLine )
1011 case SvxBoxItemLine::TOP: pVirDev->DrawLine( aPntTL, aPntTR ); break;
1012 case SvxBoxItemLine::BOTTOM: pVirDev->DrawLine( aPntBL, aPntBR ); break;
1013 case SvxBoxItemLine::LEFT: pVirDev->DrawLine( aPntTL, aPntBL ); break;
1014 case SvxBoxItemLine::RIGHT: pVirDev->DrawLine( aPntTR, aPntBR ); break;
1021 return pVirDev->GetBitmapEx(Point(0,0), aBmpSize);
1024 void TableDesignWidget::FillDesignPreviewControl()
1026 sal_uInt16 nSelectedItem = m_xValueSet->GetSelectedItemId();
1027 m_xValueSet->Clear();
1030 TableStyleSettings aSettings;
1031 if( mxSelectedTable.is() )
1033 aSettings.mbUseFirstRow = m_aCheckBoxes[CB_HEADER_ROW]->get_active();
1034 aSettings.mbUseLastRow = m_aCheckBoxes[CB_TOTAL_ROW]->get_active();
1035 aSettings.mbUseRowBanding = m_aCheckBoxes[CB_BANDED_ROWS]->get_active();
1036 aSettings.mbUseFirstColumn = m_aCheckBoxes[CB_FIRST_COLUMN]->get_active();
1037 aSettings.mbUseLastColumn = m_aCheckBoxes[CB_LAST_COLUMN]->get_active();
1038 aSettings.mbUseColumnBanding = m_aCheckBoxes[CB_BANDED_COLUMNS]->get_active();
1041 bool bIsPageDark = false;
1042 if( mxView.is() )
1044 Reference< XPropertySet > xPageSet( mxView->getCurrentPage(), UNO_QUERY );
1045 if( xPageSet.is() )
1047 xPageSet->getPropertyValue(u"IsBackgroundDark"_ustr) >>= bIsPageDark;
1051 sal_Int32 nCount = mxTableFamily->getCount();
1052 for( sal_Int32 nIndex = 0; nIndex < nCount; ++nIndex ) try
1054 Reference< XIndexAccess > xTableStyle( mxTableFamily->getByIndex( nIndex ), UNO_QUERY );
1055 if( xTableStyle.is() )
1056 m_xValueSet->InsertItem( sal::static_int_cast<sal_uInt16>( nIndex + 1 ), Image( CreateDesignPreview( xTableStyle, aSettings, bIsPageDark ) ) );
1058 catch( Exception& )
1060 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::FillDesignPreviewControl()");
1062 m_xValueSet->InsertItem(++nCount, Image(StockImage::Yes, BMP_INSERT_TABLESTYLE), SdResId(STR_INSERT_TABLESTYLE));
1064 sal_Int32 nCols = 3;
1065 sal_Int32 nRows = std::min<sal_Int32>((nCount+2)/3, TableValueSet::getMaxRowCount());
1066 m_xValueSet->SetColCount(nCols);
1067 m_xValueSet->SetLineCount(nRows);
1068 WinBits nStyle = m_xValueSet->GetStyle() & ~WB_VSCROLL;
1069 m_xValueSet->SetStyle(nStyle);
1071 m_xValueSet->SetOptimalSize();
1072 weld::DrawingArea* pDrawingArea = m_xValueSet->GetDrawingArea();
1073 Size aSize = pDrawingArea->get_preferred_size();
1074 aSize.AdjustWidth(10 * nCols);
1075 aSize.AdjustHeight(10 * nRows);
1076 pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
1078 m_xValueSet->Resize();
1080 catch( Exception& )
1082 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::FillDesignPreviewControl()");
1084 m_xValueSet->SelectItem(nSelectedItem);
1089 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */