Hi everyone, I’d need some help in embedding a TCanvas in a wxWidgets 3.0.0 program since I’m having an hard time with the instructions about this topic (How to Embed a TCanvas in External Applications).
I never tried to use wxWidgets before this project, so I’m quite unprepared about the library itself!
Here’s my code:
#include <wx\wxprec.h>
#ifndef WX_PRECOMP
#include <wx\wx.h>
#endif
// ROOT Includes
#include <TApplication.h>
#include <TVirtualX.h>
#include <TSystem.h>
#include <TCanvas.h>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
TApplication *RApp;
TSystem *RSystem;
TCanvas *RCanvas;
TVirtualX *RVirtualX;
wxTimer *Timer;
};
DECLARE_APP(MyApp);
wxIMPLEMENT_APP(MyApp);
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnExit(wxCommandEvent& event);
void OnTimer(wxTimerEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Timer = 1
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
EVT_TIMER(ID_Timer, MyFrame::OnTimer)
wxEND_EVENT_TABLE()
bool MyApp::OnInit()
{
Timer = new wxTimer(this, ID_Timer);
Timer->Start();
RSystem = new TSystem;
RApp = new TApplication("app", 0, NULL);
RApp->SetReturnFromRun(true);
MyFrame* frame = new MyFrame("ROOT Test", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
RVirtualX = new TVirtualX;
int windowID = RVirtualX->AddWindow((ULong_t) frame->GetId(), 450, 340);
RCanvas = new TCanvas("RCanvas", 450, 340, windowID);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu* menuFile = new wxMenu;
menuFile->Append(wxID_EXIT);
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
}
void MyFrame::OnExit(wxCommandEvent& event) { Close(true); }
void MyFrame::OnTimer(wxTimerEvent& event)
{
wxGetApp().RApp->StartIdleing();
wxGetApp().RSystem->InnerLoop();
wxGetApp().RApp->StopIdleing();
}
I tried to modify the “Hello World!” example on the wxWidgets documentation (here’s the link) and show a TCanvas on a wxFrame.
Following the instructions on how to embed the TCanvas i created a TApplication in my wxApp initializer and then I started having some problem, the instructions say that I should create a TCanvas in a class called “MyWindow” and that should be the wxFrame class, but by doing so nothing shows up running the program!
I then tried to create the TCanvas in the wxApp initializer, as you can see in my code, and by doing so the program starts with a menu bar as it should and immediately crashes!
The program breaks trying to initialize TCanvas, so I think this is the code poorly written:
RVirtualX = new TVirtualX;
int windowID = RVirtualX->AddWindow((ULong_t) frame->GetId(), 450, 340);
RCanvas = new TCanvas("RCanvas", 450, 340, windowID);
I believe I’m not handling correctly the Window ID that I have to pass to the TCanvas constructor, so the problem could be about the TVirtualX class, but, since the instrunction don’t tell anything about that, I just created it a bit randomly! (I don’t usually have to do with that class!)
I also tried to implement a timer, but nothing changed!
I’m using ROOT 5.34.14, wxWidgets 3.0.0, Visual Studio Express 2013 on Windows 7 64bit.
Since I’m not an expert programmer, my code is probably really flawed!
Thanks in advance, Nicolò!