Applied initial patches for wx2.8 compatibility
[ctsim.git] / src / backgroundmgr.cpp
index ad92028ec3b5675b653c27a8b0ce6d775d010b0e..5ba1b786d08e99104791d1caad4f922c716820a5 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2001 Kevin Rosenberg
 **
-**  $Id: backgroundmgr.cpp,v 1.14 2001/03/09 18:50:46 kevin Exp $
+**  $Id$
 **
 **  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
@@ -40,6 +40,7 @@
 
 #ifdef HAVE_WXTHREADS
 
+int BackgroundManager::s_iNextButtonID = 0;
 
 IMPLEMENT_DYNAMIC_CLASS(BackgroundManager, wxMiniFrame)
 BEGIN_EVENT_TABLE(BackgroundManager, wxMiniFrame)
@@ -47,7 +48,7 @@ EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, BackgroundManager:
 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)
+EVT_COMMAND_RANGE(0, 30000, wxEVT_COMMAND_BUTTON_CLICKED, BackgroundManager::OnCancelButton)
 END_EVENT_TABLE()
 
 
@@ -63,10 +64,10 @@ BackgroundManager::BackgroundManager ()
   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);
 
+  theApp->getMainFrame()->SetFocus();
   Show(false);
 }
 
@@ -88,7 +89,7 @@ void
 BackgroundManager::OnUnitTick (wxCommandEvent& event)
 {
   int iUnits = event.GetInt();
-  
+
   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
   if (pSupervisor == NULL) {
     sys_error (ERR_SEVERE, "Received NULL task [BackgroundManager::OnUnitTick]");
@@ -97,8 +98,8 @@ BackgroundManager::OnUnitTick (wxCommandEvent& event)
 
   BackgroundManagerTask* pTask = lookupTask (pSupervisor);
   if (pTask == NULL) {
-         sys_error (ERR_SEVERE, "Error looking up task [BackgroundManager::OnUnitTick]");
-         return;
+          sys_error (ERR_SEVERE, "Error looking up task [BackgroundManager::OnUnitTick]");
+          return;
   }
   pTask->gauge()->SetValue (iUnits);
 }
@@ -107,7 +108,7 @@ void
 BackgroundManager::OnAddTask (wxCommandEvent& event)
 {
   int iNumUnits = event.GetInt();
-  const char* const pszTaskName = event.GetString().c_str();
+  const char* const pszTaskName = event.GetString().mb_str(wxConvUTF8);
   BackgroundSupervisor* pSupervisor = reinterpret_cast<BackgroundSupervisor*>(event.GetClientData());
   if (pSupervisor == NULL) {
     sys_error (ERR_SEVERE, "Received NULL supervisor [BackgroundManager::OnAddTask]");
@@ -117,7 +118,7 @@ BackgroundManager::OnAddTask (wxCommandEvent& event)
   wxCriticalSectionLocker locker (m_criticalSection);
 
   int iNumTasks = m_vecpTasks.size();
-  std::vector<bool> vecPositionUsed (iNumTasks);  //vector of used table positions 
+  std::vector<bool> vecPositionUsed (iNumTasks);  //vector of used table positions
   for (int iP = 0; iP < iNumTasks; iP++)
     vecPositionUsed[iP] = false;
 
@@ -133,24 +134,25 @@ BackgroundManager::OnAddTask (wxCommandEvent& event)
       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* pButton = NULL; // new wxButton (m_pCanvas, reinterpret_cast<wxWindowID>(pSupervisor), _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT);
+  wxStaticText* pLabel = new wxStaticText (m_pCanvas, -1, wxConvUTF8.cMB2WX(pszTaskName), posLabel, m_sizeLabel);
+  wxButton* pButton = new wxButton (m_pCanvas, s_iNextButtonID, _T("Cancel"), posButton, m_sizeButton, wxBU_LEFT);
 
-  BackgroundManagerTask* pTask = new BackgroundManagerTask (pSupervisor, pszTaskName, iFirstUnusedPos, pGauge, pLabel, pButton);
+  BackgroundManagerTask* pTask = new BackgroundManagerTask (pSupervisor, pszTaskName,
+    iFirstUnusedPos, pGauge, pLabel, pButton, s_iNextButtonID);
 
   m_vecpTasks.push_back (pTask);
   m_iNumTasks++;
+  s_iNextButtonID++;
 
   resizeWindow();
   if (m_iNumTasks == 1) {
-       SetFocus();
-    Show(true);  
-    theApp->getMainFrame()->SetFocus();  // necessary to keep wxWindows from crashing
+    m_pCanvas->SetFocus();
+    Show(true);
   }
 }
 
@@ -168,27 +170,38 @@ BackgroundManager::OnRemoveTask (wxCommandEvent& event)
   bool bFound = false;
   for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
     if ((*iTask)->supervisor() == pSupervisor) {
-         delete (*iTask)->gauge();
-         delete (*iTask)->label();
-         delete (*iTask)->button();
+          delete (*iTask)->gauge();
+            delete (*iTask)->label();
+            delete (*iTask)->button();
       delete *iTask;
       m_vecpTasks.erase (iTask);
       m_iNumTasks--;
-         bFound = true;
+            bFound = true;
       break;
     }
   }
-  if (! bFound) 
-         sys_error (ERR_SEVERE, "Unable to find supervisor [BackgroundManager::OnRemoveTask]");
-
+  if (! bFound)  {
+          sys_error (ERR_SEVERE, "Unable to find supervisor [BackgroundManager::OnRemoveTask]");
+    return;
+  }
+  pSupervisor->ackRemoveBackgroundManager();
   resizeWindow();
-  if (m_iNumTasks <= 0)
+  if (m_iNumTasks <= 0) {
+    m_pCanvas->SetFocus();
     Show(false);
+  }
 }
 
 void
 BackgroundManager::OnCancelButton (wxCommandEvent& event)
 {
+  BackgroundManagerTask* pTask = lookupTask (event.GetId());
+  if (! pTask) {
+    sys_error (ERR_SEVERE, "Unable to lookup task for button");
+    return;
+  }
+
+  pTask->supervisor()->onCancel();
 }
 
 BackgroundManagerTask*
@@ -207,6 +220,22 @@ BackgroundManager::lookupTask (BackgroundSupervisor* pSupervisor)
   return pTask;
 }
 
+BackgroundManagerTask*
+BackgroundManager::lookupTask (int iButtonID)
+{
+  BackgroundManagerTask* pTask = NULL;
+
+  wxCriticalSectionLocker locker (m_criticalSection);
+  for (TaskContainer::iterator iTask = m_vecpTasks.begin(); iTask != m_vecpTasks.end(); iTask++) {
+    if ((*iTask)->buttonID() == iButtonID) {
+      pTask = *iTask;
+      break;
+    }
+  }
+
+  return pTask;
+}
+
 void
 BackgroundManager::resizeWindow()
 {