1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_fpicker.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //------------------------------------------------------------------------
32*cdf0e10cSrcweir // includes
33*cdf0e10cSrcweir //------------------------------------------------------------------------
34*cdf0e10cSrcweir #include "filepickerstate.hxx"
35*cdf0e10cSrcweir #include <osl/diagnose.h>
36*cdf0e10cSrcweir #include "controlaccess.hxx"
37*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ListBoxControlActions.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ControlActions.hpp>
41*cdf0e10cSrcweir #include "controlcommandrequest.hxx"
42*cdf0e10cSrcweir #include "controlcommandresult.hxx"
43*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
44*cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
45*cdf0e10cSrcweir #include <osl/file.hxx>
46*cdf0e10cSrcweir #include "FileOpenDlg.hxx"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <memory>
49*cdf0e10cSrcweir #include "..\misc\WinImplHelper.hxx"
50*cdf0e10cSrcweir //---------------------------------------------
51*cdf0e10cSrcweir //
52*cdf0e10cSrcweir //---------------------------------------------
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir using rtl::OUString;
55*cdf0e10cSrcweir using com::sun::star::uno::Any;
56*cdf0e10cSrcweir using com::sun::star::uno::Sequence;
57*cdf0e10cSrcweir using com::sun::star::uno::Reference;
58*cdf0e10cSrcweir using com::sun::star::uno::XInterface;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
61*cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
62*cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs::ListboxControlActions;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir //---------------------------------------------
65*cdf0e10cSrcweir //
66*cdf0e10cSrcweir //---------------------------------------------
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir const sal_Int32 MAX_LABEL = 256;
69*cdf0e10cSrcweir const sal_Int16 LISTBOX_LABEL_OFFSET = 100;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir //---------------------------------------------
72*cdf0e10cSrcweir // declaration
73*cdf0e10cSrcweir //---------------------------------------------
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir CFilePickerState::~CFilePickerState( )
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir //---------------------------------------------
80*cdf0e10cSrcweir //
81*cdf0e10cSrcweir //---------------------------------------------
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir CNonExecuteFilePickerState::CNonExecuteFilePickerState( ) :
84*cdf0e10cSrcweir     m_FirstControlCommand( NULL )
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir //---------------------------------------------
89*cdf0e10cSrcweir //
90*cdf0e10cSrcweir //---------------------------------------------
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir CNonExecuteFilePickerState::~CNonExecuteFilePickerState( )
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir     reset( );
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir //---------------------------------------------
98*cdf0e10cSrcweir //
99*cdf0e10cSrcweir //---------------------------------------------
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir void SAL_CALL CNonExecuteFilePickerState::setValue( sal_Int16 aControlId, sal_Int16 aControlAction, const Any& aValue )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir     CValueControlCommand* value_command = new CValueControlCommand(
104*cdf0e10cSrcweir         aControlId, aControlAction, aValue );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     addControlCommand( value_command );
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir //---------------------------------------------
110*cdf0e10cSrcweir //
111*cdf0e10cSrcweir //---------------------------------------------
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir Any SAL_CALL CNonExecuteFilePickerState::getValue( sal_Int16 aControlId, sal_Int16 aControlAction )
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir     CValueControlCommandRequest value_request( aControlId, aControlAction );
116*cdf0e10cSrcweir     Any aAny;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	if (m_FirstControlCommand)
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		// pass the request along the command-chain
121*cdf0e10cSrcweir 		std::auto_ptr< CControlCommandResult > result( m_FirstControlCommand->handleRequest( &value_request ) );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 		OSL_ENSURE( result.get(), "invalid getValue request" );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 		if ( result.get() )
126*cdf0e10cSrcweir 		{
127*cdf0e10cSrcweir 			// #101753 must remove assertion else running into deadlock
128*cdf0e10cSrcweir 			// because getValue may be called asynchronously from main thread
129*cdf0e10cSrcweir 			// with locked SOLAR_MUTEX but we also need SOLAR_MUTEX in
130*cdf0e10cSrcweir 			// WinFileOpenDialog::onInitDone ... but we cannot dismiss the
131*cdf0e10cSrcweir 			// assertion dialog because at this point the FileOpen Dialog
132*cdf0e10cSrcweir 			// has aleady the focus but is not yet visible :-(
133*cdf0e10cSrcweir 			// The real cure is to remove the VCL/SOLAR_MUTEX dependency
134*cdf0e10cSrcweir 			// cause by the use of our resource manager and not being able to
135*cdf0e10cSrcweir 			// generate native windows resources
136*cdf0e10cSrcweir 			//OSL_ENSURE( result->hasResult( ), "invalid getValue request" );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 			if ( result->hasResult( ) )
139*cdf0e10cSrcweir 			{
140*cdf0e10cSrcweir 				CValueCommandResult* value_result = dynamic_cast< CValueCommandResult* >( result.get( ) );
141*cdf0e10cSrcweir 				OSL_ENSURE( value_result, "should have be a CValueCommandResult" );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 				aAny = value_result->getValue( );
144*cdf0e10cSrcweir 				OSL_ENSURE( aAny.hasValue( ), "empty any" );
145*cdf0e10cSrcweir 			}
146*cdf0e10cSrcweir 		}
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     return aAny;
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir //---------------------------------------------
153*cdf0e10cSrcweir //
154*cdf0e10cSrcweir //---------------------------------------------
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir void SAL_CALL CNonExecuteFilePickerState::enableControl( sal_Int16 aControlId, sal_Bool bEnable )
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir     CEnableControlCommand* enable_command = new CEnableControlCommand(
159*cdf0e10cSrcweir         aControlId, bEnable );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     addControlCommand( enable_command );
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir //---------------------------------------------
165*cdf0e10cSrcweir //
166*cdf0e10cSrcweir //---------------------------------------------
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir void SAL_CALL CNonExecuteFilePickerState::setLabel( sal_Int16 aControlId, const ::rtl::OUString& aLabel )
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir     CLabelControlCommand* label_command = new CLabelControlCommand(
171*cdf0e10cSrcweir         aControlId, aLabel );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     addControlCommand( label_command );
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir //---------------------------------------------
177*cdf0e10cSrcweir //
178*cdf0e10cSrcweir //---------------------------------------------
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir OUString SAL_CALL CNonExecuteFilePickerState::getLabel( sal_Int16 aControlId )
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir     CControlCommandRequest label_request( aControlId );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     // pass the request along the command-chain
185*cdf0e10cSrcweir     std::auto_ptr< CControlCommandResult > result( m_FirstControlCommand->handleRequest( &label_request ) );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     OSL_ENSURE( result->hasResult( ), "invalid getValue request" );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     OUString aLabel;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     if ( result->hasResult( ) )
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         CLabelCommandResult* label_result = dynamic_cast< CLabelCommandResult* >( result.get( ) );
194*cdf0e10cSrcweir         OSL_ENSURE( label_result, "should have be a CLabelCommandResult" );
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         aLabel = label_result->getLabel( );
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     return aLabel;
200*cdf0e10cSrcweir }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir /*  #i26224#
203*cdf0e10cSrcweir     When typing file names with drive letter but without '\'
204*cdf0e10cSrcweir     in the "File name" box of the FileOpen dialog the FileOpen
205*cdf0e10cSrcweir     dialog makes strange paths out of them e.g. "d:.\test.sxw".
206*cdf0e10cSrcweir     Such file names will not be accepted by sal so we fix these
207*cdf0e10cSrcweir     somehow broken paths here. */
208*cdf0e10cSrcweir OUString MatchFixBrokenPath(const OUString& path)
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir     OSL_ASSERT(path.getLength() >= 4);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     if (path[1] == ':' && path[2] == '.' && path[3] == '\\')
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir         // skip the '.'
215*cdf0e10cSrcweir         return OUString(path, 2) + path.copy(3, path.getLength() - 3);
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir     return path;
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir //-----------------------------------------------------------------------------------------
221*cdf0e10cSrcweir //
222*cdf0e10cSrcweir //-----------------------------------------------------------------------------------------
223*cdf0e10cSrcweir static ::rtl::OUString trimTrailingSpaces(const ::rtl::OUString& rString)
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     rtl::OUString aResult(rString);
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     sal_Int32 nIndex = rString.lastIndexOf(' ');
228*cdf0e10cSrcweir     if (nIndex == rString.getLength()-1)
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir         while (nIndex >= 0 && rString[nIndex] == ' ')
231*cdf0e10cSrcweir             nIndex--;
232*cdf0e10cSrcweir         if (nIndex >= 0)
233*cdf0e10cSrcweir             aResult = rString.copy(0,nIndex+1);
234*cdf0e10cSrcweir         else
235*cdf0e10cSrcweir             aResult = ::rtl::OUString();
236*cdf0e10cSrcweir     }
237*cdf0e10cSrcweir     return aResult;
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir Sequence< OUString > SAL_CALL CNonExecuteFilePickerState::getFiles( CFileOpenDialog* aFileOpenDialog )
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir     OSL_PRECOND( aFileOpenDialog, "invalid parameter" );
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     Sequence< OUString > aFilePathList;
245*cdf0e10cSrcweir     OUString aFilePathURL;
246*cdf0e10cSrcweir     OUString aFilePath;
247*cdf0e10cSrcweir     ::osl::FileBase::RC rc;
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     aFilePath = aFileOpenDialog->getFullFileName( );
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     if ( aFilePath.getLength( ) )
252*cdf0e10cSrcweir     {
253*cdf0e10cSrcweir         // tokenize the returned string and copy the
254*cdf0e10cSrcweir         // sub-strings separately into a sequence
255*cdf0e10cSrcweir         const sal_Unicode* pTemp = aFilePath.getStr();
256*cdf0e10cSrcweir         const sal_Unicode* pStrEnd = pTemp + aFilePath.getLength();
257*cdf0e10cSrcweir         sal_uInt32 lSubStr;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         while (pTemp < pStrEnd)
260*cdf0e10cSrcweir         {
261*cdf0e10cSrcweir             // detect the length of the next sub string
262*cdf0e10cSrcweir             lSubStr = rtl_ustr_getLength(pTemp);
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir             aFilePathList.realloc(aFilePathList.getLength() + 1);
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir             aFilePathList[aFilePathList.getLength() - 1] =
267*cdf0e10cSrcweir                 MatchFixBrokenPath(OUString(pTemp, lSubStr));
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir             pTemp += (lSubStr + 1);
270*cdf0e10cSrcweir         }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir         // change all entries to file URLs
273*cdf0e10cSrcweir         sal_Int32 lenFileList = aFilePathList.getLength( );
274*cdf0e10cSrcweir 		OSL_ASSERT( lenFileList >= 1 );
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < lenFileList; i++ )
277*cdf0e10cSrcweir         {
278*cdf0e10cSrcweir 			aFilePath = trimTrailingSpaces(aFilePathList[i]);
279*cdf0e10cSrcweir 			rc = ::osl::FileBase::getFileURLFromSystemPath(
280*cdf0e10cSrcweir                 aFilePath, aFilePathURL );
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir             // we do return all or nothing, that means
283*cdf0e10cSrcweir             // in case of failures we destroy the sequence
284*cdf0e10cSrcweir             // and return an empty sequence
285*cdf0e10cSrcweir             if ( rc != ::osl::FileBase::E_None )
286*cdf0e10cSrcweir             {
287*cdf0e10cSrcweir                 aFilePathList.realloc( 0 );
288*cdf0e10cSrcweir                 break;
289*cdf0e10cSrcweir             }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir             aFilePathList[i] = aFilePathURL;
292*cdf0e10cSrcweir         }
293*cdf0e10cSrcweir     }
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir     return aFilePathList;
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir //---------------------------------------------
299*cdf0e10cSrcweir //
300*cdf0e10cSrcweir //---------------------------------------------
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir OUString SAL_CALL CNonExecuteFilePickerState::getDisplayDirectory( CFileOpenDialog* aFileOpenDialog )
303*cdf0e10cSrcweir {
304*cdf0e10cSrcweir     OSL_PRECOND( aFileOpenDialog, "invalid parameter" );
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     OUString pathURL;
307*cdf0e10cSrcweir     OUString displayDir;
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir     displayDir = aFileOpenDialog->getLastDisplayDirectory( );
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir 	if ( displayDir.getLength( ) )
312*cdf0e10cSrcweir         ::osl::FileBase::getFileURLFromSystemPath( displayDir, pathURL );
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir 	return pathURL;
315*cdf0e10cSrcweir }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir //---------------------------------------------
318*cdf0e10cSrcweir //
319*cdf0e10cSrcweir //---------------------------------------------
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir void SAL_CALL CNonExecuteFilePickerState::reset( )
322*cdf0e10cSrcweir {
323*cdf0e10cSrcweir     CControlCommand* nextCommand;
324*cdf0e10cSrcweir     CControlCommand* currentCommand = m_FirstControlCommand;
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir     while( currentCommand )
327*cdf0e10cSrcweir     {
328*cdf0e10cSrcweir         nextCommand = currentCommand->getNextCommand( );
329*cdf0e10cSrcweir         delete currentCommand;
330*cdf0e10cSrcweir         currentCommand = nextCommand;
331*cdf0e10cSrcweir     }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir     m_FirstControlCommand = NULL;
334*cdf0e10cSrcweir }
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir //---------------------------------------------
337*cdf0e10cSrcweir //
338*cdf0e10cSrcweir //---------------------------------------------
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir CControlCommand* SAL_CALL CNonExecuteFilePickerState::getControlCommand( ) const
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir     return m_FirstControlCommand;
343*cdf0e10cSrcweir }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir //---------------------------------------------
346*cdf0e10cSrcweir // we append new control commands to the end
347*cdf0e10cSrcweir // of the list so that we are sure the commands
348*cdf0e10cSrcweir // will be executed as the client issued it
349*cdf0e10cSrcweir //---------------------------------------------
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir void SAL_CALL CNonExecuteFilePickerState::addControlCommand( CControlCommand* aControlCommand )
352*cdf0e10cSrcweir {
353*cdf0e10cSrcweir     OSL_ASSERT( aControlCommand );
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir     if ( NULL == m_FirstControlCommand )
356*cdf0e10cSrcweir     {
357*cdf0e10cSrcweir         m_FirstControlCommand = aControlCommand;
358*cdf0e10cSrcweir     }
359*cdf0e10cSrcweir     else
360*cdf0e10cSrcweir     {
361*cdf0e10cSrcweir         CControlCommand* pNextControlCommand = m_FirstControlCommand;
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir         while ( pNextControlCommand->getNextCommand( ) != NULL )
364*cdf0e10cSrcweir             pNextControlCommand = pNextControlCommand->getNextCommand( );
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir         pNextControlCommand->setNextCommand( aControlCommand );
367*cdf0e10cSrcweir     }
368*cdf0e10cSrcweir }
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir //#######################################################################
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir //---------------------------------------------
373*cdf0e10cSrcweir //
374*cdf0e10cSrcweir //---------------------------------------------
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir CExecuteFilePickerState::CExecuteFilePickerState( HWND hwndDlg ) :
377*cdf0e10cSrcweir     m_hwndDlg( hwndDlg )
378*cdf0e10cSrcweir {
379*cdf0e10cSrcweir }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir //---------------------------------------------
382*cdf0e10cSrcweir //
383*cdf0e10cSrcweir //---------------------------------------------
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir void SAL_CALL CExecuteFilePickerState::setValue( sal_Int16 aControlId, sal_Int16 aControlAction, const Any& aValue )
386*cdf0e10cSrcweir {
387*cdf0e10cSrcweir     // we do not support SET_HELP_URL/GET_HELP_URL
388*cdf0e10cSrcweir     if ( com::sun::star::ui::dialogs::ControlActions::SET_HELP_URL == aControlAction )
389*cdf0e10cSrcweir         return;
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir     HWND hwndCtrl = GetHwndDlgItem( aControlId );
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir 	// the filter listbox can be manipulated via this
394*cdf0e10cSrcweir 	// method the caller should use XFilterManager
395*cdf0e10cSrcweir     if ( !hwndCtrl || (aControlId == LISTBOX_FILTER) )
396*cdf0e10cSrcweir     {
397*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "invalid control id" );
398*cdf0e10cSrcweir         return;
399*cdf0e10cSrcweir     }
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir     CTRL_CLASS aCtrlClass = GetCtrlClass( hwndCtrl );
402*cdf0e10cSrcweir     if ( UNKNOWN == aCtrlClass )
403*cdf0e10cSrcweir     {
404*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "unsupported control class" );
405*cdf0e10cSrcweir         return;
406*cdf0e10cSrcweir     }
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     CTRL_SETVALUE_FUNCTION_T lpfnSetValue =
409*cdf0e10cSrcweir         GetCtrlSetValueFunction( aCtrlClass, aControlAction );
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir     if ( !lpfnSetValue )
412*cdf0e10cSrcweir     {
413*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "unsupported control action" );
414*cdf0e10cSrcweir         return;
415*cdf0e10cSrcweir     }
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir     // the function that we call should throw an IllegalArgumentException if
418*cdf0e10cSrcweir     // the given value is invalid or empty, that's why we provide a Reference
419*cdf0e10cSrcweir     // to an XInterface and a argument position
420*cdf0e10cSrcweir     lpfnSetValue( hwndCtrl, aValue, Reference< XInterface >( ), 3 );
421*cdf0e10cSrcweir }
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir //---------------------------------------------
424*cdf0e10cSrcweir //
425*cdf0e10cSrcweir //---------------------------------------------
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir Any SAL_CALL CExecuteFilePickerState::getValue( sal_Int16 aControlId, sal_Int16 aControlAction )
428*cdf0e10cSrcweir {
429*cdf0e10cSrcweir     // we do not support SET_HELP_URL/GET_HELP_URL
430*cdf0e10cSrcweir     if ( com::sun::star::ui::dialogs::ControlActions::GET_HELP_URL == aControlAction )
431*cdf0e10cSrcweir         return Any( );
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir     HWND hwndCtrl = GetHwndDlgItem( aControlId );
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir 	// the filter listbox can be manipulated via this
436*cdf0e10cSrcweir 	// method the caller should use XFilterManager
437*cdf0e10cSrcweir     if ( !hwndCtrl || (aControlId == LISTBOX_FILTER) )
438*cdf0e10cSrcweir     {
439*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "invalid control id" );
440*cdf0e10cSrcweir         return Any( );
441*cdf0e10cSrcweir     }
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir     CTRL_CLASS aCtrlClass = GetCtrlClass( hwndCtrl );
444*cdf0e10cSrcweir     if ( UNKNOWN == aCtrlClass )
445*cdf0e10cSrcweir     {
446*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "unsupported control class" );
447*cdf0e10cSrcweir         return Any( );
448*cdf0e10cSrcweir     }
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir     CTRL_GETVALUE_FUNCTION_T lpfnGetValue =
451*cdf0e10cSrcweir         GetCtrlGetValueFunction( aCtrlClass, aControlAction );
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir     if ( !lpfnGetValue )
454*cdf0e10cSrcweir     {
455*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "unsupported control action" );
456*cdf0e10cSrcweir         return Any( );
457*cdf0e10cSrcweir     }
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir     return lpfnGetValue( hwndCtrl );
460*cdf0e10cSrcweir }
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir //---------------------------------------------
463*cdf0e10cSrcweir //
464*cdf0e10cSrcweir //---------------------------------------------
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir void SAL_CALL CExecuteFilePickerState::enableControl( sal_Int16 aControlId, sal_Bool bEnable )
467*cdf0e10cSrcweir {
468*cdf0e10cSrcweir     HWND hwndCtrl = GetHwndDlgItem( aControlId );
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir     OSL_ENSURE( IsWindow( hwndCtrl ), "invalid element id");
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir     EnableWindow( hwndCtrl, bEnable );
473*cdf0e10cSrcweir }
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir //---------------------------------------------
476*cdf0e10cSrcweir //
477*cdf0e10cSrcweir //---------------------------------------------
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir void SAL_CALL CExecuteFilePickerState::setLabel( sal_Int16 aControlId, const OUString& aLabel )
480*cdf0e10cSrcweir {
481*cdf0e10cSrcweir     HWND hwndCtrl = GetHwndDlgItem( aControlId );
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir     OSL_ENSURE( IsWindow( hwndCtrl ), "invalid element id");
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir     if ( IsListboxControl( hwndCtrl ) )
486*cdf0e10cSrcweir         hwndCtrl = GetListboxLabelItem( aControlId );
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir     OUString aWinLabel = SOfficeToWindowsLabel( aLabel );
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir 	// somewhat risky because we don't know if OUString
491*cdf0e10cSrcweir 	// has a terminating '\0'
492*cdf0e10cSrcweir     SetWindowText( hwndCtrl, reinterpret_cast<LPCTSTR>(aWinLabel.getStr( )) );
493*cdf0e10cSrcweir }
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir //---------------------------------------------
496*cdf0e10cSrcweir //
497*cdf0e10cSrcweir //---------------------------------------------
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir OUString SAL_CALL CExecuteFilePickerState::getLabel( sal_Int16 aControlId )
500*cdf0e10cSrcweir {
501*cdf0e10cSrcweir     HWND hwndCtrl = GetHwndDlgItem( aControlId );
502*cdf0e10cSrcweir 
503*cdf0e10cSrcweir     OSL_ENSURE( IsWindow( hwndCtrl ), "invalid element id");
504*cdf0e10cSrcweir 
505*cdf0e10cSrcweir     if ( IsListboxControl( hwndCtrl ) )
506*cdf0e10cSrcweir         hwndCtrl = GetListboxLabelItem( aControlId );
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir     sal_Unicode aLabel[MAX_LABEL];
509*cdf0e10cSrcweir     int nRet = GetWindowText( hwndCtrl, reinterpret_cast<LPTSTR>(aLabel), MAX_LABEL );
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir     OUString ctrlLabel;
512*cdf0e10cSrcweir     if ( nRet )
513*cdf0e10cSrcweir     {
514*cdf0e10cSrcweir         ctrlLabel = OUString( aLabel, rtl_ustr_getLength( aLabel ) );
515*cdf0e10cSrcweir         ctrlLabel = WindowsToSOfficeLabel( aLabel );
516*cdf0e10cSrcweir     }
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir     return ctrlLabel;
519*cdf0e10cSrcweir }
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir //---------------------------------------------
522*cdf0e10cSrcweir //
523*cdf0e10cSrcweir //---------------------------------------------
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir Sequence< OUString > SAL_CALL CExecuteFilePickerState::getFiles( CFileOpenDialog* aFileOpenDialog )
526*cdf0e10cSrcweir {
527*cdf0e10cSrcweir     OSL_POSTCOND( aFileOpenDialog, "invalid parameter" );
528*cdf0e10cSrcweir 
529*cdf0e10cSrcweir     Sequence< OUString > aFilePathList;
530*cdf0e10cSrcweir     OUString aFilePathURL;
531*cdf0e10cSrcweir     OUString aFilePath;
532*cdf0e10cSrcweir     ::osl::FileBase::RC rc;
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir 	// in execution mode getFullFileName doesn't
535*cdf0e10cSrcweir 	// return anything, so we must use another way
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir     // returns the currently selected file(s)
538*cdf0e10cSrcweir     // including path information
539*cdf0e10cSrcweir     aFilePath = aFileOpenDialog->getCurrentFilePath( );
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir 	// if multiple files are selected or the user
542*cdf0e10cSrcweir 	// typed anything that doesn't seem to be a valid
543*cdf0e10cSrcweir 	// file name getFileURLFromSystemPath fails
544*cdf0e10cSrcweir 	// and we return an empty file list
545*cdf0e10cSrcweir     rc = ::osl::FileBase::getFileURLFromSystemPath(
546*cdf0e10cSrcweir         aFilePath, aFilePathURL );
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir     if ( ::osl::FileBase::E_None == rc )
549*cdf0e10cSrcweir     {
550*cdf0e10cSrcweir         aFilePathList.realloc( 1 );
551*cdf0e10cSrcweir         aFilePathList[0] = aFilePathURL;
552*cdf0e10cSrcweir     }
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir     return aFilePathList;
555*cdf0e10cSrcweir }
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir //---------------------------------------------
558*cdf0e10cSrcweir //
559*cdf0e10cSrcweir //---------------------------------------------
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir OUString SAL_CALL CExecuteFilePickerState::getDisplayDirectory( CFileOpenDialog* aFileOpenDialog )
562*cdf0e10cSrcweir {
563*cdf0e10cSrcweir     OSL_POSTCOND( aFileOpenDialog, "invalid parameter" );
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir     OUString pathURL;
566*cdf0e10cSrcweir     OUString displayDir;
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir     displayDir = aFileOpenDialog->getCurrentFolderPath( );
569*cdf0e10cSrcweir 
570*cdf0e10cSrcweir     if ( displayDir.getLength( ) )
571*cdf0e10cSrcweir         ::osl::FileBase::getFileURLFromSystemPath( displayDir, pathURL );
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir 	return pathURL;
574*cdf0e10cSrcweir }
575*cdf0e10cSrcweir 
576*cdf0e10cSrcweir //---------------------------------------------
577*cdf0e10cSrcweir //
578*cdf0e10cSrcweir //---------------------------------------------
579*cdf0e10cSrcweir 
580*cdf0e10cSrcweir void SAL_CALL CExecuteFilePickerState::initFilePickerControls( CControlCommand* firstControlCommand )
581*cdf0e10cSrcweir {
582*cdf0e10cSrcweir     CControlCommand* aControlCommand = firstControlCommand;
583*cdf0e10cSrcweir 
584*cdf0e10cSrcweir     while ( aControlCommand )
585*cdf0e10cSrcweir     {
586*cdf0e10cSrcweir         aControlCommand->exec( this );
587*cdf0e10cSrcweir         aControlCommand = aControlCommand->getNextCommand( );
588*cdf0e10cSrcweir     }
589*cdf0e10cSrcweir }
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir //---------------------------------------------
592*cdf0e10cSrcweir //
593*cdf0e10cSrcweir //---------------------------------------------
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir void SAL_CALL CExecuteFilePickerState::cacheControlState( HWND hwndControl, CFilePickerState* aNonExecFilePickerState )
596*cdf0e10cSrcweir {
597*cdf0e10cSrcweir     OSL_ENSURE( hwndControl && aNonExecFilePickerState, "invalid parameters" );
598*cdf0e10cSrcweir 
599*cdf0e10cSrcweir     CTRL_CLASS aCtrlClass = GetCtrlClass( hwndControl );
600*cdf0e10cSrcweir 
601*cdf0e10cSrcweir     sal_Int16 aControlAction;
602*cdf0e10cSrcweir     CTRL_GETVALUE_FUNCTION_T lpfnGetValue;
603*cdf0e10cSrcweir 
604*cdf0e10cSrcweir     if ( CHECKBOX == aCtrlClass )
605*cdf0e10cSrcweir     {
606*cdf0e10cSrcweir         aControlAction = 0;
607*cdf0e10cSrcweir 
608*cdf0e10cSrcweir         lpfnGetValue = GetCtrlGetValueFunction( aCtrlClass, aControlAction );
609*cdf0e10cSrcweir 
610*cdf0e10cSrcweir         OSL_ASSERT( lpfnGetValue );
611*cdf0e10cSrcweir 
612*cdf0e10cSrcweir         aNonExecFilePickerState->setValue(
613*cdf0e10cSrcweir             sal::static_int_cast< sal_Int16 >( GetDlgCtrlID( hwndControl ) ),
614*cdf0e10cSrcweir             aControlAction,
615*cdf0e10cSrcweir             lpfnGetValue( hwndControl ) );
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir         aNonExecFilePickerState->setLabel(
618*cdf0e10cSrcweir             sal::static_int_cast< sal_Int16 >( GetDlgCtrlID( hwndControl ) ),
619*cdf0e10cSrcweir             getLabel(
620*cdf0e10cSrcweir                 sal::static_int_cast< sal_Int16 >(
621*cdf0e10cSrcweir                     GetDlgCtrlID( hwndControl ) ) ) );
622*cdf0e10cSrcweir     }
623*cdf0e10cSrcweir     else if ( LISTBOX == aCtrlClass )
624*cdf0e10cSrcweir     {
625*cdf0e10cSrcweir         // for listboxes we save only the
626*cdf0e10cSrcweir         // last selected item and the last
627*cdf0e10cSrcweir         // selected item index
628*cdf0e10cSrcweir 
629*cdf0e10cSrcweir         aControlAction = GET_SELECTED_ITEM;
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir         lpfnGetValue = GetCtrlGetValueFunction( aCtrlClass, aControlAction );
632*cdf0e10cSrcweir 
633*cdf0e10cSrcweir         aNonExecFilePickerState->setValue(
634*cdf0e10cSrcweir             sal::static_int_cast< sal_Int16 >( GetDlgCtrlID( hwndControl ) ),
635*cdf0e10cSrcweir             aControlAction,
636*cdf0e10cSrcweir             lpfnGetValue( hwndControl ) );
637*cdf0e10cSrcweir 
638*cdf0e10cSrcweir         aControlAction = ::com::sun::star::ui::dialogs::ControlActions::GET_SELECTED_ITEM_INDEX;
639*cdf0e10cSrcweir 
640*cdf0e10cSrcweir         lpfnGetValue = GetCtrlGetValueFunction( aCtrlClass, aControlAction );
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir         aNonExecFilePickerState->setValue(
643*cdf0e10cSrcweir             sal::static_int_cast< sal_Int16 >( GetDlgCtrlID( hwndControl ) ),
644*cdf0e10cSrcweir             aControlAction,
645*cdf0e10cSrcweir             lpfnGetValue( hwndControl ) );
646*cdf0e10cSrcweir     }
647*cdf0e10cSrcweir }
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir //---------------------------------------------
650*cdf0e10cSrcweir //
651*cdf0e10cSrcweir //---------------------------------------------
652*cdf0e10cSrcweir 
653*cdf0e10cSrcweir void SAL_CALL CExecuteFilePickerState::setHwnd( HWND hwndDlg )
654*cdf0e10cSrcweir {
655*cdf0e10cSrcweir     m_hwndDlg = hwndDlg;
656*cdf0e10cSrcweir }
657*cdf0e10cSrcweir 
658*cdf0e10cSrcweir //---------------------------------------------
659*cdf0e10cSrcweir //
660*cdf0e10cSrcweir //---------------------------------------------
661*cdf0e10cSrcweir 
662*cdf0e10cSrcweir inline sal_Bool SAL_CALL CExecuteFilePickerState::IsListboxControl( HWND hwndControl ) const
663*cdf0e10cSrcweir {
664*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( hwndControl ), "invalid parameter" );
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir     CTRL_CLASS aCtrlClass = GetCtrlClass( hwndControl );
667*cdf0e10cSrcweir     return ( LISTBOX == aCtrlClass );
668*cdf0e10cSrcweir }
669*cdf0e10cSrcweir 
670*cdf0e10cSrcweir //---------------------------------------------
671*cdf0e10cSrcweir // because listboxes (comboboxes) and their labels
672*cdf0e10cSrcweir // are seperated we have to translate the listbox
673*cdf0e10cSrcweir // id to their corresponding label id
674*cdf0e10cSrcweir // the convention is that the label id of a listbox
675*cdf0e10cSrcweir // is the id of the listbox + 100
676*cdf0e10cSrcweir //---------------------------------------------
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir inline sal_Int16 SAL_CALL CExecuteFilePickerState::ListboxIdToListboxLabelId( sal_Int16 aListboxId ) const
679*cdf0e10cSrcweir {
680*cdf0e10cSrcweir     return ( aListboxId + LISTBOX_LABEL_OFFSET );
681*cdf0e10cSrcweir }
682*cdf0e10cSrcweir 
683*cdf0e10cSrcweir //---------------------------------------------
684*cdf0e10cSrcweir //
685*cdf0e10cSrcweir //---------------------------------------------
686*cdf0e10cSrcweir 
687*cdf0e10cSrcweir inline HWND SAL_CALL CExecuteFilePickerState::GetListboxLabelItem( sal_Int16 aControlId ) const
688*cdf0e10cSrcweir {
689*cdf0e10cSrcweir     sal_Int16 aLabelId = ListboxIdToListboxLabelId( aControlId );
690*cdf0e10cSrcweir     HWND hwndCtrl = GetHwndDlgItem( aLabelId );
691*cdf0e10cSrcweir 
692*cdf0e10cSrcweir     OSL_ASSERT( IsWindow( hwndCtrl ) );
693*cdf0e10cSrcweir 
694*cdf0e10cSrcweir     return hwndCtrl;
695*cdf0e10cSrcweir }
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir //---------------------------------------------
698*cdf0e10cSrcweir //
699*cdf0e10cSrcweir //---------------------------------------------
700*cdf0e10cSrcweir 
701*cdf0e10cSrcweir HWND SAL_CALL CExecuteFilePickerState::GetHwndDlgItem( sal_Int16 aControlId, sal_Bool bIncludeStdCtrls ) const
702*cdf0e10cSrcweir {
703*cdf0e10cSrcweir     OSL_ENSURE( IsWindow( m_hwndDlg ), "no valid parent window set before" );
704*cdf0e10cSrcweir 
705*cdf0e10cSrcweir     HWND hwndCtrl = GetDlgItem( m_hwndDlg, aControlId );
706*cdf0e10cSrcweir 
707*cdf0e10cSrcweir     // maybe it's a control of the dialog itself for instance
708*cdf0e10cSrcweir     // the ok and cancel button
709*cdf0e10cSrcweir     if ( !hwndCtrl && bIncludeStdCtrls )
710*cdf0e10cSrcweir     {
711*cdf0e10cSrcweir 		hwndCtrl = GetDlgItem(
712*cdf0e10cSrcweir             GetParent( m_hwndDlg ),
713*cdf0e10cSrcweir             CommonFilePickerCtrlIdToWinFileOpenCtrlId( aControlId ) );
714*cdf0e10cSrcweir     }
715*cdf0e10cSrcweir 
716*cdf0e10cSrcweir 	return hwndCtrl;
717*cdf0e10cSrcweir }
718