forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dialogs.C
301 lines (237 loc) · 8.88 KB
/
Dialogs.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// This file contains the class InputDialog.
// An InputDialog object prompts for an input string using a simple
// dialog box. The InputDialog class is also a good example of how
// to use the ROOT GUI classes via the interpreter. Since interpreted
// classes can not call virtual functions via base class pointers, all
// GUI objects are used by composition instead of by inheritance.
//
// This file contains also some utility functions that use
// the InputDialog class to either get a string, integer or
// floating point number. There are also two functions showing
// how to use the file open and save dialogs. The utility functions are:
//
// const char *OpenFileDialog()
// const char *SaveFileDialog()
// const char *GetStringDialog(const char *prompt, const char *defval)
// Int_t GetIntegerDialog(const char *prompt, Int_t defval)
// Float_t GetFloatDialog(const char *prompt, Float_t defval)
//
// To use the InputDialog class and the utility functions you just
// have to load the Dialogs.C file as follows:
// .L Dialogs.C
//
// Now you can use them like:
// {
// const char *file = OpenFileDialog();
// Int_t run = GetIntegerDialog("Give run number:", 0);
// Int_t event = GetIntegerDialog("Give event number:", 0);
// printf("analyse run %d, event %d from file %s\n", run ,event, file);
// }
//
///////////////////////////////////////////////////////////////////////////
// //
// Input Dialog Widget //
// //
///////////////////////////////////////////////////////////////////////////
class InputDialog {
private:
TGTransientFrame *fDialog; // transient frame, main dialog window
TGTextEntry *fTE; // text entry widget containing
TList *fWidgets; // keep track of widgets to be deleted in dtor
char *fRetStr; // address to store return string
public:
InputDialog(const char *prompt, const char *defval, char *retstr);
~InputDialog();
void ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2);
};
InputDialog::~InputDialog()
{
// Cleanup dialog.
fWidgets->Delete();
delete fWidgets;
delete fTE;
delete fDialog;
}
InputDialog::InputDialog(const char *prompt, const char *defval, char *retstr)
{
// Create simple input dialog.
fWidgets = new TList;
TGWindow *main = gClient->GetRoot();
fDialog = new TGTransientFrame(main, main, 10, 10);
// command to be executed by buttons and text entry widget
char cmd[128];
sprintf(cmd, "{long r__ptr=0x%lx; ((InputDialog*)r__ptr)->ProcessMessage($MSG,$PARM1,$PARM2);}", (Long_t)this);
// create prompt label and textentry widget
TGLabel *label = new TGLabel(fDialog, prompt);
fWidgets->Add(label);
TGTextBuffer *tbuf = new TGTextBuffer(256); //will be deleted by TGtextEntry
tbuf->AddText(0, defval);
fTE = new TGTextEntry(fDialog, tbuf);
fTE->Resize(260, fTE->GetDefaultHeight());
fTE->SetCommand(cmd);
TGLayoutHints *l1 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0);
TGLayoutHints *l2 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5);
fWidgets->Add(l1);
fWidgets->Add(l2);
fDialog->AddFrame(label, l1);
fDialog->AddFrame(fTE, l2);
// create frame and layout hints for Ok and Cancel buttons
TGHorizontalFrame *hf = new TGHorizontalFrame(fDialog, 60, 20, kFixedWidth);
TGLayoutHints *l3 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0);
// put hf as last in list to be deleted
fWidgets->Add(l3);
// create OK and Cancel buttons in their own frame (hf)
UInt_t nb = 0, width = 0, height = 0;
TGTextButton *b;
b = new TGTextButton(hf, "&Ok", cmd, 1);
fWidgets->Add(b);
b->Associate(fDialog);
hf->AddFrame(b, l3);
height = b->GetDefaultHeight();
width = TMath::Max(width, b->GetDefaultWidth()); ++nb;
b = new TGTextButton(hf, "&Cancel", cmd, 2);
fWidgets->Add(b);
b->Associate(fDialog);
hf->AddFrame(b, l3);
height = b->GetDefaultHeight();
width = TMath::Max(width, b->GetDefaultWidth()); ++nb;
// place button frame (hf) at the bottom
TGLayoutHints *l4 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
fWidgets->Add(l4);
fWidgets->Add(hf);
fDialog->AddFrame(hf, l4);
// keep buttons centered and with the same width
hf->Resize((width + 20) * nb, height);
// set dialog title
fDialog->SetWindowName("Get Input");
// map all widgets and calculate size of dialog
fDialog->MapSubwindows();
width = fDialog->GetDefaultWidth();
height = fDialog->GetDefaultHeight();
fDialog->Resize(width, height);
// position relative to the parent window (which is the root window)
Window_t wdum;
int ax, ay;
gVirtualX->TranslateCoordinates(main->GetId(), main->GetId(),
(((TGFrame *) main)->GetWidth() - width) >> 1,
(((TGFrame *) main)->GetHeight() - height) >> 1,
ax, ay, wdum);
fDialog->Move(ax, ay);
fDialog->SetWMPosition(ax, ay);
// make the message box non-resizable
fDialog->SetWMSize(width, height);
fDialog->SetWMSizeHints(width, height, width, height, 0, 0);
fDialog->SetMWMHints(kMWMDecorAll | kMWMDecorResizeH | kMWMDecorMaximize |
kMWMDecorMinimize | kMWMDecorMenu,
kMWMFuncAll | kMWMFuncResize | kMWMFuncMaximize |
kMWMFuncMinimize,
kMWMInputModeless);
// popup dialog and wait till user replies
fDialog->MapWindow();
fRetStr = retstr;
gClient->WaitFor(fDialog);
}
void InputDialog::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
{
// Handle button and text enter events
switch (GET_MSG(msg)) {
case kC_COMMAND:
switch (GET_SUBMSG(msg)) {
case kCM_BUTTON:
switch (parm1) {
case 1:
// here copy the string from text buffer to return variable
strcpy(fRetStr, fTE->GetBuffer()->GetString());
delete this;
break;
case 2:
fRetStr[0] = 0;
delete this;
break;
}
default:
break;
}
break;
case kC_TEXTENTRY:
switch (GET_SUBMSG(msg)) {
case kTE_ENTER:
// here copy the string from text buffer to return variable
strcpy(fRetStr, fTE->GetBuffer()->GetString());
delete this;
break;
default:
break;
}
break;
default:
break;
}
}
//--- Utility Functions --------------------------------------------------------
const char *OpenFileDialog()
{
// Prompt for file to be opened. Depending on navigation in
// dialog the current working directory can be changed.
// The returned file name is always with respect to the
// current directory.
const char *gOpenAsTypes[] = {
"Macro files", "*.C",
"ROOT files", "*.root",
"PostScript", "*.ps",
"Encapsulated PostScript", "*.eps",
"Gif files", "*.gif",
"All files", "*",
0, 0
};
static TGFileInfo fi;
fi.fFileTypes = gOpenAsTypes;
new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDOpen, &fi);
return fi.fFilename;
}
const char *SaveFileDialog()
{
// Prompt for file to be saved. Depending on navigation in
// dialog the current working directory can be changed.
// The returned file name is always with respect to the
// current directory.
const char *gSaveAsTypes[] = {
"Macro files", "*.C",
"ROOT files", "*.root",
"PostScript", "*.ps",
"Encapsulated PostScript", "*.eps",
"Gif files", "*.gif",
"All files", "*",
0, 0
};
static TGFileInfo fi;
fi.fFileTypes = gSaveAsTypes;
new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDSave, &fi);
return fi.fFilename;
}
const char *GetStringDialog(const char *prompt, const char *defval)
{
// Prompt for string. The typed in string is returned.
static char answer[128];
new InputDialog(prompt, defval, answer);
return answer;
}
Int_t GetIntegerDialog(const char *prompt, Int_t defval)
{
// Prompt for integer. The typed in integer is returned.
static char answer[32];
char defv[32];
sprintf(defv, "%d", defval);
new InputDialog(prompt, defv, answer);
return atoi(answer);
}
Float_t GetFloatDialog(const char *prompt, Float_t defval)
{
// Prompt for float. The typed in float is returned.
static char answer[32];
char defv[32];
sprintf(defv, "%f", defval);
new InputDialog(prompt, defv, answer);
return atof(answer);
}