Fixed OleLoadFromStream: Dereference ppvObj and get IPersistStream
[wine.git] / windows / focus.c
blobab4ea08619d253c75d358fd0a427f365a0d468bc
1 /*
2 * Focus functions
4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
6 * 1995 Alex Korobka
8 */
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "wine/winuser16.h"
13 #include "win.h"
14 #include "winpos.h"
15 #include "hook.h"
16 #include "message.h"
17 #include "queue.h"
18 #include "task.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(win)
24 /*****************************************************************
25 * FOCUS_SwitchFocus
26 * pMsgQ is the queue whose perQData focus is to be modified
28 void FOCUS_SwitchFocus( MESSAGEQUEUE *pMsgQ, HWND hFocusFrom, HWND hFocusTo )
30 WND *pFocusTo = WIN_FindWndPtr( hFocusTo );
32 PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
34 #if 0
35 if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
36 #else
37 /* FIXME: must be SendMessage16() because 32A doesn't do
38 * intertask at this time */
39 if (hFocusFrom) SendMessage16( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
40 #endif
42 if( !pFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
44 WIN_ReleaseWndPtr(pFocusTo);
45 return;
48 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
49 has received the keyboard focus. */
51 pFocusTo->pDriver->pSetFocus(pFocusTo);
53 WIN_ReleaseWndPtr(pFocusTo);
54 #if 0
55 SendMessageA( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
56 #else
57 SendMessage16( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
58 #endif
62 /*****************************************************************
63 * SetFocus16 (USER.22)
65 HWND16 WINAPI SetFocus16( HWND16 hwnd )
67 return (HWND16)SetFocus( hwnd );
71 /*****************************************************************
72 * SetFocus (USER32.481)
74 HWND WINAPI SetFocus( HWND hwnd )
76 HWND hWndFocus = 0, hwndTop = hwnd;
77 WND *wndPtr = WIN_FindWndPtr( hwnd );
78 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
79 BOOL16 bRet = 0;
81 /* Get the messageQ for the current thread */
82 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
84 WARN("\tCurrent message queue not found. Exiting!\n" );
85 goto CLEANUP;
88 if (wndPtr)
90 /* Check if we can set the focus to this window */
92 while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
94 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
95 goto CLEANUP;
96 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
97 if (!wndPtr) goto CLEANUP;
98 hwndTop = wndPtr->hwndSelf;
101 /* definitely at the top window now */
102 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) ) goto CLEANUP;
104 /* Retrieve the message queue associated with this window */
105 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
106 if ( !pMsgQ )
108 WARN("\tMessage queue not found. Exiting!\n" );
109 goto CLEANUP;
112 /* Make sure that message queue for the window we are setting focus to
113 * shares the same perQ data as the current threads message queue.
114 * In other words you can't set focus to a window owned by a different
115 * thread unless AttachThreadInput has been called previously.
116 * (see AttachThreadInput and SetFocus docs)
118 if ( pCurMsgQ->pQData != pMsgQ->pQData )
119 goto CLEANUP;
121 /* Get the current focus window from the perQ data */
122 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
124 if( hwnd == hWndFocus )
126 bRet = 1; /* Success */
127 goto CLEANUP; /* Nothing to do */
130 /* call hooks */
131 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
132 (LPARAM)hWndFocus) )
133 goto CLEANUP;
135 /* activate hwndTop if needed. */
136 if (hwndTop != GetActiveWindow())
138 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
140 if (!IsWindow( hwnd )) goto CLEANUP; /* Abort if window destroyed */
143 /* Get the current focus window from the perQ data */
144 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
146 /* Change focus and send messages */
147 FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
149 else /* NULL hwnd passed in */
151 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
152 goto CLEANUP;
154 /* Get the current focus from the perQ data of the current message Q */
155 hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
157 /* Change focus and send messages */
158 FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
161 bRet = 1; /* Success */
163 CLEANUP:
165 /* Unlock the queues before returning */
166 if ( pMsgQ )
167 QUEUE_Unlock( pMsgQ );
168 if ( pCurMsgQ )
169 QUEUE_Unlock( pCurMsgQ );
171 WIN_ReleaseWndPtr(wndPtr);
172 return bRet ? hWndFocus : 0;
176 /*****************************************************************
177 * GetFocus16 (USER.23)
179 HWND16 WINAPI GetFocus16(void)
181 return (HWND16)GetFocus();
185 /*****************************************************************
186 * GetFocus (USER32.240)
188 HWND WINAPI GetFocus(void)
190 MESSAGEQUEUE *pCurMsgQ = 0;
191 HWND hwndFocus = 0;
193 /* Get the messageQ for the current thread */
194 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
196 WARN("\tCurrent message queue not found. Exiting!\n" );
197 return 0;
200 /* Get the current focus from the perQ data of the current message Q */
201 hwndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
203 QUEUE_Unlock( pCurMsgQ );
205 return hwndFocus;