//
Implementation of the QWinWidget classes
#
ifdef
QTWINMIGRATE_WITHMFC
#
include
"
qwinwidget.h
"
#
include
<
qt_windows.h
>
#
if
QT_VERSION >= 0x050000
#
include
<
qpa/qplatformnativeinterface.h
>
#
define
QT_WA
(
unicode, ansi
) unicode
\class QWinWidget qwinwidget.h
\brief The QWinWidget class is a Qt widget that can be child of a
The QWinWidget class is the bridge between an existing application
user interface developed using native Win32 APIs or toolkits like
MFC, and Qt based GUI elements.
Using QWinWidget as the parent of QDialogs will ensure that
modality, placement and stacking works properly throughout the
entire application. If the child widget is a top level window that
uses the \c WDestructiveClose flag, QWinWidget will destroy itself
when the child window closes down.
Applications moving to Qt can use QWinWidget to add new
functionality, and gradually replace the existing interface.
Creates an instance of QWinWidget. \a hParentWnd is the handle to
the native Win32 parent. If a \a parent is provided the object is
owned by that QObject. \a f is passed on to the QWidget constructor.
QWinWidget::QWinWidget
(HWND hParentWnd, QObject *parent, Qt::WindowFlags f)
: QWidget(
0
, f), hParent(hParentWnd), prevFocus(
0
), reenable_parent(
false
)
QObject::setParent
(parent);
#
ifdef
QTWINMIGRATE_WITHMFC
Creates an instance of QWinWidget. \a parentWnd is a pointer to an
MFC window object. If a \a parent is provided the object is owned
by that QObject. \a f is passed on to the QWidget constructor.
QWinWidget::QWinWidget
(CWnd *parentWnd, QObject *parent, Qt::WindowFlags f)
: QWidget(
0
, f), hParent(parentWnd ? parentWnd->m_hWnd :
0
), prevFocus(
0
), reenable_parent(
false
)
QObject::setParent
(parent);
#
if
QT_VERSION >= 0x050000
setProperty
(
"
_q_embedded_native_parent_handle
"
,
WId
(hParent));
//
make the widget window style be WS_CHILD so SetParent will work
SetWindowLong
((HWND)
winId
(), GWL_STYLE, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
SetWindowLongA
((HWND)
winId
(), GWL_STYLE, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
#
if
QT_VERSION >= 0x050000
QWindow *window =
windowHandle
();
HWND h =
static_cast
<HWND>(
QGuiApplication::platformNativeInterface
()->
nativeResourceForWindow
(
"
handle
"
, window));
window->
setFlags
(Qt::FramelessWindowHint);
SetParent
(
winId
(), hParent);
QEvent
e
(QEvent::EmbeddingControl);
QApplication::sendEvent
(
this
, &e);
Destroys this object, freeing all allocated resources.
QWinWidget::~QWinWidget
()
Returns the handle of the native Win32 parent window.
HWND
QWinWidget::parentWindow
()
const
void
QWinWidget::childEvent
(QChildEvent *e)
QObject *obj = e->
child
();
if
(obj->
isWidgetType
()) {
if
(obj->
isWidgetType
()) {
obj->
installEventFilter
(
this
);
}
else
if
(e->
removed
() && reenable_parent) {
reenable_parent =
false
;
EnableWindow
(hParent,
true
);
obj->
removeEventFilter
(
this
);
void
QWinWidget::saveFocus
()
prevFocus = ::
GetFocus
();
prevFocus =
parentWindow
();
Shows this widget. Overrides QWidget::show().
Centers this widget over the native parent window. Use this
function to have Qt toplevel windows (i.e. dialogs) positioned
correctly over their native parent windows.
QWinWidget qwin(hParent);
QMessageBox::information(&qwin, "Caption", "Information Text");
This will center the message box over the client area of hParent.
void
QWinWidget::center
()
const
QWidget *child = findChild<QWidget*>();
if
(child && !child->
isWindow
()) {
qWarning
(
"
QWinWidget::center: Call this function only for QWinWidgets with toplevel children
"
);
GetWindowRect
(hParent, &r);
setGeometry
((r.
right
-r.
left
)/
2
+r.
left
, (r.
bottom
-r.
top
)/
2
+r.
top
,
0
,
0
);
void
QWinWidget::showCentered
()
Sets the focus to the window that had the focus before this widget
was shown, or if there was no previous window, sets the focus to
void
QWinWidget::resetFocus
()
::SetFocus
(parentWindow());
#
if
QT_VERSION >= 0x050000
bool
QWinWidget::nativeEvent
(
const
QByteArray &,
void
*message,
long
*)
bool
QWinWidget::winEvent
(MSG *msg,
long
*)
#
if
QT_VERSION >= 0x050000
MSG *msg = (MSG *)message;
if
(msg->
message
== WM_SETFOCUS) {
if
(::
GetKeyState
(VK_LBUTTON) <
0
|| ::
GetKeyState
(VK_RBUTTON) <
0
)
reason = Qt::MouseFocusReason;
else
if
(::
GetKeyState
(VK_SHIFT) <
0
)
reason = Qt::BacktabFocusReason;
reason = Qt::TabFocusReason;
QFocusEvent
e
(QEvent::FocusIn, reason);
QApplication::sendEvent
(
this
, &e);
bool
QWinWidget::eventFilter
(QObject *o, QEvent *e)
QWidget *w = (QWidget*)o;
case
QEvent::WindowDeactivate:
if
(w->
isModal
() && w->
isHidden
())
BringWindowToTop
(hParent);
EnableWindow
(hParent,
true
);
reenable_parent =
false
;
if
(w->
testAttribute
(Qt::WA_DeleteOnClose) && w->
isWindow
())
if
(w->
isModal
() && !reenable_parent) {
EnableWindow
(hParent,
false
);
::SetActiveWindow
(hParent);
if
(w->
testAttribute
(Qt::WA_DeleteOnClose))
return
QWidget::eventFilter
(o, e);
void
QWinWidget::focusInEvent
(QFocusEvent *e)
QWidget *candidate =
this
;
case
Qt::BacktabFocusReason:
while
(!(candidate->
focusPolicy
() & Qt::TabFocus)) {
candidate = candidate->
nextInFocusChain
();
if
(candidate ==
this
) {
candidate->
setFocus
(e->
reason
());
if
(e->
reason
() == Qt::BacktabFocusReason || e->
reason
() == Qt::TabFocusReason) {
candidate->
setAttribute
(Qt::WA_KeyboardFocusChange);
candidate->
window
()->
setAttribute
(Qt::WA_KeyboardFocusChange);
if
(e->
reason
() == Qt::BacktabFocusReason)
QWidget::focusNextPrevChild
(
false
);
bool
QWinWidget::focusNextPrevChild
(
bool
next)
QWidget *curFocus =
focusWidget
();
if
(!curFocus->
isWindow
()) {
QWidget *nextFocus = curFocus->
nextInFocusChain
();
while
(nextFocus != curFocus) {
if
(nextFocus->
focusPolicy
() & Qt::TabFocus) {
}
else
if
(nextFocus->
isWindow
()) {
nextFocus = nextFocus->
nextInFocusChain
();
return
QWidget::focusNextPrevChild
(
false
);
QWidget *nextFocus = curFocus;
nextFocus = nextFocus->
nextInFocusChain
();
if
(nextFocus->
isWindow
())
if
(nextFocus->
focusPolicy
() & Qt::TabFocus) {
return
QWidget::focusNextPrevChild
(
true
);