// @(#)root/gl:$Id$
// Author:  Matevz Tadel, Jun 2007

/*************************************************************************
 * Copyright (C) 1995-2004, 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 "TGLClipSetEditor.h"
#include "TGLClip.h"
#include "TVirtualPad.h"
#include "TColor.h"
#include "TGLabel.h"
#include "TGButton.h"
#include "TGButtonGroup.h"
#include "TGNumberEntry.h"

#include "TGLViewerEditor.h"

//______________________________________________________________________________
//
// GUI sub-editor for TGLClipSet.


ClassImp(TGLClipSetSubEditor);

//______________________________________________________________________________
TGLClipSetSubEditor::TGLClipSetSubEditor(const TGWindow *p) :
   TGVerticalFrame(p),
   fM(0),
   fCurrentClip(TGLClip::kClipNone),
   fTypeButtons(0),
   fPlanePropFrame(0),
   fPlaneProp(),
   fBoxPropFrame(0),
   fBoxProp(),
   fClipInside(0),
   fAutoUpdate(0),
   fClipEdit(0),
   fClipShow(0),
   fApplyButton(0),
   fResetButton(0)
{
   // Constructor.

   fTypeButtons = new TGButtonGroup(this, "Clip Type", kChildFrame|kHorizontalFrame);
   new TGRadioButton(fTypeButtons, "None    ");
   new TGRadioButton(fTypeButtons, "Plane    ");
   new TGRadioButton(fTypeButtons, "Box");
   fTypeButtons->SetLayoutHints(new TGLayoutHints(kLHintsLeft|kLHintsBottom, 0, 0, 2, -10));
   AddFrame(fTypeButtons, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));
   // Clip inside / edit
   fClipInside = new TGCheckButton(this, "Clip away inside");
   AddFrame(fClipInside, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));
   fAutoUpdate = new TGCheckButton(this, "Auto update clip");
   AddFrame(fAutoUpdate, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));
   fClipEdit   = new TGCheckButton(this, "Edit In Viewer");
   AddFrame(fClipEdit, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));
   fClipShow   = new TGCheckButton(this, "Show In Viewer");
   AddFrame(fClipShow, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));

   // Plane properties
   fPlanePropFrame = new TGCompositeFrame(this);
   AddFrame(fPlanePropFrame, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));

   static const char * const planeStr[] = { "aX + ", "bY +", "cZ + ", "d = 0" };

   for (Int_t i = 0; i < 4; ++i)
      fPlaneProp[i] = TGLViewerEditor::MakeLabeledNEntry(fPlanePropFrame, planeStr[i] , 40);

   // Box properties
   fBoxPropFrame = new TGCompositeFrame(this);
   AddFrame(fBoxPropFrame, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));

   static const char * const boxStr[] = {"Center X", "Center Y", "Center Z", "Length X", "Length Y", "Length Z" };

   for (Int_t i = 0; i < 6; ++i)
      fBoxProp[i] = TGLViewerEditor::MakeLabeledNEntry(fBoxPropFrame, boxStr[i] , 60);

   // Apply button
   fApplyButton = new TGTextButton(this, "Apply");
   AddFrame(fApplyButton, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));

   fResetButton = new TGTextButton(this, "Reset");
   AddFrame(fResetButton, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, 2, 3, 2, 2));

   fTypeButtons->Connect("Clicked(Int_t)", "TGLClipSetSubEditor", this, "ClipTypeChanged(Int_t)");
   fClipInside->Connect("Clicked()", "TGLClipSetSubEditor", this, "UpdateViewerClip()");
   fAutoUpdate->Connect("Clicked()", "TGLClipSetSubEditor", this, "UpdateViewerClip()");
   fClipEdit->Connect("Clicked()", "TGLClipSetSubEditor", this, "UpdateViewerClip()");
   fClipShow->Connect("Clicked()", "TGLClipSetSubEditor", this, "UpdateViewerClip()");

   for (Int_t i = 0; i < 4; ++i)
      fPlaneProp[i]->Connect("ValueSet(Long_t)", "TGLClipSetSubEditor", this, "ClipValueChanged()");

   for (Int_t i = 0; i < 6; ++i)
      fBoxProp[i]->Connect("ValueSet(Long_t)", "TGLClipSetSubEditor", this, "ClipValueChanged()");

   fApplyButton->Connect("Pressed()", "TGLClipSetSubEditor", this, "UpdateViewerClip()");
   fResetButton->Connect("Pressed()", "TGLClipSetSubEditor", this, "ResetViewerClip()");
}


//______________________________________________________________________________
void TGLClipSetSubEditor::SetModel(TGLClipSet* m)
{
   // Set model object.

   fM = m;

   fTypeButtons->GetButton(fCurrentClip+1)->SetDown(kFALSE);
   fCurrentClip = fM->GetClipType();

   Double_t clip[6] = {0.};
   fM->GetClipState(fCurrentClip, clip);

   fApplyButton->SetState(kButtonDisabled);

   // Button ids run from 1
   if (TGButton *btn = fTypeButtons->GetButton(fCurrentClip+1)) {
      btn->SetDown();
      fTypeButtons->SetButton(fCurrentClip+1);
   }
   Bool_t active = (fCurrentClip != TGLClip::kClipNone);
   fClipInside->SetEnabled(active);
   fAutoUpdate->SetEnabled(active);
   fClipEdit  ->SetEnabled(active);
   fClipShow  ->SetEnabled(active);
   if (active) {
      fClipEdit->SetDown(fM->GetShowManip());
      fClipShow->SetDown(fM->GetShowClip());
      fClipInside->SetDown(fM->GetCurrentClip()->GetMode() == TGLClip::kInside);
      fAutoUpdate->SetDown(fM->GetAutoUpdate());

      if (fCurrentClip == TGLClip::kClipPlane) {
         HideFrame(fBoxPropFrame);
         ShowFrame(fPlanePropFrame);
         for (Int_t i = 0; i < 4; ++i)
            fPlaneProp[i]->SetNumber(clip[i]);
      } else if (fCurrentClip == TGLClip::kClipBox) {
         HideFrame(fPlanePropFrame);
         ShowFrame(fBoxPropFrame);
         for (Int_t i = 0; i < 6; ++i)
            fBoxProp[i]->SetNumber(clip[i]);
      }

      fResetButton->SetState(kButtonUp);
   } else {
      HideFrame(fPlanePropFrame);
      HideFrame(fBoxPropFrame);

      fResetButton->SetState(kButtonDisabled);
   }
}


//______________________________________________________________________________
void TGLClipSetSubEditor::Changed()
{
   // Emit Changed signal.

   Emit("Changed()");
}


//______________________________________________________________________________
void TGLClipSetSubEditor::ClipValueChanged()
{
   // One of number entries was changed.

   fApplyButton->SetState(kButtonUp);
}


//______________________________________________________________________________
void TGLClipSetSubEditor::ClipTypeChanged(Int_t id)
{
   // Clip type radio button changed - update viewer.

   switch (id)
   {
      case 2:  fCurrentClip = TGLClip::kClipPlane; break;
      case 3:  fCurrentClip = TGLClip::kClipBox;   break;
      default: fCurrentClip = TGLClip::kClipNone;  break;
   }
   fM->SetClipType(fCurrentClip);
   SetModel(fM);
   ((TGMainFrame*)GetMainFrame())->Layout();
   Changed();
}


//______________________________________________________________________________
void TGLClipSetSubEditor::UpdateViewerClip()
{
   // Change clipping volume.

   Double_t data[6] = {0.};
   // Fetch GUI state for clip if 'type' into 'data' vector
   if (fCurrentClip == TGLClip::kClipPlane)
      for (Int_t i = 0; i < 4; ++i)
         data[i] = fPlaneProp[i]->GetNumber();
   else if (fCurrentClip == TGLClip::kClipBox)
      for (Int_t i = 0; i < 6; ++i)
         data[i] = fBoxProp[i]->GetNumber();

   fApplyButton->SetState(kButtonDisabled);

   fM->SetClipState(fCurrentClip, data);
   fM->SetShowManip(fClipEdit->IsDown());
   fM->SetShowClip (fClipShow->IsDown());
   if (fCurrentClip != TGLClip::kClipNone)
      fM->GetCurrentClip()->SetMode(fClipInside->IsDown() ? TGLClip::kInside : TGLClip::kOutside);
   fM->SetAutoUpdate(fAutoUpdate->IsDown());

   Changed();
}

//______________________________________________________________________________
void TGLClipSetSubEditor::ResetViewerClip()
{
   // Reset transformation of the current clip.

   if (fCurrentClip != TGLClip::kClipNone) {
      if (fM->GetClipType() == TGLClip::kClipPlane) {
         TGLPlane plane(0.0, -1.0, 0.0, 0.0);
         dynamic_cast<TGLClipPlane*>(fM->GetCurrentClip())->Set(plane);
      } else if (fM->GetClipType() == TGLClip::kClipBox) {
         fM->GetCurrentClip()->SetTransform(TGLMatrix());
      }
      Changed();
   }
}


//______________________________________________________________________________
//
// GUI editor for TGLClipSet.

ClassImp(TGLClipSetEditor);

//______________________________________________________________________________
TGLClipSetEditor::TGLClipSetEditor(const TGWindow *p, Int_t width, Int_t height,
                                   UInt_t options, Pixel_t back) :
   TGedFrame(p, width, height, options | kVerticalFrame, back),
   fM  (0),
   fSE (0)
{
   // Constructor.

   MakeTitle("TGLClipSet");

   fSE = new TGLClipSetSubEditor(this);
   AddFrame(fSE, new TGLayoutHints(kLHintsTop, 2, 0, 2, 2));
   fSE->Connect("Changed()", "TGLClipSetEditor", this, "Update()");
}


//______________________________________________________________________________
void TGLClipSetEditor::SetModel(TObject* obj)
{
   // Set model object.

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