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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_fpicker.hxx"
26 
27 //------------------------------------------------------------------------
28 // includes
29 //------------------------------------------------------------------------
30 
31 #include <tchar.h>
32 #include <osl/diagnose.h>
33 #include "controlaccess.hxx"
34 #include "..\misc\WinImplHelper.hxx"
35 
36 //------------------------------------------------------------
37 // we are using a table based algorithm to dispatch control
38 // actions there is one table containing one action table for
39 // each control class and one action table per control class
40 // which contains function pointer to control action functions
41 //------------------------------------------------------------
42 
43 //------------------------------------------------------------
44 // namespace directives
45 //------------------------------------------------------------
46 
47 using rtl::OUString;
48 
49 namespace // private
50 {
51 
52     //------------------------------------------------------------
53     // table setup
54     //------------------------------------------------------------
55 
56     CTRL_SETVALUE_FUNCTION_T CheckboxSetValueFunctionTable[] =
57     {
58         CheckboxSetState
59     };
60     const size_t SIZE_CHECKBOX_SETVALUE_FUNCTION_TABLE =
61         sizeof( CheckboxSetValueFunctionTable ) / sizeof( CTRL_SETVALUE_FUNCTION_T );
62 
63     CTRL_GETVALUE_FUNCTION_T CheckboxGetValueFunctionTable[] =
64     {
65         CheckboxGetState
66     };
67     const size_t SIZE_CHECKBOX_GETVALUE_FUNCTION_TABLE =
68         sizeof( CheckboxGetValueFunctionTable ) / sizeof( CTRL_GETVALUE_FUNCTION_T );
69 
70     CTRL_SETVALUE_FUNCTION_T ListboxSetValueFunctionTable[] =
71     {
72         NULL,
73         ListboxAddItem,
74         ListboxAddItems,
75         ListboxDeleteItem,
76         ListboxDeleteItems,
77         ListboxSetSelectedItem
78     };
79     const size_t SIZE_LISTBOX_SETVALUE_FUNCTION_TABLE =
80         sizeof( ListboxSetValueFunctionTable ) / sizeof( CTRL_SETVALUE_FUNCTION_T );
81 
82     CTRL_GETVALUE_FUNCTION_T ListboxGetValueFunctionTable[] =
83     {
84         NULL,
85         NULL,
86         NULL,
87         NULL,
88         NULL,
89         NULL,
90         ListboxGetItems,
91         ListboxGetSelectedItem,
92         ListboxGetSelectedItemIndex
93     };
94     const size_t SIZE_LISTBOX_GETVALUE_ACTION_TABLE =
95         sizeof( ListboxGetValueFunctionTable ) / sizeof( CTRL_GETVALUE_FUNCTION_T );
96 
97     struct _ENTRY
98     {
99         LPVOID lpFunctionTable;
100         size_t TableSize;
101     };
102 
103     // an array of function tables, one for each control class
104     _ENTRY CtrlClassSetValueFunctionTable[] =
105     {
106         { NULL, 0 },
107         { CheckboxSetValueFunctionTable, SIZE_CHECKBOX_SETVALUE_FUNCTION_TABLE },
108         { ListboxSetValueFunctionTable, SIZE_LISTBOX_SETVALUE_FUNCTION_TABLE },
109         { NULL, 0 }
110     };
111 
112     // an array of function tables, one for each control class
113     _ENTRY CtrlClassGetValueFunctionTable[] =
114     {
115         { NULL, 0 },
116         { CheckboxGetValueFunctionTable, SIZE_CHECKBOX_GETVALUE_FUNCTION_TABLE },
117         { ListboxGetValueFunctionTable, SIZE_LISTBOX_GETVALUE_ACTION_TABLE },
118         { NULL, 0 }
119     };
120 
121     //------------------------------------------------------------
122     //
123     //------------------------------------------------------------
124 
GetCtrlSetValueFunction(CTRL_SETVALUE_FUNCTION_T * aCtrlSetValueFunctionTable,size_t aTableSize,sal_Int16 aCtrlAction)125     CTRL_SETVALUE_FUNCTION_T SAL_CALL GetCtrlSetValueFunction(
126         CTRL_SETVALUE_FUNCTION_T* aCtrlSetValueFunctionTable, size_t aTableSize, sal_Int16 aCtrlAction )
127     {
128         if ( !aCtrlSetValueFunctionTable ||
129              aCtrlAction < 0
130              || sal::static_int_cast< sal_uInt16 >(aCtrlAction) >= aTableSize )
131             return NULL;
132 
133         return aCtrlSetValueFunctionTable[aCtrlAction];
134     }
135 
136     //------------------------------------------------------------
137     //
138     //------------------------------------------------------------
139 
GetCtrlGetValueFunction(CTRL_GETVALUE_FUNCTION_T * aCtrlGetValueFunctionTable,size_t aTableSize,sal_Int16 aCtrlAction)140     CTRL_GETVALUE_FUNCTION_T SAL_CALL GetCtrlGetValueFunction(
141         CTRL_GETVALUE_FUNCTION_T* aCtrlGetValueFunctionTable, size_t aTableSize, sal_Int16 aCtrlAction )
142     {
143         if ( !aCtrlGetValueFunctionTable ||
144              aCtrlAction < 0 ||
145              sal::static_int_cast< sal_uInt16 >(aCtrlAction) >= aTableSize )
146             return NULL;
147 
148         return aCtrlGetValueFunctionTable[aCtrlAction];
149     }
150 
151     //------------------------------------------------------------
152     //
153     //------------------------------------------------------------
154 
155     inline
GetCtrlClassSetValueFunctionTable(CTRL_CLASS aCtrlClass)156     _ENTRY SAL_CALL GetCtrlClassSetValueFunctionTable( CTRL_CLASS aCtrlClass )
157     {
158         return CtrlClassSetValueFunctionTable[aCtrlClass];
159     }
160 
161     //------------------------------------------------------------
162     //
163     //------------------------------------------------------------
164 
165     inline
GetCtrlClassGetValueFunctionTable(CTRL_CLASS aCtrlClass)166     _ENTRY SAL_CALL GetCtrlClassGetValueFunctionTable( CTRL_CLASS aCtrlClass )
167     {
168         return CtrlClassGetValueFunctionTable[aCtrlClass];
169     }
170 
171     int WindowsFileOpenCtrlIds[] =
172     {
173         0,
174         IDOK,       //  PUSHBUTTON_OK
175         IDCANCEL,   //  PUSHBUTTON_CANCEL
176         cmb1,       //  LISTBOX_FILTER
177         0,          //  CONTROL_FILEVIEW
178         0,          //  not available in system file picker
179         stc2,       //  LISTBOX_FILTER_LABEL
180         stc3        //  LISTBOX_FILE_NAME_LABEL
181     };
182     const int SIZE_WINDOWS_FILEOPEN_CTRL_IDS =
183         sizeof(WindowsFileOpenCtrlIds)/sizeof(WindowsFileOpenCtrlIds[0]);
184 
185 }; // end namespace
186 
187 //------------------------------------------------------------
188 //
189 //------------------------------------------------------------
190 
GetCtrlSetValueFunction(CTRL_CLASS aCtrlClass,sal_Int16 aCtrlAction)191 CTRL_SETVALUE_FUNCTION_T SAL_CALL GetCtrlSetValueFunction( CTRL_CLASS aCtrlClass, sal_Int16 aCtrlAction )
192 {
193     _ENTRY aEntry =
194         GetCtrlClassSetValueFunctionTable( aCtrlClass );
195 
196     return GetCtrlSetValueFunction(
197         reinterpret_cast< CTRL_SETVALUE_FUNCTION_T* >( aEntry.lpFunctionTable ),
198         aEntry.TableSize,
199         aCtrlAction );
200 }
201 
202 //------------------------------------------------------------
203 //
204 //------------------------------------------------------------
205 
GetCtrlGetValueFunction(CTRL_CLASS aCtrlClass,sal_Int16 aCtrlAction)206 CTRL_GETVALUE_FUNCTION_T SAL_CALL GetCtrlGetValueFunction( CTRL_CLASS aCtrlClass, sal_Int16 aCtrlAction )
207 {
208     _ENTRY aEntry =
209         GetCtrlClassGetValueFunctionTable( aCtrlClass );
210 
211     return GetCtrlGetValueFunction(
212         reinterpret_cast< CTRL_GETVALUE_FUNCTION_T* >( aEntry.lpFunctionTable ),
213         aEntry.TableSize,
214         aCtrlAction );
215 }
216 
217 //------------------------------------------------------------
218 //
219 //------------------------------------------------------------
220 
GetCtrlClass(HWND hwndCtrl)221 CTRL_CLASS SAL_CALL GetCtrlClass( HWND hwndCtrl )
222 {
223     CTRL_CLASS aCtrlClass = UNKNOWN;
224     TCHAR aClassName[256];
225 
226     int nRet = GetClassName(hwndCtrl,aClassName,(sizeof(aClassName)/sizeof(TCHAR)));
227     if (nRet)
228     {
229         if (0 == _tcsicmp(aClassName,TEXT("button")))
230         {
231             // button means many things so we have
232             // to find out what button it is
233             LONG lBtnStyle = GetWindowLong(hwndCtrl,GWL_STYLE);
234             if (lBtnStyle & BS_CHECKBOX)
235                 aCtrlClass = CHECKBOX;
236             else if (((lBtnStyle & BS_PUSHBUTTON) == 0) || (lBtnStyle & BS_DEFPUSHBUTTON))
237                 aCtrlClass = PUSHBUTTON;
238         }
239         else if (0 == _tcsicmp(aClassName,TEXT("listbox")) ||
240                   0 == _tcsicmp(aClassName,TEXT("combobox")))
241             aCtrlClass = LISTBOX;
242     }
243 
244     return aCtrlClass;
245 }
246 
247 //------------------------------------------------------------
248 //
249 //------------------------------------------------------------
250 
CommonFilePickerCtrlIdToWinFileOpenCtrlId(sal_Int16 aControlId)251 int SAL_CALL CommonFilePickerCtrlIdToWinFileOpenCtrlId( sal_Int16 aControlId )
252 {
253     if ( aControlId < 0 || aControlId > SIZE_WINDOWS_FILEOPEN_CTRL_IDS )
254         return aControlId;
255 
256     return WindowsFileOpenCtrlIds[aControlId];
257 }
258