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/** \class TStyleDialog
14\ingroup ged
15
16This 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
23#include "TStyleDialog.h"
24#include "TStyleManager.h"
25
26#include <TCanvas.h>
27#include <TGButton.h>
28#include <TGLabel.h>
29#include <TGLayout.h>
30#include <TGTextEntry.h>
31#include <TROOT.h>
32#include <TStyle.h>
33#include <TVirtualMutex.h>
34
35#include <cstdlib>
36
37
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.
94 fTrashListFrame->Add(h1);
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.
104 fName = new TGTextEntry(h1, fCurStyle->GetName(), kName);
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:
124 fTitle = new TGTextEntry(h2, fCurStyle->GetTitle(), kTitle);
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);
144 fWarnLabel->SetTextColor(red, kFALSE);
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);
228 fTrashListLayout->Remove(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{
241 fStyleManager->SetLastChoice(kFALSE);
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.
263 fCurStyle->SetName(fName->GetText());
264 fCurStyle->SetTitle(fTitle->GetText());
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;
278 gStyle->SetIsReading(kFALSE);
279 if (fCurPad->GetCanvas())
280 fCurPad->GetCanvas()->UseCurrentStyle();
281 gStyle->SetIsReading(kTRUE);
282 gStyle = tmp;
283 }
284 }
285
286 fStyleManager->SetLastChoice(kTRUE);
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");
300 fOK->SetEnabled(kFALSE);
301 return;
302 }
303
304 if (strstr(fName->GetText(), " ") != 0) {
305 fWarnLabel->SetText("That name contains some spaces");
306 fOK->SetEnabled(kFALSE);
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.");
315 fOK->SetEnabled(kFALSE);
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.");
323 fOK->SetEnabled(kFALSE);
324 return;
325 }
326 }
327
328 fWarnLabel->SetText("");
329 fOK->SetEnabled(kTRUE);
330}
ULong_t Pixel_t
Pixel value.
Definition GuiTypes.h:41
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
#define gClient
Definition TGClient.h:157
@ kNoCleanup
Definition TGFrame.h:40
@ kLHintsNormal
Definition TGLayout.h:32
@ kLHintsExpandX
Definition TGLayout.h:30
#define gROOT
Definition TROOT.h:417
externTVirtualMutex * gROOTMutex
Definition TROOT.h:63
EStyleDialogWid
@ kName
@ kButOK
@ kButCancel
@ kTitle
externTStyle * gStyle
Definition TStyle.h:442
#define R__LOCKGUARD(mutex)
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1109
UInt_t GetDefaultWidth() const override
Definition TGFrame.h:314
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1156
UInt_t GetDefaultHeight() const override
Definition TGFrame.h:316
void SetCleanup(Int_t mode=kLocalCleanup) override
Turn on automatic cleanup of child frames in dtor.
Definition TGFrame.cxx:1064
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:597
void MapWindow() override
map window
Definition TGFrame.h:206
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:387
This class handles GUI labels.
Definition TGLabel.h:24
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
virtual void SendCloseMessage()
Send close message to self.
Definition TGFrame.cxx:1736
void SetWindowName(const char *name=nullptr) override
Set window name. This is typically done via the window manager.
Definition TGFrame.cxx:1780
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:1898
Yield an action as soon as it is clicked.
Definition TGButton.h:142
A TGTextEntry is a one line text input widget.
Definition TGTextEntry.h:24
TGTransientFrame(const TGTransientFrame &)=delete
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition TGFrame.cxx:1949
A doubly linked list.
Definition TList.h:38
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:42
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:865
Bool_t Disconnect(const char *signal=nullptr, void *receiver=nullptr, const char *slot=nullptr)
Disconnects signal of this object from slot of receiver.
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2363
void DoCancel()
Slot called when the Cancel button is clicked.
TList * fTrashListLayout
to avoid memory leak
void DoOK()
Slot called when the OK button is clicked.
~TStyleDialog() override
Destructor.
TStyleDialog(TStyleManager *sm, TStyle *cur, Int_t mode, TVirtualPad *currentPad=nullptr)
Constructor.
Int_t fMode
1=new, 2=rename, 3=import
TGTextEntry * fTitle
TStyle title text entry.
void DoCloseWindow()
Slot called when the window is closed via the window manager.
TVirtualPad * fCurPad
current pad from which to import
TList * fTrashListFrame
to avoid memory leak
TStyle * fCurStyle
style to copy or to rename
TGTextButton * fOK
save button
TGTextEntry * fName
TStyle name text entry.
TGTextButton * fCancel
cancel button
TStyleManager * fStyleManager
parent style manager
TGLabel * fTitleLabel
title label
TGLabel * fNameLabel
name label
void DoUpdate()
Slot called every time the name is changed.
TGLabel * fWarnLabel
label for warnings
This class provides a Graphical User Interface to manage styles in ROOT.
TStyle objects may be created to define special styles.
Definition TStyle.h:29
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
TH1F * h1
Definition legend1.C:5