Index: trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 8820)
+++ trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 8821)
@@ -34,5 +34,4 @@
 #include "MGStarg.h" 
 #include "MGNumStars.h"
-#include "TGFrame.h"
 
 #include "MGImage.h"
@@ -53,5 +52,4 @@
 #include "MAstroCamera.h"
 
-#include "MGMenu.h"
 #include "MGCosy.h"
 
@@ -148,8 +146,9 @@
 {
     fList = new MGList;
+    fList->SetOwner();
 
     const TGWindow *p=gClient->GetRoot();
 
-    fChannel = new MGPopupMenu(p);
+    fChannel = new TGPopupMenu(p);
     fChannel->AddEntry("Starfield Camera", IDM_kChannel1);
     fChannel->AddEntry("TPoint Camera",    IDM_kChannel2);
@@ -162,8 +161,117 @@
     fList->Add(fChannel);
 
+    fFileType = new TGPopupMenu(p);
+    fFileType->AddEntry("PP&M", IDM_kPPM);
+    fFileType->AddEntry("&PNG", IDM_kPNG);
+    fFileType->CheckEntry(IDM_kPNG);
+    fFileType->Associate(this);
+    fList->Add(fFileType);
+
+    fWriteType = new TGPopupMenu(p);
+    fWriteType->AddEntry("&Once",      IDM_kOnce);
+    fWriteType->AddEntry("&Continous", IDM_kContinous);
+    fWriteType->CheckEntry(IDM_kOnce);
+    fWriteType->Associate(this);
+    fList->Add(fWriteType);
+
+    fWriteRate = new TGPopupMenu(p);
+    fWriteRate->AddEntry("25/s", IDM_kRate25ps);
+    fWriteRate->AddEntry("5/s",  IDM_kRate5ps);
+    fWriteRate->AddEntry("1s",   IDM_kRate1s);
+    fWriteRate->AddEntry("5s",   IDM_kRate5s);
+    fWriteRate->AddEntry("30s",  IDM_kRate30s);
+    fWriteRate->AddEntry("1min", IDM_kRate1m);
+    fWriteRate->AddEntry("5min", IDM_kRate5m);
+    fWriteRate->CheckEntry(IDM_kRate1m);
+    fWriteRate->Associate(this);
+    fList->Add(fWriteRate);
+
+    fWrtRate = 25*60;
+
+    fLimMag = new TGPopupMenu(p);
+    fLimMag->AddEntry("3", IDM_kLimMag3);
+    fLimMag->AddEntry("4", IDM_kLimMag4);
+    fLimMag->AddEntry("5", IDM_kLimMag5);
+    fLimMag->AddEntry("6", IDM_kLimMag6);
+    fLimMag->AddEntry("7", IDM_kLimMag7);
+    fLimMag->AddEntry("8", IDM_kLimMag8);
+    fLimMag->AddEntry("9", IDM_kLimMag9);
+    fLimMag->CheckEntry(IDM_kLimMag9);
+    fLimMag->Associate(this);
+    fList->Add(fLimMag);
+
+    fSao->SetLimitMag(9.0);
+
+    fInterpol = new TGPopupMenu(p);
+    fInterpol->AddEntry("250", IDM_kInterpol250);
+    fInterpol->AddEntry("125", IDM_kInterpol125);
+    fInterpol->AddEntry("50",  IDM_kInterpol50);
+    fInterpol->AddEntry("25",  IDM_kInterpol25);
+    fInterpol->AddEntry("10",  IDM_kInterpol10);
+    fInterpol->AddEntry("5",   IDM_kInterpol5);
+    fInterpol->AddEntry("2",   IDM_kInterpol2);
+    fInterpol->AddEntry("Off", IDM_kInterpol1);
+    fInterpol->Associate(this);
+    fList->Add(fInterpol);
+
+    TString disp=gVirtualX->DisplayName();
+    cout << "Display: " << disp << endl;
+    if (disp.First(':')>=0)
+        disp=disp(0, disp.First(':'));
+
+    if (disp.IsNull() || disp==(TString)"localhost")
+    {
+        fInterpol->CheckEntry(IDM_kInterpol5);
+        fIntRate = 50;
+    }
+    else
+    {
+        fInterpol->CheckEntry(IDM_kInterpol125);
+        fIntRate = 125;
+    }
+
+    fCaosPrint = new TGPopupMenu(p);
+    fCaosPrint->AddEntry("&Leds",  IDM_kCaosPrintLeds);
+    fCaosPrint->AddEntry("&Rings", IDM_kCaosPrintRings);
+    fCaosPrint->Associate(this);
+    fList->Add(fCaosPrint);
+
+    fCaosWrite = new TGPopupMenu(p);
+    fCaosWrite->AddEntry("&Start", IDM_kCaosWriteStart);
+    fCaosWrite->AddEntry("Sto&p",  IDM_kCaosWriteStop);
+    fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
+    fCaosWrite->Associate(this);
+    fList->Add(fCaosWrite);
+
+    fCaosAnalyse = new TGPopupMenu(p);
+    fCaosAnalyse->AddEntry("S&tart Analysis", IDM_kCaosAnalStart);
+    fCaosAnalyse->AddEntry("St&op Analysis",  IDM_kCaosAnalStop);
+    fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
+    //    fCaosAnalyse->AddEntry("&Reset Histograms", IDM_kResetHistograms);
+    //    fCaosAnalyse->AddEntry("Reset &Graph", IDM_kResetGraph);
+    fCaosAnalyse->Associate(this);
+    fList->Add(fCaosAnalyse);
+
+    fMenu = new TGMenuBar(this, 0, 0, kHorizontalFrame);
+    //fMenu->SetCleanup();
+    //fMenu->AddPopup("&Display",    fDisplay,         NULL);
+    //fMenu->AddPopup("&Mode",       fMode,            NULL);
+    //fMenu->AddPopup("&WritePics",  fWritePictures,   NULL);
+    //fMenu->AddPopup("&Setup",      fSetup,           NULL);
+    //fMenu->AddPopup("&Operations", fOperations,      NULL);
+    fDisplay       = fMenu->AddPopup("&Display");
+    fMode          = fMenu->AddPopup("&Mode");
+    fWritePictures = fMenu->AddPopup("&WritePics");
+    fSetup         = fMenu->AddPopup("&Setup");
+    fOperations    = fMenu->AddPopup("&Operations");
+    fMenu->Resize(fMenu->GetDefaultSize());
+    //fMenu->BindKeys(this);
+    AddFrame(fMenu); 
+    //fList->Add(fMenu);
+
     //
     // Create Menu for MStarguider Display
     //
-    fDisplay = new MGPopupMenu(p);
+    //fDisplay = new MMGPopupMenu(p);
     fDisplay->AddEntry("&Filter",               IDM_kFilter);
     fDisplay->AddEntry("Stretch",               IDM_kStretch);
@@ -182,49 +290,13 @@
     fDisplay->CheckEntry(IDM_kStretch);
     fDisplay->Associate(this);
-    fList->Add(fDisplay);
-
-    fMode = new MGPopupMenu(p);
+    //fList->Add(fDisplay);
+
+    //fMode = new MGPopupMenu(p);
     fMode->AddEntry("Starguider", IDM_kStarguiderMode);
     fMode->AddEntry("Tpoint",     IDM_kTpointMode);
     fMode->Associate(this);
-    fList->Add(fMode);
-
-    fOperations = new MGPopupMenu(p);
-    fOperations->AddEntry("Roque Lamp Analysis", IDM_kRoqueLampAna);
-    fOperations->AddEntry("Starguider Analysis", IDM_kStargAnalysis);
-    fOperations->DisableEntry(IDM_kStargAnalysis);
-    fOperations->Associate(this);
-    fList->Add(fOperations);
-
-
-    fFileType = new MGPopupMenu(p);
-    fFileType->AddEntry("PP&M", IDM_kPPM);
-    fFileType->AddEntry("&PNG", IDM_kPNG);
-    fFileType->CheckEntry(IDM_kPNG);
-    fFileType->Associate(this);
-    fList->Add(fFileType);
-
-    fWriteType = new MGPopupMenu(p);
-    fWriteType->AddEntry("&Once",      IDM_kOnce);
-    fWriteType->AddEntry("&Continous", IDM_kContinous);
-    fWriteType->CheckEntry(IDM_kOnce);
-    fWriteType->Associate(this);
-    fList->Add(fWriteType);
-
-    fWriteRate = new MGPopupMenu(p);
-    fWriteRate->AddEntry("25/s", IDM_kRate25ps);
-    fWriteRate->AddEntry("5/s",  IDM_kRate5ps);
-    fWriteRate->AddEntry("1s",   IDM_kRate1s);
-    fWriteRate->AddEntry("5s",   IDM_kRate5s);
-    fWriteRate->AddEntry("30s",  IDM_kRate30s);
-    fWriteRate->AddEntry("1min", IDM_kRate1m);
-    fWriteRate->AddEntry("5min", IDM_kRate5m);
-    fWriteRate->CheckEntry(IDM_kRate1m);
-    fWriteRate->Associate(this);
-    fList->Add(fWriteRate);
-
-    fWrtRate = 25*60;
-
-    fWritePictures = new MGPopupMenu(p);
+    //fList->Add(fMode);
+
+    //fWritePictures = new MGPopupMenu(p);
     fWritePictures->AddEntry("&Start",      IDM_kStart);
     fWritePictures->AddEntry("Sto&p",       IDM_kStop);
@@ -235,102 +307,33 @@
     fWritePictures->DisableEntry(IDM_kStop);
     fWritePictures->Associate(this);
-    fList->Add(fWritePictures);
-
-    fLimMag = new MGPopupMenu(p);
-    fLimMag->AddEntry("3", IDM_kLimMag3);
-    fLimMag->AddEntry("4", IDM_kLimMag4);
-    fLimMag->AddEntry("5", IDM_kLimMag5);
-    fLimMag->AddEntry("6", IDM_kLimMag6);
-    fLimMag->AddEntry("7", IDM_kLimMag7);
-    fLimMag->AddEntry("8", IDM_kLimMag8);
-    fLimMag->AddEntry("9", IDM_kLimMag9);
-    fLimMag->CheckEntry(IDM_kLimMag9);
-    fLimMag->Associate(this);
-    fList->Add(fLimMag);
-
-    fSao->SetLimitMag(9.0);
-
-    fInterpol = new MGPopupMenu(p);
-    fInterpol->AddEntry("250", IDM_kInterpol250);
-    fInterpol->AddEntry("125", IDM_kInterpol125);
-    fInterpol->AddEntry("50",  IDM_kInterpol50);
-    fInterpol->AddEntry("25",  IDM_kInterpol25);
-    fInterpol->AddEntry("10",  IDM_kInterpol10);
-    fInterpol->AddEntry("5",   IDM_kInterpol5);
-    fInterpol->AddEntry("2",   IDM_kInterpol2);
-    fInterpol->AddEntry("Off", IDM_kInterpol1);
-    fInterpol->Associate(this);
-    fList->Add(fInterpol);
-
-    TString disp=gVirtualX->DisplayName();
-    cout << "Display: " << disp << endl;
-    if (disp.First(':')>=0)
-        disp=disp(0, disp.First(':'));
-
-    if (disp.IsNull() || disp==(TString)"localhost")
-    {
-        fInterpol->CheckEntry(IDM_kInterpol5);
-        fIntRate = 50;
-    }
-    else
-    {
-        fInterpol->CheckEntry(IDM_kInterpol125);
-        fIntRate = 125;
-    }
-
-    fSetup = new MGPopupMenu(p);
+    //fList->Add(fWritePictures);
+
+    //fSetup = new MGPopupMenu(p);
     fSetup->AddPopup("Lim. &Magnitude",      fLimMag);
     fSetup->AddPopup("Disp. &Interpolation", fInterpol);
     //fSetup->AddEntry("Use Ra/Dec from file", IDM_kUseFileRaDec);
     fSetup->Associate(this);
-    fList->Add(fSetup);
-
-    fCaosPrint = new MGPopupMenu(p);
-    fCaosPrint->AddEntry("&Leds",  IDM_kCaosPrintLeds);
-    fCaosPrint->AddEntry("&Rings", IDM_kCaosPrintRings);
-    fCaosPrint->Associate(this);
-    fList->Add(fCaosPrint);
-
-    fCaosWrite = new MGPopupMenu(p);
-    fCaosWrite->AddEntry("&Start", IDM_kCaosWriteStart);
-    fCaosWrite->AddEntry("Sto&p",  IDM_kCaosWriteStop);
-    fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
-    fCaosWrite->Associate(this);
-    fList->Add(fCaosWrite);
-
-    fCaosAnalyse = new MGPopupMenu(p);
-    fCaosAnalyse->AddEntry("S&tart Analysis", IDM_kCaosAnalStart);
-    fCaosAnalyse->AddEntry("St&op Analysis",  IDM_kCaosAnalStop);
-    fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
-    //    fCaosAnalyse->AddEntry("&Reset Histograms", IDM_kResetHistograms);
-    //    fCaosAnalyse->AddEntry("Reset &Graph", IDM_kResetGraph);
-    fCaosAnalyse->Associate(this);
-    fList->Add(fCaosAnalyse);
-
-    fMenu = new MGMenuBar(this, 0, 0, kHorizontalFrame);
-    fMenu->AddPopup("&Display",    fDisplay,         NULL);
-    fMenu->AddPopup("&Mode",       fMode,            NULL);
-    fMenu->AddPopup("&WritePics",  fWritePictures,   NULL);
-    fMenu->AddPopup("&Setup",      fSetup,           NULL);
-    fMenu->AddPopup("&Operations", fOperations,      NULL);
-
-    fMenu->Resize(fMenu->GetDefaultSize());
-    fMenu->BindKeys(this);
-    AddFrame(fMenu); 
-    fList->Add(fMenu);
-
-    fCaOs = new MGPopupMenu(p);
+    //fList->Add(fSetup);
+
+    //fOperations = new MGPopupMenu(p);
+    fOperations->AddEntry("Roque Lamp Analysis", IDM_kRoqueLampAna);
+    fOperations->AddEntry("Starguider Analysis", IDM_kStargAnalysis);
+    fOperations->DisableEntry(IDM_kStargAnalysis);
+    fOperations->Associate(this);
+    //fList->Add(fOperations);
+
+    fCaOs = new TGPopupMenu(p);
     fCaOs->AddPopup("&Write",   fCaosWrite);
     fCaOs->AddPopup("&Print",   fCaosPrint);
     fCaOs->AddPopup("&Analyse", fCaosAnalyse);
     fCaOs->Associate(this);
-    fCaOs->BindKeys(fMenu, this);
+    //fCaOs->BindKeys(fMenu, this);
     fList->Add(fCaOs);
-
+/*
     TGLayoutHints *hints2a = 
 	new TGLayoutHints(kLHintsCenterX|kLHintsCenterY|
-			  kLHintsExpandX|kLHintsExpandY,1,1);
+        		  kLHintsExpandX|kLHintsExpandY,1,1);
     fList->Add(hints2a);
-
+ */
     fGStarg = new MGStarg(this, 235);
     fGStarg->DrawText("0.75'", "1.50'", "3.00'", "Mispointing [min]");
@@ -343,5 +346,5 @@
     fCRaDec->Move(4, fMenu->GetDefaultHeight()+584);
     AddFrame(fCRaDec);
-    fList->Add(fCRaDec);
+    //fList->Add(fCRaDec);
 
     //telescope position
@@ -349,5 +352,5 @@
     fCZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+597);
     AddFrame(fCZdAz);
