bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / tabpages / tpcolor.cxx
blob94231c5f0f32d7a3b2e61950f3e43936f8ba21ae
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 <tools/urlobj.hxx>
21 #include <unotools/pathoptions.hxx>
22 #include <sfx2/app.hxx>
23 #include <sfx2/module.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <sfx2/viewsh.hxx>
26 #include <sfx2/viewfrm.hxx>
27 #include <sfx2/dispatch.hxx>
28 #include <svtools/colrdlg.hxx>
29 #include <vcl/msgbox.hxx>
30 #include <sfx2/filedlghelper.hxx>
31 #include <svx/ofaitem.hxx>
32 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
34 #include <cuires.hrc>
35 #include "helpid.hrc"
36 #include "svx/xattr.hxx"
37 #include <svx/xpool.hxx>
38 #include <svx/xtable.hxx>
39 #include "svx/drawitem.hxx"
40 #include "cuitabarea.hxx"
41 #include "defdlgname.hxx"
42 #include "dlgname.hxx"
43 #include <svx/svxdlg.hxx>
44 #include <dialmgr.hxx>
45 #include <cuitabline.hxx>
46 #include <svx/dialmgr.hxx>
47 #include <svx/dialogs.hrc>
48 #include <boost/scoped_ptr.hpp>
50 using namespace com::sun::star;
52 XPropertyListRef SvxColorTabPage::GetList()
54 SvxAreaTabDialog* pArea = dynamic_cast< SvxAreaTabDialog* >( mpTopDlg.get() );
55 SvxLineTabDialog* pLine = dynamic_cast< SvxLineTabDialog* >( mpTopDlg.get() );
57 XColorListRef pList;
58 if( pArea )
59 pList = pArea->GetNewColorList();
60 if( pLine )
61 pList = pLine->GetNewColorList();
63 if( !pList.is() ) {
64 if( pArea )
65 pList = pArea->GetColorList();
66 if( pLine )
67 pList = pLine->GetColorList();
70 // URGH - abstract this nicely ... for re-using SvxLoadSaveEmbed
71 if( !pList.is() ) {
72 pList = GetColorList();
75 return XPropertyListRef( static_cast< XPropertyList * >( pList.get() ) );
78 void SvxColorTabPage::SetEmbed( bool bEmbed )
80 XPropertyListRef pList = GetList();
81 if( pList.is() )
82 pList->SetEmbedInDocument( bEmbed );
83 m_pBoxEmbed->Check( bEmbed );
86 bool SvxColorTabPage::GetEmbed()
88 XPropertyListRef pList = GetList();
89 return pList.is() && pList->IsEmbedInDocument();
92 IMPL_LINK_NOARG(SvxColorTabPage, EmbedToggleHdl_Impl)
94 SetEmbed( m_pBoxEmbed->IsChecked() );
95 return 0;
98 void SvxColorTabPage::UpdateTableName()
100 // Truncate the name if necessary ...
101 OUString aString( CUI_RES( RID_SVXSTR_TABLE ) );
102 aString += ": ";
104 XPropertyListRef pList = GetList();
105 if( !pList.is() )
106 return;
108 INetURLObject aURL( pList->GetPath() );
109 aURL.Append( pList->GetName() );
111 if ( aURL.getBase().getLength() > 18 )
113 aString += aURL.getBase().copy( 0, 15 );
114 aString += "...";
116 else
117 aString += aURL.getBase();
119 m_pTableName->SetText( aString );
124 IMPL_LINK_NOARG(SvxColorTabPage, ClickLoadHdl_Impl)
126 sal_uInt16 nReturn = RET_YES;
127 bool bLoaded = false;
129 if( IsModified() && GetList()->Count() > 0 )
131 nReturn = ScopedVclPtrInstance<MessageDialog>::Create( GetParentDialog()
132 ,"AskSaveList"
133 ,"cui/ui/querysavelistdialog.ui")->Execute();
135 if ( nReturn == RET_YES )
136 GetList()->Save();
139 if ( nReturn != RET_CANCEL )
141 ::sfx2::FileDialogHelper aDlg( css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
142 OUString aStrFilterType( XPropertyList::GetDefaultExtFilter( meType ) );
143 aDlg.AddFilter( aStrFilterType, aStrFilterType );
145 INetURLObject aFile( SvtPathOptions().GetPalettePath() );
146 aDlg.SetDisplayDirectory( aFile.GetMainURL( INetURLObject::NO_DECODE ) );
148 if ( aDlg.Execute() == ERRCODE_NONE )
150 XColorListRef pList = XPropertyList::AsColorList(
151 XPropertyList::CreatePropertyListFromURL(
152 meType, aDlg.GetPath()));
153 if( pList->Load() )
155 // check whether the table may be deleted:
156 SvxAreaTabDialog* pArea = dynamic_cast< SvxAreaTabDialog* >( mpTopDlg.get() );
157 SvxLineTabDialog* pLine = dynamic_cast< SvxLineTabDialog* >( mpTopDlg.get() );
159 // FIXME: want to have a generic set and get method by type ...
160 if( pArea )
161 pArea->SetNewColorList(pList);
162 else if( pLine )
163 pLine->SetNewColorList(pList);
164 else
165 SetColorList(pList);
167 bLoaded = true;
168 UpdateTableName();
170 AddState( ChangeType::CHANGED );
171 SetModified( false );
172 SetEmbed( true );
174 else
176 ScopedVclPtrInstance<MessageDialog>::Create( mpTopDlg
177 ,"NoLoadedFileDialog"
178 ,"cui/ui/querynoloadedfiledialog.ui")->Execute();
182 Update( bLoaded );
184 return 0;
187 void SvxColorTabPage::EnableSave( bool bCanSave )
189 if ( bCanSave )
190 m_pBtnSave->Enable();
191 else
192 m_pBtnSave->Disable();
197 IMPL_LINK_NOARG(SvxColorTabPage, ClickSaveHdl_Impl)
199 ::sfx2::FileDialogHelper aDlg(
200 css::ui::dialogs::TemplateDescription::FILESAVE_SIMPLE, 0 );
202 OUString aStrFilterType( XPropertyList::GetDefaultExtFilter( meType ) );
203 aDlg.AddFilter( aStrFilterType, aStrFilterType );
205 INetURLObject aFile( SvtPathOptions().GetPalettePath() );
206 DBG_ASSERT( aFile.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
208 XPropertyListRef pList = GetList();
210 if( !pList->GetName().isEmpty() )
212 aFile.Append( pList->GetName() );
214 if( aFile.getExtension().isEmpty() )
215 aFile.SetExtension( XPropertyList::GetDefaultExt( meType ) );
218 aDlg.SetDisplayDirectory( aFile.GetMainURL( INetURLObject::NO_DECODE ) );
219 if ( aDlg.Execute() == ERRCODE_NONE )
221 INetURLObject aURL( aDlg.GetPath() );
222 INetURLObject aPathURL( aURL );
224 aPathURL.removeSegment();
225 aPathURL.removeFinalSlash();
227 pList->SetName( aURL.getName() );
228 pList->SetPath( aPathURL.GetMainURL( INetURLObject::NO_DECODE ) );
230 if( pList->Save() )
232 UpdateTableName();
233 AddState( ChangeType::SAVED );
234 SetModified( false );
236 else
238 ScopedVclPtrInstance<MessageDialog>::Create( mpTopDlg
239 ,"NoSaveFileDialog"
240 ,"cui/ui/querynosavefiledialog.ui")->Execute();
243 return 0;
246 void SvxColorTabPage::Update(bool bLoaded)
248 pColorList = XColorListRef( static_cast<XColorList *>( GetList().get() ) );
250 if (bLoaded)
252 m_pLbColor->Clear();
253 m_pValSetColorList->Clear();
254 Construct();
255 Reset( &rOutAttrs );
257 if( m_pLbColor->GetSelectEntryPos() == LISTBOX_ENTRY_NOTFOUND )
258 m_pLbColor->SelectEntryPos( 0 );
259 else
260 m_pLbColor->SelectEntryPos( m_pLbColor->GetSelectEntryPos() );
262 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
263 if( nPos != LISTBOX_ENTRY_NOTFOUND )
265 XColorEntry* pEntry = pColorList->GetColor( nPos );
266 ChangeColor(pEntry->GetColor());
268 SelectColorLBHdl_Impl( this );
271 UpdateModified();
274 // FIXME: you have to hate yourself for this - all this
275 // horrible and broadly unused pointer based coupling
276 // needs to die. cf SetupForViewFrame
277 #define COLORPAGE_UNKNOWN ((sal_uInt16)0xFFFF)
279 struct SvxColorTabPageShadow
281 sal_uInt16 nUnknownType;
282 sal_Int32 nUnknownPos;
283 bool bIsAreaTP;
284 ChangeType nChangeType;
285 SvxColorTabPageShadow()
286 : nUnknownType( COLORPAGE_UNKNOWN )
287 , nUnknownPos( LISTBOX_ENTRY_NOTFOUND )
288 , bIsAreaTP( false )
289 , nChangeType( ChangeType::NONE )
296 SvxColorTabPage::SvxColorTabPage(vcl::Window* pParent, const SfxItemSet& rInAttrs)
297 : SfxTabPage(pParent, "ColorPage", "cui/ui/colorpage.ui", &rInAttrs)
298 , meType( XCOLOR_LIST )
299 , mpTopDlg( GetParentDialog() )
300 , pShadow ( new SvxColorTabPageShadow() )
301 , rOutAttrs ( rInAttrs )
302 // All the horrific pointers we store and should not
303 , pnColorListState( 0 )
304 , pPageType( NULL )
305 , nDlgType( 0 )
306 , pPos( NULL )
307 , pbAreaTP( NULL )
308 , aXFStyleItem( drawing::FillStyle_SOLID )
309 , aXFillColorItem( OUString(), Color( COL_BLACK ) )
310 , aXFillAttr( static_cast<XOutdevItemPool*>( rInAttrs.GetPool() ))
311 , rXFSet( aXFillAttr.GetItemSet() )
312 , eCM( CM_RGB )
314 get(m_pBoxEmbed, "embed");
315 get(m_pBtnLoad, "load");
316 get(m_pBtnSave, "save");
317 get(m_pTableName, "colortableft");
319 get(m_pEdtName, "name");
320 get(m_pLbColor, "colorlb");
321 get(m_pValSetColorList, "colorset");
322 Size aSize = LogicToPixel(Size(94 , 117), MAP_APPFONT);
323 m_pValSetColorList->set_width_request(aSize.Width());
324 m_pValSetColorList->set_height_request(aSize.Height());
325 get(m_pCtlPreviewOld, "oldpreview");
326 get(m_pCtlPreviewNew, "newpreview");
327 aSize = LogicToPixel(Size(34 , 25), MAP_APPFONT);
328 m_pCtlPreviewOld->set_width_request(aSize.Width());
329 m_pCtlPreviewOld->set_height_request(aSize.Height());
330 m_pCtlPreviewNew->set_width_request(aSize.Width());
331 m_pCtlPreviewNew->set_height_request(aSize.Height());
332 get(m_pLbColorModel, "modellb");
333 get(m_pRGB, "rgb");
334 get(m_pR, "R");
335 get(m_pG, "G");
336 get(m_pB, "B");
337 get(m_pCMYK, "cmyk");
338 get(m_pC, "C");
339 get(m_pY, "Y");
340 get(m_pM, "M");
341 get(m_pK, "K");
342 get(m_pBtnAdd, "add");
343 get(m_pBtnModify, "modify");
344 get(m_pBtnWorkOn, "edit");
345 get(m_pBtnDelete, "delete");
347 m_pBoxEmbed->SetToggleHdl( LINK( this, SvxColorTabPage, EmbedToggleHdl_Impl ) );
349 m_pBtnLoad->SetClickHdl( LINK( this, SvxColorTabPage, ClickLoadHdl_Impl ) );
350 m_pBtnSave->SetClickHdl( LINK( this, SvxColorTabPage, ClickSaveHdl_Impl ) );
352 SetEmbed( GetEmbed() );
353 UpdateTableName();
355 // this page needs ExchangeSupport
356 SetExchangeSupport();
358 // setting the output device
359 rXFSet.Put( aXFStyleItem );
360 rXFSet.Put( aXFillColorItem );
361 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
362 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
364 // set handler
365 m_pLbColor->SetSelectHdl(
366 LINK( this, SvxColorTabPage, SelectColorLBHdl_Impl ) );
367 m_pValSetColorList->SetSelectHdl(
368 LINK( this, SvxColorTabPage, SelectValSetHdl_Impl ) );
369 m_pLbColorModel->SetSelectHdl(
370 LINK( this, SvxColorTabPage, SelectColorModelHdl_Impl ) );
372 Link<> aLink = LINK( this, SvxColorTabPage, ModifiedHdl_Impl );
373 m_pR->SetModifyHdl( aLink );
374 m_pG->SetModifyHdl( aLink );
375 m_pB->SetModifyHdl( aLink );
376 m_pC->SetModifyHdl( aLink );
377 m_pY->SetModifyHdl( aLink );
378 m_pM->SetModifyHdl( aLink );
379 m_pK->SetModifyHdl( aLink );
381 m_pBtnAdd->SetClickHdl( LINK( this, SvxColorTabPage, ClickAddHdl_Impl ) );
382 m_pBtnModify->SetClickHdl( LINK( this, SvxColorTabPage, ClickModifyHdl_Impl ) );
383 m_pBtnWorkOn->SetClickHdl( LINK( this, SvxColorTabPage, ClickWorkOnHdl_Impl ) );
384 m_pBtnDelete->SetClickHdl( LINK( this, SvxColorTabPage, ClickDeleteHdl_Impl ) );
386 // ValueSet
387 m_pValSetColorList->SetStyle( m_pValSetColorList->GetStyle() | WB_ITEMBORDER );
388 m_pValSetColorList->Show();
391 SvxColorTabPage::~SvxColorTabPage()
393 disposeOnce();
396 void SvxColorTabPage::dispose()
398 delete pShadow;
399 pShadow = NULL;
400 mpTopDlg.clear();
401 m_pBoxEmbed.clear();
402 m_pBtnLoad.clear();
403 m_pBtnSave.clear();
404 m_pTableName.clear();
405 m_pEdtName.clear();
406 m_pLbColor.clear();
407 m_pValSetColorList.clear();
408 m_pCtlPreviewOld.clear();
409 m_pCtlPreviewNew.clear();
410 m_pLbColorModel.clear();
411 m_pRGB.clear();
412 m_pR.clear();
413 m_pG.clear();
414 m_pB.clear();
415 m_pCMYK.clear();
416 m_pC.clear();
417 m_pY.clear();
418 m_pM.clear();
419 m_pK.clear();
420 m_pBtnAdd.clear();
421 m_pBtnModify.clear();
422 m_pBtnWorkOn.clear();
423 m_pBtnDelete.clear();
424 SfxTabPage::dispose();
427 void SvxColorTabPage::ImpColorCountChanged()
429 if (!pColorList.is())
430 return;
431 m_pValSetColorList->SetColCount(SvxColorValueSet::getColumnCount());
436 void SvxColorTabPage::Construct()
438 if (pColorList.is())
440 m_pLbColor->Fill(pColorList);
441 m_pValSetColorList->addEntriesForXColorList(*pColorList);
442 ImpColorCountChanged();
443 UpdateTableName();
447 void SvxColorTabPage::ActivatePage( const SfxItemSet& )
449 if( nDlgType == 0 ) // area dialog
451 *pbAreaTP = false;
453 if( pColorList.is() )
455 if( *pPageType == PT_COLOR && *pPos != LISTBOX_ENTRY_NOTFOUND )
457 m_pLbColor->SelectEntryPos( *pPos );
458 m_pValSetColorList->SelectItem( m_pLbColor->GetSelectEntryPos() + 1 );
459 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
460 XColorEntry* pEntry = pColorList->GetColor( *pPos );
461 ChangeColor(pEntry->GetColor());
463 else if( *pPageType == PT_COLOR && *pPos == LISTBOX_ENTRY_NOTFOUND )
465 const SfxPoolItem* pPoolItem = NULL;
466 if( SfxItemState::SET == rOutAttrs.GetItemState( GetWhich( XATTR_FILLCOLOR ), true, &pPoolItem ) )
468 m_pLbColorModel->SelectEntryPos( CM_RGB );
470 ChangeColor(static_cast<const XFillColorItem*>(pPoolItem)->GetColorValue());
472 m_pEdtName->SetText( static_cast<const XFillColorItem*>( pPoolItem )->GetName() );
474 m_pR->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
475 m_pG->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
476 m_pB->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
478 // fill ItemSet and pass it on to XOut
479 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
480 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
481 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
483 m_pCtlPreviewNew->Invalidate();
484 m_pCtlPreviewOld->Invalidate();
488 // so that the possibly changed color is discarded
489 SelectColorLBHdl_Impl( this );
491 *pPageType = PT_COLOR;
492 *pPos = LISTBOX_ENTRY_NOTFOUND;
495 else
496 m_pBoxEmbed->Hide();
499 SfxTabPage::sfxpg SvxColorTabPage::DeactivatePage( SfxItemSet* _pSet )
501 if ( CheckChanges_Impl() == -1L )
502 return KEEP_PAGE;
504 if( _pSet )
505 FillItemSet( _pSet );
507 return LEAVE_PAGE;
512 long SvxColorTabPage::CheckChanges_Impl()
514 // used to NOT lose changes
515 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
516 if( nPos != LISTBOX_ENTRY_NOTFOUND )
518 Color aColor = pColorList->GetColor( nPos )->GetColor();
519 OUString aString = m_pLbColor->GetSelectEntry();
521 // aNewColor, because COL_USER != COL_something, even if RGB values are the same
522 // Color aNewColor( aColor.GetRed(), aColor.GetGreen(), aColor.GetBlue() );
524 if( ColorToPercent_Impl( aCurrentColor.GetRed() ) != ColorToPercent_Impl( aColor.GetRed() ) ||
525 ColorToPercent_Impl( aCurrentColor.GetGreen() ) != ColorToPercent_Impl( aColor.GetGreen() ) ||
526 ColorToPercent_Impl( aCurrentColor.GetBlue() ) != ColorToPercent_Impl( aColor.GetBlue() ) ||
527 aString != m_pEdtName->GetText() )
529 ResMgr& rMgr = CUI_MGR();
530 Image aWarningBoxImage = WarningBox::GetStandardImage();
531 ScopedVclPtrInstance<SvxMessDialog> aMessDlg( GetParentDialog(),
532 SVX_RESSTR( RID_SVXSTR_COLOR ),
533 ResId( RID_SVXSTR_ASK_CHANGE_COLOR, rMgr ),
534 &aWarningBoxImage );
535 aMessDlg->SetButtonText( MESS_BTN_1,
536 ResId( RID_SVXSTR_CHANGE, rMgr ) );
537 aMessDlg->SetButtonText( MESS_BTN_2,
538 ResId( RID_SVXSTR_ADD, rMgr ) );
540 short nRet = aMessDlg->Execute();
542 switch( nRet )
544 case RET_BTN_1:
546 ClickModifyHdl_Impl( this );
547 aColor = pColorList->GetColor( nPos )->GetColor();
549 break;
551 case RET_BTN_2:
553 ClickAddHdl_Impl( this );
554 nPos = m_pLbColor->GetSelectEntryPos();
555 aColor = pColorList->GetColor( nPos )->GetColor();
557 break;
559 case RET_CANCEL:
560 break;
564 if( nDlgType == 0 ) // area dialog
566 nPos = m_pLbColor->GetSelectEntryPos();
567 if( nPos != LISTBOX_ENTRY_NOTFOUND )
569 *pPos = nPos;
572 return 0;
577 bool SvxColorTabPage::FillItemSet( SfxItemSet* rSet )
579 if( ( nDlgType != 0 ) ||
580 ( *pPageType == PT_COLOR && !*pbAreaTP ) )
582 OUString aString;
583 Color aColor;
585 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
586 if( nPos != LISTBOX_ENTRY_NOTFOUND )
588 aColor = pColorList->GetColor( nPos )->GetColor();
589 aString = m_pLbColor->GetSelectEntry();
591 else
593 aColor.SetColor (aCurrentColor.GetColor());
595 rSet->Put( XFillColorItem( aString, aColor ) );
596 rSet->Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
599 return true;
602 void SvxColorTabPage::UpdateModified()
604 bool bEnable = pColorList.is() && pColorList->Count();
605 m_pBtnModify->Enable( bEnable );
606 m_pBtnWorkOn->Enable( bEnable );
607 m_pBtnDelete->Enable( bEnable );
608 EnableSave( bEnable );
611 void SvxColorTabPage::Reset( const SfxItemSet* rSet )
613 SfxItemState nState = rSet->GetItemState( XATTR_FILLCOLOR );
615 Color aNewColor;
617 if ( nState >= SfxItemState::DEFAULT )
619 XFillColorItem aColorItem( static_cast<const XFillColorItem&>(rSet->Get( XATTR_FILLCOLOR )) );
620 aNewColor = aColorItem.GetColorValue();
621 m_pLbColor->SelectEntry(aNewColor);
622 m_pValSetColorList->SelectItem( m_pLbColor->GetSelectEntryPos() + 1 );
623 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
626 // set color model
627 OUString aStr = GetUserData();
628 m_pLbColorModel->SelectEntryPos( aStr.toInt32() );
630 ChangeColor(aNewColor);
631 SelectColorModelHdl_Impl( this );
633 m_pCtlPreviewOld->Invalidate();
634 UpdateModified();
639 VclPtr<SfxTabPage> SvxColorTabPage::Create( vcl::Window* pWindow,
640 const SfxItemSet* rOutAttrs )
642 return VclPtr<SvxColorTabPage>::Create( pWindow, *rOutAttrs );
645 // is called when the content of the MtrFields is changed for color values
646 IMPL_LINK_NOARG(SvxColorTabPage, ModifiedHdl_Impl)
648 if (eCM == CM_RGB)
650 // read current MtrFields, if cmyk, then k-value as transparency
651 aCurrentColor.SetColor ( Color( (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pR->GetValue() ),
652 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pG->GetValue() ),
653 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pB->GetValue() ) ).GetColor() );
655 else
657 // read current MtrFields, if cmyk, then k-value as transparency
658 aCurrentColor.SetColor ( Color( (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pK->GetValue() ),
659 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pC->GetValue() ),
660 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pY->GetValue() ),
661 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pM->GetValue() ) ).GetColor() );
662 ConvertColorValues (aCurrentColor, CM_RGB);
665 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
666 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
668 m_pCtlPreviewNew->Invalidate();
670 return 0;
675 IMPL_LINK_NOARG(SvxColorTabPage, ClickAddHdl_Impl)
677 OUString aNewName( SVX_RES( RID_SVXSTR_COLOR ) );
678 OUString aDesc( CUI_RES( RID_SVXSTR_DESC_COLOR ) );
679 OUString aName( m_pEdtName->GetText() );
681 long nCount = pColorList->Count();
682 long j = 1;
684 // check if name is already existing
685 while (true)
687 bool bDifferent = true;
689 for( long i = 0; i < nCount && bDifferent; i++ )
690 if ( aName == pColorList->GetColor( i )->GetName() )
691 bDifferent = false;
693 if (bDifferent)
694 break;
696 aName = aNewName;
697 aName += " ";
698 aName += OUString::number( j++ );
701 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
702 boost::scoped_ptr<AbstractSvxNameDialog> pDlg(pFact->CreateSvxNameDialog( GetParentDialog(), aName, aDesc ));
703 ScopedVclPtr<MessageDialog> pWarnBox;
704 sal_uInt16 nError = 1;
706 while (pDlg->Execute() == RET_OK)
708 pDlg->GetName( aName );
710 bool bDifferent = true;
712 for (long i = 0; i < nCount && bDifferent; ++i)
714 if( aName == pColorList->GetColor( i )->GetName() )
715 bDifferent = false;
718 if (bDifferent)
720 nError = 0;
721 break;
724 if( !pWarnBox )
726 pWarnBox.reset(VclPtr<MessageDialog>::Create( GetParentDialog()
727 ,"DuplicateNameDialog"
728 ,"cui/ui/queryduplicatedialog.ui"));
731 if( pWarnBox->Execute() != RET_OK )
732 break;
735 pDlg.reset();
736 pWarnBox.reset();
738 if (!nError)
740 XColorEntry* pEntry = new XColorEntry( aCurrentColor, aName );
742 pColorList->Insert( pEntry, pColorList->Count() );
744 m_pLbColor->Append( *pEntry );
745 m_pValSetColorList->InsertItem( m_pValSetColorList->GetItemCount() + 1, pEntry->GetColor(), pEntry->GetName() );
746 ImpColorCountChanged();
748 m_pLbColor->SelectEntryPos( m_pLbColor->GetEntryCount() - 1 );
750 *pnColorListState |= ChangeType::MODIFIED;
752 SelectColorLBHdl_Impl( this );
755 UpdateModified();
757 return 0;
762 IMPL_LINK_NOARG(SvxColorTabPage, ClickModifyHdl_Impl)
764 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
766 if( nPos != LISTBOX_ENTRY_NOTFOUND )
768 ResMgr& rMgr = CUI_MGR();
769 OUString aDesc( ResId( RID_SVXSTR_DESC_COLOR, rMgr ) );
770 OUString aName( m_pEdtName->GetText() );
771 long nCount = pColorList->Count();
772 bool bDifferent = true;
774 // check if name is already existing
775 for ( long i = 0; i < nCount && bDifferent; i++ )
776 if ( aName == pColorList->GetColor( i )->GetName() && nPos != i )
777 bDifferent = false;
779 // if yes, it is repeated and a new name is demanded
780 if ( !bDifferent )
782 ScopedVclPtrInstance<MessageDialog> aWarningBox( GetParentDialog()
783 ,"DuplicateNameDialog"
784 ,"cui/ui/queryduplicatedialog.ui");
785 aWarningBox->Execute();
787 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
788 boost::scoped_ptr<AbstractSvxNameDialog> pDlg(pFact->CreateSvxNameDialog( GetParentDialog(), aName, aDesc ));
789 bool bLoop = true;
791 while ( !bDifferent && bLoop && pDlg->Execute() == RET_OK )
793 pDlg->GetName( aName );
794 bDifferent = true;
796 for ( long i = 0; i < nCount && bDifferent; i++ )
797 if( aName == pColorList->GetColor( i )->GetName() && nPos != i )
798 bDifferent = false;
800 if( bDifferent )
801 bLoop = false;
802 else
803 aWarningBox->Execute();
807 // if not existing the entry is entered
808 if( bDifferent )
810 // #123497# Need to replace the existing entry with a new one (old returned needs to be deleted)
811 XColorEntry* pEntry = new XColorEntry(aCurrentColor, aName);
812 delete pColorList->Replace(pEntry, nPos);
814 m_pLbColor->Modify( *pEntry, nPos );
815 m_pLbColor->SelectEntryPos( nPos );
817 m_pValSetColorList->SetItemColor( nPos + 1, pEntry->GetColor() );
818 m_pValSetColorList->SetItemText( nPos + 1, pEntry->GetName() );
819 m_pEdtName->SetText( aName );
821 m_pCtlPreviewOld->Invalidate();
823 *pnColorListState |= ChangeType::MODIFIED;
826 return 0;
831 IMPL_LINK_NOARG(SvxColorTabPage, ClickWorkOnHdl_Impl)
833 boost::scoped_ptr<SvColorDialog> pColorDlg(new SvColorDialog( GetParentDialog() ));
835 pColorDlg->SetColor (aCurrentColor);
836 pColorDlg->SetMode( svtools::ColorPickerMode_MODIFY );
838 if( pColorDlg->Execute() == RET_OK )
840 sal_uInt16 nK = 0;
841 Color aPreviewColor = pColorDlg->GetColor();
842 aCurrentColor = aPreviewColor;
843 if (eCM != CM_RGB)
845 ConvertColorValues (aCurrentColor, eCM);
846 m_pC->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
847 m_pY->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
848 m_pM->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
849 m_pK->SetValue( ColorToPercent_Impl( nK ) );
850 ConvertColorValues (aCurrentColor, CM_RGB);
852 else
854 m_pR->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
855 m_pG->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
856 m_pB->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
859 // fill ItemSet and pass it on to XOut
860 rXFSet.Put( XFillColorItem( OUString(), aPreviewColor ) );
861 //m_pCtlPreviewOld->SetAttributes( aXFillAttr );
862 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
864 m_pCtlPreviewNew->Invalidate();
867 return 0;
872 IMPL_LINK_NOARG(SvxColorTabPage, ClickDeleteHdl_Impl)
874 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
876 if( nPos != LISTBOX_ENTRY_NOTFOUND )
878 ScopedVclPtrInstance< MessageDialog > aQueryBox( GetParentDialog(),"AskDelColorDialog","cui/ui/querydeletecolordialog.ui");
880 if( aQueryBox->Execute() == RET_YES )
882 XColorEntry* pEntry = pColorList->Remove( nPos );
883 DBG_ASSERT( pEntry, "ColorEntry not found !" );
884 delete pEntry;
886 // update Listbox and ValueSet
887 m_pLbColor->RemoveEntry( nPos );
888 m_pValSetColorList->Clear();
889 m_pValSetColorList->addEntriesForXColorList(*pColorList);
890 ImpColorCountChanged();
891 //FillValueSet_Impl(*m_pValSetColorList);
893 // positioning
894 m_pLbColor->SelectEntryPos( nPos );
895 SelectColorLBHdl_Impl( this );
897 m_pCtlPreviewOld->Invalidate();
899 *pnColorListState |= ChangeType::MODIFIED;
902 UpdateModified();
904 return 0;
909 IMPL_LINK_NOARG(SvxColorTabPage, SelectColorLBHdl_Impl)
911 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
912 if( nPos != LISTBOX_ENTRY_NOTFOUND )
914 m_pValSetColorList->SelectItem( nPos + 1 );
915 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
917 rXFSet.Put( XFillColorItem( OUString(),
918 m_pLbColor->GetSelectEntryColor() ) );
919 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
920 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
922 m_pCtlPreviewOld->Invalidate();
923 m_pCtlPreviewNew->Invalidate();
925 XColorEntry* pEntry = pColorList->GetColor(nPos);
926 ChangeColor(pEntry->GetColor());
928 return 0;
933 IMPL_LINK_NOARG(SvxColorTabPage, SelectValSetHdl_Impl)
935 sal_Int32 nPos = m_pValSetColorList->GetSelectItemId();
936 if( nPos != LISTBOX_ENTRY_NOTFOUND )
938 m_pLbColor->SelectEntryPos( nPos - 1 );
939 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
941 rXFSet.Put( XFillColorItem( OUString(),
942 m_pLbColor->GetSelectEntryColor() ) );
943 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
944 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
946 m_pCtlPreviewOld->Invalidate();
947 m_pCtlPreviewNew->Invalidate();
949 XColorEntry* pEntry = pColorList->GetColor(nPos-1);
950 ChangeColor(pEntry->GetColor());
952 return 0;
957 void SvxColorTabPage::ConvertColorValues (Color& rColor, ColorModel eModell)
959 switch (eModell)
961 case CM_RGB:
963 CmykToRgb_Impl (rColor, (sal_uInt16)rColor.GetTransparency() );
964 rColor.SetTransparency ((sal_uInt8) 0);
966 break;
968 case CM_CMYK:
970 sal_uInt16 nK;
971 RgbToCmyk_Impl (rColor, nK );
972 rColor.SetTransparency ((sal_uInt8) nK);
974 break;
978 IMPL_LINK_NOARG(SvxColorTabPage, SelectColorModelHdl_Impl)
980 int nPos = m_pLbColorModel->GetSelectEntryPos();
981 if( nPos != LISTBOX_ENTRY_NOTFOUND )
983 eCM = (ColorModel) nPos;
985 switch( eCM )
987 case CM_RGB:
989 m_pRGB->Show();
990 m_pCMYK->Hide();
993 break;
995 case CM_CMYK:
997 m_pCMYK->Show();
998 m_pRGB->Hide();
1000 break;
1003 ChangeColor(aCurrentColor);
1007 return 0;
1012 void SvxColorTabPage::ChangeColor(const Color &rNewColor)
1014 aCurrentColor = rNewColor;
1015 if (eCM != CM_RGB)
1017 ConvertColorValues (aCurrentColor, eCM);
1018 m_pC->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
1019 m_pY->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
1020 m_pM->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
1021 m_pK->SetValue( ColorToPercent_Impl( aCurrentColor.GetTransparency() ) );
1022 ConvertColorValues (aCurrentColor, CM_RGB);
1024 else
1026 m_pR->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
1027 m_pG->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
1028 m_pB->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
1031 // fill ItemSet and pass it on to XOut
1032 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
1033 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
1034 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
1036 m_pCtlPreviewNew->Invalidate();
1041 //void SvxColorTabPage::FillValueSet_Impl( ValueSet& rVs )
1043 // long nCount = pColorList->Count();
1044 // XColorEntry* pColorEntry;
1046 // if( nCount > 104 )
1047 // rVs.SetStyle( rVs.GetStyle() | WB_VSCROLL );
1049 // for( long i = 0; i < nCount; i++ )
1050 // {
1051 // pColorEntry = pColorList->GetColor( i );
1052 // rVs.InsertItem( i + 1, pColorEntry->GetColor(), pColorEntry->GetName() );
1053 // }
1058 // A RGB value is converted to a CMYK value - not in an ideal way as
1059 // R is converted into C, G into M and B into Y. The K value is held in an
1060 // extra variable. For further color models one should develop own
1061 // classes which should contain the respective casts.
1063 void SvxColorTabPage::RgbToCmyk_Impl( Color& rColor, sal_uInt16& rK )
1065 sal_uInt16 const nColor1 = 255 - rColor.GetRed();
1066 sal_uInt16 const nColor2 = 255 - rColor.GetGreen();
1067 sal_uInt16 const nColor3 = 255 - rColor.GetBlue();
1069 rK = std::min( std::min( nColor1, nColor2 ), nColor3 );
1071 rColor.SetRed( sal::static_int_cast< sal_uInt8 >( nColor1 - rK ) );
1072 rColor.SetGreen( sal::static_int_cast< sal_uInt8 >( nColor2 - rK ) );
1073 rColor.SetBlue( sal::static_int_cast< sal_uInt8 >( nColor3 - rK ) );
1078 // reverse case to RgbToCmyk_Impl (see above)
1080 void SvxColorTabPage::CmykToRgb_Impl( Color& rColor, const sal_uInt16 nK )
1082 long lTemp;
1084 lTemp = 255 - ( rColor.GetRed() + nK );
1086 if( lTemp < 0L )
1087 lTemp = 0L;
1088 rColor.SetRed( (sal_uInt8)lTemp );
1090 lTemp = 255 - ( rColor.GetGreen() + nK );
1092 if( lTemp < 0L )
1093 lTemp = 0L;
1094 rColor.SetGreen( (sal_uInt8)lTemp );
1096 lTemp = 255 - ( rColor.GetBlue() + nK );
1098 if( lTemp < 0L )
1099 lTemp = 0L;
1100 rColor.SetBlue( (sal_uInt8)lTemp );
1105 sal_uInt16 SvxColorTabPage::ColorToPercent_Impl( sal_uInt16 nColor )
1107 sal_uInt16 nWert = 0;
1109 switch (eCM)
1111 case CM_RGB :
1112 nWert = nColor;
1113 break;
1115 case CM_CMYK:
1116 nWert = (sal_uInt16) ( (double) nColor * 100.0 / 255.0 + 0.5 );
1117 break;
1120 return nWert;
1125 sal_uInt16 SvxColorTabPage::PercentToColor_Impl( sal_uInt16 nPercent )
1127 sal_uInt16 nWert = 0;
1129 switch (eCM)
1131 case CM_RGB :
1132 nWert = nPercent;
1133 break;
1135 case CM_CMYK:
1136 nWert = (sal_uInt16) ( (double) nPercent * 255.0 / 100.0 + 0.5 );
1137 break;
1140 return nWert;
1145 void SvxColorTabPage::FillUserData()
1147 // the color model is saved in the Ini-file
1148 SetUserData( OUString::number( eCM ) );
1153 void SvxColorTabPage::SetupForViewFrame( SfxViewFrame *pViewFrame )
1155 const OfaRefItem<XColorList> *pPtr = NULL;
1156 if ( pViewFrame != NULL && pViewFrame->GetDispatcher() )
1157 pPtr = static_cast<const OfaRefItem<XColorList> *>(pViewFrame->
1158 GetDispatcher()->Execute( SID_GET_COLORLIST,
1159 SfxCallMode::SYNCHRON ));
1160 pColorList = pPtr ? pPtr->GetValue() : XColorList::GetStdColorList();
1162 SetPageType( &pShadow->nUnknownType );
1163 SetDlgType( COLORPAGE_UNKNOWN );
1164 SetPos( &pShadow->nUnknownPos );
1165 SetAreaTP( &pShadow->bIsAreaTP );
1166 SetColorChgd( &pShadow->nChangeType );
1167 Construct();
1170 void SvxColorTabPage::SaveToViewFrame( SfxViewFrame *pViewFrame )
1172 if( !pColorList.is() )
1173 return;
1175 pColorList->Save();
1177 if( !pViewFrame )
1178 return;
1180 // notify current viewframe that it uses the same color table
1181 if ( !pViewFrame->GetDispatcher() )
1182 return;
1184 const OfaRefItem<XColorList> * pPtr;
1185 pPtr = static_cast<const OfaRefItem<XColorList>*>(pViewFrame->GetDispatcher()->Execute( SID_GET_COLORLIST, SfxCallMode::SYNCHRON ));
1186 if( pPtr )
1188 XColorListRef pReference = pPtr->GetValue();
1190 if( pReference.is() &&
1191 pReference->GetPath() == pColorList->GetPath() &&
1192 pReference->GetName() == pColorList->GetName() )
1193 SfxObjectShell::Current()->PutItem( SvxColorListItem( pColorList,
1194 SID_COLOR_TABLE ) );
1198 void SvxColorTabPage::SetPropertyList( XPropertyListType t, const XPropertyListRef &xRef )
1200 (void) t;
1201 OSL_ASSERT( t == XCOLOR_LIST );
1202 pColorList = XColorListRef( static_cast<XColorList *>(xRef.get() ) );
1205 void SvxColorTabPage::SetColorList( XColorListRef pColList )
1207 SetPropertyList( XCOLOR_LIST, XPropertyListRef( ( pColList.get() ) ) );
1210 XPropertyListRef SvxColorTabPage::GetPropertyList( XPropertyListType t )
1212 (void) t;
1213 OSL_ASSERT( t == XCOLOR_LIST );
1214 return XPropertyListRef( pColorList.get() );
1218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */