merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / dlg / filedlg.cxx
blob2c8c39e240abd04bdebf3062f459bfecf9b1517a
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: filedlg.cxx,v $
10 * $Revision: 1.19 $
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_sd.hxx"
33 #include <tools/debug.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
38 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
40 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
41 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
42 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
43 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
44 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
45 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
46 #include <vcl/msgbox.hxx>
47 #include <sal/types.h>
48 #include <tools/urlobj.hxx>
49 #include <vos/thread.hxx>
50 #include <vos/mutex.hxx>
51 #include <vcl/svapp.hxx>
52 #include <sfx2/filedlghelper.hxx>
54 #include <svx/impgrf.hxx>
56 #include <avmedia/mediawindow.hxx>
57 #include "filedlg.hxx"
58 #include "sdresid.hxx"
59 #include "strings.hrc"
63 //-----------------------------------------------------------------------------
65 namespace css = ::com::sun::star;
68 // --------------------------------------------------------------------
69 // ----------- SdFileDialog_Imp ---------------------------
70 // --------------------------------------------------------------------
71 class SdFileDialog_Imp : public sfx2::FileDialogHelper
73 private:
74 #if defined __SUNPRO_CC
75 using sfx2::FileDialogHelper::Execute;
76 #endif
78 friend class SdExportFileDialog;
79 friend class SdOpenSoundFileDialog;
81 css::uno::Reference< css::ui::dialogs::XFilePickerControlAccess > mxControlAccess;
83 css::uno::Reference< css::media::XPlayer > mxPlayer;
84 ULONG mnPlaySoundEvent;
85 BOOL mbUsableSelection;
86 BOOL mbLabelPlaying;
88 void CheckSelectionState();
90 DECL_LINK( PlayMusicHdl, void * );
92 Timer maUpdateTimer;
94 DECL_LINK( IsMusicStoppedHdl, void * );
96 public:
97 SdFileDialog_Imp( const short nDialogType, sal_Bool bUsableSelection );
98 ~SdFileDialog_Imp();
100 ErrCode Execute();
102 // overwritten from FileDialogHelper, to receive user feedback
103 virtual void SAL_CALL ControlStateChanged( const css::ui::dialogs::FilePickerEvent& aEvent );
105 sal_Bool SelectionBoxState() const;
108 // ------------------------------------------------------------------------
109 void SAL_CALL SdFileDialog_Imp::ControlStateChanged( const css::ui::dialogs::FilePickerEvent& aEvent )
111 ::vos::OGuard aGuard( Application::GetSolarMutex() );
113 switch( aEvent.ElementId )
115 case css::ui::dialogs::CommonFilePickerElementIds::LISTBOX_FILTER:
116 CheckSelectionState();
117 break;
119 case css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
120 if( mxControlAccess.is() )
122 if( mnPlaySoundEvent )
123 Application::RemoveUserEvent( mnPlaySoundEvent );
125 mnPlaySoundEvent = Application::PostUserEvent( LINK( this, SdFileDialog_Imp, PlayMusicHdl ) );
127 break;
131 // ------------------------------------------------------------------------
132 IMPL_LINK( SdFileDialog_Imp, PlayMusicHdl, void *, EMPTYARG )
134 maUpdateTimer.Stop();
135 mnPlaySoundEvent = 0;
137 if (mxPlayer.is())
139 if (mxPlayer->isPlaying())
140 mxPlayer->stop();
141 mxPlayer.clear();
144 if( mbLabelPlaying )
148 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
149 String( SdResId( STR_PLAY ) ) );
151 mbLabelPlaying = FALSE;
153 catch( css::lang::IllegalArgumentException )
155 #ifdef DBG_UTIL
156 DBG_ERROR( "Cannot access play button" );
157 #endif
160 else
162 rtl::OUString aUrl( GetPath() );
163 if ( aUrl.getLength() )
167 mxPlayer.set( avmedia::MediaWindow::createPlayer( aUrl ), css::uno::UNO_QUERY_THROW );
168 mxPlayer->start();
169 maUpdateTimer.SetTimeout( 100 );
170 maUpdateTimer.Start();
172 catch( css::uno::Exception& e )
174 (void)e;
175 mxPlayer.clear();
178 if (mxPlayer.is())
182 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
183 String( SdResId( STR_STOP ) ) );
185 mbLabelPlaying = TRUE;
187 catch( css::lang::IllegalArgumentException )
189 #ifdef DBG_UTIL
190 DBG_ERROR( "Cannot access play button" );
191 #endif
197 return 0;
200 // ------------------------------------------------------------------------
201 IMPL_LINK( SdFileDialog_Imp, IsMusicStoppedHdl, void *, EMPTYARG )
203 ::vos::OGuard aGuard( Application::GetSolarMutex() );
205 if (
206 mxPlayer.is() && mxPlayer->isPlaying() &&
207 mxPlayer->getMediaTime() < mxPlayer->getDuration()
210 maUpdateTimer.Start();
211 return 0L;
215 if( mxControlAccess.is() )
219 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
220 String( SdResId( STR_PLAY ) ) );
221 mbLabelPlaying = FALSE;
223 catch( css::lang::IllegalArgumentException )
225 #ifdef DBG_UTIL
226 DBG_ERROR( "Cannot access play button" );
227 #endif
231 return( 0L );
234 // check whether to disable the "selection" checkbox
235 void SdFileDialog_Imp::CheckSelectionState()
237 if( mbUsableSelection && mxControlAccess.is() )
239 String aCurrFilter( GetCurrentFilter() );
243 if( !aCurrFilter.Len() || ( aCurrFilter == String( SdResId( STR_EXPORT_HTML_NAME ) ) ) )
244 mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, FALSE );
245 else
246 mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, TRUE );
248 catch( css::lang::IllegalArgumentException )
250 #ifdef DBG_UTIL
251 DBG_ERROR( "Cannot access \"selection\" checkbox" );
252 #endif
257 //-----------------------------------------------------------------------------
258 SdFileDialog_Imp::SdFileDialog_Imp( const short nDialogType,
259 sal_Bool bUsableSelection ) :
260 FileDialogHelper( nDialogType, 0 ),
261 mnPlaySoundEvent( 0 ),
262 mbUsableSelection( bUsableSelection ),
263 mbLabelPlaying(FALSE)
265 maUpdateTimer.SetTimeoutHdl(LINK(this, SdFileDialog_Imp, IsMusicStoppedHdl));
267 css::uno::Reference < ::com::sun::star::ui::dialogs::XFilePicker > xFileDlg = GetFilePicker();
269 // get the control access
270 mxControlAccess = css::uno::Reference< css::ui::dialogs::XFilePickerControlAccess > ( xFileDlg, css::uno::UNO_QUERY );
272 if( mxControlAccess.is() )
274 if( nDialogType ==
275 css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY )
279 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
280 String( SdResId( STR_PLAY ) ) );
282 catch( css::lang::IllegalArgumentException )
284 #ifdef DBG_UTIL
285 DBG_ERROR( "Cannot set play button label" );
286 #endif
289 else if( mbUsableSelection != sal_True )
293 mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, FALSE );
295 catch( css::lang::IllegalArgumentException )
297 #ifdef DBG_UTIL
298 DBG_ERROR( "Cannot disable selection checkbox" );
299 #endif
306 // ------------------------------------------------------------------------
307 SdFileDialog_Imp::~SdFileDialog_Imp()
309 if( mnPlaySoundEvent )
310 Application::RemoveUserEvent( mnPlaySoundEvent );
313 // ------------------------------------------------------------------------
314 ErrCode SdFileDialog_Imp::Execute()
316 // make sure selection checkbox is disabled if
317 // HTML is current filter!
318 CheckSelectionState();
319 return FileDialogHelper::Execute();
322 // ------------------------------------------------------------------------
323 sal_Bool SdFileDialog_Imp::SelectionBoxState() const
325 if ( !mbUsableSelection || !mxControlAccess.is() )
326 return sal_False;
328 sal_Bool bState(0);
331 mxControlAccess->getValue( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, 0 ) >>= bState;
333 catch( css::lang::IllegalArgumentException )
335 #ifdef DBG_UTIL
336 DBG_ERROR( "Cannot access \"selection\" checkbox" );
337 #endif
340 return bState;
344 // --------------------------------------------------------------------
345 // ----------- SdExportFileDialog ---------------------------
346 // --------------------------------------------------------------------
348 // these are simple forwarders
349 SdExportFileDialog::SdExportFileDialog(BOOL bHaveCheckbox) :
350 mpImpl( new SdFileDialog_Imp( css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_SELECTION,
351 bHaveCheckbox ) )
353 // setup filter
354 const String aHTMLFilter( SdResId( STR_EXPORT_HTML_NAME ) );
355 GraphicFilter* pFilter = GetGrfFilter();
356 const USHORT nFilterCount = pFilter->GetExportFormatCount();
358 // add HTML filter
359 mpImpl->AddFilter( aHTMLFilter, String( SdResId( STR_EXPORT_HTML_FILTER ) ) );
361 // add other graphic filters
362 for ( USHORT i = 0; i < nFilterCount; i++ )
364 mpImpl->AddFilter( pFilter->GetExportFormatName( i ),
365 pFilter->GetExportWildcard( i ) );
368 // set dialog title
369 mpImpl->SetTitle( String( SdResId( STR_EXPORT_DIALOG_TITLE ) ) );
372 // ------------------------------------------------------------------------
373 SdExportFileDialog::~SdExportFileDialog()
377 // ------------------------------------------------------------------------
378 ErrCode SdExportFileDialog::Execute()
380 return mpImpl->Execute();
383 String SdExportFileDialog::GetPath() const
385 return mpImpl->GetPath();
388 // ------------------------------------------------------------------------
389 void SdExportFileDialog::SetPath( const String& rPath )
391 mpImpl->SetDisplayDirectory( rPath );
394 // ------------------------------------------------------------------------
395 String SdExportFileDialog::ReqCurrentFilter() const
397 return mpImpl->GetCurrentFilter();
400 // ------------------------------------------------------------------------
401 BOOL SdExportFileDialog::IsExportSelection() const
403 return mpImpl->SelectionBoxState();
407 // --------------------------------------------------------------------
408 // ----------- SdOpenSoundFileDialog -----------------------
409 // --------------------------------------------------------------------
411 // these are simple forwarders
412 SdOpenSoundFileDialog::SdOpenSoundFileDialog() :
413 mpImpl(
414 new SdFileDialog_Imp(
415 css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY, sal_False ) )
417 String aDescr;
418 aDescr = String(SdResId(STR_ALL_FILES));
419 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.*" ) ) );
421 // setup filter
422 #if defined UNX
423 aDescr = String(SdResId(STR_AU_FILE));
424 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.au;*.snd" ) ) );
425 aDescr = String(SdResId(STR_VOC_FILE));
426 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.voc" ) ) );
427 aDescr = String(SdResId(STR_WAV_FILE));
428 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.wav" ) ) );
429 aDescr = String(SdResId(STR_AIFF_FILE));
430 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.aiff" ) ) );
431 aDescr = String(SdResId(STR_SVX_FILE));
432 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.svx" ) ) );
433 #else
434 aDescr = String(SdResId(STR_WAV_FILE));
435 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.wav;*.mp3;*.ogg" ) ) );
436 aDescr = String(SdResId(STR_MIDI_FILE));
437 mpImpl->AddFilter( aDescr, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.mid" ) ) );
438 #endif
441 // ------------------------------------------------------------------------
442 SdOpenSoundFileDialog::~SdOpenSoundFileDialog()
446 // ------------------------------------------------------------------------
447 ErrCode SdOpenSoundFileDialog::Execute()
449 return mpImpl->Execute();
452 // ------------------------------------------------------------------------
453 String SdOpenSoundFileDialog::GetPath() const
455 return mpImpl->GetPath();
458 // ------------------------------------------------------------------------
459 void SdOpenSoundFileDialog::SetPath( const String& rPath )
461 mpImpl->SetDisplayDirectory( rPath );