-    fList->Add(fCZdAz);
+    //fList->Add(fCZdAz);
 
     //starguider position
@@ -355,5 +358,5 @@
     fPZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+640);
     AddFrame(fPZdAz);
-    fList->Add(fPZdAz);
+    //fList->Add(fPZdAz);
 
     //mispointing
@@ -361,10 +364,10 @@
     fDZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+683);
     AddFrame(fDZdAz);
-    fList->Add(fDZdAz);
+    //fList->Add(fDZdAz);
 
     fSZdAz = new MGCoordinates(this, kETypeZdAz, 2);
     fSZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+795);
     AddFrame(fSZdAz);
-    fList->Add(fSZdAz);
+    //fList->Add(fSZdAz);
 
     fGNumStars = new MGNumStars(this, 235);
@@ -377,5 +380,5 @@
     fTPoint->AllowStayDown(kTRUE);
     AddFrame(fTPoint);
-    fList->Add(fTPoint);
+    //fList->Add(fTPoint);
 
     fStargTPoint = new TGTextButton(this, "StargTPoint");
@@ -383,5 +386,5 @@
     fStargTPoint->AllowStayDown(kTRUE);
     AddFrame(fStargTPoint);
-    fList->Add(fStargTPoint);
+    //fList->Add(fStargTPoint);
 
     fFps = new TGLabel(this, "---fps");
