1 /*****************************************************************************
4 ** Name: backgroundmgr.cpp
5 ** Purpose: Background manager class
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: February 2001
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2001 Kevin Rosenberg
12 ** $Id: backgroundmgr.cpp,v 1.20 2001/03/18 18:08:26 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
28 #include "wx/wxprec.h"
38 #include "backgroundsupr.h"
39 #include "backgroundmgr.h"
43 int BackgroundManager::s_iNextButtonID = 0;
45 IMPLEMENT_DYNAMIC_CLASS(BackgroundManager, wxMiniFrame)
46 BEGIN_EVENT_TABLE(BackgroundManager, wxMiniFrame)
47 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundManager::OnAddTask)
48 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, BackgroundManager::OnRemoveTask)
49 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_UNIT_TICK, BackgroundManager::OnUnitTick)
50 EVT_CLOSE(BackgroundManager::OnCloseWindow)
51 EVT_COMMAND_RANGE(0, 30000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton)
55 BackgroundManager::BackgroundManager ()
56 : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50))
59 m_pCanvas = new BackgroundManagerCanvas (this);
60 theApp->setIconForFrame (this);
62 m_sizeGauge.Set (70, 20);
63 m_sizeLabel.Set (140, 20);
64 m_sizeBorder.Set (4, 4);
65 m_sizeCellSpacing.Set (3, 3);
66 m_sizeButton.Set (70, 20);
68 m_sizeCell.Set (m_sizeGauge.x + m_sizeLabel.x + m_sizeCellSpacing.x + m_sizeButton.x, 25);
70 theApp->getMainFrame()->SetFocus();
75 BackgroundManager::~BackgroundManager()
80 BackgroundManager::OnCloseWindow (wxCloseEvent& event)
82 if (theApp->getMainFrame()->getShuttingDown())
83 wxMiniFrame::OnCloseWindow (event);
89 BackgroundManager::OnUnitTick (wxCommandEvent& event)
91 int iUnits = event.GetInt();
93 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
94 if (pSupervisor == NULL) {
95 sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnUnitTick]");
99 BackgroundManagerTask* pTask = lookupTask (pSupervisor);
101 sys_error (ERR_SEVERE, "Error looking up task [BackgroundManager::OnUnitTick]");
104 pTask->gauge()->SetValue (iUnits);
108 BackgroundManager::OnAddTask (wxCommandEvent& event)
110 int iNumUnits = event.GetInt();
111 const char* const pszTaskName = event.GetString().c_str();
112 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
113 if (pSupervisor == NULL) {
114 sys_error (ERR_SEVERE, "Received NULL supervisor [BackgroundManager::OnAddTask]");
118 wxCriticalSectionLocker locker (m_criticalSection);
120 int iNumTasks = m_vecpTasks.size();
121 std::vector<bool> vecPositionUsed (iNumTasks); //vector of used table positions
122 for (int iP = 0; iP < iNumTasks; iP++)
123 vecPositionUsed[iP] = false;
125 for (TaskContainer::iterator iT = m_vecpTasks.begin(); iT != m_vecpTasks.end(); iT++) {
126 int iPosUsed = (*iT)->position();
127 if (iPosUsed < iNumTasks)
128 vecPositionUsed[iPosUsed] = true;
131 int iFirstUnusedPos = iNumTasks; // default is just past current number of tasks
132 for (int i = 0; i < iNumTasks; i++)
133 if (! vecPositionUsed[i]) {
138 wxPoint posGauge (m_sizeBorder.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
139 wxPoint posLabel (m_sizeBorder.x + m_sizeGauge.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
140 wxPoint posButton (m_sizeBorder.x + m_sizeGauge.x + m_sizeLabel.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
141 wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, posGauge, m_sizeGauge);
142 wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, pszTaskName, posLabel, m_sizeLabel);
143 wxButton* pButton = new wxButton (m_pCanvas, s_iNextButtonID, _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT);
145 BackgroundManagerTask* pTask = new BackgroundManagerTask (pSupervisor, pszTaskName,
146 iFirstUnusedPos, pGauge, pLabel, pButton, s_iNextButtonID);
148 m_vecpTasks.push_back (pTask);
153 if (m_iNumTasks == 1) {
154 m_pCanvas->SetFocus();
160 BackgroundManager::OnRemoveTask (wxCommandEvent& event)
162 BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
163 if (pSupervisor == NULL) {
164 sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnRemoveTask]");
168 wxCriticalSectionLocker locker (m_criticalSection);
171 for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
172 if ((*iTask)->supervisor() == pSupervisor) {
173 delete (*iTask)->gauge();
174 delete (*iTask)->label();
175 delete (*iTask)->button();
177 m_vecpTasks.erase (iTask);
184 sys_error (ERR_SEVERE, "Unable to find supervisor [BackgroundManager::OnRemoveTask]");
187 pSupervisor->ackRemoveBackgroundManager();
189 if (m_iNumTasks <= 0) {
190 m_pCanvas->SetFocus();
196 BackgroundManager::OnCancelButton (wxCommandEvent& event)
198 BackgroundManagerTask* pTask = lookupTask (event.GetId());
200 sys_error (ERR_SEVERE, "Unable to lookup task for button");
204 pTask->supervisor()->onCancel();
207 BackgroundManagerTask*
208 BackgroundManager::lookupTask (BackgroundSupervisor* pSupervisor)
210 BackgroundManagerTask* pTask = NULL;
212 wxCriticalSectionLocker locker (m_criticalSection);
213 for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
214 if ((*iTask)->supervisor() == pSupervisor) {
223 BackgroundManagerTask*
224 BackgroundManager::lookupTask (int iButtonID)
226 BackgroundManagerTask* pTask = NULL;
228 wxCriticalSectionLocker locker (m_criticalSection);
229 for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
230 if ((*iTask)->buttonID() == iButtonID) {
240 BackgroundManager::resizeWindow()
242 int iHighestPosition = -1;
244 wxCriticalSectionLocker lock (m_criticalSection);
245 for (TaskContainer::iterator i = m_vecpTasks.begin(); i != m_vecpTasks.end(); i++)
246 if (iHighestPosition < (*i)->position())
247 iHighestPosition = (*i)->position();
249 wxSize sizeWindow (m_sizeCell.x, m_sizeCell.y * (iHighestPosition + 1));
250 SetClientSize (sizeWindow);
251 m_pCanvas->Refresh();
256 IMPLEMENT_DYNAMIC_CLASS(BackgroundManagerCanvas, wxPanel)
257 BEGIN_EVENT_TABLE(BackgroundManagerCanvas, wxPanel)
260 BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
261 : wxPanel (pMgr), m_pBackgroundManager(pMgr)
266 #endif // HAVE_WXTHREADS