Implement MRU commands

Originally committed to SVN as r5199.
This commit is contained in:
Thomas Goyne 2011-01-16 07:15:46 +00:00
parent ecea389e62
commit 08ec92046f
3 changed files with 112 additions and 21 deletions

View file

@ -37,12 +37,15 @@
#include "../config.h"
#ifndef AGI_PRE
#include <sstream>
#include <wx/event.h>
#endif
#include "command.h"
#include "../include/aegisub/context.h"
#include "../audio_controller.h"
#include "../main.h"
#include "../frame_main.h"
#include "../compat.h"
@ -58,6 +61,80 @@ COMMAND_GROUP(recent_subtitle, "recent/subtitle", "Recent", "Recent", "Open rece
COMMAND_GROUP(recent_timecode, "recent/timecode", "Recent", "Recent", "Open recent timecodes.");
COMMAND_GROUP(recent_video, "recent/video", "Recent", "Recent", "Open recent video.");
struct recent_audio_entry : public Command {
CMD_NAME("recent/audio/")
STR_MENU("Recent")
STR_DISP("Recent")
STR_HELP("Open recent audio.")
void operator()(agi::Context *c, int id) {
c->audioController->OpenAudio(lagi_wxString(config::mru->GetEntry("Audio", id)));
}
};
struct recent_keyframe_entry : public Command {
CMD_NAME("recent/keyframe/")
STR_MENU("Recent")
STR_DISP("Recent")
STR_HELP("Open recent keyframes.")
void operator()(agi::Context *c, int id) {
c->videoContext->LoadKeyframes(lagi_wxString(config::mru->GetEntry("Keyframes", id)));
}
};
struct recent_subtitle_entry : public Command {
CMD_NAME("recent/subtitle/")
STR_MENU("Recent")
STR_DISP("Recent")
STR_HELP("Open recent subtitles.")
void operator()(agi::Context *c, int id) {
wxGetApp().frame->LoadSubtitles(lagi_wxString(config::mru->GetEntry("Subtitle", id)));
}
};
struct recent_timecode_entry : public Command {
CMD_NAME("recent/timecode/")
STR_MENU("Recent")
STR_DISP("Recent")
STR_HELP("Open recent timecodes.")
void operator()(agi::Context *c, int id) {
c->videoContext->LoadTimecodes(lagi_wxString(config::mru->GetEntry("Timecodes", id)));
}
};
struct recent_video_entry : public Command {
CMD_NAME("recent/video/")
STR_MENU("Recent")
STR_DISP("Recent")
STR_HELP("Open recent videos.")
void operator()(agi::Context *c, int id) {
c->videoContext->SetVideo(lagi_wxString(config::mru->GetEntry("Video", id)));
}
};
/// @class mru_wrapper
/// @brief Wrapper class for mru commands to
template<class T>
class mru_wrapper : public T {
int id;
std::string full_name;
public:
const char *name() { return full_name.c_str(); }
void operator()(agi::Context *c) {
T::operator()(c, id);
}
mru_wrapper(int id) : id(id) {
std::stringstream ss;
ss << T::name();
ss << id;
full_name = ss.str();
}
};
/// @}
/// Init recent/ commands.
@ -67,6 +144,15 @@ void init_recent(CommandManager *cm) {
cm->reg(new recent_subtitle());
cm->reg(new recent_timecode());
cm->reg(new recent_video());
/// @todo 16 is an implementation detail that maybe needs to be exposed
for (int i = 0; i < 16; ++i) {
cm->reg(new mru_wrapper<recent_audio_entry>(i));
cm->reg(new mru_wrapper<recent_keyframe_entry>(i));
cm->reg(new mru_wrapper<recent_subtitle_entry>(i));
cm->reg(new mru_wrapper<recent_timecode_entry>(i));
cm->reg(new mru_wrapper<recent_video_entry>(i));
}
}
} // namespace cmd

View file

