diff --git a/src/drivers/win/config.cpp b/src/drivers/win/config.cpp index dd3aee28..cb0ebdad 100644 --- a/src/drivers/win/config.cpp +++ b/src/drivers/win/config.cpp @@ -92,6 +92,12 @@ static CFGSTRUCT fceuconfig[] = { ACS(memw_recent_files[3]), ACS(memw_recent_files[4]), + ACS(recent_lua[0]), + ACS(recent_lua[1]), + ACS(recent_lua[2]), + ACS(recent_lua[3]), + ACS(recent_lua[4]), + AC(gNoBGFillColor), AC(ntsccol),AC(ntsctint),AC(ntschue), diff --git a/src/drivers/win/res.rc b/src/drivers/win/res.rc index 26dddead..8fa20cc6 100644 --- a/src/drivers/win/res.rc +++ b/src/drivers/win/res.rc @@ -72,6 +72,7 @@ BEGIN END POPUP "Lua" BEGIN + MENUITEM "Recent", MENU_LUA_RECENT MENUITEM "Run Lua Script...", ID_FILE_RUNLUASCRIPT MENUITEM "Stop Lua Script", ID_FILE_STOPLUASCRIPT MENUITEM "Reload Lua Script", ID_FILE_LUA_RELOADLUASCRIPT diff --git a/src/drivers/win/resource.h b/src/drivers/win/resource.h index f1b216ab..76b3f6a2 100644 --- a/src/drivers/win/resource.h +++ b/src/drivers/win/resource.h @@ -663,6 +663,8 @@ #define MENU_PREVIOUSSAVESTATE 40348 #define ID_SAVESTATE_VIEWSAVESLOTS 40349 #define MENU_VIEWSAVESLOTS 40350 +#define ID_LUA_RECENT 40351 +#define MENU_LUA_RECENT 40352 #define IDC_DEBUGGER_ICONTRAY 55535 #define MW_ValueLabel2 65423 #define MW_ValueLabel1 65426 @@ -672,7 +674,7 @@ #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 125 -#define _APS_NEXT_COMMAND_VALUE 40351 +#define _APS_NEXT_COMMAND_VALUE 40353 #define _APS_NEXT_CONTROL_VALUE 1199 #define _APS_NEXT_SYMED_VALUE 101 #endif diff --git a/src/drivers/win/window.cpp b/src/drivers/win/window.cpp index 0366c389..8ebe3733 100644 --- a/src/drivers/win/window.cpp +++ b/src/drivers/win/window.cpp @@ -76,6 +76,7 @@ using namespace std; static HMENU fceumenu = 0; //Main menu. HWND pwindow; //Client Area static HMENU recentmenu; //Recent Menu +static HMENU recentluamenu; //Recent Lua Files Menu HMENU hfceuxcontext; //Handle to context menu HMENU hfceuxcontextsub; //Handle to context sub menu @@ -124,6 +125,11 @@ char *recent_files[] = { 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; const unsigned int MENU_FIRST_RECENT_FILE = 600; const unsigned int MAX_NUMBER_OF_RECENT_FILES = sizeof(recent_files)/sizeof(*recent_files); +//Lua Recent Menu --------------------------------------- +char *recent_lua[] = {0,0,0,0,0}; +const unsigned int LUA_FIRST_RECENT_FILE = 50000; +const unsigned int MAX_NUMBER_OF_LUA_RECENT_FILES = sizeof(recent_lua)/sizeof(*recent_lua); + //Exported variables ------------------------------------ int EnableBackgroundInput = 0; @@ -590,8 +596,119 @@ void AddRecentFile(const char *filename) UpdateRecentArray(filename, recent_files, MAX_NUMBER_OF_RECENT_FILES, recentmenu, MENU_RECENT_FILES, MENU_FIRST_RECENT_FILE); } -/// Hides the main menu. -///@param hide_menu Flag to turn the main menu on (0) or off (1) +void UpdateLuaRMenu(HMENU menu, char **strs, unsigned int mitem, unsigned int baseid) +{ + MENUITEMINFO moo; + int x; + + moo.cbSize = sizeof(moo); + moo.fMask = MIIM_SUBMENU | MIIM_STATE; + + GetMenuItemInfo(GetSubMenu(fceumenu, 0), mitem, FALSE, &moo); + moo.hSubMenu = menu; + moo.fState = strs[0] ? MFS_ENABLED : MFS_GRAYED; + + SetMenuItemInfo(GetSubMenu(fceumenu, 0), mitem, FALSE, &moo); + + // Remove all recent files submenus + for(x = 0; x < MAX_NUMBER_OF_LUA_RECENT_FILES; x++) + { + RemoveMenu(menu, baseid + x, MF_BYCOMMAND); + } + + // Recreate the menus + for(x = MAX_NUMBER_OF_LUA_RECENT_FILES - 1; x >= 0; x--) + { + // Skip empty strings + if(!strs[x]) + continue; + + string tmp = strs[x]; + + //clamp this string to 128 chars + if(tmp.size()>128) + tmp = tmp.substr(0,128); + + moo.cbSize = sizeof(moo); + moo.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE; + + // Insert the menu item + moo.cch = tmp.size(); + moo.fType = 0; + moo.wID = baseid + x; + moo.dwTypeData = (LPSTR)tmp.c_str(); + InsertMenuItem(menu, 0, 1, &moo); + } + + DrawMenuBar(hAppWnd); +} + +void UpdateRecentLuaArray(const char* addString, char** bufferArray, unsigned int arrayLen, HMENU menu, unsigned int menuItem, unsigned int baseId) +{ + // Try to find out if the filename is already in the recent files list. + for(unsigned int x = 0; x < arrayLen; x++) + { + if(bufferArray[x]) + { + if(!strcmp(bufferArray[x], addString)) // Item is already in list. + { + // If the filename is in the file list don't add it again. + // Move it up in the list instead. + + int y; + char *tmp; + + // Save pointer. + tmp = bufferArray[x]; + + for(y = x; y; y--) + { + // Move items down. + bufferArray[y] = bufferArray[y - 1]; + } + + // Put item on top. + bufferArray[0] = tmp; + + // Update the recent files menu + UpdateRMenu(menu, bufferArray, menuItem, baseId); + + return; + } + } + } + + // The filename wasn't found in the list. That means we need to add it. + + // If there's no space left in the recent files list, get rid of the last + // item in the list. + if(bufferArray[arrayLen - 1]) + { + free(bufferArray[arrayLen - 1]); + } + + // Move the other items down. + for(unsigned int x = arrayLen - 1; x; x--) + { + bufferArray[x] = bufferArray[x - 1]; + } + + // Add the new item. + bufferArray[0] = (char*)malloc(strlen(addString) + 1); //mbg merge 7/17/06 added cast + strcpy(bufferArray[0], addString); + + // Update the recent files menu + UpdateLuaRMenu(menu, bufferArray, menuItem, baseId); + +} + +void AddRecentLuaFile(const char *filename) +{ + UpdateRecentLuaArray(filename, recent_lua, MAX_NUMBER_OF_LUA_RECENT_FILES, recentluamenu, MENU_LUA_RECENT, LUA_FIRST_RECENT_FILE); +} + +// Hides the main menu. +//@param hide_menu Flag to turn the main menu on (0) or off (1) void HideMenu(unsigned int hide_menu) { if(hide_menu) @@ -950,6 +1067,16 @@ LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) ALoad(fname); } } + + // A menu item for the recent lua files menu was clicked. + if(wParam >= LUA_FIRST_RECENT_FILE && wParam <= LUA_FIRST_RECENT_FILE + MAX_NUMBER_OF_LUA_RECENT_FILES - 1) + { + char*& fname = recent_lua[wParam - LUA_FIRST_RECENT_FILE]; + if(fname) + { + FCEU_LoadLuaCode(fname); + } + } switch(LOWORD(wParam)) { //File Menu------------------------------------------------------------- @@ -1621,9 +1748,11 @@ int CreateMainWindow() fceumenu = LoadMenu(fceu_hInstance,"FCEUMENU"); recentmenu = CreateMenu(); + recentluamenu = CreateMenu(); // Update recent files menu UpdateRMenu(recentmenu, recent_files, MENU_RECENT_FILES, MENU_FIRST_RECENT_FILE); + UpdateLuaRMenu(recentluamenu, recent_lua, MENU_LUA_RECENT, LUA_FIRST_RECENT_FILE); updateGameDependentMenus(0); if (MainWindow_wndx==-32000) MainWindow_wndx=0; //Just in case @@ -1886,6 +2015,7 @@ void FCEUD_LuaRunFrom(void) ofn.lpstrInitialDir=initdir.c_str(); if(GetOpenFileName( &ofn )) { + AddRecentLuaFile(szFileName); FCEU_LoadLuaCode(szFileName); } } diff --git a/src/drivers/win/window.h b/src/drivers/win/window.h index 05abffee..76a1ffe2 100644 --- a/src/drivers/win/window.h +++ b/src/drivers/win/window.h @@ -17,6 +17,7 @@ struct CreateMovieParameters }; extern char *recent_files[]; +extern char *recent_lua[]; extern HWND pwindow; void HideFWindow(int h); diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 35881f1d..2a5ce57f 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -41,6 +41,10 @@ extern "C" #define FALSE 0 #endif +#ifdef WIN32 +extern void AddRecentLuaFile(const char *filename); +#endif + struct LuaSaveState { std::string filename; memorystream *data; @@ -1729,7 +1733,9 @@ int FCEU_LoadLuaCode(const char *filename) { lua_settop(L,0); return 0; // Oh shit. } - +#ifdef WIN32 + AddRecentLuaFile(filename); //Add the filename to our recent lua menu +#endif // Get our function into it lua_xmove(L, thread, 1);