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