Release 951212
[wine/gsoc-2012-control.git] / windows / focus.c
blob6e8e46a1297d4adc9b837205cf6150ad1d93812c
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"
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 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 and install colormap */
40 if (!(win = WIN_GetXWindow( hwnd ))) return;
41 if (!XGetWindowAttributes( display, win, &win_attr ) ||
42 (win_attr.map_state != IsViewable))
43 return; /* If window is not viewable, don't change anything */
44 XSetInputFocus( display, win, RevertToParent, CurrentTime );
45 if (COLOR_WinColormap != DefaultColormapOfScreen(screen))
46 XInstallColormap( display, COLOR_WinColormap );
49 /*****************************************************************
50 * FOCUS_SwitchFocus
52 void FOCUS_SwitchFocus(HWND hFocusFrom, HWND hFocusTo)
54 hwndFocus = hFocusTo;
56 if (hFocusFrom) SendMessage( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0L);
57 if( !hFocusTo || hFocusTo != hwndFocus )
58 return;
60 SendMessage( hFocusTo, WM_SETFOCUS, hFocusFrom, 0L);
61 FOCUS_SetXFocus( hFocusTo );
65 /*****************************************************************
66 * SetFocus (USER.22)
68 HWND SetFocus(HWND hwnd)
70 HWND hWndPrevFocus, hwndTop;
71 WND *wndPtr = WIN_FindWndPtr( hwndTop = hwnd );
73 if (wndPtr)
75 /* Check if we can set the focus to this window */
77 while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
79 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
80 return 0;
82 hwndTop = wndPtr->hwndParent;
83 wndPtr = WIN_FindWndPtr( hwndTop );
84 if ( !wndPtr )
85 return 0;
88 if( hwnd == hwndFocus ) return hwnd;
90 /* call hooks */
91 if( HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, hwnd, hwndFocus) )
92 return 0;
94 /* activate hwndTop if needed. */
95 if (hwndTop != GetActiveWindow())
97 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) return 0;
99 if (!IsWindow( hwnd )) return 0; /* Abort if window destroyed */
102 else if( HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, hwndFocus ) )
103 return 0;
105 /* Change focus and send messages */
106 hWndPrevFocus = hwndFocus;
108 FOCUS_SwitchFocus( hwndFocus , hwnd );
110 return hWndPrevFocus;
114 /*****************************************************************
115 * GetFocus (USER.23)
117 HWND GetFocus(void)
119 return hwndFocus;