jsdialog: send message for weld::Menu
[LibreOffice.git] / cui / source / tabpages / tpcolor.cxx
blobef8f208205e70d87140ee1bcb97a5c4baec5998f
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 <memory>
21 #include <i18nutil/unicode.hxx>
22 #include <svtools/colrdlg.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/weld.hxx>
26 #include <strings.hrc>
27 #include <svx/xfillit0.hxx>
28 #include <svx/xflclit.hxx>
29 #include <svx/xtable.hxx>
30 #include <cuitabarea.hxx>
31 #include <svx/svxdlg.hxx>
32 #include <dialmgr.hxx>
33 #include <cuitabline.hxx>
34 #include <svx/dialmgr.hxx>
35 #include <svx/strings.hrc>
36 #include <officecfg/Office/Common.hxx>
37 #include <osl/diagnose.h>
38 #include <comphelper/dispatchcommand.hxx>
39 #include <comphelper/lok.hxx>
40 #include <comphelper/propertyvalue.hxx>
42 using namespace com::sun::star;
44 SvxColorTabPage::SvxColorTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rInAttrs)
45 : SfxTabPage(pPage, pController, u"cui/ui/colorpage.ui"_ustr, u"ColorPage"_ustr, &rInAttrs)
46 , rOutAttrs ( rInAttrs )
47 // All the horrific pointers we store and should not
48 , pnColorListState( nullptr )
49 , aXFillAttr( rInAttrs.GetPool() )
50 , rXFSet( aXFillAttr.GetItemSet() )
51 , eCM( ColorModel::RGB )
52 , m_xValSetColorList(new SvxColorValueSet(m_xBuilder->weld_scrolled_window(u"colorsetwin"_ustr, true)))
53 , m_xValSetRecentList(new SvxColorValueSet(nullptr))
54 , m_xSelectPalette(m_xBuilder->weld_combo_box(u"paletteselector"_ustr))
55 , m_xRbRGB(m_xBuilder->weld_radio_button(u"RGB"_ustr))
56 , m_xRbCMYK(m_xBuilder->weld_radio_button(u"CMYK"_ustr))
57 , m_xRGBcustom(m_xBuilder->weld_widget(u"rgbcustom"_ustr))
58 , m_xRGBpreset(m_xBuilder->weld_widget(u"rgbpreset"_ustr))
59 , m_xRpreset(m_xBuilder->weld_entry(u"R_preset"_ustr))
60 , m_xGpreset(m_xBuilder->weld_entry(u"G_preset"_ustr))
61 , m_xBpreset(m_xBuilder->weld_entry(u"B_preset"_ustr))
62 , m_xRcustom(m_xBuilder->weld_spin_button(u"R_custom"_ustr))
63 , m_xGcustom(m_xBuilder->weld_spin_button(u"G_custom"_ustr))
64 , m_xBcustom(m_xBuilder->weld_spin_button(u"B_custom"_ustr))
65 , m_xHexpreset(new weld::HexColorControl(m_xBuilder->weld_entry(u"hex_preset"_ustr)))
66 , m_xHexcustom(new weld::HexColorControl(m_xBuilder->weld_entry(u"hex_custom"_ustr)))
67 , m_xCMYKcustom(m_xBuilder->weld_widget(u"cmykcustom"_ustr))
68 , m_xCMYKpreset(m_xBuilder->weld_widget(u"cmykpreset"_ustr))
69 , m_xCpreset(m_xBuilder->weld_entry(u"C_preset"_ustr))
70 , m_xYpreset(m_xBuilder->weld_entry(u"Y_preset"_ustr))
71 , m_xMpreset(m_xBuilder->weld_entry(u"M_preset"_ustr))
72 , m_xKpreset(m_xBuilder->weld_entry(u"K_preset"_ustr))
73 , m_xCcustom(m_xBuilder->weld_metric_spin_button(u"C_custom"_ustr, FieldUnit::PERCENT))
74 , m_xYcustom(m_xBuilder->weld_metric_spin_button(u"Y_custom"_ustr, FieldUnit::PERCENT))
75 , m_xMcustom(m_xBuilder->weld_metric_spin_button(u"M_custom"_ustr, FieldUnit::PERCENT))
76 , m_xKcustom(m_xBuilder->weld_metric_spin_button(u"K_custom"_ustr, FieldUnit::PERCENT))
77 , m_xBtnAdd(m_xBuilder->weld_button(u"add"_ustr))
78 , m_xBtnDelete(m_xBuilder->weld_button(u"delete"_ustr))
79 , m_xBtnWorkOn(m_xBuilder->weld_button(u"edit"_ustr))
80 , m_xMoreColors(m_xBuilder->weld_button(u"btnMoreColors"_ustr))
81 , m_xCtlPreviewOld(new weld::CustomWeld(*m_xBuilder, u"oldpreview"_ustr, m_aCtlPreviewOld))
82 , m_xCtlPreviewNew(new weld::CustomWeld(*m_xBuilder, u"newpreview"_ustr, m_aCtlPreviewNew))
83 , m_xValSetColorListWin(new weld::CustomWeld(*m_xBuilder, u"colorset"_ustr, *m_xValSetColorList))
84 , m_xValSetRecentListWin(new weld::CustomWeld(*m_xBuilder, u"recentcolorset"_ustr, *m_xValSetRecentList))
86 Size aSize(m_xBtnWorkOn->get_approximate_digit_width() * 25,
87 m_xBtnWorkOn->get_text_height() * 10);
88 m_xValSetColorList->set_size_request(aSize.Width(), aSize.Height());
89 aSize = Size(m_xBtnWorkOn->get_approximate_digit_width() * 8,
90 m_xBtnWorkOn->get_text_height() * 3);
91 m_aCtlPreviewOld.set_size_request(aSize.Width(), aSize.Height());
92 m_aCtlPreviewNew.set_size_request(aSize.Width(), aSize.Height());
93 // this page needs ExchangeSupport
94 SetExchangeSupport();
96 // setting the output device
97 rXFSet.Put( XFillStyleItem(drawing::FillStyle_SOLID) );
98 rXFSet.Put( XFillColorItem(OUString(), COL_BLACK) );
99 m_aCtlPreviewOld.SetAttributes( aXFillAttr.GetItemSet() );
100 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
102 // set handler
103 m_xSelectPalette->connect_changed(LINK(this, SvxColorTabPage, SelectPaletteLBHdl));
104 Link<ValueSet*, void> aValSelectLink = LINK(this, SvxColorTabPage, SelectValSetHdl_Impl);
105 m_xValSetColorList->SetSelectHdl(aValSelectLink);
106 m_xValSetRecentList->SetSelectHdl(aValSelectLink);
108 Link<weld::SpinButton&,void> aSpinLink = LINK(this, SvxColorTabPage, SpinValueHdl_Impl);
109 m_xRcustom->connect_value_changed(aSpinLink);
110 m_xGcustom->connect_value_changed(aSpinLink);
111 m_xBcustom->connect_value_changed(aSpinLink);
112 Link<weld::Entry&,void> aEntryLink = LINK(this, SvxColorTabPage, ModifiedHdl_Impl);
113 m_xHexcustom->connect_changed(aEntryLink);
114 Link<weld::MetricSpinButton&,void> aMetricSpinLink = LINK(this, SvxColorTabPage, MetricSpinValueHdl_Impl);
115 m_xCcustom->connect_value_changed(aMetricSpinLink);
116 m_xYcustom->connect_value_changed(aMetricSpinLink);
117 m_xMcustom->connect_value_changed(aMetricSpinLink);
118 m_xKcustom->connect_value_changed(aMetricSpinLink);
120 Link<weld::Toggleable&,void> aLink2 = LINK( this, SvxColorTabPage, SelectColorModeHdl_Impl );
121 m_xRbRGB->connect_toggled(aLink2);
122 m_xRbCMYK->connect_toggled(aLink2);
123 SetColorModel( eCM );
124 ChangeColorModel();
126 m_xBtnAdd->connect_clicked( LINK( this, SvxColorTabPage, ClickAddHdl_Impl ) );
127 m_xBtnWorkOn->connect_clicked( LINK( this, SvxColorTabPage, ClickWorkOnHdl_Impl ) );
128 m_xBtnDelete->connect_clicked( LINK( this, SvxColorTabPage, ClickDeleteHdl_Impl ) );
129 // disable modify buttons
130 // Color palettes can't be modified
131 m_xBtnDelete->set_sensitive(false);
132 m_xBtnDelete->set_tooltip_text( CuiResId(RID_CUISTR_DELETEUSERCOLOR1) );
134 m_xMoreColors->connect_clicked(LINK(this, SvxColorTabPage, OnMoreColorsClick));
136 // disable preset color values
137 m_xRGBpreset->set_sensitive(false);
138 m_xCMYKpreset->set_sensitive(false);
140 // ValueSet
141 m_xValSetColorList->SetStyle(m_xValSetColorList->GetStyle() |
142 WB_FLATVALUESET | WB_ITEMBORDER | WB_NO_DIRECTSELECT | WB_TABSTOP);
143 m_xValSetColorList->Show();
145 m_xValSetRecentList->SetStyle(m_xValSetRecentList->GetStyle() |
146 WB_FLATVALUESET | WB_ITEMBORDER | WB_NO_DIRECTSELECT | WB_TABSTOP);
147 m_xValSetRecentList->Show();
149 maPaletteManager.ReloadRecentColorSet(*m_xValSetRecentList);
150 aSize = m_xValSetRecentList->layoutAllVisible(maPaletteManager.GetRecentColorCount());
151 m_xValSetRecentList->set_size_request(aSize.Width(), aSize.Height());
153 // it is not possible to install color palette extensions in Online or mobile apps
154 if(comphelper::LibreOfficeKit::isActive())
156 m_xMoreColors->hide();
160 SvxColorTabPage::~SvxColorTabPage()
162 m_xValSetRecentListWin.reset();
163 m_xValSetRecentList.reset();
164 m_xValSetColorListWin.reset();
165 m_xValSetColorList.reset();
168 void SvxColorTabPage::ImpColorCountChanged()
170 if (!pColorList.is())
171 return;
172 m_xValSetColorList->SetColCount(SvxColorValueSet::getColumnCount());
173 m_xValSetRecentList->SetColCount(SvxColorValueSet::getColumnCount());
176 void SvxColorTabPage::FillPaletteLB()
178 m_xSelectPalette->clear();
179 std::vector<OUString> aPaletteList = maPaletteManager.GetPaletteList();
180 for (auto const& palette : aPaletteList)
182 m_xSelectPalette->append_text(palette);
184 OUString aPaletteName( officecfg::Office::Common::UserColors::PaletteName::get() );
185 m_xSelectPalette->set_active_text(aPaletteName);
186 if (m_xSelectPalette->get_active() != -1)
188 SelectPaletteLBHdl(*m_xSelectPalette);
192 void SvxColorTabPage::Construct()
194 if (pColorList.is())
196 FillPaletteLB();
197 ImpColorCountChanged();
201 void SvxColorTabPage::ActivatePage( const SfxItemSet& )
203 if( !pColorList.is() )
204 return;
206 if( const XFillColorItem* pFillColorItem = rOutAttrs.GetItemIfSet( GetWhich( XATTR_FILLCOLOR ) ) )
208 SetColorModel( ColorModel::RGB );
209 ChangeColorModel();
211 const Color aColor = pFillColorItem->GetColorValue();
212 NamedColor aNamedColor;
213 aNamedColor.m_aColor = aColor;
214 ChangeColor(aNamedColor);
215 sal_Int32 nPos = FindInPalette( aColor );
217 if ( nPos != -1 )
218 m_xValSetColorList->SelectItem(m_xValSetColorList->GetItemId(nPos));
219 // else search in other palettes?
223 m_aCtlPreviewOld.SetAttributes(aXFillAttr.GetItemSet());
224 m_aCtlPreviewOld.Invalidate();
226 SelectValSetHdl_Impl(m_xValSetColorList.get());
229 DeactivateRC SvxColorTabPage::DeactivatePage( SfxItemSet* _pSet )
231 if( _pSet )
232 FillItemSet( _pSet );
234 return DeactivateRC::LeavePage;
237 bool SvxColorTabPage::FillItemSet( SfxItemSet* rSet )
239 Color aColor = m_xValSetColorList->GetItemColor( m_xValSetColorList->GetSelectedItemId() );
240 OUString sColorName;
241 if (m_aCurrentColor.m_aColor == aColor)
242 sColorName = m_xValSetColorList->GetItemText( m_xValSetColorList->GetSelectedItemId() );
243 else
244 sColorName = "#" + m_aCurrentColor.m_aColor.AsRGBHexString().toAsciiUpperCase();
246 maPaletteManager.AddRecentColor(m_aCurrentColor.m_aColor, sColorName);
247 XFillColorItem aColorItem(sColorName, m_aCurrentColor.m_aColor);
248 aColorItem.setComplexColor(m_aCurrentColor.getComplexColor());
249 rSet->Put(aColorItem);
250 rSet->Put(XFillStyleItem(drawing::FillStyle_SOLID));
251 return true;
254 void SvxColorTabPage::UpdateModified()
256 bool bEnable = pColorList.is() && pColorList->Count();
257 m_xBtnWorkOn->set_sensitive(bEnable);
260 void SvxColorTabPage::Reset( const SfxItemSet* rSet )
262 SfxItemState nState = rSet->GetItemState( XATTR_FILLCOLOR );
264 Color aNewColor;
266 if ( nState >= SfxItemState::DEFAULT )
268 const XFillColorItem& aColorItem( rSet->Get( XATTR_FILLCOLOR ) );
269 m_aPreviousColor = aColorItem.GetColorValue();
270 aNewColor = aColorItem.GetColorValue();
273 // set color model
274 OUString aStr = GetUserData();
275 eCM = static_cast<ColorModel>(aStr.toInt32());
276 SetColorModel( eCM );
277 ChangeColorModel();
279 NamedColor aColor;
280 aColor.m_aColor = aNewColor;
281 ChangeColor(aColor);
283 UpdateModified();
286 std::unique_ptr<SfxTabPage> SvxColorTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rOutAttrs)
288 return std::make_unique<SvxColorTabPage>(pPage, pController, *rOutAttrs);
291 // is called when the content of the MtrFields is changed for color values
292 IMPL_LINK_NOARG(SvxColorTabPage, SpinValueHdl_Impl, weld::SpinButton&, void)
294 // read current MtrFields, if cmyk, then k-value as transparency
295 m_aCurrentColor.m_aColor = Color(static_cast<sal_uInt8>(PercentToColor_Impl(m_xRcustom->get_value())),
296 static_cast<sal_uInt8>(PercentToColor_Impl(m_xGcustom->get_value())),
297 static_cast<sal_uInt8>(PercentToColor_Impl(m_xBcustom->get_value())));
298 UpdateColorValues();
300 rXFSet.Put( XFillColorItem( OUString(), m_aCurrentColor.m_aColor ) );
301 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
303 m_aCtlPreviewNew.Invalidate();
306 IMPL_LINK_NOARG(SvxColorTabPage, MetricSpinValueHdl_Impl, weld::MetricSpinButton&, void)
308 // read current MtrFields, if cmyk, then k-value as transparency
309 m_aCurrentColor.m_aColor = Color(ColorTransparency, static_cast<sal_uInt8>(PercentToColor_Impl(m_xKcustom->get_value(FieldUnit::NONE))),
310 static_cast<sal_uInt8>(PercentToColor_Impl(m_xCcustom->get_value(FieldUnit::NONE))),
311 static_cast<sal_uInt8>(PercentToColor_Impl(m_xYcustom->get_value(FieldUnit::NONE))),
312 static_cast<sal_uInt8>(PercentToColor_Impl(m_xMcustom->get_value(FieldUnit::NONE))));
313 ConvertColorValues (m_aCurrentColor.m_aColor, ColorModel::RGB);
315 rXFSet.Put( XFillColorItem( OUString(), m_aCurrentColor.m_aColor ) );
316 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
318 m_aCtlPreviewNew.Invalidate();
321 IMPL_LINK_NOARG(SvxColorTabPage, ModifiedHdl_Impl, weld::Entry&, void)
323 m_aCurrentColor.m_aColor = m_xHexcustom->GetColor();
324 UpdateColorValues();
326 rXFSet.Put( XFillColorItem( OUString(), m_aCurrentColor.m_aColor ) );
327 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
329 m_aCtlPreviewNew.Invalidate();
332 IMPL_LINK_NOARG(SvxColorTabPage, ClickAddHdl_Impl, weld::Button&, void)
334 OUString aNewName( SvxResId( RID_SVXSTR_COLOR ) );
335 OUString aDesc( CuiResId( RID_CUISTR_DESC_COLOR ) );
336 OUString aName;
338 tools::Long j = 1;
339 bool bValidColorName = false;
340 // check if name is already existing
341 while (!bValidColorName)
343 aName = aNewName + " " + OUString::number( j++ );
344 bValidColorName = (FindInCustomColors(aName) == -1);
347 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
348 ScopedVclPtr<AbstractSvxNameDialog> pDlg(pFact->CreateSvxNameDialog(GetFrameWeld(), aName, aDesc));
349 sal_uInt16 nError = 1;
351 while (pDlg->Execute() == RET_OK)
353 aName = pDlg->GetName();
355 bValidColorName = (FindInCustomColors(aName) == -1);
356 if (bValidColorName)
358 nError = 0;
359 break;
362 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), u"cui/ui/queryduplicatedialog.ui"_ustr));
363 std::unique_ptr<weld::MessageDialog> xWarnBox(xBuilder->weld_message_dialog(u"DuplicateNameDialog"_ustr));
364 if (xWarnBox->run() != RET_OK)
365 break;
368 pDlg.disposeAndClear();
370 if (!nError)
372 m_xSelectPalette->set_active(0);
373 SelectPaletteLBHdl(*m_xSelectPalette);
374 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
375 css::uno::Sequence< sal_Int32 > aCustomColorList(officecfg::Office::Common::UserColors::CustomColor::get());
376 css::uno::Sequence< OUString > aCustomColorNameList(officecfg::Office::Common::UserColors::CustomColorName::get());
377 sal_Int32 nSize = aCustomColorList.getLength();
378 aCustomColorList.realloc( nSize + 1 );
379 aCustomColorNameList.realloc( nSize + 1 );
380 aCustomColorList.getArray()[nSize] = sal_Int32(m_aCurrentColor.m_aColor);
381 aCustomColorNameList.getArray()[nSize] = aName;
382 officecfg::Office::Common::UserColors::CustomColor::set(aCustomColorList, batch);
383 officecfg::Office::Common::UserColors::CustomColorName::set(aCustomColorNameList, batch);
384 batch->commit();
385 sal_uInt16 nId = m_xValSetColorList->GetItemId(nSize - 1);
386 m_xValSetColorList->InsertItem( nId + 1 , m_aCurrentColor.m_aColor, aName );
387 m_xValSetColorList->SelectItem( nId + 1 );
388 m_xBtnDelete->set_sensitive(false);
389 m_xBtnDelete->set_tooltip_text( CuiResId(RID_CUISTR_DELETEUSERCOLOR2) );
390 ImpColorCountChanged();
393 UpdateModified();
396 IMPL_LINK_NOARG(SvxColorTabPage, ClickWorkOnHdl_Impl, weld::Button&, void)
398 SvColorDialog aColorDlg;
400 aColorDlg.SetColor (m_aCurrentColor.m_aColor);
401 aColorDlg.SetMode( svtools::ColorPickerMode::Modify );
403 if (aColorDlg.Execute(GetFrameWeld()) == RET_OK)
405 Color aPreviewColor = aColorDlg.GetColor();
406 m_aCurrentColor.m_aColor = aPreviewColor;
407 UpdateColorValues( false );
408 // fill ItemSet and pass it on to XOut
409 rXFSet.Put( XFillColorItem( OUString(), aPreviewColor ) );
410 //m_aCtlPreviewOld.SetAttributes( aXFillAttr );
411 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
413 m_aCtlPreviewNew.Invalidate();
417 IMPL_LINK_NOARG(SvxColorTabPage, ClickDeleteHdl_Impl, weld::Button&, void)
419 sal_uInt16 nId = m_xValSetColorList->GetSelectedItemId();
420 size_t nPos = m_xValSetColorList->GetSelectItemPos();
421 if (m_xSelectPalette->get_active() != 0 || nPos == VALUESET_ITEM_NOTFOUND)
422 return;
424 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
425 css::uno::Sequence< sal_Int32 > aCustomColorList(officecfg::Office::Common::UserColors::CustomColor::get());
426 auto aCustomColorListRange = asNonConstRange(aCustomColorList);
427 css::uno::Sequence< OUString > aCustomColorNameList(officecfg::Office::Common::UserColors::CustomColorName::get());
428 auto aCustomColorNameListRange = asNonConstRange(aCustomColorNameList);
429 sal_Int32 nSize = aCustomColorList.getLength() - 1;
430 for(sal_Int32 nIndex = static_cast<sal_Int32>(nPos);nIndex < nSize;nIndex++)
432 aCustomColorListRange[nIndex] = aCustomColorList[nIndex+1];
433 aCustomColorNameListRange[nIndex] = aCustomColorNameList[nIndex+1];
435 aCustomColorList.realloc(nSize);
436 aCustomColorNameList.realloc(nSize);
437 officecfg::Office::Common::UserColors::CustomColor::set(aCustomColorList, batch);
438 officecfg::Office::Common::UserColors::CustomColorName::set(aCustomColorNameList, batch);
439 batch->commit();
440 m_xValSetColorList->RemoveItem(nId);
441 if (m_xValSetColorList->GetItemCount() != 0)
443 nId = m_xValSetColorList->GetItemId(0);
444 m_xValSetColorList->SelectItem(nId);
445 SelectValSetHdl_Impl(m_xValSetColorList.get());
447 else
449 m_xBtnDelete->set_sensitive(false);
450 m_xBtnDelete->set_tooltip_text( CuiResId(RID_CUISTR_DELETEUSERCOLOR2) );
454 IMPL_LINK_NOARG(SvxColorTabPage, SelectPaletteLBHdl, weld::ComboBox&, void)
456 m_xValSetColorList->Clear();
457 sal_Int32 nPos = m_xSelectPalette->get_active();
458 maPaletteManager.SetPalette( nPos );
459 maPaletteManager.ReloadColorSet(*m_xValSetColorList);
461 if(nPos != maPaletteManager.GetPaletteCount() - 1 && nPos != 0)
463 XColorListRef pList = XPropertyList::AsColorList(
464 XPropertyList::CreatePropertyListFromURL(
465 XPropertyListType::Color, maPaletteManager.GetSelectedPalettePath()));
466 pList->SetName(maPaletteManager.GetPaletteName());
467 if(pList->Load())
469 SfxOkDialogController* pController = GetDialogController();
470 SvxAreaTabDialog* pArea = dynamic_cast<SvxAreaTabDialog*>(pController);
471 SvxLineTabDialog* pLine = dynamic_cast<SvxLineTabDialog*>(pController);
472 pColorList = pList;
473 if( pArea )
474 pArea->SetNewColorList(pList);
475 else if( pLine )
476 pLine->SetNewColorList(pList);
477 else
478 SetColorList(pList);
479 *pnColorListState |= ChangeType::CHANGED;
480 *pnColorListState &= ~ChangeType::MODIFIED;
483 if (nPos != 0)
485 m_xBtnDelete->set_sensitive(false);
486 m_xBtnDelete->set_tooltip_text( CuiResId(RID_CUISTR_DELETEUSERCOLOR1) );
489 m_xValSetColorList->Resize();
492 IMPL_LINK(SvxColorTabPage, SelectValSetHdl_Impl, ValueSet*, pValSet, void)
494 sal_Int32 nPos = pValSet->GetSelectedItemId();
495 if( nPos == 0 )
496 return;
498 Color aColor = pValSet->GetItemColor( nPos );
500 rXFSet.Put( XFillColorItem( OUString(), aColor ) );
501 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
502 m_aCtlPreviewNew.Invalidate();
504 NamedColor aNamedColor;
505 aNamedColor.m_aColor = aColor;
507 if (pValSet == m_xValSetColorList.get() && maPaletteManager.IsThemePaletteSelected())
509 sal_uInt16 nThemeIndex;
510 sal_uInt16 nEffectIndex;
511 if (PaletteManager::GetThemeAndEffectIndex(nPos, nThemeIndex, nEffectIndex))
513 aNamedColor.m_nThemeIndex = nThemeIndex;
514 maPaletteManager.GetLumModOff(nThemeIndex, nEffectIndex, aNamedColor.m_nLumMod, aNamedColor.m_nLumOff);
518 ChangeColor(aNamedColor, false);
520 if (pValSet == m_xValSetColorList.get())
522 m_xValSetRecentList->SetNoSelection();
523 if (m_xSelectPalette->get_active() == 0 && m_xValSetColorList->GetSelectedItemId() != 0)
525 m_xBtnDelete->set_sensitive(true);
526 m_xBtnDelete->set_tooltip_text(u""_ustr);
528 else
530 m_xBtnDelete->set_sensitive(false);
531 m_xBtnDelete->set_tooltip_text( CuiResId(RID_CUISTR_DELETEUSERCOLOR1) );
534 if (pValSet == m_xValSetRecentList.get())
536 m_xValSetColorList->SetNoSelection();
537 m_xBtnDelete->set_sensitive(false);
538 m_xBtnDelete->set_tooltip_text( CuiResId(RID_CUISTR_DELETEUSERCOLOR2) );
542 void SvxColorTabPage::ConvertColorValues (Color& rColor, ColorModel eModell)
544 switch (eModell)
546 case ColorModel::RGB:
548 CmykToRgb_Impl (rColor, static_cast<sal_uInt16>(255 - rColor.GetAlpha()) );
549 rColor.SetAlpha (255);
551 break;
553 case ColorModel::CMYK:
555 sal_uInt16 nK;
556 RgbToCmyk_Impl (rColor, nK );
557 rColor.SetAlpha (255 - static_cast<sal_uInt8>(nK));
559 break;
563 IMPL_LINK_NOARG(SvxColorTabPage, SelectColorModeHdl_Impl, weld::Toggleable&, void)
565 if (m_xRbRGB->get_active())
566 eCM = ColorModel::RGB;
567 else if (m_xRbCMYK->get_active())
568 eCM = ColorModel::CMYK;
569 ChangeColorModel();
570 UpdateColorValues();
574 IMPL_STATIC_LINK_NOARG(SvxColorTabPage, OnMoreColorsClick, weld::Button&, void)
576 css::uno::Sequence<css::beans::PropertyValue> aArgs{ comphelper::makePropertyValue(
577 u"AdditionsTag"_ustr, u"Color Palette"_ustr) };
578 comphelper::dispatchCommand(u".uno:AdditionsDialog"_ustr, aArgs);
581 void SvxColorTabPage::ChangeColor(const NamedColor &rNewColor, bool bUpdatePreset )
583 m_aPreviousColor = rNewColor.m_aColor;
584 m_aCurrentColor = rNewColor;
585 UpdateColorValues( bUpdatePreset );
586 // fill ItemSet and pass it on to XOut
587 XFillColorItem aItem(OUString(), m_aCurrentColor.m_aColor);
588 aItem.setComplexColor(m_aCurrentColor.getComplexColor());
589 rXFSet.Put(aItem);
591 m_aCtlPreviewNew.SetAttributes(aXFillAttr.GetItemSet());
592 m_aCtlPreviewNew.Invalidate();
595 void SvxColorTabPage::SetColorModel( ColorModel eModel )
597 if (eModel == ColorModel::RGB)
598 m_xRbRGB->set_active(true);
599 else if (eModel == ColorModel::CMYK)
600 m_xRbCMYK->set_active(true);
603 void SvxColorTabPage::ChangeColorModel()
605 switch( eCM )
607 case ColorModel::RGB:
609 m_xRGBcustom->show();
610 m_xRGBpreset->show();
611 m_xCMYKcustom->hide();
612 m_xCMYKpreset->hide();
614 break;
616 case ColorModel::CMYK:
618 m_xCMYKcustom->show();
619 m_xCMYKpreset->show();
620 m_xRGBcustom->hide();
621 m_xRGBpreset->hide();
623 break;
627 void SvxColorTabPage::UpdateColorValues( bool bUpdatePreset )
629 if (eCM != ColorModel::RGB)
631 ConvertColorValues (m_aPreviousColor, eCM );
632 ConvertColorValues (m_aCurrentColor.m_aColor, eCM);
634 m_xCcustom->set_value( ColorToPercent_Impl( m_aCurrentColor.m_aColor.GetRed() ), FieldUnit::PERCENT );
635 m_xMcustom->set_value( ColorToPercent_Impl( m_aCurrentColor.m_aColor.GetBlue() ), FieldUnit::PERCENT );
636 m_xYcustom->set_value( ColorToPercent_Impl( m_aCurrentColor.m_aColor.GetGreen() ), FieldUnit::PERCENT );
637 m_xKcustom->set_value( ColorToPercent_Impl( 255 - m_aCurrentColor.m_aColor.GetAlpha() ), FieldUnit::PERCENT );
639 if( bUpdatePreset )
641 m_xCpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(m_aPreviousColor.GetRed()),
642 Application::GetSettings().GetUILanguageTag()));
643 m_xMpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(m_aPreviousColor.GetBlue()),
644 Application::GetSettings().GetUILanguageTag()));
645 m_xYpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(m_aPreviousColor.GetGreen()),
646 Application::GetSettings().GetUILanguageTag()));
647 m_xKpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(255 - m_aPreviousColor.GetAlpha()),
648 Application::GetSettings().GetUILanguageTag()));
651 ConvertColorValues (m_aPreviousColor, ColorModel::RGB);
652 ConvertColorValues (m_aCurrentColor.m_aColor, ColorModel::RGB);
654 else
656 m_xRcustom->set_value( ColorToPercent_Impl( m_aCurrentColor.m_aColor.GetRed() ) );
657 m_xGcustom->set_value( ColorToPercent_Impl( m_aCurrentColor.m_aColor.GetGreen() ) );
658 m_xBcustom->set_value( ColorToPercent_Impl( m_aCurrentColor.m_aColor.GetBlue() ) );
659 m_xHexcustom->SetColor( m_aCurrentColor.m_aColor );
661 if( bUpdatePreset )
663 m_xRpreset->set_text(OUString::number(ColorToPercent_Impl(m_aPreviousColor.GetRed())));
664 m_xGpreset->set_text(OUString::number(ColorToPercent_Impl(m_aPreviousColor.GetGreen())));
665 m_xBpreset->set_text(OUString::number(ColorToPercent_Impl(m_aPreviousColor.GetBlue())));
666 m_xHexpreset->SetColor( m_aPreviousColor );
671 sal_Int32 SvxColorTabPage::FindInCustomColors(std::u16string_view aColorName)
673 css::uno::Sequence< OUString > aCustomColorNameList(officecfg::Office::Common::UserColors::CustomColorName::get());
674 tools::Long nCount = aCustomColorNameList.getLength();
675 bool bValidColorName = true;
676 sal_Int32 nPos = -1;
678 for(tools::Long i = 0;i < nCount && bValidColorName;i++)
680 if(aColorName == aCustomColorNameList[i])
682 nPos = i;
683 bValidColorName = false;
686 return nPos;
689 sal_Int32 SvxColorTabPage::FindInPalette( const Color& rColor )
691 return pColorList->GetIndexOfColor(rColor);
694 // A RGB value is converted to a CMYK value - not in an ideal way as
695 // R is converted into C, G into M and B into Y. The K value is held in an
696 // extra variable. For further color models one should develop own
697 // classes which should contain the respective casts.
699 void SvxColorTabPage::RgbToCmyk_Impl( Color& rColor, sal_uInt16& rK )
701 sal_uInt16 const nColor1 = 255 - rColor.GetRed();
702 sal_uInt16 const nColor2 = 255 - rColor.GetGreen();
703 sal_uInt16 const nColor3 = 255 - rColor.GetBlue();
705 rK = std::min( std::min( nColor1, nColor2 ), nColor3 );
707 rColor.SetRed( sal::static_int_cast< sal_uInt8 >( nColor1 - rK ) );
708 rColor.SetGreen( sal::static_int_cast< sal_uInt8 >( nColor2 - rK ) );
709 rColor.SetBlue( sal::static_int_cast< sal_uInt8 >( nColor3 - rK ) );
713 // reverse case to RgbToCmyk_Impl (see above)
715 void SvxColorTabPage::CmykToRgb_Impl( Color& rColor, const sal_uInt16 nK )
717 tools::Long lTemp;
719 lTemp = 255 - ( rColor.GetRed() + nK );
721 if( lTemp < 0 )
722 lTemp = 0;
723 rColor.SetRed( static_cast<sal_uInt8>(lTemp) );
725 lTemp = 255 - ( rColor.GetGreen() + nK );
727 if( lTemp < 0 )
728 lTemp = 0;
729 rColor.SetGreen( static_cast<sal_uInt8>(lTemp) );
731 lTemp = 255 - ( rColor.GetBlue() + nK );
733 if( lTemp < 0 )
734 lTemp = 0;
735 rColor.SetBlue( static_cast<sal_uInt8>(lTemp) );
739 sal_uInt16 SvxColorTabPage::ColorToPercent_Impl( sal_uInt16 nColor )
741 sal_uInt16 nValue = 0;
743 switch (eCM)
745 case ColorModel::RGB :
746 nValue = nColor;
747 break;
749 case ColorModel::CMYK:
750 nValue = static_cast<sal_uInt16>( static_cast<double>(nColor) * 100.0 / 255.0 + 0.5 );
751 break;
754 return nValue;
758 sal_uInt16 SvxColorTabPage::PercentToColor_Impl( sal_uInt16 nPercent )
760 sal_uInt16 nValue = 0;
762 switch (eCM)
764 case ColorModel::RGB :
765 nValue = nPercent;
766 break;
768 case ColorModel::CMYK:
769 nValue = static_cast<sal_uInt16>( static_cast<double>(nPercent) * 255.0 / 100.0 + 0.5 );
770 break;
773 return nValue;
777 void SvxColorTabPage::FillUserData()
779 // the color model is saved in the Ini-file
780 SetUserData( OUString::number( static_cast<int>(eCM) ) );
784 void SvxColorTabPage::SetPropertyList( XPropertyListType t, const XPropertyListRef &xRef )
786 OSL_ASSERT( t == XPropertyListType::Color );
787 pColorList = XColorListRef( static_cast<XColorList *>(xRef.get() ) );
790 void SvxColorTabPage::SetColorList( const XColorListRef& pColList )
792 SetPropertyList( XPropertyListType::Color, XPropertyListRef( ( pColList.get() ) ) );
795 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */