xref: /aoo41x/main/formula/source/ui/dlg/funcpage.cxx (revision c25918c1)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_formula.hxx"
26 
27 
28 
29 //----------------------------------------------------------------------------
30 
31 #include <sfx2/dispatch.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <svl/zforlist.hxx>
34 #include <svl/stritem.hxx>
35 #include "formula/IFunctionDescription.hxx"
36 
37 #include "funcpage.hxx"
38 #include "formdlgs.hrc"
39 #include "ForResId.hrc"
40 #include "ModuleHelper.hxx"
41 //============================================================================
42 namespace formula
43 {
44 
45 FormulaListBox::FormulaListBox( Window* pParent, WinBits nWinStyle):
46 	ListBox(pParent,nWinStyle)
47 {}
48 
49 FormulaListBox::FormulaListBox( Window* pParent, const ResId& rResId ):
50 	ListBox(pParent,rResId)
51 {}
52 
53 void FormulaListBox::KeyInput( const KeyEvent& rKEvt )
54 {
55 	KeyEvent aKEvt=rKEvt;
56 	//ListBox::KeyInput(rKEvt);
57 
58 	if(aKEvt.GetCharCode()==' ')
59 		DoubleClick();
60 }
61 
62 long FormulaListBox::PreNotify( NotifyEvent& rNEvt )
63 {
64 	NotifyEvent aNotifyEvt=rNEvt;
65 
66 	long nResult=ListBox::PreNotify(rNEvt);
67 
68 	sal_uInt16 nSwitch=aNotifyEvt.GetType();
69 	if(nSwitch==EVENT_KEYINPUT)
70 	{
71 		KeyInput(*aNotifyEvt.GetKeyEvent());
72 	}
73 	return nResult;
74 }
75 
76 
77 
78 //============================================================================
79 
80 inline sal_uInt16 Lb2Cat( sal_uInt16 nLbPos )
81 {
82 	// Kategorie 0 == LRU, sonst Categories == LbPos-1
83 	if ( nLbPos > 0 )
84 		nLbPos -= 1;
85 
86 	return nLbPos;
87 }
88 
89 //============================================================================
90 
91 FuncPage::FuncPage(Window* pParent,const IFunctionManager* _pFunctionManager):
92 	TabPage(pParent,ModuleRes(RID_FORMULATAB_FUNCTION)),
93 	//
94 	aFtCategory		( this, ModuleRes( FT_CATEGORY ) ),
95 	aLbCategory		( this, ModuleRes( LB_CATEGORY ) ),
96 	aFtFunction		( this, ModuleRes( FT_FUNCTION ) ),
97 	aLbFunction		( this, ModuleRes( LB_FUNCTION ) ),
98     m_pFunctionManager(_pFunctionManager)
99 {
100 	FreeResource();
101     m_aHelpId = aLbFunction.GetHelpId();
102     aLbFunction.SetUniqueId(m_aHelpId);
103 
104 	InitLRUList();
105 
106     const sal_uInt32 nCategoryCount = m_pFunctionManager->getCount();
107     for(sal_uInt32 j= 0; j < nCategoryCount; ++j)
108     {
109         const IFunctionCategory* pCategory = m_pFunctionManager->getCategory(j);
110         aLbCategory.SetEntryData(aLbCategory.InsertEntry(pCategory->getName()),(void*)pCategory);
111     }
112 
113 	aLbCategory.SelectEntryPos(1);
114 	UpdateFunctionList();
115 	aLbCategory.SetSelectHdl( LINK( this, FuncPage, SelHdl ) );
116 	aLbFunction.SetSelectHdl( LINK( this, FuncPage, SelHdl ) );
117 	aLbFunction.SetDoubleClickHdl( LINK( this, FuncPage, DblClkHdl ) );
118 }
119 // -----------------------------------------------------------------------------
120 void FuncPage::impl_addFunctions(const IFunctionCategory* _pCategory)
121 {
122     const sal_uInt32 nCount = _pCategory->getCount();
123     for(sal_uInt32 i = 0 ; i < nCount; ++i)
124     {
125         TFunctionDesc pDesc(_pCategory->getFunction(i));
126         aLbFunction.SetEntryData(
127 		    aLbFunction.InsertEntry(pDesc->getFunctionName() ),(void*)pDesc );
128     } // for(sal_uInt32 i = 0 ; i < nCount; ++i)
129 }
130 
131 void FuncPage::UpdateFunctionList()
132 {
133 	sal_uInt16	nSelPos	  = aLbCategory.GetSelectEntryPos();
134     const IFunctionCategory* pCategory = static_cast<const IFunctionCategory*>(aLbCategory.GetEntryData(nSelPos));
135 	sal_uInt16	nCategory = ( LISTBOX_ENTRY_NOTFOUND != nSelPos )
136 							? Lb2Cat( nSelPos ) : 0;
137 
138     (void)nCategory;
139 
140 	aLbFunction.Clear();
141 	aLbFunction.SetUpdateMode( sal_False );
142 	//------------------------------------------------------
143 
144 	if ( nSelPos > 0 )
145 	{
146         if ( pCategory == NULL )
147         {
148             const sal_uInt32 nCount = m_pFunctionManager->getCount();
149             for(sal_uInt32 i = 0 ; i < nCount; ++i)
150             {
151                 impl_addFunctions(m_pFunctionManager->getCategory(i));
152             }
153         }
154         else
155         {
156             impl_addFunctions(pCategory);
157         }
158 	}
159 	else // LRU-Liste
160 	{
161         ::std::vector< TFunctionDesc >::iterator aIter = aLRUList.begin();
162         ::std::vector< TFunctionDesc >::iterator aEnd = aLRUList.end();
163 
164 		for ( ; aIter != aEnd; ++aIter )
165 		{
166             const IFunctionDescription* pDesc = *aIter;
167             if (pDesc)  // may be null if a function is no longer available
168             {
169                 aLbFunction.SetEntryData(
170                     aLbFunction.InsertEntry( pDesc->getFunctionName() ), (void*)pDesc );
171             }
172 		}
173 	}
174 
175 	//------------------------------------------------------
176 	aLbFunction.SetUpdateMode( sal_True );
177 	aLbFunction.SelectEntryPos(0);
178 
179 	if(IsVisible())	SelHdl(&aLbFunction);
180 }
181 
182 IMPL_LINK( FuncPage, SelHdl, ListBox*, pLb )
183 {
184 	if(pLb==&aLbFunction)
185 	{
186         const IFunctionDescription* pDesc = GetFuncDesc( GetFunction() );
187         if ( pDesc )
188         {
189             const rtl::OString sHelpId = pDesc->getHelpId();
190             if ( sHelpId.getLength() )
191                 aLbFunction.SetHelpId(sHelpId);
192         }
193 		aSelectionLink.Call(this);
194 	}
195 	else
196 	{
197         aLbFunction.SetHelpId(m_aHelpId);
198 		UpdateFunctionList();
199 	}
200 	return 0;
201 }
202 
203 IMPL_LINK( FuncPage, DblClkHdl, ListBox*, EMPTYARG )
204 {
205 	aDoubleClickLink.Call(this);
206 	return 0;
207 }
208 
209 void FuncPage::SetCategory(sal_uInt16 nCat)
210 {
211 	aLbCategory.SelectEntryPos(nCat);
212 	UpdateFunctionList();
213 }
214 sal_uInt16 FuncPage::GetFuncPos(const IFunctionDescription* _pDesc)
215 {
216     return aLbFunction.GetEntryPos(_pDesc);
217 }
218 void FuncPage::SetFunction(sal_uInt16 nFunc)
219 {
220 	aLbFunction.SelectEntryPos(nFunc);
221 }
222 
223 void FuncPage::SetFocus()
224 {
225 	aLbFunction.GrabFocus();
226 }
227 
228 sal_uInt16 FuncPage::GetCategory()
229 {
230 	return aLbCategory.GetSelectEntryPos();
231 }
232 
233 sal_uInt16 FuncPage::GetFunction()
234 {
235 	return aLbFunction.GetSelectEntryPos();
236 }
237 
238 sal_uInt16 FuncPage::GetFunctionEntryCount()
239 {
240 	return aLbFunction.GetSelectEntryCount();
241 }
242 
243 String FuncPage::GetSelFunctionName() const
244 {
245 	return aLbFunction.GetSelectEntry();
246 }
247 const IFunctionDescription*	FuncPage::GetFuncDesc( sal_uInt16 nPos ) const
248 {
249 	// nicht schoen, aber hoffentlich selten
250 	return (const IFunctionDescription*) aLbFunction.GetEntryData(nPos);
251 }
252 
253 void FuncPage::InitLRUList()
254 {
255     ::std::vector< const IFunctionDescription*> aRUFunctions;
256     m_pFunctionManager->fillLastRecentlyUsedFunctions(aLRUList);
257 }
258 
259 
260 } // formula
261 
262