Logo ROOT  
Reference Guide
TGInputDialog.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: David Gonzalez Maline 19/07/2006
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 // //
14 // Input Dialog Widget //
15 // //
16 // An Input dialog box //
17 // //
18 //////////////////////////////////////////////////////////////////////////
19 
20 #include "TGInputDialog.h"
21 #include "TGButton.h"
22 #include "TGLabel.h"
23 #include "TGTextEntry.h"
24 
26 
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 
31  const char *prompt, const char *defval,
32  char *retstr, UInt_t options) :
33  TGTransientFrame(p, main, 10, 10, options)
34 {
35  /** Create simple input dialog.
36 
37  It is important to know that the case where the constructor in
38  which all the variables are initialized to their default values is
39  only used for the TBrowser to inspect on the classes. For normal
40  use the only variable that should be free is options.
41 
42  Variables prompt, defval are the content of the input dialog while
43  retstr has to be initialized to a char[256]. In case these are not
44  initialized, they will show default values while retstr will be
45  automatically allocated by the dialog. However this will make
46  impossible to retrieve the value entered by the dialog.
47 
48  To see TGInputDialog in use see:
49  $ROOTSYS/tutorials/testInputDialog.cxx
50  */
51 
52  if (!p && !main) {
53  MakeZombie();
54  // coverity [uninit_ctor]
55  return;
56  }
58  // create prompt label and textentry widget
59  fLabel = new TGLabel(this, prompt?prompt:"Introduce value:");
60 
61  TGTextBuffer *tbuf = new TGTextBuffer(256); //will be deleted by TGtextEntry
62  tbuf->AddText(0, defval?defval:"");
63 
64  fTE = new TGTextEntry(this, tbuf);
65  fTE->Resize(260, fTE->GetDefaultHeight());
66 
67  AddFrame(fLabel, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
68  AddFrame(fTE, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5));
69 
70  // create frame and layout hints for Ok and Cancel buttons
71  TGHorizontalFrame *hf = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
73 
74  // create OK and Cancel buttons in their own frame (hf)
75  UInt_t width = 0, height = 0;
76 
77  fOk = new TGTextButton(hf, "&Ok", 1);
78  fOk->Associate(this);
79  hf->AddFrame(fOk, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0));
80  height = fOk->GetDefaultHeight();
82 
83  fCancel = new TGTextButton(hf, "&Cancel", 2);
84  fCancel->Associate(this);
86  height = fCancel->GetDefaultHeight();
88 
89  // place button frame (hf) at the bottom
90  AddFrame(hf, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
91 
92  // keep buttons centered and with the same width
93  hf->Resize((width + 20) * 2, height);
94 
95  // set dialog title
96  SetWindowName("Get Input");
97 
98  // map all widgets and calculate size of dialog
99  MapSubwindows();
100 
102  height = GetDefaultHeight();
103 
104  Resize(width, height);
105 
106  // position relative to the parent's window
107  CenterOnParent();
108 
109  // make the message box non-resizable
110  SetWMSize(width, height);
111  SetWMSizeHints(width, height, width, height, 0, 0);
112 
117 
118  // popup dialog and wait till user replies
119  MapWindow();
120  fTE->SetFocus();
121 
122  if (!retstr)
123  retstr = fOwnBuf = new char[256];
124 
125  fRetStr = retstr;
126 
127  gClient->WaitFor(this);
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Cleanup dialog.
132 
134 {
135  Cleanup();
136  delete [] fOwnBuf;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Handle button and text enter events
141 
143 {
144  switch (GET_MSG(msg)) {
145  case kC_COMMAND:
146  switch (GET_SUBMSG(msg)) {
147  case kCM_BUTTON:
148  switch (parm1) {
149  case 1:
150  // here copy the string from text buffer to return variable
151  // coverity[secure_coding]
152  strcpy(fRetStr, fTE->GetBuffer()->GetString());
153  // if user selected an empty string, set the second
154  // char to 1,in order to distinguish between empty string
155  // selected with OK and Cancel button pressed
156  if (!strcmp(fRetStr, ""))
157  fRetStr[1] = 1;
158  delete this;
159  break;
160  case 2:
161  // hack to detect the case where the user pressed the
162  // Cancel button
163  fRetStr[0] = 0;
164  fRetStr[1] = 0;
165  delete this;
166  break;
167  }
168  default:
169  break;
170  }
171  break;
172 
173  case kC_TEXTENTRY:
174  switch (GET_SUBMSG(msg)) {
175  case kTE_ENTER:
176  // here copy the string from text buffer to return variable
177  // coverity[secure_coding]
178  strcpy(fRetStr, fTE->GetBuffer()->GetString());
179  // if user selected an empty string, set the second
180  // char to 1,in order to distinguish between empty string
181  // selected with OK and Cancel button pressed
182  if (!strcmp(fRetStr, ""))
183  fRetStr[1] = 1;
184  delete this;
185  break;
186  default:
187  break;
188  }
189  break;
190 
191  default:
192  break;
193  }
194  return kTRUE;
195 }
TGTransientFrame
Definition: TGFrame.h:553
GET_MSG
Int_t GET_MSG(Long_t val)
Definition: WidgetMessageTypes.h:112
TGWindow
Definition: TGWindow.h:31
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:32
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
kFixedWidth
@ kFixedWidth
Definition: GuiTypes.h:387
TGInputDialog.h
TMath::Max
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
kLHintsTop
@ kLHintsTop
Definition: TGLayout.h:34
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
kLHintsLeft
@ kLHintsLeft
Definition: TGLayout.h:31
TGInputDialog::fCancel
TGTextButton * fCancel
Definition: TGInputDialog.h:35
TGLabel
Definition: TGLabel.h:32
TGCompositeFrame::Cleanup
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:952
TGInputDialog::TGInputDialog
TGInputDialog(const TGInputDialog &)=delete
TGFrame::GetDefaultHeight
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:216
TGMainFrame::SetWMSizeHints
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
width
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
TGTextButton
Definition: TGButton.h:142
TGMainFrame::SetWindowName
void SetWindowName(const char *name=0)
Set window name. This is typically done via the window manager.
Definition: TGFrame.cxx:1749
TGHorizontalFrame
Definition: TGFrame.h:423
TGTextEntry::GetBuffer
TGTextBuffer * GetBuffer() const
Definition: TGTextEntry.h:127
TGInputDialog::fRetStr
char * fRetStr
Definition: TGInputDialog.h:36
kMWMFuncMinimize
@ kMWMFuncMinimize
Definition: TGFrame.h:61
TGFrame::MapWindow
virtual void MapWindow()
map window
Definition: TGFrame.h:229
bool
TGTextBuffer
Definition: TGTextBuffer.h:30
kMWMDecorMenu
@ kMWMDecorMenu
Definition: TGFrame.h:76
kMWMInputModeless
@ kMWMInputModeless
Definition: TGFrame.h:66
kMWMDecorResizeH
@ kMWMDecorResizeH
Definition: TGFrame.h:74
gClient
#define gClient
Definition: TGClient.h:166
kLHintsBottom
@ kLHintsBottom
Definition: TGLayout.h:36
kLHintsCenterY
@ kLHintsCenterY
Definition: TGLayout.h:35
TGCompositeFrame::MapSubwindows
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1149
TGWidget::Associate
virtual void Associate(const TGWindow *w)
Definition: TGWidget.h:84
TGFrame::Resize
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:590
TGTextEntry
Definition: TGTextEntry.h:39
TGTextEntry::SetFocus
virtual void SetFocus()
Set focus to this text entry.
Definition: TGTextEntry.cxx:1677
kCM_BUTTON
@ kCM_BUTTON
Definition: WidgetMessageTypes.h:36
Long_t
long Long_t
Definition: RtypesCore.h:54
main
int main(int argc, char **argv)
Definition: histspeedtest.cxx:751
TGLayoutHints
Definition: TGLayout.h:57
kTE_ENTER
@ kTE_ENTER
Definition: WidgetMessageTypes.h:52
TObject::MakeZombie
void MakeZombie()
Definition: TObject.h:49
TGInputDialog::~TGInputDialog
~TGInputDialog()
Cleanup dialog.
Definition: TGInputDialog.cxx:133
unsigned int
TGCompositeFrame::GetDefaultHeight
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:351
TGLabel.h
TGInputDialog::ProcessMessage
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t)
Handle button and text enter events.
Definition: TGInputDialog.cxx:142
TGMainFrame::SetWMSize
void SetWMSize(UInt_t w, UInt_t h)
Give the window manager a window size hint.
Definition: TGFrame.cxx:1852
kC_TEXTENTRY
@ kC_TEXTENTRY
Definition: WidgetMessageTypes.h:50
TGTransientFrame::CenterOnParent
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition: TGFrame.cxx:1916
kC_COMMAND
@ kC_COMMAND
Definition: WidgetMessageTypes.h:33
TGMainFrame::SetMWMHints
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
kMWMFuncAll
@ kMWMFuncAll
Definition: TGFrame.h:58
TGTextEntry.h
TGButton.h
kMWMDecorAll
@ kMWMDecorAll
Definition: TGFrame.h:72
GET_SUBMSG
Int_t GET_SUBMSG(Long_t val)
Definition: WidgetMessageTypes.h:113
kDeepCleanup
@ kDeepCleanup
Definition: TGFrame.h:51
kMWMDecorMaximize
@ kMWMDecorMaximize
Definition: TGFrame.h:78
TGInputDialog
Definition: TGInputDialog.h:29
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:37
TGInputDialog::fOk
TGTextButton * fOk
Definition: TGInputDialog.h:34
TGCompositeFrame::SetCleanup
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1057
kMWMFuncResize
@ kMWMFuncResize
Definition: TGFrame.h:59
TGCompositeFrame::GetDefaultWidth
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:349
kMWMFuncMaximize
@ kMWMFuncMaximize
Definition: TGFrame.h:62
TGInputDialog::fTE
TGTextEntry * fTE
Definition: TGInputDialog.h:33
TGTextBuffer::AddText
void AddText(Int_t pos, const char *text)
Definition: TGTextBuffer.h:49
kMWMDecorMinimize
@ kMWMDecorMinimize
Definition: TGFrame.h:77
TGCompositeFrame::AddFrame
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1102
TGFrame::GetDefaultWidth
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:215
TGTextBuffer::GetString
const char * GetString() const
Definition: TGTextBuffer.h:47
TGInputDialog::fOwnBuf
char * fOwnBuf
Definition: TGInputDialog.h:37
TGInputDialog::fLabel
TGLabel * fLabel
Definition: TGInputDialog.h:32