#if defined(HAVE_CONFIG) || defined (R__HAVE_CONFIG) 
# include "config.h"
#endif
#ifdef R__QTWIN32
#include <process.h>
#endif
#include <assert.h>
#include <qapplication.h>
#if (QT_VERSION < 0x030200)
#  include <qthread.h>
#endif
#include <qwidget.h>
#if QT_VERSION < 0x40000
#  include <qptrvector.h>
#  include <qvaluestack.h>
#  include <qpicture.h>
#else /* QT_VERSION */
#  include <Q3CString>
#  include <Q3PointArray>
#  include <QEvent>
#  include <QImageWriter>
#  include <q3ptrvector.h>
#  include <q3valuestack.h>
#  include <QPicture>
#  include <QDebug>
#endif /* QT_VERSION */
#include <qpixmap.h>
#include <qcursor.h>
#include <qpen.h>
#include <qdesktopwidget.h>
#include <qimage.h>
#include <qfontmetrics.h>
#include <qdialog.h>
#include <qlineedit.h>
#include <qfileinfo.h>
#include <qtextcodec.h>
#include <qdir.h>
#include "TROOT.h"
#include "TMath.h"
#include "TColor.h"
#include "TEnv.h"
#include "TQtApplication.h"
#include "TQtWidget.h"
#include "TGQt.h"
#include "TQtBrush.h"
#include "TQtClientFilter.h"
#include "TQtEventQueue.h"
#include "TQtSymbolCodec.h"
#include "TQtLock.h"
#include "TSystem.h"
#ifdef R__QTWIN32
#  include "TWinNTSystem.h"
#  include "Win32Constants.h"
#  include <Winuser.h>
#else
# ifdef R__QTX11
#  include <X11/Xlib.h>
# endif
#endif
#include "TSysEvtHandler.h"
#include "TQtMarker.h"
#include "TImage.h"
#include "TError.h"
TGQt *gQt=0;
TVirtualX *TGQt::fgTQt = 0; 
#if 1 
   static const char *romanFontName   = "Times New Roman";
   static const char *arialFontName   = "Arial";
   static const char *courierFontName = "Courier New";
#else
   static const char *romanFontName   = "Nimbus Roman No9 L";
   static const char *arialFontName   = "Nimbus Sans L";
   static const char *courierFontName = "Nimbus Mono L ";
   static const char *symbolFontName  = "Standard Symbols L";
#endif      
static QWidget *IsWidget(QPaintDevice *d)
{ return  dynamic_cast<QWidget *>(d);   }
static QPixmap *IsPixmap(QPaintDevice *d)
{ return dynamic_cast<QPixmap *>(d); }
QString TGQt::SetFileName(const QString &fileName)
{
   
   QFileInfo  fi(fileName);
   QString saveFileMoviePattern = 
            fi.dirPath()+"/" + fi.baseName(TRUE)+ "_%04d" + "." + fi.extension(FALSE);
   return saveFileMoviePattern;
}
QString TGQt::GetNewFileName(const QString &fileNamePrototype)
{
   
   TString flN = (const char *)fileNamePrototype;
   gSystem->ExpandPathName(flN);
   QString fileName = (const char *)flN;
   Int_t counter = 0;
   QString formatPattern = SetFileName(fileName);
   while (gSystem->AccessPathName((const char *)fileName)==0) {
      fileName = QString().sprintf(formatPattern,counter++);
   }
   return  fileName;     
}
class TQtEventInputHandler : public TTimer {
protected: 
   TQtEventInputHandler() : TTimer(340) {  }
   static TQtEventInputHandler *gfQtEventInputHandler;
public:
   static TQtEventInputHandler *Instance() {
     if (!gfQtEventInputHandler)
       gfQtEventInputHandler =  new  TQtEventInputHandler();
       gfQtEventInputHandler->Start(240);
       return gfQtEventInputHandler;
   }
   Bool_t Notify()     {
      Timeout();       
      Bool_t ret = gQt->processQtEvents();
      Start(240);
      Reset();
      return  ret;
   }
   Bool_t ReadNotify() { return Notify(); }
};
TQtEventInputHandler *TQtEventInputHandler::gfQtEventInputHandler = 0;
class TQWidgetCollection {
 private:
#if QT_VERSION < 0x40000
   QValueStack<int>         fFreeWindowsIdStack;
   QPtrVector<QPaintDevice> fWidgetCollection;
#else /* QT_VERSION */
   Q3ValueStack<int>         fFreeWindowsIdStack;
   Q3PtrVector<QPaintDevice> fWidgetCollection;
#endif /* QT_VERSION */
   Int_t                    fIDMax;       
   Int_t                    fIDTotalMax;  
protected:
   
   inline  Int_t SetMaxId(Int_t newId)
   {
      fIDMax =  newId;
      fIDTotalMax  = newId>fIDTotalMax ? newId:fIDTotalMax;
      return fIDMax;
   }
 public:
   
   TQWidgetCollection () : fWidgetCollection(20) , fIDMax(-1), fIDTotalMax(-1)
   {
       
       
       int kDefault = 1;
       assert((kNone >= 0) &&  (kDefault > 0 ));
       Int_t firstRange  = kNone;
       Int_t secondRange = kDefault;
       
       for (int i=0;i<secondRange;i++)  {
             if (i == firstRange) continue; 
             fWidgetCollection.insert(i,(QPaintDevice *)(-1));
             fFreeWindowsIdStack.push(i);
       }
       SetMaxId (secondRange);
       fWidgetCollection.insert(kNone,0);
       fWidgetCollection.insert(kDefault,(QPaintDevice *)QApplication::desktop());
   }
   
   inline Int_t GetFreeId(QPaintDevice *device) {
      Int_t Id = 0;
      if (!fFreeWindowsIdStack.isEmpty() ) {
         Id = fFreeWindowsIdStack.pop();
         if (Id > fIDMax ) SetMaxId ( Id );
      } else {
         Id = fWidgetCollection.count()+1;
         if (Id >= int(fWidgetCollection.size()) )
              fWidgetCollection.resize(2*Id);
         assert(fIDMax <= Id  );
         SetMaxId ( Id );
      }
      fWidgetCollection.insert(Id,device);
      
      return Id;
   }
   
   inline Int_t RemoveByPointer(QPaintDevice *device)
   {
      
      Int_t intWid = kNone;             
      if ((ULong_t) device != (ULong_t) -1) {
          intWid = find( device);
          if ( intWid != -1 && fWidgetCollection.take(intWid)) {
             fFreeWindowsIdStack.push(intWid);
             if (fIDMax == intWid) SetMaxId(--fIDMax);
          } else {
             intWid = kNone;
          }
      }
      return intWid;
   }
   
   inline const QPaintDevice *DeleteById(Int_t Id)
   {
     QPaintDevice *device = fWidgetCollection[Id];
     if (device) {
        delete fWidgetCollection.take(Id);
        fFreeWindowsIdStack.push(Id);
        if (fIDMax == Id) SetMaxId(--fIDMax);
     }
     return device;
   }
   
   inline uint count() const { return fWidgetCollection.count();}
   
   inline uint MaxId() const { return fIDMax;}
   
   inline uint MaxTotalId() const { return fIDTotalMax;}
   
   inline int find(const QPaintDevice *device, uint i=0) const
         { return fWidgetCollection.find(device,i); }
   
   inline QPaintDevice *operator[](int i) const {return fWidgetCollection[i];}
};
TQWidgetCollection *fWidgetArray = 0;
QPaintDevice *TGQt::iwid(Window_t wid)
{
   
   QPaintDevice *topDevice = 0;
   if ( wid != kNone )   {
       topDevice = (wid == kDefault) ?
              (QPaintDevice *)QApplication::desktop()
             :
              (QPaintDevice*)wid;
   }
   return topDevice;
}
Int_t         TGQt::iwid(QPaintDevice *wid)
{
   
   Int_t intWid = kNone;
       
   if ((ULong_t) wid == (ULong_t) -1) intWid = -1;
   else {
      intWid = fWidgetArray->find(wid);
      assert(intWid != -1);
      
   }
   return intWid;
}
QPaintDevice *TGQt::iwid(Int_t wid)
{
   
   QPaintDevice *topDevice = 0;
   if (0 <= wid && wid <= int(fWidgetArray->MaxId()) )
     topDevice = (*fWidgetArray)[wid];
	else {
     assert(wid <= Int_t(fWidgetArray->MaxTotalId()));
     
     
     
   }
   return topDevice;
}
QWidget      *TGQt::winid(Window_t id)
{
   
   return (id != kNone)? TGQt::wid(id)->topLevelWidget():0;
}
Window_t    TGQt::wid(TQtClientWidget *widget)
{
   return rootwid(widget);
}
Window_t    TGQt::rootwid(QPaintDevice *dev)
{
   return Window_t(dev);
}
QWidget      *TGQt::wid(Window_t id)
{
   
   QPaintDevice *dev = 0;
   if (id == (Window_t)kNone || id == (unsigned int)(-1) ) return (QWidget *)dev;
   if ( id <= fWidgetArray->MaxId() )
      dev = (*fWidgetArray)[id];
   else
      dev = (QPaintDevice *)id;
#if 0
     if ( dev->devType() != QInternal::Widget) {
        fprintf(stderr," %s %i type=%d QInternal::Widget = %d id =%x id = %d\n", "TGQt::wid", __LINE__
           , dev->devType()
           , QInternal::Widget, id, id );
   }
#endif
   assert(dev->devType() == QInternal::Widget);
   return (QWidget *)dev;
}
void TGQt::PrintEvent(Event_t &ev)
{
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   fprintf(stderr,"----- Window %p %s\n", TGQt::wid(ev.fWindow),(const char *)TGQt::wid(ev.fWindow)->name());
   fprintf(stderr,"event type =  %x, key or button code %d \n", ev.fType, ev.fCode);
   fprintf(stderr,"fX, fY, fXRoot, fYRoot = %d %d  :: %d %d\n", ev.fX, ev.fY,ev.fXRoot, ev.fYRoot);
}
static float CalibrateFont()
{
    
    
    static float fontCalibFactor = -1;
    if (fontCalibFactor  < 0 ) {
       const char * envFactor = gSystem->Getenv("ROOTFONTFACTOR");
       bool ok=false;
       if (envFactor && envFactor[0])
          fontCalibFactor= QString(envFactor).toFloat(&ok);
       if (!ok) {
          bool  italic = TRUE;
          long  bold   = 5;
          QString fontName = 
#if QT_VERSION < 0x40000
            romanFontName;
#else          
            "Nimbus Roman No9 L";
#endif           
          QFont pattern;
          pattern.setWeight(bold*10);
          pattern.setItalic(italic);
          pattern.setFamily(fontName);
          pattern.setPixelSize(12);
          int w,h;
          QFontMetrics metrics(pattern);
          w = metrics.width("This is a PX distribution");
          h = metrics.height();
         
          float f;
          switch (h) {
             case 12: f = 1.10;  break;
             case 14: f = 0.915; break;
             case 16: f = 0.94;  break;
             case 18: f = 0.92;  break;
             default: f = 1.10;  break;
          }
          fontCalibFactor = f;
       }
    }
    return fontCalibFactor;
}
static inline float FontMagicFactor(float size)
{
   
   static float calibration =0;
   if (calibration == 0) calibration = CalibrateFont();
   return TMath::Max(calibration*size,Float_t(1.0));
}
int TGQt::fgCoinFlag = 0; 
int TGQt::fgCoinLoaded = 0; 
int TGQt::CoinFlag()
{
  
   TQtLock lock;
   int ret = fgCoinFlag;
   return ret;
}
void TGQt::SetCoinFlag(int flag)
{
  
   TQtLock lock;
   fgCoinFlag=flag;
   
}
void TGQt::SetCoinLoaded() {  fgCoinLoaded = 1; }
Int_t TGQt::IsCoinLoaded(){ return fgCoinLoaded;}
class TQtInputHandler : public TFileHandler
{
  protected:
    
    Int_t OpenDisplay()
    {
      Int_t dsp = 0;
      if (GetFd() == -1 &&  (dsp =  gVirtualX->OpenDisplay(0)) ) {
        SetFd(dsp);
        if (gSystem) {
          gSystem->AddFileHandler(this);
          Added(); 
        }
      }
      return GetFd();
    }
  public:
    
    TQtInputHandler(int fd=-1,int mask=1):TFileHandler(fd,mask)
    {
      gXDisplay = this;
      OpenDisplay();
    }
    
    virtual Bool_t  Notify()
    {
      OpenDisplay();
      Event_t evnt;
      if ( qApp->hasPendingEvents ()) {
         gVirtualX->NextEvent(evnt); 
         Notified(); 
         return kTRUE;
      }
      return TFileHandler::Notify();
    }
};
#if ROOT_VERSION_CODE < ROOT_VERSION(5,13,0)
QPixmap *TGQt::MakeIcon(Int_t i)
{
   QPixmap *tempIcon = NULL;
   if (i) {  }
#ifdef R__QTWIN32
   HICON largeIcon[1];
   HICON smallIcon[1];
   HICON icon = ((TWinNTSystem *)gSystem)->GetNormalIcon(i);
#if 0
   int numIcons = ::ExtractIconEx(
    "c:\winnt\explorer.exe",
    0,
    largeIcon,
    smallIcon,
    1);
   if (numIcons > 0)
   {
#endif
   tempIcon =new QPixmap (GetSystemMetrics(SM_CXSMICON),
                          GetSystemMetrics(SM_CYSMICON));
   HDC dc = tempIcon->handle();
   DrawIcon (dc, 0, 0, icon);
#else
# ifdef ROOTICONPATH
   gSystem->ExpandPathName(ROOTICONPATH);
# else
   gSystem->ExpandPathName("$ROOTSYS/icons/");
# endif
#endif
   return tempIcon;
}
#endif
#define NoOperation (QPaintDevice *)(-1)
ClassImp(TGQt)
QString TGQt::RootFileFormat(const char *selector)
{  return RootFileFormat(QString(selector)); }
QString TGQt::RootFileFormat(const QString &selector)
{
   
   
   
   QString saveType;
   QString defExtension[] = {"cpp","cxx","eps","svg","root","pdf","ps","xml"
#if ROOT_VERSION_CODE >= ROOT_VERSION(5,13,0)   
                             ,"gif"
#endif
                             ,"C"};
   UInt_t nExt = sizeof(defExtension)/sizeof(const char *);
    UInt_t i = 0;
   for (i=0; i < nExt; i++) {
      if (selector.contains(defExtension[i],FALSE)) {
         saveType = defExtension[i];
         break;
      }
   }
   if (saveType.contains("C",FALSE)) saveType= "cxx";
   return saveType;
}
QString TGQt::QtFileFormat(const char *selector)
{ return QtFileFormat(QString(selector)); }
QString TGQt::QtFileFormat(const QString &selector)
{
   
   
   
   
   
   
   
   
   
   QString saveType="PNG"; 
   if (!selector.isEmpty())  {
#if QT_VERSION < 0x40000
      for (UInt_t j = 0; j < QImageIO::outputFormats().count(); j++ )
#else /* QT_VERSION */
      QList<QByteArray> formats =  QImageWriter::supportedImageFormats();
      QList<QByteArray>::const_iterator j;
      for (j = formats.constBegin(); j != formats.constEnd(); ++j)
#endif /* QT_VERSION */
      {
#if QT_VERSION < 0x40000
         QString nextFormat =  QImageIO::outputFormats().at( j );
#else /* QT_VERSION */
         QString nextFormat =  *j;
#endif /* QT_VERSION */
         
         QString checkString = selector.contains("jpg",FALSE) ? "JPEG" : selector;
         if (checkString.contains(nextFormat,FALSE) ) {
            saveType = nextFormat;
            break;
         }
      }
      
      
      
      
   }
   return saveType;
}
TQtApplication *TGQt::CreateQtApplicationImp()
{
   
   static TQtApplication *app = 0;
   if (!app) {
      
      static TString argvString (
#ifdef ROOTBINDIR
				 ROOTBINDIR "/root.exe" 
#else
				 "$ROOTSYS/bin/root.exe"
#endif
				 );
      gSystem->ExpandPathName(argvString);
      static char *argv[] = {(char *)argvString.Data()};
      static int nArg = 1;
      app = new TQtApplication("Qt",nArg,argv);
   }
   return app;
}
void TGQt::PostQtEvent(QObject *receiver, QEvent *event)
{
   
   
#if (QT_VERSION < 0x030200)
  QThread::postEvent(receiver,event);
#else
  QApplication::postEvent(receiver,event);
#endif
}
TGQt::TGQt() : TVirtualX(),fDisplayOpened(kFALSE),fQPainter(0),fQClientFilterBuffer(0)
,fCodec(0),fSymbolFontFamily("Symbol")
{
   
   
   fgTQt = this;
   gQt   = this;
   fSelectedBuffer = 0; 
   fSelectedWindow = fPrevWindow = NoOperation;
}
TGQt::TGQt(const char *name, const char *title) : TVirtualX(name,title),fDisplayOpened(kFALSE)
,fQPainter(0),fCursors(kNumCursors),fQClientFilter(0),fQClientFilterBuffer(0),fPointerGrabber(0)
,fCodec(0),fSymbolFontFamily("Symbol")
{
   
   
   fgTQt = this;
   gQt   = this;
   fSelectedBuffer = 0;
   fSelectedWindow = fPrevWindow = NoOperation;
   CreateQtApplicationImp();
   Init();
}
TGQt::~TGQt()
{
   
   
   {  
      TQtLock lock;
      gVirtualX = gGXBatch;
      gROOT->SetBatch();
      delete fQClientFilter;
      delete fQClientFilterBuffer;
      delete fQPainter; fQPainter = 0;
   }
   
   TQtApplication::Terminate();
   
}
Bool_t TGQt::Init(void* )
{
   
   
   fprintf(stderr,"** $Id: TGQt.cxx,v 1.43 2007/06/22 15:02:00 antcheva Exp $ this=%p\n",this);
#if QT_VERSION >= 0x40000
#ifndef R__QTWIN32
   extern void qt_x11_set_global_double_buffer(bool);
#endif   
#endif   
   if(fDisplayOpened)   return fDisplayOpened;
   fSelectedBuffer = fSelectedWindow = fPrevWindow = NoOperation;
   fTextAlignH      = 1;
   fTextAlignV      = 1;
   fTextMagnitude   = 1;
   fCharacterUpX    = 1;
   fCharacterUpY    = 1;
#if QT_VERSION < 0x40000
   fDrawMode        = Qt::CopyROP;
#else /* QT_VERSION */
   fDrawMode        = QPainter::CompositionMode_Source; 
#endif /* QT_VERSION */
   fTextFontModified = 0;
   fTextAlign   = 0;
   fTextSize    = -1;
   fTextFont    = -1;
   fLineWidth   = -1;
   fFillColor   = -1;
   fLineColor   = -1;
   fLineStyle   = -1;
   fMarkerSize  = -1;
   fMarkerStyle = -1;
   
   
   
   
   
   
   
   
   
   fCursors.setAutoDelete(true);
   fCursors.insert(kBottomLeft, new QCursor(Qt::SizeBDiagCursor)); 
   fCursors.insert(kBottomRight,new QCursor(Qt::SizeFDiagCursor)); 
   fCursors.insert(kTopLeft,    new QCursor(Qt::SizeFDiagCursor)); 
   fCursors.insert(kTopRight,   new QCursor(Qt::SizeBDiagCursor)); 
   
   
   
   
   fCursors.insert(kBottomSide, new QCursor(Qt::SizeVerCursor));    
   fCursors.insert(kLeftSide,   new QCursor(Qt::SizeHorCursor));    
   fCursors.insert(kTopSide,    new QCursor(Qt::SizeVerCursor));    
   fCursors.insert(kRightSide,  new QCursor(Qt::SizeHorCursor));    
   fCursors.insert(kMove,       new QCursor(Qt::SizeAllCursor));   
   fCursors.insert(kCross,      new QCursor(Qt::CrossCursor));     
   fCursors.insert(kArrowHor,   new QCursor(Qt::SizeHorCursor));   
   fCursors.insert(kArrowVer,   new QCursor(Qt::SizeVerCursor));   
   fCursors.insert(kHand,       new QCursor(Qt::PointingHandCursor)); 
   fCursors.insert(kRotate,     new QCursor(Qt::ForbiddenCursor)); 
   fCursors.insert(kPointer,    new QCursor(Qt::ArrowCursor));     
   fCursors.insert(kArrowRight, new QCursor(Qt::UpArrowCursor));   
#if QT_VERSION < 0x40000
   fCursors.insert(kCaret,      new QCursor(Qt::IbeamCursor));     
#else /* QT_VERSION */
   fCursors.insert(kCaret,      new QCursor(Qt::IBeamCursor));     
#endif /* QT_VERSION */
   fCursors.insert(kWatch,      new QCursor(Qt::WaitCursor));      
   
   fCursor = kCross;
   
   fQPen     = new QPen;
   fQBrush   = new TQtBrush;
   fQtMarker = new TQtMarker;
   fQFont    = new QFont();
   
   fQClientFilter = new TQtClientFilter();
   
   fFontTextCode = "ISO8859-1";
   const char *default_font =
      gEnv->GetValue("Gui.DefaultFont",  "-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-iso8859-1");
   QApplication::setFont(*(QFont *)LoadQueryFont(default_font));
   
   QString fontName(default_font);
   fFontTextCode = fontName.section('-',13).upper();
   if  ( fFontTextCode.isEmpty() ) fFontTextCode = "ISO8859-1";
   
    QFontDatabase fdb;
    QStringList families = fdb.families();
    Bool_t symbolFontFound = kFALSE;
    Bool_t isXdfSupport = 
#if QT_VERSION < 0x40000          
          kTRUE;
#else
          !gSystem->Getenv("QT_X11_NO_FONTCONFIG");
#endif            
    for ( QStringList::Iterator f = families.begin(); f != families.end(); ++f ) {
        
        if ( (isXdfSupport && (*f).contains(fSymbolFontFamily))
            || (!isXdfSupport && (*f) == fSymbolFontFamily) ) 
        { 
           symbolFontFound = kTRUE; 
           fSymbolFontFamily = *f;
#if QT_VERSION < 0x40000
#else
#endif                                        
           break; 
        }
    }
#if QT_VERSION >= 0x40000
    if (isXdfSupport && !symbolFontFound) {
       QString fontdir =
#ifdef TTFFONTDIR
    		TTFFONTDIR
#else
		  "$ROOOTSYS/fonts"
#endif
        ;
        QString symbolFontFile = fontdir + "/" + QString(fSymbolFontFamily).toLower() + ".ttf";
        symbolFontFound = QFontDatabase::addApplicationFont(symbolFontFile);
    }
#endif                
    if (!symbolFontFound) {       
        fprintf(stderr, "The font \"symbol.ttf\" was not installed yet\n");
         
        fSymbolFontFamily = "Arial";
        fprintf(stderr, " Substitute it with \"%s\"\n",fSymbolFontFamily);
        fprintf(stderr, " Make sure your local \"~/.fonts.conf\" or \"/etc/fonts/fonts.conf\" file points to \""
#ifdef TTFFONTDIR
		TTFFONTDIR
#else
		"$ROOOTSYS/fonts"
#endif
		"\" directory to get the proper support for ROOT TLatex class\n");
        
        new QSymbolCodec();
    }
   
   
   
   fWidgetArray =  new TQWidgetCollection();
   fDisplayOpened = kTRUE;
   TQtEventInputHandler::Instance();
   
   
   if (gSystem->Getenv("QTDIR")) {
      TString qtdir = "$(QTDIR)/include";
      gSystem->ExpandPathName(qtdir);
      TString testQtHeader = qtdir + "/qglobal.h";
      if (!gSystem->AccessPathName((const char *)testQtHeader) ) {
         
         TString incpath= "-I"; incpath+=qtdir;
         gSystem->AddIncludePath((const char*)incpath);
#ifdef R__WIN32
         QString libPath = gSystem->GetLinkedLibs();
         
         TString qtlibdir= "$(QTDIR)"; 
         qtlibdir += QDir::separator(); 
         qtlibdir += "lib";
         gSystem->ExpandPathName(qtlibdir);
         QDir qtdir((const char*)qtlibdir);
         if (qtdir.isReadable ()) {
            QStringList qtLibFile =  qtdir.entryList("qt-mt*.lib");
            if (qtLibFile.count() ) {
               libPath += " -LIBPATH:\"";libPath += qtlibdir;  libPath += "\" "; libPath += qtLibFile.first();
               gSystem->SetLinkedLibs((const char*)libPath);
            }
         } else {
            qWarning(" Can not open the QTDIR %s",(const char*)qtlibdir);
         }
#endif 
      }
   }
   TString newPath = 
# ifdef CINTINCDIR
      CINTINCDIR
# else 
   "$(ROOTSYS)/cint/"
# endif
     ; newPath += "include";
#ifndef R__WIN32
   newPath += ":";
#else
   newPath += ";";
#endif
   newPath += gSystem->GetDynamicPath();
   gSystem->SetDynamicPath(newPath.Data());
   return fDisplayOpened;
}
Int_t TGQt::CreatROOTThread()
{
  return 0;
}
Int_t  TGQt::RegisterWid(QPaintDevice *wid)
{
 
   Int_t id = fWidgetArray->find(wid);
   if (id == -1) id = fWidgetArray->GetFreeId(wid);
   return id;
}
Int_t  TGQt::UnRegisterWid(QPaintDevice *wid)
{
   
   
   return fWidgetArray->RemoveByPointer(wid);
}
Bool_t  TGQt::IsRegistered(QPaintDevice *wid)
{
   
   return fWidgetArray->find(wid) == -1 ? kFALSE : kTRUE;
}
Int_t TGQt::InitWindow(ULong_t window)
{
   
   
   
   
   
   
   TQtWidget *wid    = 0;
   QWidget   *parent = 0;
   if (window <= fWidgetArray->MaxId() )
      parent = dynamic_cast<TQtWidget *> (iwid(int     (window)));
   else {
      QPaintDevice *dev = dynamic_cast<QPaintDevice *>(iwid(Window_t(window)));
      parent = dynamic_cast<QWidget *>(dev);
   }
 
 
   wid = new TQtWidget(parent,"virtualx",Qt::WStyle_NoBorder,FALSE);
   wid->setCursor(*fCursors[kCross]);
   if (parent && gDebug==3) 
   {
      
   }
   Int_t id = fWidgetArray->GetFreeId(wid);
   
   wid->SetDoubleBuffer(1);
  
   return id;
}
Int_t TGQt::OpenPixmap(UInt_t w, UInt_t h)
{
   
   QPixmap *obj =  new QPixmap(w,h);
   
   return fWidgetArray->GetFreeId(obj);
   
}
const QColor &TGQt::ColorIndex(Color_t ic) const
{
   
#ifndef R__QTWIN32
   
   
   const int BIGGEST_RGB_VALUE=255;
   static QColor colorBuffer;
   
   TColor *myColor = gROOT->GetColor(ic);
   if (myColor) {
      colorBuffer.setRgb(  int(myColor->GetRed()  *BIGGEST_RGB_VALUE +0.5)
         ,int(myColor->GetGreen()*BIGGEST_RGB_VALUE +0.5)
         ,int(myColor->GetBlue() *BIGGEST_RGB_VALUE +0.5)
         );
   }
#ifdef QTDEBUG
   else {
      fprintf(stderr," TGQt::%s:%d - Wrong color index %d\n", "TGQt::wid",__LINE__, ic);
   }
#endif
   return colorBuffer;
#else
   const QColor &c = fPallete[ic+ColorOffset];
   return c;
#endif
}
UInt_t TGQt::ExecCommand(TGWin32Command* )
{
   
   fprintf(stderr,"** Error **:  TGQt::ExecCommand no implementation\n");
   return 0;
}
void TGQt::SetDoubleBufferOFF()
{
   
   fprintf(stderr,"** Error **:  TGQt::SetDoubleBufferOFF no implementation\n");
}
void TGQt::SetDoubleBufferON()
{
   
   fprintf(stderr,"** Error **:  TGQt::SetDoubleBufferON no implementation\n");
}
void TGQt::GetPlanes(Int_t &nplanes){
   nplanes  = QPixmap::defaultDepth();
}
void  TGQt::ClearWindow()
{
   
   if (fSelectedWindow && fSelectedWindow != NoOperation)
   {
      if (IsWidget(fSelectedWindow)) 
         ((TQtWidget *)fSelectedWindow)->Erase();
      else if (IsPixmap(fSelectedWindow) )
         ((QPixmap *)fSelectedWindow)->fill();
      else 
         fQPainter->eraseRect(GetQRect(*fSelectedWindow));
   }
}
void  TGQt::ClosePixmap()
{
   
   DeleteSelectedObj();
}
void  TGQt::CloseWindow()
{
   
   DeleteSelectedObj();
}
void  TGQt::DeleteSelectedObj()
{
    
  End();
  if (fSelectedWindow->devType() == QInternal::Widget) {
     TQtWidget *canvasWidget = dynamic_cast<TQtWidget *>(fSelectedWindow);
     QWidget *wrapper = 0;
     if (canvasWidget && (wrapper=canvasWidget->GetRootID())) {
        wrapper->hide();
        DestroyWindow(rootwid(wrapper) );
     } else {
        
        if(UnRegisterWid(fSelectedWindow) != (Int_t) kNone) {
           ((QWidget *)fSelectedWindow)->hide();
           ((QWidget *)fSelectedWindow)->close(true);
        }
     }
  } else {
     UnRegisterWid(fSelectedWindow);
     delete  fSelectedWindow;
  }
  fSelectedBuffer = fSelectedWindow = 0;
  fPrevWindow     = 0;
}
QRect TGQt::GetQRect(QPaintDevice &dev)
{
   
  QRect res;
  switch (dev.devType()) {
  case QInternal::Widget:
    res = ((TQtWidget*)&dev)->rect();
    break;
  case QInternal::Pixmap: {
     TQtWidgetBuffer *pxmBuffer = dynamic_cast<TQtWidgetBuffer *>(&dev);
     if (pxmBuffer) res = pxmBuffer->rect();
     else           res = ((QPixmap *)&dev)->rect();
     break;
                          }
  case QInternal::Picture:
     res = ((QPicture *)&dev)->boundingRect();
     break;
  case QInternal::Printer:
#if QT_VERSION < 0x40000
  case QInternal::UndefinedDevice:
#else /* QT_VERSION */
 
#endif /* QT_VERSION */
     break;
  default:
     assert(0);
     break;
  };
  return res;
}
void  TGQt::CopyPixmap(int wid, int xpos, int ypos)
{
   
   if (!wid || (wid == -1) ) return;
   QPaintDevice *dev = iwid(wid);
   assert(dev->devType() == QInternal::Pixmap);
   QPixmap *src = (QPixmap *)dev;
     
     
     
   if (fSelectedWindow )
   {
      
      QPaintDevice *dst = fSelectedBuffer ? fSelectedBuffer : fSelectedWindow;
      bool isPainted = dst->paintingActive ();
      if (isPainted) End();
#if QT_VERSION < 0x40000
      QRect sr = src->rect();
      bitBlt ( dst,QPoint(xpos,ypos),src,sr,Qt::CopyROP); 
#else /* QT_VERSION */
      {
        QPainter paint(dst);
        paint.drawPixmap(xpos,ypos,*src);
      }
     
#endif /* QT_VERSION */
      if (isPainted) Begin();
      Emitter()->EmitPadPainted(src);
      if (!fSelectedBuffer && (fSelectedWindow->devType() == QInternal::Widget ) )
      {
        TQtWidget *w = (TQtWidget *)fSelectedWindow;
        w->EmitCanvasPainted();
      }
   }
}
void TGQt::CopyPixmap(const QPixmap &src, Int_t xpos, Int_t ypos) 
{
   
   if (fSelectedWindow )
   {
      
      QPaintDevice *dst = fSelectedBuffer ? fSelectedBuffer : fSelectedWindow;
      bool isPainted = dst->paintingActive ();
      if (isPainted) End();
#if QT_VERSION < 0x40000
      QRect sr = src.rect();
      bitBlt ( dst,QPoint(xpos,ypos),&src,sr,Qt::CopyROP ); 
#else /* QT_VERSION */
      {
        QPainter paint(dst);
        
        
        paint.drawPixmap(xpos,ypos,src);
      }
     
#endif /* QT_VERSION */
      if (isPainted) Begin();
   }
}
void TGQt::CreateOpenGLContext(int wid)
{
 
 
 
  if (!wid || (wid == -1) ) return;
#ifdef QtGL
    if (!wid)
    {
      SafeCallWin32
         ->W32_CreateOpenGL();
    }
    else
    {
      SafeCallW32(((TQtSwitch *)wid))
         ->W32_CreateOpenGL();
    }
#endif
}
void TGQt::DeleteOpenGLContext(int wid)
{
  
  if (!wid || (wid == -1) ) return;
#ifdef QtGL
    if (!wid)
    {
      SafeCallWin32
         ->W32_DeleteOpenGL();
    }
    else
    {
      SafeCallW32(((TQtSwitch *)wid))
         ->W32_DeleteOpenGL();
    }
#endif
}
void  TGQt::DrawBox(int x1, int y1, int x2, int y2, EBoxMode mode)
{
   
   
   
#if QT_VERSION < 0x40000
   static const int Q3=1;
#else  
   
   
   
   
 
   static const int Q3=0;
#endif   
   TQtLock lock;
   if (fSelectedWindow)
   {
      fQPainter->save();
      
      if (mode == kHollow)
      {
         fQPainter->setBrush(Qt::NoBrush);
         fQPainter->drawRect(x1,y2,x2-x1+Q3,y1-y2+Q3);
      } else {
         if (fQBrush->style() != Qt::SolidPattern)
            fQPainter->setPen(fQBrush->GetColor());  
#ifdef R__WIN32
         if ( 3000 <= fQBrush->GetStyle() && fQBrush->GetStyle() < 4000) {
            QPixmap &px = *fQBrush->pixmap();
            px.fill(fQBrush->GetColor());
            fQPainter->drawTiledPixmap(x1,y2,x2-x1+1,y1-y2+1, px);
         } else 
#endif
           fQPainter->fillRect(x1,y2,x2-x1+1,y1-y2+1,*fQBrush);
      }
      fQPainter->restore();
   }
}
void  TGQt::DrawCellArray(int x1, int y1, int x2, int y2, int nx, int ny, int *ic)
{
   
   
   
   
   
   
   
   
   
   TQtLock lock;
   if (fSelectedWindow)
   {
      fQPainter->save();
      int i,j,icol,ix,w,h,current_icol,lh;
      current_icol = -1;
      w            = TMath::Max((x2-x1)/(nx),1);
      h            = TMath::Max((y1-y2)/(ny),1);
      lh           = y1-y2;
      ix           = x1;
      if (w+h == 2)
      {
         
         for ( i=x1; i<x1+nx; i++){
            for (j = 0; j<ny; j++){
               icol = ic[i+(nx*j)];
               if (current_icol != icol) {
                  current_icol = icol;
                  fQPainter->setPen(ColorIndex(current_icol));
               }
               fQPainter->drawPoint(i,y1-j);
            }
         }
      }
      else
      {
         
         QRect box(x1,y1,w,h);
         for ( i=0; i<nx; i++ ) {
            for ( j=0; j<ny; j++ ) {
               icol = ic[i+(nx*j)];
               if(icol != current_icol){
                  current_icol = icol;
                  fQPainter->setBrush(ColorIndex(current_icol));
               }
               fQPainter->drawRect(box);
               box.moveBy(0,-h);   
            }
            box.moveBy(w,lh);
         }
      }
      fQPainter->restore();
   }
}
void  TGQt::DrawFillArea(int n, TPoint *xy)
{
   
   
   
   TQtLock lock;
   if (fSelectedWindow && n>0)
   {
      fQPainter->save();
      if (fQBrush->style() == Qt::SolidPattern)
         fQPainter->setPen(Qt::NoPen);
      else 
         fQPainter->setPen(fQBrush->GetColor());      
#if QT_VERSION < 0x40000
      QPointArray qtPoints(n);
#else /* QT_VERSION */
      Q3PointArray qtPoints(n);
#endif /* QT_VERSION */
      TPoint *rootPoint = xy;
      for (int i =0;i<n;i++,rootPoint++)
         qtPoints.setPoint(i,rootPoint->fX,rootPoint->fY);
#ifdef R__WIN32
      if ( 3000 <= fQBrush->GetStyle() && fQBrush->GetStyle() < 4000) {
            QPixmap &px = *fQBrush->pixmap();
            px.fill(fQBrush->GetColor());
            QRegion clip(qtPoints);
            fQPainter->setClipRegion(clip);
            fQPainter->drawTiledPixmap(clip.boundingRect (), px);
      } else 
#endif
      fQPainter->drawPolygon(qtPoints);
      fQPainter->restore();
   }
}
void  TGQt::DrawLine(int x1, int y1, int x2, int y2)
{
   
   
   
  TQtLock lock;
  if (fSelectedWindow) fQPainter->drawLine(x1,y1,x2,y2);
}
void  TGQt::DrawPolyLine(int n, TPoint *xy)
{
   
   
   
  TQtLock lock;
  if (fSelectedWindow)  {
#if QT_VERSION < 0x40000
    QPointArray qtPoints(n);
#else /* QT_VERSION */
    Q3PointArray qtPoints(n);
#endif /* QT_VERSION */
    TPoint *rootPoint = xy;
    for (int i =0;i<n;i++,rootPoint++)
       qtPoints.setPoint(i,rootPoint->fX,rootPoint->fY);
    fQPainter->drawPolyline(qtPoints);
  }
}
void  TGQt::DrawPolyMarker(int n, TPoint *xy)
{
   
   
   
   TQtLock lock;
   if (fSelectedWindow)
   {
      fQPainter->save();
      TQtMarker *CurMarker = fQtMarker;
      
      const QColor &mColor  = ColorIndex(fMarkerColor);
      if( CurMarker->GetNumber() <= 0 )
      {
         fQPainter->setPen(mColor);
#if QT_VERSION < 0x40000
         QPointArray qtPoints(n);
#else /* QT_VERSION */
         Q3PointArray qtPoints(n);
#endif /* QT_VERSION */
         TPoint *rootPoint = xy;
         for (int i=0;i<n;i++,rootPoint++)
            qtPoints.setPoint(i,rootPoint->fX,rootPoint->fY);
         fQPainter->drawPoints(qtPoints);
      } else {
         int r = CurMarker->GetNumber()/2;
         fQPainter->setPen(mColor);
         switch (CurMarker -> GetType())
         {
         case 1:
         case 3:
         default:
            fQPainter->setBrush(mColor);
            break;
         case 0:
         case 2:
            fQPainter->setBrush(Qt::NoBrush);
            break;
         case 4:
            break;
         }
         for( int m = 0; m < n; m++ )
         {
            int i;
            switch( CurMarker->GetType() )
            {
            case 0:        
            case 1:        
               fQPainter->drawEllipse(xy[m].fX-r, xy[m].fY-r, 2*r, 2*r);
               break;
            case 2:        
            case 3:        
               {
#if QT_VERSION < 0x40000
                  QPointArray &mxy = fQtMarker->GetNodes();
#else /* QT_VERSION */
                  Q3PointArray &mxy = fQtMarker->GetNodes();
#endif /* QT_VERSION */
                  QPoint delta(xy[m].fX,xy[m].fY);
                  for( i = 0; i < CurMarker->GetNumber(); i++ )
                  {
                     mxy[i] += delta;
                  }
                  fQPainter->drawPolygon(mxy);
                  for( i = 0; i < CurMarker->GetNumber(); i++ )
                  {
                     mxy[i] -= delta;
                  }
                  break;
               }
            case 4:        
               {
#if QT_VERSION < 0x40000
                  QPointArray &mxy = fQtMarker->GetNodes();
#else /* QT_VERSION */
                  Q3PointArray &mxy = fQtMarker->GetNodes();
#endif /* QT_VERSION */
                  QPoint delta(xy[m].fX,xy[m].fY);
                  for( i = 0; i < CurMarker->GetNumber(); i++ )
                  {
                     mxy[i] += delta;
                  }
                  fQPainter->drawLineSegments(mxy);
                  for( i = 0; i < CurMarker->GetNumber(); i++ )
                  {
                     mxy[i] -= delta;
                  }
                  break;
               }
            }
         }
      }
      fQPainter->restore();
   }
}
void  TGQt::DrawText(int x, int y, float angle, float mgn, const char *text, TVirtualX::ETextMode )
{
   
   
   
   
   
   
   
   
   
#if 0
   if (fROOTFont.lfEscapement != (LONG) fTextAngle*10)  {
      fTextFontModified=1;
      fROOTFont.lfEscapement   = (LONG) fTextAngle*10;
   }
#endif
   
   if (text && text[0]) {
      TQtLock lock;
      Int_t tsize = (Int_t)(fTextSize+0.5);
      if (TMath::Abs(mgn-1) >0.05)  fQFont->setPixelSizeFloat(mgn*FontMagicFactor(tsize));
      UpdateFont();
      fQPainter->save();
      fQPainter->setPen(ColorIndex(fTextColor));
      fQPainter->setBrush(ColorIndex(fTextColor));
      QFontMetrics metrics(*fQFont);
      QRect bRect = metrics.boundingRect(text);
      fQPainter->translate(x,y);
      if (TMath::Abs(angle) > 0.1 ) fQPainter->rotate(-angle);
      int dx =0; int dy =0;
      switch( fTextAlignH ) {
           case 2: dx = -bRect.width()/2;                    
              break;
           case 3: dx = -bRect.width();                      
              break;
      };
      switch( fTextAlignV ) {
          case 2: dy = bRect.height()/2 - metrics.descent(); 
             break;
          case 3: dy = bRect.height()   - metrics.descent(); 
      };
      fQPainter->drawText (dx, dy, GetTextDecoder()->toUnicode (text));
      fQPainter->restore();
   }
}
void  TGQt::GetCharacterUp(Float_t &chupx, Float_t &chupy)
{
   
   TQtLock lock;
   chupx = fCharacterUpX;
   chupy = fCharacterUpY;
}
QPaintDevice *TGQt::GetDoubleBuffer(QPaintDevice *dev)
{
   
   QPaintDevice *buffer = 0;
   if (dev) {
       TQtWidget *widget = dynamic_cast<TQtWidget *>(dev);
       buffer = widget && widget->IsDoubleBuffered() ? &widget->GetBuffer() : 0;
    }
    return buffer;
}
Int_t  TGQt::GetDoubleBuffer(Int_t wid)
{
   
   
   if (wid == -1 || wid == kDefault ) return 0;
   assert(0);
   QPaintDevice *dev = iwid(wid);
   TQtWidget *widget = dynamic_cast<TQtWidget *>(dev);
   return  Int_t(widget && widget->IsDoubleBuffered());
}
void  TGQt::GetGeometry(int wid, int &x, int &y, unsigned int &w, unsigned int &h)
{
   
   QRect devSize(0,0,0,0);
   if( wid == -1 || wid == 0 || wid == kDefault)
   {
      QDesktopWidget *d = QApplication::desktop();
      devSize.setWidth (d->width() );
      devSize.setHeight(d->height());
   } else {
      QPaintDevice  *dev = iwid(wid);
      if (dev) {
         if ( dev->devType() == QInternal::Widget) {
            TQtWidget &thisWidget = *(TQtWidget *)dev;
            if (thisWidget.GetRootID() ) {
               
               devSize = thisWidget.parentWidget()->geometry();
            } else{
               devSize = thisWidget.geometry();
            }
            devSize.moveTopLeft(thisWidget.mapToGlobal(QPoint(0,0)));
         } else {
            devSize = GetQRect(*dev);
         }
      }
   }
   x = devSize.left();
   y = devSize.top();
   w = devSize.width();
   h = devSize.height();
 
}
const char *TGQt::DisplayName(const char *){ return "localhost"; }
ULong_t  TGQt::GetPixel(Color_t cindex)
{
   
   
   ULong_t rootPixel = 0;
   QColor color = ColorIndex(cindex);
#ifdef R__WIN32
   rootPixel =                    ( color.blue () & 255 );
   rootPixel = (rootPixel << 8) | ( color.green() & 255 ) ;
   rootPixel = (rootPixel << 8) | ( color.red  () & 255 );
#else
   rootPixel =                    ( color.red () & 255 );
   rootPixel = (rootPixel << 8) | ( color.green() & 255 ) ;
   rootPixel = (rootPixel << 8) | ( color.blue  () & 255 );
#endif   
   return rootPixel;
}
void  TGQt::GetRGB(int index, float &r, float &g, float &b)
{
   
   const float BIGGEST_RGB_VALUE=255.;
   r = g = b = 0;
   TQtLock lock;
   if (fSelectedWindow != NoOperation) {
      int c[3];
      const QColor &color = fPallete[index];
      color.rgb(&c[0],&c[1],&c[2]);
      r = c[0]/BIGGEST_RGB_VALUE;
      g = c[1]/BIGGEST_RGB_VALUE;
      b = c[2]/BIGGEST_RGB_VALUE;
   }
}
const QTextCodec *TGQt::GetTextDecoder()
{
   static  QTextCodec  *fGreekCodec = 0;
   QTextCodec  *codec = 0;
   if (!fCodec) {
      fCodec =  QTextCodec::codecForName(fFontTextCode); 
      if (!fCodec)
         fCodec=QTextCodec::codecForLocale();
      else
         QTextCodec::setCodecForLocale(fCodec);
   }
   codec = fCodec;
   if (fTextFont/10 == 12 ) {
        
      if (!fGreekCodec) {
         if (QString(fSymbolFontFamily).contains("Symbol")) {
            fGreekCodec = (fFontTextCode == "ISO8859-1") ? fCodec:
                          QTextCodec::codecForName("ISO8859-1"); 
         } else {
            fGreekCodec  = QTextCodec::codecForName("symbol"); 
         }
      }
      if (fGreekCodec) codec=fGreekCodec;
   }
   return codec;
}
Float_t      TGQt::GetTextMagnitude(){return fTextMagnitude;}
void         TGQt::SetTextMagnitude(Float_t mgn){ fTextMagnitude = mgn;}
void  TGQt::GetTextExtent(unsigned int &w, unsigned int &h, char *mess)
{
   
   
   
   
   TQtLock lock;
   if (fQFont) {
#if QT_VERSION < 0x40000
      QSize textSize = QFontMetrics(*fQFont).size(Qt::SingleLine,GetTextDecoder()->toUnicode(mess)) ;
#else /* QT_VERSION */
      QSize textSize = QFontMetrics(*fQFont).size(Qt::TextSingleLine,GetTextDecoder()->toUnicode(mess)) ;
#endif /* QT_VERSION */
      w = textSize.width() ;
      h = (unsigned int)(textSize.height());
   }
}
Bool_t  TGQt::HasTTFonts() const {return kTRUE;}
void  TGQt::MoveWindow(Int_t wid, Int_t x, Int_t y)
{
   
   
   
   
   if (wid != -1 && wid != 0 && wid != kDefault)
   {
      QPaintDevice *widget = iwid(wid);
      assert(widget->devType() == QInternal::Widget );
      ((TQtWidget *)widget)->move(x,y);
   }
}
void  TGQt::PutByte(Byte_t )
{ 
}
void  TGQt::QueryPointer(int &ix, int &iy){
   
	if (ix*iy){;}
}
Pixmap_t TGQt::ReadGIF(Int_t x0, Int_t y0, const char *file, Window_t id)
{
   
   
   Int_t thisId = 0;
   QPixmap *pix = new QPixmap( QString (file) );
   if ( pix->isNull () ) { delete pix; pix = 0;         }
   else {
      thisId=fWidgetArray->GetFreeId(pix);
      if (!id ) { CopyPixmap(thisId,x0,y0); fWidgetArray->DeleteById(thisId); thisId = 0;}
   }
   return thisId;
}
Int_t  TGQt::RequestLocator(Int_t , Int_t , Int_t &, Int_t &)
{
   
   return 0;
}
  class requestString : public QDialog {
  public:
    QString   fText;
    QLineEdit fEdit;
    requestString(const char *text="") : QDialog(0,0
          , TRUE,Qt::WStyle_Customize | Qt::WStyle_NoBorder|Qt::WStyle_StaysOnTop | Qt::WType_Popup)
          , fText(text),fEdit(this)
    {
       setBackgroundMode(Qt::NoBackground);
       connect(&fEdit,SIGNAL( returnPressed () ), this, SLOT( accept() ));
    }
    ~requestString(){;}
  };
Int_t  TGQt::RequestString(int x, int y, char *text)
{
  int  res = QDialog::Rejected;
  if (fSelectedWindow->devType() == QInternal::Widget ) {
     TQtWidget *w = (TQtWidget *)fSelectedWindow;
     static requestString reqDialog;
     reqDialog.fEdit.setText(QString(text).stripWhiteSpace());
     int yFrame = reqDialog.frameGeometry().height() - reqDialog.geometry().height() + reqDialog.fontMetrics().height();
     reqDialog.move(w->mapToGlobal(QPoint(x,y-yFrame)));
     if (QClientFilter() && QClientFilter()->PointerGrabber() ) {
        
        QClientFilter()->PointerGrabber()->DisactivateGrabbing();
     }
     res = reqDialog.exec();
     if (res == QDialog::Accepted ) {
        
        Font_t textFontSave =  fTextFont;
        fTextFont = 62;
#if QT_VERSION < 0x40000
        QCString r = GetTextDecoder()->fromUnicode(reqDialog.fEdit.text());
#else /* QT_VERSION */
        Q3CString r = GetTextDecoder()->fromUnicode(reqDialog.fEdit.text());
#endif /* QT_VERSION */
        qstrcpy(text, (const char *)r);
        
        fTextFont = textFontSave;
     }
     reqDialog.hide();
     if (QClientFilter() && QClientFilter()->PointerGrabber()) {
        
        QClientFilter()->PointerGrabber()->ActivateGrabbing();
     }
  }
  return res == QDialog::Accepted ? 1 : 0;
}
void  TGQt::RescaleWindow(int wid, UInt_t w, UInt_t h)
{
   
   
   
   
   TQtLock lock;
   if (wid && wid != -1 && wid != kDefault )
   {
      QPaintDevice *widget = iwid(wid);
      if (widget->devType() == QInternal::Widget )
      {
         if (QSize(w,h) != ((TQtWidget *)widget)->size()) {
            if (((TQtWidget *)widget)->paintingActive() ) End();
            
            ((TQtWidget *)widget)->resize(w,h);
         }
      }
   }
}
Int_t  TGQt::ResizePixmap(int wid, UInt_t w, UInt_t h)
{
   
   
   
   TQtLock lock;
   if (wid && wid != -1 && wid != kDefault )
   {
      QPaintDevice *pixmap = iwid(wid);
      if (pixmap->devType() == QInternal::Pixmap )
      {
         if (QSize(w,h) != ((QPixmap *)pixmap)->size()) {
            bool paintStatus = pixmap->paintingActive ();
            if (paintStatus ) End();
            ((QPixmap *)pixmap)->resize(w,h);
            ((QPixmap *)pixmap)->fill();
            
            if (paintStatus) Begin();
         }
      }
   }
   return 1;
}
void  TGQt::ResizeWindow(int )
{
   
   
   
   return;
}
void   TGQt::SelectPixmap(Int_t qpixid){ SelectWindow(qpixid);}
void  TGQt::SelectWindow(int wid)
{
   
   
   
   
   if (wid == -1 || wid == (int) kNone) {
       fSelectedBuffer=0; fSelectedWindow = NoOperation;
      
   } else {
      QPaintDevice *dev = iwid(wid);
      if (!dev) {
         fSelectedBuffer=0; fSelectedWindow = NoOperation;
      } else {
         QPixmap *offScreenBuffer = (QPixmap *)GetDoubleBuffer(dev);
         if ((dev == fSelectedWindow) && !( (fSelectedBuffer==0) ^ (offScreenBuffer == 0) ) ) return;
         fPrevWindow     = fSelectedWindow;
         if (wid == -1 || wid == (int) kNone) { fSelectedBuffer=0; fSelectedWindow = NoOperation; }
         else {
            fSelectedWindow = dev;
            fSelectedBuffer = offScreenBuffer; 
         }
      }
   }
   if (fPrevWindow && fPrevWindow != (void *)-1 && (fWidgetArray->find(fPrevWindow) != -1) )  {
      End();
   }
   if (fSelectedWindow && (fSelectedWindow != NoOperation)) {
      Begin();
   }
}
void  TGQt::SetCharacterUp(Float_t chupx, Float_t chupy)
{
   
   TQtLock lock;
   if (chupx == fCharacterUpX  && chupy == fCharacterUpY) {
      
      return;
   }
   if      (chupx == 0  && chupy == 0)  fTextAngle = 0;
   else if (chupx == 0  && chupy == 1)  fTextAngle = 0;
   else if (chupx == -1 && chupy == 0)  fTextAngle = 90;
   else if (chupx == 0  && chupy == -1) fTextAngle = 180;
   else if (chupx == 1  && chupy ==  0) fTextAngle = 270;
   else {
      fTextAngle = ((TMath::ACos(chupx/TMath::Sqrt(chupx*chupx +chupy*chupy))*180.)/3.14159)-90;
      if (chupy < 0) fTextAngle = 180 - fTextAngle;
      if (TMath::Abs(fTextAngle) < 0.01) fTextAngle = 0;
   }
   fCharacterUpX = chupx;
   fCharacterUpY = chupy;
}
void  TGQt::SetClipOFF(Int_t )
{
   
   
   
}
void  TGQt::SetClipRegion(int wid, int x, int y, UInt_t w, UInt_t h)
{
   
   
   
   
   QRect rect(x,y,w,h);
   TQtLock lock;
   fClipMap.replace(iwid(wid),rect);
   if (fSelectedWindow == iwid(wid) && fSelectedWindow->paintingActive())
   {
      UpdateClipRectangle();
   }
}
void  TGQt::SetCursor(Int_t wid, ECursor cursor)
{
   
   fCursor = cursor;
   if (wid && wid != -1 && wid != kDefault)
   {
      QPaintDevice *widget = iwid(wid);
      if ( TQtWidget *w = (TQtWidget *)IsWidget(widget) )
         w->setCursor(*fCursors[fCursor]);
   }
}
void  TGQt::SetDoubleBuffer(int wid, int mode)
{
   
   
   
   
   
   if (wid == -1 && wid == kDefault) return;
   QPaintDevice *dev = iwid(wid);
   if ( TQtWidget *widget = (TQtWidget *)IsWidget(dev) ) {              
      widget->SetDoubleBuffer(mode);
      
#if QT_VERSION >= 0x40000
 
#endif      
   }
}
void  TGQt::SetDrawMode(TVirtualX::EDrawMode mode)
{
   
   
   
#if QT_VERSION < 0x40000
   Qt::RasterOp newMode = Qt::CopyROP;
#else /* QT_VERSION */
   QPainter::CompositionMode newMode = QPainter::CompositionMode_Source;
#endif /* QT_VERSION */
   switch (mode) {
#if QT_VERSION < 0x40000
    case kCopy:   newMode = Qt::CopyROP; break;
    case kXor:    newMode = Qt::XorROP;  break;
    case kInvert: newMode = Qt::NotROP;  break;
    default:      newMode = Qt::CopyROP; break;
#else /* QT_VERSION */
    case kCopy:   newMode = QPainter::CompositionMode_Source; break;
    case kXor:    newMode = QPainter::CompositionMode_Xor;  break;
    case kInvert: newMode = QPainter::CompositionMode_Destination;  break;
    default:      newMode = QPainter::CompositionMode_Source; break;
#endif /* QT_VERSION */
   };
   if (newMode != fDrawMode)
   {
      fDrawMode = newMode;
#if QT_VERSION < 0x40000
      if (fQPainter->isActive()) { fQPainter->setRasterOp(fDrawMode); }
#else /* QT_VERSION */
      if (fQPainter->isActive() && (fQPainter->device()->devType() ==  QInternal::Image )) 
      {
         fQPainter->setCompositionMode(fDrawMode);
     }
#endif /* QT_VERSION */
     
   }
}
void  TGQt::SetFillColor(Color_t cindex)
{
   
   if (fFillColor != cindex )
   {
      fFillColor = cindex;
      if (fFillColor != -1) {
         fQBrush->SetColor(ColorIndex(cindex));
         UpdateBrush();
      }
   }
}
void  TGQt::SetFillStyle(Style_t fstyle)
{
   
   
   
   
  
  
  
  
   
   if (fFillStyle != fstyle)
   {
      fFillStyle = fstyle;
      if (fFillStyle != -1) {
         Int_t style = fstyle/1000;
         Int_t fasi  = fstyle%1000;
         fQBrush->SetStyle(style,fasi);
         UpdateBrush();
      }
   }
}
void TGQt::SetFillStyleIndex( Int_t style, Int_t fasi )
{
   
   SetFillStyle(1000*style + fasi);
}
void  TGQt::SetLineColor(Color_t cindex)
{
  if (fLineColor != cindex) {
    fLineColor = cindex;
    if (fLineColor >= 0) {
      fQPen->setColor(ColorIndex(fLineColor));
      UpdatePen();
    }
  }
}
void  TGQt::SetLineType(int n, int*dash)
{
  if (n == 0 ) n = -1; 
  if (n < 0) {
    Qt::PenStyle styles[] = {
      Qt::NoPen          
     ,Qt::SolidLine      
     ,Qt::DashLine       
     ,Qt::DotLine        
     ,Qt::DashDotLine    
     ,Qt::DashDotDotLine 
    };
    int l = -n;
    if (l > int(sizeof(styles)/sizeof(Qt::PenStyle)) ) l = 1; 
    fQPen->setStyle(styles[l]);
  } 
  else if (dash) {
#if QT_VERSION >= 0x40000
     
     QVector<qreal> dashes;
     int i;
     for (i=0;i<n;i++) dashes << dash[i];
     fQPen->setDashPattern(dashes);
#endif  
  }
  UpdatePen();          
}
void  TGQt::SetLineStyle(Style_t linestyle)
{
   if (fLineStyle != linestyle) { 
      fLineStyle = linestyle;
      SetLineType(-linestyle, NULL);
   }
}
void  TGQt::SetLineWidth(Width_t width)
{
   
   
   
   
   if (width==1) width =0;
   if (fLineWidth != width) {
      fLineWidth = width;
      if (fLineWidth >= 0 ) {
         fQPen->setWidth(fLineWidth);
         UpdatePen();
      }
   }
}
void  TGQt::SetMarkerColor( Color_t cindex)
{
   
   
   
   
   if (fMarkerColor != cindex) fMarkerColor = cindex;
}
void  TGQt::SetMarkerSize(Float_t markersize)
{
   
   
   
   
   if (markersize != fMarkerSize) {
      fMarkerSize = markersize;
      if (markersize >= 0) {
         SetMarkerStyle(-fMarkerStyle);
      }
   }
}
void  TGQt::SetMarkerStyle(Style_t markerstyle){
   
   
   if (fMarkerStyle == markerstyle) return;
   TPoint shape[15];
   if (markerstyle >= 31) return;
   markerstyle  = TMath::Abs(markerstyle);
   fMarkerStyle = markerstyle;
   Int_t im = Int_t(4*fMarkerSize + 0.5);
   switch (markerstyle) {
case 2:
   
   shape[0].SetX(-im); shape[0].SetY( 0);
   shape[1].SetX(im);  shape[1].SetY( 0);
   shape[2].SetX(0) ;  shape[2].SetY( -im);
   shape[3].SetX(0) ;  shape[3].SetY( im);
   SetMarkerType(4,4,shape);
   break;
case 3:
   
   shape[0].SetX(-im);  shape[0].SetY(  0);
   shape[1].SetX( im);  shape[1].SetY(  0);
   shape[2].SetX(  0);  shape[2].SetY(-im);
   shape[3].SetX(  0);  shape[3].SetY( im);
   im = Int_t(0.707*Float_t(im) + 0.5);
   shape[4].SetX(-im);  shape[4].SetY(-im);
   shape[5].SetX( im);  shape[5].SetY( im);
   shape[6].SetX(-im);  shape[6].SetY( im);
   shape[7].SetX( im);  shape[7].SetY(-im);
   SetMarkerType(4,8,shape);
   break;
case 4:
case 24:
   
   SetMarkerType(0,im*2,shape);
   break;
case 5:
   
   im = Int_t(0.707*Float_t(im) + 0.5);
   shape[0].SetX(-im);  shape[0].SetY(-im);
   shape[1].SetX( im);  shape[1].SetY( im);
   shape[2].SetX(-im);  shape[2].SetY( im);
   shape[3].SetX( im);  shape[3].SetY(-im);
   SetMarkerType(4,4,shape);
   break;
case  6:
   
   shape[0].SetX(-1);  shape[0].SetY( 0);
   shape[1].SetX( 1);  shape[1].SetY( 0);
   shape[2].SetX( 0);  shape[2].SetY(-1);
   shape[3].SetX( 0);  shape[3].SetY( 1);
   SetMarkerType(4,4,shape);
   break;
case 7:
   
   shape[0].SetX(-1);  shape[0].SetY( 1);
   shape[1].SetX( 1);  shape[1].SetY( 1);
   shape[2].SetX(-1);  shape[2].SetY( 0);
   shape[3].SetX( 1);  shape[3].SetY( 0);
   shape[4].SetX(-1);  shape[4].SetY(-1);
   shape[5].SetX( 1);  shape[5].SetY(-1);
   SetMarkerType(4,6,shape);
   break;
case  8:
case 20:
   
   SetMarkerType(1,im*2,shape);
   break;
case 21:      
   
   shape[0].SetX(-im);  shape[0].SetY(-im);
   shape[1].SetX( im);  shape[1].SetY(-im);
   shape[2].SetX( im);  shape[2].SetY( im);
   shape[3].SetX(-im);  shape[3].SetY( im);
   
   SetMarkerType(3,4,shape);
   break;
case 22:
   
   shape[0].SetX(-im);  shape[0].SetY( im);
   shape[1].SetX( im);  shape[1].SetY( im);
   shape[2].SetX(  0);  shape[2].SetY(-im);
   
   SetMarkerType(3,3,shape);
   break;
case 23:
   
   shape[0].SetX(  0);  shape[0].SetY( im);
   shape[1].SetX( im);  shape[1].SetY(-im);
   shape[2].SetX(-im);  shape[2].SetY(-im);
   
   SetMarkerType(3,3,shape);
   break;
case 25:
   
   shape[0].SetX(-im);  shape[0].SetY(-im);
   shape[1].SetX( im);  shape[1].SetY(-im);
   shape[2].SetX( im);  shape[2].SetY( im);
   shape[3].SetX(-im);  shape[3].SetY( im);
   
   SetMarkerType(2,4,shape);
   break;
case 26:
   
   shape[0].SetX(-im);  shape[0].SetY( im);
   shape[1].SetX( im);  shape[1].SetY( im);
   shape[2].SetX(  0);  shape[2].SetY(-im);
   
   SetMarkerType(2,3,shape);
   break;
case 27: {
   
   Int_t imx = Int_t(2.66*fMarkerSize + 0.5);
   shape[0].SetX(-imx); shape[0].SetY( 0);
   shape[1].SetX(  0);  shape[1].SetY(-im);
   shape[2].SetX(imx);  shape[2].SetY( 0);
   shape[3].SetX(  0);  shape[3].SetY( im);
   
   SetMarkerType(2,4,shape);
   break;
         }
case 28: {
   
   Int_t imx = Int_t(1.33*fMarkerSize + 0.5);
   shape[0].SetX(-im);  shape[0].SetY(-imx);
   shape[1].SetX(-imx); shape[1].SetY(-imx);
   shape[2].SetX(-imx); shape[2].SetY( -im);
   shape[3].SetX(imx);  shape[3].SetY( -im);
   shape[4].SetX(imx);  shape[4].SetY(-imx);
   shape[5].SetX( im);  shape[5].SetY(-imx);
   shape[6].SetX( im);  shape[6].SetY( imx);
   shape[7].SetX(imx);  shape[7].SetY( imx);
   shape[8].SetX(imx);  shape[8].SetY( im);
   shape[9].SetX(-imx); shape[9].SetY( im);
   shape[10].SetX(-imx);shape[10].SetY(imx);
   shape[11].SetX(-im); shape[11].SetY(imx);
   
   SetMarkerType(2,12,shape);
   break;
         }
case 29: {
   
   Int_t im1 = Int_t(0.66*fMarkerSize + 0.5);
   Int_t im2 = Int_t(2.00*fMarkerSize + 0.5);
   Int_t im3 = Int_t(2.66*fMarkerSize + 0.5);
   Int_t im4 = Int_t(1.33*fMarkerSize + 0.5);
   shape[0].SetX(-im);  shape[0].SetY( im4);
   shape[1].SetX(-im2); shape[1].SetY(-im1);
   shape[2].SetX(-im3); shape[2].SetY( -im);
   shape[3].SetX(  0);  shape[3].SetY(-im2);
   shape[4].SetX(im3);  shape[4].SetY( -im);
   shape[5].SetX(im2);  shape[5].SetY(-im1);
   shape[6].SetX( im);  shape[6].SetY( im4);
   shape[7].SetX(im4);  shape[7].SetY( im4);
   shape[8].SetX(  0);  shape[8].SetY( im);
   shape[9].SetX(-im4); shape[9].SetY( im4);
   
   SetMarkerType(3,10,shape);
   break;
         }
case 30: {
   
   Int_t im1 = Int_t(0.66*fMarkerSize + 0.5);
   Int_t im2 = Int_t(2.00*fMarkerSize + 0.5);
   Int_t im3 = Int_t(2.66*fMarkerSize + 0.5);
   Int_t im4 = Int_t(1.33*fMarkerSize + 0.5);
   shape[0].SetX(-im);  shape[0].SetY( im4);
   shape[1].SetX(-im2); shape[1].SetY(-im1);
   shape[2].SetX(-im3); shape[2].SetY( -im);
   shape[3].SetX(  0);  shape[3].SetY(-im2);
   shape[4].SetX(im3);  shape[4].SetY( -im);
   shape[5].SetX(im2);  shape[5].SetY(-im1);
   shape[6].SetX( im);  shape[6].SetY( im4);
   shape[7].SetX(im4);  shape[7].SetY( im4);
   shape[8].SetX(  0);  shape[8].SetY( im);
   shape[9].SetX(-im4); shape[9].SetY( im4);
   SetMarkerType(2,10,shape);
   break;
         }
case 31:
   
   SetMarkerType(1,im*2,shape);
   break;
default:
   
   SetMarkerType(0,0,shape);
   }
}
void  TGQt::SetMarkerType( int type, int n, TPoint *xy )
{
     fQtMarker->SetMarker(n,xy,type);
}
void  TGQt::SetRGB(int cindex, float r, float g, float b)
{
#define BIGGEST_RGB_VALUE 255  // 65535
   
   if (cindex < 0 ) return;
   else {
      
      
      fPallete[cindex] = QColor(
          int(r*BIGGEST_RGB_VALUE+0.5)
         ,int(g*BIGGEST_RGB_VALUE+0.5)
         ,int(b*BIGGEST_RGB_VALUE+0.5)
         );
   }
}
void  TGQt::SetRGB(Int_t cindex, Float_t r, Float_t g, Float_t b, Float_t a)
{
   
   SetRGB(cindex, r, g,b);
   SetAlpha(cindex,a);
}
void  TGQt::SetAlpha(Int_t cindex, Float_t a)
{      
   
  if (cindex < 0 || a < 0 ) return;
#if QT_VERSION >= 0x40000
   QColor &color = fPallete[cindex];
   color.setAlphaF(a);
#endif
}
void  TGQt::GetRGBA(Int_t cindex, Float_t &r, Float_t &g, Float_t &b, Float_t &a)
{
   
   GetRGB(cindex,r,g,b);
   a = GetAlpha(cindex);
}
Float_t TGQt::GetAlpha(Int_t cindex)
{
   
   if (cindex < 0 ) return 1.0;
#if QT_VERSION >= 0x40000
   const QColor &color = fPallete[cindex];
   return (Float_t)color.alphaF();
#else
   return 1.0;
#endif
  
}
void  TGQt::SetTextAlign(Short_t talign)
{
   
   
   
   
   
   Int_t txalh = talign/10;
   Int_t txalv = talign%10;
   fTextAlignH = txalh;
   fTextAlignV = txalv;
#if QT_VERSION < 0x40000
   fTextAlign = Qt::AlignAuto;
#else /* QT_VERSION */
   fTextAlign = Qt::AlignLeft;
#endif /* QT_VERSION */
   switch( txalh ) {
  case 2:
     fTextAlign |= Qt::AlignHCenter;
     break;
  case 3:
     fTextAlign |= Qt::AlignRight;
     break;
  default:
     fTextAlign |= Qt::AlignLeft;
   }
   switch( txalv ) {
  case 1:
     fTextAlign |= Qt::AlignBottom;
     break;
  case 2:
     fTextAlign |= Qt::AlignVCenter;
     break;
  case 3:
     fTextAlign |= Qt::AlignTop;
     break;
  default:
     fTextAlign = Qt::AlignBottom;
   }
}
void  TGQt::SetTextColor(Color_t cindex)
{
   
   
   
   
   if (fTextColor == cindex) return;
   fTextColor = cindex;
   if (cindex < 0) return;
}
Int_t  TGQt::SetTextFont(char* , TVirtualX::ETextSetMode )
{
   
   
   
   
   
   
   
   
   
   return 0;
}
void  TGQt::SetTextFont(const char *fontname, int italic, int bold)
{
   
   
   
   
   
   
   
   fQFont->setWeight((long) bold*10);
   fQFont->setItalic((Bool_t)italic);
   fQFont->setFamily(fontname);
#if QT_VERSION >= 0x40000
   if (!strcmp(fontname,romanFontName)) {
      fQFont->setStyleHint(QFont::Serif);
   } else if (!strcmp(fontname,arialFontName)) {
      fQFont->setStyleHint(QFont::SansSerif);
   } else if (!strcmp(fontname,courierFontName)){
      fQFont->setStyleHint(QFont::TypeWriter);
   }  
   fQFont->setStyleStrategy(QFont::PreferDevice);
#if 0   
   qDebug() << "TGQt::SetTextFont:" << fontname 
         << fQFont->lastResortFamily ()
         << fQFont->lastResortFont () 
         << fQFont->substitute (fontname);
#endif   
#endif   
   fTextFontModified = 1;
   
}
void  TGQt::SetTextFont(Font_t fontnumber)
{
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   if ( fTextFont == fontnumber) return;
   fTextFont = fontnumber;
   if (fTextFont == -1) {
      fTextFontModified = 1;
      return;
   }
   int italic, bold;
   const char *fontName = romanFontName;
   switch(fontnumber/10) {
   case  1:
      italic = 1;
      bold   = 5;
      fontName = romanFontName;
      break;
   case  2:
      italic = 0;
      bold   = 8;
      fontName = romanFontName;
      break;
   case  3:
      italic = 1;
      bold   = 8;
      fontName = romanFontName;
      break;
   case  4:
      italic = 0;
      bold   = 5;
      fontName = arialFontName;
      break;
   case  5:
      italic = 1;
      bold   = 5;
      fontName = arialFontName;
      break;
   case  6:
      italic = 0;
      bold   = 8;
      fontName = arialFontName;
      break;
   case  7:
      italic = 1;
      bold   = 8;
      fontName = arialFontName;
      break;
   case  8:
      italic = 0;
      bold   = 5;
      fontName = courierFontName;
      break;
   case  9:
      italic = 1;
      bold   = 5;
      fontName = courierFontName;
      break;
   case 10:
      italic = 0;
      bold   = 8;
      fontName = courierFontName;
      break;
   case 11:
      italic = 1;
      bold   = 8;
      fontName = courierFontName;
      break;
   case 12:
      italic = 0;
      bold   = 5;
      fontName = fSymbolFontFamily;
      break;
   case 13:
      italic = 0;
      bold   = 5;
      fontName = romanFontName;
      break;
   case 14:
      italic = 0;
      bold   = 5;
      fontName = "Wingdings";
      break;
   default:
      italic = 0;
      bold   = 5;
      fontName = romanFontName;
      break;
   }
   SetTextFont(fontName, italic, bold);
}
void  TGQt::SetTextSize(Float_t textsize)
{
   
   
   if ( fTextSize != textsize ) {
      fTextSize = textsize;
      if (fTextSize > 0) {
         Int_t   tsize =(Int_t)( textsize+0.5);
         fQFont->setPixelSize(int(FontMagicFactor(tsize)));
         fTextFontModified = 1;
      }
   }
}
void  TGQt::SetTitle(const char *title)
{
   
   
   if (fSelectedWindow->devType() == QInternal::Widget)
   {
      ((TQtWidget *)fSelectedWindow)->topLevelWidget()->setCaption(GetTextDecoder()->toUnicode(title));
   }
}
void  TGQt::UpdateWindow(int mode)
{
   
   
   
   if (fSelectedWindow && mode != 2 ) {
      ((TQtWidget *)fSelectedWindow)->paintFlag();
      ((TQtWidget *)fSelectedWindow)->repaint();
   }
}
Int_t  TGQt::WriteGIF(char *name)
{
   
   
   
   
   
   
   
   
   
   
   
   
   
   WritePixmap(iwid(fSelectedWindow),UInt_t(-1),UInt_t(-1),name);
   return kTRUE;
}
void  TGQt::WritePixmap(int wid, UInt_t w, UInt_t h, char *pxname)
{
   
   
   
   
   
   
   
   
   
   
   
   
   if (!wid || (wid == -1) ) return;
   QPaintDevice &dev = *iwid(wid);
   QPixmap grabWidget;
   QPixmap *pix=0;
   switch (dev.devType()) {
   case QInternal::Widget: {
        TQtWidget *thisWidget = (TQtWidget*)&dev;
        if (thisWidget->IsDoubleBuffered() ) {
           pix = &((TQtWidget*)&dev)->GetBuffer();
        } else {
           
           
           grabWidget = QPixmap::grabWindow(thisWidget->winId());
           pix = &grabWidget;
        }
     }
     break;
   case QInternal::Pixmap: {
      pix = (QPixmap *)&dev;
      break;
                          }
   case QInternal::Picture:
   case QInternal::Printer:
#if QT_VERSION < 0x40000
   case QInternal::UndefinedDevice:
#else /* QT_VERSION */
   
#endif /* QT_VERSION */
   default: assert(0);
     break;
   };
   if (pix) {
      
      QPixmap outMap(0,0);
      QPixmap *finalPixmap = pix;
      if ( ( (h == w) && (w == UInt_t(-1) ) ) || ( QSize(w,h) == pix->size()) ) {
      }  else  {
         outMap.resize(w,h);
         QPainter pnt(&outMap);
         pnt.drawPixmap(outMap.rect(),*pix);
         finalPixmap = &outMap;
      }
      
      QString fname = pxname;
      int plus = fname.find("+");
      if (plus>=0) fname = fname.left(plus);
      
      
      QString saveType = QtFileFormat(QFileInfo(fname).extension(FALSE));
      
      
      if (saveType.isEmpty()) saveType="PNG";
      else if (QFileInfo(fname).extension(FALSE) == "gif") {
         
         
         Int_t saver = gErrorIgnoreLevel;
         gErrorIgnoreLevel = kFatal;
         TImage *img = TImage::Create();
         if (img) {
            img->SetImage(Pixmap_t(rootwid(finalPixmap)),0);
            img->WriteImage(pxname, 
#if ROOT_VERSION_CODE >= ROOT_VERSION(5,13,0)            
                  plus>=0 ? TImage::kAnimGif: TImage::kGif);
#else            
                  TImage::kGif);
#endif                  
            delete img;
         }
         gErrorIgnoreLevel = saver;
      } else {
         if (plus>=0) fname = GetNewFileName(fname);
         finalPixmap->save(fname,saveType);
      }
   }
}
void TGQt::UpdateFont()
{
   
   if (fQFont && fQPainter->isActive()) {
      fQPainter->setFont(*fQFont);
      fTextFontModified = 0;
   }
}
void TGQt::UpdatePen()
{
   
   if (fQPen  && fQPainter->isActive()) {
      fQPainter->setPen(*fQPen);
      
   }
}
void TGQt::UpdateBrush()
{
   
  if (!fQPainter) fQPainter = new QPainter();
  if (fQBrush && fQPainter->isActive())
   {
      fQPainter->setBrush(*fQBrush);
      
      
   }
}
void TGQt::UpdateClipRectangle()
{
   
   if (!fQPainter->isActive()) return;
   TQTCLIPMAP::iterator it= fClipMap.find(fSelectedWindow);
   QRect clipRect;
   if (it != fClipMap.end())  {
      clipRect = it.data();
      fQPainter->setClipRect(clipRect);
      fQPainter->setClipping(TRUE);
   }
}
void TGQt::Begin()
{
   
   if (!fQPainter) fQPainter = new QPainter();
   if (!fQPainter->isActive() )
   {
      QPaintDevice *src = fSelectedBuffer ? fSelectedBuffer : fSelectedWindow;
      
      if ( fSelectedWindow->devType() ==  QInternal::Widget)
      {
         ((TQtWidget *)fSelectedWindow)->AdjustBufferSize();
#if QT_VERSION >= 0x40000
#ifdef SHWDOWBUFFER         
         if (src == fSelectedWindow) {
            TQtWidget *ww = (TQtWidget *)fSelectedWindow;
            if (ww && !ww->IsShadow()) 
               src = &ww->GetShadow()->GetBuffer();
         }
#endif         
#endif         
      }
      if (!fQPainter->begin(src) ) {
         fprintf(stderr,"---> TGQt::Begin() win=%p dev=%p\n",src,fQPainter->device());
      } else {
         UpdatePen();
         UpdateBrush();
         UpdateFont();
         TQTCLIPMAP::iterator it= fClipMap.find(fSelectedWindow);
         QRect clipRect;
         if (it != fClipMap.end())  {
           clipRect = it.data();
            fQPainter->setClipRect(clipRect);
            fQPainter->setClipping(TRUE);
         }
#if QT_VERSION < 0x40000
         fQPainter->setRasterOp(fDrawMode);
#else /* QT_VERSION */
         if (fQPainter->device()->devType() ==  QInternal::Image )
                 fQPainter->setCompositionMode(fDrawMode);
#endif /* QT_VERSION */
      }
      
   }
}
void TGQt::End()
{
   
   if ( fQPainter && fQPainter->isActive() )
   {
      
      fQPainter->end();
   }
}
TVirtualX *TGQt::GetVirtualX(){ return fgTQt;}
Int_t TGQt::LoadQt(const char *shareLibFileName)
{
   
   return gSystem->Load(shareLibFileName);
}
Int_t TGQt::processQtEvents(Int_t maxtime)
{
   
#if QT_VERSION < 0x40000
   qApp->processEvents(maxtime);
#else /* QT_VERSION */
   qApp->processEvents(QEventLoop::AllEvents,maxtime);
#endif /* QT_VERSION */
   
   return 0;
 }
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.