xref: /trunk/main/cui/source/options/optchart.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 #include <unotools/pathoptions.hxx>
31 #include <cuires.hrc>
32 #include "optchart.hxx"
33 #include "optchart.hrc"
34 #include <dialmgr.hxx>
35 #include <svx/svxids.hrc> // for SID_SCH_EDITOPTIONS
36 
37 // ====================
38 // class ChartColorLB
39 // ====================
40 void ChartColorLB::FillBox( const SvxChartColorTable & rTab )
41 {
42     long nCount = rTab.size();
43 	SetUpdateMode( sal_False );
44 
45 	for( long i = 0; i < nCount; i++ )
46 	{
47         Append( const_cast< XColorEntry * >( & rTab[ i ] ));
48 	}
49 	SetUpdateMode( sal_True );
50 }
51 
52 
53 // ====================
54 // class SvxDefaultColorOptPage
55 // ====================
56 SvxDefaultColorOptPage::SvxDefaultColorOptPage( Window* pParent, const SfxItemSet& rInAttrs ) :
57 
58 	SfxTabPage( pParent, CUI_RES( RID_OPTPAGE_CHART_DEFCOLORS ), rInAttrs ),
59 
60 	aGbChartColors	( this, CUI_RES( FL_CHART_COLOR_LIST ) ),
61 	aLbChartColors	( this, CUI_RES( LB_CHART_COLOR_LIST ) ),
62 	aGbColorBox		( this, CUI_RES( FL_COLOR_BOX ) ),
63 	aValSetColorBox	( this, CUI_RES( CT_COLOR_BOX ) ),
64 	aPBDefault		( this, CUI_RES( PB_RESET_TO_DEFAULT ) )
65 {
66 	FreeResource();
67 
68 	aPBDefault.SetClickHdl( LINK( this, SvxDefaultColorOptPage, ResetToDefaults ) );
69 	aLbChartColors.SetSelectHdl( LINK( this, SvxDefaultColorOptPage, ListClickedHdl ) );
70 	aValSetColorBox.SetSelectHdl( LINK( this, SvxDefaultColorOptPage, BoxClickedHdl ) );
71 
72 	aValSetColorBox.SetStyle( aValSetColorBox.GetStyle()
73 									| WB_VSCROLL | WB_ITEMBORDER | WB_NAMEFIELD );
74 	aValSetColorBox.SetColCount( 8 );
75 	aValSetColorBox.SetLineCount( 12 );
76 	aValSetColorBox.SetExtraSpacing( 0 );
77 	aValSetColorBox.Show();
78 
79 	pChartOptions = new SvxChartOptions;
80 	pColorTab = new XColorTable( SvtPathOptions().GetPalettePath() );
81 
82 	const SfxPoolItem* pItem = NULL;
83 	if ( rInAttrs.GetItemState( SID_SCH_EDITOPTIONS, sal_False, &pItem ) == SFX_ITEM_SET )
84 	{
85 		pColorConfig = SAL_STATIC_CAST( SvxChartColorTableItem*, pItem->Clone() );
86 	}
87 	else
88 	{
89 		SvxChartColorTable aTable;
90 		aTable.useDefault();
91 		pColorConfig = new SvxChartColorTableItem( SID_SCH_EDITOPTIONS, aTable );
92 		pColorConfig->SetOptions( pChartOptions );
93 	}
94 
95 	Construct();
96 }
97 
98 SvxDefaultColorOptPage::~SvxDefaultColorOptPage()
99 {
100 	// save changes
101 	pChartOptions->SetDefaultColors( pColorConfig->GetColorTable() );
102 	pChartOptions->Commit();
103 
104 	delete pColorConfig;
105 	delete pColorTab;
106 	delete pChartOptions;
107 }
108 
109 void SvxDefaultColorOptPage::Construct()
110 {
111 	if( pColorConfig )
112 		aLbChartColors.FillBox( pColorConfig->GetColorTable() );
113 
114 	FillColorBox();
115 
116 	aLbChartColors.SelectEntryPos( 0 );
117 	ListClickedHdl( &aLbChartColors );
118 }
119 
120 
121 SfxTabPage* __EXPORT SvxDefaultColorOptPage::Create( Window* pParent, const SfxItemSet& rAttrs )
122 {
123 	return new SvxDefaultColorOptPage( pParent, rAttrs );
124 }
125 
126 sal_Bool __EXPORT SvxDefaultColorOptPage::FillItemSet( SfxItemSet& rOutAttrs )
127 {
128 	if( pColorConfig )
129 		rOutAttrs.Put( *SAL_STATIC_CAST( SfxPoolItem*, pColorConfig ));
130 
131 	return sal_True;
132 }
133 
134 void __EXPORT SvxDefaultColorOptPage::Reset( const SfxItemSet& )
135 {
136 	aLbChartColors.SelectEntryPos( 0 );
137 	ListClickedHdl( &aLbChartColors );
138 }
139 
140 void SvxDefaultColorOptPage::FillColorBox()
141 {
142 	if( !pColorTab ) return;
143 
144 	long nCount = pColorTab->Count();
145 	XColorEntry* pColorEntry;
146 
147 	for( long i = 0; i < nCount; i++ )
148 	{
149 		pColorEntry = pColorTab->GetColor( i );
150 		aValSetColorBox.InsertItem( (sal_uInt16) i + 1, pColorEntry->GetColor(), pColorEntry->GetName() );
151 	}
152 }
153 
154 
155 long SvxDefaultColorOptPage::GetColorIndex( const Color& rCol )
156 {
157 	if( pColorTab )
158 	{
159 		long nCount = pColorTab->Count();
160 		XColorEntry* pColorEntry;
161 
162 		for( long i = nCount - 1; i >= 0; i-- )			// default chart colors are at the end of the table
163 		{
164 			pColorEntry = pColorTab->GetColor( i );
165 			if( pColorEntry && pColorEntry->GetColor() == rCol )
166 				return SAL_STATIC_CAST( XPropertyTable*, pColorTab )->Get( pColorEntry->GetName() );
167 		}
168 	}
169 	return -1L;
170 }
171 
172 
173 
174 // --------------------
175 // event handlers
176 // --------------------
177 
178 // ResetToDefaults
179 // ---------------
180 
181 IMPL_LINK( SvxDefaultColorOptPage, ResetToDefaults, void *, EMPTYARG )
182 {
183     if( pColorConfig )
184     {
185         pColorConfig->GetColorTable().useDefault();
186 
187         aLbChartColors.Clear();
188         aLbChartColors.FillBox( pColorConfig->GetColorTable() );
189 
190         aLbChartColors.GetFocus();
191     }
192 
193 	return 0L;
194 }
195 
196 // ListClickedHdl
197 // --------------
198 
199 IMPL_LINK( SvxDefaultColorOptPage, ListClickedHdl, ChartColorLB*,  pColorList )
200 {
201 	Color aCol = pColorList->GetSelectEntryColor();
202 
203 	long nIndex = GetColorIndex( aCol );
204 
205 	if( nIndex == -1 )		// not found
206 	{
207 		aValSetColorBox.SetNoSelection();
208 	}
209 	else
210 	{
211 		aValSetColorBox.SelectItem( (sal_uInt16)nIndex + 1 );		// ValueSet is 1-based
212 	}
213 
214 	return 0L;
215 }
216 
217 // BoxClickedHdl
218 // -------------
219 
220 IMPL_LINK( SvxDefaultColorOptPage, BoxClickedHdl, ValueSet*, EMPTYARG )
221 {
222 	sal_uInt16 nIdx = aLbChartColors.GetSelectEntryPos();
223 	if( nIdx != LISTBOX_ENTRY_NOTFOUND )
224 	{
225 		XColorEntry aEntry( aValSetColorBox.GetItemColor( aValSetColorBox.GetSelectItemId() ),
226                             aLbChartColors.GetSelectEntry() );
227 
228 		aLbChartColors.Modify( & aEntry, nIdx );
229 		pColorConfig->ReplaceColorByIndex( nIdx, aEntry );
230 
231         aLbChartColors.SelectEntryPos( nIdx );	// reselect entry
232 	}
233 
234     return 0L;
235 }
236 
237