1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef DBACCESS_SBA_GRID_HRC
28 #include "sbagrid.hrc"
29 #endif
30 
31 #ifndef _SVX_SVXIDS_HRC
32 #include <svx/svxids.hrc>
33 #endif
34 
35 #define ITEMID_HORJUSTIFY		SID_ATTR_ALIGN_HOR_JUSTIFY
36 #define ITEMID_VERJUSTIFY		SID_ATTR_ALIGN_VER_JUSTIFY
37 //#define ITEMID_ORIENTATION     SID_ATTR_ALIGN_ORIENTATION
38 #define ITEMID_LINEBREAK		SID_ATTR_ALIGN_LINEBREAK
39 #define ITEMID_MARGIN			SID_ATTR_ALIGN_MARGIN
40 #define ITEMID_NUMBERINFO		SID_ATTR_NUMBERFORMAT_INFO
41 
42 
43 #define _ZFORLIST_DECLARE_TABLE
44 #ifndef _SVX_NUMINF_HXX
45 #include <svx/numinf.hxx>
46 #endif
47 #ifndef _SVX_DBAEXCHANGE_HXX_
48 #include <svx/dbaexchange.hxx>
49 #endif
50 #ifndef _COM_SUN_STAR_UI_DIALOGS_XEXECUTABLEDIALOG_HPP_
51 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
52 #endif
53 
54 #ifndef _SBA_GRID_HXX
55 #include "sbagrid.hxx"
56 #endif
57 #ifndef DBAUI_SBATTRDLG_HXX
58 #include "dlgattr.hxx"
59 #endif
60 #ifndef _DBAUI_DLGSIZE_HXX
61 #include "dlgsize.hxx"
62 #endif
63 #ifndef _COM_SUN_STAR_FORM_XLOADABLE_HPP_
64 #include <com/sun/star/form/XLoadable.hpp>
65 #endif
66 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
67 #include <com/sun/star/sdb/CommandType.hpp>
68 #endif
69 #ifndef _COM_SUN_STAR_SDB_XSQLQUERYCOMPOSERFACTORY_HPP_
70 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
71 #endif
72 #ifndef _COM_SUN_STAR_SDB_XRESULTSETACCESS_HPP_
73 #include <com/sun/star/sdb/XResultSetAccess.hpp>
74 #endif
75 #ifndef _COM_SUN_STAR_FORM_XFORM_HPP_
76 #include <com/sun/star/form/XForm.hpp>
77 #endif
78 #ifndef _COM_SUN_STAR_CONTAINER_XINDEXCONTAINER_HPP_
79 #include <com/sun/star/container/XIndexContainer.hpp>
80 #endif
81 #ifndef _COM_SUN_STAR_UTIL_NUMBERFORMAT_HPP_
82 #include <com/sun/star/util/NumberFormat.hpp>
83 #endif
84 
85 #ifndef _COM_SUN_STAR_VIEW_XSELECTIONSUPPLIER_HPP_
86 #include <com/sun/star/view/XSelectionSupplier.hpp>
87 #endif
88 #ifndef _COM_SUN_STAR_FORM_DATASELECTIONTYPE_HPP_
89 #include <com/sun/star/form/DataSelectionType.hpp>
90 #endif
91 #ifndef _COM_SUN_STAR_AWT_TEXTALIGN_HPP_
92 #include <com/sun/star/awt/TextAlign.hpp>
93 #endif
94 #ifndef _COM_SUN_STAR_AWT_XTEXTCOMPONENT_HPP_
95 #include <com/sun/star/awt/XTextComponent.hpp>
96 #endif
97 #ifndef _COM_SUN_STAR_UTIL_DATE_HPP_
98 #include <com/sun/star/util/Date.hpp>
99 #endif
100 #ifndef _COM_SUN_STAR_UTIL_TIME_HPP_
101 #include <com/sun/star/util/Time.hpp>
102 #endif
103 #ifndef _COM_SUN_STAR_UTIL_DATETIME_HPP_
104 #include <com/sun/star/util/DateTime.hpp>
105 #endif
106 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETUPDATE_HPP_
107 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
108 #endif
109 #ifndef _URLOBJ_HXX
110 #include <tools/urlobj.hxx>
111 #endif
112 #ifndef TOOLS_DIAGNOSE_EX_H
113 #include <tools/diagnose_ex.h>
114 #endif
115 
116 #ifndef _SFXINTITEM_HXX
117 #include <svl/intitem.hxx>
118 #endif
119 
120 #ifndef _SVX_ALGITEM_HXX //autogen
121 #include <svx/algitem.hxx>
122 #endif
123 
124 #ifndef _SV_MULTISEL_HXX //autogen
125 #include <tools/multisel.hxx>
126 #endif
127 
128 #ifndef _SVX_SVXIDS_HRC
129 #include <svx/svxids.hrc>
130 #endif
131 
132 #ifndef _NUMUNO_HXX
133 #include <svl/numuno.hxx>
134 #endif
135 
136 #ifndef _SFXITEMPOOL_HXX //autogen wg. SfxItemInfo
137 #include <svl/itempool.hxx>
138 #endif
139 
140 #ifndef _SFXITEMSET_HXX //autogen wg. SfxItemSet
141 #include <svl/itemset.hxx>
142 #endif
143 
144 #ifndef _SFXRNGITEM_HXX
145 #include <svl/rngitem.hxx>
146 #endif
147 
148 #ifndef _SV_WAITOBJ_HXX
149 #include <vcl/waitobj.hxx>
150 #endif
151 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
152 #include <toolkit/helper/vclunohelper.hxx>
153 #endif
154 
155 #ifndef _ZFORLIST_HXX
156 #include <svl/zforlist.hxx>
157 #endif
158 #ifndef _CPPUHELPER_QUERYINTERFACE_HXX_
159 #include <cppuhelper/queryinterface.hxx>
160 #endif
161 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
162 #include <connectivity/dbtools.hxx>
163 #endif
164 #ifndef _DBHELPER_DBCONVERSION_HXX_
165 #include <connectivity/dbconversion.hxx>
166 #endif
167 #ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
168 #include <cppuhelper/typeprovider.hxx>
169 #endif
170 #ifndef _RTL_UUID_H_
171 #include <rtl/uuid.h>
172 #endif
173 #ifndef _RTL_MEMORY_H_
174 #include <rtl/memory.h>
175 #endif
176 #ifndef _COMPHELPER_EXTRACT_HXX_
177 #include <comphelper/extract.hxx>
178 #endif
179 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
180 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
181 #endif
182 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
183 #include <com/sun/star/sdbc/DataType.hpp>
184 #endif
185 #ifndef _SV_MSGBOX_HXX
186 #include <vcl/msgbox.hxx>
187 #endif
188 #ifndef _SVX_DBEXCH_HRC
189 #include <svx/dbexch.hrc>
190 #endif
191 #ifndef _DBU_BRW_HRC_
192 #include "dbu_brw.hrc"
193 #endif
194 #ifndef DBACCESS_UI_BROWSER_ID_HXX
195 #include "browserids.hxx"
196 #endif
197 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
198 #include "dbustrings.hrc"
199 #endif
200 #ifndef _DBU_REGHELPER_HXX_
201 #include "dbu_reghelper.hxx"
202 #endif
203 #ifndef DBAUI_DBEXCHANGE_HXX
204 #include "dbexchange.hxx"
205 #endif
206 #ifndef DBAUI_TABLEROW_EXCHANGE_HXX
207 #include "TableRowExchange.hxx"
208 #endif
209 #ifndef DBAUI_TABLEROW_HXX
210 #include "TableRow.hxx"
211 #endif
212 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
213 #include "FieldDescriptions.hxx"
214 #endif
215 #ifndef _SVTOOLS_STRINGTRANSFER_HXX_
216 #include <svtools/stringtransfer.hxx>
217 #endif
218 #ifndef _VCL_STDTEXT_HXX
219 #include <vcl/stdtext.hxx>
220 #endif
221 #ifndef DBAUI_TOOLS_HXX
222 #include "UITools.hxx"
223 #endif
224 #ifndef DBAUI_TOKENWRITER_HXX
225 #include "TokenWriter.hxx"
226 #endif
227 
228 using namespace ::com::sun::star::ui::dialogs;
229 using namespace ::com::sun::star::uno;
230 using namespace ::com::sun::star::sdb;
231 using namespace ::com::sun::star::sdbc;
232 using namespace ::com::sun::star::sdbcx;
233 using namespace ::com::sun::star::beans;
234 using namespace ::com::sun::star::container;
235 using namespace ::com::sun::star::datatransfer;
236 using namespace ::com::sun::star::lang;
237 using namespace ::com::sun::star::view;
238 using namespace ::com::sun::star::form;
239 using namespace ::com::sun::star::frame;
240 using namespace ::com::sun::star::util;
241 using namespace ::dbaui;
242 using namespace ::dbtools;
243 using namespace ::svx;
244 using namespace ::svt;
245 
createRegistryInfo_SbaXGridControl()246 extern "C" void SAL_CALL createRegistryInfo_SbaXGridControl()
247 {
248 	static OMultiInstanceAutoRegistration< SbaXGridControl > aAutoRegistration;
249 }
250 //-------------------------------------------------------------------------
getSupportedServiceNames()251 ::comphelper::StringSequence SAL_CALL SbaXGridControl::getSupportedServiceNames() throw()
252 {
253 	return getSupportedServiceNames_Static();
254 }
255 // -------------------------------------------------------------------------
Create(const Reference<XMultiServiceFactory> & _rxFactory)256 Reference< XInterface > SAL_CALL SbaXGridControl::Create(const Reference<XMultiServiceFactory >& _rxFactory)
257 {
258 	return *(new SbaXGridControl(_rxFactory));
259 }
260 
261 //------------------------------------------------------------------
262 
263 //------------------------------------------------------------------
264 //=======================================================================================
265 // SbaXGridControl
266 //=======================================================================================
267 
268 //------------------------------------------------------------------------------
getImplementationName()269 ::rtl::OUString SAL_CALL SbaXGridControl::getImplementationName() throw()
270 {
271 	return getImplementationName_Static();
272 }
273 
274 //------------------------------------------------------------------------------
getImplementationName_Static()275 ::rtl::OUString SbaXGridControl::getImplementationName_Static() throw( RuntimeException )
276 {
277 	return ::rtl::OUString::createFromAscii("com.sun.star.comp.dbu.SbaXGridControl");
278 }
279 
280 //------------------------------------------------------------------------------
getSupportedServiceNames_Static(void)281 Sequence< ::rtl::OUString> SbaXGridControl::getSupportedServiceNames_Static(void) throw( RuntimeException )
282 {
283 	Sequence< ::rtl::OUString> aSupported(3);
284 	aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.form.control.InteractionGridControl");
285 	aSupported[1] = ::rtl::OUString::createFromAscii("com.sun.star.form.control.GridControl");
286 	aSupported[2] = ::rtl::OUString::createFromAscii("com.sun.star.awt.UnoControl");
287 	return aSupported;
288 }
289 DBG_NAME(SbaXGridControl );
290 //---------------------------------------------------------------------------------------
SbaXGridControl(const Reference<XMultiServiceFactory> & _rM)291 SbaXGridControl::SbaXGridControl(const Reference< XMultiServiceFactory >& _rM)
292 	: FmXGridControl(_rM)
293 {
294 	DBG_CTOR(SbaXGridControl ,NULL);
295 }
296 
297 //---------------------------------------------------------------------------------------
~SbaXGridControl()298 SbaXGridControl::~SbaXGridControl()
299 {
300 	DBG_DTOR(SbaXGridControl ,NULL);
301 }
302 
303 //---------------------------------------------------------------------------------------
imp_CreatePeer(Window * pParent)304 FmXGridPeer* SbaXGridControl::imp_CreatePeer(Window* pParent)
305 {
306 	FmXGridPeer* pReturn = new SbaXGridPeer(m_xServiceFactory);
307 
308 	// translate properties into WinBits
309 	WinBits nStyle = WB_TABSTOP;
310 	Reference< XPropertySet >  xModelSet(getModel(), UNO_QUERY);
311 	if (xModelSet.is())
312 	{
313 		try
314 		{
315 			if (::comphelper::getINT16(xModelSet->getPropertyValue(PROPERTY_BORDER)))
316 				nStyle |= WB_BORDER;
317 		}
318 		catch(Exception&)
319 		{
320 		}
321 
322 	}
323 
324 	pReturn->Create(pParent, nStyle);
325 	return pReturn;
326 }
327 
328 //------------------------------------------------------------------------------
queryInterface(const Type & _rType)329 Any	SAL_CALL SbaXGridControl::queryInterface(const Type& _rType) throw (RuntimeException)
330 {
331 	Any aRet = FmXGridControl::queryInterface(_rType);
332 	return aRet.hasValue() ? aRet : ::cppu::queryInterface(_rType,(::com::sun::star::frame::XDispatch*)this);
333 }
334 
335 //------------------------------------------------------------------------------
getTypes()336 Sequence< Type > SAL_CALL SbaXGridControl::getTypes(  ) throw (RuntimeException)
337 {
338 	Sequence< Type > aTypes = FmXGridControl::getTypes();
339 
340 	sal_Int32 nTypes = aTypes.getLength();
341 	aTypes.realloc(nTypes + 1);
342 	aTypes[nTypes] = ::getCppuType(static_cast< Reference< ::com::sun::star::frame::XDispatch >* >(NULL));
343 
344 	return aTypes;
345 }
346 
347 //------------------------------------------------------------------------------
getImplementationId()348 Sequence< sal_Int8 > SAL_CALL SbaXGridControl::getImplementationId(  ) throw (RuntimeException)
349 {
350 	static ::cppu::OImplementationId * pId = 0;
351 	if (! pId)
352 	{
353 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
354 		if (! pId)
355 		{
356 			static ::cppu::OImplementationId aId;
357 			pId = &aId;
358 		}
359 	}
360 	return pId->getImplementationId();
361 }
362 
363 //---------------------------------------------------------------------------------------
createPeer(const Reference<::com::sun::star::awt::XToolkit> & rToolkit,const Reference<::com::sun::star::awt::XWindowPeer> & rParentPeer)364 void SAL_CALL SbaXGridControl::createPeer(const Reference< ::com::sun::star::awt::XToolkit > & rToolkit, const Reference< ::com::sun::star::awt::XWindowPeer > & rParentPeer) throw( RuntimeException )
365 {
366 	FmXGridControl::createPeer(rToolkit, rParentPeer);
367 
368 	DBG_ASSERT(/*(0 == m_nPeerCreationLevel) && */!mbCreatingPeer, "FmXGridControl::createPeer : recursion!");
369 		// see the base class' createPeer for a comment on this
370 		// 14.05.2001 - 86836 - frank.schoenheit@germany.sun.com
371 
372 	// TODO: why the hell this whole class does not use any mutex?
373 
374 //	if (0 == m_nPeerCreationLevel)
375 	{
376 		Reference< ::com::sun::star::frame::XDispatch >  xDisp(getPeer(), UNO_QUERY);
377 		for (	StatusMultiplexerArray::iterator aIter = m_aStatusMultiplexer.begin();
378 				aIter != m_aStatusMultiplexer.end();
379 				++aIter)
380 		{
381 			if ((*aIter).second && (*aIter).second->getLength())
382 				xDisp->addStatusListener((*aIter).second, (*aIter).first);
383 		}
384 	}
385 }
386 
387 //---------------------------------------------------------------------------------------
dispatch(const::com::sun::star::util::URL & aURL,const Sequence<PropertyValue> & aArgs)388 void SAL_CALL SbaXGridControl::dispatch(const ::com::sun::star::util::URL& aURL, const Sequence< PropertyValue >& aArgs) throw( RuntimeException )
389 {
390 	Reference< ::com::sun::star::frame::XDispatch >  xDisp(getPeer(), UNO_QUERY);
391 	if (xDisp.is())
392 		xDisp->dispatch(aURL, aArgs);
393 }
394 //---------------------------------------------------------------------------------------
addStatusListener(const Reference<XStatusListener> & _rxListener,const URL & _rURL)395 void SAL_CALL SbaXGridControl::addStatusListener( const Reference< XStatusListener > & _rxListener, const URL& _rURL ) throw( RuntimeException )
396 {
397 	::osl::MutexGuard aGuard( GetMutex() );
398 	if ( _rxListener.is() )
399 	{
400 		SbaXStatusMultiplexer*& pMultiplexer = m_aStatusMultiplexer[ _rURL ];
401 		if ( !pMultiplexer )
402 		{
403 			pMultiplexer = new SbaXStatusMultiplexer( *this, GetMutex() );
404 			pMultiplexer->acquire();
405 		}
406 
407 		pMultiplexer->addInterface( _rxListener );
408 		if ( getPeer().is() )
409 		{
410 			if ( 1 == pMultiplexer->getLength() )
411 			{	// the first external listener for this URL
412 				Reference< XDispatch >  xDisp( getPeer(), UNO_QUERY );
413 				xDisp->addStatusListener( pMultiplexer, _rURL );
414 			}
415 			else
416 			{	// already have other listeners for this URL
417 				_rxListener->statusChanged( pMultiplexer->getLastEvent() );
418 			}
419 		}
420 	}
421 }
422 
423 //---------------------------------------------------------------------------------------
removeStatusListener(const Reference<::com::sun::star::frame::XStatusListener> & _rxListener,const::com::sun::star::util::URL & _rURL)424 void SAL_CALL SbaXGridControl::removeStatusListener(const Reference< ::com::sun::star::frame::XStatusListener > & _rxListener, const ::com::sun::star::util::URL& _rURL) throw( RuntimeException )
425 {
426 	::osl::MutexGuard aGuard( GetMutex() );
427 
428 	SbaXStatusMultiplexer*& pMultiplexer = m_aStatusMultiplexer[_rURL];
429 	if (!pMultiplexer)
430 	{
431 		pMultiplexer = new SbaXStatusMultiplexer(*this,GetMutex());
432 		pMultiplexer->acquire();
433 	}
434 
435 	if (getPeer().is() && pMultiplexer->getLength() == 1)
436 	{
437 		Reference< ::com::sun::star::frame::XDispatch >  xDisp(getPeer(), UNO_QUERY);
438 		xDisp->removeStatusListener(pMultiplexer, _rURL);
439 	}
440 	pMultiplexer->removeInterface( _rxListener );
441 }
442 
443 //---------------------------------------------------------------------------------------
dispose(void)444 void SAL_CALL SbaXGridControl::dispose(void) throw( RuntimeException )
445 {
446 	::vos::OGuard aGuard( Application::GetSolarMutex() );
447 
448 	EventObject aEvt;
449 	aEvt.Source = *this;
450 
451 	for (	StatusMultiplexerArray::iterator aIter = m_aStatusMultiplexer.begin();
452 			aIter != m_aStatusMultiplexer.end();
453 			++aIter)
454 	{
455 		if ((*aIter).second)
456 		{
457 			(*aIter).second->disposeAndClear(aEvt);
458 			(*aIter).second->release();
459 			(*aIter).second = NULL;
460 		}
461 	}
462 	StatusMultiplexerArray().swap(m_aStatusMultiplexer);
463 
464 	FmXGridControl::dispose();
465 }
466 
467 //=======================================================================================
468 // SbaXGridPeer
469 //=======================================================================================
DBG_NAME(SbaXGridPeer)470 DBG_NAME(SbaXGridPeer )
471 //---------------------------------------------------------------------------------------
472 SbaXGridPeer::SbaXGridPeer(const Reference< XMultiServiceFactory >& _rM)
473 : FmXGridPeer(_rM)
474 ,m_aStatusListeners(m_aMutex)
475 {
476 	DBG_CTOR(SbaXGridPeer ,NULL);
477 }
478 
479 //---------------------------------------------------------------------------------------
~SbaXGridPeer()480 SbaXGridPeer::~SbaXGridPeer()
481 {
482 	DBG_DTOR(SbaXGridPeer ,NULL);
483 }
484 
485 //---------------------------------------------------------------------------------------
dispose(void)486 void SAL_CALL SbaXGridPeer::dispose(void) throw( RuntimeException )
487 {
488 	EventObject aEvt(*this);
489 
490 	m_aStatusListeners.disposeAndClear(aEvt);
491 
492 	FmXGridPeer::dispose();
493 }
494 
495 //---------------------------------------------------------------------------------------
NotifyStatusChanged(const::com::sun::star::util::URL & _rUrl,const Reference<::com::sun::star::frame::XStatusListener> & xControl)496 void SbaXGridPeer::NotifyStatusChanged(const ::com::sun::star::util::URL& _rUrl, const Reference< ::com::sun::star::frame::XStatusListener > & xControl)
497 {
498 	SbaGridControl* pGrid = (SbaGridControl*) GetWindow();
499 	if (!pGrid)
500 		return;
501 
502 	::com::sun::star::frame::FeatureStateEvent aEvt;
503 	aEvt.Source = *this;
504 	aEvt.IsEnabled = !pGrid->IsReadOnlyDB();
505 	aEvt.FeatureURL = _rUrl;
506 
507 	ConstMapDispatchToBoolIterator aURLStatePos = m_aDispatchStates.find( classifyDispatchURL( _rUrl ) );
508 	if ( m_aDispatchStates.end() != aURLStatePos )
509 		aEvt.State <<= aURLStatePos->second;
510 	else
511 		aEvt.State <<= sal_False;
512 
513 	if (xControl.is())
514 		xControl->statusChanged(aEvt);
515 	else
516 	{
517 		::cppu::OInterfaceContainerHelper * pIter = m_aStatusListeners.getContainer(_rUrl);
518 
519 		if (pIter)
520 		{
521 			::cppu::OInterfaceIteratorHelper aListIter(*pIter);
522 			while (aListIter.hasMoreElements())
523 				((::com::sun::star::frame::XStatusListener*)aListIter.next())->statusChanged(aEvt);
524 		}
525 	}
526 }
527 
528 //------------------------------------------------------------------------------
queryInterface(const Type & _rType)529 Any	SAL_CALL SbaXGridPeer::queryInterface(const Type& _rType) throw (RuntimeException)
530 {
531 	Any aRet = ::cppu::queryInterface(_rType,(::com::sun::star::frame::XDispatch*)this);
532 	if(aRet.hasValue())
533 		return aRet;
534 	return FmXGridPeer::queryInterface(_rType);
535 }
536 
537 //---------------------------------------------------------------------------------------
queryDispatch(const::com::sun::star::util::URL & aURL,const::rtl::OUString & aTargetFrameName,sal_Int32 nSearchFlags)538 Reference< ::com::sun::star::frame::XDispatch >  SAL_CALL SbaXGridPeer::queryDispatch(const ::com::sun::star::util::URL& aURL, const ::rtl::OUString& aTargetFrameName, sal_Int32 nSearchFlags) throw( RuntimeException )
539 {
540 	if	(	(aURL.Complete.equals(::rtl::OUString::createFromAscii(".uno:GridSlots/BrowserAttribs")))
541 		||	(aURL.Complete.equals(::rtl::OUString::createFromAscii(".uno:GridSlots/RowHeight")))
542 		||	(aURL.Complete.equals(::rtl::OUString::createFromAscii(".uno:GridSlots/ColumnAttribs")))
543 		||	(aURL.Complete.equals(::rtl::OUString::createFromAscii(".uno:GridSlots/ColumnWidth")))
544 		)
545 	{
546 		return (::com::sun::star::frame::XDispatch*)this;
547 	}
548 
549 	return FmXGridPeer::queryDispatch(aURL, aTargetFrameName, nSearchFlags);
550 }
551 
552 //---------------------------------------------------------------------------------------
553 IMPL_LINK( SbaXGridPeer, OnDispatchEvent, void*, /*NOTINTERESTEDIN*/ )
554 {
555 	SbaGridControl* pGrid = static_cast< SbaGridControl* >( GetWindow() );
556 	if ( pGrid )	// if this fails, we were disposing before arriving here
557 	{
558 		if ( Application::GetMainThreadIdentifier() != ::vos::OThread::getCurrentIdentifier() )
559 		{
560 			// still not in the main thread (see SbaXGridPeer::dispatch). post an event, again
561 			// without moving the special even to the back of the queue
562 			pGrid->PostUserEvent( LINK( this, SbaXGridPeer, OnDispatchEvent ) );
563 		}
564 		else
565 		{
566 			DispatchArgs aArgs = m_aDispatchArgs.front();
567 			m_aDispatchArgs.pop();
568 
569 			SbaXGridPeer::dispatch( aArgs.aURL, aArgs.aArgs );
570 		}
571 	}
572 
573 	return 0;
574 }
575 
576 //---------------------------------------------------------------------------------------
classifyDispatchURL(const URL & _rURL)577 SbaXGridPeer::DispatchType SbaXGridPeer::classifyDispatchURL( const URL& _rURL )
578 {
579 	DispatchType eURLType = dtUnknown;
580 	if ( _rURL.Complete.equalsAscii( ".uno:GridSlots/BrowserAttribs" ) )
581 		eURLType = dtBrowserAttribs;
582 	else if ( _rURL.Complete.equalsAscii( ".uno:GridSlots/RowHeight" ) )
583 		eURLType = dtRowHeight;
584 	else if ( _rURL.Complete.equalsAscii( ".uno:GridSlots/ColumnAttribs" ) )
585 		eURLType = dtColumnAttribs;
586 	else if ( _rURL.Complete.equalsAscii( ".uno:GridSlots/ColumnWidth" ) )
587 		eURLType = dtColumnWidth;
588 	return eURLType;
589 }
590 
591 //---------------------------------------------------------------------------------------
dispatch(const URL & aURL,const Sequence<PropertyValue> & aArgs)592 void SAL_CALL SbaXGridPeer::dispatch(const URL& aURL, const Sequence< PropertyValue >& aArgs) throw( RuntimeException )
593 {
594 	SbaGridControl* pGrid = (SbaGridControl*)GetWindow();
595 	if (!pGrid)
596 		return;
597 
598 	if ( Application::GetMainThreadIdentifier() != ::vos::OThread::getCurrentIdentifier() )
599 	{
600 		// we're not in the main thread. This is bad, as we want to raise windows here,
601 		// and VCL does not like windows to be opened in non-main threads (at least on Win32).
602 		// Okay, do this async. No problem with this, as XDispatch::dispatch is defined to be
603 		// a one-way method.
604 
605 		// save the args
606 		DispatchArgs aDispatchArgs;
607 		aDispatchArgs.aURL = aURL;
608 		aDispatchArgs.aArgs = aArgs;
609 		m_aDispatchArgs.push( aDispatchArgs );
610 
611 		// post an event
612 		// we use the Window::PostUserEvent here, instead of the application::PostUserEvent
613 		// this saves us from keeping track of these events - as soon as the window dies,
614 		// the events are deleted automatically. For the application way, we would need to
615 		// do this ourself.
616 		// As we use our grid as window, and the grid dies before we dy, this should be no problem.
617 		pGrid->PostUserEvent( LINK( this, SbaXGridPeer, OnDispatchEvent ) );
618 		return;
619 	}
620 
621 	::vos::OGuard aGuard(Application::GetSolarMutex());
622 	sal_Int16 nColId = -1;
623 	const PropertyValue* pArgs = aArgs.getConstArray();
624 	for (sal_uInt16 i=0; i<aArgs.getLength(); ++i, ++pArgs)
625 	{
626 		if (pArgs->Name == ::rtl::OUString::createFromAscii("ColumnViewPos"))
627 		{
628 			nColId = pGrid->GetColumnIdFromViewPos(::comphelper::getINT16(pArgs->Value));
629 			break;
630 		}
631 		if (pArgs->Name == ::rtl::OUString::createFromAscii("ColumnModelPos"))
632 		{
633 			nColId = pGrid->GetColumnIdFromModelPos(::comphelper::getINT16(pArgs->Value));
634 			break;
635 		}
636 		if (pArgs->Name == ::rtl::OUString::createFromAscii("ColumnId"))
637 		{
638 			nColId = ::comphelper::getINT16(pArgs->Value);
639 			break;
640 		}
641 	}
642 
643 	DispatchType eURLType = classifyDispatchURL( aURL );
644 
645 	if ( dtUnknown != eURLType )
646 	{
647 		// notify any status listeners that the dialog is now active (well, about to be active)
648 		MapDispatchToBool::iterator aThisURLState = m_aDispatchStates.insert( MapDispatchToBool::value_type( eURLType, sal_True ) ).first;
649 		NotifyStatusChanged( aURL, NULL );
650 
651 		// execute the dialog
652 		switch ( eURLType )
653 		{
654 			case dtBrowserAttribs:
655 				pGrid->SetBrowserAttrs();
656 				break;
657 
658 			case dtRowHeight:
659 				pGrid->SetRowHeight();
660 				break;
661 
662 			case dtColumnAttribs:
663 			{
664 				DBG_ASSERT(nColId != -1, "SbaXGridPeer::dispatch : invalid parameter !");
665 				if (nColId != -1)
666 					break;
667 				pGrid->SetColAttrs(nColId);
668 			}
669 			break;
670 
671 			case dtColumnWidth:
672 			{
673 				DBG_ASSERT(nColId != -1, "SbaXGridPeer::dispatch : invalid parameter !");
674 				if (nColId != -1)
675 					break;
676 				pGrid->SetColWidth(nColId);
677 			}
678 			break;
679 
680             case dtUnknown:
681                 break;
682 		}
683 
684 		// notify any status listeners that the dialog vanished
685 		m_aDispatchStates.erase( aThisURLState );
686 		NotifyStatusChanged( aURL, NULL );
687 	}
688 }
689 
690 //---------------------------------------------------------------------------------------
addStatusListener(const Reference<::com::sun::star::frame::XStatusListener> & xControl,const::com::sun::star::util::URL & aURL)691 void SAL_CALL SbaXGridPeer::addStatusListener(const Reference< ::com::sun::star::frame::XStatusListener > & xControl, const ::com::sun::star::util::URL& aURL) throw( RuntimeException )
692 {
693 	::cppu::OInterfaceContainerHelper* pCont = m_aStatusListeners.getContainer(aURL);
694 	if (!pCont)
695 		m_aStatusListeners.addInterface(aURL,xControl);
696 	else
697 		pCont->addInterface(xControl);
698 	NotifyStatusChanged(aURL, xControl);
699 }
700 
701 //---------------------------------------------------------------------------------------
removeStatusListener(const Reference<::com::sun::star::frame::XStatusListener> & xControl,const::com::sun::star::util::URL & aURL)702 void SAL_CALL SbaXGridPeer::removeStatusListener(const Reference< ::com::sun::star::frame::XStatusListener > & xControl, const ::com::sun::star::util::URL& aURL) throw( RuntimeException )
703 {
704 	::cppu::OInterfaceContainerHelper* pCont = m_aStatusListeners.getContainer(aURL);
705 	if ( pCont )
706 		pCont->removeInterface(xControl);
707 }
708 
709 //---------------------------------------------------------------------------------------
getUnoTunnelId()710 const Sequence< sal_Int8 > & SbaXGridPeer::getUnoTunnelId()
711 {
712 	static Sequence< sal_Int8 > * pSeq = 0;
713 	if( !pSeq )
714 	{
715 		::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
716         if( !pSeq )
717 		{
718 			static Sequence< sal_Int8 > aSeq( 16 );
719 				rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0,sal_True );
720 				pSeq = &aSeq;
721 		}
722 	}
723 	return *pSeq;
724 }
725 
726 //---------------------------------------------------------------------------------------
getTypes()727 Sequence< Type > SAL_CALL SbaXGridPeer::getTypes() throw (RuntimeException)
728 {
729 	Sequence< Type > aTypes = FmXGridPeer::getTypes();
730 	sal_Int32 nOldLen = aTypes.getLength();
731 	aTypes.realloc(nOldLen + 1);
732 	aTypes.getArray()[nOldLen] = ::getCppuType( reinterpret_cast< Reference< ::com::sun::star::frame::XDispatch >* >(NULL) );
733 
734 	return aTypes;
735 }
736 
737 // return implementation specific data
738 //------------------------------------------------------------------
getSomething(const Sequence<sal_Int8> & rId)739 sal_Int64 SAL_CALL SbaXGridPeer::getSomething( const Sequence< sal_Int8 > & rId ) throw(::com::sun::star::uno::RuntimeException)
740 {
741 	if( rId.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),  rId.getConstArray(), 16 ) )
742 		return reinterpret_cast< sal_Int64 >( this );
743 
744 	return FmXGridPeer::getSomething(rId);
745 }
746 
747 //---------------------------------------------------------------------------------------
getImplementation(const Reference<XInterface> & _rxIFace)748 SbaXGridPeer* SbaXGridPeer::getImplementation(const Reference< XInterface >& _rxIFace)
749 {
750 	Reference< XUnoTunnel > xTunnel(
751 		_rxIFace, UNO_QUERY);
752 	if (xTunnel.is())
753 		return reinterpret_cast<SbaXGridPeer*>(xTunnel->getSomething(getUnoTunnelId()));
754 	return NULL;
755 }
756 
757 //---------------------------------------------------------------------------------------
imp_CreateControl(Window * pParent,WinBits nStyle)758 FmGridControl* SbaXGridPeer::imp_CreateControl(Window* pParent, WinBits nStyle)
759 {
760 	return new SbaGridControl(m_xServiceFactory, pParent, this, nStyle);
761 }
762 
763 //==================================================================
764 // SbaGridHeader
765 //==================================================================
766 
767 //---------------------------------------------------------------------------------------
SbaGridHeader(BrowseBox * pParent,WinBits nWinBits)768 SbaGridHeader::SbaGridHeader(BrowseBox* pParent, WinBits nWinBits)
769 	:FmGridHeader(pParent, nWinBits)
770 	,DragSourceHelper(this)
771 {
772 }
773 
774 //---------------------------------------------------------------------------------------
StartDrag(sal_Int8 _nAction,const Point & _rPosPixel)775 void SbaGridHeader::StartDrag( sal_Int8 _nAction, const Point& _rPosPixel )
776 {
777 	::vos::OGuard aGuard(Application::GetSolarMutex());
778 		// in the new DnD API, the solar mutex is not locked when StartDrag get's called
779 
780 	ImplStartColumnDrag( _nAction, _rPosPixel );
781 }
782 
783 //---------------------------------------------------------------------------------------
MouseButtonDown(const MouseEvent & _rMEvt)784 void SbaGridHeader::MouseButtonDown( const MouseEvent& _rMEvt )
785 {
786 	if (_rMEvt.IsLeft())
787 		if (_rMEvt.GetClicks() != 2)
788 		{
789 			// the base class will start a column move here, which we don't want to allow
790 			// (at the moment. If we store relative positions with the columns, we can allow column moves ....)
791 
792 //			sal_uInt16	nPos(0);
793 //			sal_uInt16	nHitTest = ImplHitTest( _rMEvt.GetPosPixel(), mnMouseOff, nPos );
794 //			if (!nHitTest & HEAD_HITTEST_DIVIDER)
795 //				return;
796 		}
797 
798 	FmGridHeader::MouseButtonDown(_rMEvt);
799 }
800 
801 //---------------------------------------------------------------------------------------
ImplStartColumnDrag(sal_Int8 _nAction,const Point & _rMousePos)802 sal_Bool SbaGridHeader::ImplStartColumnDrag(sal_Int8 _nAction, const Point& _rMousePos)
803 {
804 	sal_uInt16 nId = GetItemId(_rMousePos);
805 	sal_Bool bResizingCol = sal_False;
806 	if (HEADERBAR_ITEM_NOTFOUND != nId)
807 	{
808 		Rectangle aColRect = GetItemRect(nId);
809 		aColRect.Left() += nId ? 3 : 0;	// the handle col (nId == 0) does not have a left margin for resizing
810 		aColRect.Right() -= 3;
811 		bResizingCol = !aColRect.IsInside(_rMousePos);
812 	}
813 	if (!bResizingCol)
814 	{
815 		// force the the base class to end it's drag mode
816 		EndTracking(ENDTRACK_CANCEL | ENDTRACK_END);
817 
818 		// because we have 3d-buttons the select handler is called from MouseButtonUp, but StartDrag
819 		// occures earlier (while the mouse button is down)
820 		// so for optical reasons we select the column before really starting the drag operation.
821 		notifyColumnSelect(nId);
822 
823 		static_cast<SbaGridControl*>(GetParent())->StartDrag(_nAction,
824 				Point(
825 					_rMousePos.X() + GetPosPixel().X(),		// we aren't left-justified with our parent, in contrast to the data window
826 					_rMousePos.Y() - GetSizePixel().Height()
827 				)
828 			);
829 		return sal_True;
830 	}
831 
832 	return sal_False;
833 }
834 
835 //---------------------------------------------------------------------------------------
PreExecuteColumnContextMenu(sal_uInt16 nColId,PopupMenu & rMenu)836 void SbaGridHeader::PreExecuteColumnContextMenu(sal_uInt16 nColId, PopupMenu& rMenu)
837 {
838 	FmGridHeader::PreExecuteColumnContextMenu(nColId, rMenu);
839 
840 	// some items are valid only if the db isn't readonly
841 	sal_Bool bDBIsReadOnly = ((SbaGridControl*)GetParent())->IsReadOnlyDB();
842 
843 	if (bDBIsReadOnly)
844 	{
845 		rMenu.EnableItem(SID_FM_HIDECOL, sal_False);
846 		PopupMenu* pShowColsMenu = rMenu.GetPopupMenu(SID_FM_SHOWCOLS);
847 		if (pShowColsMenu)
848 		{
849 			// at most 16 items which mean "show column <name>"
850 			for (sal_uInt16 i=1; i<16; ++i)
851 				pShowColsMenu->EnableItem(i, sal_False);
852 			// "show cols/more..." and "show cols/all"
853 			pShowColsMenu->EnableItem(SID_FM_SHOWCOLS_MORE, sal_False);
854 			pShowColsMenu->EnableItem(SID_FM_SHOWALLCOLS, sal_False);
855 		}
856 	}
857 
858 	// prepend some new items
859 	sal_Bool bColAttrs = (nColId != (sal_uInt16)-1) && (nColId != 0);
860 	if ( bColAttrs && !bDBIsReadOnly)
861 	{
862 		PopupMenu aNewItems(ModuleRes(RID_SBA_GRID_COLCTXMENU));
863 		sal_uInt16 nPos = 0;
864 		sal_uInt16 nModelPos = ((SbaGridControl*)GetParent())->GetModelColumnPos(nColId);
865 		Reference< XPropertySet >  xField = ((SbaGridControl*)GetParent())->getField(nModelPos);
866 
867 		if ( xField.is() )
868 		{
869 			switch( ::comphelper::getINT32(xField->getPropertyValue(PROPERTY_TYPE)) )
870 			{
871 			case DataType::BINARY:
872 			case DataType::VARBINARY:
873 			case DataType::LONGVARBINARY:
874 			case DataType::SQLNULL:
875 			case DataType::OBJECT:
876 			case DataType::BLOB:
877 			case DataType::CLOB:
878 			case DataType::REF:
879 				break;
880 			default:
881 				rMenu.InsertItem(ID_BROWSER_COLATTRSET, aNewItems.GetItemText(ID_BROWSER_COLATTRSET), 0, nPos++);
882 				rMenu.SetHelpId(ID_BROWSER_COLATTRSET, aNewItems.GetHelpId(ID_BROWSER_COLATTRSET));
883 				rMenu.InsertSeparator(nPos++);
884 			}
885 		}
886 
887 		rMenu.InsertItem(ID_BROWSER_COLWIDTH, aNewItems.GetItemText(ID_BROWSER_COLWIDTH), 0, nPos++);
888 		rMenu.SetHelpId(ID_BROWSER_COLWIDTH, aNewItems.GetHelpId(ID_BROWSER_COLWIDTH));
889 		rMenu.InsertSeparator(nPos++);
890 	}
891 }
892 
893 //---------------------------------------------------------------------------------------
PostExecuteColumnContextMenu(sal_uInt16 nColId,const PopupMenu & rMenu,sal_uInt16 nExecutionResult)894 void SbaGridHeader::PostExecuteColumnContextMenu(sal_uInt16 nColId, const PopupMenu& rMenu, sal_uInt16 nExecutionResult)
895 {
896 	switch (nExecutionResult)
897 	{
898 		case ID_BROWSER_COLWIDTH:
899 			((SbaGridControl*)GetParent())->SetColWidth(nColId);
900 			break;
901 
902 		case ID_BROWSER_COLATTRSET:
903 			((SbaGridControl*)GetParent())->SetColAttrs(nColId);
904 			break;
905 		case ID_BROWSER_COLUMNINFO:
906 			{
907 				sal_uInt16 nModelPos = ((SbaGridControl*)GetParent())->GetModelColumnPos(nColId);
908 				Reference< XPropertySet >  xField = ((SbaGridControl*)GetParent())->getField(nModelPos);
909 
910 				if(!xField.is())
911 					break;
912 				::std::vector< ::boost::shared_ptr<OTableRow> > vClipboardList;
913 				// send it to the clipboard
914 				vClipboardList.push_back(::boost::shared_ptr<OTableRow>(new OTableRow(xField)));
915 				OTableRowExchange* pData = new OTableRowExchange(vClipboardList);
916 				Reference< ::com::sun::star::datatransfer::XTransferable> xRef = pData;
917 				pData->CopyToClipboard(GetParent());
918 			}
919 			break;
920 
921 		default: FmGridHeader::PostExecuteColumnContextMenu(nColId, rMenu, nExecutionResult);
922 	}
923 }
924 
925 //==================================================================
926 // SbaGridControl
927 //==================================================================
928 DBG_NAME(SbaGridControl );
929 //---------------------------------------------------------------------------------------
SbaGridControl(Reference<XMultiServiceFactory> _rM,Window * pParent,FmXGridPeer * _pPeer,WinBits nBits)930 SbaGridControl::SbaGridControl(Reference< XMultiServiceFactory > _rM,
931 							   Window* pParent, FmXGridPeer* _pPeer, WinBits nBits)
932 	:FmGridControl(_rM,pParent, _pPeer, nBits)
933 	,m_pMasterListener(NULL)
934 	,m_nAsyncDropEvent(0)
935 	,m_nCurrentActionColId((sal_uInt16)-1)
936 	,m_bActivatingForDrop(sal_False)
937 {
938 	DBG_CTOR(SbaGridControl ,NULL);
939 }
940 
941 //---------------------------------------------------------------------------------------
~SbaGridControl()942 SbaGridControl::~SbaGridControl()
943 {
944 	DBG_DTOR(SbaGridControl ,NULL);
945 	if (m_nAsyncDropEvent)
946 		Application::RemoveUserEvent(m_nAsyncDropEvent);
947 }
948 
949 //---------------------------------------------------------------------------------------
imp_CreateHeaderBar(BrowseBox * pParent)950 BrowserHeader* SbaGridControl::imp_CreateHeaderBar(BrowseBox* pParent)
951 {
952 	return new SbaGridHeader(pParent);
953 }
954 
955 //---------------------------------------------------------------------------------------
GetController(long nRow,sal_uInt16 nCol)956 CellController* SbaGridControl::GetController(long nRow, sal_uInt16 nCol)
957 {
958 	if ( m_bActivatingForDrop )
959 		return NULL;
960 
961 	return FmGridControl::GetController(nRow, nCol);
962 }
963 
964 //---------------------------------------------------------------------------------------
PreExecuteRowContextMenu(sal_uInt16 nRow,PopupMenu & rMenu)965 void SbaGridControl::PreExecuteRowContextMenu(sal_uInt16 nRow, PopupMenu& rMenu)
966 {
967 	FmGridControl::PreExecuteRowContextMenu(nRow, rMenu);
968 
969 	PopupMenu aNewItems(ModuleRes(RID_SBA_GRID_ROWCTXMENU));
970 	sal_uInt16 nPos = 0;
971 
972 	if (!IsReadOnlyDB())
973 	{
974 		rMenu.InsertItem(ID_BROWSER_TABLEATTR, aNewItems.GetItemText(ID_BROWSER_TABLEATTR), 0, nPos++);
975 		rMenu.SetHelpId(ID_BROWSER_TABLEATTR, aNewItems.GetHelpId(ID_BROWSER_TABLEATTR));
976 
977 		rMenu.InsertItem(ID_BROWSER_ROWHEIGHT, aNewItems.GetItemText(ID_BROWSER_ROWHEIGHT), 0, nPos++);
978 		rMenu.SetHelpId(ID_BROWSER_ROWHEIGHT, aNewItems.GetHelpId(ID_BROWSER_ROWHEIGHT));
979         rMenu.InsertSeparator(nPos++);
980 	} // if (!IsReadOnlyDB())
981 
982     if ( GetSelectRowCount() > 0 )
983     {
984 	    rMenu.InsertItem(ID_BROWSER_COPY, aNewItems.GetItemText(SID_COPY), 0, nPos++);
985 	    rMenu.SetHelpId(ID_BROWSER_COPY, aNewItems.GetHelpId(SID_COPY));
986 
987         rMenu.InsertSeparator(nPos++);
988     }
989 }
990 
991 //------------------------------------------------------------------------------
GetDatasourceFormatter()992 SvNumberFormatter* SbaGridControl::GetDatasourceFormatter()
993 {
994 	Reference< ::com::sun::star::util::XNumberFormatsSupplier >  xSupplier = ::dbtools::getNumberFormats(::dbtools::getConnection(Reference< XRowSet > (getDataSource(),UNO_QUERY)), sal_True,getServiceManager());
995 
996 	SvNumberFormatsSupplierObj* pSupplierImpl = SvNumberFormatsSupplierObj::getImplementation( xSupplier );
997 	if ( !pSupplierImpl )
998 		return NULL;
999 
1000 	SvNumberFormatter* pFormatter = pSupplierImpl->GetNumberFormatter();
1001 	return pFormatter;
1002 }
1003 
1004 //------------------------------------------------------------------------------
SetColWidth(sal_uInt16 nColId)1005 void SbaGridControl::SetColWidth(sal_uInt16 nColId)
1006 {
1007 	// get the (UNO) column model
1008 	sal_uInt16 nModelPos = GetModelColumnPos(nColId);
1009 	Reference< XIndexAccess >  xCols(GetPeer()->getColumns(), UNO_QUERY);
1010 	Reference< XPropertySet >  xAffectedCol;
1011 	if (xCols.is() && (nModelPos != (sal_uInt16)-1))
1012 		::cppu::extractInterface(xAffectedCol,xCols->getByIndex(nModelPos));
1013 
1014 	if (xAffectedCol.is())
1015 	{
1016 		Any aWidth = xAffectedCol->getPropertyValue(PROPERTY_WIDTH);
1017 		sal_Int32 nCurWidth = aWidth.hasValue() ? ::comphelper::getINT32(aWidth) : -1;
1018 
1019 		DlgSize	aDlgColWidth(this, nCurWidth, sal_False);
1020 		if (aDlgColWidth.Execute())
1021 		{
1022 			sal_Int32 nValue = aDlgColWidth.GetValue();
1023 			Any aNewWidth;
1024 			if (-1 == nValue)
1025 			{	// set to default
1026 				Reference< XPropertyState >  xPropState(xAffectedCol, UNO_QUERY);
1027 				if (xPropState.is())
1028 				{
1029 					try { aNewWidth = xPropState->getPropertyDefault(PROPERTY_WIDTH); } catch(Exception&) { } ;
1030 				}
1031 			}
1032 			else
1033 				aNewWidth <<= nValue;
1034 			try {  xAffectedCol->setPropertyValue(PROPERTY_WIDTH, aNewWidth); } catch(Exception&) { } ;
1035 		}
1036 	}
1037 }
1038 
1039 //------------------------------------------------------------------------------
SetRowHeight()1040 void SbaGridControl::SetRowHeight()
1041 {
1042 	Reference< XPropertySet >  xCols(GetPeer()->getColumns(), UNO_QUERY);
1043 	if (!xCols.is())
1044 		return;
1045 
1046 	Any aHeight = xCols->getPropertyValue(PROPERTY_ROW_HEIGHT);
1047 	sal_Int32 nCurHeight = aHeight.hasValue() ? ::comphelper::getINT32(aHeight) : -1;
1048 
1049 	DlgSize aDlgRowHeight(this, nCurHeight, sal_True);
1050 	if (aDlgRowHeight.Execute())
1051 	{
1052 		sal_Int32 nValue = aDlgRowHeight.GetValue();
1053 		Any aNewHeight;
1054 		if ((sal_Int16)-1 == nValue)
1055 		{	// set to default
1056 			Reference< XPropertyState >  xPropState(xCols, UNO_QUERY);
1057 			if (xPropState.is())
1058 			{
1059 				try
1060 				{
1061 					aNewHeight = xPropState->getPropertyDefault(PROPERTY_ROW_HEIGHT);
1062 				}
1063 				catch(Exception&)
1064 				{ }
1065 			}
1066 		}
1067 		else
1068 			aNewHeight <<= nValue;
1069 		try
1070 		{
1071 			xCols->setPropertyValue(PROPERTY_ROW_HEIGHT, aNewHeight);
1072 		}
1073 		catch(Exception&)
1074 		{
1075 			OSL_ENSURE(0,"setPropertyValue: PROPERTY_ROW_HEIGHT throws a exception");
1076 		}
1077 	}
1078 }
1079 
1080 //------------------------------------------------------------------------------
SetColAttrs(sal_uInt16 nColId)1081 void SbaGridControl::SetColAttrs(sal_uInt16 nColId)
1082 {
1083 	SvNumberFormatter* pFormatter = GetDatasourceFormatter();
1084 	if (!pFormatter)
1085 		return;
1086 
1087 	sal_uInt16 nModelPos = GetModelColumnPos(nColId);
1088 
1089 	// get the (UNO) column model
1090 	Reference< XIndexAccess >  xCols(GetPeer()->getColumns(), UNO_QUERY);
1091 	Reference< XPropertySet >  xAffectedCol;
1092 	if (xCols.is() && (nModelPos != (sal_uInt16)-1))
1093 		::cppu::extractInterface(xAffectedCol,xCols->getByIndex(nModelPos));
1094 
1095 	// get the field the column is bound to
1096 	Reference< XPropertySet >  xField = getField(nModelPos);
1097 	::dbaui::callColumnFormatDialog(xAffectedCol,xField,pFormatter,this);//(Window::GetSettings().GetLanguage());
1098 }
1099 
1100 
1101 //------------------------------------------------------------------------------
SetBrowserAttrs()1102 void SbaGridControl::SetBrowserAttrs()
1103 {
1104 	Reference< XPropertySet >  xGridModel(GetPeer()->getColumns(), UNO_QUERY);
1105 	if (!xGridModel.is())
1106 		return;
1107 
1108 	try
1109 	{
1110 		PropertyValue aArg;
1111 		aArg.Name = ::rtl::OUString::createFromAscii("IntrospectedObject");
1112 		aArg.Value <<= xGridModel;
1113 		Sequence< Any > aDialogArgs(1);
1114 		aDialogArgs[0] <<= aArg;
1115 
1116 		Reference< XInterface > xDialog = getServiceManager()->createInstanceWithArguments(
1117 			::rtl::OUString::createFromAscii("com.sun.star.form.ControlFontDialog"),
1118 			aDialogArgs
1119 			);
1120 		if (!xDialog.is())
1121 		{
1122 			ShowServiceNotAvailableError(this, String::CreateFromAscii("com.sun.star.form.ControlFontDialog"), sal_True);
1123 			return;
1124 		}
1125 
1126 		Reference< XExecutableDialog > xExecute(xDialog, UNO_QUERY);
1127 		OSL_ENSURE(xExecute.is(), "SbaGridControl::SetBrowserAttrs: missing an interface on the dialog!");
1128 		if (xExecute.is())
1129 			xExecute->execute();
1130 	}
1131     catch( const Exception& )
1132     {
1133         DBG_UNHANDLED_EXCEPTION();
1134     }
1135 }
1136 
1137 //---------------------------------------------------------------------------------------
PostExecuteRowContextMenu(sal_uInt16 nRow,const PopupMenu & rMenu,sal_uInt16 nExecutionResult)1138 void SbaGridControl::PostExecuteRowContextMenu(sal_uInt16 nRow, const PopupMenu& rMenu, sal_uInt16 nExecutionResult)
1139 {
1140 	switch (nExecutionResult)
1141 	{
1142 		case ID_BROWSER_TABLEATTR:
1143 			SetBrowserAttrs();
1144 			break;
1145 		case ID_BROWSER_ROWHEIGHT:
1146 			SetRowHeight();
1147 			break;
1148         case ID_BROWSER_COPY:
1149             CopySelectedRowsToClipboard();
1150             break;
1151 
1152 		default:
1153 			FmGridControl::PostExecuteRowContextMenu(nRow, rMenu, nExecutionResult);
1154 			break;
1155 	}
1156 }
1157 
1158 //---------------------------------------------------------------------------------------
Select()1159 void SbaGridControl::Select()
1160 {
1161 	// irgendeine Selektion hat sich geaendert ....
1162 	FmGridControl::Select();
1163 
1164 	if (m_pMasterListener)
1165 		m_pMasterListener->SelectionChanged();
1166 }
1167 
1168 //---------------------------------------------------------------------------------------
CursorMoved()1169 void SbaGridControl::CursorMoved()
1170 {
1171 	FmGridControl::CursorMoved();
1172 }
1173 
1174 //---------------------------------------------------------------------------------------
ActivateCell(long nRow,sal_uInt16 nCol,sal_Bool bSetCellFocus)1175 void SbaGridControl::ActivateCell(long nRow, sal_uInt16	nCol, sal_Bool bSetCellFocus /*= sal_True*/ )
1176 {
1177 	FmGridControl::ActivateCell(nRow, nCol, bSetCellFocus);
1178 	if (m_pMasterListener)
1179 		m_pMasterListener->CellActivated();
1180 }
1181 
1182 //---------------------------------------------------------------------------------------
DeactivateCell(sal_Bool bUpdate)1183 void SbaGridControl::DeactivateCell(sal_Bool bUpdate /*= sal_True*/)
1184 {
1185 	FmGridControl::DeactivateCell(bUpdate);
1186 	if (m_pMasterListener)
1187 		m_pMasterListener->CellDeactivated();
1188 }
1189 
1190 //---------------------------------------------------------------------------------------
onRowChange()1191 void SbaGridControl::onRowChange()
1192 {
1193 	if ( m_pMasterListener )
1194 		m_pMasterListener->RowChanged();
1195 }
1196 
1197 //---------------------------------------------------------------------------------------
onColumnChange()1198 void SbaGridControl::onColumnChange()
1199 {
1200 	if ( m_pMasterListener )
1201 		m_pMasterListener->ColumnChanged();
1202 }
1203 
1204 //---------------------------------------------------------------------------------------
BeforeDrop()1205 void SbaGridControl::BeforeDrop()
1206 {
1207 	if (m_pMasterListener)
1208 		m_pMasterListener->BeforeDrop();
1209 }
1210 //---------------------------------------------------------------------------------------
AfterDrop()1211 void SbaGridControl::AfterDrop()
1212 {
1213 	if (m_pMasterListener)
1214 		m_pMasterListener->AfterDrop();
1215 }
1216 
1217 
1218 //------------------------------------------------------------------------------
getField(sal_uInt16 nModelPos)1219 Reference< XPropertySet >  SbaGridControl::getField(sal_uInt16 nModelPos)
1220 {
1221 	Reference< XPropertySet >  xEmptyReturn;
1222 	try
1223 	{
1224 		// first get the name of the column
1225 		Reference< XIndexAccess >  xCols(GetPeer()->getColumns(), UNO_QUERY);
1226 		if ( xCols.is() && xCols->getCount() > nModelPos )
1227 		{
1228 			Reference< XPropertySet >  xCol(xCols->getByIndex(nModelPos),UNO_QUERY);
1229 			if ( xCol.is() )
1230 				xEmptyReturn.set(xCol->getPropertyValue(PROPERTY_BOUNDFIELD),UNO_QUERY);
1231 		}
1232 		else
1233 			OSL_ENSURE(0,"SbaGridControl::getField getColumns returns NULL or ModelPos is > than count!");
1234 	}
1235 	catch(Exception&)
1236 	{
1237 		OSL_ENSURE(0,"SbaGridControl::getField Exception occured!");
1238 	}
1239 
1240 	return xEmptyReturn;
1241 }
1242 
1243 //---------------------------------------------------------------------------------------
IsReadOnlyDB() const1244 sal_Bool SbaGridControl::IsReadOnlyDB() const
1245 {
1246 	// assume yes if anything fails
1247 	sal_Bool bDBIsReadOnly = sal_True;
1248 
1249 	// the db is the implemented by the parent of the grid control's model ...
1250 	Reference< XChild >  xColumns(GetPeer()->getColumns(), UNO_QUERY);
1251 	if (xColumns.is())
1252 	{
1253 		Reference< XRowSet >  xDataSource(xColumns->getParent(), UNO_QUERY);
1254 		Reference< XChild >  xConn(::dbtools::getConnection(xDataSource),UNO_QUERY);
1255 		if (xConn.is())
1256 		{
1257 			// ... and the RO-flag simply is implemented by a property
1258 			Reference< XPropertySet >  xDbProps(xConn->getParent(), UNO_QUERY);
1259 			if (xDbProps.is())
1260 			{
1261 				Reference< XPropertySetInfo >  xInfo = xDbProps->getPropertySetInfo();
1262 				if (xInfo->hasPropertyByName(PROPERTY_ISREADONLY))
1263 					bDBIsReadOnly = ::comphelper::getBOOL(xDbProps->getPropertyValue(PROPERTY_ISREADONLY));
1264 			}
1265 		}
1266 	}
1267 	return bDBIsReadOnly;
1268 }
1269 
1270 //---------------------------------------------------------------------------------------
MouseButtonDown(const BrowserMouseEvent & rMEvt)1271 void SbaGridControl::MouseButtonDown( const BrowserMouseEvent& rMEvt)
1272 {
1273 	long nRow = GetRowAtYPosPixel(rMEvt.GetPosPixel().Y());
1274 	sal_uInt16 nColPos = GetColumnAtXPosPixel(rMEvt.GetPosPixel().X());
1275 	sal_uInt16 nViewPos = (nColPos == BROWSER_INVALIDID) ? (sal_uInt16)-1 : nColPos-1;
1276 		// 'the handle column' and 'no valid column' will both result in a view position of -1 !
1277 
1278 	sal_Bool bHitEmptySpace = (nRow > GetRowCount()) || (nViewPos == (sal_uInt16)-1);
1279 
1280 	if (bHitEmptySpace && (rMEvt.GetClicks() == 2) && rMEvt.IsMod1())
1281 		Control::MouseButtonDown(rMEvt);
1282 	else
1283 		FmGridControl::MouseButtonDown(rMEvt);
1284 }
1285 
1286 //---------------------------------------------------------------------------------------
StartDrag(sal_Int8 _nAction,const Point & _rPosPixel)1287 void SbaGridControl::StartDrag( sal_Int8 _nAction, const Point& _rPosPixel )
1288 {
1289 	::vos::OGuard aGuard(Application::GetSolarMutex());
1290 		// in the new DnD API, the solar mutex is not locked when StartDrag get's called
1291 
1292 	sal_Bool bHandled = sal_False;
1293 
1294 	do
1295 	{
1296 		// determine if dragging is allowed
1297 		// (Yes, this is controller (not view) functionality. But collecting and evaluating all the
1298 		// informations necessary via UNO would be quite difficult (if not impossible) so
1299 		// my laziness says 'do it here' ...)
1300 		long nRow = GetRowAtYPosPixel(_rPosPixel.Y());
1301 		sal_uInt16 nColPos = GetColumnAtXPosPixel(_rPosPixel.X());
1302 		sal_uInt16 nViewPos = (nColPos == BROWSER_INVALIDID) ? (sal_uInt16)-1 : nColPos-1;
1303 			// 'the handle column' and 'no valid column' will both result in a view position of -1 !
1304 
1305 		sal_Bool bCurrentRowVirtual = IsCurrentAppending() && IsModified();
1306 		// the current row doesn't really exist : the user's appendign a new one and already has entered some data,
1307 		// so the row contains data which has no counter part within the data source
1308 
1309 		long nCorrectRowCount = GetRowCount();
1310 		if (GetOptions() & OPT_INSERT)
1311 			--nCorrectRowCount;	// there is a empty row for inserting records
1312 		if (bCurrentRowVirtual)
1313 			--nCorrectRowCount;
1314 
1315 		if ((nColPos == BROWSER_INVALIDID) || (nRow >= nCorrectRowCount))
1316 			break;
1317 
1318 		sal_Bool bHitHandle = (nColPos == 0);
1319 
1320 		// check which kind of dragging has to be initiated
1321 		if	(	bHitHandle							//	the handle column
1322 													// AND
1323 			&&	(	GetSelectRowCount()						//	at least one row is selected
1324 														// OR
1325 				||	(	(nRow >= 0)							//	a row below the header
1326 					&&	!bCurrentRowVirtual					//	we aren't appending a new record
1327 					&&	(nRow != GetCurrentPos())			//	a row which is not the current one
1328 					)									// OR
1329 				||	(	(0 == GetSelectRowCount())			// no rows selected
1330 					&&	(-1 == nRow)						// hit the header
1331 					)
1332 				)
1333 			)
1334 		{	// => start dragging the row
1335 			if (GetDataWindow().IsMouseCaptured())
1336 				GetDataWindow().ReleaseMouse();
1337 
1338 			if (0 == GetSelectRowCount())
1339 				// no rows selected, but here in this branch
1340 				// -> the user started dragging the upper left corner, which symbolizes the whole table
1341 				SelectAll();
1342 
1343 			getMouseEvent().Clear();
1344 			DoRowDrag((sal_Int16)nRow);
1345 
1346 			bHandled = sal_True;
1347 		}
1348 		else if	(	(nRow < 0)						// the header
1349 				&&	(!bHitHandle)					// non-handle column
1350 				&&	(nViewPos < GetViewColCount())	// valid (existing) column
1351 				)
1352 		{	// => start dragging the column
1353 			if (GetDataWindow().IsMouseCaptured())
1354 				GetDataWindow().ReleaseMouse();
1355 
1356 			getMouseEvent().Clear();
1357 			DoColumnDrag(nViewPos);
1358 
1359 			bHandled = sal_True;
1360 		}
1361 		else if	(	!bHitHandle		// non-handle column
1362 				&&	(nRow >= 0)		// non-header row
1363 				)
1364 		{	// => start dragging the field content
1365 			if (GetDataWindow().IsMouseCaptured())
1366 				GetDataWindow().ReleaseMouse();
1367 
1368 			getMouseEvent().Clear();
1369 			DoFieldDrag(nViewPos, (sal_Int16)nRow);
1370 
1371 			bHandled = sal_True;
1372 		}
1373 	}
1374 	while (sal_False);
1375 
1376 	if (!bHandled)
1377 		FmGridControl::StartDrag(_nAction, _rPosPixel);
1378 }
1379 
1380 //------------------------------------------------------------------------------
Command(const CommandEvent & rEvt)1381 void SbaGridControl::Command(const CommandEvent& rEvt)
1382 {
1383 	FmGridControl::Command(rEvt);
1384 }
1385 
1386 // -----------------------------------------------------------------------
DoColumnDrag(sal_uInt16 nColumnPos)1387 void SbaGridControl::DoColumnDrag(sal_uInt16 nColumnPos)
1388 {
1389 	Reference< XPropertySet >  xDataSource(getDataSource(), UNO_QUERY);
1390 	DBG_ASSERT(xDataSource.is(), "SbaGridControl::DoColumnDrag : invalid data source !");
1391 
1392 	Reference< XPropertySet > xAffectedCol;
1393 	Reference< XPropertySet > xAffectedField;
1394 	Reference< XConnection > xActiveConnection;
1395 
1396 	// determine the field to drag
1397 	::rtl::OUString sField;
1398 	try
1399 	{
1400 		xActiveConnection = ::dbtools::getConnection(Reference< XRowSet >(getDataSource(),UNO_QUERY));
1401 
1402 		sal_uInt16 nModelPos = GetModelColumnPos(GetColumnIdFromViewPos(nColumnPos));
1403 		Reference< XIndexContainer >  xCols(GetPeer()->getColumns(), UNO_QUERY);
1404 		xAffectedCol.set(xCols->getByIndex(nModelPos),UNO_QUERY);
1405 		if (xAffectedCol.is())
1406 		{
1407 			xAffectedCol->getPropertyValue(PROPERTY_CONTROLSOURCE) >>= sField;
1408 			xAffectedField.set(xAffectedCol->getPropertyValue(PROPERTY_BOUNDFIELD),UNO_QUERY);
1409 		}
1410 	}
1411 	catch(Exception&)
1412 	{
1413 		DBG_ERROR("SbaGridControl::DoColumnDrag : something went wrong while getting the column");
1414 	}
1415 	if (0 == sField.getLength())
1416 		return;
1417 
1418 	OColumnTransferable* pDataTransfer = new OColumnTransferable(xDataSource, sField, xAffectedField, xActiveConnection, CTF_FIELD_DESCRIPTOR | CTF_COLUMN_DESCRIPTOR);
1419 	Reference< XTransferable > xEnsureDelete = pDataTransfer;
1420 	pDataTransfer->StartDrag(this, DND_ACTION_COPY | DND_ACTION_LINK);
1421 }
1422 
1423 // -----------------------------------------------------------------------
CopySelectedRowsToClipboard()1424 void SbaGridControl::CopySelectedRowsToClipboard()
1425 {
1426     DBG_ASSERT( GetSelectRowCount() > 0, "SbaGridControl::CopySelectedRowsToClipboard: invalid call!" );
1427     implTransferSelectedRows( (sal_Int16)FirstSelectedRow(), true );
1428 }
1429 
1430 // -----------------------------------------------------------------------
DoRowDrag(sal_Int16 nRowPos)1431 void SbaGridControl::DoRowDrag( sal_Int16 nRowPos )
1432 {
1433     implTransferSelectedRows( nRowPos, false );
1434 }
1435 
1436 // -----------------------------------------------------------------------
implTransferSelectedRows(sal_Int16 nRowPos,bool _bTrueIfClipboardFalseIfDrag)1437 void SbaGridControl::implTransferSelectedRows( sal_Int16 nRowPos, bool _bTrueIfClipboardFalseIfDrag )
1438 {
1439 	Reference< XPropertySet > xForm( getDataSource(), UNO_QUERY );
1440 	DBG_ASSERT( xForm.is(), "SbaGridControl::implTransferSelectedRows: invalid form!" );
1441 
1442 	// build the sequence of numbers of selected rows
1443 	Sequence< Any > aSelectedRows;
1444     sal_Bool bSelectionBookmarks = sal_True;
1445 
1446 	// collect the affected rows
1447 	if ((GetSelectRowCount() == 0) && (nRowPos >= 0))
1448 	{
1449 		aSelectedRows.realloc( 1 );
1450 		aSelectedRows[0] <<= (sal_Int32)(nRowPos + 1);
1451         bSelectionBookmarks = sal_False;
1452 	}
1453 	else if ( !IsAllSelected() && GetSelectRowCount() )
1454 	{
1455         aSelectedRows = getSelectionBookmarks();
1456         bSelectionBookmarks = sal_True;
1457 	}
1458 
1459 	Reference< XResultSet> xRowSetClone;
1460 	try
1461 	{
1462 		ODataClipboard* pTransfer = new ODataClipboard( xForm, aSelectedRows, bSelectionBookmarks, getServiceManager() );
1463 
1464 		Reference< XTransferable > xEnsureDelete = pTransfer;
1465         if ( _bTrueIfClipboardFalseIfDrag )
1466             pTransfer->CopyToClipboard( this );
1467         else
1468 		    pTransfer->StartDrag(this, DND_ACTION_COPY | DND_ACTION_LINK);
1469 	}
1470 	catch(Exception&)
1471 	{
1472 	}
1473 }
1474 
1475 // -----------------------------------------------------------------------
DoFieldDrag(sal_uInt16 nColumnPos,sal_Int16 nRowPos)1476 void SbaGridControl::DoFieldDrag(sal_uInt16 nColumnPos, sal_Int16 nRowPos)
1477 {
1478 	// the only thing to do here is dragging the pure cell text
1479 	// the old implementation copied a SBA_FIELDDATAEXCHANGE_FORMAT, too, (which was rather expensive to obtain),
1480 	// but we have no client for this DnD format anymore (the mail part of SO 5.2 was the only client)
1481 
1482 	::rtl::OUString sCellText;
1483 	try
1484 	{
1485 		Reference< XGridFieldDataSupplier >  xFieldData(static_cast< XGridPeer* >(GetPeer()), UNO_QUERY);
1486 		Sequence<sal_Bool> aSupportingText = xFieldData->queryFieldDataType(::getCppuType(&sCellText));
1487 		if (aSupportingText.getConstArray()[nColumnPos])
1488 		{
1489 			Sequence< Any> aCellContents = xFieldData->queryFieldData(nRowPos, ::getCppuType(&sCellText));
1490 			sCellText = ::comphelper::getString(aCellContents.getConstArray()[nColumnPos]);
1491 			::svt::OStringTransfer::StartStringDrag(sCellText, this, DND_ACTION_COPY);
1492 		}
1493 	}
1494 	catch(Exception&)
1495 	{
1496 		DBG_ERROR("SbaGridControl::DoFieldDrag : could not retrieve the cell's contents !");
1497 		return;
1498 	}
1499 
1500 }
1501 /// unary_function Functor object for class ZZ returntype is void
1502 	struct SbaGridControlPrec : ::std::unary_function<DataFlavorExVector::value_type,bool>
1503 	{
1504 		sal_Bool	bQueryDrop;
SbaGridControlPrecSbaGridControlPrec1505 		SbaGridControlPrec(sal_Bool _bQueryDrop)
1506 			: bQueryDrop(_bQueryDrop)
1507 		{
1508 		}
1509 
operator ()SbaGridControlPrec1510 		inline bool operator()(const DataFlavorExVector::value_type& _aType)
1511 		{
1512 			switch (_aType.mnSotId)
1513 			{
1514 //				case SOT_FORMAT_RTF:					// RTF data descriptions
1515 //				case SOT_FORMATSTR_ID_HTML:				// HTML data descriptions
1516 				case SOT_FORMATSTR_ID_DBACCESS_TABLE:	// table descriptor
1517 				case SOT_FORMATSTR_ID_DBACCESS_QUERY:	// query descriptor
1518 				case SOT_FORMATSTR_ID_DBACCESS_COMMAND:	// SQL command
1519 					return true;
1520 			}
1521 			return false;
1522 		}
1523 	};
1524 //------------------------------------------------------------------------------
AcceptDrop(const BrowserAcceptDropEvent & rEvt)1525 sal_Int8 SbaGridControl::AcceptDrop( const BrowserAcceptDropEvent& rEvt )
1526 {
1527 	sal_Int8 nAction = DND_ACTION_NONE;
1528 
1529 	// we need a valid connection
1530 	if (!::dbtools::getConnection(Reference< XRowSet > (getDataSource(),UNO_QUERY)).is())
1531 		return nAction;
1532 
1533 	if ( IsDropFormatSupported( FORMAT_STRING ) ) do
1534 	{	// odd construction, but spares us a lot of (explicit ;) goto's
1535 
1536 		if (!GetEmptyRow().Is())
1537 			// without an empty row we're not in update mode
1538 			break;
1539 
1540 		long	nRow = GetRowAtYPosPixel(rEvt.maPosPixel.Y(), sal_False);
1541 		sal_uInt16	nCol = GetColumnAtXPosPixel(rEvt.maPosPixel.X(), sal_False);
1542 
1543 		long nCorrectRowCount = GetRowCount();
1544 		if (GetOptions() & OPT_INSERT)
1545 			--nCorrectRowCount;	// there is a empty row for inserting records
1546 		if (IsCurrentAppending())
1547 			--nCorrectRowCount;	// the current data record doesn't really exist, we are appending a new one
1548 
1549 		if ((nCol == BROWSER_INVALIDID) || (nRow >= nCorrectRowCount) || GetColumnId(nCol) == 0 )
1550 			// no valid cell under the mouse cursor
1551 			break;
1552 
1553 		Rectangle aRect = GetCellRect(nRow, nCol, sal_False);
1554 		if (!aRect.IsInside(rEvt.maPosPixel))
1555 			// not dropped within a cell (a cell isn't as wide as the column - the are small spaces)
1556 			break;
1557 
1558 		if ((IsModified() || (GetCurrentRow().Is() && GetCurrentRow()->IsModified())) && (GetCurrentPos() != nRow))
1559 			// there is a current and modified row or cell and he text is to be dropped into another one
1560 			break;
1561 
1562 		CellControllerRef xCurrentController = Controller();
1563 		if (xCurrentController.Is() && xCurrentController->IsModified() && ((nRow != GetCurRow()) || (nCol != GetCurColumnId())))
1564 			// the current controller is modified and the user wants to drop in another cell -> no chance
1565 			// (when leaving the modified cell a error may occur - this is deadly while dragging)
1566 			break;
1567 
1568 		Reference< XPropertySet >  xField = getField(GetModelColumnPos(nCol));
1569 		if (!xField.is())
1570 			// the column is not valid bound (for instance a binary field)
1571 			break;
1572 
1573 		try
1574 		{
1575 			if (::comphelper::getBOOL(xField->getPropertyValue(PROPERTY_ISREADONLY)))
1576 				break;
1577 		}
1578 		catch (const Exception& e )
1579 		{
1580 			(void)e; // make compiler happy
1581 			// assume RO
1582 			break;
1583 		}
1584 
1585 		try
1586 		{
1587 			// assume that text can be dropped into a field if the column has a ::com::sun::star::awt::XTextComponent interface
1588 			Reference< XIndexAccess >  xColumnControls((::com::sun::star::form::XGridPeer*)GetPeer(), UNO_QUERY);
1589 			if (xColumnControls.is())
1590 			{
1591 				Reference< ::com::sun::star::awt::XTextComponent >  xColControl;
1592 				::cppu::extractInterface(xColControl,xColumnControls->getByIndex(GetViewColumnPos(nCol)));
1593 				if (xColControl.is())
1594 				{
1595 					m_bActivatingForDrop = sal_True;
1596 					GoToRowColumnId(nRow, nCol);
1597 					m_bActivatingForDrop = sal_False;
1598 
1599 					nAction = DND_ACTION_COPY;
1600 				}
1601 			}
1602 		}
1603 		catch( const Exception& )
1604 		{
1605 			DBG_UNHANDLED_EXCEPTION();
1606 		}
1607 
1608 	} while (sal_False);
1609 
1610 	if(nAction != DND_ACTION_COPY && GetEmptyRow().Is())
1611 	{
1612 		const DataFlavorExVector& _rFlavors = GetDataFlavors();
1613 		if(::std::find_if(_rFlavors.begin(),_rFlavors.end(),SbaGridControlPrec(sal_True)) != _rFlavors.end())
1614 			nAction = DND_ACTION_COPY;
1615 	}
1616 
1617 /*
1618 	// check formats
1619 	SvDataObjectRef xDataObj = SvDataObject::PasteDragServer( rEvt );
1620 	if (!xDataObj.Is())
1621 		return sal_False;
1622 
1623 	const SvDataTypeList& rTypeList = xDataObj->GetTypeList();
1624 	if ((rTypeList.Get(Exchange::RegisterFormatName(String::CreateFromAscii(SBA_DATAEXCHANGE_FORMAT)))) )
1625 	{
1626 		bAllow = (GetOptions() & OPT_INSERT) && rEvt.GetColumnId() > 0 && rEvt.GetRow() >= 0;
1627 		((BrowserDropEvent&)rEvt).SetAction(DROP_COPY);
1628 	}
1629 
1630 */
1631 	return (DND_ACTION_NONE != nAction) ? nAction : FmGridControl::AcceptDrop(rEvt);
1632 }
1633 
1634 //------------------------------------------------------------------------------
ExecuteDrop(const BrowserExecuteDropEvent & rEvt)1635 sal_Int8 SbaGridControl::ExecuteDrop( const BrowserExecuteDropEvent& rEvt )
1636 {
1637 	// we need some properties of our data source
1638 	Reference< XPropertySet >  xDataSource = getDataSource();
1639 	if (!xDataSource.is())
1640 		return DND_ACTION_NONE;
1641 
1642 	// we need a valid connection
1643 	if (!::dbtools::getConnection(Reference< XRowSet > (xDataSource,UNO_QUERY)).is())
1644 		return DND_ACTION_NONE;
1645 
1646 	if ( IsDropFormatSupported( FORMAT_STRING ) )
1647 	{
1648 		long	nRow = GetRowAtYPosPixel(rEvt.maPosPixel.Y(), sal_False);
1649 		sal_uInt16	nCol = GetColumnAtXPosPixel(rEvt.maPosPixel.X(), sal_False);
1650 
1651 		long nCorrectRowCount = GetRowCount();
1652 		if (GetOptions() & OPT_INSERT)
1653 			--nCorrectRowCount;	// there is a empty row for inserting records
1654 		if (IsCurrentAppending())
1655 			--nCorrectRowCount;	// the current data record doesn't really exist, we are appending a new one
1656 
1657 		DBG_ASSERT((nCol != BROWSER_INVALIDID) && (nRow < nCorrectRowCount), "SbaGridControl::Drop : dropped on an invalid position !");
1658 			// AcceptDrop should have caught this
1659 
1660 		// from now we work with ids instead of positions
1661 		nCol = GetColumnId(nCol);
1662 
1663 		GoToRowColumnId(nRow, nCol);
1664 		if (!IsEditing())
1665 			ActivateCell();
1666 
1667 		CellControllerRef xCurrentController = Controller();
1668 		if (!xCurrentController.Is() || !xCurrentController->ISA(EditCellController))
1669 			return DND_ACTION_NONE;
1670 		Edit& rEdit = (Edit&)xCurrentController->GetWindow();
1671 
1672 		// get the dropped string
1673 		TransferableDataHelper aDropped( rEvt.maDropEvent.Transferable );
1674 		String sDropped;
1675 		if ( !aDropped.GetString( FORMAT_STRING, sDropped ) )
1676 			return DND_ACTION_NONE;
1677 
1678 		rEdit.SetText( sDropped );
1679 		xCurrentController->SetModified();
1680 		rEdit.Modify();
1681 			// SetText itself doesn't call a Modify as it isn't a user interaction
1682 
1683 		return DND_ACTION_COPY;
1684 	}
1685 
1686 	if(GetEmptyRow().Is())
1687 	{
1688 		const DataFlavorExVector& _rFlavors = GetDataFlavors();
1689 		DataFlavorExVector::const_iterator aFind = ::std::find_if(_rFlavors.begin(),_rFlavors.end(),SbaGridControlPrec(sal_True));
1690 		if( aFind != _rFlavors.end())
1691 		{
1692 			TransferableDataHelper aDropped( rEvt.maDropEvent.Transferable );
1693 			m_aDataDescriptor = ODataAccessObjectTransferable::extractObjectDescriptor(aDropped);
1694 			if (m_nAsyncDropEvent)
1695 				Application::RemoveUserEvent(m_nAsyncDropEvent);
1696 			m_nAsyncDropEvent = Application::PostUserEvent(LINK(this, SbaGridControl, AsynchDropEvent));
1697 			return DND_ACTION_COPY;
1698 		}
1699 	}
1700 
1701 	return DND_ACTION_NONE;
1702 }
1703 
1704 //------------------------------------------------------------------------------
getDataSource() const1705 Reference< XPropertySet >  SbaGridControl::getDataSource() const
1706 {
1707 	Reference< XPropertySet >  xReturn;
1708 
1709 	Reference< XChild >  xColumns(GetPeer()->getColumns(), UNO_QUERY);
1710 	Reference< XPropertySet >  xDataSource;
1711 	if (xColumns.is())
1712 		xReturn = Reference< XPropertySet > (xColumns->getParent(), UNO_QUERY);
1713 
1714 	return xReturn;
1715 }
1716 // -----------------------------------------------------------------------------
1717 IMPL_LINK(SbaGridControl, AsynchDropEvent, void*, /*EMPTY_ARG*/)
1718 {
1719 	m_nAsyncDropEvent = 0;
1720 
1721 	Reference< XPropertySet >  xDataSource = getDataSource();
1722 	if ( xDataSource.is() )
1723 	{
1724 		sal_Bool bCountFinal = sal_False;
1725 		xDataSource->getPropertyValue(PROPERTY_ISROWCOUNTFINAL) >>= bCountFinal;
1726 		if ( !bCountFinal )
1727 			setDataSource(NULL); // deattach from grid control
1728 		Reference< XResultSetUpdate > xResultSetUpdate(xDataSource,UNO_QUERY);
1729 		ODatabaseImportExport* pImExport = new ORowSetImportExport(this,xResultSetUpdate,m_aDataDescriptor,getServiceManager());
1730 		Reference<XEventListener> xHolder = pImExport;
1731 		Hide();
1732 		try
1733 		{
1734             pImExport->initialize(m_aDataDescriptor);
1735 			BeforeDrop();
1736 			if(!pImExport->Read())
1737 			{
1738 				String sError = String(ModuleRes(STR_NO_COLUMNNAME_MATCHING));
1739 				throwGenericSQLException(sError,NULL);
1740 			}
1741 			AfterDrop();
1742 			Show();
1743 		}
1744 		catch(const SQLException& e)
1745 		{
1746 			AfterDrop();
1747 			Show();
1748 			::dbaui::showError(::dbtools::SQLExceptionInfo(e),this,getServiceManager());
1749 		}
1750 		catch(const Exception& )
1751 		{
1752 			AfterDrop();
1753 			Show();
1754             DBG_UNHANDLED_EXCEPTION();
1755 		}
1756 		if ( !bCountFinal )
1757 			setDataSource(Reference< XRowSet >(xDataSource,UNO_QUERY));
1758 	}
1759 	m_aDataDescriptor.clear();
1760 
1761 	return 0L;
1762 }
1763 // -------------------------------------------------------------------------
GetAccessibleObjectDescription(::svt::AccessibleBrowseBoxObjType eObjType,sal_Int32 _nPosition) const1764 ::rtl::OUString SbaGridControl::GetAccessibleObjectDescription( ::svt::AccessibleBrowseBoxObjType eObjType,sal_Int32 _nPosition) const
1765 {
1766 	::rtl::OUString sRet;
1767 	if ( ::svt::BBTYPE_BROWSEBOX == eObjType )
1768 	{
1769 		::vos::OGuard aGuard(Application::GetSolarMutex());
1770 		sRet = String(ModuleRes(STR_DATASOURCE_GRIDCONTROL_DESC));
1771 	}
1772 	else
1773 		sRet = FmGridControl::GetAccessibleObjectDescription( eObjType,_nPosition);
1774 	return sRet;
1775 }
1776 // -----------------------------------------------------------------------------
DeleteSelectedRows()1777 void SbaGridControl::DeleteSelectedRows()
1778 {
1779 	FmGridControl::DeleteSelectedRows();
1780 }
1781 
1782 
1783