r623: 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.14 2001/03/09 18:50:46 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
44 IMPLEMENT_DYNAMIC_CLASS(BackgroundManager, wxMiniFrame)
45 BEGIN_EVENT_TABLE(BackgroundManager, wxMiniFrame)
46 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundManager::OnAddTask)
47 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, BackgroundManager::OnRemoveTask)
48 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_UNIT_TICK, BackgroundManager::OnUnitTick)
49 EVT_CLOSE(BackgroundManager::OnCloseWindow)
50 EVT_COMMAND_RANGE(0, 1000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton)
51 END_EVENT_TABLE()
52
53
54 BackgroundManager::BackgroundManager ()
55   : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50))
56 {
57   m_iNumTasks = 0;
58   m_pCanvas = new BackgroundManagerCanvas (this);
59   theApp->setIconForFrame (this);
60
61   m_sizeGauge.Set (70, 20);
62   m_sizeLabel.Set (140, 20);
63   m_sizeBorder.Set (4, 4);
64   m_sizeCellSpacing.Set (3, 3);
65   m_sizeButton.Set (70, 20);
66   m_sizeButton.Set (0, 0);
67
68   m_sizeCell.Set (m_sizeGauge.x + m_sizeLabel.x + m_sizeCellSpacing.x + m_sizeButton.x, 25);
69
70   Show(false);
71 }
72
73
74 BackgroundManager::~BackgroundManager()
75 {
76 }
77
78 void
79 BackgroundManager::OnCloseWindow (wxCloseEvent& event)
80 {
81   if (theApp->getMainFrame()->getShuttingDown())
82     wxMiniFrame::OnCloseWindow (event);
83   else
84     event.Veto();
85 }
86
87 void
88 BackgroundManager::OnUnitTick (wxCommandEvent& event)
89 {
90   int iUnits = event.GetInt();
91   
92   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
93   if (pSupervisor == NULL) {
94     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnUnitTick]");
95     return;
96   }
97
98   BackgroundManagerTask* pTask = lookupTask (pSupervisor);
99   if (pTask == NULL) {
100           sys_error (ERR_SEVERE, "Error looking up task [BackgroundManager::OnUnitTick]");
101           return;
102   }
103   pTask->gauge()->SetValue (iUnits);
104 }
105
106 void
107 BackgroundManager::OnAddTask (wxCommandEvent& event)
108 {
109   int iNumUnits = event.GetInt();
110   const char* const pszTaskName = event.GetString().c_str();
111   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
112   if (pSupervisor == NULL) {
113     sys_error (ERR_SEVERE, "Received NULL supervisor [BackgroundManager::OnAddTask]");
114     return;
115   }
116
117   wxCriticalSectionLocker locker (m_criticalSection);
118
119   int iNumTasks = m_vecpTasks.size();
120   std::vector<bool> vecPositionUsed (iNumTasks);  //vector of used table positions 
121   for (int iP = 0; iP < iNumTasks; iP++)
122     vecPositionUsed[iP] = false;
123
124   for (TaskContainer::iterator iT = m_vecpTasks.begin(); iT != m_vecpTasks.end(); iT++) {
125     int iPosUsed = (*iT)->position();
126     if (iPosUsed < iNumTasks)
127       vecPositionUsed[iPosUsed] = true;
128   }
129
130   int iFirstUnusedPos = iNumTasks;  // default is just past current number of tasks
131   for (int i = 0; i < iNumTasks; i++)
132     if (! vecPositionUsed[i]) {
133       iFirstUnusedPos = i;
134       break;
135     }
136   
137   wxPoint posGauge (m_sizeBorder.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
138   wxPoint posLabel (m_sizeBorder.x + m_sizeGauge.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
139   wxPoint posButton (m_sizeBorder.x + m_sizeGauge.x + m_sizeLabel.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
140   wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, posGauge, m_sizeGauge);
141   wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, pszTaskName, posLabel, m_sizeLabel);
142   wxButton* pButton = NULL; // new wxButton (m_pCanvas, reinterpret_cast<wxWindowID>(pSupervisor), _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT);
143
144   BackgroundManagerTask* pTask = new BackgroundManagerTask (pSupervisor, pszTaskName, iFirstUnusedPos, pGauge, pLabel, pButton);
145
146   m_vecpTasks.push_back (pTask);
147   m_iNumTasks++;
148
149   resizeWindow();
150   if (m_iNumTasks == 1) {
151         SetFocus();
152     Show(true);  
153     theApp->getMainFrame()->SetFocus();  // necessary to keep wxWindows from crashing
154   }
155 }
156
157 void
158 BackgroundManager::OnRemoveTask (wxCommandEvent& event)
159 {
160   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
161   if (pSupervisor == NULL) {
162     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnRemoveTask]");
163     return;
164   }
165
166   wxCriticalSectionLocker locker (m_criticalSection);
167
168   bool bFound = false;
169   for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
170     if ((*iTask)->supervisor() == pSupervisor) {
171           delete (*iTask)->gauge();
172           delete (*iTask)->label();
173           delete (*iTask)->button();
174       delete *iTask;
175       m_vecpTasks.erase (iTask);
176       m_iNumTasks--;
177           bFound = true;
178       break;
179     }
180   }
181   if (! bFound) 
182           sys_error (ERR_SEVERE, "Unable to find supervisor [BackgroundManager::OnRemoveTask]");
183
184   resizeWindow();
185   if (m_iNumTasks <= 0)
186     Show(false);
187 }
188
189 void
190 BackgroundManager::OnCancelButton (wxCommandEvent& event)
191 {
192 }
193
194 BackgroundManagerTask*
195 BackgroundManager::lookupTask (BackgroundSupervisor* pSupervisor)
196 {
197   BackgroundManagerTask* pTask = NULL;
198
199   wxCriticalSectionLocker locker (m_criticalSection);
200   for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
201     if ((*iTask)->supervisor() == pSupervisor) {
202       pTask = *iTask;
203       break;
204     }
205   }
206
207   return pTask;
208 }
209
210 void
211 BackgroundManager::resizeWindow()
212 {
213   int iHighestPosition = -1;
214
215   wxCriticalSectionLocker lock (m_criticalSection);
216   for (TaskContainer::iterator i = m_vecpTasks.begin(); i != m_vecpTasks.end(); i++)
217     if (iHighestPosition < (*i)->position())
218       iHighestPosition = (*i)->position();
219
220   wxSize sizeWindow (m_sizeCell.x, m_sizeCell.y * (iHighestPosition + 1));
221   SetClientSize (sizeWindow);
222   m_pCanvas->Refresh();
223 }
224
225
226
227 IMPLEMENT_DYNAMIC_CLASS(BackgroundManagerCanvas, wxPanel)
228 BEGIN_EVENT_TABLE(BackgroundManagerCanvas, wxPanel)
229 END_EVENT_TABLE()
230
231 BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
232 : wxPanel (pMgr), m_pBackgroundManager(pMgr)
233 {
234 }
235
236
237 #endif // HAVE_WXTHREADS