summaryrefslogtreecommitdiffstats
path: root/qwidgethostnative.cpp
blob: a8112a69449ca1d260aab21138145fc3fb5fdde8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "qwidgethostnative.h"

#include "qtjambi_core.h"

#include <jawt.h>
#include <jawt_md.h>

#include <QtGui/QApplication>

static const char *qtjambi_awt_title = "Qt Jambi / AWT";

QWidgetHostNative::QWidgetHostNative(jobject parentWindow)

#if defined(Q_OS_WIN32)
    : QWIDGETHOSTNATIVE_BASECLASS(getWindowHandle(parentWindow))
#else
    : QWIDGETHOSTNATIVE_BASECLASS(0)
#endif

{
#if !defined(Q_OS_WIN32)
    embedInto(getWindowHandle(parentWindow));
#endif
}

WId QWidgetHostNative::getWindowHandle(jobject awtWidget)
{
    Q_ASSERT(awtWidget != 0);

    // Call winId() to make sure widget gets a window handle
    QApplication::setAttribute(Qt::AA_NativeWindows);

    JNIEnv *env = qtjambi_current_environment();
    Q_ASSERT(env != 0);

    JAWT awt;
    awt.version = JAWT_VERSION_1_3;
    if (JAWT_GetAWT(env, &awt) == JNI_FALSE) {
        qWarning("%s [%s:%d]: Couldn't get JAWT interface\n", qtjambi_awt_title, __FILE__, __LINE__);
        return false;
    }

    WId awtWindowId = 0;
    {
        JAWT_DrawingSurface *ds;
        ds = awt.GetDrawingSurface(env, awtWidget);
        if (ds == 0) {
            qWarning("%s [%s:%d]: Couldn't get drawing surface\n", qtjambi_awt_title, __FILE__, __LINE__);
            return false;
        }

        jint lock = ds->Lock(ds);
        if (lock & JAWT_LOCK_ERROR) {
            qWarning("%s [%s:%d]: Couldn't lock drawing surface\n", qtjambi_awt_title, __FILE__, __LINE__);
            return false;
        }

        {
            JAWT_DrawingSurfaceInfo *dsi = ds->GetDrawingSurfaceInfo(ds);
            if (dsi == 0) {
                qWarning("%s [%s:%d]: Couldn't get drawing surface info\n", qtjambi_awt_title, __FILE__, __LINE__);
                return false;
            }

#if defined(Q_OS_WIN32)
            awtWindowId = reinterpret_cast<JAWT_Win32DrawingSurfaceInfo *>(dsi->platformInfo)->hwnd;
#else
            awtWindowId = reinterpret_cast<JAWT_X11DrawingSurfaceInfo *>(dsi->platformInfo)->drawable;
#endif
            ds->FreeDrawingSurfaceInfo(dsi);
        }

        ds->Unlock(ds);
        awt.FreeDrawingSurface(ds);
    }

    return awtWindowId;
}

/* ### Empty implementations to work around limitation in generator */
void QWidgetHostNative::childEvent(QChildEvent *e)
{
    QWIDGETHOSTNATIVE_BASECLASS::childEvent(e);
}

bool QWidgetHostNative::eventFilter(QObject *o, QEvent *e)
{
    return QWIDGETHOSTNATIVE_BASECLASS::eventFilter(o, e);
}

void QWidgetHostNative::focusInEvent(QFocusEvent *e)
{
    QWIDGETHOSTNATIVE_BASECLASS::focusInEvent(e);
}

bool QWidgetHostNative::event(QEvent *e)
{
    return QWIDGETHOSTNATIVE_BASECLASS::event(e);
}

void QWidgetHostNative::resizeEvent(QResizeEvent *e)
{
    QWIDGETHOSTNATIVE_BASECLASS::resizeEvent(e);
}