Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / cui / source / options / optchart.cxx
blob06f25ef1ba2f6ca60b6a95b942b0573a75b53c7f
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 <unotools/pathoptions.hxx>
21 #include <cuires.hrc>
22 #include "optchart.hxx"
23 #include "optchart.hrc"
24 #include <dialmgr.hxx>
25 #include <vcl/msgbox.hxx>
26 #include <svx/svxids.hrc> // for SID_SCH_EDITOPTIONS
28 // ====================
29 // class ChartColorLB
30 // ====================
31 void ChartColorLB::FillBox( const SvxChartColorTable & rTab )
33 long nCount = rTab.size();
34 SetUpdateMode( sal_False );
36 for( long i = 0; i < nCount; i++ )
38 Append( const_cast< XColorEntry * >( & rTab[ i ] ));
40 SetUpdateMode( sal_True );
44 // ====================
45 // class SvxDefaultColorOptPage
46 // ====================
47 SvxDefaultColorOptPage::SvxDefaultColorOptPage( Window* pParent, const SfxItemSet& rInAttrs ) :
49 SfxTabPage( pParent, CUI_RES( RID_OPTPAGE_CHART_DEFCOLORS ), rInAttrs ),
51 aGbChartColors ( this, CUI_RES( FL_CHART_COLOR_LIST ) ),
52 aLbChartColors ( this, CUI_RES( LB_CHART_COLOR_LIST ) ),
53 aGbColorBox ( this, CUI_RES( FL_COLOR_BOX ) ),
54 aValSetColorBox ( this, CUI_RES( CT_COLOR_BOX ) ),
55 aPBDefault ( this, CUI_RES( PB_RESET_TO_DEFAULT ) ),
56 aPBAdd ( this, CUI_RES( PB_ADD_CHART_COLOR ) ),
57 aPBRemove ( this, CUI_RES( PB_REMOVE_CHART_COLOR ) )
59 FreeResource();
61 aPBDefault.SetClickHdl( LINK( this, SvxDefaultColorOptPage, ResetToDefaults ) );
62 aPBAdd.SetClickHdl( LINK( this, SvxDefaultColorOptPage, AddChartColor ) );
63 aPBRemove.SetClickHdl( LINK( this, SvxDefaultColorOptPage, RemoveChartColor ) );
64 aLbChartColors.SetSelectHdl( LINK( this, SvxDefaultColorOptPage, ListClickedHdl ) );
65 aValSetColorBox.SetSelectHdl( LINK( this, SvxDefaultColorOptPage, BoxClickedHdl ) );
67 aValSetColorBox.SetStyle( aValSetColorBox.GetStyle()
68 | WB_ITEMBORDER | WB_NAMEFIELD );
69 aValSetColorBox.SetColCount( 8 );
70 aValSetColorBox.SetLineCount( 13 );
71 aValSetColorBox.SetExtraSpacing( 0 );
72 aValSetColorBox.Show();
74 pChartOptions = new SvxChartOptions;
75 pColorList = XColorList::CreateStdColorList();
77 const SfxPoolItem* pItem = NULL;
78 if ( rInAttrs.GetItemState( SID_SCH_EDITOPTIONS, sal_False, &pItem ) == SFX_ITEM_SET )
80 pColorConfig = (static_cast< SvxChartColorTableItem* >(pItem->Clone()) );
82 else
84 SvxChartColorTable aTable;
85 aTable.useDefault();
86 pColorConfig = new SvxChartColorTableItem( SID_SCH_EDITOPTIONS, aTable );
87 pColorConfig->SetOptions( pChartOptions );
90 Construct();
93 SvxDefaultColorOptPage::~SvxDefaultColorOptPage()
95 // save changes
96 pChartOptions->SetDefaultColors( pColorConfig->GetColorList() );
97 pChartOptions->Commit();
99 delete pColorConfig;
100 delete pChartOptions;
103 void SvxDefaultColorOptPage::Construct()
105 if( pColorConfig )
106 aLbChartColors.FillBox( pColorConfig->GetColorList() );
108 FillColorBox();
110 aLbChartColors.SelectEntryPos( 0 );
111 ListClickedHdl( &aLbChartColors );
115 SfxTabPage* SvxDefaultColorOptPage::Create( Window* pParent, const SfxItemSet& rAttrs )
117 return new SvxDefaultColorOptPage( pParent, rAttrs );
120 sal_Bool SvxDefaultColorOptPage::FillItemSet( SfxItemSet& rOutAttrs )
122 if( pColorConfig )
123 rOutAttrs.Put( *(static_cast< SfxPoolItem* >(pColorConfig)));
125 return sal_True;
128 void SvxDefaultColorOptPage::Reset( const SfxItemSet& )
130 aLbChartColors.SelectEntryPos( 0 );
131 ListClickedHdl( &aLbChartColors );
134 void SvxDefaultColorOptPage::FillColorBox()
136 if( !pColorList.is() ) return;
138 long nCount = pColorList->Count();
139 XColorEntry* pColorEntry;
141 if( nCount > 104 )
142 aValSetColorBox.SetStyle( aValSetColorBox.GetStyle() | WB_VSCROLL );
144 for( long i = 0; i < nCount; i++ )
146 pColorEntry = pColorList->GetColor( i );
147 aValSetColorBox.InsertItem( (sal_uInt16) i + 1, pColorEntry->GetColor(), pColorEntry->GetName() );
152 long SvxDefaultColorOptPage::GetColorIndex( const Color& rCol )
154 if( pColorList.is() )
156 long nCount = pColorList->Count();
157 XColorEntry* pColorEntry;
159 for( long i = nCount - 1; i >= 0; i-- ) // default chart colors are at the end of the table
161 pColorEntry = pColorList->GetColor( i );
162 if( pColorEntry && pColorEntry->GetColor() == rCol )
163 return pColorList->Get( pColorEntry->GetName() );
166 return -1L;
171 // --------------------
172 // event handlers
173 // --------------------
175 // ResetToDefaults
176 // ---------------
178 IMPL_LINK_NOARG(SvxDefaultColorOptPage, ResetToDefaults)
180 if( pColorConfig )
182 pColorConfig->GetColorList().useDefault();
184 aLbChartColors.Clear();
185 aLbChartColors.FillBox( pColorConfig->GetColorList() );
187 aLbChartColors.GetFocus();
188 aLbChartColors.SelectEntryPos( 0 );
189 aPBRemove.Enable( sal_True );
192 return 0L;
195 // AddChartColor
196 // ------------
198 IMPL_LINK_NOARG(SvxDefaultColorOptPage, AddChartColor)
200 if( pColorConfig )
202 ColorData black = RGB_COLORDATA( 0x00, 0x00, 0x00 );
204 pColorConfig->GetColorList().append (XColorEntry ( black, pColorConfig->GetColorList().getDefaultName(pColorConfig->GetColorList().size())));
206 aLbChartColors.Clear();
207 aLbChartColors.FillBox( pColorConfig->GetColorList() );
209 aLbChartColors.GetFocus();
210 aLbChartColors.SelectEntryPos( pColorConfig->GetColorList().size() - 1 );
211 aPBRemove.Enable( sal_True );
214 return 0L;
217 // RemoveChartColor
218 // ----------------
220 IMPL_LINK( SvxDefaultColorOptPage, RemoveChartColor, PushButton*, pButton )
222 size_t nIndex = aLbChartColors.GetSelectEntryPos();
224 if (aLbChartColors.GetSelectEntryCount() == 0)
225 return 0L;
227 if( pColorConfig )
229 OSL_ENSURE(pColorConfig->GetColorList().size() > 1, "don't delete the last chart color");
230 QueryBox aQuery(pButton, CUI_RES(RID_OPTQB_COLOR_CHART_DELETE));
231 aQuery.SetText(String(CUI_RES(RID_OPTSTR_COLOR_CHART_DELETE)));
232 if(RET_YES == aQuery.Execute())
235 pColorConfig->GetColorList().remove( nIndex );
237 aLbChartColors.Clear();
238 aLbChartColors.FillBox( pColorConfig->GetColorList() );
240 aLbChartColors.GetFocus();
242 if (nIndex == aLbChartColors.GetEntryCount() && aLbChartColors.GetEntryCount() > 0)
243 aLbChartColors.SelectEntryPos( pColorConfig->GetColorList().size() - 1 );
244 else if (aLbChartColors.GetEntryCount() > 0)
245 aLbChartColors.SelectEntryPos( nIndex );
246 else
247 aPBRemove.Enable( sal_False );
251 return 0L;
254 IMPL_LINK( SvxDefaultColorOptPage, ListClickedHdl, ChartColorLB*, _pColorList )
256 Color aCol = _pColorList->GetSelectEntryColor();
258 long nIndex = GetColorIndex( aCol );
260 if( nIndex == -1 ) // not found
261 aValSetColorBox.SetNoSelection();
262 else
263 aValSetColorBox.SelectItem( (sal_uInt16)nIndex + 1 ); // ValueSet is 1-based
265 return 0L;
268 IMPL_LINK_NOARG(SvxDefaultColorOptPage, BoxClickedHdl)
270 sal_uInt16 nIdx = aLbChartColors.GetSelectEntryPos();
271 if( nIdx != LISTBOX_ENTRY_NOTFOUND )
273 XColorEntry aEntry( aValSetColorBox.GetItemColor( aValSetColorBox.GetSelectItemId() ),
274 aLbChartColors.GetSelectEntry() );
276 aLbChartColors.Modify( & aEntry, nIdx );
277 pColorConfig->ReplaceColorByIndex( nIdx, aEntry );
279 aLbChartColors.SelectEntryPos( nIdx ); // reselect entry
282 return 0L;
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */