LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/svx/source/form - tabwin.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 2 176 1.1 %
Date: 2013-07-09 Functions: 3 30 10.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : #include "tabwin.hxx"
      22             : #include "svx/fmtools.hxx"
      23             : #include "fmservs.hxx"
      24             : #include "stringlistresource.hxx"
      25             : 
      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>
      36             : 
      37             : #include "fmhelp.hrc"
      38             : #include <svx/fmshell.hxx>
      39             : #include "fmshimp.hxx"
      40             : #include "svx/dbtoolsclient.hxx"
      41             : #include <svx/fmpage.hxx>
      42             : 
      43             : #include "fmpgeimp.hxx"
      44             : 
      45             : #include "fmprop.hrc"
      46             : 
      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"
      57             : 
      58             : const long STD_WIN_SIZE_X = 120;
      59             : const long STD_WIN_SIZE_Y = 150;
      60             : 
      61             : const long LISTBOX_BORDER = 2;
      62             : 
      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;
      74             : 
      75             : 
      76             : struct ColumnInfo
      77             : {
      78             :     OUString sColumnName;
      79             :     OUString sLabel;
      80             :     bool bColumn;
      81           0 :     ColumnInfo(const OUString& i_sColumnName,const OUString& i_sLabel)
      82             :         : sColumnName(i_sColumnName)
      83             :         , sLabel(i_sLabel)
      84           0 :         , bColumn(true)
      85             :     {
      86           0 :     }
      87             :     ColumnInfo(const OUString& i_sColumnName)
      88             :         : sColumnName(i_sColumnName)
      89             :         , bColumn(false)
      90             :     {
      91             :     }
      92             : };
      93             : 
      94           0 : static void lcl_addToList( SvTreeListBox& _rListBox, const uno::Reference< container::XNameAccess>& i_xColumns )
      95             : {
      96           0 :     uno::Sequence< OUString > aEntries = i_xColumns->getElementNames();
      97           0 :     const OUString* pEntries = aEntries.getConstArray();
      98           0 :     sal_Int32 nEntries = aEntries.getLength();
      99           0 :     for ( sal_Int32 i = 0; i < nEntries; ++i, ++pEntries )
     100             :     {
     101           0 :         uno::Reference< beans::XPropertySet> xColumn(i_xColumns->getByName(*pEntries),UNO_QUERY_THROW);
     102           0 :         OUString sLabel;
     103           0 :         if ( xColumn->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
     104           0 :             xColumn->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
     105           0 :         if ( !sLabel.isEmpty() )
     106           0 :             _rListBox.InsertEntry( sLabel,NULL,sal_False,LIST_APPEND,new ColumnInfo(*pEntries,sLabel) );
     107             :         else
     108           0 :             _rListBox.InsertEntry( *pEntries,NULL,sal_False,LIST_APPEND,new ColumnInfo(*pEntries,sLabel) );
     109           0 :     }
     110           0 : }
     111             : //==================================================================
     112             : // class FmFieldWinListBox
     113             : //==================================================================
     114             : DBG_NAME(FmFieldWinListBox)
     115             : //------------------------------------------------------------------------------
     116           0 : FmFieldWinListBox::FmFieldWinListBox( FmFieldWin* pParent )
     117             :     :SvTreeListBox( pParent, WB_HASBUTTONS|WB_BORDER )
     118           0 :     ,pTabWin( pParent )
     119             : {
     120             :     DBG_CTOR(FmFieldWinListBox,NULL);
     121           0 :     SetHelpId( HID_FIELD_SEL );
     122             : 
     123           0 :     SetHighlightRange( );
     124           0 : }
     125             : 
     126             : //------------------------------------------------------------------------------
     127           0 : FmFieldWinListBox::~FmFieldWinListBox()
     128             : {
     129             :     DBG_DTOR(FmFieldWinListBox,NULL);
     130           0 : }
     131             : 
     132             : //------------------------------------------------------------------------------
     133           0 : sal_Int8 FmFieldWinListBox::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
     134             : {
     135           0 :     return DND_ACTION_NONE;
     136             : }
     137             : 
     138             : //------------------------------------------------------------------------------
     139           0 : sal_Int8 FmFieldWinListBox::ExecuteDrop( const ExecuteDropEvent& /*rEvt*/ )
     140             : {
     141           0 :     return DND_ACTION_NONE;
     142             : }
     143             : 
     144             : //------------------------------------------------------------------------------
     145           0 : sal_Bool FmFieldWinListBox::DoubleClickHdl()
     146             : {
     147           0 :     if ( pTabWin->createSelectionControls() )
     148           0 :         return sal_True;
     149             : 
     150           0 :     return SvTreeListBox::DoubleClickHdl();
     151             : }
     152             : 
     153             : //------------------------------------------------------------------------------
     154           0 : void FmFieldWinListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
     155             : {
     156           0 :     SvTreeListEntry* pSelected = FirstSelected();
     157           0 :     if (!pSelected)
     158             :         // no drag without a field
     159           0 :         return;
     160             : 
     161           0 :     ::svx::ODataAccessDescriptor aDescriptor;
     162           0 :     aDescriptor[ daDataSource ] <<= pTabWin->GetDatabaseName();
     163           0 :     aDescriptor[ daConnection ] <<= pTabWin->GetConnection().getTyped();
     164           0 :     aDescriptor[ daCommand ]    <<= pTabWin->GetObjectName();
     165           0 :     aDescriptor[ daCommandType ]<<= pTabWin->GetObjectType();
     166           0 :     ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
     167           0 :     aDescriptor[ daColumnName ] <<= pInfo->sColumnName;
     168             : 
     169             :     TransferableHelper* pTransferColumn = new OColumnTransferable(
     170             :         aDescriptor, CTF_FIELD_DESCRIPTOR | CTF_CONTROL_EXCHANGE | CTF_COLUMN_DESCRIPTOR
     171           0 :     );
     172           0 :     Reference< XTransferable> xEnsureDelete = pTransferColumn;
     173           0 :     if (pTransferColumn)
     174             :     {
     175           0 :         EndSelection();
     176           0 :         pTransferColumn->StartDrag( this, DND_ACTION_COPY );
     177           0 :     }
     178             : }
     179             : 
     180             : //========================================================================
     181             : // class FmFieldWinData
     182             : //========================================================================
     183             : DBG_NAME(FmFieldWinData);
     184             : //-----------------------------------------------------------------------
     185           0 : FmFieldWinData::FmFieldWinData()
     186             : {
     187             :     DBG_CTOR(FmFieldWinData,NULL);
     188           0 : }
     189             : 
     190             : //-----------------------------------------------------------------------
     191           0 : FmFieldWinData::~FmFieldWinData()
     192             : {
     193             :     DBG_DTOR(FmFieldWinData,NULL);
     194           0 : }
     195             : 
     196             : //========================================================================
     197             : // class FmFieldWin
     198             : //========================================================================
     199             : DBG_NAME(FmFieldWin);
     200             : //-----------------------------------------------------------------------
     201           0 : 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           0 :             ,pData(new FmFieldWinData)
     206             :             ,m_nObjectType(0)
     207           0 :             ,m_pChangeListener(NULL)
     208             : {
     209             :     DBG_CTOR(FmFieldWin,NULL);
     210           0 :     SetHelpId( HID_FIELD_SEL_WIN );
     211             : 
     212           0 :     SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
     213           0 :     pListBox = new FmFieldWinListBox( this );
     214           0 :     pListBox->Show();
     215           0 :     UpdateContent(NULL);
     216           0 :     SetSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
     217           0 : }
     218             : 
     219             : //-----------------------------------------------------------------------
     220           0 : FmFieldWin::~FmFieldWin()
     221             : {
     222           0 :     if (m_pChangeListener)
     223             :     {
     224           0 :         m_pChangeListener->dispose();
     225           0 :         m_pChangeListener->release();
     226             :         //  delete m_pChangeListener;
     227             :     }
     228           0 :     delete pListBox;
     229           0 :     delete pData;
     230             :     DBG_DTOR(FmFieldWin,NULL);
     231           0 : }
     232             : 
     233             : //-----------------------------------------------------------------------
     234           0 : void FmFieldWin::GetFocus()
     235             : {
     236           0 :     if ( pListBox )
     237           0 :         pListBox->GrabFocus();
     238             :     else
     239           0 :         SfxFloatingWindow::GetFocus();
     240           0 : }
     241             : 
     242             : //-----------------------------------------------------------------------
     243           0 : sal_Bool FmFieldWin::createSelectionControls( )
     244             : {
     245           0 :     SvTreeListEntry* pSelected = pListBox->FirstSelected();
     246           0 :     if ( pSelected )
     247             :     {
     248             :         // build a descriptor for the currently selected field
     249           0 :         ODataAccessDescriptor aDescr;
     250           0 :         aDescr.setDataSource(GetDatabaseName());
     251             : 
     252           0 :         aDescr[ daConnection ]  <<= GetConnection().getTyped();
     253             : 
     254           0 :         aDescr[ daCommand ]     <<= GetObjectName();
     255           0 :         aDescr[ daCommandType ] <<= GetObjectType();
     256           0 :         ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
     257           0 :         aDescr[ daColumnName ]  <<= pInfo->sColumnName;//OUString( pListBox->GetEntryText( pSelected) );
     258             : 
     259             :         // transfer this to the SFX world
     260           0 :         SfxUnoAnyItem aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR, makeAny( aDescr.createPropertyValueSequence() ) );
     261             :         const SfxPoolItem* pArgs[] =
     262             :         {
     263             :             &aDescriptorItem, NULL
     264           0 :         };
     265             : 
     266             :         // execute the create slot
     267           0 :         GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL, pArgs );
     268             :     }
     269             : 
     270           0 :     return NULL != pSelected;
     271             : }
     272             : 
     273             : //-----------------------------------------------------------------------
     274           0 : long FmFieldWin::PreNotify( NotifyEvent& _rNEvt )
     275             : {
     276           0 :     if ( EVENT_KEYINPUT == _rNEvt.GetType() )
     277             :     {
     278           0 :         const KeyCode& rKeyCode = _rNEvt.GetKeyEvent()->GetKeyCode();
     279           0 :         if ( ( 0 == rKeyCode.GetModifier() ) && ( KEY_RETURN == rKeyCode.GetCode() ) )
     280             :         {
     281           0 :             if ( createSelectionControls() )
     282           0 :                 return 1;
     283             :         }
     284             :     }
     285             : 
     286           0 :     return SfxFloatingWindow::PreNotify( _rNEvt );
     287             : }
     288             : 
     289             : //-----------------------------------------------------------------------
     290           0 : sal_Bool FmFieldWin::Close()
     291             : {
     292           0 :     return SfxFloatingWindow::Close();
     293             : }
     294             : 
     295             : //-----------------------------------------------------------------------
     296           0 : void FmFieldWin::_propertyChanged(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw( ::com::sun::star::uno::RuntimeException )
     297             : {
     298           0 :     ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm >  xForm(evt.Source, ::com::sun::star::uno::UNO_QUERY);
     299           0 :     UpdateContent(xForm);
     300           0 : }
     301             : 
     302             : //-----------------------------------------------------------------------
     303           0 : void FmFieldWin::StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
     304             : {
     305           0 :     if (!pState  || SID_FM_FIELDS_CONTROL != nSID)
     306           0 :         return;
     307             : 
     308           0 :     if (eState >= SFX_ITEM_AVAILABLE)
     309             :     {
     310           0 :         FmFormShell* pShell = PTR_CAST(FmFormShell,((SfxObjectItem*)pState)->GetShell());
     311           0 :         UpdateContent(pShell);
     312             :     }
     313             :     else
     314           0 :         UpdateContent(NULL);
     315             : }
     316             : 
     317             : //-----------------------------------------------------------------------
     318           0 : void FmFieldWin::UpdateContent(FmFormShell* pShell)
     319             : {
     320           0 :     pListBox->Clear();
     321           0 :     String aTitle( SVX_RES( RID_STR_FIELDSELECTION ) );
     322           0 :     SetText( aTitle );
     323             : 
     324           0 :     if (!pShell || !pShell->GetImpl())
     325           0 :         return;
     326             : 
     327           0 :     Reference< XForm >  xForm = pShell->GetImpl()->getCurrentForm();
     328           0 :     if ( xForm.is() )
     329           0 :         UpdateContent( xForm );
     330             : }
     331             : 
     332             : //-----------------------------------------------------------------------
     333           0 : void FmFieldWin::UpdateContent(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm > & xForm)
     334             : {
     335             :     try
     336             :     {
     337             :         // ListBox loeschen
     338           0 :         pListBox->Clear();
     339           0 :         OUString aTitle(SVX_RES(RID_STR_FIELDSELECTION));
     340           0 :         SetText(aTitle);
     341             : 
     342           0 :         if (!xForm.is())
     343           0 :             return;
     344             : 
     345           0 :         Reference< XPreparedStatement >  xStatement;
     346           0 :         Reference< XPropertySet >  xSet(xForm, UNO_QUERY);
     347             : 
     348           0 :         m_aObjectName   = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_COMMAND));
     349           0 :         m_aDatabaseName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_DATASOURCE));
     350           0 :         m_nObjectType   = ::comphelper::getINT32(xSet->getPropertyValue(FM_PROP_COMMANDTYPE));
     351             : 
     352             :         // get the connection of the form
     353           0 :         OStaticDataAccessTools aTools;
     354             :         m_aConnection.reset(
     355             :             aTools.connectRowset( Reference< XRowSet >( xForm, UNO_QUERY ), ::comphelper::getProcessComponentContext(), sal_True ),
     356             :             SharedConnection::NoTakeOwnership
     357           0 :         );
     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
     361             : 
     362             :         // get the fields of the object
     363             : 
     364           0 :         if ( m_aConnection.is() && !m_aObjectName.isEmpty() )
     365             :         {
     366           0 :             Reference< XComponent > xKeepFieldsAlive;
     367           0 :             Reference< XNameAccess > xColumns = getFieldsByCommandDescriptor( m_aConnection, m_nObjectType, m_aObjectName,xKeepFieldsAlive );
     368           0 :             if ( xColumns.is() )
     369           0 :                 lcl_addToList(*pListBox,xColumns);
     370             :         }
     371             : 
     372             :         // Prefix setzen
     373           0 :         OUString  aPrefix;
     374           0 :         StringListResource aPrefixes( SVX_RES( RID_RSC_TABWIN_PREFIX ) );
     375             : 
     376           0 :         switch (m_nObjectType)
     377             :         {
     378             :             case CommandType::TABLE:
     379           0 :                 aPrefix = aPrefixes[0];
     380           0 :                 break;
     381             :             case CommandType::QUERY:
     382           0 :                 aPrefix = aPrefixes[1];
     383           0 :                 break;
     384             :             default:
     385           0 :                 aPrefix = aPrefixes[2];
     386           0 :                 break;
     387             :         }
     388             : 
     389             :         // an dem PropertySet nach Aenderungen der ControlSource lauschen
     390           0 :         if (m_pChangeListener)
     391             :         {
     392           0 :             m_pChangeListener->dispose();
     393           0 :             m_pChangeListener->release();
     394             :         }
     395           0 :         m_pChangeListener = new ::comphelper::OPropertyChangeMultiplexer(this, xSet);
     396           0 :         m_pChangeListener->acquire();
     397           0 :         m_pChangeListener->addProperty(FM_PROP_DATASOURCE);
     398           0 :         m_pChangeListener->addProperty(FM_PROP_COMMAND);
     399           0 :         m_pChangeListener->addProperty(FM_PROP_COMMANDTYPE);
     400             : 
     401             :         // Titel setzen
     402           0 :         aTitle = aTitle + " " + aPrefix + " " + OUString(m_aObjectName.getStr());
     403           0 :         SetText( aTitle );
     404             :     }
     405           0 :     catch( const Exception& )
     406             :     {
     407             :         OSL_FAIL( "FmTabWin::UpdateContent: caught an exception!" );
     408             :     }
     409             : }
     410             : 
     411             : //-----------------------------------------------------------------------
     412           0 : void FmFieldWin::Resize()
     413             : {
     414           0 :     SfxFloatingWindow::Resize();
     415             : 
     416           0 :     Size aOutputSize( GetOutputSizePixel() );
     417             : 
     418             :     //////////////////////////////////////////////////////////////////////
     419             : 
     420             :     // Groesse der ::com::sun::star::form::ListBox anpassen
     421           0 :     Point aLBPos( LISTBOX_BORDER, LISTBOX_BORDER );
     422           0 :     Size aLBSize( aOutputSize );
     423           0 :     aLBSize.Width() -= (2*LISTBOX_BORDER);
     424           0 :     aLBSize.Height() -= (2*LISTBOX_BORDER);
     425             : 
     426           0 :     pListBox->SetPosSizePixel( aLBPos, aLBSize );
     427           0 : }
     428             : 
     429             : //-----------------------------------------------------------------------
     430           0 : void FmFieldWin::FillInfo( SfxChildWinInfo& rInfo ) const
     431             : {
     432           0 :     rInfo.bVisible = sal_False;
     433           0 : }
     434             : 
     435             : //-----------------------------------------------------------------------
     436          68 : SFX_IMPL_FLOATINGWINDOW(FmFieldWinMgr, SID_FM_ADD_FIELD)
     437             : 
     438             : //-----------------------------------------------------------------------
     439           0 : FmFieldWinMgr::FmFieldWinMgr(Window* _pParent, sal_uInt16 _nId,
     440             :                SfxBindings* _pBindings, SfxChildWinInfo* _pInfo)
     441           0 :               :SfxChildWindow(_pParent, _nId)
     442             : {
     443           0 :     pWindow = new FmFieldWin(_pBindings, this, _pParent);
     444           0 :     SetHideNotDelete(sal_True);
     445           0 :     eChildAlignment = SFX_ALIGN_NOALIGNMENT;
     446           0 :     ((SfxFloatingWindow*)pWindow)->Initialize( _pInfo );
     447         216 : }
     448             : 
     449             : 
     450             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10