Subversion Repositories pentevo

Rev

Rev 796 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "std.h"
  2.  
  3. #include "resource.h"
  4.  
  5. #include "emul.h"
  6. #include "vars.h"
  7. #include "gui.h"
  8. #include "opendlg.h"
  9.  
  10. #include "util.h"
  11.  
  12. static struct FILEPREVIEWINFO
  13. {
  14.    OPENFILENAME *ofn;
  15.    struct { HWND h; int dx,dy; } list;
  16.    struct { HWND h; } dlg;
  17.    struct { HWND h; } innerdlg;
  18.  
  19.    void OnResize();
  20.    void OnChange();
  21.  
  22.    void PreviewTRD(char *filename);
  23.    void PreviewSCL(char *filename);
  24.    void Preview(unsigned char *cat);
  25.  
  26. } FilePreviewInfo;
  27.  
  28. void FILEPREVIEWINFO::OnResize()
  29. {
  30.    const int dlgbase = 280;
  31.    const int listbase = 136;
  32.  
  33.    RECT dlgrc; GetWindowRect(dlg.h, &dlgrc);
  34.    list.dy = (dlgrc.bottom - dlgrc.top) - dlgbase + listbase;
  35.  
  36.    SetWindowPos(list.h, nullptr, 0, 0, list.dx, list.dy,
  37.                   SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOACTIVATE);
  38. }
  39.  
  40. void FILEPREVIEWINFO::OnChange()
  41. {
  42.    char filename[512];
  43.    int r = int(SendMessage(dlg.h, CDM_GETFILEPATH, sizeof(filename), (LPARAM) filename));
  44.    SendMessage(list.h, LVM_DELETEALLITEMS, 0, 0);
  45.    if (r < 0 || (GetFileAttributes(filename) & FILE_ATTRIBUTE_DIRECTORY)) return;
  46.  
  47.    #if 0 // too slow for every file
  48.    TRKCACHE t;
  49.    FDD TestDrive;
  50.    unsigned char type = what_is(filename);
  51.    if (type < snSCL) return;
  52.    TestDrive.emptydisk();
  53.    if (!TestDrive.read(type)) return;
  54.    #endif
  55.  
  56.    char *ext = strrchr(filename, '.');
  57.    if (!ext) return;
  58.    ext++;
  59.  
  60.    if (!stricmp(ext, "trd")) PreviewTRD(filename);
  61.    if (!stricmp(ext, "scl")) PreviewSCL(filename);
  62. }
  63.  
  64. void FILEPREVIEWINFO::Preview(unsigned char *cat)
  65. {
  66.    ::dlg = innerdlg.h;
  67.    unsigned char bas = getcheck(IDC_PREVIEW_BASIC);
  68.    unsigned char del = getcheck(IDC_PREVIEW_ERASED);
  69.  
  70.    int count = 0;
  71.    char fn[10];
  72.  
  73.    LVITEM item;
  74.    item.mask = LVIF_TEXT;
  75.    item.pszText = fn;
  76.  
  77.    for (unsigned p = 0; p < 0x800; p += 0x10) {
  78.       if (!cat[p]) break;
  79.       if (!del && cat[p] == 1) continue;
  80.       if (bas && cat[p+8] != 'B') continue;
  81.  
  82.       memcpy(fn, cat+p, 8); fn[8] = 0;
  83.       item.iItem = count++;
  84.       item.iSubItem = 0;
  85.       item.iItem = int(SendMessage(list.h, LVM_INSERTITEM, 0, (LPARAM) &item));
  86.  
  87.       fn[0] = char(cat[p+8]); fn[1] = 0;
  88.       item.iSubItem = 1;
  89.       SendMessage(list.h, LVM_SETITEM, 0, (LPARAM) &item);
  90.  
  91.       sprintf(fn, "%d", cat[p+13]);
  92.       item.iSubItem = 2;
  93.       SendMessage(list.h, LVM_SETITEM, 0, (LPARAM) &item);
  94.    }
  95. }
  96.  
  97. void FILEPREVIEWINFO::PreviewTRD(char *filename)
  98. {
  99.    unsigned char cat[0x800];
  100.    FILE *ff = fopen(filename, "rb");
  101.    size_t sz = fread(cat, 1, 0x800, ff);
  102.    fclose(ff);
  103.    if (sz != 0x800) return;
  104.    Preview(cat);
  105. }
  106.  
  107. void FILEPREVIEWINFO::PreviewSCL(char *filename)
  108. {
  109.    unsigned char cat[0x800] = { 0 };
  110.    unsigned char hdr[16];
  111.  
  112.    FILE *ff = fopen(filename, "rb");
  113.    size_t sz = fread(hdr, 1, 9, ff), count = 0;
  114.  
  115.    if (sz == 9 && !memcmp(hdr, "SINCLAIR", 8)) {
  116.       unsigned max = hdr[8]; sz = max*14;
  117.       unsigned char *cat1 = (unsigned char*)alloca(sz);
  118.       if (fread(cat1, 1, sz, ff) == sz) {
  119.          for (unsigned i = 0; i < sz; i += 14) {
  120.             memcpy(cat+count*0x10, cat1+i, 14);
  121.             count++; if (count == 0x80) break;
  122.          }
  123.       }
  124.    }
  125.  
  126.    fclose(ff);
  127.    if (count) Preview(cat);
  128. }
  129.  
  130. static UINT_PTR CALLBACK PreviewDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
  131. {
  132.    switch (msg)
  133.    {
  134.       case WM_INITDIALOG:
  135.       {
  136.          FilePreviewInfo.ofn = (OPENFILENAME*)lp;
  137.          FilePreviewInfo.innerdlg.h = dlg;
  138.          FilePreviewInfo.dlg.h = GetParent(dlg);
  139.          FilePreviewInfo.list.h = GetDlgItem(dlg, IDC_PREVIEW_BOX);
  140.  
  141.          unsigned exflags = LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT;
  142.          SendMessage(FilePreviewInfo.list.h, LVM_SETEXTENDEDLISTVIEWSTYLE, exflags, exflags);
  143.  
  144.          LVCOLUMN sizeCol;
  145.          sizeCol.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  146.          sizeCol.fmt = LVCFMT_LEFT;
  147.  
  148.          sizeCol.cx = 80;
  149.          sizeCol.pszText = PSTR("Filename");
  150.          SendMessage(FilePreviewInfo.list.h, LVM_INSERTCOLUMN, 0, (LPARAM)&sizeCol);
  151.  
  152.          sizeCol.cx = 40;
  153.          sizeCol.pszText = PSTR("Ext");
  154.          SendMessage(FilePreviewInfo.list.h, LVM_INSERTCOLUMN, 1, (LPARAM)&sizeCol);
  155.  
  156.          sizeCol.cx = 50;
  157.          sizeCol.pszText = PSTR("Size");
  158.          SendMessage(FilePreviewInfo.list.h, LVM_INSERTCOLUMN, 2, (LPARAM)&sizeCol);
  159.  
  160.          RECT rc; GetWindowRect(FilePreviewInfo.list.h, &rc);
  161.          FilePreviewInfo.list.dx = rc.right - rc.left;
  162.          FilePreviewInfo.list.dy = rc.bottom - rc.top;
  163.  
  164.          break;
  165.       }
  166.  
  167.       case WM_COMMAND:
  168.          if (LOWORD(wp) == IDC_PREVIEW_BASIC || LOWORD(wp) == IDC_PREVIEW_ERASED)
  169.             FilePreviewInfo.OnChange();
  170.          break;
  171.  
  172.       case WM_SIZE:
  173.          FilePreviewInfo.OnResize();
  174.          break;
  175.  
  176.       case WM_NOTIFY:
  177.          if (((OFNOTIFY*)lp)->hdr.code == CDN_SELCHANGE)
  178.             FilePreviewInfo.OnChange();
  179.          break;
  180.  
  181.    }
  182.    return 0;
  183. }
  184.  
  185. int GetSnapshotFileName(OPENFILENAME *ofn, int save)
  186. {
  187.    ofn->Flags |= save? OFN_PATHMUSTEXIST : OFN_FILEMUSTEXIST;
  188.    ofn->Flags |= OFN_HIDEREADONLY | OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_ENABLESIZING;
  189.    ofn->Flags |= OFN_ENABLEHOOK | OFN_ENABLETEMPLATE;
  190.  
  191.    ofn->hwndOwner = GetForegroundWindow();
  192.    ofn->hInstance = hIn;
  193.    ofn->lpstrTitle = save? "Save Snapshot / Disk / Tape as" : "Load Snapshot / Disk / Tape";
  194.  
  195.    ofn->lpfnHook          = PreviewDlgProc;
  196.    ofn->lpTemplateName    = MAKEINTRESOURCE(IDD_FILEPREVIEW);
  197.    ofn->lpstrInitialDir   = temp.SnapDir;
  198.  
  199.    BOOL res = save? GetSaveFileName(ofn) : GetOpenFileName(ofn);
  200.  
  201.    if (res)
  202.    {
  203.        strcpy(temp.SnapDir, ofn->lpstrFile);
  204.        char *Ptr = strrchr(temp.SnapDir, '\\');
  205.        if(Ptr)
  206.         *Ptr = 0;
  207.        return res;
  208.    }
  209.    DWORD errcode = CommDlgExtendedError();
  210.    if (!errcode) return 0;
  211.  
  212.    color(CONSCLR_ERROR);
  213.    printf("Error while selecting file. Code is 0x%08lX\n", errcode);
  214.    return 0;
  215. }
  216.