📄 qx11embed_x11.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qx11embed_x11.h"#include <qapplication.h>#include <qevent.h>#include <qpainter.h>#include <qlayout.h>#include <qstyle.h>#include <qstyleoption.h>#include <qdatetime.h>#include <qpointer.h>#include <qdebug.h>#include <qx11info_x11.h>#include <private/qt_x11_p.h>#include <private/qwidget_p.h>#define XK_MISCELLANY#define XK_LATIN1#define None 0#include <X11/Xlib.h>#include <X11/Xatom.h>#include <X11/Xutil.h>#include <X11/keysymdef.h>#include <X11/X.h>#ifndef XK_ISO_Left_Tab#define XK_ISO_Left_Tab 0xFE20#endif//#define QX11EMBED_DEBUG#ifdef QX11EMBED_DEBUG#include <qdebug.h>#endif/*! \class QX11EmbedWidget \brief The QX11EmbedWidget class provides an XEmbed client widget. XEmbed is an X11 protocol that supports the embedding of a widget from one application into another application. An XEmbed \e{client widget} is a window that is embedded into a \e container. A container is the graphical location that embeds (or \e swallows) an external application. QX11EmbedWidget is a widget used for writing XEmbed applets or plugins. When it has been embedded and the container receives tab focus, focus is passed on to the widget. When the widget reaches the end of its focus chain, focus is passed back to the container. Window activation, accelerator support, modality and drag and drop (XDND) are also handled. The widget and container can both initiate the embedding. If the widget is the initiator, the X11 window ID of the container that it wants to embed itself into must be passed to embedInto(). If the container initiates the embedding, the window ID of the embedded widget must be known. The container calls embed(), passing the window ID. This example shows an application that embeds a QX11EmbedWidget subclass into the window whose ID is passed as a command-line argument: \quotefromfile snippets/qx11embedwidget/main.cpp \skipto main \printuntil /^\}/ The problem of obtaining the window IDs is often solved by the container invoking the application that provides the widget as a separate process (as a panel invokes a docked applet), passing its window ID to the new process as a command-line argument. The new process can then call embedInto() with the container's window ID, as shown in the example code above. Similarly, the new process can report its window ID to the container through IPC, in which case the container can embed the widget. When the widget has been embedded, it emits the signal embedded(). If it is closed by the container, the widget emits containerClosed(). If an error occurs when embedding, error() is emitted. There are XEmbed widgets available for KDE and GTK+. The GTK+ equivalent of QX11EmbedWidget is GtkPlug. The KDE widget is called QXEmbed. \sa QX11EmbedContainer, {XEmbed Specification}*//*! \class QX11EmbedContainer \brief The QX11EmbedContainer class provides an XEmbed container widget. XEmbed is an X11 protocol that supports the embedding of a widget from one application into another application. An XEmbed \e container is the graphical location that embeds an external \e {client widget}. A client widget is a window that is embedded into a container. When a widget has been embedded and the container receives tab focus, focus is passed on to the widget. When the widget reaches the end of its focus chain, focus is passed back to the container. Window activation, accelerator support, modality and drag and drop (XDND) are also handled. QX11EmbedContainer is commonly used for writing panels or toolbars that hold applets, or for \e swallowing X11 applications. When writing a panel application, one container widget is created on the toolbar, and it can then either swallow another widget using embed(), or allow an XEmbed widget to be embedded into itself. The container's X11 window ID, which is retrieved with winId(), must then be known to the client widget. After embedding, the client's window ID can be retrieved with clientWinId(). In the following example, a container widget is created as the main widget. It then invokes an application called "playmovie", passing its window ID as a command line argument. The "playmovie" program is an XEmbed client widget. The widget embeds itself into the container using the container's window ID. \quotefromfile snippets/qx11embedcontainer/main.cpp \skipto main \printuntil /^\}/ When the client widget is embedded, the container emits the signal clientIsEmbedded(). The signal clientClosed() is emitted when a widget is closed. It is possible for QX11EmbedContainer to embed XEmbed widgets from toolkits other than Qt, such as GTK+. Arbitrary (non-XEmbed) X11 widgets can also be embedded, but the XEmbed-specific features such as window activation and focus handling are then lost. The GTK+ equivalent of QX11EmbedContainer is GtkSocket. The KDE widget is called QXEmbed. \sa QX11EmbedWidget, {XEmbed Specification}*//*! \fn void QX11EmbedWidget::embedded() This signal is emitted by the widget that has been embedded by an XEmbed container.*//*! \fn void QX11EmbedWidget::containerClosed() This signal is emitted by the client widget when the container closes the widget. This can happen if the container itself closes, or if the widget is rejected. The container can reject a widget for any reason, but the most common cause of a rejection is when an attempt is made to embed a widget into a container that already has an embedded widget.*//*! \fn void QX11EmbedContainer::clientIsEmbedded() This signal is emitted by the container when a client widget has been embedded.*//*! \fn void QX11EmbedContainer::clientClosed() This signal is emitted by the container when the client widget closes.*//*! \fn void QX11EmbedWidget::error(QX11EmbedWidget::Error error) This signal is emitted if an error occurred as a result of embedding into or communicating with a container. The specified \a error describes the problem that occurred. \sa QX11EmbedWidget::Error*//*! \fn QX11EmbedContainer::Error QX11EmbedContainer::error() const Returns the last error that occurred.*//*! \fn void QX11EmbedContainer::error(QX11EmbedContainer::Error error) This signal is emitted if an error occurred when embedding or communicating with a client. The specified \a error describes the problem that occurred. \sa QX11EmbedContainer::Error*//*! \enum QX11EmbedWidget::Error \value Unknown An unrecognized error occurred. \value InvalidWindowID The X11 window ID of the container was invalid. This error is usually triggered by passing an invalid window ID to embedInto(). \omitvalue Internal*//*! \enum QX11EmbedContainer::Error \value Unknown An unrecognized error occurred. \value InvalidWindowID The X11 window ID of the container was invalid. This error is usually triggered by passing an invalid window ID to embed(). \omitvalue Internal*/const int XButtonPress = ButtonPress;const int XButtonRelease = ButtonRelease;#undef ButtonPress#undef ButtonRelease// This is a hack to move topData() out from QWidgetPrivate to public. We// need to to inspect window()'s embedded state.class QHackWidget : public QWidget{ Q_DECLARE_PRIVATE(QWidget)public: QTLWExtra* topData() { return d_func()->topData(); }};static unsigned int XEMBED_VERSION = 0;static Atom _XEMBED = None;static Atom _XEMBED_INFO = None;enum QX11EmbedMessageType { XEMBED_EMBEDDED_NOTIFY = 0, XEMBED_WINDOW_ACTIVATE = 1, XEMBED_WINDOW_DEACTIVATE = 2, XEMBED_REQUEST_FOCUS = 3, XEMBED_FOCUS_IN = 4, XEMBED_FOCUS_OUT = 5, XEMBED_FOCUS_NEXT = 6, XEMBED_FOCUS_PREV = 7, XEMBED_MODALITY_ON = 10, XEMBED_MODALITY_OFF = 11, XEMBED_REGISTER_ACCELERATOR = 12, XEMBED_UNREGISTER_ACCELERATOR = 13, XEMBED_ACTIVATE_ACCELERATOR = 14};enum QX11EmbedFocusInDetail { XEMBED_FOCUS_CURRENT = 0, XEMBED_FOCUS_FIRST = 1, XEMBED_FOCUS_LAST = 2};enum QX11EmbedFocusInFlags { XEMBED_FOCUS_OTHER = (0 << 0), XEMBED_FOCUS_WRAPAROUND = (1 << 0)};enum QX11EmbedInfoFlags { XEMBED_MAPPED = (1 << 0)};enum QX11EmbedAccelModifiers { XEMBED_MODIFIER_SHIFT = (1 << 0), XEMBED_MODIFIER_CONTROL = (1 << 1), XEMBED_MODIFIER_ALT = (1 << 2), XEMBED_MODIFIER_SUPER = (1 << 3), XEMBED_MODIFIER_HYPER = (1 << 4)};enum QX11EmbedAccelFlags { XEMBED_ACCELERATOR_OVERLOADED = (1 << 0)};// Silence the default X11 error handler.static int x11ErrorHandler(Display *, XErrorEvent *){ return 0;}// Initializes X11 atomsstatic void initXEmbedAtoms(Display *d){ if (_XEMBED == None) _XEMBED = XInternAtom(d, "_XEMBED", false); if (_XEMBED_INFO == None) _XEMBED_INFO = XInternAtom(d, "_XEMBED_INFO", false);}// Returns the X11 timestamp. Maintained mainly by qapplication// internals, but also updated by the XEmbed widgets.static Time x11Time(){ return qt_x11Data->time;}// Gives the version and flags of the supported XEmbed protocol.static unsigned int XEmbedVersion(){ return 0;}// Sends an XEmbed message.static void sendXEmbedMessage(WId window, Display *display, long message, long detail = 0, long data1 = 0, long data2 = 0){ XClientMessageEvent c; memset(&c, 0, sizeof(c)); c.type = ClientMessage; c.message_type = _XEMBED; c.format = 32; c.display = display; c.window = window; c.data.l[0] = x11Time(); c.data.l[1] = message; c.data.l[2] = detail; c.data.l[3] = data1; c.data.l[4] = data2; XSendEvent(display, window, false, NoEventMask, (XEvent *) &c);}// From qapplication_x11.cppstatic XKeyEvent lastKeyEvent;static QCoreApplication::EventFilter oldX11EventFilter;// The purpose of this global x11 filter is for one to capture the key// events in their original state, but most importantly this is the// only way to get the WM_TAKE_FOCUS message from WM_PROTOCOLS.static bool x11EventFilter(void *message, long *result){ XEvent *event = reinterpret_cast<XEvent *>(message); if (event->type == XKeyPress || event->type == XKeyRelease) lastKeyEvent = event->xkey; // Update qt_x_time when a focusin is received. If the widget that // receives this event is active, send a WindowActivate // event. This will cause that widget to call XSetInputFocus // immediately, in which case the timestamp will be correct. if (event->type == ClientMessage && event->xclient.message_type == ATOM(WM_PROTOCOLS)) { QWidget *w = QWidget::find(event->xclient.window); if (w) { Atom a = event->xclient.data.l[0]; if (a == ATOM(WM_TAKE_FOCUS)) { if ((ulong) event->xclient.data.l[1] > X11->time ) X11->time = event->xclient.data.l[1]; if (w->isActiveWindow()) { QEvent e(QEvent::WindowActivate); QApplication::sendEvent(w, &e); } } } } if (oldX11EventFilter && oldX11EventFilter != &x11EventFilter) return oldX11EventFilter(message, result); else return false;}//struct functorData{ Window id, rootWindow; bool clearedWmState; bool reparentedToRoot;};static Bool functor(Display *display, XEvent *event, XPointer arg){ functorData *data = (functorData *) arg; if (!data->reparentedToRoot && event->type == ReparentNotify && event->xreparent.window == data->id && event->xreparent.parent == data->rootWindow) { data->reparentedToRoot = true; return true; } if (!data->clearedWmState && event->type == PropertyNotify && event->xproperty.window == data->id && event->xproperty.atom == ATOM(WM_STATE)) { if (event->xproperty.state == PropertyDelete) { data->clearedWmState = true; return true; } Atom ret; int format, status; unsigned char *retval; unsigned long nitems, after; status = XGetWindowProperty(display, data->id, ATOM(WM_STATE), 0, 2, False, ATOM(WM_STATE), &ret, &format, &nitems, &after, &retval ); if (status == Success && ret == ATOM(WM_STATE) && format == 32 && nitems > 0) { long *state = (long *)retval; if (state[0] == WithdrawnState) { data->clearedWmState = true; return true; } } } return false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -