nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / ui / browser / sbagrid.cxx
blobf5c909600f54e0c492ae890a84f1812913ccc9cd
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 .
20 #include <core_resource.hxx>
21 #include <helpids.h>
23 #include <sot/exchange.hxx>
25 #include <svx/dbaexchange.hxx>
26 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
28 #include <sbagrid.hxx>
29 #include <dlgsize.hxx>
30 #include <com/sun/star/beans/XPropertyState.hpp>
31 #include <com/sun/star/form/XForm.hpp>
32 #include <com/sun/star/container/XIndexContainer.hpp>
34 #include <com/sun/star/view/XSelectionSupplier.hpp>
35 #include <com/sun/star/awt/XTextComponent.hpp>
36 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
37 #include <tools/diagnose_ex.h>
39 #include <svl/numuno.hxx>
40 #include <toolkit/helper/vclunohelper.hxx>
42 #include <vcl/svapp.hxx>
44 #include <svl/zforlist.hxx>
45 #include <cppuhelper/queryinterface.hxx>
46 #include <connectivity/dbtools.hxx>
47 #include <comphelper/types.hxx>
48 #include <com/sun/star/sdbc/DataType.hpp>
49 #include <com/sun/star/sdbc/SQLException.hpp>
50 #include <browserids.hxx>
51 #include <strings.hrc>
52 #include <strings.hxx>
53 #include <dbexchange.hxx>
54 #include <TableRowExchange.hxx>
55 #include <TableRow.hxx>
56 #include <svtools/stringtransfer.hxx>
57 #include <UITools.hxx>
58 #include <TokenWriter.hxx>
59 #include <osl/diagnose.h>
60 #include <algorithm>
62 using namespace ::com::sun::star::ui::dialogs;
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::sdb;
65 using namespace ::com::sun::star::sdbc;
66 using namespace ::com::sun::star::sdbcx;
67 using namespace ::com::sun::star::beans;
68 using namespace ::com::sun::star::container;
69 using namespace ::com::sun::star::datatransfer;
70 using namespace ::com::sun::star::lang;
71 using namespace ::com::sun::star::view;
72 using namespace ::com::sun::star::form;
73 using namespace ::com::sun::star::frame;
74 using namespace ::com::sun::star::util;
75 using namespace ::dbaui;
76 using namespace ::dbtools;
77 using namespace ::svx;
78 using namespace ::svt;
80 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
81 com_sun_star_comp_dbu_SbaXGridControl_get_implementation(
82 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
84 return cppu::acquire(new SbaXGridControl(context));
87 css::uno::Sequence<OUString> SAL_CALL SbaXGridControl::getSupportedServiceNames()
89 return { "com.sun.star.form.control.InteractionGridControl", "com.sun.star.form.control.GridControl",
90 "com.sun.star.awt.UnoControl" };
94 // SbaXGridControl
96 OUString SAL_CALL SbaXGridControl::getImplementationName()
98 return "com.sun.star.comp.dbu.SbaXGridControl";
101 SbaXGridControl::SbaXGridControl(const Reference< XComponentContext >& _rM)
102 : FmXGridControl(_rM)
106 SbaXGridControl::~SbaXGridControl()
110 FmXGridPeer* SbaXGridControl::imp_CreatePeer(vcl::Window* pParent)
112 FmXGridPeer* pReturn = new SbaXGridPeer(m_xContext);
114 // translate properties into WinBits
115 WinBits nStyle = WB_TABSTOP;
116 Reference< XPropertySet > xModelSet(getModel(), UNO_QUERY);
117 if (xModelSet.is())
121 if (::comphelper::getINT16(xModelSet->getPropertyValue(PROPERTY_BORDER)))
122 nStyle |= WB_BORDER;
124 catch(Exception&)
130 pReturn->Create(pParent, nStyle);
131 return pReturn;
134 Any SAL_CALL SbaXGridControl::queryInterface(const Type& _rType)
136 Any aRet = FmXGridControl::queryInterface(_rType);
137 return aRet.hasValue() ? aRet : ::cppu::queryInterface(_rType,static_cast<css::frame::XDispatch*>(this));
140 Sequence< Type > SAL_CALL SbaXGridControl::getTypes( )
142 return comphelper::concatSequences(
143 FmXGridControl::getTypes(),
144 Sequence { cppu::UnoType<css::frame::XDispatch>::get() });
147 Sequence< sal_Int8 > SAL_CALL SbaXGridControl::getImplementationId( )
149 return css::uno::Sequence<sal_Int8>();
152 void SAL_CALL SbaXGridControl::createPeer(const Reference< css::awt::XToolkit > & rToolkit, const Reference< css::awt::XWindowPeer > & rParentPeer)
154 FmXGridControl::createPeer(rToolkit, rParentPeer);
156 OSL_ENSURE(!mbCreatingPeer, "FmXGridControl::createPeer : recursion!");
157 // see the base class' createPeer for a comment on this
159 // TODO: why the hell this whole class does not use any mutex?
161 Reference< css::frame::XDispatch > xDisp(getPeer(), UNO_QUERY);
162 for (auto const& elem : m_aStatusMultiplexer)
164 if (elem.second.is() && elem.second->getLength())
165 xDisp->addStatusListener(elem.second.get(), elem.first);
169 void SAL_CALL SbaXGridControl::dispatch(const css::util::URL& aURL, const Sequence< PropertyValue >& aArgs)
171 Reference< css::frame::XDispatch > xDisp(getPeer(), UNO_QUERY);
172 if (xDisp.is())
173 xDisp->dispatch(aURL, aArgs);
176 void SAL_CALL SbaXGridControl::addStatusListener( const Reference< XStatusListener > & _rxListener, const URL& _rURL )
178 ::osl::MutexGuard aGuard( GetMutex() );
179 if ( !_rxListener.is() )
180 return;
182 rtl::Reference<SbaXStatusMultiplexer>& xMultiplexer = m_aStatusMultiplexer[ _rURL ];
183 if ( !xMultiplexer.is() )
185 xMultiplexer = new SbaXStatusMultiplexer( *this, GetMutex() );
188 xMultiplexer->addInterface( _rxListener );
189 if ( getPeer().is() )
191 if ( 1 == xMultiplexer->getLength() )
192 { // the first external listener for this URL
193 Reference< XDispatch > xDisp( getPeer(), UNO_QUERY );
194 xDisp->addStatusListener( xMultiplexer.get(), _rURL );
196 else
197 { // already have other listeners for this URL
198 _rxListener->statusChanged( xMultiplexer->getLastEvent() );
203 void SAL_CALL SbaXGridControl::removeStatusListener(const Reference< css::frame::XStatusListener > & _rxListener, const css::util::URL& _rURL)
205 ::osl::MutexGuard aGuard( GetMutex() );
207 rtl::Reference<SbaXStatusMultiplexer>& xMultiplexer = m_aStatusMultiplexer[_rURL];
208 if (!xMultiplexer.is())
210 xMultiplexer = new SbaXStatusMultiplexer(*this,GetMutex());
213 if (getPeer().is() && xMultiplexer->getLength() == 1)
215 Reference< css::frame::XDispatch > xDisp(getPeer(), UNO_QUERY);
216 xDisp->removeStatusListener(xMultiplexer.get(), _rURL);
218 xMultiplexer->removeInterface( _rxListener );
221 void SAL_CALL SbaXGridControl::dispose()
223 SolarMutexGuard aGuard;
225 EventObject aEvt;
226 aEvt.Source = *this;
228 for (auto & elem : m_aStatusMultiplexer)
230 if (elem.second.is())
232 elem.second->disposeAndClear(aEvt);
233 elem.second.clear();
236 StatusMultiplexerArray().swap(m_aStatusMultiplexer);
238 FmXGridControl::dispose();
241 // SbaXGridPeer
242 SbaXGridPeer::SbaXGridPeer(const Reference< XComponentContext >& _rM)
243 : FmXGridPeer(_rM)
244 ,m_aStatusListeners(m_aMutex)
248 SbaXGridPeer::~SbaXGridPeer()
252 void SAL_CALL SbaXGridPeer::dispose()
254 EventObject aEvt(*this);
256 m_aStatusListeners.disposeAndClear(aEvt);
258 FmXGridPeer::dispose();
261 void SbaXGridPeer::NotifyStatusChanged(const css::util::URL& _rUrl, const Reference< css::frame::XStatusListener > & xControl)
263 VclPtr< SbaGridControl > pGrid = GetAs< SbaGridControl >();
264 if (!pGrid)
265 return;
267 css::frame::FeatureStateEvent aEvt;
268 aEvt.Source = *this;
269 aEvt.IsEnabled = !pGrid->IsReadOnlyDB();
270 aEvt.FeatureURL = _rUrl;
272 MapDispatchToBool::const_iterator aURLStatePos = m_aDispatchStates.find( classifyDispatchURL( _rUrl ) );
273 if ( m_aDispatchStates.end() != aURLStatePos )
274 aEvt.State <<= aURLStatePos->second;
275 else
276 aEvt.State <<= false;
278 if (xControl.is())
279 xControl->statusChanged(aEvt);
280 else
282 ::cppu::OInterfaceContainerHelper * pIter = m_aStatusListeners.getContainer(_rUrl);
284 if (pIter)
286 ::cppu::OInterfaceIteratorHelper aListIter(*pIter);
287 while (aListIter.hasMoreElements())
288 static_cast< css::frame::XStatusListener*>(aListIter.next())->statusChanged(aEvt);
293 Any SAL_CALL SbaXGridPeer::queryInterface(const Type& _rType)
295 Any aRet = ::cppu::queryInterface(_rType,static_cast<css::frame::XDispatch*>(this));
296 if(aRet.hasValue())
297 return aRet;
298 return FmXGridPeer::queryInterface(_rType);
301 Reference< css::frame::XDispatch > SAL_CALL SbaXGridPeer::queryDispatch(const css::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags)
303 if ( ( aURL.Complete == ".uno:GridSlots/BrowserAttribs" ) || ( aURL.Complete == ".uno:GridSlots/RowHeight" )
304 || ( aURL.Complete == ".uno:GridSlots/ColumnAttribs" ) || ( aURL.Complete == ".uno:GridSlots/ColumnWidth" )
307 return static_cast<css::frame::XDispatch*>(this);
310 return FmXGridPeer::queryDispatch(aURL, aTargetFrameName, nSearchFlags);
313 IMPL_LINK_NOARG( SbaXGridPeer, OnDispatchEvent, void*, void )
315 VclPtr< SbaGridControl > pGrid = GetAs< SbaGridControl >();
316 if ( !pGrid ) // if this fails, we were disposing before arriving here
317 return;
319 if ( !Application::IsMainThread() )
321 // still not in the main thread (see SbaXGridPeer::dispatch). post an event, again
322 // without moving the special even to the back of the queue
323 pGrid->PostUserEvent( LINK( this, SbaXGridPeer, OnDispatchEvent ) );
325 else
327 DispatchArgs aArgs = m_aDispatchArgs.front();
328 m_aDispatchArgs.pop();
330 SbaXGridPeer::dispatch( aArgs.aURL, aArgs.aArgs );
334 SbaXGridPeer::DispatchType SbaXGridPeer::classifyDispatchURL( const URL& _rURL )
336 DispatchType eURLType = dtUnknown;
337 if ( _rURL.Complete == ".uno:GridSlots/BrowserAttribs" )
338 eURLType = dtBrowserAttribs;
339 else if ( _rURL.Complete == ".uno:GridSlots/RowHeight" )
340 eURLType = dtRowHeight;
341 else if ( _rURL.Complete == ".uno:GridSlots/ColumnAttribs" )
342 eURLType = dtColumnAttribs;
343 else if ( _rURL.Complete == ".uno:GridSlots/ColumnWidth" )
344 eURLType = dtColumnWidth;
345 return eURLType;
348 void SAL_CALL SbaXGridPeer::dispatch(const URL& aURL, const Sequence< PropertyValue >& aArgs)
350 VclPtr< SbaGridControl > pGrid = GetAs< SbaGridControl >();
351 if (!pGrid)
352 return;
354 if ( !Application::IsMainThread() )
356 // we're not in the main thread. This is bad, as we want to raise windows here,
357 // and VCL does not like windows to be opened in non-main threads (at least on Win32).
358 // Okay, do this async. No problem with this, as XDispatch::dispatch is defined to be
359 // a one-way method.
361 // save the args
362 DispatchArgs aDispatchArgs;
363 aDispatchArgs.aURL = aURL;
364 aDispatchArgs.aArgs = aArgs;
365 m_aDispatchArgs.push( aDispatchArgs );
367 // post an event
368 // we use the Window::PostUserEvent here, instead of the application::PostUserEvent
369 // this saves us from keeping track of these events - as soon as the window dies,
370 // the events are deleted automatically. For the application way, we would need to
371 // do this ourself.
372 // As we use our grid as window, and the grid dies before we die, this should be no problem.
373 pGrid->PostUserEvent( LINK( this, SbaXGridPeer, OnDispatchEvent ) );
374 return;
377 SolarMutexGuard aGuard;
378 sal_Int16 nColId = -1;
379 for (const PropertyValue& rArg : aArgs)
381 if (rArg.Name == "ColumnViewPos")
383 nColId = pGrid->GetColumnIdFromViewPos(::comphelper::getINT16(rArg.Value));
384 break;
386 if (rArg.Name == "ColumnModelPos")
388 nColId = pGrid->GetColumnIdFromModelPos(::comphelper::getINT16(rArg.Value));
389 break;
391 if (rArg.Name == "ColumnId")
393 nColId = ::comphelper::getINT16(rArg.Value);
394 break;
398 DispatchType eURLType = classifyDispatchURL( aURL );
400 if ( dtUnknown == eURLType )
401 return;
403 // notify any status listeners that the dialog is now active (well, about to be active)
404 MapDispatchToBool::const_iterator aThisURLState = m_aDispatchStates.emplace( eURLType, true ).first;
405 NotifyStatusChanged( aURL, nullptr );
407 // execute the dialog
408 switch ( eURLType )
410 case dtBrowserAttribs:
411 pGrid->SetBrowserAttrs();
412 break;
414 case dtRowHeight:
415 pGrid->SetRowHeight();
416 break;
418 case dtColumnAttribs:
420 OSL_ENSURE(nColId != -1, "SbaXGridPeer::dispatch : invalid parameter !");
421 if (nColId != -1)
422 break;
423 pGrid->SetColAttrs(nColId);
425 break;
427 case dtColumnWidth:
429 OSL_ENSURE(nColId != -1, "SbaXGridPeer::dispatch : invalid parameter !");
430 if (nColId != -1)
431 break;
432 pGrid->SetColWidth(nColId);
434 break;
436 case dtUnknown:
437 break;
440 // notify any status listeners that the dialog vanished
441 m_aDispatchStates.erase( aThisURLState );
442 NotifyStatusChanged( aURL, nullptr );
445 void SAL_CALL SbaXGridPeer::addStatusListener(const Reference< css::frame::XStatusListener > & xControl, const css::util::URL& aURL)
447 ::cppu::OInterfaceContainerHelper* pCont = m_aStatusListeners.getContainer(aURL);
448 if (!pCont)
449 m_aStatusListeners.addInterface(aURL,xControl);
450 else
451 pCont->addInterface(xControl);
452 NotifyStatusChanged(aURL, xControl);
455 void SAL_CALL SbaXGridPeer::removeStatusListener(const Reference< css::frame::XStatusListener > & xControl, const css::util::URL& aURL)
457 ::cppu::OInterfaceContainerHelper* pCont = m_aStatusListeners.getContainer(aURL);
458 if ( pCont )
459 pCont->removeInterface(xControl);
462 Sequence< Type > SAL_CALL SbaXGridPeer::getTypes()
464 return comphelper::concatSequences(
465 FmXGridPeer::getTypes(),
466 Sequence { cppu::UnoType<css::frame::XDispatch>::get() });
469 UNO3_GETIMPLEMENTATION2_IMPL(SbaXGridPeer, FmXGridPeer);
471 VclPtr<FmGridControl> SbaXGridPeer::imp_CreateControl(vcl::Window* pParent, WinBits nStyle)
473 return VclPtr<SbaGridControl>::Create( m_xContext, pParent, this, nStyle);
476 // SbaGridHeader
478 SbaGridHeader::SbaGridHeader(BrowseBox* pParent)
479 :FmGridHeader(pParent, WB_STDHEADERBAR | WB_DRAG)
480 ,DragSourceHelper(this)
484 SbaGridHeader::~SbaGridHeader()
486 disposeOnce();
489 void SbaGridHeader::dispose()
491 DragSourceHelper::dispose();
492 FmGridHeader::dispose();
495 void SbaGridHeader::StartDrag( sal_Int8 _nAction, const Point& _rPosPixel )
497 SolarMutexGuard aGuard;
498 // in the new DnD API, the solar mutex is not locked when StartDrag is called
500 ImplStartColumnDrag( _nAction, _rPosPixel );
503 void SbaGridHeader::MouseButtonDown( const MouseEvent& _rMEvt )
505 if (_rMEvt.IsLeft())
506 if (_rMEvt.GetClicks() != 2)
508 // the base class will start a column move here, which we don't want to allow
509 // (at the moment. If we store relative positions with the columns, we can allow column moves...)
513 FmGridHeader::MouseButtonDown(_rMEvt);
516 void SbaGridHeader::ImplStartColumnDrag(sal_Int8 _nAction, const Point& _rMousePos)
518 sal_uInt16 nId = GetItemId(_rMousePos);
519 bool bResizingCol = false;
520 if (HEADERBAR_ITEM_NOTFOUND != nId)
522 tools::Rectangle aColRect = GetItemRect(nId);
523 aColRect.AdjustLeft(nId ? 3 : 0 ); // the handle col (nId == 0) does not have a left margin for resizing
524 aColRect.AdjustRight( -3 );
525 bResizingCol = !aColRect.IsInside(_rMousePos);
527 if (bResizingCol)
528 return;
530 // force the base class to end its drag mode
531 EndTracking(TrackingEventFlags::Cancel | TrackingEventFlags::End);
533 // because we have 3d-buttons the select handler is called from MouseButtonUp, but StartDrag
534 // occurs earlier (while the mouse button is down)
535 // so for optical reasons we select the column before really starting the drag operation.
536 notifyColumnSelect(nId);
538 static_cast<SbaGridControl*>(GetParent())->StartDrag(_nAction,
539 Point(
540 _rMousePos.X() + GetPosPixel().X(), // we aren't left-justified with our parent, in contrast to the data window
541 _rMousePos.Y() - GetSizePixel().Height()
546 void SbaGridHeader::PreExecuteColumnContextMenu(sal_uInt16 nColId, PopupMenu& rMenu)
548 FmGridHeader::PreExecuteColumnContextMenu(nColId, rMenu);
550 // some items are valid only if the db isn't readonly
551 bool bDBIsReadOnly = static_cast<SbaGridControl*>(GetParent())->IsReadOnlyDB();
553 if (bDBIsReadOnly)
555 rMenu.EnableItem(rMenu.GetItemId("hide"), false);
556 PopupMenu* pShowColsMenu = rMenu.GetPopupMenu(rMenu.GetItemId("show"));
557 if (pShowColsMenu)
559 // at most 16 items which mean "show column <name>"
560 for (sal_uInt16 i=1; i<16; ++i)
561 pShowColsMenu->EnableItem(i, false);
562 // "show cols/more..." and "show cols/all"
563 pShowColsMenu->EnableItem(pShowColsMenu->GetItemId("more"), false);
564 pShowColsMenu->EnableItem(pShowColsMenu->GetItemId("all"), false);
568 // prepend some new items
569 bool bColAttrs = (nColId != sal_uInt16(-1)) && (nColId != 0);
570 if ( !bColAttrs || bDBIsReadOnly)
571 return;
573 sal_uInt16 nPos = 0;
574 sal_uInt16 nModelPos = static_cast<SbaGridControl*>(GetParent())->GetModelColumnPos(nColId);
575 Reference< XPropertySet > xField = static_cast<SbaGridControl*>(GetParent())->getField(nModelPos);
577 if ( xField.is() )
579 switch( ::comphelper::getINT32(xField->getPropertyValue(PROPERTY_TYPE)) )
581 case DataType::BINARY:
582 case DataType::VARBINARY:
583 case DataType::LONGVARBINARY:
584 case DataType::SQLNULL:
585 case DataType::OBJECT:
586 case DataType::BLOB:
587 case DataType::CLOB:
588 case DataType::REF:
589 break;
590 default:
591 rMenu.InsertItem(ID_BROWSER_COLATTRSET, DBA_RES(RID_STR_COLUMN_FORMAT), MenuItemBits::NONE, OString(), nPos++);
592 rMenu.SetHelpId(ID_BROWSER_COLATTRSET, HID_BROWSER_COLUMNFORMAT);
593 rMenu.InsertSeparator(OString(), nPos++);
597 rMenu.InsertItem(ID_BROWSER_COLWIDTH, DBA_RES(RID_STR_COLUMN_WIDTH), MenuItemBits::NONE, OString(), nPos++);
598 rMenu.SetHelpId(ID_BROWSER_COLWIDTH, HID_BROWSER_COLUMNWIDTH);
599 rMenu.InsertSeparator(OString(), nPos++);
602 void SbaGridHeader::PostExecuteColumnContextMenu(sal_uInt16 nColId, const PopupMenu& rMenu, sal_uInt16 nExecutionResult)
604 switch (nExecutionResult)
606 case ID_BROWSER_COLWIDTH:
607 static_cast<SbaGridControl*>(GetParent())->SetColWidth(nColId);
608 break;
610 case ID_BROWSER_COLATTRSET:
611 static_cast<SbaGridControl*>(GetParent())->SetColAttrs(nColId);
612 break;
613 case ID_BROWSER_COLUMNINFO:
615 sal_uInt16 nModelPos = static_cast<SbaGridControl*>(GetParent())->GetModelColumnPos(nColId);
616 Reference< XPropertySet > xField = static_cast<SbaGridControl*>(GetParent())->getField(nModelPos);
618 if(!xField.is())
619 break;
620 std::vector< std::shared_ptr<OTableRow> > vClipboardList;
621 // send it to the clipboard
622 vClipboardList.push_back(std::make_shared<OTableRow>(xField));
623 rtl::Reference<OTableRowExchange> pData = new OTableRowExchange(vClipboardList);
624 pData->CopyToClipboard(GetParent());
626 break;
628 default: FmGridHeader::PostExecuteColumnContextMenu(nColId, rMenu, nExecutionResult);
632 // SbaGridControl
633 SbaGridControl::SbaGridControl(Reference< XComponentContext > const & _rM,
634 vcl::Window* pParent, FmXGridPeer* _pPeer, WinBits nBits)
635 :FmGridControl(_rM,pParent, _pPeer, nBits)
636 ,m_pMasterListener(nullptr)
637 ,m_nAsyncDropEvent(nullptr)
638 ,m_bActivatingForDrop(false)
642 SbaGridControl::~SbaGridControl()
644 disposeOnce();
647 void SbaGridControl::dispose()
649 if (m_nAsyncDropEvent)
650 Application::RemoveUserEvent(m_nAsyncDropEvent);
651 m_nAsyncDropEvent = nullptr;
652 FmGridControl::dispose();
655 VclPtr<BrowserHeader> SbaGridControl::imp_CreateHeaderBar(BrowseBox* pParent)
657 return VclPtr<SbaGridHeader>::Create(pParent);
660 CellController* SbaGridControl::GetController(sal_Int32 nRow, sal_uInt16 nCol)
662 if ( m_bActivatingForDrop )
663 return nullptr;
665 return FmGridControl::GetController(nRow, nCol);
668 void SbaGridControl::PreExecuteRowContextMenu(sal_uInt16 nRow, PopupMenu& rMenu)
670 FmGridControl::PreExecuteRowContextMenu(nRow, rMenu);
672 sal_uInt16 nPos = 0;
674 if (!IsReadOnlyDB())
676 rMenu.InsertItem(ID_BROWSER_TABLEATTR, DBA_RES(RID_STR_TABLE_FORMAT), MenuItemBits::NONE, OString(), nPos++);
677 rMenu.SetHelpId(ID_BROWSER_TABLEATTR, HID_BROWSER_TABLEFORMAT);
679 rMenu.InsertItem(ID_BROWSER_ROWHEIGHT, DBA_RES(RID_STR_ROW_HEIGHT), MenuItemBits::NONE, OString(), nPos++);
680 rMenu.SetHelpId(ID_BROWSER_ROWHEIGHT, HID_BROWSER_ROWHEIGHT);
681 rMenu.InsertSeparator(OString(), nPos++);
684 if ( GetSelectRowCount() > 0 )
686 rMenu.InsertItem(ID_BROWSER_COPY, DBA_RES(RID_STR_COPY), MenuItemBits::NONE, OString(), nPos++);
687 rMenu.InsertSeparator(OString(), nPos++);
691 SvNumberFormatter* SbaGridControl::GetDatasourceFormatter()
693 Reference< css::util::XNumberFormatsSupplier > xSupplier = ::dbtools::getNumberFormats(::dbtools::getConnection(Reference< XRowSet > (getDataSource(),UNO_QUERY)), true, getContext());
695 SvNumberFormatsSupplierObj* pSupplierImpl = comphelper::getUnoTunnelImplementation<SvNumberFormatsSupplierObj>( xSupplier );
696 if ( !pSupplierImpl )
697 return nullptr;
699 SvNumberFormatter* pFormatter = pSupplierImpl->GetNumberFormatter();
700 return pFormatter;
703 void SbaGridControl::SetColWidth(sal_uInt16 nColId)
705 // get the (UNO) column model
706 sal_uInt16 nModelPos = GetModelColumnPos(nColId);
707 Reference< XIndexAccess > xCols = GetPeer()->getColumns();
708 Reference< XPropertySet > xAffectedCol;
709 if (xCols.is() && (nModelPos != sal_uInt16(-1)))
710 xAffectedCol.set(xCols->getByIndex(nModelPos), css::uno::UNO_QUERY);
712 if (!xAffectedCol.is())
713 return;
715 Any aWidth = xAffectedCol->getPropertyValue(PROPERTY_WIDTH);
716 sal_Int32 nCurWidth = aWidth.hasValue() ? ::comphelper::getINT32(aWidth) : -1;
718 DlgSize aDlgColWidth(GetFrameWeld(), nCurWidth, false);
719 if (aDlgColWidth.run() != RET_OK)
720 return;
722 sal_Int32 nValue = aDlgColWidth.GetValue();
723 Any aNewWidth;
724 if (-1 == nValue)
725 { // set to default
726 Reference< XPropertyState > xPropState(xAffectedCol, UNO_QUERY);
727 if (xPropState.is())
729 try { aNewWidth = xPropState->getPropertyDefault(PROPERTY_WIDTH); } catch(Exception&) { } ;
732 else
733 aNewWidth <<= nValue;
734 try { xAffectedCol->setPropertyValue(PROPERTY_WIDTH, aNewWidth); } catch(Exception&) { } ;
737 void SbaGridControl::SetRowHeight()
739 Reference< XPropertySet > xCols(GetPeer()->getColumns(), UNO_QUERY);
740 if (!xCols.is())
741 return;
743 Any aHeight = xCols->getPropertyValue(PROPERTY_ROW_HEIGHT);
744 sal_Int32 nCurHeight = aHeight.hasValue() ? ::comphelper::getINT32(aHeight) : -1;
746 DlgSize aDlgRowHeight(GetFrameWeld(), nCurHeight, true);
747 if (aDlgRowHeight.run() != RET_OK)
748 return;
750 sal_Int32 nValue = aDlgRowHeight.GetValue();
751 Any aNewHeight;
752 if (sal_Int16(-1) == nValue)
753 { // set to default
754 Reference< XPropertyState > xPropState(xCols, UNO_QUERY);
755 if (xPropState.is())
759 aNewHeight = xPropState->getPropertyDefault(PROPERTY_ROW_HEIGHT);
761 catch(Exception&)
765 else
766 aNewHeight <<= nValue;
769 xCols->setPropertyValue(PROPERTY_ROW_HEIGHT, aNewHeight);
771 catch(Exception&)
773 OSL_FAIL("setPropertyValue: PROPERTY_ROW_HEIGHT throws an exception");
777 void SbaGridControl::SetColAttrs(sal_uInt16 nColId)
779 SvNumberFormatter* pFormatter = GetDatasourceFormatter();
780 if (!pFormatter)
781 return;
783 sal_uInt16 nModelPos = GetModelColumnPos(nColId);
785 // get the (UNO) column model
786 Reference< XIndexAccess > xCols = GetPeer()->getColumns();
787 Reference< XPropertySet > xAffectedCol;
788 if (xCols.is() && (nModelPos != sal_uInt16(-1)))
789 xAffectedCol.set(xCols->getByIndex(nModelPos), css::uno::UNO_QUERY);
791 // get the field the column is bound to
792 Reference< XPropertySet > xField = getField(nModelPos);
793 ::dbaui::callColumnFormatDialog(xAffectedCol,xField,pFormatter,GetFrameWeld());
796 void SbaGridControl::SetBrowserAttrs()
798 Reference< XPropertySet > xGridModel(GetPeer()->getColumns(), UNO_QUERY);
799 if (!xGridModel.is())
800 return;
804 Reference< XComponentContext > xContext = getContext();
805 css::beans::PropertyValue aArg;
806 css::uno::Sequence<css::uno::Any> aArguments(2);
807 aArg.Name = "IntrospectedObject";
808 aArg.Value <<= xGridModel;
809 aArguments[0] <<= aArg;
810 aArg.Name = "ParentWindow";
811 aArg.Value <<= VCLUnoHelper::GetInterface(this);
812 aArguments[1] <<= aArg;
813 Reference<XExecutableDialog> xExecute(xContext->getServiceManager()->createInstanceWithArgumentsAndContext("com.sun.star.form.ControlFontDialog",
814 aArguments, xContext), css::uno::UNO_QUERY_THROW);
815 xExecute->execute();
817 catch( const Exception& )
819 DBG_UNHANDLED_EXCEPTION("dbaccess");
823 void SbaGridControl::PostExecuteRowContextMenu(sal_uInt16 nRow, const PopupMenu& rMenu, sal_uInt16 nExecutionResult)
825 switch (nExecutionResult)
827 case ID_BROWSER_TABLEATTR:
828 SetBrowserAttrs();
829 break;
830 case ID_BROWSER_ROWHEIGHT:
831 SetRowHeight();
832 break;
833 case ID_BROWSER_COPY:
834 CopySelectedRowsToClipboard();
835 break;
837 default:
838 FmGridControl::PostExecuteRowContextMenu(nRow, rMenu, nExecutionResult);
839 break;
843 void SbaGridControl::Select()
845 // Some selection has changed ...
846 FmGridControl::Select();
848 if (m_pMasterListener)
849 m_pMasterListener->SelectionChanged();
852 void SbaGridControl::ActivateCell(sal_Int32 nRow, sal_uInt16 nCol, bool bSetCellFocus /*= sal_True*/ )
854 FmGridControl::ActivateCell(nRow, nCol, bSetCellFocus);
855 if (m_pMasterListener)
856 m_pMasterListener->CellActivated();
859 void SbaGridControl::DeactivateCell(bool bUpdate /*= sal_True*/)
861 FmGridControl::DeactivateCell(bUpdate);
862 if (m_pMasterListener)
863 m_pMasterListener->CellDeactivated();
866 void SbaGridControl::onRowChange()
868 if ( m_pMasterListener )
869 m_pMasterListener->RowChanged();
872 void SbaGridControl::onColumnChange()
874 if ( m_pMasterListener )
875 m_pMasterListener->ColumnChanged();
878 Reference< XPropertySet > SbaGridControl::getField(sal_uInt16 nModelPos)
880 Reference< XPropertySet > xEmptyReturn;
883 // first get the name of the column
884 Reference< XIndexAccess > xCols = GetPeer()->getColumns();
885 if ( xCols.is() && xCols->getCount() > nModelPos )
887 Reference< XPropertySet > xCol(xCols->getByIndex(nModelPos),UNO_QUERY);
888 if ( xCol.is() )
889 xEmptyReturn.set(xCol->getPropertyValue(PROPERTY_BOUNDFIELD),UNO_QUERY);
891 else
892 OSL_FAIL("SbaGridControl::getField getColumns returns NULL or ModelPos is > than count!");
894 catch (const Exception&)
896 TOOLS_WARN_EXCEPTION("dbaccess", "SbaGridControl::getField Exception occurred");
899 return xEmptyReturn;
902 bool SbaGridControl::IsReadOnlyDB() const
904 // assume yes if anything fails
905 bool bDBIsReadOnly = true;
909 // the db is the implemented by the parent of the grid control's model ...
910 Reference< XChild > xColumns(GetPeer()->getColumns(), UNO_QUERY);
911 if (xColumns.is())
913 Reference< XRowSet > xDataSource(xColumns->getParent(), UNO_QUERY);
914 ::dbtools::ensureRowSetConnection( xDataSource, getContext(), nullptr );
915 Reference< XChild > xConn(::dbtools::getConnection(xDataSource),UNO_QUERY);
916 if (xConn.is())
918 // ... and the RO-flag simply is implemented by a property
919 Reference< XPropertySet > xDbProps(xConn->getParent(), UNO_QUERY);
920 if (xDbProps.is())
922 Reference< XPropertySetInfo > xInfo = xDbProps->getPropertySetInfo();
923 if (xInfo->hasPropertyByName(PROPERTY_ISREADONLY))
924 bDBIsReadOnly = ::comphelper::getBOOL(xDbProps->getPropertyValue(PROPERTY_ISREADONLY));
929 catch (const Exception&)
931 TOOLS_WARN_EXCEPTION("dbaccess", "SbaGridControl::IsReadOnlyDB Exception occurred");
934 return bDBIsReadOnly;
937 void SbaGridControl::MouseButtonDown( const BrowserMouseEvent& rMEvt)
939 sal_Int32 nRow = GetRowAtYPosPixel(rMEvt.GetPosPixel().Y());
940 sal_uInt16 nColPos = GetColumnAtXPosPixel(rMEvt.GetPosPixel().X());
941 sal_uInt16 nViewPos = (nColPos == BROWSER_INVALIDID) ? sal_uInt16(-1) : nColPos-1;
942 // 'the handle column' and 'no valid column' will both result in a view position of -1 !
944 bool bHitEmptySpace = (nRow > GetRowCount()) || (nViewPos == sal_uInt16(-1));
946 if (bHitEmptySpace && (rMEvt.GetClicks() == 2) && rMEvt.IsMod1())
947 Control::MouseButtonDown(rMEvt);
948 else
949 FmGridControl::MouseButtonDown(rMEvt);
952 void SbaGridControl::StartDrag( sal_Int8 _nAction, const Point& _rPosPixel )
954 SolarMutexGuard aGuard;
955 // in the new DnD API, the solar mutex is not locked when StartDrag is called
957 bool bHandled = false;
961 // determine if dragging is allowed
962 // (Yes, this is controller (not view) functionality. But collecting and evaluating all the
963 // information necessary via UNO would be quite difficult (if not impossible) so
964 // my laziness says 'do it here'...)
965 sal_Int32 nRow = GetRowAtYPosPixel(_rPosPixel.Y());
966 sal_uInt16 nColPos = GetColumnAtXPosPixel(_rPosPixel.X());
967 sal_uInt16 nViewPos = (nColPos == BROWSER_INVALIDID) ? sal_uInt16(-1) : nColPos-1;
968 // 'the handle column' and 'no valid column' will both result in a view position of -1 !
970 bool bCurrentRowVirtual = IsCurrentAppending() && IsModified();
971 // the current row doesn't really exist: the user's appending a new one and already has entered some data,
972 // so the row contains data which has no counter part within the data source
974 sal_Int32 nCorrectRowCount = GetRowCount();
975 if (GetOptions() & DbGridControlOptions::Insert)
976 --nCorrectRowCount; // there is an empty row for inserting records
977 if (bCurrentRowVirtual)
978 --nCorrectRowCount;
980 if ((nColPos == BROWSER_INVALIDID) || (nRow >= nCorrectRowCount))
981 break;
983 bool bHitHandle = (nColPos == 0);
985 // check which kind of dragging has to be initiated
986 if ( bHitHandle // the handle column
987 // AND
988 && ( GetSelectRowCount() // at least one row is selected
989 // OR
990 || ( (nRow >= 0) // a row below the header
991 && !bCurrentRowVirtual // we aren't appending a new record
992 && (nRow != GetCurrentPos()) // a row which is not the current one
993 ) // OR
994 || ( (0 == GetSelectRowCount()) // no rows selected
995 && (-1 == nRow) // hit the header
999 { // => start dragging the row
1000 if (GetDataWindow().IsMouseCaptured())
1001 GetDataWindow().ReleaseMouse();
1003 if (0 == GetSelectRowCount())
1004 // no rows selected, but here in this branch
1005 // -> the user started dragging the upper left corner, which symbolizes the whole table
1006 SelectAll();
1008 getMouseEvent().Clear();
1009 implTransferSelectedRows(static_cast<sal_Int16>(nRow), false);
1011 bHandled = true;
1013 else if ( (nRow < 0) // the header
1014 && (!bHitHandle) // non-handle column
1015 && (nViewPos < GetViewColCount()) // valid (existing) column
1017 { // => start dragging the column
1018 if (GetDataWindow().IsMouseCaptured())
1019 GetDataWindow().ReleaseMouse();
1021 getMouseEvent().Clear();
1022 DoColumnDrag(nViewPos);
1024 bHandled = true;
1026 else if ( !bHitHandle // non-handle column
1027 && (nRow >= 0) // non-header row
1029 { // => start dragging the field content
1030 if (GetDataWindow().IsMouseCaptured())
1031 GetDataWindow().ReleaseMouse();
1033 getMouseEvent().Clear();
1034 DoFieldDrag(nViewPos, static_cast<sal_Int16>(nRow));
1036 bHandled = true;
1039 while (false);
1041 if (!bHandled)
1042 FmGridControl::StartDrag(_nAction, _rPosPixel);
1045 void SbaGridControl::DoColumnDrag(sal_uInt16 nColumnPos)
1047 Reference< XPropertySet > xDataSource = getDataSource();
1048 OSL_ENSURE(xDataSource.is(), "SbaGridControl::DoColumnDrag : invalid data source !");
1049 ::dbtools::ensureRowSetConnection(Reference< XRowSet >(getDataSource(),UNO_QUERY), getContext(), nullptr);
1051 Reference< XPropertySet > xAffectedCol;
1052 Reference< XPropertySet > xAffectedField;
1053 Reference< XConnection > xActiveConnection;
1055 // determine the field to drag
1056 OUString sField;
1059 xActiveConnection = ::dbtools::getConnection(Reference< XRowSet >(getDataSource(),UNO_QUERY));
1061 sal_uInt16 nModelPos = GetModelColumnPos(GetColumnIdFromViewPos(nColumnPos));
1062 Reference< XIndexContainer > xCols = GetPeer()->getColumns();
1063 xAffectedCol.set(xCols->getByIndex(nModelPos),UNO_QUERY);
1064 if (xAffectedCol.is())
1066 xAffectedCol->getPropertyValue(PROPERTY_CONTROLSOURCE) >>= sField;
1067 xAffectedField.set(xAffectedCol->getPropertyValue(PROPERTY_BOUNDFIELD),UNO_QUERY);
1070 catch(Exception&)
1072 OSL_FAIL("SbaGridControl::DoColumnDrag : something went wrong while getting the column");
1074 if (sField.isEmpty())
1075 return;
1077 rtl::Reference<OColumnTransferable> pDataTransfer = new OColumnTransferable(xDataSource, sField, xAffectedField, xActiveConnection, ColumnTransferFormatFlags::FIELD_DESCRIPTOR | ColumnTransferFormatFlags::COLUMN_DESCRIPTOR);
1078 pDataTransfer->StartDrag(this, DND_ACTION_COPY | DND_ACTION_LINK);
1081 void SbaGridControl::CopySelectedRowsToClipboard()
1083 OSL_ENSURE( GetSelectRowCount() > 0, "SbaGridControl::CopySelectedRowsToClipboard: invalid call!" );
1084 implTransferSelectedRows( static_cast<sal_Int16>(FirstSelectedRow()), true );
1087 void SbaGridControl::implTransferSelectedRows( sal_Int16 nRowPos, bool _bTrueIfClipboardFalseIfDrag )
1089 Reference< XPropertySet > xForm = getDataSource();
1090 OSL_ENSURE( xForm.is(), "SbaGridControl::implTransferSelectedRows: invalid form!" );
1092 // build the sequence of numbers of selected rows
1093 Sequence< Any > aSelectedRows;
1094 bool bSelectionBookmarks = true;
1096 // collect the affected rows
1097 if ((GetSelectRowCount() == 0) && (nRowPos >= 0))
1099 aSelectedRows.realloc( 1 );
1100 aSelectedRows[0] <<= static_cast<sal_Int32>(nRowPos + 1);
1101 bSelectionBookmarks = false;
1103 else if ( !IsAllSelected() && GetSelectRowCount() )
1105 aSelectedRows = getSelectionBookmarks();
1106 bSelectionBookmarks = true;
1111 rtl::Reference<ODataClipboard> pTransfer = new ODataClipboard( xForm, aSelectedRows, bSelectionBookmarks, getContext() );
1113 if ( _bTrueIfClipboardFalseIfDrag )
1114 pTransfer->CopyToClipboard( this );
1115 else
1116 pTransfer->StartDrag(this, DND_ACTION_COPY | DND_ACTION_LINK);
1118 catch(Exception&)
1123 void SbaGridControl::DoFieldDrag(sal_uInt16 nColumnPos, sal_Int16 nRowPos)
1125 // the only thing to do here is dragging the pure cell text
1126 // the old implementation copied a SBA_FIELDDATAEXCHANGE_FORMAT, too, (which was rather expensive to obtain),
1127 // but we have no client for this DnD format anymore (the mail part of SO 5.2 was the only client)
1131 OUString sCellText;
1132 Reference< XGridFieldDataSupplier > xFieldData(static_cast< XGridPeer* >(GetPeer()), UNO_QUERY);
1133 Sequence<sal_Bool> aSupportingText = xFieldData->queryFieldDataType(cppu::UnoType<decltype(sCellText)>::get());
1134 if (aSupportingText.getConstArray()[nColumnPos])
1136 Sequence< Any> aCellContents = xFieldData->queryFieldData(nRowPos, cppu::UnoType<decltype(sCellText)>::get());
1137 sCellText = ::comphelper::getString(aCellContents.getConstArray()[nColumnPos]);
1138 ::svt::OStringTransfer::StartStringDrag(sCellText, this, DND_ACTION_COPY);
1141 catch(Exception&)
1143 OSL_FAIL("SbaGridControl::DoFieldDrag : could not retrieve the cell's contents !");
1144 return;
1149 namespace {
1151 /// unary_function Functor object for class ZZ returntype is void
1152 struct SbaGridControlPrec
1154 bool operator()(const DataFlavorExVector::value_type& _aType)
1156 switch (_aType.mnSotId)
1158 case SotClipboardFormatId::DBACCESS_TABLE: // table descriptor
1159 case SotClipboardFormatId::DBACCESS_QUERY: // query descriptor
1160 case SotClipboardFormatId::DBACCESS_COMMAND: // SQL command
1161 return true;
1162 default: break;
1164 return false;
1170 sal_Int8 SbaGridControl::AcceptDrop( const BrowserAcceptDropEvent& rEvt )
1172 sal_Int8 nAction = DND_ACTION_NONE;
1174 // we need a valid connection
1175 if (!::dbtools::getConnection(Reference< XRowSet > (getDataSource(),UNO_QUERY)).is())
1176 return nAction;
1178 if ( IsDropFormatSupported( SotClipboardFormatId::STRING ) ) do
1179 { // odd construction, but spares us a lot of (explicit ;) goto's
1181 if (!GetEmptyRow().is())
1182 // without an empty row we're not in update mode
1183 break;
1185 const sal_Int32 nRow = GetRowAtYPosPixel(rEvt.maPosPixel.Y(), false);
1186 const sal_uInt16 nCol = GetColumnId(GetColumnAtXPosPixel(rEvt.maPosPixel.X()));
1188 sal_Int32 nCorrectRowCount = GetRowCount();
1189 if (GetOptions() & DbGridControlOptions::Insert)
1190 --nCorrectRowCount; // there is an empty row for inserting records
1191 if (IsCurrentAppending())
1192 --nCorrectRowCount; // the current data record doesn't really exist, we are appending a new one
1194 if ( (nCol == BROWSER_INVALIDID) || (nRow >= nCorrectRowCount) || (nCol == 0) )
1195 // no valid cell under the mouse cursor
1196 break;
1198 tools::Rectangle aRect = GetCellRect(nRow, nCol, false);
1199 if (!aRect.IsInside(rEvt.maPosPixel))
1200 // not dropped within a cell (a cell isn't as wide as the column - the are small spaces)
1201 break;
1203 if ((IsModified() || (GetCurrentRow().is() && GetCurrentRow()->IsModified())) && (GetCurrentPos() != nRow))
1204 // there is a current and modified row or cell and he text is to be dropped into another one
1205 break;
1207 CellControllerRef xCurrentController = Controller();
1208 if (xCurrentController.is() && xCurrentController->IsValueChangedFromSaved() && ((nRow != GetCurRow()) || (nCol != GetCurColumnId())))
1209 // the current controller is modified and the user wants to drop in another cell -> no chance
1210 // (when leaving the modified cell an error may occur - this is deadly while dragging)
1211 break;
1213 Reference< XPropertySet > xField = getField(GetModelColumnPos(nCol));
1214 if (!xField.is())
1215 // the column is not valid bound (for instance a binary field)
1216 break;
1220 if (::comphelper::getBOOL(xField->getPropertyValue(PROPERTY_ISREADONLY)))
1221 break;
1223 catch (const Exception& )
1225 // assume RO
1226 break;
1231 // assume that text can be dropped into a field if the column has a css::awt::XTextComponent interface
1232 Reference< XIndexAccess > xColumnControls(static_cast<css::form::XGridPeer*>(GetPeer()), UNO_QUERY);
1233 if (xColumnControls.is())
1235 Reference< css::awt::XTextComponent > xColControl(
1236 xColumnControls->getByIndex(GetViewColumnPos(nCol)),
1237 css::uno::UNO_QUERY);
1238 if (xColControl.is())
1240 m_bActivatingForDrop = true;
1241 GoToRowColumnId(nRow, nCol);
1242 m_bActivatingForDrop = false;
1244 nAction = DND_ACTION_COPY;
1248 catch( const Exception& )
1250 DBG_UNHANDLED_EXCEPTION("dbaccess");
1253 } while (false);
1255 if(nAction != DND_ACTION_COPY && GetEmptyRow().is())
1257 const DataFlavorExVector& _rFlavors = GetDataFlavors();
1258 if(std::any_of(_rFlavors.begin(),_rFlavors.end(),SbaGridControlPrec()))
1259 nAction = DND_ACTION_COPY;
1262 return (DND_ACTION_NONE != nAction) ? nAction : FmGridControl::AcceptDrop(rEvt);
1265 sal_Int8 SbaGridControl::ExecuteDrop( const BrowserExecuteDropEvent& rEvt )
1267 // we need some properties of our data source
1268 Reference< XPropertySet > xDataSource = getDataSource();
1269 if (!xDataSource.is())
1270 return DND_ACTION_NONE;
1272 // we need a valid connection
1273 if (!::dbtools::getConnection(Reference< XRowSet > (xDataSource,UNO_QUERY)).is())
1274 return DND_ACTION_NONE;
1276 if ( IsDropFormatSupported( SotClipboardFormatId::STRING ) )
1278 sal_Int32 nRow = GetRowAtYPosPixel(rEvt.maPosPixel.Y(), false);
1279 sal_uInt16 nCol = GetColumnAtXPosPixel(rEvt.maPosPixel.X());
1281 sal_Int32 nCorrectRowCount = GetRowCount();
1282 if (GetOptions() & DbGridControlOptions::Insert)
1283 --nCorrectRowCount; // there is an empty row for inserting records
1284 if (IsCurrentAppending())
1285 --nCorrectRowCount; // the current data record doesn't really exist, we are appending a new one
1287 OSL_ENSURE((nCol != BROWSER_INVALIDID) && (nRow < nCorrectRowCount), "SbaGridControl::Drop : dropped on an invalid position !");
1288 // AcceptDrop should have caught this
1290 // from now we work with ids instead of positions
1291 nCol = GetColumnId(nCol);
1293 GoToRowColumnId(nRow, nCol);
1294 if (!IsEditing())
1295 ActivateCell();
1297 CellControllerRef xCurrentController = Controller();
1298 EditCellController* pController = dynamic_cast<EditCellController*>(xCurrentController.get());
1299 if (!pController)
1300 return DND_ACTION_NONE;
1302 // get the dropped string
1303 TransferableDataHelper aDropped( rEvt.maDropEvent.Transferable );
1304 OUString sDropped;
1305 if ( !aDropped.GetString( SotClipboardFormatId::STRING, sDropped ) )
1306 return DND_ACTION_NONE;
1308 IEditImplementation* pEditImplementation = pController->GetEditImplementation();
1309 pEditImplementation->SetText(sDropped);
1310 // SetText itself doesn't call a Modify as it isn't a user interaction
1311 pController->Modify();
1313 return DND_ACTION_COPY;
1316 if(GetEmptyRow().is())
1318 const DataFlavorExVector& _rFlavors = GetDataFlavors();
1319 if( std::any_of(_rFlavors.begin(),_rFlavors.end(), SbaGridControlPrec()) )
1321 TransferableDataHelper aDropped( rEvt.maDropEvent.Transferable );
1322 m_aDataDescriptor = ODataAccessObjectTransferable::extractObjectDescriptor(aDropped);
1323 if (m_nAsyncDropEvent)
1324 Application::RemoveUserEvent(m_nAsyncDropEvent);
1325 m_nAsyncDropEvent = Application::PostUserEvent(LINK(this, SbaGridControl, AsynchDropEvent), nullptr, true);
1326 return DND_ACTION_COPY;
1330 return DND_ACTION_NONE;
1333 Reference< XPropertySet > SbaGridControl::getDataSource() const
1335 Reference< XPropertySet > xReturn;
1337 Reference< XChild > xColumns(GetPeer()->getColumns(), UNO_QUERY);
1338 if (xColumns.is())
1339 xReturn.set(xColumns->getParent(), UNO_QUERY);
1341 return xReturn;
1344 IMPL_LINK_NOARG(SbaGridControl, AsynchDropEvent, void*, void)
1346 m_nAsyncDropEvent = nullptr;
1348 Reference< XPropertySet > xDataSource = getDataSource();
1349 if ( xDataSource.is() )
1351 bool bCountFinal = false;
1352 xDataSource->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bCountFinal;
1353 if ( !bCountFinal )
1354 setDataSource(nullptr); // detach from grid control
1355 Reference< XResultSetUpdate > xResultSetUpdate(xDataSource,UNO_QUERY);
1356 rtl::Reference<ODatabaseImportExport> pImExport = new ORowSetImportExport(GetFrameWeld(),xResultSetUpdate,m_aDataDescriptor, getContext());
1357 Hide();
1360 pImExport->initialize(m_aDataDescriptor);
1361 if (m_pMasterListener)
1362 m_pMasterListener->BeforeDrop();
1363 if(!pImExport->Read())
1365 OUString sError = DBA_RES(STR_NO_COLUMNNAME_MATCHING);
1366 throwGenericSQLException(sError,nullptr);
1368 if (m_pMasterListener)
1369 m_pMasterListener->AfterDrop();
1370 Show();
1372 catch(const SQLException& e)
1374 if (m_pMasterListener)
1375 m_pMasterListener->AfterDrop();
1376 Show();
1377 ::dbtools::showError( ::dbtools::SQLExceptionInfo(e), VCLUnoHelper::GetInterface(this), getContext() );
1379 catch(const Exception& )
1381 DBG_UNHANDLED_EXCEPTION("dbaccess");
1382 if (m_pMasterListener)
1383 m_pMasterListener->AfterDrop();
1384 Show();
1386 if ( !bCountFinal )
1387 setDataSource(Reference< XRowSet >(xDataSource,UNO_QUERY));
1389 m_aDataDescriptor.clear();
1392 OUString SbaGridControl::GetAccessibleObjectDescription( ::vcl::AccessibleBrowseBoxObjType eObjType,sal_Int32 _nPosition) const
1394 OUString sRet;
1395 if ( ::vcl::BBTYPE_BROWSEBOX == eObjType )
1397 SolarMutexGuard aGuard;
1398 sRet = DBA_RES(STR_DATASOURCE_GRIDCONTROL_DESC);
1400 else
1401 sRet = FmGridControl::GetAccessibleObjectDescription( eObjType,_nPosition);
1402 return sRet;
1405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */