xref: /core/dbaccess/source/ui/browser/sbagrid.cxx (revision d52a4dba)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <core_resource.hxx>
21 
22 #include <sot/exchange.hxx>
23 
24 #include <svx/dbaexchange.hxx>
25 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
26 
27 #include <sbagrid.hxx>
28 #include <dlgsize.hxx>
29 #include <com/sun/star/beans/XPropertyState.hpp>
30 #include <com/sun/star/form/XForm.hpp>
31 #include <com/sun/star/container/XIndexContainer.hpp>
32 
33 #include <com/sun/star/view/XSelectionSupplier.hpp>
34 #include <com/sun/star/awt/XTextComponent.hpp>
35 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
36 #include <comphelper/diagnose_ex.hxx>
37 
38 #include <svl/numuno.hxx>
39 #include <toolkit/helper/vclunohelper.hxx>
40 
41 #include <vcl/svapp.hxx>
42 
43 #include <cppuhelper/queryinterface.hxx>
44 #include <connectivity/dbtools.hxx>
45 #include <comphelper/propertyvalue.hxx>
46 #include <comphelper/types.hxx>
47 #include <com/sun/star/sdbc/DataType.hpp>
48 #include <com/sun/star/sdbc/SQLException.hpp>
49 #include <strings.hrc>
50 #include <strings.hxx>
51 #include <dbexchange.hxx>
52 #include <svtools/stringtransfer.hxx>
53 #include <UITools.hxx>
54 #include <TokenWriter.hxx>
55 #include <osl/diagnose.h>
56 #include <algorithm>
57 
58 using namespace ::com::sun::star::ui::dialogs;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::sdb;
61 using namespace ::com::sun::star::sdbc;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::container;
64 using namespace ::com::sun::star::datatransfer;
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::form;
67 using namespace ::com::sun::star::frame;
68 using namespace ::com::sun::star::util;
69 using namespace ::dbaui;
70 using namespace ::dbtools;
71 using namespace ::svx;
72 using namespace ::svt;
73 
74 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
com_sun_star_comp_dbu_SbaXGridControl_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const &)75 com_sun_star_comp_dbu_SbaXGridControl_get_implementation(
76     css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
77 {
78     return cppu::acquire(new SbaXGridControl(context));
79 }
80 
getSupportedServiceNames()81 css::uno::Sequence<OUString> SAL_CALL SbaXGridControl::getSupportedServiceNames()
82 {
83     return { u"com.sun.star.form.control.InteractionGridControl"_ustr, u"com.sun.star.form.control.GridControl"_ustr,
84          u"com.sun.star.awt.UnoControl"_ustr };
85 }
86 
87 
88 // SbaXGridControl
89 
getImplementationName()90 OUString SAL_CALL SbaXGridControl::getImplementationName()
91 {
92     return u"com.sun.star.comp.dbu.SbaXGridControl"_ustr;
93 }
94 
SbaXGridControl(const Reference<XComponentContext> & _rM)95 SbaXGridControl::SbaXGridControl(const Reference< XComponentContext >& _rM)
96     : FmXGridControl(_rM)
97 {
98 }
99 
~SbaXGridControl()100 SbaXGridControl::~SbaXGridControl()
101 {
102 }
103 
imp_CreatePeer(vcl::Window * pParent)104 rtl::Reference<FmXGridPeer> SbaXGridControl::imp_CreatePeer(vcl::Window* pParent)
105 {
106     rtl::Reference<FmXGridPeer> pReturn = new SbaXGridPeer(m_xContext);
107 
108     // translate properties into WinBits
109     WinBits nStyle = WB_TABSTOP;
110     Reference< XPropertySet >  xModelSet(getModel(), UNO_QUERY);
111     if (xModelSet.is())
112     {
113         try
114         {
115             if (::comphelper::getINT16(xModelSet->getPropertyValue(PROPERTY_BORDER)))
116                 nStyle |= WB_BORDER;
117         }
118         catch(Exception&)
119         {
120         }
121 
122     }
123 
124     pReturn->Create(pParent, nStyle);
125     return pReturn;
126 }
127 
queryAggregation(const Type & _rType)128 Any SAL_CALL SbaXGridControl::queryAggregation(const Type& _rType)
129 {
130     Any aRet = FmXGridControl::queryAggregation(_rType);
131     return aRet.hasValue() ? aRet : ::cppu::queryInterface(_rType,static_cast<css::frame::XDispatch*>(this));
132 }
133 
getTypes()134 Sequence< Type > SAL_CALL SbaXGridControl::getTypes(  )
135 {
136     return comphelper::concatSequences(
137         FmXGridControl::getTypes(),
138         Sequence { cppu::UnoType<css::frame::XDispatch>::get() });
139 }
140 
getImplementationId()141 Sequence< sal_Int8 > SAL_CALL SbaXGridControl::getImplementationId(  )
142 {
143     return css::uno::Sequence<sal_Int8>();
144 }
145 
createPeer(const Reference<css::awt::XToolkit> & rToolkit,const Reference<css::awt::XWindowPeer> & rParentPeer)146 void SAL_CALL SbaXGridControl::createPeer(const Reference< css::awt::XToolkit > & rToolkit, const Reference< css::awt::XWindowPeer > & rParentPeer)
147 {
148     FmXGridControl::createPeer(rToolkit, rParentPeer);
149 
150     OSL_ENSURE(!mbCreatingPeer, "FmXGridControl::createPeer : recursion!");
151         // see the base class' createPeer for a comment on this
152 
153     // TODO: why the hell this whole class does not use any mutex?
154 
155     Reference< css::frame::XDispatch >  xDisp(getPeer(), UNO_QUERY);
156     for (auto const& elem : m_aStatusMultiplexer)
157     {
158         if (elem.second.is() && elem.second->getLength())
159             xDisp->addStatusListener(elem.second, elem.first);
160     }
161 }
162 
dispatch(const css::util::URL & aURL,const Sequence<PropertyValue> & aArgs)163 void SAL_CALL SbaXGridControl::dispatch(const css::util::URL& aURL, const Sequence< PropertyValue >& aArgs)
164 {
165     Reference< css::frame::XDispatch >  xDisp(getPeer(), UNO_QUERY);
166     if (xDisp.is())
167         xDisp->dispatch(aURL, aArgs);
168 }
169 
addStatusListener(const Reference<XStatusListener> & _rxListener,const URL & _rURL)170 void SAL_CALL SbaXGridControl::addStatusListener( const Reference< XStatusListener > & _rxListener, const URL& _rURL )
171 {
172     ::osl::MutexGuard aGuard( GetMutex() );
173     if ( !_rxListener.is() )
174         return;
175 
176     rtl::Reference<SbaXStatusMultiplexer>& xMultiplexer = m_aStatusMultiplexer[ _rURL ];
177     if ( !xMultiplexer.is() )
178     {
179         xMultiplexer = new SbaXStatusMultiplexer( *this, GetMutex() );
180     }
181 
182     xMultiplexer->addInterface( _rxListener );
183     if ( getPeer().is() )
184     {
185         if ( 1 == xMultiplexer->getLength() )
186         {   // the first external listener for this URL
187             Reference< XDispatch >  xDisp( getPeer(), UNO_QUERY );
188             xDisp->addStatusListener( xMultiplexer, _rURL );
189         }
190         else
191         {   // already have other listeners for this URL
192             _rxListener->statusChanged( xMultiplexer->getLastEvent() );
193         }
194     }
195 }
196 
removeStatusListener(const Reference<css::frame::XStatusListener> & _rxListener,const css::util::URL & _rURL)197 void SAL_CALL SbaXGridControl::removeStatusListener(const Reference< css::frame::XStatusListener > & _rxListener, const css::util::URL& _rURL)
198 {
199     ::osl::MutexGuard aGuard( GetMutex() );
200 
201     rtl::Reference<SbaXStatusMultiplexer>& xMultiplexer = m_aStatusMultiplexer[_rURL];
202     if (!xMultiplexer.is())
203     {
204         xMultiplexer = new SbaXStatusMultiplexer(*this,GetMutex());
205     }
206 
207     if (getPeer().is() && xMultiplexer->getLength() == 1)
208     {
209         Reference< css::frame::XDispatch >  xDisp(getPeer(), UNO_QUERY);
210         xDisp->removeStatusListener(xMultiplexer, _rURL);
211     }
212     xMultiplexer->removeInterface( _rxListener );
213 }
214 
dispose()215 void SAL_CALL SbaXGridControl::dispose()
216 {
217     SolarMutexGuard aGuard;
218 
219     EventObject aEvt;
220     aEvt.Source = *this;
221 
222     for (auto & elem : m_aStatusMultiplexer)
223     {
224         if (elem.second.is())
225         {
226             elem.second->disposeAndClear(aEvt);
227             elem.second.clear();
228         }
229     }
230     StatusMultiplexerArray().swap(m_aStatusMultiplexer);
231 
232     FmXGridControl::dispose();
233 }
234 
235 // SbaXGridPeer
SbaXGridPeer(const Reference<XComponentContext> & _rM)236 SbaXGridPeer::SbaXGridPeer(const Reference< XComponentContext >& _rM)
237 : FmXGridPeer(_rM)
238 {
239 }
240 
~SbaXGridPeer()241 SbaXGridPeer::~SbaXGridPeer()
242 {
243 }
244 
dispose()245 void SAL_CALL SbaXGridPeer::dispose()
246 {
247     {
248         std::unique_lock g(m_aMutex);
249         EventObject aEvt(*this);
250         m_aStatusListeners.disposeAndClear(g, aEvt);
251     }
252     FmXGridPeer::dispose();
253 }
254 
NotifyStatusChanged(const css::util::URL & _rUrl,const Reference<css::frame::XStatusListener> & xControl)255 void SbaXGridPeer::NotifyStatusChanged(const css::util::URL& _rUrl, const Reference< css::frame::XStatusListener > & xControl)
256 {
257     VclPtr< SbaGridControl > pGrid = GetAs< SbaGridControl >();
258     if (!pGrid)
259         return;
260 
261     css::frame::FeatureStateEvent aEvt;
262     aEvt.Source = *this;
263     aEvt.IsEnabled = !pGrid->IsReadOnlyDB();
264     aEvt.FeatureURL = _rUrl;
265 
266     MapDispatchToBool::const_iterator aURLStatePos = m_aDispatchStates.find( classifyDispatchURL( _rUrl ) );
267     if ( m_aDispatchStates.end() != aURLStatePos )
268         aEvt.State <<= aURLStatePos->second;
269     else
270         aEvt.State <<= false;
271 
272     if (xControl.is())
273         xControl->statusChanged(aEvt);
274     else
275     {
276         std::unique_lock g(m_aMutex);
277         ::comphelper::OInterfaceContainerHelper4<css::frame::XStatusListener> * pIter
278             = m_aStatusListeners.getContainer(g, _rUrl);
279 
280         if (pIter)
281         {
282             pIter->notifyEach( g, &XStatusListener::statusChanged, aEvt );
283         }
284     }
285 }
286 
queryInterface(const Type & _rType)287 Any SAL_CALL SbaXGridPeer::queryInterface(const Type& _rType)
288 {
289     Any aRet = ::cppu::queryInterface(_rType,static_cast<css::frame::XDispatch*>(this));
290     if(aRet.hasValue())
291         return aRet;
292     return FmXGridPeer::queryInterface(_rType);
293 }
294 
queryDispatch(const css::util::URL & aURL,const OUString & aTargetFrameName,sal_Int32 nSearchFlags)295 Reference< css::frame::XDispatch >  SAL_CALL SbaXGridPeer::queryDispatch(const css::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags)
296 {
297     if  (   ( aURL.Complete == ".uno:GridSlots/BrowserAttribs" ) || ( aURL.Complete == ".uno:GridSlots/RowHeight" )
298         ||  ( aURL.Complete == ".uno:GridSlots/ColumnAttribs" )  || ( aURL.Complete == ".uno:GridSlots/ColumnWidth" )
299         )
300     {
301         return static_cast<css::frame::XDispatch*>(this);
302     }
303 
304     return FmXGridPeer::queryDispatch(aURL, aTargetFrameName, nSearchFlags);
305 }
306 
IMPL_LINK_NOARG(SbaXGridPeer,OnDispatchEvent,void *,void)307 IMPL_LINK_NOARG( SbaXGridPeer, OnDispatchEvent, void*, void )
308 {
309     VclPtr< SbaGridControl > pGrid = GetAs< SbaGridControl >();
310     if ( !pGrid )    // if this fails, we were disposing before arriving here
311         return;
312 
313     if ( !Application::IsMainThread() )
314     {
315         // still not in the main thread (see SbaXGridPeer::dispatch). post an event, again
316         // without moving the special even to the back of the queue
317         pGrid->PostUserEvent( LINK( this, SbaXGridPeer, OnDispatchEvent ) );
318     }
319     else
320     {
321         DispatchArgs aArgs = m_aDispatchArgs.front();
322         m_aDispatchArgs.pop();
323 
324         SbaXGridPeer::dispatch( aArgs.aURL, aArgs.aArgs );
325     }
326 }
327 
classifyDispatchURL(const URL & _rURL)328 SbaXGridPeer::DispatchType SbaXGridPeer::classifyDispatchURL( const URL& _rURL )
329 {
330     DispatchType eURLType = dtUnknown;
331     if ( _rURL.Complete == ".uno:GridSlots/BrowserAttribs" )
332         eURLType = dtBrowserAttribs;
333     else if ( _rURL.Complete == ".uno:GridSlots/RowHeight" )
334         eURLType = dtRowHeight;
335     else if ( _rURL.Complete == ".uno:GridSlots/ColumnAttribs" )
336         eURLType = dtColumnAttribs;
337     else if ( _rURL.Complete == ".uno:GridSlots/ColumnWidth" )
338         eURLType = dtColumnWidth;
339     return eURLType;
340 }
341 
dispatch(const URL & aURL,const Sequence<PropertyValue> & aArgs)342 void SAL_CALL SbaXGridPeer::dispatch(const URL& aURL, const Sequence< PropertyValue >& aArgs)
343 {
344     VclPtr< SbaGridControl > pGrid = GetAs< SbaGridControl >();
345     if (!pGrid)
346         return;
347 
348     if ( !Application::IsMainThread() )
349     {
350         // we're not in the main thread. This is bad, as we want to raise windows here,
351         // and VCL does not like windows to be opened in non-main threads (at least on Win32).
352         // Okay, do this async. No problem with this, as XDispatch::dispatch is defined to be
353         // a one-way method.
354 
355         // save the args
356         DispatchArgs aDispatchArgs;
357         aDispatchArgs.aURL = aURL;
358         aDispatchArgs.aArgs = aArgs;
359         m_aDispatchArgs.push( aDispatchArgs );
360 
361         // post an event
362         // we use the Window::PostUserEvent here, instead of the application::PostUserEvent
363         // this saves us from keeping track of these events - as soon as the window dies,
364         // the events are deleted automatically. For the application way, we would need to
365         // do this ourself.
366         // As we use our grid as window, and the grid dies before we die, this should be no problem.
367         pGrid->PostUserEvent( LINK( this, SbaXGridPeer, OnDispatchEvent ) );
368         return;
369     }
370 
371     SolarMutexGuard aGuard;
372     sal_Int16 nColId = -1;
373     for (const PropertyValue& rArg : aArgs)
374     {
375         if (rArg.Name == "ColumnViewPos")
376         {
377             nColId = pGrid->GetColumnIdFromViewPos(::comphelper::getINT16(rArg.Value));
378             break;
379         }
380         if (rArg.Name == "ColumnModelPos")
381         {
382             nColId = pGrid->GetColumnIdFromModelPos(::comphelper::getINT16(rArg.Value));
383             break;
384         }
385         if (rArg.Name == "ColumnId")
386         {
387             nColId = ::comphelper::getINT16(rArg.Value);
388             break;
389         }
390     }
391 
392     DispatchType eURLType = classifyDispatchURL( aURL );
393 
394     if ( dtUnknown == eURLType )
395         return;
396 
397     // notify any status listeners that the dialog is now active (well, about to be active)
398     MapDispatchToBool::const_iterator aThisURLState = m_aDispatchStates.emplace( eURLType, true ).first;
399     NotifyStatusChanged( aURL, nullptr );
400 
401     // execute the dialog
402     switch ( eURLType )
403     {
404         case dtBrowserAttribs:
405             pGrid->SetBrowserAttrs();
406             break;
407 
408         case dtRowHeight:
409             pGrid->SetRowHeight();
410             break;
411 
412         case dtColumnAttribs:
413         {
414             OSL_ENSURE(nColId != -1, "SbaXGridPeer::dispatch : invalid parameter !");
415             if (nColId != -1)
416                 break;
417             pGrid->SetColAttrs(nColId);
418         }
419         break;
420 
421         case dtColumnWidth:
422         {
423             OSL_ENSURE(nColId != -1, "SbaXGridPeer::dispatch : invalid parameter !");
424             if (nColId != -1)
425                 break;
426             pGrid->SetColWidth(nColId);
427         }
428         break;
429 
430         case dtUnknown:
431             break;
432     }
433 
434     // notify any status listeners that the dialog vanished
435     m_aDispatchStates.erase( aThisURLState );
436     NotifyStatusChanged( aURL, nullptr );
437 }
438 
addStatusListener(const Reference<css::frame::XStatusListener> & xControl,const css::util::URL & aURL)439 void SAL_CALL SbaXGridPeer::addStatusListener(const Reference< css::frame::XStatusListener > & xControl, const css::util::URL& aURL)
440 {
441     {
442         std::unique_lock g(m_aMutex);
443         ::comphelper::OInterfaceContainerHelper4< css::frame::XStatusListener >* pCont
444             = m_aStatusListeners.getContainer(g, aURL);
445         if (!pCont)
446             m_aStatusListeners.addInterface(g, aURL,xControl);
447         else
448             pCont->addInterface(g, xControl);
449     }
450     NotifyStatusChanged(aURL, xControl);
451 }
452 
removeStatusListener(const Reference<css::frame::XStatusListener> & xControl,const css::util::URL & aURL)453 void SAL_CALL SbaXGridPeer::removeStatusListener(const Reference< css::frame::XStatusListener > & xControl, const css::util::URL& aURL)
454 {
455     std::unique_lock g(m_aMutex);
456     ::comphelper::OInterfaceContainerHelper4< css::frame::XStatusListener >* pCont = m_aStatusListeners.getContainer(g, aURL);
457     if ( pCont )
458         pCont->removeInterface(g, xControl);
459 }
460 
getTypes()461 Sequence< Type > SAL_CALL SbaXGridPeer::getTypes()
462 {
463     return comphelper::concatSequences(
464         FmXGridPeer::getTypes(),
465         Sequence { cppu::UnoType<css::frame::XDispatch>::get() });
466 }
467 
imp_CreateControl(vcl::Window * pParent,WinBits nStyle)468 VclPtr<FmGridControl> SbaXGridPeer::imp_CreateControl(vcl::Window* pParent, WinBits nStyle)
469 {
470     return VclPtr<SbaGridControl>::Create( m_xContext, pParent, this, nStyle);
471 }
472 
473 // SbaGridHeader
474 
SbaGridHeader(BrowseBox * pParent)475 SbaGridHeader::SbaGridHeader(BrowseBox* pParent)
476     :FmGridHeader(pParent, WB_STDHEADERBAR | WB_DRAG)
477     ,DragSourceHelper(this)
478 {
479 }
480 
~SbaGridHeader()481 SbaGridHeader::~SbaGridHeader()
482 {
483     disposeOnce();
484 }
485 
dispose()486 void SbaGridHeader::dispose()
487 {
488     DragSourceHelper::dispose();
489     FmGridHeader::dispose();
490 }
491 
StartDrag(sal_Int8 _nAction,const Point & _rPosPixel)492 void SbaGridHeader::StartDrag( sal_Int8 _nAction, const Point& _rPosPixel )
493 {
494     SolarMutexGuard aGuard;
495         // in the new DnD API, the solar mutex is not locked when StartDrag is called
496 
497     ImplStartColumnDrag( _nAction, _rPosPixel );
498 }
499 
MouseButtonDown(const MouseEvent & _rMEvt)500 void SbaGridHeader::MouseButtonDown( const MouseEvent& _rMEvt )
501 {
502     if (_rMEvt.IsLeft())
503         if (_rMEvt.GetClicks() != 2)
504         {
505             // the base class will start a column move here, which we don't want to allow
506             // (at the moment. If we store relative positions with the columns, we can allow column moves...)
507 
508         }
509 
510     FmGridHeader::MouseButtonDown(_rMEvt);
511 }
512 
ImplStartColumnDrag(sal_Int8 _nAction,const Point & _rMousePos)513 void SbaGridHeader::ImplStartColumnDrag(sal_Int8 _nAction, const Point& _rMousePos)
514 {
515     sal_uInt16 nId = GetItemId(_rMousePos);
516     bool bResizingCol = false;
517     if (HEADERBAR_ITEM_NOTFOUND != nId)
518     {
519         tools::Rectangle aColRect = GetItemRect(nId);
520         aColRect.AdjustLeft(nId ? 3 : 0 ); // the handle col (nId == 0) does not have a left margin for resizing
521         aColRect.AdjustRight( -3 );
522         bResizingCol = !aColRect.Contains(_rMousePos);
523     }
524     if (bResizingCol)
525         return;
526 
527     // force the base class to end its drag mode
528     EndTracking(TrackingEventFlags::Cancel | TrackingEventFlags::End);
529 
530     // because we have 3d-buttons the select handler is called from MouseButtonUp, but StartDrag
531     // occurs earlier (while the mouse button is down)
532     // so for optical reasons we select the column before really starting the drag operation.
533     notifyColumnSelect(nId);
534 
535     static_cast<SbaGridControl*>(GetParent())->StartDrag(_nAction,
536             Point(
537                 _rMousePos.X() + GetPosPixel().X(),     // we aren't left-justified with our parent, in contrast to the data window
538                 _rMousePos.Y() - GetSizePixel().Height()
539             )
540         );
541 }
542 
PreExecuteColumnContextMenu(sal_uInt16 nColId,weld::Menu & rMenu,weld::Menu & rInsertMenu,weld::Menu & rChangeMenu,weld::Menu & rShowMenu)543 void SbaGridHeader::PreExecuteColumnContextMenu(sal_uInt16 nColId, weld::Menu& rMenu,
544                                                 weld::Menu& rInsertMenu, weld::Menu& rChangeMenu,
545                                                 weld::Menu& rShowMenu)
546 {
547     FmGridHeader::PreExecuteColumnContextMenu(nColId, rMenu, rInsertMenu, rChangeMenu, rShowMenu);
548 
549     // some items are valid only if the db isn't readonly
550     bool bDBIsReadOnly = static_cast<SbaGridControl*>(GetParent())->IsReadOnlyDB();
551 
552     if (bDBIsReadOnly)
553     {
554         rMenu.set_visible(u"hide"_ustr, false);
555         rMenu.set_sensitive(u"hide"_ustr, false);
556         rMenu.set_visible(u"show"_ustr, false);
557         rMenu.set_sensitive(u"show"_ustr, false);
558     }
559 
560     // prepend some new items
561     bool bColAttrs = (nColId != sal_uInt16(-1)) && (nColId != 0);
562     if ( !bColAttrs || bDBIsReadOnly)
563         return;
564 
565     sal_uInt16 nPos = 0;
566     sal_uInt16 nModelPos = static_cast<SbaGridControl*>(GetParent())->GetModelColumnPos(nColId);
567     Reference< XPropertySet >  xField = static_cast<SbaGridControl*>(GetParent())->getField(nModelPos);
568 
569     if ( xField.is() )
570     {
571         switch( ::comphelper::getINT32(xField->getPropertyValue(PROPERTY_TYPE)) )
572         {
573         case DataType::BINARY:
574         case DataType::VARBINARY:
575         case DataType::LONGVARBINARY:
576         case DataType::SQLNULL:
577         case DataType::OBJECT:
578         case DataType::BLOB:
579         case DataType::CLOB:
580         case DataType::REF:
581             break;
582         default:
583             rMenu.insert(nPos++, u"colattrset"_ustr, DBA_RES(RID_STR_COLUMN_FORMAT),
584                          nullptr, nullptr, nullptr, TRISTATE_INDET);
585             rMenu.insert_separator(nPos++, u"separator1"_ustr);
586         }
587     }
588 
589     rMenu.insert(nPos++, u"colwidth"_ustr, DBA_RES(RID_STR_COLUMN_WIDTH),
590                  nullptr, nullptr, nullptr, TRISTATE_INDET);
591     rMenu.insert_separator(nPos++, u"separator2"_ustr);
592 }
593 
PostExecuteColumnContextMenu(sal_uInt16 nColId,const weld::Menu & rMenu,const OUString & rExecutionResult)594 void SbaGridHeader::PostExecuteColumnContextMenu(sal_uInt16 nColId, const weld::Menu& rMenu, const OUString& rExecutionResult)
595 {
596     if (rExecutionResult == "colwidth")
597         static_cast<SbaGridControl*>(GetParent())->SetColWidth(nColId);
598     else if (rExecutionResult == "colattrset")
599         static_cast<SbaGridControl*>(GetParent())->SetColAttrs(nColId);
600     else
601         FmGridHeader::PostExecuteColumnContextMenu(nColId, rMenu, rExecutionResult);
602 }
603 
604 // SbaGridControl
SbaGridControl(Reference<XComponentContext> const & _rM,vcl::Window * pParent,FmXGridPeer * _pPeer,WinBits nBits)605 SbaGridControl::SbaGridControl(Reference< XComponentContext > const & _rM,
606                                vcl::Window* pParent, FmXGridPeer* _pPeer, WinBits nBits)
607     :FmGridControl(_rM,pParent, _pPeer, nBits)
608     ,m_pMasterListener(nullptr)
609     ,m_nAsyncDropEvent(nullptr)
610     ,m_bActivatingForDrop(false)
611 {
612 }
613 
~SbaGridControl()614 SbaGridControl::~SbaGridControl()
615 {
616     disposeOnce();
617 }
618 
dispose()619 void SbaGridControl::dispose()
620 {
621     if (m_nAsyncDropEvent)
622         Application::RemoveUserEvent(m_nAsyncDropEvent);
623     m_nAsyncDropEvent = nullptr;
624     FmGridControl::dispose();
625 }
626 
imp_CreateHeaderBar(BrowseBox * pParent)627 VclPtr<BrowserHeader> SbaGridControl::imp_CreateHeaderBar(BrowseBox* pParent)
628 {
629     return VclPtr<SbaGridHeader>::Create(pParent);
630 }
631 
GetController(sal_Int32 nRow,sal_uInt16 nCol)632 CellController* SbaGridControl::GetController(sal_Int32 nRow, sal_uInt16 nCol)
633 {
634     if ( m_bActivatingForDrop )
635         return nullptr;
636 
637     return FmGridControl::GetController(nRow, nCol);
638 }
639 
PreExecuteRowContextMenu(weld::Menu & rMenu)640 void SbaGridControl::PreExecuteRowContextMenu(weld::Menu& rMenu)
641 {
642     FmGridControl::PreExecuteRowContextMenu(rMenu);
643 
644     sal_uInt16 nPos = 0;
645 
646     if (!IsReadOnlyDB())
647     {
648         rMenu.insert(nPos++, u"tableattr"_ustr, DBA_RES(RID_STR_TABLE_FORMAT),
649                      nullptr, nullptr, nullptr, TRISTATE_INDET);
650         rMenu.insert(nPos++, u"rowheight"_ustr, DBA_RES(RID_STR_ROW_HEIGHT),
651                      nullptr, nullptr, nullptr, TRISTATE_INDET);
652         rMenu.insert_separator(nPos++, u"separator1"_ustr);
653     }
654 
655     if ( GetSelectRowCount() > 0 )
656     {
657         rMenu.insert(nPos++, u"copy"_ustr, DBA_RES(RID_STR_COPY),
658                      nullptr, nullptr, nullptr, TRISTATE_INDET);
659         rMenu.insert_separator(nPos++, u"separator2"_ustr);
660     }
661 }
662 
GetDatasourceFormatter()663 SvNumberFormatter* SbaGridControl::GetDatasourceFormatter()
664 {
665     Reference< css::util::XNumberFormatsSupplier >  xSupplier = ::dbtools::getNumberFormats(::dbtools::getConnection(Reference< XRowSet > (getDataSource(),UNO_QUERY)), true, getContext());
666 
667     SvNumberFormatsSupplierObj* pSupplierImpl = comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>( xSupplier );
668     if ( !pSupplierImpl )
669         return nullptr;
670 
671     SvNumberFormatter* pFormatter = pSupplierImpl->GetNumberFormatter();
672     return pFormatter;
673 }
674 
SetColWidth(sal_uInt16 nColId)675 void SbaGridControl::SetColWidth(sal_uInt16 nColId)
676 {
677     // get the (UNO) column model
678     sal_uInt16 nModelPos = GetModelColumnPos(nColId);
679     Reference< XIndexAccess >  xCols = GetPeer()->getColumns();
680     Reference< XPropertySet >  xAffectedCol;
681     if (xCols.is() && (nModelPos != sal_uInt16(-1)))
682         xAffectedCol.set(xCols->getByIndex(nModelPos), css::uno::UNO_QUERY);
683 
684     if (!xAffectedCol.is())
685         return;
686 
687     Any aWidth = xAffectedCol->getPropertyValue(PROPERTY_WIDTH);
688     sal_Int32 nCurWidth = aWidth.hasValue() ? ::comphelper::getINT32(aWidth) : -1;
689 
690     DlgSize aDlgColWidth(GetFrameWeld(), nCurWidth, false);
691     if (aDlgColWidth.run() != RET_OK)
692         return;
693 
694     sal_Int32 nValue = aDlgColWidth.GetValue();
695     Any aNewWidth;
696     if (-1 == nValue)
697     {   // set to default
698         Reference< XPropertyState >  xPropState(xAffectedCol, UNO_QUERY);
699         if (xPropState.is())
700         {
701             try { aNewWidth = xPropState->getPropertyDefault(PROPERTY_WIDTH); } catch(Exception&) { } ;
702         }
703     }
704     else
705         aNewWidth <<= nValue;
706     try {  xAffectedCol->setPropertyValue(PROPERTY_WIDTH, aNewWidth); } catch(Exception&) { } ;
707 }
708 
SetRowHeight()709 void SbaGridControl::SetRowHeight()
710 {
711     Reference< XPropertySet >  xCols(GetPeer()->getColumns(), UNO_QUERY);
712     if (!xCols.is())
713         return;
714 
715     Any aHeight = xCols->getPropertyValue(PROPERTY_ROW_HEIGHT);
716     sal_Int32 nCurHeight = aHeight.hasValue() ? ::comphelper::getINT32(aHeight) : -1;
717 
718     DlgSize aDlgRowHeight(GetFrameWeld(), nCurHeight, true);
719     if (aDlgRowHeight.run() != RET_OK)
720         return;
721 
722     sal_Int32 nValue = aDlgRowHeight.GetValue();
723     Any aNewHeight;
724     if (sal_Int16(-1) == nValue)
725     {   // set to default
726         Reference< XPropertyState >  xPropState(xCols, UNO_QUERY);
727         if (xPropState.is())
728         {
729             try
730             {
731                 aNewHeight = xPropState->getPropertyDefault(PROPERTY_ROW_HEIGHT);
732             }
733             catch(Exception&)
734             { }
735         }
736     }
737     else
738         aNewHeight <<= nValue;
739     try
740     {
741         xCols->setPropertyValue(PROPERTY_ROW_HEIGHT, aNewHeight);
742     }
743     catch(Exception&)
744     {
745         TOOLS_WARN_EXCEPTION( "dbaccess", "setPropertyValue: PROPERTY_ROW_HEIGHT throws an exception");
746     }
747 }
748 
SetColAttrs(sal_uInt16 nColId)749 void SbaGridControl::SetColAttrs(sal_uInt16 nColId)
750 {
751     SvNumberFormatter* pFormatter = GetDatasourceFormatter();
752     if (!pFormatter)
753         return;
754 
755     sal_uInt16 nModelPos = GetModelColumnPos(nColId);
756 
757     // get the (UNO) column model
758     Reference< XIndexAccess >  xCols = GetPeer()->getColumns();
759     Reference< XPropertySet >  xAffectedCol;
760     if (xCols.is() && (nModelPos != sal_uInt16(-1)))
761         xAffectedCol.set(xCols->getByIndex(nModelPos), css::uno::UNO_QUERY);
762 
763     // get the field the column is bound to
764     Reference< XPropertySet >  xField = getField(nModelPos);
765     ::dbaui::callColumnFormatDialog(xAffectedCol,xField,pFormatter,GetFrameWeld());
766 }
767 
SetBrowserAttrs()768 void SbaGridControl::SetBrowserAttrs()
769 {
770     Reference< XPropertySet >  xGridModel(GetPeer()->getColumns(), UNO_QUERY);
771     if (!xGridModel.is())
772         return;
773 
774     try
775     {
776         Reference< XComponentContext > xContext = getContext();
777         css::uno::Sequence<css::uno::Any> aArguments{
778             Any(comphelper::makePropertyValue(u"IntrospectedObject"_ustr, xGridModel)),
779             Any(comphelper::makePropertyValue(u"ParentWindow"_ustr, VCLUnoHelper::GetInterface(this)))
780         };
781         Reference<XExecutableDialog> xExecute(xContext->getServiceManager()->createInstanceWithArgumentsAndContext(u"com.sun.star.form.ControlFontDialog"_ustr,
782                                               aArguments, xContext), css::uno::UNO_QUERY_THROW);
783         xExecute->execute();
784     }
785     catch( const Exception& )
786     {
787         DBG_UNHANDLED_EXCEPTION("dbaccess");
788     }
789 }
790 
PostExecuteRowContextMenu(const OUString & rExecutionResult)791 void SbaGridControl::PostExecuteRowContextMenu(const OUString& rExecutionResult)
792 {
793     if (rExecutionResult == "tableattr")
794         SetBrowserAttrs();
795     else if (rExecutionResult == "rowheight")
796         SetRowHeight();
797     else if (rExecutionResult == "copy")
798         CopySelectedRowsToClipboard();
799     else
800         FmGridControl::PostExecuteRowContextMenu(rExecutionResult);
801 }
802 
Select()803 void SbaGridControl::Select()
804 {
805     // Some selection has changed ...
806     FmGridControl::Select();
807 
808     if (m_pMasterListener)
809         m_pMasterListener->SelectionChanged();
810 }
811 
ActivateCell(sal_Int32 nRow,sal_uInt16 nCol,bool bSetCellFocus)812 void SbaGridControl::ActivateCell(sal_Int32 nRow, sal_uInt16 nCol, bool bSetCellFocus /*= sal_True*/ )
813 {
814     FmGridControl::ActivateCell(nRow, nCol, bSetCellFocus);
815     if (m_pMasterListener)
816         m_pMasterListener->CellActivated();
817 }
818 
DeactivateCell(bool bUpdate)819 void SbaGridControl::DeactivateCell(bool bUpdate /*= sal_True*/)
820 {
821     FmGridControl::DeactivateCell(bUpdate);
822     if (m_pMasterListener)
823         m_pMasterListener->CellDeactivated();
824 }
825 
onRowChange()826 void SbaGridControl::onRowChange()
827 {
828     if ( m_pMasterListener )
829         m_pMasterListener->RowChanged();
830 }
831 
onColumnChange()832 void SbaGridControl::onColumnChange()
833 {
834     if ( m_pMasterListener )
835         m_pMasterListener->ColumnChanged();
836 }
837 
getField(sal_uInt16 nModelPos)838 Reference< XPropertySet >  SbaGridControl::getField(sal_uInt16 nModelPos)
839 {
840     Reference< XPropertySet >  xEmptyReturn;
841     try
842     {
843         // first get the name of the column
844         Reference< XIndexAccess >  xCols = GetPeer()->getColumns();
845         if ( xCols.is() && xCols->getCount() > nModelPos )
846         {
847             Reference< XPropertySet >  xCol(xCols->getByIndex(nModelPos),UNO_QUERY);
848             if ( xCol.is() )
849                 xEmptyReturn.set(xCol->getPropertyValue(PROPERTY_BOUNDFIELD),UNO_QUERY);
850         }
851         else
852             OSL_FAIL("SbaGridControl::getField getColumns returns NULL or ModelPos is > than count!");
853     }
854     catch (const Exception&)
855     {
856         TOOLS_WARN_EXCEPTION("dbaccess", "SbaGridControl::getField Exception occurred");
857     }
858 
859     return xEmptyReturn;
860 }
861 
IsReadOnlyDB() const862 bool SbaGridControl::IsReadOnlyDB() const
863 {
864     // assume yes if anything fails
865     bool bDBIsReadOnly = true;
866 
867     try
868     {
869         // the db is the implemented by the parent of the grid control's model ...
870         Reference< XChild >  xColumns(GetPeer()->getColumns(), UNO_QUERY);
871         if (xColumns.is())
872         {
873             Reference< XRowSet >  xDataSource(xColumns->getParent(), UNO_QUERY);
874             ::dbtools::ensureRowSetConnection( xDataSource, getContext(), nullptr );
875             Reference< XChild >  xConn(::dbtools::getConnection(xDataSource),UNO_QUERY);
876             if (xConn.is())
877             {
878                 // ... and the RO-flag simply is implemented by a property
879                 Reference< XPropertySet >  xDbProps(xConn->getParent(), UNO_QUERY);
880                 if (xDbProps.is())
881                 {
882                     Reference< XPropertySetInfo >  xInfo = xDbProps->getPropertySetInfo();
883                     if (xInfo->hasPropertyByName(PROPERTY_ISREADONLY))
884                         bDBIsReadOnly = ::comphelper::getBOOL(xDbProps->getPropertyValue(PROPERTY_ISREADONLY));
885                 }
886             }
887         }
888     }
889     catch (const Exception&)
890     {
891         TOOLS_WARN_EXCEPTION("dbaccess", "SbaGridControl::IsReadOnlyDB Exception occurred");
892     }
893 
894     return bDBIsReadOnly;
895 }
896 
MouseButtonDown(const BrowserMouseEvent & rMEvt)897 void SbaGridControl::MouseButtonDown( const BrowserMouseEvent& rMEvt)
898 {
899     sal_Int32 nRow = GetRowAtYPosPixel(rMEvt.GetPosPixel().Y());
900     sal_uInt16 nColPos = GetColumnAtXPosPixel(rMEvt.GetPosPixel().X());
901     sal_uInt16 nViewPos = (nColPos == BROWSER_INVALIDID) ? sal_uInt16(-1) : nColPos-1;
902         // 'the handle column' and 'no valid column' will both result in a view position of -1 !
903 
904     bool bHitEmptySpace = (nRow > GetRowCount()) || (nViewPos == sal_uInt16(-1));
905 
906     if (bHitEmptySpace && (rMEvt.GetClicks() == 2) && rMEvt.IsMod1())
907         Control::MouseButtonDown(rMEvt);
908     else
909         FmGridControl::MouseButtonDown(rMEvt);
910 }
911 
StartDrag(sal_Int8 _nAction,const Point & _rPosPixel)912 void SbaGridControl::StartDrag( sal_Int8 _nAction, const Point& _rPosPixel )
913 {
914     SolarMutexGuard aGuard;
915         // in the new DnD API, the solar mutex is not locked when StartDrag is called
916 
917     bool bHandled = false;
918 
919     do
920     {
921         // determine if dragging is allowed
922         // (Yes, this is controller (not view) functionality. But collecting and evaluating all the
923         // information necessary via UNO would be quite difficult (if not impossible) so
924         // my laziness says 'do it here'...)
925         sal_Int32 nRow = GetRowAtYPosPixel(_rPosPixel.Y());
926         sal_uInt16 nColPos = GetColumnAtXPosPixel(_rPosPixel.X());
927         sal_uInt16 nViewPos = (nColPos == BROWSER_INVALIDID) ? sal_uInt16(-1) : nColPos-1;
928             // 'the handle column' and 'no valid column' will both result in a view position of -1 !
929 
930         bool bCurrentRowVirtual = IsCurrentAppending() && IsModified();
931         // the current row doesn't really exist: the user's appending a new one and already has entered some data,
932         // so the row contains data which has no counter part within the data source
933 
934         sal_Int32 nCorrectRowCount = GetRowCount();
935         if (GetOptions() & DbGridControlOptions::Insert)
936             --nCorrectRowCount; // there is an empty row for inserting records
937         if (bCurrentRowVirtual)
938             --nCorrectRowCount;
939 
940         if ((nColPos == BROWSER_INVALIDID) || (nRow >= nCorrectRowCount))
941             break;
942 
943         bool bHitHandle = (nColPos == 0);
944 
945         // check which kind of dragging has to be initiated
946         if  (   bHitHandle                          //  the handle column
947                                                     // AND
948             &&  (   GetSelectRowCount()             //  at least one row is selected
949                                                     // OR
950                 ||  (   (nRow >= 0)                 //  a row below the header
951                     &&  !bCurrentRowVirtual         //  we aren't appending a new record
952                     &&  (nRow != GetCurrentPos())   //  a row which is not the current one
953                     )                               // OR
954                 ||  (   (0 == GetSelectRowCount())  // no rows selected
955                     &&  (-1 == nRow)                // hit the header
956                     )
957                 )
958             )
959         {   // => start dragging the row
960             if (GetDataWindow().IsMouseCaptured())
961                 GetDataWindow().ReleaseMouse();
962 
963             if (0 == GetSelectRowCount())
964                 // no rows selected, but here in this branch
965                 // -> the user started dragging the upper left corner, which symbolizes the whole table
966                 SelectAll();
967 
968             getMouseEvent().Clear();
969             implTransferSelectedRows(static_cast<sal_Int16>(nRow), false);
970 
971             bHandled = true;
972         }
973         else if (   (nRow < 0)                      // the header
974                 &&  (!bHitHandle)                   // non-handle column
975                 &&  (nViewPos < GetViewColCount())  // valid (existing) column
976                 )
977         {   // => start dragging the column
978             if (GetDataWindow().IsMouseCaptured())
979                 GetDataWindow().ReleaseMouse();
980 
981             getMouseEvent().Clear();
982             DoColumnDrag(nViewPos);
983 
984             bHandled = true;
985         }
986         else if (   !bHitHandle     // non-handle column
987                 &&  (nRow >= 0)     // non-header row
988                 )
989         {   // => start dragging the field content
990             if (GetDataWindow().IsMouseCaptured())
991                 GetDataWindow().ReleaseMouse();
992 
993             getMouseEvent().Clear();
994             DoFieldDrag(nViewPos, static_cast<sal_Int16>(nRow));
995 
996             bHandled = true;
997         }
998     }
999     while (false);
1000 
1001     if (!bHandled)
1002         FmGridControl::StartDrag(_nAction, _rPosPixel);
1003 }
1004 
DoColumnDrag(sal_uInt16 nColumnPos)1005 void SbaGridControl::DoColumnDrag(sal_uInt16 nColumnPos)
1006 {
1007     Reference< XPropertySet >  xDataSource = getDataSource();
1008     OSL_ENSURE(xDataSource.is(), "SbaGridControl::DoColumnDrag : invalid data source !");
1009     ::dbtools::ensureRowSetConnection(Reference< XRowSet >(getDataSource(),UNO_QUERY), getContext(), nullptr);
1010 
1011     Reference< XPropertySet > xAffectedCol;
1012     Reference< XPropertySet > xAffectedField;
1013     Reference< XConnection > xActiveConnection;
1014 
1015     // determine the field to drag
1016     OUString sField;
1017     try
1018     {
1019         xActiveConnection = ::dbtools::getConnection(Reference< XRowSet >(getDataSource(),UNO_QUERY));
1020 
1021         sal_uInt16 nModelPos = GetModelColumnPos(GetColumnIdFromViewPos(nColumnPos));
1022         Reference< XIndexContainer >  xCols = GetPeer()->getColumns();
1023         xAffectedCol.set(xCols->getByIndex(nModelPos),UNO_QUERY);
1024         if (xAffectedCol.is())
1025         {
1026             xAffectedCol->getPropertyValue(PROPERTY_CONTROLSOURCE) >>= sField;
1027             xAffectedField.set(xAffectedCol->getPropertyValue(PROPERTY_BOUNDFIELD),UNO_QUERY);
1028         }
1029     }
1030     catch(Exception&)
1031     {
1032         OSL_FAIL("SbaGridControl::DoColumnDrag : something went wrong while getting the column");
1033     }
1034     if (sField.isEmpty())
1035         return;
1036 
1037     rtl::Reference<OColumnTransferable> pDataTransfer = new OColumnTransferable(xDataSource, sField, xAffectedField, xActiveConnection, ColumnTransferFormatFlags::FIELD_DESCRIPTOR | ColumnTransferFormatFlags::COLUMN_DESCRIPTOR);
1038     pDataTransfer->StartDrag(this, DND_ACTION_COPY | DND_ACTION_LINK);
1039 }
1040 
CopySelectedRowsToClipboard()1041 void SbaGridControl::CopySelectedRowsToClipboard()
1042 {
1043     OSL_ENSURE( GetSelectRowCount() > 0, "SbaGridControl::CopySelectedRowsToClipboard: invalid call!" );
1044     implTransferSelectedRows( static_cast<sal_Int16>(FirstSelectedRow()), true );
1045 }
1046 
implTransferSelectedRows(sal_Int16 nRowPos,bool _bTrueIfClipboardFalseIfDrag)1047 void SbaGridControl::implTransferSelectedRows( sal_Int16 nRowPos, bool _bTrueIfClipboardFalseIfDrag )
1048 {
1049     Reference< XPropertySet > xForm = getDataSource();
1050     OSL_ENSURE( xForm.is(), "SbaGridControl::implTransferSelectedRows: invalid form!" );
1051 
1052     // build the sequence of numbers of selected rows
1053     Sequence< Any > aSelectedRows;
1054     bool bSelectionBookmarks = true;
1055 
1056     // collect the affected rows
1057     if ((GetSelectRowCount() == 0) && (nRowPos >= 0))
1058     {
1059         aSelectedRows = { Any(static_cast<sal_Int32>(nRowPos + 1)) };
1060         bSelectionBookmarks = false;
1061     }
1062     else if ( !IsAllSelected() && GetSelectRowCount() )
1063     {
1064         aSelectedRows = getSelectionBookmarks();
1065         bSelectionBookmarks = true;
1066     }
1067 
1068     try
1069     {
1070         rtl::Reference<ODataClipboard> pTransfer = new ODataClipboard( xForm, aSelectedRows, bSelectionBookmarks, getContext() );
1071 
1072         if ( _bTrueIfClipboardFalseIfDrag )
1073             pTransfer->CopyToClipboard( this );
1074         else
1075             pTransfer->StartDrag(this, DND_ACTION_COPY | DND_ACTION_LINK);
1076     }
1077     catch(Exception&)
1078     {
1079     }
1080 }
1081 
DoFieldDrag(sal_uInt16 nColumnPos,sal_Int16 nRowPos)1082 void SbaGridControl::DoFieldDrag(sal_uInt16 nColumnPos, sal_Int16 nRowPos)
1083 {
1084     // the only thing to do here is dragging the pure cell text
1085     // the old implementation copied a SBA_FIELDDATAEXCHANGE_FORMAT, too, (which was rather expensive to obtain),
1086     // but we have no client for this DnD format anymore (the mail part of SO 5.2 was the only client)
1087 
1088     try
1089     {
1090         OUString sCellText;
1091         Reference< XGridFieldDataSupplier >  xFieldData(GetPeer());
1092         Sequence<sal_Bool> aSupportingText = xFieldData->queryFieldDataType(cppu::UnoType<decltype(sCellText)>::get());
1093         if (aSupportingText[nColumnPos])
1094         {
1095             Sequence< Any> aCellContents = xFieldData->queryFieldData(nRowPos, cppu::UnoType<decltype(sCellText)>::get());
1096             sCellText = ::comphelper::getString(aCellContents[nColumnPos]);
1097             ::svt::OStringTransfer::StartStringDrag(sCellText, this, DND_ACTION_COPY);
1098         }
1099     }
1100     catch(Exception&)
1101     {
1102         OSL_FAIL("SbaGridControl::DoFieldDrag : could not retrieve the cell's contents !");
1103         return;
1104     }
1105 
1106 }
1107 
1108     namespace {
1109 
1110 /// unary_function Functor object for class ZZ returntype is void
1111     struct SbaGridControlPrec
1112     {
operator ()__anon7fbc2a390111::SbaGridControlPrec1113         bool operator()(const DataFlavorExVector::value_type& _aType)
1114         {
1115             switch (_aType.mnSotId)
1116             {
1117                 case SotClipboardFormatId::DBACCESS_TABLE:   // table descriptor
1118                 case SotClipboardFormatId::DBACCESS_QUERY:   // query descriptor
1119                 case SotClipboardFormatId::DBACCESS_COMMAND: // SQL command
1120                     return true;
1121                 default: break;
1122             }
1123             return false;
1124         }
1125     };
1126 
1127     }
1128 
AcceptDrop(const BrowserAcceptDropEvent & rEvt)1129 sal_Int8 SbaGridControl::AcceptDrop( const BrowserAcceptDropEvent& rEvt )
1130 {
1131     sal_Int8 nAction = DND_ACTION_NONE;
1132 
1133     // we need a valid connection
1134     if (!::dbtools::getConnection(Reference< XRowSet > (getDataSource(),UNO_QUERY)).is())
1135         return nAction;
1136 
1137     if ( IsDropFormatSupported( SotClipboardFormatId::STRING ) )
1138         do
1139         {   // odd construction, but spares us a lot of (explicit ;) goto's
1140 
1141             if (!GetEmptyRow().is())
1142                 // without an empty row we're not in update mode
1143                 break;
1144 
1145             const sal_Int32   nRow = GetRowAtYPosPixel(rEvt.maPosPixel.Y(), false);
1146             const sal_uInt16  nCol = GetColumnId(GetColumnAtXPosPixel(rEvt.maPosPixel.X()));
1147 
1148             sal_Int32 nCorrectRowCount = GetRowCount();
1149             if (GetOptions() & DbGridControlOptions::Insert)
1150                 --nCorrectRowCount; // there is an empty row for inserting records
1151             if (IsCurrentAppending())
1152                 --nCorrectRowCount; // the current data record doesn't really exist, we are appending a new one
1153 
1154             if ( (nCol == BROWSER_INVALIDID) || (nRow >= nCorrectRowCount) || (nCol == 0) )
1155                 // no valid cell under the mouse cursor
1156                 break;
1157 
1158             tools::Rectangle aRect = GetCellRect(nRow, nCol, false);
1159             if (!aRect.Contains(rEvt.maPosPixel))
1160                 // not dropped within a cell (a cell isn't as wide as the column - the are small spaces)
1161                 break;
1162 
1163             if ((IsModified() || (GetCurrentRow().is() && GetCurrentRow()->IsModified())) && (GetCurrentPos() != nRow))
1164                 // there is a current and modified row or cell and he text is to be dropped into another one
1165                 break;
1166 
1167             CellControllerRef xCurrentController = Controller();
1168             if (xCurrentController.is() && xCurrentController->IsValueChangedFromSaved() && ((nRow != GetCurRow()) || (nCol != GetCurColumnId())))
1169                 // the current controller is modified and the user wants to drop in another cell -> no chance
1170                 // (when leaving the modified cell an error may occur - this is deadly while dragging)
1171                 break;
1172 
1173             Reference< XPropertySet >  xField = getField(GetModelColumnPos(nCol));
1174             if (!xField.is())
1175                 // the column is not valid bound (for instance a binary field)
1176                 break;
1177 
1178             try
1179             {
1180                 if (::comphelper::getBOOL(xField->getPropertyValue(PROPERTY_ISREADONLY)))
1181                     break;
1182             }
1183             catch (const Exception& )
1184             {
1185                 // assume RO
1186                 break;
1187             }
1188 
1189             try
1190             {
1191                 // assume that text can be dropped into a field if the column has a css::awt::XTextComponent interface
1192                 Reference< XIndexAccess >  xColumnControls(GetPeer());
1193                 if (xColumnControls.is())
1194                 {
1195                     Reference< css::awt::XTextComponent >  xColControl(
1196                         xColumnControls->getByIndex(GetViewColumnPos(nCol)),
1197                         css::uno::UNO_QUERY);
1198                     if (xColControl.is())
1199                     {
1200                         m_bActivatingForDrop = true;
1201                         GoToRowColumnId(nRow, nCol);
1202                         m_bActivatingForDrop = false;
1203 
1204                         nAction = DND_ACTION_COPY;
1205                     }
1206                 }
1207             }
1208             catch( const Exception& )
1209             {
1210                 DBG_UNHANDLED_EXCEPTION("dbaccess");
1211             }
1212 
1213         } while (false);
1214 
1215     if(nAction != DND_ACTION_COPY && GetEmptyRow().is())
1216     {
1217         const DataFlavorExVector& _rFlavors = GetDataFlavors();
1218         if(std::any_of(_rFlavors.begin(),_rFlavors.end(),SbaGridControlPrec()))
1219             nAction = DND_ACTION_COPY;
1220     }
1221 
1222     return (DND_ACTION_NONE != nAction) ? nAction : FmGridControl::AcceptDrop(rEvt);
1223 }
1224 
ExecuteDrop(const BrowserExecuteDropEvent & rEvt)1225 sal_Int8 SbaGridControl::ExecuteDrop( const BrowserExecuteDropEvent& rEvt )
1226 {
1227     // we need some properties of our data source
1228     Reference< XPropertySet >  xDataSource = getDataSource();
1229     if (!xDataSource.is())
1230         return DND_ACTION_NONE;
1231 
1232     // we need a valid connection
1233     if (!::dbtools::getConnection(Reference< XRowSet > (xDataSource,UNO_QUERY)).is())
1234         return DND_ACTION_NONE;
1235 
1236     if ( IsDropFormatSupported( SotClipboardFormatId::STRING ) )
1237     {
1238         sal_Int32   nRow = GetRowAtYPosPixel(rEvt.maPosPixel.Y(), false);
1239         sal_uInt16  nCol = GetColumnAtXPosPixel(rEvt.maPosPixel.X());
1240 
1241         sal_Int32 nCorrectRowCount = GetRowCount();
1242         if (GetOptions() & DbGridControlOptions::Insert)
1243             --nCorrectRowCount; // there is an empty row for inserting records
1244         if (IsCurrentAppending())
1245             --nCorrectRowCount; // the current data record doesn't really exist, we are appending a new one
1246 
1247         OSL_ENSURE((nCol != BROWSER_INVALIDID) && (nRow < nCorrectRowCount), "SbaGridControl::Drop : dropped on an invalid position !");
1248             // AcceptDrop should have caught this
1249 
1250         // from now we work with ids instead of positions
1251         nCol = GetColumnId(nCol);
1252 
1253         GoToRowColumnId(nRow, nCol);
1254         if (!IsEditing())
1255             ActivateCell();
1256 
1257         CellControllerRef xCurrentController = Controller();
1258         EditCellController* pController = dynamic_cast<EditCellController*>(xCurrentController.get());
1259         if (!pController)
1260             return DND_ACTION_NONE;
1261 
1262         // get the dropped string
1263         TransferableDataHelper aDropped( rEvt.maDropEvent.Transferable );
1264         OUString sDropped;
1265         if ( !aDropped.GetString( SotClipboardFormatId::STRING, sDropped ) )
1266             return DND_ACTION_NONE;
1267 
1268         IEditImplementation* pEditImplementation = pController->GetEditImplementation();
1269         pEditImplementation->SetText(sDropped);
1270         // SetText itself doesn't call a Modify as it isn't a user interaction
1271         pController->Modify();
1272 
1273         return DND_ACTION_COPY;
1274     }
1275 
1276     if(GetEmptyRow().is())
1277     {
1278         const DataFlavorExVector& _rFlavors = GetDataFlavors();
1279         if( std::any_of(_rFlavors.begin(),_rFlavors.end(), SbaGridControlPrec()) )
1280         {
1281             TransferableDataHelper aDropped( rEvt.maDropEvent.Transferable );
1282             m_aDataDescriptor = ODataAccessObjectTransferable::extractObjectDescriptor(aDropped);
1283             if (m_nAsyncDropEvent)
1284                 Application::RemoveUserEvent(m_nAsyncDropEvent);
1285             m_nAsyncDropEvent = Application::PostUserEvent(LINK(this, SbaGridControl, AsynchDropEvent), nullptr, true);
1286             return DND_ACTION_COPY;
1287         }
1288     }
1289 
1290     return DND_ACTION_NONE;
1291 }
1292 
getDataSource() const1293 Reference< XPropertySet >  SbaGridControl::getDataSource() const
1294 {
1295     Reference< XPropertySet >  xReturn;
1296 
1297     Reference< XChild >  xColumns(GetPeer()->getColumns(), UNO_QUERY);
1298     if (xColumns.is())
1299         xReturn.set(xColumns->getParent(), UNO_QUERY);
1300 
1301     return xReturn;
1302 }
1303 
IMPL_LINK_NOARG(SbaGridControl,AsynchDropEvent,void *,void)1304 IMPL_LINK_NOARG(SbaGridControl, AsynchDropEvent, void*, void)
1305 {
1306     m_nAsyncDropEvent = nullptr;
1307 
1308     Reference< XPropertySet >  xDataSource = getDataSource();
1309     if ( xDataSource.is() )
1310     {
1311         bool bCountFinal = false;
1312         xDataSource->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bCountFinal;
1313         if ( !bCountFinal )
1314             setDataSource(nullptr); // detach from grid control
1315         Reference< XResultSetUpdate > xResultSetUpdate(xDataSource,UNO_QUERY);
1316         rtl::Reference<ODatabaseImportExport> pImExport = new ORowSetImportExport(GetFrameWeld(),xResultSetUpdate,m_aDataDescriptor, getContext());
1317         Hide();
1318         try
1319         {
1320             pImExport->initialize(m_aDataDescriptor);
1321             if (m_pMasterListener)
1322                 m_pMasterListener->BeforeDrop();
1323             if(!pImExport->Read())
1324             {
1325                 OUString sError = DBA_RES(STR_NO_COLUMNNAME_MATCHING);
1326                 throwGenericSQLException(sError,nullptr);
1327             }
1328             if (m_pMasterListener)
1329                 m_pMasterListener->AfterDrop();
1330             Show();
1331         }
1332         catch(const SQLException& e)
1333         {
1334             if (m_pMasterListener)
1335                 m_pMasterListener->AfterDrop();
1336             Show();
1337             ::dbtools::showError( ::dbtools::SQLExceptionInfo(e), VCLUnoHelper::GetInterface(this), getContext() );
1338         }
1339         catch(const Exception& )
1340         {
1341             DBG_UNHANDLED_EXCEPTION("dbaccess");
1342             if (m_pMasterListener)
1343                 m_pMasterListener->AfterDrop();
1344             Show();
1345         }
1346         if ( !bCountFinal )
1347             setDataSource(Reference< XRowSet >(xDataSource,UNO_QUERY));
1348     }
1349     m_aDataDescriptor.clear();
1350 }
1351 
GetAccessibleObjectDescription(AccessibleBrowseBoxObjType eObjType,sal_Int32 _nPosition) const1352 OUString SbaGridControl::GetAccessibleObjectDescription( AccessibleBrowseBoxObjType eObjType,sal_Int32 _nPosition) const
1353 {
1354     OUString sRet;
1355     if ( AccessibleBrowseBoxObjType::BrowseBox == eObjType )
1356     {
1357         SolarMutexGuard aGuard;
1358         sRet = DBA_RES(STR_DATASOURCE_GRIDCONTROL_DESC);
1359     }
1360     else
1361         sRet = FmGridControl::GetAccessibleObjectDescription( eObjType,_nPosition);
1362     return sRet;
1363 }
1364 
1365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1366