home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / MainWndFile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  20.4 KB  |  897 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20. #include "MainWnd.h"
  21.  
  22. #include "ExportGSASnapshot.h"
  23. #include "FileDlg.h"
  24. #include "GSACodeSelect.h"
  25. #include "RomInfo.h"
  26. #include "Reg.h"
  27. #include "WinResUtil.h"
  28.  
  29. #include "../GBA.h"
  30. #include "../Globals.h"
  31. #include "../NLS.h"
  32. #include "../Sound.h"
  33. #include "../gb/GB.h"
  34. #include "../gb/gbCheats.h"
  35. #include "../gb/gbGlobals.h"
  36.  
  37. extern int emulating;
  38.  
  39. extern void remoteCleanUp();
  40.  
  41. void MainWnd::OnFileOpen() 
  42. {
  43.   theApp.winCheckFullscreen();
  44.   if(fileOpenSelect()) {
  45.     FileRun();
  46.   }
  47. }
  48.  
  49. void MainWnd::OnFilePause() 
  50. {
  51.   theApp.paused = !theApp.paused;
  52.   if(emulating) {
  53.     if(theApp.paused) {
  54.       theApp.wasPaused = true;
  55.       soundPause();
  56.     } else {
  57.       soundResume();
  58.     }
  59.   }
  60. }
  61.  
  62. void MainWnd::OnUpdateFilePause(CCmdUI* pCmdUI) 
  63. {
  64.   pCmdUI->SetCheck(theApp.paused);  
  65. }
  66.  
  67. void MainWnd::OnFileReset() 
  68. {
  69.   if(emulating) {
  70.     theApp.emulator.emuReset();
  71.     systemScreenMessage(winResLoadString(IDS_RESET));
  72.   }
  73. }
  74.  
  75. void MainWnd::OnUpdateFileReset(CCmdUI* pCmdUI) 
  76. {
  77.   pCmdUI->Enable(emulating);
  78. }
  79.  
  80. void MainWnd::OnUpdateFileRecentFreeze(CCmdUI* pCmdUI) 
  81. {
  82.   pCmdUI->SetCheck(theApp.recentFreeze);
  83.  
  84.   if(pCmdUI->m_pMenu == NULL)
  85.     return;
  86.   
  87.   CMenu *pMenu = pCmdUI->m_pMenu;
  88.  
  89.   int i;
  90.   for(i = 0; i < 10; i++) {
  91.     if(!pMenu->RemoveMenu(ID_FILE_MRU_FILE1+i, MF_BYCOMMAND))
  92.       break;
  93.   }
  94.  
  95.   for(i = 0; i < 10; i++) {
  96.     CString p = theApp.recentFiles[i];
  97.     if(p.GetLength() == 0)
  98.       break;
  99.     int index = p.ReverseFind('\\');
  100.  
  101.     if(index != -1)
  102.       p = p.Right(p.GetLength()-index-1);
  103.  
  104.     pMenu->AppendMenu(MF_STRING, ID_FILE_MRU_FILE1+i, p);
  105.   }
  106.   theApp.winAccelMgr.UpdateMenu((HMENU)*pMenu);
  107. }
  108.  
  109. BOOL MainWnd::OnFileRecentFile(UINT nID)
  110. {
  111.   if(theApp.recentFiles[(nID&0xFFFF)-ID_FILE_MRU_FILE1].GetLength()) {
  112.     theApp.szFile = theApp.recentFiles[(nID&0xFFFF)-ID_FILE_MRU_FILE1];
  113.     if(FileRun())
  114.       emulating = true;
  115.     else {
  116.       emulating = false;
  117.       soundPause();
  118.     }
  119.   }
  120.   return TRUE;
  121. }
  122.  
  123. void MainWnd::OnFileRecentReset() 
  124. {
  125.   int i = 0;
  126.   for(i = 0; i < 10; i++)
  127.     theApp.recentFiles[i] = "";
  128. }
  129.  
  130. void MainWnd::OnFileRecentFreeze() 
  131. {
  132.   theApp.recentFreeze = !theApp.recentFreeze;
  133. }
  134.  
  135. void MainWnd::OnFileExit() 
  136. {
  137.   SendMessage(WM_CLOSE);
  138. }
  139.  
  140. void MainWnd::OnFileClose() 
  141. {
  142.   // save battery file before we change the filename...
  143.   if(rom != NULL || gbRom != NULL) {
  144.     if(theApp.autoSaveLoadCheatList)
  145.       winSaveCheatListDefault();
  146.     writeBatteryFile();
  147.     soundPause();
  148.     theApp.emulator.emuCleanUp();
  149.     remoteCleanUp();
  150.   }
  151.   emulating = 0;
  152.   RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
  153.   systemSetTitle("VisualBoyAdvance");
  154. }
  155.  
  156. void MainWnd::OnUpdateFileClose(CCmdUI* pCmdUI) 
  157. {
  158.   pCmdUI->Enable(emulating);
  159. }
  160.  
  161. void MainWnd::OnFileOpengameboy() 
  162. {
  163.   theApp.winCheckFullscreen();
  164.   theApp.dir = "";
  165.   CString initialDir = regQueryStringValue("gbromdir",".");
  166.   if(!initialDir.IsEmpty())
  167.     theApp.dir = initialDir;
  168.  
  169.   theApp.szFile = "";
  170.  
  171.   LPCTSTR exts[] = { "" };
  172.   CString filter = winLoadFilter(IDS_FILTER_GBROM);
  173.   CString title = winResLoadString(IDS_SELECT_ROM);
  174.  
  175.   FileDlg dlg(this, "", filter, 0, "", exts, initialDir, title, false);
  176.  
  177.   if(dlg.DoModal() == IDOK) {
  178.     theApp.szFile = dlg.GetPathName();
  179.     theApp.dir = theApp.szFile.Left(dlg.m_ofn.nFileOffset);
  180.     if(theApp.dir.GetLength() > 3 && theApp.dir[theApp.dir.GetLength()-1] == '\\')
  181.       theApp.dir = theApp.dir.Left(theApp.dir.GetLength()-1);
  182.     regSetStringValue("gbromdir", theApp.dir);
  183.     FileRun();
  184.   }
  185. }
  186.  
  187. void MainWnd::OnFileLoad() 
  188. {
  189.   theApp.winCheckFullscreen();
  190.   CString buffer;
  191.   CString filename;
  192.  
  193.   int index = theApp.filename.ReverseFind('\\');
  194.  
  195.   if(index != -1)
  196.     buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  197.   else
  198.     buffer = theApp.filename;
  199.  
  200.   CString saveDir = regQueryStringValue("saveDir", NULL);
  201.  
  202.   if(saveDir.IsEmpty())
  203.     saveDir = getDirFromFile(theApp.filename);
  204.  
  205.   if(isDriveRoot(saveDir))
  206.     filename.Format("%s%s.sgm", saveDir, buffer);
  207.   else
  208.     filename.Format("%s\\%s.sgm", saveDir, buffer);
  209.  
  210.   LPCTSTR exts[] = { ".sgm" };
  211.   CString filter = winLoadFilter(IDS_FILTER_SGM);
  212.   CString title = winResLoadString(IDS_SELECT_SAVE_GAME_NAME);
  213.  
  214.   FileDlg dlg(this, filename, filter, 0, "", exts, saveDir, title, false);
  215.  
  216.   if(dlg.DoModal() == IDOK) {
  217.     bool res = loadSaveGame(dlg.GetPathName());
  218.  
  219.     theApp.rewindCount = 0;
  220.     theApp.rewindCounter = 0;
  221.     theApp.rewindSaveNeeded = false;
  222.   
  223.     if(res)
  224.       systemScreenMessage(winResLoadString(IDS_LOADED_STATE));  
  225.   }
  226. }
  227.  
  228. void MainWnd::OnUpdateFileLoad(CCmdUI* pCmdUI) 
  229. {
  230.   pCmdUI->Enable(emulating);
  231. }
  232.  
  233. BOOL MainWnd::OnFileLoadSlot(UINT nID)
  234. {
  235.   nID = nID + 1 - ID_FILE_LOADGAME_SLOT1;
  236.  
  237.   CString buffer;
  238.   CString filename;
  239.  
  240.   int index = theApp.filename.ReverseFind('\\');
  241.  
  242.   if(index != -1)
  243.     buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  244.   else
  245.     buffer = theApp.filename;
  246.  
  247.   CString saveDir = regQueryStringValue("saveDir", NULL);
  248.  
  249.   if(saveDir.IsEmpty())
  250.     saveDir = getDirFromFile(theApp.filename);
  251.  
  252.   if(isDriveRoot(saveDir))
  253.     filename.Format("%s%s%d.sgm", saveDir, buffer, nID);
  254.   else
  255.     filename.Format("%s\\%s%d.sgm", saveDir, buffer, nID);
  256.  
  257.   bool res = loadSaveGame(filename);
  258.  
  259.   theApp.rewindCount = 0;
  260.   theApp.rewindCounter = 0;
  261.   theApp.rewindSaveNeeded = false;
  262.   
  263.   CString format = winResLoadString(IDS_LOADED_STATE_N);
  264.   buffer.Format(format, nID);
  265.   
  266.   systemScreenMessage(buffer);
  267.   
  268.   return res;
  269. }
  270.  
  271. void MainWnd::OnFileSave() 
  272. {
  273.   theApp.winCheckFullscreen();
  274.   CString buffer;
  275.   CString filename;
  276.  
  277.   int index = theApp.filename.ReverseFind('\\');
  278.  
  279.   if(index != -1)
  280.     buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  281.   else
  282.     buffer = theApp.filename;
  283.  
  284.   CString saveDir = regQueryStringValue("saveDir", NULL);
  285.  
  286.   if(saveDir.IsEmpty())
  287.     saveDir = getDirFromFile(theApp.filename);
  288.  
  289.   if(isDriveRoot(saveDir))
  290.     filename.Format("%s%s.sgm", saveDir, buffer);
  291.   else
  292.     filename.Format("%s\\%s.sgm", saveDir, buffer);
  293.  
  294.   LPCTSTR exts[] = { ".sgm" };
  295.   CString filter = winLoadFilter(IDS_FILTER_SGM);
  296.   CString title = winResLoadString(IDS_SELECT_SAVE_GAME_NAME);
  297.  
  298.   FileDlg dlg(this, filename, filter, 0, "", exts, saveDir, title, true);
  299.  
  300.   if(dlg.DoModal() == IDOK) {
  301.     bool res = writeSaveGame(dlg.GetPathName());
  302.     if(res)
  303.       systemScreenMessage(winResLoadString(IDS_WROTE_STATE));  
  304.   }
  305. }
  306.  
  307. void MainWnd::OnUpdateFileSave(CCmdUI* pCmdUI) 
  308. {
  309.   pCmdUI->Enable(emulating);
  310. }
  311.  
  312. BOOL MainWnd::OnFileSaveSlot(UINT nID)
  313. {
  314.   nID = nID + 1 - ID_FILE_SAVEGAME_SLOT1;
  315.  
  316.   CString buffer;
  317.   CString filename;
  318.  
  319.   int index = theApp.filename.ReverseFind('\\');
  320.  
  321.   if(index != -1)
  322.     buffer = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  323.   else
  324.     buffer = theApp.filename;
  325.  
  326.   CString saveDir = regQueryStringValue("saveDir", NULL);
  327.  
  328.   if(saveDir.IsEmpty())
  329.     saveDir = getDirFromFile(theApp.filename);
  330.  
  331.   if(isDriveRoot(saveDir))
  332.     filename.Format("%s%s%d.sgm", saveDir, buffer, nID);
  333.   else
  334.     filename.Format("%s\\%s%d.sgm", saveDir, buffer, nID);
  335.  
  336.   bool res = writeSaveGame(filename);
  337.  
  338.   CString format = winResLoadString(IDS_WROTE_STATE_N);
  339.   buffer.Format(format, nID);
  340.   
  341.   systemScreenMessage(buffer);
  342.   
  343.   return res;
  344. }
  345.  
  346. void MainWnd::OnFileImportBatteryfile() 
  347. {
  348.   theApp.winCheckFullscreen();
  349.   LPCTSTR exts[] = { ".sav" };
  350.   CString filter = winLoadFilter(IDS_FILTER_SAV);
  351.   CString title = winResLoadString(IDS_SELECT_BATTERY_FILE);
  352.  
  353.   CString saveDir = regQueryStringValue("batteryDir", NULL);
  354.  
  355.   if(saveDir.IsEmpty())
  356.     saveDir = getDirFromFile(theApp.filename);
  357.  
  358.   FileDlg dlg(this, "", filter, 0, "", exts, saveDir, title, false);
  359.   
  360.   if(dlg.DoModal() == IDCANCEL)
  361.     return;
  362.   
  363.   CString str1 = winResLoadString(IDS_SAVE_WILL_BE_LOST);
  364.   CString str2 = winResLoadString(IDS_CONFIRM_ACTION);
  365.  
  366.   if(MessageBox(str1,
  367.                 str2,
  368.                 MB_OKCANCEL) == IDCANCEL)
  369.     return;
  370.   
  371.   bool res = false;
  372.  
  373.   res = theApp.emulator.emuReadBattery(dlg.GetPathName());
  374.   
  375.   if(!res)
  376.     systemMessage(MSG_CANNOT_OPEN_FILE, "Cannot open file %s", dlg.GetPathName());
  377.   else {
  378.     theApp.emulator.emuReset();
  379.   }
  380. }
  381.  
  382. void MainWnd::OnUpdateFileImportBatteryfile(CCmdUI* pCmdUI) 
  383. {
  384.   pCmdUI->Enable(emulating);  
  385. }
  386.  
  387. void MainWnd::OnFileImportGamesharkcodefile() 
  388. {
  389.   theApp.winCheckFullscreen();
  390.   LPCTSTR exts[] = { "" };
  391.   CString filter = theApp.cartridgeType == 0 ? winLoadFilter(IDS_FILTER_SPC) : winLoadFilter(IDS_FILTER_GCF);
  392.   CString title = winResLoadString(IDS_SELECT_CODE_FILE);
  393.  
  394.   FileDlg dlg(this, "", filter, 0, "", exts, "", title, false);
  395.   
  396.   if(dlg.DoModal() == IDCANCEL)
  397.     return;
  398.  
  399.   CString str1 = winResLoadString(IDS_CODES_WILL_BE_LOST);
  400.   CString str2 = winResLoadString(IDS_CONFIRM_ACTION);
  401.  
  402.   if(MessageBox(str1,
  403.                 str2,
  404.                 MB_OKCANCEL) == IDCANCEL)
  405.     return;
  406.   
  407.   bool res = false;
  408.   CString file = dlg.GetPathName();
  409.   if(theApp.cartridgeType == 1)
  410.     res = gbCheatReadGSCodeFile(file);
  411.   else {
  412.     res = fileImportGSACodeFile(file);
  413.   }
  414. }
  415.  
  416. void MainWnd::OnUpdateFileImportGamesharkcodefile(CCmdUI* pCmdUI) 
  417. {
  418.   pCmdUI->Enable(emulating);  
  419. }
  420.  
  421. void MainWnd::OnFileImportGamesharksnapshot() 
  422. {
  423.   theApp.winCheckFullscreen();
  424.   LPCTSTR exts[] = { "" };
  425.   CString filter = theApp.cartridgeType == 1 ? winLoadFilter(IDS_FILTER_GBS) : winLoadFilter(IDS_FILTER_SPS);
  426.   CString title = winResLoadString(IDS_SELECT_SNAPSHOT_FILE);
  427.  
  428.   FileDlg dlg(this, "", filter, 0, "", exts, "", title, false);
  429.   
  430.   if(dlg.DoModal() == IDCANCEL)
  431.     return;
  432.  
  433.   CString str1 = winResLoadString(IDS_SAVE_WILL_BE_LOST);
  434.   CString str2 = winResLoadString(IDS_CONFIRM_ACTION);
  435.  
  436.   if(MessageBox(str1,
  437.                 str2,
  438.                 MB_OKCANCEL) == IDCANCEL)
  439.     return;
  440.  
  441.   if(theApp.cartridgeType == 1)
  442.     gbReadGSASnapshot(dlg.GetPathName());
  443.   else
  444.     CPUReadGSASnapshot(dlg.GetPathName());
  445. }
  446.  
  447. void MainWnd::OnUpdateFileImportGamesharksnapshot(CCmdUI* pCmdUI) 
  448. {
  449.   pCmdUI->Enable(emulating);  
  450. }
  451.  
  452. void MainWnd::OnFileExportBatteryfile() 
  453. {
  454.   theApp.winCheckFullscreen();
  455.   CString name;
  456.  
  457.   int index = theApp.filename.ReverseFind('\\');
  458.  
  459.   if(index != -1)
  460.     name = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  461.   else
  462.     name = theApp.filename;
  463.  
  464.   LPCTSTR exts[] = {".sav", ".dat" };
  465.  
  466.   CString filter = winLoadFilter(IDS_FILTER_SAV);
  467.   CString title = winResLoadString(IDS_SELECT_BATTERY_FILE);
  468.  
  469.   CString saveDir = regQueryStringValue("batteryDir", NULL);
  470.  
  471.   if(saveDir.IsEmpty())
  472.     saveDir = getDirFromFile(theApp.filename);
  473.  
  474.   FileDlg dlg(this,
  475.               name,
  476.               filter,
  477.               1,
  478.               "SAV",
  479.               exts,
  480.               saveDir,
  481.               title,
  482.               true);
  483.  
  484.   if(dlg.DoModal() == IDCANCEL) {
  485.     return;
  486.   }
  487.  
  488.   bool result = false;
  489.  
  490.   if(theApp.cartridgeType == 1) {
  491.     result = gbWriteBatteryFile(dlg.GetPathName(), false);
  492.   } else
  493.     result = theApp.emulator.emuWriteBattery(dlg.GetPathName());
  494.  
  495.   if(!result)
  496.     systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s",
  497.                   dlg.GetPathName());
  498. }
  499.  
  500. void MainWnd::OnUpdateFileExportBatteryfile(CCmdUI* pCmdUI) 
  501. {
  502.   pCmdUI->Enable(emulating);  
  503. }
  504.  
  505. void MainWnd::OnFileExportGamesharksnapshot() 
  506. {
  507.   theApp.winCheckFullscreen();
  508.   if(eepromInUse) {
  509.     systemMessage(IDS_EEPROM_NOT_SUPPORTED, "EEPROM saves cannot be exported");
  510.     return;
  511.   }
  512.   
  513.   CString name;
  514.  
  515.   int index = theApp.filename.ReverseFind('\\');
  516.  
  517.   if(index != -1)
  518.     name = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  519.   else
  520.     name = theApp.filename;
  521.  
  522.   LPCTSTR exts[] = {".sps" };
  523.   
  524.   CString filter = winLoadFilter(IDS_FILTER_SPS);
  525.   CString title = winResLoadString(IDS_SELECT_SNAPSHOT_FILE);
  526.  
  527.   FileDlg dlg(this,
  528.               name,
  529.               filter,
  530.               1,
  531.               "SPS",
  532.               exts,
  533.               "",
  534.               title,
  535.               true);
  536.  
  537.   if(dlg.DoModal() == IDCANCEL)
  538.     return;
  539.  
  540.   char buffer[16];
  541.   strncpy(buffer, (const char *)&rom[0xa0], 12);
  542.   buffer[12] = 0;
  543.  
  544.   ExportGSASnapshot dlg2(dlg.GetPathName(), buffer, this);
  545.   dlg2.DoModal();
  546. }
  547.  
  548. void MainWnd::OnUpdateFileExportGamesharksnapshot(CCmdUI* pCmdUI) 
  549. {
  550.   pCmdUI->Enable(emulating && theApp.cartridgeType == 0);  
  551. }
  552.  
  553. void MainWnd::OnFileScreencapture() 
  554. {
  555.   theApp.winCheckFullscreen();
  556.   CString name;
  557.   CString filename;
  558.  
  559.   int index = theApp.filename.ReverseFind('\\');
  560.  
  561.   if(index != -1)
  562.     name = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  563.   else
  564.     name = theApp.filename;
  565.  
  566.   CString capdir = regQueryStringValue("captureDir", "");
  567.   if(capdir.IsEmpty())
  568.     capdir = getDirFromFile(name);
  569.  
  570.   CString ext = "png";
  571.  
  572.   if(theApp.captureFormat != 0)
  573.     ext = "bmp";
  574.   
  575.   if(isDriveRoot(capdir))
  576.     filename.Format("%s%s.%s", capdir, name, ext);
  577.   else
  578.     filename.Format("%s\\%s.%s", capdir, name, ext);
  579.  
  580.   LPCTSTR exts[] = {".png", ".bmp" };
  581.     
  582.   CString filter = winLoadFilter(IDS_FILTER_PNG);
  583.   CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME);
  584.  
  585.   FileDlg dlg(this,
  586.               filename,
  587.               filter,
  588.               theApp.captureFormat ? 2 : 1,
  589.               theApp.captureFormat ? "BMP" : "PNG",
  590.               exts,
  591.               capdir,
  592.               title,
  593.               true);
  594.  
  595.   if(dlg.DoModal() == IDCANCEL)
  596.     return;
  597.   
  598.   if(dlg.getFilterIndex() == 2)
  599.     theApp.emulator.emuWriteBMP(dlg.GetPathName());
  600.   else
  601.     theApp.emulator.emuWritePNG(dlg.GetPathName());
  602.  
  603.   systemScreenMessage(winResLoadString(IDS_SCREEN_CAPTURE));
  604. }
  605.  
  606. void MainWnd::OnUpdateFileScreencapture(CCmdUI* pCmdUI) 
  607. {
  608.   pCmdUI->Enable(emulating);  
  609. }
  610.  
  611. void MainWnd::OnFileRominformation() 
  612. {
  613.   theApp.winCheckFullscreen();
  614.   if(theApp.cartridgeType == 0) {
  615.     RomInfoGBA dlg(rom);
  616.     dlg.DoModal();
  617.   } else {
  618.     RomInfoGB dlg(gbRom);
  619.     dlg.DoModal();
  620.   }
  621. }
  622.  
  623. void MainWnd::OnUpdateFileRominformation(CCmdUI* pCmdUI) 
  624. {
  625.   pCmdUI->Enable(emulating);  
  626. }
  627.  
  628. void MainWnd::OnFileTogglemenu() 
  629. {
  630.   if(theApp.videoOption <= VIDEO_4X)
  631.     return;
  632.   
  633.   theApp.menuToggle = !theApp.menuToggle;
  634.  
  635.   if(theApp.menuToggle) {
  636.     theApp.updateMenuBar();
  637.     if(theApp.tripleBuffering) {
  638.       if(theApp.display)
  639.         theApp.display->checkFullScreen();
  640.       DrawMenuBar();
  641.     }    
  642.   } else {
  643.     SetMenu(NULL);
  644.     DestroyMenu(theApp.menu);
  645.   }
  646.  
  647.   theApp.adjustDestRect();
  648. }
  649.  
  650. void MainWnd::OnUpdateFileTogglemenu(CCmdUI* pCmdUI) 
  651. {
  652.   pCmdUI->Enable(theApp.videoOption > VIDEO_4X);  
  653. }
  654.  
  655. bool MainWnd::fileImportGSACodeFile(CString& fileName)
  656. {
  657.   FILE *f = fopen(fileName, "rb");
  658.   
  659.   if(f == NULL) {
  660.     systemMessage(MSG_CANNOT_OPEN_FILE, "Cannot open file %s", fileName);
  661.     return false;
  662.   }
  663.  
  664.   u32 len;
  665.   fread(&len, 1, 4, f);
  666.   if(len != 14) {
  667.     fclose(f);
  668.     systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s",
  669.                   fileName);
  670.     return false;
  671.   }
  672.   char buffer[16];
  673.   fread(buffer, 1, 14, f);
  674.   buffer[14] = 0;
  675.   if(memcmp(buffer, "SharkPortCODES", 14)) {
  676.     fclose(f);
  677.     systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s",
  678.                   fileName);
  679.     return false;
  680.   }    
  681.   fseek(f, 0x1e, SEEK_SET);
  682.   fread(&len, 1, 4, f);
  683.   int game = 0;
  684.   if(len > 1) {
  685.     GSACodeSelect dlg(f);
  686.     game = dlg.DoModal();
  687.   }
  688.   fclose(f);
  689.  
  690.   bool v3 = false;
  691.   
  692.   int index = fileName.ReverseFind('.');
  693.  
  694.   if(index != -1) {
  695.     if(fileName.Right(3).CompareNoCase("XPC") == 0)
  696.       v3 = true;
  697.   }
  698.  
  699.   if(game != -1) {
  700.     return cheatsImportGSACodeFile(fileName, game, v3);
  701.   }
  702.   
  703.   return true;
  704. }
  705.  
  706. void MainWnd::OnFileSavegameOldestslot() 
  707. {
  708.   if(!emulating)
  709.     return;
  710.  
  711.   CString filename;
  712.  
  713.   int index = theApp.filename.ReverseFind('\\');
  714.  
  715.   if(index != -1)
  716.     filename = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  717.   else
  718.     filename = theApp.filename;
  719.  
  720.   CString saveDir = regQueryStringValue("saveDir", NULL);
  721.  
  722.   if(saveDir.IsEmpty())
  723.     saveDir = getDirFromFile(theApp.filename);
  724.  
  725.   if(!isDriveRoot(saveDir))
  726.     saveDir += "\\";
  727.  
  728.   CString name;
  729.   CFileStatus status;
  730.   CString str;
  731.   time_t time = -1;
  732.   int found = 0;
  733.     
  734.   for(int i = 0; i < 10; i++) {
  735.     name.Format("%s%s%d.sgm", saveDir, filename, i+1);
  736.  
  737.     if(emulating && CFile::GetStatus(name, status)) {
  738.       if(status.m_mtime.GetTime() < time) {
  739.         time = status.m_mtime.GetTime();
  740.         found = i;
  741.       }
  742.     } else {
  743.       found = i;
  744.       break;
  745.     }
  746.   }
  747.   OnFileSaveSlot(ID_FILE_SAVEGAME_SLOT1+found);
  748. }
  749.  
  750. void MainWnd::OnUpdateFileSavegameOldestslot(CCmdUI* pCmdUI) 
  751. {
  752.   pCmdUI->Enable(emulating);
  753.   if(pCmdUI->m_pSubMenu != NULL) {
  754.     CMenu *pMenu = pCmdUI->m_pSubMenu;
  755.     CString filename;
  756.  
  757.     int index = theApp.filename.ReverseFind('\\');
  758.  
  759.     if(index != -1)
  760.       filename = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  761.     else
  762.       filename = theApp.filename;
  763.  
  764.     CString saveDir = regQueryStringValue("saveDir", NULL);
  765.  
  766.     if(saveDir.IsEmpty())
  767.       saveDir = getDirFromFile(theApp.filename);
  768.  
  769.     if(!isDriveRoot(saveDir))
  770.       saveDir += "\\";
  771.  
  772.     CString name;
  773.     CFileStatus status;
  774.     CString str;
  775.  
  776.     for(int i = 0; i < 10; i++) {
  777.       name.Format("%s%s%d.sgm", saveDir, filename, i+1);
  778.  
  779.       if(emulating && CFile::GetStatus(name, status)) {
  780.         CString timestamp = status.m_mtime.Format("%Y/%m/%d %H:%M:%S");
  781.         str.Format("%d %s", i+1, timestamp);
  782.       } else {
  783.         str.Format("%d ----/--/-- --:--:--", i+1);
  784.       }
  785.       pMenu->ModifyMenu(ID_FILE_SAVEGAME_SLOT1+i, MF_STRING|MF_BYCOMMAND, ID_FILE_SAVEGAME_SLOT1+i, str);
  786.     }
  787.     
  788.     theApp.winAccelMgr.UpdateMenu(pMenu->GetSafeHmenu());
  789.   }
  790. }
  791.  
  792. void MainWnd::OnFileLoadgameMostrecent() 
  793. {
  794.   if(!emulating)
  795.     return;
  796.  
  797.   CString filename;
  798.  
  799.   int index = theApp.filename.ReverseFind('\\');
  800.  
  801.   if(index != -1)
  802.     filename = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  803.   else
  804.     filename = theApp.filename;
  805.  
  806.   CString saveDir = regQueryStringValue("saveDir", NULL);
  807.  
  808.   if(saveDir.IsEmpty())
  809.     saveDir = getDirFromFile(theApp.filename);
  810.  
  811.   if(!isDriveRoot(saveDir))
  812.     saveDir += "\\";
  813.  
  814.   CString name;
  815.   CFileStatus status;
  816.   CString str;
  817.   time_t time = 0;
  818.   int found = -1;
  819.     
  820.   for(int i = 0; i < 10; i++) {
  821.     name.Format("%s%s%d.sgm", saveDir, filename, i+1);
  822.  
  823.     if(emulating && CFile::GetStatus(name, status)) {
  824.       if(status.m_mtime.GetTime() > time) {
  825.         time = status.m_mtime.GetTime();
  826.         found = i;
  827.       }
  828.     } 
  829.   }
  830.   if(found != -1) {
  831.     OnFileLoadSlot(ID_FILE_LOADGAME_SLOT1+found);
  832.   }
  833. }
  834.  
  835. void MainWnd::OnUpdateFileLoadgameMostrecent(CCmdUI* pCmdUI) 
  836. {
  837.   pCmdUI->Enable(emulating);
  838.  
  839.   if(pCmdUI->m_pSubMenu != NULL) {
  840.     CMenu *pMenu = pCmdUI->m_pSubMenu;
  841.     CString filename;
  842.  
  843.     int index = theApp.filename.ReverseFind('\\');
  844.  
  845.     if(index != -1)
  846.       filename = theApp.filename.Right(theApp.filename.GetLength()-index-1);
  847.     else
  848.       filename = theApp.filename;
  849.  
  850.     CString saveDir = regQueryStringValue("saveDir", NULL);
  851.  
  852.     if(saveDir.IsEmpty())
  853.       saveDir = getDirFromFile(theApp.filename);
  854.  
  855.     if(!isDriveRoot(saveDir))
  856.       saveDir += "\\";
  857.  
  858.     CString name;
  859.     CFileStatus status;
  860.     CString str;
  861.     
  862.     for(int i = 0; i < 10; i++) {
  863.       name.Format("%s%s%d.sgm", saveDir, filename, i+1);
  864.  
  865.       if(emulating && CFile::GetStatus(name, status)) {
  866.         CString timestamp = status.m_mtime.Format("%Y/%m/%d %H:%M:%S");
  867.         str.Format("%d %s", i+1, timestamp);
  868.       } else {
  869.         str.Format("%d ----/--/-- --:--:--", i+1);
  870.       }
  871.       pMenu->ModifyMenu(ID_FILE_LOADGAME_SLOT1+i, MF_STRING|MF_BYCOMMAND, ID_FILE_LOADGAME_SLOT1+i, str);
  872.     }
  873.  
  874.     theApp.winAccelMgr.UpdateMenu(pMenu->GetSafeHmenu());
  875.   }
  876. }
  877.  
  878. void MainWnd::OnUpdateFileLoadGameSlot(CCmdUI *pCmdUI)
  879. {
  880.   pCmdUI->Enable(emulating);
  881. }
  882.  
  883. void MainWnd::OnUpdateFileSaveGameSlot(CCmdUI *pCmdUI)
  884. {
  885.   pCmdUI->Enable(emulating);
  886. }
  887.  
  888. void MainWnd::OnFileLoadgameAutoloadmostrecent() 
  889. {
  890.   theApp.autoLoadMostRecent = !theApp.autoLoadMostRecent;
  891. }
  892.  
  893. void MainWnd::OnUpdateFileLoadgameAutoloadmostrecent(CCmdUI* pCmdUI) 
  894. {
  895.   pCmdUI->SetCheck(theApp.autoLoadMostRecent);
  896. }
  897.