// @(#)root/gui:$Id$
// Author: Bertrand Bellenot 23/01/2008

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

#include "TGFrame.h"
#include "TGLayout.h"
#include "TString.h"
#include "TGPicture.h"
#include "TImage.h"
#include "TGShapedFrame.h"
#include "Riostream.h"

ClassImp(TGShapedFrame)

//______________________________________________________________________________
TGShapedFrame::TGShapedFrame(const char *pname, const TGWindow *p, UInt_t w,
                             UInt_t h, UInt_t options) :
      TGCompositeFrame(p, w, h, options), fBgnd(0), fImage(0)
{
   // Shaped window default constructor

   TString picName;
   // set a few attributes
   if (options & kTempFrame) {
      SetWindowAttributes_t attr;
      attr.fMask             = kWAOverrideRedirect | kWASaveUnder;
      attr.fOverrideRedirect = kTRUE;
      attr.fSaveUnder        = kTRUE;
      gVirtualX->ChangeWindowAttributes(fId, &attr);
   }
   // open the image file used as shape & background
   if (pname)
      picName = pname;
   else
      picName = "Default.png";
   fImage = TImage::Open(picName.Data());
   if (!fImage || !fImage->IsValid())
      Error("TGShapedFrame", "%s not found", picName.Data());
   fBgnd = fClient->GetPicturePool()->GetPicture(picName.Data(),
           fImage->GetPixmap(), fImage->GetMask());
   // shape the window with the picture mask
   gVirtualX->ShapeCombineMask(fId, 0, 0, fBgnd->GetMask());
   // and finally set the background picture
   SetBackgroundPixmap(fBgnd->GetPicture());

   MapSubwindows();
   Resize();
   Resize(fBgnd->GetWidth(), fBgnd->GetHeight());
}

//______________________________________________________________________________
TGShapedFrame::~TGShapedFrame()
{
   // Destructor.

   delete fImage;
   fClient->FreePicture(fBgnd);
}