@@ -389,5 +392,5 @@
     fFps->Move(650-495, fMenu->GetDefaultHeight()+714+23);
     AddFrame(fFps);
-    fList->Add(fFps);
+    //fList->Add(fFps);
 
     fPosZoom = new TGLabel(this, "(----, ----) ----.--d/----.--d");
@@ -395,5 +398,5 @@
     fPosZoom->Move(4, fMenu->GetDefaultHeight()+765);
     AddFrame(fPosZoom);
-    fList->Add(fPosZoom);
+    //fList->Add(fPosZoom);
 
     fSkyBright = new TGLabel(this, "Sky Brightness: ---         ");
@@ -401,5 +404,5 @@
     fSkyBright->Move(4, fMenu->GetDefaultHeight()+785);
     AddFrame(fSkyBright);
-    fList->Add(fSkyBright);
+    //fList->Add(fSkyBright);
 
     TGLabel *l = new TGLabel(this, "deg");
@@ -407,5 +410,5 @@
     l->Move(606-412, fMenu->GetDefaultHeight()+669);
     AddFrame(l);
-    fList->Add(l);
+    //fList->Add(l);
 
     l = new TGLabel(this, "arcsec/pix");
@@ -413,5 +416,5 @@
     l->Move(606-412, fMenu->GetDefaultHeight()+692);
     AddFrame(l);
