Bump version to 4.3-4
[LibreOffice.git] / cui / source / tabpages / tpcolor.cxx
blobc20bb39c921863127046e7a738142061df3b2be8
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/shl.hxx>
21 #include <tools/urlobj.hxx>
22 #include <unotools/pathoptions.hxx>
23 #include <sfx2/app.hxx>
24 #include <sfx2/module.hxx>
25 #include <sfx2/objsh.hxx>
26 #include <sfx2/viewsh.hxx>
27 #include <sfx2/viewfrm.hxx>
28 #include <sfx2/dispatch.hxx>
29 #include <svtools/colrdlg.hxx>
30 #include <vcl/msgbox.hxx>
31 #include <sfx2/filedlghelper.hxx>
32 #include <svx/ofaitem.hxx>
33 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
35 #include <cuires.hrc>
36 #include "helpid.hrc"
37 #include "svx/xattr.hxx"
38 #include <svx/xpool.hxx>
39 #include <svx/xtable.hxx>
40 #include "svx/drawitem.hxx"
41 #include "cuitabarea.hxx"
42 #include "defdlgname.hxx"
43 #include "dlgname.hxx"
44 #include <svx/svxdlg.hxx>
45 #include <dialmgr.hxx>
46 #include <cuitabline.hxx>
47 #include <svx/dialmgr.hxx>
48 #include <svx/dialogs.hrc>
51 XPropertyListRef SvxColorTabPage::GetList()
53 SvxAreaTabDialog* pArea = dynamic_cast< SvxAreaTabDialog* >( mpTopDlg );
54 SvxLineTabDialog* pLine = dynamic_cast< SvxLineTabDialog* >( mpTopDlg );
56 XColorListRef pList;
57 if( pArea )
58 pList = pArea->GetNewColorList();
59 if( pLine )
60 pList = pLine->GetNewColorList();
62 if( !pList.is() ) {
63 if( pArea )
64 pList = pArea->GetColorList();
65 if( pLine )
66 pList = pLine->GetColorList();
69 // URGH - abstract this nicely ... for re-using SvxLoadSaveEmbed
70 if( !pList.is() ) {
71 SvxColorTabPage *pPage = dynamic_cast< SvxColorTabPage *>( this );
72 if( pPage )
73 pList = pPage->GetColorList();
76 return XPropertyListRef( static_cast< XPropertyList * >( pList.get() ) );
79 void SvxColorTabPage::SetEmbed( bool bEmbed )
81 XPropertyListRef pList = GetList();
82 if( pList.is() )
83 pList->SetEmbedInDocument( bEmbed );
84 m_pBoxEmbed->Check( bEmbed );
87 bool SvxColorTabPage::GetEmbed()
89 XPropertyListRef pList = GetList();
90 return pList.is() && pList->IsEmbedInDocument();
93 IMPL_LINK_NOARG(SvxColorTabPage, EmbedToggleHdl_Impl)
95 SetEmbed( m_pBoxEmbed->IsChecked() );
96 return 0;
99 void SvxColorTabPage::HideLoadSaveEmbed()
101 m_pBtnLoad->Hide();
102 m_pBtnSave->Hide();
103 m_pBoxEmbed->Hide();
106 void SvxColorTabPage::UpdateTableName()
108 // Truncate the name if necessary ...
109 OUString aString( CUI_RES( RID_SVXSTR_TABLE ) );
110 aString += ": ";
112 XPropertyListRef pList = GetList();
113 if( !pList.is() )
114 return;
116 INetURLObject aURL( pList->GetPath() );
117 aURL.Append( pList->GetName() );
119 if ( aURL.getBase().getLength() > 18 )
121 aString += aURL.getBase().copy( 0, 15 );
122 aString += "...";
124 else
125 aString += aURL.getBase();
127 m_pTableName->SetText( aString );
132 IMPL_LINK_NOARG(SvxColorTabPage, ClickLoadHdl_Impl)
134 sal_uInt16 nReturn = RET_YES;
135 bool bLoaded = false;
137 if( IsModified() && GetList()->Count() > 0 )
139 nReturn = MessageDialog( GetParentDialog()
140 ,"AskSaveList"
141 ,"cui/ui/querysavelistdialog.ui").Execute();
143 if ( nReturn == RET_YES )
144 GetList()->Save();
147 if ( nReturn != RET_CANCEL )
149 ::sfx2::FileDialogHelper aDlg( css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
150 OUString aStrFilterType( XPropertyList::GetDefaultExtFilter( meType ) );
151 aDlg.AddFilter( aStrFilterType, aStrFilterType );
153 INetURLObject aFile( SvtPathOptions().GetPalettePath() );
154 aDlg.SetDisplayDirectory( aFile.GetMainURL( INetURLObject::NO_DECODE ) );
156 if ( aDlg.Execute() == ERRCODE_NONE )
158 XColorListRef pList = XPropertyList::CreatePropertyListFromURL(
159 meType, aDlg.GetPath() )->AsColorList();
160 if( pList->Load() )
162 // check whether the table may be deleted:
163 SvxAreaTabDialog* pArea = dynamic_cast< SvxAreaTabDialog* >( mpTopDlg );
164 SvxLineTabDialog* pLine = dynamic_cast< SvxLineTabDialog* >( mpTopDlg );
166 // FIXME: want to have a generic set and get method by type ...
167 if( pArea )
168 pArea->SetNewColorList( pList );
169 else if( pLine )
170 pLine->SetNewColorList( pList );
172 bLoaded = true;
173 UpdateTableName();
175 AddState( CT_CHANGED );
176 SetModified( false );
177 SetEmbed( true );
179 else
181 MessageDialog( mpTopDlg
182 ,"NoLoadedFileDialog"
183 ,"cui/ui/querynoloadedfiledialog.ui").Execute();
187 Update( bLoaded );
189 return 0;
192 void SvxColorTabPage::EnableSave( bool bCanSave )
194 if ( bCanSave )
195 m_pBtnSave->Enable();
196 else
197 m_pBtnSave->Disable();
202 IMPL_LINK_NOARG(SvxColorTabPage, ClickSaveHdl_Impl)
204 ::sfx2::FileDialogHelper aDlg(
205 css::ui::dialogs::TemplateDescription::FILESAVE_SIMPLE, 0 );
207 OUString aStrFilterType( XPropertyList::GetDefaultExtFilter( meType ) );
208 aDlg.AddFilter( aStrFilterType, aStrFilterType );
210 INetURLObject aFile( SvtPathOptions().GetPalettePath() );
211 DBG_ASSERT( aFile.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
213 XPropertyListRef pList = GetList();
215 if( !pList->GetName().isEmpty() )
217 aFile.Append( pList->GetName() );
219 if( aFile.getExtension().isEmpty() )
220 aFile.SetExtension( XPropertyList::GetDefaultExt( meType ) );
223 aDlg.SetDisplayDirectory( aFile.GetMainURL( INetURLObject::NO_DECODE ) );
224 if ( aDlg.Execute() == ERRCODE_NONE )
226 INetURLObject aURL( aDlg.GetPath() );
227 INetURLObject aPathURL( aURL );
229 aPathURL.removeSegment();
230 aPathURL.removeFinalSlash();
232 pList->SetName( aURL.getName() );
233 pList->SetPath( aPathURL.GetMainURL( INetURLObject::NO_DECODE ) );
235 if( pList->Save() )
237 UpdateTableName();
238 AddState( CT_SAVED );
239 SetModified( false );
241 else
243 MessageDialog( mpTopDlg
244 ,"NoSaveFileDialog"
245 ,"cui/ui/querynosavefiledialog.ui").Execute();
248 return 0;
251 void SvxColorTabPage::Update(bool bLoaded)
253 pColorList = XColorListRef( static_cast<XColorList *>( GetList().get() ) );
255 if (bLoaded)
257 m_pLbColor->Clear();
258 m_pValSetColorList->Clear();
259 Construct();
260 Reset( rOutAttrs );
262 if( m_pLbColor->GetSelectEntryPos() == LISTBOX_ENTRY_NOTFOUND )
263 m_pLbColor->SelectEntryPos( 0 );
264 else
265 m_pLbColor->SelectEntryPos( m_pLbColor->GetSelectEntryPos() );
267 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
268 if( nPos != LISTBOX_ENTRY_NOTFOUND )
270 XColorEntry* pEntry = pColorList->GetColor( nPos );
271 ChangeColor(pEntry->GetColor());
273 SelectColorLBHdl_Impl( this );
276 UpdateModified();
279 // FIXME: you have to hate yourself for this - all this
280 // horrible and broadly unused pointer based coupling
281 // needs to die. cf SetupForViewFrame
282 #define COLORPAGE_UNKNOWN ((sal_uInt16)0xFFFF)
284 struct SvxColorTabPageShadow
286 sal_uInt16 nUnknownType;
287 sal_Int32 nUnknownPos;
288 bool bIsAreaTP;
289 sal_uInt16 nChangeType;
290 SvxColorTabPageShadow()
291 : nUnknownType( COLORPAGE_UNKNOWN )
292 , nUnknownPos( LISTBOX_ENTRY_NOTFOUND )
293 , bIsAreaTP( false )
294 , nChangeType( 0 )
301 SvxColorTabPage::SvxColorTabPage(Window* pParent, const SfxItemSet& rInAttrs)
302 : SfxTabPage(pParent, "ColorPage", "cui/ui/colorpage.ui", rInAttrs)
303 , meType( XCOLOR_LIST )
304 , mpXPool( (XOutdevItemPool*) rInAttrs.GetPool() )
305 , mpTopDlg( GetParentDialog() )
306 , pShadow ( new SvxColorTabPageShadow() )
307 , rOutAttrs ( rInAttrs )
308 // All the horrific pointers we store and should not
309 , pnColorListState( 0 )
310 , pPageType( NULL )
311 , nDlgType( 0 )
312 , pPos( NULL )
313 , pbAreaTP( NULL )
314 , aXFStyleItem( XFILL_SOLID )
315 , aXFillColorItem( OUString(), Color( COL_BLACK ) )
316 , aXFillAttr( (XOutdevItemPool*) rInAttrs.GetPool() )
317 , rXFSet( aXFillAttr.GetItemSet() )
318 , eCM( CM_RGB )
320 get(m_pBoxEmbed, "embed");
321 get(m_pBtnLoad, "load");
322 get(m_pBtnSave, "save");
323 get(m_pTableName, "colortableft");
325 get(m_pEdtName, "name");
326 get(m_pLbColor, "colorlb");
327 get(m_pValSetColorList, "colorset");
328 Size aSize = LogicToPixel(Size(94 , 117), MAP_APPFONT);
329 m_pValSetColorList->set_width_request(aSize.Width());
330 m_pValSetColorList->set_height_request(aSize.Height());
331 get(m_pCtlPreviewOld, "oldpreview");
332 get(m_pCtlPreviewNew, "newpreview");
333 aSize = LogicToPixel(Size(34 , 25), MAP_APPFONT);
334 m_pCtlPreviewOld->set_width_request(aSize.Width());
335 m_pCtlPreviewOld->set_height_request(aSize.Height());
336 m_pCtlPreviewNew->set_width_request(aSize.Width());
337 m_pCtlPreviewNew->set_height_request(aSize.Height());
338 get(m_pLbColorModel, "modellb");
339 get(m_pRGB, "rgb");
340 get(m_pR, "R");
341 get(m_pG, "G");
342 get(m_pB, "B");
343 get(m_pCMYK, "cmyk");
344 get(m_pC, "C");
345 get(m_pY, "Y");
346 get(m_pM, "M");
347 get(m_pK, "K");
348 get(m_pBtnAdd, "add");
349 get(m_pBtnModify, "modify");
350 get(m_pBtnWorkOn, "edit");
351 get(m_pBtnDelete, "delete");
353 m_pBoxEmbed->SetToggleHdl( LINK( this, SvxColorTabPage, EmbedToggleHdl_Impl ) );
355 m_pBtnLoad->SetClickHdl( LINK( this, SvxColorTabPage, ClickLoadHdl_Impl ) );
356 m_pBtnSave->SetClickHdl( LINK( this, SvxColorTabPage, ClickSaveHdl_Impl ) );
358 SetEmbed( GetEmbed() );
359 UpdateTableName();
361 // this page needs ExchangeSupport
362 SetExchangeSupport();
364 // setting the output device
365 rXFSet.Put( aXFStyleItem );
366 rXFSet.Put( aXFillColorItem );
367 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
368 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
370 // overload handler
371 m_pLbColor->SetSelectHdl(
372 LINK( this, SvxColorTabPage, SelectColorLBHdl_Impl ) );
373 m_pValSetColorList->SetSelectHdl(
374 LINK( this, SvxColorTabPage, SelectValSetHdl_Impl ) );
375 m_pLbColorModel->SetSelectHdl(
376 LINK( this, SvxColorTabPage, SelectColorModelHdl_Impl ) );
378 Link aLink = LINK( this, SvxColorTabPage, ModifiedHdl_Impl );
379 m_pR->SetModifyHdl( aLink );
380 m_pG->SetModifyHdl( aLink );
381 m_pB->SetModifyHdl( aLink );
382 m_pC->SetModifyHdl( aLink );
383 m_pY->SetModifyHdl( aLink );
384 m_pM->SetModifyHdl( aLink );
385 m_pK->SetModifyHdl( aLink );
387 m_pBtnAdd->SetClickHdl( LINK( this, SvxColorTabPage, ClickAddHdl_Impl ) );
388 m_pBtnModify->SetClickHdl( LINK( this, SvxColorTabPage, ClickModifyHdl_Impl ) );
389 m_pBtnWorkOn->SetClickHdl( LINK( this, SvxColorTabPage, ClickWorkOnHdl_Impl ) );
390 m_pBtnDelete->SetClickHdl( LINK( this, SvxColorTabPage, ClickDeleteHdl_Impl ) );
392 // ValueSet
393 m_pValSetColorList->SetStyle( m_pValSetColorList->GetStyle() | WB_ITEMBORDER );
394 m_pValSetColorList->Show();
397 SvxColorTabPage::~SvxColorTabPage()
399 delete pShadow;
404 void SvxColorTabPage::ImpColorCountChanged()
406 if (!pColorList.is())
407 return;
408 m_pValSetColorList->SetColCount(m_pValSetColorList->getColumnCount());
413 void SvxColorTabPage::Construct()
415 if (pColorList.is())
417 m_pLbColor->Fill(pColorList);
418 m_pValSetColorList->addEntriesForXColorList(*pColorList);
419 ImpColorCountChanged();
420 UpdateTableName();
426 void SvxColorTabPage::ActivatePage( const SfxItemSet& )
428 if( nDlgType == 0 ) // area dialog
430 *pbAreaTP = false;
432 if( pColorList.is() )
434 if( *pPageType == PT_COLOR && *pPos != LISTBOX_ENTRY_NOTFOUND )
436 m_pLbColor->SelectEntryPos( *pPos );
437 m_pValSetColorList->SelectItem( m_pLbColor->GetSelectEntryPos() + 1 );
438 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
439 XColorEntry* pEntry = pColorList->GetColor( *pPos );
440 ChangeColor(pEntry->GetColor());
442 else if( *pPageType == PT_COLOR && *pPos == LISTBOX_ENTRY_NOTFOUND )
444 const SfxPoolItem* pPoolItem = NULL;
445 if( SFX_ITEM_SET == rOutAttrs.GetItemState( GetWhich( XATTR_FILLCOLOR ), true, &pPoolItem ) )
447 m_pLbColorModel->SelectEntryPos( CM_RGB );
449 ChangeColor(((const XFillColorItem*)pPoolItem)->GetColorValue());
451 m_pEdtName->SetText( ( ( const XFillColorItem* ) pPoolItem )->GetName() );
453 m_pR->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
454 m_pG->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
455 m_pB->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
457 // fill ItemSet and pass it on to XOut
458 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
459 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
460 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
462 m_pCtlPreviewNew->Invalidate();
463 m_pCtlPreviewOld->Invalidate();
467 // so that the possibly changed color is discarded
468 SelectColorLBHdl_Impl( this );
470 *pPageType = PT_COLOR;
471 *pPos = LISTBOX_ENTRY_NOTFOUND;
474 else
475 HideLoadSaveEmbed();
480 int SvxColorTabPage::DeactivatePage( SfxItemSet* _pSet )
482 if ( CheckChanges_Impl() == -1L )
483 return( KEEP_PAGE );
485 if( _pSet )
486 FillItemSet( *_pSet );
488 return( LEAVE_PAGE );
493 long SvxColorTabPage::CheckChanges_Impl()
495 // used to NOT lose changes
496 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
497 if( nPos != LISTBOX_ENTRY_NOTFOUND )
499 Color aColor = pColorList->GetColor( nPos )->GetColor();
500 OUString aString = m_pLbColor->GetSelectEntry();
502 // aNewColor, because COL_USER != COL_something, even if RGB values are the same
503 // Color aNewColor( aColor.GetRed(), aColor.GetGreen(), aColor.GetBlue() );
505 if( ColorToPercent_Impl( aCurrentColor.GetRed() ) != ColorToPercent_Impl( aColor.GetRed() ) ||
506 ColorToPercent_Impl( aCurrentColor.GetGreen() ) != ColorToPercent_Impl( aColor.GetGreen() ) ||
507 ColorToPercent_Impl( aCurrentColor.GetBlue() ) != ColorToPercent_Impl( aColor.GetBlue() ) ||
508 aString != m_pEdtName->GetText() )
510 ResMgr& rMgr = CUI_MGR();
511 Image aWarningBoxImage = WarningBox::GetStandardImage();
512 SvxMessDialog* aMessDlg = new SvxMessDialog(GetParentDialog(),
513 SVX_RESSTR( RID_SVXSTR_COLOR ),
514 ResId( RID_SVXSTR_ASK_CHANGE_COLOR, rMgr ),
515 &aWarningBoxImage );
516 aMessDlg->SetButtonText( MESS_BTN_1,
517 ResId( RID_SVXSTR_CHANGE, rMgr ) );
518 aMessDlg->SetButtonText( MESS_BTN_2,
519 ResId( RID_SVXSTR_ADD, rMgr ) );
521 short nRet = aMessDlg->Execute();
523 switch( nRet )
525 case RET_BTN_1:
527 ClickModifyHdl_Impl( this );
528 aColor = pColorList->GetColor( nPos )->GetColor();
530 break;
532 case RET_BTN_2:
534 ClickAddHdl_Impl( this );
535 nPos = m_pLbColor->GetSelectEntryPos();
536 aColor = pColorList->GetColor( nPos )->GetColor();
538 break;
540 case RET_CANCEL:
541 break;
543 delete aMessDlg;
546 if( nDlgType == 0 ) // area dialog
548 nPos = m_pLbColor->GetSelectEntryPos();
549 if( nPos != LISTBOX_ENTRY_NOTFOUND )
551 *pPos = nPos;
554 return 0;
559 bool SvxColorTabPage::FillItemSet( SfxItemSet& rSet )
561 if( ( nDlgType != 0 ) ||
562 ( *pPageType == PT_COLOR && *pbAreaTP == false ) )
564 OUString aString;
565 Color aColor;
567 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
568 if( nPos != LISTBOX_ENTRY_NOTFOUND )
570 aColor = pColorList->GetColor( nPos )->GetColor();
571 aString = m_pLbColor->GetSelectEntry();
573 else
575 aColor.SetColor (aCurrentColor.GetColor());
577 rSet.Put( XFillColorItem( aString, aColor ) );
578 rSet.Put( XFillStyleItem( XFILL_SOLID ) );
581 return true;
584 void SvxColorTabPage::UpdateModified()
586 bool bEnable = pColorList->Count();
587 m_pBtnModify->Enable( bEnable );
588 m_pBtnWorkOn->Enable( bEnable );
589 m_pBtnDelete->Enable( bEnable );
590 EnableSave( bEnable );
595 void SvxColorTabPage::Reset( const SfxItemSet& rSet )
597 sal_uInt16 nState = rSet.GetItemState( XATTR_FILLCOLOR );
599 Color aNewColor;
601 if ( nState >= SFX_ITEM_DEFAULT )
603 XFillColorItem aColorItem( (const XFillColorItem&)rSet.Get( XATTR_FILLCOLOR ) );
604 aNewColor = aColorItem.GetColorValue();
605 m_pLbColor->SelectEntry(aNewColor);
606 m_pValSetColorList->SelectItem( m_pLbColor->GetSelectEntryPos() + 1 );
607 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
610 // set color model
611 OUString aStr = GetUserData();
612 m_pLbColorModel->SelectEntryPos( aStr.toInt32() );
614 ChangeColor(aNewColor);
615 SelectColorModelHdl_Impl( this );
617 m_pCtlPreviewOld->Invalidate();
618 UpdateModified();
623 SfxTabPage* SvxColorTabPage::Create( Window* pWindow,
624 const SfxItemSet& rOutAttrs )
626 return( new SvxColorTabPage( pWindow, rOutAttrs ) );
629 // is called when the content of the MtrFields is changed for color values
630 IMPL_LINK_NOARG(SvxColorTabPage, ModifiedHdl_Impl)
632 if (eCM == CM_RGB)
634 // read current MtrFields, if cmyk, then k-value as transparency
635 aCurrentColor.SetColor ( Color( (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pR->GetValue() ),
636 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pG->GetValue() ),
637 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pB->GetValue() ) ).GetColor() );
639 else
641 // read current MtrFields, if cmyk, then k-value as transparency
642 aCurrentColor.SetColor ( Color( (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pK->GetValue() ),
643 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pC->GetValue() ),
644 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pY->GetValue() ),
645 (sal_uInt8)PercentToColor_Impl( (sal_uInt16) m_pM->GetValue() ) ).GetColor() );
646 ConvertColorValues (aCurrentColor, CM_RGB);
649 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
650 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
652 m_pCtlPreviewNew->Invalidate();
654 return 0;
659 IMPL_LINK_NOARG(SvxColorTabPage, ClickAddHdl_Impl)
661 Window *pWindow = this;
662 while( pWindow )
664 pWindow = pWindow->GetParent();
667 ResMgr& rMgr = CUI_MGR();
668 OUString aDesc( ResId( RID_SVXSTR_DESC_COLOR, rMgr ) );
669 OUString aName( m_pEdtName->GetText() );
670 XColorEntry* pEntry;
671 long nCount = pColorList->Count();
672 bool bDifferent = true;
674 // check if name is already existing
675 for ( long i = 0; i < nCount && bDifferent; i++ )
676 if ( aName == pColorList->GetColor( i )->GetName() )
677 bDifferent = false;
679 // if yes, it is repeated and a new name is demanded
680 if ( !bDifferent )
682 MessageDialog aWarningBox( GetParentDialog()
683 ,"DuplicateNameDialog"
684 ,"cui/ui/queryduplicatedialog.ui");
685 aWarningBox.Execute();
687 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
688 AbstractSvxNameDialog* pDlg = pFact->CreateSvxNameDialog( GetParentDialog(), aName, aDesc );
689 bool bLoop = true;
691 while ( !bDifferent && bLoop && pDlg->Execute() == RET_OK )
693 pDlg->GetName( aName );
694 bDifferent = true;
696 for( long i = 0; i < nCount && bDifferent; i++ )
698 if( aName == pColorList->GetColor( i )->GetName() )
699 bDifferent = false;
702 if( bDifferent )
703 bLoop = false;
704 else
705 aWarningBox.Execute();
707 delete( pDlg );
710 // if not existing the entry is entered
711 if( bDifferent )
713 pEntry = new XColorEntry( aCurrentColor, aName );
715 pColorList->Insert( pEntry, pColorList->Count() );
717 m_pLbColor->Append( *pEntry );
718 m_pValSetColorList->InsertItem( m_pValSetColorList->GetItemCount() + 1, pEntry->GetColor(), pEntry->GetName() );
719 ImpColorCountChanged();
721 m_pLbColor->SelectEntryPos( m_pLbColor->GetEntryCount() - 1 );
723 *pnColorListState |= CT_MODIFIED;
725 SelectColorLBHdl_Impl( this );
727 UpdateModified();
729 return 0;
734 IMPL_LINK_NOARG(SvxColorTabPage, ClickModifyHdl_Impl)
736 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
738 if( nPos != LISTBOX_ENTRY_NOTFOUND )
740 ResMgr& rMgr = CUI_MGR();
741 OUString aDesc( ResId( RID_SVXSTR_DESC_COLOR, rMgr ) );
742 OUString aName( m_pEdtName->GetText() );
743 long nCount = pColorList->Count();
744 bool bDifferent = true;
746 // check if name is already existing
747 for ( long i = 0; i < nCount && bDifferent; i++ )
748 if ( aName == pColorList->GetColor( i )->GetName() && nPos != i )
749 bDifferent = false;
751 // if yes, it is repeated and a new name is demanded
752 if ( !bDifferent )
754 MessageDialog aWarningBox( GetParentDialog()
755 ,"DuplicateNameDialog"
756 ,"cui/ui/queryduplicatedialog.ui");
757 aWarningBox.Execute();
759 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
760 AbstractSvxNameDialog* pDlg = pFact->CreateSvxNameDialog( GetParentDialog(), aName, aDesc );
761 bool bLoop = true;
763 while ( !bDifferent && bLoop && pDlg->Execute() == RET_OK )
765 pDlg->GetName( aName );
766 bDifferent = true;
768 for ( long i = 0; i < nCount && bDifferent; i++ )
769 if( aName == pColorList->GetColor( i )->GetName() && nPos != i )
770 bDifferent = false;
772 if( bDifferent )
773 bLoop = false;
774 else
775 aWarningBox.Execute();
777 delete( pDlg );
780 // if not existing the entry is entered
781 if( bDifferent )
783 // #123497# Need to replace the existing entry with a new one (old returned needs to be deleted)
784 XColorEntry* pEntry = new XColorEntry(aCurrentColor, aName);
785 delete pColorList->Replace(pEntry, nPos);
787 m_pLbColor->Modify( *pEntry, nPos );
788 m_pLbColor->SelectEntryPos( nPos );
790 m_pValSetColorList->SetItemColor( nPos + 1, pEntry->GetColor() );
791 m_pValSetColorList->SetItemText( nPos + 1, pEntry->GetName() );
792 m_pEdtName->SetText( aName );
794 m_pCtlPreviewOld->Invalidate();
796 *pnColorListState |= CT_MODIFIED;
799 return 0;
804 IMPL_LINK_NOARG(SvxColorTabPage, ClickWorkOnHdl_Impl)
806 SvColorDialog* pColorDlg = new SvColorDialog( GetParentDialog() );
808 pColorDlg->SetColor (aCurrentColor);
809 pColorDlg->SetMode( svtools::ColorPickerMode_MODIFY );
811 if( pColorDlg->Execute() == RET_OK )
813 sal_uInt16 nK = 0;
814 Color aPreviewColor = pColorDlg->GetColor();
815 aCurrentColor = aPreviewColor;
816 if (eCM != CM_RGB)
818 ConvertColorValues (aCurrentColor, eCM);
819 m_pC->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
820 m_pY->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
821 m_pM->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
822 m_pK->SetValue( ColorToPercent_Impl( nK ) );
823 ConvertColorValues (aCurrentColor, CM_RGB);
825 else
827 m_pR->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
828 m_pG->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
829 m_pB->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
832 // fill ItemSet and pass it on to XOut
833 rXFSet.Put( XFillColorItem( OUString(), aPreviewColor ) );
834 //m_pCtlPreviewOld->SetAttributes( aXFillAttr );
835 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
837 m_pCtlPreviewNew->Invalidate();
839 delete( pColorDlg );
841 return 0;
846 IMPL_LINK_NOARG(SvxColorTabPage, ClickDeleteHdl_Impl)
848 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
850 if( nPos != LISTBOX_ENTRY_NOTFOUND )
852 MessageDialog aQueryBox( GetParentDialog(),"AskDelColorDialog","cui/ui/querydeletecolordialog.ui");
854 if( aQueryBox.Execute() == RET_YES )
856 XColorEntry* pEntry = pColorList->Remove( nPos );
857 DBG_ASSERT( pEntry, "ColorEntry not found !" );
858 delete pEntry;
860 // update Listbox and ValueSet
861 m_pLbColor->RemoveEntry( nPos );
862 m_pValSetColorList->Clear();
863 m_pValSetColorList->addEntriesForXColorList(*pColorList);
864 ImpColorCountChanged();
865 //FillValueSet_Impl(*m_pValSetColorList);
867 // positioning
868 m_pLbColor->SelectEntryPos( nPos );
869 SelectColorLBHdl_Impl( this );
871 m_pCtlPreviewOld->Invalidate();
873 *pnColorListState |= CT_MODIFIED;
876 UpdateModified();
878 return 0;
883 IMPL_LINK_NOARG(SvxColorTabPage, SelectColorLBHdl_Impl)
885 sal_Int32 nPos = m_pLbColor->GetSelectEntryPos();
886 if( nPos != LISTBOX_ENTRY_NOTFOUND )
888 m_pValSetColorList->SelectItem( nPos + 1 );
889 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
891 rXFSet.Put( XFillColorItem( OUString(),
892 m_pLbColor->GetSelectEntryColor() ) );
893 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
894 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
896 m_pCtlPreviewOld->Invalidate();
897 m_pCtlPreviewNew->Invalidate();
899 XColorEntry* pEntry = pColorList->GetColor(nPos);
900 ChangeColor(pEntry->GetColor());
902 return 0;
907 IMPL_LINK_NOARG(SvxColorTabPage, SelectValSetHdl_Impl)
909 sal_Int32 nPos = m_pValSetColorList->GetSelectItemId();
910 if( nPos != LISTBOX_ENTRY_NOTFOUND )
912 m_pLbColor->SelectEntryPos( nPos - 1 );
913 m_pEdtName->SetText( m_pLbColor->GetSelectEntry() );
915 rXFSet.Put( XFillColorItem( OUString(),
916 m_pLbColor->GetSelectEntryColor() ) );
917 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
918 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
920 m_pCtlPreviewOld->Invalidate();
921 m_pCtlPreviewNew->Invalidate();
923 XColorEntry* pEntry = pColorList->GetColor(nPos-1);
924 ChangeColor(pEntry->GetColor());
926 return 0;
931 void SvxColorTabPage::ConvertColorValues (Color& rColor, ColorModel eModell)
933 switch (eModell)
935 case CM_RGB:
937 CmykToRgb_Impl (rColor, (sal_uInt16)rColor.GetTransparency() );
938 rColor.SetTransparency ((sal_uInt8) 0);
940 break;
942 case CM_CMYK:
944 sal_uInt16 nK;
945 RgbToCmyk_Impl (rColor, nK );
946 rColor.SetTransparency ((sal_uInt8) nK);
948 break;
952 IMPL_LINK_NOARG(SvxColorTabPage, SelectColorModelHdl_Impl)
954 int nPos = m_pLbColorModel->GetSelectEntryPos();
955 if( nPos != LISTBOX_ENTRY_NOTFOUND )
957 eCM = (ColorModel) nPos;
959 switch( eCM )
961 case CM_RGB:
963 m_pRGB->Show();
964 m_pCMYK->Hide();
967 break;
969 case CM_CMYK:
971 m_pCMYK->Show();
972 m_pRGB->Hide();
974 break;
977 ChangeColor(aCurrentColor);
981 return 0;
986 void SvxColorTabPage::ChangeColor(const Color &rNewColor)
988 aCurrentColor = rNewColor;
989 if (eCM != CM_RGB)
991 ConvertColorValues (aCurrentColor, eCM);
992 m_pC->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
993 m_pY->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
994 m_pM->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
995 m_pK->SetValue( ColorToPercent_Impl( aCurrentColor.GetTransparency() ) );
996 ConvertColorValues (aCurrentColor, CM_RGB);
998 else
1000 m_pR->SetValue( ColorToPercent_Impl( aCurrentColor.GetRed() ) );
1001 m_pG->SetValue( ColorToPercent_Impl( aCurrentColor.GetGreen() ) );
1002 m_pB->SetValue( ColorToPercent_Impl( aCurrentColor.GetBlue() ) );
1005 // fill ItemSet and pass it on to XOut
1006 rXFSet.Put( XFillColorItem( OUString(), aCurrentColor ) );
1007 m_pCtlPreviewOld->SetAttributes( aXFillAttr.GetItemSet() );
1008 m_pCtlPreviewNew->SetAttributes( aXFillAttr.GetItemSet() );
1010 m_pCtlPreviewNew->Invalidate();
1015 //void SvxColorTabPage::FillValueSet_Impl( ValueSet& rVs )
1017 // long nCount = pColorList->Count();
1018 // XColorEntry* pColorEntry;
1020 // if( nCount > 104 )
1021 // rVs.SetStyle( rVs.GetStyle() | WB_VSCROLL );
1023 // for( long i = 0; i < nCount; i++ )
1024 // {
1025 // pColorEntry = pColorList->GetColor( i );
1026 // rVs.InsertItem( i + 1, pColorEntry->GetColor(), pColorEntry->GetName() );
1027 // }
1032 // A RGB value is converted to a CMYK value - not in an ideal way as
1033 // R is converted into C, G into M and B into Y. The K value is held in an
1034 // extra variable. For further color models one should develop own
1035 // classes which should contain the respective casts.
1037 void SvxColorTabPage::RgbToCmyk_Impl( Color& rColor, sal_uInt16& rK )
1039 sal_uInt16 const nColor1 = 255 - rColor.GetRed();
1040 sal_uInt16 const nColor2 = 255 - rColor.GetGreen();
1041 sal_uInt16 const nColor3 = 255 - rColor.GetBlue();
1043 rK = std::min( std::min( nColor1, nColor2 ), nColor3 );
1045 rColor.SetRed( sal::static_int_cast< sal_uInt8 >( nColor1 - rK ) );
1046 rColor.SetGreen( sal::static_int_cast< sal_uInt8 >( nColor2 - rK ) );
1047 rColor.SetBlue( sal::static_int_cast< sal_uInt8 >( nColor3 - rK ) );
1052 // reverse case to RgbToCmyk_Impl (see above)
1054 void SvxColorTabPage::CmykToRgb_Impl( Color& rColor, const sal_uInt16 nK )
1056 long lTemp;
1058 lTemp = 255 - ( rColor.GetRed() + nK );
1060 if( lTemp < 0L )
1061 lTemp = 0L;
1062 rColor.SetRed( (sal_uInt8)lTemp );
1064 lTemp = 255 - ( rColor.GetGreen() + nK );
1066 if( lTemp < 0L )
1067 lTemp = 0L;
1068 rColor.SetGreen( (sal_uInt8)lTemp );
1070 lTemp = 255 - ( rColor.GetBlue() + nK );
1072 if( lTemp < 0L )
1073 lTemp = 0L;
1074 rColor.SetBlue( (sal_uInt8)lTemp );
1079 sal_uInt16 SvxColorTabPage::ColorToPercent_Impl( sal_uInt16 nColor )
1081 sal_uInt16 nWert = 0;
1083 switch (eCM)
1085 case CM_RGB :
1086 nWert = nColor;
1087 break;
1089 case CM_CMYK:
1090 nWert = (sal_uInt16) ( (double) nColor * 100.0 / 255.0 + 0.5 );
1091 break;
1094 return ( nWert );
1099 sal_uInt16 SvxColorTabPage::PercentToColor_Impl( sal_uInt16 nPercent )
1101 sal_uInt16 nWert = 0;
1103 switch (eCM)
1105 case CM_RGB :
1106 nWert = nPercent;
1107 break;
1109 case CM_CMYK:
1110 nWert = (sal_uInt16) ( (double) nPercent * 255.0 / 100.0 + 0.5 );
1111 break;
1114 return ( nWert );
1119 void SvxColorTabPage::FillUserData()
1121 // the color model is saved in the Ini-file
1122 SetUserData( OUString::number( eCM ) );
1127 void SvxColorTabPage::SetupForViewFrame( SfxViewFrame *pViewFrame )
1129 const OfaRefItem<XColorList> *pPtr = NULL;
1130 if ( pViewFrame != NULL && pViewFrame->GetDispatcher() )
1131 pPtr = (const OfaRefItem<XColorList> *)pViewFrame->
1132 GetDispatcher()->Execute( SID_GET_COLORLIST,
1133 SFX_CALLMODE_SYNCHRON );
1134 pColorList = pPtr ? pPtr->GetValue() : XColorList::GetStdColorList();
1136 SetPageType( &pShadow->nUnknownType );
1137 SetDlgType( COLORPAGE_UNKNOWN );
1138 SetPos( &pShadow->nUnknownPos );
1139 SetAreaTP( &pShadow->bIsAreaTP );
1140 SetColorChgd( (ChangeType*)&pShadow->nChangeType );
1141 Construct();
1144 void SvxColorTabPage::SaveToViewFrame( SfxViewFrame *pViewFrame )
1146 if( !pColorList.is() )
1147 return;
1149 pColorList->Save();
1151 if( !pViewFrame )
1152 return;
1154 // notify current viewframe that it uses the same color table
1155 if ( !pViewFrame->GetDispatcher() )
1156 return;
1158 const OfaRefItem<XColorList> * pPtr;
1159 pPtr = (const OfaRefItem<XColorList>*)pViewFrame->GetDispatcher()->Execute( SID_GET_COLORLIST, SFX_CALLMODE_SYNCHRON );
1160 if( pPtr )
1162 XColorListRef pReference = pPtr->GetValue();
1164 if( pReference.is() &&
1165 pReference->GetPath() == pColorList->GetPath() &&
1166 pReference->GetName() == pColorList->GetName() )
1167 SfxObjectShell::Current()->PutItem( SvxColorListItem( pColorList,
1168 SID_COLOR_TABLE ) );
1172 void SvxColorTabPage::SetPropertyList( XPropertyListType t, const XPropertyListRef &xRef )
1174 (void) t;
1175 OSL_ASSERT( t == XCOLOR_LIST );
1176 pColorList = XColorListRef( static_cast<XColorList *>(xRef.get() ) );
1179 void SvxColorTabPage::SetColorList( XColorListRef pColList )
1181 SetPropertyList( XCOLOR_LIST, XPropertyListRef( ( pColList.get() ) ) );
1184 XPropertyListRef SvxColorTabPage::GetPropertyList( XPropertyListType t )
1186 (void) t;
1187 OSL_ASSERT( t == XCOLOR_LIST );
1188 return XPropertyListRef( pColorList.get() );
1192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */