bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / form / tabwin.cxx
blob2a21874116811c41558d4d1a45237a69e616a691
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/fmpage.hxx>
41 #include "fmpgeimp.hxx"
43 #include "fmprop.hrc"
45 #include "svx/fmresids.hrc"
46 #include <svx/dialmgr.hxx>
47 #include <svx/svdpagv.hxx>
48 #include <sfx2/objitem.hxx>
49 #include <sfx2/dispatch.hxx>
50 #include <comphelper/property.hxx>
51 #include <sfx2/frame.hxx>
52 #include <svx/dataaccessdescriptor.hxx>
53 #include "svtools/treelistentry.hxx"
54 #include <vcl/settings.hxx>
56 const long STD_WIN_SIZE_X = 120;
57 const long STD_WIN_SIZE_Y = 150;
59 const long LISTBOX_BORDER = 2;
61 using namespace ::com::sun::star::sdbc;
62 using namespace ::com::sun::star::sdb;
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::datatransfer;
65 using namespace ::com::sun::star::beans;
66 using namespace ::com::sun::star::lang;
67 using namespace ::com::sun::star::form;
68 using namespace ::com::sun::star::container;
69 using namespace ::com::sun::star;
70 using namespace ::svxform;
71 using namespace ::svx;
72 using namespace ::dbtools;
74 namespace {
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)
89 static void lcl_addToList( SvTreeListBox& _rListBox, const uno::Reference< container::XNameAccess>& i_xColumns )
91 uno::Sequence< OUString > aEntries = i_xColumns->getElementNames();
92 const OUString* pEntries = aEntries.getConstArray();
93 sal_Int32 nEntries = aEntries.getLength();
94 for ( sal_Int32 i = 0; i < nEntries; ++i, ++pEntries )
96 uno::Reference< beans::XPropertySet> xColumn(i_xColumns->getByName(*pEntries),UNO_QUERY_THROW);
97 OUString sLabel;
98 if ( xColumn->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
99 xColumn->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
100 if ( !sLabel.isEmpty() )
101 _rListBox.InsertEntry( sLabel, NULL, false, TREELIST_APPEND, new ColumnInfo(*pEntries,sLabel) );
102 else
103 _rListBox.InsertEntry( *pEntries, NULL, false, TREELIST_APPEND, new ColumnInfo(*pEntries,sLabel) );
107 FmFieldWinListBox::FmFieldWinListBox( FmFieldWin* pParent )
108 :SvTreeListBox( pParent, WB_HASBUTTONS|WB_BORDER )
109 ,pTabWin( pParent )
111 SetHelpId( HID_FIELD_SEL );
113 SetHighlightRange( );
116 FmFieldWinListBox::~FmFieldWinListBox()
118 disposeOnce();
121 void FmFieldWinListBox::dispose()
123 pTabWin.clear();
124 SvTreeListBox::dispose();
128 sal_Int8 FmFieldWinListBox::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
130 return DND_ACTION_NONE;
134 sal_Int8 FmFieldWinListBox::ExecuteDrop( const ExecuteDropEvent& /*rEvt*/ )
136 return DND_ACTION_NONE;
140 bool FmFieldWinListBox::DoubleClickHdl()
142 if ( pTabWin->createSelectionControls() )
143 return true;
145 return SvTreeListBox::DoubleClickHdl();
149 void FmFieldWinListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
151 SvTreeListEntry* pSelected = FirstSelected();
152 if (!pSelected)
153 // no drag without a field
154 return;
156 svx::ODataAccessDescriptor aDescriptor;
157 aDescriptor[ daDataSource ] <<= pTabWin->GetDatabaseName();
158 aDescriptor[ daConnection ] <<= pTabWin->GetConnection().getTyped();
159 aDescriptor[ daCommand ] <<= pTabWin->GetObjectName();
160 aDescriptor[ daCommandType ]<<= pTabWin->GetObjectType();
161 ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
162 aDescriptor[ daColumnName ] <<= pInfo->sColumnName;
164 TransferableHelper* pTransferColumn = new OColumnTransferable(
165 aDescriptor, ColumnTransferFormatFlags::FIELD_DESCRIPTOR | ColumnTransferFormatFlags::CONTROL_EXCHANGE | ColumnTransferFormatFlags::COLUMN_DESCRIPTOR
167 Reference< XTransferable> xEnsureDelete = pTransferColumn;
168 EndSelection();
169 pTransferColumn->StartDrag( this, DND_ACTION_COPY );
172 FmFieldWinData::FmFieldWinData()
177 FmFieldWinData::~FmFieldWinData()
181 FmFieldWin::FmFieldWin(SfxBindings* _pBindings, SfxChildWindow* _pMgr, vcl::Window* _pParent)
182 :SfxFloatingWindow(_pBindings, _pMgr, _pParent, WinBits(WB_STDMODELESS|WB_SIZEABLE))
183 ,SfxControllerItem(SID_FM_FIELDS_CONTROL, *_pBindings)
184 ,::comphelper::OPropertyChangeListener(m_aMutex)
185 ,pData(new FmFieldWinData)
186 ,m_nObjectType(0)
187 ,m_pChangeListener(NULL)
189 SetHelpId( HID_FIELD_SEL_WIN );
191 SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
192 pListBox = VclPtr<FmFieldWinListBox>::Create( this );
193 pListBox->Show();
194 UpdateContent(NULL);
195 SetSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
199 FmFieldWin::~FmFieldWin()
201 disposeOnce();
204 void FmFieldWin::dispose()
206 if (m_pChangeListener)
208 m_pChangeListener->dispose();
209 m_pChangeListener->release();
210 // delete m_pChangeListener;
212 pListBox.disposeAndClear();
213 delete pData;
214 ::SfxControllerItem::dispose();
215 SfxFloatingWindow::dispose();
219 void FmFieldWin::GetFocus()
221 if ( pListBox )
222 pListBox->GrabFocus();
223 else
224 SfxFloatingWindow::GetFocus();
228 bool FmFieldWin::createSelectionControls( )
230 SvTreeListEntry* pSelected = pListBox->FirstSelected();
231 if ( pSelected )
233 // build a descriptor for the currently selected field
234 ODataAccessDescriptor aDescr;
235 aDescr.setDataSource(GetDatabaseName());
237 aDescr[ daConnection ] <<= GetConnection().getTyped();
239 aDescr[ daCommand ] <<= GetObjectName();
240 aDescr[ daCommandType ] <<= GetObjectType();
241 ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
242 aDescr[ daColumnName ] <<= pInfo->sColumnName;//OUString( pListBox->GetEntryText( pSelected) );
244 // transfer this to the SFX world
245 SfxUnoAnyItem aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR, makeAny( aDescr.createPropertyValueSequence() ) );
246 const SfxPoolItem* pArgs[] =
248 &aDescriptorItem, NULL
251 // execute the create slot
252 GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL, pArgs );
255 return NULL != pSelected;
259 bool FmFieldWin::PreNotify( NotifyEvent& _rNEvt )
261 if ( MouseNotifyEvent::KEYINPUT == _rNEvt.GetType() )
263 const vcl::KeyCode& rKeyCode = _rNEvt.GetKeyEvent()->GetKeyCode();
264 if ( ( 0 == rKeyCode.GetModifier() ) && ( KEY_RETURN == rKeyCode.GetCode() ) )
266 if ( createSelectionControls() )
267 return true;
271 return SfxFloatingWindow::PreNotify( _rNEvt );
275 bool FmFieldWin::Close()
277 return SfxFloatingWindow::Close();
281 void FmFieldWin::_propertyChanged(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw( ::com::sun::star::uno::RuntimeException )
283 ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm > xForm(evt.Source, ::com::sun::star::uno::UNO_QUERY);
284 UpdateContent(xForm);
288 void FmFieldWin::StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
290 if (!pState || SID_FM_FIELDS_CONTROL != nSID)
291 return;
293 if (eState >= SfxItemState::DEFAULT)
295 FmFormShell* pShell = PTR_CAST(FmFormShell, static_cast<const SfxObjectItem*>(pState)->GetShell());
296 UpdateContent(pShell);
298 else
299 UpdateContent(NULL);
303 void FmFieldWin::UpdateContent(FmFormShell* pShell)
305 pListBox->Clear();
306 OUString aTitle(SVX_RESSTR(RID_STR_FIELDSELECTION));
307 SetText( aTitle );
309 if (!pShell || !pShell->GetImpl())
310 return;
312 Reference< XForm > xForm = pShell->GetImpl()->getCurrentForm();
313 if ( xForm.is() )
314 UpdateContent( xForm );
318 void FmFieldWin::UpdateContent(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm > & xForm)
322 // delete ListBox
323 pListBox->Clear();
324 OUString aTitle(SVX_RES(RID_STR_FIELDSELECTION));
325 SetText(aTitle);
327 if (!xForm.is())
328 return;
330 Reference< XPreparedStatement > xStatement;
331 Reference< XPropertySet > xSet(xForm, UNO_QUERY);
333 m_aObjectName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_COMMAND));
334 m_aDatabaseName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_DATASOURCE));
335 m_nObjectType = ::comphelper::getINT32(xSet->getPropertyValue(FM_PROP_COMMANDTYPE));
337 // get the connection of the form
338 m_aConnection.reset(
339 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 // set prefix
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 // listen for changes at ControlSource in PropertySet
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 // adapt size of ::com::sun::star::form::ListBox
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(vcl::Window* _pParent, sal_uInt16 _nId,
424 SfxBindings* _pBindings, SfxChildWinInfo* _pInfo)
425 :SfxChildWindow(_pParent, _nId)
427 pWindow = VclPtr<FmFieldWin>::Create(_pBindings, this, _pParent);
428 SetHideNotDelete(true);
429 eChildAlignment = SfxChildAlignment::NOALIGNMENT;
430 static_cast<SfxFloatingWindow*>(pWindow.get())->Initialize( _pInfo );
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */