lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / form / tabwin.cxx
blob2055f438a3ff3f00db9646fba79e39daa706b409
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 SfxFloatingWindow::dispose();
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 ( MouseNotifyEvent::KEYINPUT == _rNEvt.GetType() )
262 const vcl::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 >= SfxItemState::DEFAULT)
294 FmFormShell* pShell = PTR_CAST(FmFormShell, static_cast<const 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 // delete ListBox
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 m_aConnection.reset(
338 connectRowset( Reference< XRowSet >( xForm, UNO_QUERY ), ::comphelper::getProcessComponentContext(), true ),
339 SharedConnection::NoTakeOwnership
341 // TODO: When incompatible changes (such as extending the "virtualdbtools" interface by ensureRowSetConnection)
342 // are allowed, again, we should change this: dbtools should consistently use SharedConnection all over
343 // the place, and connectRowset should be replaced with ensureRowSetConnection
345 // get the fields of the object
347 if ( m_aConnection.is() && !m_aObjectName.isEmpty() )
349 Reference< XComponent > xKeepFieldsAlive;
350 Reference< XNameAccess > xColumns = getFieldsByCommandDescriptor( m_aConnection, m_nObjectType, m_aObjectName,xKeepFieldsAlive );
351 if ( xColumns.is() )
352 lcl_addToList(*pListBox,xColumns);
355 // set prefix
356 OUString aPrefix;
357 StringListResource aPrefixes( SVX_RES( RID_RSC_TABWIN_PREFIX ) );
359 switch (m_nObjectType)
361 case CommandType::TABLE:
362 aPrefix = aPrefixes[0];
363 break;
364 case CommandType::QUERY:
365 aPrefix = aPrefixes[1];
366 break;
367 default:
368 aPrefix = aPrefixes[2];
369 break;
372 // listen for changes at ControlSource in PropertySet
373 if (m_pChangeListener)
375 m_pChangeListener->dispose();
376 m_pChangeListener->release();
378 m_pChangeListener = new ::comphelper::OPropertyChangeMultiplexer(this, xSet);
379 m_pChangeListener->acquire();
380 m_pChangeListener->addProperty(FM_PROP_DATASOURCE);
381 m_pChangeListener->addProperty(FM_PROP_COMMAND);
382 m_pChangeListener->addProperty(FM_PROP_COMMANDTYPE);
384 // Titel setzen
385 aTitle = aTitle + " " + aPrefix + " " + OUString(m_aObjectName.getStr());
386 SetText( aTitle );
388 catch( const Exception& )
390 OSL_FAIL( "FmTabWin::UpdateContent: caught an exception!" );
395 void FmFieldWin::Resize()
397 SfxFloatingWindow::Resize();
399 Size aOutputSize( GetOutputSizePixel() );
403 // adapt size of ::com::sun::star::form::ListBox
404 Point aLBPos( LISTBOX_BORDER, LISTBOX_BORDER );
405 Size aLBSize( aOutputSize );
406 aLBSize.Width() -= (2*LISTBOX_BORDER);
407 aLBSize.Height() -= (2*LISTBOX_BORDER);
409 pListBox->SetPosSizePixel( aLBPos, aLBSize );
413 void FmFieldWin::FillInfo( SfxChildWinInfo& rInfo ) const
415 rInfo.bVisible = false;
419 SFX_IMPL_FLOATINGWINDOW(FmFieldWinMgr, SID_FM_ADD_FIELD)
422 FmFieldWinMgr::FmFieldWinMgr(vcl::Window* _pParent, sal_uInt16 _nId,
423 SfxBindings* _pBindings, SfxChildWinInfo* _pInfo)
424 :SfxChildWindow(_pParent, _nId)
426 pWindow = VclPtr<FmFieldWin>::Create(_pBindings, this, _pParent);
427 SetHideNotDelete(true);
428 eChildAlignment = SfxChildAlignment::NOALIGNMENT;
429 static_cast<SfxFloatingWindow*>(pWindow.get())->Initialize( _pInfo );
433 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */