Implemented OleCreatePropertyFrame and
[wine] / windows / focus.c
1 /*
2  * Focus functions
3  *
4  * Copyright 1993 David Metcalfe
5  *           1994 Alexandre Julliard
6  *           1995 Alex Korobka
7  *
8  */
9
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 "user.h"
19 #include "debugtools.h"
20
21 DEFAULT_DEBUG_CHANNEL(win);
22
23
24 /*****************************************************************
25  *               FOCUS_SwitchFocus 
26  * pMsgQ is the queue whose perQData focus is to be modified
27  */
28 void FOCUS_SwitchFocus( MESSAGEQUEUE *pMsgQ, HWND hFocusFrom, HWND hFocusTo )
29 {
30     PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
31
32     if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, (WPARAM)hFocusTo, 0 );
33
34     if( !hFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
35     {
36         return;
37     }
38
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);
42
43     SendMessageA( hFocusTo, WM_SETFOCUS, (WPARAM)hFocusFrom, 0 );
44 }
45
46
47 /*****************************************************************
48  *              SetFocus (USER32.@)
49  */
50 HWND WINAPI SetFocus( HWND hwnd )
51 {
52     HWND hWndFocus = 0, hwndTop = hwnd;
53     MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
54     BOOL bRet = 0;
55
56     /* Get the messageQ for the current thread */
57     if (!(pCurMsgQ = QUEUE_Current()))
58     {
59         WARN("\tCurrent message queue not found. Exiting!\n" );
60         return 0;
61     }
62
63     if (hwnd)
64     {
65         /* Check if we can set the focus to this window */
66         WND *wndPtr;
67
68         hwnd = WIN_GetFullHandle( hwnd );
69         for (;;)
70         {
71             HWND parent;
72             LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
73             if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
74             parent = GetAncestor( hwndTop, GA_PARENT );
75             if (!parent || parent == GetDesktopWindow()) break;
76             hwndTop = parent;
77         }
78
79         if (!(wndPtr = WIN_FindWndPtr( hwndTop ))) return 0;
80
81         /* Retrieve the message queue associated with this window */
82         pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
83         WIN_ReleaseWndPtr( wndPtr );
84         if ( !pMsgQ )
85         {
86             WARN("\tMessage queue not found. Exiting!\n" );
87             return 0;
88         }
89
90         /* Make sure that message queue for the window we are setting focus to
91          * shares the same perQ data as the current threads message queue.
92          * In other words you can't set focus to a window owned by a different
93          * thread unless AttachThreadInput has been called previously.
94          * (see AttachThreadInput and SetFocus docs)
95          */
96         if ( pCurMsgQ->pQData != pMsgQ->pQData )
97             goto CLEANUP;
98
99         /* Get the current focus window from the perQ data */
100         hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
101         
102         if( hwnd == hWndFocus )
103         {
104             bRet = 1;      /* Success */
105             goto CLEANUP;  /* Nothing to do */
106         }
107         
108         /* call hooks */
109         if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)hWndFocus) )
110             goto CLEANUP;
111
112         /* activate hwndTop if needed. */
113         if (hwndTop != GetActiveWindow())
114         {
115             if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
116
117             if (!IsWindow( hwnd )) goto CLEANUP;  /* Abort if window destroyed */
118         }
119
120         /* Get the current focus window from the perQ data */
121         hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
122         
123         /* Change focus and send messages */
124         FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
125     }
126     else /* NULL hwnd passed in */
127     {
128         if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
129             return 0;
130
131         /* Get the current focus from the perQ data of the current message Q */
132         hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
133
134       /* Change focus and send messages */
135         FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
136     }
137
138     bRet = 1;      /* Success */
139     
140 CLEANUP:
141
142     /* Unlock the queues before returning */
143     if ( pMsgQ )
144         QUEUE_Unlock( pMsgQ );
145
146     return bRet ? hWndFocus : 0;
147 }
148
149
150 /*****************************************************************
151  *              GetFocus (USER32.@)
152  */
153 HWND WINAPI GetFocus(void)
154 {
155     MESSAGEQUEUE *pCurMsgQ = 0;
156
157     /* Get the messageQ for the current thread */
158     if (!(pCurMsgQ = QUEUE_Current()))
159     {
160         WARN("\tCurrent message queue not found. Exiting!\n" );
161         return 0;
162     }
163
164     /* Get the current focus from the perQ data of the current message Q */
165     return PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
166 }