1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "pushbuttonnavigation.hxx"
21 #include <com/sun/star/form/FormButtonType.hpp>
22 #include <com/sun/star/beans/XPropertyState.hpp>
23 #include "formstrings.hxx"
24 #include <comphelper/extract.hxx>
25 #include <comphelper/property.hxx>
26 #include <osl/diagnose.h>
27 #include <tools/diagnose_ex.h>
29 //............................................................................
32 //............................................................................
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::form
;
38 //------------------------------------------------------------------------
41 static const sal_Int32 s_nFirstVirtualButtonType
= 1 + (sal_Int32
)FormButtonType_URL
;
43 static const sal_Char
* pNavigationURLs
[] =
45 ".uno:FormController/moveToFirst",
46 ".uno:FormController/moveToPrev",
47 ".uno:FormController/moveToNext",
48 ".uno:FormController/moveToLast",
49 ".uno:FormController/saveRecord",
50 ".uno:FormController/undoRecord",
51 ".uno:FormController/moveToNew",
52 ".uno:FormController/deleteRecord",
53 ".uno:FormController/refreshForm",
57 static sal_Int32
lcl_getNavigationURLIndex( const OUString
& _rNavURL
)
59 const sal_Char
** pLookup
= pNavigationURLs
;
62 if ( _rNavURL
.equalsAscii( *pLookup
) )
63 return pLookup
- pNavigationURLs
;
69 static const sal_Char
* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex
)
71 const sal_Char
** pLookup
= pNavigationURLs
;
72 while ( _nButtonTypeIndex
-- && *pLookup
++ )
74 OSL_ENSURE( *pLookup
, "lcl_getNavigationURL: invalid index!" );
79 //========================================================================
80 //= PushButtonNavigation
81 //========================================================================
82 //------------------------------------------------------------------------
83 PushButtonNavigation::PushButtonNavigation( const Reference
< XPropertySet
>& _rxControlModel
)
84 :m_xControlModel( _rxControlModel
)
85 ,m_bIsPushButton( sal_False
)
87 OSL_ENSURE( m_xControlModel
.is(), "PushButtonNavigation::PushButtonNavigation: invalid control model!" );
91 m_bIsPushButton
= ::comphelper::hasProperty( PROPERTY_BUTTONTYPE
, m_xControlModel
);
93 catch( const Exception
& )
95 OSL_FAIL( "PushButtonNavigation::PushButtonNavigation: caught an exception!" );
99 //------------------------------------------------------------------------
100 sal_Int32
PushButtonNavigation::implGetCurrentButtonType() const SAL_THROW((Exception
))
102 sal_Int32 nButtonType
= FormButtonType_PUSH
;
103 if ( !m_xControlModel
.is() )
105 OSL_VERIFY( ::cppu::enum2int( nButtonType
, m_xControlModel
->getPropertyValue( PROPERTY_BUTTONTYPE
) ) );
107 if ( nButtonType
== FormButtonType_URL
)
109 // there's a chance that this is a "virtual" button type
110 // (which are realized by special URLs)
112 m_xControlModel
->getPropertyValue( PROPERTY_TARGET_URL
) >>= sTargetURL
;
114 sal_Int32 nNavigationURLIndex
= lcl_getNavigationURLIndex( sTargetURL
);
115 if ( nNavigationURLIndex
>= 0)
116 // it actually *is* a virtual button type
117 nButtonType
= s_nFirstVirtualButtonType
+ nNavigationURLIndex
;
122 //------------------------------------------------------------------------
123 Any
PushButtonNavigation::getCurrentButtonType() const SAL_THROW(())
125 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
130 aReturn
<<= implGetCurrentButtonType();
132 catch( const Exception
& )
134 OSL_FAIL( "PushButtonNavigation::getCurrentButtonType: caught an exception!" );
139 //------------------------------------------------------------------------
140 void PushButtonNavigation::setCurrentButtonType( const Any
& _rValue
) const SAL_THROW(())
142 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
143 if ( !m_xControlModel
.is() )
148 sal_Int32 nButtonType
= FormButtonType_PUSH
;
149 OSL_VERIFY( ::cppu::enum2int( nButtonType
, _rValue
) );
152 bool bIsVirtualButtonType
= nButtonType
>= s_nFirstVirtualButtonType
;
153 if ( bIsVirtualButtonType
)
155 const sal_Char
* pURL
= lcl_getNavigationURL( nButtonType
- s_nFirstVirtualButtonType
);
156 sTargetURL
= OUString::createFromAscii( pURL
);
158 nButtonType
= FormButtonType_URL
;
161 m_xControlModel
->setPropertyValue( PROPERTY_BUTTONTYPE
, makeAny( static_cast< FormButtonType
>( nButtonType
) ) );
162 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, makeAny( sTargetURL
) );
164 catch( const Exception
& )
166 OSL_FAIL( "PushButtonNavigation::setCurrentButtonType: caught an exception!" );
170 //------------------------------------------------------------------------
171 PropertyState
PushButtonNavigation::getCurrentButtonTypeState( ) const SAL_THROW(())
173 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::getCurrentButtonTypeState: not expected to be called for forms!" );
174 PropertyState eState
= PropertyState_DIRECT_VALUE
;
178 Reference
< XPropertyState
> xStateAccess( m_xControlModel
, UNO_QUERY
);
179 if ( xStateAccess
.is() )
181 // let's see what the model says about the ButtonType property
182 eState
= xStateAccess
->getPropertyState( PROPERTY_BUTTONTYPE
);
183 if ( eState
== PropertyState_DIRECT_VALUE
)
185 sal_Int32 nRealButtonType
= FormButtonType_PUSH
;
186 OSL_VERIFY( ::cppu::enum2int( nRealButtonType
, m_xControlModel
->getPropertyValue( PROPERTY_BUTTONTYPE
) ) );
187 // perhaps it's one of the virtual button types?
188 if ( FormButtonType_URL
== nRealButtonType
)
190 // yes, it is -> rely on the state of the URL property
191 eState
= xStateAccess
->getPropertyState( PROPERTY_TARGET_URL
);
196 catch( const Exception
& )
198 OSL_FAIL( "PushButtonNavigation::getCurrentButtonTypeState: caught an exception!" );
204 //------------------------------------------------------------------------
205 Any
PushButtonNavigation::getCurrentTargetURL() const SAL_THROW(())
208 if ( !m_xControlModel
.is() )
213 aReturn
= m_xControlModel
->getPropertyValue( PROPERTY_TARGET_URL
);
214 if ( m_bIsPushButton
)
216 sal_Int32 nCurrentButtonType
= implGetCurrentButtonType();
217 bool bIsVirtualButtonType
= nCurrentButtonType
>= s_nFirstVirtualButtonType
;
218 if ( bIsVirtualButtonType
)
220 // pretend (to the user) that there's no URL set - since
221 // virtual button types imply a special (technical) URL which
222 // the user should not see
223 aReturn
<<= OUString();
227 catch( const Exception
& )
229 OSL_FAIL( "PushButtonNavigation::getCurrentTargetURL: caught an exception!" );
234 //------------------------------------------------------------------------
235 void PushButtonNavigation::setCurrentTargetURL( const Any
& _rValue
) const SAL_THROW(())
237 if ( !m_xControlModel
.is() )
242 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, _rValue
);
244 catch( const Exception
& )
246 OSL_FAIL( "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
250 //------------------------------------------------------------------------
251 PropertyState
PushButtonNavigation::getCurrentTargetURLState( ) const SAL_THROW(())
253 PropertyState eState
= PropertyState_DIRECT_VALUE
;
257 Reference
< XPropertyState
> xStateAccess( m_xControlModel
, UNO_QUERY
);
258 if ( xStateAccess
.is() )
260 eState
= xStateAccess
->getPropertyState( PROPERTY_TARGET_URL
);
263 catch( const Exception
& )
265 OSL_FAIL( "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
271 //------------------------------------------------------------------------
272 bool PushButtonNavigation::currentButtonTypeIsOpenURL() const
274 sal_Int32
nButtonType( FormButtonType_PUSH
);
277 nButtonType
= implGetCurrentButtonType();
279 catch( const Exception
& )
281 DBG_UNHANDLED_EXCEPTION();
283 return nButtonType
== FormButtonType_URL
;
286 //------------------------------------------------------------------------
287 bool PushButtonNavigation::hasNonEmptyCurrentTargetURL() const
290 OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL
);
291 return !sTargetURL
.isEmpty();
294 //............................................................................
296 //............................................................................
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */