r575: no message
[ctsim.git] / src / backgroundmgr.cpp
index 12d337df3ff5ea52982b002b4e12aeaae685b220..e915624bf7fe00db1bb17b3b7312a168a74170ce 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2001 Kevin Rosenberg
 **
-**  $Id: backgroundmgr.cpp,v 1.1 2001/02/22 15:00:20 kevin Exp $
+**  $Id: backgroundmgr.cpp,v 1.3 2001/02/23 02:06:02 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -50,15 +50,18 @@ EVT_CLOSE(BackgroundManager::OnCloseWindow)
 END_EVENT_TABLE()
 
 BackgroundManager::BackgroundManager ()
-  : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(200, 100)) //, wxTHICK_FRAME)
+  : wxMiniFrame (theApp->getMainFrame(), -1, _T("Background Tasks"), wxPoint(0,0), wxSize(210, 50)) //, wxTHICK_FRAME)
 {
   m_iNumTasks = 0;
   m_pCanvas = new BackgroundManagerCanvas (this);
   theApp->setIconForFrame (this);
 
-  Show(true);
- // if (m_iNumTasks == 0)
- //   Show(false);
+  m_sizeGauge.Set (50, 15);
+  m_sizeLabel.Set (140, 15);
+  m_sizeCell.Set (200, 25);
+  m_sizeBorder.Set (4, 4);
+
+  Show(false);
 }
 
 
@@ -73,14 +76,42 @@ BackgroundManager::OnCloseWindow (wxCloseEvent& event)
 
 
 wxGauge* 
-BackgroundManager::addTask (BackgroundTask* pTask, int iNumUnits)
+BackgroundManager::addTask (BackgroundTask* pTask, int iNumUnits, const char* const pszTaskName)
 {
-  wxSize size (50, 10);
-  wxPoint pos (4, 5);
+  wxCriticalSectionLocker locker (m_criticalSection);
+  int iNumTasks = m_vecpBackgroundTasks.size();
+  std::vector<bool> vecPositionUsed (iNumTasks);
+  int i;
+  for (i = 0; i < iNumTasks; i++)
+    vecPositionUsed[i] = false;
+
+  for (i = 0; i < iNumTasks; i++) {
+    int iPosUsed = m_vecpPositions[i];
+    if (iPosUsed < iNumTasks)
+      vecPositionUsed[iPosUsed] = true;
+  }
+
+  int iFirstUnusedPos = iNumTasks;  // default is just past current number of tasks
+  for (i = 0; i < iNumTasks; i++)
+    if (! vecPositionUsed[i]) {
+      iFirstUnusedPos = i;
+      break;
+    }
+
+  wxPoint posGauge (m_sizeBorder.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
+  wxPoint posLabel (m_sizeBorder.x + m_sizeGauge.x, m_sizeBorder.y + iFirstUnusedPos * m_sizeCell.y);
+  wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, posGauge, m_sizeGauge);
+  wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, pszTaskName, posLabel, m_sizeLabel);
 
-  wxGauge* pGauge = new wxGauge (m_pCanvas, -1, iNumUnits, pos, size);
   m_vecpBackgroundTasks.push_back (pTask);
+  m_vecpGauges.push_back (pGauge);
+  m_vecpNames.push_back (new std::string (pszTaskName));
+  m_vecpPositions.push_back (iFirstUnusedPos);
+  m_vecpLabels.push_back (pLabel);
   m_iNumTasks++;
+
+  resizeWindow();
+  Show(true);
   return (pGauge);
 }
 
@@ -88,9 +119,52 @@ BackgroundManager::addTask (BackgroundTask* pTask, int iNumUnits)
 void 
 BackgroundManager::taskDone (BackgroundTask* pTask)
 {
-  m_iNumTasks--;
+  wxCriticalSectionLocker locker (m_criticalSection);
+
+  StringContainer::iterator iName = m_vecpNames.begin();
+  GaugeContainer::iterator iGauge = m_vecpGauges.begin();
+  PositionContainer::iterator iPosition = m_vecpPositions.begin();
+  LabelContainer::iterator iLabel = m_vecpLabels.begin();
+  for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
+    if (*iTask == pTask) {
+      delete *iName;
+      delete *iGauge;
+      delete *iLabel;
+      m_vecpBackgroundTasks.erase (iTask);
+      m_vecpGauges.erase (iGauge);
+      m_vecpNames.erase (iName);
+      m_vecpPositions.erase (iPosition);
+      m_vecpLabels.erase (iLabel);
+      m_iNumTasks--;
+      break;
+    }
+    iName++;
+    iGauge++;
+    iPosition++;
+    iLabel++;
+  }
+
+  resizeWindow();
+  if (m_iNumTasks <= 0)
+    Show(false);
+ // delete pTask;
 }
 
+void
+BackgroundManager::resizeWindow()
+{
+  int iHighestPosition = -1;
+
+  for (int i = 0; i < m_vecpPositions.size(); i++)
+    if (iHighestPosition < m_vecpPositions[i])
+      iHighestPosition = m_vecpPositions[i];
+
+  wxSize sizeWindow (m_sizeCell.x, m_sizeCell.y * (iHighestPosition + 1));
+  SetClientSize (sizeWindow);
+  m_pCanvas->Refresh();
+}
+
+
 bool
 BackgroundManager::isCancelling (BackgroundTask* pTask)
 {