Release 951226
[wine.git] / windows / focus.c
blobbbd591bded74a328a9ac7596ceedb62315bcfc88
1 /*
2 * Focus functions
4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
6 * 1995 Alex Korobka
8 */
10 #include "win.h"
11 #include "winpos.h"
12 #include "hook.h"
13 #include "color.h"
14 #include "options.h"
16 static HWND hwndFocus = 0;
18 /*****************************************************************
19 * FOCUS_SetXFocus
21 * Set the X focus.
23 static void FOCUS_SetXFocus( HWND hwnd )
25 XWindowAttributes win_attr;
26 Window win;
28 /* Only mess with the X focus if there's */
29 /* no desktop window and no window manager. */
30 if ((rootWindow != DefaultRootWindow(display)) || Options.managed) return;
32 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
34 if (COLOR_WinColormap != DefaultColormapOfScreen(screen))
35 XUninstallColormap( display, COLOR_WinColormap );
36 return;
39 /* Set X focus and install colormap */
41 if (!(win = WIN_GetXWindow( hwnd ))) return;
42 if (!XGetWindowAttributes( display, win, &win_attr ) ||
43 (win_attr.map_state != IsViewable))
44 return; /* If window is not viewable, don't change anything */
45 XSetInputFocus( display, win, RevertToParent, CurrentTime );
46 if (COLOR_WinColormap != DefaultColormapOfScreen(screen))
47 XInstallColormap( display, COLOR_WinColormap );
50 /*****************************************************************
51 * FOCUS_SwitchFocus
53 void FOCUS_SwitchFocus(HWND hFocusFrom, HWND hFocusTo)
55 hwndFocus = hFocusTo;
57 if (hFocusFrom) SendMessage( hFocusFrom, WM_KILLFOCUS, (WPARAM)hFocusTo, 0L);
58 if( !hFocusTo || hFocusTo != hwndFocus )
59 return;
61 SendMessage( hFocusTo, WM_SETFOCUS, (WPARAM)hFocusFrom, 0L);
62 FOCUS_SetXFocus( hFocusTo );
66 /*****************************************************************
67 * SetFocus (USER.22)
69 HWND SetFocus(HWND hwnd)
71 HWND hWndPrevFocus, hwndTop;
72 WND *wndPtr = WIN_FindWndPtr( hwndTop = hwnd );
74 if (wndPtr)
76 /* Check if we can set the focus to this window */
78 while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
80 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
81 return 0;
83 hwndTop = wndPtr->hwndParent;
84 wndPtr = WIN_FindWndPtr( hwndTop );
85 if ( !wndPtr )
86 return 0;
89 if( hwnd == hwndFocus ) return hwnd;
91 /* call hooks */
92 if( HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)hwndFocus) )
93 return 0;
95 /* activate hwndTop if needed. */
96 if (hwndTop != GetActiveWindow())
98 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) return 0;
100 if (!IsWindow( hwnd )) return 0; /* Abort if window destroyed */
103 else if( HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hwndFocus ) )
104 return 0;
106 /* Change focus and send messages */
107 hWndPrevFocus = hwndFocus;
109 FOCUS_SwitchFocus( hwndFocus , hwnd );
111 return hWndPrevFocus;
115 /*****************************************************************
116 * GetFocus (USER.23)
118 HWND GetFocus(void)
120 return hwndFocus;