X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=src%2Fbackgroundmgr.cpp;h=5ba1b786d08e99104791d1caad4f922c716820a5;hp=3a93370bea1c274d5ffc4696cf3caaf74ae8ec0a;hb=f7ee98f7d964ed361068179f0e7ea4475ed1abdf;hpb=fbe99ce7041eb409981b1fd8e269827daf4ed168 diff --git a/src/backgroundmgr.cpp b/src/backgroundmgr.cpp index 3a93370..5ba1b78 100644 --- a/src/backgroundmgr.cpp +++ b/src/backgroundmgr.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2001 Kevin Rosenberg ** -** $Id: backgroundmgr.cpp,v 1.11 2001/03/05 19:14:40 kevin Exp $ +** $Id$ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -38,11 +38,9 @@ #include "backgroundsupr.h" #include "backgroundmgr.h" +#ifdef HAVE_WXTHREADS -#if defined(HAVE_CONFIG_H) -#include "config.h" -#endif - +int BackgroundManager::s_iNextButtonID = 0; IMPLEMENT_DYNAMIC_CLASS(BackgroundManager, wxMiniFrame) BEGIN_EVENT_TABLE(BackgroundManager, wxMiniFrame) @@ -50,11 +48,12 @@ EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundManager: EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, BackgroundManager::OnRemoveTask) EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_UNIT_TICK, BackgroundManager::OnUnitTick) EVT_CLOSE(BackgroundManager::OnCloseWindow) -EVT_COMMAND_RANGE(0, 1000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton) +EVT_COMMAND_RANGE(0, 30000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton) END_EVENT_TABLE() + BackgroundManager::BackgroundManager () - : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50)) //, wxTHICK_FRAME) + : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50)) { m_iNumTasks = 0; m_pCanvas = new BackgroundManagerCanvas (this); @@ -64,15 +63,19 @@ BackgroundManager::BackgroundManager () m_sizeLabel.Set (140, 20); m_sizeBorder.Set (4, 4); m_sizeCellSpacing.Set (3, 3); - //m_sizeButton.Set (70, 20); - m_sizeButton.Set (0, 0); + m_sizeButton.Set (70, 20); m_sizeCell.Set (m_sizeGauge.x + m_sizeLabel.x + m_sizeCellSpacing.x + m_sizeButton.x, 25); + theApp->getMainFrame()->SetFocus(); Show(false); } +BackgroundManager::~BackgroundManager() +{ +} + void BackgroundManager::OnCloseWindow (wxCloseEvent& event) { @@ -86,41 +89,47 @@ void BackgroundManager::OnUnitTick (wxCommandEvent& event) { int iUnits = event.GetInt(); - BackgroundSupervisor* pTask = reinterpret_cast(event.GetClientData()); - if (pTask == NULL) { - sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]"); + + BackgroundSupervisor* pSupervisor = reinterpret_cast(event.GetClientData()); + if (pSupervisor == NULL) { + sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnUnitTick]"); return; } - if (wxGauge* pGauge = lookupGauge (pTask)) - pGauge->SetValue (iUnits); + + BackgroundManagerTask* pTask = lookupTask (pSupervisor); + if (pTask == NULL) { + sys_error (ERR_SEVERE, "Error looking up task [BackgroundManager::OnUnitTick]"); + return; + } + pTask->gauge()->SetValue (iUnits); } void BackgroundManager::OnAddTask (wxCommandEvent& event) { int iNumUnits = event.GetInt(); - const char* const pszTaskName = event.GetString().c_str(); - BackgroundSupervisor* pTask = reinterpret_cast(event.GetClientData()); - if (pTask == NULL) { - sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]"); + const char* const pszTaskName = event.GetString().mb_str(wxConvUTF8); + BackgroundSupervisor* pSupervisor = reinterpret_cast(event.GetClientData()); + if (pSupervisor == NULL) { + sys_error (ERR_SEVERE, "Received NULL supervisor [BackgroundManager::OnAddTask]"); return; } wxCriticalSectionLocker locker (m_criticalSection); - int iNumTasks = m_vecpBackgroundTasks.size(); - std::vector vecPositionUsed (iNumTasks); - int i; - for (i = 0; i < iNumTasks; i++) - vecPositionUsed[i] = false; - - for (i = 0; i < iNumTasks; i++) { - int iPosUsed = m_vecpPositions[i]; + + int iNumTasks = m_vecpTasks.size(); + std::vector vecPositionUsed (iNumTasks); //vector of used table positions + for (int iP = 0; iP < iNumTasks; iP++) + vecPositionUsed[iP] = false; + + for (TaskContainer::iterator iT = m_vecpTasks.begin(); iT != m_vecpTasks.end(); iT++) { + int iPosUsed = (*iT)->position(); if (iPosUsed < iNumTasks) vecPositionUsed[iPosUsed] = true; } int iFirstUnusedPos = iNumTasks; // default is just past current number of tasks - for (i = 0; i < iNumTasks; i++) + for (int i = 0; i < iNumTasks; i++) if (! vecPositionUsed[i]) { iFirstUnusedPos = i; break; @@ -128,90 +137,103 @@ BackgroundManager::OnAddTask (wxCommandEvent& event) wxPoint posGauge (m_sizeBorder.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y); wxPoint posLabel (m_sizeBorder.x + m_sizeGauge.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y); + wxPoint posButton (m_sizeBorder.x + m_sizeGauge.x + m_sizeLabel.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y); wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, posGauge, m_sizeGauge); - wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, pszTaskName, posLabel, m_sizeLabel); - // wxPoint posButton (m_sizeBorder.x + m_sizeGauge.x + m_sizeLabel.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y); -// wxButton* pCancelButton = new wxButton (m_pCanvas, iFirstUnusedPos, _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT); - - m_vecpBackgroundTasks.push_back (pTask); - m_vecpGauges.push_back (pGauge); - m_vecpNames.push_back (new std::string (pszTaskName)); - m_vecpPositions.push_back (iFirstUnusedPos); - m_vecpLabels.push_back (pLabel); - //m_vecpCancelButtons.push_back (pCancelButton); + wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, wxConvUTF8.cMB2WX(pszTaskName), posLabel, m_sizeLabel); + wxButton* pButton = new wxButton (m_pCanvas, s_iNextButtonID, _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT); + + BackgroundManagerTask* pTask = new BackgroundManagerTask (pSupervisor, pszTaskName, + iFirstUnusedPos, pGauge, pLabel, pButton, s_iNextButtonID); + + m_vecpTasks.push_back (pTask); m_iNumTasks++; + s_iNextButtonID++; resizeWindow(); if (m_iNumTasks == 1) { - Show(true); - theApp->getMainFrame()->SetFocus(); // necessary to keep wxWindows from crashing + m_pCanvas->SetFocus(); + Show(true); } } void BackgroundManager::OnRemoveTask (wxCommandEvent& event) { - BackgroundSupervisor* pTask = reinterpret_cast(event.GetClientData()); - if (pTask == NULL) { - sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]"); + BackgroundSupervisor* pSupervisor = reinterpret_cast(event.GetClientData()); + if (pSupervisor == NULL) { + sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnRemoveTask]"); return; } wxCriticalSectionLocker locker (m_criticalSection); - StringContainer::iterator iName = m_vecpNames.begin(); - GaugeContainer::iterator iGauge = m_vecpGauges.begin(); - PositionContainer::iterator iPosition = m_vecpPositions.begin(); - LabelContainer::iterator iLabel = m_vecpLabels.begin(); - //ButtonContainer::iterator iCancelButton = m_vecpCancelButtons.begin(); - for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) { - if (*iTask == pTask) { - delete *iName; - delete *iGauge; - delete *iLabel; - //delete *iCancelButton; - m_vecpBackgroundTasks.erase (iTask); - m_vecpGauges.erase (iGauge); - m_vecpNames.erase (iName); - m_vecpPositions.erase (iPosition); - m_vecpLabels.erase (iLabel); - //m_vecpCancelButtons.erase (iCancelButton); + bool bFound = false; + for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) { + if ((*iTask)->supervisor() == pSupervisor) { + delete (*iTask)->gauge(); + delete (*iTask)->label(); + delete (*iTask)->button(); + delete *iTask; + m_vecpTasks.erase (iTask); m_iNumTasks--; + bFound = true; break; } - iName++; - iGauge++; - iPosition++; - iLabel++; - //iCancelButton++; } - + if (! bFound) { + sys_error (ERR_SEVERE, "Unable to find supervisor [BackgroundManager::OnRemoveTask]"); + return; + } + pSupervisor->ackRemoveBackgroundManager(); resizeWindow(); - if (m_iNumTasks <= 0) + if (m_iNumTasks <= 0) { + m_pCanvas->SetFocus(); Show(false); + } } void BackgroundManager::OnCancelButton (wxCommandEvent& event) { + BackgroundManagerTask* pTask = lookupTask (event.GetId()); + if (! pTask) { + sys_error (ERR_SEVERE, "Unable to lookup task for button"); + return; + } + + pTask->supervisor()->onCancel(); } -wxGauge* -BackgroundManager::lookupGauge (BackgroundSupervisor* pTask) +BackgroundManagerTask* +BackgroundManager::lookupTask (BackgroundSupervisor* pSupervisor) { - wxGauge* pGauge = NULL; - int i = 0; + BackgroundManagerTask* pTask = NULL; wxCriticalSectionLocker locker (m_criticalSection); - for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) { - if (*iTask == pTask) { - pGauge = m_vecpGauges[i]; + for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) { + if ((*iTask)->supervisor() == pSupervisor) { + pTask = *iTask; break; } - i++; } - return pGauge; + return pTask; +} + +BackgroundManagerTask* +BackgroundManager::lookupTask (int iButtonID) +{ + BackgroundManagerTask* pTask = NULL; + + wxCriticalSectionLocker locker (m_criticalSection); + for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) { + if ((*iTask)->buttonID() == iButtonID) { + pTask = *iTask; + break; + } + } + + return pTask; } void @@ -219,9 +241,10 @@ BackgroundManager::resizeWindow() { int iHighestPosition = -1; - for (unsigned int i = 0; i < m_vecpPositions.size(); i++) - if (iHighestPosition < m_vecpPositions[i]) - iHighestPosition = m_vecpPositions[i]; + wxCriticalSectionLocker lock (m_criticalSection); + for (TaskContainer::iterator i = m_vecpTasks.begin(); i != m_vecpTasks.end(); i++) + if (iHighestPosition < (*i)->position()) + iHighestPosition = (*i)->position(); wxSize sizeWindow (m_sizeCell.x, m_sizeCell.y * (iHighestPosition + 1)); SetClientSize (sizeWindow); @@ -232,7 +255,6 @@ BackgroundManager::resizeWindow() IMPLEMENT_DYNAMIC_CLASS(BackgroundManagerCanvas, wxPanel) BEGIN_EVENT_TABLE(BackgroundManagerCanvas, wxPanel) -//EVT_PAINT(BackgroundManagerCanvas::OnPaint) END_EVENT_TABLE() BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr) @@ -240,12 +262,5 @@ BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr) { } -#if 0 -void -BackgroundManagerCanvas::OnPaint (wxPaintEvent& event) -{ - wxPaintDC dc (this); -// dc.DrawLine (0, 0, 30, 30); -// dc.DrawLine (30,0, 0, 30); -} -#endif + +#endif // HAVE_WXTHREADS