wined3d: Inherit from the proper state table in atifs.
[wine] / dlls / gphoto2.ds / ui.c
1 /*
2 * TWAIN32 Options UI
3 *
4 * Copyright 2006 CodeWeavers, Aric Stewart
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "wingdi.h"
34 #include "winreg.h"
35 #include "commctrl.h"
36 #include "prsht.h"
37 #include "twain.h"
38 #include "gphoto2_i.h"
39 #include "wine/debug.h"
40 #include "resource.h"
41
42 LPCSTR settings_key = "Software\\Wine\\Gphoto2";
43 LPCSTR settings_value = "SkipUI";
44 static BOOL disable_dialog;
45 static HBITMAP static_bitmap;
46
47 static INT_PTR CALLBACK ConnectingProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
48 {
49         return FALSE;
50 }
51
52 static void on_disable_dialog_clicked(HWND dialog)
53 {
54     if (IsDlgButtonChecked(dialog, IDC_SKIP) == BST_CHECKED)
55         disable_dialog = TRUE;
56     else
57         disable_dialog = FALSE;
58 }
59
60 static void UI_EndDialog(HWND hwnd, INT_PTR rc)
61 {
62     if (disable_dialog)
63     {
64         HKEY key;
65         const DWORD data = 1;
66         if (RegCreateKeyExA(HKEY_CURRENT_USER, settings_key, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL) == ERROR_SUCCESS)
67         {
68             RegSetValueExA(key, settings_value, 0, REG_DWORD, (const BYTE *)&data, sizeof(DWORD));
69             RegCloseKey(key);
70         }
71     }
72     EndDialog(hwnd, rc);
73 }
74
75 static int GetAllImages(void)
76 {
77     struct gphoto2_file *file;
78     int has_images = 0;
79
80     LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry)
81     {
82         if (strstr(file->filename,".JPG") || strstr(file->filename,".jpg"))
83         {
84             file->download = TRUE;
85             has_images = 1;
86         }
87     }
88     return has_images;
89 }
90
91 static void PopulateListView(HWND List)
92 {
93         struct gphoto2_file *file;
94         LVITEMA item;
95         int index = 0;
96
97         LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry)
98         {
99                 if (strstr(file->filename,".JPG") || strstr(file->filename,".jpg")) 
100                 {
101                         item.mask = LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE ;
102                         item.iItem = index;
103                         item.iSubItem = 0;
104                         item.pszText = file->filename;
105                         item.iImage = index;
106                         item.lParam= (LPARAM)file;
107
108                         SendMessageA(List, LVM_INSERTITEMA,0,(LPARAM)&item);
109                         index ++;
110                 }
111         }
112 }
113
114 static void PopulateImageList(HIMAGELIST *iList, HWND list)
115 {
116         struct gphoto2_file *file;
117         INT rc;
118         HWND    progress_dialog;
119
120         progress_dialog =
121                 CreateDialogW(GPHOTO2_instance,(LPWSTR)MAKEINTRESOURCE(IDD_CONNECTING),
122                                 NULL, ConnectingProc);
123         
124         LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry)
125         {
126                 if (strstr(file->filename,".JPG") || strstr(file->filename,".jpg")) 
127                 {
128                         HBITMAP         bitmap;
129                         BITMAP          bmpInfo;
130
131 #ifdef HAVE_GPHOTO2
132                         _get_gphoto2_file_as_DIB(file->folder, file->filename,
133                                         GP_FILE_TYPE_PREVIEW, 0, &bitmap); 
134 #else
135                         bitmap = 0;
136 #endif
137                         GetObjectA(bitmap,sizeof(BITMAP),(LPVOID)&bmpInfo);
138
139                         if (*iList == 0)
140                         {
141                                 *iList = ImageList_Create(bmpInfo.bmWidth,
142                                                 bmpInfo.bmHeight,ILC_COLOR24, 10,10);
143
144                                 SendMessageW(list, LVM_SETICONSPACING, 0,
145                                                 MAKELONG(bmpInfo.bmWidth+6, bmpInfo.bmHeight+15) ); }
146                         
147                         rc = ImageList_Add(*iList, bitmap, 0);
148
149                         DeleteObject(static_bitmap);
150                         static_bitmap = bitmap;
151                         SendMessageW(GetDlgItem(progress_dialog,IDC_BITMAP),STM_SETIMAGE,
152                                         IMAGE_BITMAP, (LPARAM)static_bitmap);
153                         RedrawWindow(progress_dialog,NULL,NULL,RDW_INTERNALPAINT|RDW_UPDATENOW|RDW_ALLCHILDREN);
154                 }
155         }
156         EndDialog(progress_dialog,0);
157         
158 }
159
160 static INT_PTR CALLBACK DialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
161 {
162         switch(msg)
163         {
164                 case WM_INITDIALOG:
165                         {
166                                 disable_dialog = FALSE;
167                                 EnableWindow(GetDlgItem(hwnd,IDC_IMPORT),FALSE);
168                         }
169                         break;
170                 case WM_NOTIFY:
171                         if (((LPNMHDR)lParam)->code == LVN_ITEMCHANGED)
172                         {
173                                 HWND list = GetDlgItem(hwnd,IDC_LIST1);
174                                 int count = SendMessageA(list,LVM_GETSELECTEDCOUNT,0,0);
175                                 if (count > 0)
176                                         EnableWindow(GetDlgItem(hwnd,IDC_IMPORT),TRUE);
177                                 else
178                                         EnableWindow(GetDlgItem(hwnd,IDC_IMPORT),FALSE);
179                         }
180                         break;
181                 case WM_COMMAND:
182                         switch LOWORD(wParam)
183                         {
184                                 case IDC_SKIP:
185                                         on_disable_dialog_clicked(hwnd);
186                                         break;
187                                 case IDC_EXIT:
188                                         UI_EndDialog(hwnd,0);
189                                         break;
190                                 case IDC_IMPORT:
191                                         {
192                                                 HWND list = GetDlgItem(hwnd,IDC_LIST1);
193                                                 int count = SendMessageA(list,LVM_GETSELECTEDCOUNT,0,0);
194                                                 int i;
195
196                                                 if (count ==0)
197                                                 {
198                                                         UI_EndDialog(hwnd,0);
199                                                         return FALSE;
200                                                 }
201
202                                                 count = SendMessageA(list,LVM_GETITEMCOUNT,0,0);
203                                                 for ( i = 0; i < count; i++)
204                                                 {
205                                                         INT state = 0x00000000;
206
207                                                         state = SendMessageA(list,LVM_GETITEMSTATE,i,
208                                                                         LVIS_SELECTED);
209
210                                                         if (state)
211                                                         {
212                                                                 LVITEMA item;
213                                                                 struct gphoto2_file *file;
214
215
216                                                                 item.mask = LVIF_PARAM;
217                                                                 item.iItem = i;
218
219                                                                 item.iSubItem = 0;
220                                                                 SendMessageA(list,LVM_GETITEMA,0,(LPARAM)&item);
221
222                                                                 file = (struct gphoto2_file*)item.lParam;
223                                                                 file->download = TRUE;
224                                                         }
225                                                 }
226
227                                                 UI_EndDialog(hwnd,1);
228                                         }
229                                         break;
230                                 case IDC_IMPORTALL:
231                                         {
232                                                 if (!GetAllImages())
233                                                 {
234                                                         UI_EndDialog(hwnd,0);
235                                                         return FALSE;
236                                                 }
237                                                 UI_EndDialog(hwnd,1);
238                                         }
239                                         break;
240                                 case IDC_FETCH:
241                                         {
242                                                 HIMAGELIST ilist = 0;
243                                                 HWND list = GetDlgItem(hwnd,IDC_LIST1);
244                                                 PopulateImageList(&ilist,list);
245
246                                                 SendMessageA(list, LVM_SETIMAGELIST,LVSIL_NORMAL,(LPARAM)ilist);
247                                                 PopulateListView(list);
248                                                 EnableWindow(GetDlgItem(hwnd,IDC_FETCH),FALSE);
249                                         }
250                                         break;
251                         }
252                         break;
253         }
254         return FALSE;
255 }
256
257 BOOL DoCameraUI(void)
258 {
259         HKEY key;
260         DWORD data = 0;
261         DWORD size = sizeof(data);
262         if (RegOpenKeyExA(HKEY_CURRENT_USER, settings_key, 0, KEY_READ, &key) == ERROR_SUCCESS) {
263                 RegQueryValueExA(key, settings_value, NULL, NULL, (LPBYTE) &data, &size);
264                 RegCloseKey(key);
265                 if (data)
266                         return GetAllImages();
267         }
268         return DialogBoxW(GPHOTO2_instance,
269                         (LPWSTR)MAKEINTRESOURCE(IDD_CAMERAUI),NULL, DialogProc);
270 }
271
272 static INT_PTR CALLBACK ProgressProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
273                 lParam)
274 {
275             return FALSE;
276 }   
277
278 HWND TransferringDialogBox(HWND dialog, DWORD progress)
279 {
280         if (!dialog)
281                 dialog = CreateDialogW(GPHOTO2_instance,
282                                 (LPWSTR)MAKEINTRESOURCE(IDD_DIALOG1), NULL, ProgressProc);
283                                 
284         if (progress == -1)
285         {
286                 EndDialog(dialog,0);
287                 return NULL;
288         }
289
290         RedrawWindow(dialog,NULL,NULL,
291                         RDW_INTERNALPAINT|RDW_UPDATENOW|RDW_ALLCHILDREN);
292
293         return dialog;
294 }