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/beans/XPropertyState.hpp>
22 #include "formstrings.hxx"
23 #include <comphelper/extract.hxx>
24 #include <comphelper/property.hxx>
25 #include <osl/diagnose.h>
26 #include <tools/diagnose_ex.h>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::form
;
40 static const sal_Int32 s_nFirstVirtualButtonType
= 1 + sal_Int32(FormButtonType_URL
);
42 static const sal_Char
* pNavigationURLs
[] =
44 ".uno:FormController/moveToFirst",
45 ".uno:FormController/moveToPrev",
46 ".uno:FormController/moveToNext",
47 ".uno:FormController/moveToLast",
48 ".uno:FormController/saveRecord",
49 ".uno:FormController/undoRecord",
50 ".uno:FormController/moveToNew",
51 ".uno:FormController/deleteRecord",
52 ".uno:FormController/refreshForm",
56 sal_Int32
lcl_getNavigationURLIndex( const OUString
& _rNavURL
)
58 const sal_Char
** pLookup
= pNavigationURLs
;
61 if ( _rNavURL
.equalsAscii( *pLookup
) )
62 return pLookup
- pNavigationURLs
;
68 const sal_Char
* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex
)
70 const sal_Char
** pLookup
= pNavigationURLs
;
71 while ( _nButtonTypeIndex
-- && *pLookup
++ )
73 OSL_ENSURE( *pLookup
, "lcl_getNavigationURL: invalid index!" );
79 //= PushButtonNavigation
82 PushButtonNavigation::PushButtonNavigation( const Reference
< XPropertySet
>& _rxControlModel
)
83 :m_xControlModel( _rxControlModel
)
84 ,m_bIsPushButton( false )
86 OSL_ENSURE( m_xControlModel
.is(), "PushButtonNavigation::PushButtonNavigation: invalid control model!" );
90 m_bIsPushButton
= ::comphelper::hasProperty( PROPERTY_BUTTONTYPE
, m_xControlModel
);
92 catch( const Exception
& )
94 OSL_FAIL( "PushButtonNavigation::PushButtonNavigation: caught an exception!" );
99 FormButtonType
PushButtonNavigation::implGetCurrentButtonType() const
101 sal_Int32 nButtonType
= sal_Int32(FormButtonType_PUSH
);
102 if ( !m_xControlModel
.is() )
103 return static_cast<FormButtonType
>(nButtonType
);
104 OSL_VERIFY( ::cppu::enum2int( nButtonType
, m_xControlModel
->getPropertyValue( PROPERTY_BUTTONTYPE
) ) );
106 if ( nButtonType
== sal_Int32(FormButtonType_URL
) )
108 // there's a chance that this is a "virtual" button type
109 // (which are realized by special URLs)
111 m_xControlModel
->getPropertyValue( PROPERTY_TARGET_URL
) >>= sTargetURL
;
113 sal_Int32 nNavigationURLIndex
= lcl_getNavigationURLIndex( sTargetURL
);
114 if ( nNavigationURLIndex
>= 0)
115 // it actually *is* a virtual button type
116 nButtonType
= s_nFirstVirtualButtonType
+ nNavigationURLIndex
;
118 return static_cast<FormButtonType
>(nButtonType
);
122 Any
PushButtonNavigation::getCurrentButtonType() const
124 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
129 aReturn
<<= implGetCurrentButtonType();
131 catch( const Exception
& )
133 OSL_FAIL( "PushButtonNavigation::getCurrentButtonType: caught an exception!" );
139 void PushButtonNavigation::setCurrentButtonType( const Any
& _rValue
) const
141 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
142 if ( !m_xControlModel
.is() )
147 sal_Int32 nButtonType
= sal_Int32(FormButtonType_PUSH
);
148 OSL_VERIFY( ::cppu::enum2int( nButtonType
, _rValue
) );
151 bool bIsVirtualButtonType
= nButtonType
>= s_nFirstVirtualButtonType
;
152 if ( bIsVirtualButtonType
)
154 const sal_Char
* pURL
= lcl_getNavigationURL( nButtonType
- s_nFirstVirtualButtonType
);
155 sTargetURL
= OUString::createFromAscii( pURL
);
157 nButtonType
= sal_Int32(FormButtonType_URL
);
160 m_xControlModel
->setPropertyValue( PROPERTY_BUTTONTYPE
, makeAny( static_cast< FormButtonType
>( nButtonType
) ) );
161 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, makeAny( sTargetURL
) );
163 catch( const Exception
& )
165 OSL_FAIL( "PushButtonNavigation::setCurrentButtonType: caught an exception!" );
170 PropertyState
PushButtonNavigation::getCurrentButtonTypeState( ) const
172 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::getCurrentButtonTypeState: not expected to be called for forms!" );
173 PropertyState eState
= PropertyState_DIRECT_VALUE
;
177 Reference
< XPropertyState
> xStateAccess( m_xControlModel
, UNO_QUERY
);
178 if ( xStateAccess
.is() )
180 // let's see what the model says about the ButtonType property
181 eState
= xStateAccess
->getPropertyState( PROPERTY_BUTTONTYPE
);
182 if ( eState
== PropertyState_DIRECT_VALUE
)
184 sal_Int32 nRealButtonType
= sal_Int32(FormButtonType_PUSH
);
185 OSL_VERIFY( ::cppu::enum2int( nRealButtonType
, m_xControlModel
->getPropertyValue( PROPERTY_BUTTONTYPE
) ) );
186 // perhaps it's one of the virtual button types?
187 if ( sal_Int32(FormButtonType_URL
) == nRealButtonType
)
189 // yes, it is -> rely on the state of the URL property
190 eState
= xStateAccess
->getPropertyState( PROPERTY_TARGET_URL
);
195 catch( const Exception
& )
197 OSL_FAIL( "PushButtonNavigation::getCurrentButtonTypeState: caught an exception!" );
204 Any
PushButtonNavigation::getCurrentTargetURL() const
207 if ( !m_xControlModel
.is() )
212 aReturn
= m_xControlModel
->getPropertyValue( PROPERTY_TARGET_URL
);
213 if ( m_bIsPushButton
)
215 FormButtonType nCurrentButtonType
= implGetCurrentButtonType();
216 bool bIsVirtualButtonType
= nCurrentButtonType
>= FormButtonType(s_nFirstVirtualButtonType
);
217 if ( bIsVirtualButtonType
)
219 // pretend (to the user) that there's no URL set - since
220 // virtual button types imply a special (technical) URL which
221 // the user should not see
222 aReturn
<<= OUString();
226 catch( const Exception
& )
228 OSL_FAIL( "PushButtonNavigation::getCurrentTargetURL: caught an exception!" );
234 void PushButtonNavigation::setCurrentTargetURL( const Any
& _rValue
) const
236 if ( !m_xControlModel
.is() )
241 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, _rValue
);
243 catch( const Exception
& )
245 OSL_FAIL( "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
250 PropertyState
PushButtonNavigation::getCurrentTargetURLState( ) const
252 PropertyState eState
= PropertyState_DIRECT_VALUE
;
256 Reference
< XPropertyState
> xStateAccess( m_xControlModel
, UNO_QUERY
);
257 if ( xStateAccess
.is() )
259 eState
= xStateAccess
->getPropertyState( PROPERTY_TARGET_URL
);
262 catch( const Exception
& )
264 OSL_FAIL( "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
271 bool PushButtonNavigation::currentButtonTypeIsOpenURL() const
273 FormButtonType
nButtonType( FormButtonType_PUSH
);
276 nButtonType
= implGetCurrentButtonType();
278 catch( const Exception
& )
280 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
282 return nButtonType
== FormButtonType_URL
;
286 bool PushButtonNavigation::hasNonEmptyCurrentTargetURL() const
289 OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL
);
290 return !sTargetURL
.isEmpty();
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */