Logo ROOT   6.10/09
Reference Guide
TStyleDialog.cxx
Go to the documentation of this file.
1 // @(#)root/ged:$Id$
2 // Author: Denis Favre-Miville 08/09/05
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TStyleDialog //
15 // //
16 // This small class is useful to ask the user for a name and a title, //
17 // in order to rename a style, create a new style or import a //
18 // style from a canvas. //
19 // //
20 //////////////////////////////////////////////////////////////////////////
21 
22 #include "TStyleDialog.h"
23 #include "TStyleManager.h"
24 
25 #include <TCanvas.h>
26 #include <TGButton.h>
27 #include <TGFrame.h>
28 #include <TGLabel.h>
29 #include <TGLayout.h>
30 #include <TGTextEntry.h>
31 #include <TROOT.h>
32 #include <TStyle.h>
33 #include <TVirtualPad.h>
34 #include <TVirtualMutex.h>
35 #include <stdlib.h>
36 
38 
44 };
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Constructor. Create the dialog window and draw it centered over the
48 /// main window 'mf'. A pointer to the style to copy or rename is given
49 /// by 'cur' and the parameter 'mode' specify the mode:
50 /// 1 = copy | 2 = rename | 3 = import from canvas.
51 
53  TVirtualPad *currentPad)
54  : TGTransientFrame(0, sm)
55 {
56  fStyleManager = sm;
57 
58  // Create the main frame.
60  fCurStyle = cur;
61  fMode = mode;
62  fCurPad = currentPad;
63 
64  switch (fMode) {
65  case 1: SetWindowName("Create a New Style"); break;
66  case 2: SetWindowName("Rename the Selected Style"); break;
67  case 3: SetWindowName("Import a New Style from Canvas");
68  }
69 
70  // Create the trash lists to have an effective deletion of every object.
71  fTrashListLayout = new TList();
72  fTrashListFrame = new TList();
73 
74  // Create the layouts and add them to the layout trash list.
75  TGLayoutHints *layoutNameLabel = new TGLayoutHints(kLHintsNormal, 0, 70, 3);
76  fTrashListLayout->Add(layoutNameLabel);
77  TGLayoutHints *layoutTitleLabel = new TGLayoutHints(kLHintsNormal, 0, 39, 3);
78  fTrashListLayout->Add(layoutTitleLabel);
79  TGLayoutHints *layoutWarningLabel = new TGLayoutHints(kLHintsExpandX);
80  fTrashListLayout->Add(layoutWarningLabel);
81  TGLayoutHints *layoutOKButton = new TGLayoutHints(kLHintsExpandX, 0, 5);
82  fTrashListLayout->Add(layoutOKButton);
83  TGLayoutHints *layoutCancelButton = new TGLayoutHints(kLHintsExpandX, 5);
84  fTrashListLayout->Add(layoutCancelButton);
85  TGLayoutHints *layoutH1 = new TGLayoutHints(kLHintsExpandX, 10, 10, 10, 5);
86  fTrashListLayout->Add(layoutH1);
87  TGLayoutHints *layoutH2 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 5);
88  fTrashListLayout->Add(layoutH2);
89  TGLayoutHints *layoutH4 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 10);
90  fTrashListLayout->Add(layoutH4);
91 
92  // Create and place the widgets in the main window.
93  // Every frame created here must be added to the frame trash list.
95  fTrashListFrame->Add(h1);
96  fNameLabel = new TGLabel(h1, "Name:");
97  h1->AddFrame(fNameLabel, layoutNameLabel);
98 
99  if (fMode == 1) {
100  TString newName;
101  newName.Form("%s_1", fCurStyle->GetName());
102  fName = new TGTextEntry(h1, newName.Data(), kName);
103  } else if (fMode == 2) {
104  // The names of the 5 basics styles can not be modified.
105  fName = new TGTextEntry(h1, fCurStyle->GetName(), kName);
106  if ((!strcmp(fName->GetText(), "Default"))
107  || (!strcmp(fName->GetText(), "Plain" ))
108  || (!strcmp(fName->GetText(), "Bold" ))
109  || (!strcmp(fName->GetText(), "Video" ))
110  || (!strcmp(fName->GetText(), "Pub" ))) fName->SetEnabled(kFALSE);
111  } else
112  fName = new TGTextEntry(h1, "Imported_Style", kName);
113  fName->Associate(this);
114  fName->Resize(200, 22);
115  h1->AddFrame(fName);
116  AddFrame(h1, layoutH1);
117 
118  TGHorizontalFrame *h2 = new TGHorizontalFrame(this);
119  fTrashListFrame->Add(h2);
120  fTitleLabel = new TGLabel(h2, "Description:");
121  h2->AddFrame(fTitleLabel, layoutTitleLabel);
122  switch (fMode) {
123  case 1:
124  case 2:
125  fTitle = new TGTextEntry(h2, fCurStyle->GetTitle(), kTitle);
126  break;
127  case 3: {
128  TString newTitle("Imported from canvas ");
129  if (fCurPad->GetCanvas())
130  newTitle += fCurPad->GetCanvas()->GetName();
131  fTitle = new TGTextEntry(h2, newTitle.Data(), kTitle);
132  }
133  }
134  fTitle->Associate(this);
135  fTitle->Resize(200, 22);
136  h2->AddFrame(fTitle);
137  fTitle->Associate(h2);
138  AddFrame(h2, layoutH2);
139 
140  TGHorizontalFrame *h3 = new TGHorizontalFrame(this);
141  fTrashListFrame->Add(h3);
142  fWarnLabel = new TGLabel(h3);
143  Pixel_t red;
144  gClient->GetColorByName("#FF0000", red);
146  fWarnLabel->Resize(200, 22);
147  h3->AddFrame(fWarnLabel, layoutWarningLabel);
148  AddFrame(h3, layoutH2);
149 
150  TGHorizontalFrame *h4 = new TGHorizontalFrame(this);
151  fTrashListFrame->Add(h4);
152  fOK = new TGTextButton(h4, "&OK", kButOK);
153  fOK->Associate(this);
154  h4->AddFrame(fOK, layoutOKButton);
155  fOK->Associate(h4);
156  fCancel = new TGTextButton(h4, "&Cancel", kButCancel);
157  fCancel->Associate(this);
158  h4->AddFrame(fCancel, layoutCancelButton);
159  fCancel->Associate(h4);
160  AddFrame(h4, layoutH4);
161 
162  // Refresh the warning message.
163  DoUpdate();
164 
165  Resize();
166  CenterOnParent();
167  MapSubwindows();
168  Int_t w = GetDefaultWidth();
170  SetWMSizeHints(w, h, w, h, 0, 0);
171  MapWindow();
172 
173  switch (fMode) {
174  case 1:
175  fOK->SetToolTipText("Create this new style");
176  fCancel->SetToolTipText("Cancel the creation ");
177  break;
178  case 2:
179  fOK->SetToolTipText("Rename the selected style");
180  fCancel->SetToolTipText("Cancel the rename ");
181  break;
182  case 3:
183  fOK->SetToolTipText("Import this new style from the canvas");
184  fCancel->SetToolTipText("Cancel the import");
185  break;
186  }
187 
188  Connect("CloseWindow()", "TStyleDialog", this, "DoCloseWindow()");
189  fName->Connect("TextChanged(const char *)", "TStyleDialog", this, "DoUpdate()");
190  fOK->Connect("Clicked()", "TStyleDialog", this, "DoOK()");
191  fCancel->Connect("Clicked()", "TStyleDialog", this, "DoCancel()");
192 
193  gClient->WaitFor(this);
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Destructor.
198 
200 {
201  Disconnect("DoCloseWindow()");
202  fName->Disconnect("TextChanged(const char *)");
203  fOK->Disconnect("Clicked()");
204  fCancel->Disconnect("Clicked()");
205 
206  delete fName;
207  delete fNameLabel;
208  delete fTitle;
209  delete fTitleLabel;
210  delete fWarnLabel;
211  delete fOK;
212  delete fCancel;
213 
214  TObject *obj1;
215  TObject *obj2;
216 
217  obj1 = fTrashListFrame->First();
218  while (obj1) {
219  obj2 = fTrashListFrame->After(obj1);
220  fTrashListFrame->Remove(obj1);
221  delete obj1;
222  obj1 = obj2;
223  }
224  delete fTrashListFrame;
225 
226  obj1 = fTrashListLayout->First();
227  while (obj1) {
228  obj2 = fTrashListLayout->After(obj1);
229  fTrashListLayout->Remove(obj1);
230  delete obj1;
231  obj1 = obj2;
232  }
233  delete fTrashListLayout;
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 /// Slot called when the Cancel button is clicked. Close the window
238 /// without saving submitted changes.
239 
241 {
243 
245 }
246 
247 ////////////////////////////////////////////////////////////////////////////////
248 /// Slot called when the window is closed via the window manager.
249 /// Close the window without saving submitted changes.
250 
252 {
253  delete this;
254 }
255 
256 ////////////////////////////////////////////////////////////////////////////////
257 /// Slot called when the OK button is clicked. Rename or create the style
258 /// before closing the window.
259 
261 {
262  if (fMode == 2) {
263  // Update the name and the title of the style.
266  } else {
267  // Create a new style (copy of fCurStyle), with the given name and title.
268  TStyle *tmpStyle = new TStyle(*fCurStyle);
269  tmpStyle->SetName(fName->GetText());
270  tmpStyle->SetTitle(fTitle->GetText());
271  {
273  gROOT->GetListOfStyles()->Add(tmpStyle);
274  }
275  if (fMode == 3) {
276  // Import the properties of the canvas.
277  TStyle *tmp = gStyle;
278  gStyle = tmpStyle;
280  if (fCurPad->GetCanvas())
283  gStyle = tmp;
284  }
285  }
286 
288 
290 }
291 
292 ////////////////////////////////////////////////////////////////////////////////
293 /// Slot called every time the name is changed. Provide some protection
294 /// to avoid letting the user use an empty name or an already used one.
295 /// A warning message can be shown and the OK button disabled.
296 
298 {
299  if (!strlen(fName->GetText())) {
300  fWarnLabel->SetText("That name is empty");
302  return;
303  }
304 
305  if (strstr(fName->GetText(), " ") != 0) {
306  fWarnLabel->SetText("That name contains some spaces");
308  return;
309  }
310 
311  switch (fMode) {
312  case 1:
313  case 3:
314  if (gROOT->GetStyle(fName->GetText())) {
315  fWarnLabel->SetText("That name is already used by another style.");
317  return;
318  }
319  break;
320  case 2:
321  TStyle *tmp = gROOT->GetStyle(fName->GetText());
322  if (tmp && (tmp != fCurStyle)) {
323  fWarnLabel->SetText("That name is already used by another style.");
325  return;
326  }
327  }
328 
329  fWarnLabel->SetText("");
330  fOK->SetEnabled(kTRUE);
331 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
void DoOK()
Slot called when the OK button is clicked.
TGLabel * fTitleLabel
Definition: TStyleDialog.h:43
TGLabel * fWarnLabel
Definition: TStyleDialog.h:44
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition: TGFrame.cxx:1913
void SetWindowName(const char *name=0)
Set window name. This is typically done via the window manager.
Definition: TGFrame.cxx:1746
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:359
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
TH1 * h
Definition: legend2.C:5
TGTextEntry * fName
Definition: TStyleDialog.h:40
#define gROOT
Definition: TROOT.h:375
TStyleDialog(TStyleManager *sm, TStyle *cur, Int_t mode, TVirtualPad *currentPad=0)
Constructor.
Basic string class.
Definition: TString.h:129
#define gClient
Definition: TGClient.h:166
int Int_t
Definition: RtypesCore.h:41
EStyleDialogWid
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:373
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
TVirtualPad * fCurPad
Definition: TStyleDialog.h:49
TList * fTrashListFrame
Definition: TStyleDialog.h:50
TGLabel * fNameLabel
Definition: TStyleDialog.h:41
void DoCancel()
Slot called when the Cancel button is clicked.
ULong_t Pixel_t
Definition: GuiTypes.h:39
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:177
void SetWMSizeHints(UInt_t wmin, UInt_t hmin, UInt_t wmax, UInt_t hmax, UInt_t winc, UInt_t hinc)
Give the window manager minimum and maximum size hints.
Definition: TGFrame.cxx:1862
TH1F * h1
Definition: legend1.C:5
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:49
A doubly linked list.
Definition: TList.h:43
TStyle objects may be created to define special styles.
Definition: TStyle.h:27
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot...
Definition: TQObject.cxx:867
TStyleManager * fStyleManager
Definition: TStyleDialog.h:39
virtual void SendCloseMessage()
Send close message to self.
Definition: TGFrame.cxx:1702
void SetLastChoice(Bool_t choice)
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:561
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:371
TStyle * fCurStyle
Definition: TStyleDialog.h:47
virtual void Associate(const TGWindow *w)
Definition: TGWidget.h:84
void SetIsReading(Bool_t reading=kTRUE)
Sets the fIsReading member to reading (default=kTRUE).
Definition: TStyle.cxx:1003
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:679
virtual ~TStyleDialog()
Destructor.
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2332
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition: TList.cxx:293
#define R__LOCKGUARD2(mutex)
const Bool_t kFALSE
Definition: RtypesCore.h:92
void DoUpdate()
Slot called every time the name is changed.
const char * GetText() const
Definition: TGTextEntry.h:134
void SetEnabled(Bool_t flag=kTRUE)
Definition: TGTextEntry.h:164
#define ClassImp(name)
Definition: Rtypes.h:336
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1054
virtual void SetEnabled(Bool_t e=kTRUE)
Set enabled or disabled state of button.
Definition: TGButton.cxx:409
Bool_t Disconnect(const char *signal=0, void *receiver=0, const char *slot=0)
Disconnects signal of this object from slot of receiver.
Definition: TQObject.cxx:1025
TGTextButton * fCancel
Definition: TStyleDialog.h:46
void DoCloseWindow()
Slot called when the window is closed via the window manager.
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
TList * fTrashListLayout
Definition: TStyleDialog.h:51
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1146
Mother of all ROOT objects.
Definition: TObject.h:37
void UseCurrentStyle()
Force a copy of current style for all objects in canvas.
Definition: TCanvas.cxx:1110
const char * GetName() const
Returns name of object.
Definition: TPad.h:251
virtual void Add(TObject *obj)
Definition: TList.h:77
virtual void MapWindow()
Definition: TGFrame.h:251
TGTextButton * fOK
Definition: TStyleDialog.h:45
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void SetToolTipText(const char *text, Long_t delayms=400)
Set tool tip text associated with this button.
Definition: TGButton.cxx:395
TGTextEntry * fTitle
Definition: TStyleDialog.h:42
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual TCanvas * GetCanvas() const =0
const char * Data() const
Definition: TString.h:347