sync master with lastest vba changes
[ooovba.git] / reportdesign / source / ui / dlg / AddField.cxx
blobd526e304eb75bdcdd8442e17a209c90a821478bf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AddField.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "precompiled_reportdesign.hxx"
31 #include "AddField.hxx"
32 #include "UITools.hxx"
33 #include <svx/dbaexchange.hxx>
34 #include <svx/svdpagv.hxx>
35 #include <com/sun/star/sdb/CommandType.hpp>
36 #include <com/sun/star/util/URL.hpp>
37 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
38 #include <com/sun/star/util/URL.hpp>
39 #include <com/sun/star/i18n/XCollator.hpp>
41 #include <vcl/waitobj.hxx>
42 #include <vcl/svapp.hxx>
43 #include <tools/diagnose_ex.h>
44 #include <comphelper/stl_types.hxx>
45 #include "rptui_slotid.hrc"
47 #include <connectivity/dbtools.hxx>
48 #include "helpids.hrc"
49 #include "RptResId.hrc"
50 #include "CondFormat.hrc"
51 #include "ModuleHelper.hxx"
52 #include "uistrings.hrc"
53 #include <comphelper/property.hxx>
54 #include <svtools/imgdef.hxx>
56 namespace rptui
58 const long STD_WIN_SIZE_X = 180;
59 const long STD_WIN_SIZE_Y = 220;
61 const long LISTBOX_BORDER = 2;
63 using namespace ::com::sun::star;
64 using namespace sdbc;
65 using namespace sdb;
66 using namespace uno;
67 using namespace datatransfer;
68 using namespace beans;
69 using namespace lang;
70 using namespace container;
71 using namespace ::svx;
72 class OAddFieldWindowListBox : public SvTreeListBox
74 OAddFieldWindow* m_pTabWin;
76 OAddFieldWindowListBox(const OAddFieldWindowListBox&);
77 void operator =(const OAddFieldWindowListBox&);
78 protected:
79 // virtual void Command( const CommandEvent& rEvt );
81 public:
82 OAddFieldWindowListBox( OAddFieldWindow* _pParent );
83 virtual ~OAddFieldWindowListBox();
85 sal_Int8 AcceptDrop( const AcceptDropEvent& rEvt );
86 sal_Int8 ExecuteDrop( const ExecuteDropEvent& rEvt );
88 uno::Sequence< beans::PropertyValue > getSelectedFieldDescriptors();
90 protected:
91 // DragSourceHelper
92 virtual void StartDrag( sal_Int8 nAction, const Point& rPosPixel );
94 private:
95 using SvTreeListBox::ExecuteDrop;
97 // -----------------------------------------------------------------------------
98 uno::Sequence< beans::PropertyValue > OAddFieldWindowListBox::getSelectedFieldDescriptors()
100 uno::Sequence< beans::PropertyValue > aArgs(GetSelectionCount());
101 sal_Int32 i = 0;
102 SvLBoxEntry* pSelected = FirstSelected();
103 while( pSelected )
105 // build a descriptor for the currently selected field
106 ::svx::ODataAccessDescriptor aDescriptor;
107 m_pTabWin->fillDescriptor(pSelected,aDescriptor);
108 aArgs[i++].Value <<= aDescriptor.createPropertyValueSequence();
109 pSelected = NextSelected(pSelected);
111 return aArgs;
113 //==================================================================
114 // class OAddFieldWindowListBox
115 //==================================================================
116 DBG_NAME( rpt_OAddFieldWindowListBox );
117 //------------------------------------------------------------------------------
118 OAddFieldWindowListBox::OAddFieldWindowListBox( OAddFieldWindow* _pParent )
119 :SvTreeListBox( _pParent, WB_BORDER|WB_SORT )
120 ,m_pTabWin( _pParent )
122 DBG_CTOR( rpt_OAddFieldWindowListBox,NULL);
123 SetHelpId( HID_RPT_FIELD_SEL );
124 SetSelectionMode(MULTIPLE_SELECTION);
125 SetDragDropMode( 0xFFFF );
126 SetHighlightRange( );
129 //------------------------------------------------------------------------------
130 OAddFieldWindowListBox::~OAddFieldWindowListBox()
132 DBG_DTOR( rpt_OAddFieldWindowListBox,NULL);
135 //------------------------------------------------------------------------------
136 sal_Int8 OAddFieldWindowListBox::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
138 return DND_ACTION_NONE;
141 //------------------------------------------------------------------------------
142 sal_Int8 OAddFieldWindowListBox::ExecuteDrop( const ExecuteDropEvent& /*rEvt*/ )
144 return DND_ACTION_NONE;
147 //------------------------------------------------------------------------------
148 void OAddFieldWindowListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
150 if ( GetSelectionCount() < 1 )
151 // no drag without a field
152 return;
154 OMultiColumnTransferable* pDataContainer = new OMultiColumnTransferable(getSelectedFieldDescriptors());
155 Reference< XTransferable> xEnsureDelete = pDataContainer;
157 EndSelection();
158 pDataContainer->StartDrag( this, DND_ACTION_COPYMOVE | DND_ACTION_LINK );
160 //========================================================================
161 // class OAddFieldWindow
162 //========================================================================
163 DBG_NAME( rpt_OAddFieldWindow );
164 //-----------------------------------------------------------------------
165 OAddFieldWindow::OAddFieldWindow(Window* pParent
166 ,const uno::Reference< beans::XPropertySet >& _xRowSet
168 :FloatingWindow(pParent, WinBits(WB_STDMODELESS|WB_SIZEABLE))
169 ,::comphelper::OPropertyChangeListener(m_aMutex)
170 ,::comphelper::OContainerListener(m_aMutex)
171 ,m_xRowSet(_xRowSet)
172 ,m_aActions(this,ModuleRes(RID_TB_SORTING))
173 ,m_pListBox(new OAddFieldWindowListBox( this ))
174 ,m_aInsertButton(this, WB_TABSTOP|WB_CENTER)
175 ,m_nCommandType(0)
176 ,m_bEscapeProcessing(sal_False)
177 ,m_pChangeListener(NULL)
178 ,m_pContainerListener(NULL)
180 DBG_CTOR( rpt_OAddFieldWindow,NULL);
181 SetHelpId( HID_RPT_FIELD_SEL_WIN );
182 SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
183 SetMinOutputSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
185 m_aActions.SetStyle(m_aActions.GetStyle()|WB_LINESPACING);
186 m_aActions.SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
188 m_aActions.SetSelectHdl(LINK(this, OAddFieldWindow, OnSortAction));
189 setToolBox(&m_aActions);
190 m_aActions.CheckItem(SID_FM_SORTUP);
191 m_aActions.EnableItem(SID_ADD_CONTROL_PAIR, FALSE);
193 m_pListBox->SetDoubleClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
194 m_pListBox->SetSelectHdl(LINK( this, OAddFieldWindow, OnSelectHdl ) );
195 m_pListBox->SetDeselectHdl(LINK( this, OAddFieldWindow, OnSelectHdl ) );
196 m_pListBox->SetDoubleClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
197 m_pListBox->Show();
198 const String sTitle(ModuleRes(RID_STR_INSERT));
199 m_aInsertButton.SetText(sTitle);
200 m_aInsertButton.SetClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
201 m_aInsertButton.Show();
203 SetSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
204 //Show();
206 if ( m_xRowSet.is() )
210 // be notified when the settings of report definition change
211 m_pChangeListener = new ::comphelper::OPropertyChangeMultiplexer( this, m_xRowSet );
212 m_pChangeListener->addProperty( PROPERTY_COMMAND );
213 m_pChangeListener->addProperty( PROPERTY_COMMANDTYPE );
214 m_pChangeListener->addProperty( PROPERTY_ESCAPEPROCESSING );
215 m_pChangeListener->addProperty( PROPERTY_FILTER );
217 catch( const Exception& )
219 DBG_UNHANDLED_EXCEPTION();
224 //-----------------------------------------------------------------------
225 OAddFieldWindow::~OAddFieldWindow()
227 if (m_pChangeListener.is())
228 m_pChangeListener->dispose();
229 if ( m_pContainerListener.is() )
230 m_pContainerListener->dispose();
231 DBG_DTOR( rpt_OAddFieldWindow,NULL);
234 //-----------------------------------------------------------------------
235 void OAddFieldWindow::GetFocus()
237 if ( m_pListBox.get() )
238 m_pListBox->GrabFocus();
239 else
240 FloatingWindow::GetFocus();
242 //-----------------------------------------------------------------------
243 uno::Sequence< beans::PropertyValue > OAddFieldWindow::getSelectedFieldDescriptors()
245 return m_pListBox->getSelectedFieldDescriptors();
248 //-----------------------------------------------------------------------
249 long OAddFieldWindow::PreNotify( NotifyEvent& _rNEvt )
251 if ( EVENT_KEYINPUT == _rNEvt.GetType() )
253 const KeyCode& rKeyCode = _rNEvt.GetKeyEvent()->GetKeyCode();
254 if ( ( 0 == rKeyCode.GetModifier() ) && ( KEY_RETURN == rKeyCode.GetCode() ) )
256 if ( m_aCreateLink.IsSet() )
258 m_aCreateLink.Call(this);
259 return 1;
264 return FloatingWindow::PreNotify( _rNEvt );
266 //-----------------------------------------------------------------------
267 void OAddFieldWindow::_propertyChanged( const beans::PropertyChangeEvent& _evt ) throw( uno::RuntimeException )
269 OSL_ENSURE( _evt.Source == m_xRowSet, "OAddFieldWindow::_propertyChanged: where did this come from?" );
270 (void)_evt;
271 Update();
274 //-----------------------------------------------------------------------
275 namespace
277 void lcl_addToList( OAddFieldWindowListBox& _rListBox, const uno::Sequence< ::rtl::OUString >& _rEntries )
279 const ::rtl::OUString* pEntries = _rEntries.getConstArray();
280 sal_Int32 nEntries = _rEntries.getLength();
281 for ( sal_Int32 i = 0; i < nEntries; ++i, ++pEntries )
282 _rListBox.InsertEntry( *pEntries );
286 //-----------------------------------------------------------------------
287 void OAddFieldWindow::Update()
289 if ( m_pContainerListener.is() )
290 m_pContainerListener->dispose();
291 m_pContainerListener = NULL;
292 m_xColumns.clear();
296 // ListBox loeschen
297 m_pListBox->Clear();
298 const USHORT nItemCount = m_aActions.GetItemCount();
299 for (USHORT j = 0; j< nItemCount; ++j)
301 m_aActions.EnableItem(m_aActions.GetItemId(j),FALSE);
304 String aTitle(ModuleRes(RID_STR_FIELDSELECTION));
305 SetText(aTitle);
306 if ( m_xRowSet.is() )
308 ::rtl::OUString sCommand( m_aCommandName );
309 sal_Int32 nCommandType( m_nCommandType );
310 sal_Bool bEscapeProcessing( m_bEscapeProcessing );
311 ::rtl::OUString sFilter( m_sFilter );
313 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_COMMAND ) >>= sCommand );
314 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType );
315 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_ESCAPEPROCESSING ) >>= bEscapeProcessing );
316 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_FILTER ) >>= sFilter );
318 m_aCommandName = sCommand;
319 m_nCommandType = nCommandType;
320 m_bEscapeProcessing = bEscapeProcessing;
321 m_sFilter = sFilter;
323 // add the columns to the list
324 uno::Reference< sdbc::XConnection> xCon = getConnection();
325 if ( xCon.is() && m_aCommandName.getLength() )
326 m_xColumns = dbtools::getFieldsByCommandDescriptor( xCon, GetCommandType(), GetCommand(), m_xHoldAlive );
327 if ( m_xColumns.is() )
329 lcl_addToList( *m_pListBox, m_xColumns->getElementNames() );
330 uno::Reference< container::XContainer> xContainer(m_xColumns,uno::UNO_QUERY);
331 if ( xContainer.is() )
332 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
335 // add the parameter columns to the list
336 uno::Reference< ::com::sun::star::sdbc::XRowSet > xRowSet(m_xRowSet,uno::UNO_QUERY);
337 Sequence< ::rtl::OUString > aParamNames( getParameterNames( xRowSet ) );
338 lcl_addToList( *m_pListBox, aParamNames );
340 // set title
341 aTitle.AppendAscii(" ");
342 aTitle += m_aCommandName.getStr();
343 SetText( aTitle );
344 if ( m_aCommandName.getLength() )
346 for (USHORT i = 0; i < nItemCount; ++i)
348 m_aActions.EnableItem(m_aActions.GetItemId(i));
351 OnSelectHdl(NULL);
354 catch( const Exception& )
356 DBG_UNHANDLED_EXCEPTION();
360 //-----------------------------------------------------------------------
361 void OAddFieldWindow::Resize()
363 FloatingWindow::Resize();
365 const Size aSize( GetOutputSizePixel() );
367 //////////////////////////////////////////////////////////////////////
368 Size aToolbarSize( m_aActions.GetSizePixel() );
370 const Size aRelated(LogicToPixel( Size( RELATED_CONTROLS, RELATED_CONTROLS ), MAP_APPFONT ));
371 Point aPos( aRelated.Width(), aToolbarSize.Height() + 2*aRelated.Height());
372 Point aToolbarPos( aPos.X(),aRelated.Height());
373 Size aLBSize( aSize );
374 aLBSize.Width() -= (2*aRelated.Width());
375 aLBSize.Height() -= (aRelated.Height() + aPos.Y() );
377 m_aActions.SetPosPixel(Point(aPos.X(),aToolbarPos.Y()));
378 m_pListBox->SetPosSizePixel( aPos, aLBSize );
380 // -----------------------------------------------------------------------------
381 uno::Reference< sdbc::XConnection> OAddFieldWindow::getConnection() const
383 return uno::Reference< sdbc::XConnection>(m_xRowSet->getPropertyValue( PROPERTY_ACTIVECONNECTION ),uno::UNO_QUERY);
385 // -----------------------------------------------------------------------------
386 void OAddFieldWindow::fillDescriptor(SvLBoxEntry* _pSelected,::svx::ODataAccessDescriptor& _rDescriptor)
388 if ( _pSelected && m_xColumns.is() )
390 uno::Reference<container::XChild> xChild(getConnection(),uno::UNO_QUERY);
391 if ( xChild.is( ) )
393 uno::Reference<sdb::XDocumentDataSource> xDocument( xChild->getParent(), uno::UNO_QUERY );
394 if ( xDocument.is() )
396 uno::Reference<frame::XModel> xModel(xDocument->getDatabaseDocument(),uno::UNO_QUERY);
397 if ( xModel.is() )
398 _rDescriptor[ daDatabaseLocation ] <<= xModel->getURL();
399 } // if ( xDocument.is() )
402 _rDescriptor[ ::svx::daCommand ] <<= GetCommand();
403 _rDescriptor[ ::svx::daCommandType ] <<= GetCommandType();
404 _rDescriptor[ ::svx::daEscapeProcessing ] <<= GetEscapeProcessing();
405 _rDescriptor[ ::svx::daConnection ] <<= getConnection();
407 ::rtl::OUString sColumnName = m_pListBox->GetEntryText( _pSelected );
408 _rDescriptor[ ::svx::daColumnName ] <<= sColumnName;
409 if ( m_xColumns->hasByName( sColumnName ) )
410 _rDescriptor[ ::svx::daColumnObject ] <<= m_xColumns->getByName(sColumnName);
413 // -----------------------------------------------------------------------------
414 void OAddFieldWindow::_elementInserted( const container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException)
416 if ( m_pListBox.get() )
418 ::rtl::OUString sName;
419 if ( _rEvent.Accessor >>= sName )
420 m_pListBox->InsertEntry(sName);
423 // -----------------------------------------------------------------------------
424 void OAddFieldWindow::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
426 if ( m_pListBox.get() )
428 m_pListBox->Clear();
429 if ( m_xColumns.is() )
430 lcl_addToList( *m_pListBox, m_xColumns->getElementNames() );
433 // -----------------------------------------------------------------------------
434 void OAddFieldWindow::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
437 // -----------------------------------------------------------------------------
438 IMPL_LINK( OAddFieldWindow, OnSelectHdl, void* ,/*_pAddFieldDlg*/)
440 m_aActions.EnableItem(SID_ADD_CONTROL_PAIR, ( m_pListBox.get() && m_pListBox->GetSelectionCount() > 0 ));
442 return 0L;
444 // -----------------------------------------------------------------------------
445 IMPL_LINK( OAddFieldWindow, OnDoubleClickHdl, void* ,/*_pAddFieldDlg*/)
447 if ( m_aCreateLink.IsSet() )
448 m_aCreateLink.Call(this);
450 return 0L;
452 //------------------------------------------------------------------------------
453 ImageList OAddFieldWindow::getImageList(sal_Int16 _eBitmapSet,sal_Bool _bHiContast) const
455 sal_Int16 nN = IMG_ADDFIELD_DLG_SC;
456 sal_Int16 nH = IMG_ADDFIELD_DLG_SCH;
457 if ( _eBitmapSet == SFX_SYMBOLS_SIZE_LARGE )
459 nN = IMG_ADDFIELD_DLG_LC;
460 nH = IMG_ADDFIELD_DLG_LCH;
462 return ImageList(ModuleRes( _bHiContast ? nH : nN ));
464 //------------------------------------------------------------------
465 void OAddFieldWindow::resizeControls(const Size& _rDiff)
467 // we use large images so we must change them
468 if ( _rDiff.Width() || _rDiff.Height() )
470 Invalidate();
473 //------------------------------------------------------------------
474 IMPL_LINK( OAddFieldWindow, OnSortAction, ToolBox*, /*NOTINTERESTEDIN*/ )
476 const USHORT nCurItem = m_aActions.GetCurItemId();
477 if ( SID_ADD_CONTROL_PAIR == nCurItem )
478 OnDoubleClickHdl(NULL);
479 else
481 if ( SID_FM_REMOVE_FILTER_SORT == nCurItem || !m_aActions.IsItemChecked(nCurItem) )
483 const USHORT nItemCount = m_aActions.GetItemCount();
484 for (USHORT j = 0; j< nItemCount; ++j)
486 const USHORT nItemId = m_aActions.GetItemId(j);
487 if ( nCurItem != nItemId )
488 m_aActions.CheckItem(nItemId,FALSE);
490 SvSortMode eSortMode = SortNone;
491 if ( SID_FM_REMOVE_FILTER_SORT != nCurItem )
493 m_aActions.CheckItem(nCurItem,!m_aActions.IsItemChecked(nCurItem));
494 if ( m_aActions.IsItemChecked(SID_FM_SORTUP) )
495 eSortMode = SortAscending;
496 else if ( m_aActions.IsItemChecked(SID_FM_SORTDOWN) )
497 eSortMode = SortDescending;
498 } // if ( SID_FM_REMOVE_FILTER_SORT != nCurItem )
500 m_pListBox->GetModel()->SetSortMode(eSortMode);
501 if ( SID_FM_REMOVE_FILTER_SORT == nCurItem )
502 Update();
504 m_pListBox->GetModel()->Resort();
507 return 0L;
509 // -----------------------------------------------------------------------------
510 // =============================================================================
511 } // namespace rptui
512 // =============================================================================