r582: no message
[ctsim.git] / src / backgroundmgr.cpp
index 12d337df3ff5ea52982b002b4e12aeaae685b220..5e1987990bac1cd8ae120806e34e80c573367a26 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.8 2001/02/25 10:52:55 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
@@ -35,7 +35,7 @@
 #include "ctsim.h"
 #include "docs.h"
 #include "views.h"
-#include "threadrecon.h"
+#include "backgroundsupr.h"
 #include "backgroundmgr.h"
 
 
 
 IMPLEMENT_DYNAMIC_CLASS(BackgroundManager, wxMiniFrame)
 BEGIN_EVENT_TABLE(BackgroundManager, wxMiniFrame)
+EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundManager::OnAddTask)
+EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, BackgroundManager::OnRemoveTask)
+EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_UNIT_TICK, BackgroundManager::OnUnitTick)
 EVT_CLOSE(BackgroundManager::OnCloseWindow)
+EVT_COMMAND_RANGE(0, 1000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton)
 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 (70, 20);
+  m_sizeLabel.Set (140, 20);
+  m_sizeBorder.Set (4, 4);
+  m_sizeCellSpacing.Set (3, 3);
+  //m_sizeButton.Set (70, 20);
+  m_sizeButton.Set (0, 0);
+
+  m_sizeCell.Set (m_sizeGauge.x + m_sizeLabel.x + m_sizeCellSpacing.x + m_sizeButton.x, 25);
+
+  Show(false);
 }
 
 
@@ -71,37 +82,157 @@ BackgroundManager::OnCloseWindow (wxCloseEvent& event)
     event.Veto();
 }
 
+void
+BackgroundManager::OnUnitTick (wxCommandEvent& event)
+{
+  int iUnits = event.GetInt();
+  BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
+  if (pTask == NULL) {
+    sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
+    return;
+  }
+  if (wxGauge* pGauge = lookupGauge (pTask))
+    pGauge->SetValue (iUnits);
+}
 
-wxGauge* 
-BackgroundManager::addTask (BackgroundTask* pTask, int iNumUnits)
+void
+BackgroundManager::OnAddTask (wxCommandEvent& event)
 {
-  wxSize size (50, 10);
-  wxPoint pos (4, 5);
+  int iNumUnits = event.GetInt();
+  const char* const pszTaskName = event.GetString().c_str();
+  BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
+  if (pTask == NULL) {
+    sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
+    return;
+  }
+
+  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);
+  wxPoint posButton (m_sizeBorder.x + m_sizeGauge.x + m_sizeLabel.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);
+//  wxButton* pCancelButton = new wxButton (m_pCanvas, iFirstUnusedPos, _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT);
 
-  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_vecpCancelButtons.push_back (pCancelButton);
   m_iNumTasks++;
-  return (pGauge);
+
+  resizeWindow();
+  if (m_iNumTasks == 1) {
+    Show(true);  
+    theApp->getMainFrame()->SetFocus();  // necessary to keep wxWindows from crashing
+  }
+}
+
+void
+BackgroundManager::OnRemoveTask (wxCommandEvent& event)
+{
+  BackgroundSupervisor* pTask = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
+  if (pTask == NULL) {
+    sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnAddTask]");
+    return;
+  }
+
+  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();
+  //ButtonContainer::iterator iCancelButton = m_vecpCancelButtons.begin();
+  for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
+    if (*iTask == pTask) {
+      delete *iName;
+      delete *iGauge;
+      delete *iLabel;
+      //delete *iCancelButton;
+      m_vecpBackgroundTasks.erase (iTask);
+      m_vecpGauges.erase (iGauge);
+      m_vecpNames.erase (iName);
+      m_vecpPositions.erase (iPosition);
+      m_vecpLabels.erase (iLabel);
+      //m_vecpCancelButtons.erase (iCancelButton);
+      m_iNumTasks--;
+      break;
+    }
+    iName++;
+    iGauge++;
+    iPosition++;
+    iLabel++;
+    //iCancelButton++;
+  }
+
+  resizeWindow();
+  if (m_iNumTasks <= 0)
+    Show(false);
 }
 
+void
+BackgroundManager::OnCancelButton (wxCommandEvent& event)
+{
+}
 
-void 
-BackgroundManager::taskDone (BackgroundTask* pTask)
+wxGauge*
+BackgroundManager::lookupGauge (BackgroundSupervisor* pTask)
 {
-  m_iNumTasks--;
+  wxGauge* pGauge = NULL;
+  int i = 0;
+
+  wxCriticalSectionLocker locker (m_criticalSection);
+  for (TaskContainer::iterator iTask = m_vecpBackgroundTasks.begin(); iTask != m_vecpBackgroundTasks.end(); iTask++) {
+    if (*iTask == pTask) {
+      pGauge = m_vecpGauges[i];
+      break;
+    }
+    i++;
+  }
+
+  return pGauge;
 }
 
-bool
-BackgroundManager::isCancelling (BackgroundTask* pTask)
+void
+BackgroundManager::resizeWindow()
 {
-  return false;
+  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();
 }
 
 
 
 IMPLEMENT_DYNAMIC_CLASS(BackgroundManagerCanvas, wxPanel)
 BEGIN_EVENT_TABLE(BackgroundManagerCanvas, wxPanel)
-EVT_PAINT(BackgroundManagerCanvas::OnPaint)
+//EVT_PAINT(BackgroundManagerCanvas::OnPaint)
 END_EVENT_TABLE()
 
 BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
@@ -109,7 +240,7 @@ BackgroundManagerCanvas::BackgroundManagerCanvas (BackgroundManager* pMgr)
 {
 }
 
-
+#if 0
 void
 BackgroundManagerCanvas::OnPaint (wxPaintEvent& event)
 {
@@ -117,3 +248,4 @@ BackgroundManagerCanvas::OnPaint (wxPaintEvent& event)
 //  dc.DrawLine (0, 0, 30, 30);
 //  dc.DrawLine (30,0, 0, 30);
 }
+#endif
\ No newline at end of file