From 4112a269520b5d3ade347b46c05d5bcde3ad7432 Mon Sep 17 00:00:00 2001 From: Dimitris Panokostas Date: Fri, 22 Dec 2017 13:27:09 +0100 Subject: [PATCH] Fixed config loaded and emulation restarted as soon as you click on an entry in the list. --- src/osdep/gui/PanelConfig.cpp | 561 +++++++++++++++++----------------- 1 file changed, 283 insertions(+), 278 deletions(-) diff --git a/src/osdep/gui/PanelConfig.cpp b/src/osdep/gui/PanelConfig.cpp index a62dce1f..5131b67f 100644 --- a/src/osdep/gui/PanelConfig.cpp +++ b/src/osdep/gui/PanelConfig.cpp @@ -21,7 +21,7 @@ #include "gui.h" #include "gui_handling.h" -static char last_active_config[MAX_DPATH] = {'\0'}; +static char last_active_config[MAX_DPATH] = { '\0' }; static int ensureVisible = -1; static gcn::Button* cmdLoad; @@ -56,306 +56,311 @@ bool LoadConfigByName(const char *name) } return false; - } +} - void SetLastActiveConfig(const char* filename) - { - extractFileName(filename, last_active_config); - removeFileExtension(last_active_config); - } +void SetLastActiveConfig(const char* filename) +{ + extractFileName(filename, last_active_config); + removeFileExtension(last_active_config); +} - class ConfigsListModel : public gcn::ListModel - { - vector configs; +class ConfigsListModel : public gcn::ListModel +{ + vector configs; - public: - ConfigsListModel() +public: + ConfigsListModel() = default; - int getNumberOfElements() override - { - return configs.size(); - } - - string getElementAt(int i) override - { - if (i >= configs.size() || i < 0) - return "---"; - return configs[i]; - } - - void InitConfigsList(void) - { - configs.clear(); - for (auto & i : ConfigFilesList) - { - char tmp[MAX_DPATH]; - strncpy(tmp, i->Name, MAX_DPATH); - if (strlen(i->Description) > 0) - { - strncat(tmp, " (", MAX_DPATH - 1); - strncat(tmp, i->Description, MAX_DPATH - 1); - strncat(tmp, ")", MAX_DPATH - 1); - } - configs.emplace_back(tmp); - } - } - }; - - static ConfigsListModel* configsList; - - - class ConfigButtonActionListener : public gcn::ActionListener + int getNumberOfElements() override { - public: - void action(const gcn::ActionEvent& actionEvent) override - { - int i; - if (actionEvent.getSource() == cmdLoad) - { - //----------------------------------------------- - // Load selected configuration - //----------------------------------------------- - i = lstConfigs->getSelected(); - if (emulating) - { - uae_restart(-1, ConfigFilesList[i]->FullPath); - } - else - { - target_cfgfile_load(&changed_prefs, ConfigFilesList[i]->FullPath, 0, 0); - strncpy(last_active_config, ConfigFilesList[i]->Name, MAX_DPATH); - DisableResume(); - RefreshAllPanels(); - } - } - else if (actionEvent.getSource() == cmdSave) - { - //----------------------------------------------- - // Save current configuration - //----------------------------------------------- - char filename[MAX_DPATH]; - if (!txtName->getText().empty()) - { - fetch_configurationpath(filename, MAX_DPATH); - strncat(filename, txtName->getText().c_str(), MAX_DPATH - 1); - strncat(filename, ".uae", MAX_DPATH); - strncpy(changed_prefs.description, txtDesc->getText().c_str(), 256); - if (cfgfile_save(&changed_prefs, filename, 0)) - RefreshPanelConfig(); - } - } - else if (actionEvent.getSource() == cmdDelete) - { - //----------------------------------------------- - // Delete selected config - //----------------------------------------------- - char msg[256]; - i = lstConfigs->getSelected(); - if (i >= 0 && strcmp(ConfigFilesList[i]->Name, OPTIONSFILENAME) != 0) - { - snprintf(msg, 256, "Do you want to delete '%s' ?", ConfigFilesList[i]->Name); - if (ShowMessage("Delete Configuration", msg, "", "Yes", "No")) - { - remove(ConfigFilesList[i]->FullPath); - if (!strcmp(last_active_config, ConfigFilesList[i]->Name)) - { - txtName->setText(""); - txtDesc->setText(""); - last_active_config[0] = '\0'; - } - ConfigFilesList.erase(ConfigFilesList.begin() + i); - RefreshPanelConfig(); - } - cmdDelete->requestFocus(); - } - } - } - }; + return configs.size(); + } - static ConfigButtonActionListener* configButtonActionListener; - - - class ConfigsListActionListener : public gcn::ActionListener + string getElementAt(int i) override { - public: - void action(const gcn::ActionEvent& actionEvent) override + if (i >= configs.size() || i < 0) + return "---"; + return configs[i]; + } + + void InitConfigsList(void) + { + configs.clear(); + for (auto & i : ConfigFilesList) { - const int selected_item = lstConfigs->getSelected(); - if (txtName->getText() != ConfigFilesList[selected_item]->Name) + char tmp[MAX_DPATH]; + strncpy(tmp, i->Name, MAX_DPATH); + if (strlen(i->Description) > 0) { - //----------------------------------------------- - // Selected same config again -> load and start it - //----------------------------------------------- - if (emulating) - { - uae_restart(0, ConfigFilesList[selected_item]->FullPath); - } - else - { - target_cfgfile_load(&changed_prefs, ConfigFilesList[selected_item]->FullPath, 0, 0); - strncpy(last_active_config, ConfigFilesList[selected_item]->Name, MAX_DPATH); - DisableResume(); - RefreshAllPanels(); - uae_reset(0, 1); - } - gui_running = false; + strncat(tmp, " (", MAX_DPATH - 1); + strncat(tmp, i->Description, MAX_DPATH - 1); + strncat(tmp, ")", MAX_DPATH - 1); + } + configs.emplace_back(tmp); + } + } +}; + +static ConfigsListModel* configsList; + + +class ConfigButtonActionListener : public gcn::ActionListener +{ +public: + void action(const gcn::ActionEvent& actionEvent) override + { + int i; + if (actionEvent.getSource() == cmdLoad) + { + //----------------------------------------------- + // Load selected configuration + //----------------------------------------------- + i = lstConfigs->getSelected(); + if (emulating) + { + uae_restart(-1, ConfigFilesList[i]->FullPath); } else { - txtName->setText(ConfigFilesList[selected_item]->Name); - txtDesc->setText(ConfigFilesList[selected_item]->Description); + target_cfgfile_load(&changed_prefs, ConfigFilesList[i]->FullPath, 0, 0); + strncpy(last_active_config, ConfigFilesList[i]->Name, MAX_DPATH); + DisableResume(); + RefreshAllPanels(); } } - }; - - static ConfigsListActionListener* configsListActionListener; - - void InitPanelConfig(const struct _ConfigCategory& category) - { - configButtonActionListener = new ConfigButtonActionListener(); - - cmdLoad = new gcn::Button("Load"); - cmdLoad->setSize(BUTTON_WIDTH, BUTTON_HEIGHT); - cmdLoad->setBaseColor(gui_baseCol); - cmdLoad->setId("ConfigLoad"); - cmdLoad->addActionListener(configButtonActionListener); - - cmdSave = new gcn::Button("Save"); - cmdSave->setSize(BUTTON_WIDTH, BUTTON_HEIGHT); - cmdSave->setBaseColor(gui_baseCol); - cmdSave->setId("ConfigSave"); - cmdSave->addActionListener(configButtonActionListener); - - cmdDelete = new gcn::Button("Delete"); - cmdDelete->setSize(BUTTON_WIDTH, BUTTON_HEIGHT); - cmdDelete->setBaseColor(gui_baseCol); - cmdDelete->setId("CfgDelete"); - cmdDelete->addActionListener(configButtonActionListener); - - auto buttonX = DISTANCE_BORDER; - const auto buttonY = category.panel->getHeight() - DISTANCE_BORDER - BUTTON_HEIGHT; - category.panel->add(cmdLoad, buttonX, buttonY); - buttonX += BUTTON_WIDTH + DISTANCE_NEXT_X; - category.panel->add(cmdSave, buttonX, buttonY); - buttonX += BUTTON_WIDTH + 3 * DISTANCE_NEXT_X; - buttonX += BUTTON_WIDTH + DISTANCE_NEXT_X; - buttonX = category.panel->getWidth() - DISTANCE_BORDER - BUTTON_WIDTH; - category.panel->add(cmdDelete, buttonX, buttonY); - - lblName = new gcn::Label("Name:"); - lblName->setSize(lblName->getWidth(), lblName->getHeight()); - lblName->setAlignment(gcn::Graphics::RIGHT); - txtName = new gcn::TextField(); - txtName->setSize(300, TEXTFIELD_HEIGHT); - txtName->setId("ConfigName"); - txtName->setBackgroundColor(colTextboxBackground); - - lblDesc = new gcn::Label("Description:"); - lblDesc->setSize(lblDesc->getWidth(), lblDesc->getHeight()); - lblDesc->setAlignment(gcn::Graphics::RIGHT); - txtDesc = new gcn::TextField(); - txtDesc->setSize(300, TEXTFIELD_HEIGHT); - txtDesc->setId("ConfigDesc"); - txtDesc->setBackgroundColor(colTextboxBackground); - - ReadConfigFileList(); - configsList = new ConfigsListModel(); - configsList->InitConfigsList(); - configsListActionListener = new ConfigsListActionListener(); - - lstConfigs = new gcn::UaeListBox(configsList); - lstConfigs->setSize(category.panel->getWidth() - 2 * DISTANCE_BORDER - 22, 232); - lstConfigs->setBackgroundColor(colTextboxBackground); - lstConfigs->setWrappingEnabled(true); - lstConfigs->setId("ConfigList"); - lstConfigs->addActionListener(configsListActionListener); - - scrAreaConfigs = new gcn::ScrollArea(lstConfigs); -#ifdef USE_SDL1 - scrAreaConfigs->setFrameSize(1); -#elif USE_SDL2 - scrAreaConfigs->setBorderSize(1); -#endif - scrAreaConfigs->setPosition(DISTANCE_BORDER, DISTANCE_BORDER); - scrAreaConfigs->setSize(category.panel->getWidth() - 2 * DISTANCE_BORDER - 2, 252); - scrAreaConfigs->setScrollbarWidth(20); - scrAreaConfigs->setBackgroundColor(colTextboxBackground); - category.panel->add(scrAreaConfigs); - - category.panel->add(lblName, DISTANCE_BORDER, scrAreaConfigs->getY() + scrAreaConfigs->getHeight() + DISTANCE_NEXT_Y); - category.panel->add(txtName, DISTANCE_BORDER + lblDesc->getWidth() + 8, scrAreaConfigs->getY() + scrAreaConfigs->getHeight() + DISTANCE_NEXT_Y); - category.panel->add(lblDesc, DISTANCE_BORDER, lblName->getY() + lblName->getHeight() + DISTANCE_NEXT_Y); - category.panel->add(txtDesc, DISTANCE_BORDER + lblDesc->getWidth() + 8, txtName->getY() + txtName->getHeight() + DISTANCE_NEXT_Y); - - if (strlen(last_active_config) == 0) - strncpy(last_active_config, OPTIONSFILENAME, MAX_DPATH); - txtName->setText(last_active_config); - txtDesc->setText(changed_prefs.description); - ensureVisible = -1; - RefreshPanelConfig(); - } - - void ExitPanelConfig() - { - delete lstConfigs; - delete scrAreaConfigs; - delete configsListActionListener; - delete configsList; - - delete cmdLoad; - delete cmdSave; - delete cmdDelete; - - delete configButtonActionListener; - - delete lblName; - delete txtName; - delete lblDesc; - delete txtDesc; - } - - static void MakeCurrentVisible() - { - if (ensureVisible >= 0) + else if (actionEvent.getSource() == cmdSave) { - scrAreaConfigs->setVerticalScrollAmount(ensureVisible * 19); - ensureVisible = -1; - } - } - - void RefreshPanelConfig() - { - ReadConfigFileList(); - configsList->InitConfigsList(); - - // Search current entry - if (!txtName->getText().empty()) - { - for (auto i = 0; i < ConfigFilesList.size(); ++i) + //----------------------------------------------- + // Save current configuration + //----------------------------------------------- + char filename[MAX_DPATH]; + if (!txtName->getText().empty()) { - if (!_tcscmp(ConfigFilesList[i]->Name, txtName->getText().c_str())) + fetch_configurationpath(filename, MAX_DPATH); + strncat(filename, txtName->getText().c_str(), MAX_DPATH - 1); + strncat(filename, ".uae", MAX_DPATH); + strncpy(changed_prefs.description, txtDesc->getText().c_str(), 256); + if (cfgfile_save(&changed_prefs, filename, 0)) + RefreshPanelConfig(); + } + } + else if (actionEvent.getSource() == cmdDelete) + { + //----------------------------------------------- + // Delete selected config + //----------------------------------------------- + char msg[256]; + i = lstConfigs->getSelected(); + if (i >= 0 && strcmp(ConfigFilesList[i]->Name, OPTIONSFILENAME) != 0) + { + snprintf(msg, 256, "Do you want to delete '%s' ?", ConfigFilesList[i]->Name); + if (ShowMessage("Delete Configuration", msg, "", "Yes", "No")) { - // Select current entry - lstConfigs->setSelected(i); - ensureVisible = i; - RegisterRefreshFunc(MakeCurrentVisible); - break; + remove(ConfigFilesList[i]->FullPath); + if (!strcmp(last_active_config, ConfigFilesList[i]->Name)) + { + txtName->setText(""); + txtDesc->setText(""); + last_active_config[0] = '\0'; + } + ConfigFilesList.erase(ConfigFilesList.begin() + i); + RefreshPanelConfig(); } + cmdDelete->requestFocus(); } } } +}; - bool HelpPanelConfig(std::vector &helptext) +static ConfigButtonActionListener* configButtonActionListener; + + +class ConfigsListActionListener : public gcn::ActionListener +{ +public: + void action(const gcn::ActionEvent& actionEvent) override { - helptext.clear(); - helptext.emplace_back("To load a configuration, select the entry in the list and then click on \"Load\"."); - helptext.emplace_back("If you doubleclick on an entry in the list, the emulation starts with this configuration."); - helptext.emplace_back(""); - helptext.emplace_back("If you want to create a new configuration, setup all options, enter a new name in"); - helptext.emplace_back(R"("Name", provide a short description and then click on "Save".)"); - helptext.emplace_back(""); - helptext.emplace_back("\"Delete\" will delete the selected configuration."); - return true; + const int selected_item = lstConfigs->getSelected(); + if (txtName->getText() != ConfigFilesList[selected_item]->Name || txtDesc->getText() != ConfigFilesList[selected_item]->Description) + { + txtName->setText(ConfigFilesList[selected_item]->Name); + txtDesc->setText(ConfigFilesList[selected_item]->Description); + //----------------------------------------------- + // Selected same config again -> load and start it + //----------------------------------------------- + //if (emulating) + //{ + // uae_restart(0, ConfigFilesList[selected_item]->FullPath); + //} + //else + //{ + // target_cfgfile_load(&changed_prefs, ConfigFilesList[selected_item]->FullPath, 0, 0); + // //strncpy(last_active_config, ConfigFilesList[selected_item]->Name, MAX_DPATH); + // txtName->setText(ConfigFilesList[selected_item]->Name); + // txtDesc->setText(ConfigFilesList[selected_item]->Description); + // DisableResume(); + // RefreshAllPanels(); + //uae_reset(0, 1); + //} + //gui_running = false; + } + //else + //{ + // txtName->setText(ConfigFilesList[selected_item]->Name); + // txtDesc->setText(ConfigFilesList[selected_item]->Description); + //} } +}; + +static ConfigsListActionListener* configsListActionListener; + +void InitPanelConfig(const struct _ConfigCategory& category) +{ + configButtonActionListener = new ConfigButtonActionListener(); + + cmdLoad = new gcn::Button("Load"); + cmdLoad->setSize(BUTTON_WIDTH, BUTTON_HEIGHT); + cmdLoad->setBaseColor(gui_baseCol); + cmdLoad->setId("ConfigLoad"); + cmdLoad->addActionListener(configButtonActionListener); + + cmdSave = new gcn::Button("Save"); + cmdSave->setSize(BUTTON_WIDTH, BUTTON_HEIGHT); + cmdSave->setBaseColor(gui_baseCol); + cmdSave->setId("ConfigSave"); + cmdSave->addActionListener(configButtonActionListener); + + cmdDelete = new gcn::Button("Delete"); + cmdDelete->setSize(BUTTON_WIDTH, BUTTON_HEIGHT); + cmdDelete->setBaseColor(gui_baseCol); + cmdDelete->setId("CfgDelete"); + cmdDelete->addActionListener(configButtonActionListener); + + auto buttonX = DISTANCE_BORDER; + const auto buttonY = category.panel->getHeight() - DISTANCE_BORDER - BUTTON_HEIGHT; + category.panel->add(cmdLoad, buttonX, buttonY); + buttonX += BUTTON_WIDTH + DISTANCE_NEXT_X; + category.panel->add(cmdSave, buttonX, buttonY); + buttonX += BUTTON_WIDTH + 3 * DISTANCE_NEXT_X; + buttonX += BUTTON_WIDTH + DISTANCE_NEXT_X; + buttonX = category.panel->getWidth() - DISTANCE_BORDER - BUTTON_WIDTH; + category.panel->add(cmdDelete, buttonX, buttonY); + + lblName = new gcn::Label("Name:"); + lblName->setSize(lblName->getWidth(), lblName->getHeight()); + lblName->setAlignment(gcn::Graphics::RIGHT); + txtName = new gcn::TextField(); + txtName->setSize(300, TEXTFIELD_HEIGHT); + txtName->setId("ConfigName"); + txtName->setBackgroundColor(colTextboxBackground); + + lblDesc = new gcn::Label("Description:"); + lblDesc->setSize(lblDesc->getWidth(), lblDesc->getHeight()); + lblDesc->setAlignment(gcn::Graphics::RIGHT); + txtDesc = new gcn::TextField(); + txtDesc->setSize(300, TEXTFIELD_HEIGHT); + txtDesc->setId("ConfigDesc"); + txtDesc->setBackgroundColor(colTextboxBackground); + + ReadConfigFileList(); + configsList = new ConfigsListModel(); + configsList->InitConfigsList(); + configsListActionListener = new ConfigsListActionListener(); + + lstConfigs = new gcn::UaeListBox(configsList); + lstConfigs->setSize(category.panel->getWidth() - 2 * DISTANCE_BORDER - 22, 232); + lstConfigs->setBaseColor(gui_baseCol); + lstConfigs->setBackgroundColor(colTextboxBackground); + lstConfigs->setWrappingEnabled(true); + lstConfigs->setId("ConfigList"); + lstConfigs->addActionListener(configsListActionListener); + + scrAreaConfigs = new gcn::ScrollArea(lstConfigs); +#ifdef USE_SDL1 + scrAreaConfigs->setFrameSize(1); +#elif USE_SDL2 + scrAreaConfigs->setBorderSize(1); +#endif + scrAreaConfigs->setPosition(DISTANCE_BORDER, DISTANCE_BORDER); + scrAreaConfigs->setSize(category.panel->getWidth() - 2 * DISTANCE_BORDER - 2, 252); + scrAreaConfigs->setScrollbarWidth(20); + scrAreaConfigs->setBackgroundColor(colTextboxBackground); + category.panel->add(scrAreaConfigs); + + category.panel->add(lblName, DISTANCE_BORDER, scrAreaConfigs->getY() + scrAreaConfigs->getHeight() + DISTANCE_NEXT_Y); + category.panel->add(txtName, DISTANCE_BORDER + lblDesc->getWidth() + 8, scrAreaConfigs->getY() + scrAreaConfigs->getHeight() + DISTANCE_NEXT_Y); + category.panel->add(lblDesc, DISTANCE_BORDER, lblName->getY() + lblName->getHeight() + DISTANCE_NEXT_Y); + category.panel->add(txtDesc, DISTANCE_BORDER + lblDesc->getWidth() + 8, txtName->getY() + txtName->getHeight() + DISTANCE_NEXT_Y); + + if (strlen(last_active_config) == 0) + strncpy(last_active_config, OPTIONSFILENAME, MAX_DPATH); + txtName->setText(last_active_config); + txtDesc->setText(changed_prefs.description); + ensureVisible = -1; + RefreshPanelConfig(); +} + +void ExitPanelConfig() +{ + delete lstConfigs; + delete scrAreaConfigs; + delete configsListActionListener; + delete configsList; + + delete cmdLoad; + delete cmdSave; + delete cmdDelete; + + delete configButtonActionListener; + + delete lblName; + delete txtName; + delete lblDesc; + delete txtDesc; +} + +static void MakeCurrentVisible() +{ + if (ensureVisible >= 0) + { + scrAreaConfigs->setVerticalScrollAmount(ensureVisible * 19); + ensureVisible = -1; + } +} + +void RefreshPanelConfig() +{ + ReadConfigFileList(); + configsList->InitConfigsList(); + + // Search current entry + if (!txtName->getText().empty()) + { + for (auto i = 0; i < ConfigFilesList.size(); ++i) + { + if (!_tcscmp(ConfigFilesList[i]->Name, txtName->getText().c_str())) + { + // Select current entry + lstConfigs->setSelected(i); + ensureVisible = i; + RegisterRefreshFunc(MakeCurrentVisible); + break; + } + } + } +} + +bool HelpPanelConfig(std::vector &helptext) +{ + helptext.clear(); + helptext.emplace_back("To load a configuration, select the entry in the list and then click on \"Load\"."); + helptext.emplace_back("If you doubleclick on an entry in the list, the emulation starts with this configuration."); + helptext.emplace_back(""); + helptext.emplace_back("If you want to create a new configuration, setup all options, enter a new name in"); + helptext.emplace_back(R"("Name", provide a short description and then click on "Save".)"); + helptext.emplace_back(""); + helptext.emplace_back("\"Delete\" will delete the selected configuration."); + return true; +}