Small change.
[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 "color.h"
14 #include "message.h"
15 #include "options.h"
16
17 static HWND32 hwndFocus = 0;
18
19 /*****************************************************************
20  *               FOCUS_SetXFocus
21  *
22  * Set the X focus.
23  * Explicit colormap management seems to work only with OLVWM.
24  */
25 void FOCUS_SetXFocus( HWND32 hwnd )
26 {
27     XWindowAttributes win_attr;
28     Window win;
29
30     /* Only mess with the X focus if there's */
31     /* no desktop window and no window manager. */
32     if ((rootWindow != DefaultRootWindow(display)) || Options.managed) return;
33
34     if (!hwnd)  /* If setting the focus to 0, uninstall the colormap */
35     {
36         if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
37             TSXUninstallColormap( display, COLOR_GetColormap() );
38         return;
39     }
40
41       /* Set X focus and install colormap */
42
43     if (!(win = WIN_GetXWindow( hwnd ))) return;
44     if (!TSXGetWindowAttributes( display, win, &win_attr ) ||
45         (win_attr.map_state != IsViewable))
46         return;  /* If window is not viewable, don't change anything */
47
48     TSXSetInputFocus( display, win, RevertToParent, CurrentTime );
49     if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
50         TSXInstallColormap( display, COLOR_GetColormap() );
51
52     EVENT_Synchronize();
53 }
54
55 /*****************************************************************
56  *               FOCUS_SwitchFocus 
57  */
58 void FOCUS_SwitchFocus( HWND32 hFocusFrom, HWND32 hFocusTo )
59 {
60     hwndFocus = hFocusTo;
61
62 #if 0
63     if (hFocusFrom) SendMessage32A( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
64 #else
65     /* FIXME: must be SendMessage16() because 32A doesn't do
66      * intertask at this time */
67     if (hFocusFrom) SendMessage16( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
68 #endif
69     if( !hFocusTo || hFocusTo != hwndFocus )
70         return;
71
72     /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
73        has received the keyboard focus. */
74     FOCUS_SetXFocus( hFocusTo );
75
76 #if 0
77     SendMessage32A( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
78 #else
79     SendMessage16( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
80 #endif
81 }
82
83
84 /*****************************************************************
85  *               SetFocus16   (USER.22)
86  */
87 HWND16 WINAPI SetFocus16( HWND16 hwnd )
88 {
89     return (HWND16)SetFocus32( hwnd );
90 }
91
92
93 /*****************************************************************
94  *               SetFocus32   (USER32.481)
95  */
96 HWND32 WINAPI SetFocus32( HWND32 hwnd )
97 {
98     HWND32 hWndPrevFocus, hwndTop = hwnd;
99     WND *wndPtr = WIN_FindWndPtr( hwnd );
100
101     if (wndPtr)
102     {
103           /* Check if we can set the focus to this window */
104
105         while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD  )
106         {
107             if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
108                  return 0;
109             if (!(wndPtr = wndPtr->parent)) return 0;
110             hwndTop = wndPtr->hwndSelf;
111         }
112
113         if( hwnd == hwndFocus ) return hwnd;
114
115         /* call hooks */
116         if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
117                               (LPARAM)hwndFocus) )
118             return 0;
119
120         /* activate hwndTop if needed. */
121         if (hwndTop != GetActiveWindow32())
122         {
123             if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) return 0;
124
125             if (!IsWindow32( hwnd )) return 0;  /* Abort if window destroyed */
126         }
127     }
128     else if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hwndFocus ) )
129              return 0;
130
131       /* Change focus and send messages */
132     hWndPrevFocus = hwndFocus;
133
134     FOCUS_SwitchFocus( hwndFocus , hwnd );
135
136     return hWndPrevFocus;
137 }
138
139
140 /*****************************************************************
141  *               GetFocus16   (USER.23)
142  */
143 HWND16 WINAPI GetFocus16(void)
144 {
145     return (HWND16)hwndFocus;
146 }
147
148
149 /*****************************************************************
150  *               GetFocus32   (USER32.240)
151  */
152 HWND32 WINAPI GetFocus32(void)
153 {
154     return hwndFocus;
155 }