Fix includes.
[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 "win.h"
11 #include "winpos.h"
12 #include "hook.h"
13 #include "message.h"
14
15 static HWND32 hwndFocus = 0;
16
17 /*****************************************************************
18  *               FOCUS_SwitchFocus 
19  */
20 void FOCUS_SwitchFocus( HWND32 hFocusFrom, HWND32 hFocusTo )
21 {
22     WND *pFocusTo = WIN_FindWndPtr( hFocusTo );
23     hwndFocus = hFocusTo;
24
25 #if 0
26     if (hFocusFrom) SendMessage32A( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
27 #else
28     /* FIXME: must be SendMessage16() because 32A doesn't do
29      * intertask at this time */
30     if (hFocusFrom) SendMessage16( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
31 #endif
32     if( !hFocusTo || hFocusTo != hwndFocus )
33         return;
34
35     /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
36        has received the keyboard focus. */
37
38     pFocusTo->pDriver->pSetFocus(pFocusTo);
39
40 #if 0
41     SendMessage32A( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
42 #else
43     SendMessage16( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
44 #endif
45 }
46
47
48 /*****************************************************************
49  *               SetFocus16   (USER.22)
50  */
51 HWND16 WINAPI SetFocus16( HWND16 hwnd )
52 {
53     return (HWND16)SetFocus32( hwnd );
54 }
55
56
57 /*****************************************************************
58  *               SetFocus32   (USER32.481)
59  */
60 HWND32 WINAPI SetFocus32( HWND32 hwnd )
61 {
62     HWND32 hWndPrevFocus, hwndTop = hwnd;
63     WND *wndPtr = WIN_FindWndPtr( hwnd );
64
65     if (wndPtr)
66     {
67           /* Check if we can set the focus to this window */
68
69         while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD  )
70         {
71             if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
72                  return 0;
73             if (!(wndPtr = wndPtr->parent)) return 0;
74             hwndTop = wndPtr->hwndSelf;
75         }
76
77         if( hwnd == hwndFocus ) return hwnd;
78
79         /* call hooks */
80         if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
81                               (LPARAM)hwndFocus) )
82             return 0;
83
84         /* activate hwndTop if needed. */
85         if (hwndTop != GetActiveWindow32())
86         {
87             if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) return 0;
88
89             if (!IsWindow32( hwnd )) return 0;  /* Abort if window destroyed */
90         }
91     }
92     else if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hwndFocus ) )
93              return 0;
94
95       /* Change focus and send messages */
96     hWndPrevFocus = hwndFocus;
97
98     FOCUS_SwitchFocus( hwndFocus , hwnd );
99
100     return hWndPrevFocus;
101 }
102
103
104 /*****************************************************************
105  *               GetFocus16   (USER.23)
106  */
107 HWND16 WINAPI GetFocus16(void)
108 {
109     return (HWND16)hwndFocus;
110 }
111
112
113 /*****************************************************************
114  *               GetFocus32   (USER32.240)
115  */
116 HWND32 WINAPI GetFocus32(void)
117 {
118     return hwndFocus;
119 }