r574: no message
[ctsim.git] / src / backgroundmgr.cpp
index 32c32a6e0f6ce7f6f9da7ac4a76fab1f83e07ab8..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.2 2001/02/22 18:22:40 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,13 +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(false);
+  m_sizeGauge.Set (50, 15);
+  m_sizeLabel.Set (140, 15);
+  m_sizeCell.Set (200, 25);
+  m_sizeBorder.Set (4, 4);
+
+  Show(false);
 }
 
 
@@ -75,16 +80,37 @@ BackgroundManager::addTask (BackgroundTask* pTask, int iNumUnits, const char* co
 {
   wxCriticalSectionLocker locker (m_criticalSection);
   int iNumTasks = m_vecpBackgroundTasks.size();
-  int iTaskHeight = 20;
-  wxSize size (50, 10);
-  wxPoint pos (4, 5 + iNumTasks * iTaskHeight);
+  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);
 }
@@ -93,32 +119,52 @@ BackgroundManager::addTask (BackgroundTask* pTask, int iNumUnits, const char* co
 void 
 BackgroundManager::taskDone (BackgroundTask* pTask)
 {
-  wxCriticalSection doneSection;
-  doneSection.Enter();
+  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;
     }
-    iTask++;
+    iName++;
     iGauge++;
+    iPosition++;
+    iLabel++;
   }
 
-  doneSection.Leave();
+  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();
-  // delete pTask;
 }
 
+
 bool
 BackgroundManager::isCancelling (BackgroundTask* pTask)
 {