-    fList->Add(l);
+    //fList->Add(l);
 
     l = new TGLabel(this, "sigma");
@@ -419,5 +422,5 @@
     l->Move(606-412, fMenu->GetDefaultHeight()+715);
     AddFrame(l);
-    fList->Add(l);
+    //fList->Add(l);
 
     fCZdAzText = new TGLabel(this, "Zd/Az telescope pointing at");
@@ -425,5 +428,5 @@
     fCZdAzText->Move(240+12+20+7, fMenu->GetDefaultHeight()+584-5);
     AddFrame(fCZdAzText);
-    fList->Add(fCZdAzText);
+    //fList->Add(fCZdAzText);
 
     fPZdAzText = new TGLabel(this, "Zd/Az starguider pointing at");
@@ -431,5 +434,5 @@
     fPZdAzText->Move(240+12+20+7, fMenu->GetDefaultHeight()+630+20-5-23);
     AddFrame(fPZdAzText);
-    fList->Add(fPZdAzText);
+    //fList->Add(fPZdAzText);
 
     fDZdAzText = new TGLabel(this, "Zd/Az mispointing");
@@ -437,5 +440,5 @@
     fDZdAzText->Move(240+12+20+7, fMenu->GetDefaultHeight()+676+2*20-5-46);
     AddFrame(fDZdAzText);
