Keep track of per-column information inside the listview.
[wine] / dlls / user / user_main.c
1 /*
2  * USER initialization code
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <string.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
29
30 #include "controls.h"
31 #include "cursoricon.h"
32 #include "global.h"
33 #include "input.h"
34 #include "hook.h"
35 #include "message.h"
36 #include "queue.h"
37 #include "spy.h"
38 #include "sysmetrics.h"
39 #include "user.h"
40 #include "win.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
44
45 USER_DRIVER USER_Driver;
46
47 WINE_LOOK TWEAK_WineLook = WIN31_LOOK;
48
49 WORD USER_HeapSel = 0;  /* USER heap selector */
50
51 extern HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd );
52 extern UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc);
53
54 static HMODULE graphics_driver;
55 static DWORD exiting_thread_id;
56
57 extern void COMM_Init(void);
58 extern void WDML_NotifyThreadDetach(void);
59
60 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
61
62 /* load the graphics driver */
63 static BOOL load_driver(void)
64 {
65     char buffer[MAX_PATH];
66     HKEY hkey;
67     DWORD type, count;
68
69     strcpy( buffer, "x11drv" );  /* default value */
70     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
71     {
72         count = sizeof(buffer);
73         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
74         RegCloseKey( hkey );
75     }
76
77     if (!(graphics_driver = LoadLibraryA( buffer )))
78     {
79         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
80         return FALSE;
81     }
82
83     GET_USER_FUNC(InitKeyboard);
84     GET_USER_FUNC(VkKeyScan);
85     GET_USER_FUNC(MapVirtualKey);
86     GET_USER_FUNC(GetKeyNameText);
87     GET_USER_FUNC(ToUnicode);
88     GET_USER_FUNC(Beep);
89     GET_USER_FUNC(InitMouse);
90     GET_USER_FUNC(SetCursor);
91     GET_USER_FUNC(GetCursorPos);
92     GET_USER_FUNC(SetCursorPos);
93     GET_USER_FUNC(GetScreenSaveActive);
94     GET_USER_FUNC(SetScreenSaveActive);
95     GET_USER_FUNC(AcquireClipboard);
96     GET_USER_FUNC(ReleaseClipboard);
97     GET_USER_FUNC(SetClipboardData);
98     GET_USER_FUNC(GetClipboardData);
99     GET_USER_FUNC(IsClipboardFormatAvailable);
100     GET_USER_FUNC(RegisterClipboardFormat);
101     GET_USER_FUNC(GetClipboardFormatName);
102     GET_USER_FUNC(IsSelectionOwner);
103     GET_USER_FUNC(ResetSelectionOwner);
104     GET_USER_FUNC(CreateWindow);
105     GET_USER_FUNC(DestroyWindow);
106     GET_USER_FUNC(GetDC);
107     GET_USER_FUNC(ForceWindowRaise);
108     GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
109     GET_USER_FUNC(ScrollDC);
110     GET_USER_FUNC(ScrollWindowEx);
111     GET_USER_FUNC(SetFocus);
112     GET_USER_FUNC(SetParent);
113     GET_USER_FUNC(SetWindowPos);
114     GET_USER_FUNC(SetWindowRgn);
115     GET_USER_FUNC(SetWindowIcon);
116     GET_USER_FUNC(SetWindowStyle);
117     GET_USER_FUNC(SetWindowText);
118     GET_USER_FUNC(ShowWindow);
119     GET_USER_FUNC(SysCommandSizeMove);
120
121     return TRUE;
122 }
123
124
125 /***********************************************************************
126  *           controls_init
127  *
128  * Register the classes for the builtin controls
129  */
130 static void controls_init(void)
131 {
132     extern const struct builtin_class_descr BUTTON_builtin_class;
133     extern const struct builtin_class_descr COMBO_builtin_class;
134     extern const struct builtin_class_descr COMBOLBOX_builtin_class;
135     extern const struct builtin_class_descr DIALOG_builtin_class;
136     extern const struct builtin_class_descr DESKTOP_builtin_class;
137     extern const struct builtin_class_descr EDIT_builtin_class;
138     extern const struct builtin_class_descr ICONTITLE_builtin_class;
139     extern const struct builtin_class_descr LISTBOX_builtin_class;
140     extern const struct builtin_class_descr MDICLIENT_builtin_class;
141     extern const struct builtin_class_descr MENU_builtin_class;
142     extern const struct builtin_class_descr SCROLL_builtin_class;
143     extern const struct builtin_class_descr STATIC_builtin_class;
144
145     CLASS_RegisterBuiltinClass( &BUTTON_builtin_class );
146     CLASS_RegisterBuiltinClass( &COMBO_builtin_class );
147     CLASS_RegisterBuiltinClass( &COMBOLBOX_builtin_class );
148     CLASS_RegisterBuiltinClass( &DIALOG_builtin_class );
149     CLASS_RegisterBuiltinClass( &DESKTOP_builtin_class );
150     CLASS_RegisterBuiltinClass( &EDIT_builtin_class );
151     CLASS_RegisterBuiltinClass( &ICONTITLE_builtin_class );
152     CLASS_RegisterBuiltinClass( &LISTBOX_builtin_class );
153     CLASS_RegisterBuiltinClass( &MDICLIENT_builtin_class );
154     CLASS_RegisterBuiltinClass( &MENU_builtin_class );
155     CLASS_RegisterBuiltinClass( &SCROLL_builtin_class );
156     CLASS_RegisterBuiltinClass( &STATIC_builtin_class );
157 }
158
159
160 /***********************************************************************
161  *           palette_init
162  *
163  * Patch the function pointers in GDI for SelectPalette and RealizePalette
164  */
165 static void palette_init(void)
166 {
167     void **ptr;
168     HMODULE module = GetModuleHandleA( "gdi32" );
169     if (!module)
170     {
171         ERR( "cannot get GDI32 handle\n" );
172         return;
173     }
174     if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
175         pfnGDISelectPalette = InterlockedExchangePointer( ptr, SelectPalette );
176     else ERR( "cannot find pfnSelectPalette in GDI32\n" );
177     if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
178         pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
179     else ERR( "cannot find pfnRealizePalette in GDI32\n" );
180 }
181
182
183 /***********************************************************************
184  *           tweak_init
185  */
186 static void tweak_init(void)
187 {
188     static const char *OS = "Win3.1";
189     char buffer[80];
190     HKEY hkey;
191     DWORD type, count = sizeof(buffer);
192
193     if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Tweak.Layout", &hkey ))
194         return;
195     if (RegQueryValueExA( hkey, "WineLook", 0, &type, buffer, &count ))
196         strcpy( buffer, "Win31" );  /* default value */
197     RegCloseKey( hkey );
198
199     /* WIN31_LOOK is default */
200     if (!strncasecmp( buffer, "Win95", 5 ))
201     {
202         TWEAK_WineLook = WIN95_LOOK;
203         OS = "Win95";
204     }
205     else if (!strncasecmp( buffer, "Win98", 5 ))
206     {
207         TWEAK_WineLook = WIN98_LOOK;
208         OS = "Win98";
209     }
210     TRACE("Using %s look and feel.\n", OS);
211 }
212
213
214 /***********************************************************************
215  *           USER initialisation routine
216  */
217 static BOOL process_attach(void)
218 {
219     HINSTANCE16 instance;
220
221     /* Create USER heap */
222     if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
223     else
224     {
225         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
226         LocalInit16( USER_HeapSel, 32, 65534 );
227     }
228
229     /* Load the graphics driver */
230     tweak_init();
231     if (!load_driver()) return FALSE;
232
233     /* Initialize system colors and metrics */
234     SYSMETRICS_Init();
235     SYSCOLOR_Init();
236
237     /* Setup palette function pointers */
238     palette_init();
239
240     /* Initialize window procedures */
241     if (!WINPROC_Init()) return FALSE;
242
243     /* Initialize built-in window classes */
244     controls_init();
245
246     /* Initialize dialog manager */
247     if (!DIALOG_Init()) return FALSE;
248
249     /* Initialize menus */
250     if (!MENU_Init()) return FALSE;
251
252     /* Initialize message spying */
253     if (!SPY_Init()) return FALSE;
254
255     /* Create message queue of initial thread */
256     InitThreadInput16( 0, 0 );
257
258     /* Create desktop window */
259     if (!WIN_CreateDesktopWindow()) return FALSE;
260
261     /* Initialize keyboard driver */
262     if (USER_Driver.pInitKeyboard) USER_Driver.pInitKeyboard( InputKeyStateTable );
263
264     /* Initialize mouse driver */
265     if (USER_Driver.pInitMouse) USER_Driver.pInitMouse( InputKeyStateTable );
266
267     /* Initialize 16-bit serial communications */
268     COMM_Init();
269
270     return TRUE;
271 }
272
273
274 /**********************************************************************
275  *           USER_IsExitingThread
276  */
277 BOOL USER_IsExitingThread( DWORD tid )
278 {
279     return (tid == exiting_thread_id);
280 }
281
282
283 /**********************************************************************
284  *           thread_detach
285  */
286 static void thread_detach(void)
287 {
288     HQUEUE16 hQueue = GetThreadQueue16( 0 );
289
290     exiting_thread_id = GetCurrentThreadId();
291
292     WDML_NotifyThreadDetach();
293
294     if (hQueue)
295     {
296         TIMER_RemoveQueueTimers( hQueue );
297         HOOK_FreeQueueHooks();
298         WIN_DestroyThreadWindows( GetDesktopWindow() );
299         QUEUE_DeleteMsgQueue();
300     }
301
302     if (!(NtCurrentTeb()->tibflags & TEBF_WIN32))
303     {
304         HMODULE16 hModule = GetExePtr( MapHModuleLS(0) );
305
306         /* FIXME: maybe destroy menus (Windows only complains about them
307          * but does nothing);
308          */
309         if (GetModuleUsage16( hModule ) <= 1)
310         {
311             /* ModuleUnload() in "Internals" */
312             HOOK_FreeModuleHooks( hModule );
313             CLASS_FreeModuleClasses( hModule );
314             CURSORICON_FreeModuleIcons( hModule );
315         }
316     }
317     exiting_thread_id = 0;
318 }
319
320
321 /***********************************************************************
322  *           UserClientDllInitialize  (USER32.@)
323  *
324  * USER dll initialisation routine
325  */
326 BOOL WINAPI UserClientDllInitialize( HINSTANCE inst, DWORD reason, LPVOID reserved )
327 {
328     BOOL ret = TRUE;
329     switch(reason)
330     {
331     case DLL_PROCESS_ATTACH:
332         ret = process_attach();
333         break;
334     case DLL_THREAD_DETACH:
335         thread_detach();
336         break;
337     }
338     return ret;
339 }