ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TGPasswdDialog.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: G. Ganis 10/10/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, 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 // TGPasswdDialog //
15 // //
16 // Graphic dialog to enter passwords //
17 // //
18 // Usage: //
19 // //
20 // { //
21 // // Buffer for the passwd //
22 // char pwdbuf[128] //
23 // //
24 // Open the dialog box //
25 // TGPasswdDialog dialog("My prompt", pwdbuf, 128); //
26 // //
27 // // Wait until the user is done //
28 // while (gROOT->IsInterrupted()) //
29 // gSystem->DispatchOneEvent(kFALSE); //
30 // //
31 // // Password is now in pwdbuf //
32 // ... //
33 // //
34 // } //
35 // //
36 // //
37 //////////////////////////////////////////////////////////////////////////
38 
39 #include "TGPasswdDialog.h"
40 
41 #include "TError.h"
42 #include "TGFrame.h"
43 #include "TGButton.h"
44 #include "TGLabel.h"
45 #include "TGTextEntry.h"
46 #include "TGTextBuffer.h"
47 #include "TGString.h"
48 #include "TROOT.h"
49 
50 
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Create an editor in a dialog.
55 
56 TGPasswdDialog::TGPasswdDialog(const char *prompt, char *pwdbuf, Int_t pwdlenmax,
57  UInt_t w, UInt_t h)
58 {
59  fPwdBuf = pwdbuf;
60  fPwdLenMax = pwdlenmax;
61 
62  const TGWindow *mainw = gClient->GetRoot();
63  fDialog = new TGTransientFrame(mainw, mainw, w, h);
64  fDialog->Connect("CloseWindow()", "TGPasswdDialog", this, "CloseWindow()");
65 
66  // Prompt
67  fDialog->AddFrame(new TGLabel(fDialog, prompt),
68  new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 5, 10, 5));
69 
70  // Passwd
71  fPasswdText = new TGTextBuffer(40);
72  fPasswd = new TGTextEntry(fDialog, fPasswdText);
73  fPasswd->SetCursorPosition(0);
74  fPasswd->Resize(300, fPasswd->GetDefaultHeight());
75  fPasswd->SetEchoMode(TGTextEntry::kPassword);
76  fPasswd->Connect("ReturnPressed()", "TGPasswdDialog", this, "ReturnPressed()");
77 
78  fDialog->AddFrame(fPasswd, new TGLayoutHints(kLHintsCenterY |
80  5, 5, 5, 5));
81  // Ok button
82  fOk = new TGTextButton(fDialog, " &Ok ");
83  fOk->Connect("Clicked()", "TGPasswdDialog", this, "ReturnPressed()");
84  fDialog->AddFrame(fOk, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
85  // set window title and icon name
86  fDialog->SetWindowName("Password dialog");
87  fDialog->SetIconName("Password dialog");
88 
89  fDialog->MapSubwindows();
90 
91  Int_t width = fDialog->GetDefaultWidth();
92  Int_t height = fDialog->GetDefaultHeight();
93 
94  fDialog->Resize(width, height);
95 
96  fPasswd->SetFocus();
97  // position relative to the parent window (which is the root window)
98  Window_t wdum;
99  int ax, ay;
100  Int_t mw = ((TGFrame *) mainw)->GetWidth();
101  Int_t mh = ((TGFrame *) mainw)->GetHeight();
102 
103  gVirtualX->TranslateCoordinates(mainw->GetId(), mainw->GetId(),
104  (mw - width) >> 1, (mh - height) >> 1, ax, ay, wdum);
105  fDialog->Move(ax, ay);
106  fDialog->SetWMPosition(ax, ay);
107 
108  // make the message box non-resizable
109  fDialog->SetWMSize(width, height);
110  fDialog->SetWMSizeHints(width, height, width, height, 0, 0);
111 
112  // Now we wait for the user
113  gROOT->SetInterrupt(kTRUE);
114 
115  fDialog->MapWindow();
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Delete log window.
120 
122 {
123  DoClose();
124  delete fDialog;
125 }
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 /// Handle close button.
129 
131 {
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Called when closed via window manager action.
137 
139 {
140  delete this;
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Handle return
145 
147 {
148  if (fPwdBuf) {
149  Int_t len = strlen(fPasswdText->GetString());
150  len = (len < (fPwdLenMax - 1)) ? len : fPwdLenMax - 1;
151  memcpy(fPwdBuf, fPasswdText->GetString(), len);
152  fPwdBuf[len] = 0;
153  fPasswdText->Clear();
154  } else
155  Error("ReturnPressed", "passwd buffer undefined");
156 
157  // We are done
158  gROOT->SetInterrupt(kFALSE);
159 
160  // Close window
161  fDialog->UnmapWindow();
162 }
TH1 * h
Definition: legend2.C:5
TGTextBuffer * fPasswdText
#define gROOT
Definition: TROOT.h:344
#define gClient
Definition: TGClient.h:174
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
TGTransientFrame * fDialog
void DoClose()
Handle close button.
const char * GetString() const
Definition: TGTextBuffer.h:49
void CloseWindow()
Called when closed via window manager action.
ClassImp(TGPasswdDialog) TGPasswdDialog
Create an editor in a dialog.
void Error(const char *location, const char *msgfmt,...)
virtual void SendCloseMessage()
Send close message to self.
Definition: TGFrame.cxx:1702
unsigned int UInt_t
Definition: RtypesCore.h:42
tuple w
Definition: qtexample.py:51
virtual ~TGPasswdDialog()
Delete log window.
#define gVirtualX
Definition: TVirtualX.h:362
void Clear()
Definition: TGTextBuffer.h:54
void ReturnPressed()
Handle return.
Handle_t Window_t
Definition: GuiTypes.h:30
virtual void UnmapWindow()
Definition: TGFrame.h:269
const Bool_t kTRUE
Definition: Rtypes.h:91
Handle_t GetId() const
Definition: TGObject.h:52