Logo ROOT   6.08/07
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 
91  case kMBIconExclamation:
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 
115 void 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 =
122  fYesAll = fNoAll = fNewer = fAppend = fDismiss = 0;
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);
142  width = TMath::Max(width, fYes->GetDefaultWidth()); ++nb;
143  }
144 
145  if (buttons & kMBNo) {
146  fNo = new TGTextButton(fButtonFrame, new TGHotString("&No"), kMBNo);
147  fNo->Associate(this);
149  width = TMath::Max(width, fNo->GetDefaultWidth()); ++nb;
150  }
151 
152  if (buttons & kMBOk) {
153  fOK = new TGTextButton(fButtonFrame, new TGHotString("&OK"), kMBOk);
154  fOK->Associate(this);
156  width = TMath::Max(width, fOK->GetDefaultWidth()); ++nb;
157  }
158 
159  if (buttons & kMBApply) {
160  fApply = new TGTextButton(fButtonFrame, new TGHotString("&Apply"), kMBApply);
161  fApply->Associate(this);
163  width = TMath::Max(width, fApply->GetDefaultWidth()); ++nb;
164  }
165 
166  if (buttons & kMBRetry) {
167  fRetry = new TGTextButton(fButtonFrame, new TGHotString("&Retry"), kMBRetry);
168  fRetry->Associate(this);
170  width = TMath::Max(width, fRetry->GetDefaultWidth()); ++nb;
171  }
172 
173  if (buttons & kMBIgnore) {
174  fIgnore = new TGTextButton(fButtonFrame, new TGHotString("&Ignore"), kMBIgnore);
175  fIgnore->Associate(this);
177  width = TMath::Max(width, fIgnore->GetDefaultWidth()); ++nb;
178  }
179 
180  if (buttons & kMBCancel) {
181  fCancel = new TGTextButton(fButtonFrame, new TGHotString("&Cancel"), kMBCancel);
182  fCancel->Associate(this);
184  width = TMath::Max(width, fCancel->GetDefaultWidth()); ++nb;
185  }
186 
187  if (buttons & kMBClose) {
188  fClose = new TGTextButton(fButtonFrame, new TGHotString("C&lose"), kMBClose);
189  fClose->Associate(this);
191  width = TMath::Max(width, fClose->GetDefaultWidth()); ++nb;
192  }
193 
194  if (buttons & kMBYesAll) {
195  fYesAll = new TGTextButton(fButtonFrame, new TGHotString("Y&es to All"), kMBYesAll);
196  fYesAll->Associate(this);
198  width = TMath::Max(width, fYesAll->GetDefaultWidth()); ++nb;
199  }
200 
201  if (buttons & kMBNoAll) {
202  fNoAll = new TGTextButton(fButtonFrame, new TGHotString("No &to All"), kMBNoAll);
203  fNoAll->Associate(this);
205  width = TMath::Max(width, fNoAll->GetDefaultWidth()); ++nb;
206  }
207 
208  if (buttons & kMBNewer) {
209  fNewer = new TGTextButton(fButtonFrame, new TGHotString("Ne&wer Only"), kMBNewer);
210  fNewer->Associate(this);
212  width = TMath::Max(width, fNewer->GetDefaultWidth()); ++nb;
213  }
214 
215  if (buttons & kMBAppend) {
216  fAppend = new TGTextButton(fButtonFrame, new TGHotString("A&ppend"), kMBAppend);
217  fAppend->Associate(this);
219  width = TMath::Max(width, fAppend->GetDefaultWidth()); ++nb;
220  }
221 
222  if (buttons & kMBDismiss) {
223  fDismiss = new TGTextButton(fButtonFrame, new TGHotString("&Dismiss"), kMBDismiss);
224  fDismiss->Associate(this);
226  width = TMath::Max(width, fDismiss->GetDefaultWidth()); ++nb;
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 
247  fLabelFrame = new TGVerticalFrame(fIconFrame, 60, 20);
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 
283  MapSubwindows();
284 
285  width = GetDefaultWidth();
286  height = GetDefaultHeight();
287 
288  Resize(width, height);
290 
291  // position relative to the parent's window
292 
293  CenterOnParent();
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) {
390  if (fRetCode) *fRetCode = (Int_t) kMBCancel;
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 
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:405
TLine * line
TGButton * fCancel
Definition: TGMsgBox.h:75
void AddInput(UInt_t emask)
Add events specified in the emask to the events the frame should handle.
Definition: TGFrame.cxx:321
TGButton * fIgnore
Definition: TGMsgBox.h:75
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition: TGFrame.cxx:1913
virtual Bool_t HandleKey(Event_t *event)
Handle keyboard events.
Definition: TGFrame.cxx:1564
void SetWindowName(const char *name=0)
Set window name. This is typically done via the window manager.
Definition: TGFrame.cxx:1746
TGButton * fClose
Definition: TGMsgBox.h:76
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:389
TGButton * fOK
Definition: TGMsgBox.h:74
virtual void MapRaised()
Definition: TGFrame.h:268
void SetIconName(const char *name)
Set window icon name. This is typically done via the window manager.
Definition: TGFrame.cxx:1759
UInt_t GetHeight() const
Definition: TGPicture.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
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
Process message dialog box event.
Definition: TGMsgBox.cxx:358
TGLayoutHints * fL5
Definition: TGMsgBox.h:82
virtual void CloseWindow()
Close dialog box.
Definition: TGMsgBox.cxx:349
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
TGMsgBox(const TGMsgBox &)
TGButton * fApply
Definition: TGMsgBox.h:74
Definition: TGIcon.h:34
EMsgBoxIcon
Definition: TGMsgBox.h:40
void SetClassHints(const char *className, const char *resourceName)
Set the windows class and resource name.
Definition: TGFrame.cxx:1814
TGLayoutHints * fL1
Definition: TGMsgBox.h:82
TList * fMsgList
Definition: TGMsgBox.h:83
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
Definition: TGMsgBox.h:51
void WaitFor(TGWindow *w)
Wait for window to be destroyed.
Definition: TGClient.cxx:706
A doubly linked list.
Definition: TList.h:47
const Mask_t kKeyPressMask
Definition: GuiTypes.h:160
TGLayoutHints * fL4
Definition: TGMsgBox.h:82
Int_t * fRetCode
Definition: TGMsgBox.h:84
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:387
EGEventType fType
Definition: GuiTypes.h:176
TGButton * fAppend
Definition: TGMsgBox.h:77
Int_t GET_SUBMSG(Long_t val)
virtual void Associate(const TGWindow *w)
Definition: TGWidget.h:90
TGIcon * fIcon
Definition: TGMsgBox.h:78
TGButton * fNoAll
Definition: TGMsgBox.h:76
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:925
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:253
TGButton * fNewer
Definition: TGMsgBox.h:77
#define gVirtualX
Definition: TVirtualX.h:362
Int_t GET_MSG(Long_t val)
EKeySym
Definition: KeySymbols.h:27
long Long_t
Definition: RtypesCore.h:50
TGButton * fYesAll
Definition: TGMsgBox.h:76
TGButton * fNo
Definition: TGMsgBox.h:74
TGVerticalFrame * fLabelFrame
Definition: TGMsgBox.h:81
#define ClassImp(name)
Definition: Rtypes.h:279
Bool_t IsZombie() const
Definition: TObject.h:120
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition: TGClient.cxx:287
UInt_t GetWidth() const
Definition: TGPicture.h:73
virtual ~TGMsgBox()
Destroy message dialog box.
Definition: TGMsgBox.cxx:319
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
TGHorizontalFrame * fIconFrame
Definition: TGMsgBox.h:80
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1146
TGHorizontalFrame * fButtonFrame
Definition: TGMsgBox.h:79
TGLayoutHints * fL3
Definition: TGMsgBox.h:82
void SetTextJustify(Int_t tmode)
Set text justification.
Definition: TGLabel.cxx:393
virtual void Add(TObject *obj)
Definition: TList.h:81
TGClient * fClient
Definition: TGObject.h:41
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
void MakeZombie()
Definition: TObject.h:47
void SetWMSize(UInt_t w, UInt_t h)
Give the window manager a window size hint.
Definition: TGFrame.cxx:1849
TGButton * fYes
Definition: TGMsgBox.h:74
virtual void DeleteWindow()
Delete window.
Definition: TGFrame.cxx:258
const Bool_t kTRUE
Definition: Rtypes.h:91
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
TGButton * fRetry
Definition: TGMsgBox.h:75
TGLayoutHints * fL2
Definition: TGMsgBox.h:82
TGButton * fDismiss
Definition: TGMsgBox.h:77
int main(int argc, char **argv)
Definition: TGMsgBox.h:52