• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kxutils.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE libraries
00003     Copyright (C) 2008 Lubos Lunak (l.lunak@kde.org)
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kxutils.h"
00022 
00023 #include <config.h>
00024 
00025 #ifdef Q_WS_X11
00026 
00027 #include <kxerrorhandler.h>
00028 #include <qbitmap.h>
00029 #include <qpixmap.h>
00030 
00031 #ifdef HAVE_XRENDER
00032 #include <X11/extensions/Xrender.h>
00033 #endif
00034 
00035 namespace KXUtils
00036 {
00037 
00038 // Create QPixmap from X pixmap. Take care of different depths if needed.
00039 QPixmap createPixmapFromHandle( WId pixmap, WId pixmap_mask )
00040 {
00041     Display* dpy = QX11Info::display();
00042     KXErrorHandler handler;
00043     Window root;
00044     int x, y;
00045     unsigned int w = 0;
00046     unsigned int h = 0;
00047     unsigned int border_w, depth;
00048     if( XGetGeometry( dpy, pixmap, &root, &x, &y, &w, &h, &border_w, &depth )
00049         && !handler.error( false ) && w > 0 && h > 0 )
00050     {
00051         QPixmap pm( w, h );
00052         // Always detach before doing something behind QPixmap's back.
00053         pm.detach();
00054 #ifdef HAVE_XRENDER
00055         if( int( depth ) != pm.depth() && depth != 1 && pm.x11PictureHandle() != None )
00056         {
00057             XRenderPictFormat tmpl;
00058             tmpl.type = PictTypeDirect;
00059             tmpl.depth = depth;
00060             XRenderPictFormat* format = XRenderFindFormat( dpy, PictFormatType | PictFormatDepth, &tmpl, 0 );
00061             Picture pic = XRenderCreatePicture( dpy, pixmap, format, 0, NULL );
00062             XRenderComposite( dpy, PictOpSrc, pic, None, pm.x11PictureHandle(), 0, 0, 0, 0, 0, 0, w, h );
00063             XRenderFreePicture( dpy, pic );
00064         }
00065         else
00066 #endif
00067         { // the normal X11 way
00068             GC gc = XCreateGC( dpy, pixmap, 0, NULL );
00069             if( depth == 1 )
00070             {
00071                 QBitmap bm( w, h );
00072                 XCopyArea( dpy, pixmap, bm.handle(), gc, 0, 0, w, h, 0, 0 );
00073                 pm = bm;
00074             }
00075             else // depth == pm.depth()
00076                 XCopyArea( dpy, pixmap, pm.handle(), gc, 0, 0, w, h, 0, 0 );
00077             XFreeGC( dpy, gc );
00078         }
00079 
00080         if( pixmap_mask != None )
00081         {
00082             QBitmap bm( w, h );
00083             bm.detach();
00084             GC gc = XCreateGC( dpy, pixmap_mask, 0, NULL );
00085             XCopyArea( dpy, pixmap_mask, bm.handle(), gc, 0, 0, w, h, 0, 0 );
00086             pm.setMask( bm );
00087             XFreeGC( dpy, gc );
00088         }
00089         if( !handler.error( true )) // sync, check for error
00090             return pm;
00091     }
00092     return QPixmap();
00093 }
00094 
00095 // Functions for X timestamp comparing. For Time being 32bit they're fairly simple
00096 // (the #if 0 part), but on 64bit architectures Time is 64bit unsigned long,
00097 // so there special care needs to be taken to always use only the lower 32bits.
00098 #if 0
00099 int timestampCompare( Time time1, Time time2 ) // like strcmp()
00100     {
00101     if( time1 == time2 )
00102         return 0;
00103     return ( time1 - time2 ) < 0x7fffffffU ? 1 : -1; // time1 > time2 -> 1, handle wrapping
00104     }
00105 
00106 Time timestampDiff( Time time1, Time time2 ) // returns time2 - time1
00107     { // no need to handle wrapping?
00108     return time2 - time1;
00109     }
00110 #else
00111 int timestampCompare( unsigned long time1_, unsigned long time2_ ) // like strcmp()
00112     {
00113     quint32 time1 = time1_;
00114     quint32 time2 = time2_;
00115     if( time1 == time2 )
00116         return 0;
00117     return quint32( time1 - time2 ) < 0x7fffffffU ? 1 : -1; // time1 > time2 -> 1, handle wrapping
00118     }
00119 
00120 int timestampDiff( unsigned long time1_, unsigned long time2_ ) // returns time2 - time1
00121     { // no need to handle wrapping?
00122     quint32 time1 = time1_;
00123     quint32 time2 = time2_;
00124     return quint32( time2 - time1 );
00125     }
00126 #endif
00127 
00128 
00129 } // namespace
00130 
00131 #endif

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal