1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pushbuttonnavigation.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "pushbuttonnavigation.hxx"
34 #include <com/sun/star/form/FormButtonType.hpp>
35 #include <com/sun/star/beans/XPropertyState.hpp>
36 #include "formstrings.hxx"
37 #include <cppuhelper/extract.hxx>
38 #include <comphelper/property.hxx>
39 #include <osl/diagnose.h>
40 #include <tools/diagnose_ex.h>
42 //............................................................................
45 //............................................................................
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::form
;
51 //------------------------------------------------------------------------
54 static const sal_Int32 s_nFirstVirtualButtonType
= 1 + (sal_Int32
)FormButtonType_URL
;
56 static const sal_Char
* pNavigationURLs
[] =
58 ".uno:FormController/moveToFirst",
59 ".uno:FormController/moveToPrev",
60 ".uno:FormController/moveToNext",
61 ".uno:FormController/moveToLast",
62 ".uno:FormController/saveRecord",
63 ".uno:FormController/undoRecord",
64 ".uno:FormController/moveToNew",
65 ".uno:FormController/deleteRecord",
66 ".uno:FormController/refreshForm",
70 static sal_Int32
lcl_getNavigationURLIndex( const ::rtl::OUString
& _rNavURL
)
72 const sal_Char
** pLookup
= pNavigationURLs
;
75 if ( _rNavURL
.equalsAscii( *pLookup
) )
76 return pLookup
- pNavigationURLs
;
82 static const sal_Char
* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex
)
84 const sal_Char
** pLookup
= pNavigationURLs
;
85 while ( _nButtonTypeIndex
-- && *pLookup
++ )
87 OSL_ENSURE( *pLookup
, "lcl_getNavigationURL: invalid index!" );
92 //========================================================================
93 //= PushButtonNavigation
94 //========================================================================
95 //------------------------------------------------------------------------
96 PushButtonNavigation::PushButtonNavigation( const Reference
< XPropertySet
>& _rxControlModel
)
97 :m_xControlModel( _rxControlModel
)
98 ,m_bIsPushButton( sal_False
)
100 OSL_ENSURE( m_xControlModel
.is(), "PushButtonNavigation::PushButtonNavigation: invalid control model!" );
104 m_bIsPushButton
= ::comphelper::hasProperty( PROPERTY_BUTTONTYPE
, m_xControlModel
);
106 catch( const Exception
& )
108 OSL_ENSURE( sal_False
, "PushButtonNavigation::PushButtonNavigation: caught an exception!" );
112 //------------------------------------------------------------------------
113 sal_Int32
PushButtonNavigation::implGetCurrentButtonType() const SAL_THROW((Exception
))
115 sal_Int32 nButtonType
= FormButtonType_PUSH
;
116 if ( !m_xControlModel
.is() )
118 OSL_VERIFY( ::cppu::enum2int( nButtonType
, m_xControlModel
->getPropertyValue( PROPERTY_BUTTONTYPE
) ) );
120 if ( nButtonType
== FormButtonType_URL
)
122 // there's a chance that this is a "virtual" button type
123 // (which are realized by special URLs)
124 ::rtl::OUString sTargetURL
;
125 m_xControlModel
->getPropertyValue( PROPERTY_TARGET_URL
) >>= sTargetURL
;
127 sal_Int32 nNavigationURLIndex
= lcl_getNavigationURLIndex( sTargetURL
);
128 if ( nNavigationURLIndex
>= 0)
129 // it actually *is* a virtual button type
130 nButtonType
= s_nFirstVirtualButtonType
+ nNavigationURLIndex
;
135 //------------------------------------------------------------------------
136 Any
PushButtonNavigation::getCurrentButtonType() const SAL_THROW(())
138 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
143 aReturn
<<= implGetCurrentButtonType();
145 catch( const Exception
& )
147 OSL_ENSURE( sal_False
, "PushButtonNavigation::getCurrentButtonType: caught an exception!" );
152 //------------------------------------------------------------------------
153 void PushButtonNavigation::setCurrentButtonType( const Any
& _rValue
) const SAL_THROW(())
155 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
156 if ( !m_xControlModel
.is() )
161 sal_Int32 nButtonType
= FormButtonType_PUSH
;
162 OSL_VERIFY( ::cppu::enum2int( nButtonType
, _rValue
) );
163 ::rtl::OUString sTargetURL
;
165 bool bIsVirtualButtonType
= nButtonType
>= s_nFirstVirtualButtonType
;
166 if ( bIsVirtualButtonType
)
168 const sal_Char
* pURL
= lcl_getNavigationURL( nButtonType
- s_nFirstVirtualButtonType
);
169 sTargetURL
= ::rtl::OUString::createFromAscii( pURL
);
171 nButtonType
= FormButtonType_URL
;
174 m_xControlModel
->setPropertyValue( PROPERTY_BUTTONTYPE
, makeAny( static_cast< FormButtonType
>( nButtonType
) ) );
175 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, makeAny( sTargetURL
) );
177 catch( const Exception
& )
179 OSL_ENSURE( sal_False
, "PushButtonNavigation::setCurrentButtonType: caught an exception!" );
183 //------------------------------------------------------------------------
184 PropertyState
PushButtonNavigation::getCurrentButtonTypeState( ) const SAL_THROW(())
186 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::getCurrentButtonTypeState: not expected to be called for forms!" );
187 PropertyState eState
= PropertyState_DIRECT_VALUE
;
191 Reference
< XPropertyState
> xStateAccess( m_xControlModel
, UNO_QUERY
);
192 if ( xStateAccess
.is() )
194 // let's see what the model says about the ButtonType property
195 eState
= xStateAccess
->getPropertyState( PROPERTY_BUTTONTYPE
);
196 if ( eState
== PropertyState_DIRECT_VALUE
)
198 sal_Int32 nRealButtonType
= FormButtonType_PUSH
;
199 OSL_VERIFY( ::cppu::enum2int( nRealButtonType
, m_xControlModel
->getPropertyValue( PROPERTY_BUTTONTYPE
) ) );
200 // perhaps it's one of the virtual button types?
201 if ( FormButtonType_URL
== nRealButtonType
)
203 // yes, it is -> rely on the state of the URL property
204 eState
= xStateAccess
->getPropertyState( PROPERTY_TARGET_URL
);
209 catch( const Exception
& )
211 OSL_ENSURE( sal_False
, "PushButtonNavigation::getCurrentButtonTypeState: caught an exception!" );
217 //------------------------------------------------------------------------
218 Any
PushButtonNavigation::getCurrentTargetURL() const SAL_THROW(())
221 if ( !m_xControlModel
.is() )
226 aReturn
= m_xControlModel
->getPropertyValue( PROPERTY_TARGET_URL
);
227 if ( m_bIsPushButton
)
229 sal_Int32 nCurrentButtonType
= implGetCurrentButtonType();
230 bool bIsVirtualButtonType
= nCurrentButtonType
>= s_nFirstVirtualButtonType
;
231 if ( bIsVirtualButtonType
)
233 // pretend (to the user) that there's no URL set - since
234 // virtual button types imply a special (technical) URL which
235 // the user should not see
236 aReturn
<<= ::rtl::OUString();
240 catch( const Exception
& )
242 OSL_ENSURE( sal_False
, "PushButtonNavigation::getCurrentTargetURL: caught an exception!" );
247 //------------------------------------------------------------------------
248 void PushButtonNavigation::setCurrentTargetURL( const Any
& _rValue
) const SAL_THROW(())
250 if ( !m_xControlModel
.is() )
255 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, _rValue
);
257 catch( const Exception
& )
259 OSL_ENSURE( sal_False
, "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
263 //------------------------------------------------------------------------
264 PropertyState
PushButtonNavigation::getCurrentTargetURLState( ) const SAL_THROW(())
266 PropertyState eState
= PropertyState_DIRECT_VALUE
;
270 Reference
< XPropertyState
> xStateAccess( m_xControlModel
, UNO_QUERY
);
271 if ( xStateAccess
.is() )
273 eState
= xStateAccess
->getPropertyState( PROPERTY_TARGET_URL
);
276 catch( const Exception
& )
278 OSL_ENSURE( sal_False
, "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
284 //------------------------------------------------------------------------
285 bool PushButtonNavigation::currentButtonTypeIsOpenURL() const
287 sal_Int32
nButtonType( FormButtonType_PUSH
);
290 nButtonType
= implGetCurrentButtonType();
292 catch( const Exception
& )
294 DBG_UNHANDLED_EXCEPTION();
296 return nButtonType
== FormButtonType_URL
;
299 //------------------------------------------------------------------------
300 bool PushButtonNavigation::hasNonEmptyCurrentTargetURL() const
302 ::rtl::OUString sTargetURL
;
303 OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL
);
304 return sTargetURL
.getLength() != 0;
307 //............................................................................
309 //............................................................................