Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# TQt' OpenGL widgets as an ActiveX (executable)

The ActiveX control in this example uses the TQGlWidget class in TQt to render an OpenGL scene in an ActiveX. The control exposes a few methods to change the scene. The example is based on the "box" example from the standard TQt distribution.

The example demonstrates the use of the default factory and TQAxFactory::isServer(), and the implementation of an additional COM interface using TQAxBindable and TQAxAggregated. The server executable can run both as an ActiveX server and as a stand alone application.

The application uses the default factory as provided by the TQAXFACTORY_DEFAULT macro to expose the `GLBox` widget as an ActiveX control.

        #include <qaxfactory.h>

        TQAXFACTORY_DEFAULT( GLBox,
                            "{5fd9c22e-ed45-43fa-ba13-1530bb6b03e0}",
                            "{33b051af-bb25-47cf-a390-5cfd2987d26a}",
                            "{8c996c29-eafa-46ac-a6f9-901951e765b5}",
                            "{2c3c183a-eeda-41a4-896e-3d9c12c3577d}",
                            "{83e16271-6480-45d5-aaf1-3f40b7661ae4}"
                          )

The implementation of `main` initializes the TQApplication object, and uses TQAxFactory::isServer() to determine whether or not it is appropriate to create and show the application interface.

        /*
          The main program is here.
        */

        int main( int argc, char **argv )
        {
            TQApplication::setColorSpec( TQApplication::CustomColor );
            TQApplication a(argc,argv);

            if ( !TQGLFormat::hasOpenGL() ) {
                tqWarning( "This system has no OpenGL support. Exiting." );
                return -1;
            }

            if ( !TQAxFactory::isServer() ) {
                GLObjectWindow w;
                w.resize( 400, 350 );
                a.setMainWidget( &w );
                w.show();
                return a.exec();
            }
            return a.exec();
        }

The `GLBox` class inherits from both the TQGLWidget class to be able to render OpenGL, and from TQAxBindable.

        #include <qaxbindable.h>

        class GLBox : public TQGLWidget,
                      public TQAxBindable
        {
            TQ_OBJECT

The class reimplements the TQAxBindable::createAggregate() function from TQAxBindable to return the pointer to a TQAxAggregated object.

        public:

            GLBox( TQWidget* parent, const char* name );
            ~GLBox();

            TQAxAggregated *createAggregate();

        public slots:

            void                setXRotation( int degrees );

The rest of the class declaration and the implementation of the OpenGL rendering is identical to the original "box" example.

The implementation file of the `GLBox` class includes the `objsafe.h` system header, in which the `IObjectSafety` COM interface is defined.

        #include <objsafe.h>

A class `ObjectSafetyImpl` is declared using multiple inheritance to subclass the TQAxAggregated class, and to implement the IObjectSafety interface.

        class ObjectSafetyImpl : public TQAxAggregated,
                                 public IObjectSafety
        {
        public:

The class declares a default constructor, and implements the queryInterface function to support the IObjectSafety interface.

            ObjectSafetyImpl() {}

            long queryInterface( const TQUuid &iid, void **iface )
            {
                *iface = 0;
                if ( iid == IID_IObjectSafety )
                    *iface = (IObjectSafety*)this;
                else
                    return E_NOINTERFACE;

                AddRef();
                return S_OK;
            }

Since every COM interface inherits `IUnknown` the `TQAXAGG_IUNKNOWN` macro is used to provide the default implementation of the `IUnknown` interface. The macro is defined to delegate all calls to `QueryInterface`, `AddRef` and `Release` to the interface returned by the controllingUnknown() function.

            TQAXAGG_IUNKNOWN;

The implementation of the `IObjectSafety` interface provides the caller with information about supported and enabled safety options, and returns `S_OK` for all calls to indicate that the ActiveX control is safe.

            HRESULT WINAPI GetInterfaceSafetyOptions( REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions )
            {
                *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
                *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
                return S_OK;
            }
            HRESULT WINAPI SetInterfaceSafetyOptions( REFIID riid, DWORD pdwSupportedOptions, DWORD pdwEnabledOptions )
            {
                return S_OK;
            }
        };

The implementation of the `createAggregate()` function just returns a new `ObjectSafetyImpl` object.

        TQAxAggregated *GLBox::createAggregate()
        {
            return new ObjectSafetyImpl();
        }

To build the example you must first build the TQAxServer library. Then run qmake and your make tool in `examples/wrapper`.

The demonstration requires your WebBrowser to support ActiveX controls, and scripting to be enabled.

In contrast to the other TQAxServer examples Internet Explorer will not open a dialog box to ask the user whether or not the scripting of the GLBox control should be allowed (the exact browser behaviour depends on the security settings in the Internet Options dialog).

        <SCRIPT LANGUAGE=JavaScript>
        function setRot( form )
        {
            GLBox.setXRotation( form.XEdit.value );
            GLBox.setYRotation( form.YEdit.value );
            GLBox.setZRotation( form.ZEdit.value );
        }
        </SCRIPT>

        <p>
        An OpenGL scene:<br>
        <object ID="GLBox" CLASSID="CLSID:5fd9c22e-ed45-43fa-ba13-1530bb6b03e0"
        CODEBASE=http://www.trolltech.com/demos/openglax.cab>
        [Object not available! Did you forget to build and register the server?]
        </object><br>

        <form>
        Rotate the scene:<br>
        X:<input type="edit" ID="XEdit" value="0"><br>
        Y:<input type="edit" name="YEdit" value="0"><br>
        Z:<input type="edit" name="ZEdit" value="0"><br>
        <input type="button" value="Set" onClick="setRot(this.form)">
        </form>

See also The TQAxServer Examples.