#include "TRootEmbeddedCanvasHenrik.h"
#include "TMControlSPD.h"
#include "TMControlMutex.h"
#include <TCanvas.h>

ClassImp(TRootEmbeddedCanvasHenrik)

extern pthread_mutex_t gMutex;

//______________________________________________________________________
TRootEmbeddedCanvasHenrik::TRootEmbeddedCanvasHenrik(const char *name, const TGWindow *p,
						     UInt_t w, UInt_t h, UInt_t options, ULong_t back) : 
  TRootEmbeddedCanvas(name,p,w,h,options,back),
  fConnected(kTRUE), fBusy(kFALSE)
{}
//______________________________________________________________________
TRootEmbeddedCanvasHenrik::~TRootEmbeddedCanvasHenrik() 
{}
//______________________________________________________________________
Bool_t TRootEmbeddedCanvasHenrik::HandleContainerButton(Event_t *event) {
  if (!fConnected) return kFALSE;
  fBusy=kTRUE;
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    TMControlMutex::Instance()->ITakeLock();
  }

  Bool_t returnval = kFALSE; 
  returnval = TRootEmbeddedCanvas::HandleContainerButton(event);
//!!!  Bool_t changed = fCanvas->IsModified();
//!!!  //  printf("ismod=%d\n",changed);
//!!!  if (changed) TMControlSPD::Instance()->SetTimeSafeUpdate();

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }

  fBusy=kFALSE;
  return returnval;
}
//______________________________________________________________________
Bool_t TRootEmbeddedCanvasHenrik::HandleContainerDoubleClick(Event_t *event) {
  if (!fConnected) return kFALSE;
  fBusy=kTRUE;
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    TMControlMutex::Instance()->ITakeLock();
  }

  Bool_t returnval = kFALSE;
  returnval = TRootEmbeddedCanvas::HandleContainerDoubleClick(event);
//!!!  Bool_t changed = fCanvas->IsModified();
//!!!  //  printf("ismod=%d\n",changed);
//!!!  if (changed) TMControlSPD::Instance()->SetTimeSafeUpdate();

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }

  fBusy=kFALSE;
  return returnval;
}
//______________________________________________________________________________
Bool_t TRootEmbeddedCanvasHenrik::HandleContainerConfigure(Event_t *event) {
  if (!fConnected) return kFALSE;
  fBusy=kTRUE;
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    TMControlMutex::Instance()->ITakeLock();
  }

  Bool_t returnval = kFALSE;
  returnval = TRootEmbeddedCanvas::HandleContainerConfigure(event);
//!!!  Bool_t changed = fCanvas->IsModified();
//!!!  //  printf("ismod=%d\n",changed);
//!!!  if (changed) TMControlSPD::Instance()->SetTimeSafeUpdate();

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }

  fBusy=kFALSE;
  return returnval;
}
//______________________________________________________________________________
Bool_t TRootEmbeddedCanvasHenrik::HandleContainerKey(Event_t *event) {
  if (!fConnected) return kFALSE;
  fBusy=kTRUE;
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    TMControlMutex::Instance()->ITakeLock();
  }

  Bool_t returnval = kFALSE;
  returnval = TRootEmbeddedCanvas::HandleContainerKey(event);
//!!!  Bool_t changed = fCanvas->IsModified();
//!!!  //  printf("ismod=%d\n",changed);
//!!!  if (changed) TMControlSPD::Instance()->SetTimeSafeUpdate();

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }

  fBusy=kFALSE;
  return returnval;
}
//______________________________________________________________________________
Bool_t TRootEmbeddedCanvasHenrik::HandleContainerMotion(Event_t *event) {
  if (!fConnected) return kFALSE;
  fBusy=kTRUE;
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    TMControlMutex::Instance()->ITakeLock();
  }

  Bool_t returnval = kFALSE;
  returnval = TRootEmbeddedCanvas::HandleContainerMotion(event);
//!!!  Bool_t changed = fCanvas->IsModified();
//!!!  //  printf("ismod=%d\n",changed);
//!!!  if (changed) TMControlSPD::Instance()->SetTimeSafeUpdate();

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }

  fBusy=kFALSE;
  return returnval;
}
//______________________________________________________________________________
Bool_t TRootEmbeddedCanvasHenrik::HandleContainerExpose(Event_t *event) {
  if (!fConnected) return kFALSE;
  fBusy=kTRUE;
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    TMControlMutex::Instance()->ITakeLock();
  }

  Bool_t returnval = kFALSE;
  returnval = TRootEmbeddedCanvas::HandleContainerExpose(event);
//!!!  Bool_t changed = fCanvas->IsModified();
//!!!  //  printf("ismod=%d\n",changed);
//!!!  if (changed) TMControlSPD::Instance()->SetTimeSafeUpdate();

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }

  fBusy=kFALSE;
  return returnval;
}
//______________________________________________________________________________
Bool_t TRootEmbeddedCanvasHenrik::HandleContainerCrossing(Event_t *event) {
  if (!fConnected) return kFALSE;
  fBusy=kTRUE;
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    TMControlMutex::Instance()->ITakeLock();
  }

  Bool_t returnval = kFALSE;
  returnval = TRootEmbeddedCanvas::HandleContainerCrossing(event);
//!!!  Bool_t changed = fCanvas->IsModified();
//!!!  //  printf("ismod=%d\n",changed);
//!!!  if (changed) TMControlSPD::Instance()->SetTimeSafeUpdate();

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }

  fBusy=kFALSE;
  return returnval;
}
