ROOT logo
// @(#)root/gui:$Id: TGIcon.cxx 23115 2008-04-10 13:35:37Z rdm $
// Author: Fons Rademakers   05/01/98

/*************************************************************************
 * Copyright (C) 1995-2000, 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.

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

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGIcon                                                               //
//                                                                      //
// This class handles GUI icons.                                        //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TGIcon.h"
#include "TGPicture.h"
#include "TSystem.h"
#include "TImage.h"
#include "Riostream.h"
#include "TMath.h"
#include "TGFileDialog.h"
#include "TGMsgBox.h"
#include "TVirtualDragManager.h"


ClassImp(TGIcon)

//______________________________________________________________________________
TGIcon::TGIcon(const TGWindow *p, const char *image) : TGFrame(p, 1, 1)
{
   // Create icon.

   fPic = 0;
   char *path;

   if (!image)
      image = "bld_rgb.xpm";

   path = StrDup(image);

   fPath = gSystem->DirName(path);

   fImage = TImage::Open(path);
   fPic = fClient->GetPicturePool()->GetPicture(gSystem->BaseName(path),
                                                fImage->GetPixmap(), fImage->GetMask());
   TGFrame::Resize(fImage->GetWidth(), fImage->GetHeight());
   SetWindowName();
   delete [] path;
}

//______________________________________________________________________________
TGIcon::~TGIcon()
{
   // Delete icon and free picture.

   if (fPic) fClient->FreePicture(fPic);
}

//______________________________________________________________________________
void TGIcon::SetPicture(const TGPicture *pic)
{
   // Set icon picture.

   fPic = pic;
   gVirtualX->ClearWindow(fId);
   fClient->NeedRedraw(this);
}

//______________________________________________________________________________
void TGIcon::SetImage(const char *img)
{
   // Set icon image.

   //delete fImage;
   TImage *i = TImage::Open(img);
   fPath = gSystem->DirName(img);

   SetImage(i);
}

//______________________________________________________________________________
void TGIcon::SetImage(TImage *img)
{
   // Change icon image.

   if (!img) {
      return;
   }

   delete fImage; //  !! mem.leak!!
   fImage = img;

   Resize(fImage->GetWidth(), fImage->GetHeight());
   fClient->NeedRedraw(this);
}

//______________________________________________________________________________
TGDimension TGIcon::GetDefaultSize() const
{
   // Return size of icon.

   return TGDimension((fPic) ? fPic->GetWidth()  : fWidth,
                      (fPic) ? fPic->GetHeight() : fHeight);
}

//______________________________________________________________________________
void TGIcon::DoRedraw()
{
   // Redraw picture.

   Bool_t border = (GetOptions() & kRaisedFrame) ||
                   (GetOptions() & kSunkenFrame) ||
                   (GetOptions() & kDoubleBorder);

   if (fPic) fPic->Draw(fId, GetBckgndGC()(), border, border);
   if (border)  DrawBorder();
}

//______________________________________________________________________________
void TGIcon::Resize(UInt_t w, UInt_t h)
{
   // Resize.

   TGFrame::Resize(w, h);

   // allow scaled resize for icons with TImage
   if (!fImage) {
      return;
   }

   gVirtualX->ClearWindow(fId);

   if (fPic) {
      fClient->FreePicture(fPic);
   }
   Bool_t border = (GetOptions() & kRaisedFrame) ||
                   (GetOptions() & kSunkenFrame) ||
                   (GetOptions() & kDoubleBorder);

   fImage->Scale(w - 2*border, h - 2*border);
   fPic = fClient->GetPicturePool()->GetPicture(fImage->GetName(),
                                                fImage->GetPixmap(), fImage->GetMask());
   DoRedraw();
}

//______________________________________________________________________________
void TGIcon::MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h)
{
   // Move icon to (x,y) and resize it to (w,h).

   Move(x, y);
   Resize(w, h);
}

//______________________________________________________________________________
void TGIcon::Reset()
{
   // Reset icon to original image. It can be used only via context menu.

   if (!fImage || !fClient->IsEditable()) return;

   TString name = fImage->GetName();
   name.Chop();
   char *path = gSystem->ConcatFileName(fPath.Data(), name.Data());
   SetImage(path);

   delete path;
}

//______________________________________________________________________________
void TGIcon::SetImagePath(const char *path)
{
   // Set directory where image is located

   if (!path) {
      return;
   }
   fPath = gSystem->ExpandPathName(gSystem->UnixPathName(path));
}

//______________________________________________________________________________
void TGIcon::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
{
    // Save an icon widget as a C++ statement(s) on output stream out.

   char quote = '"';

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

   if (!fPic) {
      Error("SavePrimitive()", "icon pixmap not found ");
      return;
   }

   const char *picname = fPic->GetName();

   out <<"   TGIcon *";
   if (!fImage) {
      out << GetName() << " = new TGIcon(" << fParent->GetName()
         << ",gClient->GetPicture(" << quote
         << gSystem->ExpandPathName(gSystem->UnixPathName(picname))   // if no path
         << quote << ")" << "," << GetWidth() << "," << GetHeight();
      if (fBackground == GetDefaultFrameBackground()) {
         if (!GetOptions()) {
            out <<");" << endl;
         } else {
            out << "," << GetOptionString() <<");" << endl;
         }
      } else {
         out << "," << GetOptionString() << ",ucolor);" << endl;
      }
   } else {
      TString name = fPath;
      name += "/";
      name += fImage->GetName();
      name.Chop();
      out << GetName() << " = new TGIcon(" << fParent->GetName()  << ","
          << quote << name.Data() << quote << ");" << endl;
   }
}
 TGIcon.cxx:1
 TGIcon.cxx:2
 TGIcon.cxx:3
 TGIcon.cxx:4
 TGIcon.cxx:5
 TGIcon.cxx:6
 TGIcon.cxx:7
 TGIcon.cxx:8
 TGIcon.cxx:9
 TGIcon.cxx:10
 TGIcon.cxx:11
 TGIcon.cxx:12
 TGIcon.cxx:13
 TGIcon.cxx:14
 TGIcon.cxx:15
 TGIcon.cxx:16
 TGIcon.cxx:17
 TGIcon.cxx:18
 TGIcon.cxx:19
 TGIcon.cxx:20
 TGIcon.cxx:21
 TGIcon.cxx:22
 TGIcon.cxx:23
 TGIcon.cxx:24
 TGIcon.cxx:25
 TGIcon.cxx:26
 TGIcon.cxx:27
 TGIcon.cxx:28
 TGIcon.cxx:29
 TGIcon.cxx:30
 TGIcon.cxx:31
 TGIcon.cxx:32
 TGIcon.cxx:33
 TGIcon.cxx:34
 TGIcon.cxx:35
 TGIcon.cxx:36
 TGIcon.cxx:37
 TGIcon.cxx:38
 TGIcon.cxx:39
 TGIcon.cxx:40
 TGIcon.cxx:41
 TGIcon.cxx:42
 TGIcon.cxx:43
 TGIcon.cxx:44
 TGIcon.cxx:45
 TGIcon.cxx:46
 TGIcon.cxx:47
 TGIcon.cxx:48
 TGIcon.cxx:49
 TGIcon.cxx:50
 TGIcon.cxx:51
 TGIcon.cxx:52
 TGIcon.cxx:53
 TGIcon.cxx:54
 TGIcon.cxx:55
 TGIcon.cxx:56
 TGIcon.cxx:57
 TGIcon.cxx:58
 TGIcon.cxx:59
 TGIcon.cxx:60
 TGIcon.cxx:61
 TGIcon.cxx:62
 TGIcon.cxx:63
 TGIcon.cxx:64
 TGIcon.cxx:65
 TGIcon.cxx:66
 TGIcon.cxx:67
 TGIcon.cxx:68
 TGIcon.cxx:69
 TGIcon.cxx:70
 TGIcon.cxx:71
 TGIcon.cxx:72
 TGIcon.cxx:73
 TGIcon.cxx:74
 TGIcon.cxx:75
 TGIcon.cxx:76
 TGIcon.cxx:77
 TGIcon.cxx:78
 TGIcon.cxx:79
 TGIcon.cxx:80
 TGIcon.cxx:81
 TGIcon.cxx:82
 TGIcon.cxx:83
 TGIcon.cxx:84
 TGIcon.cxx:85
 TGIcon.cxx:86
 TGIcon.cxx:87
 TGIcon.cxx:88
 TGIcon.cxx:89
 TGIcon.cxx:90
 TGIcon.cxx:91
 TGIcon.cxx:92
 TGIcon.cxx:93
 TGIcon.cxx:94
 TGIcon.cxx:95
 TGIcon.cxx:96
 TGIcon.cxx:97
 TGIcon.cxx:98
 TGIcon.cxx:99
 TGIcon.cxx:100
 TGIcon.cxx:101
 TGIcon.cxx:102
 TGIcon.cxx:103
 TGIcon.cxx:104
 TGIcon.cxx:105
 TGIcon.cxx:106
 TGIcon.cxx:107
 TGIcon.cxx:108
 TGIcon.cxx:109
 TGIcon.cxx:110
 TGIcon.cxx:111
 TGIcon.cxx:112
 TGIcon.cxx:113
 TGIcon.cxx:114
 TGIcon.cxx:115
 TGIcon.cxx:116
 TGIcon.cxx:117
 TGIcon.cxx:118
 TGIcon.cxx:119
 TGIcon.cxx:120
 TGIcon.cxx:121
 TGIcon.cxx:122
 TGIcon.cxx:123
 TGIcon.cxx:124
 TGIcon.cxx:125
 TGIcon.cxx:126
 TGIcon.cxx:127
 TGIcon.cxx:128
 TGIcon.cxx:129
 TGIcon.cxx:130
 TGIcon.cxx:131
 TGIcon.cxx:132
 TGIcon.cxx:133
 TGIcon.cxx:134
 TGIcon.cxx:135
 TGIcon.cxx:136
 TGIcon.cxx:137
 TGIcon.cxx:138
 TGIcon.cxx:139
 TGIcon.cxx:140
 TGIcon.cxx:141
 TGIcon.cxx:142
 TGIcon.cxx:143
 TGIcon.cxx:144
 TGIcon.cxx:145
 TGIcon.cxx:146
 TGIcon.cxx:147
 TGIcon.cxx:148
 TGIcon.cxx:149
 TGIcon.cxx:150
 TGIcon.cxx:151
 TGIcon.cxx:152
 TGIcon.cxx:153
 TGIcon.cxx:154
 TGIcon.cxx:155
 TGIcon.cxx:156
 TGIcon.cxx:157
 TGIcon.cxx:158
 TGIcon.cxx:159
 TGIcon.cxx:160
 TGIcon.cxx:161
 TGIcon.cxx:162
 TGIcon.cxx:163
 TGIcon.cxx:164
 TGIcon.cxx:165
 TGIcon.cxx:166
 TGIcon.cxx:167
 TGIcon.cxx:168
 TGIcon.cxx:169
 TGIcon.cxx:170
 TGIcon.cxx:171
 TGIcon.cxx:172
 TGIcon.cxx:173
 TGIcon.cxx:174
 TGIcon.cxx:175
 TGIcon.cxx:176
 TGIcon.cxx:177
 TGIcon.cxx:178
 TGIcon.cxx:179
 TGIcon.cxx:180
 TGIcon.cxx:181
 TGIcon.cxx:182
 TGIcon.cxx:183
 TGIcon.cxx:184
 TGIcon.cxx:185
 TGIcon.cxx:186
 TGIcon.cxx:187
 TGIcon.cxx:188
 TGIcon.cxx:189
 TGIcon.cxx:190
 TGIcon.cxx:191
 TGIcon.cxx:192
 TGIcon.cxx:193
 TGIcon.cxx:194
 TGIcon.cxx:195
 TGIcon.cxx:196
 TGIcon.cxx:197
 TGIcon.cxx:198
 TGIcon.cxx:199
 TGIcon.cxx:200
 TGIcon.cxx:201
 TGIcon.cxx:202
 TGIcon.cxx:203
 TGIcon.cxx:204
 TGIcon.cxx:205
 TGIcon.cxx:206
 TGIcon.cxx:207
 TGIcon.cxx:208
 TGIcon.cxx:209
 TGIcon.cxx:210
 TGIcon.cxx:211
 TGIcon.cxx:212
 TGIcon.cxx:213
 TGIcon.cxx:214
 TGIcon.cxx:215
 TGIcon.cxx:216
 TGIcon.cxx:217
 TGIcon.cxx:218
 TGIcon.cxx:219
 TGIcon.cxx:220
 TGIcon.cxx:221
 TGIcon.cxx:222
 TGIcon.cxx:223
 TGIcon.cxx:224
 TGIcon.cxx:225
 TGIcon.cxx:226
 TGIcon.cxx:227
 TGIcon.cxx:228
 TGIcon.cxx:229
 TGIcon.cxx:230
 TGIcon.cxx:231
 TGIcon.cxx:232
 TGIcon.cxx:233
 TGIcon.cxx:234
 TGIcon.cxx:235
 TGIcon.cxx:236