TClassMenuItem Issue


I have a class ICoolMainFrame derived from TGMainFrame, which has several TRootEmbeddedCanvas member variables like so:

class ICoolMainFrame : public TGMainFrame {
    TRootEmbeddedCanvas *fCanvas;
    TRootEmbeddedCanvas *fCanvas1;

I set up a TClassMenuItem like so:

TClass *cl=fCanvas->GetCanvas()->IsA();
    TList *l;
    TClassMenuItem *n;

    n= new TClassMenuItem(TClassMenuItem::kPopupUserFunction,cl,
                          "Remove Canvas","RemoveCanvas",this,"TObject*",1);

I want my function RemoveCanvas, to be able to hide the canvas that the user clicked on when selecting RemoveCanvas. RemoveCanvas

void ICoolMainFrame::RemoveCanvas(TObject *obj)
    printf("TCanvas : %lx\n",(Long_t)obj);
    printf("title of the canvas : %s\n",obj->IsA()->GetTitle());

However this doesn’t seem to work. How do I actually pass the object to a function associated with a TClassMenuItem so that I can perform actions on that object itself?

Any help would be greatly appreciated.



In the example I’ve sent you a week ago, I had a special check:

void TestFrame::Action(const TObject *clickedOn) const
   if (clickedOn == embCnv->GetCanvas())
      std::cout<<"clicked in embedded canvas ... the object is "<<clickedOn->IsA()->GetName()<<std::endl;
      std::cout<<"Method was called, but ...\n";

So it looks like you’ve completely ignored my code. If you know that clickedOn is a TCanvas from embCnv, you know now where a click happened … in embCnv (or it will be fCanvas1/fCanvas2 in your case), so you hide/remove the corresponding embedded canvas now.

void MyNotSoCoolMainFrame::HideCanvas(TObject *obj)
    if (obj == fCanvas->GetCanvas())
    else if (obj == fCanvas1->GetCanvas())

Before doing wild type casts, be sure you understand what you are casting.
TCanvas is not a TGFrame, so such a cast is illegal and gives you nothing.
Even worse obj->IsA() returns TClass * which has nothing to do with TGFrame.
You can not cast TClass * to TGFrame *.
And even more obj->IsA()->GetTitle() is not a title for TCanvas.
Have a look at classes/methods declarations to check how to use them.

For example, TCanvas itself is not a window, it has a “canvas imp” - a pointer to a window (either TRootCanvas or TRootEmbeddedCanvas). TRootCanvas/TRootEmbeddedCanvas are already GUI (window) classes.

Best regards,