4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
12 #include "wine/winuser16.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(win
);
24 /*****************************************************************
26 * pMsgQ is the queue whose perQData focus is to be modified
28 void FOCUS_SwitchFocus( MESSAGEQUEUE
*pMsgQ
, HWND hFocusFrom
, HWND hFocusTo
)
30 PERQDATA_SetFocusWnd( pMsgQ
->pQData
, hFocusTo
);
32 if (hFocusFrom
) SendMessageA( hFocusFrom
, WM_KILLFOCUS
, hFocusTo
, 0 );
34 if( !hFocusTo
|| hFocusTo
!= PERQDATA_GetFocusWnd( pMsgQ
->pQData
) )
39 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
40 has received the keyboard focus. */
41 if (USER_Driver
.pSetFocus
) USER_Driver
.pSetFocus(hFocusTo
);
43 SendMessageA( hFocusTo
, WM_SETFOCUS
, hFocusFrom
, 0 );
47 /*****************************************************************
50 HWND16 WINAPI
SetFocus16( HWND16 hwnd
)
52 return (HWND16
)SetFocus( hwnd
);
56 /*****************************************************************
59 HWND WINAPI
SetFocus( HWND hwnd
)
61 HWND hWndFocus
= 0, hwndTop
= hwnd
;
62 MESSAGEQUEUE
*pMsgQ
= 0, *pCurMsgQ
= 0;
65 /* Get the messageQ for the current thread */
66 if (!(pCurMsgQ
= QUEUE_Current()))
68 WARN("\tCurrent message queue not found. Exiting!\n" );
74 /* Check if we can set the focus to this window */
80 LONG style
= GetWindowLongW( hwndTop
, GWL_STYLE
);
81 if (style
& (WS_MINIMIZE
| WS_DISABLED
)) return 0;
82 parent
= GetAncestor( hwndTop
, GA_PARENT
);
83 if (!parent
|| parent
== GetDesktopWindow()) break;
87 if (!(wndPtr
= WIN_FindWndPtr( hwndTop
))) return 0;
89 /* Retrieve the message queue associated with this window */
90 pMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( wndPtr
->hmemTaskQ
);
91 WIN_ReleaseWndPtr( wndPtr
);
94 WARN("\tMessage queue not found. Exiting!\n" );
98 /* Make sure that message queue for the window we are setting focus to
99 * shares the same perQ data as the current threads message queue.
100 * In other words you can't set focus to a window owned by a different
101 * thread unless AttachThreadInput has been called previously.
102 * (see AttachThreadInput and SetFocus docs)
104 if ( pCurMsgQ
->pQData
!= pMsgQ
->pQData
)
107 /* Get the current focus window from the perQ data */
108 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
110 if( hwnd
== hWndFocus
)
112 bRet
= 1; /* Success */
113 goto CLEANUP
; /* Nothing to do */
117 if( HOOK_CallHooksA( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)hwnd
, (LPARAM
)hWndFocus
) )
120 /* activate hwndTop if needed. */
121 if (hwndTop
!= GetActiveWindow())
123 if (!WINPOS_SetActiveWindow(hwndTop
, 0, 0)) goto CLEANUP
;
125 if (!IsWindow( hwnd
)) goto CLEANUP
; /* Abort if window destroyed */
128 /* Get the current focus window from the perQ data */
129 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
131 /* Change focus and send messages */
132 FOCUS_SwitchFocus( pMsgQ
, hWndFocus
, hwnd
);
134 else /* NULL hwnd passed in */
136 if( HOOK_CallHooksA( WH_CBT
, HCBT_SETFOCUS
, 0, (LPARAM
)hWndFocus
) )
139 /* Get the current focus from the perQ data of the current message Q */
140 hWndFocus
= PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);
142 /* Change focus and send messages */
143 FOCUS_SwitchFocus( pCurMsgQ
, hWndFocus
, hwnd
);
146 bRet
= 1; /* Success */
150 /* Unlock the queues before returning */
152 QUEUE_Unlock( pMsgQ
);
154 return bRet
? hWndFocus
: 0;
158 /*****************************************************************
161 HWND16 WINAPI
GetFocus16(void)
163 return (HWND16
)GetFocus();
167 /*****************************************************************
168 * GetFocus (USER32.@)
170 HWND WINAPI
GetFocus(void)
172 MESSAGEQUEUE
*pCurMsgQ
= 0;
174 /* Get the messageQ for the current thread */
175 if (!(pCurMsgQ
= QUEUE_Current()))
177 WARN("\tCurrent message queue not found. Exiting!\n" );
181 /* Get the current focus from the perQ data of the current message Q */
182 return PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);