Bump version to 4.3-4
[LibreOffice.git] / cui / source / options / optchart.cxx
blob673a08f013af8d6f4dd061282efe7fd41cf521c4
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 <dialmgr.hxx>
24 #include <vcl/msgbox.hxx>
25 #include <svx/svxids.hrc>
27 namespace
29 void FillBoxChartColorLB(ColorLB *pLB, const SvxChartColorTable & rTab)
31 pLB->SetUpdateMode(false);
32 pLB->Clear();
33 long nCount = rTab.size();
34 for(long i = 0; i < nCount; ++i)
36 pLB->Append(rTab[i]);
38 pLB->SetUpdateMode(true);
42 SvxDefaultColorOptPage::SvxDefaultColorOptPage(Window* pParent, const SfxItemSet& rInAttrs)
43 : SfxTabPage(pParent, "OptChartColorsPage","cui/ui/optchartcolorspage.ui", rInAttrs)
45 get(m_pPBRemove, "delete");
46 get(m_pPBAdd, "add");
47 get(m_pPBDefault, "default");
48 get(m_pValSetColorBox, "table");
49 get(m_pLbChartColors, "colors");
50 m_pLbChartColors->set_height_request(m_pLbChartColors->GetTextHeight()*16);
52 m_pPBDefault->SetClickHdl( LINK( this, SvxDefaultColorOptPage, ResetToDefaults ) );
53 m_pPBAdd->SetClickHdl( LINK( this, SvxDefaultColorOptPage, AddChartColor ) );
54 m_pPBRemove->SetClickHdl( LINK( this, SvxDefaultColorOptPage, RemoveChartColor ) );
55 m_pLbChartColors->SetSelectHdl( LINK( this, SvxDefaultColorOptPage, ListClickedHdl ) );
56 m_pValSetColorBox->SetSelectHdl( LINK( this, SvxDefaultColorOptPage, BoxClickedHdl ) );
58 m_pValSetColorBox->SetStyle( m_pValSetColorBox->GetStyle()
59 | WB_ITEMBORDER | WB_NAMEFIELD );
60 m_pValSetColorBox->SetColCount( 8 );
61 m_pValSetColorBox->SetLineCount( 13 );
62 m_pValSetColorBox->SetExtraSpacing( 0 );
63 m_pValSetColorBox->Show();
65 pChartOptions = new SvxChartOptions;
66 pColorList = XColorList::CreateStdColorList();
68 const SfxPoolItem* pItem = NULL;
69 if ( rInAttrs.GetItemState( SID_SCH_EDITOPTIONS, false, &pItem ) == SFX_ITEM_SET )
71 pColorConfig = (static_cast< SvxChartColorTableItem* >(pItem->Clone()) );
73 else
75 SvxChartColorTable aTable;
76 aTable.useDefault();
77 pColorConfig = new SvxChartColorTableItem( SID_SCH_EDITOPTIONS, aTable );
78 pColorConfig->SetOptions( pChartOptions );
81 Construct();
84 SvxDefaultColorOptPage::~SvxDefaultColorOptPage()
86 // save changes
87 pChartOptions->SetDefaultColors( pColorConfig->GetColorList() );
88 pChartOptions->Commit();
90 delete pColorConfig;
91 delete pChartOptions;
94 void SvxDefaultColorOptPage::Construct()
96 if( pColorConfig )
97 FillBoxChartColorLB(m_pLbChartColors, pColorConfig->GetColorList());
99 FillColorBox();
101 m_pLbChartColors->SelectEntryPos( 0 );
102 ListClickedHdl(m_pLbChartColors);
106 SfxTabPage* SvxDefaultColorOptPage::Create( Window* pParent, const SfxItemSet& rAttrs )
108 return new SvxDefaultColorOptPage( pParent, rAttrs );
111 bool SvxDefaultColorOptPage::FillItemSet( SfxItemSet& rOutAttrs )
113 if( pColorConfig )
114 rOutAttrs.Put( *(static_cast< SfxPoolItem* >(pColorConfig)));
116 return true;
119 void SvxDefaultColorOptPage::Reset( const SfxItemSet& )
121 m_pLbChartColors->SelectEntryPos( 0 );
122 ListClickedHdl(m_pLbChartColors);
125 void SvxDefaultColorOptPage::FillColorBox()
127 if( !pColorList.is() ) return;
129 long nCount = pColorList->Count();
130 XColorEntry* pColorEntry;
132 if( nCount > 104 )
133 m_pValSetColorBox->SetStyle( m_pValSetColorBox->GetStyle() | WB_VSCROLL );
135 for( long i = 0; i < nCount; i++ )
137 pColorEntry = pColorList->GetColor( i );
138 m_pValSetColorBox->InsertItem( i + 1, pColorEntry->GetColor(), pColorEntry->GetName() );
143 long SvxDefaultColorOptPage::GetColorIndex( const Color& rCol )
145 if( pColorList.is() )
147 long nCount = pColorList->Count();
148 XColorEntry* pColorEntry;
150 for( long i = nCount - 1; i >= 0; i-- ) // default chart colors are at the end of the table
152 pColorEntry = pColorList->GetColor( i );
153 if( pColorEntry && pColorEntry->GetColor() == rCol )
154 return pColorList->GetIndex( pColorEntry->GetName() );
157 return -1L;
163 // event handlers
166 // ResetToDefaults
169 IMPL_LINK_NOARG(SvxDefaultColorOptPage, ResetToDefaults)
171 if( pColorConfig )
173 pColorConfig->GetColorList().useDefault();
175 FillBoxChartColorLB(m_pLbChartColors, pColorConfig->GetColorList());
177 m_pLbChartColors->GetFocus();
178 m_pLbChartColors->SelectEntryPos( 0 );
179 m_pPBRemove->Enable( true );
182 return 0L;
185 // AddChartColor
188 IMPL_LINK_NOARG(SvxDefaultColorOptPage, AddChartColor)
190 if( pColorConfig )
192 ColorData black = RGB_COLORDATA( 0x00, 0x00, 0x00 );
194 pColorConfig->GetColorList().append (XColorEntry ( black, pColorConfig->GetColorList().getDefaultName(pColorConfig->GetColorList().size())));
196 FillBoxChartColorLB(m_pLbChartColors, pColorConfig->GetColorList());
198 m_pLbChartColors->GetFocus();
199 m_pLbChartColors->SelectEntryPos( pColorConfig->GetColorList().size() - 1 );
200 m_pPBRemove->Enable( true );
203 return 0L;
206 // RemoveChartColor
209 IMPL_LINK( SvxDefaultColorOptPage, RemoveChartColor, PushButton*, pButton )
211 sal_Int32 nIndex = m_pLbChartColors->GetSelectEntryPos();
213 if (m_pLbChartColors->GetSelectEntryCount() == 0)
214 return 0L;
216 if( pColorConfig )
218 OSL_ENSURE(pColorConfig->GetColorList().size() > 1, "don't delete the last chart color");
220 MessageDialog aQuery(pButton, "QueryDeleteChartColorDialog",
221 "cui/ui/querydeletechartcolordialog.ui");
222 if (RET_YES == aQuery.Execute())
224 pColorConfig->GetColorList().remove( nIndex );
226 FillBoxChartColorLB(m_pLbChartColors, pColorConfig->GetColorList());
228 m_pLbChartColors->GetFocus();
230 if (nIndex == m_pLbChartColors->GetEntryCount() && m_pLbChartColors->GetEntryCount() > 0)
231 m_pLbChartColors->SelectEntryPos( pColorConfig->GetColorList().size() - 1 );
232 else if (m_pLbChartColors->GetEntryCount() > 0)
233 m_pLbChartColors->SelectEntryPos( nIndex );
234 else
235 m_pPBRemove->Enable(true);
239 return 0L;
242 IMPL_LINK( SvxDefaultColorOptPage, ListClickedHdl, ColorLB*, _pColorList )
244 Color aCol = _pColorList->GetSelectEntryColor();
246 long nIndex = GetColorIndex( aCol );
248 if( nIndex == -1 ) // not found
249 m_pValSetColorBox->SetNoSelection();
250 else
251 m_pValSetColorBox->SelectItem( nIndex + 1 ); // ValueSet is 1-based
253 return 0L;
256 IMPL_LINK_NOARG(SvxDefaultColorOptPage, BoxClickedHdl)
258 sal_Int32 nIdx = m_pLbChartColors->GetSelectEntryPos();
259 if( nIdx != LISTBOX_ENTRY_NOTFOUND )
261 const XColorEntry aEntry( m_pValSetColorBox->GetItemColor( m_pValSetColorBox->GetSelectItemId() ), m_pLbChartColors->GetSelectEntry() );
263 m_pLbChartColors->Modify( aEntry, nIdx );
264 pColorConfig->ReplaceColorByIndex( nIdx, aEntry );
266 m_pLbChartColors->SelectEntryPos( nIdx ); // reselect entry
269 return 0L;
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */