Index: trunk/MagicSoft/Mars/mgui/MCamDisplay.cc
===================================================================
--- trunk/MagicSoft/Mars/mgui/MCamDisplay.cc	(revision 978)
+++ trunk/MagicSoft/Mars/mgui/MCamDisplay.cc	(revision 979)
@@ -15,5 +15,5 @@
 #include "MCerPhotEvt.h"
 
-#define kItemsLegend 25
+#define kItemsLegend 25 // see SetPalette(1,0)
 
 ClassImp(MCamDisplay);
@@ -23,30 +23,24 @@
 //  default constructor
 //
-MCamDisplay::MCamDisplay(MGeomCam *geom) : fAutoScale(kTRUE)
-{
-    //
-    //    set the color palette
-    //
-    gStyle->SetPalette(1, 0);
-
+MCamDisplay::MCamDisplay(MGeomCam *geom)
+    : fAutoScale(kTRUE), fW(0), fH(0), fMinPhe(-2), fMaxPhe(50), fDrawingPad(NULL)
+{
     //
     //  create the hexagons of the display
     //
     fNumPixels = geom->GetNumPixels();
-    fRange     = (Int_t)geom->GetMaxRadius();
-
-    fPixels    = new TClonesArray("MHexagon", fNumPixels);
+    fRange     = geom->GetMaxRadius();
 
     //
     // Construct all hexagons. Use new-operator with placement
     //
+    fPixels = new TClonesArray("MHexagon", fNumPixels);
     for (UInt_t i=0; i<fNumPixels; i++)
         new ((*fPixels)[i]) MHexagon((*geom)[i]);
 
     //
-    // set the range to default
-    //
-    fMinPhe = -2.;
-    fMaxPhe = 50.;
+    // set the color palette for the TBox elements
+    //
+    gStyle->SetPalette(1, 0);
 
     //
@@ -58,12 +52,4 @@
     for (Int_t i = 0; i<kItemsLegend; i++)
     {
-        //const Int_t y = il*40;
-
-        //
-        // Construct gui elements of legend. Use new-operator with placement
-        //
-        //TBox  *newbox = new ((*fLegend)[il])  TBox (650, y-500, 700, y-460);
-        //TText *newtxt = new ((*fLegText)[il]) TText(720, y-480, text);
-
         TBox  *newbox = new ((*fLegend)[i])  TBox;
         TText *newtxt = new ((*fLegText)[i]) TText;
@@ -80,4 +66,5 @@
 // ------------------------------------------------------------------------
 //
+// Destructor. Deletes TClonesArrays for hexagons and legend elements.
 //
 MCamDisplay::~MCamDisplay()
@@ -95,4 +82,54 @@
 // ------------------------------------------------------------------------
 //
+// This is called at any time the canvas should get repainted.
+// Here we maintain an aspect ratio of 5/4=1.15. This makes sure,
+// that the camera image doesn't get distorted by resizing the canvas.
+//
+void MCamDisplay::Paint(Option_t *opt)
+{
+    const UInt_t w = gPad->GetWw();
+    const UInt_t h = gPad->GetWh();
+
+    //
+    // Check for a change in width or height, and make sure, that the
+    // first call also sets the range
+    //
+    if (w*fH == h*fW && fW && fH)
+        return;
+
+    //
+    // Calculate aspect ratio (5/4=1.25 recommended)
+    //
+    const Double_t ratio = (Double_t)w/h;
+
+    Float_t x;
+    Float_t y;
+
+    if (ratio>1.25)
+    {
+        x = (ratio*2-1)*fRange; 
+        y = fRange;
+    }
+    else
+    {
+        x = fRange*1.5;
+        y = fRange*1.25/ratio;
+    }
+
+    fH = h;
+    fW = w;
+
+    //
+    // Set new range
+    //
+    gPad->Range(-fRange, -y, x, y);
+}
+
+// ------------------------------------------------------------------------
+//
+// Call this function to draw the camera layout into your canvas.
+// Setup a drawing canvas. Add this object and all child objects
+// (hexagons, etc) to the current pad. If no pad exists a new one is
+// created.
 //
 void MCamDisplay::Draw(Option_t *option)
@@ -110,24 +147,8 @@
 
     //
-    // FIXME: This class should be a TPad, so that it is informed if the
-    // ratio between height and size changes!
-    //
-    const Float_t ratio = (Float_t)gPad->GetWw()/gPad->GetWh();
-
-    //
-    // The recommended size ratio is 5:4
-    //
-    Int_t x, y;
-
-    if (ratio > 5./4.)
-    {
-        x = (UInt_t)((ratio-1)*3*fRange);
-        y = fRange;
-    }
-    else
-    {
-        x = 3*fRange/2;
-        y = (UInt_t)(fRange/ratio);
-    }
+    // Append this object, so that the aspect ratio is maintained
+    // (Paint-function is called)
+    //
+    AppendPad(option);
 
     //
@@ -136,5 +157,4 @@
     gStyle->SetPalette(1, 0);
 
-    gPad->Range(-fRange, -y, x, y);
     gPad->SetFillColor(22);
 
@@ -172,12 +192,16 @@
 // ------------------------------------------------------------------------
 //
+// Call this function to draw the number of photo electron into the
+// camera.
 //
 void MCamDisplay::DrawPhotNum(const MCerPhotEvt *event)
 {
+    if (!fDrawingPad)
+        Draw();
+
     fDrawingPad->cd();
 
     //
-    // loop over all pixels in the MCerPhotEvt and
-    // determine the Pixel Id and the content
+    // Reset pixel colors to default value
     //
     Reset();
@@ -219,37 +243,4 @@
     gPad->Update();
 }
-
-// ------------------------------------------------------------------------
-//
-//
-void MCamDisplay::DrawPhotErr(const MCerPhotEvt *event)
-{
-    fDrawingPad->cd();
-
-    //
-    // reset the all pixel colors to a default value
-    //
-    Reset();
-
-    //
-    // loop over all pixels in the MCerPhotEvt and
-    // determine the Pixel Id and the content
-    //
-    const Int_t entries = event->GetNumPixels();
-
-    for (Int_t i=0 ; i<entries; i++)
-    {
-        const MCerPhotPix &pix = (*event)[i];
-
-        SetPixColor(pix);
-    }
-
-    //
-    // Update display physically
-    //
-    gPad->Modified();
-    gPad->Update();
-}
-
 
 // ------------------------------------------------------------------------
@@ -305,11 +296,11 @@
     char text[10];
 
-    for (Int_t il=0; il < kItemsLegend; il++)
-    {
-        const Float_t val = fMinPhe + (Float_t)il/kItemsLegend * (fMaxPhe-fMinPhe) ;
+    for (Int_t i=0; i<kItemsLegend; i++)
+    {
+        const Float_t val = fMinPhe + (Float_t)i/kItemsLegend * (fMaxPhe-fMinPhe) ;
 
         sprintf(text, "%5.1f", val);
 
-        TText &txt = *GetText(il);
+        TText &txt = *GetText(i);
 
         txt.SetText(txt.GetX(), txt.GetY(), text);
