Now that we have our system configure, the test beds working and our IDE project set up, we are now ready to embed our OpenCascade Window into our wxWidget Window.
There’s a handful of tutorials on ways to do this, but they’re either out of date by over a decade or are missing some important points that cause segmentation faults, X11 Bad Window errors, or simply just weird crashes.
Creating the Control
The initial tests kept OpenCascade as a separate entity being parented into a wxPanel during creation of the window at runtime. This was great for a first try, but it becomes tricky too get automatic refreshing and resizing working.
Includes
There’s a large list of includes needed even for a simple file. Instead of adding dozens of lines to this post, you can go over too here and see the required #includes.
Global Variables
The set of global variables needed are shown and described below:
[code language=”cpp”]</pre>
<pre>[/code]
[gist https://gist.github.com/virte-c/83b60b2230e9d587f903 /]
Constructor
We then need to fill in the constructor. Note the Gist shown below is only an exert of a the full constructor.
[gist https://gist.github.com/virte-c/c0c8fd9ad21e3927a519 /]
The line which we need too dive into now is the Viewer(…) method. This is where the X11 window needs grabs the wxPanel Widget handle and creates a wrapper window around it.
Creating a OCC Window
But before we wrap any windows, first we want to make sure we can create one to make sure everything’s working.
This first gist shows creating the OCC window separate from wxWindow.
[gist https://gist.github.com/virte-c/d450f49f7e4410d73ac5 /]
Embeding the OCC Window in wxWidgets
Too embed the OCC window into awxPanel control, First the Handle is needed of the wxPanel.
GtkWidget* widget = panel->GetHandle(); |
A major issue is when the OCC window is created, the GTK window hasn’t always been fully realized yet and this causes random Segment Faults and crashes. To get around this, we run the next two lines.
gtk_widget_realize( widget ); |
gtk_widget_set_double_buffered(widget, 0); |
The next line is too access the window from the Handle that was retrieved from the wxPanel.
Window wid = GDK_WINDOW_XWINDOW( widget->window ); |
Note that the Window object is a X11 window.
From there, we can now use the second Xw_Window constructor which creates the OCC window wrapped around a previous control.
wind = new Xw_Window(aDisplayConnection, wid); |
The final thing too do is too tell OCC which Xw_Window too use and too map the window.
//Set Window
mView->SetWindow(wind); if(!wind->IsMapped()) wind->Map(); |
When it’s all put together, the Viewer(..) method looks like it does below.
[gist https://gist.github.com/rtrawr/d956c689de0b3e28dbf5 /]
Calling Draw and OnSize
With all that down, all we need to do now is handle resizing and drawing. It’s simply down by setting up the following methods.
[gist https://gist.github.com/virte-c/a96847681154b1959a9c /]
And that’s that.
Actually Rendering something!
I’ll cover opening model files in another post, but for now, It’s simple too Create the ‘Demo Bottle’ from the DRAWEXE test bed. Simply add the following method to your class and call it at the end of the constructor after everything else.
[gist https://gist.github.com/virte-c/1789a9ab60ee390c357a /]
Putting everything together, you should get something looking like the following:
But what’s that DoZoomFit(); method in that last Gist??? I’ll show that and more Input controls and orientation controls in the next Tutorial.