r628: no message
[ctsim.git] / src / backgroundmgr.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          backgroundmgr.cpp
5 **   Purpose:       Background manager class
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  February 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: backgroundmgr.cpp,v 1.16 2001/03/11 06:34:37 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "wx/wxprec.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/wx.h"
32 #endif
33
34 #include "ct.h"
35 #include "ctsim.h"
36 #include "docs.h"
37 #include "views.h"
38 #include "backgroundsupr.h"
39 #include "backgroundmgr.h"
40
41 #ifdef HAVE_WXTHREADS
42
43 int BackgroundManager::s_iNextButtonID = 0;
44
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)
52 END_EVENT_TABLE()
53
54
55 BackgroundManager::BackgroundManager ()
56   : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50))
57 {
58   m_iNumTasks = 0;
59   m_pCanvas = new BackgroundManagerCanvas (this);
60   theApp->setIconForFrame (this);
61
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);
67
68   m_sizeCell.Set (m_sizeGauge.x + m_sizeLabel.x + m_sizeCellSpacing.x + m_sizeButton.x, 25);
69
70   theApp->getMainFrame()->SetFocus();
71   Show(false);
72 }
73
74
75 BackgroundManager::~BackgroundManager()
76 {
77 }
78
79 void
80 BackgroundManager::OnCloseWindow (wxCloseEvent& event)
81 {
82   if (theApp->getMainFrame()->getShuttingDown())
83     wxMiniFrame::OnCloseWindow (event);
84   else
85     event.Veto();
86 }
87
88 void
89 BackgroundManager::OnUnitTick (wxCommandEvent& event)
90 {
91   int iUnits = event.GetInt();
92   
93   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
94   if (pSupervisor == NULL) {
95     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnUnitTick]");
96     return;
97   }
98
99   BackgroundManagerTask* pTask = lookupTask (pSupervisor);
100   if (pTask == NULL) {
101           sys_error (ERR_SEVERE, "Error looking up task [BackgroundManager::OnUnitTick]");
102           return;
103   }
104   pTask->gauge()->SetValue (iUnits);
105 }
106
107 void
108 BackgroundManager::OnAddTask (wxCommandEvent& event)
109 {
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]");
115     return;
116   }
117
118   wxCriticalSectionLocker locker (m_criticalSection);
119
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;
124
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;
129   }
130
131   int iFirstUnusedPos = iNumTasks;  // default is just past current number of tasks
132   for (int i = 0; i < iNumTasks; i++)
133     if (! vecPositionUsed[i]) {
134       iFirstUnusedPos = i;
135       break;
136     }
137   
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);
144
145   BackgroundManagerTask* pTask = new BackgroundManagerTask (pSupervisor, pszTaskName, 
146     iFirstUnusedPos, pGauge, pLabel, pButton, s_iNextButtonID);
147
148   m_vecpTasks.push_back (pTask);
149   m_iNumTasks++;
150   s_iNextButtonID++;
151
152   resizeWindow();
153   if (m_iNumTasks == 1) {
154         theApp->getMainFrame()->SetFocus();
155     Show(true);  
156         theApp->getMainFrame()->SetFocus();
157   }
158 }
159
160 void
161 BackgroundManager::OnRemoveTask (wxCommandEvent& event)
162 {
163   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
164   if (pSupervisor == NULL) {
165     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnRemoveTask]");
166     return;
167   }
168
169   wxCriticalSectionLocker locker (m_criticalSection);
170
171   bool bFound = false;
172   for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
173     if ((*iTask)->supervisor() == pSupervisor) {
174           delete (*iTask)->gauge();
175             delete (*iTask)->label();
176             delete (*iTask)->button();
177       delete *iTask;
178       m_vecpTasks.erase (iTask);
179       m_iNumTasks--;
180             bFound = true;
181       break;
182     }
183   }
184   if (! bFound)  {
185           sys_error (ERR_SEVERE, "Unable to find supervisor [BackgroundManager::OnRemoveTask]");
186     return;
187   }
188   pSupervisor->ackRemoveBackgroundManager();
189   resizeWindow();
190   if (m_iNumTasks <= 0) {
191     theApp->getMainFrame()->SetFocus();
192     Show(false);
193     theApp->getMainFrame()->SetFocus();
194   }
195 }
196
197 void
198 BackgroundManager::OnCancelButton (wxCommandEvent& event)
199 {
200   BackgroundManagerTask* pTask = lookupTask (event.GetId());
201   if (! pTask) {
202     sys_error (ERR_SEVERE, "Unable to lookup task for button");
203     return;
204   }
205
206   pTask->supervisor()->onCancel();
207 }
208
209 BackgroundManagerTask*
210 BackgroundManager::lookupTask (BackgroundSupervisor* pSupervisor)
211 {
212   BackgroundManagerTask* pTask = NULL;
213
214   wxCriticalSectionLocker locker (m_criticalSection);
215   for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
216     if ((*iTask)->supervisor() == pSupervisor) {
217       pTask = *iTask;
218       break;
219     }
220   }
221
222   return pTask;
223 }
224
225 BackgroundManagerTask*
226 BackgroundManager::lookupTask (int iButtonID)
227 {
228   BackgroundManagerTask* pTask = NULL;
229
230   wxCriticalSectionLocker locker (m_criticalSection);
231   for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
232     if ((*iTask)->buttonID() == iButtonID) {
233       pTask = *iTask;
234       break;
235     }
236   }
237
238   return pTask;
239 }
240
241 void
242 BackgroundManager::resizeWindow()
243 {
244   int iHighestPosition = -1;
245
246   wxCriticalSectionLocker lock (m_criticalSection);
247   for (TaskContainer::iterator i = m_vecpTasks.begin(); i != m_vecpTasks.end(); i++)
248     if (iHighestPosition < (*i)->position())
249       iHighestPosition = (*i)->position();
250
251   wxSize sizeWindow (m_sizeCell.x, m_sizeCell.y * (iHighestPosition + 1));
252   SetClientSize (sizeWindow);
253   m_pCanvas->Refresh();
254 }
255
256
257
258 IMPLEMENT_DYNAMIC_CLASS(BackgroundManagerCanvas, wxPanel)
259 BEGIN_EVENT_TABLE(BackgroundManagerCanvas, wxPanel)
260 END_EVENT_TABLE()
261
262 BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
263 : wxPanel (pMgr), m_pBackgroundManager(pMgr)
264 {
265 }
266
267
268 #endif // HAVE_WXTHREADS