Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <TGLabel.h>
28#include <TGLayout.h>
29#include <TGTextEntry.h>
30#include <TROOT.h>
31#include <TStyle.h>
32#include <TVirtualMutex.h>
33
34#include <cstdlib>
35
37
43};
44
45////////////////////////////////////////////////////////////////////////////////
46/// Constructor. Create the dialog window and draw it centered over the
47/// main window 'mf'. A pointer to the style to copy or rename is given
48/// by 'cur' and the parameter 'mode' specify the mode:
49/// 1 = copy | 2 = rename | 3 = import from canvas.
50
52 TVirtualPad *currentPad)
53 : TGTransientFrame(0, sm)
54{
55 fStyleManager = sm;
56
57 // Create the main frame.
59 fCurStyle = cur;
60 fMode = mode;
61 fCurPad = currentPad;
62
63 switch (fMode) {
64 case 1: SetWindowName("Create a New Style"); break;
65 case 2: SetWindowName("Rename the Selected Style"); break;
66 case 3: SetWindowName("Import a New Style from Canvas");
67 }
68
69 // Create the trash lists to have an effective deletion of every object.
70 fTrashListLayout = new TList();
71 fTrashListFrame = new TList();
72
73 // Create the layouts and add them to the layout trash list.
74 TGLayoutHints *layoutNameLabel = new TGLayoutHints(kLHintsNormal, 0, 70, 3);
75 fTrashListLayout->Add(layoutNameLabel);
76 TGLayoutHints *layoutTitleLabel = new TGLayoutHints(kLHintsNormal, 0, 39, 3);
77 fTrashListLayout->Add(layoutTitleLabel);
78 TGLayoutHints *layoutWarningLabel = new TGLayoutHints(kLHintsExpandX);
79 fTrashListLayout->Add(layoutWarningLabel);
80 TGLayoutHints *layoutOKButton = new TGLayoutHints(kLHintsExpandX, 0, 5);
81 fTrashListLayout->Add(layoutOKButton);
82 TGLayoutHints *layoutCancelButton = new TGLayoutHints(kLHintsExpandX, 5);
83 fTrashListLayout->Add(layoutCancelButton);
84 TGLayoutHints *layoutH1 = new TGLayoutHints(kLHintsExpandX, 10, 10, 10, 5);
85 fTrashListLayout->Add(layoutH1);
86 TGLayoutHints *layoutH2 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 5);
87 fTrashListLayout->Add(layoutH2);
88 TGLayoutHints *layoutH4 = new TGLayoutHints(kLHintsExpandX, 10, 10, 5, 10);
89 fTrashListLayout->Add(layoutH4);
90
91 // Create and place the widgets in the main window.
92 // Every frame created here must be added to the frame trash list.
95 fNameLabel = new TGLabel(h1, "Name:");
96 h1->AddFrame(fNameLabel, layoutNameLabel);
97
98 if (fMode == 1) {
99 TString newName;
100 newName.Form("%s_1", fCurStyle->GetName());
101 fName = new TGTextEntry(h1, newName.Data(), kName);
102 } else if (fMode == 2) {
103 // The names of the 5 basics styles can not be modified.
105 if ((!strcmp(fName->GetText(), "Default"))
106 || (!strcmp(fName->GetText(), "Plain" ))
107 || (!strcmp(fName->GetText(), "Bold" ))
108 || (!strcmp(fName->GetText(), "Video" ))
109 || (!strcmp(fName->GetText(), "Pub" ))) fName->SetEnabled(kFALSE);
110 } else
111 fName = new TGTextEntry(h1, "Imported_Style", kName);
112 fName->Associate(this);
113 fName->Resize(200, 22);
114 h1->AddFrame(fName);
115 AddFrame(h1, layoutH1);
116
117 TGHorizontalFrame *h2 = new TGHorizontalFrame(this);
118 fTrashListFrame->Add(h2);
119 fTitleLabel = new TGLabel(h2, "Description:");
120 h2->AddFrame(fTitleLabel, layoutTitleLabel);
121 switch (fMode) {
122 case 1:
123 case 2:
125 break;
126 case 3: {
127 TString newTitle("Imported from canvas ");
128 if (fCurPad->GetCanvas())
129 newTitle += fCurPad->GetCanvas()->GetName();
130 fTitle = new TGTextEntry(h2, newTitle.Data(), kTitle);
131 }
132 }
133 fTitle->Associate(this);
134 fTitle->Resize(200, 22);
135 h2->AddFrame(fTitle);
136 fTitle->Associate(h2);
137 AddFrame(h2, layoutH2);
138
139 TGHorizontalFrame *h3 = new TGHorizontalFrame(this);
140 fTrashListFrame->Add(h3);
141 fWarnLabel = new TGLabel(h3);
142 Pixel_t red;
143 gClient->GetColorByName("#FF0000", red);
145 fWarnLabel->Resize(200, 22);
146 h3->AddFrame(fWarnLabel, layoutWarningLabel);
147 AddFrame(h3, layoutH2);
148
149 TGHorizontalFrame *h4 = new TGHorizontalFrame(this);
150 fTrashListFrame->Add(h4);
151 fOK = new TGTextButton(h4, "&OK", kButOK);
152 fOK->Associate(this);
153 h4->AddFrame(fOK, layoutOKButton);
154 fOK->Associate(h4);
155 fCancel = new TGTextButton(h4, "&Cancel", kButCancel);
156 fCancel->Associate(this);
157 h4->AddFrame(fCancel, layoutCancelButton);
158 fCancel->Associate(h4);
159 AddFrame(h4, layoutH4);
160
161 // Refresh the warning message.
162 DoUpdate();
163
164 Resize();
169 SetWMSizeHints(w, h, w, h, 0, 0);
170 MapWindow();
171
172 switch (fMode) {
173 case 1:
174 fOK->SetToolTipText("Create this new style");
175 fCancel->SetToolTipText("Cancel the creation ");
176 break;
177 case 2:
178 fOK->SetToolTipText("Rename the selected style");
179 fCancel->SetToolTipText("Cancel the rename ");
180 break;
181 case 3:
182 fOK->SetToolTipText("Import this new style from the canvas");
183 fCancel->SetToolTipText("Cancel the import");
184 break;
185 }
186
187 Connect("CloseWindow()", "TStyleDialog", this, "DoCloseWindow()");
188 fName->Connect("TextChanged(const char *)", "TStyleDialog", this, "DoUpdate()");
189 fOK->Connect("Clicked()", "TStyleDialog", this, "DoOK()");
190 fCancel->Connect("Clicked()", "TStyleDialog", this, "DoCancel()");
191
192 gClient->WaitFor(this);
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Destructor.
197
199{
200 Disconnect("DoCloseWindow()");
201 fName->Disconnect("TextChanged(const char *)");
202 fOK->Disconnect("Clicked()");
203 fCancel->Disconnect("Clicked()");
204
205 delete fName;
206 delete fNameLabel;
207 delete fTitle;
208 delete fTitleLabel;
209 delete fWarnLabel;
210 delete fOK;
211 delete fCancel;
212
213 TObject *obj1;
214 TObject *obj2;
215
216 obj1 = fTrashListFrame->First();
217 while (obj1) {
218 obj2 = fTrashListFrame->After(obj1);
219 fTrashListFrame->Remove(obj1);
220 delete obj1;
221 obj1 = obj2;
222 }
223 delete fTrashListFrame;
224
225 obj1 = fTrashListLayout->First();
226 while (obj1) {
227 obj2 = fTrashListLayout->After(obj1);
229 delete obj1;
230 obj1 = obj2;
231 }
232 delete fTrashListLayout;
233}
234
235////////////////////////////////////////////////////////////////////////////////
236/// Slot called when the Cancel button is clicked. Close the window
237/// without saving submitted changes.
238
240{
242
244}
245
246////////////////////////////////////////////////////////////////////////////////
247/// Slot called when the window is closed via the window manager.
248/// Close the window without saving submitted changes.
249
251{
252 delete this;
253}
254
255////////////////////////////////////////////////////////////////////////////////
256/// Slot called when the OK button is clicked. Rename or create the style
257/// before closing the window.
258
260{
261 if (fMode == 2) {
262 // Update the name and the title of the style.
265 } else {
266 // Create a new style (copy of fCurStyle), with the given name and title.
267 TStyle *tmpStyle = new TStyle(*fCurStyle);
268 tmpStyle->SetName(fName->GetText());
269 tmpStyle->SetTitle(fTitle->GetText());
270 {
272 gROOT->GetListOfStyles()->Add(tmpStyle);
273 }
274 if (fMode == 3) {
275 // Import the properties of the canvas.
276 TStyle *tmp = gStyle;
277 gStyle = tmpStyle;
279 if (fCurPad->GetCanvas())
282 gStyle = tmp;
283 }
284 }
285
287
289}
290
291////////////////////////////////////////////////////////////////////////////////
292/// Slot called every time the name is changed. Provide some protection
293/// to avoid letting the user use an empty name or an already used one.
294/// A warning message can be shown and the OK button disabled.
295
297{
298 if (!strlen(fName->GetText())) {
299 fWarnLabel->SetText("That name is empty");
301 return;
302 }
303
304 if (strstr(fName->GetText(), " ") != 0) {
305 fWarnLabel->SetText("That name contains some spaces");
307 return;
308 }
309
310 switch (fMode) {
311 case 1:
312 case 3:
313 if (gROOT->GetStyle(fName->GetText())) {
314 fWarnLabel->SetText("That name is already used by another style.");
316 return;
317 }
318 break;
319 case 2:
320 TStyle *tmp = gROOT->GetStyle(fName->GetText());
321 if (tmp && (tmp != fCurStyle)) {
322 fWarnLabel->SetText("That name is already used by another style.");
324 return;
325 }
326 }
327
328 fWarnLabel->SetText("");
330}
ULong_t Pixel_t
Pixel value.
Definition GuiTypes.h:40
#define h(i)
Definition RSha256.hxx:106
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
#define gClient
Definition TGClient.h:166
@ kNoCleanup
Definition TGFrame.h:48
@ kLHintsNormal
Definition TGLayout.h:39
@ kLHintsExpandX
Definition TGLayout.h:37
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:406
EStyleDialogWid
@ kName
@ kButOK
@ kButCancel
@ kTitle
R__EXTERN TStyle * gStyle
Definition TStyle.h:412
#define R__LOCKGUARD(mutex)
void UseCurrentStyle() override
Force a copy of current style for all objects in canvas.
Definition TCanvas.cxx:1176
virtual void SetToolTipText(const char *text, Long_t delayms=400)
Set tool tip text associated with this button.
Definition TGButton.cxx:398
virtual void SetEnabled(Bool_t e=kTRUE)
Set enabled or disabled state of button.
Definition TGButton.cxx:412
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1102
virtual UInt_t GetDefaultWidth() const
Definition TGFrame.h:348
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition TGFrame.cxx:1057
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1149
virtual UInt_t GetDefaultHeight() const
Definition TGFrame.h:350
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition TGFrame.cxx:590
virtual void MapWindow()
map window
Definition TGFrame.h:228
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition TGLabel.cxx:361
virtual void SetText(TGString *newText)
Set new text in label.
Definition TGLabel.cxx:179
virtual void SendCloseMessage()
Send close message to self.
Definition TGFrame.cxx:1705
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:1865
void SetWindowName(const char *name=0)
Set window name. This is typically done via the window manager.
Definition TGFrame.cxx:1749
const char * GetText() const
void SetEnabled(Bool_t flag=kTRUE)
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition TGFrame.cxx:1916
virtual void Associate(const TGWindow *w)
Definition TGWidget.h:82
A doubly linked list.
Definition TList.h:44
virtual void Add(TObject *obj)
Definition TList.h:87
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition TList.cxx:330
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition TList.cxx:822
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:659
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:37
const char * GetName() const override
Returns name of object.
Definition TPad.h:254
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:866
Bool_t Disconnect(const char *signal=0, void *receiver=0, const char *slot=0)
Disconnects signal of this object from slot of receiver.
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2309
void DoCancel()
Slot called when the Cancel button is clicked.
TList * fTrashListLayout
void DoOK()
Slot called when the OK button is clicked.
TGTextEntry * fTitle
void DoCloseWindow()
Slot called when the window is closed via the window manager.
TVirtualPad * fCurPad
TList * fTrashListFrame
TStyle * fCurStyle
TGTextButton * fOK
TGTextEntry * fName
TGTextButton * fCancel
virtual ~TStyleDialog()
Destructor.
TStyleDialog(TStyleManager *sm, TStyle *cur, Int_t mode, TVirtualPad *currentPad=0)
Constructor.
TStyleManager * fStyleManager
TGLabel * fTitleLabel
TGLabel * fNameLabel
void DoUpdate()
Slot called every time the name is changed.
TGLabel * fWarnLabel
void SetLastChoice(Bool_t choice)
TStyle objects may be created to define special styles.
Definition TStyle.h:29
void SetIsReading(Bool_t reading=kTRUE)
Sets the fIsReading member to reading (default=kTRUE).
Definition TStyle.cxx:1322
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TCanvas * GetCanvas() const =0
TH1F * h1
Definition legend1.C:5