ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/VTBFileUtil2/DiscBrowse.cxx
Revision: 285
Committed: 2003-09-02T20:40:35-07:00 (21 years, 9 months ago) by douglas
File size: 15425 byte(s)
Log Message:
The world turns.

File Contents

# Content
1 // Vance Thrift and Biller File Utility 2
2 //
3 // Douglas Thrift
4 //
5 // $Id: DiscBrowse.cxx,v 1.6 2003/09/03 03:40:35 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 = folder;
125 }
126 break;
127 case REG_SZ:
128 discDir = 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 = 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
266 if (clients.empty())
267 {
268 if (MessageBox(parent, "No client documents found.",
269 title.c_str(), MB_RETRYCANCEL | MB_ICONQUESTION) == IDCANCEL)
270 break;
271 }
272 }
273 while (clients.empty());
274
275 if (!clients.empty())
276 {
277 SHFILEINFO info;
278 HIMAGELIST icons =
279 HIMAGELIST(SHGetFileInfo((*clients.begin()).getFile().c_str(),
280 0, &info, sizeof(info), SHGFI_SMALLICON | SHGFI_SYSICONINDEX));
281
282 ListView_SetImageList(GetDlgItem(parent, IDC_BROWSE_DISC), icons,
283 LVSIL_SMALL);
284
285 if (debug) cerr << "clients = {\n";
286
287 for (set<IndividualClient>::iterator itor = clients.begin(); itor !=
288 clients.end(); itor++)
289 {
290 IndividualClient client = *itor;
291
292 if (debug) cerr << " {\n"
293 << " number = " << client.getNumber() << "\n"
294 << " name = " << client.getName() << "\n"
295 << " file = " << client.getFile() << "\n"
296 << " }\n";
297
298 char clientFile[MAX_PATH], clientNumber[MAX_PATH],
299 clientName[MAX_PATH], clientSize[MAX_PATH];
300 HANDLE fileSize = CreateFile(client.getFile().c_str(),
301 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
302 FILE_ATTRIBUTE_NORMAL, NULL);
303 DWORD bytes = GetFileSize(fileSize, NULL);
304
305 CloseHandle(fileSize);
306 sprintf(clientFile, "%s", client.getFile().c_str());
307 sprintf(clientNumber, "%u", client.getNumber());
308 sprintf(clientName, "%s", client.getName().c_str());
309 sprintf(clientSize, "%.2f MB", FLOAT(bytes) / FLOAT(1024 * 1024));
310
311 LVITEM number, name, file, size;
312
313 file.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT;
314 file.iItem = 0;
315 file.iSubItem = 0;
316 file.pszText = clientFile;
317 file.iImage = info.iIcon;
318 file.lParam = bytes;
319
320 int item = ListView_InsertItem(GetDlgItem(parent, IDC_BROWSE_DISC),
321 &file);
322
323 number.mask = LVIF_TEXT;
324 number.iItem = item;
325 number.iSubItem = 1;
326 number.pszText = clientNumber;
327 name.mask = LVIF_TEXT;
328 name.iItem = item;
329 name.iSubItem = 2;
330 name.pszText = clientName;
331 size.mask = LVIF_TEXT;
332 size.iItem = item;
333 size.iSubItem = 3;
334 size.pszText = clientSize;
335
336 ListView_SetItem(GetDlgItem(parent, IDC_BROWSE_DISC), &number);
337 ListView_SetItem(GetDlgItem(parent, IDC_BROWSE_DISC), &name);
338 ListView_SetItem(GetDlgItem(parent, IDC_BROWSE_DISC), &size);
339 }
340
341 if (debug) cerr << "}\n";
342
343 Thing thing;
344
345 sortFile = true, sortNumber = true, sortName = true, sortSize = true;
346 thing.index = 1;
347 thing.order = sortNumber;
348 thing.list = GetDlgItem(parent, IDC_BROWSE_DISC);
349 sortNumber = !sortNumber;
350
351 ListView_SortItemsEx(thing.list, sort, &thing);
352 }
353 }
354
355 DiscBrowse* DiscBrowse::which(HWND window)
356 {
357 map<HWND, DiscBrowse*>::iterator itor = windows.find(window);
358
359 return itor->second;
360 }
361
362 DiscBrowse* DiscBrowse::which(HWND window, LPARAM l)
363 {
364 LPPROPSHEETPAGE page = LPPROPSHEETPAGE(l);
365 map<unsigned, DiscBrowse*>::iterator itor = browsers.find(page->lParam);
366
367 windows.insert(pair<HWND, DiscBrowse*>(window, itor->second));
368
369 return itor->second;
370 }
371
372 int DiscBrowse::browse(HWND dialog, UINT msg, LPARAM l, LPARAM d)
373 {
374 map<unsigned, DiscBrowse*>::iterator itor = browsers.find(LOWORD(d));
375 DiscBrowse* data = itor->second;
376
377 switch (msg)
378 {
379 case BFFM_INITIALIZED:
380 center(dialog);
381 SendMessage(dialog, BFFM_SETOKTEXT, 0,
382 LPARAM(toWide("&Select").c_str()));
383 SendMessage(dialog, BFFM_SETEXPANDED, FALSE, CSIDL_DRIVES);
384 SendMessage(dialog, BFFM_SETSELECTION, TRUE,
385 LPARAM(data->discDir.c_str()));
386 break;
387 case BFFM_SELCHANGED:
388 {
389 SHFILEINFO info;
390
391 SHGetFileInfo(LPCSTR(l), 0, &info, sizeof(info), SHGFI_DISPLAYNAME
392 | SHGFI_PIDL);
393 SendMessage(dialog, BFFM_SETSTATUSTEXT, 0,
394 LPARAM(info.szDisplayName));
395
396 char folder[MAX_PATH];
397
398 if (!SHGetPathFromIDList(LPCITEMIDLIST(l), folder))
399 {
400 SendMessage(dialog, BFFM_ENABLEOK, 0, 0);
401 }
402 }
403 break;
404 }
405
406 return 0;
407 }
408
409 int DiscBrowse::sort(LPARAM first, LPARAM second, LPARAM l)
410 {
411 Thing* thing = (Thing*)(l);
412
413 switch (thing->index)
414 {
415 case 0:
416 {
417 char one[MAX_PATH], two[MAX_PATH];
418
419 ListView_GetItemText(thing->list, first, 0, one, MAX_PATH);
420 ListView_GetItemText(thing->list, second, 0, two, MAX_PATH);
421
422 string first = one, second = two;
423
424 if (first < second)
425 {
426 return thing->order ? -1 : 1;
427 }
428 else if (first > second)
429 {
430 return thing->order ? 1 : -1;
431 }
432 }
433 break;
434 case 1:
435 {
436 char one[MAX_PATH], two[MAX_PATH];
437
438 ListView_GetItemText(thing->list, first, 0, one, MAX_PATH);
439 ListView_GetItemText(thing->list, second, 0, two, MAX_PATH);
440
441 IndividualClient first(one), second(two);
442
443 if (first < second)
444 {
445 return thing->order ? -1 : 1;
446 }
447 else if (first > second)
448 {
449 return thing->order ? 1 : -1;
450 }
451 }
452 break;
453 case 2:
454 {
455 char one[MAX_PATH], two[MAX_PATH];
456
457 ListView_GetItemText(thing->list, first, 2, one, MAX_PATH);
458 ListView_GetItemText(thing->list, second, 2, two, MAX_PATH);
459
460 if (mcLess(one, two))
461 {
462 return thing->order ? -1 : 1;
463 }
464 else if (mcGreater(one, two))
465 {
466 return thing->order ? 1 : -1;
467 }
468 }
469 break;
470 case 3:
471 if (first < second)
472 {
473 return thing->order ? -1 : 1;
474 }
475 else if (first > second)
476 {
477 return thing->order ? 1 : -1;
478 }
479 break;
480 }
481
482 return 0;
483 }
484
485 INT_PTR DiscBrowse::start(HWND dialog, UINT msg, WPARAM w, LPARAM l)
486 {
487 DiscBrowse* data = which(dialog);
488
489 switch (msg)
490 {
491 case WM_INITDIALOG:
492 center(GetParent(dialog));
493 SendMessage(GetParent(dialog), WM_SETICON, ICON_BIG, LPARAM(gui.icon));
494
495 data = which(dialog, l);
496 break;
497 case WM_NOTIFY:
498 {
499 LPNMHDR nm = LPNMHDR(l);
500
501 switch (nm->code)
502 {
503 case PSN_SETACTIVE:
504 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_FINISH |
505 PSWIZB_NEXT);
506
507 {
508 SHFILEINFO info;
509
510 SHGetFileInfo(data->discDir.c_str(), 0, &info,
511 sizeof(info), SHGFI_ICONLOCATION);
512
513 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
514 info.iIcon);
515
516 SendDlgItemMessage(dialog, IDC_BEGIN_ICON, STM_SETIMAGE,
517 IMAGE_ICON, LPARAM(icon));
518 }
519
520 {
521 SHFILEINFO info;
522
523 SHGetFileInfo(data->discDir.c_str(), 0, &info,
524 sizeof(info), SHGFI_DISPLAYNAME);
525
526 SetDlgItemText(dialog, IDC_BEGIN_TEXT, info.szDisplayName);
527 }
528 break;
529 case PSN_WIZNEXT:
530 SetCurrentDirectory(data->discDir.c_str());
531 break;
532 }
533 }
534 break;
535 case WM_COMMAND:
536 switch (LOWORD(w))
537 {
538 case IDC_BEGIN_BROWSE:
539 data->setDiscDir(dialog);
540
541 {
542 SHFILEINFO info;
543
544 SHGetFileInfo(data->discDir.c_str(), 0, &info, sizeof(info),
545 SHGFI_ICONLOCATION);
546
547 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
548 info.iIcon);
549
550 SendDlgItemMessage(dialog, IDC_BEGIN_ICON, STM_SETIMAGE,
551 IMAGE_ICON, LPARAM(icon));
552 }
553
554 {
555 SHFILEINFO info;
556
557 SHGetFileInfo(data->discDir.c_str(), 0, &info,
558 sizeof(info), SHGFI_DISPLAYNAME);
559
560 SetDlgItemText(dialog, IDC_BEGIN_TEXT, info.szDisplayName);
561 }
562 break;
563 }
564 }
565
566 return FALSE;
567 }
568
569 INT_PTR DiscBrowse::browse(HWND dialog, UINT msg, WPARAM w, LPARAM l)
570 {
571 DiscBrowse* data = which(dialog);
572
573 switch (msg)
574 {
575 case WM_INITDIALOG:
576 data = which(dialog, l);
577
578 ListView_SetExtendedListViewStyleEx(GetDlgItem(dialog,
579 IDC_BROWSE_DISC), LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP |
580 LVS_EX_LABELTIP, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP |
581 LVS_EX_LABELTIP);
582
583 {
584 LVCOLUMN file, number, name, size;
585
586 file.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
587 file.fmt = LVCFMT_IMAGE;
588 file.cx = 224;
589 file.pszText = "File Name";
590 number.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
591 number.cx = 56;
592 number.pszText = "Number";
593 number.iSubItem = 1;
594 name.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
595 name.cx = 152;
596 name.pszText = "Client Name";
597 name.iSubItem = 2;
598 size.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
599 size.cx = 68;
600 size.pszText = "Size";
601 size.iSubItem = 3;
602
603 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 0,
604 &file);
605 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 1,
606 &number);
607 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 2,
608 &name);
609 ListView_InsertColumn(GetDlgItem(dialog, IDC_BROWSE_DISC), 3,
610 &size);
611 }
612 break;
613 case WM_NOTIFY:
614 if (w == IDC_BROWSE_DISC)
615 {
616 LPNMITEMACTIVATE ni = LPNMITEMACTIVATE(l);
617
618 switch (ni->hdr.code)
619 {
620 case LVN_COLUMNCLICK:
621 {
622 Thing thing;
623
624 thing.index = ni->iSubItem;
625
626 switch (thing.index)
627 {
628 case 0:
629 thing.order = data->sortFile;
630 data->sortFile = !data->sortFile;
631 break;
632 case 1:
633 thing.order = data->sortNumber;
634 data->sortNumber = !data->sortNumber;
635 break;
636 case 2:
637 thing.order = data->sortName;
638 data->sortName = !data->sortName;
639 break;
640 case 3:
641 thing.order = data->sortSize;
642 data->sortSize = !data->sortSize;
643 break;
644 }
645
646 thing.list = GetDlgItem(dialog, IDC_BROWSE_DISC);
647
648 if (ni->iSubItem == 3)
649 {
650 ListView_SortItems(thing.list, sort, &thing);
651 }
652 else
653 {
654 ListView_SortItemsEx(thing.list, sort, &thing);
655 }
656 }
657 break;
658 case NM_DBLCLK:
659 if (ni->iItem != -1)
660 {
661 //
662 }
663 break;
664 }
665 }
666 else
667 {
668 LPNMHDR nm = LPNMHDR(l);
669
670 switch (nm->code)
671 {
672 case PSN_SETACTIVE:
673 PropSheet_SetWizButtons(GetParent(dialog), PSWIZB_BACK |
674 PSWIZB_FINISH);
675 data->populate(dialog);
676
677 {
678 SHFILEINFO info;
679
680 SHGetFileInfo(data->discDir.c_str(), 0, &info,
681 sizeof(info), SHGFI_ICONLOCATION);
682
683 HICON icon = ExtractIcon(gui.instance, info.szDisplayName,
684 info.iIcon);
685
686 SendDlgItemMessage(dialog, IDC_BROWSE_ICON, STM_SETIMAGE,
687 IMAGE_ICON, LPARAM(icon));
688 }
689
690 {
691 SHFILEINFO info;
692
693 SHGetFileInfo(data->discDir.c_str(), 0, &info,
694 sizeof(info), SHGFI_DISPLAYNAME);
695
696 SetDlgItemText(dialog, IDC_BROWSE_TEXT,
697 info.szDisplayName);
698 }
699 break;
700 case PSN_WIZBACK:
701 ListView_DeleteAllItems(GetDlgItem(dialog, IDC_BROWSE_DISC));
702 break;
703 }
704 }
705 break;
706 }
707
708 return FALSE;
709 }