// @(#)root/ged:$Id$
// Author: Ilka Antcheva   10/05/04

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

//////////////////////////////////////////////////////////////////////////
//                                                                      //
//  TAttFillEditor                                                      //
//                                                                      //
//  Implements GUI for editing fill attributes.                         //                                             //
//             color and fill style                                     //
//                                                                      //
//////////////////////////////////////////////////////////////////////////
//Begin_Html
/*
<img src="gif/TAttFillEditor.gif">
*/
//End_Html

#include "TAttFillEditor.h"
#include "TGedPatternSelect.h"
#include "TGColorSelect.h"
#include "TColor.h"
#include "TGLabel.h"
#include "TGNumberEntry.h"
#include "TPad.h"
#include "TCanvas.h"
#include "TROOT.h"

ClassImp(TAttFillEditor)

enum EFillWid {
   kCOLOR,
   kPATTERN,
   kALPHA,
   kALPHAFIELD
};


//______________________________________________________________________________
TAttFillEditor::TAttFillEditor(const TGWindow *p, Int_t width,
                               Int_t height, UInt_t options, Pixel_t back)
   : TGedFrame(p, width, height, options | kVerticalFrame, back)
{
   // Constructor of fill attributes GUI.

   fPriority = 2;

   fAttFill = 0;

   MakeTitle("Fill");

   TGCompositeFrame *f2 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
   fColorSelect = new TGColorSelect(f2, 0, kCOLOR);
   f2->AddFrame(fColorSelect, new TGLayoutHints(kLHintsLeft, 1, 1, 1, 1));
   fColorSelect->Associate(this);
   fPatternSelect = new TGedPatternSelect(f2, 1, kPATTERN);
   f2->AddFrame(fPatternSelect, new TGLayoutHints(kLHintsLeft, 1, 1, 1, 1));
   fPatternSelect->Associate(this);
   AddFrame(f2, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));

   TGLabel *AlphaLabel = new TGLabel(this,"Opacity");
   AddFrame(AlphaLabel,
            new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
   TGHorizontalFrame *f2a = new TGHorizontalFrame(this);
   fAlpha = new TGHSlider(f2a,100,kSlider2|kScaleNo,kALPHA);
   fAlpha->SetRange(0,1000);
   f2a->AddFrame(fAlpha,new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
   fAlphaField = new TGNumberEntryField(f2a, kALPHAFIELD, 0,
                                        TGNumberFormat::kNESReal,
                                        TGNumberFormat::kNEANonNegative);
   fAlphaField->Resize(40,20);
   if (!TCanvas::SupportAlpha()) {
      fAlpha->SetEnabled(kFALSE);
      AlphaLabel->Disable(kTRUE);
      fAlphaField->SetEnabled(kFALSE);
   }
   f2a->AddFrame(fAlphaField,new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
   AddFrame(f2a, new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
}

//______________________________________________________________________________
TAttFillEditor::~TAttFillEditor()
{
  // Destructor of fill editor.
}

//______________________________________________________________________________
void TAttFillEditor::ConnectSignals2Slots()
{
   // Connect signals to slots.

   fColorSelect->Connect("ColorSelected(Pixel_t)", "TAttFillEditor", this, "DoFillColor(Pixel_t)");
   fColorSelect->Connect("AlphaColorSelected(ULong_t)", "TAttFillEditor", this, "DoFillAlphaColor(ULong_t)");
   fPatternSelect->Connect("PatternSelected(Style_t)", "TAttFillEditor", this, "DoFillPattern(Style_t)");
   fAlpha->Connect("Released()","TAttFillEditor", this, "DoAlpha()");
   fAlpha->Connect("PositionChanged(Int_t)","TAttFillEditor", this, "DoLiveAlpha(Int_t)");
   fAlphaField->Connect("ReturnPressed()","TAttFillEditor", this, "DoAlphaField()");
   fAlpha->Connect("Pressed()","TAttFillEditor", this, "GetCurAlpha()");
   fInit = kFALSE;
}

//______________________________________________________________________________
void TAttFillEditor::SetModel(TObject* obj)
{
   // Pick up the used fill attributes.

   TAttFill *attfill = dynamic_cast<TAttFill *>(obj);
   if (!attfill) return;

   fAttFill = attfill;
   fAvoidSignal = kTRUE;

   Color_t c = fAttFill->GetFillColor();
   Pixel_t p = TColor::Number2Pixel(c);
   fColorSelect->SetColor(p, kFALSE);

   Style_t s = fAttFill->GetFillStyle();
   fPatternSelect->SetPattern(s, kFALSE);

   if (fInit) ConnectSignals2Slots();
   fAvoidSignal = kFALSE;

   if (TColor *color = gROOT->GetColor(fAttFill->GetFillColor())) {
      fAlpha->SetPosition((Int_t)(color->GetAlpha()*1000));
      fAlphaField->SetNumber(color->GetAlpha());
   }
}

//______________________________________________________________________________
void TAttFillEditor::DoFillAlphaColor(ULong_t p)
{
   // Slot connected to the fill area color with alpha.

   TColor *color = (TColor *)p;

   if (fAvoidSignal) return;
   fAttFill->SetFillColor(color->GetNumber());
   fAlpha->SetPosition((Int_t)(color->GetAlpha()*1000));
   fAlphaField->SetNumber(color->GetAlpha());

   Update();
}

//______________________________________________________________________________
void TAttFillEditor::DoFillColor(Pixel_t color)
{
   // Slot connected to the fill area color.

   if (fAvoidSignal) return;
   fAttFill->SetFillColor(TColor::GetColor(color));

   if (TColor *tcolor = gROOT->GetColor(TColor::GetColor(color))) {
      fAlpha->SetPosition((Int_t)(tcolor->GetAlpha()*1000));
      fAlphaField->SetNumber(tcolor->GetAlpha());
   }

   Update();
}

//______________________________________________________________________________
void TAttFillEditor::DoFillPattern(Style_t pattern)
{
   // Slot connected to the fill area pattern.

   if (fAvoidSignal) return;
   fAttFill->SetFillStyle(pattern);
   Update();
}

//______________________________________________________________________________
void TAttFillEditor::DoAlphaField()
{
   // Slot to set the alpha value from the entry field.

   if (fAvoidSignal) return;

   if (TColor *color = gROOT->GetColor(fAttFill->GetFillColor())) {
      color->SetAlpha((Float_t)fAlphaField->GetNumber());
      fAlpha->SetPosition((Int_t)fAlphaField->GetNumber()*1000);
   }
   Update();
}

//______________________________________________________________________________
void TAttFillEditor::DoAlpha()
{
   // Slot to set the alpha value

   if (fAvoidSignal) return;

   if (TColor *color = gROOT->GetColor(fAttFill->GetFillColor())) {
      color->SetAlpha((Float_t)fAlpha->GetPosition()/1000);
      fAlphaField->SetNumber((Float_t)fAlpha->GetPosition()/1000);
   }
   Update();
}

//______________________________________________________________________________
void TAttFillEditor::DoLiveAlpha(Int_t a)
{
   // Slot to set alpha value online.

   if (fAvoidSignal) return;
   fAlphaField->SetNumber((Float_t)a/1000);

   if (TColor *color = gROOT->GetColor(fAttFill->GetFillColor()))  {
      // In case the color is not transparent a new color is created.
      if (color->GetAlpha() == 1.) {
         fAttFill->SetFillColor(TColor::GetColorTransparent(color->GetNumber(),0.99));
      } else {
         color->SetAlpha((Float_t)a/1000);
      }
   }
   Update();
}

//_______________________________________________________________________________
void TAttFillEditor::GetCurAlpha()
{
   // Slot to update alpha value on click on Slider

   if (fAvoidSignal) return;

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