Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cui / source / tabpages / tpcolor.cxx
blob66fa7f9cd16510d1e54318b6483dc5d2d2718eac
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>
38 using namespace com::sun::star;
40 SvxColorTabPage::SvxColorTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rInAttrs)
41 : SfxTabPage(pPage, pController, "cui/ui/colorpage.ui", "ColorPage", &rInAttrs)
42 , rOutAttrs ( rInAttrs )
43 // All the horrific pointers we store and should not
44 , pnColorListState( nullptr )
45 , aXFillAttr( rInAttrs.GetPool() )
46 , rXFSet( aXFillAttr.GetItemSet() )
47 , eCM( ColorModel::RGB )
48 , m_context(comphelper::getProcessComponentContext())
49 , m_xValSetColorList(new ColorValueSet(m_xBuilder->weld_scrolled_window("colorsetwin")))
50 , m_xValSetRecentList(new ColorValueSet(nullptr))
51 , m_xSelectPalette(m_xBuilder->weld_combo_box("paletteselector"))
52 , m_xRbRGB(m_xBuilder->weld_radio_button("RGB"))
53 , m_xRbCMYK(m_xBuilder->weld_radio_button("CMYK"))
54 , m_xRGBcustom(m_xBuilder->weld_widget("rgbcustom"))
55 , m_xRGBpreset(m_xBuilder->weld_widget("rgbpreset"))
56 , m_xRpreset(m_xBuilder->weld_entry("R_preset"))
57 , m_xGpreset(m_xBuilder->weld_entry("G_preset"))
58 , m_xBpreset(m_xBuilder->weld_entry("B_preset"))
59 , m_xRcustom(m_xBuilder->weld_spin_button("R_custom"))
60 , m_xGcustom(m_xBuilder->weld_spin_button("G_custom"))
61 , m_xBcustom(m_xBuilder->weld_spin_button("B_custom"))
62 , m_xHexpreset(new weld::HexColorControl(m_xBuilder->weld_entry("hex_preset")))
63 , m_xHexcustom(new weld::HexColorControl(m_xBuilder->weld_entry("hex_custom")))
64 , m_xCMYKcustom(m_xBuilder->weld_widget("cmykcustom"))
65 , m_xCMYKpreset(m_xBuilder->weld_widget("cmykpreset"))
66 , m_xCpreset(m_xBuilder->weld_entry("C_preset"))
67 , m_xYpreset(m_xBuilder->weld_entry("Y_preset"))
68 , m_xMpreset(m_xBuilder->weld_entry("M_preset"))
69 , m_xKpreset(m_xBuilder->weld_entry("K_preset"))
70 , m_xCcustom(m_xBuilder->weld_metric_spin_button("C_custom", FieldUnit::PERCENT))
71 , m_xYcustom(m_xBuilder->weld_metric_spin_button("Y_custom", FieldUnit::PERCENT))
72 , m_xMcustom(m_xBuilder->weld_metric_spin_button("M_custom", FieldUnit::PERCENT))
73 , m_xKcustom(m_xBuilder->weld_metric_spin_button("K_custom", FieldUnit::PERCENT))
74 , m_xBtnAdd(m_xBuilder->weld_button("add"))
75 , m_xBtnDelete(m_xBuilder->weld_button("delete"))
76 , m_xBtnWorkOn(m_xBuilder->weld_button("edit"))
77 , m_xCtlPreviewOld(new weld::CustomWeld(*m_xBuilder, "oldpreview", m_aCtlPreviewOld))
78 , m_xCtlPreviewNew(new weld::CustomWeld(*m_xBuilder, "newpreview", m_aCtlPreviewNew))
79 , m_xValSetColorListWin(new weld::CustomWeld(*m_xBuilder, "colorset", *m_xValSetColorList))
80 , m_xValSetRecentListWin(new weld::CustomWeld(*m_xBuilder, "recentcolorset", *m_xValSetRecentList))
82 Size aSize(m_xBtnWorkOn->get_approximate_digit_width() * 25,
83 m_xBtnWorkOn->get_text_height() * 10);
84 m_xValSetColorList->set_size_request(aSize.Width(), aSize.Height());
85 aSize = Size(m_xBtnWorkOn->get_approximate_digit_width() * 8,
86 m_xBtnWorkOn->get_text_height() * 3);
87 m_aCtlPreviewOld.set_size_request(aSize.Width(), aSize.Height());
88 m_aCtlPreviewNew.set_size_request(aSize.Width(), aSize.Height());
89 // this page needs ExchangeSupport
90 SetExchangeSupport();
92 // setting the output device
93 rXFSet.Put( XFillStyleItem(drawing::FillStyle_SOLID) );
94 rXFSet.Put( XFillColorItem(OUString(), COL_BLACK) );
95 m_aCtlPreviewOld.SetAttributes( aXFillAttr.GetItemSet() );
96 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
98 // set handler
99 m_xSelectPalette->connect_changed(LINK(this, SvxColorTabPage, SelectPaletteLBHdl));
100 Link<SvtValueSet*, void> aValSelectLink = LINK(this, SvxColorTabPage, SelectValSetHdl_Impl);
101 m_xValSetColorList->SetSelectHdl(aValSelectLink);
102 m_xValSetRecentList->SetSelectHdl(aValSelectLink);
104 Link<weld::SpinButton&,void> aSpinLink = LINK(this, SvxColorTabPage, SpinValueHdl_Impl);
105 m_xRcustom->connect_value_changed(aSpinLink);
106 m_xGcustom->connect_value_changed(aSpinLink);
107 m_xBcustom->connect_value_changed(aSpinLink);
108 Link<weld::Entry&,void> aEntryLink = LINK(this, SvxColorTabPage, ModifiedHdl_Impl);
109 m_xHexcustom->connect_changed(aEntryLink);
110 Link<weld::MetricSpinButton&,void> aMetricSpinLink = LINK(this, SvxColorTabPage, MetricSpinValueHdl_Impl);
111 m_xCcustom->connect_value_changed(aMetricSpinLink);
112 m_xYcustom->connect_value_changed(aMetricSpinLink);
113 m_xMcustom->connect_value_changed(aMetricSpinLink);
114 m_xKcustom->connect_value_changed(aMetricSpinLink);
116 Link<weld::ToggleButton&,void> aLink2 = LINK( this, SvxColorTabPage, SelectColorModeHdl_Impl );
117 m_xRbRGB->connect_toggled(aLink2);
118 m_xRbCMYK->connect_toggled(aLink2);
119 SetColorModel( eCM );
120 ChangeColorModel();
122 m_xBtnAdd->connect_clicked( LINK( this, SvxColorTabPage, ClickAddHdl_Impl ) );
123 m_xBtnWorkOn->connect_clicked( LINK( this, SvxColorTabPage, ClickWorkOnHdl_Impl ) );
124 m_xBtnDelete->connect_clicked( LINK( this, SvxColorTabPage, ClickDeleteHdl_Impl ) );
125 // disable modify buttons
126 // Color palettes can't be modified
127 m_xBtnDelete->set_sensitive(false);
129 // disable preset color values
130 m_xRGBpreset->set_sensitive(false);
131 m_xCMYKpreset->set_sensitive(false);
133 // ValueSet
134 m_xValSetColorList->SetStyle(m_xValSetColorList->GetStyle() | WB_ITEMBORDER);
135 m_xValSetColorList->Show();
137 m_xValSetRecentList->SetStyle(m_xValSetRecentList->GetStyle() | WB_ITEMBORDER);
138 m_xValSetRecentList->Show();
140 maPaletteManager.ReloadRecentColorSet(*m_xValSetRecentList);
141 aSize = m_xValSetRecentList->layoutAllVisible(maPaletteManager.GetRecentColorCount());
142 m_xValSetRecentList->set_size_request(aSize.Width(), aSize.Height());
145 SvxColorTabPage::~SvxColorTabPage()
147 m_xValSetRecentListWin.reset();
148 m_xValSetRecentList.reset();
149 m_xValSetColorListWin.reset();
150 m_xValSetColorList.reset();
153 void SvxColorTabPage::ImpColorCountChanged()
155 if (!pColorList.is())
156 return;
157 m_xValSetColorList->SetColCount(SvxColorValueSet::getColumnCount());
158 m_xValSetRecentList->SetColCount(SvxColorValueSet::getColumnCount());
161 void SvxColorTabPage::FillPaletteLB()
163 m_xSelectPalette->clear();
164 std::vector<OUString> aPaletteList = maPaletteManager.GetPaletteList();
165 for (auto const& palette : aPaletteList)
167 m_xSelectPalette->append_text(palette);
169 OUString aPaletteName( officecfg::Office::Common::UserColors::PaletteName::get() );
170 m_xSelectPalette->set_active_text(aPaletteName);
171 if (m_xSelectPalette->get_active() != -1)
173 SelectPaletteLBHdl(*m_xSelectPalette);
177 void SvxColorTabPage::Construct()
179 if (pColorList.is())
181 FillPaletteLB();
182 ImpColorCountChanged();
186 void SvxColorTabPage::ActivatePage( const SfxItemSet& )
188 if( pColorList.is() )
190 const SfxPoolItem* pPoolItem = nullptr;
191 if( SfxItemState::SET == rOutAttrs.GetItemState( GetWhich( XATTR_FILLCOLOR ), true, &pPoolItem ) )
193 SetColorModel( ColorModel::RGB );
194 ChangeColorModel();
196 const Color aColor = static_cast<const XFillColorItem*>(pPoolItem)->GetColorValue();
197 ChangeColor( aColor );
198 sal_Int32 nPos = FindInPalette( aColor );
200 if ( nPos != -1 )
201 m_xValSetColorList->SelectItem(m_xValSetColorList->GetItemId(nPos));
202 // else search in other palettes?
206 m_aCtlPreviewOld.SetAttributes(aXFillAttr.GetItemSet());
207 m_aCtlPreviewOld.Invalidate();
209 SelectValSetHdl_Impl(m_xValSetColorList.get());
213 DeactivateRC SvxColorTabPage::DeactivatePage( SfxItemSet* _pSet )
215 if( _pSet )
216 FillItemSet( _pSet );
218 return DeactivateRC::LeavePage;
221 bool SvxColorTabPage::FillItemSet( SfxItemSet* rSet )
223 Color aColor = m_xValSetColorList->GetItemColor( m_xValSetColorList->GetSelectedItemId() );
224 OUString sColorName;
225 if ( aCurrentColor == aColor )
226 sColorName = m_xValSetColorList->GetItemText( m_xValSetColorList->GetSelectedItemId() );
227 else
228 sColorName = "#" + aCurrentColor.AsRGBHexString().toAsciiUpperCase();
229 maPaletteManager.AddRecentColor( aCurrentColor, sColorName );
230 rSet->Put( XFillColorItem( sColorName, aCurrentColor ) );
231 rSet->Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
232 return true;
235 void SvxColorTabPage::UpdateModified()
237 bool bEnable = pColorList.is() && pColorList->Count();
238 m_xBtnWorkOn->set_sensitive(bEnable);
241 void SvxColorTabPage::Reset( const SfxItemSet* rSet )
243 SfxItemState nState = rSet->GetItemState( XATTR_FILLCOLOR );
245 Color aNewColor;
247 if ( nState >= SfxItemState::DEFAULT )
249 XFillColorItem aColorItem( rSet->Get( XATTR_FILLCOLOR ) );
250 aPreviousColor = aColorItem.GetColorValue();
251 aNewColor = aColorItem.GetColorValue();
254 // set color model
255 OUString aStr = GetUserData();
256 eCM = static_cast<ColorModel>(aStr.toInt32());
257 SetColorModel( eCM );
258 ChangeColorModel();
260 ChangeColor(aNewColor);
262 UpdateModified();
265 std::unique_ptr<SfxTabPage> SvxColorTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rOutAttrs)
267 return std::make_unique<SvxColorTabPage>(pPage, pController, *rOutAttrs);
270 // is called when the content of the MtrFields is changed for color values
271 IMPL_LINK_NOARG(SvxColorTabPage, SpinValueHdl_Impl, weld::SpinButton&, void)
273 // read current MtrFields, if cmyk, then k-value as transparency
274 aCurrentColor = Color(static_cast<sal_uInt8>(PercentToColor_Impl(m_xRcustom->get_value())),
275 static_cast<sal_uInt8>(PercentToColor_Impl(m_xGcustom->get_value())),
276 static_cast<sal_uInt8>(PercentToColor_Impl(m_xBcustom->get_value())));
277 UpdateColorValues();
279 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
280 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
282 m_aCtlPreviewNew.Invalidate();
285 IMPL_LINK_NOARG(SvxColorTabPage, MetricSpinValueHdl_Impl, weld::MetricSpinButton&, void)
287 // read current MtrFields, if cmyk, then k-value as transparency
288 aCurrentColor = Color(static_cast<sal_uInt8>(PercentToColor_Impl(m_xKcustom->get_value(FieldUnit::NONE))),
289 static_cast<sal_uInt8>(PercentToColor_Impl(m_xCcustom->get_value(FieldUnit::NONE))),
290 static_cast<sal_uInt8>(PercentToColor_Impl(m_xYcustom->get_value(FieldUnit::NONE))),
291 static_cast<sal_uInt8>(PercentToColor_Impl(m_xMcustom->get_value(FieldUnit::NONE))));
292 ConvertColorValues (aCurrentColor, ColorModel::RGB);
294 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
295 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
297 m_aCtlPreviewNew.Invalidate();
300 IMPL_LINK_NOARG(SvxColorTabPage, ModifiedHdl_Impl, weld::Entry&, void)
302 aCurrentColor = m_xHexcustom->GetColor();
303 UpdateColorValues();
305 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
306 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
308 m_aCtlPreviewNew.Invalidate();
311 IMPL_LINK_NOARG(SvxColorTabPage, ClickAddHdl_Impl, weld::Button&, void)
313 OUString aNewName( SvxResId( RID_SVXSTR_COLOR ) );
314 OUString aDesc( CuiResId( RID_SVXSTR_DESC_COLOR ) );
315 OUString aName;
317 long j = 1;
318 bool bValidColorName = false;
319 // check if name is already existing
320 while (!bValidColorName)
322 aName = aNewName + " " + OUString::number( j++ );
323 bValidColorName = (FindInCustomColors(aName) == LISTBOX_ENTRY_NOTFOUND);
326 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
327 ScopedVclPtr<AbstractSvxNameDialog> pDlg(pFact->CreateSvxNameDialog(GetFrameWeld(), aName, aDesc));
328 sal_uInt16 nError = 1;
330 while (pDlg->Execute() == RET_OK)
332 pDlg->GetName( aName );
334 bValidColorName = (FindInCustomColors(aName) == LISTBOX_ENTRY_NOTFOUND);
335 if (bValidColorName)
337 nError = 0;
338 break;
341 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), "cui/ui/queryduplicatedialog.ui"));
342 std::unique_ptr<weld::MessageDialog> xWarnBox(xBuilder->weld_message_dialog("DuplicateNameDialog"));
343 if (xWarnBox->run() != RET_OK)
344 break;
347 pDlg.disposeAndClear();
349 if (!nError)
351 m_xSelectPalette->set_active(0);
352 SelectPaletteLBHdl(*m_xSelectPalette);
353 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create(m_context));
354 css::uno::Sequence< sal_Int32 > aCustomColorList(officecfg::Office::Common::UserColors::CustomColor::get());
355 css::uno::Sequence< OUString > aCustomColorNameList(officecfg::Office::Common::UserColors::CustomColorName::get());
356 sal_Int32 nSize = aCustomColorList.getLength();
357 aCustomColorList.realloc( nSize + 1 );
358 aCustomColorNameList.realloc( nSize + 1 );
359 aCustomColorList[nSize] = sal_Int32(aCurrentColor);
360 aCustomColorNameList[nSize] = aName;
361 officecfg::Office::Common::UserColors::CustomColor::set(aCustomColorList, batch);
362 officecfg::Office::Common::UserColors::CustomColorName::set(aCustomColorNameList, batch);
363 batch->commit();
364 sal_uInt16 nId = m_xValSetColorList->GetItemId(nSize - 1);
365 m_xValSetColorList->InsertItem( nId + 1 , aCurrentColor, aName );
366 m_xValSetColorList->SelectItem( nId + 1 );
367 m_xBtnDelete->set_sensitive(false);
368 ImpColorCountChanged();
371 UpdateModified();
374 IMPL_LINK_NOARG(SvxColorTabPage, ClickWorkOnHdl_Impl, weld::Button&, void)
376 SvColorDialog aColorDlg;
378 aColorDlg.SetColor (aCurrentColor);
379 aColorDlg.SetMode( svtools::ColorPickerMode::Modify );
381 if (aColorDlg.Execute(GetFrameWeld()) == RET_OK)
383 Color aPreviewColor = aColorDlg.GetColor();
384 aCurrentColor = aPreviewColor;
385 UpdateColorValues( false );
386 // fill ItemSet and pass it on to XOut
387 rXFSet.Put( XFillColorItem( OUString(), aPreviewColor ) );
388 //m_aCtlPreviewOld.SetAttributes( aXFillAttr );
389 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
391 m_aCtlPreviewNew.Invalidate();
395 IMPL_LINK_NOARG(SvxColorTabPage, ClickDeleteHdl_Impl, weld::Button&, void)
397 sal_uInt16 nId = m_xValSetColorList->GetSelectedItemId();
398 size_t nPos = m_xValSetColorList->GetSelectItemPos();
399 if (m_xSelectPalette->get_active() == 0 && nPos != VALUESET_ITEM_NOTFOUND )
401 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create(m_context));
402 css::uno::Sequence< sal_Int32 > aCustomColorList(officecfg::Office::Common::UserColors::CustomColor::get());
403 css::uno::Sequence< OUString > aCustomColorNameList(officecfg::Office::Common::UserColors::CustomColorName::get());
404 sal_Int32 nSize = aCustomColorList.getLength() - 1;
405 for(sal_Int32 nIndex = static_cast<sal_Int32>(nPos);nIndex < nSize;nIndex++)
407 aCustomColorList[nIndex] = aCustomColorList[nIndex+1];
408 aCustomColorNameList[nIndex] = aCustomColorNameList[nIndex+1];
410 aCustomColorList.realloc(nSize);
411 aCustomColorNameList.realloc(nSize);
412 officecfg::Office::Common::UserColors::CustomColor::set(aCustomColorList, batch);
413 officecfg::Office::Common::UserColors::CustomColorName::set(aCustomColorNameList, batch);
414 batch->commit();
415 m_xValSetColorList->RemoveItem(nId);
416 if (m_xValSetColorList->GetItemCount() != 0)
418 nId = m_xValSetColorList->GetItemId(0);
419 m_xValSetColorList->SelectItem(nId);
420 SelectValSetHdl_Impl(m_xValSetColorList.get());
422 else
423 m_xBtnDelete->set_sensitive(false);
427 IMPL_LINK_NOARG(SvxColorTabPage, SelectPaletteLBHdl, weld::ComboBox&, void)
429 m_xValSetColorList->Clear();
430 sal_Int32 nPos = m_xSelectPalette->get_active();
431 maPaletteManager.SetPalette( nPos );
432 maPaletteManager.ReloadColorSet(*m_xValSetColorList);
434 if(nPos != maPaletteManager.GetPaletteCount() - 1 && nPos != 0)
436 XColorListRef pList = XPropertyList::AsColorList(
437 XPropertyList::CreatePropertyListFromURL(
438 XPropertyListType::Color, maPaletteManager.GetSelectedPalettePath()));
439 pList->SetName(maPaletteManager.GetPaletteName());
440 if(pList->Load())
442 SfxOkDialogController* pController = GetDialogController();
443 SvxAreaTabDialog* pArea = dynamic_cast<SvxAreaTabDialog*>(pController);
444 SvxLineTabDialog* pLine = dynamic_cast<SvxLineTabDialog*>(pController);
445 pColorList = pList;
446 if( pArea )
447 pArea->SetNewColorList(pList);
448 else if( pLine )
449 pLine->SetNewColorList(pList);
450 else
451 SetColorList(pList);
452 *pnColorListState |= ChangeType::CHANGED;
453 *pnColorListState &= ~ChangeType::MODIFIED;
456 if (nPos != 0)
457 m_xBtnDelete->set_sensitive(false);
459 m_xValSetColorList->Resize();
462 IMPL_LINK(SvxColorTabPage, SelectValSetHdl_Impl, SvtValueSet*, pValSet, void)
464 sal_Int32 nPos = pValSet->GetSelectedItemId();
465 if( nPos != 0 )
467 Color aColor = pValSet->GetItemColor( nPos );
469 rXFSet.Put( XFillColorItem( OUString(), aColor ) );
470 m_aCtlPreviewNew.SetAttributes( aXFillAttr.GetItemSet() );
471 m_aCtlPreviewNew.Invalidate();
472 ChangeColor(aColor, false);
474 if (pValSet == m_xValSetColorList.get())
476 m_xValSetRecentList->SetNoSelection();
477 if (m_xSelectPalette->get_active() == 0 && m_xValSetColorList->GetSelectedItemId() != 0)
478 m_xBtnDelete->set_sensitive(true);
479 else
480 m_xBtnDelete->set_sensitive(false);
482 if (pValSet == m_xValSetRecentList.get())
484 m_xValSetColorList->SetNoSelection();
485 m_xBtnDelete->set_sensitive(false);
490 void SvxColorTabPage::ConvertColorValues (Color& rColor, ColorModel eModell)
492 switch (eModell)
494 case ColorModel::RGB:
496 CmykToRgb_Impl (rColor, static_cast<sal_uInt16>(rColor.GetTransparency()) );
497 rColor.SetTransparency (sal_uInt8(0));
499 break;
501 case ColorModel::CMYK:
503 sal_uInt16 nK;
504 RgbToCmyk_Impl (rColor, nK );
505 rColor.SetTransparency (static_cast<sal_uInt8>(nK));
507 break;
511 IMPL_LINK_NOARG(SvxColorTabPage, SelectColorModeHdl_Impl, weld::ToggleButton&, void)
513 if (m_xRbRGB->get_active())
514 eCM = ColorModel::RGB;
515 else if (m_xRbCMYK->get_active())
516 eCM = ColorModel::CMYK;
517 ChangeColorModel();
518 UpdateColorValues();
521 void SvxColorTabPage::ChangeColor(const Color &rNewColor, bool bUpdatePreset )
523 aPreviousColor = rNewColor;
524 aCurrentColor = rNewColor;
525 UpdateColorValues( bUpdatePreset );
526 // fill ItemSet and pass it on to XOut
527 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
528 m_aCtlPreviewNew.SetAttributes(aXFillAttr.GetItemSet());
529 m_aCtlPreviewNew.Invalidate();
532 void SvxColorTabPage::SetColorModel( ColorModel eModel )
534 if (eModel == ColorModel::RGB)
535 m_xRbRGB->set_active(true);
536 else if (eModel == ColorModel::CMYK)
537 m_xRbCMYK->set_active(true);
540 void SvxColorTabPage::ChangeColorModel()
542 switch( eCM )
544 case ColorModel::RGB:
546 m_xRGBcustom->show();
547 m_xRGBpreset->show();
548 m_xCMYKcustom->hide();
549 m_xCMYKpreset->hide();
551 break;
553 case ColorModel::CMYK:
555 m_xCMYKcustom->show();
556 m_xCMYKpreset->show();
557 m_xRGBcustom->hide();
558 m_xRGBpreset->hide();
560 break;
564 void SvxColorTabPage::UpdateColorValues( bool bUpdatePreset )
566 if (eCM != ColorModel::RGB)
568 ConvertColorValues (aPreviousColor, eCM );
569 ConvertColorValues (aCurrentColor, eCM);
571 m_xCcustom->set_value( ColorToPercent_Impl( aCurrentColor.GetRed() ), FieldUnit::PERCENT );
572 m_xMcustom->set_value( ColorToPercent_Impl( aCurrentColor.GetBlue() ), FieldUnit::PERCENT );
573 m_xYcustom->set_value( ColorToPercent_Impl( aCurrentColor.GetGreen() ), FieldUnit::PERCENT );
574 m_xKcustom->set_value( ColorToPercent_Impl( aCurrentColor.GetTransparency() ), FieldUnit::PERCENT );
576 if( bUpdatePreset )
578 m_xCpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(aPreviousColor.GetRed()),
579 Application::GetSettings().GetUILanguageTag()));
580 m_xMpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(aPreviousColor.GetBlue()),
581 Application::GetSettings().GetUILanguageTag()));
582 m_xYpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(aPreviousColor.GetGreen()),
583 Application::GetSettings().GetUILanguageTag()));
584 m_xKpreset->set_text(unicode::formatPercent(ColorToPercent_Impl(aPreviousColor.GetTransparency()),
585 Application::GetSettings().GetUILanguageTag()));
588 ConvertColorValues (aPreviousColor, ColorModel::RGB);
589 ConvertColorValues (aCurrentColor, ColorModel::RGB);
591 else
593 m_xRcustom->set_value( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
594 m_xGcustom->set_value( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
595 m_xBcustom->set_value( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
596 m_xHexcustom->SetColor( aCurrentColor );
598 if( bUpdatePreset )
600 m_xRpreset->set_text(OUString::number(ColorToPercent_Impl(aPreviousColor.GetRed())));
601 m_xGpreset->set_text(OUString::number(ColorToPercent_Impl(aPreviousColor.GetGreen())));
602 m_xBpreset->set_text(OUString::number(ColorToPercent_Impl(aPreviousColor.GetBlue())));
603 m_xHexpreset->SetColor( aPreviousColor );
608 sal_Int32 SvxColorTabPage::FindInCustomColors(OUString const & aColorName)
610 css::uno::Sequence< OUString > aCustomColorNameList(officecfg::Office::Common::UserColors::CustomColorName::get());
611 long nCount = aCustomColorNameList.getLength();
612 bool bValidColorName = true;
613 sal_Int32 nPos = LISTBOX_ENTRY_NOTFOUND;
615 for(long i = 0;i < nCount && bValidColorName;i++)
617 if(aColorName == aCustomColorNameList[i])
619 nPos = i;
620 bValidColorName = false;
623 return nPos;
626 sal_Int32 SvxColorTabPage::FindInPalette( const Color& rColor )
628 sal_Int32 nPos = pColorList->GetIndexOfColor( rColor );
630 return ( nPos == -1) ? LISTBOX_ENTRY_NOTFOUND : nPos;
633 // A RGB value is converted to a CMYK value - not in an ideal way as
634 // R is converted into C, G into M and B into Y. The K value is held in an
635 // extra variable. For further color models one should develop own
636 // classes which should contain the respective casts.
638 void SvxColorTabPage::RgbToCmyk_Impl( Color& rColor, sal_uInt16& rK )
640 sal_uInt16 const nColor1 = 255 - rColor.GetRed();
641 sal_uInt16 const nColor2 = 255 - rColor.GetGreen();
642 sal_uInt16 const nColor3 = 255 - rColor.GetBlue();
644 rK = std::min( std::min( nColor1, nColor2 ), nColor3 );
646 rColor.SetRed( sal::static_int_cast< sal_uInt8 >( nColor1 - rK ) );
647 rColor.SetGreen( sal::static_int_cast< sal_uInt8 >( nColor2 - rK ) );
648 rColor.SetBlue( sal::static_int_cast< sal_uInt8 >( nColor3 - rK ) );
652 // reverse case to RgbToCmyk_Impl (see above)
654 void SvxColorTabPage::CmykToRgb_Impl( Color& rColor, const sal_uInt16 nK )
656 long lTemp;
658 lTemp = 255 - ( rColor.GetRed() + nK );
660 if( lTemp < 0 )
661 lTemp = 0;
662 rColor.SetRed( static_cast<sal_uInt8>(lTemp) );
664 lTemp = 255 - ( rColor.GetGreen() + nK );
666 if( lTemp < 0 )
667 lTemp = 0;
668 rColor.SetGreen( static_cast<sal_uInt8>(lTemp) );
670 lTemp = 255 - ( rColor.GetBlue() + nK );
672 if( lTemp < 0 )
673 lTemp = 0;
674 rColor.SetBlue( static_cast<sal_uInt8>(lTemp) );
678 sal_uInt16 SvxColorTabPage::ColorToPercent_Impl( sal_uInt16 nColor )
680 sal_uInt16 nValue = 0;
682 switch (eCM)
684 case ColorModel::RGB :
685 nValue = nColor;
686 break;
688 case ColorModel::CMYK:
689 nValue = static_cast<sal_uInt16>( static_cast<double>(nColor) * 100.0 / 255.0 + 0.5 );
690 break;
693 return nValue;
697 sal_uInt16 SvxColorTabPage::PercentToColor_Impl( sal_uInt16 nPercent )
699 sal_uInt16 nValue = 0;
701 switch (eCM)
703 case ColorModel::RGB :
704 nValue = nPercent;
705 break;
707 case ColorModel::CMYK:
708 nValue = static_cast<sal_uInt16>( static_cast<double>(nPercent) * 255.0 / 100.0 + 0.5 );
709 break;
712 return nValue;
716 void SvxColorTabPage::FillUserData()
718 // the color model is saved in the Ini-file
719 SetUserData( OUString::number( static_cast<int>(eCM) ) );
723 void SvxColorTabPage::SetPropertyList( XPropertyListType t, const XPropertyListRef &xRef )
725 OSL_ASSERT( t == XPropertyListType::Color );
726 pColorList = XColorListRef( static_cast<XColorList *>(xRef.get() ) );
729 void SvxColorTabPage::SetColorList( const XColorListRef& pColList )
731 SetPropertyList( XPropertyListType::Color, XPropertyListRef( ( pColList.get() ) ) );
734 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */