home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / opengl / overlay_x11 / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  3.5 KB  |  127 lines

  1. /****************************************************************************
  2. ** $Id:  qt/main.cpp   3.0.0   edited Jul 30 22:45 $
  3. **
  4. ** Example application showing how to use Qt and Qt OpenGL Extension on an 
  5. ** X11 overlay visual 
  6. **
  7. ** Copyright (C) 1999 by Trolltech AS.  All rights reserved.
  8. **
  9. ** This file is part of an example program for Qt.  This example
  10. ** program may be used, distributed and modified without limitation.
  11. **
  12. *****************************************************************************/
  13.  
  14. #include <qapplication.h>
  15. #include "gearwidget.h"
  16. #include "rubberbandwidget.h"
  17.  
  18. #if defined(Q_WS_X11)
  19. #include <X11/Xlib.h>
  20. #endif
  21.  
  22. QColor findOverlayTransparentColor()
  23. {
  24.     QColor invalidColor;
  25.  
  26. #if defined(Q_WS_X11)
  27.  
  28.     Display* appDisplay;
  29.     Visual* appVisual;
  30.  
  31.     // The static methods are called 'App' in Qt 2.x
  32. #if QT_VERSION < 200
  33.     appDisplay = QPaintDevice::x__Display();
  34.     appVisual = (Visual*)QPaintDevice::x11Visual();
  35. #else
  36.     appDisplay = QPaintDevice::x11AppDisplay();
  37.     appVisual = (Visual*)QPaintDevice::x11AppVisual();
  38. #endif
  39.  
  40.     qDebug( "Default Visual ID: 0x%x", (int)XVisualIDFromVisual(appVisual) );
  41.  
  42.     typedef struct OverlayProp {
  43.     long  visual;
  44.     long  type;
  45.     long  value;
  46.     long  layer;
  47.     } OverlayProp;
  48.  
  49.     QWidget* rootWin = QApplication::desktop();
  50.     if ( !rootWin )
  51.     return invalidColor; // Should not happen
  52.  
  53.     Atom overlayVisualsAtom = XInternAtom( appDisplay, 
  54.                        "SERVER_OVERLAY_VISUALS", True );
  55.     if ( overlayVisualsAtom == None ) {
  56.     warning( "Server has no overlay visuals" );
  57.     return invalidColor;
  58.     }
  59.  
  60.     Atom actualType;
  61.     int actualFormat;
  62.     ulong nItems;
  63.     ulong bytesAfter;
  64.     OverlayProp* overlayProp;
  65.     int res = XGetWindowProperty( appDisplay, QApplication::desktop()->winId(),
  66.                   overlayVisualsAtom, 0, 10000, False, 
  67.                   overlayVisualsAtom, &actualType, 
  68.                   &actualFormat, &nItems, &bytesAfter,
  69.                   (uchar**)&overlayProp );
  70.  
  71.     if ( res != Success || actualType != overlayVisualsAtom 
  72.      || actualFormat != 32 || nItems < 4 ) {
  73.     warning( "Failed to get overlay visual property from server" );
  74.     return invalidColor;
  75.     }
  76.  
  77.  
  78.     for ( uint i = 0; i < nItems/4; i++ ) {
  79.     if ( (VisualID)overlayProp[i].visual == XVisualIDFromVisual(appVisual)
  80.          && overlayProp[i].type == 1 )
  81.         return QColor( qRgb( 1, 2, 3 ), overlayProp[i].value );
  82.     }
  83.  
  84.     qWarning( "Default visual is not in overlay plane" );
  85.     return invalidColor;
  86.  
  87. #else // defined(Q_WS_X11)
  88.     qWarning( "Wrong window system - Only X11 has overlay support." );
  89.     return invalidColor;
  90. #endif
  91. }
  92.  
  93.  
  94. int main( int argc, char **argv )
  95. {
  96.     QApplication::setColorSpec( QApplication::CustomColor );
  97.     QApplication a( argc, argv );
  98.  
  99.     if ( !QGLFormat::hasOpenGL() ) {
  100.     qWarning( "This system has no OpenGL support. Exiting." );
  101.     return -1;
  102.     }
  103.  
  104.     QColor transparentColor = findOverlayTransparentColor();
  105.     if ( !transparentColor.isValid() ) {
  106.     qWarning( "Failed to get transparent color for overlay. Exiting." );
  107.     return -1;
  108.     }
  109.  
  110.     QWidget top;
  111.     a.setMainWidget( &top );
  112.     top.setGeometry( 50, 50, 600, 400 );
  113.  
  114.     // Make an OpenGL widget. It will use the deepest visual available
  115.     // (typically a TrueColor visual), which typically is in the normal layer.
  116.     GearWidget g( &top );
  117.     g.setGeometry( 20, 20, 560, 360 );
  118.  
  119.     // Put the rubberband widget (which uses the default, i.e. overlay visual)
  120.     // on top of the OpenGL widget:
  121.     RubberbandWidget r( transparentColor, &top );
  122.     r.setGeometry( 20, 20, 560, 360 );
  123.  
  124.     top.show();
  125.     return a.exec();
  126. }
  127.