Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / control / opendoccontrols.cxx
blob12ca3060a9c6b5f1af4b15998c52cff497d001f4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "opendoccontrols.hxx"
32 /** === begin UNO includes === **/
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/container/XNameAccess.hpp>
37 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
38 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
39 #include <com/sun/star/graphic/XGraphic.hpp>
40 #include <com/sun/star/ui/XImageManager.hpp>
41 /** === end UNO includes === **/
43 #include <comphelper/processfactory.hxx>
44 #include <vcl/graph.hxx>
45 #include <vcl/help.hxx>
46 #include <unotools/historyoptions.hxx>
47 #include <comphelper/sequenceashashmap.hxx>
48 #include <tools/urlobj.hxx>
49 #include <svl/filenotation.hxx>
50 #include <osl/diagnose.h>
52 //........................................................................
53 namespace dbaui
55 //........................................................................
57 namespace
59 using ::com::sun::star::uno::Reference;
60 using ::com::sun::star::uno::Exception;
61 using ::com::sun::star::uno::Sequence;
62 using ::com::sun::star::uno::UNO_QUERY_THROW;
63 using ::com::sun::star::container::XNameAccess;
64 using ::com::sun::star::lang::XMultiServiceFactory;
65 using ::com::sun::star::beans::PropertyValue;
66 using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
67 using ::com::sun::star::ui::XUIConfigurationManager;
68 using ::com::sun::star::ui::XImageManager;
69 using ::com::sun::star::graphic::XGraphic;
71 String GetCommandText( const sal_Char* _pCommandURL, const ::rtl::OUString& _rModuleName )
73 ::rtl::OUString sLabel;
74 if ( !_pCommandURL || !*_pCommandURL )
75 return sLabel;
77 Reference< XNameAccess > xUICommandLabels;
78 ::rtl::OUString sCommandURL = ::rtl::OUString::createFromAscii( _pCommandURL );
80 try
84 // Retrieve popup menu labels
85 Reference< XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
86 if ( !xFactory.is() )
87 break;
89 Reference< XNameAccess> xNameAccess;
90 xNameAccess = xNameAccess.query( xFactory->createInstance(
91 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.UICommandDescription" ) )
92 ) );
93 if ( !xNameAccess.is() )
94 break;
96 xNameAccess->getByName( _rModuleName ) >>= xUICommandLabels;
97 if ( !xUICommandLabels.is() )
98 break;
100 Sequence< PropertyValue > aProperties;
101 if ( !( xUICommandLabels->getByName(sCommandURL) >>= aProperties ) )
102 break;
104 sal_Int32 nCount( aProperties.getLength() );
105 for ( sal_Int32 i=0; i<nCount; ++i )
107 ::rtl::OUString sPropertyName( aProperties[i].Name );
108 if ( sPropertyName == "Label" )
110 aProperties[i].Value >>= sLabel;
111 break;
115 while ( false );
117 catch( Exception& rException )
119 (void)rException;
122 return sLabel;
125 Image GetCommandIcon( const sal_Char* _pCommandURL, const ::rtl::OUString& _rModuleName )
127 Image aIcon;
128 if ( !_pCommandURL || !*_pCommandURL )
129 return aIcon;
131 Reference< XNameAccess > xUICommandLabels;
132 ::rtl::OUString sCommandURL = ::rtl::OUString::createFromAscii( _pCommandURL );
137 // Retrieve popup menu labels
138 Reference< XMultiServiceFactory> xFactory( ::comphelper::getProcessServiceFactory() );
139 if ( !xFactory.is() )
140 break;
142 Reference< XModuleUIConfigurationManagerSupplier > xSupplier(
143 xFactory->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
144 "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ) ) ),
145 UNO_QUERY_THROW );
147 Reference< XUIConfigurationManager > xManager( xSupplier->getUIConfigurationManager( _rModuleName ) );
148 Reference< XImageManager > xImageManager;
149 if ( xManager.is() )
150 xImageManager = xImageManager.query( xManager->getImageManager() );
151 if ( !xImageManager.is() )
152 break;
154 Sequence< ::rtl::OUString > aCommandList( &sCommandURL, 1 );
155 Sequence<Reference< XGraphic> > xIconList( xImageManager->getImages( 0, aCommandList ) );
156 if ( !xIconList.hasElements() )
157 break;
159 aIcon = Graphic( xIconList[0] ).GetBitmapEx();
161 while ( false );
163 catch ( Exception& rException )
165 (void)rException;
168 return aIcon;
174 //====================================================================
175 //= OpenButton
176 //====================================================================
177 //--------------------------------------------------------------------
178 OpenDocumentButton::OpenDocumentButton( Window* _pParent, const sal_Char* _pAsciiModuleName, const ResId& _rResId )
179 :PushButton( _pParent, _rResId )
181 impl_init( _pAsciiModuleName );
184 //--------------------------------------------------------------------
185 void OpenDocumentButton::impl_init( const sal_Char* _pAsciiModuleName )
187 OSL_ENSURE( _pAsciiModuleName, "OpenDocumentButton::impl_init: invalid module name!" );
188 m_sModule = ::rtl::OUString::createFromAscii( _pAsciiModuleName );
190 // our label should equal the UI text of the "Open" command
191 String sLabel( GetCommandText( ".uno:Open", m_sModule ) );
192 sLabel.SearchAndReplaceAllAscii( "~", String() );
193 sLabel.Insert( (sal_Unicode)' ', 0 );
194 SetText( sLabel );
196 // Place icon left of text and both centered in the button.
197 SetModeImage( GetCommandIcon( ".uno:Open", m_sModule ) );
198 EnableImageDisplay( sal_True );
199 EnableTextDisplay( sal_True );
200 SetImageAlign( IMAGEALIGN_LEFT );
201 SetStyle( GetStyle() | WB_CENTER );
204 //====================================================================
205 //= OpenDocumentListBox
206 //====================================================================
207 //--------------------------------------------------------------------
208 OpenDocumentListBox::OpenDocumentListBox( Window* _pParent, const sal_Char* _pAsciiModuleName, const ResId& _rResId )
209 :ListBox( _pParent, _rResId )
211 impl_init( _pAsciiModuleName );
214 //--------------------------------------------------------------------
215 void OpenDocumentListBox::impl_init( const sal_Char* _pAsciiModuleName )
217 OSL_ENSURE( _pAsciiModuleName, "OpenDocumentListBox::impl_init: invalid module name!" );
219 Sequence< Sequence< PropertyValue> > aHistory = SvtHistoryOptions().GetList( ePICKLIST );
220 Reference< XNameAccess > xFilterFactory;
221 xFilterFactory = xFilterFactory.query( ::comphelper::getProcessServiceFactory()->createInstance(
222 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" ) ) ) );
224 sal_uInt32 nCount = aHistory.getLength();
225 for ( sal_uInt32 nItem = 0; nItem < nCount; ++nItem )
229 // Get the current history item's properties.
230 ::comphelper::SequenceAsHashMap aItemProperties( aHistory[ nItem ] );
231 ::rtl::OUString sURL = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_URL, ::rtl::OUString() );
232 ::rtl::OUString sFilter = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_FILTER, ::rtl::OUString() );
233 String sTitle = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_TITLE, ::rtl::OUString() );
234 ::rtl::OUString sPassword = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_PASSWORD, ::rtl::OUString() );
236 // If the entry is an impress file then insert it into the
237 // history list and the list box.
238 Sequence< PropertyValue > aProps;
239 xFilterFactory->getByName( sFilter ) >>= aProps;
241 ::comphelper::SequenceAsHashMap aFilterProperties( aProps );
242 ::rtl::OUString sDocumentService = aFilterProperties.getUnpackedValueOrDefault(
243 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DocumentService" ) ), ::rtl::OUString() );
244 if ( sDocumentService.equalsAscii( _pAsciiModuleName ) )
246 // yes, it's a Base document
247 INetURLObject aURL;
248 aURL.SetSmartURL( sURL );
249 // The password is set only when it is not empty.
250 if ( !sPassword.isEmpty() )
251 aURL.SetPass( sPassword );
253 if ( !sTitle.Len() )
254 sTitle = aURL.getBase( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_UNAMBIGUOUS );
256 String sDecodedURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
258 sal_uInt16 nPos = InsertEntry( sTitle );
259 m_aURLs.insert( MapIndexToStringPair::value_type( nPos, StringPair( sDecodedURL, sFilter ) ) );
262 catch( Exception& rException )
264 (void)rException;
269 //--------------------------------------------------------------------
270 String OpenDocumentListBox::GetSelectedDocumentURL() const
272 String sURL;
273 sal_uInt16 nSelected = GetSelectEntryPos();
274 if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
275 sURL = impl_getDocumentAtIndex( nSelected ).first;
276 return sURL;
279 //--------------------------------------------------------------------
280 String OpenDocumentListBox::GetSelectedDocumentFilter() const
282 String sFilter;
283 sal_uInt16 nSelected = GetSelectEntryPos();
284 if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
285 sFilter = impl_getDocumentAtIndex( nSelected ).second;
286 return sFilter;
289 //--------------------------------------------------------------------
290 OpenDocumentListBox::StringPair OpenDocumentListBox::impl_getDocumentAtIndex( sal_uInt16 _nListIndex, bool _bSystemNotation ) const
292 MapIndexToStringPair::const_iterator pos = m_aURLs.find( _nListIndex );
293 OSL_ENSURE( pos != m_aURLs.end(), "OpenDocumentListBox::impl_getDocumentAtIndex: invalid index!" );
295 StringPair aDocumentDescriptor;
296 if ( pos != m_aURLs.end() )
298 aDocumentDescriptor = pos->second;
299 if ( _bSystemNotation && aDocumentDescriptor.first.Len() )
301 ::svt::OFileNotation aNotation( aDocumentDescriptor.first );
302 aDocumentDescriptor.first = aNotation.get( ::svt::OFileNotation::N_SYSTEM );
305 return aDocumentDescriptor;
308 //--------------------------------------------------------------------
309 void OpenDocumentListBox::RequestHelp( const HelpEvent& _rHEvt )
311 if( !( _rHEvt.GetMode() & HELPMODE_QUICK ) )
312 return;
313 if ( !IsEnabled() )
314 return;
316 Point aRequestPos( ScreenToOutputPixel( _rHEvt.GetMousePosPixel() ) );
317 sal_uInt16 nItemIndex = LISTBOX_ENTRY_NOTFOUND;
318 if ( GetIndexForPoint( aRequestPos, nItemIndex ) != -1 )
320 Rectangle aItemRect( GetBoundingRectangle( nItemIndex ) );
321 aItemRect = Rectangle(
322 OutputToScreenPixel( aItemRect.TopLeft() ),
323 OutputToScreenPixel( aItemRect.BottomRight() ) );
324 String sHelpText = impl_getDocumentAtIndex( nItemIndex, true ).first;
325 Help::ShowQuickHelp( this, aItemRect, sHelpText, QUICKHELP_LEFT | QUICKHELP_VCENTER );
329 //........................................................................
330 } // namespace dbaui
331 //........................................................................
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */