rsaenh/tests: Fix memory leaks.
[wine] / dlls / user32 / user16.c
1 /*
2  * Misc 16-bit USER functions
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  * Copyright 2002 Patrik Stridvall
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #define OEMRESOURCE
28
29 #include "wine/winuser16.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wownt32.h"
33 #include "user_private.h"
34 #include "win.h"
35 #include "controls.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(user);
39
40 /* handle to handle 16 conversions */
41 #define HANDLE_16(h32)          (LOWORD(h32))
42
43 /* handle16 to handle conversions */
44 #define HANDLE_32(h16)          ((HANDLE)(ULONG_PTR)(h16))
45 #define HINSTANCE_32(h16)       ((HINSTANCE)(ULONG_PTR)(h16))
46
47 #define IS_MENU_STRING_ITEM(flags) \
48     (((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR)) == MF_STRING)
49
50 /* UserSeeUserDo parameters */
51 #define USUD_LOCALALLOC        0x0001
52 #define USUD_LOCALFREE         0x0002
53 #define USUD_LOCALCOMPACT      0x0003
54 #define USUD_LOCALHEAP         0x0004
55 #define USUD_FIRSTCLASS        0x0005
56
57 WORD WINAPI DestroyIcon32(HGLOBAL16, UINT16);
58
59 WORD USER_HeapSel = 0;  /* USER heap selector */
60
61 struct gray_string_info
62 {
63     GRAYSTRINGPROC16 proc;
64     LPARAM           param;
65     char             str[1];
66 };
67
68 /* callback for 16-bit gray string proc with opaque pointer */
69 static BOOL CALLBACK gray_string_callback( HDC hdc, LPARAM param, INT len )
70 {
71     const struct gray_string_info *info = (struct gray_string_info *)param;
72     WORD args[4];
73     DWORD ret;
74
75     args[3] = HDC_16(hdc);
76     args[2] = HIWORD(info->param);
77     args[1] = LOWORD(info->param);
78     args[0] = len;
79     WOWCallback16Ex( (DWORD)info->proc, WCB16_PASCAL, sizeof(args), args, &ret );
80     return LOWORD(ret);
81 }
82
83 /* callback for 16-bit gray string proc with string pointer */
84 static BOOL CALLBACK gray_string_callback_ptr( HDC hdc, LPARAM param, INT len )
85 {
86     const struct gray_string_info *info;
87     char *str = (char *)param;
88
89     info = (struct gray_string_info *)(str - offsetof( struct gray_string_info, str ));
90     return gray_string_callback( hdc, (LPARAM)info, len );
91 }
92
93 struct draw_state_info
94 {
95     DRAWSTATEPROC16 proc;
96     LPARAM          param;
97 };
98
99 /* callback for 16-bit DrawState functions */
100 static BOOL CALLBACK draw_state_callback( HDC hdc, LPARAM lparam, WPARAM wparam, int cx, int cy )
101 {
102     const struct draw_state_info *info = (struct draw_state_info *)lparam;
103     WORD args[6];
104     DWORD ret;
105
106     args[5] = HDC_16(hdc);
107     args[4] = HIWORD(info->param);
108     args[3] = LOWORD(info->param);
109     args[2] = wparam;
110     args[1] = cx;
111     args[0] = cy;
112     WOWCallback16Ex( (DWORD)info->proc, WCB16_PASCAL, sizeof(args), args, &ret );
113     return LOWORD(ret);
114 }
115
116
117 /**********************************************************************
118  *              InitApp (USER.5)
119  */
120 INT16 WINAPI InitApp16( HINSTANCE16 hInstance )
121 {
122     /* Create task message queue */
123     return (InitThreadInput16( 0, 0 ) != 0);
124 }
125
126
127 /***********************************************************************
128  *              ExitWindows (USER.7)
129  */
130 BOOL16 WINAPI ExitWindows16( DWORD dwReturnCode, UINT16 wReserved )
131 {
132     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
133 }
134
135
136 /***********************************************************************
137  *              GetTimerResolution (USER.14)
138  */
139 LONG WINAPI GetTimerResolution16(void)
140 {
141     return (1000);
142 }
143
144
145 /***********************************************************************
146  *              ClipCursor (USER.16)
147  */
148 BOOL16 WINAPI ClipCursor16( const RECT16 *rect )
149 {
150     RECT rect32;
151
152     if (!rect) return ClipCursor( NULL );
153     rect32.left   = rect->left;
154     rect32.top    = rect->top;
155     rect32.right  = rect->right;
156     rect32.bottom = rect->bottom;
157     return ClipCursor( &rect32 );
158 }
159
160
161 /***********************************************************************
162  *              GetCursorPos (USER.17)
163  */
164 BOOL16 WINAPI GetCursorPos16( POINT16 *pt )
165 {
166     POINT pos;
167     if (!pt) return 0;
168     GetCursorPos(&pos);
169     pt->x = pos.x;
170     pt->y = pos.y;
171     return 1;
172 }
173
174
175 /***********************************************************************
176  *              SetCursor (USER.69)
177  */
178 HCURSOR16 WINAPI SetCursor16(HCURSOR16 hCursor)
179 {
180   return HCURSOR_16(SetCursor(HCURSOR_32(hCursor)));
181 }
182
183
184 /***********************************************************************
185  *              SetCursorPos (USER.70)
186  */
187 void WINAPI SetCursorPos16( INT16 x, INT16 y )
188 {
189     SetCursorPos( x, y );
190 }
191
192
193 /***********************************************************************
194  *              ShowCursor (USER.71)
195  */
196 INT16 WINAPI ShowCursor16(BOOL16 bShow)
197 {
198   return ShowCursor(bShow);
199 }
200
201
202 /***********************************************************************
203  *              SetRect (USER.72)
204  */
205 void WINAPI SetRect16( LPRECT16 rect, INT16 left, INT16 top, INT16 right, INT16 bottom )
206 {
207     rect->left   = left;
208     rect->right  = right;
209     rect->top    = top;
210     rect->bottom = bottom;
211 }
212
213
214 /***********************************************************************
215  *              SetRectEmpty (USER.73)
216  */
217 void WINAPI SetRectEmpty16( LPRECT16 rect )
218 {
219     rect->left = rect->right = rect->top = rect->bottom = 0;
220 }
221
222
223 /***********************************************************************
224  *              CopyRect (USER.74)
225  */
226 BOOL16 WINAPI CopyRect16( RECT16 *dest, const RECT16 *src )
227 {
228     *dest = *src;
229     return TRUE;
230 }
231
232
233 /***********************************************************************
234  *              IsRectEmpty (USER.75)
235  *
236  * Bug compat: Windows checks for 0 or negative width/height.
237  */
238 BOOL16 WINAPI IsRectEmpty16( const RECT16 *rect )
239 {
240     return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
241 }
242
243
244 /***********************************************************************
245  *              PtInRect (USER.76)
246  */
247 BOOL16 WINAPI PtInRect16( const RECT16 *rect, POINT16 pt )
248 {
249     return ((pt.x >= rect->left) && (pt.x < rect->right) &&
250             (pt.y >= rect->top) && (pt.y < rect->bottom));
251 }
252
253
254 /***********************************************************************
255  *              OffsetRect (USER.77)
256  */
257 void WINAPI OffsetRect16( LPRECT16 rect, INT16 x, INT16 y )
258 {
259     rect->left   += x;
260     rect->right  += x;
261     rect->top    += y;
262     rect->bottom += y;
263 }
264
265
266 /***********************************************************************
267  *              InflateRect (USER.78)
268  */
269 void WINAPI InflateRect16( LPRECT16 rect, INT16 x, INT16 y )
270 {
271     rect->left   -= x;
272     rect->top    -= y;
273     rect->right  += x;
274     rect->bottom += y;
275 }
276
277
278 /***********************************************************************
279  *              IntersectRect (USER.79)
280  */
281 BOOL16 WINAPI IntersectRect16( LPRECT16 dest, const RECT16 *src1,
282                                const RECT16 *src2 )
283 {
284     if (IsRectEmpty16(src1) || IsRectEmpty16(src2) ||
285         (src1->left >= src2->right) || (src2->left >= src1->right) ||
286         (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
287     {
288         SetRectEmpty16( dest );
289         return FALSE;
290     }
291     dest->left   = max( src1->left, src2->left );
292     dest->right  = min( src1->right, src2->right );
293     dest->top    = max( src1->top, src2->top );
294     dest->bottom = min( src1->bottom, src2->bottom );
295     return TRUE;
296 }
297
298
299 /***********************************************************************
300  *              UnionRect (USER.80)
301  */
302 BOOL16 WINAPI UnionRect16( LPRECT16 dest, const RECT16 *src1,
303                            const RECT16 *src2 )
304 {
305     if (IsRectEmpty16(src1))
306     {
307         if (IsRectEmpty16(src2))
308         {
309             SetRectEmpty16( dest );
310             return FALSE;
311         }
312         else *dest = *src2;
313     }
314     else
315     {
316         if (IsRectEmpty16(src2)) *dest = *src1;
317         else
318         {
319             dest->left   = min( src1->left, src2->left );
320             dest->right  = max( src1->right, src2->right );
321             dest->top    = min( src1->top, src2->top );
322             dest->bottom = max( src1->bottom, src2->bottom );
323         }
324     }
325     return TRUE;
326 }
327
328
329 /***********************************************************************
330  *              FillRect (USER.81)
331  * NOTE
332  *   The Win16 variant doesn't support special color brushes like
333  *   the Win32 one, despite the fact that Win16, as well as Win32,
334  *   supports special background brushes for a window class.
335  */
336 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
337 {
338     HBRUSH prevBrush;
339
340     /* coordinates are logical so we cannot fast-check 'rect',
341      * it will be done later in the PatBlt().
342      */
343
344     if (!(prevBrush = SelectObject( HDC_32(hdc), HBRUSH_32(hbrush) ))) return 0;
345     PatBlt( HDC_32(hdc), rect->left, rect->top,
346               rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
347     SelectObject( HDC_32(hdc), prevBrush );
348     return 1;
349 }
350
351
352 /***********************************************************************
353  *              InvertRect (USER.82)
354  */
355 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
356 {
357     PatBlt( HDC_32(hdc), rect->left, rect->top,
358               rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
359 }
360
361
362 /***********************************************************************
363  *              FrameRect (USER.83)
364  */
365 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
366 {
367     RECT rect;
368
369     rect.left   = rect16->left;
370     rect.top    = rect16->top;
371     rect.right  = rect16->right;
372     rect.bottom = rect16->bottom;
373     return FrameRect( HDC_32(hdc), &rect, HBRUSH_32(hbrush) );
374 }
375
376
377 /***********************************************************************
378  *              DrawIcon (USER.84)
379  */
380 BOOL16 WINAPI DrawIcon16(HDC16 hdc, INT16 x, INT16 y, HICON16 hIcon)
381 {
382   return DrawIcon(HDC_32(hdc), x, y, HICON_32(hIcon));
383 }
384
385
386 /***********************************************************************
387  *           DrawText    (USER.85)
388  */
389 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
390 {
391     INT16 ret;
392
393     if (rect)
394     {
395         RECT rect32;
396
397         rect32.left   = rect->left;
398         rect32.top    = rect->top;
399         rect32.right  = rect->right;
400         rect32.bottom = rect->bottom;
401         ret = DrawTextA( HDC_32(hdc), str, count, &rect32, flags );
402         rect->left   = rect32.left;
403         rect->top    = rect32.top;
404         rect->right  = rect32.right;
405         rect->bottom = rect32.bottom;
406     }
407     else ret = DrawTextA( HDC_32(hdc), str, count, NULL, flags);
408     return ret;
409 }
410
411
412 /***********************************************************************
413  *              IconSize (USER.86)
414  *
415  * See "Undocumented Windows". Used by W2.0 paint.exe.
416  */
417 DWORD WINAPI IconSize16(void)
418 {
419   return MAKELONG(GetSystemMetrics(SM_CYICON), GetSystemMetrics(SM_CXICON));
420 }
421
422
423 /***********************************************************************
424  *              AdjustWindowRect (USER.102)
425  */
426 BOOL16 WINAPI AdjustWindowRect16( LPRECT16 rect, DWORD style, BOOL16 menu )
427 {
428     return AdjustWindowRectEx16( rect, style, menu, 0 );
429 }
430
431
432 /***********************************************************************
433  *              MessageBeep (USER.104)
434  */
435 void WINAPI MessageBeep16( UINT16 i )
436 {
437     MessageBeep( i );
438 }
439
440
441 /**************************************************************************
442  *              CloseClipboard (USER.138)
443  */
444 BOOL16 WINAPI CloseClipboard16(void)
445 {
446     return CloseClipboard();
447 }
448
449
450 /**************************************************************************
451  *              EmptyClipboard (USER.139)
452  */
453 BOOL16 WINAPI EmptyClipboard16(void)
454 {
455     return EmptyClipboard();
456 }
457
458
459 /**************************************************************************
460  *              CountClipboardFormats (USER.143)
461  */
462 INT16 WINAPI CountClipboardFormats16(void)
463 {
464     return CountClipboardFormats();
465 }
466
467
468 /**************************************************************************
469  *              EnumClipboardFormats (USER.144)
470  */
471 UINT16 WINAPI EnumClipboardFormats16( UINT16 id )
472 {
473     return EnumClipboardFormats( id );
474 }
475
476
477 /**************************************************************************
478  *              RegisterClipboardFormat (USER.145)
479  */
480 UINT16 WINAPI RegisterClipboardFormat16( LPCSTR name )
481 {
482     return RegisterClipboardFormatA( name );
483 }
484
485
486 /**************************************************************************
487  *              GetClipboardFormatName (USER.146)
488  */
489 INT16 WINAPI GetClipboardFormatName16( UINT16 id, LPSTR buffer, INT16 maxlen )
490 {
491     return GetClipboardFormatNameA( id, buffer, maxlen );
492 }
493
494
495 /**********************************************************************
496  *         CreateMenu    (USER.151)
497  */
498 HMENU16 WINAPI CreateMenu16(void)
499 {
500     return HMENU_16( CreateMenu() );
501 }
502
503
504 /**********************************************************************
505  *         DestroyMenu    (USER.152)
506  */
507 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
508 {
509     return DestroyMenu( HMENU_32(hMenu) );
510 }
511
512
513 /*******************************************************************
514  *         ChangeMenu    (USER.153)
515  */
516 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
517                             UINT16 id, UINT16 flags )
518 {
519     if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND, id, data );
520
521     /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
522     /* for MF_DELETE. We should check the parameters for all others */
523     /* MF_* actions also (anybody got a doc on ChangeMenu?). */
524
525     if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
526     if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE, id, data );
527     if (flags & MF_REMOVE) return RemoveMenu16(hMenu, flags & MF_BYPOSITION ? pos : id,
528                                                flags & ~MF_REMOVE );
529     /* Default: MF_INSERT */
530     return InsertMenu16( hMenu, pos, flags, id, data );
531 }
532
533
534 /*******************************************************************
535  *         CheckMenuItem    (USER.154)
536  */
537 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
538 {
539     return CheckMenuItem( HMENU_32(hMenu), id, flags );
540 }
541
542
543 /**********************************************************************
544  *         EnableMenuItem    (USER.155)
545  */
546 BOOL16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
547 {
548     return EnableMenuItem( HMENU_32(hMenu), wItemID, wFlags );
549 }
550
551
552 /**********************************************************************
553  *         GetSubMenu    (USER.159)
554  */
555 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
556 {
557     return HMENU_16( GetSubMenu( HMENU_32(hMenu), nPos ) );
558 }
559
560
561 /*******************************************************************
562  *         GetMenuString    (USER.161)
563  */
564 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
565                               LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
566 {
567     return GetMenuStringA( HMENU_32(hMenu), wItemID, str, nMaxSiz, wFlags );
568 }
569
570
571 /**********************************************************************
572  *              WinHelp (USER.171)
573  */
574 BOOL16 WINAPI WinHelp16( HWND16 hWnd, LPCSTR lpHelpFile, UINT16 wCommand,
575                          DWORD dwData )
576 {
577     BOOL ret;
578     DWORD mutex_count;
579
580     /* We might call WinExec() */
581     ReleaseThunkLock(&mutex_count);
582
583     ret = WinHelpA(WIN_Handle32(hWnd), lpHelpFile, wCommand, (DWORD)MapSL(dwData));
584
585     RestoreThunkLock(mutex_count);
586     return ret;
587 }
588
589
590 /***********************************************************************
591  *              LoadCursor (USER.173)
592  */
593 HCURSOR16 WINAPI LoadCursor16(HINSTANCE16 hInstance, LPCSTR name)
594 {
595   return HCURSOR_16(LoadCursorA(HINSTANCE_32(hInstance), name));
596 }
597
598
599 /***********************************************************************
600  *              LoadIcon (USER.174)
601  */
602 HICON16 WINAPI LoadIcon16(HINSTANCE16 hInstance, LPCSTR name)
603 {
604   return HICON_16(LoadIconA(HINSTANCE_32(hInstance), name));
605 }
606
607 /**********************************************************************
608  *              LoadBitmap (USER.175)
609  */
610 HBITMAP16 WINAPI LoadBitmap16(HINSTANCE16 hInstance, LPCSTR name)
611 {
612   return HBITMAP_16(LoadBitmapA(HINSTANCE_32(hInstance), name));
613 }
614
615 /**********************************************************************
616  *     LoadString   (USER.176)
617  */
618 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id, LPSTR buffer, INT16 buflen )
619 {
620     HGLOBAL16 hmem;
621     HRSRC16 hrsrc;
622     unsigned char *p;
623     int string_num;
624     int ret;
625
626     TRACE("inst=%04x id=%04x buff=%p len=%d\n", instance, resource_id, buffer, buflen);
627
628     hrsrc = FindResource16( instance, MAKEINTRESOURCEA((resource_id>>4)+1), (LPSTR)RT_STRING );
629     if (!hrsrc) return 0;
630     hmem = LoadResource16( instance, hrsrc );
631     if (!hmem) return 0;
632
633     p = LockResource16(hmem);
634     string_num = resource_id & 0x000f;
635     while (string_num--) p += *p + 1;
636
637     if (buffer == NULL) ret = *p;
638     else
639     {
640         ret = min(buflen - 1, *p);
641         if (ret > 0)
642         {
643             memcpy(buffer, p + 1, ret);
644             buffer[ret] = '\0';
645         }
646         else if (buflen > 1)
647         {
648             buffer[0] = '\0';
649             ret = 0;
650         }
651         TRACE( "%s loaded\n", debugstr_a(buffer));
652     }
653     FreeResource16( hmem );
654     return ret;
655 }
656
657 /**********************************************************************
658  *              LoadAccelerators  (USER.177)
659  */
660 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
661 {
662     HRSRC16 hRsrc;
663     HGLOBAL16 hMem;
664     ACCEL16 *table16;
665     HACCEL ret = 0;
666
667     TRACE("%04x %s\n", instance, debugstr_a(lpTableName) );
668
669     if (!(hRsrc = FindResource16( instance, lpTableName, (LPSTR)RT_ACCELERATOR )) ||
670         !(hMem = LoadResource16(instance,hRsrc)))
671     {
672         WARN("couldn't find %04x %s\n", instance, debugstr_a(lpTableName));
673         return 0;
674     }
675     if ((table16 = LockResource16( hMem )))
676     {
677         DWORD i, count = SizeofResource16( instance, hRsrc ) / sizeof(*table16);
678         ACCEL *table = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*table) );
679         if (table)
680         {
681             for (i = 0; i < count; i++)
682             {
683                 table[i].fVirt = table16[i].fVirt & 0x7f;
684                 table[i].key   = table16[i].key;
685                 table[i].cmd   = table16[i].cmd;
686             }
687             ret = CreateAcceleratorTableA( table, count );
688             HeapFree( GetProcessHeap(), 0, table );
689         }
690     }
691     FreeResource16( hMem );
692     return HACCEL_16(ret);
693 }
694
695 /***********************************************************************
696  *              GetSystemMetrics (USER.179)
697  */
698 INT16 WINAPI GetSystemMetrics16( INT16 index )
699 {
700     return GetSystemMetrics( index );
701 }
702
703
704 /*************************************************************************
705  *              GetSysColor (USER.180)
706  */
707 COLORREF WINAPI GetSysColor16( INT16 index )
708 {
709     return GetSysColor( index );
710 }
711
712
713 /*************************************************************************
714  *              SetSysColors (USER.181)
715  */
716 VOID WINAPI SetSysColors16( INT16 count, const INT16 *list16, const COLORREF *values )
717 {
718     INT i, *list;
719
720     if ((list = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*list) )))
721     {
722         for (i = 0; i < count; i++) list[i] = list16[i];
723         SetSysColors( count, list, values );
724         HeapFree( GetProcessHeap(), 0, list );
725     }
726 }
727
728
729 /***********************************************************************
730  *           GrayString   (USER.185)
731  */
732 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
733                             LPARAM lParam, INT16 cch, INT16 x, INT16 y,
734                             INT16 cx, INT16 cy )
735 {
736     BOOL ret;
737
738     if (!gsprc) return GrayStringA( HDC_32(hdc), HBRUSH_32(hbr), NULL,
739                                     (LPARAM)MapSL(lParam), cch, x, y, cx, cy );
740
741     if (cch == -1 || (cch && cx && cy))
742     {
743         /* lParam can be treated as an opaque pointer */
744         struct gray_string_info info;
745
746         info.proc  = gsprc;
747         info.param = lParam;
748         ret = GrayStringA( HDC_32(hdc), HBRUSH_32(hbr), gray_string_callback,
749                            (LPARAM)&info, cch, x, y, cx, cy );
750     }
751     else  /* here we need some string conversions */
752     {
753         char *str16 = MapSL(lParam);
754         struct gray_string_info *info;
755
756         if (!cch) cch = strlen(str16);
757         if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) + cch ))) return FALSE;
758         info->proc  = gsprc;
759         info->param = lParam;
760         memcpy( info->str, str16, cch );
761         ret = GrayStringA( HDC_32(hdc), HBRUSH_32(hbr), gray_string_callback_ptr,
762                            (LPARAM)info->str, cch, x, y, cx, cy );
763         HeapFree( GetProcessHeap(), 0, info );
764     }
765     return ret;
766 }
767
768
769 /***********************************************************************
770  *              SwapMouseButton (USER.186)
771  */
772 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
773 {
774     return SwapMouseButton( fSwap );
775 }
776
777
778 /**************************************************************************
779  *              IsClipboardFormatAvailable (USER.193)
780  */
781 BOOL16 WINAPI IsClipboardFormatAvailable16( UINT16 wFormat )
782 {
783     return IsClipboardFormatAvailable( wFormat );
784 }
785
786
787 /***********************************************************************
788  *           TabbedTextOut    (USER.196)
789  */
790 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
791                              INT16 count, INT16 nb_tabs, const INT16 *tabs16, INT16 tab_org )
792 {
793     LONG ret;
794     INT i, *tabs = HeapAlloc( GetProcessHeap(), 0, nb_tabs * sizeof(tabs) );
795     if (!tabs) return 0;
796     for (i = 0; i < nb_tabs; i++) tabs[i] = tabs16[i];
797     ret = TabbedTextOutA( HDC_32(hdc), x, y, lpstr, count, nb_tabs, tabs, tab_org );
798     HeapFree( GetProcessHeap(), 0, tabs );
799     return ret;
800 }
801
802
803 /***********************************************************************
804  *           GetTabbedTextExtent    (USER.197)
805  */
806 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
807                                     INT16 nb_tabs, const INT16 *tabs16 )
808 {
809     LONG ret;
810     INT i, *tabs = HeapAlloc( GetProcessHeap(), 0, nb_tabs * sizeof(tabs) );
811     if (!tabs) return 0;
812     for (i = 0; i < nb_tabs; i++) tabs[i] = tabs16[i];
813     ret = GetTabbedTextExtentA( HDC_32(hdc), lpstr, count, nb_tabs, tabs );
814     HeapFree( GetProcessHeap(), 0, tabs );
815     return ret;
816 }
817
818
819 /***********************************************************************
820  *              UserSeeUserDo (USER.216)
821  */
822 DWORD WINAPI UserSeeUserDo16(WORD wReqType, WORD wParam1, WORD wParam2, WORD wParam3)
823 {
824     STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
825     HANDLE16 oldDS = stack16->ds;
826     DWORD ret = (DWORD)-1;
827
828     stack16->ds = USER_HeapSel;
829     switch (wReqType)
830     {
831     case USUD_LOCALALLOC:
832         ret = LocalAlloc16(wParam1, wParam3);
833         break;
834     case USUD_LOCALFREE:
835         ret = LocalFree16(wParam1);
836         break;
837     case USUD_LOCALCOMPACT:
838         ret = LocalCompact16(wParam3);
839         break;
840     case USUD_LOCALHEAP:
841         ret = USER_HeapSel;
842         break;
843     case USUD_FIRSTCLASS:
844         FIXME("return a pointer to the first window class.\n");
845         break;
846     default:
847         WARN("wReqType %04x (unknown)\n", wReqType);
848     }
849     stack16->ds = oldDS;
850     return ret;
851 }
852
853
854 /*************************************************************************
855  *              ScrollDC (USER.221)
856  */
857 BOOL16 WINAPI ScrollDC16( HDC16 hdc, INT16 dx, INT16 dy, const RECT16 *rect,
858                           const RECT16 *cliprc, HRGN16 hrgnUpdate,
859                           LPRECT16 rcUpdate )
860 {
861     RECT rect32, clipRect32, rcUpdate32;
862     BOOL16 ret;
863
864     if (rect)
865     {
866         rect32.left   = rect->left;
867         rect32.top    = rect->top;
868         rect32.right  = rect->right;
869         rect32.bottom = rect->bottom;
870     }
871     if (cliprc)
872     {
873         clipRect32.left   = cliprc->left;
874         clipRect32.top    = cliprc->top;
875         clipRect32.right  = cliprc->right;
876         clipRect32.bottom = cliprc->bottom;
877     }
878     ret = ScrollDC( HDC_32(hdc), dx, dy, rect ? &rect32 : NULL,
879                     cliprc ? &clipRect32 : NULL, HRGN_32(hrgnUpdate),
880                     &rcUpdate32 );
881     if (rcUpdate)
882     {
883         rcUpdate->left   = rcUpdate32.left;
884         rcUpdate->top    = rcUpdate32.top;
885         rcUpdate->right  = rcUpdate32.right;
886         rcUpdate->bottom = rcUpdate32.bottom;
887     }
888     return ret;
889 }
890
891
892 /***********************************************************************
893  *              GetSystemDebugState (USER.231)
894  */
895 WORD WINAPI GetSystemDebugState16(void)
896 {
897     return 0;  /* FIXME */
898 }
899
900
901 /***********************************************************************
902  *              EqualRect (USER.244)
903  */
904 BOOL16 WINAPI EqualRect16( const RECT16* rect1, const RECT16* rect2 )
905 {
906     return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
907             (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
908 }
909
910
911 /***********************************************************************
912  *              ExitWindowsExec (USER.246)
913  */
914 BOOL16 WINAPI ExitWindowsExec16( LPCSTR lpszExe, LPCSTR lpszParams )
915 {
916     TRACE("Should run the following in DOS-mode: \"%s %s\"\n",
917           lpszExe, lpszParams);
918     return ExitWindowsEx( EWX_LOGOFF, 0xffffffff );
919 }
920
921
922 /***********************************************************************
923  *              GetCursor (USER.247)
924  */
925 HCURSOR16 WINAPI GetCursor16(void)
926 {
927   return HCURSOR_16(GetCursor());
928 }
929
930
931 /**********************************************************************
932  *              GetAsyncKeyState (USER.249)
933  */
934 INT16 WINAPI GetAsyncKeyState16( INT16 key )
935 {
936     return GetAsyncKeyState( key );
937 }
938
939
940 /**********************************************************************
941  *         GetMenuState    (USER.250)
942  */
943 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
944 {
945     return GetMenuState( HMENU_32(hMenu), wItemID, wFlags );
946 }
947
948
949 /**************************************************************************
950  *              SendDriverMessage (USER.251)
951  */
952 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
953                                    LPARAM lParam2)
954 {
955     FIXME("(%04x, %04x, %08lx, %08lx): stub\n", hDriver, msg, lParam1, lParam2);
956     return 0;
957 }
958
959
960 /**************************************************************************
961  *              OpenDriver (USER.252)
962  */
963 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
964 {
965     FIXME( "(%s, %s, %08lx): stub\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
966     return 0;
967 }
968
969
970 /**************************************************************************
971  *              CloseDriver (USER.253)
972  */
973 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
974 {
975     FIXME( "(%04x, %08lx, %08lx): stub\n", hDrvr, lParam1, lParam2);
976     return FALSE;
977 }
978
979
980 /**************************************************************************
981  *              GetDriverModuleHandle (USER.254)
982  */
983 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
984 {
985     FIXME("(%04x): stub\n", hDrvr);
986     return 0;
987 }
988
989
990 /**************************************************************************
991  *              DefDriverProc (USER.255)
992  */
993 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
994                                LPARAM lParam1, LPARAM lParam2)
995 {
996     FIXME( "devID=0x%08x hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx: stub\n",
997           dwDevID, hDriv, wMsg, lParam1, lParam2);
998     return 0;
999 }
1000
1001
1002 /**************************************************************************
1003  *              GetDriverInfo (USER.256)
1004  */
1005 struct DRIVERINFOSTRUCT16;
1006 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, struct DRIVERINFOSTRUCT16 *lpDrvInfo)
1007 {
1008     FIXME( "(%04x, %p): stub\n", hDrvr, lpDrvInfo);
1009     return FALSE;
1010 }
1011
1012
1013 /**************************************************************************
1014  *              GetNextDriver (USER.257)
1015  */
1016 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
1017 {
1018     FIXME( "(%04x, %08x): stub\n", hDrvr, dwFlags);
1019     return 0;
1020 }
1021
1022
1023 /**********************************************************************
1024  *         GetMenuItemCount    (USER.263)
1025  */
1026 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
1027 {
1028     return GetMenuItemCount( HMENU_32(hMenu) );
1029 }
1030
1031
1032 /**********************************************************************
1033  *         GetMenuItemID    (USER.264)
1034  */
1035 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
1036 {
1037     return GetMenuItemID( HMENU_32(hMenu), nPos );
1038 }
1039
1040
1041 /***********************************************************************
1042  *              GlobalAddAtom (USER.268)
1043  */
1044 ATOM WINAPI GlobalAddAtom16(LPCSTR lpString)
1045 {
1046   return GlobalAddAtomA(lpString);
1047 }
1048
1049 /***********************************************************************
1050  *              GlobalDeleteAtom (USER.269)
1051  */
1052 ATOM WINAPI GlobalDeleteAtom16(ATOM nAtom)
1053 {
1054   return GlobalDeleteAtom(nAtom);
1055 }
1056
1057 /***********************************************************************
1058  *              GlobalFindAtom (USER.270)
1059  */
1060 ATOM WINAPI GlobalFindAtom16(LPCSTR lpString)
1061 {
1062   return GlobalFindAtomA(lpString);
1063 }
1064
1065 /***********************************************************************
1066  *              GlobalGetAtomName (USER.271)
1067  */
1068 UINT16 WINAPI GlobalGetAtomName16(ATOM nAtom, LPSTR lpBuffer, INT16 nSize)
1069 {
1070   return GlobalGetAtomNameA(nAtom, lpBuffer, nSize);
1071 }
1072
1073
1074 /***********************************************************************
1075  *              ControlPanelInfo (USER.273)
1076  */
1077 void WINAPI ControlPanelInfo16( INT16 nInfoType, WORD wData, LPSTR lpBuffer )
1078 {
1079     FIXME("(%d, %04x, %p): stub.\n", nInfoType, wData, lpBuffer);
1080 }
1081
1082
1083 /***********************************************************************
1084  *           OldSetDeskPattern   (USER.279)
1085  */
1086 BOOL16 WINAPI SetDeskPattern16(void)
1087 {
1088     return SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
1089 }
1090
1091
1092 /***********************************************************************
1093  *              GetSysColorBrush (USER.281)
1094  */
1095 HBRUSH16 WINAPI GetSysColorBrush16( INT16 index )
1096 {
1097     return HBRUSH_16( GetSysColorBrush(index) );
1098 }
1099
1100
1101 /***********************************************************************
1102  *              SelectPalette (USER.282)
1103  */
1104 HPALETTE16 WINAPI SelectPalette16( HDC16 hdc, HPALETTE16 hpal, BOOL16 bForceBackground )
1105 {
1106     return HPALETTE_16( SelectPalette( HDC_32(hdc), HPALETTE_32(hpal), bForceBackground ));
1107 }
1108
1109 /***********************************************************************
1110  *              RealizePalette (USER.283)
1111  */
1112 UINT16 WINAPI RealizePalette16( HDC16 hdc )
1113 {
1114     return UserRealizePalette( HDC_32(hdc) );
1115 }
1116
1117
1118 /***********************************************************************
1119  *              GetFreeSystemResources (USER.284)
1120  */
1121 WORD WINAPI GetFreeSystemResources16( WORD resType )
1122 {
1123     STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1124     HANDLE16 oldDS = stack16->ds;
1125     HINSTANCE16 gdi_inst;
1126     int userPercent, gdiPercent;
1127
1128     if ((gdi_inst = LoadLibrary16( "GDI" )) < 32) return 0;
1129
1130     switch(resType)
1131     {
1132     case GFSR_USERRESOURCES:
1133         stack16->ds = USER_HeapSel;
1134         userPercent = (int)LocalCountFree16() * 100 / LocalHeapSize16();
1135         gdiPercent  = 100;
1136         stack16->ds = oldDS;
1137         break;
1138
1139     case GFSR_GDIRESOURCES:
1140         stack16->ds = gdi_inst;
1141         gdiPercent  = (int)LocalCountFree16() * 100 / LocalHeapSize16();
1142         userPercent = 100;
1143         stack16->ds = oldDS;
1144         break;
1145
1146     case GFSR_SYSTEMRESOURCES:
1147         stack16->ds = USER_HeapSel;
1148         userPercent = (int)LocalCountFree16() * 100 / LocalHeapSize16();
1149         stack16->ds = gdi_inst;
1150         gdiPercent  = (int)LocalCountFree16() * 100 / LocalHeapSize16();
1151         stack16->ds = oldDS;
1152         break;
1153
1154     default:
1155         userPercent = gdiPercent = 0;
1156         break;
1157     }
1158     FreeLibrary16( gdi_inst );
1159     TRACE("<- userPercent %d, gdiPercent %d\n", userPercent, gdiPercent);
1160     return (WORD)min( userPercent, gdiPercent );
1161 }
1162
1163
1164 /***********************************************************************
1165  *           SetDeskWallPaper   (USER.285)
1166  */
1167 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
1168 {
1169     return SetDeskWallPaper( filename );
1170 }
1171
1172
1173 /***********************************************************************
1174  *              keybd_event (USER.289)
1175  */
1176 void WINAPI keybd_event16( CONTEXT86 *context )
1177 {
1178     DWORD dwFlags = 0;
1179
1180     if (HIBYTE(context->Eax) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
1181     if (HIBYTE(context->Ebx) & 0x01) dwFlags |= KEYEVENTF_EXTENDEDKEY;
1182
1183     keybd_event( LOBYTE(context->Eax), LOBYTE(context->Ebx),
1184                  dwFlags, MAKELONG(LOWORD(context->Esi), LOWORD(context->Edi)) );
1185 }
1186
1187
1188 /***********************************************************************
1189  *              mouse_event (USER.299)
1190  */
1191 void WINAPI mouse_event16( CONTEXT86 *context )
1192 {
1193     mouse_event( LOWORD(context->Eax), LOWORD(context->Ebx), LOWORD(context->Ecx),
1194                  LOWORD(context->Edx), MAKELONG(context->Esi, context->Edi) );
1195 }
1196
1197
1198 /***********************************************************************
1199  *              GetClipCursor (USER.309)
1200  */
1201 void WINAPI GetClipCursor16( RECT16 *rect )
1202 {
1203     if (rect)
1204     {
1205         RECT rect32;
1206         GetClipCursor( &rect32 );
1207         rect->left   = rect32.left;
1208         rect->top    = rect32.top;
1209         rect->right  = rect32.right;
1210         rect->bottom = rect32.bottom;
1211     }
1212 }
1213
1214
1215 /***********************************************************************
1216  *              SignalProc (USER.314)
1217  */
1218 void WINAPI SignalProc16( HANDLE16 hModule, UINT16 code,
1219                           UINT16 uExitFn, HINSTANCE16 hInstance, HQUEUE16 hQueue )
1220 {
1221     if (code == USIG16_DLL_UNLOAD)
1222     {
1223         /* HOOK_FreeModuleHooks( hModule ); */
1224         CLASS_FreeModuleClasses( hModule );
1225         CURSORICON_FreeModuleIcons( hModule );
1226     }
1227 }
1228
1229
1230 /***********************************************************************
1231  *              SetEventHook (USER.321)
1232  *
1233  *      Used by Turbo Debugger for Windows
1234  */
1235 FARPROC16 WINAPI SetEventHook16(FARPROC16 lpfnEventHook)
1236 {
1237     FIXME("(lpfnEventHook=%p): stub\n", lpfnEventHook);
1238     return 0;
1239 }
1240
1241
1242 /**********************************************************************
1243  *              EnableHardwareInput (USER.331)
1244  */
1245 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
1246 {
1247     FIXME("(%d) - stub\n", bEnable);
1248     return TRUE;
1249 }
1250
1251
1252 /***********************************************************************
1253  *              GetMouseEventProc (USER.337)
1254  */
1255 FARPROC16 WINAPI GetMouseEventProc16(void)
1256 {
1257     HMODULE16 hmodule = GetModuleHandle16("USER");
1258     return GetProcAddress16( hmodule, "mouse_event" );
1259 }
1260
1261
1262 /***********************************************************************
1263  *              IsUserIdle (USER.333)
1264  */
1265 BOOL16 WINAPI IsUserIdle16(void)
1266 {
1267     if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
1268         return FALSE;
1269     if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
1270         return FALSE;
1271     if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
1272         return FALSE;
1273     /* Should check for screen saver activation here ... */
1274     return TRUE;
1275 }
1276
1277
1278 /**********************************************************************
1279  *              LoadDIBIconHandler (USER.357)
1280  *
1281  * RT_ICON resource loader, installed by USER_SignalProc when module
1282  * is initialized.
1283  */
1284 HGLOBAL16 WINAPI LoadDIBIconHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1285 {
1286     /* If hResource is zero we must allocate a new memory block, if it's
1287      * non-zero but GlobalLock() returns NULL then it was discarded and
1288      * we have to recommit some memory, otherwise we just need to check
1289      * the block size. See LoadProc() in 16-bit SDK for more.
1290      */
1291     FIXME( "%x %x %x: stub, not supported anymore\n", hMemObj, hModule, hRsrc );
1292     return 0;
1293 }
1294
1295 /**********************************************************************
1296  *              LoadDIBCursorHandler (USER.356)
1297  *
1298  * RT_CURSOR resource loader. Same as above.
1299  */
1300 HGLOBAL16 WINAPI LoadDIBCursorHandler16( HGLOBAL16 hMemObj, HMODULE16 hModule, HRSRC16 hRsrc )
1301 {
1302     FIXME( "%x %x %x: stub, not supported anymore\n", hMemObj, hModule, hRsrc );
1303     return 0;
1304 }
1305
1306
1307 /**********************************************************************
1308  *              IsMenu    (USER.358)
1309  */
1310 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
1311 {
1312     return IsMenu( HMENU_32(hmenu) );
1313 }
1314
1315
1316 /***********************************************************************
1317  *              DCHook (USER.362)
1318  */
1319 BOOL16 WINAPI DCHook16( HDC16 hdc, WORD code, DWORD data, LPARAM lParam )
1320 {
1321     FIXME( "hDC = %x, %i: stub\n", hdc, code );
1322     return FALSE;
1323 }
1324
1325
1326 /***********************************************************************
1327  *              SubtractRect (USER.373)
1328  */
1329 BOOL16 WINAPI SubtractRect16( LPRECT16 dest, const RECT16 *src1,
1330                               const RECT16 *src2 )
1331 {
1332     RECT16 tmp;
1333
1334     if (IsRectEmpty16( src1 ))
1335     {
1336         SetRectEmpty16( dest );
1337         return FALSE;
1338     }
1339     *dest = *src1;
1340     if (IntersectRect16( &tmp, src1, src2 ))
1341     {
1342         if (EqualRect16( &tmp, dest ))
1343         {
1344             SetRectEmpty16( dest );
1345             return FALSE;
1346         }
1347         if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
1348         {
1349             if (tmp.left == dest->left) dest->left = tmp.right;
1350             else if (tmp.right == dest->right) dest->right = tmp.left;
1351         }
1352         else if ((tmp.left == dest->left) && (tmp.right == dest->right))
1353         {
1354             if (tmp.top == dest->top) dest->top = tmp.bottom;
1355             else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
1356         }
1357     }
1358     return TRUE;
1359 }
1360
1361
1362 /**********************************************************************
1363  *              DllEntryPoint (USER.374)
1364  */
1365 BOOL WINAPI DllEntryPoint( DWORD reason, HINSTANCE16 inst, WORD ds,
1366                            WORD heap, DWORD reserved1, WORD reserved2 )
1367 {
1368     if (reason != DLL_PROCESS_ATTACH) return TRUE;
1369     if (USER_HeapSel) return TRUE;  /* already called */
1370
1371     USER_HeapSel = ds;
1372     return TRUE;
1373 }
1374
1375
1376 /**********************************************************************
1377  *         SetMenuContextHelpId    (USER.384)
1378  */
1379 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpID)
1380 {
1381     return SetMenuContextHelpId( HMENU_32(hMenu), dwContextHelpID );
1382 }
1383
1384
1385 /**********************************************************************
1386  *         GetMenuContextHelpId    (USER.385)
1387  */
1388 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
1389 {
1390     return GetMenuContextHelpId( HMENU_32(hMenu) );
1391 }
1392
1393
1394 /***********************************************************************
1395  *              LoadImage (USER.389)
1396  *
1397  */
1398 HANDLE16 WINAPI LoadImage16(HINSTANCE16 hinst, LPCSTR name, UINT16 type,
1399                             INT16 desiredx, INT16 desiredy, UINT16 loadflags)
1400 {
1401   return HANDLE_16(LoadImageA(HINSTANCE_32(hinst), name, type, desiredx,
1402                               desiredy, loadflags));
1403 }
1404
1405 /******************************************************************************
1406  *              CopyImage (USER.390) Creates new image and copies attributes to it
1407  *
1408  */
1409 HICON16 WINAPI CopyImage16(HANDLE16 hnd, UINT16 type, INT16 desiredx,
1410                            INT16 desiredy, UINT16 flags)
1411 {
1412   return HICON_16(CopyImage(HANDLE_32(hnd), (UINT)type, (INT)desiredx,
1413                             (INT)desiredy, (UINT)flags));
1414 }
1415
1416 /**********************************************************************
1417  *              DrawIconEx (USER.394)
1418  */
1419 BOOL16 WINAPI DrawIconEx16(HDC16 hdc, INT16 xLeft, INT16 yTop, HICON16 hIcon,
1420                            INT16 cxWidth, INT16 cyWidth, UINT16 istep,
1421                            HBRUSH16 hbr, UINT16 flags)
1422 {
1423   return DrawIconEx(HDC_32(hdc), xLeft, yTop, HICON_32(hIcon), cxWidth, cyWidth,
1424                     istep, HBRUSH_32(hbr), flags);
1425 }
1426
1427 /**********************************************************************
1428  *              GetIconInfo (USER.395)
1429  */
1430 BOOL16 WINAPI GetIconInfo16(HICON16 hIcon, LPICONINFO16 iconinfo)
1431 {
1432   ICONINFO ii32;
1433   BOOL16 ret = GetIconInfo(HICON_32(hIcon), &ii32);
1434
1435   iconinfo->fIcon = ii32.fIcon;
1436   iconinfo->xHotspot = ii32.xHotspot;
1437   iconinfo->yHotspot = ii32.yHotspot;
1438   iconinfo->hbmMask  = HBITMAP_16(ii32.hbmMask);
1439   iconinfo->hbmColor = HBITMAP_16(ii32.hbmColor);
1440   return ret;
1441 }
1442
1443
1444 /***********************************************************************
1445  *              FinalUserInit (USER.400)
1446  */
1447 void WINAPI FinalUserInit16( void )
1448 {
1449     /* FIXME: Should chain to FinalGdiInit */
1450 }
1451
1452
1453 /***********************************************************************
1454  *              CreateCursor (USER.406)
1455  */
1456 HCURSOR16 WINAPI CreateCursor16(HINSTANCE16 hInstance,
1457                                 INT16 xHotSpot, INT16 yHotSpot,
1458                                 INT16 nWidth, INT16 nHeight,
1459                                 LPCVOID lpANDbits, LPCVOID lpXORbits)
1460 {
1461   CURSORICONINFO info;
1462
1463   info.ptHotSpot.x = xHotSpot;
1464   info.ptHotSpot.y = yHotSpot;
1465   info.nWidth = nWidth;
1466   info.nHeight = nHeight;
1467   info.nWidthBytes = 0;
1468   info.bPlanes = 1;
1469   info.bBitsPerPixel = 1;
1470
1471   return CreateCursorIconIndirect16(hInstance, &info, lpANDbits, lpXORbits);
1472 }
1473
1474
1475 /***********************************************************************
1476  *              InitThreadInput   (USER.409)
1477  */
1478 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1479 {
1480     /* nothing to do here */
1481     return 0xbeef;
1482 }
1483
1484
1485 /*******************************************************************
1486  *         InsertMenu    (USER.410)
1487  */
1488 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
1489                             UINT16 id, SEGPTR data )
1490 {
1491     UINT pos32 = (UINT)pos;
1492     if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT)-1;
1493     if (IS_MENU_STRING_ITEM(flags) && data)
1494         return InsertMenuA( HMENU_32(hMenu), pos32, flags, id, MapSL(data) );
1495     return InsertMenuA( HMENU_32(hMenu), pos32, flags, id, (LPSTR)data );
1496 }
1497
1498
1499 /*******************************************************************
1500  *         AppendMenu    (USER.411)
1501  */
1502 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
1503 {
1504     return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
1505 }
1506
1507
1508 /**********************************************************************
1509  *         RemoveMenu   (USER.412)
1510  */
1511 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
1512 {
1513     return RemoveMenu( HMENU_32(hMenu), nPos, wFlags );
1514 }
1515
1516
1517 /**********************************************************************
1518  *         DeleteMenu    (USER.413)
1519  */
1520 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
1521 {
1522     return DeleteMenu( HMENU_32(hMenu), nPos, wFlags );
1523 }
1524
1525
1526 /*******************************************************************
1527  *         ModifyMenu    (USER.414)
1528  */
1529 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
1530                             UINT16 id, SEGPTR data )
1531 {
1532     if (IS_MENU_STRING_ITEM(flags))
1533         return ModifyMenuA( HMENU_32(hMenu), pos, flags, id, MapSL(data) );
1534     return ModifyMenuA( HMENU_32(hMenu), pos, flags, id, (LPSTR)data );
1535 }
1536
1537
1538 /**********************************************************************
1539  *         CreatePopupMenu    (USER.415)
1540  */
1541 HMENU16 WINAPI CreatePopupMenu16(void)
1542 {
1543     return HMENU_16( CreatePopupMenu() );
1544 }
1545
1546
1547 /**********************************************************************
1548  *         SetMenuItemBitmaps    (USER.418)
1549  */
1550 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
1551                                     HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
1552 {
1553     return SetMenuItemBitmaps( HMENU_32(hMenu), nPos, wFlags,
1554                                HBITMAP_32(hNewUnCheck), HBITMAP_32(hNewCheck) );
1555 }
1556
1557
1558 /***********************************************************************
1559  *           lstrcmp   (USER.430)
1560  */
1561 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
1562 {
1563     return strcmp( str1, str2 );
1564 }
1565
1566
1567 /***********************************************************************
1568  *           AnsiUpper   (USER.431)
1569  */
1570 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
1571 {
1572     /* uppercase only one char if strOrChar < 0x10000 */
1573     if (HIWORD(strOrChar))
1574     {
1575         CharUpperA( MapSL(strOrChar) );
1576         return strOrChar;
1577     }
1578     else return (SEGPTR)CharUpperA( (LPSTR)strOrChar );
1579 }
1580
1581
1582 /***********************************************************************
1583  *           AnsiLower   (USER.432)
1584  */
1585 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
1586 {
1587     /* lowercase only one char if strOrChar < 0x10000 */
1588     if (HIWORD(strOrChar))
1589     {
1590         CharLowerA( MapSL(strOrChar) );
1591         return strOrChar;
1592     }
1593     else return (SEGPTR)CharLowerA( (LPSTR)strOrChar );
1594 }
1595
1596
1597 /***********************************************************************
1598  *           AnsiUpperBuff   (USER.437)
1599  */
1600 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
1601 {
1602     CharUpperBuffA( str, len ? len : 65536 );
1603     return len;
1604 }
1605
1606
1607 /***********************************************************************
1608  *           AnsiLowerBuff   (USER.438)
1609  */
1610 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
1611 {
1612     CharLowerBuffA( str, len ? len : 65536 );
1613     return len;
1614 }
1615
1616
1617 /*******************************************************************
1618  *              InsertMenuItem   (USER.441)
1619  *
1620  * FIXME: untested
1621  */
1622 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
1623                                 const MENUITEMINFO16 *mii )
1624 {
1625     MENUITEMINFOA miia;
1626
1627     miia.cbSize        = sizeof(miia);
1628     miia.fMask         = mii->fMask;
1629     miia.dwTypeData    = (LPSTR)mii->dwTypeData;
1630     miia.fType         = mii->fType;
1631     miia.fState        = mii->fState;
1632     miia.wID           = mii->wID;
1633     miia.hSubMenu      = HMENU_32(mii->hSubMenu);
1634     miia.hbmpChecked   = HBITMAP_32(mii->hbmpChecked);
1635     miia.hbmpUnchecked = HBITMAP_32(mii->hbmpUnchecked);
1636     miia.dwItemData    = mii->dwItemData;
1637     miia.cch           = mii->cch;
1638     if (IS_MENU_STRING_ITEM(miia.fType))
1639         miia.dwTypeData = MapSL(mii->dwTypeData);
1640     return InsertMenuItemA( HMENU_32(hmenu), pos, byposition, &miia );
1641 }
1642
1643
1644 /**********************************************************************
1645  *           DrawState    (USER.449)
1646  */
1647 BOOL16 WINAPI DrawState16( HDC16 hdc, HBRUSH16 hbr, DRAWSTATEPROC16 func, LPARAM ldata,
1648                            WPARAM16 wdata, INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags )
1649 {
1650     struct draw_state_info info;
1651     UINT opcode = flags & 0xf;
1652
1653     if (opcode == DST_TEXT || opcode == DST_PREFIXTEXT)
1654     {
1655         /* make sure DrawStateA doesn't try to use ldata as a pointer */
1656         if (!wdata) wdata = strlen( MapSL(ldata) );
1657         if (!cx || !cy)
1658         {
1659             SIZE s;
1660             if (!GetTextExtentPoint32A( HDC_32(hdc), MapSL(ldata), wdata, &s )) return FALSE;
1661             if (!cx) cx = s.cx;
1662             if (!cy) cy = s.cy;
1663         }
1664     }
1665     info.proc  = func;
1666     info.param = ldata;
1667     return DrawStateA( HDC_32(hdc), HBRUSH_32(hbr), draw_state_callback,
1668                        (LPARAM)&info, wdata, x, y, cx, cy, flags );
1669 }
1670
1671
1672 /**********************************************************************
1673  *              CreateIconFromResourceEx (USER.450)
1674  *
1675  * FIXME: not sure about exact parameter types
1676  */
1677 HICON16 WINAPI CreateIconFromResourceEx16(LPBYTE bits, UINT16 cbSize,
1678                                           BOOL16 bIcon, DWORD dwVersion,
1679                                           INT16 width, INT16 height,
1680                                           UINT16 cFlag)
1681 {
1682   return HICON_16(CreateIconFromResourceEx(bits, cbSize, bIcon, dwVersion,
1683                                            width, height, cFlag));
1684 }
1685
1686
1687 /***********************************************************************
1688  *              AdjustWindowRectEx (USER.454)
1689  */
1690 BOOL16 WINAPI AdjustWindowRectEx16( LPRECT16 rect, DWORD style, BOOL16 menu, DWORD exStyle )
1691 {
1692     RECT rect32;
1693     BOOL ret;
1694
1695     rect32.left   = rect->left;
1696     rect32.top    = rect->top;
1697     rect32.right  = rect->right;
1698     rect32.bottom = rect->bottom;
1699     ret = AdjustWindowRectEx( &rect32, style, menu, exStyle );
1700     rect->left   = rect32.left;
1701     rect->top    = rect32.top;
1702     rect->right  = rect32.right;
1703     rect->bottom = rect32.bottom;
1704     return ret;
1705 }
1706
1707
1708 /***********************************************************************
1709  *              DestroyIcon (USER.457)
1710  */
1711 BOOL16 WINAPI DestroyIcon16(HICON16 hIcon)
1712 {
1713   return DestroyIcon32(hIcon, 0);
1714 }
1715
1716 /***********************************************************************
1717  *              DestroyCursor (USER.458)
1718  */
1719 BOOL16 WINAPI DestroyCursor16(HCURSOR16 hCursor)
1720 {
1721   return DestroyIcon32(hCursor, 0);
1722 }
1723
1724 /*******************************************************************
1725  *                      DRAG_QueryUpdate16
1726  *
1727  * Recursively find a child that contains spDragInfo->pt point
1728  * and send WM_QUERYDROPOBJECT. Helper for DragObject16.
1729  */
1730 static BOOL DRAG_QueryUpdate16( HWND hQueryWnd, SEGPTR spDragInfo )
1731 {
1732     BOOL bResult = 0;
1733     WPARAM wParam;
1734     POINT pt, old_pt;
1735     LPDRAGINFO16 ptrDragInfo = MapSL(spDragInfo);
1736     RECT tempRect;
1737     HWND child;
1738
1739     if (!IsWindowEnabled(hQueryWnd)) return FALSE;
1740
1741     old_pt.x = ptrDragInfo->pt.x;
1742     old_pt.y = ptrDragInfo->pt.y;
1743     pt = old_pt;
1744     ScreenToClient( hQueryWnd, &pt );
1745     child = ChildWindowFromPointEx( hQueryWnd, pt, CWP_SKIPINVISIBLE );
1746     if (!child) return FALSE;
1747
1748     if (child != hQueryWnd)
1749     {
1750         wParam = 0;
1751         if (DRAG_QueryUpdate16( child, spDragInfo )) return TRUE;
1752     }
1753     else
1754     {
1755         GetClientRect( hQueryWnd, &tempRect );
1756         wParam = !PtInRect( &tempRect, pt );
1757     }
1758
1759     ptrDragInfo->pt.x = pt.x;
1760     ptrDragInfo->pt.y = pt.y;
1761     ptrDragInfo->hScope = HWND_16(hQueryWnd);
1762
1763     bResult = SendMessage16( HWND_16(hQueryWnd), WM_QUERYDROPOBJECT, wParam, spDragInfo );
1764
1765     if (!bResult)
1766     {
1767         ptrDragInfo->pt.x = old_pt.x;
1768         ptrDragInfo->pt.y = old_pt.y;
1769     }
1770     return bResult;
1771 }
1772
1773
1774 /******************************************************************************
1775  *              DragObject (USER.464)
1776  */
1777 DWORD WINAPI DragObject16( HWND16 hwndScope, HWND16 hWnd, UINT16 wObj,
1778                            HANDLE16 hOfStruct, WORD szList, HCURSOR16 hCursor )
1779 {
1780     MSG msg;
1781     LPDRAGINFO16 lpDragInfo;
1782     SEGPTR      spDragInfo;
1783     HCURSOR     hOldCursor=0, hBummer=0;
1784     HGLOBAL16   hDragInfo  = GlobalAlloc16( GMEM_SHARE | GMEM_ZEROINIT, 2*sizeof(DRAGINFO16));
1785     HCURSOR     hCurrentCursor = 0;
1786     HWND16      hCurrentWnd = 0;
1787
1788     lpDragInfo = (LPDRAGINFO16) GlobalLock16(hDragInfo);
1789     spDragInfo = WOWGlobalLock16(hDragInfo);
1790
1791     if( !lpDragInfo || !spDragInfo ) return 0L;
1792
1793     if (!(hBummer = LoadCursorA(0, MAKEINTRESOURCEA(OCR_NO))))
1794     {
1795         GlobalFree16(hDragInfo);
1796         return 0L;
1797     }
1798
1799     if(hCursor) hOldCursor = SetCursor(HCURSOR_32(hCursor));
1800
1801     lpDragInfo->hWnd   = hWnd;
1802     lpDragInfo->hScope = 0;
1803     lpDragInfo->wFlags = wObj;
1804     lpDragInfo->hList  = szList; /* near pointer! */
1805     lpDragInfo->hOfStruct = hOfStruct;
1806     lpDragInfo->l = 0L;
1807
1808     SetCapture( HWND_32(hWnd) );
1809     ShowCursor( TRUE );
1810
1811     do
1812     {
1813         GetMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST );
1814
1815        *(lpDragInfo+1) = *lpDragInfo;
1816
1817         lpDragInfo->pt.x = msg.pt.x;
1818         lpDragInfo->pt.y = msg.pt.y;
1819
1820         /* update DRAGINFO struct */
1821         if( DRAG_QueryUpdate16(WIN_Handle32(hwndScope), spDragInfo) > 0 )
1822             hCurrentCursor = HCURSOR_32(hCursor);
1823         else
1824         {
1825             hCurrentCursor = hBummer;
1826             lpDragInfo->hScope = 0;
1827         }
1828         if( hCurrentCursor )
1829             SetCursor(hCurrentCursor);
1830
1831         /* send WM_DRAGLOOP */
1832         SendMessage16( hWnd, WM_DRAGLOOP, (WPARAM16)(hCurrentCursor != hBummer),
1833                                           (LPARAM) spDragInfo );
1834         /* send WM_DRAGSELECT or WM_DRAGMOVE */
1835         if( hCurrentWnd != lpDragInfo->hScope )
1836         {
1837             if( hCurrentWnd )
1838                 SendMessage16( hCurrentWnd, WM_DRAGSELECT, 0,
1839                        (LPARAM)MAKELONG(LOWORD(spDragInfo)+sizeof(DRAGINFO16),
1840                                         HIWORD(spDragInfo)) );
1841             hCurrentWnd = lpDragInfo->hScope;
1842             if( hCurrentWnd )
1843                 SendMessage16( hCurrentWnd, WM_DRAGSELECT, 1, (LPARAM)spDragInfo);
1844         }
1845         else
1846             if( hCurrentWnd )
1847                 SendMessage16( hCurrentWnd, WM_DRAGMOVE, 0, (LPARAM)spDragInfo);
1848
1849     } while( msg.message != WM_LBUTTONUP && msg.message != WM_NCLBUTTONUP );
1850
1851     ReleaseCapture();
1852     ShowCursor( FALSE );
1853
1854     if( hCursor ) SetCursor(hOldCursor);
1855
1856     if( hCurrentCursor != hBummer )
1857         msg.lParam = SendMessage16( lpDragInfo->hScope, WM_DROPOBJECT,
1858                                    (WPARAM16)hWnd, (LPARAM)spDragInfo );
1859     else
1860         msg.lParam = 0;
1861     GlobalFree16(hDragInfo);
1862
1863     return (DWORD)(msg.lParam);
1864 }
1865
1866
1867 /***********************************************************************
1868  *              DrawFocusRect (USER.466)
1869  */
1870 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1871 {
1872     RECT rect32;
1873
1874     rect32.left   = rc->left;
1875     rect32.top    = rc->top;
1876     rect32.right  = rc->right;
1877     rect32.bottom = rc->bottom;
1878     DrawFocusRect( HDC_32(hdc), &rect32 );
1879 }
1880
1881
1882 /***********************************************************************
1883  *           AnsiNext   (USER.472)
1884  */
1885 SEGPTR WINAPI AnsiNext16(SEGPTR current)
1886 {
1887     char *ptr = MapSL(current);
1888     return current + (CharNextA(ptr) - ptr);
1889 }
1890
1891
1892 /***********************************************************************
1893  *           AnsiPrev   (USER.473)
1894  */
1895 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
1896 {
1897     char *ptr = MapSL(current);
1898     return current - (ptr - CharPrevA( start, ptr ));
1899 }
1900
1901
1902 /****************************************************************************
1903  *              GetKeyboardLayoutName (USER.477)
1904  */
1905 INT16 WINAPI GetKeyboardLayoutName16( LPSTR name )
1906 {
1907     return GetKeyboardLayoutNameA( name );
1908 }
1909
1910
1911 /***********************************************************************
1912  *           FormatMessage   (USER.606)
1913  */
1914 DWORD WINAPI FormatMessage16(
1915     DWORD   dwFlags,
1916     SEGPTR lpSource,     /* [in] NOTE: not always a valid pointer */
1917     WORD   dwMessageId,
1918     WORD   dwLanguageId,
1919     LPSTR  lpBuffer,     /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
1920     WORD   nSize,
1921     LPDWORD args )       /* [in] NOTE: va_list *args */
1922 {
1923 #ifdef __i386__
1924 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
1925     LPSTR       target,t;
1926     DWORD       talloced;
1927     LPSTR       from,f;
1928     DWORD       width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
1929     BOOL        eos = FALSE;
1930     LPSTR       allocstring = NULL;
1931
1932     TRACE("(0x%x,%x,%d,0x%x,%p,%d,%p)\n",
1933           dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
1934         if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
1935                 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
1936         if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
1937                 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
1938                         || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
1939
1940     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
1941         FIXME("line wrapping (%u) not supported.\n", width);
1942     from = NULL;
1943     if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
1944     {
1945         char *source = MapSL(lpSource);
1946         from = HeapAlloc( GetProcessHeap(), 0, strlen(source)+1 );
1947         strcpy( from, source );
1948     }
1949     else if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
1950         from = HeapAlloc( GetProcessHeap(),0,200 );
1951         sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
1952     }
1953     else if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
1954         INT16   bufsize;
1955         HINSTANCE16 hinst16 = ((HINSTANCE16)lpSource & 0xffff);
1956
1957         dwMessageId &= 0xFFFF;
1958         bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
1959         if (bufsize) {
1960             from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
1961             LoadString16(hinst16,dwMessageId,from,bufsize+1);
1962         }
1963     }
1964     target      = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
1965     t   = target;
1966     talloced= 100;
1967
1968 #define ADD_TO_T(c) \
1969         *t++=c;\
1970         if (t-target == talloced) {\
1971                 target  = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
1972                 t       = target+talloced;\
1973                 talloced*=2;\
1974         }
1975
1976     if (from) {
1977         f=from;
1978         while (*f && !eos) {
1979             if (*f=='%') {
1980                 int     insertnr;
1981                 char    *fmtstr,*x,*lastf;
1982                 DWORD   *argliststart;
1983
1984                 fmtstr = NULL;
1985                 lastf = f;
1986                 f++;
1987                 if (!*f) {
1988                     ADD_TO_T('%');
1989                     continue;
1990                 }
1991                 switch (*f) {
1992                 case '1':case '2':case '3':case '4':case '5':
1993                 case '6':case '7':case '8':case '9':
1994                     insertnr=*f-'0';
1995                     switch (f[1]) {
1996                     case '0':case '1':case '2':case '3':
1997                     case '4':case '5':case '6':case '7':
1998                     case '8':case '9':
1999                         f++;
2000                         insertnr=insertnr*10+*f-'0';
2001                         f++;
2002                         break;
2003                     default:
2004                         f++;
2005                         break;
2006                     }
2007                     if (*f=='!') {
2008                         f++;
2009                         if (NULL!=(x=strchr(f,'!'))) {
2010                             *x='\0';
2011                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
2012                             sprintf(fmtstr,"%%%s",f);
2013                             f=x+1;
2014                         } else {
2015                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
2016                             sprintf(fmtstr,"%%%s",f);
2017                             f+=strlen(f); /*at \0*/
2018                         }
2019                     }
2020                     else
2021                     {
2022                         if(!args) break;
2023                         fmtstr=HeapAlloc( GetProcessHeap(), 0, 3 );
2024                         strcpy( fmtstr, "%s" );
2025                     }
2026                     if (args) {
2027                         int     ret;
2028                         int     sz;
2029                         LPSTR   b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
2030
2031                         argliststart=args+insertnr-1;
2032
2033                         /* CMF - This makes a BIG assumption about va_list */
2034                         while ((ret = vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) || (ret >= sz)) {
2035                             sz = (ret == -1 ? sz + 100 : ret + 1);
2036                             b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz);
2037                         }
2038                         for (x=b; *x; x++) ADD_TO_T(*x);
2039                         HeapFree(GetProcessHeap(), 0, b);
2040                     } else {
2041                         /* NULL args - copy formatstr
2042                          * (probably wrong)
2043                          */
2044                         while ((lastf<f)&&(*lastf)) {
2045                             ADD_TO_T(*lastf++);
2046                         }
2047                     }
2048                     HeapFree(GetProcessHeap(),0,fmtstr);
2049                     break;
2050                 case '0': /* Just stop processing format string */
2051                     eos = TRUE;
2052                     f++;
2053                     break;
2054                 case 'n': /* 16 bit version just outputs 'n' */
2055                 default:
2056                     ADD_TO_T(*f++);
2057                     break;
2058                 }
2059             } else { /* '\n' or '\r' gets mapped to "\r\n" */
2060                 if(*f == '\n' || *f == '\r') {
2061                     if (width == 0) {
2062                         ADD_TO_T('\r');
2063                         ADD_TO_T('\n');
2064                         if(*f++ == '\r' && *f == '\n')
2065                             f++;
2066                     }
2067                 } else {
2068                     ADD_TO_T(*f++);
2069                 }
2070             }
2071         }
2072         *t='\0';
2073     }
2074     talloced = strlen(target)+1;
2075     if (nSize && talloced<nSize) {
2076         target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
2077     }
2078     TRACE("-- %s\n",debugstr_a(target));
2079     if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
2080         /* nSize is the MINIMUM size */
2081         HLOCAL16 h = LocalAlloc16(LPTR,talloced);
2082         SEGPTR ptr = LocalLock16(h);
2083         allocstring = MapSL( ptr );
2084         memcpy( allocstring,target,talloced);
2085         LocalUnlock16( h );
2086         *((HLOCAL16*)lpBuffer) = h;
2087     } else
2088         lstrcpynA(lpBuffer,target,nSize);
2089     HeapFree(GetProcessHeap(),0,target);
2090     HeapFree(GetProcessHeap(),0,from);
2091     return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
2092         strlen(allocstring):
2093         strlen(lpBuffer);
2094 #else
2095         return 0;
2096 #endif /* __i386__ */
2097 }
2098 #undef ADD_TO_T
2099
2100
2101 /***********************************************************************
2102  *              ChangeDisplaySettings (USER.620)
2103  */
2104 LONG WINAPI ChangeDisplaySettings16( LPDEVMODEA devmode, DWORD flags )
2105 {
2106     return ChangeDisplaySettingsA( devmode, flags );
2107 }
2108
2109
2110 /***********************************************************************
2111  *              EnumDisplaySettings (USER.621)
2112  */
2113 BOOL16 WINAPI EnumDisplaySettings16( LPCSTR name, DWORD n, LPDEVMODEA devmode )
2114 {
2115     return EnumDisplaySettingsA( name, n, devmode );
2116 }
2117
2118 /**********************************************************************
2119  *          DrawFrameControl  (USER.656)
2120  */
2121 BOOL16 WINAPI DrawFrameControl16( HDC16 hdc, LPRECT16 rc, UINT16 uType, UINT16 uState )
2122 {
2123     RECT rect32;
2124     BOOL ret;
2125
2126     rect32.left   = rc->left;
2127     rect32.top    = rc->top;
2128     rect32.right  = rc->right;
2129     rect32.bottom = rc->bottom;
2130     ret = DrawFrameControl( HDC_32(hdc), &rect32, uType, uState );
2131     rc->left   = rect32.left;
2132     rc->top    = rect32.top;
2133     rc->right  = rect32.right;
2134     rc->bottom = rect32.bottom;
2135     return ret;
2136 }
2137
2138 /**********************************************************************
2139  *          DrawEdge   (USER.659)
2140  */
2141 BOOL16 WINAPI DrawEdge16( HDC16 hdc, LPRECT16 rc, UINT16 edge, UINT16 flags )
2142 {
2143     RECT rect32;
2144     BOOL ret;
2145
2146     rect32.left   = rc->left;
2147     rect32.top    = rc->top;
2148     rect32.right  = rc->right;
2149     rect32.bottom = rc->bottom;
2150     ret = DrawEdge( HDC_32(hdc), &rect32, edge, flags );
2151     rc->left   = rect32.left;
2152     rc->top    = rect32.top;
2153     rc->right  = rect32.right;
2154     rc->bottom = rect32.bottom;
2155     return ret;
2156 }
2157
2158 /**********************************************************************
2159  *              CheckMenuRadioItem (USER.666)
2160  */
2161 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu, UINT16 first, UINT16 last,
2162                                    UINT16 check, BOOL16 bypos)
2163 {
2164      return CheckMenuRadioItem( HMENU_32(hMenu), first, last, check, bypos );
2165 }