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