-    fList->Add(fDZdAzText);
+    //fList->Add(fDZdAzText);
 
 #ifdef EXPERT
@@ -444,5 +447,5 @@
     l->Move(240+12+20, fMenu->GetDefaultHeight()+722+3*20-5);
     AddFrame(l);
-    fList->Add(l);
+    //fList->Add(l);
 #endif
 
@@ -458,5 +461,5 @@
     fAngle->Move(547-410, fMenu->GetDefaultHeight()+667);
     AddFrame(fAngle);
-    fList->Add(fAngle);
+    //fList->Add(fAngle);
 
     // Set input box for pixel size
@@ -472,5 +475,5 @@
     fPixSize->Move(547-410, fMenu->GetDefaultHeight()+690);
     AddFrame(fPixSize);
-    fList->Add(fPixSize);
+    //fList->Add(fPixSize);
 
     // Set input box for cleaning cut
@@ -484,5 +487,5 @@
     fCut->Move(547-410, fMenu->GetDefaultHeight()+713);
     AddFrame(fCut);
-    fList->Add(fCut);
+    //fList->Add(fCut);
 
     // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
@@ -497,10 +500,10 @@
     fZoomImage->Move(4, 700-kZOOM-2+85);
     AddFrame(fZoomImage);
-    fList->Add(fZoomImage);
+    //fList->Add(fZoomImage);
 
     fImage = new MGImage(this, 768, 576);
     fImage->Move(0, fMenu->GetDefaultHeight());
     AddFrame(fImage);
-    fList->Add(fImage);
+    //fList->Add(fImage);
 
     const Int_t w = 768;
@@ -542,13 +545,19 @@
 MStarguider::MStarguider(MObservatory::LocationName_t obs, Int_t channel)
     : TGMainFrame(gClient->GetRoot(), 768, 840), 
-      fCosy(NULL), 
-      fOutTp(0), 
+      fNumStarsDetected(0),
+      fNumStarsCorrelated(0),
+      fCosy(NULL),
+      fOutTp(0),
       fOutStargTp(0),
       fOutRq(0),
