1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/lang/XComponent.hpp>
21 #include <com/sun/star/registry/XSimpleRegistry.hpp>
22 #include <osl/file.hxx>
24 #include <cppuhelper/servicefactory.hxx>
25 #include <rtl/ustring.hxx>
26 #include <sal/types.h>
27 #include <osl/diagnose.h>
28 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
29 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
31 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
32 #include <cppuhelper/implbase.hxx>
33 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
34 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
35 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
36 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
38 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
39 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
40 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
42 #include <osl/thread.h>
45 #if !defined WIN32_LEAN_AND_MEAN
46 # define WIN32_LEAN_AND_MEAN
50 #include "..\FPServiceInfo.hxx"
56 using namespace ::cppu
;
57 using namespace ::com::sun::star::uno
;
58 using namespace ::com::sun::star::lang
;
59 using namespace ::com::sun::star::ui::dialogs
;
60 using namespace ::com::sun::star::ui::dialogs::TemplateDescription
;
62 using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds
;
63 using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds
;
64 using namespace ::com::sun::star::ui::dialogs::ListboxControlActions
;
70 void TestFilterManager( Reference
< XFilePicker
> xFilePicker
);
73 #define RDB_SYSPATH "D:\\Projects\\gsl\\sysui\\wntmsci7\\bin\\applicat.rdb"
79 Reference
< XMultiServiceFactory
> g_xFactory
;
81 static const OUStringLiteral
BMP_EXTENSION( u
"bmp" );
87 class FilePickerListener
: public WeakImplHelper
< XFilePickerListener
>
92 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
)
93 throw(css::uno::RuntimeException
);
95 // XFilePickerListener
96 virtual void SAL_CALL
fileSelectionChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
97 throw(css::uno::RuntimeException
);
99 virtual void SAL_CALL
directoryChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
100 throw(css::uno::RuntimeException
);
102 virtual OUString SAL_CALL
helpRequested( const css::ui::dialogs::FilePickerEvent
& aEvent
)
103 throw(css::uno::RuntimeException
);
105 virtual void SAL_CALL
controlStateChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
106 throw(css::uno::RuntimeException
);
108 virtual void SAL_CALL
dialogSizeChanged( )
109 throw (css::uno::RuntimeException
);
112 void SAL_CALL
FilePickerListener::disposing( const css::lang::EventObject
& Source
)
113 throw(css::uno::RuntimeException
)
117 void SAL_CALL
FilePickerListener::fileSelectionChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
118 throw(css::uno::RuntimeException
)
122 Reference
< XFilePicker
> rXFilePicker( aEvent
.Source
, UNO_QUERY
);
123 Reference
< XFilePreview
> rXFilePreview( rXFilePicker
, UNO_QUERY
);
125 if ( !rXFilePreview
.is( ) )
128 Sequence
< OUString
> aFileList
= rXFilePicker
->getFiles( );
129 if ( 1 == aFileList
.getLength( ) )
131 OUString FilePath
= aFileList
[0];
133 // detect file extension
134 sal_Int32 nIndex
= FilePath
.lastIndexOf( BMP_EXTENSION
);
135 if ( (FilePath
.getLength( ) - 3) == nIndex
)
137 OUString FileSysPath
;
138 ::osl::FileBase::getSystemPathFromFileURL(
139 FilePath
, FileSysPath
);
141 HANDLE hFile
= CreateFileW(
142 FileSysPath
.getStr( ),
143 GENERIC_READ
, FILE_SHARE_READ
, NULL
,
144 OPEN_EXISTING
, FILE_FLAG_SEQUENTIAL_SCAN
, NULL
) ;
146 if (hFile
== INVALID_HANDLE_VALUE
)
150 DWORD dwFileSize
= GetFileSize (hFile
, &dwHighSize
) ;
154 CloseHandle (hFile
) ;
158 Sequence
< sal_Int8
> aDIB( dwFileSize
);
161 sal_Bool bSuccess
= ReadFile (hFile
, aDIB
.getArray( ), dwFileSize
, &dwBytesRead
, NULL
) ;
164 BITMAPFILEHEADER
* pbmfh
= (BITMAPFILEHEADER
*)aDIB
.getConstArray( );
165 if (!bSuccess
|| (dwBytesRead
!= dwFileSize
)
166 || (pbmfh
->bfType
!= * (WORD
*) "BM")
167 || (pbmfh
->bfSize
!= dwFileSize
))
175 rXFilePreview
->setImage( 1, aAny
);
179 catch( IllegalArgumentException
& )
184 void SAL_CALL
FilePickerListener::directoryChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
185 throw(css::uno::RuntimeException
)
187 Reference
< XFilePickerControlAccess
> rFilePickerCtrlAccess( aEvent
.Source
, UNO_QUERY
);
190 OUString SAL_CALL
FilePickerListener::helpRequested( const css::ui::dialogs::FilePickerEvent
& aEvent
)
191 throw(css::uno::RuntimeException
)
196 void SAL_CALL
FilePickerListener::controlStateChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
197 throw(css::uno::RuntimeException
)
201 Reference
< XFilePickerControlAccess
> rFPCtrlAccess( aEvent
.Source
, UNO_QUERY
);
205 OUString
lbString( L
"Ein Eintrag 1" );
207 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aValue
);
209 lbString
= L
"Ein Eintrag 2";
211 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aValue
);
213 lbString
= L
"Ein Eintrag 3";
215 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aValue
);
219 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, SET_SELECT_ITEM
, aValue
);
223 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, DELETE_ITEM
, aValue
);
230 void SAL_CALL
FilePickerListener::dialogSizeChanged( )
231 throw(css::uno::RuntimeException
)
239 int SAL_CALL
main(int nArgc
, char* Argv
[], char* Env
[] )
241 printf("Starting test of FPS-Service\n");
244 // get the global service-manager
247 // Get global factory for uno services.
248 Reference
< XMultiServiceFactory
> g_xFactory( createRegistryServiceFactory( RDB_SYSPATH
) );
250 // Print a message if an error occurred.
251 if ( g_xFactory
.is() == sal_False
)
253 OSL_FAIL("Can't create RegistryServiceFactory");
258 // try to get an Interface to a XFilePicker Service
261 Sequence
< Any
> arguments(1);
262 arguments
[0] = makeAny( FILEOPEN_READONLY_VERSION
);
264 Reference
< XFilePicker
> xFilePicker(
265 g_xFactory
->createInstanceWithArguments(
266 "com.sun.star.ui.dialogs.SystemFilePicker", arguments
), UNO_QUERY
);
268 // install a FilePicker notifier
269 Reference
< XFilePickerListener
> xFPListener(
270 static_cast< XFilePickerListener
* >( new FilePickerListener()), UNO_QUERY
);
272 Reference
< XFilePickerNotifier
> xFPNotifier( xFilePicker
, UNO_QUERY
);
273 if ( xFPNotifier
.is( ) )
274 xFPNotifier
->addFilePickerListener( xFPListener
);
276 xFilePicker
->setTitle( OUString("FileOpen Simple..."));
277 xFilePicker
->setMultiSelectionMode( sal_True
);
278 xFilePicker
->setDefaultName( OUString("d:\\test2.sxw"));
281 OUString aSysPath
= OStringToOUString( "d:\\ueaeoe", osl_getThreadTextEncoding( ) );
282 ::osl::FileBase::getFileURLFromSystemPath( aSysPath
, aDirURL
);
283 xFilePicker
->setDisplayDirectory( aDirURL
);
285 Reference
< XFilterManager
> xFilterMgr( xFilePicker
, UNO_QUERY
);
286 if ( xFilterMgr
.is( ) )
288 xFilterMgr
->appendFilter( L
"Alle", L
"*.*" );
289 xFilterMgr
->appendFilter( L
"BMP", L
"*.bmp" );
290 xFilterMgr
->appendFilter( L
"SDW", L
"*.sdw;*.sdc;*.sdi" );
291 xFilterMgr
->appendFilter( L
"SXW", L
"*.sxw;*.sxi" );
294 Reference
< XFilePickerControlAccess
> xFPControlAccess( xFilePicker
, UNO_QUERY
);
297 sal_Bool bChkState
= sal_False
;
299 aAny
.setValue( &bChkState
, cppu::UnoType
<sal_Bool
>::get());
300 xFPControlAccess
->setValue( CHECKBOX_AUTOEXTENSION
, 0, aAny
);
302 OUString
aVersion( L
"Version 1" );
304 xFPControlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aAny
);
305 xFPControlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aAny
);
306 xFPControlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aAny
);
308 xFilePicker
->execute( );
310 sal_Bool bCheckState
;
311 aAny
= xFPControlAccess
->getValue( CHECKBOX_AUTOEXTENSION
, 0 );
312 if ( aAny
.hasValue( ) )
313 bCheckState
= *reinterpret_cast< const sal_Bool
* >( aAny
.getValue( ) );
315 aAny
= xFPControlAccess
->getValue( CHECKBOX_READONLY
, 0 );
316 if ( aAny
.hasValue( ) )
317 bCheckState
= *reinterpret_cast< const sal_Bool
* >( aAny
.getValue( ) );
319 aAny
= xFPControlAccess
->getValue( LISTBOX_VERSION
, GET_SELECTED_ITEM
);
321 if ( aAny
.hasValue( ) )
324 aDirURL
= xFilePicker
->getDisplayDirectory( );
325 Sequence
< OUString
> aFileList
= xFilePicker
->getFiles( );
326 for ( int i
= 0; i
< aFileList
.getLength( ); i
++ )
328 OUString nextPath
= aFileList
[i
];
331 if ( xFPNotifier
.is( ) )
332 xFPNotifier
->removeFilePickerListener( xFPListener
);
338 // Cast factory to XComponent
339 Reference
< XComponent
> xComponent( g_xFactory
, UNO_QUERY
);
341 // Print a message if an error occurred.
342 if ( xComponent
.is() == sal_False
)
344 OSL_FAIL("Error shutting down");
347 // Dispose and clear factory
348 xComponent
->dispose();
351 printf("Test successful\n");
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */