From 756c40163d48aadc3e2d42de30cb3a693188556d Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Thu, 3 Mar 2011 22:47:48 +0000 Subject: [PATCH] Merge the log window CreateGUIControls and LoadSettings methods. This allows the settings from the ini file to be applied when the controls are created rather than setting a default, and then changing the settings later. In particular word wrap is applied when the text control is created. This works around the crash at application start that users are reporting in issue 4196. Also change the for loops in SysConf to use iterators to placate godisgovernment and billiard. :P git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7283 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/SysConf.cpp | 28 ++--- Source/Core/Common/Src/SysConf.h | 40 +++---- Source/Core/DolphinWX/Src/LogWindow.cpp | 151 +++++++++++------------- Source/Core/DolphinWX/Src/LogWindow.h | 9 +- 4 files changed, 105 insertions(+), 123 deletions(-) diff --git a/Source/Core/Common/Src/SysConf.cpp b/Source/Core/Common/Src/SysConf.cpp index f98ef21e41..af45ca525d 100644 --- a/Source/Core/Common/Src/SysConf.cpp +++ b/Source/Core/Common/Src/SysConf.cpp @@ -36,11 +36,9 @@ SysConf::~SysConf() void SysConf::Clear() { - for (size_t i = 0; i < m_Entries.size() - 1; i++) - { - delete [] m_Entries[i].data; - m_Entries[i].data = NULL; - } + for (std::vector::const_iterator i = m_Entries.begin(); + i < m_Entries.end() - 1; i++) + delete [] i->data; m_Entries.clear(); } @@ -86,9 +84,10 @@ bool SysConf::LoadFromFileInternal(FILE *f) } // Last offset is an invalid entry. We ignore it throughout this class - for (size_t i = 0; i < m_Entries.size() - 1; i++) + for (std::vector::iterator i = m_Entries.begin(); + i < m_Entries.end() - 1; i++) { - SSysConfEntry& curEntry = m_Entries[i]; + SSysConfEntry& curEntry = *i; if (fseeko(f, curEntry.offset, SEEK_SET) != 0) return false; u8 description = 0; @@ -144,22 +143,23 @@ bool SysConf::SaveToFile(const char *filename) if (f == NULL) return false; - for (size_t i = 0; i < m_Entries.size() - 1; i++) + for (std::vector::iterator i = m_Entries.begin(); + i < m_Entries.end() - 1; i++) { // Seek to after the name of this entry - if (fseeko(f, m_Entries[i].offset + m_Entries[i].nameLength + 1, SEEK_SET) != 0) return false; + if (fseeko(f, i->offset + i->nameLength + 1, SEEK_SET) != 0) return false; // We may have to write array length value... - if (m_Entries[i].type == Type_BigArray) + if (i->type == Type_BigArray) { - u16 tmpDataLength = Common::swap16(m_Entries[i].dataLength); + u16 tmpDataLength = Common::swap16(i->dataLength); if (fwrite(&tmpDataLength, 2, 1, f) != 1) return false; } - else if (m_Entries[i].type == Type_SmallArray) + else if (i->type == Type_SmallArray) { - if (fwrite(&m_Entries[i].dataLength, 1, 1, f) != 1) return false; + if (fwrite(&i->dataLength, 1, 1, f) != 1) return false; } // Now write the actual data - if (fwrite(m_Entries[i].data, m_Entries[i].dataLength, 1, f) != 1) return false; + if (fwrite(i->data, i->dataLength, 1, f) != 1) return false; } fclose(f); diff --git a/Source/Core/Common/Src/SysConf.h b/Source/Core/Common/Src/SysConf.h index bb15722af6..6041e70ee8 100644 --- a/Source/Core/Common/Src/SysConf.h +++ b/Source/Core/Common/Src/SysConf.h @@ -93,19 +93,19 @@ public: return 0; } - size_t index = 0; - for (; index < m_Entries.size() - 1; index++) + std::vector::iterator index = m_Entries.begin(); + for (; index < m_Entries.end() - 1; index++) { - if (strcmp(m_Entries[index].name, sectionName) == 0) + if (strcmp(index->name, sectionName) == 0) break; } - if (index == m_Entries.size() - 1) + if (index == m_Entries.end() - 1) { PanicAlertT("Section %s not found in SYSCONF", sectionName); return 0; } - return m_Entries[index].GetData(); + return index->GetData(); } bool GetArrayData(const char* sectionName, u8* dest, u16 destSize) @@ -116,19 +116,19 @@ public: return 0; } - size_t index = 0; - for (; index < m_Entries.size() - 1; index++) + std::vector::iterator index = m_Entries.begin(); + for (; index < m_Entries.end() - 1; index++) { - if (strcmp(m_Entries[index].name, sectionName) == 0) + if (strcmp(index->name, sectionName) == 0) break; } - if (index == m_Entries.size() - 1) + if (index == m_Entries.end() - 1) { PanicAlertT("Section %s not found in SYSCONF", sectionName); return 0; } - return m_Entries[index].GetArrayData(dest, destSize); + return index->GetArrayData(dest, destSize); } bool SetArrayData(const char* sectionName, u8* buffer, u16 bufferSize) @@ -136,19 +136,19 @@ public: if (!m_IsValid) return false; - size_t index = 0; - for (; index < m_Entries.size() - 1; index++) + std::vector::iterator index = m_Entries.begin(); + for (; index < m_Entries.end() - 1; index++) { - if (strcmp(m_Entries[index].name, sectionName) == 0) + if (strcmp(index->name, sectionName) == 0) break; } - if (index == m_Entries.size() - 1) + if (index == m_Entries.end() - 1) { PanicAlertT("Section %s not found in SYSCONF", sectionName); return false; } - return m_Entries[index].SetArrayData(buffer, bufferSize); + return index->SetArrayData(buffer, bufferSize); } template @@ -157,19 +157,19 @@ public: if (!m_IsValid) return false; - size_t index = 0; - for (; index < m_Entries.size() - 1; index++) + std::vector::iterator index = m_Entries.begin(); + for (; index < m_Entries.end() - 1; index++) { - if (strcmp(m_Entries[index].name, sectionName) == 0) + if (strcmp(index->name, sectionName) == 0) break; } - if (index == m_Entries.size() - 1) + if (index == m_Entries.end() - 1) { PanicAlertT("Section %s not found in SYSCONF", sectionName); return false; } - *(T*)m_Entries[index].data = newValue; + *(T*)index->data = newValue; return true; } diff --git a/Source/Core/DolphinWX/Src/LogWindow.cpp b/Source/Core/DolphinWX/Src/LogWindow.cpp index 3438b05178..768ad8c1ea 100644 --- a/Source/Core/DolphinWX/Src/LogWindow.cpp +++ b/Source/Core/DolphinWX/Src/LogWindow.cpp @@ -61,43 +61,83 @@ CLogWindow::CLogWindow(CFrame *parent, wxWindowID id, const wxPoint& pos, #endif m_LogManager = LogManager::GetInstance(); - for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) - m_LogManager->addListener((LogTypes::LOG_TYPE)i, this); - m_fileLog = m_LogManager->getFileListener(); - m_console = m_LogManager->getConsoleListener(); CreateGUIControls(); - LoadSettings(); - - m_WrapLine->SetValue(m_bWrapLines); - ToggleWrapLine(m_bWrapLines); - m_LogTimer = new wxTimer(this, IDTM_UPDATELOG); m_LogTimer->Start(UPDATETIME); } void CLogWindow::CreateGUIControls() { + IniFile ini; + ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX)); + + ini.Get("LogWindow", "x", &x, Parent->GetSize().GetX() / 2); + ini.Get("LogWindow", "y", &y, Parent->GetSize().GetY()); + ini.Get("LogWindow", "pos", &winpos, wxAUI_DOCK_RIGHT); + + // Set up log listeners + int verbosity; + ini.Get("Options", "Verbosity", &verbosity, 0); + if (verbosity < 1) verbosity = 1; + if (verbosity > MAX_LOGLEVEL) verbosity = MAX_LOGLEVEL; + + ini.Get("Options", "WriteToFile", &m_writeFile, false); + ini.Get("Options", "WriteToConsole", &m_writeConsole, true); + ini.Get("Options", "WriteToWindow", &m_writeWindow, true); + for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) + { + bool enable; + ini.Get("Logs", m_LogManager->getShortName((LogTypes::LOG_TYPE)i), &enable, true); + + if (m_writeWindow && enable) + m_LogManager->addListener((LogTypes::LOG_TYPE)i, this); + else + m_LogManager->removeListener((LogTypes::LOG_TYPE)i, this); + + if (m_writeFile && enable) + m_LogManager->addListener((LogTypes::LOG_TYPE)i, m_LogManager->getFileListener()); + else + m_LogManager->removeListener((LogTypes::LOG_TYPE)i, m_LogManager->getFileListener()); + + if (m_writeConsole && enable) + m_LogManager->addListener((LogTypes::LOG_TYPE)i, m_LogManager->getConsoleListener()); + else + m_LogManager->removeListener((LogTypes::LOG_TYPE)i, m_LogManager->getConsoleListener()); + m_LogManager->setLogLevel((LogTypes::LOG_TYPE)i, (LogTypes::LOG_LEVELS)(verbosity)); + } + // Font m_FontChoice = new wxChoice(this, IDM_FONT, - wxDefaultPosition, wxDefaultSize, 0, NULL, 0, wxDefaultValidator); + wxDefaultPosition, wxDefaultSize, 0, NULL, 0, wxDefaultValidator); m_FontChoice->Append(_("Default font")); m_FontChoice->Append(_("Monospaced font")); m_FontChoice->Append(_("Selected font")); - m_FontChoice->SetSelection(0); + DefaultFont = GetFont(); MonoSpaceFont.SetNativeFontInfoUserDesc(_T("lucida console windows-1252")); LogFont.push_back(DefaultFont); LogFont.push_back(MonoSpaceFont); LogFont.push_back(DebuggerFont); - m_WrapLine = new wxCheckBox(this, IDM_WRAPLINE, _("Word Wrap")); + int font; + ini.Get("Options", "Font", &font, 0); + m_FontChoice->SetSelection(font); - // Log viewer and submit row - m_Log = CreateTextCtrl(this, IDM_LOG, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP); + // Word wrap + bool wrap_lines; + ini.Get("Options", "WrapLines", &wrap_lines, false); + m_WrapLine = new wxCheckBox(this, IDM_WRAPLINE, _("Word Wrap")); + m_WrapLine->SetValue(wrap_lines); + + // Log viewer + m_Log = CreateTextCtrl(this, IDM_LOG, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | + (wrap_lines ? wxTE_WORDWRAP : wxTE_DONTWRAP)); + + // submit row m_cmdline = new wxTextCtrl(this, IDM_SUBMITCMD, wxEmptyString, wxDefaultPosition, wxDefaultSize, - wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB); + wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB); // Sizers wxBoxSizer *sTop = new wxBoxSizer(wxHORIZONTAL); @@ -107,7 +147,7 @@ void CLogWindow::CreateGUIControls() sTop->Add(m_WrapLine, 0, wxALIGN_CENTER_VERTICAL); sBottom = new wxBoxSizer(wxVERTICAL); - PopulateRight(); + PopulateBottom(); wxBoxSizer *sMain = new wxBoxSizer(wxVERTICAL); sMain->Add(sTop, 0, wxEXPAND); @@ -157,57 +197,10 @@ void CLogWindow::SaveSettings() ini.Set("LogWindow", "pos", winpos); } ini.Set("Options", "Font", m_FontChoice->GetSelection()); - ini.Set("Options", "WrapLines", m_bWrapLines); + ini.Set("Options", "WrapLines", m_WrapLine->IsChecked()); ini.Save(File::GetUserPath(F_LOGGERCONFIG_IDX)); } -void CLogWindow::LoadSettings() -{ - IniFile ini; - ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX)); - - ini.Get("LogWindow", "x", &x, Parent->GetSize().GetX() / 2); - ini.Get("LogWindow", "y", &y, Parent->GetSize().GetY()); - ini.Get("LogWindow", "pos", &winpos, wxAUI_DOCK_RIGHT); - - int verbosity,font; - ini.Get("Options", "Verbosity", &verbosity, 0); - if (verbosity < 1) verbosity = 1; - if (verbosity > MAX_LOGLEVEL) verbosity = MAX_LOGLEVEL; - - ini.Get("Options", "Font", &font, 0); - m_FontChoice->SetSelection(font); - if (m_FontChoice->GetSelection() < (int)LogFont.size()) - m_Log->SetDefaultStyle(wxTextAttr(wxNullColour, wxNullColour, LogFont[m_FontChoice->GetSelection()])); - - ini.Get("Options", "WrapLines", &m_bWrapLines, false); - - ini.Get("Options", "WriteToFile", &m_writeFile, false); - ini.Get("Options", "WriteToConsole", &m_writeConsole, true); - ini.Get("Options", "WriteToWindow", &m_writeWindow, true); - for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) - { - bool enable; - ini.Get("Logs", m_LogManager->getShortName((LogTypes::LOG_TYPE)i), &enable, true); - - if (m_writeWindow && enable) - m_LogManager->addListener((LogTypes::LOG_TYPE)i, this); - else - m_LogManager->removeListener((LogTypes::LOG_TYPE)i, this); - - if (m_writeFile && enable) - m_LogManager->addListener((LogTypes::LOG_TYPE)i, m_fileLog); - else - m_LogManager->removeListener((LogTypes::LOG_TYPE)i, m_fileLog); - - if (m_writeConsole && enable) - m_LogManager->addListener((LogTypes::LOG_TYPE)i, m_console); - else - m_LogManager->removeListener((LogTypes::LOG_TYPE)i, m_console); - m_LogManager->setLogLevel((LogTypes::LOG_TYPE)i, (LogTypes::LOG_LEVELS)(verbosity)); - } -} - void CLogWindow::OnSubmit(wxCommandEvent& WXUNUSED (event)) { if (!m_cmdline) return; @@ -225,16 +218,16 @@ void CLogWindow::OnClear(wxCommandEvent& WXUNUSED (event)) msgQueue.pop(); m_LogSection.Leave(); - m_console->ClearScreen(); + m_LogManager->getConsoleListener()->ClearScreen(); } -void CLogWindow::UnPopulateRight() +void CLogWindow::UnPopulateBottom() { sBottom->Detach(m_Log); sBottom->Detach(m_cmdline); } -void CLogWindow::PopulateRight() +void CLogWindow::PopulateBottom() { sBottom->Add(m_Log, 1, wxEXPAND | wxSHRINK); sBottom->Add(m_cmdline, 0, wxEXPAND); @@ -249,11 +242,9 @@ wxTextCtrl* CLogWindow::CreateTextCtrl(wxPanel* parent, wxWindowID id, long Styl #else TC->SetBackgroundColour(*wxBLACK); #endif - if (m_FontChoice) - { - if (m_FontChoice->GetSelection() < (int)LogFont.size()) - TC->SetDefaultStyle(wxTextAttr(wxNullColour, wxNullColour, LogFont[m_FontChoice->GetSelection()])); - } + if (m_FontChoice && m_FontChoice->GetSelection() < (int)LogFont.size()) + TC->SetDefaultStyle(wxTextAttr(wxNullColour, wxNullColour, LogFont[m_FontChoice->GetSelection()])); + return TC; } @@ -268,17 +259,10 @@ void CLogWindow::OnFontChange(wxCommandEvent& event) SaveSettings(); } -// When an option is changed, save the change void CLogWindow::OnWrapLineCheck(wxCommandEvent& event) -{ - m_bWrapLines ^= true; - ToggleWrapLine(event.IsChecked()); - SaveSettings(); -} - -void CLogWindow::ToggleWrapLine(bool word_wrap) { #ifdef __WXGTK__ + // Clear the old word wrap state and set the new m_Log->SetWindowStyleFlag(m_Log->GetWindowStyleFlag() ^ (wxTE_WORDWRAP | wxTE_DONTWRAP)); #else wxString Text; @@ -287,10 +271,10 @@ void CLogWindow::ToggleWrapLine(bool word_wrap) // loop through every letter with GetStyle and then reapply them letter by letter // Prevent m_Log access while it's being destroyed m_LogAccess = false; - UnPopulateRight(); + UnPopulateBottom(); Text = m_Log->GetValue(); m_Log->Destroy(); - if (word_wrap) + if (event.IsChecked()) m_Log = CreateTextCtrl(this, IDM_LOG, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | wxTE_WORDWRAP); else @@ -298,9 +282,10 @@ void CLogWindow::ToggleWrapLine(bool word_wrap) wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP); m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE)); m_Log->AppendText(Text); - PopulateRight(); + PopulateBottom(); m_LogAccess = true; #endif + SaveSettings(); } void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event)) diff --git a/Source/Core/DolphinWX/Src/LogWindow.h b/Source/Core/DolphinWX/Src/LogWindow.h index 2cd30f02e6..551ac4d8c6 100644 --- a/Source/Core/DolphinWX/Src/LogWindow.h +++ b/Source/Core/DolphinWX/Src/LogWindow.h @@ -53,7 +53,6 @@ public: ~CLogWindow(); void SaveSettings(); - void LoadSettings(); void Log(LogTypes::LOG_LEVELS, const char *text); int x, y, winpos; @@ -63,11 +62,9 @@ private: wxFont DefaultFont, MonoSpaceFont; std::vector LogFont; wxTimer *m_LogTimer; - FileLogListener *m_fileLog; - ConsoleListener *m_console; LogManager *m_LogManager; std::queue > msgQueue; - bool m_writeFile, m_writeConsole, m_writeWindow, m_LogAccess, m_bWrapLines; + bool m_writeFile, m_writeConsole, m_writeWindow, m_LogAccess; // Controls wxBoxSizer *sBottom; @@ -83,13 +80,13 @@ private: wxTextCtrl * CreateTextCtrl(wxPanel* parent, wxWindowID id = wxID_ANY, long Style = NULL); void CreateGUIControls(); - void PopulateRight(); void UnPopulateRight(); + void PopulateBottom(); + void UnPopulateBottom(); void OnClose(wxCloseEvent& event); void OnSize(wxSizeEvent& event); void OnSubmit(wxCommandEvent& event); void OnFontChange(wxCommandEvent& event); void OnWrapLineCheck(wxCommandEvent& event); - void ToggleWrapLine(bool word_wrap); void OnClear(wxCommandEvent& event); void OnLogTimer(wxTimerEvent& WXUNUSED(event)); void UpdateLog();