TGIcon.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGIcon.cxx 35582 2010-09-22 13:38:27Z bellenot $
00002 // Author: Fons Rademakers   05/01/98
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 /**************************************************************************
00012 
00013     This source is based on Xclass95, a Win95-looking GUI toolkit.
00014     Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
00015 
00016     Xclass95 is free software; you can redistribute it and/or
00017     modify it under the terms of the GNU Library General Public
00018     License as published by the Free Software Foundation; either
00019     version 2 of the License, or (at your option) any later version.
00020 
00021 **************************************************************************/
00022 
00023 //////////////////////////////////////////////////////////////////////////
00024 //                                                                      //
00025 // TGIcon                                                               //
00026 //                                                                      //
00027 // This class handles GUI icons.                                        //
00028 //                                                                      //
00029 //////////////////////////////////////////////////////////////////////////
00030 
00031 #include "TGIcon.h"
00032 #include "TGPicture.h"
00033 #include "TSystem.h"
00034 #include "TImage.h"
00035 #include "Riostream.h"
00036 #include "TMath.h"
00037 #include "TGFileDialog.h"
00038 #include "TGMsgBox.h"
00039 #include "TVirtualDragManager.h"
00040 
00041 
00042 ClassImp(TGIcon)
00043 
00044 //______________________________________________________________________________
00045 TGIcon::TGIcon(const TGWindow *p, const char *image) : TGFrame(p, 1, 1)
00046 {
00047    // Create icon.
00048 
00049    fPic = 0;
00050    char *path;
00051 
00052    if (!image)
00053       image = "bld_rgb.xpm";
00054 
00055    path = StrDup(image);
00056 
00057    fPath = gSystem->DirName(path);
00058 
00059    fImage = TImage::Open(path);
00060    fPic = fClient->GetPicturePool()->GetPicture(gSystem->BaseName(path),
00061                                                 fImage->GetPixmap(), fImage->GetMask());
00062    TGFrame::Resize(fImage->GetWidth(), fImage->GetHeight());
00063    SetWindowName();
00064    delete [] path;
00065 }
00066 
00067 //______________________________________________________________________________
00068 TGIcon::~TGIcon()
00069 {
00070    // Delete icon and free picture.
00071 
00072    if (fPic) fClient->FreePicture(fPic);
00073 }
00074 
00075 //______________________________________________________________________________
00076 void TGIcon::SetPicture(const TGPicture *pic)
00077 {
00078    // Set icon picture.
00079 
00080    fPic = pic;
00081    gVirtualX->ClearWindow(fId);
00082    fClient->NeedRedraw(this);
00083 }
00084 
00085 //______________________________________________________________________________
00086 void TGIcon::SetImage(const char *img)
00087 {
00088    // Set icon image.
00089 
00090    //delete fImage;
00091    TImage *i = TImage::Open(img);
00092    fPath = gSystem->DirName(img);
00093 
00094    SetImage(i);
00095 }
00096 
00097 //______________________________________________________________________________
00098 void TGIcon::SetImage(TImage *img)
00099 {
00100    // Change icon image.
00101 
00102    if (!img) {
00103       return;
00104    }
00105 
00106    delete fImage; //  !! mem.leak!!
00107    fImage = img;
00108 
00109    Resize(fImage->GetWidth(), fImage->GetHeight());
00110    fClient->NeedRedraw(this);
00111 }
00112 
00113 //______________________________________________________________________________
00114 TGDimension TGIcon::GetDefaultSize() const
00115 {
00116    // Return size of icon.
00117 
00118    return TGDimension((fPic) ? fPic->GetWidth()  : fWidth,
00119                       (fPic) ? fPic->GetHeight() : fHeight);
00120 }
00121 
00122 //______________________________________________________________________________
00123 void TGIcon::DoRedraw()
00124 {
00125    // Redraw picture.
00126 
00127    Bool_t border = (GetOptions() & kRaisedFrame) ||
00128                    (GetOptions() & kSunkenFrame) ||
00129                    (GetOptions() & kDoubleBorder);
00130 
00131    if (fPic) fPic->Draw(fId, GetBckgndGC()(), border, border);
00132    if (border)  DrawBorder();
00133 }
00134 
00135 //______________________________________________________________________________
00136 void TGIcon::Resize(UInt_t w, UInt_t h)
00137 {
00138    // Resize.
00139 
00140    TGFrame::Resize(w, h);
00141 
00142    // allow scaled resize for icons with TImage
00143    if (!fImage) {
00144       return;
00145    }
00146 
00147    gVirtualX->ClearWindow(fId);
00148 
00149    if (fPic) {
00150       fClient->FreePicture(fPic);
00151    }
00152    Bool_t border = (GetOptions() & kRaisedFrame) ||
00153                    (GetOptions() & kSunkenFrame) ||
00154                    (GetOptions() & kDoubleBorder);
00155 
00156    fImage->Scale(w - 2*border, h - 2*border);
00157    fPic = fClient->GetPicturePool()->GetPicture(fImage->GetName(),
00158                                                 fImage->GetPixmap(), fImage->GetMask());
00159    DoRedraw();
00160 }
00161 
00162 //______________________________________________________________________________
00163 void TGIcon::MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h)
00164 {
00165    // Move icon to (x,y) and resize it to (w,h).
00166 
00167    Move(x, y);
00168    Resize(w, h);
00169 }
00170 
00171 //______________________________________________________________________________
00172 void TGIcon::Reset()
00173 {
00174    // Reset icon to original image. It can be used only via context menu.
00175 
00176    if (!fImage || !fClient->IsEditable()) return;
00177 
00178    TString name = fImage->GetName();
00179    name.Chop();
00180    char *path = gSystem->ConcatFileName(fPath.Data(), name.Data());
00181    SetImage(path);
00182 
00183    delete [] path;
00184 }
00185 
00186 //______________________________________________________________________________
00187 void TGIcon::SetImagePath(const char *path)
00188 {
00189    // Set directory where image is located
00190 
00191    if (!path) {
00192       return;
00193    }
00194    fPath = gSystem->ExpandPathName(gSystem->UnixPathName(path));
00195 }
00196 
00197 //______________________________________________________________________________
00198 void TGIcon::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00199 {
00200     // Save an icon widget as a C++ statement(s) on output stream out.
00201 
00202    char quote = '"';
00203 
00204    if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
00205 
00206    if (!fPic) {
00207       Error("SavePrimitive()", "icon pixmap not found ");
00208       return;
00209    }
00210 
00211    const char *picname = fPic->GetName();
00212 
00213    out <<"   TGIcon *";
00214    if (!fImage) {
00215       out << GetName() << " = new TGIcon(" << fParent->GetName()
00216          << ",gClient->GetPicture(" << quote
00217          << gSystem->ExpandPathName(gSystem->UnixPathName(picname))   // if no path
00218          << quote << ")" << "," << GetWidth() << "," << GetHeight();
00219       if (fBackground == GetDefaultFrameBackground()) {
00220          if (!GetOptions()) {
00221             out <<");" << endl;
00222          } else {
00223             out << "," << GetOptionString() <<");" << endl;
00224          }
00225       } else {
00226          out << "," << GetOptionString() << ",ucolor);" << endl;
00227       }
00228    } else {
00229       TString name = fPath;
00230       name += "/";
00231       name += fImage->GetName();
00232       name.Chop();
00233       out << GetName() << " = new TGIcon(" << fParent->GetName()  << ","
00234           << quote << name.Data() << quote << ");" << endl;
00235    }
00236    if (option && strstr(option, "keep_names"))
00237       out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00238 }

Generated on Tue Jul 5 14:22:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1