Logo ROOT  
Reference Guide
TGMsgBox.cxx
Go to the documentation of this file.
1// @(#)root/gui:$Id: d25b6f3b0b2546fe028288bd22c21588bdd1b8c1 $
2// Author: Fons Rademakers 09/01/98
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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 This source is based on Xclass95, a Win95-looking GUI toolkit.
14 Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
15
16 Xclass95 is free software; you can redistribute it and/or
17 modify it under the terms of the GNU Library General Public
18 License as published by the Free Software Foundation; either
19 version 2 of the License, or (at your option) any later version.
20
21**************************************************************************/
22
23//////////////////////////////////////////////////////////////////////////
24// //
25// TMsgBox //
26// //
27// A message dialog box. //
28// //
29//////////////////////////////////////////////////////////////////////////
30
31#include "TGMsgBox.h"
32#include "TGButton.h"
33#include "TGIcon.h"
34#include "TGLabel.h"
35#include "TList.h"
36#include "KeySymbols.h"
37
38
40
41
42////////////////////////////////////////////////////////////////////////////////
43/// Create a message dialog box.
44
46 const char *title, const char *msg, const TGPicture *icon,
47 Int_t buttons, Int_t *ret_code, UInt_t options,
48 Int_t text_align) :
49 TGTransientFrame(p, main, 10, 10, options)
50{
51 if (p)
52 PMsgBox(title, msg, icon, buttons, ret_code, text_align);
53 else
54 MakeZombie();
55}
56
57////////////////////////////////////////////////////////////////////////////////
58/// Create a message dialog box with the following parameters:.
59/// title: Window title
60/// msg: Message to be shown ('\n' may be used to split it in lines)
61/// icon: Picture to be shown at the left on the dialog window.
62/// It might take any of the following values:
63/// kMBIconStop, kMBIconQuestion,
64/// kMBIconExclamation, kMBIconAsterisk
65/// buttons: Buttons to be shown at the botton of the dialgo window.
66/// Look at EMsgBoxButton for the different possible values.
67/// ret_code: It will hold the value of the button pressed when the
68/// dialog is closed
69/// options: Frame options of this dialog window.
70/// text_align: Align options for 'msg'. See ETextJustification for the values.
71
73 const char *title, const char *msg, EMsgBoxIcon icon,
74 Int_t buttons, Int_t *ret_code, UInt_t options,
75 Int_t text_align) :
76 TGTransientFrame(p, main, 10, 10, options)
77{
78 const TGPicture *icon_pic;
79
80 switch (icon) {
81 case kMBIconStop:
82 icon_pic = fClient->GetPicture("mb_stop_s.xpm");
83 if (!icon_pic) Error("TGMsgBox", "mb_stop_s.xpm not found");
84 break;
85
86 case kMBIconQuestion:
87 icon_pic = fClient->GetPicture("mb_question_s.xpm");
88 if (!icon_pic) Error("TGMsgBox", "mb_question_s.xpm not found");
89 break;
90
92 icon_pic = fClient->GetPicture("mb_exclamation_s.xpm");
93 if (!icon_pic) Error("TGMsgBox", "mb_exclamation_s.xpm not found");
94 break;
95
96 case kMBIconAsterisk:
97 icon_pic = fClient->GetPicture("mb_asterisk_s.xpm");
98 if (!icon_pic) Error("TGMsgBox", "mb_asterisk_s.xpm not found");
99 break;
100
101 default:
102 icon_pic = 0;
103 break;
104 }
105
106 if (p)
107 PMsgBox(title, msg, icon_pic, buttons, ret_code, text_align);
108 else
109 MakeZombie();
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Protected, common message dialog box initialization.
114
115void TGMsgBox::PMsgBox(const char *title, const char *msg,
116 const TGPicture *icon, Int_t buttons, Int_t *ret_code,
117 Int_t text_align)
118{
119 UInt_t nb, width, height;
120
121 fYes = fNo = fOK = fApply = fRetry = fIgnore = fCancel = fClose =
123 fIcon = 0;
124 fMsgList = new TList;
125 fRetCode = ret_code;
126 nb = width = 0;
127
128 // create the buttons
129
130 fButtonFrame = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
131 fL1 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 3, 3, 0, 0);
132
133 buttons &= (kMBYes | kMBNo | kMBOk | kMBApply |
136 if (buttons == 0) buttons = kMBDismiss;
137
138 if (buttons & kMBYes) {
139 fYes = new TGTextButton(fButtonFrame, new TGHotString("&Yes"), kMBYes);
140 fYes->Associate(this);
143 }
144
145 if (buttons & kMBNo) {
146 fNo = new TGTextButton(fButtonFrame, new TGHotString("&No"), kMBNo);
147 fNo->Associate(this);
150 }
151
152 if (buttons & kMBOk) {
153 fOK = new TGTextButton(fButtonFrame, new TGHotString("&OK"), kMBOk);
154 fOK->Associate(this);
157 }
158
159 if (buttons & kMBApply) {
160 fApply = new TGTextButton(fButtonFrame, new TGHotString("&Apply"), kMBApply);
161 fApply->Associate(this);
164 }
165
166 if (buttons & kMBRetry) {
167 fRetry = new TGTextButton(fButtonFrame, new TGHotString("&Retry"), kMBRetry);
168 fRetry->Associate(this);
171 }
172
173 if (buttons & kMBIgnore) {
174 fIgnore = new TGTextButton(fButtonFrame, new TGHotString("&Ignore"), kMBIgnore);
175 fIgnore->Associate(this);
178 }
179
180 if (buttons & kMBCancel) {
181 fCancel = new TGTextButton(fButtonFrame, new TGHotString("&Cancel"), kMBCancel);
182 fCancel->Associate(this);
185 }
186
187 if (buttons & kMBClose) {
188 fClose = new TGTextButton(fButtonFrame, new TGHotString("C&lose"), kMBClose);
189 fClose->Associate(this);
192 }
193
194 if (buttons & kMBYesAll) {
195 fYesAll = new TGTextButton(fButtonFrame, new TGHotString("Y&es to All"), kMBYesAll);
196 fYesAll->Associate(this);
199 }
200
201 if (buttons & kMBNoAll) {
202 fNoAll = new TGTextButton(fButtonFrame, new TGHotString("No &to All"), kMBNoAll);
203 fNoAll->Associate(this);
206 }
207
208 if (buttons & kMBNewer) {
209 fNewer = new TGTextButton(fButtonFrame, new TGHotString("Ne&wer Only"), kMBNewer);
210 fNewer->Associate(this);
213 }
214
215 if (buttons & kMBAppend) {
216 fAppend = new TGTextButton(fButtonFrame, new TGHotString("A&ppend"), kMBAppend);
217 fAppend->Associate(this);
220 }
221
222 if (buttons & kMBDismiss) {
224 fDismiss->Associate(this);
227 }
228
229 // place buttons at the bottom
230
231 fL2 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
233
234 // keep the buttons centered and with the same width
235
236 fButtonFrame->Resize((width + 20) * nb, GetDefaultHeight());
237
238 fIconFrame = new TGHorizontalFrame(this, 60, 20);
239
240 fL3 = new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 2, 2, 2, 2);
241
242 if (icon) {
243 fIcon = new TGIcon(fIconFrame, icon, icon->GetWidth(), icon->GetHeight());
245 }
246
248
250 4, 2, 2, 2);
251 fL5 = new TGLayoutHints(kLHintsTop | kLHintsExpandX, 10, 10, 7, 2);
252
253 // make one label per line of the message
254 TGLabel *label;
255
256 char *line;
257 char *tmpMsg, *nextLine;
258
259 int len = strlen(msg) + 1;
260 tmpMsg = new char[len];
261 nextLine = tmpMsg;
262
263 line = tmpMsg;
264 strlcpy(nextLine, msg, len);
265 while ((nextLine = strchr(line, '\n'))) {
266 *nextLine = 0;
267 label = new TGLabel(fLabelFrame, line);
268 label->SetTextJustify(text_align);
269 fMsgList->Add(label);
270 fLabelFrame->AddFrame(label, fL4);
271 line = nextLine + 1;
272 }
273
274 label = new TGLabel(fLabelFrame, line);
275 label->SetTextJustify(text_align);
276 fMsgList->Add(label);
277 fLabelFrame->AddFrame(label, fL4);
278 delete [] tmpMsg;
279
282
284
286 height = GetDefaultHeight();
287
288 Resize(width, height);
290
291 // position relative to the parent's window
292
294
295 // make the message box non-resizable
296
297 SetWMSize(width, height);
298 SetWMSizeHints(width, height, width, height, 0, 0);
299
300 // set names
301
302 SetWindowName(title);
303 SetIconName(title);
304 SetClassHints("ROOT", "MsgBox");
305
311
312 MapRaised();
313 fClient->WaitFor(this);
314}
315
316////////////////////////////////////////////////////////////////////////////////
317/// Destroy message dialog box.
318
320{
321 if (IsZombie()) return;
322 if (fYes) delete fYes;
323 if (fNo) delete fNo;
324 if (fOK) delete fOK;
325 if (fApply) delete fApply;
326 if (fRetry) delete fRetry;
327 if (fIgnore) delete fIgnore;
328 if (fCancel) delete fCancel;
329 if (fClose) delete fClose;
330 if (fDismiss) delete fDismiss;
331 if (fYesAll) delete fYesAll;
332 if (fNoAll) delete fNoAll;
333 if (fNewer) delete fNewer;
334 if (fAppend) delete fAppend;
335
336 if (fIcon) delete fIcon;
337 delete fButtonFrame;
338 delete fIconFrame;
339 delete fLabelFrame;
340 fMsgList->Delete();
341 delete fMsgList;
342 delete fL1; delete fL2; delete fL3; delete fL4; delete fL5;
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Close dialog box. Before deleting itself it sets the return code
347/// to kMBClose.
348
350{
351 if (fRetCode) *fRetCode = kMBClose;
352 DeleteWindow();
353}
354
355////////////////////////////////////////////////////////////////////////////////
356/// Process message dialog box event.
357
359{
360 switch (GET_MSG(msg)) {
361 case kC_COMMAND:
362 switch (GET_SUBMSG(msg)) {
363 case kCM_BUTTON:
364 if (fRetCode) *fRetCode = (Int_t) parm1;
365 DeleteWindow();
366 break;
367
368 default:
369 break;
370 }
371 break;
372 default:
373 break;
374 }
375 return kTRUE;
376}
377
378////////////////////////////////////////////////////////////////////////////////
379/// Handle enter and escape keys (used as Ok and Cancel for now).
380
382{
383 if (event->fType == kGKeyPress) {
384 UInt_t keysym;
385 char input[10];
386 gVirtualX->LookupString(event, input, sizeof(input), keysym);
387
388 if ((EKeySym)keysym == kKey_Escape) {
389 if (fCancel) {
391 DeleteWindow();
392 }
393 return kTRUE;
394 }
395 else if ((EKeySym)keysym == kKey_Enter || (EKeySym)keysym == kKey_Return) {
396 if (fOK) {
397 if (fRetCode) *fRetCode = (Int_t) kMBOk;
398 DeleteWindow();
399 }
400 return kTRUE;
401 }
402 }
403 return TGMainFrame::HandleKey(event);
404}
405
@ kGKeyPress
Definition: GuiTypes.h:59
const Mask_t kKeyPressMask
Definition: GuiTypes.h:158
EKeySym
Definition: KeySymbols.h:25
@ kKey_Return
Definition: KeySymbols.h:30
@ kKey_Escape
Definition: KeySymbols.h:26
@ kKey_Enter
Definition: KeySymbols.h:31
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
long Long_t
Definition: RtypesCore.h:50
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
@ kMWMDecorResizeH
Definition: TGFrame.h:96
@ kMWMFuncAll
Definition: TGFrame.h:80
@ kMWMFuncResize
Definition: TGFrame.h:81
@ kMWMDecorMaximize
Definition: TGFrame.h:100
@ kMWMDecorMinimize
Definition: TGFrame.h:99
@ kMWMDecorMenu
Definition: TGFrame.h:98
@ kMWMDecorAll
Definition: TGFrame.h:94
@ kMWMFuncMaximize
Definition: TGFrame.h:84
@ kMWMInputModeless
Definition: TGFrame.h:88
@ kMWMFuncMinimize
Definition: TGFrame.h:83
@ kFixedWidth
Definition: TGFrame.h:65
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsCenterY
Definition: TGLayout.h:35
@ kLHintsCenterX
Definition: TGLayout.h:32
@ kLHintsBottom
Definition: TGLayout.h:36
@ kLHintsTop
Definition: TGLayout.h:34
@ kLHintsExpandX
Definition: TGLayout.h:37
@ kMBNo
Definition: TGMsgBox.h:43
@ kMBIgnore
Definition: TGMsgBox.h:47
@ kMBApply
Definition: TGMsgBox.h:45
@ kMBNoAll
Definition: TGMsgBox.h:52
@ kMBClose
Definition: TGMsgBox.h:49
@ kMBYes
Definition: TGMsgBox.h:42
@ kMBAppend
Definition: TGMsgBox.h:53
@ kMBRetry
Definition: TGMsgBox.h:46
@ kMBCancel
Definition: TGMsgBox.h:48
@ kMBOk
Definition: TGMsgBox.h:44
@ kMBYesAll
Definition: TGMsgBox.h:51
@ kMBNewer
Definition: TGMsgBox.h:54
@ kMBDismiss
Definition: TGMsgBox.h:50
EMsgBoxIcon
Definition: TGMsgBox.h:32
@ kMBIconExclamation
Definition: TGMsgBox.h:35
@ kMBIconAsterisk
Definition: TGMsgBox.h:36
@ kMBIconStop
Definition: TGMsgBox.h:33
@ kMBIconQuestion
Definition: TGMsgBox.h:34
#define gVirtualX
Definition: TVirtualX.h:345
Int_t GET_MSG(Long_t val)
@ kC_COMMAND
@ kCM_BUTTON
Int_t GET_SUBMSG(Long_t val)
void WaitFor(TGWindow *w)
Wait for window to be destroyed.
Definition: TGClient.cxx:708
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition: TGClient.cxx:289
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:371
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1146
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:373
void AddInput(UInt_t emask)
Add events specified in the emask to the events the frame should handle.
Definition: TGFrame.cxx:321
virtual void MapRaised()
Definition: TGFrame.h:252
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:237
virtual void DeleteWindow()
Delete window.
Definition: TGFrame.cxx:258
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
Definition: TGIcon.h:30
void SetTextJustify(Int_t tmode)
Set text justification.
Definition: TGLabel.cxx:393
void SetClassHints(const char *className, const char *resourceName)
Set the windows class and resource name.
Definition: TGFrame.cxx:1814
void SetIconName(const char *name)
Set window icon name. This is typically done via the window manager.
Definition: TGFrame.cxx:1759
virtual Bool_t HandleKey(Event_t *event)
Handle keyboard events.
Definition: TGFrame.cxx:1564
void SetWMSize(UInt_t w, UInt_t h)
Give the window manager a window size hint.
Definition: TGFrame.cxx:1849
void SetMWMHints(UInt_t value, UInt_t funcs, UInt_t input)
Set decoration style for MWM-compatible wm (mwm, ncdwm, fvwm?).
Definition: TGFrame.cxx:1824
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
void SetWindowName(const char *name=0)
Set window name. This is typically done via the window manager.
Definition: TGFrame.cxx:1746
TGButton * fNoAll
Definition: TGMsgBox.h:68
TGButton * fYes
Definition: TGMsgBox.h:66
TGButton * fOK
Definition: TGMsgBox.h:66
TGLayoutHints * fL5
Definition: TGMsgBox.h:74
TGLayoutHints * fL2
Definition: TGMsgBox.h:74
TGLayoutHints * fL1
Definition: TGMsgBox.h:74
virtual Bool_t HandleKey(Event_t *event)
Handle enter and escape keys (used as Ok and Cancel for now).
Definition: TGMsgBox.cxx:381
TList * fMsgList
Definition: TGMsgBox.h:75
TGButton * fYesAll
Definition: TGMsgBox.h:68
TGHorizontalFrame * fIconFrame
Definition: TGMsgBox.h:72
virtual ~TGMsgBox()
Destroy message dialog box.
Definition: TGMsgBox.cxx:319
void PMsgBox(const char *title, const char *msg, const TGPicture *icon, Int_t buttons, Int_t *ret_code, Int_t text_align)
Protected, common message dialog box initialization.
Definition: TGMsgBox.cxx:115
TGVerticalFrame * fLabelFrame
Definition: TGMsgBox.h:73
TGButton * fNo
Definition: TGMsgBox.h:66
TGLayoutHints * fL3
Definition: TGMsgBox.h:74
virtual void CloseWindow()
Close dialog box.
Definition: TGMsgBox.cxx:349
TGButton * fAppend
Definition: TGMsgBox.h:69
TGButton * fRetry
Definition: TGMsgBox.h:67
TGHorizontalFrame * fButtonFrame
Definition: TGMsgBox.h:71
TGButton * fIgnore
Definition: TGMsgBox.h:67
TGMsgBox(const TGMsgBox &)
TGButton * fDismiss
Definition: TGMsgBox.h:69
TGButton * fCancel
Definition: TGMsgBox.h:67
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
Process message dialog box event.
Definition: TGMsgBox.cxx:358
TGIcon * fIcon
Definition: TGMsgBox.h:70
TGLayoutHints * fL4
Definition: TGMsgBox.h:74
TGButton * fClose
Definition: TGMsgBox.h:68
TGButton * fNewer
Definition: TGMsgBox.h:69
Int_t * fRetCode
Definition: TGMsgBox.h:76
TGButton * fApply
Definition: TGMsgBox.h:66
TGClient * fClient
Definition: TGObject.h:37
UInt_t GetHeight() const
Definition: TGPicture.h:64
UInt_t GetWidth() const
Definition: TGPicture.h:63
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition: TGFrame.cxx:1913
virtual void Associate(const TGWindow *w)
Definition: TGWidget.h:84
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:467
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
void MakeZombie()
Definition: TObject.h:49
TLine * line
int main(int argc, char **argv)
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
EGEventType fType
Definition: GuiTypes.h:174