Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / control / opendoccontrols.cxx
blob0eadbf03fc2138b994d7182c412f235cbe98f6c7
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 .
20 #include "opendoccontrols.hxx"
22 #include <com/sun/star/uno/Sequence.hxx>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
27 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
28 #include <com/sun/star/graphic/XGraphic.hpp>
29 #include <com/sun/star/ui/XImageManager.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <vcl/graph.hxx>
33 #include <vcl/help.hxx>
34 #include <vcl/commandinfoprovider.hxx>
35 #include <unotools/historyoptions.hxx>
36 #include <comphelper/sequenceashashmap.hxx>
37 #include <tools/urlobj.hxx>
38 #include <svl/filenotation.hxx>
39 #include <osl/diagnose.h>
40 #include <vcl/builderfactory.hxx>
42 namespace dbaui
45 namespace
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Exception;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::XComponentContext;
51 using ::com::sun::star::container::XNameAccess;
52 using ::com::sun::star::beans::PropertyValue;
53 using ::com::sun::star::ui::theModuleUIConfigurationManagerSupplier;
54 using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
55 using ::com::sun::star::ui::XUIConfigurationManager;
56 using ::com::sun::star::ui::XImageManager;
57 using ::com::sun::star::graphic::XGraphic;
59 Image GetCommandIcon( const sal_Char* _pCommandURL, const OUString& _rModuleName )
61 Image aIcon;
62 if ( !_pCommandURL || !*_pCommandURL )
63 return aIcon;
65 Reference< XNameAccess > xUICommandLabels;
66 OUString sCommandURL = OUString::createFromAscii( _pCommandURL );
67 try
71 // Retrieve popup menu labels
72 Reference< css::uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
73 if ( !xContext.is() )
74 break;
76 Reference< XModuleUIConfigurationManagerSupplier > xSupplier(
77 theModuleUIConfigurationManagerSupplier::get(xContext) );
79 Reference< XUIConfigurationManager > xManager( xSupplier->getUIConfigurationManager( _rModuleName ) );
80 Reference< XImageManager > xImageManager;
81 if ( xManager.is() )
82 xImageManager.set(xManager->getImageManager(), css::uno::UNO_QUERY);
83 if ( !xImageManager.is() )
84 break;
86 Sequence< OUString > aCommandList( &sCommandURL, 1 );
87 Sequence<Reference< XGraphic> > xIconList( xImageManager->getImages( 0, aCommandList ) );
88 if ( !xIconList.hasElements() )
89 break;
91 aIcon = Image(Graphic(xIconList[0]).GetBitmapEx());
93 while ( false );
95 catch ( Exception& rException )
97 (void)rException;
100 return aIcon;
105 // OpenButton
107 OpenDocumentButton::OpenDocumentButton( vcl::Window* _pParent, const sal_Char* _pAsciiModuleName )
108 :PushButton( _pParent )
110 impl_init( _pAsciiModuleName );
113 VCL_BUILDER_FACTORY_ARGS( OpenDocumentButton, "com.sun.star.sdb.OfficeDatabaseDocument" );
115 void OpenDocumentButton::impl_init( const sal_Char* _pAsciiModuleName )
117 OSL_ENSURE( _pAsciiModuleName, "OpenDocumentButton::impl_init: invalid module name!" );
118 m_sModule = OUString::createFromAscii( _pAsciiModuleName );
120 // our label should equal the UI text of the "Open" command
121 OUString sLabel(vcl::CommandInfoProvider::Instance().GetCommandPropertyFromModule(".uno:Open", m_sModule));
122 SetText(" " + sLabel.replaceAll("~", ""));
124 // Place icon left of text and both centered in the button.
125 SetModeImage( GetCommandIcon( ".uno:Open", m_sModule ) );
126 EnableImageDisplay( true );
127 EnableTextDisplay( true );
128 SetImageAlign( ImageAlign::Left );
129 SetStyle( GetStyle() | WB_CENTER );
132 // OpenDocumentListBox
134 OpenDocumentListBox::OpenDocumentListBox( vcl::Window* _pParent, const sal_Char* _pAsciiModuleName )
135 :ListBox( _pParent, WB_BORDER | WB_DROPDOWN )
137 impl_init( _pAsciiModuleName );
140 VCL_BUILDER_FACTORY_ARGS( OpenDocumentListBox, "com.sun.star.sdb.OfficeDatabaseDocument" );
142 void OpenDocumentListBox::impl_init( const sal_Char* _pAsciiModuleName )
144 OSL_ENSURE( _pAsciiModuleName, "OpenDocumentListBox::impl_init: invalid module name!" );
146 Sequence< Sequence< PropertyValue> > aHistory = SvtHistoryOptions().GetList( ePICKLIST );
147 Reference< XNameAccess > xFilterFactory;
148 xFilterFactory.set(::comphelper::getProcessServiceFactory()->createInstance(
149 "com.sun.star.document.FilterFactory" ), css::uno::UNO_QUERY);
151 sal_uInt32 nCount = aHistory.getLength();
152 for ( sal_uInt32 nItem = 0; nItem < nCount; ++nItem )
156 // Get the current history item's properties.
157 ::comphelper::SequenceAsHashMap aItemProperties( aHistory[ nItem ] );
158 OUString sURL = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_URL, OUString() );
159 OUString sFilter = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_FILTER, OUString() );
160 OUString sTitle = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_TITLE, OUString() );
161 OUString sPassword = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_PASSWORD, OUString() );
163 // If the entry is an impress file then insert it into the
164 // history list and the list box.
165 Sequence< PropertyValue > aProps;
166 xFilterFactory->getByName( sFilter ) >>= aProps;
168 ::comphelper::SequenceAsHashMap aFilterProperties( aProps );
169 OUString sDocumentService = aFilterProperties.getUnpackedValueOrDefault(
170 "DocumentService", OUString() );
171 if ( sDocumentService.equalsAscii( _pAsciiModuleName ) )
173 // yes, it's a Base document
174 INetURLObject aURL;
175 aURL.SetSmartURL( sURL );
176 // The password is set only when it is not empty.
177 if ( !sPassword.isEmpty() )
178 aURL.SetPass( sPassword );
180 if ( sTitle.isEmpty() )
181 sTitle = aURL.getBase( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_UNAMBIGUOUS );
183 OUString sDecodedURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
185 sal_Int32 nPos = InsertEntry( sTitle );
186 m_aURLs.insert( MapIndexToStringPair::value_type( nPos, StringPair( sDecodedURL, sFilter ) ) );
189 catch( Exception& rException )
191 (void)rException;
196 OUString OpenDocumentListBox::GetSelectedDocumentURL() const
198 OUString sURL;
199 sal_Int32 nSelected = GetSelectEntryPos();
200 if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
201 sURL = impl_getDocumentAtIndex( nSelected ).first;
202 return sURL;
205 OUString OpenDocumentListBox::GetSelectedDocumentFilter() const
207 OUString sFilter;
208 sal_Int32 nSelected = GetSelectEntryPos();
209 if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
210 sFilter = impl_getDocumentAtIndex( nSelected ).second;
211 return sFilter;
214 OpenDocumentListBox::StringPair OpenDocumentListBox::impl_getDocumentAtIndex( sal_uInt16 _nListIndex, bool _bSystemNotation ) const
216 MapIndexToStringPair::const_iterator pos = m_aURLs.find( _nListIndex );
217 OSL_ENSURE( pos != m_aURLs.end(), "OpenDocumentListBox::impl_getDocumentAtIndex: invalid index!" );
219 StringPair aDocumentDescriptor;
220 if ( pos != m_aURLs.end() )
222 aDocumentDescriptor = pos->second;
223 if ( _bSystemNotation && !aDocumentDescriptor.first.isEmpty() )
225 ::svt::OFileNotation aNotation( aDocumentDescriptor.first );
226 aDocumentDescriptor.first = aNotation.get( ::svt::OFileNotation::N_SYSTEM );
229 return aDocumentDescriptor;
232 void OpenDocumentListBox::RequestHelp( const HelpEvent& _rHEvt )
234 if( !( _rHEvt.GetMode() & HelpEventMode::QUICK ) )
235 return;
236 if ( !IsEnabled() )
237 return;
239 Point aRequestPos( ScreenToOutputPixel( _rHEvt.GetMousePosPixel() ) );
240 sal_Int32 nItemIndex = LISTBOX_ENTRY_NOTFOUND;
241 if ( GetIndexForPoint( aRequestPos, nItemIndex ) != -1 )
243 Rectangle aItemRect( GetBoundingRectangle( nItemIndex ) );
244 aItemRect = Rectangle(
245 OutputToScreenPixel( aItemRect.TopLeft() ),
246 OutputToScreenPixel( aItemRect.BottomRight() ) );
247 OUString sHelpText = impl_getDocumentAtIndex( nItemIndex, true ).first;
248 Help::ShowQuickHelp( this, aItemRect, sHelpText, QuickHelpFlags::Left | QuickHelpFlags::VCenter );
252 } // namespace dbaui
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */