ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/VTBFileUtil2/ScanUtility.cxx
Revision: 284
Committed: 2003-09-01T23:05:56-07:00 (21 years, 10 months ago) by douglas
File size: 23006 byte(s)
Log Message:
Did stuff, fixed errors.

File Contents

# User Rev Content
1 douglas 260 // Vance Thrift and Biller File Utility 2
2     //
3     // Douglas Thrift
4     //
5 douglas 284 // $Id: ScanUtility.cxx,v 1.13 2003/09/02 06:05:56 douglas Exp $
6 douglas 260
7     #include "ScanUtility.h"
8 douglas 265
9 douglas 282 ScanUtility::ScanUtility() : DiscBrowse()
10 douglas 268 {
11     number = count++;
12 douglas 282
13 douglas 268 utilities.insert(pair<unsigned, ScanUtility*>(number, this));
14    
15 douglas 282 title = programName + " - Scan Utility";
16 douglas 274
17 douglas 268 // start
18 douglas 271 wizard[0].dwSize = sizeof(wizard[0]);
19 douglas 282 wizard[0].dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USEHEADERTITLE |
20     PSP_USEHEADERSUBTITLE;
21 douglas 271 wizard[0].hInstance = gui.instance;
22     wizard[0].pszTemplate = MAKEINTRESOURCE(IDD_START);
23 douglas 282 wizard[0].pszTitle = title.c_str();
24 douglas 271 wizard[0].pfnDlgProc = start;
25     wizard[0].lParam = number;
26     wizard[0].pszHeaderTitle = "Start";
27     wizard[0].pszHeaderSubTitle = "Change any settings before scanning.";
28 douglas 273
29     // select
30     wizard[1].dwSize = sizeof(wizard[1]);
31 douglas 282 wizard[1].dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USEHEADERTITLE |
32     PSP_USEHEADERSUBTITLE;
33 douglas 273 wizard[1].hInstance = gui.instance;
34     wizard[1].pszTemplate = MAKEINTRESOURCE(IDD_SELECT);
35 douglas 282 wizard[1].pszTitle = title.c_str();
36 douglas 273 wizard[1].pfnDlgProc = select;
37     wizard[1].lParam = number;
38     wizard[1].pszHeaderTitle = "Select";
39     wizard[1].pszHeaderSubTitle = "Choose the scanned document to save.";
40 douglas 274
41     // enter
42     wizard[2].dwSize = sizeof(wizard[2]);
43 douglas 282 wizard[2].dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USEHEADERTITLE |
44     PSP_USEHEADERSUBTITLE;
45 douglas 274 wizard[2].hInstance = gui.instance;
46     wizard[2].pszTemplate = MAKEINTRESOURCE(IDD_ENTER);
47 douglas 282 wizard[2].pszTitle = title.c_str();
48 douglas 274 wizard[2].pfnDlgProc = enter;
49 douglas 277 wizard[2].lParam = number;
50 douglas 274 wizard[2].pszHeaderTitle = "Enter";
51     wizard[2].pszHeaderSubTitle = "Input the client information.";
52 douglas 278
53     // confirm
54     wizard[3].dwSize = sizeof(wizard[3]);
55 douglas 282 wizard[3].dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USEHEADERTITLE |
56     PSP_USEHEADERSUBTITLE;
57 douglas 278 wizard[3].hInstance = gui.instance;
58     wizard[3].pszTemplate = MAKEINTRESOURCE(IDD_CONFIRM);
59 douglas 282 wizard[3].pszTitle = title.c_str();
60 douglas 278 wizard[3].pfnDlgProc = confirm;
61     wizard[3].lParam = number;
62     wizard[3].pszHeaderTitle = "Confirm";
63     wizard[3].pszHeaderSubTitle = "Make sure the file information is correct.";
64    
65     // complete
66     wizard[4].dwSize = sizeof(wizard[3]);
67 douglas 282 wizard[4].dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USEHEADERTITLE |
68     PSP_USEHEADERSUBTITLE;
69 douglas 278 wizard[4].hInstance = gui.instance;
70     wizard[4].pszTemplate = MAKEINTRESOURCE(IDD_DONE);
71 douglas 282 wizard[4].pszTitle = title.c_str();
72 douglas 278 wizard[4].pfnDlgProc = done;
73     wizard[4].lParam = number;
74     wizard[4].pszHeaderTitle = "Done";
75     wizard[4].pszHeaderSubTitle = "Exit or start over for another scan.";
76 douglas 268 }
77    
78     ScanUtility::~ScanUtility()
79     {
80     utilities.erase(number);
81     }
82    
83 douglas 265 void ScanUtility::run(void)
84     {
85 douglas 282 loadDirs();
86    
87 douglas 268 PROPSHEETHEADER header;
88 douglas 265
89 douglas 268 // header
90     header.dwSize = sizeof(header);
91 douglas 271 header.dwFlags = PSH_DEFAULT | PSH_HEADER | PSH_PROPSHEETPAGE |
92 douglas 278 PSH_USEICONID | PSH_WIZARD97 | PSH_WIZARDHASFINISH;
93 douglas 268 header.hwndParent = NULL;
94     header.hInstance = gui.instance;
95     header.pszIcon = MAKEINTRESOURCE(IDI_VTB_ICON);
96 douglas 278 header.nPages = 5;
97 douglas 268 header.nStartPage = 0;
98 douglas 271 header.ppsp = wizard;
99 douglas 269 header.pszbmHeader = MAKEINTRESOURCE(IDB_VTB_BMP);
100 douglas 265
101 douglas 268 PropertySheet(&header);
102 douglas 282 saveDirs();
103 douglas 268 }
104    
105     map<unsigned, ScanUtility*> ScanUtility::utilities;
106     map<HWND, ScanUtility*> ScanUtility::windows;
107    
108 douglas 269 void ScanUtility::loadDirs(void)
109     {
110     HKEY key;
111    
112     if (RegOpenKeyEx(HKEY_CURRENT_USER,
113 douglas 271 "Software\\DouglasThrift\\VTBFileUtil2", 0, KEY_QUERY_VALUE, &key) ==
114 douglas 269 ERROR_SUCCESS)
115     {
116     DWORD type;
117 douglas 271 char data[MAX_PATH];
118 douglas 269 DWORD size = MAX_PATH;
119    
120 douglas 271 if (RegQueryValueEx(key, "ScanDir", NULL, &type, LPBYTE(data), &size)
121     == ERROR_SUCCESS)
122 douglas 269 {
123     data[size - 1] = '\0';
124    
125     switch (type)
126     {
127     case REG_EXPAND_SZ:
128 douglas 273 {
129     char folder[MAX_PATH];
130    
131     ExpandEnvironmentStrings(data, folder, MAX_PATH);
132    
133     scanDir = folder;
134     }
135 douglas 269 break;
136     case REG_SZ:
137 douglas 271 scanDir = data;
138 douglas 269 break;
139     default:
140     setScanDir();
141     break;
142     }
143     }
144     else
145     {
146     setScanDir();
147     }
148    
149     size = MAX_PATH;
150    
151 douglas 271 if (RegQueryValueEx(key, "SaveDir", NULL, &type, LPBYTE(data), &size)
152     == ERROR_SUCCESS)
153 douglas 269 {
154     data[size - 1] = '\0';
155    
156     switch (type)
157     {
158     case REG_EXPAND_SZ:
159 douglas 273 {
160     char folder[MAX_PATH];
161    
162     ExpandEnvironmentStrings(data, folder, MAX_PATH);
163    
164     saveDir = folder;
165     }
166 douglas 269 break;
167     case REG_SZ:
168 douglas 271 saveDir = data;
169 douglas 269 break;
170     default:
171     setSaveDir();
172     break;
173     }
174     }
175     else
176     {
177     setSaveDir();
178     }
179    
180     RegCloseKey(key);
181     }
182     else
183     {
184     setScanDir();
185     setSaveDir();
186     }
187 douglas 271
188     if (debug) cerr << "scanDir = " << scanDir << "\n"
189 douglas 273 << "saveDir = " << saveDir << "\n";
190 douglas 269 }
191    
192     void ScanUtility::saveDirs(void)
193     {
194     HKEY key;
195    
196     if (RegCreateKeyEx(HKEY_CURRENT_USER,
197     "Software\\DouglasThrift\\VTBFileUtil2", 0, NULL,
198 douglas 271 REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE | KEY_SET_VALUE, NULL, &key,
199     NULL) == ERROR_SUCCESS)
200 douglas 269 {
201 douglas 271 DWORD type;
202     char data[MAX_PATH];
203     DWORD size = MAX_PATH;
204    
205     if (RegQueryValueEx(key, "ScanDir", NULL, &type, LPBYTE(data), &size)
206     == ERROR_SUCCESS)
207 douglas 269 {
208 douglas 271 data[size - 1] = '\0';
209 douglas 269 }
210 douglas 271 else
211     {
212     data[0] = '\0';
213     }
214 douglas 269
215 douglas 273 if (scanDir != data || type != REG_SZ)
216 douglas 269 {
217 douglas 273 if (RegSetValueEx(key, "ScanDir", 0, REG_SZ,
218     LPBYTE(scanDir.c_str()), scanDir.length() + 1) !=
219 douglas 271 ERROR_SUCCESS)
220     {
221     error();
222     }
223 douglas 269 }
224    
225 douglas 271 size = MAX_PATH;
226    
227     if (RegQueryValueEx(key, "SaveDir", NULL, &type, LPBYTE(data), &size)
228     == ERROR_SUCCESS)
229     {
230     data[size - 1] = '\0';
231     }
232     else
233     {
234     data[0] = '\0';
235     }
236    
237 douglas 273 if (saveDir != data || type != REG_SZ)
238 douglas 271 {
239 douglas 273 if (RegSetValueEx(key, "SaveDir", 0, REG_SZ,
240     LPBYTE(saveDir.c_str()), saveDir.length() + 1) !=
241 douglas 271 ERROR_SUCCESS)
242     {
243     error();
244     }
245     }
246    
247 douglas 269 RegCloseKey(key);
248     }
249     else
250     {
251     error();
252     }
253     }
254    
255     void ScanUtility::setScanDir(HWND parent)
256     {
257 douglas 271 BROWSEINFO info;
258 douglas 269
259 douglas 271 info.hwndOwner = parent;
260     info.pidlRoot = NULL;
261     info.pszDisplayName = NULL;
262     info.lpszTitle = "Select the Scan Directory";
263     info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
264     info.lpfn = browse;
265     info.lParam = MAKELPARAM(number, true);
266     info.iImage = 0;
267    
268     do
269     {
270     LPITEMIDLIST id = SHBrowseForFolder(&info);
271    
272     if (id != NULL)
273     {
274     char folder[MAX_PATH];
275    
276     if (SHGetPathFromIDList(id, folder))
277     {
278     scanDir = folder;
279     }
280     }
281    
282     LPMALLOC destruct;
283    
284     SHGetMalloc(&destruct);
285     destruct->Free(id);
286     destruct->Release();
287    
288     if (scanDir == "")
289     {
290     switch (MessageBox(parent, "Scan Directory needs to be selected.",
291 douglas 282 title.c_str(), MB_ABORTRETRYIGNORE | MB_ICONERROR))
292 douglas 271 {
293     case IDABORT:
294     exit(1);
295     break;
296     case IDRETRY:
297     break;
298     case IDIGNORE:
299     Beep(2200, 250);
300     Beep(1100, 500);
301     Beep(3300, 250);
302     exit(2);
303     break;
304     }
305     }
306     }
307     while (scanDir == "");
308 douglas 273
309     if (debug) cerr << "scanDir = " << scanDir << "\n";
310 douglas 269 }
311    
312     void ScanUtility::setSaveDir(HWND parent)
313     {
314 douglas 271 BROWSEINFO info;
315 douglas 269
316 douglas 271 info.hwndOwner = parent;
317     info.pidlRoot = NULL;
318     info.pszDisplayName = NULL;
319     info.lpszTitle = "Select the Save Directory";
320     info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
321     info.lpfn = browse;
322     info.lParam = MAKELPARAM(number, false);
323     info.iImage = 0;
324    
325     do
326     {
327     LPITEMIDLIST id = SHBrowseForFolder(&info);
328    
329     if (id != NULL)
330     {
331     char folder[MAX_PATH];
332    
333     if (SHGetPathFromIDList(id, folder))
334     {
335     saveDir = folder;
336     }
337     }
338    
339     LPMALLOC destruct;
340    
341     SHGetMalloc(&destruct);
342     destruct->Free(id);
343     destruct->Release();
344    
345     if (saveDir == "")
346     {
347     switch (MessageBox(parent, "Scan Directory needs to be selected.",
348 douglas 282 title.c_str(), MB_ABORTRETRYIGNORE | MB_ICONERROR))
349 douglas 271 {
350     case IDABORT:
351     exit(1);
352     break;
353     case IDRETRY:
354     break;
355     case IDIGNORE:
356     Beep(2200, 250);
357     Beep(1100, 500);
358     Beep(3300, 250);
359     exit(2);
360     break;
361     }
362     }
363     }
364     while (saveDir == "");
365 douglas 273
366     if (debug) cerr << "saveDir = " << saveDir << "\n";
367 douglas 269 }
368    
369 douglas 273 void ScanUtility::populate(HWND parent)
370     {
371     set<string> scans;
372    
373     do
374     {
375     string scan = scanDir + "\\SCAN????_000." +
376     IndividualClient::getExtension();
377     WIN32_FIND_DATA found;
378     HANDLE finder = FindFirstFile(scan.c_str(), &found);
379    
380     if (finder != INVALID_HANDLE_VALUE)
381     {
382     do
383     {
384     scans.insert(found.cFileName);
385     }
386     while (FindNextFile(finder, &found));
387    
388     FindClose(finder);
389     }
390    
391     if (scans.empty())
392     {
393     if (MessageBox(parent, "No scanned documents found.",
394 douglas 282 title.c_str(), MB_RETRYCANCEL | MB_ICONQUESTION) == IDCANCEL)
395     break;
396 douglas 273 }
397     }
398     while (scans.empty());
399    
400     if (scans.empty())
401     {
402     PropSheet_PressButton(GetParent(parent), PSBTN_BACK);
403     }
404     else
405     {
406     SHFILEINFO info;
407     HIMAGELIST icons = HIMAGELIST(SHGetFileInfo((*scans.begin()).c_str(),
408     0, &info, sizeof(info), SHGFI_SMALLICON | SHGFI_SYSICONINDEX));
409    
410     ListView_SetImageList(GetDlgItem(parent, IDC_SELECT_SCANS), icons,
411     LVSIL_SMALL);
412    
413     if (debug) cerr << "scans = {\n";
414    
415     for (set<string>::iterator itor = scans.begin(); itor != scans.end();
416     itor++)
417     {
418     if (debug) cerr << " " << *itor << "\n";
419    
420     char scan[MAX_PATH];
421    
422     sprintf(scan, "%s", (*itor).c_str());
423    
424     LVITEM item;
425    
426     item.mask = LVIF_IMAGE | LVIF_TEXT;
427 douglas 274 item.iItem = 0;
428 douglas 273 item.iSubItem = 0;
429     item.pszText = scan;
430     item.iImage = info.iIcon;
431    
432     ListView_InsertItem(GetDlgItem(parent, IDC_SELECT_SCANS), &item);
433     }
434    
435     if (debug) cerr << "}\n";
436 douglas 274
437     ListView_SetItemState(GetDlgItem(parent, IDC_SELECT_SCANS), 0,
438     LVIS_SELECTED, LVIS_SELECTED);
439 douglas 273 }
440     }
441    
442 douglas 277 ScanUtility* ScanUtility::which(HWND window)
443     {
444     map<HWND, ScanUtility*>::iterator itor = windows.find(window);
445    
446     return itor->second;
447     }
448    
449     ScanUtility* ScanUtility::which(HWND window, LPARAM l)
450     {
451     LPPROPSHEETPAGE page = LPPROPSHEETPAGE(l);
452     map<unsigned, ScanUtility*>::iterator itor = utilities.find(page->lParam);
453    
454     windows.insert(pair<HWND, ScanUtility*>(window, itor->second));
455    
456     return itor->second;
457     }
458    
459 douglas 271 int ScanUtility::browse(HWND dialog, UINT msg, LPARAM l, LPARAM d)
460     {
461     map<unsigned, ScanUtility*>::iterator itor = utilities.find(LOWORD(d));
462     ScanUtility* data = itor->second;
463    
464     switch (msg)
465     {
466     case BFFM_INITIALIZED:
467     center(dialog);
468     SendMessage(dialog, BFFM_SETOKTEXT, 0,
469     LPARAM(toWide("&Select").c_str()));
470     SendMessage(dialog, BFFM_SETEXPANDED, FALSE, CSIDL_DRIVES);
471 douglas 273 SendMessage(dialog, BFFM_SETSELECTION, TRUE, LPARAM(HIWORD(d) ?
472     data->scanDir.c_str() : data->saveDir.c_str()));
473 douglas 271 break;
474     case BFFM_SELCHANGED:
475     {
476 douglas 284 SHFILEINFO info;
477 douglas 271
478 douglas 284 SHGetFileInfo(LPCSTR(l), 0, &info, sizeof(info), SHGFI_DISPLAYNAME
479     | SHGFI_PIDL);
480     SendMessage(dialog, BFFM_SETSTATUSTEXT, 0,
481     LPARAM(info.szDisplayName));
482 douglas 271
483 douglas 284 char folder[MAX_PATH];
484 douglas 271
485 douglas 284 if (!SHGetPathFromIDList(LPCITEMIDLIST(l), folder))
486 douglas 271 {
487     SendMessage(dialog, BFFM_ENABLEOK, 0, 0);
488     }
489 douglas 284 else
490     {
491     SendMessage(dialog, BFFM_SETSTATUSTEXT, 0, LPARAM(folder));
492     }
493 douglas 271 }
494     break;
495     }
496    
497     return 0;
498     }
499    
500 douglas 268 INT_PTR ScanUtility::start(HWND dialog, UINT msg, WPARAM w, LPARAM l)
501     {
502 douglas 277 ScanUtility* data = which(dialog);
503 douglas 268
504     switch (msg)
505 douglas 265 {
506 douglas 268 case WM_INITDIALOG:
507 douglas 269 center(GetParent(dialog));
508 douglas 274 SendMessage(GetParent(dialog), WM_SETICON, ICON_BIG, LPARAM(gui.icon));
509 douglas 277
510     data = which(dialog, l);
511 douglas 265
512 douglas 273 {
513     ostringstream instructions;
514    
515 douglas 274 instructions << "1. If you need instructions, you should not be ru"
516     << "nning this program in Scan Utility mode.\n"
517     << "2. Otherwise, go forth and scan.\n"
518     << "3. Then come back and click Next.\n";
519 douglas 273
520     SetDlgItemText(dialog, IDC_START_INSTRUCTIONS,
521     instructions.str().c_str());
522     }
523 douglas 268 break;
524 douglas 269 case WM_NOTIFY:
525     {
526 douglas 273 LPNMHDR nm = LPNMHDR(l);
527 douglas 269
528     switch (nm->code)
529     {
530     case PSN_SETACTIVE:
531 douglas 278 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_FINISH |
532     PSWIZB_NEXT);
533    
534 douglas 271 {
535 douglas 283 SHFILEINFO info;
536 douglas 271
537 douglas 283 SHGetFileInfo(data->scanDir.c_str(), 0, &info,
538     sizeof(info), SHGFI_ICONLOCATION);
539    
540 douglas 284 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
541     info.iIcon);
542 douglas 283
543     SendDlgItemMessage(dialog, IDC_START_SCAN_ICON,
544     STM_SETIMAGE, IMAGE_ICON, LPARAM(icon));
545     SHGetFileInfo(data->saveDir.c_str(), 0, &info,
546     sizeof(info), SHGFI_ICONLOCATION);
547    
548 douglas 284 icon = ExtractIcon(gui.instance, info.szDisplayName,
549     info.iIcon);
550 douglas 283
551     SendDlgItemMessage(dialog, IDC_START_SAVE_ICON,
552     STM_SETIMAGE, IMAGE_ICON, LPARAM(icon));
553     }
554    
555     {
556     char folder[38];
557    
558     PathCompactPathEx(folder, data->scanDir.c_str(), 38, 0);
559 douglas 271 SetDlgItemText(dialog, IDC_START_SCAN_TEXT, folder);
560 douglas 283 PathCompactPathEx(folder, data->saveDir.c_str(), 38, 0);
561 douglas 271 SetDlgItemText(dialog, IDC_START_SAVE_TEXT, folder);
562     }
563 douglas 269 break;
564 douglas 273 case PSN_WIZNEXT:
565     SetCurrentDirectory(data->scanDir.c_str());
566     break;
567 douglas 269 }
568     }
569     break;
570 douglas 271 case WM_COMMAND:
571     switch (LOWORD(w))
572     {
573     case IDC_START_SCAN_BROWSE:
574     data->setScanDir(dialog);
575 douglas 283
576 douglas 271 {
577 douglas 283 SHFILEINFO info;
578 douglas 271
579 douglas 283 SHGetFileInfo(data->scanDir.c_str(), 0, &info, sizeof(info),
580     SHGFI_ICONLOCATION);
581    
582 douglas 284 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
583     info.iIcon);
584 douglas 283
585     SendDlgItemMessage(dialog, IDC_START_SCAN_ICON, STM_SETIMAGE,
586     IMAGE_ICON, LPARAM(icon));
587     }
588    
589     {
590     char folder[38];
591    
592     PathCompactPathEx(folder, data->scanDir.c_str(), 38, 0);
593 douglas 271 SetDlgItemText(dialog, IDC_START_SCAN_TEXT, folder);
594     }
595     break;
596     case IDC_START_SAVE_BROWSE:
597     data->setSaveDir(dialog);
598 douglas 283
599 douglas 271 {
600 douglas 283 SHFILEINFO info;
601 douglas 271
602 douglas 283 SHGetFileInfo(data->saveDir.c_str(), 0, &info, sizeof(info),
603     SHGFI_ICONLOCATION);
604    
605 douglas 284 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
606     info.iIcon);
607 douglas 283
608     SendDlgItemMessage(dialog, IDC_START_SAVE_ICON, STM_SETIMAGE,
609     IMAGE_ICON, LPARAM(icon));
610     }
611    
612     {
613     char folder[38];
614    
615     PathCompactPathEx(folder, data->saveDir.c_str(), 38, 0);
616 douglas 271 SetDlgItemText(dialog, IDC_START_SAVE_TEXT, folder);
617     }
618     break;
619     }
620     break;
621 douglas 268 }
622 douglas 265
623 douglas 268 return FALSE;
624 douglas 265 }
625 douglas 273
626     INT_PTR ScanUtility::select(HWND dialog, UINT msg, WPARAM w, LPARAM l)
627     {
628 douglas 277 ScanUtility* data = which(dialog);
629 douglas 273
630     switch (msg)
631     {
632     case WM_INITDIALOG:
633 douglas 277 data = which(dialog, l);
634 douglas 273
635     {
636 douglas 274 ostringstream select;
637    
638 douglas 282 select << "Select the scanned document that you need to save.";
639 douglas 274
640     SetDlgItemText(dialog, IDC_SELECT_TEXT, select.str().c_str());
641     }
642 douglas 277
643 douglas 274 {
644 douglas 273 LVCOLUMN column;
645    
646     column.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
647     column.fmt = LVCFMT_IMAGE;
648     column.cx = 120;
649     column.pszText = "Scanned Documents";
650    
651     ListView_InsertColumn(GetDlgItem(dialog, IDC_SELECT_SCANS), 0,
652     &column);
653     }
654     break;
655     case WM_NOTIFY:
656     if (w == IDC_SELECT_SCANS)
657     {
658 douglas 274 LPNMITEMACTIVATE ni = LPNMITEMACTIVATE(l);
659    
660     switch (ni->hdr.code)
661 douglas 273 {
662 douglas 274 case NM_DBLCLK:
663 douglas 284 if (ni->iItem != -1)
664 douglas 274 {
665     char scan[MAX_PATH];
666    
667     ListView_GetItemText(GetDlgItem(dialog, IDC_SELECT_SCANS),
668     ni->iItem, 0, scan, MAX_PATH);
669     ShellExecute(dialog, NULL, scan, NULL, NULL,
670     SW_SHOWDEFAULT);
671     }
672 douglas 273 break;
673     }
674     }
675     else
676     {
677     LPNMHDR nm = LPNMHDR(l);
678    
679     switch (nm->code)
680     {
681     case PSN_SETACTIVE:
682 douglas 274 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
683 douglas 278 PSWIZB_DISABLEDFINISH | PSWIZB_NEXT);
684 douglas 273 data->populate(dialog);
685     break;
686     case PSN_WIZBACK:
687     ListView_DeleteAllItems(GetDlgItem(dialog, IDC_SELECT_SCANS));
688 douglas 277 data->client.setFile("");
689 douglas 273 break;
690     case PSN_WIZNEXT:
691 douglas 274 {
692     int index = ListView_GetNextItem(GetDlgItem(dialog,
693     IDC_SELECT_SCANS), -1, LVNI_SELECTED);
694     char scan[MAX_PATH];
695    
696 douglas 284 if (index != -1)
697     {
698     ListView_GetItemText(GetDlgItem(dialog,
699     IDC_SELECT_SCANS), index, 0, scan, MAX_PATH);
700     }
701     else
702     {
703     ListView_GetItemText(GetDlgItem(dialog,
704     IDC_SELECT_SCANS), 0, 0, scan, MAX_PATH);
705     }
706 douglas 274
707     data->scan = scan;
708     }
709 douglas 277
710 douglas 284 if (debug) cerr << "scan = " << data->scan << "\n";
711    
712 douglas 273 ListView_DeleteAllItems(GetDlgItem(dialog, IDC_SELECT_SCANS));
713     break;
714     }
715     }
716     break;
717 douglas 277 case WM_CONTEXTMENU:
718 douglas 284 if (ListView_GetNextItem(GetDlgItem(dialog, IDC_SELECT_SCANS), -1,
719     LVNI_SELECTED) != -1)
720 douglas 277 {
721     char scan[MAX_PATH];
722     POINT spot;
723     LVHITTESTINFO test;
724    
725     test.pt.x = GET_X_LPARAM(l);
726     test.pt.y = GET_Y_LPARAM(l);
727    
728     ScreenToClient(GetDlgItem(dialog, IDC_SELECT_SCANS), &test.pt);
729     ListView_HitTest(GetDlgItem(dialog, IDC_SELECT_SCANS), &test);
730    
731     if (test.iItem != -1)
732     {
733     ListView_GetItemText(GetDlgItem(dialog, IDC_SELECT_SCANS),
734     test.iItem, 0, scan, MAX_PATH);
735    
736     spot.x = test.pt.x;
737     spot.y = test.pt.y;
738     }
739     else if (GET_X_LPARAM(l) == -1 && GET_Y_LPARAM(l) == -1)
740     {
741     int index = ListView_GetNextItem(GetDlgItem(dialog,
742     IDC_SELECT_SCANS), -1, LVNI_SELECTED);
743     RECT rect;
744    
745     ListView_GetItemText(GetDlgItem(dialog, IDC_SELECT_SCANS),
746     index, 0, scan, MAX_PATH);
747     ListView_EnsureVisible(GetDlgItem(dialog, IDC_SELECT_SCANS),
748     index, FALSE);
749     ListView_GetItemRect(GetDlgItem(dialog, IDC_SELECT_SCANS),
750     index, &rect, LVIR_SELECTBOUNDS);
751    
752     spot.x = rect.left;
753     spot.y = rect.top;
754     }
755    
756     ClientToScreen(GetDlgItem(dialog, IDC_SELECT_SCANS), &spot);
757    
758     int code = TrackPopupMenuEx(data->popup, TPM_LEFTALIGN |
759     TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD | TPM_RIGHTBUTTON,
760     spot.x, spot.y, GetDlgItem(dialog, IDC_SELECT_SCANS), NULL);
761    
762     switch (code)
763     {
764     case 1:
765     ShellExecute(dialog, NULL, scan, NULL, NULL, SW_SHOWDEFAULT);
766     break;
767     case 2:
768     {
769     SHELLEXECUTEINFO info;
770    
771     info.cbSize = sizeof(info);
772     info.fMask = SEE_MASK_INVOKEIDLIST;
773     info.hwnd = dialog;
774     info.lpVerb = "properties";
775     info.lpFile = scan;
776     info.lpParameters = NULL;
777     info.lpDirectory = NULL;
778     info.nShow = SW_SHOWDEFAULT;
779     info.lpIDList = NULL;
780    
781     ShellExecuteEx(&info);
782     }
783     break;
784     }
785     }
786 douglas 273 break;
787     }
788    
789     return FALSE;
790     }
791 douglas 274
792     INT_PTR ScanUtility::enter(HWND dialog, UINT msg, WPARAM w, LPARAM l)
793     {
794 douglas 277 ScanUtility* data = which(dialog);
795    
796     switch (msg)
797     {
798     case WM_INITDIALOG:
799     data = which(dialog, l);
800 douglas 278
801 douglas 277 {
802     ostringstream enter;
803    
804     enter << "Enter the client name and number.";
805    
806     SetDlgItemText(dialog, IDC_ENTER_TEXT, enter.str().c_str());
807     }
808     break;
809     case WM_NOTIFY:
810     {
811     LPNMHDR nm = LPNMHDR(l);
812    
813     switch (nm->code)
814     {
815     case PSN_SETACTIVE:
816     if (data->client.getName() != "" && data->client.getNumber() !=
817     0)
818     {
819     PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
820 douglas 278 PSWIZB_DISABLEDFINISH | PSWIZB_NEXT);
821 douglas 277 }
822     else
823     {
824 douglas 278 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
825     PSWIZB_DISABLEDFINISH);
826 douglas 277 }
827    
828     SetDlgItemText(dialog, IDC_ENTER_NAME,
829     data->client.getName().c_str());
830    
831     if (data->client.getNumber() != 0)
832     {
833     SetDlgItemInt(dialog, IDC_ENTER_NUM,
834     data->client.getNumber(), FALSE);
835     }
836     else
837     {
838     SetDlgItemText(dialog, IDC_ENTER_NUM, "");
839     }
840     break;
841     case PSN_WIZBACK:
842     break;
843     case PSN_WIZNEXT:
844     if (debug)
845     {
846     cerr << "client = {\n"
847     << " name = " << data->client.getName() << "\n"
848     << " number = " << data->client.getNumber() << "\n"
849 douglas 278 << " file = " << data->client.getFile() << "\n"
850 douglas 277 << "}\n";
851     }
852 douglas 278
853     data->save = data->saveDir + '\\' + data->client.getFile();
854    
855     if (debug) cerr << "save = " << data->save << "\n";
856    
857 douglas 277 break;
858     }
859     }
860     break;
861     case WM_COMMAND:
862     switch (LOWORD(w))
863     {
864     case IDC_ENTER_NAME:
865     {
866     char name[BUFSIZ];
867    
868     GetDlgItemText(dialog, IDC_ENTER_NAME, name, BUFSIZ);
869    
870     if (name != data->client.getName())
871     {
872     data->client.setName(name);
873     }
874     }
875    
876     if (data->client.getName() != "" && data->client.getNumber() != 0)
877     {
878     PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
879 douglas 278 PSWIZB_DISABLEDFINISH | PSWIZB_NEXT);
880 douglas 277 }
881     else
882     {
883 douglas 278 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
884     PSWIZB_DISABLEDFINISH);
885 douglas 277 }
886     break;
887     case IDC_ENTER_NUM:
888     {
889     unsigned number = GetDlgItemInt(dialog, IDC_ENTER_NUM, NULL,
890     FALSE);
891    
892     if (number != data->client.getNumber())
893     {
894     data->client.setNumber(number);
895     }
896     }
897    
898     if (data->client.getName() != "" && data->client.getNumber() != 0)
899     {
900     PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
901 douglas 278 PSWIZB_DISABLEDFINISH | PSWIZB_NEXT);
902 douglas 277 }
903     else
904     {
905 douglas 278 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
906     PSWIZB_DISABLEDFINISH);
907 douglas 277 }
908     break;
909     }
910     break;
911     }
912    
913 douglas 274 return FALSE;
914     }
915 douglas 278
916     INT_PTR ScanUtility::confirm(HWND dialog, UINT msg, WPARAM w, LPARAM l)
917     {
918     ScanUtility* data = which(dialog);
919    
920     switch (msg)
921     {
922     case WM_INITDIALOG:
923     data = which(dialog, l);
924    
925     {
926     ostringstream confirm;
927    
928     confirm << "Confirm the client file\'s name and size.";
929    
930     SetDlgItemText(dialog, IDC_CONFIRM_TEXT, confirm.str().c_str());
931     }
932     break;
933     case WM_NOTIFY:
934     {
935     LPNMHDR nm = LPNMHDR(l);
936    
937     switch (nm->code)
938     {
939     case PSN_SETACTIVE:
940     CheckDlgButton(dialog, IDC_CONFIRM_GOOD, BST_UNCHECKED);
941     PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
942     PSWIZB_DISABLEDFINISH);
943     SetDlgItemText(dialog, IDC_CONFIRM_FILE,
944     data->client.getFile().c_str());
945    
946     {
947     HANDLE scan = CreateFile(data->scan.c_str(), GENERIC_READ,
948     FILE_SHARE_READ, NULL, OPEN_EXISTING,
949     FILE_ATTRIBUTE_NORMAL, NULL);
950     DWORD bytes = GetFileSize(scan, NULL);
951    
952     CloseHandle(scan);
953    
954     ostringstream size;
955    
956     size << format(bytes);
957    
958     size.setf(ios_base::fixed, ios_base::floatfield);
959     size.precision(2);
960    
961     FLOAT megabytes = FLOAT(bytes) / FLOAT(1024 * 1024);
962    
963     size << " bytes (" << megabytes << " MB)";
964    
965     SetDlgItemText(dialog, IDC_CONFIRM_SIZE,
966     size.str().c_str());
967     }
968     break;
969     case PSN_WIZBACK:
970     break;
971     case PSN_WIZNEXT:
972     if (MoveFile(data->scan.c_str(), data->save.c_str()) == 0)
973     {
974     do
975     {
976     LPVOID message;
977    
978     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
979 douglas 282 FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
980     0, LPTSTR(&message), 0, NULL);
981 douglas 278
982     int code = MessageBox(dialog, LPCTSTR(message),
983 douglas 282 data->title.c_str(), MB_RETRYCANCEL |
984 douglas 278 MB_ICONEXCLAMATION);
985    
986     LocalFree(message);
987    
988     if (code == IDCANCEL)
989     {
990     PropSheet_PressButton(GetParent(dialog),
991     PSBTN_BACK);
992     break;
993     }
994     }
995     while (MoveFileEx(data->scan.c_str(), data->save.c_str(),
996     MOVEFILE_REPLACE_EXISTING) == 0);
997     }
998     break;
999     }
1000     }
1001     break;
1002     case WM_COMMAND:
1003     switch (LOWORD(w))
1004     {
1005     case IDC_CONFIRM_GOOD:
1006     if (IsDlgButtonChecked(dialog, IDC_CONFIRM_GOOD) == BST_CHECKED)
1007     {
1008     PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
1009     PSWIZB_DISABLEDFINISH | PSWIZB_NEXT);
1010     }
1011     else
1012     {
1013     PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
1014     PSWIZB_DISABLEDFINISH);
1015     }
1016     break;
1017     }
1018     break;
1019     }
1020    
1021     return FALSE;
1022     }
1023    
1024     INT_PTR ScanUtility::done(HWND dialog, UINT msg, WPARAM w, LPARAM l)
1025     {
1026     ScanUtility* data = which(dialog);
1027    
1028     switch (msg)
1029     {
1030     case WM_INITDIALOG:
1031     data = which(dialog, l);
1032 douglas 281
1033     {
1034     ostringstream done;
1035    
1036     done << "You are done saving the scanned document. Click Finish to"
1037     << " exit or click Back to return to the beginning.";
1038    
1039     SetDlgItemText(dialog, IDC_DONE_TEXT, done.str().c_str());
1040     }
1041 douglas 278 break;
1042     case WM_NOTIFY:
1043     {
1044     LPNMHDR nm = LPNMHDR(l);
1045    
1046     switch (nm->code)
1047     {
1048     case PSN_SETACTIVE:
1049     PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
1050     PSWIZB_FINISH);
1051     break;
1052     case PSN_WIZBACK:
1053     data->client.setFile("");
1054     PropSheet_SetCurSelByID(GetParent(dialog), IDD_START);
1055     break;
1056     case PSN_WIZFINISH:
1057     break;
1058     }
1059     }
1060     break;
1061     }
1062    
1063     return FALSE;
1064     }