-      fDx((768-kZOOM)/2), 
-      fDy((512-kZOOM)/2), 
+      fDx((768-kZOOM)/2),
+      fDy((512-kZOOM)/2),
       fStatus(MDriveCom::kStandby)
 {
     cout << " #### FIXME: Make MCaos Thread safe!" << endl;
+
+    // This means that all objects added with AddFrame are deleted
+    // automatically, including all LayoutHints.
+    SetCleanup();
 
     fAmcSocket = new TSocket("amc", 7307);
@@ -609,6 +618,4 @@
     delete fSao;
     delete fRaDec;
-    delete fTPoint;//
-    delete fStargTPoint;//
 
     if (fOutTp)
@@ -663,5 +670,5 @@
 }
 
-void MStarguider::SwitchOff(MGPopupMenu *p, UInt_t id)
+void MStarguider::SwitchOff(TGPopupMenu *p, UInt_t id)
 {
     p->UnCheckEntry(id);
@@ -669,5 +676,28 @@
 }
 
-void MStarguider::Toggle(MGPopupMenu *p, UInt_t id)
+void MStarguider::SetChannel()
+{
+    if (fChannel->IsEntryChecked(IDM_kChannel3))
+    {
+        if (dynamic_cast<PngReader*>(fGetter)==0)
+        {
+            delete fGetter;
+            fGetter=new PngReader(*this);
+        }
+    }
+    else
+    {
+        const Int_t ch = fChannel->IsEntryChecked(IDM_kChannel1) ? 0 : 1;
+        if (dynamic_cast<Camera*>(fGetter)==0)
+        {
+            delete fGetter;
+            fGetter = new Camera(*this, ch);
+        }
+        else
+            fGetter->SetChannel(ch);
+    }
+}
+
+void MStarguider::Toggle(TGPopupMenu *p, UInt_t id)
 {
     if (p->IsEntryChecked(id))
@@ -736,22 +766,4 @@
         fGStarg->MapWindow();
         fGNumStars->MapWindow();
-
-        const Int_t ch0 =
-            fChannel->IsEntryChecked(IDM_kChannel1) ? 0 : 1;
-        const Int_t ch1 = 0;
-
-        if (ch0!=ch1)
-        {
-//            fGetter->ExitLoop();
-            delete fGetter;
-            usleep(150000); // FIX: Device or resource busy.
-            if (fChannel->IsEntryChecked(IDM_kChannel3))
-                fGetter=new PngReader(*this);
-            else
-            {
-                fGetter = new Camera(*this, ch1);
-                ((Camera*)fGetter)->Loop(0);
-            }
-        }
 
         SwitchOff(fChannel, IDM_kChannel2);
@@ -785,4 +797,6 @@
         fOperations->DisableEntry(IDM_kStargAnalysis);
     }
+
+    SetChannel();
     //gSystem->Unlink("tracking_error.txt");
 }
@@ -991,19 +1005,8 @@
 
                     //switch camera
-                    const Int_t ch0 =
-                        fChannel->IsEntryChecked(IDM_kChannel1) ? 0 : 1;
-                    const Int_t ch1 = 1;
-
-                    if (ch0!=ch1)
-                    {
-//                        fGetter->ExitLoop();
-                        delete fGetter;
-                        usleep(150000); // FIX: Device or resource busy.
-                        fGetter = new Camera(*this, ch1);
-                        ((Camera*)fGetter)->Loop(0);
-                    }
-
                     SwitchOff(fChannel, IDM_kChannel1);
                     fChannel->CheckEntry(IDM_kChannel2);
+
+                    SetChannel();
 
                     //checking needed items
@@ -1182,14 +1185,6 @@
                     fChannel->CheckEntry  (ch1==0?IDM_kChannel1:IDM_kChannel2);
                     fChannel->UnCheckEntry(ch1==1?IDM_kChannel1:IDM_kChannel2);
-//                    fGetter->ExitLoop();
-                    delete fGetter;
-                    usleep(150000); // FIX: Device or resource busy.
-		    if (fChannel->IsEntryChecked(IDM_kChannel3))
-                        fGetter=new PngReader(*this);
-                    else
-                    {
-                        fGetter = new Camera(*this, ch1);
-                        ((Camera*)fGetter)->Loop(0);
-                    }
+
+                    SetChannel();
                 }
                 return kTRUE;
