xref: /AOO41X/main/fpicker/source/win32/folderpicker/MtaFop.hxx (revision 10ce801889b00990ebdbcb412f4f91e2a9e6f21d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _MTAFOP_HXX_
25 #define _MTAFOP_HXX_
26 
27 #include <sal/types.h>
28 #include <rtl/ustring.hxx>
29 #include <osl/mutex.hxx>
30 
31 #include <utility>
32 #ifdef __MINGW32__
33 #include <windows.h>
34 #endif
35 #if defined _MSC_VER
36 #pragma warning(push, 1)
37 #pragma warning(disable: 4917)
38 #endif
39 #include <objidl.h>
40 #include <shlobj.h>
41 #if defined _MSC_VER
42 #pragma warning(pop)
43 #endif
44 #include "..\misc\WinImplHelper.hxx"
45 
46 //----------------------------------------------------------------
47 // a simple helper class used to provide a buffer for different
48 // Win32 file and directory functions
49 //----------------------------------------------------------------
50 
51 class CAutoPathBuff
52 {
53 public:
CAutoPathBuff(size_t size=0)54     CAutoPathBuff( size_t size = 0 )
55     {
56         if (0 == size)
57             size = 32000; // max path length under Win2000
58 
59         pBuff = new sal_Unicode[size];
60 
61         OSL_POSTCOND(pBuff,"Could not allocate path buffer");
62     }
63 
~CAutoPathBuff()64     ~CAutoPathBuff( )
65     {
66         delete [] pBuff;
67     }
68 
operator sal_Unicode*()69     operator sal_Unicode*( )
70     {
71         OSL_PRECOND( pBuff, \
72             "No path buffer allocated" );
73         return pBuff;
74     }
75 
get()76     sal_Unicode* get( )
77     {
78         OSL_PRECOND( pBuff, \
79             "No path buffer allocated" );
80         return pBuff;
81     }
82 
83 private:
84     sal_Unicode* pBuff;
85 };
86 
87 //--------------------------------------------------------
88 // the Mta-Ole clipboard class is for internal use only!
89 // only one instance of this class should be created, the
90 // user has to ensure this!
91 // the class is not thread-safe because it will be used
92 // only from within the clipboard service and the methods
93 // of the clipboard service are already synchronized
94 //--------------------------------------------------------
95 
96 class CMtaFolderPicker
97 {
98 public:
99     CMtaFolderPicker( sal_uInt32 Flags );
100     virtual ~CMtaFolderPicker( );
101 
102     // shell functions
103     sal_Bool SAL_CALL browseForFolder( );
104 
105     virtual void  SAL_CALL setDisplayDirectory( const rtl::OUString& aDirectory );
106     virtual rtl::OUString  SAL_CALL getDisplayDirectory( );
107     virtual rtl::OUString SAL_CALL getDirectory( );
108 
109     virtual void SAL_CALL setDescription( const rtl::OUString& aDescription );
110 
111     virtual void SAL_CALL setTitle( const rtl::OUString& aTitle );
112     rtl::OUString  SAL_CALL getTitle( );
113 
114     //-----------------------------------------------------
115     // XCancellable
116     //-----------------------------------------------------
117 
118     virtual void SAL_CALL cancel( );
119 
120 protected:
121     void SAL_CALL enableOk( sal_Bool bEnable );
122     void SAL_CALL setSelection( const rtl::OUString& aDirectory );
123     void SAL_CALL setStatusText( const rtl::OUString& aStatusText );
124 
125     virtual void SAL_CALL onInitialized( );
126     virtual void SAL_CALL onSelChanged( const rtl::OUString& aNewPath ) = 0;
127 
128 private:
129     sal_uInt32 onValidateFailed();
130 
131     // helper functions
132     LPITEMIDLIST  SAL_CALL getItemIdListFromPath( const rtl::OUString& aDirectory );
133     rtl::OUString SAL_CALL getPathFromItemIdList( LPCITEMIDLIST lpItemIdList );
134     void SAL_CALL releaseItemIdList( LPITEMIDLIST lpItemIdList );
135 
136     unsigned int run( );
137 
138     // create a hidden windows which serves as an request
139     // target; so we guarantee synchronization
140     sal_Bool SAL_CALL createStaRequestWindow( );
141 
142     //---------------------------------------------------------------
143     // message handler functions; remeber these functions are called
144     // from a different thread context!
145     //---------------------------------------------------------------
146 
147     sal_Bool SAL_CALL onBrowseForFolder( );
148 
149     static LRESULT CALLBACK StaWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
150     static unsigned int WINAPI StaThreadProc( LPVOID pParam );
151 
152     static int CALLBACK FolderPickerCallback( HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData );
153 
154 protected:
155     HWND m_hwnd;
156 
157 private:
158     ATOM SAL_CALL RegisterStaRequestWindowClass( );
159     void SAL_CALL UnregisterStaRequestWindowClass( );
160 
161 private:
162     HANDLE                      m_hStaThread;
163     unsigned                    m_uStaThreadId;
164     HANDLE                      m_hEvtThrdReady;
165     HWND                        m_hwndStaRequestWnd;
166     rtl::OUString               m_dialogTitle;
167     rtl::OUString               m_Description;
168     rtl::OUString               m_displayDir;
169     rtl::OUString               m_SelectedDir;
170     BROWSEINFOW                 m_bi;
171     CAutoPathBuff               m_pathBuff;
172     HINSTANCE                   m_hInstance;
173 
174     // the request window class has to be registered only
175     // once per process, so multiple instance of this class
176     // share the registered window class
177     static ATOM       s_ClassAtom;
178     static osl::Mutex s_Mutex;
179     static sal_Int32  s_StaRequestWndRegisterCount;
180 
181 // prevent copy and assignment
182 private:
183     CMtaFolderPicker( const CMtaFolderPicker& );
184     CMtaFolderPicker& operator=( const CMtaFolderPicker& );
185 };
186 
187 #endif
188