update dev300-m58
[ooovba.git] / dbaccess / source / ui / control / opendoccontrols.cxx
blobef071b0287a080e7d5aa4cc552e396e82403c782
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: opendoccontrols.cxx,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef DBACCESS_SOURCE_UI_INC_OPENDOCCONTROLS_HXX
35 #include "opendoccontrols.hxx"
36 #endif
38 /** === begin UNO includes === **/
39 #ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_
40 #include <com/sun/star/uno/Sequence.hxx>
41 #endif
42 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 #endif
45 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
49 #include <com/sun/star/container/XNameAccess.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_UI_XMODULEUICONFIGURATIONMANAGERSUPPLIER_HPP_
52 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_UI_XUICONFIGURATIONMANAGER_HPP_
55 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_GRAPHIC_XGRAPHIC_HPP_
58 #include <com/sun/star/graphic/XGraphic.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_UI_XIMAGEMANAGER_HPP_
61 #include <com/sun/star/ui/XImageManager.hpp>
62 #endif
63 /** === end UNO includes === **/
65 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
66 #include <comphelper/processfactory.hxx>
67 #endif
68 #ifndef _SV_GRAPH_HXX
69 #include <vcl/graph.hxx>
70 #endif
71 #ifndef _SV_HELP_HXX
72 #include <vcl/help.hxx>
73 #endif
74 #ifndef INCLUDED_SVTOOLS_HISTORYOPTIONS_HXX
75 #include <svtools/historyoptions.hxx>
76 #endif
77 #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX_
78 #include <comphelper/sequenceashashmap.hxx>
79 #endif
80 #ifndef _URLOBJ_HXX
81 #include <tools/urlobj.hxx>
82 #endif
83 #ifndef SVTOOLS_FILENOTATION_HXX
84 #include <svtools/filenotation.hxx>
85 #endif
87 //........................................................................
88 namespace dbaui
90 //........................................................................
92 namespace
94 using ::com::sun::star::uno::Reference;
95 using ::com::sun::star::uno::Exception;
96 using ::com::sun::star::uno::Sequence;
97 using ::com::sun::star::uno::UNO_QUERY_THROW;
98 using ::com::sun::star::container::XNameAccess;
99 using ::com::sun::star::lang::XMultiServiceFactory;
100 using ::com::sun::star::beans::PropertyValue;
101 using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
102 using ::com::sun::star::ui::XUIConfigurationManager;
103 using ::com::sun::star::ui::XImageManager;
104 using ::com::sun::star::graphic::XGraphic;
106 String GetCommandText( const sal_Char* _pCommandURL, const ::rtl::OUString& _rModuleName )
108 ::rtl::OUString sLabel;
109 if ( !_pCommandURL || !*_pCommandURL )
110 return sLabel;
112 Reference< XNameAccess > xUICommandLabels;
113 ::rtl::OUString sCommandURL = ::rtl::OUString::createFromAscii( _pCommandURL );
119 // Retrieve popup menu labels
120 Reference< XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
121 if ( !xFactory.is() )
122 break;
124 Reference< XNameAccess> xNameAccess;
125 xNameAccess = xNameAccess.query( xFactory->createInstance(
126 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.UICommandDescription" ) )
127 ) );
128 if ( !xNameAccess.is() )
129 break;
131 xNameAccess->getByName( _rModuleName ) >>= xUICommandLabels;
132 if ( !xUICommandLabels.is() )
133 break;
135 Sequence< PropertyValue > aProperties;
136 if ( !( xUICommandLabels->getByName(sCommandURL) >>= aProperties ) )
137 break;
139 sal_Int32 nCount( aProperties.getLength() );
140 for ( sal_Int32 i=0; i<nCount; ++i )
142 ::rtl::OUString sPropertyName( aProperties[i].Name );
143 if ( sPropertyName.equalsAscii("Label" ) )
145 aProperties[i].Value >>= sLabel;
146 break;
150 while ( false );
152 catch( Exception& rException )
154 (void)rException;
157 return sLabel;
160 Image GetCommandIcon( const sal_Char* _pCommandURL, const ::rtl::OUString& _rModuleName )
162 Image aIcon;
163 if ( !_pCommandURL || !*_pCommandURL )
164 return aIcon;
166 Reference< XNameAccess > xUICommandLabels;
167 ::rtl::OUString sCommandURL = ::rtl::OUString::createFromAscii( _pCommandURL );
172 // Retrieve popup menu labels
173 Reference< XMultiServiceFactory> xFactory( ::comphelper::getProcessServiceFactory() );
174 if ( !xFactory.is() )
175 break;
177 Reference< XModuleUIConfigurationManagerSupplier > xSupplier(
178 xFactory->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
179 "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ) ) ),
180 UNO_QUERY_THROW );
182 Reference< XUIConfigurationManager > xManager( xSupplier->getUIConfigurationManager( _rModuleName ) );
183 Reference< XImageManager > xImageManager;
184 if ( xManager.is() )
185 xImageManager = xImageManager.query( xManager->getImageManager() );
186 if ( !xImageManager.is() )
187 break;
189 Sequence< ::rtl::OUString > aCommandList( &sCommandURL, 1 );
190 Sequence<Reference< XGraphic> > xIconList( xImageManager->getImages( 0, aCommandList ) );
191 if ( !xIconList.hasElements() )
192 break;
194 aIcon = Graphic( xIconList[0] ).GetBitmapEx();
196 while ( false );
198 catch ( Exception& rException )
200 (void)rException;
203 return aIcon;
209 //====================================================================
210 //= OpenButton
211 //====================================================================
212 //--------------------------------------------------------------------
213 OpenDocumentButton::OpenDocumentButton( Window* _pParent, const sal_Char* _pAsciiModuleName, WinBits _nStyle )
214 :PushButton( _pParent, _nStyle )
216 impl_init( _pAsciiModuleName );
219 //--------------------------------------------------------------------
220 OpenDocumentButton::OpenDocumentButton( Window* _pParent, const sal_Char* _pAsciiModuleName, const ResId& _rResId )
221 :PushButton( _pParent, _rResId )
223 impl_init( _pAsciiModuleName );
226 //--------------------------------------------------------------------
227 void OpenDocumentButton::impl_init( const sal_Char* _pAsciiModuleName )
229 DBG_ASSERT( _pAsciiModuleName, "OpenDocumentButton::impl_init: invalid module name!" );
230 m_sModule = ::rtl::OUString::createFromAscii( _pAsciiModuleName );
232 // our label should equal the UI text of the "Open" command
233 String sLabel( GetCommandText( ".uno:Open", m_sModule ) );
234 sLabel.SearchAndReplaceAllAscii( "~", String() );
235 sLabel.Insert( (sal_Unicode)' ', 0 );
236 SetText( sLabel );
238 // Place icon left of text and both centered in the button.
239 SetModeImage( GetCommandIcon( ".uno:Open", m_sModule ), BMP_COLOR_NORMAL );
240 EnableImageDisplay( TRUE );
241 EnableTextDisplay( TRUE );
242 SetImageAlign( IMAGEALIGN_LEFT );
243 SetStyle( GetStyle() | WB_CENTER );
246 //====================================================================
247 //= OpenDocumentListBox
248 //====================================================================
249 //--------------------------------------------------------------------
250 OpenDocumentListBox::OpenDocumentListBox( Window* _pParent, const sal_Char* _pAsciiModuleName, WinBits _nStyle )
251 :ListBox( _pParent, _nStyle )
253 impl_init( _pAsciiModuleName );
256 //--------------------------------------------------------------------
257 OpenDocumentListBox::OpenDocumentListBox( Window* _pParent, const sal_Char* _pAsciiModuleName, const ResId& _rResId )
258 :ListBox( _pParent, _rResId )
260 impl_init( _pAsciiModuleName );
263 //--------------------------------------------------------------------
264 void OpenDocumentListBox::impl_init( const sal_Char* _pAsciiModuleName )
266 DBG_ASSERT( _pAsciiModuleName, "OpenDocumentListBox::impl_init: invalid module name!" );
268 Sequence< Sequence< PropertyValue> > aHistory = SvtHistoryOptions().GetList( ePICKLIST );
269 Reference< XNameAccess > xFilterFactory;
270 xFilterFactory = xFilterFactory.query( ::comphelper::getProcessServiceFactory()->createInstance(
271 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" ) ) ) );
273 sal_uInt32 nCount = aHistory.getLength();
274 for ( sal_uInt32 nItem = 0; nItem < nCount; ++nItem )
278 // Get the current history item's properties.
279 ::comphelper::SequenceAsHashMap aItemProperties( aHistory[ nItem ] );
280 ::rtl::OUString sURL = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_URL, ::rtl::OUString() );
281 ::rtl::OUString sFilter = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_FILTER, ::rtl::OUString() );
282 String sTitle = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_TITLE, ::rtl::OUString() );
283 ::rtl::OUString sPassword = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_PASSWORD, ::rtl::OUString() );
285 // If the entry is an impress file then insert it into the
286 // history list and the list box.
287 Sequence< PropertyValue > aProps;
288 xFilterFactory->getByName( sFilter ) >>= aProps;
290 ::comphelper::SequenceAsHashMap aFilterProperties( aProps );
291 ::rtl::OUString sDocumentService = aFilterProperties.getUnpackedValueOrDefault(
292 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DocumentService" ) ), ::rtl::OUString() );
293 if ( sDocumentService.equalsAscii( _pAsciiModuleName ) )
295 // yes, it's a Base document
296 INetURLObject aURL;
297 aURL.SetSmartURL( sURL );
298 // The password is set only when it is not empty.
299 if ( sPassword.getLength() > 0 )
300 aURL.SetPass( sPassword );
302 if ( !sTitle.Len() )
303 sTitle = aURL.getBase( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_UNAMBIGUOUS );
305 String sDecodedURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
307 USHORT nPos = InsertEntry( sTitle );
308 m_aURLs.insert( MapIndexToStringPair::value_type( nPos, StringPair( sDecodedURL, sFilter ) ) );
311 catch( Exception& rException )
313 (void)rException;
318 //--------------------------------------------------------------------
319 String OpenDocumentListBox::GetSelectedDocumentURL() const
321 String sURL;
322 USHORT nSelected = GetSelectEntryPos();
323 if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
324 sURL = impl_getDocumentAtIndex( nSelected ).first;
325 return sURL;
328 //--------------------------------------------------------------------
329 String OpenDocumentListBox::GetSelectedDocumentFilter() const
331 String sFilter;
332 USHORT nSelected = GetSelectEntryPos();
333 if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
334 sFilter = impl_getDocumentAtIndex( nSelected ).second;
335 return sFilter;
338 //--------------------------------------------------------------------
339 OpenDocumentListBox::StringPair OpenDocumentListBox::impl_getDocumentAtIndex( USHORT _nListIndex, bool _bSystemNotation ) const
341 MapIndexToStringPair::const_iterator pos = m_aURLs.find( _nListIndex );
342 DBG_ASSERT( pos != m_aURLs.end(), "OpenDocumentListBox::impl_getDocumentAtIndex: invalid index!" );
344 StringPair aDocumentDescriptor;
345 if ( pos != m_aURLs.end() )
347 aDocumentDescriptor = pos->second;
348 if ( _bSystemNotation && aDocumentDescriptor.first.Len() )
350 ::svt::OFileNotation aNotation( aDocumentDescriptor.first );
351 aDocumentDescriptor.first = aNotation.get( ::svt::OFileNotation::N_SYSTEM );
354 return aDocumentDescriptor;
357 //--------------------------------------------------------------------
358 void OpenDocumentListBox::RequestHelp( const HelpEvent& _rHEvt )
360 if( !( _rHEvt.GetMode() & HELPMODE_QUICK ) )
361 return;
362 if ( !IsEnabled() )
363 return;
365 Point aRequestPos( ScreenToOutputPixel( _rHEvt.GetMousePosPixel() ) );
366 USHORT nItemIndex = LISTBOX_ENTRY_NOTFOUND;
367 if ( GetIndexForPoint( aRequestPos, nItemIndex ) != -1 )
369 Rectangle aItemRect( GetBoundingRectangle( nItemIndex ) );
370 aItemRect = Rectangle(
371 OutputToScreenPixel( aItemRect.TopLeft() ),
372 OutputToScreenPixel( aItemRect.BottomRight() ) );
373 String sHelpText = impl_getDocumentAtIndex( nItemIndex, true ).first;
374 Help::ShowQuickHelp( this, aItemRect, sHelpText, QUICKHELP_LEFT | QUICKHELP_VCENTER );
378 //........................................................................
379 } // namespace dbaui
380 //........................................................................