khtml Library API Documentation

kjavaappletwidget.cpp

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2000 Richard Moore <rich@kde.org>
00004  *               2000 Wynn Wilkes <wynnw@caldera.com>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #include "kjavaappletwidget.h"
00023 #include "kjavaappletserver.h"
00024 
00025 #include <kwin.h>
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 
00029 #include <qmetaobject.h>
00030 #include <qlabel.h>
00031 
00032 #include <X11/X.h>
00033 #include <X11/Xlib.h>
00034 #include <X11/Xutil.h>
00035 #include <X11/Xatom.h>
00036 
00037 const int XFocusOut = FocusOut;
00038 const int XFocusIn = FocusIn;
00039 #undef FocusOut
00040 #undef FocusIn
00041 
00042 typedef int (*QX11EventFilter) (XEvent*);
00043 extern QX11EventFilter qt_set_x11_event_filter (QX11EventFilter filter);
00044 static QX11EventFilter oldFilter = 0;
00045 
00046 static int qxembed_x11_event_filter( XEvent* e)
00047 {
00048     switch ( e->type ) {
00049     case LeaveNotify: {
00050         QWidget* w = QWidget::find( e->xkey.window );
00051         if (w && w->metaObject()->inherits("KJavaAppletWidget"))
00052             XUngrabButton( qt_xdisplay(), AnyButton, AnyModifier, e->xany.window );
00053         break;
00054     }
00055     case EnterNotify: {
00056         QWidget* w = QWidget::find( e->xkey.window );
00057         if (w && w->metaObject()->inherits("KJavaAppletWidget") && !w->hasFocus())
00058             XGrabButton(qt_xdisplay(), AnyButton, AnyModifier, e->xany.window,
00059                     FALSE, ButtonPressMask, GrabModeSync, GrabModeAsync,
00060                     None, None );
00061         break;
00062     }
00063     }
00064     if ( oldFilter )
00065         return oldFilter( e );
00066     return FALSE;
00067 }
00068 
00069 
00070 // For future expansion
00071 class KJavaAppletWidgetPrivate
00072 {
00073 friend class KJavaAppletWidget;
00074 private:
00075     QLabel* tmplabel;
00076 };
00077 
00078 int KJavaAppletWidget::appletCount = 0;
00079 
00080 KJavaAppletWidget::KJavaAppletWidget( KJavaAppletContext* context,
00081                                       QWidget* parent, const char* name )
00082    : QXEmbed ( parent, name)
00083 {
00084     static bool initialized = false;
00085     if (!initialized) {
00086         oldFilter = qt_set_x11_event_filter( qxembed_x11_event_filter );
00087         initialized = true;
00088     }
00089     m_applet = new KJavaApplet( this, context );
00090     d        = new KJavaAppletWidgetPrivate;
00091     m_kwm    = new KWinModule( this );
00092 
00093     d->tmplabel = new QLabel( this );
00094     d->tmplabel->setText( KJavaAppletServer::getAppletLabel() );
00095     d->tmplabel->setAlignment( Qt::AlignCenter | Qt::WordBreak );
00096     d->tmplabel->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
00097     d->tmplabel->show();
00098 
00099     m_swallowTitle.sprintf( "KJAS Applet - Ticket number %u", appletCount++ );
00100     m_applet->setWindowName( m_swallowTitle );
00101 }
00102 
00103 KJavaAppletWidget::~KJavaAppletWidget()
00104 {
00105     delete m_applet;
00106     delete d;
00107 }
00108 
00109 void KJavaAppletWidget::showApplet()
00110 {
00111     connect( m_kwm, SIGNAL( windowAdded( WId ) ),
00112              this,  SLOT( setWindow( WId ) ) );
00113 
00114     m_kwm->doNotManage( m_swallowTitle );
00115 
00116     //Now we send applet info to the applet server
00117     if ( !m_applet->isCreated() )
00118         m_applet->create();
00119 }
00120 
00121 void KJavaAppletWidget::setWindow( WId w )
00122 {
00123     //make sure that this window has the right name, if so, embed it...
00124     KWin::Info w_info = KWin::info( w );
00125     if ( m_swallowTitle == w_info.name ||
00126          m_swallowTitle == w_info.visibleName )
00127     {
00128         kdDebug(6100) << "swallowing our window: " << m_swallowTitle
00129                       << ", window id = " << w << endl;
00130         delete d->tmplabel;
00131         d->tmplabel = 0;
00132 
00133         // disconnect from KWM events
00134         disconnect( m_kwm, SIGNAL( windowAdded( WId ) ),
00135                     this,  SLOT( setWindow( WId ) ) );
00136 
00137 
00138         embed( w );
00139         setFocus();
00140     }
00141 }
00142 
00143 QSize KJavaAppletWidget::sizeHint() const
00144 {
00145     kdDebug(6100) << "KJavaAppletWidget::sizeHint()" << endl;
00146     QSize rval = QXEmbed::sizeHint();
00147 
00148     if( rval.width() == 0 || rval.height() == 0 )
00149     {
00150         if( width() != 0 && height() != 0 )
00151         {
00152             rval = QSize( width(), height() );
00153         }
00154     }
00155 
00156     kdDebug(6100) << "returning: (" << rval.width() << ", " << rval.height() << ")" << endl;
00157 
00158     return rval;
00159 }
00160 
00161 void KJavaAppletWidget::resize( int w, int h )
00162 {
00163     kdDebug(6100) << "KJavaAppletWidget, id = " << m_applet->appletId() << ", ::resize to: " << w << ", " << h << endl;
00164 
00165     if( d->tmplabel )
00166     {
00167         d->tmplabel->resize( w, h );
00168         m_applet->setSize( QSize( w, h ) );
00169     }
00170 
00171     QXEmbed::resize( w, h );
00172 }
00173 
00174 void KJavaAppletWidget::focusInEvent( QFocusEvent * e ){
00175     WId window = embeddedWinId();
00176     if (!window)
00177         return;
00178     XUngrabButton( qt_xdisplay(), AnyButton, AnyModifier, window );
00179     XFocusInEvent inev = { XFocusIn, 0, TRUE, qt_xdisplay(), window, 
00180                            NotifyNormal, NotifyPointer };
00181     XSendEvent(qt_xdisplay(), window, TRUE, FocusChangeMask, (XEvent*) &inev);
00182     QXEmbed::focusInEvent(e);
00183 }
00184 
00187 void KJavaAppletWidget::focusOutEvent( QFocusEvent * e ) {
00188     if (!embeddedWinId())
00189         return;
00190     XFocusOutEvent outev = { XFocusOut, 0, TRUE, qt_xdisplay(), embeddedWinId(),
00191                              NotifyNormal, NotifyPointer };
00192     XSendEvent(qt_xdisplay(), embeddedWinId(), TRUE, FocusChangeMask, (XEvent*) &outev);
00193     QXEmbed::focusOutEvent(e);
00194 }
00195 
00196 bool KJavaAppletWidget::x11Event( XEvent* e )
00197 {
00198     switch ( e->type ) {
00199 
00200     case ButtonPress:
00201         QFocusEvent::setReason( QFocusEvent::Mouse );
00202         setFocus();
00203         QFocusEvent::resetReason();
00204         XAllowEvents(qt_xdisplay(), ReplayPointer, CurrentTime);
00205         return TRUE;
00206     case ButtonRelease:
00207         XAllowEvents(qt_xdisplay(), SyncPointer, CurrentTime);
00208         break;
00209     }
00210     return QXEmbed::x11Event(e);
00211 }
00212 
00213 #include "kjavaappletwidget.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:40 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001