Release 940405
[wine/gsoc-2012-control.git] / windows / focus.c
blob8850af0b6077fbdc7c8d86f6d26de0e6af34bf5e
1 /*
2 * Focus functions
4 * Copyright 1993 David Metcalfe
5 * Copyright 1994 Alexandre Julliard
6 */
8 static char Copyright[] = "Copyright David Metcalfe, 1993";
9 static char Copyright2[] = "Copyright Alexandre Julliard, 1994";
11 #include "win.h"
13 extern Colormap COLOR_WinColormap;
15 static HWND hWndFocus = 0;
18 /*****************************************************************
19 * FOCUS_SetXFocus
21 * Set the X focus.
23 static void FOCUS_SetXFocus( HWND hwnd )
25 WND *wndPtr;
26 XWindowAttributes win_attr;
28 /* Only mess with the X focus if there's no desktop window */
29 if (rootWindow != DefaultRootWindow(display)) return;
31 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
33 if (COLOR_WinColormap != DefaultColormapOfScreen(screen))
34 XUninstallColormap( display, COLOR_WinColormap );
35 return;
38 /* Set X focus on the top-level ancestor. */
40 /* Find ancestor */
41 wndPtr = WIN_FindWndPtr( hWndFocus );
42 while (wndPtr && (wndPtr->dwStyle & WS_CHILD))
43 wndPtr = WIN_FindWndPtr( wndPtr->hwndParent );
44 if (!wndPtr) return;
46 /* Make sure window is viewable */
47 if (!XGetWindowAttributes( display, wndPtr->window, &win_attr ) ||
48 (win_attr.map_state != IsViewable)) return;
50 /* Set X focus and install colormap */
51 XSetInputFocus( display, wndPtr->window, RevertToParent, CurrentTime );
52 if (COLOR_WinColormap != DefaultColormapOfScreen(screen))
53 XInstallColormap( display, COLOR_WinColormap );
57 /*****************************************************************
58 * SetFocus (USER.22)
61 HWND SetFocus(HWND hwnd)
63 HWND hWndPrevFocus, hwndParent;
64 WND *wndPtr;
66 if (hwnd == hWndFocus) return hWndFocus; /* Nothing to do! */
68 if (hwnd)
70 /* Check if we can set the focus to this window */
72 hwndParent = hwnd;
73 while ((wndPtr = WIN_FindWndPtr( hwndParent )) != NULL)
75 if ((wndPtr->dwStyle & WS_MINIMIZE) ||
76 (wndPtr->dwStyle & WS_DISABLED)) return 0;
77 if (!(wndPtr->dwStyle & WS_CHILD)) break;
78 hwndParent = wndPtr->hwndParent;
81 /* Now hwndParent is the top-level ancestor. Activate it. */
83 if (hwndParent != GetActiveWindow())
85 SetWindowPos( hwndParent, HWND_TOP, 0, 0, 0, 0,
86 SWP_NOSIZE | SWP_NOMOVE );
87 if (!IsWindow( hwnd )) return 0; /* Abort if window destroyed */
91 /* Change focus and send messages */
93 hWndPrevFocus = hWndFocus;
94 hWndFocus = hwnd;
95 if (hWndPrevFocus) SendMessage( hWndPrevFocus, WM_KILLFOCUS, hwnd, 0 );
96 if (hwnd == hWndFocus) /* Maybe already changed during WM_KILLFOCUS */
98 if (hwnd) SendMessage( hWndFocus, WM_SETFOCUS, hWndPrevFocus, 0 );
99 FOCUS_SetXFocus( hwnd );
101 return hWndPrevFocus;
105 /*****************************************************************
106 * GetFocus (USER.23)
109 HWND GetFocus(void)
111 return hWndFocus;