00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include <qapplication.h>
00050 #include <qptrlist.h>
00051 #include <qptrdict.h>
00052 #include <qguardedptr.h>
00053 #include <qwhatsthis.h>
00054 #include <qfocusdata.h>
00055
00056
00057 #ifdef Q_WS_X11
00058
00059 # include <X11/X.h>
00060 # include <X11/Xlib.h>
00061 # include <X11/Xutil.h>
00062 # include <X11/Xatom.h>
00063 # define XK_MISCELLANY
00064 # define XK_LATIN1
00065 # include <X11/keysymdef.h>
00066 # include <kdebug.h>
00067 # include <kxerrorhandler.h>
00068
00069
00070 # include <config.h>
00071 # ifdef HAVE_UNISTD_H
00072 # include <unistd.h>
00073 # ifdef HAVE_USLEEP
00074 # define USLEEP(x) usleep(x)
00075 # else
00076 # define USLEEP(x) sleep(0)
00077 # endif
00078 # else
00079 # define USLEEP(x) sleep(0)
00080 # endif
00081
00082 # include "qxembed.h"
00083
00084
00085 # ifndef XK_ISO_Left_Tab
00086 # define XK_ISO_Left_Tab 0xFE20
00087 # endif
00088
00089
00090 const int XFocusOut = FocusOut;
00091 const int XFocusIn = FocusIn;
00092 const int XKeyPress = KeyPress;
00093 const int XKeyRelease = KeyRelease;
00094 # undef KeyRelease
00095 # undef KeyPress
00096 # undef FocusOut
00097 # undef FocusIn
00098
00099
00100 extern Atom qt_wm_protocols;
00101 extern Atom qt_wm_delete_window;
00102 extern Atom qt_wm_take_focus;
00103 extern Atom qt_wm_state;
00104 extern Time qt_x_time;
00105
00106
00107 static Atom xembed = 0;
00108 static Atom context_help = 0;
00109
00110
00111 #define XEMBED_EMBEDDED_NOTIFY 0
00112 #define XEMBED_WINDOW_ACTIVATE 1
00113 #define XEMBED_WINDOW_DEACTIVATE 2
00114 #define XEMBED_REQUEST_FOCUS 3
00115 #define XEMBED_FOCUS_IN 4
00116 #define XEMBED_FOCUS_OUT 5
00117 #define XEMBED_FOCUS_NEXT 6
00118 #define XEMBED_FOCUS_PREV 7
00119
00120
00121
00122 #define XEMBED_FOCUS_CURRENT 0
00123 #define XEMBED_FOCUS_FIRST 1
00124 #define XEMBED_FOCUS_LAST 2
00125
00126
00127
00128
00129 class QXEmbedData
00130 {
00131 public:
00132 QXEmbedData(){
00133 autoDelete = true;
00134 xplain = false;
00135 xgrab = false;
00136 mapAfterRelease = false;
00137 lastPos = QPoint(0,0);
00138 }
00139 ~QXEmbedData(){};
00140
00141 bool autoDelete;
00142 bool xplain;
00143 bool xgrab;
00144 bool mapAfterRelease;
00145 QWidget* focusProxy;
00146 QPoint lastPos;
00147 };
00148
00149 namespace
00150 {
00151
00152
00153 class QXEmbedAppFilter : public QObject
00154 {
00155 public:
00156 QXEmbedAppFilter() { qApp->installEventFilter( this ); }
00157 ~QXEmbedAppFilter() { };
00158 bool eventFilter( QObject *, QEvent * );
00159 };
00160 }
00161
00162
00163 static QXEmbedAppFilter* filter = 0;
00164
00165 static QPtrDict<QGuardedPtr<QWidget> > *focusMap = 0;
00166
00167 static XKeyEvent last_key_event;
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 class QPublicWidget : public QWidget
00178 {
00179 public:
00180 QTLWExtra* topData() { return QWidget::topData(); }
00181 QFocusData *focusData(){ return QWidget::focusData(); }
00182 bool focusNextPrev(bool b) { return focusNextPrevChild(b); }
00183 };
00184
00185
00186
00187 typedef int (*QX11EventFilter) (XEvent*);
00188 extern QX11EventFilter qt_set_x11_event_filter (QX11EventFilter filter);
00189 static QX11EventFilter oldFilter = 0;
00190
00191
00192
00193 static void sendXEmbedMessage( WId window, long message, long detail = 0,
00194 long data1 = 0, long data2 = 0)
00195 {
00196 if (!window) return;
00197 XEvent ev;
00198 memset(&ev, 0, sizeof(ev));
00199 ev.xclient.type = ClientMessage;
00200 ev.xclient.window = window;
00201 ev.xclient.message_type = xembed;
00202 ev.xclient.format = 32;
00203 ev.xclient.data.l[0] = qt_x_time;
00204 ev.xclient.data.l[1] = message;
00205 ev.xclient.data.l[2] = detail;
00206 ev.xclient.data.l[3] = data1;
00207 ev.xclient.data.l[4] = data2;
00208 XSendEvent(qt_xdisplay(), window, false, NoEventMask, &ev);
00209 }
00210
00211
00212
00213 static void sendClientMessage(Window window, Atom a, long x)
00214 {
00215 if (!window) return;
00216 XEvent ev;
00217 memset(&ev, 0, sizeof(ev));
00218 ev.xclient.type = ClientMessage;
00219 ev.xclient.window = window;
00220 ev.xclient.message_type = a;
00221 ev.xclient.format = 32;
00222 ev.xclient.data.l[0] = x;
00223 ev.xclient.data.l[1] = qt_x_time;
00224 XSendEvent(qt_xdisplay(), window, false, NoEventMask, &ev);
00225 }
00226
00227
00228
00229 static void sendFocusMessage(Window window, int type, int mode, int detail)
00230 {
00231 if (!window) return;
00232 XEvent ev;
00233 memset(&ev, 0, sizeof(ev));
00234 ev.xfocus.type = type;
00235 ev.xfocus.window = window;
00236 ev.xfocus.mode = mode;
00237 ev.xfocus.detail = detail;
00238 XSendEvent(qt_xdisplay(), window, false, FocusChangeMask, &ev);
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 bool QXEmbedAppFilter::eventFilter( QObject *o, QEvent * e)
00301 {
00302 static bool obeyFocus = false;
00303 switch ( e->type() ) {
00304 case QEvent::MouseButtonPress:
00305
00306 if ( !((QWidget*)o)->isActiveWindow() )
00307 obeyFocus = true;
00308 break;
00309 case QEvent::FocusIn:
00310
00311
00312
00313
00314 if ( qApp->focusWidget() == o &&
00315 ((QPublicWidget*)qApp->focusWidget()->topLevelWidget())->topData()->embedded ) {
00316 QFocusEvent* fe = (QFocusEvent*) e;
00317 if ( obeyFocus || fe->reason() != QFocusEvent::ActiveWindow
00318 ) {
00319
00320
00321
00322
00323 WId window = ((QPublicWidget*)qApp->focusWidget()->topLevelWidget())->topData()->parentWinId;
00324 focusMap->remove( qApp->focusWidget()->topLevelWidget() );
00325 sendXEmbedMessage( window, XEMBED_REQUEST_FOCUS );
00326 } else if ( fe->reason() == QFocusEvent::ActiveWindow ) {
00327
00328
00329
00330
00331
00332 focusMap->remove( qApp->focusWidget()->topLevelWidget() );
00333 focusMap->insert( qApp->focusWidget()->topLevelWidget(),
00334 new QGuardedPtr<QWidget>(qApp->focusWidget()->topLevelWidget()->focusWidget() ) );
00335
00336
00337 qApp->focusWidget()->clearFocus();
00338
00339 }
00340 obeyFocus = false;
00341 }
00342 break;
00343 case QEvent::KeyPress:
00344 if (qApp->focusWidget() == o &&
00345 ((QPublicWidget*)qApp->focusWidget()->topLevelWidget())->topData()->embedded ) {
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 QKeyEvent *k = (QKeyEvent *)e;
00359 QWidget *w = qApp->focusWidget();
00360
00361 bool res = false;
00362 bool tabForward = true;
00363 if ( !(k->state() & ControlButton || k->state() & AltButton) ) {
00364 if ( k->key() == Key_Backtab || (k->key() == Key_Tab && (k->state() & ShiftButton)) ) {
00365 QFocusEvent::setReason( QFocusEvent::Backtab );
00366 res = ((QPublicWidget*)w)->focusNextPrev( tabForward = false );
00367 QFocusEvent::resetReason();
00368 } else if ( k->key() == Key_Tab ) {
00369 QFocusEvent::setReason( QFocusEvent::Tab );
00370 res = ((QPublicWidget*)w)->focusNextPrev( tabForward = true );
00371 QFocusEvent::resetReason();
00372 }
00373 }
00374 if (res) {
00375
00376
00377 QFocusData *fd = ((QPublicWidget*)w)->focusData();
00378 WId window = ((QPublicWidget*)w->topLevelWidget())->topData()->parentWinId;
00379 QWidget *cw = 0;
00380 QWidget *fw = fd->home();
00381 if (tabForward && window) {
00382 while (cw != w && cw != fw && cw != w->topLevelWidget())
00383 cw = fd->prev();
00384 if (cw != w)
00385 sendXEmbedMessage( window, XEMBED_FOCUS_NEXT );
00386 } else if (window) {
00387 while (cw != w && cw != fw && cw != w->topLevelWidget())
00388 cw = fd->next();
00389 if (cw != w)
00390 sendXEmbedMessage( window, XEMBED_FOCUS_PREV );
00391 }
00392
00393 return true;
00394 }
00395 }
00396 break;
00397 default:
00398 break;
00399 }
00400
00401 return false;
00402 }
00403
00404
00405
00406 static int qxembed_x11_event_filter( XEvent* e)
00407 {
00408 switch ( e->type ) {
00409 case XKeyPress:
00410 case XKeyRelease: {
00411
00412 last_key_event = e->xkey;
00413 break;
00414 }
00415 case ClientMessage:
00416 if ( e->xclient.message_type == xembed ) {
00417
00418
00419 Time msgtime = (Time) e->xclient.data.l[0];
00420 long message = e->xclient.data.l[1];
00421 long detail = e->xclient.data.l[2];
00422
00423 if ( msgtime > qt_x_time )
00424 qt_x_time = msgtime;
00425 QWidget* w = QWidget::find( e->xclient.window );
00426 if ( !w )
00427 break;
00428 switch ( message) {
00429 case XEMBED_EMBEDDED_NOTIFY: {
00430
00431 QTLWExtra *extra = ((QPublicWidget*)w->topLevelWidget())->topData();
00432 extra->embedded = 1;
00433 extra->parentWinId = e->xclient.data.l[3];
00434 w->topLevelWidget()->show();
00435 break;
00436 }
00437 case XEMBED_WINDOW_ACTIVATE: {
00438
00439
00440
00441
00442
00443 XEvent ev;
00444 memset(&ev, 0, sizeof(ev));
00445 ev.xfocus.display = qt_xdisplay();
00446 ev.xfocus.type = XFocusIn;
00447 ev.xfocus.window = w->topLevelWidget()->winId();
00448 ev.xfocus.mode = NotifyNormal;
00449 ev.xfocus.detail = NotifyAncestor;
00450 qApp->x11ProcessEvent( &ev );
00451 }
00452 break;
00453 case XEMBED_WINDOW_DEACTIVATE: {
00454
00455
00456
00457 XEvent ev;
00458 memset(&ev, 0, sizeof(ev));
00459 ev.xfocus.display = qt_xdisplay();
00460 ev.xfocus.type = XFocusOut;
00461 ev.xfocus.window = w->topLevelWidget()->winId();
00462 ev.xfocus.mode = NotifyNormal;
00463 ev.xfocus.detail = NotifyAncestor;
00464 qApp->x11ProcessEvent( &ev );
00465 }
00466 break;
00467 case XEMBED_FOCUS_IN:
00468
00469 {
00470
00471 QWidget* focusCurrent = 0;
00472 QGuardedPtr<QWidget>* fw = focusMap->find( w->topLevelWidget() );
00473 if ( fw ) {
00474 focusCurrent = *fw;
00475
00476 focusMap->remove( w->topLevelWidget() );
00477 }
00478 switch ( detail ) {
00479 case XEMBED_FOCUS_CURRENT:
00480
00481 if ( focusCurrent ) {
00482 focusCurrent->setFocus();
00483 if( QXEmbed* emb = dynamic_cast< QXEmbed* >( focusCurrent ))
00484 emb->updateEmbeddedFocus( true );
00485 }
00486 else if ( !w->topLevelWidget()->focusWidget() )
00487 w->topLevelWidget()->setFocus();
00488 break;
00489 case XEMBED_FOCUS_FIRST:
00490 {
00491
00492 QFocusEvent::setReason( QFocusEvent::Tab );
00493 w->topLevelWidget()->setFocus();
00494 ((QPublicWidget*)w->topLevelWidget())->focusNextPrev(true);
00495 QFocusEvent::resetReason();
00496 }
00497 break;
00498 case XEMBED_FOCUS_LAST:
00499 {
00500
00501 QFocusEvent::setReason( QFocusEvent::Backtab );
00502 w->topLevelWidget()->setFocus();
00503 ((QPublicWidget*)w->topLevelWidget())->focusNextPrev(false);
00504 QFocusEvent::resetReason();
00505 }
00506 break;
00507 default:
00508 break;
00509 }
00510 }
00511 break;
00512 case XEMBED_FOCUS_OUT:
00513
00514
00515
00516 if ( w->topLevelWidget()->focusWidget() ) {
00517 if( QXEmbed* emb = dynamic_cast< QXEmbed* >( w->topLevelWidget()->focusWidget()))
00518 emb->updateEmbeddedFocus( false );
00519 focusMap->insert( w->topLevelWidget(),
00520 new QGuardedPtr<QWidget>(w->topLevelWidget()->focusWidget() ) );
00521 w->topLevelWidget()->focusWidget()->clearFocus();
00522 }
00523 break;
00524 default:
00525 break;
00526 }
00527 } else if ( e->xclient.format == 32 && e->xclient.message_type ) {
00528 if ( e->xclient.message_type == qt_wm_protocols ) {
00529 QWidget* w = QWidget::find( e->xclient.window );
00530 if ( !w )
00531 break;
00532
00533
00534
00535
00536
00537
00538
00539 Atom a = e->xclient.data.l[0];
00540 if ( a == qt_wm_take_focus ) {
00541
00542 if ( (ulong) e->xclient.data.l[1] > qt_x_time )
00543 qt_x_time = e->xclient.data.l[1];
00544
00545
00546
00547
00548 if ( w->isActiveWindow() ) {
00549 QEvent e( QEvent::WindowActivate );
00550 QApplication::sendEvent( w, &e );
00551 }
00552 }
00553 }
00554 }
00555 break;
00556 default:
00557 break;
00558 }
00559
00560 if ( oldFilter )
00561 return oldFilter( e );
00562
00563 return false;
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573 void QXEmbed::initialize()
00574 {
00575 static bool is_initialized = false;
00576 if ( is_initialized )
00577 return;
00578
00579
00580 xembed = XInternAtom( qt_xdisplay(), "_XEMBED", false );
00581
00582 oldFilter = qt_set_x11_event_filter( qxembed_x11_event_filter );
00583
00584 focusMap = new QPtrDict<QGuardedPtr<QWidget> >;
00585 focusMap->setAutoDelete( true );
00586
00587 filter = new QXEmbedAppFilter;
00588
00589 is_initialized = true;
00590 }
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613 QXEmbed::QXEmbed(QWidget *parent, const char *name, WFlags f)
00614 : QWidget(parent, name, f)
00615 {
00616
00617 d = new QXEmbedData;
00618
00619
00620
00621
00622
00623
00624 d->focusProxy = new QWidget( topLevelWidget(), "xembed_focus" );
00625 d->focusProxy->setGeometry( -1, -1, 1, 1 );
00626 d->focusProxy->show();
00627
00628 QApplication::sendPostedEvents( d->focusProxy, 0 );
00629
00630
00631
00632 initialize();
00633 window = 0;
00634 setFocusPolicy(StrongFocus);
00635 setKeyCompression( false );
00636
00637
00638 (void) topData();
00639
00640
00641
00642
00643 XSelectInput(qt_xdisplay(), winId(),
00644 KeyPressMask | KeyReleaseMask |
00645 ButtonPressMask | ButtonReleaseMask |
00646 KeymapStateMask |
00647 ButtonMotionMask |
00648 PointerMotionMask |
00649 EnterWindowMask | LeaveWindowMask |
00650 FocusChangeMask |
00651 ExposureMask |
00652 StructureNotifyMask |
00653 SubstructureRedirectMask |
00654 SubstructureNotifyMask
00655 );
00656
00657
00658
00659 topLevelWidget()->installEventFilter( this );
00660 qApp->installEventFilter( this );
00661
00662
00663
00664 if ( qApp->activeWindow() == topLevelWidget() )
00665 if ( !((QPublicWidget*) topLevelWidget())->topData()->embedded )
00666 XSetInputFocus( qt_xdisplay(), d->focusProxy->winId(),
00667 RevertToParent, qt_x_time );
00668
00669 setAcceptDrops( true );
00670 }
00671
00672
00673 QXEmbed::~QXEmbed()
00674 {
00675
00676 if ( d && d->xgrab)
00677 XUngrabButton( qt_xdisplay(), AnyButton, AnyModifier, winId() );
00678 if ( window && ( autoDelete() || !d->xplain ))
00679 {
00680
00681
00682
00683 #if 0
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 #else
00695 if( autoDelete())
00696 XUnmapWindow( qt_xdisplay(), window );
00697 #endif
00698 XReparentWindow(qt_xdisplay(), window, qt_xrootwin(), 0, 0);
00699 if( !d->xplain )
00700 XRemoveFromSaveSet( qt_xdisplay(), window );
00701 if( d->mapAfterRelease )
00702 XMapWindow( qt_xdisplay(), window );
00703 XSync(qt_xdisplay(), false);
00704
00705 if( autoDelete() )
00706
00707
00708
00709
00710 sendDelete();
00711 }
00712 window = 0;
00713
00714
00715 Window focus;
00716 int revert;
00717 XGetInputFocus( qt_xdisplay(), &focus, &revert );
00718 if( focus == d->focusProxy->winId())
00719 XSetInputFocus( qt_xdisplay(), topLevelWidget()->winId(), RevertToParent, qt_x_time );
00720
00721 delete d;
00722 }
00723
00724
00725
00726
00727
00728 void QXEmbed::sendDelete( void )
00729 {
00730 if (window)
00731 {
00732 sendClientMessage(window, qt_wm_protocols, qt_wm_delete_window);
00733 XFlush( qt_xdisplay() );
00734 }
00735 }
00736
00737
00738
00739
00740
00741
00742
00743
00744 void QXEmbed::setProtocol( Protocol proto )
00745 {
00746 if (!window) {
00747 d->xplain = false;
00748 if (proto == XPLAIN)
00749 d->xplain = true;
00750 }
00751 }
00752
00753
00754 QXEmbed::Protocol QXEmbed::protocol()
00755 {
00756 if (d->xplain)
00757 return XPLAIN;
00758 return XEMBED;
00759 }
00760
00761
00762
00763 void QXEmbed::resizeEvent(QResizeEvent*)
00764 {
00765 if (window)
00766 XResizeWindow(qt_xdisplay(), window, width(), height());
00767 }
00768
00769
00770 void QXEmbed::showEvent(QShowEvent*)
00771 {
00772 if (window)
00773 XMapRaised(qt_xdisplay(), window);
00774 }
00775
00776
00777
00778 bool QXEmbed::eventFilter( QObject *o, QEvent * e)
00779 {
00780
00781 switch ( e->type() ) {
00782 case QEvent::WindowActivate:
00783 if ( o == topLevelWidget() ) {
00784
00785
00786 if ( !((QPublicWidget*) topLevelWidget())->topData()->embedded )
00787 if (! hasFocus() )
00788 XSetInputFocus( qt_xdisplay(), d->focusProxy->winId(),
00789 RevertToParent, qt_x_time );
00790 if (d->xplain)
00791
00792 checkGrab();
00793 else
00794
00795 sendXEmbedMessage( window, XEMBED_WINDOW_ACTIVATE );
00796 }
00797 break;
00798 case QEvent::WindowDeactivate:
00799 if ( o == topLevelWidget() ) {
00800 if (d->xplain)
00801
00802 checkGrab();
00803 else
00804
00805 sendXEmbedMessage( window, XEMBED_WINDOW_DEACTIVATE );
00806 }
00807 break;
00808 case QEvent::Move:
00809 {
00810 QWidget* pos = this;
00811 while( pos != o && pos != topLevelWidget())
00812 pos = pos->parentWidget();
00813 if( pos == o ) {
00814
00815
00816 QPoint globalPos = mapToGlobal(QPoint(0,0));
00817 if (globalPos != d->lastPos) {
00818 d->lastPos = globalPos;
00819 sendSyntheticConfigureNotifyEvent();
00820 }
00821 }
00822 }
00823 break;
00824 default:
00825 break;
00826 }
00827 return false;
00828 }
00829
00830
00831 bool QXEmbed::event( QEvent * e)
00832 {
00833 return QWidget::event( e );
00834 }
00835
00836
00837
00838
00839
00840
00841
00842 void QXEmbed::keyPressEvent( QKeyEvent *)
00843 {
00844 if (!window)
00845 return;
00846 last_key_event.window = window;
00847 XSendEvent(qt_xdisplay(), window, false, KeyPressMask, (XEvent*)&last_key_event);
00848
00849 }
00850
00851
00852
00853 void QXEmbed::keyReleaseEvent( QKeyEvent *)
00854 {
00855 if (!window)
00856 return;
00857 last_key_event.window = window;
00858 XSendEvent(qt_xdisplay(), window, false, KeyReleaseMask, (XEvent*)&last_key_event);
00859 }
00860
00861
00862 void QXEmbed::focusInEvent( QFocusEvent * e ){
00863 if (!window)
00864 return;
00865
00866
00867 if ( !((QPublicWidget*) topLevelWidget())->topData()->embedded )
00868 if ( qApp->activeWindow() == topLevelWidget() )
00869
00870
00871
00872 XSetInputFocus( qt_xdisplay(), d->focusProxy->winId(),
00873 RevertToParent, qt_x_time );
00874 if (d->xplain) {
00875
00876 checkGrab();
00877
00878
00879
00880 sendFocusMessage(window, XFocusIn, NotifyNormal, NotifyPointer );
00881 } else {
00882
00883
00884 int detail = XEMBED_FOCUS_CURRENT;
00885
00886
00887
00888 if ( e->reason() == QFocusEvent::Tab )
00889 detail = XEMBED_FOCUS_FIRST;
00890 else if ( e->reason() == QFocusEvent::Backtab )
00891 detail = XEMBED_FOCUS_LAST;
00892 sendXEmbedMessage( window, XEMBED_FOCUS_IN, detail);
00893 }
00894 }
00895
00896
00897 void QXEmbed::focusOutEvent( QFocusEvent * ){
00898 if (!window)
00899 return;
00900 if (d->xplain) {
00901
00902 checkGrab();
00903
00904 sendFocusMessage(window, XFocusOut, NotifyNormal, NotifyPointer );
00905 } else {
00906
00907 sendXEmbedMessage( window, XEMBED_FOCUS_OUT );
00908 }
00909
00910
00911
00912
00913
00914
00915
00916 if ( !((QPublicWidget*) topLevelWidget())->topData()->embedded )
00917 if ( qApp->activeWindow() == topLevelWidget() )
00918
00919
00920
00921
00922 XSetInputFocus( qt_xdisplay(), d->focusProxy->winId(),
00923 RevertToParent, qt_x_time );
00924 }
00925
00926
00927
00928
00929 void QXEmbed::updateEmbeddedFocus( bool hasfocus ){
00930 if (!window || d->xplain)
00931 return;
00932 if( hasfocus )
00933 sendXEmbedMessage( window, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT);
00934 else
00935 sendXEmbedMessage( window, XEMBED_FOCUS_OUT);
00936 }
00937
00938
00939
00940 static bool wstate_withdrawn( WId winid )
00941 {
00942 Atom type;
00943 int format;
00944 unsigned long length, after;
00945 unsigned char *data;
00946 int r = XGetWindowProperty( qt_xdisplay(), winid, qt_wm_state, 0, 2,
00947 false, AnyPropertyType, &type, &format,
00948 &length, &after, &data );
00949 bool withdrawn = true;
00950
00951
00952 if ( r == Success && data && format == 32 ) {
00953 Q_UINT32 *wstate = (Q_UINT32*)data;
00954 withdrawn = (*wstate == WithdrawnState );
00955 XFree( (char *)data );
00956 }
00957 return withdrawn;
00958 }
00959
00960
00961
00962 static int get_parent(WId winid, Window *out_parent)
00963 {
00964 Window root, *children=0;
00965 unsigned int nchildren;
00966 int st = XQueryTree(qt_xdisplay(), winid, &root, out_parent, &children, &nchildren);
00967 if (st && children)
00968 XFree(children);
00969 return st;
00970 }
00971
00972
00973
00974 void QXEmbed::embed(WId w)
00975 {
00976 kdDebug() << "*** Embed " << w << " into " << winId() << ". window=" << window << endl;
00977 if (!w)
00978 return;
00979
00980
00981 bool has_window = (w == window);
00982 window = w;
00983 if ( !has_window ) {
00984 KXErrorHandler errhandler;
00985
00986
00987
00988 if ( !wstate_withdrawn(window) ) {
00989 XWithdrawWindow(qt_xdisplay(), window, qt_xscreen());
00990 QApplication::flushX();
00991
00992 while (!wstate_withdrawn(window))
00993 USLEEP(1000);
00994 }
00995
00996
00997
00998
00999 Window parent = 0;
01000 get_parent(w, &parent);
01001 kdDebug() << QString("> before reparent: parent=0x%1").arg(parent,0,16) << endl;
01002 for (int i = 0; i < 50; i++) {
01003
01004
01005 if( !d->xplain )
01006 XAddToSaveSet( qt_xdisplay(), w );
01007 XReparentWindow(qt_xdisplay(), w, winId(), 0, 0);
01008 if (get_parent(w, &parent) && parent == winId()) {
01009 kdDebug() << QString("> Loop %1: ").arg(i)
01010 << QString("> reparent of 0x%1").arg(w,0,16)
01011 << QString(" into 0x%1").arg(winId(),0,16)
01012 << QString(" successful") << endl;
01013 break;
01014 }
01015 kdDebug() << QString("> Loop %1: ").arg(i)
01016 << QString("> reparent of 0x%1").arg(w,0,16)
01017 << QString(" into 0x%1").arg(winId(),0,16)
01018 << QString(" failed") << endl;
01019 USLEEP(1000);
01020 }
01021 if( parent != winId())
01022 window = 0;
01023 }
01024 }
01025
01026
01027
01028 WId QXEmbed::embeddedWinId() const
01029 {
01030 return window;
01031 }
01032
01033
01034
01035
01036 bool QXEmbed::focusNextPrevChild( bool next )
01037 {
01038 if ( window )
01039
01040
01041
01042
01043
01044
01045 return false;
01046 else
01047
01048 return QWidget::focusNextPrevChild( next );
01049 }
01050
01051
01052
01053 bool QXEmbed::x11Event( XEvent* e)
01054 {
01055 switch ( e->type ) {
01056 case DestroyNotify:
01057 if ( e->xdestroywindow.window == window ) {
01058
01059 window = 0;
01060 windowChanged( window );
01061 emit embeddedWindowDestroyed();
01062 }
01063 break;
01064 case ReparentNotify:
01065 if ( e->xreparent.window == d->focusProxy->winId() )
01066 break;
01067 if ( window && e->xreparent.window == window &&
01068 e->xreparent.parent != winId() ) {
01069
01070 window = 0;
01071 windowChanged( window );
01072 emit embeddedWindowDestroyed();
01073
01074
01075
01076 if( !d->xplain )
01077 XRemoveFromSaveSet( qt_xdisplay(), window );
01078 } else if ( e->xreparent.parent == winId()){
01079
01080 window = e->xreparent.window;
01081
01082
01083 if( !d->xplain )
01084 XAddToSaveSet( qt_xdisplay(), window );
01085 XResizeWindow(qt_xdisplay(), window, width(), height());
01086 XMapRaised(qt_xdisplay(), window);
01087
01088 sendSyntheticConfigureNotifyEvent();
01089
01090 extraData()->xDndProxy = window;
01091 if ( parent() ) {
01092
01093
01094 QEvent * layoutHint = new QEvent( QEvent::LayoutHint );
01095 QApplication::postEvent( parent(), layoutHint );
01096 }
01097 windowChanged( window );
01098 if (d->xplain) {
01099
01100 checkGrab();
01101 if ( hasFocus() )
01102
01103 sendFocusMessage(window, XFocusIn, NotifyNormal, NotifyPointer );
01104 } else {
01105
01106 sendXEmbedMessage( window, XEMBED_EMBEDDED_NOTIFY, 0, (long) winId() );
01107 if (isActiveWindow())
01108 sendXEmbedMessage( window, XEMBED_WINDOW_ACTIVATE);
01109 else
01110 sendXEmbedMessage( window, XEMBED_WINDOW_DEACTIVATE);
01111 if ( hasFocus() )
01112 sendXEmbedMessage( window, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT );
01113 else
01114 sendXEmbedMessage( window, XEMBED_FOCUS_OUT );
01115 }
01116 }
01117 break;
01118 case ButtonPress:
01119 if (d->xplain && d->xgrab) {
01120
01121
01122 QFocusEvent::setReason( QFocusEvent::Mouse );
01123 setFocus();
01124 QFocusEvent::resetReason();
01125
01126 XAllowEvents(qt_xdisplay(), ReplayPointer, CurrentTime);
01127
01128 return true;
01129 }
01130 break;
01131 case ButtonRelease:
01132 if (d->xplain && d->xgrab) {
01133
01134 XAllowEvents(qt_xdisplay(), SyncPointer, CurrentTime);
01135 return true;
01136 }
01137 break;
01138 case MapRequest:
01139
01140 if ( window && e->xmaprequest.window == window )
01141 XMapRaised(qt_xdisplay(), window );
01142 break;
01143 case ClientMessage:
01144
01145
01146 if ( e->xclient.format == 32 && e->xclient.message_type == xembed ) {
01147 long message = e->xclient.data.l[1];
01148 switch ( message ) {
01149
01150
01151
01152 case XEMBED_FOCUS_NEXT:
01153 QWidget::focusNextPrevChild( true );
01154 break;
01155 case XEMBED_FOCUS_PREV:
01156 QWidget::focusNextPrevChild( false );
01157 break;
01158
01159 case XEMBED_REQUEST_FOCUS:
01160 if( ((QPublicWidget*)topLevelWidget())->topData()->embedded ) {
01161 focusMap->remove( topLevelWidget() );
01162 focusMap->insert( topLevelWidget(), new QGuardedPtr<QWidget>( this ));
01163 WId window = ((QPublicWidget*)topLevelWidget())->topData()->parentWinId;
01164 sendXEmbedMessage( window, XEMBED_REQUEST_FOCUS );
01165 } else {
01166 QFocusEvent::setReason( QFocusEvent::Mouse );
01167 setFocus();
01168 QFocusEvent::resetReason();
01169 }
01170 break;
01171 default:
01172 break;
01173 }
01174 }
01175 break;
01176
01177 case ConfigureRequest:
01178
01179
01180 if (e->xconfigurerequest.window == window)
01181 {
01182 sendSyntheticConfigureNotifyEvent();
01183 }
01184 break;
01185 case MotionNotify:
01186
01187 case EnterNotify:
01188
01189 if ( QWhatsThis::inWhatsThisMode() )
01190 enterWhatsThisMode();
01191 break;
01192 default:
01193 break;
01194 }
01195 return false;
01196 }
01197
01198
01199
01200
01201 void QXEmbed::enterWhatsThisMode()
01202 {
01203
01204
01205
01206
01207 QWhatsThis::leaveWhatsThisMode();
01208 if ( !context_help )
01209 context_help = XInternAtom( x11Display(), "_NET_WM_CONTEXT_HELP", false );
01210 sendClientMessage(window , qt_wm_protocols, context_help );
01211 }
01212
01213
01214
01215 void QXEmbed::windowChanged( WId )
01216 {
01217 }
01218
01219
01220
01221
01222 bool QXEmbed::processClientCmdline( QWidget* client, int& argc, char ** argv )
01223 {
01224 int myargc = argc;
01225 WId window = 0;
01226 int i, j;
01227
01228 j = 1;
01229 for ( i=1; i<myargc; i++ ) {
01230 if ( argv[i] && *argv[i] != '-' ) {
01231 argv[j++] = argv[i];
01232 continue;
01233 }
01234 QCString arg = argv[i];
01235 if ( !strcmp(arg,"-embed") && i < myargc-1 ) {
01236 QCString s = argv[++i];
01237 window = s.toInt();
01238 } else
01239 argv[j++] = argv[i];
01240 }
01241 argc = j;
01242
01243 if ( window ) {
01244 embedClientIntoWindow( client, window );
01245 return true;
01246 }
01247
01248 return false;
01249 }
01250
01251
01252
01253
01254 void QXEmbed::embedClientIntoWindow(QWidget* client, WId window)
01255 {
01256 initialize();
01257 XReparentWindow(qt_xdisplay(), client->winId(), window, 0, 0);
01258
01259 ((QXEmbed*)client)->topData()->embedded = true;
01260 ((QXEmbed*)client)->topData()->parentWinId = window;
01261
01262
01263 client->show();
01264 }
01265
01266
01267
01268
01269
01270 QSizePolicy QXEmbed::sizePolicy() const
01271 {
01272 return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
01273 }
01274
01275
01276
01277 QSize QXEmbed::sizeHint() const
01278 {
01279 return minimumSizeHint();
01280 }
01281
01282
01283 QSize QXEmbed::minimumSizeHint() const
01284 {
01285 int minw = 0;
01286 int minh = 0;
01287 if ( window ) {
01288 XSizeHints size;
01289 long msize;
01290 if (XGetWMNormalHints(qt_xdisplay(), window, &size, &msize)
01291 && ( size.flags & PMinSize) ) {
01292 minw = size.min_width;
01293 minh = size.min_height;
01294 }
01295 }
01296
01297 return QSize( minw, minh );
01298 }
01299
01300
01301
01302 void QXEmbed::setAutoDelete( bool b)
01303 {
01304 d->autoDelete = b;
01305 }
01306
01307
01308 bool QXEmbed::autoDelete() const
01309 {
01310 return d->autoDelete;
01311 }
01312
01313
01314 bool QXEmbed::customWhatsThis() const
01315 {
01316 return true;
01317 }
01318
01319
01320
01321
01322
01323
01324 void QXEmbed::checkGrab()
01325 {
01326 if (d->xplain && isActiveWindow() && !hasFocus()) {
01327 if (! d->xgrab)
01328 XGrabButton(qt_xdisplay(), AnyButton, AnyModifier, winId(),
01329 false, ButtonPressMask, GrabModeSync, GrabModeAsync,
01330 None, None );
01331 d->xgrab = true;
01332 } else {
01333 if (d->xgrab)
01334 XUngrabButton( qt_xdisplay(), AnyButton, AnyModifier, winId() );
01335 d->xgrab = false;
01336 }
01337 }
01338
01339
01340
01341 void QXEmbed::sendSyntheticConfigureNotifyEvent()
01342 {
01343
01344
01345
01346 QPoint globalPos = mapToGlobal(QPoint(0,0));
01347 if (window) {
01348 XConfigureEvent c;
01349 memset(&c, 0, sizeof(c));
01350 c.type = ConfigureNotify;
01351 c.display = qt_xdisplay();
01352 c.send_event = True;
01353 c.event = window;
01354 c.window = winId();
01355 c.x = globalPos.x();
01356 c.y = globalPos.y();
01357 c.width = width();
01358 c.height = height();
01359 c.border_width = 0;
01360 c.above = None;
01361 c.override_redirect = 0;
01362 XSendEvent( qt_xdisplay(), c.event, true, StructureNotifyMask, (XEvent*)&c );
01363 }
01364 }
01365
01366
01367 void QXEmbed::reparent( QWidget * parent, WFlags f, const QPoint & p, bool showIt )
01368 {
01369
01370
01371
01372
01373 Q_ASSERT( !window );
01374 QWidget::reparent( parent, f, p, showIt );
01375 }
01376
01377
01378 #include "qxembed.moc"
01379 #endif // Q_WS_X11