1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include "svx/fmtools.hxx"
23 #include "fmservs.hxx"
24 #include "stringlistresource.hxx"
26 #include <svx/svxids.hrc>
27 #include <svx/dbaexchange.hxx>
28 #include <com/sun/star/sdb/CommandType.hpp>
29 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
30 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
31 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
32 #include <com/sun/star/awt/XControlContainer.hpp>
33 #include <com/sun/star/util/XLocalizedAliases.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <comphelper/stl_types.hxx>
38 #include <svx/fmshell.hxx>
39 #include "fmshimp.hxx"
40 #include "svx/dbtoolsclient.hxx"
41 #include <svx/fmpage.hxx>
43 #include "fmpgeimp.hxx"
47 #include "svx/fmresids.hrc"
48 #include <svx/dialmgr.hxx>
49 #include <tools/shl.hxx>
50 #include <svx/svdpagv.hxx>
51 #include <sfx2/objitem.hxx>
52 #include <sfx2/dispatch.hxx>
53 #include <comphelper/property.hxx>
54 #include <sfx2/frame.hxx>
55 #include <svx/dataaccessdescriptor.hxx>
56 #include "svtools/treelistentry.hxx"
58 const long STD_WIN_SIZE_X
= 120;
59 const long STD_WIN_SIZE_Y
= 150;
61 const long LISTBOX_BORDER
= 2;
63 using namespace ::com::sun::star::sdbc
;
64 using namespace ::com::sun::star::sdb
;
65 using namespace ::com::sun::star::uno
;
66 using namespace ::com::sun::star::datatransfer
;
67 using namespace ::com::sun::star::beans
;
68 using namespace ::com::sun::star::lang
;
69 using namespace ::com::sun::star::form
;
70 using namespace ::com::sun::star::container
;
71 using namespace ::com::sun::star
;
72 using namespace ::svxform
;
73 using namespace ::svx
;
81 ColumnInfo(const OUString
& i_sColumnName
,const OUString
& i_sLabel
)
82 : sColumnName(i_sColumnName
)
87 ColumnInfo(const OUString
& i_sColumnName
)
88 : sColumnName(i_sColumnName
)
94 static void lcl_addToList( SvTreeListBox
& _rListBox
, const uno::Reference
< container::XNameAccess
>& i_xColumns
)
96 uno::Sequence
< OUString
> aEntries
= i_xColumns
->getElementNames();
97 const OUString
* pEntries
= aEntries
.getConstArray();
98 sal_Int32 nEntries
= aEntries
.getLength();
99 for ( sal_Int32 i
= 0; i
< nEntries
; ++i
, ++pEntries
)
101 uno::Reference
< beans::XPropertySet
> xColumn(i_xColumns
->getByName(*pEntries
),UNO_QUERY_THROW
);
103 if ( xColumn
->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL
) )
104 xColumn
->getPropertyValue(FM_PROP_LABEL
) >>= sLabel
;
105 if ( !sLabel
.isEmpty() )
106 _rListBox
.InsertEntry( sLabel
,NULL
,sal_False
,LIST_APPEND
,new ColumnInfo(*pEntries
,sLabel
) );
108 _rListBox
.InsertEntry( *pEntries
,NULL
,sal_False
,LIST_APPEND
,new ColumnInfo(*pEntries
,sLabel
) );
111 //==================================================================
112 // class FmFieldWinListBox
113 //==================================================================
114 DBG_NAME(FmFieldWinListBox
)
115 //------------------------------------------------------------------------------
116 FmFieldWinListBox::FmFieldWinListBox( FmFieldWin
* pParent
)
117 :SvTreeListBox( pParent
, WB_HASBUTTONS
|WB_BORDER
)
120 DBG_CTOR(FmFieldWinListBox
,NULL
);
121 SetHelpId( HID_FIELD_SEL
);
123 SetHighlightRange( );
126 //------------------------------------------------------------------------------
127 FmFieldWinListBox::~FmFieldWinListBox()
129 DBG_DTOR(FmFieldWinListBox
,NULL
);
132 //------------------------------------------------------------------------------
133 sal_Int8
FmFieldWinListBox::AcceptDrop( const AcceptDropEvent
& /*rEvt*/ )
135 return DND_ACTION_NONE
;
138 //------------------------------------------------------------------------------
139 sal_Int8
FmFieldWinListBox::ExecuteDrop( const ExecuteDropEvent
& /*rEvt*/ )
141 return DND_ACTION_NONE
;
144 //------------------------------------------------------------------------------
145 sal_Bool
FmFieldWinListBox::DoubleClickHdl()
147 if ( pTabWin
->createSelectionControls() )
150 return SvTreeListBox::DoubleClickHdl();
153 //------------------------------------------------------------------------------
154 void FmFieldWinListBox::StartDrag( sal_Int8
/*_nAction*/, const Point
& /*_rPosPixel*/ )
156 SvTreeListEntry
* pSelected
= FirstSelected();
158 // no drag without a field
161 ::svx::ODataAccessDescriptor aDescriptor
;
162 aDescriptor
[ daDataSource
] <<= pTabWin
->GetDatabaseName();
163 aDescriptor
[ daConnection
] <<= pTabWin
->GetConnection().getTyped();
164 aDescriptor
[ daCommand
] <<= pTabWin
->GetObjectName();
165 aDescriptor
[ daCommandType
]<<= pTabWin
->GetObjectType();
166 ColumnInfo
* pInfo
= static_cast<ColumnInfo
*>(pSelected
->GetUserData());
167 aDescriptor
[ daColumnName
] <<= pInfo
->sColumnName
;
169 TransferableHelper
* pTransferColumn
= new OColumnTransferable(
170 aDescriptor
, CTF_FIELD_DESCRIPTOR
| CTF_CONTROL_EXCHANGE
| CTF_COLUMN_DESCRIPTOR
172 Reference
< XTransferable
> xEnsureDelete
= pTransferColumn
;
176 pTransferColumn
->StartDrag( this, DND_ACTION_COPY
);
180 //========================================================================
181 // class FmFieldWinData
182 //========================================================================
183 DBG_NAME(FmFieldWinData
);
184 //-----------------------------------------------------------------------
185 FmFieldWinData::FmFieldWinData()
187 DBG_CTOR(FmFieldWinData
,NULL
);
190 //-----------------------------------------------------------------------
191 FmFieldWinData::~FmFieldWinData()
193 DBG_DTOR(FmFieldWinData
,NULL
);
196 //========================================================================
198 //========================================================================
199 DBG_NAME(FmFieldWin
);
200 //-----------------------------------------------------------------------
201 FmFieldWin::FmFieldWin(SfxBindings
* _pBindings
, SfxChildWindow
* _pMgr
, Window
* _pParent
)
202 :SfxFloatingWindow(_pBindings
, _pMgr
, _pParent
, WinBits(WB_STDMODELESS
|WB_SIZEABLE
))
203 ,SfxControllerItem(SID_FM_FIELDS_CONTROL
, *_pBindings
)
204 ,::comphelper::OPropertyChangeListener(m_aMutex
)
205 ,pData(new FmFieldWinData
)
207 ,m_pChangeListener(NULL
)
209 DBG_CTOR(FmFieldWin
,NULL
);
210 SetHelpId( HID_FIELD_SEL_WIN
);
212 SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
213 pListBox
= new FmFieldWinListBox( this );
216 SetSizePixel(Size(STD_WIN_SIZE_X
,STD_WIN_SIZE_Y
));
219 //-----------------------------------------------------------------------
220 FmFieldWin::~FmFieldWin()
222 if (m_pChangeListener
)
224 m_pChangeListener
->dispose();
225 m_pChangeListener
->release();
226 // delete m_pChangeListener;
230 DBG_DTOR(FmFieldWin
,NULL
);
233 //-----------------------------------------------------------------------
234 void FmFieldWin::GetFocus()
237 pListBox
->GrabFocus();
239 SfxFloatingWindow::GetFocus();
242 //-----------------------------------------------------------------------
243 sal_Bool
FmFieldWin::createSelectionControls( )
245 SvTreeListEntry
* pSelected
= pListBox
->FirstSelected();
248 // build a descriptor for the currently selected field
249 ODataAccessDescriptor aDescr
;
250 aDescr
.setDataSource(GetDatabaseName());
252 aDescr
[ daConnection
] <<= GetConnection().getTyped();
254 aDescr
[ daCommand
] <<= GetObjectName();
255 aDescr
[ daCommandType
] <<= GetObjectType();
256 ColumnInfo
* pInfo
= static_cast<ColumnInfo
*>(pSelected
->GetUserData());
257 aDescr
[ daColumnName
] <<= pInfo
->sColumnName
;//OUString( pListBox->GetEntryText( pSelected) );
259 // transfer this to the SFX world
260 SfxUnoAnyItem
aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR
, makeAny( aDescr
.createPropertyValueSequence() ) );
261 const SfxPoolItem
* pArgs
[] =
263 &aDescriptorItem
, NULL
266 // execute the create slot
267 GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL
, pArgs
);
270 return NULL
!= pSelected
;
273 //-----------------------------------------------------------------------
274 long FmFieldWin::PreNotify( NotifyEvent
& _rNEvt
)
276 if ( EVENT_KEYINPUT
== _rNEvt
.GetType() )
278 const KeyCode
& rKeyCode
= _rNEvt
.GetKeyEvent()->GetKeyCode();
279 if ( ( 0 == rKeyCode
.GetModifier() ) && ( KEY_RETURN
== rKeyCode
.GetCode() ) )
281 if ( createSelectionControls() )
286 return SfxFloatingWindow::PreNotify( _rNEvt
);
289 //-----------------------------------------------------------------------
290 sal_Bool
FmFieldWin::Close()
292 return SfxFloatingWindow::Close();
295 //-----------------------------------------------------------------------
296 void FmFieldWin::_propertyChanged(const ::com::sun::star::beans::PropertyChangeEvent
& evt
) throw( ::com::sun::star::uno::RuntimeException
)
298 ::com::sun::star::uno::Reference
< ::com::sun::star::form::XForm
> xForm(evt
.Source
, ::com::sun::star::uno::UNO_QUERY
);
299 UpdateContent(xForm
);
302 //-----------------------------------------------------------------------
303 void FmFieldWin::StateChanged(sal_uInt16 nSID
, SfxItemState eState
, const SfxPoolItem
* pState
)
305 if (!pState
|| SID_FM_FIELDS_CONTROL
!= nSID
)
308 if (eState
>= SFX_ITEM_AVAILABLE
)
310 FmFormShell
* pShell
= PTR_CAST(FmFormShell
,((SfxObjectItem
*)pState
)->GetShell());
311 UpdateContent(pShell
);
317 //-----------------------------------------------------------------------
318 void FmFieldWin::UpdateContent(FmFormShell
* pShell
)
321 String
aTitle( SVX_RES( RID_STR_FIELDSELECTION
) );
324 if (!pShell
|| !pShell
->GetImpl())
327 Reference
< XForm
> xForm
= pShell
->GetImpl()->getCurrentForm();
329 UpdateContent( xForm
);
332 //-----------------------------------------------------------------------
333 void FmFieldWin::UpdateContent(const ::com::sun::star::uno::Reference
< ::com::sun::star::form::XForm
> & xForm
)
339 OUString
aTitle(SVX_RES(RID_STR_FIELDSELECTION
));
345 Reference
< XPreparedStatement
> xStatement
;
346 Reference
< XPropertySet
> xSet(xForm
, UNO_QUERY
);
348 m_aObjectName
= ::comphelper::getString(xSet
->getPropertyValue(FM_PROP_COMMAND
));
349 m_aDatabaseName
= ::comphelper::getString(xSet
->getPropertyValue(FM_PROP_DATASOURCE
));
350 m_nObjectType
= ::comphelper::getINT32(xSet
->getPropertyValue(FM_PROP_COMMANDTYPE
));
352 // get the connection of the form
353 OStaticDataAccessTools aTools
;
355 aTools
.connectRowset( Reference
< XRowSet
>( xForm
, UNO_QUERY
), ::comphelper::getProcessComponentContext(), sal_True
),
356 SharedConnection::NoTakeOwnership
358 // TODO: When incompatible changes (such as extending the "virtualdbtools" interface by ensureRowSetConnection)
359 // are allowed, again, we should change this: dbtools should consistently use SharedConnection all over
360 // the place, and connectRowset should be replaced with ensureRowSetConnection
362 // get the fields of the object
364 if ( m_aConnection
.is() && !m_aObjectName
.isEmpty() )
366 Reference
< XComponent
> xKeepFieldsAlive
;
367 Reference
< XNameAccess
> xColumns
= getFieldsByCommandDescriptor( m_aConnection
, m_nObjectType
, m_aObjectName
,xKeepFieldsAlive
);
369 lcl_addToList(*pListBox
,xColumns
);
374 StringListResource
aPrefixes( SVX_RES( RID_RSC_TABWIN_PREFIX
) );
376 switch (m_nObjectType
)
378 case CommandType::TABLE
:
379 aPrefix
= aPrefixes
[0];
381 case CommandType::QUERY
:
382 aPrefix
= aPrefixes
[1];
385 aPrefix
= aPrefixes
[2];
389 // an dem PropertySet nach Aenderungen der ControlSource lauschen
390 if (m_pChangeListener
)
392 m_pChangeListener
->dispose();
393 m_pChangeListener
->release();
395 m_pChangeListener
= new ::comphelper::OPropertyChangeMultiplexer(this, xSet
);
396 m_pChangeListener
->acquire();
397 m_pChangeListener
->addProperty(FM_PROP_DATASOURCE
);
398 m_pChangeListener
->addProperty(FM_PROP_COMMAND
);
399 m_pChangeListener
->addProperty(FM_PROP_COMMANDTYPE
);
402 aTitle
= aTitle
+ " " + aPrefix
+ " " + OUString(m_aObjectName
.getStr());
405 catch( const Exception
& )
407 OSL_FAIL( "FmTabWin::UpdateContent: caught an exception!" );
411 //-----------------------------------------------------------------------
412 void FmFieldWin::Resize()
414 SfxFloatingWindow::Resize();
416 Size
aOutputSize( GetOutputSizePixel() );
418 //////////////////////////////////////////////////////////////////////
420 // Groesse der ::com::sun::star::form::ListBox anpassen
421 Point
aLBPos( LISTBOX_BORDER
, LISTBOX_BORDER
);
422 Size
aLBSize( aOutputSize
);
423 aLBSize
.Width() -= (2*LISTBOX_BORDER
);
424 aLBSize
.Height() -= (2*LISTBOX_BORDER
);
426 pListBox
->SetPosSizePixel( aLBPos
, aLBSize
);
429 //-----------------------------------------------------------------------
430 void FmFieldWin::FillInfo( SfxChildWinInfo
& rInfo
) const
432 rInfo
.bVisible
= sal_False
;
435 //-----------------------------------------------------------------------
436 SFX_IMPL_FLOATINGWINDOW(FmFieldWinMgr
, SID_FM_ADD_FIELD
)
438 //-----------------------------------------------------------------------
439 FmFieldWinMgr::FmFieldWinMgr(Window
* _pParent
, sal_uInt16 _nId
,
440 SfxBindings
* _pBindings
, SfxChildWinInfo
* _pInfo
)
441 :SfxChildWindow(_pParent
, _nId
)
443 pWindow
= new FmFieldWin(_pBindings
, this, _pParent
);
444 SetHideNotDelete(sal_True
);
445 eChildAlignment
= SFX_ALIGN_NOALIGNMENT
;
446 ((SfxFloatingWindow
*)pWindow
)->Initialize( _pInfo
);
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */