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/** \class TMsgBox
25 \ingroup guiwidgets
26
27A message dialog box.
28
29*/
30
31
32#include "TGMsgBox.h"
33#include "TGButton.h"
34#include "TGIcon.h"
35#include "TGLabel.h"
36#include "TGPicture.h"
37#include "TList.h"
38#include "KeySymbols.h"
39#include "TVirtualX.h"
40#include "strlcpy.h"
41
43
44
45////////////////////////////////////////////////////////////////////////////////
46/// Create a message dialog box.
47
49 const char *title, const char *msg, const TGPicture *icon,
50 Int_t buttons, Int_t *ret_code, UInt_t options,
51 Int_t text_align) :
52 TGTransientFrame(p, main, 10, 10, options)
53{
54 if (p)
55 PMsgBox(title, msg, icon, buttons, ret_code, text_align);
56 else
57 MakeZombie();
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// Create a message dialog box with the following parameters:.
62/// title: Window title
63/// msg: Message to be shown ('\n' may be used to split it in lines)
64/// icon: Picture to be shown at the left on the dialog window.
65/// It might take any of the following values:
66/// kMBIconStop, kMBIconQuestion,
67/// kMBIconExclamation, kMBIconAsterisk
68/// buttons: Buttons to be shown at the botton of the dialog window.
69/// Look at EMsgBoxButton for the different possible values.
70/// ret_code: It will hold the value of the button pressed when the
71/// dialog is closed
72/// options: Frame options of this dialog window.
73/// text_align: Align options for 'msg'. See ETextJustification for the values.
74
76 const char *title, const char *msg, EMsgBoxIcon icon,
77 Int_t buttons, Int_t *ret_code, UInt_t options,
78 Int_t text_align) :
79 TGTransientFrame(p, main, 10, 10, options)
80{
81 const TGPicture *icon_pic;
82
83 switch (icon) {
84 case kMBIconStop:
85 icon_pic = fClient->GetPicture("mb_stop_s.xpm");
86 if (!icon_pic) Error("TGMsgBox", "mb_stop_s.xpm not found");
87 break;
88
89 case kMBIconQuestion:
90 icon_pic = fClient->GetPicture("mb_question_s.xpm");
91 if (!icon_pic) Error("TGMsgBox", "mb_question_s.xpm not found");
92 break;
93
95 icon_pic = fClient->GetPicture("mb_exclamation_s.xpm");
96 if (!icon_pic) Error("TGMsgBox", "mb_exclamation_s.xpm not found");
97 break;
98
99 case kMBIconAsterisk:
100 icon_pic = fClient->GetPicture("mb_asterisk_s.xpm");
101 if (!icon_pic) Error("TGMsgBox", "mb_asterisk_s.xpm not found");
102 break;
103
104 default:
105 icon_pic = 0;
106 break;
107 }
108
109 if (p)
110 PMsgBox(title, msg, icon_pic, buttons, ret_code, text_align);
111 else
112 MakeZombie();
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Protected, common message dialog box initialization.
117
118void TGMsgBox::PMsgBox(const char *title, const char *msg,
119 const TGPicture *icon, Int_t buttons, Int_t *ret_code,
120 Int_t text_align)
121{
122 UInt_t nb, width, height;
123
124 fYes = fNo = fOK = fApply = fRetry = fIgnore = fCancel = fClose =
126 fIcon = 0;
127 fMsgList = new TList;
128 fRetCode = ret_code;
129 nb = width = 0;
130
131 // create the buttons
132
133 fButtonFrame = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
134 fL1 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 3, 3, 0, 0);
135
136 buttons &= (kMBYes | kMBNo | kMBOk | kMBApply |
139 if (buttons == 0) buttons = kMBDismiss;
140
141 if (buttons & kMBYes) {
142 fYes = new TGTextButton(fButtonFrame, new TGHotString("&Yes"), kMBYes);
143 fYes->Associate(this);
146 }
147
148 if (buttons & kMBNo) {
149 fNo = new TGTextButton(fButtonFrame, new TGHotString("&No"), kMBNo);
150 fNo->Associate(this);
153 }
154
155 if (buttons & kMBOk) {
156 fOK = new TGTextButton(fButtonFrame, new TGHotString("&OK"), kMBOk);
157 fOK->Associate(this);
160 }
161
162 if (buttons & kMBApply) {
163 fApply = new TGTextButton(fButtonFrame, new TGHotString("&Apply"), kMBApply);
164 fApply->Associate(this);
167 }
168
169 if (buttons & kMBRetry) {
170 fRetry = new TGTextButton(fButtonFrame, new TGHotString("&Retry"), kMBRetry);
171 fRetry->Associate(this);
174 }
175
176 if (buttons & kMBIgnore) {
177 fIgnore = new TGTextButton(fButtonFrame, new TGHotString("&Ignore"), kMBIgnore);
178 fIgnore->Associate(this);
181 }
182
183 if (buttons & kMBCancel) {
184 fCancel = new TGTextButton(fButtonFrame, new TGHotString("&Cancel"), kMBCancel);
185 fCancel->Associate(this);
188 }
189
190 if (buttons & kMBClose) {
191 fClose = new TGTextButton(fButtonFrame, new TGHotString("C&lose"), kMBClose);
192 fClose->Associate(this);
195 }
196
197 if (buttons & kMBYesAll) {
198 fYesAll = new TGTextButton(fButtonFrame, new TGHotString("Y&es to All"), kMBYesAll);
199 fYesAll->Associate(this);
202 }
203
204 if (buttons & kMBNoAll) {
205 fNoAll = new TGTextButton(fButtonFrame, new TGHotString("No &to All"), kMBNoAll);
206 fNoAll->Associate(this);
209 }
210
211 if (buttons & kMBNewer) {
212 fNewer = new TGTextButton(fButtonFrame, new TGHotString("Ne&wer Only"), kMBNewer);
213 fNewer->Associate(this);
216 }
217
218 if (buttons & kMBAppend) {
219 fAppend = new TGTextButton(fButtonFrame, new TGHotString("A&ppend"), kMBAppend);
220 fAppend->Associate(this);
223 }
224
225 if (buttons & kMBDismiss) {
227 fDismiss->Associate(this);
230 }
231
232 // place buttons at the bottom
233
234 fL2 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
236
237 // keep the buttons centered and with the same width
238
239 fButtonFrame->Resize((width + 20) * nb, GetDefaultHeight());
240
241 fIconFrame = new TGHorizontalFrame(this, 60, 20);
242
243 fL3 = new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 2, 2, 2, 2);
244
245 if (icon) {
246 fIcon = new TGIcon(fIconFrame, icon, icon->GetWidth(), icon->GetHeight());
248 }
249
251
253 4, 2, 2, 2);
254 fL5 = new TGLayoutHints(kLHintsTop | kLHintsExpandX, 10, 10, 7, 2);
255
256 // make one label per line of the message
257 TGLabel *label;
258
259 char *line;
260 char *tmpMsg, *nextLine;
261
262 int len = strlen(msg) + 1;
263 tmpMsg = new char[len];
264 nextLine = tmpMsg;
265
266 line = tmpMsg;
267 strlcpy(nextLine, msg, len);
268 while ((nextLine = strchr(line, '\n'))) {
269 *nextLine = 0;
270 label = new TGLabel(fLabelFrame, line);
271 label->SetTextJustify(text_align);
272 fMsgList->Add(label);
273 fLabelFrame->AddFrame(label, fL4);
274 line = nextLine + 1;
275 }
276
277 label = new TGLabel(fLabelFrame, line);
278 label->SetTextJustify(text_align);
279 fMsgList->Add(label);
280 fLabelFrame->AddFrame(label, fL4);
281 delete [] tmpMsg;
282
285
287
290
293
294 // position relative to the parent's window
295
297
298 // make the message box non-resizable
299
302
303 // set names
304
305 SetWindowName(title);
306 SetIconName(title);
307 SetClassHints("ROOT", "MsgBox");
308
314
315 MapRaised();
316 fClient->WaitFor(this);
317}
318
319////////////////////////////////////////////////////////////////////////////////
320/// Destroy message dialog box.
321
323{
324 if (IsZombie()) return;
325 if (fYes) delete fYes;
326 if (fNo) delete fNo;
327 if (fOK) delete fOK;
328 if (fApply) delete fApply;
329 if (fRetry) delete fRetry;
330 if (fIgnore) delete fIgnore;
331 if (fCancel) delete fCancel;
332 if (fClose) delete fClose;
333 if (fDismiss) delete fDismiss;
334 if (fYesAll) delete fYesAll;
335 if (fNoAll) delete fNoAll;
336 if (fNewer) delete fNewer;
337 if (fAppend) delete fAppend;
338
339 if (fIcon) delete fIcon;
340 delete fButtonFrame;
341 delete fIconFrame;
342 delete fLabelFrame;
343 fMsgList->Delete();
344 delete fMsgList;
345 delete fL1; delete fL2; delete fL3; delete fL4; delete fL5;
346}
347
348////////////////////////////////////////////////////////////////////////////////
349/// Close dialog box. Before deleting itself it sets the return code
350/// to kMBClose.
351
353{
354 if (fRetCode) *fRetCode = kMBClose;
355 DeleteWindow();
356}
357
358////////////////////////////////////////////////////////////////////////////////
359/// Process message dialog box event.
360
362{
363 switch (GET_MSG(msg)) {
364 case kC_COMMAND:
365 switch (GET_SUBMSG(msg)) {
366 case kCM_BUTTON:
367 if (fRetCode) *fRetCode = (Int_t) parm1;
368 DeleteWindow();
369 break;
370
371 default:
372 break;
373 }
374 break;
375 default:
376 break;
377 }
378 return kTRUE;
379}
380
381////////////////////////////////////////////////////////////////////////////////
382/// Handle enter and escape keys (used as Ok and Cancel for now).
383
385{
386 if (event->fType == kGKeyPress) {
387 UInt_t keysym;
388 char input[10];
389 gVirtualX->LookupString(event, input, sizeof(input), keysym);
390
391 if ((EKeySym)keysym == kKey_Escape) {
392 if (fCancel) {
394 DeleteWindow();
395 }
396 return kTRUE;
397 }
398 else if ((EKeySym)keysym == kKey_Enter || (EKeySym)keysym == kKey_Return) {
399 if (fOK) {
400 if (fRetCode) *fRetCode = (Int_t) kMBOk;
401 DeleteWindow();
402 }
403 return kTRUE;
404 }
405 }
406 return TGMainFrame::HandleKey(event);
407}
408
@ 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 main()
Definition Prototype.cxx:12
int Int_t
Definition RtypesCore.h:45
long Longptr_t
Definition RtypesCore.h:82
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
@ kMWMDecorResizeH
Definition TGFrame.h:65
@ kMWMFuncAll
Definition TGFrame.h:49
@ kMWMFuncResize
Definition TGFrame.h:50
@ kMWMDecorMaximize
Definition TGFrame.h:69
@ kMWMDecorMinimize
Definition TGFrame.h:68
@ kMWMDecorMenu
Definition TGFrame.h:67
@ kMWMDecorAll
Definition TGFrame.h:63
@ kMWMFuncMaximize
Definition TGFrame.h:53
@ kMWMInputModeless
Definition TGFrame.h:57
@ kMWMFuncMinimize
Definition TGFrame.h:52
@ kLHintsLeft
Definition TGLayout.h:24
@ kLHintsCenterY
Definition TGLayout.h:28
@ kLHintsCenterX
Definition TGLayout.h:25
@ kLHintsBottom
Definition TGLayout.h:29
@ kLHintsTop
Definition TGLayout.h:27
@ kLHintsExpandX
Definition TGLayout.h:30
@ kMBNo
Definition TGMsgBox.h:32
@ kMBIgnore
Definition TGMsgBox.h:36
@ kMBApply
Definition TGMsgBox.h:34
@ kMBNoAll
Definition TGMsgBox.h:41
@ kMBClose
Definition TGMsgBox.h:38
@ kMBYes
Definition TGMsgBox.h:31
@ kMBAppend
Definition TGMsgBox.h:42
@ kMBRetry
Definition TGMsgBox.h:35
@ kMBCancel
Definition TGMsgBox.h:37
@ kMBOk
Definition TGMsgBox.h:33
@ kMBYesAll
Definition TGMsgBox.h:40
@ kMBNewer
Definition TGMsgBox.h:43
@ kMBDismiss
Definition TGMsgBox.h:39
EMsgBoxIcon
Definition TGMsgBox.h:21
@ kMBIconExclamation
Definition TGMsgBox.h:24
@ kMBIconAsterisk
Definition TGMsgBox.h:25
@ kMBIconStop
Definition TGMsgBox.h:22
@ kMBIconQuestion
Definition TGMsgBox.h:23
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void SetMWMHints
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t SetWMSizeHints
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
#define gVirtualX
Definition TVirtualX.h:337
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:709
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition TGClient.cxx:289
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
UInt_t GetDefaultWidth() const override
Definition TGFrame.h:312
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1164
UInt_t GetDefaultHeight() const override
Definition TGFrame.h:314
void AddInput(UInt_t emask)
Add events specified in the emask to the events the frame should handle.
Definition TGFrame.cxx:339
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:605
virtual UInt_t GetDefaultWidth() const
Definition TGFrame.h:190
virtual void DeleteWindow()
Delete window.
Definition TGFrame.cxx:276
void MapRaised() override
map raised
Definition TGFrame.h:205
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:385
TGHotString is a string with a "hot" character underlined.
Definition TGString.h:42
This class handles GUI icons.
Definition TGIcon.h:22
This class handles GUI labels.
Definition TGLabel.h:24
void SetTextJustify(Int_t tmode)
Set text justification.
Definition TGLabel.cxx:396
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
void SetClassHints(const char *className, const char *resourceName)
Set the windows class and resource name.
Definition TGFrame.cxx:1858
void SetIconName(const char *name)
Set window icon name. This is typically done via the window manager.
Definition TGFrame.cxx:1801
void SetWMSize(UInt_t w, UInt_t h)
Give the window manager a window size hint.
Definition TGFrame.cxx:1893
void SetWindowName(const char *name=nullptr) override
Set window name. This is typically done via the window manager.
Definition TGFrame.cxx:1788
Bool_t HandleKey(Event_t *event) override
Handle keyboard events.
Definition TGFrame.cxx:1606
TGButton * fNoAll
buttons in dialog box
Definition TGMsgBox.h:57
TGButton * fYes
Definition TGMsgBox.h:55
TGButton * fOK
Definition TGMsgBox.h:55
TGLayoutHints * fL5
layout hints
Definition TGMsgBox.h:63
TGLayoutHints * fL2
Definition TGMsgBox.h:63
TGLayoutHints * fL1
Definition TGMsgBox.h:63
TList * fMsgList
text (list of TGLabels)
Definition TGMsgBox.h:64
TGButton * fYesAll
Definition TGMsgBox.h:57
TGHorizontalFrame * fIconFrame
frame containing icon and text
Definition TGMsgBox.h:61
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:118
TGVerticalFrame * fLabelFrame
frame containing text
Definition TGMsgBox.h:62
Bool_t ProcessMessage(Longptr_t msg, Longptr_t parm1, Longptr_t parm2) override
Process message dialog box event.
Definition TGMsgBox.cxx:361
TGMsgBox(const TGMsgBox &)=delete
TGButton * fNo
Definition TGMsgBox.h:55
TGLayoutHints * fL3
Definition TGMsgBox.h:63
Bool_t HandleKey(Event_t *event) override
Handle enter and escape keys (used as Ok and Cancel for now).
Definition TGMsgBox.cxx:384
TGButton * fAppend
Definition TGMsgBox.h:58
TGButton * fRetry
Definition TGMsgBox.h:56
TGHorizontalFrame * fButtonFrame
frame containing buttons
Definition TGMsgBox.h:60
TGButton * fIgnore
Definition TGMsgBox.h:56
TGButton * fDismiss
buttons in dialog box
Definition TGMsgBox.h:58
~TGMsgBox() override
Destroy message dialog box.
Definition TGMsgBox.cxx:322
TGButton * fCancel
buttons in dialog box
Definition TGMsgBox.h:56
TGIcon * fIcon
icon
Definition TGMsgBox.h:59
TGLayoutHints * fL4
Definition TGMsgBox.h:63
TGButton * fClose
Definition TGMsgBox.h:57
TGButton * fNewer
Definition TGMsgBox.h:58
Int_t * fRetCode
address to store return code
Definition TGMsgBox.h:65
void CloseWindow() override
Close dialog box.
Definition TGMsgBox.cxx:352
TGButton * fApply
buttons in dialog box
Definition TGMsgBox.h:55
TGClient * fClient
Connection to display server.
Definition TGObject.h:25
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
UInt_t GetHeight() const
Definition TGPicture.h:53
UInt_t GetWidth() const
Definition TGPicture.h:52
Yield an action as soon as it is clicked.
Definition TGButton.h:142
Defines transient windows that typically are used for dialogs windows.
Definition TGFrame.h:498
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition TGFrame.cxx:1957
A composite frame that layout their children in vertical way.
Definition TGFrame.h:374
virtual void Associate(const TGWindow *w)
Definition TGWidget.h:72
ROOT GUI Window base class.
Definition TGWindow.h:23
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:83
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:468
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition TObject.h:153
void MakeZombie()
Definition TObject.h:53
TLine * line
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250
Event structure.
Definition GuiTypes.h:174
EGEventType fType
of event (see EGEventType)
Definition GuiTypes.h:175