ROOT logo
// @(#)root/gui:$Id: TGPasswdDialog.cxx 32556 2010-03-11 15:10:57Z bellenot $
// Author: G. Ganis  10/10/2005

/*************************************************************************
 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGPasswdDialog                                                       //
//                                                                      //
// Graphic dialog to enter passwords                                    //
//                                                                      //
// Usage:                                                               //
//                                                                      //
// {                                                                    //
//   // Buffer for the passwd                                           //
//   char pwdbuf[128]                                                   //
//                                                                      //
//   Open the dialog box                                                //
//   TGPasswdDialog dialog("My prompt", pwdbuf, 128);                   //
//                                                                      //
//   // Wait until the user is done                                     //
//   while (gROOT->IsInterrupted())                                     //
//      gSystem->DispatchOneEvent(kFALSE);                              //
//                                                                      //
//   // Password is now in pwdbuf                                       //
//   ...                                                                //
//                                                                      //
// }                                                                    //
//                                                                      //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TGPasswdDialog.h"

#include "TError.h"
#include "TGFrame.h"
#include "TGButton.h"
#include "TGLabel.h"
#include "TGTextEntry.h"
#include "TGTextBuffer.h"
#include "TGString.h"
#include "TROOT.h"


ClassImp(TGPasswdDialog)

//______________________________________________________________________________
TGPasswdDialog::TGPasswdDialog(const char *prompt, char *pwdbuf, Int_t pwdlenmax,
                               UInt_t w, UInt_t h)
{
   // Create an editor in a dialog.

   fPwdBuf = pwdbuf;
   fPwdLenMax = pwdlenmax;

   const TGWindow *mainw = gClient->GetRoot();
   fDialog = new TGTransientFrame(mainw, mainw, w, h);
   fDialog->Connect("CloseWindow()", "TGPasswdDialog", this, "CloseWindow()");

   // Prompt
   fDialog->AddFrame(new TGLabel(fDialog, prompt),
                     new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 5, 10, 5));

   // Passwd
   fPasswdText = new TGTextBuffer(40);
   fPasswd = new TGTextEntry(fDialog, fPasswdText);
   fPasswd->SetCursorPosition(0);
   fPasswd->Resize(300, fPasswd->GetDefaultHeight());
   fPasswd->SetEchoMode(TGTextEntry::kPassword);
   fPasswd->Connect("ReturnPressed()", "TGPasswdDialog", this, "ReturnPressed()");

   fDialog->AddFrame(fPasswd, new TGLayoutHints(kLHintsCenterY |
                                                kLHintsLeft | kLHintsExpandX,
                                                5, 5, 5, 5));
   // Ok button
   fOk = new TGTextButton(fDialog, "     &Ok     ");
   fOk->Connect("Clicked()", "TGPasswdDialog", this, "ReturnPressed()");
   fDialog->AddFrame(fOk, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
   // set window title and icon name
   fDialog->SetWindowName("Password dialog");
   fDialog->SetIconName("Password dialog");

   fDialog->MapSubwindows();

   Int_t width  = fDialog->GetDefaultWidth();
   Int_t height = fDialog->GetDefaultHeight();

   fDialog->Resize(width, height);

   fPasswd->SetFocus();
   // position relative to the parent window (which is the root window)
   Window_t wdum;
   int      ax, ay;
   Int_t    mw = ((TGFrame *) mainw)->GetWidth();
   Int_t    mh = ((TGFrame *) mainw)->GetHeight();

   gVirtualX->TranslateCoordinates(mainw->GetId(), mainw->GetId(),
                          (mw - width) >> 1, (mh - height) >> 1, ax, ay, wdum);
   fDialog->Move(ax, ay);
   fDialog->SetWMPosition(ax, ay);

   // make the message box non-resizable
   fDialog->SetWMSize(width, height);
   fDialog->SetWMSizeHints(width, height, width, height, 0, 0);

   // Now we wait for the user
   gROOT->SetInterrupt(kTRUE);

   fDialog->MapWindow();
}

//______________________________________________________________________________
TGPasswdDialog::~TGPasswdDialog()
{
   // Delete log window.

   DoClose();
   delete fDialog;
}

//______________________________________________________________________________
void TGPasswdDialog::DoClose()
{
   // Handle close button.

   fDialog->SendCloseMessage();
}

//______________________________________________________________________________
void TGPasswdDialog::CloseWindow()
{
   // Called when closed via window manager action.

   delete this;
}

//______________________________________________________________________________
void TGPasswdDialog::ReturnPressed()
{
   // Handle return

   if (fPwdBuf) {
      Int_t len = strlen(fPasswdText->GetString());
      len = (len < (fPwdLenMax - 1)) ? len : fPwdLenMax - 1;
      memcpy(fPwdBuf, fPasswdText->GetString(), len);
      fPwdBuf[len] = 0;
      fPasswdText->Clear();
   } else
      Error("ReturnPressed", "passwd buffer undefined");

   // We are done
   gROOT->SetInterrupt(kFALSE);

   // Close window
   fDialog->UnmapWindow();
}
 TGPasswdDialog.cxx:1
 TGPasswdDialog.cxx:2
 TGPasswdDialog.cxx:3
 TGPasswdDialog.cxx:4
 TGPasswdDialog.cxx:5
 TGPasswdDialog.cxx:6
 TGPasswdDialog.cxx:7
 TGPasswdDialog.cxx:8
 TGPasswdDialog.cxx:9
 TGPasswdDialog.cxx:10
 TGPasswdDialog.cxx:11
 TGPasswdDialog.cxx:12
 TGPasswdDialog.cxx:13
 TGPasswdDialog.cxx:14
 TGPasswdDialog.cxx:15
 TGPasswdDialog.cxx:16
 TGPasswdDialog.cxx:17
 TGPasswdDialog.cxx:18
 TGPasswdDialog.cxx:19
 TGPasswdDialog.cxx:20
 TGPasswdDialog.cxx:21
 TGPasswdDialog.cxx:22
 TGPasswdDialog.cxx:23
 TGPasswdDialog.cxx:24
 TGPasswdDialog.cxx:25
 TGPasswdDialog.cxx:26
 TGPasswdDialog.cxx:27
 TGPasswdDialog.cxx:28
 TGPasswdDialog.cxx:29
 TGPasswdDialog.cxx:30
 TGPasswdDialog.cxx:31
 TGPasswdDialog.cxx:32
 TGPasswdDialog.cxx:33
 TGPasswdDialog.cxx:34
 TGPasswdDialog.cxx:35
 TGPasswdDialog.cxx:36
 TGPasswdDialog.cxx:37
 TGPasswdDialog.cxx:38
 TGPasswdDialog.cxx:39
 TGPasswdDialog.cxx:40
 TGPasswdDialog.cxx:41
 TGPasswdDialog.cxx:42
 TGPasswdDialog.cxx:43
 TGPasswdDialog.cxx:44
 TGPasswdDialog.cxx:45
 TGPasswdDialog.cxx:46
 TGPasswdDialog.cxx:47
 TGPasswdDialog.cxx:48
 TGPasswdDialog.cxx:49
 TGPasswdDialog.cxx:50
 TGPasswdDialog.cxx:51
 TGPasswdDialog.cxx:52
 TGPasswdDialog.cxx:53
 TGPasswdDialog.cxx:54
 TGPasswdDialog.cxx:55
 TGPasswdDialog.cxx:56
 TGPasswdDialog.cxx:57
 TGPasswdDialog.cxx:58
 TGPasswdDialog.cxx:59
 TGPasswdDialog.cxx:60
 TGPasswdDialog.cxx:61
 TGPasswdDialog.cxx:62
 TGPasswdDialog.cxx:63
 TGPasswdDialog.cxx:64
 TGPasswdDialog.cxx:65
 TGPasswdDialog.cxx:66
 TGPasswdDialog.cxx:67
 TGPasswdDialog.cxx:68
 TGPasswdDialog.cxx:69
 TGPasswdDialog.cxx:70
 TGPasswdDialog.cxx:71
 TGPasswdDialog.cxx:72
 TGPasswdDialog.cxx:73
 TGPasswdDialog.cxx:74
 TGPasswdDialog.cxx:75
 TGPasswdDialog.cxx:76
 TGPasswdDialog.cxx:77
 TGPasswdDialog.cxx:78
 TGPasswdDialog.cxx:79
 TGPasswdDialog.cxx:80
 TGPasswdDialog.cxx:81
 TGPasswdDialog.cxx:82
 TGPasswdDialog.cxx:83
 TGPasswdDialog.cxx:84
 TGPasswdDialog.cxx:85
 TGPasswdDialog.cxx:86
 TGPasswdDialog.cxx:87
 TGPasswdDialog.cxx:88
 TGPasswdDialog.cxx:89
 TGPasswdDialog.cxx:90
 TGPasswdDialog.cxx:91
 TGPasswdDialog.cxx:92
 TGPasswdDialog.cxx:93
 TGPasswdDialog.cxx:94
 TGPasswdDialog.cxx:95
 TGPasswdDialog.cxx:96
 TGPasswdDialog.cxx:97
 TGPasswdDialog.cxx:98
 TGPasswdDialog.cxx:99
 TGPasswdDialog.cxx:100
 TGPasswdDialog.cxx:101
 TGPasswdDialog.cxx:102
 TGPasswdDialog.cxx:103
 TGPasswdDialog.cxx:104
 TGPasswdDialog.cxx:105
 TGPasswdDialog.cxx:106
 TGPasswdDialog.cxx:107
 TGPasswdDialog.cxx:108
 TGPasswdDialog.cxx:109
 TGPasswdDialog.cxx:110
 TGPasswdDialog.cxx:111
 TGPasswdDialog.cxx:112
 TGPasswdDialog.cxx:113
 TGPasswdDialog.cxx:114
 TGPasswdDialog.cxx:115
 TGPasswdDialog.cxx:116
 TGPasswdDialog.cxx:117
 TGPasswdDialog.cxx:118
 TGPasswdDialog.cxx:119
 TGPasswdDialog.cxx:120
 TGPasswdDialog.cxx:121
 TGPasswdDialog.cxx:122
 TGPasswdDialog.cxx:123
 TGPasswdDialog.cxx:124
 TGPasswdDialog.cxx:125
 TGPasswdDialog.cxx:126
 TGPasswdDialog.cxx:127
 TGPasswdDialog.cxx:128
 TGPasswdDialog.cxx:129
 TGPasswdDialog.cxx:130
 TGPasswdDialog.cxx:131
 TGPasswdDialog.cxx:132
 TGPasswdDialog.cxx:133
 TGPasswdDialog.cxx:134
 TGPasswdDialog.cxx:135
 TGPasswdDialog.cxx:136
 TGPasswdDialog.cxx:137
 TGPasswdDialog.cxx:138
 TGPasswdDialog.cxx:139
 TGPasswdDialog.cxx:140
 TGPasswdDialog.cxx:141
 TGPasswdDialog.cxx:142
 TGPasswdDialog.cxx:143
 TGPasswdDialog.cxx:144
 TGPasswdDialog.cxx:145
 TGPasswdDialog.cxx:146
 TGPasswdDialog.cxx:147
 TGPasswdDialog.cxx:148
 TGPasswdDialog.cxx:149
 TGPasswdDialog.cxx:150
 TGPasswdDialog.cxx:151
 TGPasswdDialog.cxx:152
 TGPasswdDialog.cxx:153
 TGPasswdDialog.cxx:154
 TGPasswdDialog.cxx:155
 TGPasswdDialog.cxx:156
 TGPasswdDialog.cxx:157
 TGPasswdDialog.cxx:158
 TGPasswdDialog.cxx:159
 TGPasswdDialog.cxx:160
 TGPasswdDialog.cxx:161
 TGPasswdDialog.cxx:162