//______________________________________________________________________________
void TGShapedFrame::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
{
   // Save a shaped frame as a C++ statement(s) on output stream out.

   if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);

   out << std::endl << "   // shaped frame" << std::endl;
   out << "   TGShapedFrame *";
   out << GetName() << " = new TGShapedFrame(" << fImage->GetName()
       << "," << fParent->GetName() << "," << GetWidth() << ","
       << GetHeight();

   if (fBackground == GetDefaultFrameBackground()) {
      if (!GetOptions()) {
         out << ");" << std::endl;
      } else {
         out << "," << GetOptionString() <<");" << std::endl;
      }
   } else {
      out << "," << GetOptionString() << ",ucolor);" << std::endl;
   }
   if (option && strstr(option, "keep_names"))
      out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;

   // setting layout manager if it differs from the main frame type
   // coverity[returned_null]
   // coverity[dereference]
   TGLayoutManager * lm = GetLayoutManager();
   if ((GetOptions() & kHorizontalFrame) &&
       (lm->InheritsFrom(TGHorizontalLayout::Class()))) {
      ;
   } else if ((GetOptions() & kVerticalFrame) &&
              (lm->InheritsFrom(TGVerticalLayout::Class()))) {
      ;
   } else {
      out << "   " << GetName() <<"->SetLayoutManager(";
      lm->SavePrimitive(out, option);
      out << ");"<< std::endl;
   }

   SavePrimitiveSubframes(out, option);
}
 TGShapedFrame.cxx:1
 TGShapedFrame.cxx:2
 TGShapedFrame.cxx:3
 TGShapedFrame.cxx:4
 TGShapedFrame.cxx:5
 TGShapedFrame.cxx:6
 TGShapedFrame.cxx:7
 TGShapedFrame.cxx:8
 TGShapedFrame.cxx:9
 TGShapedFrame.cxx:10
 TGShapedFrame.cxx:11
 TGShapedFrame.cxx:12
 TGShapedFrame.cxx:13
 TGShapedFrame.cxx:14
 TGShapedFrame.cxx:15
 TGShapedFrame.cxx:16
 TGShapedFrame.cxx:17
 TGShapedFrame.cxx:18
 TGShapedFrame.cxx:19
 TGShapedFrame.cxx:20
 TGShapedFrame.cxx:21
 TGShapedFrame.cxx:22
 TGShapedFrame.cxx:23
 TGShapedFrame.cxx:24
 TGShapedFrame.cxx:25
 TGShapedFrame.cxx:26
 TGShapedFrame.cxx:27
 TGShapedFrame.cxx:28
 TGShapedFrame.cxx:29
 TGShapedFrame.cxx:30
 TGShapedFrame.cxx:31
 TGShapedFrame.cxx:32
 TGShapedFrame.cxx:33
 TGShapedFrame.cxx:34
 TGShapedFrame.cxx:35
 TGShapedFrame.cxx:36
 TGShapedFrame.cxx:37
 TGShapedFrame.cxx:38
 TGShapedFrame.cxx:39
 TGShapedFrame.cxx:40
 TGShapedFrame.cxx:41
 TGShapedFrame.cxx:42
 TGShapedFrame.cxx:43
 TGShapedFrame.cxx:44
 TGShapedFrame.cxx:45
 TGShapedFrame.cxx:46
 TGShapedFrame.cxx:47
 TGShapedFrame.cxx:48
 TGShapedFrame.cxx:49
 TGShapedFrame.cxx:50
 TGShapedFrame.cxx:51
 TGShapedFrame.cxx:52
 TGShapedFrame.cxx:53
 TGShapedFrame.cxx:54
 TGShapedFrame.cxx:55
 TGShapedFrame.cxx:56
 TGShapedFrame.cxx:57
 TGShapedFrame.cxx:58
 TGShapedFrame.cxx:59
 TGShapedFrame.cxx:60
 TGShapedFrame.cxx:61
 TGShapedFrame.cxx:62
 TGShapedFrame.cxx:63
 TGShapedFrame.cxx:64
 TGShapedFrame.cxx:65
 TGShapedFrame.cxx:66
 TGShapedFrame.cxx:67
 TGShapedFrame.cxx:68
 TGShapedFrame.cxx:69
 TGShapedFrame.cxx:70
 TGShapedFrame.cxx:71
 TGShapedFrame.cxx:72
 TGShapedFrame.cxx:73
 TGShapedFrame.cxx:74
 TGShapedFrame.cxx:75
 TGShapedFrame.cxx:76
 TGShapedFrame.cxx:77
 TGShapedFrame.cxx:78
 TGShapedFrame.cxx:79
 TGShapedFrame.cxx:80
 TGShapedFrame.cxx:81
 TGShapedFrame.cxx:82
 TGShapedFrame.cxx:83
 TGShapedFrame.cxx:84
 TGShapedFrame.cxx:85
 TGShapedFrame.cxx:86
 TGShapedFrame.cxx:87
 TGShapedFrame.cxx:88
 TGShapedFrame.cxx:89
 TGShapedFrame.cxx:90
 TGShapedFrame.cxx:91
 TGShapedFrame.cxx:92
 TGShapedFrame.cxx:93
 TGShapedFrame.cxx:94
 TGShapedFrame.cxx:95
 TGShapedFrame.cxx:96
 TGShapedFrame.cxx:97
 TGShapedFrame.cxx:98
 TGShapedFrame.cxx:99
 TGShapedFrame.cxx:100
 TGShapedFrame.cxx:101
 TGShapedFrame.cxx:102
 TGShapedFrame.cxx:103
 TGShapedFrame.cxx:104
 TGShapedFrame.cxx:105
 TGShapedFrame.cxx:106
 TGShapedFrame.cxx:107
 TGShapedFrame.cxx:108
 TGShapedFrame.cxx:109