Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / form / tabwin.cxx
blobd1edce99b220bb0ec4b624e324054a3f6142e771
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>
36 #include "fmhelp.hrc"
37 #include <svx/fmshell.hxx>
38 #include "fmshimp.hxx"
39 #include "svx/dbtoolsclient.hxx"
40 #include <svx/fmpage.hxx>
42 #include "fmpgeimp.hxx"
44 #include "fmprop.hrc"
46 #include "svx/fmresids.hrc"
47 #include <svx/dialmgr.hxx>
48 #include <tools/shl.hxx>
49 #include <svx/svdpagv.hxx>
50 #include <sfx2/objitem.hxx>
51 #include <sfx2/dispatch.hxx>
52 #include <comphelper/property.hxx>
53 #include <sfx2/frame.hxx>
54 #include <svx/dataaccessdescriptor.hxx>
55 #include "svtools/treelistentry.hxx"
56 #include <vcl/settings.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 ColumnInfo(const OUString& i_sColumnName,const OUString& i_sLabel)
81 : sColumnName(i_sColumnName)
82 , sLabel(i_sLabel)
87 static void lcl_addToList( SvTreeListBox& _rListBox, const uno::Reference< container::XNameAccess>& i_xColumns )
89 uno::Sequence< OUString > aEntries = i_xColumns->getElementNames();
90 const OUString* pEntries = aEntries.getConstArray();
91 sal_Int32 nEntries = aEntries.getLength();
92 for ( sal_Int32 i = 0; i < nEntries; ++i, ++pEntries )
94 uno::Reference< beans::XPropertySet> xColumn(i_xColumns->getByName(*pEntries),UNO_QUERY_THROW);
95 OUString sLabel;
96 if ( xColumn->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
97 xColumn->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
98 if ( !sLabel.isEmpty() )
99 _rListBox.InsertEntry( sLabel, NULL, false, TREELIST_APPEND, new ColumnInfo(*pEntries,sLabel) );
100 else
101 _rListBox.InsertEntry( *pEntries, NULL, false, TREELIST_APPEND, new ColumnInfo(*pEntries,sLabel) );
105 // class FmFieldWinListBox
108 FmFieldWinListBox::FmFieldWinListBox( FmFieldWin* pParent )
109 :SvTreeListBox( pParent, WB_HASBUTTONS|WB_BORDER )
110 ,pTabWin( pParent )
112 SetHelpId( HID_FIELD_SEL );
114 SetHighlightRange( );
118 FmFieldWinListBox::~FmFieldWinListBox()
123 sal_Int8 FmFieldWinListBox::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
125 return DND_ACTION_NONE;
129 sal_Int8 FmFieldWinListBox::ExecuteDrop( const ExecuteDropEvent& /*rEvt*/ )
131 return DND_ACTION_NONE;
135 bool FmFieldWinListBox::DoubleClickHdl()
137 if ( pTabWin->createSelectionControls() )
138 return true;
140 return SvTreeListBox::DoubleClickHdl();
144 void FmFieldWinListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
146 SvTreeListEntry* pSelected = FirstSelected();
147 if (!pSelected)
148 // no drag without a field
149 return;
151 ::svx::ODataAccessDescriptor aDescriptor;
152 aDescriptor[ daDataSource ] <<= pTabWin->GetDatabaseName();
153 aDescriptor[ daConnection ] <<= pTabWin->GetConnection().getTyped();
154 aDescriptor[ daCommand ] <<= pTabWin->GetObjectName();
155 aDescriptor[ daCommandType ]<<= pTabWin->GetObjectType();
156 ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
157 aDescriptor[ daColumnName ] <<= pInfo->sColumnName;
159 TransferableHelper* pTransferColumn = new OColumnTransferable(
160 aDescriptor, CTF_FIELD_DESCRIPTOR | CTF_CONTROL_EXCHANGE | CTF_COLUMN_DESCRIPTOR
162 Reference< XTransferable> xEnsureDelete = pTransferColumn;
163 if (pTransferColumn)
165 EndSelection();
166 pTransferColumn->StartDrag( this, DND_ACTION_COPY );
171 // class FmFieldWinData
174 FmFieldWinData::FmFieldWinData()
179 FmFieldWinData::~FmFieldWinData()
184 // class FmFieldWin
187 FmFieldWin::FmFieldWin(SfxBindings* _pBindings, SfxChildWindow* _pMgr, Window* _pParent)
188 :SfxFloatingWindow(_pBindings, _pMgr, _pParent, WinBits(WB_STDMODELESS|WB_SIZEABLE))
189 ,SfxControllerItem(SID_FM_FIELDS_CONTROL, *_pBindings)
190 ,::comphelper::OPropertyChangeListener(m_aMutex)
191 ,pData(new FmFieldWinData)
192 ,m_nObjectType(0)
193 ,m_pChangeListener(NULL)
195 SetHelpId( HID_FIELD_SEL_WIN );
197 SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
198 pListBox = new FmFieldWinListBox( this );
199 pListBox->Show();
200 UpdateContent(NULL);
201 SetSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
205 FmFieldWin::~FmFieldWin()
207 if (m_pChangeListener)
209 m_pChangeListener->dispose();
210 m_pChangeListener->release();
211 // delete m_pChangeListener;
213 delete pListBox;
214 delete pData;
218 void FmFieldWin::GetFocus()
220 if ( pListBox )
221 pListBox->GrabFocus();
222 else
223 SfxFloatingWindow::GetFocus();
227 bool FmFieldWin::createSelectionControls( )
229 SvTreeListEntry* pSelected = pListBox->FirstSelected();
230 if ( pSelected )
232 // build a descriptor for the currently selected field
233 ODataAccessDescriptor aDescr;
234 aDescr.setDataSource(GetDatabaseName());
236 aDescr[ daConnection ] <<= GetConnection().getTyped();
238 aDescr[ daCommand ] <<= GetObjectName();
239 aDescr[ daCommandType ] <<= GetObjectType();
240 ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
241 aDescr[ daColumnName ] <<= pInfo->sColumnName;//OUString( pListBox->GetEntryText( pSelected) );
243 // transfer this to the SFX world
244 SfxUnoAnyItem aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR, makeAny( aDescr.createPropertyValueSequence() ) );
245 const SfxPoolItem* pArgs[] =
247 &aDescriptorItem, NULL
250 // execute the create slot
251 GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL, pArgs );
254 return NULL != pSelected;
258 bool FmFieldWin::PreNotify( NotifyEvent& _rNEvt )
260 if ( EVENT_KEYINPUT == _rNEvt.GetType() )
262 const KeyCode& rKeyCode = _rNEvt.GetKeyEvent()->GetKeyCode();
263 if ( ( 0 == rKeyCode.GetModifier() ) && ( KEY_RETURN == rKeyCode.GetCode() ) )
265 if ( createSelectionControls() )
266 return true;
270 return SfxFloatingWindow::PreNotify( _rNEvt );
274 bool FmFieldWin::Close()
276 return SfxFloatingWindow::Close();
280 void FmFieldWin::_propertyChanged(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw( ::com::sun::star::uno::RuntimeException )
282 ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm > xForm(evt.Source, ::com::sun::star::uno::UNO_QUERY);
283 UpdateContent(xForm);
287 void FmFieldWin::StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
289 if (!pState || SID_FM_FIELDS_CONTROL != nSID)
290 return;
292 if (eState >= SFX_ITEM_AVAILABLE)
294 FmFormShell* pShell = PTR_CAST(FmFormShell,((SfxObjectItem*)pState)->GetShell());
295 UpdateContent(pShell);
297 else
298 UpdateContent(NULL);
302 void FmFieldWin::UpdateContent(FmFormShell* pShell)
304 pListBox->Clear();
305 OUString aTitle(SVX_RESSTR(RID_STR_FIELDSELECTION));
306 SetText( aTitle );
308 if (!pShell || !pShell->GetImpl())
309 return;
311 Reference< XForm > xForm = pShell->GetImpl()->getCurrentForm();
312 if ( xForm.is() )
313 UpdateContent( xForm );
317 void FmFieldWin::UpdateContent(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm > & xForm)
321 // ListBox loeschen
322 pListBox->Clear();
323 OUString aTitle(SVX_RES(RID_STR_FIELDSELECTION));
324 SetText(aTitle);
326 if (!xForm.is())
327 return;
329 Reference< XPreparedStatement > xStatement;
330 Reference< XPropertySet > xSet(xForm, UNO_QUERY);
332 m_aObjectName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_COMMAND));
333 m_aDatabaseName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_DATASOURCE));
334 m_nObjectType = ::comphelper::getINT32(xSet->getPropertyValue(FM_PROP_COMMANDTYPE));
336 // get the connection of the form
337 OStaticDataAccessTools aTools;
338 m_aConnection.reset(
339 aTools.connectRowset( Reference< XRowSet >( xForm, UNO_QUERY ), ::comphelper::getProcessComponentContext(), true ),
340 SharedConnection::NoTakeOwnership
342 // TODO: When incompatible changes (such as extending the "virtualdbtools" interface by ensureRowSetConnection)
343 // are allowed, again, we should change this: dbtools should consistently use SharedConnection all over
344 // the place, and connectRowset should be replaced with ensureRowSetConnection
346 // get the fields of the object
348 if ( m_aConnection.is() && !m_aObjectName.isEmpty() )
350 Reference< XComponent > xKeepFieldsAlive;
351 Reference< XNameAccess > xColumns = getFieldsByCommandDescriptor( m_aConnection, m_nObjectType, m_aObjectName,xKeepFieldsAlive );
352 if ( xColumns.is() )
353 lcl_addToList(*pListBox,xColumns);
356 // Prefix setzen
357 OUString aPrefix;
358 StringListResource aPrefixes( SVX_RES( RID_RSC_TABWIN_PREFIX ) );
360 switch (m_nObjectType)
362 case CommandType::TABLE:
363 aPrefix = aPrefixes[0];
364 break;
365 case CommandType::QUERY:
366 aPrefix = aPrefixes[1];
367 break;
368 default:
369 aPrefix = aPrefixes[2];
370 break;
373 // an dem PropertySet nach Aenderungen der ControlSource lauschen
374 if (m_pChangeListener)
376 m_pChangeListener->dispose();
377 m_pChangeListener->release();
379 m_pChangeListener = new ::comphelper::OPropertyChangeMultiplexer(this, xSet);
380 m_pChangeListener->acquire();
381 m_pChangeListener->addProperty(FM_PROP_DATASOURCE);
382 m_pChangeListener->addProperty(FM_PROP_COMMAND);
383 m_pChangeListener->addProperty(FM_PROP_COMMANDTYPE);
385 // Titel setzen
386 aTitle = aTitle + " " + aPrefix + " " + OUString(m_aObjectName.getStr());
387 SetText( aTitle );
389 catch( const Exception& )
391 OSL_FAIL( "FmTabWin::UpdateContent: caught an exception!" );
396 void FmFieldWin::Resize()
398 SfxFloatingWindow::Resize();
400 Size aOutputSize( GetOutputSizePixel() );
404 // Groesse der ::com::sun::star::form::ListBox anpassen
405 Point aLBPos( LISTBOX_BORDER, LISTBOX_BORDER );
406 Size aLBSize( aOutputSize );
407 aLBSize.Width() -= (2*LISTBOX_BORDER);
408 aLBSize.Height() -= (2*LISTBOX_BORDER);
410 pListBox->SetPosSizePixel( aLBPos, aLBSize );
414 void FmFieldWin::FillInfo( SfxChildWinInfo& rInfo ) const
416 rInfo.bVisible = false;
420 SFX_IMPL_FLOATINGWINDOW(FmFieldWinMgr, SID_FM_ADD_FIELD)
423 FmFieldWinMgr::FmFieldWinMgr(Window* _pParent, sal_uInt16 _nId,
424 SfxBindings* _pBindings, SfxChildWinInfo* _pInfo)
425 :SfxChildWindow(_pParent, _nId)
427 pWindow = new FmFieldWin(_pBindings, this, _pParent);
428 SetHideNotDelete(true);
429 eChildAlignment = SFX_ALIGN_NOALIGNMENT;
430 ((SfxFloatingWindow*)pWindow)->Initialize( _pInfo );
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */