// @(#)root/gui:$Id: TGResourcePool.cxx 33926 2010-06-15 15:35:40Z bellenot $
// Author: Fons Rademakers   19/5/2003

/*************************************************************************
 * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/
/**************************************************************************

    This source is based on Xclass95, a Win95-looking GUI toolkit.
    Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.

    Xclass95 is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

**************************************************************************/


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGResourcePool                                                       //
//                                                                      //
// This class implements a pool for the default GUI resource set,       //
// like GC's, colors, fonts, etc..                                      //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "RConfigure.h"

#include "TGResourcePool.h"
#include "TGClient.h"
#include "TGWindow.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TEnv.h"
#include "TVirtualX.h"
#include "TGGC.h"
#include "TGPicture.h"
#include "TGFont.h"
#include "TGMimeTypes.h"


const int gray_width  = 8;
const int gray_height = 8;
static unsigned char gray_bits[] = {
   0x55, 0xaa, 0x55, 0xaa,
   0x55, 0xaa, 0x55, 0xaa
};


ClassImp(TGResourcePool)

//______________________________________________________________________________
TGResourcePool::TGResourcePool(TGClient *client)
{
   // Create the global GUI resource pool manager.

   fClient = client;
   fDefaultColormap = gVirtualX->GetColormap();

   // Get GUI defaults from [system].rootrc
   TString default_font;
   TString menu_font;
   TString menu_hi_font;
   TString doc_fixed_font;
   TString doc_prop_font;
   TString icon_font;
   TString status_font;
   TString backcolor;
   TString forecolor;
   TString selbackcolor;
   TString selforecolor;
   TString docbackcolor;
   TString docforecolor;
   TString tipbackcolor;
   TString tipforecolor;
   TString framebgpixmap;
   TString docbgpixmap;
   TString icon_path;
   TString mime_file;
   TString line;

   default_font   = gEnv->GetValue("Gui.DefaultFont",  "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-iso8859-1");
   menu_font      = gEnv->GetValue("Gui.MenuFont",     "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-iso8859-1");
   menu_hi_font   = gEnv->GetValue("Gui.MenuHiFont",   "-*-helvetica-bold-r-*-*-12-*-*-*-*-*-iso8859-1");
   doc_fixed_font = gEnv->GetValue("Gui.DocFixedFont", "-*-courier-medium-r-*-*-12-*-*-*-*-*-iso8859-1");
   doc_prop_font  = gEnv->GetValue("Gui.DocPropFont",  "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-iso8859-1");
   icon_font      = gEnv->GetValue("Gui.IconFont",     "-*-helvetica-medium-r-*-*-10-*-*-*-*-*-iso8859-1");
   status_font    = gEnv->GetValue("Gui.StatusFont",   "-*-helvetica-medium-r-*-*-10-*-*-*-*-*-iso8859-1");
   backcolor      = gEnv->GetValue("Gui.BackgroundColor",         "#c0c0c0");
   forecolor      = gEnv->GetValue("Gui.ForegroundColor",         "black");
   selbackcolor   = gEnv->GetValue("Gui.SelectBackgroundColor",   "#000080");
   selforecolor   = gEnv->GetValue("Gui.SelectForegroundColor",   "white");
   docbackcolor   = gEnv->GetValue("Gui.DocumentBackgroundColor", "white");
   docforecolor   = gEnv->GetValue("Gui.DocumentForegroundColor", "black");
   tipbackcolor   = gEnv->GetValue("Gui.TooltipBackgroundColor",  "LightYellow");
   tipforecolor   = gEnv->GetValue("Gui.TooltipForegroundColor",  "black");
   framebgpixmap  = gEnv->GetValue("Gui.FrameBackgroundPixmap", "");
   docbgpixmap    = gEnv->GetValue("Gui.DocumentBackgroundPixmap", "");

#ifndef R__WIN32
#ifndef R__VMS
# ifdef ROOTICONPATH
   icon_path = TString::Format("%s/icons:%s:.:", gSystem->HomeDirectory(),
                               ROOTICONPATH);
#  ifdef EXTRAICONPATH
   icon_path += gEnv->GetValue("Gui.IconPath", EXTRAICONPATH);
#  else
   icon_path += gEnv->GetValue("Gui.IconPath", "");
#  endif
# else
   icon_path = TString::Format("%s/icons:%s/icons:.:", gSystem->HomeDirectory(),
                                                       gSystem->Getenv("ROOTSYS"));
   icon_path += gEnv->GetValue("Gui.IconPath", "");
# endif
   line = TString::Format("%s/.root.mimes", gSystem->HomeDirectory());
#else
   line = TString::Format("[%s.ICONS]", gSystem->Getenv("ROOTSYS"));
   icon_path = gEnv->GetValue("Gui.IconPath", line.Data());
   line = TString::Format("%sroot.mimes", gSystem->HomeDirectory());
#endif

   mime_file = gEnv->GetValue("Gui.MimeTypeFile", line.Data());
   char *mf = gSystem->ExpandPathName(mime_file.Data());
   if (mf) {
      mime_file = mf;
      delete [] mf;
   }
   if (gSystem->AccessPathName(mime_file.Data(), kReadPermission))
#ifdef R__VMS
      mime_file = TString::Format("[%s.ETC]root.mimes",gSystem->Getenv("ROOTSYS"));
#else
# ifdef ROOTETCDIR
      mime_file = TString::Format("%s/root.mimes", ROOTETCDIR);
# else
      mime_file = TString::Format("%s/etc/root.mimes", gSystem->Getenv("ROOTSYS"));
# endif
#endif
#else // R__WIN32
   icon_path = TString::Format("%s\\icons:.:\\", gSystem->Getenv("ROOTSYS"));
   icon_path += gEnv->GetValue("Gui.IconPath", "");
   line = TString::Format("%s\\root.mimes", gSystem->HomeDirectory());
   mime_file = gEnv->GetValue("Gui.MimeTypeFile", line.Data());
   if (gSystem->AccessPathName(mime_file.Data(), kReadPermission))
      mime_file = TString::Format("%s\\etc\\root.mimes", gSystem->Getenv("ROOTSYS"));
#endif

   // Setup colors...
   fClient->GetColorByName("white", fWhite);  // white and black always exist
   fClient->GetColorByName("black", fBlack);
   if (!fClient->GetColorByName(backcolor.Data(), fBackColor))
      fBackColor = fWhite;
   if (!fClient->GetColorByName(forecolor.Data(), fForeColor))
      fForeColor = fBlack;
   fHilite = fClient->GetHilite(fBackColor);
   fShadow = fClient->GetShadow(fBackColor);
   if (!fClient->GetColorByName(selbackcolor.Data(), fSelBackColor))
      fSelBackColor = fBlack;
   if (!fClient->GetColorByName(selforecolor.Data(), fSelForeColor))
      fSelForeColor = fWhite;
   if (!fClient->GetColorByName(docbackcolor.Data(), fDocBackColor))
      fDocBackColor = fWhite;
   if (!fClient->GetColorByName(docforecolor.Data(), fDocForeColor))
      fDocForeColor = fBlack;
   if (!fClient->GetColorByName(tipbackcolor.Data(), fTipBackColor))
      fTipBackColor = fWhite;
   if (!fClient->GetColorByName(tipforecolor.Data(), fTipForeColor))
      fTipForeColor = fBlack;

   // Setup checkered pix/bit-maps...
   fCheckered = gVirtualX->CreatePixmap(fClient->GetDefaultRoot()->GetId(),
                                        (const char *)gray_bits,
                                        gray_width, gray_height,
                                        fBackColor, fWhite /*fHilite*/,
                                        gVirtualX->GetDepth());
   fCheckeredBitmap = gVirtualX->CreatePixmap(fClient->GetDefaultRoot()->GetId(),
                                              (const char *)gray_bits,
                                              gray_width, gray_height,
                                              1, 0, 1);

   // Create picture pool, GC pool, font pool, mime type list, etc.

   // Create picture pool and pre-load some pictures...
   fPicturePool = new TGPicturePool(fClient, icon_path.Data());

   fDefaultBackPicture    = 0;
   fDefaultDocBackPicture = 0;
   if (!framebgpixmap.IsNull())
      fDefaultBackPicture = fPicturePool->GetPicture(framebgpixmap.Data());
   if (!docbgpixmap.IsNull())
      fDefaultDocBackPicture = fPicturePool->GetPicture(docbgpixmap.Data());

   // Create font pool and pre-load some fonts...
   fFontPool = new TGFontPool(fClient);
   fDefaultFont  = fFontPool->GetFont(default_font.Data());
   fMenuFont     = fFontPool->GetFont(menu_font.Data());
   fMenuHiFont   = fFontPool->GetFont(menu_hi_font.Data());
   fDocFixedFont = fFontPool->GetFont(doc_fixed_font.Data());
   fDocPropFont  = fFontPool->GetFont(doc_prop_font.Data());
   fIconFont     = fFontPool->GetFont(icon_font.Data());
   fStatusFont   = fFontPool->GetFont(status_font.Data());

   // Create GC pool and pre-load some GCs...
   fGCPool = new TGGCPool(fClient);

   GCValues_t gval;
   gval.fMask = kGCForeground | kGCBackground | kGCFont |
                kGCFillStyle  | kGCGraphicsExposures;
   gval.fGraphicsExposures = kFALSE;
   gval.fFillStyle  = kFillSolid;
   gval.fFont       = fDefaultFont->GetFontHandle();
   gval.fBackground = fBackColor;
   gval.fForeground = fBlack;
   fBlackGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fWhite;
   fWhiteGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fHilite;
   fHiliteGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fShadow;
   fShadowGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fBackColor;
   fBckgndGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fForeColor;
   fFrameGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fSelBackColor;
   fSelbgndGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fSelForeColor;
   gval.fBackground = fSelBackColor;
   fSelGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fFont       = fDocPropFont->GetFontHandle();
   gval.fForeground = fDocForeColor;
   gval.fBackground = fDocBackColor;
   fDocGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fForeground = fDocBackColor;
   fDocbgndGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fFont       = fStatusFont->GetFontHandle();
   gval.fForeground = fTipForeColor;
   gval.fBackground = fTipBackColor;
   fTipGC = fGCPool->GetGC(&gval, kTRUE);

   gval.fMask = kGCForeground | kGCBackground | kGCFillStyle |
                kGCLineWidth  | kGCSubwindowMode |
                kGCGraphicsExposures | kGCStipple;
   gval.fForeground = fBlack;
   gval.fBackground = fSelBackColor;
   gval.fLineWidth  = 0;
   gval.fFillStyle  = kFillStippled;
   gval.fSubwindowMode = kIncludeInferiors;
   gval.fGraphicsExposures = kFALSE;
   gval.fStipple = fCheckeredBitmap;
   fFocusGC = fGCPool->GetGC(&gval, kTRUE);

   // Setup cursors...
   fDefaultCursor = kNone;
   fGrabCursor    = gVirtualX->CreateCursor(kArrowRight);
   fTextCursor    = gVirtualX->CreateCursor(kCaret);
   fWaitCursor    = gVirtualX->CreateCursor(kWatch);

   // Read in mime type...
   fMimeTypeList  = new TGMimeTypes(fClient, mime_file.Data());

   // Clipboard handle...
#ifndef R__WIN32
   fClipboardAtom = gVirtualX->InternAtom("_ROOT_CLIPBOARD", kFALSE);
#else
   fClipboardAtom = gVirtualX->InternAtom("CLIPBOARD", kFALSE);
#endif
}

//______________________________________________________________________________
TGResourcePool::~TGResourcePool()
{
   // Cleanup the resource pool...

   delete fPicturePool;
   delete fFontPool;
   delete fGCPool;
   delete fMimeTypeList;

   gVirtualX->DeletePixmap(fCheckered);
   gVirtualX->DeletePixmap(fCheckeredBitmap);
}