@@ -1810,12 +1805,12 @@
     FilterLed f(img,   768, 576, 2.5); // 2.5
     FilterLed f2(cimg, 768, 576); // former color 0xb0
-    
+
     if (fDisplay->IsEntryChecked(IDM_kStretch))
         f.Stretch();
-    
+
     if (!fWritePictures->IsEntryEnabled(IDM_kStart) &&
         (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
     {     
- 
+
         if (fFileType->IsEntryChecked(IDM_kPNG))
             Writer::Png("pix/file", img, tm, fCRaDec->GetCoordinates());
@@ -1827,9 +1822,9 @@
             ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
     }
-    
+
     // Visual Filter, whole FOV
     if (fDisplay->IsEntryChecked(IDM_kFilter))
         f.Execute();
-   
+
     // Find Center of Camera for Caos and Tpoints
     Ring center(768/2, 576/2);
Index: trunk/MagicSoft/Cosy/main/MStarguider.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 8820)
+++ trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 8821)
@@ -4,5 +4,6 @@
 #include "PixClient.h"
 #include "MGImage.h"
-#include "coord.h"
+
+#include "MPointing.h"
 
 #ifndef MARS_MObservatory
@@ -24,6 +25,6 @@
 class TTimer;
 
-class MGMenuBar;
-class MGPopupMenu;
+class TGMenuBar;
+class TGPopupMenu;
 class TGTextEntry;
 
@@ -53,5 +54,5 @@
     MGList        *fList;
 
-    MGMenuBar     *fMenu;
+    TGMenuBar     *fMenu;
     MGImage       *fImage;
     MGImage       *fZoomImage;
@@ -60,21 +61,21 @@
     MGNumStars    *fGNumStars;
 
-    MGPopupMenu   *fDisplay;
-    MGPopupMenu   *fMode;
-    MGPopupMenu   *fWritePictures;
-    MGPopupMenu   *fFileType;
-    MGPopupMenu   *fWriteType;
-    MGPopupMenu   *fWriteRate;
-    MGPopupMenu   *fInterpol;
-    MGPopupMenu   *fSetup;
-    MGPopupMenu   *fLimMag;
-    MGPopupMenu   *fChannel;
-
-    MGPopupMenu   *fOperations;
-
-    MGPopupMenu   *fCaosWrite;
-    MGPopupMenu   *fCaosPrint;
-    MGPopupMenu   *fCaosAnalyse;
-    MGPopupMenu   *fCaOs;
+    TGPopupMenu   *fDisplay;
+    TGPopupMenu   *fMode;
+    TGPopupMenu   *fWritePictures;
+    TGPopupMenu   *fSetup;
+    TGPopupMenu   *fOperations;
+
+    TGPopupMenu   *fFileType;
+    TGPopupMenu   *fWriteType;
+    TGPopupMenu   *fWriteRate;
+    TGPopupMenu   *fInterpol;
+    TGPopupMenu   *fLimMag;
+    TGPopupMenu   *fChannel;
+
+    TGPopupMenu   *fCaosWrite;
+    TGPopupMenu   *fCaosPrint;
+    TGPopupMenu   *fCaosAnalyse;
+    TGPopupMenu   *fCaOs;
 
     MGCoordinates *fCRaDec;
@@ -133,10 +134,11 @@
 
     void SetPixSize(const double pixsize);
-    void Toggle(MGPopupMenu *p, UInt_t id);
-    void SwitchOff(MGPopupMenu *p, UInt_t id);
+    void Toggle(TGPopupMenu *p, UInt_t id);
+    void SwitchOff(TGPopupMenu *p, UInt_t id);
     void ToggleStargAnalysis();
     void ToggleFindStar();
     void ToggleStarguider();
     void ToggleCaosFilter();
+    void SetChannel();
     //void GetCoordinates();
     Int_t CalcTrackingError(Leds &, MStarList &, ZdAz &, MTime &, double &bright, Int_t &num);
@@ -145,6 +147,6 @@
     bool Interpolate(const unsigned long n, byte *img) const;
 
-    XY FindRoqueLamp(FilterLed &f, FilterLed &f2, Ring &CameraCenter, MTime &t, 
-		     Double_t cut, Int_t box, XY SearchCenter);
+    XY FindRoqueLamp(FilterLed &f, FilterLed &f2, Ring &CameraCenter, MTime &t,
+                     Double_t cut, Int_t box, XY SearchCenter);
 
     ZdAz FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t, 
