xref: /trunk/main/formula/source/ui/dlg/funcpage.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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_formula.hxx"
30 
31 
32 
33 //----------------------------------------------------------------------------
34 
35 #include <sfx2/dispatch.hxx>
36 #include <sfx2/docfile.hxx>
37 #include <svl/zforlist.hxx>
38 #include <svl/stritem.hxx>
39 #include "formula/IFunctionDescription.hxx"
40 
41 #include "funcpage.hxx"
42 #include "formdlgs.hrc"
43 #include "ForResId.hrc"
44 #include "ModuleHelper.hxx"
45 //============================================================================
46 namespace formula
47 {
48 
49 FormulaListBox::FormulaListBox( Window* pParent, WinBits nWinStyle):
50     ListBox(pParent,nWinStyle)
51 {}
52 
53 FormulaListBox::FormulaListBox( Window* pParent, const ResId& rResId ):
54     ListBox(pParent,rResId)
55 {}
56 
57 void FormulaListBox::KeyInput( const KeyEvent& rKEvt )
58 {
59     KeyEvent aKEvt=rKEvt;
60     //ListBox::KeyInput(rKEvt);
61 
62     if(aKEvt.GetCharCode()==' ')
63         DoubleClick();
64 }
65 
66 long FormulaListBox::PreNotify( NotifyEvent& rNEvt )
67 {
68     NotifyEvent aNotifyEvt=rNEvt;
69 
70     long nResult=ListBox::PreNotify(rNEvt);
71 
72     sal_uInt16 nSwitch=aNotifyEvt.GetType();
73     if(nSwitch==EVENT_KEYINPUT)
74     {
75         KeyInput(*aNotifyEvt.GetKeyEvent());
76     }
77     return nResult;
78 }
79 
80 
81 
82 //============================================================================
83 
84 inline sal_uInt16 Lb2Cat( sal_uInt16 nLbPos )
85 {
86     // Kategorie 0 == LRU, sonst Categories == LbPos-1
87     if ( nLbPos > 0 )
88         nLbPos -= 1;
89 
90     return nLbPos;
91 }
92 
93 //============================================================================
94 
95 FuncPage::FuncPage(Window* pParent,const IFunctionManager* _pFunctionManager):
96     TabPage(pParent,ModuleRes(RID_FORMULATAB_FUNCTION)),
97     //
98     aFtCategory     ( this, ModuleRes( FT_CATEGORY ) ),
99     aLbCategory     ( this, ModuleRes( LB_CATEGORY ) ),
100     aFtFunction     ( this, ModuleRes( FT_FUNCTION ) ),
101     aLbFunction     ( this, ModuleRes( LB_FUNCTION ) ),
102     m_pFunctionManager(_pFunctionManager)
103 {
104     FreeResource();
105     m_aHelpId = aLbFunction.GetHelpId();
106     aLbFunction.SetUniqueId(m_aHelpId);
107 
108     InitLRUList();
109 
110     const sal_uInt32 nCategoryCount = m_pFunctionManager->getCount();
111     for(sal_uInt32 j= 0; j < nCategoryCount; ++j)
112     {
113         const IFunctionCategory* pCategory = m_pFunctionManager->getCategory(j);
114         aLbCategory.SetEntryData(aLbCategory.InsertEntry(pCategory->getName()),(void*)pCategory);
115     }
116 
117     aLbCategory.SelectEntryPos(1);
118     UpdateFunctionList();
119     aLbCategory.SetSelectHdl( LINK( this, FuncPage, SelHdl ) );
120     aLbFunction.SetSelectHdl( LINK( this, FuncPage, SelHdl ) );
121     aLbFunction.SetDoubleClickHdl( LINK( this, FuncPage, DblClkHdl ) );
122 }
123 // -----------------------------------------------------------------------------
124 void FuncPage::impl_addFunctions(const IFunctionCategory* _pCategory)
125 {
126     const sal_uInt32 nCount = _pCategory->getCount();
127     for(sal_uInt32 i = 0 ; i < nCount; ++i)
128     {
129         TFunctionDesc pDesc(_pCategory->getFunction(i));
130         aLbFunction.SetEntryData(
131             aLbFunction.InsertEntry(pDesc->getFunctionName() ),(void*)pDesc );
132     } // for(sal_uInt32 i = 0 ; i < nCount; ++i)
133 }
134 
135 void FuncPage::UpdateFunctionList()
136 {
137     sal_uInt16  nSelPos   = aLbCategory.GetSelectEntryPos();
138     const IFunctionCategory* pCategory = static_cast<const IFunctionCategory*>(aLbCategory.GetEntryData(nSelPos));
139     sal_uInt16  nCategory = ( LISTBOX_ENTRY_NOTFOUND != nSelPos )
140                             ? Lb2Cat( nSelPos ) : 0;
141 
142     (void)nCategory;
143 
144     aLbFunction.Clear();
145     aLbFunction.SetUpdateMode( sal_False );
146     //------------------------------------------------------
147 
148     if ( nSelPos > 0 )
149     {
150         if ( pCategory == NULL )
151         {
152             const sal_uInt32 nCount = m_pFunctionManager->getCount();
153             for(sal_uInt32 i = 0 ; i < nCount; ++i)
154             {
155                 impl_addFunctions(m_pFunctionManager->getCategory(i));
156             }
157         }
158         else
159         {
160             impl_addFunctions(pCategory);
161         }
162     }
163     else // LRU-Liste
164     {
165         ::std::vector< TFunctionDesc >::iterator aIter = aLRUList.begin();
166         ::std::vector< TFunctionDesc >::iterator aEnd = aLRUList.end();
167 
168         for ( ; aIter != aEnd; ++aIter )
169         {
170             const IFunctionDescription* pDesc = *aIter;
171             if (pDesc)  // may be null if a function is no longer available
172             {
173                 aLbFunction.SetEntryData(
174                     aLbFunction.InsertEntry( pDesc->getFunctionName() ), (void*)pDesc );
175             }
176         }
177     }
178 
179     //------------------------------------------------------
180     aLbFunction.SetUpdateMode( sal_True );
181     aLbFunction.SelectEntryPos(0);
182 
183     if(IsVisible()) SelHdl(&aLbFunction);
184 }
185 
186 IMPL_LINK( FuncPage, SelHdl, ListBox*, pLb )
187 {
188     if(pLb==&aLbFunction)
189     {
190         const IFunctionDescription* pDesc = GetFuncDesc( GetFunction() );
191         if ( pDesc )
192         {
193             const rtl::OString sHelpId = pDesc->getHelpId();
194             if ( sHelpId.getLength() )
195                 aLbFunction.SetHelpId(sHelpId);
196         }
197         aSelectionLink.Call(this);
198     }
199     else
200     {
201         aLbFunction.SetHelpId(m_aHelpId);
202         UpdateFunctionList();
203     }
204     return 0;
205 }
206 
207 IMPL_LINK( FuncPage, DblClkHdl, ListBox*, EMPTYARG )
208 {
209     aDoubleClickLink.Call(this);
210     return 0;
211 }
212 
213 void FuncPage::SetCategory(sal_uInt16 nCat)
214 {
215     aLbCategory.SelectEntryPos(nCat);
216     UpdateFunctionList();
217 }
218 sal_uInt16 FuncPage::GetFuncPos(const IFunctionDescription* _pDesc)
219 {
220     return aLbFunction.GetEntryPos(_pDesc);
221 }
222 void FuncPage::SetFunction(sal_uInt16 nFunc)
223 {
224     aLbFunction.SelectEntryPos(nFunc);
225 }
226 
227 void FuncPage::SetFocus()
228 {
229     aLbFunction.GrabFocus();
230 }
231 
232 sal_uInt16 FuncPage::GetCategory()
233 {
234     return aLbCategory.GetSelectEntryPos();
235 }
236 
237 sal_uInt16 FuncPage::GetFunction()
238 {
239     return aLbFunction.GetSelectEntryPos();
240 }
241 
242 sal_uInt16 FuncPage::GetFunctionEntryCount()
243 {
244     return aLbFunction.GetSelectEntryCount();
245 }
246 
247 String FuncPage::GetSelFunctionName() const
248 {
249     return aLbFunction.GetSelectEntry();
250 }
251 const IFunctionDescription* FuncPage::GetFuncDesc( sal_uInt16 nPos ) const
252 {
253     // nicht schoen, aber hoffentlich selten
254     return (const IFunctionDescription*) aLbFunction.GetEntryData(nPos);
255 }
256 
257 void FuncPage::InitLRUList()
258 {
259     ::std::vector< const IFunctionDescription*> aRUFunctions;
260     m_pFunctionManager->fillLastRecentlyUsedFunctions(aLRUList);
261 }
262 
263 
264 } // formula
265 
266