TContextMenu.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TContextMenu.cxx 30778 2009-10-16 15:51:59Z rdm $
00002 // Author: Nenad Buncic   08/02/96
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 // TContextMenu                                                         //
00015 //                                                                      //
00016 // This class provides an interface to  context sensitive popup menus.  //
00017 // These menus pop up when the user hits the right mouse button, and    //
00018 // are destroyed when the menu pops downs.                              //
00019 //                                                                      //
00020 // Context Menus are automatically generated by ROOT using the          //
00021 // following convention: if the string // *MENU* is found in the        //
00022 // comment field of a member function. This function will be added to   //
00023 // the list of items in the menu.                                       //
00024 //                                                                      //
00025 // The picture below shows a canvas with a pop-up menu.                 //
00026 //                                                                      //
00027 //Begin_Html <img src="gif/hsumMenu.gif"> End_Html                      //
00028 //                                                                      //
00029 // The picture below shows a canvas with a pop-up menu and a dialog box.//
00030 //                                                                      //
00031 //Begin_Html <img src="gif/hsumDialog.gif"> End_Html                    //
00032 //////////////////////////////////////////////////////////////////////////
00033 
00034 
00035 // silence warning about some cast operations
00036 #if defined(__GNUC__) && __GNUC__ >= 4 && ((__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ >= 1) || (__GNUC_MINOR__ >= 3)) && !__INTEL_COMPILER
00037 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
00038 #endif
00039 
00040 #include "TROOT.h"
00041 #include "TContextMenu.h"
00042 #include "TVirtualPad.h"
00043 #include "TGuiFactory.h"
00044 #include "TMethod.h"
00045 #include "TMethodArg.h"
00046 #include "TGlobal.h"
00047 #include "TObjArray.h"
00048 #include "TObjString.h"
00049 #include "TToggle.h"
00050 #include "TClassMenuItem.h"
00051 #include "TBrowser.h"
00052 #include "TClass.h"
00053 #include "TObjectSpy.h"
00054 
00055 ClassImp(TContextMenu)
00056 
00057 
00058 //______________________________________________________________________________
00059 TContextMenu::TContextMenu(const char *name, const char *title)
00060              : TNamed(name, title)
00061 {
00062    // Create a context menu.
00063 
00064    fSelectedObject   = 0;
00065    fCalledObject     = 0;
00066    fSelectedMethod   = 0;
00067    fBrowser          = 0;
00068    fSelectedPad      = 0;
00069    fSelectedCanvas   = 0;
00070    fSelectedMenuItem = 0;
00071 
00072    fContextMenuImp = gGuiFactory->CreateContextMenuImp(this, name, title);
00073 }
00074 
00075 //______________________________________________________________________________
00076 TContextMenu::~TContextMenu()
00077 {
00078    // Destroy a context menu.
00079 
00080    delete fContextMenuImp;
00081 
00082    fSelectedMethod   = 0;
00083    fCalledObject     = 0;
00084    fSelectedObject   = 0;
00085    fSelectedMenuItem = 0;
00086    fContextMenuImp   = 0;
00087 }
00088 
00089 //______________________________________________________________________________
00090 void TContextMenu::Action(TObject *object, TMethod *method)
00091 {
00092    // Action to be performed when this menu item is selected.
00093    // If the selected method requires arguments we popup an
00094    // automatically generated dialog, otherwise the method is
00095    // directly executed.
00096 
00097    if (method) {
00098       SetMethod( method );
00099       SetSelectedMenuItem(0);
00100       SetCalledObject(object);
00101 
00102       if (method->GetListOfMethodArgs()->First())
00103          fContextMenuImp->Dialog(object, method);
00104       else {
00105          Execute(object, method, "");
00106       }
00107    }
00108 
00109    if (fBrowser) fBrowser->Refresh();
00110 }
00111 
00112 //______________________________________________________________________________
00113 void TContextMenu::Action(TClassMenuItem *menuitem)
00114 {
00115    // Action to be performed when this menu item is selected.
00116    // If the selected method requires arguments we popup an
00117    // automatically generated dialog, otherwise the method is
00118    // directly executed.
00119 
00120    TObject* object;
00121    TMethod* method = 0;
00122 
00123    SetSelectedMenuItem( menuitem );
00124 
00125    // Get the object to be called
00126    if (menuitem->IsCallSelf()) object=fSelectedObject;
00127    else object=menuitem->GetCalledObject();
00128 
00129    if (object) {
00130       // If object deleted, remove from popup and return
00131       if (!(object->TestBit(kNotDeleted))) {
00132          menuitem->SetType(TClassMenuItem::kPopupSeparator);
00133          menuitem->SetCall(0,"");
00134          return;
00135       }
00136 
00137       method = object->IsA()->GetMethodWithPrototype(menuitem->GetFunctionName(),menuitem->GetArgs());
00138 
00139    }
00140 //    if (!menuitem->IsCallSelf()) {
00141 //       funproto = menuitem->GetFunctionName();
00142 //       funproto = funproto + "(" + menuitem->GetArgs() + ")";
00143 //    }
00144 
00145    // calling object, call the method directly
00146    if (object) {
00147       if (method) {
00148          SetMethod(method);
00149          SetCalledObject(object);
00150 
00151          if ((method->GetListOfMethodArgs()->First()
00152                             && menuitem->GetSelfObjectPos() < 0 ) ||
00153               method->GetListOfMethodArgs()->GetSize() > 1)
00154             fContextMenuImp->Dialog(object, method);
00155          else {
00156             if (menuitem->GetSelfObjectPos() < 0) {
00157 #ifndef WIN32
00158                Execute(object, method, "");
00159 #else
00160                // It is a workaround of the "Dead lock under Windows
00161                char *cmd = Form("((TContextMenu *)0x%lx)->Execute((TObject *)0x%lx,"
00162                                 "(TMethod *)0x%lx,(TObjArray *)0);",
00163                                 (Long_t)this,(Long_t)object,(Long_t)method);
00164                //Printf("%s", cmd);
00165                gROOT->ProcessLine(cmd);
00166                //Execute( object, method, (TObjArray *)NULL );
00167 #endif
00168             } else {
00169 #ifndef WIN32
00170                Execute(object, method, Form("(TObject*)0x%lx",(Long_t)fSelectedObject));
00171 #else
00172                // It is a workaround of the "Dead lock under Windows
00173                char *cmd = Form("((TContextMenu *)0x%lx)->Execute((TObject *)0x%lx,"
00174                                 "(TMethod *)0x%lx,(TObjArray *)0);",
00175                                 (Long_t)this,(Long_t)object,(Long_t)method);
00176                //Printf("%s", cmd);
00177                gROOT->ProcessLine(cmd);
00178                //Execute( object, method, (TObjArray *)NULL );
00179 #endif
00180             }
00181          }
00182       }
00183 
00184    } else {
00185       // Calling a standalone global function
00186       TFunction* function = gROOT->GetGlobalFunctionWithPrototype(
00187                                  menuitem->GetFunctionName());
00188                                  //menuitem->GetArgs());
00189       if (function) {
00190          SetMethod(function);
00191          SetCalledObject(0);
00192          if ( (function->GetNargs() && menuitem->GetSelfObjectPos() < 0) ||
00193                function->GetNargs() > 1) {
00194             fContextMenuImp->Dialog(0,function);
00195          } else {
00196             char* cmd;
00197             if (menuitem->GetSelfObjectPos() < 0) {
00198                cmd = Form("%s();", menuitem->GetFunctionName());
00199             } else {
00200               cmd = Form("%s((TObject*)0x%lx);",
00201                      menuitem->GetFunctionName(), (Long_t)fSelectedObject);
00202             }
00203             gROOT->ProcessLine(cmd);
00204          }
00205       }
00206    }
00207 
00208    if (fBrowser) fBrowser->Refresh();
00209 }
00210 
00211 //______________________________________________________________________________
00212 void TContextMenu::Action(TObject *object, TToggle *toggle)
00213 {
00214    // Action to be performed when this toggle menu item is selected.
00215 
00216    if (object && toggle) {
00217       TObjectSpy savePad;
00218 
00219       gROOT->SetSelectedPrimitive(object);
00220       if (fSelectedPad && gPad) {
00221          savePad.SetObject(gPad);
00222          fSelectedPad->cd();
00223       }
00224       TObjectRefSpy fsp((TObject*&) fSelectedPad);
00225       TObjectRefSpy fsc((TObject*&) fSelectedCanvas);
00226 
00227       gROOT->SetFromPopUp(kTRUE);
00228       toggle->Toggle();
00229       if (fSelectedCanvas && fSelectedCanvas->GetPadSave())
00230          fSelectedCanvas->GetPadSave()->Modified();
00231       if (fSelectedPad)
00232          fSelectedPad->Modified();
00233       gROOT->SetFromPopUp(kFALSE);
00234 
00235       if (savePad.GetObject())
00236          ((TVirtualPad*)savePad.GetObject())->cd();
00237 
00238       if (fSelectedCanvas) {
00239          fSelectedCanvas->Update();
00240          if (fSelectedCanvas->GetPadSave())
00241             fSelectedCanvas->GetPadSave()->Update();
00242       }
00243    }
00244 
00245    if (fBrowser) fBrowser->Refresh();
00246 }
00247 
00248 //______________________________________________________________________________
00249 const char *TContextMenu::CreateArgumentTitle(TMethodArg *argument)
00250 {
00251    // Create string describing argument (for use in dialog box).
00252 
00253    static TString argTitle;
00254 
00255    if (argument) {
00256       argTitle.Form("(%s)  %s", argument->GetTitle(), argument->GetName());
00257       if (argument->GetDefault() && *(argument->GetDefault())) {
00258          argTitle += "  [default: ";
00259          argTitle += argument->GetDefault();
00260          argTitle += "]";
00261       }
00262    } else
00263       argTitle.Clear();
00264 
00265    return argTitle.Data();
00266 }
00267 
00268 //______________________________________________________________________________
00269 const char *TContextMenu::CreateDialogTitle(TObject *object, TFunction *method)
00270 {
00271    // Create title for dialog box retrieving argument values.
00272 
00273    static TString methodTitle;
00274 
00275    if (object && method)
00276       methodTitle.Form("%s::%s", object->ClassName(), method->GetName());
00277    else if (!object && method)
00278       methodTitle.Form("%s", method->GetName());
00279    else
00280       methodTitle.Clear();
00281 
00282    return methodTitle.Data();
00283 }
00284 
00285 //______________________________________________________________________________
00286 const char *TContextMenu::CreatePopupTitle(TObject *object)
00287 {
00288    // Create title for popup menu.
00289 
00290    static TString popupTitle;
00291 
00292    if (object) {
00293       if (!*(object->GetName()) || !strcmp(object->GetName(), object->ClassName())) {
00294          TGlobal *global = (TGlobal *) gROOT->GetGlobal(object);
00295          if (global && *(global->GetName()))
00296             popupTitle.Form("  %s::%s  ", object->ClassName(), global->GetName());
00297          else {
00298             if (!strcmp(object->IsA()->GetContextMenuTitle(), ""))
00299                popupTitle.Form("  %s  ", object->ClassName());
00300             else
00301                popupTitle.Form("  %s  ", object->IsA()->GetContextMenuTitle());
00302          }
00303       } else {
00304          if (!strcmp(object->IsA()->GetContextMenuTitle(), ""))
00305             popupTitle.Form("  %s::%s  ", object->ClassName(), object->GetName());
00306          else
00307             popupTitle.Form("  %s::%s  ", object->IsA()->GetContextMenuTitle(),
00308                             object->GetName());
00309       }
00310       if (popupTitle.Length() > 60) {
00311          popupTitle.Remove(60);
00312          popupTitle += "...";
00313       }
00314    } else
00315       popupTitle.Clear();
00316 
00317    return popupTitle.Data();
00318 }
00319 
00320 //______________________________________________________________________________
00321 void TContextMenu::Execute(TObject *object, TFunction *method, const char *params)
00322 {
00323    // Execute method with specified arguments for specified object.
00324 
00325    if (method) {
00326       TObjectSpy savePad;
00327 
00328       gROOT->SetSelectedPrimitive(object);
00329       if (fSelectedPad && gPad) {
00330          savePad.SetObject(gPad);
00331          fSelectedPad->cd();
00332       }
00333       TObjectRefSpy fsp((TObject*&) fSelectedPad);
00334       TObjectRefSpy fsc((TObject*&) fSelectedCanvas);
00335 
00336       gROOT->SetFromPopUp(kTRUE);
00337       if (object) {
00338          object->Execute((char *) method->GetName(), params);
00339       } else {
00340          char *cmd = Form("%s(%s);", method->GetName(),params);
00341          gROOT->ProcessLine(cmd);
00342       }
00343       if (fSelectedCanvas && fSelectedCanvas->GetPadSave())
00344          fSelectedCanvas->GetPadSave()->Modified();
00345       if (fSelectedPad)
00346          fSelectedPad->Modified();
00347       gROOT->SetFromPopUp(kFALSE);
00348 
00349       if (savePad.GetObject())
00350          ((TVirtualPad*)savePad.GetObject())->cd();
00351 
00352       if (fSelectedCanvas) {
00353          fSelectedCanvas->Update();
00354          if (fSelectedCanvas->GetPadSave())
00355             fSelectedCanvas->GetPadSave()->Update();
00356       }
00357    }
00358 
00359    if (fBrowser) fBrowser->Refresh();
00360 }
00361 
00362 //______________________________________________________________________________
00363 void TContextMenu::Execute(TObject *object, TFunction *method, TObjArray *params)
00364 {
00365    // Execute method with specified arguments for specified object.
00366 
00367    if (method) {
00368       TObjectSpy savePad;
00369 
00370       gROOT->SetSelectedPrimitive(object);
00371       if (fSelectedPad && gPad) {
00372          savePad.SetObject(gPad);
00373          fSelectedPad->cd();
00374       }
00375       TObjectRefSpy fsp((TObject*&) fSelectedPad);
00376       TObjectRefSpy fsc((TObject*&) fSelectedCanvas);
00377 
00378       gROOT->SetFromPopUp(kTRUE);
00379       if (object) {
00380          object->Execute((TMethod*)method, params);
00381       } else {
00382          TString args;
00383          TIter next(params);
00384          TObjString *s;
00385          while ((s = (TObjString*) next())) {
00386             if (!args.IsNull()) args += ",";
00387             args += s->String();
00388          }
00389          char *cmd = Form("%s(%s);", method->GetName(), args.Data());
00390          gROOT->ProcessLine(cmd);
00391       }
00392       if (fSelectedCanvas && fSelectedCanvas->GetPadSave())
00393          fSelectedCanvas->GetPadSave()->Modified();
00394       if (fSelectedPad)
00395          fSelectedPad->Modified();
00396       gROOT->SetFromPopUp(kFALSE);
00397 
00398       if (savePad.GetObject())
00399          ((TVirtualPad*)savePad.GetObject())->cd();
00400 
00401       if (fSelectedCanvas) {
00402          fSelectedCanvas->Update();
00403          if (fSelectedCanvas->GetPadSave())
00404             fSelectedCanvas->GetPadSave()->Update();
00405       }
00406    }
00407    if (fBrowser) fBrowser->Refresh();
00408 }
00409 
00410 //______________________________________________________________________________
00411 void TContextMenu::Popup(Int_t x, Int_t y, TObject *obj, TVirtualPad *c, TVirtualPad *p)
00412 {
00413    // Popup context menu at given location in canvas c and pad p for selected
00414    // object.
00415 
00416    SetBrowser(0);
00417    SetObject(obj);
00418    SetCanvas(c);
00419    SetPad(p);
00420 
00421    DisplayPopUp(x,y);
00422 }
00423 
00424 //______________________________________________________________________________
00425 void TContextMenu::Popup(Int_t x, Int_t y, TObject *obj, TBrowser *b)
00426 {
00427    // Popup context menu at given location in browser b for selected object.
00428 
00429    SetBrowser(b);
00430    SetObject(obj);
00431    SetCanvas(0);
00432    SetPad(0);
00433 
00434    DisplayPopUp(x,y);
00435 }

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