android: Update app-specific/MIME type icons
[LibreOffice.git] / sd / source / ui / table / TableDesignPane.cxx
blobc5f20588397b9fe176c61abe72aca369843ae635
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>
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;
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("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);
125 addListener();
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();
139 updateControls();
142 TableDesignWidget::~TableDesignWidget()
144 removeListener();
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);
155 catch (Exception&)
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())
168 return;
170 if (nClickedItemId)
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());
180 else
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);
188 catch (Exception&)
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")
199 InsertStyle();
200 else if (aCommand == "clone")
201 CloneStyle();
202 else if (aCommand == "delete")
203 DeleteStyle();
204 else if (aCommand == "reset")
205 ResetStyle();
206 else if (!aCommand.isEmpty())
207 EditStyle(aCommand);
210 namespace
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++);
221 return aName;
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));
245 updateControls();
246 selectStyle(aName);
247 setDocumentModified();
249 catch (Exception&)
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));
286 else
287 xDestTableStyle->replaceByName(name, Any(xSrcCellStyle));
290 updateControls();
291 selectStyle(aName);
292 setDocumentModified();
294 catch (Exception&)
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);
318 updateControls();
319 setDocumentModified();
321 catch (Exception&)
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)
339 return;
341 endTextEditForStyle(xTableStyle);
344 Reference<XNameContainer>(mxTableFamily, UNO_QUERY_THROW)->removeByName(xTableStyle->getName());
346 updateControls();
347 setDocumentModified();
349 catch (Exception&)
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();
364 if (!bUserDefined)
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);
390 if (!bUserDefined)
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));
404 updateControls();
405 setDocumentModified();
408 catch (Exception&)
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();
418 else
419 return nullptr;
422 static SfxDispatcher* getDispatcher( ViewShellBase const & rBase )
424 if( rBase.GetMainViewShell() && rBase.GetMainViewShell()->GetViewFrame() )
425 return rBase.GetMainViewShell()->GetViewFrame()->GetDispatcher();
426 else
427 return nullptr;
430 IMPL_LINK_NOARG(TableDesignWidget, implValueSetHdl, ValueSet*, void)
432 ApplyStyle();
435 void TableDesignWidget::ApplyStyle()
439 OUString sStyleName;
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())
449 InsertStyle();
450 return;
453 if( sStyleName.isEmpty() )
454 return;
456 SdrView* pView = mrBase.GetDrawView();
457 if( mxSelectedTable.is() )
459 if( pView )
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 );
472 if( pBindings )
474 pBindings->Invalidate( SID_UNDO );
475 pBindings->Invalidate( SID_REDO );
478 setDocumentModified();
480 else
482 SfxDispatcher* pDispatcher = getDispatcher( mrBase );
483 SfxStringItem aArg( SID_TABLE_STYLE, sStyleName );
484 pDispatcher->ExecuteList(SID_INSERT_TABLE, SfxCallMode::ASYNCHRON,
485 { &aArg });
488 catch( Exception& )
490 TOOLS_WARN_EXCEPTION( "sd", "TableDesignWidget::implValueSetHdl()");
494 IMPL_LINK_NOARG(TableDesignWidget, implCheckBoxHdl, weld::Toggleable&, void)
496 ApplyOptions();
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() )
509 return;
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();
519 if( !pView )
520 return;
522 const rtl::Reference< sdr::SelectionController >& xController( pView->getSelectionController() );
523 if( xController.is() )
525 xController->Execute( aReq );
527 SfxBindings* pBindings = getBindings( mrBase );
528 if( pBindings )
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];
551 else
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 );
564 catch( Exception& )
566 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::onSelectionChanged()" );
569 if( mxSelectedTable != xNewSelection )
571 mxSelectedTable = xNewSelection;
572 updateControls();
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);
582 return true;
585 void TableValueSet::Resize()
587 ValueSet::Resize();
588 // Calculate the number of rows and columns.
589 if( GetItemCount() <= 0 )
590 return;
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)
600 nColumnCount = 1;
602 int nRowCount = (GetItemCount() + nColumnCount - 1) / nColumnCount;
603 if (nRowCount < 1)
604 nRowCount = 1;
606 int nVisibleRowCount = std::min(nRowCount, getMaxRowCount());
608 SetColCount (static_cast<sal_uInt16>(nColumnCount));
609 SetLineCount (static_cast<sal_uInt16>(nVisibleRowCount));
611 if( !m_bModal )
613 WinBits nStyle = GetStyle() & ~WB_VSCROLL;
614 if( nRowCount > nVisibleRowCount )
616 nStyle |= WB_VSCROLL;
618 SetStyle( nStyle );
622 TableValueSet::TableValueSet(std::unique_ptr<weld::ScrolledWindow> pScrolledWindow)
623 : ValueSet(std::move(pScrolledWindow))
624 , m_bModal(false)
628 void TableValueSet::StyleUpdated()
630 updateSettings();
633 void TableValueSet::updateSettings()
635 if( !m_bModal )
637 Color aColor = Application::GetSettings().GetStyleSettings().GetWindowColor();
638 SetColor(aColor);
639 SetExtraSpacing(8);
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];
652 if( bHasTable ) try
654 mxSelectedTable->getPropertyValue( OUString(gPropNames[i]) ) >>= bUse;
656 catch( Exception& )
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 );
671 if( xNamed.is() )
672 selectStyle(xNamed->getName());
676 void TableDesignWidget::selectStyle(std::u16string_view rStyle)
678 Reference< XNameAccess > xNames( mxTableFamily, UNO_QUERY );
679 if( xNames.is() )
681 Sequence< OUString > aNames( xNames->getElementNames() );
682 sal_Int32 nIndex = comphelper::findValue(aNames, rStyle);
683 if (nIndex != -1)
684 m_xValueSet->SelectItem(static_cast<sal_uInt16>(nIndex) + 1);
688 void TableDesignWidget::endTextEditForStyle(const Reference<XInterface>& rStyle)
690 if (!mxSelectedTable)
691 return;
693 Reference<XInterface> xTableStyle(mxSelectedTable->getPropertyValue("TableTemplate"), UNO_QUERY);
694 if (xTableStyle != rStyle)
695 return;
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();
721 break;
723 case EventMultiplexerEventId::MainViewRemoved:
724 mxView.clear();
725 onSelectionChanged();
726 break;
728 case EventMultiplexerEventId::MainViewAdded:
729 mxView.set( mrBase.GetController(), UNO_QUERY );
730 onSelectionChanged();
731 break;
733 default: break;
737 namespace {
739 struct CellInfo
741 Color maCellColor;
742 Color maTextColor;
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 );
754 if( !pStyleSheet )
755 return;
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);
765 if( pTextColor )
766 maTextColor = pTextColor->GetValue();
767 else
768 maTextColor = COL_TRANSPARENT;
770 // get border
771 const SvxBoxItem* pBoxItem = rSet.GetItem( SDRATTR_TABLE_BORDER );
772 if( pBoxItem )
773 maBorder.reset(pBoxItem->Clone());
776 typedef std::vector< std::shared_ptr< CellInfo > > CellInfoVector;
777 typedef std::shared_ptr< CellInfo > CellInfoMatrix[nPreviewColumns * nPreviewRows];
779 namespace {
781 struct TableStyleSettings
783 bool mbUseFirstRow;
784 bool mbUseLastRow;
785 bool mbUseFirstColumn;
786 bool mbUseLastColumn;
787 bool mbUseRowBanding;
788 bool mbUseColumnBanding;
790 TableStyleSettings()
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() )
805 return;
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 );
814 if( xStyle.is() )
815 rVector[nStyle] = std::make_shared<CellInfo>( xStyle );
818 catch(Exception&)
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
836 if( bFirstRow )
838 xCellInfo = rStyle[sdr::table::first_row_style];
840 else if( bLastRow )
842 xCellInfo = rStyle[sdr::table::last_row_style];
845 if( !xCellInfo )
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];
858 if( !xCellInfo )
860 if( rSettings.mbUseRowBanding )
862 if( (nRow & 1) == 0 )
864 xCellInfo = rStyle[sdr::table::even_rows_style];
866 else
868 xCellInfo = rStyle[sdr::table::odd_rows_style];
873 if( !xCellInfo )
875 if( rSettings.mbUseColumnBanding )
877 if( (nCol & 1) == 0 )
879 xCellInfo = rStyle[sdr::table::even_columns_style];
881 else
883 xCellInfo = rStyle[sdr::table::odd_columns_style];
888 if( !xCellInfo )
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
912 // bccccccccccb
913 // bbbbbbbbbbbb
915 ScopedVclPtr<VirtualDevice> pVirDev(VclPtr<VirtualDevice>::Create());
916 Size aBmpSize(nBitmapWidth, nBitmapHeight);
917 pVirDev->SetOutputSizePixel(aBmpSize);
919 pVirDev->SetBackground( bIsPageDark ? COL_BLACK : COL_WHITE );
920 pVirDev->Erase();
922 // first draw cell background and text line previews
923 sal_Int32 nY = 0;
924 sal_Int32 nRow;
925 for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
927 sal_Int32 nX = 0;
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 );
933 if( xCellInfo )
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
958 nY = 0;
959 for( nRow = 0; nRow < nPreviewRows; ++nRow, nY += nCellHeight-1 )
961 sal_Int32 nX = 0;
962 for( sal_Int32 nCol = 0; nCol < nPreviewColumns; ++nCol, nX += nCellWidth-1 )
964 std::shared_ptr< CellInfo > xCellInfo(aMatrix[(nCol * nPreviewColumns) + nRow]);
966 if( xCellInfo )
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];
976 // draw top border
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)) )
981 continue;
983 sal_Int32 nBorderCol = nCol + *pDiff++;
984 sal_Int32 nBorderRow = nRow + *pDiff++;
985 if( (nBorderCol >= 0) && (nBorderCol < nPreviewColumns) && (nBorderRow >= 0) && (nBorderRow < nPreviewRows) )
987 // check border
988 std::shared_ptr< CellInfo > xBorderInfo(aMatrix[(nBorderCol * nPreviewColumns) + nBorderRow]);
989 if( xBorderInfo )
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() );
998 switch( nLine )
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;
1031 if( mxView.is() )
1033 Reference< XPropertySet > xPageSet( mxView->getCurrentPage(), UNO_QUERY );
1034 if( xPageSet.is() )
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 ) ) );
1047 catch( Exception& )
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();
1069 catch( Exception& )
1071 TOOLS_WARN_EXCEPTION( "sd", "sd::TableDesignWidget::FillDesignPreviewControl()");
1073 m_xValueSet->SelectItem(nSelectedItem);
1078 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */