TRootDialog.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TRootDialog.cxx 33963 2010-06-17 13:02:58Z bellenot $
00002 // Author: Fons Rademakers   20/02/98
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 // TRootDialog                                                          //
00015 //                                                                      //
00016 // A TRootDialog is used to prompt for the arguments of an object's     //
00017 // member function. A TRootDialog is created via the context menu's     //
00018 // when selecting a member function taking arguments.                   //
00019 //                                                                      //
00020 //////////////////////////////////////////////////////////////////////////
00021 
00022 #include "TRootDialog.h"
00023 #include "TRootContextMenu.h"
00024 #include "TContextMenu.h"
00025 #include "TClassMenuItem.h"
00026 #include "TList.h"
00027 #include "TGLabel.h"
00028 #include "TGTextEntry.h"
00029 #include "TGButton.h"
00030 #include "TObjString.h"
00031 #include "KeySymbols.h"
00032 
00033 extern TGTextEntry *gBlinkingEntry;
00034 
00035 ClassImp(TRootDialog)
00036 
00037 //______________________________________________________________________________
00038 TRootDialog::TRootDialog(TRootContextMenu *cmenu, const TGWindow *main,
00039     const char *title, Bool_t okB, Bool_t cancelB, Bool_t applyB,
00040     Bool_t helpB) : TGTransientFrame(gClient->GetRoot(), main, 200, 100)
00041 {
00042    // Create a method argument prompt dialog.
00043 
00044    fMenu   = cmenu;
00045 
00046    fOk     = okB;
00047    fCancel = cancelB;
00048    fApply  = applyB;
00049    fHelp   = helpB;
00050 
00051    fWidgets = new TList;
00052 
00053    fL1 = new TGLayoutHints(kLHintsTop | kLHintsCenterX, 0, 0, 5, 0);
00054    fL2 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5);
00055 
00056    SetWindowName(title);
00057    SetIconName(title);
00058    SetEditDisabled(kEditDisable);
00059 
00060    AddInput(kKeyPressMask | kEnterWindowMask | kLeaveWindowMask);
00061 }
00062 
00063 //______________________________________________________________________________
00064 TRootDialog::~TRootDialog()
00065 {
00066    // Delete the dialog.
00067 
00068    fWidgets->Delete();
00069    delete fWidgets;
00070    delete fL1;
00071    delete fL2;
00072 }
00073 
00074 //______________________________________________________________________________
00075 void TRootDialog::Add(const char *argname, const char *value, const char *type)
00076 {
00077    // Add a label and text input field.
00078 
00079    TGLabel      *l = new TGLabel(this, argname);
00080    TGTextBuffer *b = new TGTextBuffer(20); b->AddText(0, value);
00081    TGTextEntry  *t = new TGTextEntry(this, b);
00082 
00083    t->Connect("TabPressed()", "TRootDialog", this, "TabPressed()");
00084 
00085    t->Associate(fMenu);
00086    t->Resize(260, t->GetDefaultHeight());
00087    AddFrame(l, fL1);
00088    AddFrame(t, fL2);
00089 
00090    fWidgets->Add(l);
00091    fWidgets->Add(t);   // TGTextBuffer will be deleted by TGTextEntry
00092    fWidgets->Add(new TObjString(type));
00093 }
00094 
00095 //______________________________________________________________________________
00096 const char *TRootDialog::GetParameters()
00097 {
00098    // Get parameter string (called by contextmenu after OK or Apply has
00099    // been selected).
00100 
00101    static TString params;
00102    TString param;
00103 
00104    TObjString   *str;
00105    TObject      *obj;
00106 
00107    Int_t selfobjpos;
00108    if (fMenu->GetContextMenu()->GetSelectedMenuItem())
00109       selfobjpos = fMenu->GetContextMenu()->GetSelectedMenuItem()->GetSelfObjectPos();
00110    else
00111       selfobjpos = -1;
00112 
00113    params.Clear();
00114    TIter next(fWidgets);
00115    Int_t nparam = 0;
00116 
00117    while ((obj = next())) {        // first element is label, skip...
00118       if (obj->IsA() != TGLabel::Class()) break;
00119       obj = next();                // get either TGTextEntry or TGComboBox
00120       str = (TObjString *) next(); // get type string
00121 
00122       nparam++;
00123 
00124       const char *type = str->GetString().Data();
00125       const char *data = 0;
00126 
00127       if (obj->IsA() == TGTextEntry::Class())
00128          data = ((TGTextEntry *) obj)->GetBuffer()->GetString();
00129 
00130       // TODO: Combobox...
00131 
00132       // if necessary, replace the selected object by it's address
00133       if (selfobjpos == nparam-1) {
00134          if (params.Length()) params += ",";
00135          param = TString::Format("(TObject*)0x%lx",
00136                (Long_t)fMenu->GetContextMenu()->GetSelectedObject());
00137          params += param;
00138       }
00139 
00140       if (params.Length()) params += ",";
00141       if (data) {
00142          if (!strncmp(type, "char*", 5))
00143             param = TString::Format("\"%s\"", data);
00144          else
00145             param = data;
00146       } else
00147          param = "0";
00148 
00149       params += param;
00150    }
00151 
00152    // if selected object is the last argument, have to insert it here
00153    if (selfobjpos == nparam) {
00154       if (params.Length()) params += ",";
00155       param = TString::Format("(TObject*)0x%lx",
00156             (Long_t)fMenu->GetContextMenu()->GetSelectedObject());
00157       params += param;
00158    }
00159 
00160    return params.Data();
00161 }
00162 
00163 //______________________________________________________________________________
00164 void TRootDialog::Popup()
00165 {
00166    // Popup dialog.
00167 
00168    //--- create the OK, Apply and Cancel buttons
00169 
00170    UInt_t  nb = 0, width = 0, height = 0;
00171 
00172    TGHorizontalFrame *hf = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
00173    TGLayoutHints     *l1 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0);
00174 
00175    // put hf as last in the list to be deleted
00176    fWidgets->Add(l1);
00177 
00178    TGTextButton *b;
00179    if (fOk) {
00180       b = new TGTextButton(hf, "&OK", 1);
00181       fWidgets->Add(b);
00182       b->Associate(fMenu);
00183       hf->AddFrame(b, l1);
00184       height = b->GetDefaultHeight();
00185       width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;
00186    }
00187    if (fApply) {
00188       b = new TGTextButton(hf, "&Apply", 2);
00189       fWidgets->Add(b);
00190       b->Associate(fMenu);
00191       hf->AddFrame(b, l1);
00192       height = b->GetDefaultHeight();
00193       width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;
00194    }
00195    if (fCancel) {
00196       b = new TGTextButton(hf, "&Cancel", 3);
00197       fWidgets->Add(b);
00198       b->Associate(fMenu);
00199       hf->AddFrame(b, l1);
00200       height = b->GetDefaultHeight();
00201       width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;
00202    }
00203    if (fHelp) {
00204       b = new TGTextButton(hf, "Online &Help", 4);
00205       fWidgets->Add(b);
00206       b->Associate(fMenu);
00207       hf->AddFrame(b, l1);
00208       height = b->GetDefaultHeight();
00209       width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;
00210    }
00211 
00212    // place buttons at the bottom
00213    l1 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
00214    fWidgets->Add(l1);
00215    fWidgets->Add(hf);
00216 
00217    AddFrame(hf, l1);
00218 
00219    // keep the buttons centered and with the same width
00220    hf->Resize((width + 20) * nb, height);
00221 
00222    // map all widgets and calculate size of dialog
00223    MapSubwindows();
00224 
00225    width  = GetDefaultWidth();
00226    height = GetDefaultHeight();
00227 
00228    Resize(width, height);
00229 
00230    // position relative to the parent's window
00231    CenterOnParent();
00232 
00233    // make the message box non-resizable
00234    SetWMSize(width, height);
00235    SetWMSizeHints(width, height, width, height, 0, 0);
00236 
00237    SetMWMHints(kMWMDecorAll | kMWMDecorResizeH  | kMWMDecorMaximize |
00238                               kMWMDecorMinimize | kMWMDecorMenu,
00239                kMWMFuncAll  | kMWMFuncResize    | kMWMFuncMaximize |
00240                               kMWMFuncMinimize,
00241                kMWMInputModeless);
00242 
00243    MapWindow();
00244    fClient->WaitFor(this);
00245 }
00246 
00247 //______________________________________________________________________________
00248 void TRootDialog::CloseWindow()
00249 {
00250    // Called when closed via window manager action.
00251 
00252    // Send Cancel button message to context menu eventhandler
00253    SendMessage(fMenu, MK_MSG(kC_COMMAND, kCM_BUTTON), 3, 0);
00254 }
00255 
00256 //______________________________________________________________________________
00257 void TRootDialog::TabPressed()
00258 {
00259    // Handle Tab keyboard navigation in this dialog.
00260 
00261    Bool_t setNext = kFALSE;
00262    TGTextEntry *entry;
00263    TIter next(fWidgets);
00264 
00265    while ( TObject* obj = next() ) {
00266       if ( obj->IsA() == TGTextEntry::Class() ) {
00267          entry = (TGTextEntry*) obj;
00268          if ( entry == gBlinkingEntry ) {
00269             setNext = kTRUE;
00270          } else if ( setNext ) {
00271             entry->SetFocus();
00272             entry->End();
00273             return;
00274          }
00275       }
00276    }
00277 
00278    next.Reset();
00279    while ( TObject* obj = next() ) {
00280       if ( obj->IsA() == TGTextEntry::Class() ) {
00281          entry = (TGTextEntry*) obj;
00282          entry->SetFocus();
00283          entry->End();
00284          return;
00285       }
00286    }
00287 }
00288 
00289 //______________________________________________________________________________
00290 Bool_t TRootDialog::HandleKey(Event_t* event)
00291 {
00292    // The key press event handler in this dialog.
00293 
00294    Int_t  n;
00295    char   tmp[10];
00296    UInt_t keysym;
00297    gVirtualX->LookupString(event, tmp, sizeof(tmp), keysym);
00298    n = strlen(tmp);
00299    if ((EKeySym)keysym  == kKey_Tab) {
00300 
00301       TGTextEntry *entry;
00302       TIter next(fWidgets);
00303 
00304       while ( TObject* obj = next() ) {
00305          if ( obj->IsA() == TGTextEntry::Class() ) {
00306             entry = (TGTextEntry*) obj;
00307             entry->TabPressed();
00308             return kTRUE;
00309          }
00310       }
00311    }
00312 
00313    return TGMainFrame::HandleKey(event);
00314 }

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