r576: 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.4 2001/02/23 18:56:56 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_CLOSE(BackgroundManager::OnCloseWindow)
50 END_EVENT_TABLE()
51
52 BackgroundManager::BackgroundManager ()
53   : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50)) //, wxTHICK_FRAME)
54 {
55   m_iNumTasks = 0;
56   m_pCanvas = new BackgroundManagerCanvas (this);
57   theApp->setIconForFrame (this);
58
59   m_sizeGauge.Set (50, 15);
60   m_sizeLabel.Set (140, 15);
61   m_sizeCell.Set (200, 25);
62   m_sizeBorder.Set (4, 4);
63
64   Show(false);
65 }
66
67
68 void
69 BackgroundManager::OnCloseWindow (wxCloseEvent& event)
70 {
71   if (theApp->getMainFrame()->getShuttingDown())
72     wxMiniFrame::OnCloseWindow (event);
73   else
74     event.Veto();
75 }
76
77
78 wxGauge* 
79 BackgroundManager::addTask (BackgroundSupervisor* pTask, int iNumUnits, const char* const pszTaskName)
80 {
81   wxCriticalSectionLocker locker (m_criticalSection);
82   int iNumTasks = m_vecpBackgroundTasks.size();
83   std::vector<bool> vecPositionUsed (iNumTasks);
84   int i;
85   for (i = 0; i < iNumTasks; i++)
86     vecPositionUsed[i] = false;
87
88   for (i = 0; i < iNumTasks; i++) {
89     int iPosUsed = m_vecpPositions[i];
90     if (iPosUsed < iNumTasks)
91       vecPositionUsed[iPosUsed] = true;
92   }
93
94   int iFirstUnusedPos = iNumTasks;  // default is just past current number of tasks
95   for (i = 0; i < iNumTasks; i++)
96     if (! vecPositionUsed[i]) {
97       iFirstUnusedPos = i;
98       break;
99     }
100
101   wxPoint posGauge (m_sizeBorder.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
102   wxPoint posLabel (m_sizeBorder.x + m_sizeGauge.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
103   wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, posGauge, m_sizeGauge);
104   wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, pszTaskName, posLabel, m_sizeLabel);
105
106   m_vecpBackgroundTasks.push_back (pTask);
107   m_vecpGauges.push_back (pGauge);
108   m_vecpNames.push_back (new std::string (pszTaskName));
109   m_vecpPositions.push_back (iFirstUnusedPos);
110   m_vecpLabels.push_back (pLabel);
111   m_iNumTasks++;
112
113   resizeWindow();
114   Show(true);
115   return (pGauge);
116 }
117
118
119 void 
120 BackgroundManager::taskDone (BackgroundSupervisor* pTask)
121 {
122   wxCriticalSectionLocker locker (m_criticalSection);
123
124   StringContainer::iterator iName = m_vecpNames.begin();
125   GaugeContainer::iterator iGauge = m_vecpGauges.begin();
126   PositionContainer::iterator iPosition = m_vecpPositions.begin();
127   LabelContainer::iterator iLabel = m_vecpLabels.begin();
128   for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
129     if (*iTask == pTask) {
130       delete *iName;
131       delete *iGauge;
132       delete *iLabel;
133       m_vecpBackgroundTasks.erase (iTask);
134       m_vecpGauges.erase (iGauge);
135       m_vecpNames.erase (iName);
136       m_vecpPositions.erase (iPosition);
137       m_vecpLabels.erase (iLabel);
138       m_iNumTasks--;
139       break;
140     }
141     iName++;
142     iGauge++;
143     iPosition++;
144     iLabel++;
145   }
146
147   resizeWindow();
148   if (m_iNumTasks <= 0)
149     Show(false);
150  // delete pTask;
151 }
152
153 void
154 BackgroundManager::resizeWindow()
155 {
156   int iHighestPosition = -1;
157
158   for (int i = 0; i < m_vecpPositions.size(); i++)
159     if (iHighestPosition < m_vecpPositions[i])
160       iHighestPosition = m_vecpPositions[i];
161
162   wxSize sizeWindow (m_sizeCell.x, m_sizeCell.y * (iHighestPosition + 1));
163   SetClientSize (sizeWindow);
164   m_pCanvas->Refresh();
165 }
166
167
168 bool
169 BackgroundManager::isCancelling (BackgroundSupervisor* pTask)
170 {
171   return false;
172 }
173
174
175
176 IMPLEMENT_DYNAMIC_CLASS(BackgroundManagerCanvas, wxPanel)
177 BEGIN_EVENT_TABLE(BackgroundManagerCanvas, wxPanel)
178 EVT_PAINT(BackgroundManagerCanvas::OnPaint)
179 END_EVENT_TABLE()
180
181 BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
182 : m_pBackgroundManager(pMgr), wxPanel (pMgr)
183 {
184 }
185
186
187 void
188 BackgroundManagerCanvas::OnPaint (wxPaintEvent& event)
189 {
190   wxPaintDC dc (this);
191 //  dc.DrawLine (0, 0, 30, 30);
192 //  dc.DrawLine (30,0, 0, 30);
193 }