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

/*************************************************************************
 * 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 "TArrowEditor.h"
#include "TGComboBox.h"
#include "TGLabel.h"
#include "TGNumberEntry.h"
#include "TArrow.h"

ClassImp(TArrowEditor)

enum EArrowWid {
   kARROW_ANG,
   kARROW_OPT,
   kARROW_SIZ
};

//______________________________________________________________________________
/* Begin_Html
<center><h2>Implements user interface for editing of arrow attributes:
shape, size, angle.</h2></center>
The picture below shows this interface.
End_Html
Begin_Macro(GUI)
{
   gROOT->ProcessLine(".x $ROOTSYS/tutorials/graphics/arrow.C");
   TCanvas *c1 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("c1");
   c1->SetBorderMode(0);
   c1->ToggleEditor();
   c1->Selected(c1, new TArrow(), 1);
   TRootCanvas *r = (TRootCanvas*)c1->GetCanvasImp();
   r->SetWindowSize(700,500);
   return r->GetMainFrame();
}
End_Macro */


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

   fArrow = 0;

   MakeTitle("Arrow");

   TGCompositeFrame *f2 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
   AddFrame(f2, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));

   TGCompositeFrame *f2a = new TGCompositeFrame(f2, 80, 20);
   f2->AddFrame(f2a, new TGLayoutHints(kLHintsTop, 10, 0, 0, 0));

   TGLabel *fShapeLabel = new TGLabel(f2a, "Shape:");
   f2a->AddFrame(fShapeLabel, new TGLayoutHints(kLHintsNormal, 0, 0, 1, 5));

   TGLabel *fAngleLabel = new TGLabel(f2a, "Angle:");
   f2a->AddFrame(fAngleLabel, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 5));

   TGLabel *fSizeLabel = new TGLabel(f2a, "Size: ");
   f2a->AddFrame(fSizeLabel, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 1));

   TGCompositeFrame *f2b = new TGCompositeFrame(f2, 80, 20, kFixedWidth);
   f2->AddFrame(f2b, new TGLayoutHints(kLHintsTop, 10, 0, 0, 0));

   fOptionCombo = BuildOptionComboBox(f2b, kARROW_OPT);
   fOptionCombo->Resize(80, 20);
   f2b->AddFrame(fOptionCombo, new TGLayoutHints(kLHintsExpandX, 1, 1, 1, 1));
   fOptionCombo->Associate(this);

   fAngleEntry = new TGNumberEntry(f2b, 30, 8, kARROW_ANG,
                             TGNumberFormat::kNESInteger,
                             TGNumberFormat::kNEANonNegative,
                             TGNumberFormat::kNELLimitMinMax,0, 180);
   fAngleEntry->GetNumberEntry()->SetToolTipText("Set the arrow opening angle in degrees.");
   f2b->AddFrame(fAngleEntry, new TGLayoutHints(kLHintsExpandX, 1, 1, 3, 1));

   fSizeEntry = new TGNumberEntry(f2b, 0.03, 8, kARROW_SIZ,
                                  TGNumberFormat::kNESRealTwo,
                                  TGNumberFormat::kNEANonNegative,
                                  TGNumberFormat::kNELLimitMinMax, 0.01, 0.30);
   fSizeEntry->GetNumberEntry()->SetToolTipText("Set the size of arrow.");
   f2b->AddFrame(fSizeEntry, new TGLayoutHints(kLHintsExpandX, 1, 1, 3, 1));

}

//______________________________________________________________________________
TArrowEditor::~TArrowEditor()
{
   // Destructor of arrow editor.

   TGFrameElement *el;
   TIter next(GetList());

   while ((el = (TGFrameElement *)next())) {
      if (!strcmp(el->fFrame->ClassName(), "TGCompositeFrame"))
         ((TGCompositeFrame *)el->fFrame)->Cleanup();
   }
   Cleanup();
}

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

   fOptionCombo->Connect("Selected(Int_t)", "TArrowEditor", this, "DoOption(Int_t)");
   fAngleEntry->Connect("ValueSet(Long_t)", "TArrowEditor", this, "DoAngle()");
   (fAngleEntry->GetNumberEntry())->Connect("ReturnPressed()", "TArrowEditor", this, "DoAngle()");
   fSizeEntry->Connect("ValueSet(Long_t)", "TArrowEditor", this, "DoSize()");
   (fSizeEntry->GetNumberEntry())->Connect("ReturnPressed()", "TArrowEditor", this, "DoSize()");

   fInit = kFALSE;
}

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

   fArrow = (TArrow *)obj;
   fAvoidSignal = kTRUE;

   Int_t id = GetShapeEntry(fArrow->GetDrawOption());
   if (id != fOptionCombo->GetSelected())
      fOptionCombo->Select(id);

   Float_t sz = fArrow->GetArrowSize();
   fSizeEntry->SetNumber(sz);

   Int_t deg = (Int_t)fArrow->GetAngle();
   fAngleEntry->SetNumber(deg);

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

//______________________________________________________________________________
void TArrowEditor::DoAngle()
{
   // Slot connected to the arrow opening angle setting.

   if (fAvoidSignal) return;
   fArrow->SetAngle((Float_t)fAngleEntry->GetNumber());
   fArrow->Paint(fArrow->GetDrawOption());
   Update();
}

//______________________________________________________________________________
void TArrowEditor::DoOption(Int_t id)
{
   // Slot connected to the arrow shape setting.

   if (fAvoidSignal) return;
   const char* opt=0;
   switch (id) {
      case 1:
         opt = "|>";
         break;
      case 2:
         opt = "<|";
         break;
      case 3:
         opt = ">";
         break;
      case 4:
         opt = "<";
         break;
      case 5:
         opt = "->-";
         break;
      case 6:
         opt = "-<-";
         break;
      case 7:
         opt = "-|>-";
         break;
      case 8:
         opt = "-<|-";
         break;
      case 9:
         opt = "<>";
         break;
      case 10:
         opt = "<|>";
         break;
   }
   fArrow->SetDrawOption(opt);
   fArrow->Paint(fArrow->GetDrawOption());
   Update();
}


//______________________________________________________________________________
void TArrowEditor::DoSize()
{
   // Slot connected to the arrow size.

   if (fAvoidSignal) return;
   fArrow->SetArrowSize(fSizeEntry->GetNumber());
   fArrow->Paint(fArrow->GetDrawOption());
   Update();
}

//______________________________________________________________________________
TGComboBox* TArrowEditor::BuildOptionComboBox(TGFrame* parent, Int_t id)
{
   // Arrow shape combobox.

   TGComboBox *cb = new TGComboBox(parent, id);

   cb->AddEntry(" -------|>",1);
   cb->AddEntry(" <|-------",2);
   cb->AddEntry(" -------->",3);
   cb->AddEntry(" <--------",4);
   cb->AddEntry(" ---->----",5);
   cb->AddEntry(" ----<----",6);
   cb->AddEntry(" ----|>---",7);
   cb->AddEntry(" ---<|----",8);
   cb->AddEntry(" <------>", 9);
   cb->AddEntry(" <|-----|>",10);
   (cb->GetListBox())->Resize((cb->GetListBox())->GetWidth(), 136);
   cb->Select(1);
   return cb;
}

//______________________________________________________________________________
Int_t TArrowEditor::GetShapeEntry(Option_t *option)
{
   // Return shape entry according to the arrow draw option.

   TString opt = option;
   opt.ToLower();
   Int_t id = 0;

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