Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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/** \class TGPasswdDialog
14 \ingroup guiwidgets
15
16Graphic dialog to enter passwords
17
18Usage:
19
20```
21{
22 // Buffer for the passwd
23 char pwdbuf[128]
24
25 Open the dialog box
26 TGPasswdDialog dialog("My prompt", pwdbuf, 128);
27
28 // Wait until the user is done
29 while (gROOT->IsInterrupted())
30 gSystem->DispatchOneEvent(kFALSE);
31
32 // Password is now in pwdbuf
33 ...
34
35}
36```
37
38*/
39
40
41#include "TGPasswdDialog.h"
42
43#include "TError.h"
44#include "TGFrame.h"
45#include "TGButton.h"
46#include "TGLabel.h"
47#include "TGTextEntry.h"
48#include "TGTextBuffer.h"
49#include "TGString.h"
50#include "TROOT.h"
51#include "TVirtualX.h"
52
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Create an editor in a dialog.
57
58TGPasswdDialog::TGPasswdDialog(const char *prompt, char *pwdbuf, Int_t pwdlenmax,
59 UInt_t w, UInt_t h)
60{
61 fPwdBuf = pwdbuf;
62 fPwdLenMax = pwdlenmax;
63
64 const TGWindow *mainw = gClient->GetRoot();
65 fDialog = new TGTransientFrame(mainw, mainw, w, h);
66 fDialog->Connect("CloseWindow()", "TGPasswdDialog", this, "CloseWindow()");
67
68 // Prompt
69 fDialog->AddFrame(new TGLabel(fDialog, prompt),
70 new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 5, 10, 5));
71
72 // Passwd
73 fPasswdText = new TGTextBuffer(40);
75 fPasswd->SetCursorPosition(0);
76 fPasswd->Resize(300, fPasswd->GetDefaultHeight());
77 fPasswd->SetEchoMode(TGTextEntry::kPassword);
78 fPasswd->Connect("ReturnPressed()", "TGPasswdDialog", this, "ReturnPressed()");
79
82 5, 5, 5, 5));
83 // Ok button
84 fOk = new TGTextButton(fDialog, " &Ok ");
85 fOk->Connect("Clicked()", "TGPasswdDialog", this, "ReturnPressed()");
86 fDialog->AddFrame(fOk, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
87 // set window title and icon name
88 fDialog->SetWindowName("Password dialog");
89 fDialog->SetIconName("Password dialog");
90
91 fDialog->MapSubwindows();
92
93 Int_t width = fDialog->GetDefaultWidth();
94 Int_t height = fDialog->GetDefaultHeight();
95
96 fDialog->Resize(width, height);
97
98 fPasswd->SetFocus();
99 // position relative to the parent window (which is the root window)
100 Window_t wdum;
101 int ax, ay;
102 Int_t mw = ((TGFrame *) mainw)->GetWidth();
103 Int_t mh = ((TGFrame *) mainw)->GetHeight();
104
105 gVirtualX->TranslateCoordinates(mainw->GetId(), mainw->GetId(),
106 (mw - width) >> 1, (mh - height) >> 1, ax, ay, wdum);
107 fDialog->Move(ax, ay);
108 fDialog->SetWMPosition(ax, ay);
109
110 // make the message box non-resizable
111 fDialog->SetWMSize(width, height);
112 fDialog->SetWMSizeHints(width, height, width, height, 0, 0);
113
114 // Now we wait for the user
115 gROOT->SetInterrupt(kTRUE);
116
117 fDialog->MapWindow();
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Delete log window.
122
124{
125 DoClose();
126 delete fDialog;
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Handle close button.
131
133{
134 fDialog->SendCloseMessage();
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Called when closed via window manager action.
139
141{
142 delete this;
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// Handle return
147
149{
150 if (fPwdBuf) {
151 Int_t len = strlen(fPasswdText->GetString());
152 len = (len < (fPwdLenMax - 1)) ? len : fPwdLenMax - 1;
153 memcpy(fPwdBuf, fPasswdText->GetString(), len);
154 fPwdBuf[len] = 0;
155 fPasswdText->Clear();
156 } else
157 Error("ReturnPressed", "passwd buffer undefined");
158
159 // We are done
160 gROOT->SetInterrupt(kFALSE);
161
162 // Close window
163 fDialog->UnmapWindow();
164}
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
#define gClient
Definition TGClient.h:157
@ kLHintsLeft
Definition TGLayout.h:24
@ kLHintsCenterY
Definition TGLayout.h:28
@ kLHintsCenterX
Definition TGLayout.h:25
@ kLHintsBottom
Definition TGLayout.h:29
@ kLHintsExpandX
Definition TGLayout.h:30
#define gROOT
Definition TROOT.h:417
#define gVirtualX
Definition TVirtualX.h:375
A subclasses of TGWindow, and is used as base class for some simple widgets (buttons,...
Definition TGFrame.h:80
This class handles GUI labels.
Definition TGLabel.h:24
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
Handle_t GetId() const
Definition TGObject.h:41
TGTextButton * fOk
Ok button.
void ReturnPressed()
Handle return.
TGTransientFrame * fDialog
main frame of this widget
char * fPwdBuf
buffer where to store the passwd
void DoClose()
Handle close button.
TGPasswdDialog(const char *prompt, char *pwdbuf, Int_t pwdlenmax, UInt_t w=400, UInt_t h=400)
Create an editor in a dialog.
TGTextEntry * fPasswd
Password TextEntry.
TGTextBuffer * fPasswdText
Passwd Buffer.
virtual ~TGPasswdDialog()
Delete log window.
void CloseWindow()
Called when closed via window manager action.
Int_t fPwdLenMax
passwd buffer length
A text buffer is used in several widgets, like TGTextEntry, TGFileDialog, etc.
Yield an action as soon as it is clicked.
Definition TGButton.h:142
A TGTextEntry is a one line text input widget.
Definition TGTextEntry.h:24
Defines transient windows that typically are used for dialogs windows.
Definition TGFrame.h:500
ROOT GUI Window base class.
Definition TGWindow.h:23