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 <o3tl/string_view.hxx>
26 #include <osl/diagnose.h>
27 #include <comphelper/diagnose_ex.hxx>
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::form
;
41 const sal_Int32 s_nFirstVirtualButtonType
= 1 + sal_Int32(FormButtonType_URL
);
43 const 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 sal_Int32
lcl_getNavigationURLIndex( std::u16string_view _rNavURL
)
59 const char** pLookup
= pNavigationURLs
;
62 if ( o3tl::equalsAscii( _rNavURL
, *pLookup
) )
63 return pLookup
- pNavigationURLs
;
69 const char* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex
)
71 const char** pLookup
= pNavigationURLs
;
72 while ( _nButtonTypeIndex
-- && *pLookup
++ )
74 OSL_ENSURE( *pLookup
, "lcl_getNavigationURL: invalid index!" );
80 //= PushButtonNavigation
83 PushButtonNavigation::PushButtonNavigation( const Reference
< XPropertySet
>& _rxControlModel
)
84 :m_xControlModel( _rxControlModel
)
85 ,m_bIsPushButton( 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 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::PushButtonNavigation" );
100 FormButtonType
PushButtonNavigation::implGetCurrentButtonType() const
102 sal_Int32 nButtonType
= sal_Int32(FormButtonType_PUSH
);
103 if ( !m_xControlModel
.is() )
104 return static_cast<FormButtonType
>(nButtonType
);
105 OSL_VERIFY( ::cppu::enum2int( nButtonType
, m_xControlModel
->getPropertyValue( PROPERTY_BUTTONTYPE
) ) );
107 if ( nButtonType
== sal_Int32(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
;
119 return static_cast<FormButtonType
>(nButtonType
);
123 Any
PushButtonNavigation::getCurrentButtonType() const
125 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
130 aReturn
<<= implGetCurrentButtonType();
132 catch( const Exception
& )
134 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentButtonType" );
140 void PushButtonNavigation::setCurrentButtonType( const Any
& _rValue
) const
142 OSL_ENSURE( m_bIsPushButton
, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
143 if ( !m_xControlModel
.is() )
148 sal_Int32 nButtonType
= sal_Int32(FormButtonType_PUSH
);
149 OSL_VERIFY( ::cppu::enum2int( nButtonType
, _rValue
) );
152 bool bIsVirtualButtonType
= nButtonType
>= s_nFirstVirtualButtonType
;
153 if ( bIsVirtualButtonType
)
155 const char* pURL
= lcl_getNavigationURL( nButtonType
- s_nFirstVirtualButtonType
);
156 sTargetURL
= OUString::createFromAscii( pURL
);
158 nButtonType
= sal_Int32(FormButtonType_URL
);
161 m_xControlModel
->setPropertyValue( PROPERTY_BUTTONTYPE
, Any( static_cast< FormButtonType
>( nButtonType
) ) );
162 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, Any( sTargetURL
) );
164 catch( const Exception
& )
166 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentButtonType" );
171 PropertyState
PushButtonNavigation::getCurrentButtonTypeState( ) const
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
= sal_Int32(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 ( sal_Int32(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 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentButtonTypeState" );
205 Any
PushButtonNavigation::getCurrentTargetURL() const
208 if ( !m_xControlModel
.is() )
213 aReturn
= m_xControlModel
->getPropertyValue( PROPERTY_TARGET_URL
);
214 if ( m_bIsPushButton
)
216 FormButtonType nCurrentButtonType
= implGetCurrentButtonType();
217 bool bIsVirtualButtonType
= nCurrentButtonType
>= FormButtonType(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 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentTargetURL" );
235 void PushButtonNavigation::setCurrentTargetURL( const Any
& _rValue
) const
237 if ( !m_xControlModel
.is() )
242 m_xControlModel
->setPropertyValue( PROPERTY_TARGET_URL
, _rValue
);
244 catch( const Exception
& )
246 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentTargetURL" );
251 PropertyState
PushButtonNavigation::getCurrentTargetURLState( ) const
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 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentTargetURL" );
272 bool PushButtonNavigation::currentButtonTypeIsOpenURL() const
274 FormButtonType
nButtonType( FormButtonType_PUSH
);
277 nButtonType
= implGetCurrentButtonType();
279 catch( const Exception
& )
281 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
283 return nButtonType
== FormButtonType_URL
;
287 bool PushButtonNavigation::hasNonEmptyCurrentTargetURL() const
290 OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL
);
291 return !sTargetURL
.isEmpty();
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */