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#include "TVirtualX.h"
38
39
41
42
43////////////////////////////////////////////////////////////////////////////////
44/// Create a message dialog box.
45
47 const char *title, const char *msg, const TGPicture *icon,
48 Int_t buttons, Int_t *ret_code, UInt_t options,
49 Int_t text_align) :
50 TGTransientFrame(p, main, 10, 10, options)
51{
52 if (p)
53 PMsgBox(title, msg, icon, buttons, ret_code, text_align);
54 else
55 MakeZombie();
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Create a message dialog box with the following parameters:.
60/// title: Window title
61/// msg: Message to be shown ('\n' may be used to split it in lines)
62/// icon: Picture to be shown at the left on the dialog window.
63/// It might take any of the following values:
64/// kMBIconStop, kMBIconQuestion,
65/// kMBIconExclamation, kMBIconAsterisk
66/// buttons: Buttons to be shown at the botton of the dialgo window.
67/// Look at EMsgBoxButton for the different possible values.
68/// ret_code: It will hold the value of the button pressed when the
69/// dialog is closed
70/// options: Frame options of this dialog window.
71/// text_align: Align options for 'msg'. See ETextJustification for the values.
72
74 const char *title, const char *msg, EMsgBoxIcon icon,
75 Int_t buttons, Int_t *ret_code, UInt_t options,
76 Int_t text_align) :
77 TGTransientFrame(p, main, 10, 10, options)
78{
79 const TGPicture *icon_pic;
80
81 switch (icon) {
82 case kMBIconStop:
83 icon_pic = fClient->GetPicture("mb_stop_s.xpm");
84 if (!icon_pic) Error("TGMsgBox", "mb_stop_s.xpm not found");
85 break;
86
87 case kMBIconQuestion:
88 icon_pic = fClient->GetPicture("mb_question_s.xpm");
89 if (!icon_pic) Error("TGMsgBox", "mb_question_s.xpm not found");
90 break;
91
93 icon_pic = fClient->GetPicture("mb_exclamation_s.xpm");
94 if (!icon_pic) Error("TGMsgBox", "mb_exclamation_s.xpm not found");
95 break;
96
97 case kMBIconAsterisk:
98 icon_pic = fClient->GetPicture("mb_asterisk_s.xpm");
99 if (!icon_pic) Error("TGMsgBox", "mb_asterisk_s.xpm not found");
100 break;
101
102 default:
103 icon_pic = 0;
104 break;
105 }
106
107 if (p)
108 PMsgBox(title, msg, icon_pic, buttons, ret_code, text_align);
109 else
110 MakeZombie();
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Protected, common message dialog box initialization.
115
116void TGMsgBox::PMsgBox(const char *title, const char *msg,
117 const TGPicture *icon, Int_t buttons, Int_t *ret_code,
118 Int_t text_align)
119{
120 UInt_t nb, width, height;
121
122 fYes = fNo = fOK = fApply = fRetry = fIgnore = fCancel = fClose =
124 fIcon = 0;
125 fMsgList = new TList;
126 fRetCode = ret_code;
127 nb = width = 0;
128
129 // create the buttons
130
131 fButtonFrame = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
132 fL1 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 3, 3, 0, 0);
133
134 buttons &= (kMBYes | kMBNo | kMBOk | kMBApply |
137 if (buttons == 0) buttons = kMBDismiss;
138
139 if (buttons & kMBYes) {
140 fYes = new TGTextButton(fButtonFrame, new TGHotString("&Yes"), kMBYes);
141 fYes->Associate(this);
144 }
145
146 if (buttons & kMBNo) {
147 fNo = new TGTextButton(fButtonFrame, new TGHotString("&No"), kMBNo);
148 fNo->Associate(this);
151 }
152
153 if (buttons & kMBOk) {
154 fOK = new TGTextButton(fButtonFrame, new TGHotString("&OK"), kMBOk);
155 fOK->Associate(this);
158 }
159
160 if (buttons & kMBApply) {
161 fApply = new TGTextButton(fButtonFrame, new TGHotString("&Apply"), kMBApply);
162 fApply->Associate(this);
165 }
166
167 if (buttons & kMBRetry) {
168 fRetry = new TGTextButton(fButtonFrame, new TGHotString("&Retry"), kMBRetry);
169 fRetry->Associate(this);
172 }
173
174 if (buttons & kMBIgnore) {
175 fIgnore = new TGTextButton(fButtonFrame, new TGHotString("&Ignore"), kMBIgnore);
176 fIgnore->Associate(this);
179 }
180
181 if (buttons & kMBCancel) {
182 fCancel = new TGTextButton(fButtonFrame, new TGHotString("&Cancel"), kMBCancel);
183 fCancel->Associate(this);
186 }
187
188 if (buttons & kMBClose) {
189 fClose = new TGTextButton(fButtonFrame, new TGHotString("C&lose"), kMBClose);
190 fClose->Associate(this);
193 }
194
195 if (buttons & kMBYesAll) {
196 fYesAll = new TGTextButton(fButtonFrame, new TGHotString("Y&es to All"), kMBYesAll);
197 fYesAll->Associate(this);
200 }
201
202 if (buttons & kMBNoAll) {
203 fNoAll = new TGTextButton(fButtonFrame, new TGHotString("No &to All"), kMBNoAll);
204 fNoAll->Associate(this);
207 }
208
209 if (buttons & kMBNewer) {
210 fNewer = new TGTextButton(fButtonFrame, new TGHotString("Ne&wer Only"), kMBNewer);
211 fNewer->Associate(this);
214 }
215
216 if (buttons & kMBAppend) {
217 fAppend = new TGTextButton(fButtonFrame, new TGHotString("A&ppend"), kMBAppend);
218 fAppend->Associate(this);
221 }
222
223 if (buttons & kMBDismiss) {
225 fDismiss->Associate(this);
228 }
229
230 // place buttons at the bottom
231
232 fL2 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
234
235 // keep the buttons centered and with the same width
236
237 fButtonFrame->Resize((width + 20) * nb, GetDefaultHeight());
238
239 fIconFrame = new TGHorizontalFrame(this, 60, 20);
240
241 fL3 = new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 2, 2, 2, 2);
242
243 if (icon) {
244 fIcon = new TGIcon(fIconFrame, icon, icon->GetWidth(), icon->GetHeight());
246 }
247
249
251 4, 2, 2, 2);
252 fL5 = new TGLayoutHints(kLHintsTop | kLHintsExpandX, 10, 10, 7, 2);
253
254 // make one label per line of the message
255 TGLabel *label;
256
257 char *line;
258 char *tmpMsg, *nextLine;
259
260 int len = strlen(msg) + 1;
261 tmpMsg = new char[len];
262 nextLine = tmpMsg;
263
264 line = tmpMsg;
265 strlcpy(nextLine, msg, len);
266 while ((nextLine = strchr(line, '\n'))) {
267 *nextLine = 0;
268 label = new TGLabel(fLabelFrame, line);
269 label->SetTextJustify(text_align);
270 fMsgList->Add(label);
271 fLabelFrame->AddFrame(label, fL4);
272 line = nextLine + 1;
273 }
274
275 label = new TGLabel(fLabelFrame, line);
276 label->SetTextJustify(text_align);
277 fMsgList->Add(label);
278 fLabelFrame->AddFrame(label, fL4);
279 delete [] tmpMsg;
280
283
285
287 height = GetDefaultHeight();
288
289 Resize(width, height);
291
292 // position relative to the parent's window
293
295
296 // make the message box non-resizable
297
298 SetWMSize(width, height);
299 SetWMSizeHints(width, height, width, height, 0, 0);
300
301 // set names
302
303 SetWindowName(title);
304 SetIconName(title);
305 SetClassHints("ROOT", "MsgBox");
306
312
313 MapRaised();
314 fClient->WaitFor(this);
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// Destroy message dialog box.
319
321{
322 if (IsZombie()) return;
323 if (fYes) delete fYes;
324 if (fNo) delete fNo;
325 if (fOK) delete fOK;
326 if (fApply) delete fApply;
327 if (fRetry) delete fRetry;
328 if (fIgnore) delete fIgnore;
329 if (fCancel) delete fCancel;
330 if (fClose) delete fClose;
331 if (fDismiss) delete fDismiss;
332 if (fYesAll) delete fYesAll;
333 if (fNoAll) delete fNoAll;
334 if (fNewer) delete fNewer;
335 if (fAppend) delete fAppend;
336
337 if (fIcon) delete fIcon;
338 delete fButtonFrame;
339 delete fIconFrame;
340 delete fLabelFrame;
341 fMsgList->Delete();
342 delete fMsgList;
343 delete fL1; delete fL2; delete fL3; delete fL4; delete fL5;
344}
345
346////////////////////////////////////////////////////////////////////////////////
347/// Close dialog box. Before deleting itself it sets the return code
348/// to kMBClose.
349
351{
352 if (fRetCode) *fRetCode = kMBClose;
353 DeleteWindow();
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Process message dialog box event.
358
360{
361 switch (GET_MSG(msg)) {
362 case kC_COMMAND:
363 switch (GET_SUBMSG(msg)) {
364 case kCM_BUTTON:
365 if (fRetCode) *fRetCode = (Int_t) parm1;
366 DeleteWindow();
367 break;
368
369 default:
370 break;
371 }
372 break;
373 default:
374 break;
375 }
376 return kTRUE;
377}
378
379////////////////////////////////////////////////////////////////////////////////
380/// Handle enter and escape keys (used as Ok and Cancel for now).
381
383{
384 if (event->fType == kGKeyPress) {
385 UInt_t keysym;
386 char input[10];
387 gVirtualX->LookupString(event, input, sizeof(input), keysym);
388
389 if ((EKeySym)keysym == kKey_Escape) {
390 if (fCancel) {
392 DeleteWindow();
393 }
394 return kTRUE;
395 }
396 else if ((EKeySym)keysym == kKey_Enter || (EKeySym)keysym == kKey_Return) {
397 if (fOK) {
398 if (fRetCode) *fRetCode = (Int_t) kMBOk;
399 DeleteWindow();
400 }
401 return kTRUE;
402 }
403 }
404 return TGMainFrame::HandleKey(event);
405}
406
@ kGKeyPress
Definition: GuiTypes.h:59
const Mask_t kKeyPressMask
Definition: GuiTypes.h:158
@ kFixedWidth
Definition: GuiTypes.h:387
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:43
long Long_t
Definition: RtypesCore.h:52
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
@ kMWMDecorResizeH
Definition: TGFrame.h:74
@ kMWMFuncAll
Definition: TGFrame.h:58
@ kMWMFuncResize
Definition: TGFrame.h:59
@ kMWMDecorMaximize
Definition: TGFrame.h:78
@ kMWMDecorMinimize
Definition: TGFrame.h:77
@ kMWMDecorMenu
Definition: TGFrame.h:76
@ kMWMDecorAll
Definition: TGFrame.h:72
@ kMWMFuncMaximize
Definition: TGFrame.h:62
@ kMWMInputModeless
Definition: TGFrame.h:66
@ kMWMFuncMinimize
Definition: TGFrame.h:61
@ 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:338
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:1101
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:349
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1148
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:351
void AddInput(UInt_t emask)
Add events specified in the emask to the events the frame should handle.
Definition: TGFrame.cxx:323
virtual void MapRaised()
map raised
Definition: TGFrame.h:230
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:215
virtual void DeleteWindow()
Delete window.
Definition: TGFrame.cxx:260
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:589
Definition: TGIcon.h:30
void SetTextJustify(Int_t tmode)
Set text justification.
Definition: TGLabel.cxx:394
void SetClassHints(const char *className, const char *resourceName)
Set the windows class and resource name.
Definition: TGFrame.cxx:1816
void SetIconName(const char *name)
Set window icon name. This is typically done via the window manager.
Definition: TGFrame.cxx:1761
virtual Bool_t HandleKey(Event_t *event)
Handle keyboard events.
Definition: TGFrame.cxx:1566
void SetWMSize(UInt_t w, UInt_t h)
Give the window manager a window size hint.
Definition: TGFrame.cxx:1851
void SetMWMHints(UInt_t value, UInt_t funcs, UInt_t input)
Set decoration style for MWM-compatible wm (mwm, ncdwm, fvwm?).
Definition: TGFrame.cxx:1826
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:1864
void SetWindowName(const char *name=0)
Set window name. This is typically done via the window manager.
Definition: TGFrame.cxx:1748
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:382
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:320
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:116
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:350
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:359
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:1915
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:469
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:149
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
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