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