Dialogs.C

Go to the documentation of this file.
00001 //
00002 // This file contains the class InputDialog.
00003 // An InputDialog object prompts for an input string using a simple
00004 // dialog box. The InputDialog class is also a good example of how
00005 // to use the ROOT GUI classes via the interpreter. Since interpreted
00006 // classes can not call virtual functions via base class pointers, all
00007 // GUI objects are used by composition instead of by inheritance.
00008 //
00009 // This file contains also some utility functions that use
00010 // the InputDialog class to either get a string, integer or
00011 // floating point number. There are also two functions showing
00012 // how to use the file open and save dialogs. The utility functions are:
00013 //
00014 // const char *OpenFileDialog()
00015 // const char *SaveFileDialog()
00016 // const char *GetStringDialog(const char *prompt, const char *defval)
00017 // Int_t GetIntegerDialog(const char *prompt, Int_t defval)
00018 // Float_t GetFloatDialog(const char *prompt, Float_t defval)
00019 //
00020 // To use the InputDialog class and the utility functions you just
00021 // have to load the Dialogs.C file as follows:
00022 // .L Dialogs.C
00023 //
00024 // Now you can use them like:
00025 // {
00026 //    const char *file = OpenFileDialog();
00027 //    Int_t run   = GetIntegerDialog("Give run number:", 0);
00028 //    Int_t event = GetIntegerDialog("Give event number:", 0);
00029 //    printf("analyse run %d, event %d from file %s\n", run ,event, file);
00030 // }
00031 //
00032 
00033 ///////////////////////////////////////////////////////////////////////////
00034 //                                                                       //
00035 // Input Dialog Widget                                                   //
00036 //                                                                       //
00037 ///////////////////////////////////////////////////////////////////////////
00038 
00039 class InputDialog {
00040 
00041 private:
00042    TGTransientFrame *fDialog;  // transient frame, main dialog window
00043    TGTextEntry      *fTE;      // text entry widget containing
00044    TList            *fWidgets; // keep track of widgets to be deleted in dtor
00045    char             *fRetStr;  // address to store return string
00046 
00047 public:
00048    InputDialog(const char *prompt, const char *defval, char *retstr);
00049    ~InputDialog();
00050    void ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2);
00051 };
00052 
00053 InputDialog::~InputDialog()
00054 {
00055    // Cleanup dialog.
00056 
00057    fWidgets->Delete();
00058    delete fWidgets;
00059 
00060    delete fTE;
00061    delete fDialog;
00062 }
00063 
00064 InputDialog::InputDialog(const char *prompt, const char *defval, char *retstr)
00065 {
00066    // Create simple input dialog.
00067 
00068    fWidgets = new TList;
00069 
00070    TGWindow *main = gClient->GetRoot();
00071    fDialog = new TGTransientFrame(main, main, 10, 10);
00072 
00073    // command to be executed by buttons and text entry widget
00074    char cmd[128];
00075    sprintf(cmd, "{long r__ptr=0x%lx; ((InputDialog*)r__ptr)->ProcessMessage($MSG,$PARM1,$PARM2);}", (Long_t)this);
00076 
00077    // create prompt label and textentry widget
00078    TGLabel *label = new TGLabel(fDialog, prompt);
00079    fWidgets->Add(label);
00080 
00081    TGTextBuffer *tbuf = new TGTextBuffer(256);  //will be deleted by TGtextEntry
00082    tbuf->AddText(0, defval);
00083 
00084    fTE = new TGTextEntry(fDialog, tbuf);
00085    fTE->Resize(260, fTE->GetDefaultHeight());
00086    fTE->SetCommand(cmd);
00087 
00088    TGLayoutHints *l1 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0);
00089    TGLayoutHints *l2 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5);
00090    fWidgets->Add(l1);
00091    fWidgets->Add(l2);
00092 
00093    fDialog->AddFrame(label, l1);
00094    fDialog->AddFrame(fTE, l2);
00095 
00096    // create frame and layout hints for Ok and Cancel buttons
00097    TGHorizontalFrame *hf = new TGHorizontalFrame(fDialog, 60, 20, kFixedWidth);
00098    TGLayoutHints     *l3 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0);
00099 
00100    // put hf as last in list to be deleted
00101    fWidgets->Add(l3);
00102 
00103    // create OK and Cancel buttons in their own frame (hf)
00104    UInt_t  nb = 0, width = 0, height = 0;
00105    TGTextButton *b;
00106 
00107    b = new TGTextButton(hf, "&Ok", cmd, 1);
00108    fWidgets->Add(b);
00109    b->Associate(fDialog);
00110    hf->AddFrame(b, l3);
00111    height = b->GetDefaultHeight();
00112    width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;
00113 
00114    b = new TGTextButton(hf, "&Cancel", cmd, 2);
00115    fWidgets->Add(b);
00116    b->Associate(fDialog);
00117    hf->AddFrame(b, l3);
00118    height = b->GetDefaultHeight();
00119    width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;
00120 
00121    // place button frame (hf) at the bottom
00122    TGLayoutHints *l4 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
00123    fWidgets->Add(l4);
00124    fWidgets->Add(hf);
00125 
00126    fDialog->AddFrame(hf, l4);
00127 
00128    // keep buttons centered and with the same width
00129    hf->Resize((width + 20) * nb, height);
00130 
00131    // set dialog title
00132    fDialog->SetWindowName("Get Input");
00133 
00134    // map all widgets and calculate size of dialog
00135    fDialog->MapSubwindows();
00136 
00137    width  = fDialog->GetDefaultWidth();
00138    height = fDialog->GetDefaultHeight();
00139 
00140    fDialog->Resize(width, height);
00141 
00142    // position relative to the parent window (which is the root window)
00143    Window_t wdum;
00144    int      ax, ay;
00145 
00146    gVirtualX->TranslateCoordinates(main->GetId(), main->GetId(),
00147                           (((TGFrame *) main)->GetWidth() - width) >> 1,
00148                           (((TGFrame *) main)->GetHeight() - height) >> 1,
00149                           ax, ay, wdum);
00150    fDialog->Move(ax, ay);
00151    fDialog->SetWMPosition(ax, ay);
00152 
00153    // make the message box non-resizable
00154    fDialog->SetWMSize(width, height);
00155    fDialog->SetWMSizeHints(width, height, width, height, 0, 0);
00156 
00157    fDialog->SetMWMHints(kMWMDecorAll | kMWMDecorResizeH  | kMWMDecorMaximize |
00158                                        kMWMDecorMinimize | kMWMDecorMenu,
00159                         kMWMFuncAll  | kMWMFuncResize    | kMWMFuncMaximize |
00160                                        kMWMFuncMinimize,
00161                         kMWMInputModeless);
00162 
00163    // popup dialog and wait till user replies
00164    fDialog->MapWindow();
00165 
00166    fRetStr = retstr;
00167 
00168    gClient->WaitFor(fDialog);
00169 }
00170 
00171 void InputDialog::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
00172 {
00173    // Handle button and text enter events
00174 
00175    switch (GET_MSG(msg)) {
00176       case kC_COMMAND:
00177          switch (GET_SUBMSG(msg)) {
00178              case kCM_BUTTON:
00179                 switch (parm1) {
00180                    case 1:
00181                       // here copy the string from text buffer to return variable
00182                       strcpy(fRetStr, fTE->GetBuffer()->GetString());
00183                       delete this;
00184                       break;
00185 
00186                    case 2:
00187                       fRetStr[0] = 0;
00188                       delete this;
00189                       break;
00190                  }
00191               default:
00192                  break;
00193           }
00194           break;
00195 
00196        case kC_TEXTENTRY:
00197          switch (GET_SUBMSG(msg)) {
00198              case kTE_ENTER:
00199                 // here copy the string from text buffer to return variable
00200                 strcpy(fRetStr, fTE->GetBuffer()->GetString());
00201                 delete this;
00202                 break;
00203              default:
00204                 break;
00205           }
00206           break;
00207 
00208        default:
00209           break;
00210    }
00211 }
00212 
00213 
00214 //--- Utility Functions --------------------------------------------------------
00215 
00216 const char *OpenFileDialog()
00217 {
00218    // Prompt for file to be opened. Depending on navigation in
00219    // dialog the current working directory can be changed.
00220    // The returned file name is always with respect to the
00221    // current directory.
00222 
00223    const char *gOpenAsTypes[] = {
00224       "Macro files",  "*.C",
00225       "ROOT files",   "*.root",
00226       "PostScript",   "*.ps",
00227       "Encapsulated PostScript", "*.eps",
00228       "Gif files",    "*.gif",
00229       "All files",    "*",
00230        0,              0
00231    };
00232 
00233    static TGFileInfo fi;
00234    fi.fFileTypes = gOpenAsTypes;
00235    new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDOpen, &fi);
00236 
00237    return fi.fFilename;
00238 }
00239 
00240 const char *SaveFileDialog()
00241 {
00242    // Prompt for file to be saved. Depending on navigation in
00243    // dialog the current working directory can be changed.
00244    // The returned file name is always with respect to the
00245    // current directory.
00246 
00247    const char *gSaveAsTypes[] = {
00248       "Macro files",  "*.C",
00249       "ROOT files",   "*.root",
00250       "PostScript",   "*.ps",
00251       "Encapsulated PostScript", "*.eps",
00252       "Gif files",    "*.gif",
00253       "All files",    "*",
00254        0,              0
00255    };
00256 
00257    static TGFileInfo fi;
00258    fi.fFileTypes = gSaveAsTypes;
00259    new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDSave, &fi);
00260 
00261    return fi.fFilename;
00262 }
00263 
00264 const char *GetStringDialog(const char *prompt, const char *defval)
00265 {
00266    // Prompt for string. The typed in string is returned.
00267 
00268    static char answer[128];
00269 
00270    new InputDialog(prompt, defval, answer);
00271 
00272    return answer;
00273 }
00274 
00275 Int_t GetIntegerDialog(const char *prompt, Int_t defval)
00276 {
00277    // Prompt for integer. The typed in integer is returned.
00278 
00279    static char answer[32];
00280 
00281    char defv[32];
00282    sprintf(defv, "%d", defval);
00283 
00284    new InputDialog(prompt, defv, answer);
00285 
00286    return atoi(answer);
00287 }
00288 
00289 Float_t GetFloatDialog(const char *prompt, Float_t defval)
00290 {
00291    // Prompt for float. The typed in float is returned.
00292 
00293    static char answer[32];
00294 
00295    char defv[32];
00296    sprintf(defv, "%f", defval);
00297 
00298    new InputDialog(prompt, defv, answer);
00299 
00300    return atof(answer);
00301 }

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