TGPasswdDialog.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGPasswdDialog.cxx 32556 2010-03-11 15:10:57Z bellenot $
00002 // Author: G. Ganis  10/10/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TGPasswdDialog                                                       //
00015 //                                                                      //
00016 // Graphic dialog to enter passwords                                    //
00017 //                                                                      //
00018 // Usage:                                                               //
00019 //                                                                      //
00020 // {                                                                    //
00021 //   // Buffer for the passwd                                           //
00022 //   char pwdbuf[128]                                                   //
00023 //                                                                      //
00024 //   Open the dialog box                                                //
00025 //   TGPasswdDialog dialog("My prompt", pwdbuf, 128);                   //
00026 //                                                                      //
00027 //   // Wait until the user is done                                     //
00028 //   while (gROOT->IsInterrupted())                                     //
00029 //      gSystem->DispatchOneEvent(kFALSE);                              //
00030 //                                                                      //
00031 //   // Password is now in pwdbuf                                       //
00032 //   ...                                                                //
00033 //                                                                      //
00034 // }                                                                    //
00035 //                                                                      //
00036 //                                                                      //
00037 //////////////////////////////////////////////////////////////////////////
00038 
00039 #include "TGPasswdDialog.h"
00040 
00041 #include "TError.h"
00042 #include "TGFrame.h"
00043 #include "TGButton.h"
00044 #include "TGLabel.h"
00045 #include "TGTextEntry.h"
00046 #include "TGTextBuffer.h"
00047 #include "TGString.h"
00048 #include "TROOT.h"
00049 
00050 
00051 ClassImp(TGPasswdDialog)
00052 
00053 //______________________________________________________________________________
00054 TGPasswdDialog::TGPasswdDialog(const char *prompt, char *pwdbuf, Int_t pwdlenmax,
00055                                UInt_t w, UInt_t h)
00056 {
00057    // Create an editor in a dialog.
00058 
00059    fPwdBuf = pwdbuf;
00060    fPwdLenMax = pwdlenmax;
00061 
00062    const TGWindow *mainw = gClient->GetRoot();
00063    fDialog = new TGTransientFrame(mainw, mainw, w, h);
00064    fDialog->Connect("CloseWindow()", "TGPasswdDialog", this, "CloseWindow()");
00065 
00066    // Prompt
00067    fDialog->AddFrame(new TGLabel(fDialog, prompt),
00068                      new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 5, 10, 5));
00069 
00070    // Passwd
00071    fPasswdText = new TGTextBuffer(40);
00072    fPasswd = new TGTextEntry(fDialog, fPasswdText);
00073    fPasswd->SetCursorPosition(0);
00074    fPasswd->Resize(300, fPasswd->GetDefaultHeight());
00075    fPasswd->SetEchoMode(TGTextEntry::kPassword);
00076    fPasswd->Connect("ReturnPressed()", "TGPasswdDialog", this, "ReturnPressed()");
00077 
00078    fDialog->AddFrame(fPasswd, new TGLayoutHints(kLHintsCenterY |
00079                                                 kLHintsLeft | kLHintsExpandX,
00080                                                 5, 5, 5, 5));
00081    // Ok button
00082    fOk = new TGTextButton(fDialog, "     &Ok     ");
00083    fOk->Connect("Clicked()", "TGPasswdDialog", this, "ReturnPressed()");
00084    fDialog->AddFrame(fOk, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
00085    // set window title and icon name
00086    fDialog->SetWindowName("Password dialog");
00087    fDialog->SetIconName("Password dialog");
00088 
00089    fDialog->MapSubwindows();
00090 
00091    Int_t width  = fDialog->GetDefaultWidth();
00092    Int_t height = fDialog->GetDefaultHeight();
00093 
00094    fDialog->Resize(width, height);
00095 
00096    fPasswd->SetFocus();
00097    // position relative to the parent window (which is the root window)
00098    Window_t wdum;
00099    int      ax, ay;
00100    Int_t    mw = ((TGFrame *) mainw)->GetWidth();
00101    Int_t    mh = ((TGFrame *) mainw)->GetHeight();
00102 
00103    gVirtualX->TranslateCoordinates(mainw->GetId(), mainw->GetId(),
00104                           (mw - width) >> 1, (mh - height) >> 1, ax, ay, wdum);
00105    fDialog->Move(ax, ay);
00106    fDialog->SetWMPosition(ax, ay);
00107 
00108    // make the message box non-resizable
00109    fDialog->SetWMSize(width, height);
00110    fDialog->SetWMSizeHints(width, height, width, height, 0, 0);
00111 
00112    // Now we wait for the user
00113    gROOT->SetInterrupt(kTRUE);
00114 
00115    fDialog->MapWindow();
00116 }
00117 
00118 //______________________________________________________________________________
00119 TGPasswdDialog::~TGPasswdDialog()
00120 {
00121    // Delete log window.
00122 
00123    DoClose();
00124    delete fDialog;
00125 }
00126 
00127 //______________________________________________________________________________
00128 void TGPasswdDialog::DoClose()
00129 {
00130    // Handle close button.
00131 
00132    fDialog->SendCloseMessage();
00133 }
00134 
00135 //______________________________________________________________________________
00136 void TGPasswdDialog::CloseWindow()
00137 {
00138    // Called when closed via window manager action.
00139 
00140    delete this;
00141 }
00142 
00143 //______________________________________________________________________________
00144 void TGPasswdDialog::ReturnPressed()
00145 {
00146    // Handle return
00147 
00148    if (fPwdBuf) {
00149       Int_t len = strlen(fPasswdText->GetString());
00150       len = (len < (fPwdLenMax - 1)) ? len : fPwdLenMax - 1;
00151       memcpy(fPwdBuf, fPasswdText->GetString(), len);
00152       fPwdBuf[len] = 0;
00153       fPasswdText->Clear();
00154    } else
00155       Error("ReturnPressed", "passwd buffer undefined");
00156 
00157    // We are done
00158    gROOT->SetInterrupt(kFALSE);
00159 
00160    // Close window
00161    fDialog->UnmapWindow();
00162 }

Generated on Tue Jul 5 14:22:02 2011 for ROOT_528-00b_version by  doxygen 1.5.1