Release 960913
[wine] / windows / focus.c
1 /*
2  * Focus functions
3  *
4  * Copyright 1993 David Metcalfe
5  *           1994 Alexandre Julliard
6  *           1995 Alex Korobka
7  *
8  */
9
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"
17
18 static HWND32 hwndFocus = 0;
19
20 /*****************************************************************
21  *               FOCUS_SetXFocus
22  *
23  * Set the X focus.
24  * Explicit colormap management seems to work only with OLVWM.
25  */
26 void FOCUS_SetXFocus( HWND32 hwnd )
27 {
28     XWindowAttributes win_attr;
29     Window win;
30
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;
34
35     if (!hwnd)  /* If setting the focus to 0, uninstall the colormap */
36     {
37         if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
38             XUninstallColormap( display, COLOR_GetColormap() );
39         return;
40     }
41
42       /* Set X focus and install colormap */
43
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 */
48
49     XSetInputFocus( display, win, RevertToParent, CurrentTime );
50     if (COLOR_GetSystemPaletteFlags() & COLOR_PRIVATE)
51         XInstallColormap( display, COLOR_GetColormap() );
52
53     EVENT_Synchronize();
54 }
55
56 /*****************************************************************
57  *               FOCUS_SwitchFocus 
58  */
59 void FOCUS_SwitchFocus( HWND32 hFocusFrom, HWND32 hFocusTo )
60 {
61     hwndFocus = hFocusTo;
62
63     if (hFocusFrom) SendMessage32A( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
64     if( !hFocusTo || hFocusTo != hwndFocus )
65         return;
66
67     SendMessage32A( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
68     FOCUS_SetXFocus( hFocusTo );
69 }
70
71
72 /*****************************************************************
73  *               SetFocus16   (USER.22)
74  */
75 HWND16 SetFocus16( HWND16 hwnd )
76 {
77     return (HWND16)SetFocus32( hwnd );
78 }
79
80
81 /*****************************************************************
82  *               SetFocus32   (USER32.480)
83  */
84 HWND32 SetFocus32( HWND32 hwnd )
85 {
86     HWND32 hWndPrevFocus, hwndTop = hwnd;
87     WND *wndPtr = WIN_FindWndPtr( hwnd );
88
89     if (wndPtr)
90     {
91           /* Check if we can set the focus to this window */
92
93         while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD  )
94         {
95             if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
96                  return 0;
97             if (!(wndPtr = wndPtr->parent)) return 0;
98             hwndTop = wndPtr->hwndSelf;
99         }
100
101         if( hwnd == hwndFocus ) return hwnd;
102
103         /* call hooks */
104         if( HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
105                             (LPARAM)hwndFocus) )
106             return 0;
107
108         /* activate hwndTop if needed. */
109         if (hwndTop != GetActiveWindow())
110         {
111             if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) return 0;
112
113             if (!IsWindow( hwnd )) return 0;  /* Abort if window destroyed */
114         }
115     }
116     else if( HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hwndFocus ) )
117              return 0;
118
119       /* Change focus and send messages */
120     hWndPrevFocus = hwndFocus;
121
122     FOCUS_SwitchFocus( hwndFocus , hwnd );
123
124     return hWndPrevFocus;
125 }
126
127
128 /*****************************************************************
129  *               GetFocus16   (USER.23)
130  */
131 HWND16 GetFocus16(void)
132 {
133     return (HWND16)hwndFocus;
134 }
135
136
137 /*****************************************************************
138  *               GetFocus32   (USER32.239)
139  */
140 HWND32 GetFocus32(void)
141 {
142     return hwndFocus;
143 }