// @(#)root/ged:$Id$
// Author: Denis Favre-Miville   08/09/05

/*************************************************************************
 * 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.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
//  TStyleDialog                                                        //
//                                                                      //
//  This small class is useful to ask the user for a name and a title,  //
//       in order to rename a style, create a new style or import a     //
//       style from a canvas.                                           //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TStyleDialog.h"
#include "TStyleManager.h"

#include <TCanvas.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TGLabel.h>
#include <TGLayout.h>
#include <TGTextEntry.h>
#include <TROOT.h>
#include <TStyle.h>
#include <TVirtualPad.h>
#include <TVirtualMutex.h>
#include <stdlib.h>

ClassImp(TStyleDialog)

enum EStyleDialogWid {
   kName,
   kTitle,
   kButOK,
   kButCancel
};

//______________________________________________________________________________
TStyleDialog::TStyleDialog(TStyleManager *sm, TStyle *cur, Int_t mode,
                              TVirtualPad *currentPad)
                     : TGTransientFrame(0, sm)
{
   //  Constructor. Create the dialog window and draw it centered over the
   // main window 'mf'. A pointer to the style to copy or rename is given
   // by 'cur' and the parameter 'mode' specify the mode:
   //    1 = copy  |  2 = rename  |  3 = import from canvas.

   fStyleManager = sm;

   // Create the main frame.
   SetCleanup(kNoCleanup);
   fCurStyle = cur;
   fMode = mode;
   fCurPad = currentPad;

   switch (fMode) {
      case 1:  SetWindowName("Create a New Style");            break;
      case 2:  SetWindowName("Rename the Selected Style");     break;
      case 3:  SetWindowName("Import a New Style from Canvas");
   }

   // Create the trash lists to have an effective deletion of every object.
   fTrashListLayout = new TList();
   fTrashListFrame = new TList();

   // Create the layouts and add them to the layout trash list.
   TGLayoutHints *layoutNameLabel = new TGLayoutHints(kLHintsNormal, 0, 70, 3);
   fTrashListLayout->Add(layoutNameLabel);
   TGLayoutHints *layoutTitleLabel = new TGLayoutHints(kLHintsNormal, 0, 39, 3);
   fTrashListLayout->Add(layoutTitleLabel);
   TGLayoutHints *layoutWarningLabel = new TGLayoutHints(kLHintsExpandX);
   fTrashListLayout->Add(layoutWarningLabel);
   TGLayoutHints *layoutOKButton = new TGLayoutHints(kLHintsExpandX, 0, 5);
   fTrashListLayout->Add(layoutOKButton);
   TGLayoutHints *layoutCancelButton = new TGLayoutHints(kLHintsExpandX, 5);
   fTrashListLayout->Add(layoutCancelButton);
   TGLayoutHints *layoutH1 = new TGLayoutHints(kLHintsExpandX, 10, 10, 10, 5);
   fTrashListLayout->Add(layoutH1);
   TGLayoutHints *layoutH2 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 5);
   fTrashListLayout->Add(layoutH2);
   TGLayoutHints *layoutH4 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 10);
   fTrashListLayout->Add(layoutH4);

   // Create and place the widgets in the main window.
   // Every frame created here must be added to the frame trash list.
   TGHorizontalFrame *h1 = new TGHorizontalFrame(this);
   fTrashListFrame->Add(h1);
   fNameLabel = new TGLabel(h1, "Name:");
   h1->AddFrame(fNameLabel, layoutNameLabel);

   if (fMode == 1) {
      TString newName;
      newName.Form("%s_1", fCurStyle->GetName());
      fName = new TGTextEntry(h1, newName.Data(), kName);
   } else if (fMode == 2) {
      // The names of the 5 basics styles can not be modified.
      fName = new TGTextEntry(h1, fCurStyle->GetName(), kName);
      if ((!strcmp(fName->GetText(), "Default"))
       || (!strcmp(fName->GetText(), "Plain"  ))
       || (!strcmp(fName->GetText(), "Bold"   ))
       || (!strcmp(fName->GetText(), "Video"  ))
       || (!strcmp(fName->GetText(), "Pub"    ))) fName->SetEnabled(kFALSE);
   } else
      fName = new TGTextEntry(h1, "Imported_Style", kName);
   fName->Associate(this);
   fName->Resize(200, 22);
   h1->AddFrame(fName);
   AddFrame(h1, layoutH1);

   TGHorizontalFrame *h2 = new TGHorizontalFrame(this);
   fTrashListFrame->Add(h2);
   fTitleLabel = new TGLabel(h2, "Description:");
   h2->AddFrame(fTitleLabel, layoutTitleLabel);
   switch (fMode) {
      case 1:
      case 2:
         fTitle = new TGTextEntry(h2, fCurStyle->GetTitle(), kTitle);
         break;
      case 3: {
         TString newTitle("Imported from canvas ");
         if (fCurPad->GetCanvas())
            newTitle += fCurPad->GetCanvas()->GetName();
         fTitle = new TGTextEntry(h2, newTitle.Data(), kTitle);
      }
   }
   fTitle->Associate(this);
   fTitle->Resize(200, 22);
   h2->AddFrame(fTitle);
   fTitle->Associate(h2);
   AddFrame(h2, layoutH2);

   TGHorizontalFrame *h3 = new TGHorizontalFrame(this);
   fTrashListFrame->Add(h3);
   fWarnLabel = new TGLabel(h3);
   Pixel_t red;
   gClient->GetColorByName("#FF0000", red);
   fWarnLabel->SetTextColor(red, kFALSE);
   fWarnLabel->Resize(200, 22);
   h3->AddFrame(fWarnLabel, layoutWarningLabel);
   AddFrame(h3, layoutH2);

   TGHorizontalFrame *h4 = new TGHorizontalFrame(this);
   fTrashListFrame->Add(h4);
   fOK = new TGTextButton(h4, "&OK", kButOK);
   fOK->Associate(this);
   h4->AddFrame(fOK, layoutOKButton);
   fOK->Associate(h4);
   fCancel = new TGTextButton(h4, "&Cancel", kButCancel);
   fCancel->Associate(this);
   h4->AddFrame(fCancel, layoutCancelButton);
   fCancel->Associate(h4);
   AddFrame(h4, layoutH4);

   // Refresh the warning message.
   DoUpdate();

   Resize();
   CenterOnParent();
   MapSubwindows();
   Int_t w = GetDefaultWidth();
   Int_t h = GetDefaultHeight();
   SetWMSizeHints(w, h, w, h, 0, 0);
   MapWindow();

   switch (fMode) {
      case 1:
         fOK->SetToolTipText("Create this new style");
         fCancel->SetToolTipText("Cancel the creation ");
         break;
      case 2:
         fOK->SetToolTipText("Rename the selected style");
         fCancel->SetToolTipText("Cancel the rename ");
         break;
      case 3:
         fOK->SetToolTipText("Import this new style from the canvas");
         fCancel->SetToolTipText("Cancel the import");
         break;
   }

   Connect("CloseWindow()", "TStyleDialog", this, "DoCloseWindow()");
   fName->Connect("TextChanged(const char *)", "TStyleDialog", this, "DoUpdate()");
   fOK->Connect("Clicked()", "TStyleDialog", this, "DoOK()");
   fCancel->Connect("Clicked()", "TStyleDialog", this, "DoCancel()");

   gClient->WaitFor(this);
}

//______________________________________________________________________________
TStyleDialog::~TStyleDialog()
{
   // Destructor.

   Disconnect("DoCloseWindow()");
   fName->Disconnect("TextChanged(const char *)");
   fOK->Disconnect("Clicked()");
   fCancel->Disconnect("Clicked()");

   delete fName;
   delete fNameLabel;
   delete fTitle;
   delete fTitleLabel;
   delete fWarnLabel;
   delete fOK;
   delete fCancel;

   TObject *obj1;
   TObject *obj2;

   obj1 = fTrashListFrame->First();
   while (obj1) {
      obj2 = fTrashListFrame->After(obj1);
      fTrashListFrame->Remove(obj1);
      delete obj1;
      obj1 = obj2;
   }
   delete fTrashListFrame;

   obj1 = fTrashListLayout->First();
   while (obj1) {
      obj2 = fTrashListLayout->After(obj1);
      fTrashListLayout->Remove(obj1);
      delete obj1;
      obj1 = obj2;
   }
   delete fTrashListLayout;
}

//______________________________________________________________________________
void TStyleDialog::DoCancel()
{
   //  Slot called when the Cancel button is clicked. Close the window
   // without saving submitted changes.

   fStyleManager->SetLastChoice(kFALSE);

   SendCloseMessage();
}

//______________________________________________________________________________
void TStyleDialog::DoCloseWindow()
{
   //  Slot called when the window is closed via the window manager.
   // Close the window without saving submitted changes.

   delete this;
}

//______________________________________________________________________________
void TStyleDialog::DoOK()
{
   //  Slot called when the OK button is clicked. Rename or create the style
   // before closing the window.

   if (fMode == 2) {
      // Update the name and the title of the style.
      fCurStyle->SetName(fName->GetText());
      fCurStyle->SetTitle(fTitle->GetText());
   } else {
      // Create a new style (copy of fCurStyle), with the given name and title.
      TStyle *tmpStyle = new TStyle(*fCurStyle);
      tmpStyle->SetName(fName->GetText());
      tmpStyle->SetTitle(fTitle->GetText());
      {
         R__LOCKGUARD2(gROOTMutex);
         gROOT->GetListOfStyles()->Add(tmpStyle);
      }
      if (fMode == 3) {
         // Import the properties of the canvas.
         TStyle *tmp = gStyle;
         gStyle = tmpStyle;
         gStyle->SetIsReading(kFALSE);
         if (fCurPad->GetCanvas())
            fCurPad->GetCanvas()->UseCurrentStyle();
         gStyle->SetIsReading(kTRUE);
         gStyle = tmp;
      }
   }

   fStyleManager->SetLastChoice(kTRUE);

   SendCloseMessage();
}

//______________________________________________________________________________
void TStyleDialog::DoUpdate()
{
   //  Slot called every time the name is changed. Provide some protection
   // to avoid letting the user use an empty name or an already used one.
   //  A warning message can be shown and the OK button disabled.

   if (!strlen(fName->GetText())) {
      fWarnLabel->SetText("That name is empty");
      fOK->SetEnabled(kFALSE);
      return;
   }

   if (strstr(fName->GetText(), " ") != 0) {
      fWarnLabel->SetText("That name contains some spaces");
      fOK->SetEnabled(kFALSE);
      return;
   }

   switch (fMode) {
      case 1:
      case 3:
         if (gROOT->GetStyle(fName->GetText())) {
            fWarnLabel->SetText("That name is already used by another style.");
            fOK->SetEnabled(kFALSE);
            return;
         }
         break;
      case 2:
         TStyle *tmp = gROOT->GetStyle(fName->GetText());
         if (tmp && (tmp != fCurStyle)) {
            fWarnLabel->SetText("That name is already used by another style.");
            fOK->SetEnabled(kFALSE);
            return;
         }
   }

   fWarnLabel->SetText("");
   fOK->SetEnabled(kTRUE);
}
 TStyleDialog.cxx:1
 TStyleDialog.cxx:2
 TStyleDialog.cxx:3
 TStyleDialog.cxx:4
 TStyleDialog.cxx:5
 TStyleDialog.cxx:6
 TStyleDialog.cxx:7
 TStyleDialog.cxx:8
 TStyleDialog.cxx:9
 TStyleDialog.cxx:10
 TStyleDialog.cxx:11
 TStyleDialog.cxx:12
 TStyleDialog.cxx:13
 TStyleDialog.cxx:14
 TStyleDialog.cxx:15
 TStyleDialog.cxx:16
 TStyleDialog.cxx:17
 TStyleDialog.cxx:18
 TStyleDialog.cxx:19
 TStyleDialog.cxx:20
 TStyleDialog.cxx:21
 TStyleDialog.cxx:22
 TStyleDialog.cxx:23
 TStyleDialog.cxx:24
 TStyleDialog.cxx:25
 TStyleDialog.cxx:26
 TStyleDialog.cxx:27
 TStyleDialog.cxx:28
 TStyleDialog.cxx:29
 TStyleDialog.cxx:30
 TStyleDialog.cxx:31
 TStyleDialog.cxx:32
 TStyleDialog.cxx:33
 TStyleDialog.cxx:34
 TStyleDialog.cxx:35
 TStyleDialog.cxx:36
 TStyleDialog.cxx:37
 TStyleDialog.cxx:38
 TStyleDialog.cxx:39
 TStyleDialog.cxx:40
 TStyleDialog.cxx:41
 TStyleDialog.cxx:42
 TStyleDialog.cxx:43
 TStyleDialog.cxx:44
 TStyleDialog.cxx:45
 TStyleDialog.cxx:46
 TStyleDialog.cxx:47
 TStyleDialog.cxx:48
 TStyleDialog.cxx:49
 TStyleDialog.cxx:50
 TStyleDialog.cxx:51
 TStyleDialog.cxx:52
 TStyleDialog.cxx:53
 TStyleDialog.cxx:54
 TStyleDialog.cxx:55
 TStyleDialog.cxx:56
 TStyleDialog.cxx:57
 TStyleDialog.cxx:58
 TStyleDialog.cxx:59
 TStyleDialog.cxx:60
 TStyleDialog.cxx:61
 TStyleDialog.cxx:62
 TStyleDialog.cxx:63
 TStyleDialog.cxx:64
 TStyleDialog.cxx:65
 TStyleDialog.cxx:66
 TStyleDialog.cxx:67
 TStyleDialog.cxx:68
 TStyleDialog.cxx:69
 TStyleDialog.cxx:70
 TStyleDialog.cxx:71
 TStyleDialog.cxx:72
 TStyleDialog.cxx:73
 TStyleDialog.cxx:74
 TStyleDialog.cxx:75
 TStyleDialog.cxx:76
 TStyleDialog.cxx:77
 TStyleDialog.cxx:78
 TStyleDialog.cxx:79
 TStyleDialog.cxx:80
 TStyleDialog.cxx:81
 TStyleDialog.cxx:82
 TStyleDialog.cxx:83
 TStyleDialog.cxx:84
 TStyleDialog.cxx:85
 TStyleDialog.cxx:86
 TStyleDialog.cxx:87
 TStyleDialog.cxx:88
 TStyleDialog.cxx:89
 TStyleDialog.cxx:90
 TStyleDialog.cxx:91
 TStyleDialog.cxx:92
 TStyleDialog.cxx:93
 TStyleDialog.cxx:94
 TStyleDialog.cxx:95
 TStyleDialog.cxx:96
 TStyleDialog.cxx:97
 TStyleDialog.cxx:98
 TStyleDialog.cxx:99
 TStyleDialog.cxx:100
 TStyleDialog.cxx:101
 TStyleDialog.cxx:102
 TStyleDialog.cxx:103
 TStyleDialog.cxx:104
 TStyleDialog.cxx:105
 TStyleDialog.cxx:106
 TStyleDialog.cxx:107
 TStyleDialog.cxx:108
 TStyleDialog.cxx:109
 TStyleDialog.cxx:110
 TStyleDialog.cxx:111
 TStyleDialog.cxx:112
 TStyleDialog.cxx:113
 TStyleDialog.cxx:114
 TStyleDialog.cxx:115
 TStyleDialog.cxx:116
 TStyleDialog.cxx:117
 TStyleDialog.cxx:118
 TStyleDialog.cxx:119
 TStyleDialog.cxx:120
 TStyleDialog.cxx:121
 TStyleDialog.cxx:122
 TStyleDialog.cxx:123
 TStyleDialog.cxx:124
 TStyleDialog.cxx:125
 TStyleDialog.cxx:126
 TStyleDialog.cxx:127
 TStyleDialog.cxx:128
 TStyleDialog.cxx:129
 TStyleDialog.cxx:130
 TStyleDialog.cxx:131
 TStyleDialog.cxx:132
 TStyleDialog.cxx:133
 TStyleDialog.cxx:134
 TStyleDialog.cxx:135
 TStyleDialog.cxx:136
 TStyleDialog.cxx:137
 TStyleDialog.cxx:138
 TStyleDialog.cxx:139
 TStyleDialog.cxx:140
 TStyleDialog.cxx:141
 TStyleDialog.cxx:142
 TStyleDialog.cxx:143
 TStyleDialog.cxx:144
 TStyleDialog.cxx:145
 TStyleDialog.cxx:146
 TStyleDialog.cxx:147
 TStyleDialog.cxx:148
 TStyleDialog.cxx:149
 TStyleDialog.cxx:150
 TStyleDialog.cxx:151
 TStyleDialog.cxx:152
 TStyleDialog.cxx:153
 TStyleDialog.cxx:154
 TStyleDialog.cxx:155
 TStyleDialog.cxx:156
 TStyleDialog.cxx:157
 TStyleDialog.cxx:158
 TStyleDialog.cxx:159
 TStyleDialog.cxx:160
 TStyleDialog.cxx:161
 TStyleDialog.cxx:162
 TStyleDialog.cxx:163
 TStyleDialog.cxx:164
 TStyleDialog.cxx:165
 TStyleDialog.cxx:166
 TStyleDialog.cxx:167
 TStyleDialog.cxx:168
 TStyleDialog.cxx:169
 TStyleDialog.cxx:170
 TStyleDialog.cxx:171
 TStyleDialog.cxx:172
 TStyleDialog.cxx:173
 TStyleDialog.cxx:174
 TStyleDialog.cxx:175
 TStyleDialog.cxx:176
 TStyleDialog.cxx:177
 TStyleDialog.cxx:178
 TStyleDialog.cxx:179
 TStyleDialog.cxx:180
 TStyleDialog.cxx:181
 TStyleDialog.cxx:182
 TStyleDialog.cxx:183
 TStyleDialog.cxx:184
 TStyleDialog.cxx:185
 TStyleDialog.cxx:186
 TStyleDialog.cxx:187
 TStyleDialog.cxx:188
 TStyleDialog.cxx:189
 TStyleDialog.cxx:190
 TStyleDialog.cxx:191
 TStyleDialog.cxx:192
 TStyleDialog.cxx:193
 TStyleDialog.cxx:194
 TStyleDialog.cxx:195
 TStyleDialog.cxx:196
 TStyleDialog.cxx:197
 TStyleDialog.cxx:198
 TStyleDialog.cxx:199
 TStyleDialog.cxx:200
 TStyleDialog.cxx:201
 TStyleDialog.cxx:202
 TStyleDialog.cxx:203
 TStyleDialog.cxx:204
 TStyleDialog.cxx:205
 TStyleDialog.cxx:206
 TStyleDialog.cxx:207
 TStyleDialog.cxx:208
 TStyleDialog.cxx:209
 TStyleDialog.cxx:210
 TStyleDialog.cxx:211
 TStyleDialog.cxx:212
 TStyleDialog.cxx:213
 TStyleDialog.cxx:214
 TStyleDialog.cxx:215
 TStyleDialog.cxx:216
 TStyleDialog.cxx:217
 TStyleDialog.cxx:218
 TStyleDialog.cxx:219
 TStyleDialog.cxx:220
 TStyleDialog.cxx:221
 TStyleDialog.cxx:222
 TStyleDialog.cxx:223
 TStyleDialog.cxx:224
 TStyleDialog.cxx:225
 TStyleDialog.cxx:226
 TStyleDialog.cxx:227
 TStyleDialog.cxx:228
 TStyleDialog.cxx:229
 TStyleDialog.cxx:230
 TStyleDialog.cxx:231
 TStyleDialog.cxx:232
 TStyleDialog.cxx:233
 TStyleDialog.cxx:234
 TStyleDialog.cxx:235
 TStyleDialog.cxx:236
 TStyleDialog.cxx:237
 TStyleDialog.cxx:238
 TStyleDialog.cxx:239
 TStyleDialog.cxx:240
 TStyleDialog.cxx:241
 TStyleDialog.cxx:242
 TStyleDialog.cxx:243
 TStyleDialog.cxx:244
 TStyleDialog.cxx:245
 TStyleDialog.cxx:246
 TStyleDialog.cxx:247
 TStyleDialog.cxx:248
 TStyleDialog.cxx:249
 TStyleDialog.cxx:250
 TStyleDialog.cxx:251
 TStyleDialog.cxx:252
 TStyleDialog.cxx:253
 TStyleDialog.cxx:254
 TStyleDialog.cxx:255
 TStyleDialog.cxx:256
 TStyleDialog.cxx:257
 TStyleDialog.cxx:258
 TStyleDialog.cxx:259
 TStyleDialog.cxx:260
 TStyleDialog.cxx:261
 TStyleDialog.cxx:262
 TStyleDialog.cxx:263
 TStyleDialog.cxx:264
 TStyleDialog.cxx:265
 TStyleDialog.cxx:266
 TStyleDialog.cxx:267
 TStyleDialog.cxx:268
 TStyleDialog.cxx:269
 TStyleDialog.cxx:270
 TStyleDialog.cxx:271
 TStyleDialog.cxx:272
 TStyleDialog.cxx:273
 TStyleDialog.cxx:274
 TStyleDialog.cxx:275
 TStyleDialog.cxx:276
 TStyleDialog.cxx:277
 TStyleDialog.cxx:278
 TStyleDialog.cxx:279
 TStyleDialog.cxx:280
 TStyleDialog.cxx:281
 TStyleDialog.cxx:282
 TStyleDialog.cxx:283
 TStyleDialog.cxx:284
 TStyleDialog.cxx:285
 TStyleDialog.cxx:286
 TStyleDialog.cxx:287
 TStyleDialog.cxx:288
 TStyleDialog.cxx:289
 TStyleDialog.cxx:290
 TStyleDialog.cxx:291
 TStyleDialog.cxx:292
 TStyleDialog.cxx:293
 TStyleDialog.cxx:294
 TStyleDialog.cxx:295
 TStyleDialog.cxx:296
 TStyleDialog.cxx:297
 TStyleDialog.cxx:298
 TStyleDialog.cxx:299
 TStyleDialog.cxx:300
 TStyleDialog.cxx:301
 TStyleDialog.cxx:302
 TStyleDialog.cxx:303
 TStyleDialog.cxx:304
 TStyleDialog.cxx:305
 TStyleDialog.cxx:306
 TStyleDialog.cxx:307
 TStyleDialog.cxx:308
 TStyleDialog.cxx:309
 TStyleDialog.cxx:310
 TStyleDialog.cxx:311
 TStyleDialog.cxx:312
 TStyleDialog.cxx:313
 TStyleDialog.cxx:314
 TStyleDialog.cxx:315
 TStyleDialog.cxx:316
 TStyleDialog.cxx:317
 TStyleDialog.cxx:318
 TStyleDialog.cxx:319
 TStyleDialog.cxx:320
 TStyleDialog.cxx:321
 TStyleDialog.cxx:322
 TStyleDialog.cxx:323
 TStyleDialog.cxx:324
 TStyleDialog.cxx:325
 TStyleDialog.cxx:326
 TStyleDialog.cxx:327
 TStyleDialog.cxx:328
 TStyleDialog.cxx:329
 TStyleDialog.cxx:330
 TStyleDialog.cxx:331