@ -1071,28 +1071,33 @@ void FrameMain::OnGridEvent (wxCommandEvent &event) {
/// @param listName
/// @param menu
/// @param startID
void FrameMain::RebuildRecentList(wxString listName,wxMenu *menu,int startID) {
// Wipe previous list
void FrameMain::RebuildRecentList(const char *root_command, const char *mru_name) {
wxMenu *menu = menu::menu->GetMenu(root_command);
int count = (int)menu->GetMenuItemCount();
for (int i=count;--i>=0;) {
menu->Destroy(menu->FindItemByPosition(i));
}
// Rebuild
int added = 0;
wxString n;
wxArrayString entries = lagi_MRU_wxAS(listName);
for (size_t i=0;i<entries.Count();i++) {
n = wxString::Format(_T("%ld"),i+1);
if (i < 9) n = _T("&") + n;
wxFileName shortname(entries[i]);
wxString filename = shortname.GetFullName();
menu->Append(startID+i,n + _T(" ") + filename);
added++;
const agi::MRUManager::MRUListMap *map_list = config::mru->Get(mru_name);
if (map_list->empty()) {
menu->Append(-1, _("Empty"))->Enable(false);
return;
}
// Nothing added, add an empty placeholder
if (added == 0) menu->Append(startID,_("Empty"))->Enable(false);
int i = 0;
for (agi::MRUManager::MRUListMap::const_iterator it = map_list->begin(); it != map_list->end(); ++it) {
std::stringstream ss;
ss << root_command;
ss << "/";
ss << i;
wxFileName shortname(lagi_wxString(it->second));
menu->Append(cmd::id(ss.str()),
wxString::Format("%s%d %s", i <= 9 ? "&" : "", i + 1, shortname.GetFullName()));
++i;
}
}
/// @brief Menu is being opened
@ -1107,7 +1112,7 @@ void FrameMain::OnMenuOpen (wxMenuEvent &event) {
// File menu
if (curMenu == menu::menu->GetMenu("main/file")) {
// Rebuild recent
RebuildRecentList(_T("Subtitle"),menu::menu->GetMenu("recent/subtitle"), cmd::id("recent/subtitle"));
RebuildRecentList("recent/subtitle", "Subtitle");
MenuBar->Enable(cmd::id("subtitle/open/video"),VideoContext::Get()->HasSubtitles());
}
@ -1184,9 +1189,9 @@ void FrameMain::OnMenuOpen (wxMenuEvent &event) {
MenuBar->Check(cmd::id("video/show_overscan"),OPT_GET("Video/Overscan Mask")->GetBool());
// Rebuild recent lists
RebuildRecentList(_T("Video"),menu::menu->GetMenu("recent/video"), cmd::id("recent/video"));
RebuildRecentList(_T("Timecodes"),menu::menu->GetMenu("recent/timecode"), cmd::id("recent/timecode"));
RebuildRecentList(_T("Keyframes"),menu::menu->GetMenu("recent/keyframe"), cmd::id("recent/keyframe"));
RebuildRecentList("recent/video", "Video");
RebuildRecentList("recent/timecode", "Timecodes");
RebuildRecentList("recent/keyframe", "Keyframes");
}
// Audio menu
@ -1198,7 +1203,7 @@ void FrameMain::OnMenuOpen (wxMenuEvent &event) {
MenuBar->Enable(cmd::id("audio/close"),state);
// Rebuild recent
RebuildRecentList(_T("Audio"),menu::menu->GetMenu("recent/audio"), cmd::id("recent/audio"));
RebuildRecentList("recent/audio", "Audio");
}
// Subtitles menu

View file

@ -165,7 +165,7 @@ private:
int TryToCloseSubs(bool enableCancel=true);
void RebuildRecentList(wxString listName,wxMenu *menu,int startID);
void RebuildRecentList(const char *root_command, const char *mru_name);
void SynchronizeProject(bool FromSubs=false);
// AudioControllerAudioEventListener implementation