00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
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    
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    
00071 
00072    if (fPic) fClient->FreePicture(fPic);
00073 }
00074 
00075 
00076 void TGIcon::SetPicture(const TGPicture *pic)
00077 {
00078    
00079 
00080    fPic = pic;
00081    gVirtualX->ClearWindow(fId);
00082    fClient->NeedRedraw(this);
00083 }
00084 
00085 
00086 void TGIcon::SetImage(const char *img)
00087 {
00088    
00089 
00090    
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    
00101 
00102    if (!img) {
00103       return;
00104    }
00105 
00106    delete fImage; 
00107    fImage = img;
00108 
00109    Resize(fImage->GetWidth(), fImage->GetHeight());
00110    fClient->NeedRedraw(this);
00111 }
00112 
00113 
00114 TGDimension TGIcon::GetDefaultSize() const
00115 {
00116    
00117 
00118    return TGDimension((fPic) ? fPic->GetWidth()  : fWidth,
00119                       (fPic) ? fPic->GetHeight() : fHeight);
00120 }
00121 
00122 
00123 void TGIcon::DoRedraw()
00124 {
00125    
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    
00139 
00140    TGFrame::Resize(w, h);
00141 
00142    
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    
00166 
00167    Move(x, y);
00168    Resize(w, h);
00169 }
00170 
00171 
00172 void TGIcon::Reset()
00173 {
00174    
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    
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     
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))   
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 }