Release 960717
[wine/multimedia.git] / windows / focus.c
blob1c2802587a6aaee63c209bf551923bda57000174
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.
22 * Explicit colormap management seems to work only with OLVWM.
24 static void FOCUS_SetXFocus( HWND hwnd )
26 XWindowAttributes win_attr;
27 Window win;
29 /* Only mess with the X focus if there's */
30 /* no desktop window and no window manager. */
31 if ((rootWindow != DefaultRootWindow(display)) || Options.managed) return;
33 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
35 if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
36 XUninstallColormap( display, COLOR_GetColormap() );
37 return;
40 /* Set X focus and install colormap */
42 if (!(win = WIN_GetXWindow( hwnd ))) return;
43 if (!XGetWindowAttributes( display, win, &win_attr ) ||
44 (win_attr.map_state != IsViewable))
45 return; /* If window is not viewable, don't change anything */
47 XSetInputFocus( display, win, RevertToParent, CurrentTime );
48 if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
49 XInstallColormap( display, COLOR_GetColormap() );
52 /*****************************************************************
53 * FOCUS_SwitchFocus
55 void FOCUS_SwitchFocus(HWND hFocusFrom, HWND hFocusTo)
57 hwndFocus = hFocusTo;
59 if (hFocusFrom) SendMessage16( hFocusFrom, WM_KILLFOCUS, (WPARAM)hFocusTo, 0L);
60 if( !hFocusTo || hFocusTo != hwndFocus )
61 return;
63 SendMessage16( hFocusTo, WM_SETFOCUS, (WPARAM)hFocusFrom, 0L);
64 FOCUS_SetXFocus( hFocusTo );
68 /*****************************************************************
69 * SetFocus (USER.22)
71 HWND SetFocus(HWND hwnd)
73 HWND hWndPrevFocus, hwndTop = hwnd;
74 WND *wndPtr = WIN_FindWndPtr( hwnd );
76 if (wndPtr)
78 /* Check if we can set the focus to this window */
80 while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
82 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
83 return 0;
84 if (!(wndPtr = wndPtr->parent)) return 0;
85 hwndTop = wndPtr->hwndSelf;
88 if( hwnd == hwndFocus ) return hwnd;
90 /* call hooks */
91 if( HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)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, (LPARAM)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;