TGInputDialog.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGInputDialog.cxx 26223 2008-11-17 11:00:01Z brun $
00002 // Author: David Gonzalez Maline  19/07/2006
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, 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 // Input Dialog Widget                                                   //
00015 //                                                                       //
00016 // An Input dialog box                                                   //
00017 //                                                                      //
00018 //////////////////////////////////////////////////////////////////////////
00019 
00020 #include "TGInputDialog.h"
00021 #include "TGButton.h"
00022 #include "TGLabel.h"
00023 #include "TGTextEntry.h"
00024 
00025 ClassImp(TGInputDialog)
00026 
00027 
00028 //______________________________________________________________________________
00029 TGInputDialog::TGInputDialog(const TGWindow *p, const TGWindow *main, 
00030                              const char *prompt, const char *defval, 
00031                              char *retstr, UInt_t options) :
00032       TGTransientFrame(p, main, 10, 10, options)
00033 {
00034    /** Create simple input dialog.  
00035 
00036    It is important to know that the case where the constructor in
00037    which all the variables are initialized to their default values is
00038    only used for the TBrowser to inspect on the classes. For normal
00039    use the only variable that should be free is options.
00040 
00041    Variables prompt, defval are the content of the input dialog while
00042    retstr has to be initialized to a char[256]. In case these are not
00043    initialized, they will show default values while retstr will be
00044    automatically allocated by the dialog. However this will make
00045    impossible to retrieve the value entered by the dialog.
00046 
00047    To see TGInputDialog in use see:
00048    $ROOTSYS/tutorials/testInputDialog.cxx
00049    */
00050 
00051    if (!p && !main) {
00052       MakeZombie();
00053       return;
00054    }
00055    SetCleanup(kDeepCleanup);
00056    // create prompt label and textentry widget
00057    fLabel = new TGLabel(this, prompt?prompt:"Introduce value:");
00058 
00059    TGTextBuffer *tbuf = new TGTextBuffer(256);  //will be deleted by TGtextEntry
00060    tbuf->AddText(0, defval?defval:"");
00061 
00062    fTE = new TGTextEntry(this, tbuf);
00063    fTE->Resize(260, fTE->GetDefaultHeight());
00064 
00065    AddFrame(fLabel, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00066    AddFrame(fTE, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5));
00067 
00068    // create frame and layout hints for Ok and Cancel buttons
00069    TGHorizontalFrame *hf = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
00070    hf->SetCleanup(kDeepCleanup);
00071 
00072    // create OK and Cancel buttons in their own frame (hf)
00073    UInt_t  width = 0, height = 0;
00074 
00075    fOk = new TGTextButton(hf, "&Ok", 1);
00076    fOk->Associate(this);
00077    hf->AddFrame(fOk, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0));
00078    height = fOk->GetDefaultHeight();
00079    width  = TMath::Max(width, fOk->GetDefaultWidth());
00080 
00081    fCancel = new TGTextButton(hf, "&Cancel", 2);
00082    fCancel->Associate(this);
00083    hf->AddFrame(fCancel, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0));
00084    height = fCancel->GetDefaultHeight();
00085    width  = TMath::Max(width, fCancel->GetDefaultWidth());
00086 
00087    // place button frame (hf) at the bottom
00088    AddFrame(hf, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
00089 
00090    // keep buttons centered and with the same width
00091    hf->Resize((width + 20) * 2, height);
00092 
00093    // set dialog title
00094    SetWindowName("Get Input");
00095 
00096    // map all widgets and calculate size of dialog
00097    MapSubwindows();
00098 
00099    width  = GetDefaultWidth();
00100    height = GetDefaultHeight();
00101 
00102    Resize(width, height);
00103 
00104    // position relative to the parent's window
00105    CenterOnParent();
00106 
00107    // make the message box non-resizable
00108    SetWMSize(width, height);
00109    SetWMSizeHints(width, height, width, height, 0, 0);
00110 
00111    SetMWMHints(kMWMDecorAll | kMWMDecorResizeH  | kMWMDecorMaximize |
00112                                        kMWMDecorMinimize | kMWMDecorMenu,
00113                         kMWMFuncAll  | kMWMFuncResize    | kMWMFuncMaximize |
00114                                        kMWMFuncMinimize,
00115                         kMWMInputModeless);
00116 
00117    // popup dialog and wait till user replies
00118    MapWindow();
00119    fTE->SetFocus();
00120 
00121    if (retstr == 0)
00122       retstr = new char[256];
00123 
00124    fRetStr = retstr;
00125 
00126    gClient->WaitFor(this);
00127 }
00128 
00129 //______________________________________________________________________________
00130 TGInputDialog::~TGInputDialog()
00131 {
00132    // Cleanup dialog.
00133 
00134    Cleanup();
00135 }
00136 
00137 //______________________________________________________________________________
00138 Bool_t TGInputDialog::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
00139 {
00140    // Handle button and text enter events
00141 
00142    switch (GET_MSG(msg)) {
00143       case kC_COMMAND:
00144          switch (GET_SUBMSG(msg)) {
00145             case kCM_BUTTON:
00146                switch (parm1) {
00147                   case 1:
00148                      // here copy the string from text buffer to return variable
00149                      strcpy(fRetStr, fTE->GetBuffer()->GetString());
00150                      delete this;
00151                      break;
00152                   case 2:
00153                      fRetStr[0] = 0;
00154                      delete this;
00155                      break;
00156                }
00157                default:
00158                   break;
00159          }
00160          break;
00161 
00162       case kC_TEXTENTRY:
00163          switch (GET_SUBMSG(msg)) {
00164             case kTE_ENTER:
00165                // here copy the string from text buffer to return variable
00166                strcpy(fRetStr, fTE->GetBuffer()->GetString());
00167                delete this;
00168                break;
00169             default:
00170                break;
00171          }
00172          break;
00173 
00174       default:
00175          break;
00176    }
00177    return kTRUE;
00178 }

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