Subversion Repositories pentevo

Rev

Rev 716 | Blame | Last modification | View Log | Download | RSS feed

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