Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / user / user16.c
1 /*
2  * Misc 16-bit USER functions
3  *
4  * Copyright 2002 Patrik Stridvall
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 <stdarg.h>
22 #include <stdlib.h>
23 #include "wine/winuser16.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wownt32.h"
27 #include "task.h"
28 #include "user.h"
29 #include "win.h"
30 #include "winproc.h"
31 #include "cursoricon.h"
32
33 /* handle to handle 16 conversions */
34 #define HANDLE_16(h32)          (LOWORD(h32))
35
36 /* handle16 to handle conversions */
37 #define HANDLE_32(h16)          ((HANDLE)(ULONG_PTR)(h16))
38 #define HINSTANCE_32(h16)       ((HINSTANCE)(ULONG_PTR)(h16))
39
40 #define IS_MENU_STRING_ITEM(flags) \
41     (((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR)) == MF_STRING)
42
43 WORD WINAPI DestroyIcon32(HGLOBAL16, UINT16);
44
45
46 struct gray_string_info
47 {
48     GRAYSTRINGPROC16 proc;
49     LPARAM           param;
50     char             str[1];
51 };
52
53 /* callback for 16-bit gray string proc with opaque pointer */
54 static BOOL CALLBACK gray_string_callback( HDC hdc, LPARAM param, INT len )
55 {
56     const struct gray_string_info *info = (struct gray_string_info *)param;
57     WORD args[4];
58     DWORD ret;
59
60     args[3] = HDC_16(hdc);
61     args[2] = HIWORD(info->param);
62     args[1] = LOWORD(info->param);
63     args[0] = len;
64     WOWCallback16Ex( (DWORD)info->proc, WCB16_PASCAL, sizeof(args), args, &ret );
65     return LOWORD(ret);
66 }
67
68 /* callback for 16-bit gray string proc with string pointer */
69 static BOOL CALLBACK gray_string_callback_ptr( HDC hdc, LPARAM param, INT len )
70 {
71     const struct gray_string_info *info;
72     char *str = (char *)param;
73
74     info = (struct gray_string_info *)(str - offsetof( struct gray_string_info, str ));
75     return gray_string_callback( hdc, (LPARAM)info, len );
76 }
77
78 struct draw_state_info
79 {
80     DRAWSTATEPROC16 proc;
81     LPARAM          param;
82 };
83
84 /* callback for 16-bit DrawState functions */
85 static BOOL CALLBACK draw_state_callback( HDC hdc, LPARAM lparam, WPARAM wparam, int cx, int cy )
86 {
87     const struct draw_state_info *info = (struct draw_state_info *)lparam;
88     WORD args[6];
89     DWORD ret;
90
91     args[5] = HDC_16(hdc);
92     args[4] = HIWORD(info->param);
93     args[3] = LOWORD(info->param);
94     args[2] = wparam;
95     args[1] = cx;
96     args[0] = cy;
97     WOWCallback16Ex( (DWORD)info->proc, WCB16_PASCAL, sizeof(args), args, &ret );
98     return LOWORD(ret);
99 }
100
101 /***********************************************************************
102  *              SetCursor (USER.69)
103  */
104 HCURSOR16 WINAPI SetCursor16(HCURSOR16 hCursor)
105 {
106   return HCURSOR_16(SetCursor(HCURSOR_32(hCursor)));
107 }
108
109 /***********************************************************************
110  *              ShowCursor (USER.71)
111  */
112 INT16 WINAPI ShowCursor16(BOOL16 bShow)
113 {
114   return ShowCursor(bShow);
115 }
116
117 /***********************************************************************
118  *              DrawIcon (USER.84)
119  */
120 BOOL16 WINAPI DrawIcon16(HDC16 hdc, INT16 x, INT16 y, HICON16 hIcon)
121 {
122   return DrawIcon(HDC_32(hdc), x, y, HICON_32(hIcon));
123 }
124
125
126 /***********************************************************************
127  *           DrawText    (USER.85)
128  */
129 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
130 {
131     INT16 ret;
132
133     if (rect)
134     {
135         RECT rect32;
136         CONV_RECT16TO32( rect, &rect32 );
137         ret = DrawTextA( HDC_32(hdc), str, count, &rect32, flags );
138         CONV_RECT32TO16( &rect32, rect );
139     }
140     else ret = DrawTextA( HDC_32(hdc), str, count, NULL, flags);
141     return ret;
142 }
143
144
145 /***********************************************************************
146  *              IconSize (USER.86)
147  *
148  * See "Undocumented Windows". Used by W2.0 paint.exe.
149  */
150 DWORD WINAPI IconSize16(void)
151 {
152   return MAKELONG(GetSystemMetrics(SM_CYICON), GetSystemMetrics(SM_CXICON));
153 }
154
155
156 /**********************************************************************
157  *         CreateMenu    (USER.151)
158  */
159 HMENU16 WINAPI CreateMenu16(void)
160 {
161     return HMENU_16( CreateMenu() );
162 }
163
164
165 /**********************************************************************
166  *         DestroyMenu    (USER.152)
167  */
168 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
169 {
170     return DestroyMenu( HMENU_32(hMenu) );
171 }
172
173
174 /*******************************************************************
175  *         ChangeMenu    (USER.153)
176  */
177 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
178                             UINT16 id, UINT16 flags )
179 {
180     if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND, id, data );
181
182     /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
183     /* for MF_DELETE. We should check the parameters for all others */
184     /* MF_* actions also (anybody got a doc on ChangeMenu?). */
185
186     if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
187     if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE, id, data );
188     if (flags & MF_REMOVE) return RemoveMenu16(hMenu, flags & MF_BYPOSITION ? pos : id,
189                                                flags & ~MF_REMOVE );
190     /* Default: MF_INSERT */
191     return InsertMenu16( hMenu, pos, flags, id, data );
192 }
193
194
195 /*******************************************************************
196  *         CheckMenuItem    (USER.154)
197  */
198 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
199 {
200     return CheckMenuItem( HMENU_32(hMenu), id, flags );
201 }
202
203
204 /**********************************************************************
205  *         EnableMenuItem    (USER.155)
206  */
207 UINT16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
208 {
209     return EnableMenuItem( HMENU_32(hMenu), wItemID, wFlags );
210 }
211
212
213 /**********************************************************************
214  *         GetSubMenu    (USER.159)
215  */
216 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
217 {
218     return HMENU_16( GetSubMenu( HMENU_32(hMenu), nPos ) );
219 }
220
221
222 /*******************************************************************
223  *         GetMenuString    (USER.161)
224  */
225 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
226                               LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
227 {
228     return GetMenuStringA( HMENU_32(hMenu), wItemID, str, nMaxSiz, wFlags );
229 }
230
231
232 /**********************************************************************
233  *              WinHelp (USER.171)
234  */
235 BOOL16 WINAPI WinHelp16( HWND16 hWnd, LPCSTR lpHelpFile, UINT16 wCommand,
236                          DWORD dwData )
237 {
238     BOOL ret;
239     DWORD mutex_count;
240
241     /* We might call WinExec() */
242     ReleaseThunkLock(&mutex_count);
243
244     ret = WinHelpA(WIN_Handle32(hWnd), lpHelpFile, wCommand, (DWORD)MapSL(dwData));
245
246     RestoreThunkLock(mutex_count);
247     return ret;
248 }
249
250
251 /***********************************************************************
252  *              LoadCursor (USER.173)
253  */
254 HCURSOR16 WINAPI LoadCursor16(HINSTANCE16 hInstance, LPCSTR name)
255 {
256   return HCURSOR_16(LoadCursorA(HINSTANCE_32(hInstance), name));
257 }
258
259
260 /***********************************************************************
261  *              LoadIcon (USER.174)
262  */
263 HICON16 WINAPI LoadIcon16(HINSTANCE16 hInstance, LPCSTR name)
264 {
265   return HICON_16(LoadIconA(HINSTANCE_32(hInstance), name));
266 }
267
268 /**********************************************************************
269  *              LoadBitmap (USER.175)
270  */
271 HBITMAP16 WINAPI LoadBitmap16(HINSTANCE16 hInstance, LPCSTR name)
272 {
273   return HBITMAP_16(LoadBitmapA(HINSTANCE_32(hInstance), name));
274 }
275
276
277 /***********************************************************************
278  *           GrayString   (USER.185)
279  */
280 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
281                             LPARAM lParam, INT16 cch, INT16 x, INT16 y,
282                             INT16 cx, INT16 cy )
283 {
284     BOOL ret;
285
286     if (!gsprc) return GrayStringA( HDC_32(hdc), HBRUSH_32(hbr), NULL,
287                                     (LPARAM)MapSL(lParam), cch, x, y, cx, cy );
288
289     if (cch == -1 || (cch && cx && cy))
290     {
291         /* lParam can be treated as an opaque pointer */
292         struct gray_string_info info;
293
294         info.proc  = gsprc;
295         info.param = lParam;
296         ret = GrayStringA( HDC_32(hdc), HBRUSH_32(hbr), gray_string_callback,
297                            (LPARAM)&info, cch, x, y, cx, cy );
298     }
299     else  /* here we need some string conversions */
300     {
301         char *str16 = MapSL(lParam);
302         struct gray_string_info *info;
303
304         if (!cch) cch = strlen(str16);
305         if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) + cch ))) return FALSE;
306         info->proc  = gsprc;
307         info->param = lParam;
308         memcpy( info->str, str16, cch );
309         ret = GrayStringA( HDC_32(hdc), HBRUSH_32(hbr), gray_string_callback_ptr,
310                            (LPARAM)info->str, cch, x, y, cx, cy );
311         HeapFree( GetProcessHeap(), 0, info );
312     }
313     return ret;
314 }
315
316
317 /***********************************************************************
318  *           TabbedTextOut    (USER.196)
319  */
320 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
321                              INT16 count, INT16 nb_tabs, const INT16 *tabs16, INT16 tab_org )
322 {
323     LONG ret;
324     INT i, *tabs = HeapAlloc( GetProcessHeap(), 0, nb_tabs * sizeof(tabs) );
325     if (!tabs) return 0;
326     for (i = 0; i < nb_tabs; i++) tabs[i] = tabs16[i];
327     ret = TabbedTextOutA( HDC_32(hdc), x, y, lpstr, count, nb_tabs, tabs, tab_org );
328     HeapFree( GetProcessHeap(), 0, tabs );
329     return ret;
330 }
331
332
333 /***********************************************************************
334  *           GetTabbedTextExtent    (USER.197)
335  */
336 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
337                                     INT16 nb_tabs, const INT16 *tabs16 )
338 {
339     LONG ret;
340     INT i, *tabs = HeapAlloc( GetProcessHeap(), 0, nb_tabs * sizeof(tabs) );
341     if (!tabs) return 0;
342     for (i = 0; i < nb_tabs; i++) tabs[i] = tabs16[i];
343     ret = GetTabbedTextExtentA( HDC_32(hdc), lpstr, count, nb_tabs, tabs );
344     HeapFree( GetProcessHeap(), 0, tabs );
345     return ret;
346 }
347
348
349 /*************************************************************************
350  *              ScrollDC (USER.221)
351  */
352 BOOL16 WINAPI ScrollDC16( HDC16 hdc, INT16 dx, INT16 dy, const RECT16 *rect,
353                           const RECT16 *cliprc, HRGN16 hrgnUpdate,
354                           LPRECT16 rcUpdate )
355 {
356     RECT rect32, clipRect32, rcUpdate32;
357     BOOL16 ret;
358
359     if (rect) CONV_RECT16TO32( rect, &rect32 );
360     if (cliprc) CONV_RECT16TO32( cliprc, &clipRect32 );
361     ret = ScrollDC( HDC_32(hdc), dx, dy, rect ? &rect32 : NULL,
362                     cliprc ? &clipRect32 : NULL, HRGN_32(hrgnUpdate),
363                     &rcUpdate32 );
364     if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
365     return ret;
366 }
367
368 /***********************************************************************
369  *              GetCursor (USER.247)
370  */
371 HCURSOR16 WINAPI GetCursor16(void)
372 {
373   return HCURSOR_16(GetCursor());
374 }
375
376
377 /**********************************************************************
378  *         GetMenuState    (USER.250)
379  */
380 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
381 {
382     return GetMenuState( HMENU_32(hMenu), wItemID, wFlags );
383 }
384
385
386 /**********************************************************************
387  *         GetMenuItemCount    (USER.263)
388  */
389 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
390 {
391     return GetMenuItemCount( HMENU_32(hMenu) );
392 }
393
394
395 /**********************************************************************
396  *         GetMenuItemID    (USER.264)
397  */
398 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
399 {
400     return GetMenuItemID( HMENU_32(hMenu), nPos );
401 }
402
403
404 /***********************************************************************
405  *              GlobalAddAtom (USER.268)
406  */
407 ATOM WINAPI GlobalAddAtom16(LPCSTR lpString)
408 {
409   return GlobalAddAtomA(lpString);
410 }
411
412 /***********************************************************************
413  *              GlobalDeleteAtom (USER.269)
414  */
415 ATOM WINAPI GlobalDeleteAtom16(ATOM nAtom)
416 {
417   return GlobalDeleteAtom(nAtom);
418 }
419
420 /***********************************************************************
421  *              GlobalFindAtom (USER.270)
422  */
423 ATOM WINAPI GlobalFindAtom16(LPCSTR lpString)
424 {
425   return GlobalFindAtomA(lpString);
426 }
427
428 /***********************************************************************
429  *              GlobalGetAtomName (USER.271)
430  */
431 UINT16 WINAPI GlobalGetAtomName16(ATOM nAtom, LPSTR lpBuffer, INT16 nSize)
432 {
433   return GlobalGetAtomNameA(nAtom, lpBuffer, nSize);
434 }
435
436
437 /***********************************************************************
438  *              GetSysColorBrush (USER.281)
439  */
440 HBRUSH16 WINAPI GetSysColorBrush16( INT16 index )
441 {
442     return HBRUSH_16( GetSysColorBrush(index) );
443 }
444
445
446 /***********************************************************************
447  *              SelectPalette (USER.282)
448  */
449 HPALETTE16 WINAPI SelectPalette16( HDC16 hdc, HPALETTE16 hpal, BOOL16 bForceBackground )
450 {
451     return HPALETTE_16( SelectPalette( HDC_32(hdc), HPALETTE_32(hpal), bForceBackground ));
452 }
453
454 /***********************************************************************
455  *              RealizePalette (USER.283)
456  */
457 UINT16 WINAPI RealizePalette16( HDC16 hdc )
458 {
459     return UserRealizePalette( HDC_32(hdc) );
460 }
461
462
463 /***********************************************************************
464  *              SignalProc (USER.314)
465  */
466 void WINAPI SignalProc16( HANDLE16 hModule, UINT16 code,
467                           UINT16 uExitFn, HINSTANCE16 hInstance, HQUEUE16 hQueue )
468 {
469     if (code == USIG16_DLL_UNLOAD)
470     {
471         /* HOOK_FreeModuleHooks( hModule ); */
472         CLASS_FreeModuleClasses( hModule );
473         CURSORICON_FreeModuleIcons( hModule );
474     }
475 }
476
477
478 /**********************************************************************
479  *              IsMenu    (USER.358)
480  */
481 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
482 {
483     return IsMenu( HMENU_32(hmenu) );
484 }
485
486
487 /**********************************************************************
488  *         SetMenuContextHelpId    (USER.384)
489  */
490 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpID)
491 {
492     return SetMenuContextHelpId( HMENU_32(hMenu), dwContextHelpID );
493 }
494
495
496 /**********************************************************************
497  *         GetMenuContextHelpId    (USER.385)
498  */
499 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
500 {
501     return GetMenuContextHelpId( HMENU_32(hMenu) );
502 }
503
504
505 /***********************************************************************
506  *              LoadImage (USER.389)
507  *
508  */
509 HANDLE16 WINAPI LoadImage16(HINSTANCE16 hinst, LPCSTR name, UINT16 type,
510                             INT16 desiredx, INT16 desiredy, UINT16 loadflags)
511 {
512   return HANDLE_16(LoadImageA(HINSTANCE_32(hinst), name, type, desiredx,
513                               desiredy, loadflags));
514 }
515
516 /******************************************************************************
517  *              CopyImage (USER.390) Creates new image and copies attributes to it
518  *
519  */
520 HICON16 WINAPI CopyImage16(HANDLE16 hnd, UINT16 type, INT16 desiredx,
521                            INT16 desiredy, UINT16 flags)
522 {
523   return HICON_16(CopyImage(HANDLE_32(hnd), (UINT)type, (INT)desiredx,
524                             (INT)desiredy, (UINT)flags));
525 }
526
527 /**********************************************************************
528  *              DrawIconEx (USER.394)
529  */
530 BOOL16 WINAPI DrawIconEx16(HDC16 hdc, INT16 xLeft, INT16 yTop, HICON16 hIcon,
531                            INT16 cxWidth, INT16 cyWidth, UINT16 istep,
532                            HBRUSH16 hbr, UINT16 flags)
533 {
534   return DrawIconEx(HDC_32(hdc), xLeft, yTop, HICON_32(hIcon), cxWidth, cyWidth,
535                     istep, HBRUSH_32(hbr), flags);
536 }
537
538 /**********************************************************************
539  *              GetIconInfo (USER.395)
540  */
541 BOOL16 WINAPI GetIconInfo16(HICON16 hIcon, LPICONINFO16 iconinfo)
542 {
543   ICONINFO ii32;
544   BOOL16 ret = GetIconInfo(HICON_32(hIcon), &ii32);
545
546   iconinfo->fIcon = ii32.fIcon;
547   iconinfo->xHotspot = ii32.xHotspot;
548   iconinfo->yHotspot = ii32.yHotspot;
549   iconinfo->hbmMask  = HBITMAP_16(ii32.hbmMask);
550   iconinfo->hbmColor = HBITMAP_16(ii32.hbmColor);
551   return ret;
552 }
553
554 /***********************************************************************
555  *              CreateCursor (USER.406)
556  */
557 HCURSOR16 WINAPI CreateCursor16(HINSTANCE16 hInstance,
558                                 INT16 xHotSpot, INT16 yHotSpot,
559                                 INT16 nWidth, INT16 nHeight,
560                                 LPCVOID lpANDbits, LPCVOID lpXORbits)
561 {
562   CURSORICONINFO info;
563
564   info.ptHotSpot.x = xHotSpot;
565   info.ptHotSpot.y = yHotSpot;
566   info.nWidth = nWidth;
567   info.nHeight = nHeight;
568   info.nWidthBytes = 0;
569   info.bPlanes = 1;
570   info.bBitsPerPixel = 1;
571
572   return CreateCursorIconIndirect16(hInstance, &info, lpANDbits, lpXORbits);
573 }
574
575
576 /*******************************************************************
577  *         InsertMenu    (USER.410)
578  */
579 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
580                             UINT16 id, SEGPTR data )
581 {
582     UINT pos32 = (UINT)pos;
583     if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT)-1;
584     if (IS_MENU_STRING_ITEM(flags) && data)
585         return InsertMenuA( HMENU_32(hMenu), pos32, flags, id, MapSL(data) );
586     return InsertMenuA( HMENU_32(hMenu), pos32, flags, id, (LPSTR)data );
587 }
588
589
590 /*******************************************************************
591  *         AppendMenu    (USER.411)
592  */
593 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
594 {
595     return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
596 }
597
598
599 /**********************************************************************
600  *         RemoveMenu   (USER.412)
601  */
602 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
603 {
604     return RemoveMenu( HMENU_32(hMenu), nPos, wFlags );
605 }
606
607
608 /**********************************************************************
609  *         DeleteMenu    (USER.413)
610  */
611 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
612 {
613     return DeleteMenu( HMENU_32(hMenu), nPos, wFlags );
614 }
615
616
617 /*******************************************************************
618  *         ModifyMenu    (USER.414)
619  */
620 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
621                             UINT16 id, SEGPTR data )
622 {
623     if (IS_MENU_STRING_ITEM(flags))
624         return ModifyMenuA( HMENU_32(hMenu), pos, flags, id, MapSL(data) );
625     return ModifyMenuA( HMENU_32(hMenu), pos, flags, id, (LPSTR)data );
626 }
627
628
629 /**********************************************************************
630  *         CreatePopupMenu    (USER.415)
631  */
632 HMENU16 WINAPI CreatePopupMenu16(void)
633 {
634     return HMENU_16( CreatePopupMenu() );
635 }
636
637
638 /**********************************************************************
639  *         SetMenuItemBitmaps    (USER.418)
640  */
641 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
642                                     HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
643 {
644     return SetMenuItemBitmaps( HMENU_32(hMenu), nPos, wFlags,
645                                HBITMAP_32(hNewUnCheck), HBITMAP_32(hNewCheck) );
646 }
647
648
649 /*******************************************************************
650  *              InsertMenuItem   (USER.441)
651  *
652  * FIXME: untested
653  */
654 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
655                                 const MENUITEMINFO16 *mii )
656 {
657     MENUITEMINFOA miia;
658
659     miia.cbSize        = sizeof(miia);
660     miia.fMask         = mii->fMask;
661     miia.dwTypeData    = (LPSTR)mii->dwTypeData;
662     miia.fType         = mii->fType;
663     miia.fState        = mii->fState;
664     miia.wID           = mii->wID;
665     miia.hSubMenu      = HMENU_32(mii->hSubMenu);
666     miia.hbmpChecked   = HBITMAP_32(mii->hbmpChecked);
667     miia.hbmpUnchecked = HBITMAP_32(mii->hbmpUnchecked);
668     miia.dwItemData    = mii->dwItemData;
669     miia.cch           = mii->cch;
670     if (IS_MENU_STRING_ITEM(miia.fType))
671         miia.dwTypeData = MapSL(mii->dwTypeData);
672     return InsertMenuItemA( HMENU_32(hmenu), pos, byposition, &miia );
673 }
674
675
676 /**********************************************************************
677  *           DrawState    (USER.449)
678  */
679 BOOL16 WINAPI DrawState16( HDC16 hdc, HBRUSH16 hbr, DRAWSTATEPROC16 func, LPARAM ldata,
680                            WPARAM16 wdata, INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags )
681 {
682     struct draw_state_info info;
683     UINT opcode = flags & 0xf;
684
685     if (opcode == DST_TEXT || opcode == DST_PREFIXTEXT)
686     {
687         /* make sure DrawStateA doesn't try to use ldata as a pointer */
688         if (!wdata) wdata = strlen( MapSL(ldata) );
689         if (!cx || !cy)
690         {
691             SIZE s;
692             if (!GetTextExtentPoint32A( HDC_32(hdc), MapSL(ldata), wdata, &s )) return FALSE;
693             if (!cx) cx = s.cx;
694             if (!cy) cy = s.cy;
695         }
696     }
697     info.proc  = func;
698     info.param = ldata;
699     return DrawStateA( HDC_32(hdc), HBRUSH_32(hbr), draw_state_callback,
700                        (LPARAM)&info, wdata, x, y, cx, cy, flags );
701 }
702
703
704 /**********************************************************************
705  *              CreateIconFromResourceEx (USER.450)
706  *
707  * FIXME: not sure about exact parameter types
708  */
709 HICON16 WINAPI CreateIconFromResourceEx16(LPBYTE bits, UINT16 cbSize,
710                                           BOOL16 bIcon, DWORD dwVersion,
711                                           INT16 width, INT16 height,
712                                           UINT16 cFlag)
713 {
714   return HICON_16(CreateIconFromResourceEx(bits, cbSize, bIcon, dwVersion,
715                                            width, height, cFlag));
716 }
717
718 /***********************************************************************
719  *              DestroyIcon (USER.457)
720  */
721 BOOL16 WINAPI DestroyIcon16(HICON16 hIcon)
722 {
723   return DestroyIcon32(hIcon, 0);
724 }
725
726 /***********************************************************************
727  *              DestroyCursor (USER.458)
728  */
729 BOOL16 WINAPI DestroyCursor16(HCURSOR16 hCursor)
730 {
731   return DestroyIcon32(hCursor, 0);
732 }
733
734 /*******************************************************************
735  *                      DRAG_QueryUpdate16
736  *
737  * Recursively find a child that contains spDragInfo->pt point
738  * and send WM_QUERYDROPOBJECT. Helper for DragObject16.
739  */
740 static BOOL DRAG_QueryUpdate16( HWND hQueryWnd, SEGPTR spDragInfo )
741 {
742     BOOL bResult = 0;
743     WPARAM wParam;
744     POINT pt, old_pt;
745     LPDRAGINFO16 ptrDragInfo = MapSL(spDragInfo);
746     RECT tempRect;
747     HWND child;
748
749     if (!IsWindowEnabled(hQueryWnd)) return FALSE;
750
751     old_pt.x = ptrDragInfo->pt.x;
752     old_pt.y = ptrDragInfo->pt.y;
753     pt = old_pt;
754     ScreenToClient( hQueryWnd, &pt );
755     child = ChildWindowFromPointEx( hQueryWnd, pt, CWP_SKIPINVISIBLE );
756     if (!child) return FALSE;
757
758     if (child != hQueryWnd)
759     {
760         wParam = 0;
761         if (DRAG_QueryUpdate16( child, spDragInfo )) return TRUE;
762     }
763     else
764     {
765         GetClientRect( hQueryWnd, &tempRect );
766         wParam = !PtInRect( &tempRect, pt );
767     }
768
769     ptrDragInfo->pt.x = pt.x;
770     ptrDragInfo->pt.y = pt.y;
771     ptrDragInfo->hScope = HWND_16(hQueryWnd);
772
773     bResult = SendMessage16( HWND_16(hQueryWnd), WM_QUERYDROPOBJECT, wParam, spDragInfo );
774
775     if (!bResult)
776     {
777         ptrDragInfo->pt.x = old_pt.x;
778         ptrDragInfo->pt.y = old_pt.y;
779     }
780     return bResult;
781 }
782
783
784 /******************************************************************************
785  *              DragObject (USER.464)
786  */
787 DWORD WINAPI DragObject16( HWND16 hwndScope, HWND16 hWnd, UINT16 wObj,
788                            HANDLE16 hOfStruct, WORD szList, HCURSOR16 hCursor )
789 {
790     MSG msg;
791     LPDRAGINFO16 lpDragInfo;
792     SEGPTR      spDragInfo;
793     HCURSOR     hOldCursor=0, hBummer=0;
794     HGLOBAL16   hDragInfo  = GlobalAlloc16( GMEM_SHARE | GMEM_ZEROINIT, 2*sizeof(DRAGINFO16));
795     HCURSOR     hCurrentCursor = 0;
796     HWND16      hCurrentWnd = 0;
797
798     lpDragInfo = (LPDRAGINFO16) GlobalLock16(hDragInfo);
799     spDragInfo = K32WOWGlobalLock16(hDragInfo);
800
801     if( !lpDragInfo || !spDragInfo ) return 0L;
802
803     if (!(hBummer = LoadCursorA(0, MAKEINTRESOURCEA(OCR_NO))))
804     {
805         GlobalFree16(hDragInfo);
806         return 0L;
807     }
808
809     if(hCursor) hOldCursor = SetCursor(HCURSOR_32(hCursor));
810
811     lpDragInfo->hWnd   = hWnd;
812     lpDragInfo->hScope = 0;
813     lpDragInfo->wFlags = wObj;
814     lpDragInfo->hList  = szList; /* near pointer! */
815     lpDragInfo->hOfStruct = hOfStruct;
816     lpDragInfo->l = 0L;
817
818     SetCapture( HWND_32(hWnd) );
819     ShowCursor( TRUE );
820
821     do
822     {
823         GetMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST );
824
825        *(lpDragInfo+1) = *lpDragInfo;
826
827         lpDragInfo->pt.x = msg.pt.x;
828         lpDragInfo->pt.y = msg.pt.y;
829
830         /* update DRAGINFO struct */
831         if( DRAG_QueryUpdate16(WIN_Handle32(hwndScope), spDragInfo) > 0 )
832             hCurrentCursor = HCURSOR_32(hCursor);
833         else
834         {
835             hCurrentCursor = hBummer;
836             lpDragInfo->hScope = 0;
837         }
838         if( hCurrentCursor )
839             SetCursor(hCurrentCursor);
840
841         /* send WM_DRAGLOOP */
842         SendMessage16( hWnd, WM_DRAGLOOP, (WPARAM16)(hCurrentCursor != hBummer),
843                                           (LPARAM) spDragInfo );
844         /* send WM_DRAGSELECT or WM_DRAGMOVE */
845         if( hCurrentWnd != lpDragInfo->hScope )
846         {
847             if( hCurrentWnd )
848                 SendMessage16( hCurrentWnd, WM_DRAGSELECT, 0,
849                        (LPARAM)MAKELONG(LOWORD(spDragInfo)+sizeof(DRAGINFO16),
850                                         HIWORD(spDragInfo)) );
851             hCurrentWnd = lpDragInfo->hScope;
852             if( hCurrentWnd )
853                 SendMessage16( hCurrentWnd, WM_DRAGSELECT, 1, (LPARAM)spDragInfo);
854         }
855         else
856             if( hCurrentWnd )
857                 SendMessage16( hCurrentWnd, WM_DRAGMOVE, 0, (LPARAM)spDragInfo);
858
859     } while( msg.message != WM_LBUTTONUP && msg.message != WM_NCLBUTTONUP );
860
861     ReleaseCapture();
862     ShowCursor( FALSE );
863
864     if( hCursor ) SetCursor(hOldCursor);
865
866     if( hCurrentCursor != hBummer )
867         msg.lParam = SendMessage16( lpDragInfo->hScope, WM_DROPOBJECT,
868                                    (WPARAM16)hWnd, (LPARAM)spDragInfo );
869     else
870         msg.lParam = 0;
871     GlobalFree16(hDragInfo);
872
873     return (DWORD)(msg.lParam);
874 }
875
876
877 /**********************************************************************
878  *          DrawFrameControl  (USER.656)
879  */
880 BOOL16 WINAPI DrawFrameControl16( HDC16 hdc, LPRECT16 rc, UINT16 uType, UINT16 uState )
881 {
882     RECT rect32;
883     BOOL ret;
884
885     CONV_RECT16TO32( rc, &rect32 );
886     ret = DrawFrameControl( HDC_32(hdc), &rect32, uType, uState );
887     CONV_RECT32TO16( &rect32, rc );
888     return ret;
889 }
890
891 /**********************************************************************
892  *          DrawEdge   (USER.659)
893  */
894 BOOL16 WINAPI DrawEdge16( HDC16 hdc, LPRECT16 rc, UINT16 edge, UINT16 flags )
895 {
896     RECT rect32;
897     BOOL ret;
898
899     CONV_RECT16TO32( rc, &rect32 );
900     ret = DrawEdge( HDC_32(hdc), &rect32, edge, flags );
901     CONV_RECT32TO16( &rect32, rc );
902     return ret;
903 }
904
905 /**********************************************************************
906  *              CheckMenuRadioItem (USER.666)
907  */
908 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu, UINT16 first, UINT16 last,
909                                    UINT16 check, BOOL16 bypos)
910 {
911      return CheckMenuRadioItem( HMENU_32(hMenu), first, last, check, bypos );
912 }