ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/VTBFileUtil2/DiscBrowse.cxx
Revision: 288
Committed: 2003-09-03T15:58:38-07:00 (21 years, 9 months ago) by douglas
File size: 15474 byte(s)
Log Message:
Ran into some trouble on Windows 95, hopefully its fixed.

File Contents

# Content
1 // Vance Thrift and Biller File Utility 2
2 //
3 // Douglas Thrift
4 //
5 // $Id: DiscBrowse.cxx,v 1.7 2003/09/03 22:58:37 douglas Exp $
6
7 #include "DiscBrowse.h"
8
9 DiscBrowse::DiscBrowse()
10 {
11 number = count++;
12
13 browsers.insert(pair<unsigned, DiscBrowse*>(number, this));
14
15 title = programName + " - Disc Browse";
16 popup = CreatePopupMenu();
17
18 MENUITEMINFO open, separator, properties;
19
20 open.cbSize = sizeof(open);
21 open.fMask = MIIM_ID | MIIM_STATE | MIIM_TYPE;
22 open.fType = MFT_STRING;
23 open.fState = MFS_DEFAULT;
24 open.wID = 1;
25 open.dwTypeData = "&Open";
26 open.cch = 5;
27 separator.cbSize = sizeof(open);
28 separator.fMask = MIIM_TYPE;
29 separator.fType = MFT_SEPARATOR;
30 properties.cbSize = sizeof(open);
31 properties.fMask = MIIM_ID | MIIM_TYPE;
32 properties.fType = MFT_STRING;
33 properties.wID = 2;
34 properties.dwTypeData = "P&roperties";
35 properties.cch = 11;
36
37 InsertMenuItem(popup, 0, TRUE, &open);
38 InsertMenuItem(popup, 1, TRUE, &separator);
39 InsertMenuItem(popup, 2, TRUE, &properties);
40
41 // start
42 wizard[0].dwSize = sizeof(wizard[0]);
43 wizard[0].dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USEHEADERTITLE |
44 PSP_USEHEADERSUBTITLE;
45 wizard[0].hInstance = gui.instance;
46 wizard[0].pszTemplate = MAKEINTRESOURCE(IDD_BEGIN);
47 wizard[0].pszTitle = title.c_str();
48 wizard[0].pfnDlgProc = start;
49 wizard[0].lParam = number;
50 wizard[0].pszHeaderTitle = "Start";
51 wizard[0].pszHeaderSubTitle = "Change any settings before browsing.";
52
53 // browse
54 wizard[1].dwSize = sizeof(wizard[1]);
55 wizard[1].dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USEHEADERTITLE |
56 PSP_USEHEADERSUBTITLE;
57 wizard[1].hInstance = gui.instance;
58 wizard[1].pszTemplate = MAKEINTRESOURCE(IDD_BROWSE);
59 wizard[1].pszTitle = title.c_str();
60 wizard[1].pfnDlgProc = browse;
61 wizard[1].lParam = number;
62 wizard[1].pszHeaderTitle = "Browse";
63 wizard[1].pszHeaderSubTitle = "View documents on the disc.";
64 }
65
66 DiscBrowse::~DiscBrowse()
67 {
68 DestroyMenu(popup);
69 browsers.erase(number);
70 }
71
72 void DiscBrowse::run(void)
73 {
74 loadDir();
75
76 PROPSHEETHEADER header;
77
78 // header
79 header.dwSize = sizeof(header);
80 header.dwFlags = PSH_DEFAULT | PSH_HEADER | PSH_PROPSHEETPAGE |
81 PSH_USEICONID | PSH_WIZARD97 | PSH_WIZARDHASFINISH;
82 header.hwndParent = NULL;
83 header.hInstance = gui.instance;
84 header.pszIcon = MAKEINTRESOURCE(IDI_VTB_ICON);
85 header.nPages = 2;
86 header.nStartPage = 0;
87 header.ppsp = wizard;
88 header.pszbmHeader = MAKEINTRESOURCE(IDB_VTB_BMP);
89
90 PropertySheet(&header);
91 saveDir();
92 }
93
94 unsigned DiscBrowse::count = 0;
95 map<unsigned, DiscBrowse*> DiscBrowse::browsers;
96
97 map<HWND, DiscBrowse*> DiscBrowse::windows;
98
99 void DiscBrowse::loadDir(void)
100 {
101 HKEY key;
102
103 if (RegOpenKeyEx(HKEY_CURRENT_USER,
104 "Software\\DouglasThrift\\VTBFileUtil2", 0, KEY_QUERY_VALUE, &key) ==
105 ERROR_SUCCESS)
106 {
107 DWORD type;
108 char data[MAX_PATH];
109 DWORD size = MAX_PATH;
110
111 if (RegQueryValueEx(key, "DiscDir", NULL, &type, LPBYTE(data), &size)
112 == ERROR_SUCCESS)
113 {
114 data[size - 1] = '\0';
115
116 switch (type)
117 {
118 case REG_EXPAND_SZ:
119 {
120 char folder[MAX_PATH];
121
122 ExpandEnvironmentStrings(data, folder, MAX_PATH);
123
124 discDir = tail(folder);
125 }
126 break;
127 case REG_SZ:
128 discDir = tail(data);
129 break;
130 default:
131 setDiscDir();
132 break;
133 }
134 }
135 else
136 {
137 setDiscDir();
138 }
139
140 RegCloseKey(key);
141 }
142 else
143 {
144 setDiscDir();
145 }
146
147 if (debug) cerr << "discDir = " << discDir << "\n";
148 }
149
150 void DiscBrowse::saveDir(void)
151 {
152 HKEY key;
153
154 if (RegCreateKeyEx(HKEY_CURRENT_USER,
155 "Software\\DouglasThrift\\VTBFileUtil2", 0, NULL,
156 REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE | KEY_SET_VALUE, NULL, &key,
157 NULL) == ERROR_SUCCESS)
158 {
159 DWORD type;
160 char data[MAX_PATH];
161 DWORD size = MAX_PATH;
162
163 if (RegQueryValueEx(key, "DiscDir", NULL, &type, LPBYTE(data), &size)
164 == ERROR_SUCCESS)
165 {
166 data[size - 1] = '\0';
167 }
168 else
169 {
170 data[0] = '\0';
171 }
172
173 if (discDir != data || type != REG_SZ)
174 {
175 if (RegSetValueEx(key, "DiscDir", 0, REG_SZ,
176 LPBYTE(discDir.c_str()), discDir.length() + 1) !=
177 ERROR_SUCCESS)
178 {
179 error();
180 }
181 }
182
183 RegCloseKey(key);
184 }
185 else
186 {
187 error();
188 }
189 }
190
191 void DiscBrowse::setDiscDir(HWND parent)
192 {
193 BROWSEINFO info;
194
195 info.hwndOwner = parent;
196 info.pidlRoot = NULL;
197 info.pszDisplayName = NULL;
198 info.lpszTitle = "Select the Disc";
199 info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
200 info.lpfn = browse;
201 info.lParam = MAKELPARAM(number, true);
202 info.iImage = 0;
203
204 do
205 {
206 LPITEMIDLIST id = SHBrowseForFolder(&info);
207
208 if (id != NULL)
209 {
210 char folder[MAX_PATH];
211
212 if (SHGetPathFromIDList(id, folder)) discDir = tail(folder);
213 }
214
215 LPMALLOC destruct;
216
217 SHGetMalloc(&destruct);
218 destruct->Free(id);
219 destruct->Release();
220
221 if (discDir == "")
222 {
223 switch (MessageBox(parent, "Disc needs to be selected.",
224 title.c_str(), MB_ABORTRETRYIGNORE | MB_ICONERROR))
225 {
226 case IDABORT:
227 exit(1);
228 break;
229 case IDRETRY:
230 break;
231 case IDIGNORE:
232 Beep(2200, 250);
233 Beep(1100, 500);
234 Beep(3300, 250);
235 exit(2);
236 break;
237 }
238 }
239 }
240 while (discDir == "");
241
242 if (debug) cerr << "discDir = " << discDir << "\n";
243 }
244
245 void DiscBrowse::populate(HWND parent)
246 {
247 set<IndividualClient> clients;
248
249 do
250 {
251 string client = discDir + "*_*." + IndividualClient::getExtension();
252 WIN32_FIND_DATA found;
253 HANDLE finder = FindFirstFile(client.c_str(), &found);
254
255 if (finder != INVALID_HANDLE_VALUE)
256 {
257 do
258 {
259 clients.insert(IndividualClient(found.cFileName));
260 }
261 while (FindNextFile(finder, &found));
262
263 FindClose(finder);
264 }
265 else
266 {
267 error(parent);
268 }
269
270 if (clients.empty())
271 {
272 if (MessageBox(parent, "No client documents found.",
273 title.c_str(), MB_RETRYCANCEL | MB_ICONQUESTION) == IDCANCEL)
274 break;
275 }
276 }
277 while (clients.empty());
278
279 if (!clients.empty())
280 {
281 SHFILEINFO info;
282 HIMAGELIST icons =
283 HIMAGELIST(SHGetFileInfo((*clients.begin()).getFile().c_str(),
284 0, &info, sizeof(info), SHGFI_SMALLICON | SHGFI_SYSICONINDEX));
285
286 ListView_SetImageList(GetDlgItem(parent, IDC_BROWSE_DISC), icons,
287 LVSIL_SMALL);
288
289 if (debug) cerr << "clients = {\n";
290
291 for (set<IndividualClient>::iterator itor = clients.begin(); itor !=
292 clients.end(); itor++)
293 {
294 IndividualClient client = *itor;
295
296 if (debug) cerr << " {\n"
297 << " number = " << client.getNumber() << "\n"
298 << " name = " << client.getName() << "\n"
299 << " file = " << client.getFile() << "\n"
300 << " }\n";
301
302 char clientFile[MAX_PATH], clientNumber[MAX_PATH],
303 clientName[MAX_PATH], clientSize[MAX_PATH];
304 HANDLE fileSize = CreateFile(client.getFile().c_str(),
305 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
306 FILE_ATTRIBUTE_NORMAL, NULL);
307 DWORD bytes = GetFileSize(fileSize, NULL);
308
309 CloseHandle(fileSize);
310 sprintf(clientFile, "%s", client.getFile().c_str());
311 sprintf(clientNumber, "%u", client.getNumber());
312 sprintf(clientName, "%s", client.getName().c_str());
313 sprintf(clientSize, "%.2f MB", FLOAT(bytes) / FLOAT(1024 * 1024));
314
315 LVITEM number, name, file, size;
316
317 file.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT;
318 file.iItem = 0;
319 file.iSubItem = 0;
320 file.pszText = clientFile;
321 file.iImage = info.iIcon;
322 file.lParam = bytes;
323
324 int item = ListView_InsertItem(GetDlgItem(parent, IDC_BROWSE_DISC),
325 &file);
326
327 number.mask = LVIF_TEXT;
328 number.iItem = item;
329 number.iSubItem = 1;
330 number.pszText = clientNumber;
331 name.mask = LVIF_TEXT;
332 name.iItem = item;
333 name.iSubItem = 2;
334 name.pszText = clientName;
335 size.mask = LVIF_TEXT;
336 size.iItem = item;
337 size.iSubItem = 3;
338 size.pszText = clientSize;
339
340 ListView_SetItem(GetDlgItem(parent, IDC_BROWSE_DISC), &number);
341 ListView_SetItem(GetDlgItem(parent, IDC_BROWSE_DISC), &name);
342 ListView_SetItem(GetDlgItem(parent, IDC_BROWSE_DISC), &size);
343 }
344
345 if (debug) cerr << "}\n";
346
347 Thing thing;
348
349 sortFile = true, sortNumber = true, sortName = true, sortSize = true;
350 thing.index = 1;
351 thing.order = sortNumber;
352 thing.list = GetDlgItem(parent, IDC_BROWSE_DISC);
353 sortNumber = !sortNumber;
354
355 ListView_SortItemsEx(thing.list, sort, &thing);
356 }
357 }
358
359 DiscBrowse* DiscBrowse::which(HWND window)
360 {
361 map<HWND, DiscBrowse*>::iterator itor = windows.find(window);
362
363 return itor->second;
364 }
365
366 DiscBrowse* DiscBrowse::which(HWND window, LPARAM l)
367 {
368 LPPROPSHEETPAGE page = LPPROPSHEETPAGE(l);
369 map<unsigned, DiscBrowse*>::iterator itor = browsers.find(page->lParam);
370
371 windows.insert(pair<HWND, DiscBrowse*>(window, itor->second));
372
373 return itor->second;
374 }
375
376 int DiscBrowse::browse(HWND dialog, UINT msg, LPARAM l, LPARAM d)
377 {
378 map<unsigned, DiscBrowse*>::iterator itor = browsers.find(LOWORD(d));
379 DiscBrowse* data = itor->second;
380
381 switch (msg)
382 {
383 case BFFM_INITIALIZED:
384 center(dialog);
385 SendMessage(dialog, BFFM_SETOKTEXT, 0,
386 LPARAM(toWide("&Select").c_str()));
387 SendMessage(dialog, BFFM_SETEXPANDED, FALSE, CSIDL_DRIVES);
388 SendMessage(dialog, BFFM_SETSELECTION, TRUE,
389 LPARAM(data->discDir.c_str()));
390 break;
391 case BFFM_SELCHANGED:
392 {
393 SHFILEINFO info;
394
395 SHGetFileInfo(LPCSTR(l), 0, &info, sizeof(info), SHGFI_DISPLAYNAME
396 | SHGFI_PIDL);
397 SendMessage(dialog, BFFM_SETSTATUSTEXT, 0,
398 LPARAM(info.szDisplayName));
399
400 char folder[MAX_PATH];
401
402 if (!SHGetPathFromIDList(LPCITEMIDLIST(l), folder))
403 {
404 SendMessage(dialog, BFFM_ENABLEOK, 0, 0);
405 }
406 }
407 break;
408 }
409
410 return 0;
411 }
412
413 int DiscBrowse::sort(LPARAM first, LPARAM second, LPARAM l)
414 {
415 Thing* thing = (Thing*)(l);
416
417 switch (thing->index)
418 {
419 case 0:
420 {
421 char one[MAX_PATH], two[MAX_PATH];
422
423 ListView_GetItemText(thing->list, first, 0, one, MAX_PATH);
424 ListView_GetItemText(thing->list, second, 0, two, MAX_PATH);
425
426 string first = one, second = two;
427
428 if (first < second)
429 {
430 return thing->order ? -1 : 1;
431 }
432 else if (first > second)
433 {
434 return thing->order ? 1 : -1;
435 }
436 }
437 break;
438 case 1:
439 {
440 char one[MAX_PATH], two[MAX_PATH];
441
442 ListView_GetItemText(thing->list, first, 0, one, MAX_PATH);
443 ListView_GetItemText(thing->list, second, 0, two, MAX_PATH);
444
445 IndividualClient first(one), second(two);
446
447 if (first < second)
448 {
449 return thing->order ? -1 : 1;
450 }
451 else if (first > second)
452 {
453 return thing->order ? 1 : -1;
454 }
455 }
456 break;
457 case 2:
458 {
459 char one[MAX_PATH], two[MAX_PATH];
460
461 ListView_GetItemText(thing->list, first, 2, one, MAX_PATH);
462 ListView_GetItemText(thing->list, second, 2, two, MAX_PATH);
463
464 if (mcLess(one, two))
465 {
466 return thing->order ? -1 : 1;
467 }
468 else if (mcGreater(one, two))
469 {
470 return thing->order ? 1 : -1;
471 }
472 }
473 break;
474 case 3:
475 if (first < second)
476 {
477 return thing->order ? -1 : 1;
478 }
479 else if (first > second)
480 {
481 return thing->order ? 1 : -1;
482 }
483 break;
484 }
485
486 return 0;
487 }
488
489 INT_PTR DiscBrowse::start(HWND dialog, UINT msg, WPARAM w, LPARAM l)
490 {
491 DiscBrowse* data = which(dialog);
492
493 switch (msg)
494 {
495 case WM_INITDIALOG:
496 center(GetParent(dialog));
497 SendMessage(GetParent(dialog), WM_SETICON, ICON_BIG, LPARAM(gui.icon));
498
499 data = which(dialog, l);
500 break;
501 case WM_NOTIFY:
502 {
503 LPNMHDR nm = LPNMHDR(l);
504
505 switch (nm->code)
506 {
507 case PSN_SETACTIVE:
508 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_FINISH |
509 PSWIZB_NEXT);
510
511 {
512 SHFILEINFO info;
513
514 SHGetFileInfo(data->discDir.c_str(), 0, &info,
515 sizeof(info), SHGFI_ICONLOCATION);
516
517 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
518 info.iIcon);
519
520 SendDlgItemMessage(dialog, IDC_BEGIN_ICON, STM_SETIMAGE,
521 IMAGE_ICON, LPARAM(icon));
522 }
523
524 {
525 SHFILEINFO info;
526
527 SHGetFileInfo(data->discDir.c_str(), 0, &info,
528 sizeof(info), SHGFI_DISPLAYNAME);
529
530 SetDlgItemText(dialog, IDC_BEGIN_TEXT, info.szDisplayName);
531 }
532 break;
533 case PSN_WIZNEXT:
534 SetCurrentDirectory(data->discDir.c_str());
535 break;
536 }
537 }
538 break;
539 case WM_COMMAND:
540 switch (LOWORD(w))
541 {
542 case IDC_BEGIN_BROWSE:
543 data->setDiscDir(dialog);
544
545 {
546 SHFILEINFO info;
547
548 SHGetFileInfo(data->discDir.c_str(), 0, &info, sizeof(info),
549 SHGFI_ICONLOCATION);
550
551 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
552 info.iIcon);
553
554 SendDlgItemMessage(dialog, IDC_BEGIN_ICON, STM_SETIMAGE,
555 IMAGE_ICON, LPARAM(icon));
556 }
557
558 {
559 SHFILEINFO info;
560
561 SHGetFileInfo(data->discDir.c_str(), 0, &info,
562 sizeof(info), SHGFI_DISPLAYNAME);
563
564 SetDlgItemText(dialog, IDC_BEGIN_TEXT, info.szDisplayName);
565 }
566 break;
567 }
568 }
569
570 return FALSE;
571 }
572
573 INT_PTR DiscBrowse::browse(HWND dialog, UINT msg, WPARAM w, LPARAM l)
574 {
575 DiscBrowse* data = which(dialog);
576
577 switch (msg)
578 {
579 case WM_INITDIALOG:
580 data = which(dialog, l);
581
582 ListView_SetExtendedListViewStyleEx(GetDlgItem(dialog,
583 IDC_BROWSE_DISC), LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP |
584 LVS_EX_LABELTIP, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP |
585 LVS_EX_LABELTIP);
586
587 {
588 LVCOLUMN file, number, name, size;
589
590 file.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
591 file.fmt = LVCFMT_IMAGE;
592 file.cx = 224;
593 file.pszText = "File Name";
594 number.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
595 number.cx = 56;
596 number.pszText = "Number";
597 number.iSubItem = 1;
598 name.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
599 name.cx = 152;
600 name.pszText = "Client Name";
601 name.iSubItem = 2;
602 size.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
603 size.cx = 68;
604 size.pszText = "Size";
605 size.iSubItem = 3;
606
607 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 0,
608 &file);
609 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 1,
610 &number);
611 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 2,
612 &name);
613 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 3,
614 &size);
615 }
616 break;
617 case WM_NOTIFY:
618 if (w == IDC_BROWSE_DISC)
619 {
620 LPNMITEMACTIVATE ni = LPNMITEMACTIVATE(l);
621
622 switch (ni->hdr.code)
623 {
624 case LVN_COLUMNCLICK:
625 {
626 Thing thing;
627
628 thing.index = ni->iSubItem;
629
630 switch (thing.index)
631 {
632 case 0:
633 thing.order = data->sortFile;
634 data->sortFile = !data->sortFile;
635 break;
636 case 1:
637 thing.order = data->sortNumber;
638 data->sortNumber = !data->sortNumber;
639 break;
640 case 2:
641 thing.order = data->sortName;
642 data->sortName = !data->sortName;
643 break;
644 case 3:
645 thing.order = data->sortSize;
646 data->sortSize = !data->sortSize;
647 break;
648 }
649
650 thing.list = GetDlgItem(dialog, IDC_BROWSE_DISC);
651
652 if (ni->iSubItem == 3)
653 {
654 ListView_SortItems(thing.list, sort, &thing);
655 }
656 else
657 {
658 ListView_SortItemsEx(thing.list, sort, &thing);
659 }
660 }
661 break;
662 case NM_DBLCLK:
663 if (ni->iItem != -1)
664 {
665 //
666 }
667 break;
668 }
669 }
670 else
671 {
672 LPNMHDR nm = LPNMHDR(l);
673
674 switch (nm->code)
675 {
676 case PSN_SETACTIVE:
677 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
678 PSWIZB_FINISH);
679 data->populate(dialog);
680
681 {
682 SHFILEINFO info;
683
684 SHGetFileInfo(data->discDir.c_str(), 0, &info,
685 sizeof(info), SHGFI_ICONLOCATION);
686
687 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
688 info.iIcon);
689
690 SendDlgItemMessage(dialog, IDC_BROWSE_ICON, STM_SETIMAGE,
691 IMAGE_ICON, LPARAM(icon));
692 }
693
694 {
695 SHFILEINFO info;
696
697 SHGetFileInfo(data->discDir.c_str(), 0, &info,
698 sizeof(info), SHGFI_DISPLAYNAME);
699
700 SetDlgItemText(dialog, IDC_BROWSE_TEXT,
701 info.szDisplayName);
702 }
703 break;
704 case PSN_WIZBACK:
705 ListView_DeleteAllItems(GetDlgItem(dialog, IDC_BROWSE_DISC));
706 break;
707 }
708 }
709 break;
710 }
711
712 return FALSE;
713 }