Release 970112
[wine.git] / windows / focus.c
blob7d284d0ede0d7ed470aa03f90ba55cea0f555410
1 /*
2 * Focus functions
4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
6 * 1995 Alex Korobka
8 */
10 #define NO_TRANSITION_TYPES /* This file is Win32-clean */
11 #include "win.h"
12 #include "winpos.h"
13 #include "hook.h"
14 #include "color.h"
15 #include "message.h"
16 #include "options.h"
18 static HWND32 hwndFocus = 0;
20 /*****************************************************************
21 * FOCUS_SetXFocus
23 * Set the X focus.
24 * Explicit colormap management seems to work only with OLVWM.
26 void FOCUS_SetXFocus( HWND32 hwnd )
28 XWindowAttributes win_attr;
29 Window win;
31 /* Only mess with the X focus if there's */
32 /* no desktop window and no window manager. */
33 if ((rootWindow != DefaultRootWindow(display)) || Options.managed) return;
35 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
37 if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
38 XUninstallColormap( display, COLOR_GetColormap() );
39 return;
42 /* Set X focus and install colormap */
44 if (!(win = WIN_GetXWindow( hwnd ))) return;
45 if (!XGetWindowAttributes( display, win, &win_attr ) ||
46 (win_attr.map_state != IsViewable))
47 return; /* If window is not viewable, don't change anything */
49 XSetInputFocus( display, win, RevertToParent, CurrentTime );
50 if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
51 XInstallColormap( display, COLOR_GetColormap() );
53 EVENT_Synchronize();
56 /*****************************************************************
57 * FOCUS_SwitchFocus
59 void FOCUS_SwitchFocus( HWND32 hFocusFrom, HWND32 hFocusTo )
61 hwndFocus = hFocusTo;
63 if (hFocusFrom) SendMessage32A( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
64 if( !hFocusTo || hFocusTo != hwndFocus )
65 return;
67 SendMessage32A( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
68 FOCUS_SetXFocus( hFocusTo );
72 /*****************************************************************
73 * SetFocus16 (USER.22)
75 HWND16 SetFocus16( HWND16 hwnd )
77 return (HWND16)SetFocus32( hwnd );
81 /*****************************************************************
82 * SetFocus32 (USER32.480)
84 HWND32 SetFocus32( HWND32 hwnd )
86 HWND32 hWndPrevFocus, hwndTop = hwnd;
87 WND *wndPtr = WIN_FindWndPtr( hwnd );
89 if (wndPtr)
91 /* Check if we can set the focus to this window */
93 while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
95 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
96 return 0;
97 if (!(wndPtr = wndPtr->parent)) return 0;
98 hwndTop = wndPtr->hwndSelf;
101 if( hwnd == hwndFocus ) return hwnd;
103 /* call hooks */
104 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
105 (LPARAM)hwndFocus) )
106 return 0;
108 /* activate hwndTop if needed. */
109 if (hwndTop != GetActiveWindow())
111 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) return 0;
113 if (!IsWindow( hwnd )) return 0; /* Abort if window destroyed */
116 else if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hwndFocus ) )
117 return 0;
119 /* Change focus and send messages */
120 hWndPrevFocus = hwndFocus;
122 FOCUS_SwitchFocus( hwndFocus , hwnd );
124 return hWndPrevFocus;
128 /*****************************************************************
129 * GetFocus16 (USER.23)
131 HWND16 GetFocus16(void)
133 return (HWND16)hwndFocus;
137 /*****************************************************************
138 * GetFocus32 (USER32.239)
140 HWND32 GetFocus32(void)
142 return hwndFocus;