r609: *** empty log 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.12 2001/03/05 20:29:23 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
42 #ifdef HAVE_WXTHREADS
43
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, 1000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton)
52 END_EVENT_TABLE()
53
54 BackgroundManager::BackgroundManager ()
55   : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50)) //, wxTHICK_FRAME)
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 void
75 BackgroundManager::OnCloseWindow (wxCloseEvent& event)
76 {
77   if (theApp->getMainFrame()->getShuttingDown())
78     wxMiniFrame::OnCloseWindow (event);
79   else
80     event.Veto();
81 }
82
83 void
84 BackgroundManager::OnUnitTick (wxCommandEvent& event)
85 {
86   int iUnits = event.GetInt();
87   BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
88   if (pTask == NULL) {
89     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
90     return;
91   }
92   if (wxGauge* pGauge = lookupGauge (pTask))
93     pGauge->SetValue (iUnits);
94 }
95
96 void
97 BackgroundManager::OnAddTask (wxCommandEvent& event)
98 {
99   int iNumUnits = event.GetInt();
100   const char* const pszTaskName = event.GetString().c_str();
101   BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
102   if (pTask == NULL) {
103     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
104     return;
105   }
106
107   wxCriticalSectionLocker locker (m_criticalSection);
108   int iNumTasks = m_vecpBackgroundTasks.size();
109   std::vector<bool> vecPositionUsed (iNumTasks);
110   int i;
111   for (i = 0; i < iNumTasks; i++)
112     vecPositionUsed[i] = false;
113
114   for (i = 0; i < iNumTasks; i++) {
115     int iPosUsed = m_vecpPositions[i];
116     if (iPosUsed < iNumTasks)
117       vecPositionUsed[iPosUsed] = true;
118   }
119
120   int iFirstUnusedPos = iNumTasks;  // default is just past current number of tasks
121   for (i = 0; i < iNumTasks; i++)
122     if (! vecPositionUsed[i]) {
123       iFirstUnusedPos = i;
124       break;
125     }
126
127   wxPoint posGauge (m_sizeBorder.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
128   wxPoint posLabel (m_sizeBorder.x + m_sizeGauge.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
129   wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, posGauge, m_sizeGauge);
130   wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, pszTaskName, posLabel, m_sizeLabel);
131   //  wxPoint posButton (m_sizeBorder.x + m_sizeGauge.x + m_sizeLabel.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
132 //  wxButton* pCancelButton = new wxButton (m_pCanvas, iFirstUnusedPos, _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT);
133
134   m_vecpBackgroundTasks.push_back (pTask);
135   m_vecpGauges.push_back (pGauge);
136   m_vecpNames.push_back (new std::string (pszTaskName));
137   m_vecpPositions.push_back (iFirstUnusedPos);
138   m_vecpLabels.push_back (pLabel);
139   //m_vecpCancelButtons.push_back (pCancelButton);
140   m_iNumTasks++;
141
142   resizeWindow();
143   if (m_iNumTasks == 1) {
144     Show(true);  
145     theApp->getMainFrame()->SetFocus();  // necessary to keep wxWindows from crashing
146   }
147 }
148
149 void
150 BackgroundManager::OnRemoveTask (wxCommandEvent& event)
151 {
152   BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
153   if (pTask == NULL) {
154     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
155     return;
156   }
157
158   wxCriticalSectionLocker locker (m_criticalSection);
159
160   StringContainer::iterator iName = m_vecpNames.begin();
161   GaugeContainer::iterator iGauge = m_vecpGauges.begin();
162   PositionContainer::iterator iPosition = m_vecpPositions.begin();
163   LabelContainer::iterator iLabel = m_vecpLabels.begin();
164   //ButtonContainer::iterator iCancelButton = m_vecpCancelButtons.begin();
165   for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
166     if (*iTask == pTask) {
167       delete *iName;
168       delete *iGauge;
169       delete *iLabel;
170       //delete *iCancelButton;
171       m_vecpBackgroundTasks.erase (iTask);
172       m_vecpGauges.erase (iGauge);
173       m_vecpNames.erase (iName);
174       m_vecpPositions.erase (iPosition);
175       m_vecpLabels.erase (iLabel);
176       //m_vecpCancelButtons.erase (iCancelButton);
177       m_iNumTasks--;
178       break;
179     }
180     iName++;
181     iGauge++;
182     iPosition++;
183     iLabel++;
184     //iCancelButton++;
185   }
186
187   resizeWindow();
188   if (m_iNumTasks <= 0)
189     Show(false);
190 }
191
192 void
193 BackgroundManager::OnCancelButton (wxCommandEvent& event)
194 {
195 }
196
197 wxGauge*
198 BackgroundManager::lookupGauge (BackgroundSupervisor* pTask)
199 {
200   wxGauge* pGauge = NULL;
201   int i = 0;
202
203   wxCriticalSectionLocker locker (m_criticalSection);
204   for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
205     if (*iTask == pTask) {
206       pGauge = m_vecpGauges[i];
207       break;
208     }
209     i++;
210   }
211
212   return pGauge;
213 }
214
215 void
216 BackgroundManager::resizeWindow()
217 {
218   int iHighestPosition = -1;
219
220   for (unsigned int i = 0; i < m_vecpPositions.size(); i++)
221     if (iHighestPosition < m_vecpPositions[i])
222       iHighestPosition = m_vecpPositions[i];
223
224   wxSize sizeWindow (m_sizeCell.x, m_sizeCell.y * (iHighestPosition + 1));
225   SetClientSize (sizeWindow);
226   m_pCanvas->Refresh();
227 }
228
229
230
231 IMPLEMENT_DYNAMIC_CLASS(BackgroundManagerCanvas, wxPanel)
232 BEGIN_EVENT_TABLE(BackgroundManagerCanvas, wxPanel)
233 //EVT_PAINT(BackgroundManagerCanvas::OnPaint)
234 END_EVENT_TABLE()
235
236 BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
237 : wxPanel (pMgr), m_pBackgroundManager(pMgr)
238 {
239 }
240
241 #if 0
242 void
243 BackgroundManagerCanvas::OnPaint (wxPaintEvent& event)
244 {
245   wxPaintDC dc (this);
246 //  dc.DrawLine (0, 0, 30, 30);
247 //  dc.DrawLine (30,0, 0, 30);
248 }
249 #endif
250
251 #endif // HAVE_WXTHREADS