lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / form / fmtools.cxx
blob40217b7a71e43fe98bc982b2fadd97bc935138d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include "fmprop.hrc"
22 #include "fmservs.hxx"
23 #include "svx/fmtools.hxx"
24 #include "svx/fmglob.hxx"
26 #include <com/sun/star/awt/LineEndFormat.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/beans/XIntrospection.hpp>
29 #include <com/sun/star/container/XChild.hpp>
30 #include <com/sun/star/form/XForm.hpp>
31 #include <com/sun/star/form/XFormComponent.hpp>
32 #include <com/sun/star/form/XGridColumnFactory.hpp>
33 #include <com/sun/star/io/XActiveDataSink.hpp>
34 #include <com/sun/star/io/XActiveDataSource.hpp>
35 #include <com/sun/star/io/XObjectInputStream.hpp>
36 #include <com/sun/star/io/XObjectOutputStream.hpp>
37 #include <com/sun/star/io/XPersistObject.hpp>
38 #include <com/sun/star/lang/Locale.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/sdb/CommandType.hpp>
41 #include <com/sun/star/sdb/ErrorCondition.hpp>
42 #include <com/sun/star/sdb/ErrorMessageDialog.hpp>
43 #include <com/sun/star/sdb/SQLContext.hpp>
44 #include <com/sun/star/sdb/XCompletedConnection.hpp>
45 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
46 #include <com/sun/star/sdb/XResultSetAccess.hpp>
47 #include <com/sun/star/sdbc/DataType.hpp>
48 #include <com/sun/star/sdbc/XDataSource.hpp>
49 #include <com/sun/star/sdbcx/Privilege.hpp>
50 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
51 #include <com/sun/star/task/XInteractionHandler.hpp>
52 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
53 #include <com/sun/star/uno/XNamingService.hpp>
54 #include <com/sun/star/util/Language.hpp>
55 #include <com/sun/star/util/NumberFormat.hpp>
56 #include <com/sun/star/util/XCloneable.hpp>
57 #include <com/sun/star/util/XNumberFormatTypes.hpp>
58 #include <com/sun/star/util/XNumberFormats.hpp>
59 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
60 #include <com/sun/star/util/XNumberFormatter.hpp>
62 #include <basic/sbxvar.hxx>
63 #include <svl/eitem.hxx>
64 #include <svl/stritem.hxx>
65 #include <comphelper/container.hxx>
66 #include <comphelper/extract.hxx>
67 #include <comphelper/processfactory.hxx>
68 #include <comphelper/property.hxx>
69 #include <comphelper/sequence.hxx>
70 #include <comphelper/types.hxx>
71 #include <comphelper/uno3.hxx>
72 #include <connectivity/dbexception.hxx>
73 #include <connectivity/dbtools.hxx>
74 #include <cppuhelper/typeprovider.hxx>
75 #include <rtl/math.hxx>
76 #include <sfx2/bindings.hxx>
77 #include <toolkit/helper/vclunohelper.hxx>
78 #include <tools/debug.hxx>
79 #include <vcl/stdtext.hxx>
80 #include <vcl/svapp.hxx>
82 #include <algorithm>
84 using namespace ::com::sun::star::uno;
85 using namespace ::com::sun::star::util;
86 using namespace ::com::sun::star::lang;
87 using namespace ::com::sun::star::frame;
88 using namespace ::com::sun::star::awt;
89 using namespace ::com::sun::star::beans;
90 using namespace ::com::sun::star::container;
91 using namespace ::com::sun::star::ui::dialogs;
92 using namespace ::com::sun::star::sdbc;
93 using namespace ::com::sun::star::sdbcx;
94 using namespace ::com::sun::star::sdb;
95 using namespace ::com::sun::star::task;
96 using namespace ::com::sun::star::form;
97 using namespace ::svxform;
100 namespace
102 static bool lcl_shouldDisplayError( const Any& _rError )
104 SQLException aError;
105 if ( !( _rError >>= aError ) )
106 return true;
108 if ( ! aError.Message.startsWith( "[OOoBase]" ) )
109 // it is an exception *not* thrown by an OOo Base core component
110 return true;
112 // the only exception we do not display ATM is a RowSetVetoException, which
113 // has been raised because an XRowSetApprovalListener vetoed a change
114 if ( aError.ErrorCode + ErrorCondition::ROW_SET_OPERATION_VETOED == 0 )
115 return false;
117 // everything else is to be displayed
118 return true;
123 void displayException(const Any& _rExcept, vcl::Window* _pParent)
125 // check whether we need to display it
126 if ( !lcl_shouldDisplayError( _rExcept ) )
127 return;
131 // the parent window
132 vcl::Window* pParentWindow = _pParent ? _pParent : Application::GetDefDialogParent();
133 Reference< XWindow > xParentWindow = VCLUnoHelper::GetInterface(pParentWindow);
135 Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", xParentWindow, _rExcept);
136 xErrorDialog->execute();
138 catch(const Exception&)
140 OSL_FAIL("displayException: could not display the error message!");
145 void displayException(const ::com::sun::star::sdbc::SQLException& _rExcept, vcl::Window* _pParent)
147 displayException(makeAny(_rExcept), _pParent);
151 void displayException(const ::com::sun::star::sdbc::SQLWarning& _rExcept, vcl::Window* _pParent)
153 displayException(makeAny(_rExcept), _pParent);
157 void displayException(const ::com::sun::star::sdb::SQLContext& _rExcept, vcl::Window* _pParent)
159 displayException(makeAny(_rExcept), _pParent);
163 void displayException(const ::com::sun::star::sdb::SQLErrorEvent& _rEvent, vcl::Window* _pParent)
165 displayException(_rEvent.Reason, _pParent);
169 sal_Int32 getElementPos(const Reference< ::com::sun::star::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
171 sal_Int32 nIndex = -1;
172 if (!xCont.is())
173 return nIndex;
176 Reference< XInterface > xNormalized( xElement, UNO_QUERY );
177 DBG_ASSERT( xNormalized.is(), "getElementPos: invalid element!" );
178 if ( xNormalized.is() )
180 // find child position
181 nIndex = xCont->getCount();
182 while (nIndex--)
186 Reference< XInterface > xCurrent(xCont->getByIndex( nIndex ),UNO_QUERY);
187 DBG_ASSERT( xCurrent.get() == Reference< XInterface >( xCurrent, UNO_QUERY ).get(),
188 "getElementPos: container element not normalized!" );
189 if ( xNormalized.get() == xCurrent.get() )
190 break;
192 catch(Exception&)
194 OSL_FAIL( "getElementPos: caught an exception!" );
199 return nIndex;
203 OUString getLabelName(const Reference< ::com::sun::star::beans::XPropertySet>& xControlModel)
205 if (!xControlModel.is())
206 return OUString();
208 if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
210 Reference< ::com::sun::star::beans::XPropertySet> xLabelSet;
211 xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
212 if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
214 Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
215 if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
216 return ::comphelper::getString(aLabel);
220 return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
224 // = CursorWrapper
226 CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor, bool bUseCloned)
228 ImplConstruct(Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY), bUseCloned);
232 CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
234 ImplConstruct(_rxCursor, bUseCloned);
238 void CursorWrapper::ImplConstruct(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
240 if (bUseCloned)
242 Reference< ::com::sun::star::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
245 m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< ::com::sun::star::sdbc::XResultSet>();
247 catch(Exception&)
251 else
252 m_xMoveOperations = _rxCursor;
254 m_xBookmarkOperations.set(m_xMoveOperations, css::uno::UNO_QUERY);
255 m_xColumnsSupplier.set(m_xMoveOperations, css::uno::UNO_QUERY);
256 m_xPropertyAccess.set(m_xMoveOperations, css::uno::UNO_QUERY);
258 if ( !m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is() || !m_xPropertyAccess.is() )
259 { // all or nothing !!
260 m_xMoveOperations = NULL;
261 m_xBookmarkOperations = NULL;
262 m_xColumnsSupplier = NULL;
264 else
265 m_xGeneric = m_xMoveOperations.get();
269 const CursorWrapper& CursorWrapper::operator=(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor)
271 m_xMoveOperations = Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY);
272 m_xBookmarkOperations = Reference< ::com::sun::star::sdbcx::XRowLocate>(_rxCursor, UNO_QUERY);
273 m_xColumnsSupplier = Reference< ::com::sun::star::sdbcx::XColumnsSupplier>(_rxCursor, UNO_QUERY);
274 if (!m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is())
275 { // all or nothing !!
276 m_xMoveOperations = NULL;
277 m_xBookmarkOperations = NULL;
278 m_xColumnsSupplier = NULL;
280 return *this;
284 FmXDisposeListener::~FmXDisposeListener()
286 setAdapter(NULL);
290 void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer* pAdapter)
292 if (m_pAdapter)
294 ::osl::MutexGuard aGuard(m_rMutex);
295 m_pAdapter->release();
296 m_pAdapter = NULL;
299 if (pAdapter)
301 ::osl::MutexGuard aGuard(m_rMutex);
302 m_pAdapter = pAdapter;
303 m_pAdapter->acquire();
309 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< ::com::sun::star::lang::XComponent>& _rxObject, sal_Int16 _nId)
310 :m_xObject(_rxObject)
311 ,m_pListener(_pListener)
312 ,m_nId(_nId)
314 m_pListener->setAdapter(this);
316 if (m_xObject.is())
317 m_xObject->addEventListener(this);
321 FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
325 // ::com::sun::star::lang::XEventListener
327 void FmXDisposeMultiplexer::disposing(const ::com::sun::star::lang::EventObject& _Source) throw( RuntimeException, std::exception )
329 Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
331 if (m_pListener)
333 m_pListener->disposing(_Source, m_nId);
334 m_pListener->setAdapter(NULL);
335 m_pListener = NULL;
337 m_xObject = NULL;
341 void FmXDisposeMultiplexer::dispose()
343 if (m_xObject.is())
345 Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
347 m_xObject->removeEventListener(this);
348 m_xObject = NULL;
350 m_pListener->setAdapter(NULL);
351 m_pListener = NULL;
357 sal_Int16 getControlTypeByObject(const Reference< ::com::sun::star::lang::XServiceInfo>& _rxObject)
359 // ask for the persistent service name
360 Reference< ::com::sun::star::io::XPersistObject> xPersistence(_rxObject, UNO_QUERY);
361 DBG_ASSERT(xPersistence.is(), "::getControlTypeByObject : argument shold be an ::com::sun::star::io::XPersistObject !");
362 if (!xPersistence.is())
363 return OBJ_FM_CONTROL;
365 OUString sPersistentServiceName = xPersistence->getServiceName();
366 if (sPersistentServiceName == FM_COMPONENT_EDIT) // 5.0-Name
368 // may be a simple edit field or a formatted field, dependent of the supported services
369 if (_rxObject->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD))
370 return OBJ_FM_FORMATTEDFIELD;
371 return OBJ_FM_EDIT;
373 if (sPersistentServiceName == FM_COMPONENT_TEXTFIELD)
374 return OBJ_FM_EDIT;
375 if (sPersistentServiceName == FM_COMPONENT_COMMANDBUTTON)
376 return OBJ_FM_BUTTON;
377 if (sPersistentServiceName == FM_COMPONENT_FIXEDTEXT)
378 return OBJ_FM_FIXEDTEXT;
379 if (sPersistentServiceName == FM_COMPONENT_LISTBOX)
380 return OBJ_FM_LISTBOX;
381 if (sPersistentServiceName == FM_COMPONENT_CHECKBOX)
382 return OBJ_FM_CHECKBOX;
383 if (sPersistentServiceName == FM_COMPONENT_RADIOBUTTON)
384 return OBJ_FM_RADIOBUTTON;
385 if (sPersistentServiceName == FM_COMPONENT_GROUPBOX)
386 return OBJ_FM_GROUPBOX;
387 if (sPersistentServiceName == FM_COMPONENT_COMBOBOX)
388 return OBJ_FM_COMBOBOX;
389 if (sPersistentServiceName == FM_COMPONENT_GRID) // 5.0-Name
390 return OBJ_FM_GRID;
391 if (sPersistentServiceName == FM_COMPONENT_GRIDCONTROL)
392 return OBJ_FM_GRID;
393 if (sPersistentServiceName == FM_COMPONENT_IMAGEBUTTON)
394 return OBJ_FM_IMAGEBUTTON;
395 if (sPersistentServiceName == FM_COMPONENT_FILECONTROL)
396 return OBJ_FM_FILECONTROL;
397 if (sPersistentServiceName == FM_COMPONENT_DATEFIELD)
398 return OBJ_FM_DATEFIELD;
399 if (sPersistentServiceName == FM_COMPONENT_TIMEFIELD)
400 return OBJ_FM_TIMEFIELD;
401 if (sPersistentServiceName == FM_COMPONENT_NUMERICFIELD)
402 return OBJ_FM_NUMERICFIELD;
403 if (sPersistentServiceName == FM_COMPONENT_CURRENCYFIELD)
404 return OBJ_FM_CURRENCYFIELD;
405 if (sPersistentServiceName == FM_COMPONENT_PATTERNFIELD)
406 return OBJ_FM_PATTERNFIELD;
407 if (sPersistentServiceName == FM_COMPONENT_HIDDEN) // 5.0-Name
408 return OBJ_FM_HIDDEN;
409 if (sPersistentServiceName == FM_COMPONENT_HIDDENCONTROL)
410 return OBJ_FM_HIDDEN;
411 if (sPersistentServiceName == FM_COMPONENT_IMAGECONTROL)
412 return OBJ_FM_IMAGECONTROL;
413 if (sPersistentServiceName == FM_COMPONENT_FORMATTEDFIELD)
415 OSL_FAIL("::getControlTypeByObject : suspicious persistent service name (formatted field) !");
416 // objects with that service name should exist as they aren't compatible with older versions
417 return OBJ_FM_FORMATTEDFIELD;
419 if ( sPersistentServiceName == FM_SUN_COMPONENT_SCROLLBAR )
420 return OBJ_FM_SCROLLBAR;
421 if ( sPersistentServiceName == FM_SUN_COMPONENT_SPINBUTTON )
422 return OBJ_FM_SPINBUTTON;
423 if ( sPersistentServiceName == FM_SUN_COMPONENT_NAVIGATIONBAR )
424 return OBJ_FM_NAVIGATIONBAR;
426 OSL_FAIL("::getControlTypeByObject : unknown object type !");
427 return OBJ_FM_CONTROL;
431 bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
433 bool bIsAlive = false;
434 Reference< ::com::sun::star::sdbcx::XColumnsSupplier> xSupplyCols(_rxRowSet, UNO_QUERY);
435 Reference< ::com::sun::star::container::XIndexAccess> xCols;
436 if (xSupplyCols.is())
437 xCols = Reference< ::com::sun::star::container::XIndexAccess>(xSupplyCols->getColumns(), UNO_QUERY);
438 if (xCols.is() && (xCols->getCount() > 0))
439 bIsAlive = true;
441 return bIsAlive;
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */