TStyleDialog.cxx

Go to the documentation of this file.
00001 // @(#)root/ged:$Id: TStyleDialog.cxx,v 1.0 2005/09/08
00002 // Author: Denis Favre-Miville   08/09/05
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 //  TStyleDialog                                                        //
00015 //                                                                      //
00016 //  This small class is useful to ask the user for a name and a title,  //
00017 //       in order to rename a style, create a new style or import a     //
00018 //       style from a canvas.                                           //
00019 //                                                                      //
00020 //////////////////////////////////////////////////////////////////////////
00021 
00022 #include "TStyleDialog.h"
00023 #include "TStyleManager.h"
00024 
00025 #include <TCanvas.h>
00026 #include <TGButton.h>
00027 #include <TGFrame.h>
00028 #include <TGLabel.h>
00029 #include <TGLayout.h>
00030 #include <TGTextEntry.h>
00031 #include <TROOT.h>
00032 #include <TStyle.h>
00033 #include <TVirtualPad.h>
00034 #include <TVirtualMutex.h>
00035 #include <stdlib.h>
00036 
00037 ClassImp(TStyleDialog)
00038 
00039 enum EStyleDialogWid {
00040    kName,
00041    kTitle,
00042    kButOK,
00043    kButCancel
00044 };
00045 
00046 //______________________________________________________________________________
00047 TStyleDialog::TStyleDialog(TStyleManager *sm, TStyle *cur, Int_t mode,
00048                               TVirtualPad *currentPad)
00049                      : TGTransientFrame(0, sm)
00050 {
00051    //  Constructor. Create the dialog window and draw it centered over the
00052    // main window 'mf'. A pointer to the style to copy or rename is given
00053    // by 'cur' and the parameter 'mode' specify the mode:
00054    //    1 = copy  |  2 = rename  |  3 = import from canvas.
00055 
00056    fStyleManager = sm;
00057 
00058    // Create the main frame.
00059    SetCleanup(kNoCleanup);
00060    fCurStyle = cur;
00061    fMode = mode;
00062    fCurPad = currentPad;
00063 
00064    switch (fMode) {
00065       case 1:  SetWindowName("Create a New Style");            break;
00066       case 2:  SetWindowName("Rename the Selected Style");     break;
00067       case 3:  SetWindowName("Import a New Style from Canvas");
00068    }
00069 
00070    // Create the trash lists to have an effective deletion of every object.
00071    fTrashListLayout = new TList();
00072    fTrashListFrame = new TList();
00073 
00074    // Create the layouts and add them to the layout trash list.
00075    TGLayoutHints *layoutNameLabel = new TGLayoutHints(kLHintsNormal, 0, 70, 3);
00076    fTrashListLayout->Add(layoutNameLabel);
00077    TGLayoutHints *layoutTitleLabel = new TGLayoutHints(kLHintsNormal, 0, 39, 3);
00078    fTrashListLayout->Add(layoutTitleLabel);
00079    TGLayoutHints *layoutWarningLabel = new TGLayoutHints(kLHintsExpandX);
00080    fTrashListLayout->Add(layoutWarningLabel);
00081    TGLayoutHints *layoutOKButton = new TGLayoutHints(kLHintsExpandX, 0, 5);
00082    fTrashListLayout->Add(layoutOKButton);
00083    TGLayoutHints *layoutCancelButton = new TGLayoutHints(kLHintsExpandX, 5);
00084    fTrashListLayout->Add(layoutCancelButton);
00085    TGLayoutHints *layoutH1 = new TGLayoutHints(kLHintsExpandX, 10, 10, 10, 5);
00086    fTrashListLayout->Add(layoutH1);
00087    TGLayoutHints *layoutH2 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 5);
00088    fTrashListLayout->Add(layoutH2);
00089    TGLayoutHints *layoutH4 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 10);
00090    fTrashListLayout->Add(layoutH4);
00091 
00092    // Create and place the widgets in the main window.
00093    // Every frame created here must be added to the frame trash list.
00094    TGHorizontalFrame *h1 = new TGHorizontalFrame(this);
00095    fTrashListFrame->Add(h1);
00096    fNameLabel = new TGLabel(h1, "Name:");
00097    h1->AddFrame(fNameLabel, layoutNameLabel);
00098 
00099    if (fMode == 1) {
00100       TString newName;
00101       newName.Form("%s_1", fCurStyle->GetName());
00102       fName = new TGTextEntry(h1, newName.Data(), kName);
00103    } else if (fMode == 2) {
00104       // The names of the 5 basics styles can not be modified.
00105       fName = new TGTextEntry(h1, fCurStyle->GetName(), kName);
00106       if ((!strcmp(fName->GetText(), "Default"))
00107        || (!strcmp(fName->GetText(), "Plain"  ))
00108        || (!strcmp(fName->GetText(), "Bold"   ))
00109        || (!strcmp(fName->GetText(), "Video"  ))
00110        || (!strcmp(fName->GetText(), "Pub"    ))) fName->SetEnabled(kFALSE);
00111    } else
00112       fName = new TGTextEntry(h1, "Imported_Style", kName);
00113    fName->Associate(this);
00114    fName->Resize(200, 22);
00115    h1->AddFrame(fName);
00116    AddFrame(h1, layoutH1);
00117 
00118    TGHorizontalFrame *h2 = new TGHorizontalFrame(this);
00119    fTrashListFrame->Add(h2);
00120    fTitleLabel = new TGLabel(h2, "Description:");
00121    h2->AddFrame(fTitleLabel, layoutTitleLabel);
00122    switch (fMode) {
00123       case 1:
00124       case 2: 
00125          fTitle = new TGTextEntry(h2, fCurStyle->GetTitle(), kTitle);
00126          break;
00127       case 3: {
00128          TString newTitle("Imported from canvas ");
00129          newTitle += fCurPad->GetCanvas()->GetName();
00130          fTitle = new TGTextEntry(h2, newTitle.Data(), kTitle);
00131       }
00132    }
00133    fTitle->Associate(this);
00134    fTitle->Resize(200, 22);
00135    h2->AddFrame(fTitle);
00136    fTitle->Associate(h2);
00137    AddFrame(h2, layoutH2);
00138 
00139    TGHorizontalFrame *h3 = new TGHorizontalFrame(this);
00140    fTrashListFrame->Add(h3);
00141    fWarnLabel = new TGLabel(h3);
00142    Pixel_t red;
00143    gClient->GetColorByName("#FF0000", red);
00144    fWarnLabel->SetTextColor(red, kFALSE);
00145    fWarnLabel->Resize(200, 22);
00146    h3->AddFrame(fWarnLabel, layoutWarningLabel);
00147    AddFrame(h3, layoutH2);
00148 
00149    TGHorizontalFrame *h4 = new TGHorizontalFrame(this);
00150    fTrashListFrame->Add(h4);
00151    fOK = new TGTextButton(h4, "&OK", kButOK);
00152    fOK->Associate(this);
00153    h4->AddFrame(fOK, layoutOKButton);
00154    fOK->Associate(h4);
00155    fCancel = new TGTextButton(h4, "&Cancel", kButCancel);
00156    fCancel->Associate(this);
00157    h4->AddFrame(fCancel, layoutCancelButton);
00158    fCancel->Associate(h4);
00159    AddFrame(h4, layoutH4);
00160 
00161    // Refresh the warning message.
00162    DoUpdate();
00163 
00164    Resize();
00165    CenterOnParent();
00166    MapSubwindows();
00167    Int_t w = GetDefaultWidth();
00168    Int_t h = GetDefaultHeight();
00169    SetWMSizeHints(w, h, w, h, 0, 0);
00170    MapWindow();
00171 
00172    switch (fMode) {
00173       case 1:
00174          fOK->SetToolTipText("Create this new style");
00175          fCancel->SetToolTipText("Cancel the creation ");
00176          break;
00177       case 2:
00178          fOK->SetToolTipText("Rename the selected style");
00179          fCancel->SetToolTipText("Cancel the rename ");
00180          break;
00181       case 3:
00182          fOK->SetToolTipText("Import this new style from the canvas");
00183          fCancel->SetToolTipText("Cancel the import");
00184          break;
00185    }
00186 
00187    Connect("CloseWindow()", "TStyleDialog", this, "DoCloseWindow()");
00188    fName->Connect("TextChanged(const char *)", "TStyleDialog", this, "DoUpdate()");
00189    fOK->Connect("Clicked()", "TStyleDialog", this, "DoOK()");
00190    fCancel->Connect("Clicked()", "TStyleDialog", this, "DoCancel()");
00191 
00192    gClient->WaitFor(this);
00193 }
00194 
00195 //______________________________________________________________________________
00196 TStyleDialog::~TStyleDialog()
00197 {
00198    // Destructor.
00199 
00200    Disconnect("DoCloseWindow()");
00201    fName->Disconnect("TextChanged(const char *)");
00202    fOK->Disconnect("Clicked()");
00203    fCancel->Disconnect("Clicked()");
00204 
00205    delete fName;
00206    delete fNameLabel;
00207    delete fTitle;
00208    delete fTitleLabel;
00209    delete fWarnLabel;
00210    delete fOK;
00211    delete fCancel;
00212 
00213    TObject *obj1;
00214    TObject *obj2;
00215 
00216    obj1 = fTrashListFrame->First();
00217    while (obj1) {
00218       obj2 = fTrashListFrame->After(obj1);
00219       fTrashListFrame->Remove(obj1);
00220       delete obj1;
00221       obj1 = obj2;
00222    }
00223    delete fTrashListFrame;
00224 
00225    obj1 = fTrashListLayout->First();
00226    while (obj1) {
00227       obj2 = fTrashListLayout->After(obj1);
00228       fTrashListLayout->Remove(obj1);
00229       delete obj1;
00230       obj1 = obj2;
00231    }
00232    delete fTrashListLayout;
00233 }
00234 
00235 //______________________________________________________________________________
00236 void TStyleDialog::DoCancel()
00237 {
00238    //  Slot called when the Cancel button is clicked. Close the window
00239    // without saving submitted changes.
00240 
00241    fStyleManager->SetLastChoice(kFALSE);
00242 
00243    SendCloseMessage();
00244 }
00245 
00246 //______________________________________________________________________________
00247 void TStyleDialog::DoCloseWindow()
00248 {
00249    //  Slot called when the window is closed via the window manager.
00250    // Close the window without saving submitted changes.
00251 
00252    delete this;
00253 }
00254 
00255 //______________________________________________________________________________
00256 void TStyleDialog::DoOK()
00257 {
00258    //  Slot called when the OK button is clicked. Rename or create the style
00259    // before closing the window.
00260 
00261    if (fMode == 2) {
00262       // Update the name and the title of the style.
00263       fCurStyle->SetName(fName->GetText());
00264       fCurStyle->SetTitle(fTitle->GetText());
00265    } else {
00266       // Create a new style (copy of fCurStyle), with the given name and title.
00267       TStyle *tmpStyle = new TStyle(*fCurStyle);
00268       tmpStyle->SetName(fName->GetText());
00269       tmpStyle->SetTitle(fTitle->GetText());
00270       {
00271          R__LOCKGUARD2(gROOTMutex);
00272          gROOT->GetListOfStyles()->Add(tmpStyle);
00273       }
00274       if (fMode == 3) {
00275          // Import the properties of the canvas.
00276          TStyle *tmp = gStyle;
00277          gStyle = tmpStyle;
00278          gStyle->SetIsReading(kFALSE);
00279          fCurPad->GetCanvas()->UseCurrentStyle();
00280          gStyle->SetIsReading(kTRUE);
00281          gStyle = tmp;
00282       }
00283    }
00284 
00285    fStyleManager->SetLastChoice(kTRUE);
00286 
00287    SendCloseMessage();
00288 }
00289 
00290 //______________________________________________________________________________
00291 void TStyleDialog::DoUpdate()
00292 {
00293    //  Slot called every time the name is changed. Provide some protection
00294    // to avoid letting the user use an empty name or an already used one.
00295    //  A warning message can be shown and the OK button disabled.
00296 
00297    if (!strlen(fName->GetText())) {
00298       fWarnLabel->SetText("That name is empty");
00299       fOK->SetEnabled(kFALSE);
00300       return;
00301    }
00302 
00303    if (strstr(fName->GetText(), " ") != 0) {
00304       fWarnLabel->SetText("That name contains some spaces");
00305       fOK->SetEnabled(kFALSE);
00306       return;
00307    }
00308 
00309    switch (fMode) {
00310       case 1:
00311       case 3:
00312          if (gROOT->GetStyle(fName->GetText())) {
00313             fWarnLabel->SetText("That name is already used by another style.");
00314             fOK->SetEnabled(kFALSE);
00315             return;
00316          }
00317          break;
00318       case 2: 
00319          TStyle *tmp = gROOT->GetStyle(fName->GetText());
00320          if (tmp && (tmp != fCurStyle)) {
00321             fWarnLabel->SetText("That name is already used by another style.");
00322             fOK->SetEnabled(kFALSE);
00323             return;
00324          }
00325    }
00326 
00327    fWarnLabel->SetText("");
00328    fOK->SetEnabled(kTRUE);
00329 }

Generated on Tue Jul 5 14:19:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1