bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / form / tabwin.cxx
bloba7af1eca601581c2ef64983ac0e1c242d5fed22c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "tabwin.hxx"
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>
37 #include "fmhelp.hrc"
38 #include <svx/fmshell.hxx>
39 #include "fmshimp.hxx"
40 #include "svx/dbtoolsclient.hxx"
41 #include <svx/fmpage.hxx>
43 #include "fmpgeimp.hxx"
45 #include "fmprop.hrc"
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;
76 struct ColumnInfo
78 OUString sColumnName;
79 OUString sLabel;
80 bool bColumn;
81 ColumnInfo(const OUString& i_sColumnName,const OUString& i_sLabel)
82 : sColumnName(i_sColumnName)
83 , sLabel(i_sLabel)
84 , bColumn(true)
87 ColumnInfo(const OUString& i_sColumnName)
88 : sColumnName(i_sColumnName)
89 , bColumn(false)
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);
102 OUString sLabel;
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) );
107 else
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 )
118 ,pTabWin( pParent )
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() )
148 return sal_True;
150 return SvTreeListBox::DoubleClickHdl();
153 //------------------------------------------------------------------------------
154 void FmFieldWinListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
156 SvTreeListEntry* pSelected = FirstSelected();
157 if (!pSelected)
158 // no drag without a field
159 return;
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;
173 if (pTransferColumn)
175 EndSelection();
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 //========================================================================
197 // class FmFieldWin
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)
206 ,m_nObjectType(0)
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 );
214 pListBox->Show();
215 UpdateContent(NULL);
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;
228 delete pListBox;
229 delete pData;
230 DBG_DTOR(FmFieldWin,NULL);
233 //-----------------------------------------------------------------------
234 void FmFieldWin::GetFocus()
236 if ( pListBox )
237 pListBox->GrabFocus();
238 else
239 SfxFloatingWindow::GetFocus();
242 //-----------------------------------------------------------------------
243 sal_Bool FmFieldWin::createSelectionControls( )
245 SvTreeListEntry* pSelected = pListBox->FirstSelected();
246 if ( pSelected )
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() )
282 return 1;
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)
306 return;
308 if (eState >= SFX_ITEM_AVAILABLE)
310 FmFormShell* pShell = PTR_CAST(FmFormShell,((SfxObjectItem*)pState)->GetShell());
311 UpdateContent(pShell);
313 else
314 UpdateContent(NULL);
317 //-----------------------------------------------------------------------
318 void FmFieldWin::UpdateContent(FmFormShell* pShell)
320 pListBox->Clear();
321 String aTitle( SVX_RES( RID_STR_FIELDSELECTION ) );
322 SetText( aTitle );
324 if (!pShell || !pShell->GetImpl())
325 return;
327 Reference< XForm > xForm = pShell->GetImpl()->getCurrentForm();
328 if ( xForm.is() )
329 UpdateContent( xForm );
332 //-----------------------------------------------------------------------
333 void FmFieldWin::UpdateContent(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm > & xForm)
337 // ListBox loeschen
338 pListBox->Clear();
339 OUString aTitle(SVX_RES(RID_STR_FIELDSELECTION));
340 SetText(aTitle);
342 if (!xForm.is())
343 return;
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;
354 m_aConnection.reset(
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 );
368 if ( xColumns.is() )
369 lcl_addToList(*pListBox,xColumns);
372 // Prefix setzen
373 OUString aPrefix;
374 StringListResource aPrefixes( SVX_RES( RID_RSC_TABWIN_PREFIX ) );
376 switch (m_nObjectType)
378 case CommandType::TABLE:
379 aPrefix = aPrefixes[0];
380 break;
381 case CommandType::QUERY:
382 aPrefix = aPrefixes[1];
383 break;
384 default:
385 aPrefix = aPrefixes[2];
386 break;
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);
401 // Titel setzen
402 aTitle = aTitle + " " + aPrefix + " " + OUString(m_aObjectName.getStr());
403 SetText( aTitle );
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: */