r577: 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.5 2001/02/23 21:58:31 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 "threadrecon.h"
39 #include "backgroundmgr.h"
40
41
42 #if defined(HAVE_CONFIG_H)
43 #include "config.h"
44 #endif
45
46
47 IMPLEMENT_DYNAMIC_CLASS(BackgroundManager, wxMiniFrame)
48 BEGIN_EVENT_TABLE(BackgroundManager, wxMiniFrame)
49 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundManager::OnAddTask)
50 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, BackgroundManager::OnRemoveTask)
51 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_UNIT_TICK, BackgroundManager::OnUnitTick)
52 EVT_CLOSE(BackgroundManager::OnCloseWindow)
53 EVT_COMMAND_RANGE(0, 1000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton)
54 END_EVENT_TABLE()
55
56 BackgroundManager::BackgroundManager ()
57   : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50)) //, wxTHICK_FRAME)
58 {
59   m_iNumTasks = 0;
60   m_pCanvas = new BackgroundManagerCanvas (this);
61   theApp->setIconForFrame (this);
62
63   m_sizeGauge.Set (70, 20);
64   m_sizeLabel.Set (140, 20);
65   m_sizeBorder.Set (4, 4);
66   m_sizeCellSpacing.Set (3, 3);
67   m_sizeButton.Set (70, 20);
68
69   m_sizeCell.Set (m_sizeGauge.x + m_sizeLabel.x + m_sizeCellSpacing.x + m_sizeButton.x, 25);
70   
71   Show(false);
72 }
73
74
75 void
76 BackgroundManager::OnCloseWindow (wxCloseEvent& event)
77 {
78   if (theApp->getMainFrame()->getShuttingDown())
79     wxMiniFrame::OnCloseWindow (event);
80   else
81     event.Veto();
82 }
83
84 void
85 BackgroundManager::OnUnitTick (wxCommandEvent& event)
86 {
87   int iUnits = event.GetInt();
88   BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
89   if (pTask == NULL) {
90     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
91     return;
92   }
93   if (wxGauge* pGauge = lookupGauge (pTask))
94     pGauge->SetValue (iUnits);
95 }
96
97 void
98 BackgroundManager::OnAddTask (wxCommandEvent& event)
99 {
100   int iNumUnits = event.GetInt();
101   const char* const pszTaskName = event.GetString().c_str();
102   BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
103   if (pTask == NULL) {
104     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
105     return;
106   }
107
108   wxCriticalSectionLocker locker (m_criticalSection);
109   int iNumTasks = m_vecpBackgroundTasks.size();
110   std::vector<bool> vecPositionUsed (iNumTasks);
111   int i;
112   for (i = 0; i < iNumTasks; i++)
113     vecPositionUsed[i] = false;
114
115   for (i = 0; i < iNumTasks; i++) {
116     int iPosUsed = m_vecpPositions[i];
117     if (iPosUsed < iNumTasks)
118       vecPositionUsed[iPosUsed] = true;
119   }
120
121   int iFirstUnusedPos = iNumTasks;  // default is just past current number of tasks
122   for (i = 0; i < iNumTasks; i++)
123     if (! vecPositionUsed[i]) {
124       iFirstUnusedPos = i;
125       break;
126     }
127
128   wxPoint posGauge (m_sizeBorder.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
129   wxPoint posLabel (m_sizeBorder.x + m_sizeGauge.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
130   wxPoint posButton (m_sizeBorder.x + m_sizeGauge.x + m_sizeLabel.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
131   wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, posGauge, m_sizeGauge);
132   wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, pszTaskName, posLabel, m_sizeLabel);
133   wxButton* pCancelButton = new wxButton (m_pCanvas, iFirstUnusedPos, _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT);
134
135   m_vecpBackgroundTasks.push_back (pTask);
136   m_vecpGauges.push_back (pGauge);
137   m_vecpNames.push_back (new std::string (pszTaskName));
138   m_vecpPositions.push_back (iFirstUnusedPos);
139   m_vecpLabels.push_back (pLabel);
140   m_vecpCancelButtons.push_back (pCancelButton);
141   m_iNumTasks++;
142
143   resizeWindow();
144   Show(true);
145 }
146
147 void
148 BackgroundManager::OnRemoveTask (wxCommandEvent& event)
149 {
150   BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
151   if (pTask == NULL) {
152     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
153     return;
154   }
155
156   wxCriticalSectionLocker locker (m_criticalSection);
157
158   StringContainer::iterator iName = m_vecpNames.begin();
159   GaugeContainer::iterator iGauge = m_vecpGauges.begin();
160   PositionContainer::iterator iPosition = m_vecpPositions.begin();
161   LabelContainer::iterator iLabel = m_vecpLabels.begin();
162   ButtonContainer::iterator iCancelButton = m_vecpCancelButtons.begin();
163   for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
164     if (*iTask == pTask) {
165       delete *iName;
166       delete *iGauge;
167       delete *iLabel;
168       delete *iCancelButton;
169       m_vecpBackgroundTasks.erase (iTask);
170       m_vecpGauges.erase (iGauge);
171       m_vecpNames.erase (iName);
172       m_vecpPositions.erase (iPosition);
173       m_vecpLabels.erase (iLabel);
174       m_vecpCancelButtons.erase (iCancelButton);
175       m_iNumTasks--;
176       break;
177     }
178     iName++;
179     iGauge++;
180     iPosition++;
181     iLabel++;
182     iCancelButton++;
183   }
184
185   resizeWindow();
186   if (m_iNumTasks <= 0)
187     Show(false);
188 }
189
190 void
191 BackgroundManager::OnCancelButton (wxCommandEvent& event)
192 {
193 }
194
195 wxGauge*
196 BackgroundManager::lookupGauge (BackgroundSupervisor* pTask)
197 {
198   wxGauge* pGauge = NULL;
199   int i = 0;
200   for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
201     if (*iTask == pTask) {
202       pGauge = m_vecpGauges[i];
203       break;
204     }
205     i++;
206   }
207
208   return pGauge;
209 }
210
211 void
212 BackgroundManager::resizeWindow()
213 {
214   int iHighestPosition = -1;
215
216   for (int i = 0; i < m_vecpPositions.size(); i++)
217     if (iHighestPosition < m_vecpPositions[i])
218       iHighestPosition = m_vecpPositions[i];
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 EVT_PAINT(BackgroundManagerCanvas::OnPaint)
230 END_EVENT_TABLE()
231
232 BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
233 : m_pBackgroundManager(pMgr), wxPanel (pMgr)
234 {
235 }
236
237
238 void
239 BackgroundManagerCanvas::OnPaint (wxPaintEvent& event)
240 {
241   wxPaintDC dc (this);
242 //  dc.DrawLine (0, 0, 30, 30);
243 //  dc.DrawLine (30,0, 0, 30);
244 }