Release 940301

Tue Mar  1 11:30:21 1994  Bob Amstadt  (bob@pooh)

	* [Configure] [*/Imakefile]
	Created configure script to handle different types of Wine builds.

	* [Configure] [tools/build.c] [if1632/Imakefile] [Wine.tmpl]
	Added ability to compile Wine on systems with 14-char filename limit.

	* [if1632/relay.c] [include/options.h] [misc/main.c]
	Added -relaydbg option to command line if DEBUG_RELAY is defined.

	* [loader/selector.c]
	Fixed bug in GetEntryDLLName() that caused Wine to seg fault.

	* [memory/heap.c]
	Fixed LocalInit() to work correctly.

	* [misc/user.c]
	Added code to call loaded DLLs' initialization routines.

Tue Mar  1 01:01:17 MET 1994  julliard@di.epfl.ch (Alexandre Julliard)

	* [windows/dce.c]
	Added clipping of child windows by their parent's client area.

	* [windows/nonclient.c]
	Bug fix in NC_DoNCPaint().

	* [windows/painting.c]
	Bug fix in RedrawWindow().

Feb 27, 94 martin2@trgcorp.solucorp.qc.ca (Martin Ayotte)

	* [mem/atom.c]
	Bug fix again in ATOM_DeleteAtom() : 
		change LocalFree() by USER_HEAP_FREE().
	(Previous patch done Feb 13th had been lost)

	* [controls/scroll.c]
	Move bitmaps loading in AllocScrollBar() instead of in WM_CREATE.

	* [windows/class.c] (sorry Alex ...)
	There was no bug there "in RegisterClass() : 
                WNDCLASS->lpszClassName was reset to NULL."
                              ^^^^^

	* [misc/clipboard.c]	--- New File ---
	New function EnumClipboardFormats().
	New function OpenClipboard().
	New function CloseClipboard().
	New function EmptyClipboard().
	New function GetClipboardOwner().
	New function GetClipboardViewer().
	New function CountClipboardFormats().
	New function IsClipboardFormatAvailable().
	New function OpenClipboard().
	New function GetClipboardData().
	New function SetClipboardViewer().
	New function EnumClipboardFormats().
	New function RegisterClipboardFormat().
	New function ChangeClipboardChain().
	New function SetClipboardData().
	New function GetOpenClipboardWindow().
	New function GetPriorityClipboardFormat().
	New function GetClipboardFormatName().

Tue Mar  1 20:15:45 1994  Erik Bos <erik@trashcan.hacktic.nl>

	* [misc/comm.c]
	bugfix in OpenComm().

Tue Feb 22 18:04:02 1994  Jeffrey Hsu <hsu@freefall.cdrom.com>

	* [include/winsock.h]
	The sockproto struct is already defined in <sys/socket.h>

	* [misc/winsock.c]
	Need to include <netinet/in.h> for struct in-addr.
	Use sys_errlist[] instead of strerror[].

	*[toolkit/heap.c]
	ANSI C specifies that the malloc functions are defined in stdlib.h,
	  so we don't need to include malloc.h.

	*[loader/ldtlib.c]
	Print informative error message about probable cause of i386_set_ldt()
	  failure and then exit.

	*[Imakefile]
	For systems that don't use gmake by default, set the MAKE variable
	  to gmake and propagate it on recursive makes.
	Take out -li386 for FreeBSD systems and define __FreeBSD__ so Wine
	  can be built with the FreeBSD 1.0.2 compiler.

Tue Feb 22 02:52:19 EST 1994    jrichard@cs.uml.edu (John Richardson)

        * [objects/bitblt.c]
        Added in three functions to do stretching and compression
        for WHITEONBLACK, BLACKONWHITE, and color copies.

Tue Feb 22 15:01:31 EST 1994    jrichard@cs.uml.edu (John Richardson)

        * [windows/graphics.c]
        Added FloodFill and FloodFill_rec.  FloodFill_rec is pretty
        inefficent, but should suffice for now.

        * [include/windows.h]
        Changed the x,y paramaters for the FloodFill prototype 
        from ints to shorts

Tue Feb 22 00:58:28 MET 1994  julliard@di.epfl.ch (Alexandre Julliard)

	* [windows/widgets.c]
	Added desktop window class.

	* [windows/painting.c]
	Bug fix in RedrawWindow().
	Implemented ExcludeUpdateRgn().

	* [windows/win.c] [windows/winpos.c]
	Implemented desktop window.

	* [controls/desktop.c]
	Preliminary desktop window procedure.

Feb 20, 94 martin2@trgcorp.solucorp.qc.ca (Martin Ayotte)

	* [controls/menu.c]
	New function LoadMenuIndirect().
	New function GetMenuCheckMarkDimensions().

	* [if1632/user.spec]
	Entry for DefDlgProc().

	* [windows/class.c]
	Fix bug in RegisterClass() : 
		WNDCLASS->lpszMenuName was reset to NULL.

	* [windows/win.c]
	In CreateWindowEx(), if hMenu == 0 then use 
		wndclass->lpszMenuName to load Menu from resource;

Mon Feb 21 22:38:28 1994 Erik Bos (erik@trashcan.hacktic.nl)

	* [loader/library.c] [loader/wine.c]
	Fixed runing DLL's as main executable problem.

	* [misc/dos_fs.c]
	Added wildcard support in DOS_readdir().

	* [misc/winsocket.c]
	Added proper error handling of BSD winsocket functions.

	* [miscemu/int21.c]
	KERNEL_DOS3Call renamed to DOS3Call and modified to use do_int21().

	* [main/main.c]
	Added functions for GetVersion, GetWinFlags and GetTimerResolution
	for libwine.a, SystemParametersInfo() partly implemented.

Tue Feb 22 19:00:02 1994  Miguel de Icaza  (miguel@xochitl.nuclecu.unam.mx)

	* [toolkit/winmain.c]
	Added _WinMain function. Setups the library (calls USER_InitApp).

	* [toolkit/sup.c]
	Added load_mz_header, load_ne_header, load_type_info and
	load_name_info functions.

	* [toolkit/heap.c] 
	Code cleanup. 

	* [misc/user.c]
	Moved from loader/misc.c. I hope to put back all the loader
	functions in the ~loader subdirectory in the future. CUrrently is
	needed since it has USER_InitApp.

	* [misc/resource.c]
	Since WineLib will probably need DLLs (currently it needs
	Sysres.dll). WineLib will be using much code of the loader again.
	So I removed some ifdefs that were used by WineLib.

	Added load_typeinfo and load_nameinfo (and the corresponding
	functions in [toolkit/sup.c]

	Added integer convertion functions in the needed places.

	Added very ugly patch (includes wine.c). In the next release I
	plan to move back all the loader routines to ~/loader. In the
	meantime I needed this patch. It doesn't affect any of the
	emulator code (its ifdefed for WineLib).

	* [misc/main.c]
	Cleaned up call to WinMain (now uses [toolkit/winmain.c]
	Ifdefed argument number checking when compiling the library.

	* [loader/wine.c]
	Modified to use load_(mz|ne)_header instead of doing a direct
	read.  When compiling the emulator it still uses the direct read
	for performance. 

	* [include/wine.h]
	Prototypes for loading routines.

	* [include/class.h]
	Added WINE_PACKED macro instead of __attribute__ ((packed))

	* [include/arch.h]
	Macros for converting integers (Little endian to big-endian).
	Needed in the Sun to allow loading of DLL files.

Mon Feb 14 23:48:09 MET 1994  julliard@di.epfl.ch (Alexandre Julliard)

	* [windows/clipping.c]
	Moved everything into windows/painting.c and removed this file.

	* [windows/message.c]
	Removed calls to memmove() in MSG_RemoveMsg().

	* [windows/nonclient.c]
	Added WM_GETMINMAXINFO support for window resizing.

	* [windows/painting.c]
	Implemented RedrawWindow().

	* [windows/scroll.c]
	Bug fix in ScrollWindowEx().

	* [windows/win.c]
	Moved UpdateWindow() to windows/painting.c.

Fri Feb 11 12:41:28 1994  Erik Bos (erik@trashcan.hacktic.nl)
	
	* [loader/wine.c] [misc/dos_fs.c]
	getenv() eq NULL bugfixes.

	* [misc/comm.c]
	cfmakeraw changed for SunOS.

Feb 13, 94 martin2@trgcorp.solucorp.qc.ca (Martin Ayotte)

	* [mem/atom.c]
	Bug fix in ATOM_DeleteAtom() : 
		change LocalFree() by USER_HEAP_FREE().

	* [misc/message.c]
	New function FatalAppExit().

	* [objects/font.c]
	New empty stub SetMapperFlags().

	* [controls/menu.c]
	Better CheckMark & other bitmaps placement.

	* [windows/graphics.c]
	New function RoundRect() : calc.exe now working...

Tue Feb 15 14:29:37 1994  John Richardson

        * [objects/bitblt.c]
        Fixed StretchBlt so it works quicker and faster.  It still doesn't
        use the StretchMode bits for bitmap compression, but that will
        come soon.

Mon Feb 14 13:21:04 1994  Miguel de Icaza  (miguel@roxanne.nuclecu.unam.mx)

	* [include/windows.h,dialog.h,gdi.h]
	Changed __atribute__ ((packed)) for WINE_PACKED. 
	When compiling the library this is defined as nothing. This gets
	rid with all the problems compiling under SunOS. Also 

	* [windows/utility.c]
	Added DebugPrintString.

	* [toolkit/sup.c]
	Added hSysRes = 1 definition to resolve externals.
	Added CallLineDDAProc function (toolkit version).
	Added toy, and hi ineficient memmove until code in message.c get
	rewritten.

	* [objects/gdiobj.c]
	Ifdefed linux/emulator-dependent code to allow compilation of
	WineLib.

	* [misc/winsocket.c]
	Added ifdef to allow compilation under SunOS.

	* [misc/resource.c]
	When compiling the library, the resource file is expected to be in
	a file called $argv[0].Res. Also the information of the resource
	is expected to be at offset 0 of the .Res file.

	* [misc/main.c]
	Call WinMain with arguments if compiling WineLib.
	Call shutdown functions (Comm_DeInit, DOS_DeInitFS).
	Call sync_profiles to preserve changes to .INI files.

	* [misc/comm.c,dos_fs.c]
	removed call to atexit(Comm_DeInit)
	removed call to atexit(DOS_DeInitFS)
	Shutdown functions are now called from the return of WinMain

	* removed memorylib subdirectory

	* moved memory/atom.c    to misc/atom.c
	  moved memorylib/heap.c to toolkit/heap.c

	* [loader/wine.c]
	Moved DebugPrintString to windows/utility.c

	* [include/winsock.h]
	Define SO_DONTLINGER only if it has not been previously defined.

	* [include/windows.h]
	added definition for DLGPROC.
	added definition for LMEM_WINE_ALIGN and other LMEM_* constants.
	When compiling WineLib WNDPROC is defined with all the parameters
	to avoid compilation problems.

	* [include/user.h]
	When compiling WineLib USER_HEAP_(ALLOC|REALLOC|ADDR|FREE) the
	calls are translated to the library allocation routines.

	* [include/gdi.h,user.h]
	When compiling WineLib GDI_HEAP_(ALLOC|ADDR|FREE) the calls are
	translated to the library allocation routines.

	* [include/atom.h]
	Defined LocalAlign. When compiling the emulator it's translated as
	a call to LocalAlloc (the original code), when compiling WineLib
	as a call to LocalAlloc with a WINE-flag called LMEM_WINE_ALIGN
	(atom.c needs aligned data on a 4 byte boundary).

	* [misc/file.c]
	Renamed KERNEL_* functions and fixed prototypes.

	* [if1632/kernel.spec]
	Renamed KERNEL_* functions in order to be used by applications
	using Wine as a library (OpeFile, _lclose, _lread, _lcreate,
	_llseek, _lopen, _lwrite).

	* [Makefile]
	Create library instead of executable when building target
	libwine.a 

Tue Feb  8 13:52:11 1994  Miguel de Icaza  (miguel@roxanne)

	* [Makefiles]
	Use $(CC) instead of cc.
	Added libwine target.

	* [include/prototypes]
	#ifdefed section for WineLib

	* moved loader/cursor.c   to misc/cursor.c
	  moved loader/resource.c to misc/resource.c
	  moved misc/emulate.c    to miscemu/emulate.c
	  moved misc/int1a.c      to miscemu/int1a.c
	  moved misc/int21.c	  to miscemu/int21.c
	  moved	misc/kernel.c	  to miscemu/kernel.c
	  moved misc/user.c       to miscemu/user.c

	* [memorylib/heap.c]
	Heap management for WineLib

	* [misc/comm.c]
	Modified to allow compilation under SunOS (#include errno, SunOS
	doesn't have atexit ()).

	* [misc/dos_fs.c]
	Modified to allow compilation under SunOS (#include vfs.h)

	* [misc/file.c]
	Modified to allow compilation under SunOS (OPEN_MAX constant,
	#include unistd.h)

	* [objects/palette.c]
	Modified to allow compilation under SunOS (#include limits)

	* [toolkit/sup.c]
	WineLib version of CallWindowProc.

	* [windows/event.c]
	Typedef XPointer under X11R4 (OpenWindows).

	* [windows/win.c]
	When compiling WineLib, use direct callbacks instead of the
	windows supplied callbacks.

Mon Feb  7 22:37:34 MET 1994  julliard@di.epfl.ch (Alexandre Julliard)

	* [loader/cursor.c]
	New function CURSOR_SetWinCursor(), for internal use, to set
	the cursor of a specific window more reliably than with SetCursor().

	* [windows/nonclient.c]
	Better window management. Moving and resizing from the system
	menu should work now.
	Added scroll-bar mouse tracking.

	* [windows/win.c]
	Moved scroll-bar creation and destruction to defwnd.c.

Feb 5, 94 martin2@trgcorp.solucorp.qc.ca (Martin Ayotte)

	* [windows/nonclient.c]
	Call to StdDrawMenuBar() during NC's drawing.
	New NC_TrackMouseMenuBar() function which call 
		MenuButtonDown(), MenuButtonUp() & MenuMouseMove().

	* [controls/menu.c]
	New ChangeMenu() function.
	Remove permanently old Xt menu code.
	Make common functions MenuButtonDown(), MenuButtonUp()
		& MenuMouseMove() for both popups & menubar.
	
	* [controls/combo.c]
	Paint OBM_COMBO directly in combo client.

	* [controls/listbox.c]
	Fix bug in multicolumns calculations.

	* [controls/Makefile]
	Remove rules for old file 'caption.c'.

	* [misc/kernel.c]
	Remove empty stub GetModuleFileName().

	* [loader/library.c]
	New GetModuleHandle() function.
	New GetModuleUsage() function.
	New GetModuleFileName() function.

	* [loader/resource.c]
	Try to find the bug a missing menu loading ... Not found yet !

	* [windows/win.c]
	Remove old menubar creation.

Thu Feb  3 22:30:11 1994  Erik Bos (erik@trashcan.hacktic.nl)

	* [misc/winsocket.c]
	More functions added.

	* [if1632/winsock.spec] [misc/winsocket.c]
	Added John Brezak's winsock.dll stuff.
diff --git a/controls/menu.c b/controls/menu.c
index add8f18..53d9f0d 100644
--- a/controls/menu.c
+++ b/controls/menu.c
@@ -1,22 +1,19 @@
+/*
+ *        Menus functions
+ */
 static char RCSId[] = "$Id$";
-static char Copyright[] = "Copyright  Robert J. Amstadt, 1993";
-static char Copyright2[] = "Copyright  Martin Ayotte, 1993";
+static char Copyright[] = "Copyright  Martin Ayotte, 1993";
 
 /*
 #define DEBUG_MENU
 #define DEBUG_SYSMENU
 */
-#define USE_POPUPMENU
 
-#include <X11/Intrinsic.h>
-#include <X11/StringDefs.h>
 #include "windows.h"
 #include "sysmetrics.h"
 #include "menu.h"
 #include "heap.h"
 #include "win.h"
-#include "bitmaps/check_bitmap"
-#include "bitmaps/nocheck_bitmap"
 
 #define SC_ABOUTWINE     SC_SCREENSAVE+1
 #define SC_SYSMENU	 SC_SCREENSAVE+2
@@ -27,22 +24,17 @@
 HBITMAP hStdCheck = 0;
 HBITMAP hStdMnArrow = 0;
 
-static LPMENUBAR firstMenu = NULL;
-static MENUITEM *parentItem;
-static MENUITEM *siblingItem;
-static int       lastLevel;
-static int       menuId = 0;
-static Pixmap    checkBitmap = XtUnspecifiedPixmap;
-static Pixmap    nocheckBitmap = XtUnspecifiedPixmap;
-
 LPPOPUPMENU PopupMenuGetStorageHeader(HWND hwnd);
 LPPOPUPMENU PopupMenuGetWindowAndStorage(HWND hwnd, WND **wndPtr);
-void StdDrawMenuBar(HWND hwnd);
+void StdDrawMenuBar(HDC hDC, LPRECT lprect, LPPOPUPMENU lppop);
+void MenuButtonDown(HWND hWnd, LPPOPUPMENU lppop, int x, int y);
+void MenuButtonUp(HWND hWnd, LPPOPUPMENU lppop, int x, int y);
+void MenuMouseMove(HWND hWnd, LPPOPUPMENU lppop, WORD wParam, int x, int y);
 void StdDrawPopupMenu(HWND hwnd);
-LPMENUITEM MenuFindItem(HWND hwnd, int x, int y, WORD *lpRet);
-LPMENUITEM MenuFindItemBySelKey(HWND hwnd, WORD key, WORD *lpRet);
+LPMENUITEM MenuFindItem(LPPOPUPMENU lppop, int x, int y, WORD *lpRet);
+LPMENUITEM MenuFindItemBySelKey(LPPOPUPMENU lppop, WORD key, WORD *lpRet);
 void PopupMenuCalcSize(HWND hwnd);
-void MenuBarCalcSize(HWND hwnd);
+void MenuBarCalcSize(HDC hDC, LPRECT lprect, LPPOPUPMENU lppop);
 LPMENUITEM GetMenuItemPtr(LPPOPUPMENU menu, WORD nPos);
 WORD GetSelectionKey(LPSTR str);
 LPSTR GetShortCutString(LPSTR str);
@@ -68,6 +60,7 @@
     HMENU	hSubMenu;
     RECT	rect;
     HDC		hDC;
+    PAINTSTRUCT ps;
     switch(message) 
     {
     case WM_CREATE:
@@ -82,20 +75,6 @@
 #ifdef DEBUG_MENU
         printf("PopupMenu WM_CREATE lppop=%08X !\n", lppop);
 #endif
-/*
-	if (lppop->BarFlags == 0) {
-	    PopupMenuCalcSize(hwnd);
-	    printf("PopupMenu WM_CREATE Width=%d Height=%d !\n", 
-			lppop->Width, lppop->Height);
-	    SetWindowPos(hwnd, 0, 0, 0, lppop->Width + 2, lppop->Height, 
-		SWP_NOZORDER | SWP_NOMOVE);
-	    }
-	else {
-	    MenuBarCalcSize(hwnd);
-	    SetWindowPos(hwnd, 0, 0, -16, lppop->Width, lppop->Height, 
-		SWP_NOZORDER);
-	    }
-*/
 	if (hStdCheck == (HBITMAP)NULL) 
 	    hStdCheck = LoadBitmap((HANDLE)NULL, (LPSTR)OBM_CHECK);
 	if (hStdMnArrow == (HBITMAP)NULL) 
@@ -129,15 +108,18 @@
 		SendMessage(lppop->ownerWnd, WM_SYSCOMMAND, wParam, lParam);
 		}
 	    }
-	if (lppop->BarFlags == 0) ShowWindow(hwnd, SW_HIDE);
+	if (lppop->BarFlags == 0)  ShowWindow(hwnd, SW_HIDE);
     	break;
     case WM_SHOWWINDOW:
 	lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
-    	if (wParam == 0) {
+    	if (wParam == 0 && lParam == 0L) {
 	    HideAllSubPopupMenu(lppop);
 #ifdef DEBUG_MENU
-    	printf("PopupMenu WM_SHOWWINDOW -> HIDE!\n");
+	    printf("PopupMenu WM_SHOWWINDOW -> HIDE!\n");
 #endif
+/*
+	    UpdateWindow(lppop->ownerWnd);
+*/
 	    break;
 	    }
 	lppop->FocusedItem = (WORD)-1;
@@ -150,167 +132,20 @@
 	    SetWindowPos(hwnd, 0, 0, 0, lppop->Width + 2, lppop->Height, 
 		SWP_NOZORDER | SWP_NOMOVE);
 	    }
-	else {
-	    MenuBarCalcSize(hwnd);
-#ifdef DEBUG_MENU
-	    printf("MenuBarMenu WM_SHOWWINDOW Width=%d Height=%d !\n", 
-			lppop->Width, lppop->Height);
-#endif
-	    SetWindowPos(hwnd, 0, 0, -16, lppop->Width, lppop->Height, 
-		SWP_NOZORDER);
-	    }
     	break;
     case WM_LBUTTONDOWN:
 	lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
 	SetCapture(hwnd);
-	lpitem = MenuFindItem(hwnd, LOWORD(lParam), HIWORD(lParam), &wRet);
-#ifdef DEBUG_MENU
-	printf("PopupMenu WM_LBUTTONDOWN wRet=%d lpitem=%08X !\n", wRet, lpitem);
-#endif
-	if (lpitem != NULL) {
-	    if (lppop->FocusedItem != (WORD)-1) {
-		HideAllSubPopupMenu(lppop);
-		lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
-		if (((lpitem2->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
-		    ((lpitem2->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
-	            hDC = GetDC(hwnd);
-	            InvertRect(hDC, &lpitem2->rect);
-		    ReleaseDC(hwnd, hDC);
-		    }
-		}
-	    lppop->FocusedItem = wRet;
-	    if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
-		((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
-	        hDC = GetDC(hwnd);
-	        InvertRect(hDC, &lpitem->rect);
-		ReleaseDC(hwnd, hDC);
-		}
-	    if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
-		hSubMenu = (HMENU)lpitem->item_id;
-		lppop2 = (LPPOPUPMENU) GlobalLock(hSubMenu);
-		if (lppop2 == NULL) break;
-		lppop2->hWndParent = hwnd;
-		GetClientRect(hwnd, &rect);
-		if (lppop->BarFlags != 0) {
-		    y = rect.bottom - rect.top;
-		    GetWindowRect(hwnd, &rect);
-		    y += rect.top;
-		    TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON, 
-			rect.left + lpitem->rect.left, 
-			y, 0, lppop->ownerWnd, (LPRECT)NULL);
-		    }
-		else {
-		    x = rect.right;
-		    GetWindowRect(hwnd, &rect);
-		    x += rect.left;
-		    TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON, 
-			x, rect.top + lpitem->rect.top,
-			0, lppop->ownerWnd, (LPRECT)NULL);
-		    }
-		break;
-		}
-	    }
+	MenuButtonDown(hwnd, lppop, LOWORD(lParam), HIWORD(lParam));
 	break;
     case WM_LBUTTONUP:
 	lppop = PopupMenuGetStorageHeader(hwnd);
 	ReleaseCapture();
-	lpitem = MenuFindItem(hwnd, LOWORD(lParam), HIWORD(lParam), &wRet);
-#ifdef DEBUG_MENU
-	printf("PopupMenu WM_LBUTTONUP wRet=%d lpitem=%08X !\n", wRet, lpitem);
-#endif
-	if (lpitem != NULL) {
-	    if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
-		break;
-		}
-	    if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
-		((lpitem->item_flags & MF_POPUP) != MF_POPUP)) {
-	    	ShowWindow(lppop->hWnd, SW_HIDE);
-		if (lppop->hWndParent != (HWND)NULL) {
-		    SendMessage(lppop->hWndParent, WM_COMMAND, 
-					lpitem->item_id, 0L);
-#ifdef DEBUG_MENU
-		    printf("PopupMenu // WM_COMMAND to ParentMenu wParam=%d !\n", 
-			lpitem->item_id);
-#endif
-		    }
-		else {
-		    if (lppop->SysFlag == 0) {
-#ifdef DEBUG_MENU
-			printf("PopupMenu // WM_COMMAND wParam=%d !\n", 
-				lpitem->item_id);
-#endif
-			SendMessage(lppop->ownerWnd, WM_COMMAND, 
-					lpitem->item_id, 0L);
-			}
-		    else {
-			if (lpitem->item_id == SC_ABOUTWINE) {
-			    printf("SysMenu // Show 'About Wine ...' !\n");
-/*			    DialogBox(hSysRes, MAKEINTRESOURCE(SC_ABOUTWINEDLG), */
-			    DialogBox(hSysRes, MAKEINTRESOURCE(2), 
-				GetParent(hwnd), (FARPROC)AboutWine_Proc);
-			    }
-			else {
-			    SendMessage(lppop->ownerWnd, WM_SYSCOMMAND,
-						 lpitem->item_id, 0L);
-#ifdef DEBUG_SYSMENU
-			    printf("PopupMenu // WM_SYSCOMMAND wParam=%04X !\n", 
-					lpitem->item_id);
-#endif
-			    }
-			}
-		    }
-#ifdef DEBUG_MENU
-		printf("PopupMenu // SendMessage WM_COMMAND wParam=%d !\n", 
-			lpitem->item_id);
-#endif
-	    	break;
-		}
-	    }
-	if (lppop->FocusedItem != (WORD)-1) {
-	    HideAllSubPopupMenu(lppop);
-	    lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
-	    if (((lpitem2->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
-		((lpitem2->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
-	        hDC = GetDC(hwnd);
-	        InvertRect(hDC, &lpitem2->rect);
-		ReleaseDC(hwnd, hDC);
-		}
-	    }
+	MenuButtonUp(hwnd, lppop, LOWORD(lParam), HIWORD(lParam));
 	break;
     case WM_MOUSEMOVE:
-        if ((wParam & MK_LBUTTON) != 0) {
-	    lppop = PopupMenuGetStorageHeader(hwnd);
-	    lpitem = MenuFindItem(hwnd, LOWORD(lParam), HIWORD(lParam), &wRet);
-	    if ((lpitem != NULL) && (lppop->FocusedItem != wRet)) {
-		lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
-		hDC = GetDC(hwnd);
-		if (((lpitem2->item_flags & MF_POPUP) == MF_POPUP) ||
-		    ((lpitem2->item_flags & MF_STRING) == MF_STRING)) {
-		    InvertRect(hDC, &lpitem2->rect);
-		    }
-		if ((lpitem2->item_flags & MF_POPUP) == MF_POPUP) {
-		    HideAllSubPopupMenu(lppop);
-		    }
-		lppop->FocusedItem = wRet;
-		if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
-		    ((lpitem->item_flags & MF_STRING) == MF_STRING)) {
-		    InvertRect(hDC, &lpitem->rect);
-		    }
-		if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
-		    hSubMenu = (HMENU)lpitem->item_id;
-		    lppop2 = (LPPOPUPMENU) GlobalLock(hSubMenu);
-		    if (lppop2 == NULL) break;
-		    if (lppop->BarFlags != 0) {
-			lppop2->hWndParent = hwnd;
-			GetWindowRect(hwnd, &rect);
-			TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON, 
-				lpitem->rect.left, rect.top, 
-				0, lppop->ownerWnd, (LPRECT)NULL);
-			}
-		    }
-		ReleaseDC(hwnd, hDC);
-		}
-            }
+	lppop = PopupMenuGetStorageHeader(hwnd);
+	MenuMouseMove(hwnd, lppop, wParam, LOWORD(lParam), HIWORD(lParam));
 	break;
 
     case WM_KEYDOWN:
@@ -441,6 +276,7 @@
 				x, lpitem->rect.top,
 				0, lppop->ownerWnd, (LPRECT)NULL);
 			}
+		    GlobalUnlock(hSubMenu);
 		    break;
 		    }
 		if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
@@ -482,7 +318,7 @@
 		}
 	    }
 	if (wParam >= 'a' && wParam <= 'z') wParam -= 'a' - 'A';
-	lpitem = MenuFindItemBySelKey(hwnd, wParam, &wRet);
+	lpitem = MenuFindItemBySelKey(lppop, wParam, &wRet);
 	if (lpitem != NULL) {
 	    printf("Found  wRet=%d !\n", wRet);
 	    if (lppop->FocusedItem != (WORD)-1) {
@@ -502,13 +338,7 @@
 	break;
     case WM_PAINT:
 	lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
-	if (lppop->BarFlags != 0) {
-	    MenuBarCalcSize(hwnd);
-	    printf("PopupMenu WM_PAINT Width=%d Height=%d !\n", 
-			lppop->Width, lppop->Height);
-	    StdDrawMenuBar(hwnd);
-	    }
-	else{
+	if (lppop->BarFlags == 0) {
 	    PopupMenuCalcSize(hwnd);
 	    StdDrawPopupMenu(hwnd);
 	    }
@@ -520,6 +350,192 @@
 }
 
 
+void MenuButtonDown(HWND hWnd, LPPOPUPMENU lppop, int x, int y)
+{
+    HDC		hDC;
+    LPMENUITEM	lpitem, lpitem2;
+    RECT	rect;
+    HMENU	hSubMenu;
+    WORD	wRet;
+    LPPOPUPMENU lppop2;
+    lpitem = MenuFindItem(lppop, x, y, &wRet);
+#ifdef DEBUG_MENU
+    printf("MenuButtonDown // x=%d y=%d // wRet=%d lpitem=%08X !\n", 
+					x, y, wRet, lpitem);
+#endif
+    if (lpitem != NULL) {
+	if (lppop->FocusedItem != (WORD)-1) {
+	    HideAllSubPopupMenu(lppop);
+	    lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
+	    if (((lpitem2->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
+		((lpitem2->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
+	        hDC = GetWindowDC(hWnd);
+	        InvertRect(hDC, &lpitem2->rect);
+		ReleaseDC(hWnd, hDC);
+		}
+	    }
+	lppop->FocusedItem = wRet;
+	if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
+	    ((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
+	    hDC = GetWindowDC(hWnd);
+	    InvertRect(hDC, &lpitem->rect);
+	    ReleaseDC(hWnd, hDC);
+	    }
+	if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
+	    hSubMenu = (HMENU)lpitem->item_id;
+	    lppop2 = (LPPOPUPMENU) GlobalLock(hSubMenu);
+	    if (lppop2 == NULL) return;
+	    lppop2->hWndParent = hWnd;
+	    if (lppop->BarFlags != 0) {
+		GetWindowRect(hWnd, &rect);
+/*		y = rect.top + lppop->Height; */
+		y = rect.top + lppop->rect.bottom;
+		TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON, 
+			rect.left + lpitem->rect.left, 
+			y, 0, lppop->ownerWnd, (LPRECT)NULL);
+		}
+	    else {
+		x = lppop->rect.right;
+		GetWindowRect(hWnd, &rect);
+		x += rect.left;
+		TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON, 
+			x, rect.top + lpitem->rect.top,
+			0, lppop->ownerWnd, (LPRECT)NULL);
+		}
+	    GlobalUnlock(hSubMenu);
+	    }
+	}
+}
+
+
+
+void MenuButtonUp(HWND hWnd, LPPOPUPMENU lppop, int x, int y)
+{
+    HDC		hDC;
+    LPMENUITEM	lpitem, lpitem2;
+    RECT	rect;
+    HMENU	hSubMenu;
+    WORD	wRet;
+    LPPOPUPMENU lppop2;
+    lpitem = MenuFindItem(lppop, x, y, &wRet);
+#ifdef DEBUG_MENU
+    printf("MenuButtonUp // x=%d y=%d // wRet=%d lpitem=%08X !\n", 
+					x, y, wRet, lpitem);
+#endif
+    if (lpitem != NULL) {
+	if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
+	    return;
+	    }
+	if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
+	    ((lpitem->item_flags & MF_POPUP) != MF_POPUP)) {
+	    ShowWindow(lppop->hWnd, SW_HIDE);
+	    if (lppop->hWndParent != (HWND)NULL) {
+		SendMessage(lppop->hWndParent, WM_COMMAND, 
+				lpitem->item_id, 0L);
+#ifdef DEBUG_MENU
+		printf("MenuButtonUp // WM_COMMAND to ParentMenu wParam=%d !\n", 
+			lpitem->item_id);
+#endif
+		}
+	    else {
+		if (lppop->SysFlag == 0) {
+#ifdef DEBUG_MENU
+		    printf("PopupMenu // WM_COMMAND wParam=%d !\n", 
+				lpitem->item_id);
+#endif
+		    SendMessage(lppop->ownerWnd, WM_COMMAND, 
+					lpitem->item_id, 0L);
+		    }
+		else {
+		    if (lpitem->item_id == SC_ABOUTWINE) {
+			printf("SysMenu // Show 'About Wine ...' !\n");
+/*			DialogBox(hSysRes, MAKEINTRESOURCE(SC_ABOUTWINEDLG), */
+			DialogBox(hSysRes, MAKEINTRESOURCE(2), 
+				GetParent(hWnd), (FARPROC)AboutWine_Proc);
+			}
+		    else {
+			SendMessage(lppop->ownerWnd, WM_SYSCOMMAND,
+					 lpitem->item_id, 0L);
+#ifdef DEBUG_SYSMENU
+			printf("MenuButtonUp // WM_SYSCOMMAND wParam=%04X !\n", 
+					lpitem->item_id);
+#endif
+			}
+		    }
+		}
+#ifdef DEBUG_MENU
+	    printf("MenuButtonUp // SendMessage WM_COMMAND wParam=%d !\n", 
+			lpitem->item_id);
+#endif
+	    return;
+	    }
+	}
+    if (lppop->FocusedItem != (WORD)-1) {
+	HideAllSubPopupMenu(lppop); 
+	lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
+	if (((lpitem2->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
+	    ((lpitem2->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
+	    hDC = GetWindowDC(hWnd);
+	    InvertRect(hDC, &lpitem2->rect);
+	    ReleaseDC(hWnd, hDC);
+	    }
+	}
+}
+
+
+
+void MenuMouseMove(HWND hWnd, LPPOPUPMENU lppop, WORD wParam, int x, int y)
+{
+    HDC		hDC;
+    LPMENUITEM	lpitem, lpitem2;
+    RECT	rect;
+    HMENU	hSubMenu;
+    WORD	wRet;
+    LPPOPUPMENU lppop2;
+    if ((wParam & MK_LBUTTON) != 0) {
+	lpitem = MenuFindItem(lppop, x, y, &wRet);
+#ifdef DEBUG_MENU
+	printf("MenuMouseMove // x=%d y=%d // wRet=%d lpitem=%08X !\n", 
+					x, y, wRet, lpitem);
+#endif
+	if ((lpitem != NULL) && (lppop->FocusedItem != wRet)) {
+	    lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
+	    hDC = GetWindowDC(hWnd);
+	    if (((lpitem2->item_flags & MF_POPUP) == MF_POPUP) ||
+		((lpitem2->item_flags & MF_STRING) == MF_STRING)) {
+		InvertRect(hDC, &lpitem2->rect);
+		}
+	    if ((lpitem2->item_flags & MF_POPUP) == MF_POPUP) {
+		HideAllSubPopupMenu(lppop);
+		}
+	    lppop->FocusedItem = wRet;
+	    if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
+		((lpitem->item_flags & MF_STRING) == MF_STRING)) {
+		InvertRect(hDC, &lpitem->rect);
+		}
+	    if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
+		hSubMenu = (HMENU)lpitem->item_id;
+		lppop2 = (LPPOPUPMENU) GlobalLock(hSubMenu);
+		if (lppop2 == NULL) {
+		    ReleaseDC(hWnd, hDC);
+		    return;
+		    }
+		if (lppop->BarFlags != 0) {
+		    lppop2->hWndParent = hWnd;
+		    GetWindowRect(hWnd, &rect);
+		    rect.top += lppop->rect.bottom;
+		    TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON, 
+				rect.left + lpitem->rect.left, rect.top, 
+				0, lppop->ownerWnd, (LPRECT)NULL);
+		    }
+		GlobalUnlock(hSubMenu);
+		}
+	    ReleaseDC(hWnd, hDC);
+	    }
+	}
+}
+
+
 
 LPPOPUPMENU PopupMenuGetWindowAndStorage(HWND hwnd, WND **wndPtr)
 {
@@ -554,6 +570,7 @@
     LPPOPUPMENU lppop;
     lppop = (LPPOPUPMENU)GlobalLock(hMenu);
     lppop->hWndParent = hWnd;
+    GlobalUnlock(hMenu);
 }
 
 
@@ -589,16 +606,40 @@
     lpitem = lppop->firstItem;
     if (lpitem == NULL) goto EndOfPaint;
     for(i = 0; i < lppop->nItems; i++) {
+	CopyRect(&rect2, &lpitem->rect);
 	if ((lpitem->item_flags & MF_SEPARATOR) == MF_SEPARATOR) {
-	    CopyRect(&rect2, &lpitem->rect);
     	    hOldPen = SelectObject(hDC, GetStockObject(BLACK_PEN));
 	    MoveTo(hDC, rect2.left, rect2.top + 1);
 	    LineTo(hDC, rect2.right, rect2.top + 1);
 	    SelectObject(hDC, hOldPen);
 	    }
+	if ((lpitem->item_flags & MF_CHECKED) == MF_CHECKED) {
+	    hMemDC = CreateCompatibleDC(hDC);
+	    if (lpitem->hCheckBit == 0) {
+		SelectObject(hMemDC, hStdCheck);
+		GetObject(hStdCheck, sizeof(BITMAP), (LPSTR)&bm);
+		}
+	    else {
+		SelectObject(hMemDC, lpitem->hCheckBit);
+		GetObject(lpitem->hCheckBit, sizeof(BITMAP), (LPSTR)&bm);
+		}
+	    BitBlt(hDC, rect2.left, rect2.top + 1,
+		bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
+	    DeleteDC(hMemDC);
+	    }
+	else {
+	    if (lpitem->hUnCheckBit != 0) {
+		hMemDC = CreateCompatibleDC(hDC);
+		SelectObject(hMemDC, lpitem->hUnCheckBit);
+		GetObject(lpitem->hUnCheckBit, sizeof(BITMAP), (LPSTR)&bm);
+		BitBlt(hDC, rect2.left, rect2.top + 1,
+			bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
+		DeleteDC(hMemDC);
+		}
+	    }
 	if ((lpitem->item_flags & MF_BITMAP) == MF_BITMAP) {
 	    hBitMap = (HBITMAP)LOWORD((LONG)lpitem->item_text);
-	    CopyRect(&rect2, &lpitem->rect);
+	    rect2.left += lppop->CheckWidth;
 	    hMemDC = CreateCompatibleDC(hDC);
 	    SelectObject(hMemDC, hBitMap);
 	    GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
@@ -616,6 +657,7 @@
 		OldTextColor = SetTextColor(hDC, 0x00000000L);
 	    CopyRect(&rect3, &lpitem->rect);
 	    InflateRect(&rect3, 0, -2);
+	    rect3.left += lppop->CheckWidth;
 	    if ((x = GetShortCutPos(lpitem->item_text)) != (WORD)-1) {
 		DrawText(hDC, lpitem->item_text, x, &rect3, 
 		    DT_LEFT | DT_VCENTER | DT_SINGLELINE);
@@ -629,31 +671,13 @@
 	    SelectObject(hDC, hOldFont);
 	    CopyRect(&rect2, &lpitem->rect);
 	    }
-	if ((lpitem->item_flags & MF_CHECKED) == MF_CHECKED) {
-	    CopyRect(&rect3, &rect2);
-	    rect3.left = rect3.right - rect3.bottom + rect3.top;
-	    hMemDC = CreateCompatibleDC(hDC);
-	    if (lpitem->hCheckBit == 0)
-		SelectObject(hMemDC, hStdCheck);
-	    else
-		SelectObject(hMemDC, lpitem->hCheckBit);
-	    GetObject(hStdCheck, sizeof(BITMAP), (LPSTR)&bm);
-	    BitBlt(hDC, rect3.left, rect3.top,
-		bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
-	    DeleteDC(hMemDC);
-	    printf("StdDrawPopupMenu // MF_CHECKED hStdCheck=%04X !\n", hStdCheck);
-	    }
-	else {
-	    if (lpitem->hUnCheckBit != 0)
-		SelectObject(hMemDC, lpitem->hUnCheckBit);
-	    }
 	if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
-	    CopyRect(&rect3, &rect2);
-	    rect3.left = rect3.right - rect3.bottom + rect3.top;
+	    CopyRect(&rect3, &lpitem->rect);
+	    rect3.left = rect3.right - lppop->PopWidth;
 	    hMemDC = CreateCompatibleDC(hDC);
 	    SelectObject(hMemDC, hStdMnArrow);
 	    GetObject(hStdMnArrow, sizeof(BITMAP), (LPSTR)&bm);
-	    BitBlt(hDC, rect3.left, rect3.top,
+	    BitBlt(hDC, rect3.left, rect3.top + 1,
 		bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
 	    DeleteDC(hMemDC);
 	    }
@@ -666,40 +690,59 @@
 
 
 
-void StdDrawMenuBar(HWND hwnd)
+void StdDrawMenuBar(HDC hDC, LPRECT lprect, LPPOPUPMENU lppop)
 {
-    WND 	*wndPtr;
-    LPPOPUPMENU lppop;
     LPMENUITEM 	lpitem;
-    PAINTSTRUCT ps;
     HBRUSH 	hBrush;
     HPEN	hOldPen;
-    HWND	hWndParent;
-    HDC 	hDC, hMemDC;
+    HDC 	hMemDC;
     RECT 	rect, rect2, rect3;
     HFONT	hOldFont;
+    DWORD	OldTextColor;
     HBITMAP	hBitMap;
     BITMAP	bm;
     UINT  	i, textwidth;
-    hDC = BeginPaint( hwnd, &ps );
-    if (!IsWindowVisible(hwnd)) {
-	EndPaint( hwnd, &ps );
-	return;
-	}
+    if (lppop == NULL || lprect == NULL) return;
+#ifdef DEBUG_MENU
+    printf("StdDrawMenuBar(%04X, %08X, %08X); !\n", hDC, lprect, lppop);
+#endif
+    MenuBarCalcSize(hDC, lprect, lppop);
     hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
-    lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
-    if (lppop == NULL) goto EndOfPaint;
     hBrush = GetStockObject(WHITE_BRUSH);
-    GetClientRect(hwnd, &rect);
+    CopyRect(&rect, lprect);
     FillRect(hDC, &rect, hBrush);
     FrameRect(hDC, &rect, GetStockObject(BLACK_BRUSH));
     if (lppop->nItems == 0) goto EndOfPaint;
     lpitem = lppop->firstItem;
     if (lpitem == NULL) goto EndOfPaint;
     for(i = 0; i < lppop->nItems; i++) {
+	CopyRect(&rect2, &lpitem->rect);
+	if ((lpitem->item_flags & MF_CHECKED) == MF_CHECKED) {
+	    hMemDC = CreateCompatibleDC(hDC);
+	    if (lpitem->hCheckBit == 0) {
+		SelectObject(hMemDC, hStdCheck);
+		GetObject(hStdCheck, sizeof(BITMAP), (LPSTR)&bm);
+		}
+	    else {
+		SelectObject(hMemDC, lpitem->hCheckBit);
+		GetObject(lpitem->hCheckBit, sizeof(BITMAP), (LPSTR)&bm);
+		}
+	    BitBlt(hDC, rect2.left, rect2.top + 1,
+		bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
+	    DeleteDC(hMemDC);
+	    }
+	else {
+	    if (lpitem->hUnCheckBit != 0) {
+		hMemDC = CreateCompatibleDC(hDC);
+		SelectObject(hMemDC, lpitem->hUnCheckBit);
+		GetObject(lpitem->hUnCheckBit, sizeof(BITMAP), (LPSTR)&bm);
+		BitBlt(hDC, rect2.left, rect2.top + 1,
+			bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
+		DeleteDC(hMemDC);
+		}
+	    }
 	if ((lpitem->item_flags & MF_BITMAP) == MF_BITMAP) {
 	    hBitMap = (HBITMAP)LOWORD((LONG)lpitem->item_text);
-	    CopyRect(&rect2, &lpitem->rect);
 	    hMemDC = CreateCompatibleDC(hDC);
 	    SelectObject(hMemDC, hBitMap);
 	    GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
@@ -710,37 +753,30 @@
 	if (((lpitem->item_flags & MF_BITMAP) != MF_BITMAP) &&
 	    ((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
 	    ((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
-	    CopyRect(&rect2, &lpitem->rect);
+	    hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
+	    if ((lpitem->item_flags & MF_DISABLED) == MF_DISABLED)
+		OldTextColor = SetTextColor(hDC, 0x00C0C0C0L);
+	    else
+		OldTextColor = SetTextColor(hDC, 0x00000000L);
 	    DrawText(hDC, lpitem->item_text, -1, &rect2, 
 	    	DT_LEFT | DT_VCENTER | DT_SINGLELINE);
-	    }
-	if ((lpitem->item_flags & MF_CHECKED) == MF_CHECKED) {
-	    CopyRect(&rect3, &rect2);
-	    rect3.left = rect3.right - rect3.bottom + rect3.top;
-	    hMemDC = CreateCompatibleDC(hDC);
-	    SelectObject(hMemDC, hStdCheck);
-	    GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
-	    BitBlt(hDC, rect3.left, rect3.top,
-		bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
-	    DeleteDC(hMemDC);
+	    SetTextColor(hDC, OldTextColor);
+	    SelectObject(hDC, hOldFont);
 	    }
 	if (lpitem->next == NULL) goto EndOfPaint;
 	lpitem = (LPMENUITEM)lpitem->next;
     }
 EndOfPaint:
     SelectObject(hDC, hOldFont);
-    EndPaint( hwnd, &ps );
-}
+} 
 
 
 
-LPMENUITEM MenuFindItem(HWND hwnd, int x, int y, WORD *lpRet)
+LPMENUITEM MenuFindItem(LPPOPUPMENU lppop, int x, int y, WORD *lpRet)
 {
-    WND 	*wndPtr;
-    LPPOPUPMENU lppop;
     LPMENUITEM 	lpitem;
     UINT  	i;
-    lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
+    if (lpRet != NULL) *lpRet = 0;
     if (lppop == NULL) return NULL;
     if (lppop->nItems == 0) return NULL;
     lpitem = lppop->firstItem;
@@ -762,13 +798,10 @@
 }
 
 
-LPMENUITEM MenuFindItemBySelKey(HWND hwnd, WORD key, WORD *lpRet)
+LPMENUITEM MenuFindItemBySelKey(LPPOPUPMENU lppop, WORD key, WORD *lpRet)
 {
-    WND 	*wndPtr;
-    LPPOPUPMENU lppop;
     LPMENUITEM 	lpitem;
     UINT  	i;
-    lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
     if (lppop == NULL) return NULL;
     if (lppop->nItems == 0) return NULL;
     lpitem = lppop->firstItem;
@@ -808,6 +841,7 @@
     if (lppop->nItems == 0) return;
     hDC = GetDC(hwnd);
     lppop->Width = 20;
+    lppop->CheckWidth = lppop->PopWidth = 0;
     hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
 CalcAGAIN:
     OldWidth = lppop->Width;
@@ -819,6 +853,23 @@
 	printf("PopupMenuCalcSize item #%d !\n", i);
 #endif
 	rect.right = rect.left + lppop->Width;
+	if ((lpitem->item_flags & MF_CHECKED) == MF_CHECKED) {
+	    if (lpitem->hCheckBit != 0)
+		GetObject(lpitem->hCheckBit, sizeof(BITMAP), (LPSTR)&bm);
+	    else
+		GetObject(hStdCheck, sizeof(BITMAP), (LPSTR)&bm);
+	    lppop->CheckWidth = max(lppop->CheckWidth, bm.bmWidth);
+	    }
+	else {
+	    if (lpitem->hUnCheckBit != 0) {
+		GetObject(lpitem->hUnCheckBit, sizeof(BITMAP), (LPSTR)&bm);
+		lppop->CheckWidth = max(lppop->CheckWidth, bm.bmWidth);
+		}
+	    }
+	if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
+	    GetObject(hStdMnArrow, sizeof(BITMAP), (LPSTR)&bm);
+	    lppop->PopWidth = max(lppop->PopWidth, bm.bmWidth);
+	    }
 	if ((lpitem->item_flags & MF_SEPARATOR) == MF_SEPARATOR) {
 	    rect.bottom = rect.top + 3;
 	    }
@@ -838,6 +889,8 @@
 	    TempWidth = LOWORD(dwRet);
 	    if (GetShortCutPos(lpitem->item_text) != (WORD)-1)
 	        TempWidth += 15;
+	    TempWidth += lppop->CheckWidth;
+	    TempWidth += lppop->PopWidth;
 	    lppop->Width = max(lppop->Width, TempWidth);
 	    }
 	CopyRect(&lpitem->rect, &rect);
@@ -846,6 +899,7 @@
 	}
     if (OldWidth < lppop->Width) goto CalcAGAIN;
     lppop->Height = rect.bottom;
+    SetRect(&lppop->rect, 1, 1, lppop->Width, lppop->Height);
 #ifdef DEBUG_MENUCALC
     printf("PopupMenuCalcSize w=%d h=%d !\n", lppop->Width, lppop->Height);
 #endif
@@ -855,31 +909,33 @@
 
 
 
-void MenuBarCalcSize(HWND hwnd)
+void MenuBarCalcSize(HDC hDC, LPRECT lprect, LPPOPUPMENU lppop)
 {
-    WND 	*wndPtr;
-    LPPOPUPMENU lppop;
     LPMENUITEM 	lpitem;
-    HDC		hDC;
     RECT 	rect;
     HBITMAP	hBitMap;
     BITMAP	bm;
     HFONT	hOldFont;
     UINT  	i, OldHeight;
     DWORD	dwRet;
-    lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
     if (lppop == NULL) return;
     if (lppop->nItems == 0) return;
-    hDC = GetDC(hwnd);
+#ifdef DEBUG_MENUCALC
+    printf("MenuBarCalcSize left=%d top=%d right=%d bottom=%d !\n", 
+    	lprect->left, lprect->top, lprect->right, lprect->bottom);
+#endif
     hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
-    lppop->Height = 10;
+    lppop->Height = lprect->bottom - lprect->top;
 CalcAGAIN:
     OldHeight = lppop->Height;
-    SetRect(&rect, 1, 1, 0, OldHeight);
+    SetRect(&rect, lprect->left, lprect->top, 0, lprect->top + OldHeight);
     lpitem = lppop->firstItem;
     for(i = 0; i < lppop->nItems; i++) {
 	if (lpitem == NULL) break;
-	rect.bottom = rect.top + lppop->Height;
+	rect.bottom = lprect->top + lppop->Height;
+	if (rect.right > lprect->right) 
+	    SetRect(&rect, lprect->left, rect.bottom, 
+		0, rect.bottom + SYSMETRICS_CYMENU);
 	if ((lpitem->item_flags & MF_BITMAP) == MF_BITMAP) {
 	    hBitMap = (HBITMAP)LOWORD((LONG)lpitem->item_text);
 	    GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
@@ -900,12 +956,13 @@
 	}
     if (OldHeight < lppop->Height) goto CalcAGAIN;
     lppop->Width = rect.right;
+    lprect->bottom =  lprect->top + lppop->Height;
+    CopyRect(&lppop->rect, lprect);
 #ifdef DEBUG_MENUCALC
     printf("MenuBarCalcSize w=%d h=%d !\n", 
     	lppop->Width, lppop->Height);
 #endif
     SelectObject(hDC, hOldFont);
-    ReleaseDC(hwnd, hDC);
 }
 
 
@@ -956,13 +1013,13 @@
 	if (str[i] == '\t' && str[i + 1] != '\t') 
 	    {
 	    str2 = &str[i + 1];
-#ifdef DEBUG_MENU
+#ifdef DEBUG_MENUSHORTCUT
 	    printf("GetShortCutString // '%s' \n", str2);
 #endif
 	    return str2;
 	    }
 	}
-#ifdef DEBUG_MENU
+#ifdef DEBUG_MENUSHORTCUT
     printf("GetShortCutString NULL \n");
 #endif
     return NULL;
@@ -976,13 +1033,13 @@
     for (i = 0; i < strlen(str); i++) {
 	if (str[i] == '\t' && str[i + 1] != '\t') 
 	    {
-#ifdef DEBUG_MENU
+#ifdef DEBUG_MENUSHORTCUT
 	    printf("GetShortCutPos = %d \n", i);
 #endif
 	    return i;
 	    }
 	}
-#ifdef DEBUG_MENU
+#ifdef DEBUG_MENUSHORTCUT
     printf("GetShortCutString NULL \n");
 #endif
     return -1;
@@ -1007,6 +1064,7 @@
 		    ShowWindow(submenu->hWnd, SW_HIDE);
 		    someClosed = TRUE;
 		    }
+		GlobalUnlock((HMENU)lpitem->item_id);
 	    	}
     	    }
     	lpitem = (LPMENUITEM)lpitem->next;
@@ -1015,553 +1073,31 @@
 }
 
 
-#ifdef USE_XTMENU
+
 
 /**********************************************************************
- *					MENU_CheckWidget
+ *			ChangeMenu		[USER.153]
  */
-void
-MENU_CheckWidget(Widget w, Boolean check)
+BOOL ChangeMenu(HMENU hMenu, WORD nPos, LPSTR lpNewItem, 
+			WORD wItemID, WORD wFlags)
 {
-    if (checkBitmap == XtUnspecifiedPixmap)
-    {
-	Display *display = XtDisplayOfObject(w);
-	    
-	checkBitmap = XCreateBitmapFromData(display,
-					    DefaultRootWindow(display),
-					    check_bitmap_bits,
-					    check_bitmap_width,
-					    check_bitmap_height);
-	nocheckBitmap = XCreateBitmapFromData(display,
-					    DefaultRootWindow(display),
-					    nocheck_bitmap_bits,
-					    nocheck_bitmap_width,
-					    nocheck_bitmap_height);
-    }
-	    
-    if (check)
-	XtVaSetValues(w, XtNleftBitmap, checkBitmap, NULL);
-    else
-	XtVaSetValues(w, XtNleftBitmap, nocheckBitmap, NULL);
-}
-
-/**********************************************************************
- *					MENU_ParseMenu
- */
-WORD *
-MENU_ParseMenu(WORD *first_item, 
-	       int level,
-	       int limit,
-	       int (*action)(WORD *item, int level, void *app_data),
-	       void *app_data)
-{
-    WORD *item;
-    WORD *next_item;
-    int   i;
-
-    level++;
-    next_item = first_item;
-    i = 0;
-    do
-    {
-	i++;
-	item = next_item;
-	(*action)(item, level, app_data);
-	if (*item & MF_POPUP)
-	{
-	    MENU_POPUPITEM *popup_item = (MENU_POPUPITEM *) item;
-
-	    next_item = (WORD *) (popup_item->item_text + 
-				  strlen(popup_item->item_text) + 1);
-	    next_item = MENU_ParseMenu(next_item, level, 0, action, app_data);
+    if (wFlags & MF_APPEND) {
+    	return AppendMenu(hMenu, wFlags, wItemID, lpNewItem);
 	}
-	else
-	{
-	    MENU_NORMALITEM *normal_item = (MENU_NORMALITEM *) item;
-
-	    next_item = (WORD *) (normal_item->item_text + 
-				  strlen(normal_item->item_text) + 1);
+    if (wFlags & MF_DELETE) {
+    	return DeleteMenu(hMenu, wItemID, wFlags);
 	}
-    }
-    while (!(*item & MF_END) && i != limit);
-
-    return next_item;
-}
-
-/**********************************************************************
- *					MENU_FindMenuBar
- */
-LPMENUBAR
-MENU_FindMenuBar(MENUITEM *this_item)
-{
-    MENUITEM *root;
-    LPMENUBAR menu;
-
-    /*
-     * Find root item on menu bar.
-     */
-    for (root = this_item; root->parent != NULL; root = root->parent)
-	;
-    for ( ; root->prev != NULL; root = root->prev)
-	;
-
-    /*
-     * Find menu bar for the root item.
-     */
-    for (menu = firstMenu; 
-	 menu != NULL && menu->firstItem != root; 
-	 menu = menu->next)
-	;
-    
-    return menu;
-}
-
-/**********************************************************************
- *					MENU_SelectionCallback
- */
-static void
-MENU_SelectionCallback(Widget w, XtPointer client_data, XtPointer call_data)
-{
-    MENUITEM *this_item = (MENUITEM *) client_data;
-    LPMENUBAR menu;
-    WND	     *wndPtr;
-
-    if (this_item->menu_w != NULL || (this_item->item_flags & MF_DISABLED))
-	return;
-
-    /*
-     * Find menu bar for the root item.
-     */
-    menu = MENU_FindMenuBar(this_item);
-    if (menu != NULL)
-    {
-	wndPtr = WIN_FindWndPtr(menu->ownerWnd);
-	if (wndPtr == NULL)
-	    return;
-
-#ifdef DEBUG_MENU
-	printf("Selected '%s' (%d).\n", 
-	       this_item->item_text, this_item->item_id);
-#endif
-	
-	CallWindowProc(wndPtr->lpfnWndProc, menu->ownerWnd, WM_COMMAND,
-		       this_item->item_id, 0);
-    }
-}
-
-/**********************************************************************
- *					MENU_CreateItems
- */
-int
-MENU_CreateItems(WORD *item, int level, void *app_data)
-{
-    MENU_POPUPITEM     *popup_item;
-    MENU_NORMALITEM    *normal_item;
-    MENUITEM	       *this_item;
-    Arg			this_args[10];
-    int			n_args = 0;
-    LPMENUBAR           menu = (LPMENUBAR) app_data;
-
-    if (menu->nItems == 0)
-	this_item = menu->firstItem;
-    else
-	this_item = (MENUITEM *) GlobalQuickAlloc(sizeof(MENUITEM));
-
-    if (this_item == NULL)
-	return 0;
-
-    if (level > lastLevel)
-    {
-	parentItem  = siblingItem;
-	siblingItem = NULL;
-    }
-
-    while (level < lastLevel)
-    {
-	siblingItem = parentItem;
-	if (siblingItem != NULL)
-	    parentItem = siblingItem->parent;
-	else
-	    parentItem = NULL;
-
-	lastLevel--;
-    }
-    lastLevel = level;
-    
-    this_item->next = NULL;
-    this_item->prev = siblingItem;
-    this_item->child = NULL;
-    this_item->parent = parentItem;
-    
-    if (siblingItem !=  NULL)
-	siblingItem->next = this_item;
-    if (parentItem != NULL && parentItem->child == NULL)
-	parentItem->child = this_item;
-    
-    siblingItem = this_item;
-    
-    if (*item & MF_POPUP)
-    {
-	popup_item = (MENU_POPUPITEM *) item;
-	this_item->item_flags = popup_item->item_flags;
-	this_item->item_id    = -1;
-	this_item->item_text  = popup_item->item_text;
-
-#ifdef DEBUG_MENU
-	printf("%d: popup %s\n", level, this_item->item_text);
-#endif
-    }
-    else
-    {
-	normal_item = (MENU_NORMALITEM *) item;
-	this_item->item_flags = normal_item->item_flags;
-	this_item->item_id    = normal_item->item_id;
-	this_item->item_text  = normal_item->item_text;
-
-#ifdef DEBUG_MENU
-	printf("%d: normal %s (%04x)\n", level, this_item->item_text,
-	       this_item->item_flags);
-#endif
-    }
-
-    if (level == 1)
-    {
-	menu->nItems++;
-
-	if (this_item->prev != NULL)
-	{
-	    XtSetArg(this_args[n_args], XtNhorizDistance, 10); 
-	    n_args++;
-	    XtSetArg(this_args[n_args], XtNfromHoriz, this_item->prev->w); 
-	    n_args++;
+    if (wFlags & MF_INSERT) {
+    	return InsertMenu(hMenu, nPos, wFlags, wItemID, lpNewItem);
 	}
-
-	if (this_item->item_flags & MF_POPUP)
-	{
-	    sprintf(this_item->menu_name, "Menu%d", menuId++);
-	    XtSetArg(this_args[n_args], XtNmenuName, this_item->menu_name);
-	    n_args++;
-	    
-	    this_item->w = XtCreateManagedWidget(this_item->item_text,
-						 winMenuButtonWidgetClass, 
-						 menu->parentWidget,
-						 this_args, n_args);
-	    this_item->menu_w = XtCreatePopupShell(this_item->menu_name,
-						 simpleMenuWidgetClass, 
-						 this_item->w,
-						 NULL, 0);
+    if (wFlags & MF_CHANGE) {
+    	return ModifyMenu(hMenu, nPos, wFlags, wItemID, lpNewItem);
 	}
-	else
-	{
-	    this_item->w = XtCreateManagedWidget(this_item->item_text,
-						 winCommandWidgetClass, 
-						 menu->parentWidget,
-						 this_args, n_args);
-	    this_item->menu_w = NULL;
-	    XtAddCallback(this_item->w, XtNcallback, MENU_SelectionCallback,
-			  (XtPointer) this_item);
+    if (wFlags & MF_REMOVE) {
+    	return RemoveMenu(hMenu, wItemID, wFlags);
 	}
-
-	if (menu->firstItem == NULL)
-	    menu->firstItem = this_item;
-    }
-    else
-    {
-	if ((this_item->item_flags & MF_MENUBREAK) ||
-	    (strlen(this_item->item_text) == 0))
-	{
-	    XtSetArg(this_args[n_args], XtNheight, 10);
-	    n_args++;
-	    this_item->w = XtCreateManagedWidget("separator",
-						 smeLineObjectClass,
-						 this_item->parent->menu_w,
-						 this_args, n_args);
-	}
-	else
-	{
-	    XtSetArg(this_args[n_args], XtNmenuName, this_item->menu_name);
-	    n_args++;
-	    this_item->w = XtCreateManagedWidget(this_item->item_text,
-						 smeMenuButtonObjectClass,
-						 this_item->parent->menu_w,
-						 this_args, n_args);
-
-	    if (this_item->item_flags & MF_POPUP)
-	    {
-		sprintf(this_item->menu_name, "Menu%d", menuId++);
-		this_item->menu_w = XtCreatePopupShell(this_item->menu_name,
-						     simpleMenuWidgetClass, 
-						     this_item->parent->menu_w,
-						     NULL, 0);
-	    }
-	    else
-	    {
-		this_item->menu_w = NULL;
-		XtAddCallback(this_item->w, XtNcallback, 
-			      MENU_SelectionCallback, (XtPointer) this_item);
-	    }
-	}
-    }
-
-    if (this_item->w != NULL)
-    {
-	if (this_item->item_flags & MF_GRAYED)
-	    XtSetSensitive(this_item->w, False);
-	if (this_item->item_flags & MF_DISABLED)
-	    XtVaSetValues(this_item->w, XtNinactive, True, NULL);
-	if (this_item->item_flags & MF_CHECKED)
-	    MENU_CheckWidget(this_item->w, True);
-    }
-
-    return 1;
+    return FALSE;
 }
-
-/**********************************************************************
- *					MENU_UseMenu
- */
-LPMENUBAR
-MENU_UseMenu(Widget parent, HANDLE instance, HWND wnd, HMENU hmenu, int width)
-{
-    LPMENUBAR 		menubar;
-    MENUITEM           *menu;
-    MENU_HEADER        *menu_desc;
-
-    menu  = (MENUITEM *) GlobalLock(hmenu);
-    if (hmenu == 0 || menu == NULL)
-    {
-	return NULL;
-    }
-
-    menubar = MENU_FindMenuBar(menu);
-    if (menubar == NULL)
-    {
-	GlobalUnlock(hmenu);
-	return NULL;
-    }
-
-    menubar->nItems 	     = 0;
-    menubar->parentWidget    = parent;
-    menubar->ownerWnd	     = wnd;
-
-    menu_desc = (MENU_HEADER *) GlobalLock(menubar->menuDescription);
-    
-    parentItem  = NULL;
-    siblingItem = NULL;
-    lastLevel   = 0;
-    MENU_ParseMenu((WORD *) (menu_desc + 1), 0, 0, MENU_CreateItems, menubar);
-
-    menubar->menuBarWidget = menubar->firstItem->w;
-
-    menubar->next = firstMenu;
-    firstMenu = menubar;
-    
-    return menubar;
-}
-
-/**********************************************************************
- *					MENU_CreateMenuBar
- */
-LPMENUBAR
-MENU_CreateMenuBar(Widget parent, HANDLE instance, HWND wnd,
-		   char *menu_name, int width)
-{
-    LPMENUBAR 		menubar;
-    HMENU     		hmenu;
-    MENUITEM           *menu;
-    MENU_HEADER        *menu_desc;
-
-#ifdef DEBUG_MENU
-    printf("CreateMenuBar: instance %02x, menu '%s', width %d\n",
-	   instance, menu_name, width);
-#endif
-
-    hmenu = LoadMenu(instance, menu_name);
-    return MENU_UseMenu(parent, instance, wnd, hmenu, width);
-}
-
-/**********************************************************************
- *					MENU_FindItem
- */
-MENUITEM *
-MENU_FindItem(MENUITEM *menu, WORD item_id, WORD flags)
-{
-    MENUITEM *item;
-    WORD position;
-    
-    if (flags & MF_BYPOSITION)
-    {
-	item = menu;
-	for (position = 0; item != NULL && position != item_id; position++)
-	    item = item->next;
-	
-	if (position == item_id)
-	    return item;
-    }
-    else
-    {
-	for ( ; menu != NULL; menu = menu->next)
-	{
-	    if (menu->item_id == item_id && menu->child == NULL)
-		return menu;
-	    if (menu->child != NULL)
-	    {
-		item = MENU_FindItem(menu->child, item_id, flags);
-		if (item != NULL)
-		    return item;
-	    }
-	}
-    }
-
-    return NULL;
-}
-
-/**********************************************************************
- *					MENU_CollapseMenu
- */
-static void
-MENU_CollapseBranch(MENUITEM *item, Boolean first_flag)
-{
-    MENUITEM *next_item;
-    
-    for ( ; item != NULL; item = next_item)
-    {
-	next_item = item->next;
-	
-	if (item->child != NULL)
-	    MENU_CollapseBranch(item->child, False);
-	    
-	if (item->w != NULL)
-	    XtDestroyWidget(item->w);
-	if (item->menu_w != NULL)
-	    XtDestroyWidget(item->menu_w);
-	
-	if (first_flag)
-	{
-	    item->prev 	     = NULL;
-	    item->child      = NULL;
-	    item->next 	     = NULL;
-	    item->parent     = NULL;
-	    item->item_flags = 0;
-	    item->item_id    = 0;
-	    item->item_text  = NULL;
-	    item->w 	     = NULL;
-	    item->menu_w     = NULL;
-
-	    first_flag = False;
-	}
-	else
-	{
-	    GlobalFree((unsigned int) item);
-	}
-    }
-}
-
-void
-MENU_CollapseMenu(LPMENUBAR menubar)
-{
-    MENU_CollapseBranch(menubar->firstItem, True);
-    
-    menubar->nItems 	     = 0;
-    menubar->parentWidget    = NULL;
-    menubar->ownerWnd	     = 0;
-    menubar->menuBarWidget   = NULL;
-}
-
-
-/**********************************************************************
- *					CheckMenu
- */
-BOOL
-CheckMenu(HMENU hmenu, WORD item_id, WORD check_flags)
-{
-    MENUITEM *item;
-    Pixmap left_bitmap;
-
-    if ((item = (MENUITEM *) GlobalLock(hmenu)) == NULL)
-	return -1;
-    
-    item = MENU_FindItem(item, item_id, check_flags);
-    if (item == NULL)
-    {
-	GlobalUnlock(hmenu);
-	return -1;
-    }
-
-    XtVaGetValues(item->w, XtNleftBitmap, &left_bitmap, NULL);
-    MENU_CheckWidget(item->w, (check_flags & MF_CHECKED));
-
-    if (left_bitmap == XtUnspecifiedPixmap)
-	return MF_UNCHECKED;
-    else
-	return MF_CHECKED;
-}
-
-
-/**********************************************************************
- *					LoadMenu
- */
-HMENU
-LoadMenu(HINSTANCE instance, char *menu_name)
-{
-    HANDLE		hmenubar;
-    LPMENUBAR 		menu;
-    HANDLE		hmenu_desc;
-    HMENU     		hmenu;
-    MENU_HEADER        *menu_desc;
-
-#ifdef DEBUG_MENU
-    printf("LoadMenu: instance %02x, menu '%s'\n",
-	   instance, menu_name);
-#endif
-
-    if (menu_name == NULL || 
-	(hmenu_desc = RSC_LoadMenu(instance, menu_name)) == 0 ||
-	(menu_desc = (MENU_HEADER *) GlobalLock(hmenu_desc)) == NULL)
-    {
-	return 0;
-    }
-
-    hmenubar = GlobalAlloc(GMEM_MOVEABLE, sizeof(MENUBAR));
-    menu = (LPMENUBAR) GlobalLock(hmenubar);
-    if (menu == NULL)
-    {
-	GlobalFree(hmenu_desc);
-	GlobalFree(hmenubar);
-	return 0;
-    }
-
-    hmenu = GlobalAlloc(GMEM_MOVEABLE, sizeof(MENUITEM));
-    if (hmenu == 0)
-    {
-	GlobalFree(hmenu_desc);
-	GlobalFree(hmenubar);
-	return 0;
-    }
-
-    menu->menuDescription = hmenu_desc;
-    menu->nItems 	  = 0;
-    menu->parentWidget    = NULL;
-    menu->firstItem       = (MENUITEM *) GlobalLock(hmenu);
-    menu->ownerWnd	  = 0;
-    menu->menuBarWidget   = NULL;
-
-    menu->firstItem->next 	= NULL;
-    menu->firstItem->prev 	= NULL;
-    menu->firstItem->child 	= NULL;
-    menu->firstItem->parent 	= NULL;
-    menu->firstItem->item_flags	= 0;
-    menu->firstItem->item_id 	= 0;
-    menu->firstItem->item_text 	= NULL;
-    menu->firstItem->w 		= NULL;
-    menu->firstItem->menu_w 	= NULL;
-
-    menu->next = firstMenu;
-    firstMenu  = menu;
-    
-    return GlobalHandleFromPointer(menu->firstItem);
-}
-
-#endif
 
 
 /**********************************************************************
@@ -1586,10 +1122,12 @@
 		lpitem->item_flags |= MF_CHECKED;
 	    else
 		lpitem->item_flags &= ((WORD)-1 ^ MF_CHECKED);
+	    GlobalUnlock(hMenu);
 	    return(TRUE);
 	    }
     	lpitem = (LPMENUITEM)lpitem->next;
     	}
+    GlobalUnlock(hMenu);
     return FALSE;
 }
 
@@ -1616,10 +1154,12 @@
 		lpitem->item_flags |= MF_DISABLED;
 	    else
 		lpitem->item_flags &= ((WORD)-1 ^ MF_DISABLED);
+	    GlobalUnlock(hMenu);
 	    return(TRUE);
 	    }
     	lpitem = (LPMENUITEM)lpitem->next;
     	}
+    GlobalUnlock(hMenu);
     return FALSE;
 }
 
@@ -1635,8 +1175,12 @@
     LPMENUITEM 	lpitem, lpitem2;
     int		i;
 #ifdef DEBUG_MENU
-    printf("InsertMenu (%04X, %04X, %04X, %04X, %08X) !\n",
-	hMenu, nPos, wFlags, wItemID, lpNewItem);
+    if (wFlags & MF_STRING) 
+	printf("InsertMenu (%04X, %04X, %04X, '%s') !\n",
+		hMenu, wFlags, wItemID, lpNewItem);
+    else
+	printf("InsertMenu (%04X, %04X, %04X, %04X, %08X) !\n",
+		hMenu, nPos, wFlags, wItemID, lpNewItem);
 #endif
     menu = (LPPOPUPMENU) GlobalLock(hMenu);
     if (menu == NULL) return FALSE;
@@ -1649,10 +1193,14 @@
     	}
     printf("InsertMenu // after loop items !\n");
     hNewItem = GlobalAlloc(GMEM_MOVEABLE, sizeof(MENUITEM));
-    if (hNewItem == 0) return FALSE;
+    if (hNewItem == 0) {
+	GlobalUnlock(hMenu);
+    	return FALSE;
+    	}
     lpitem2 = (LPMENUITEM)GlobalLock(hNewItem);
     if (lpitem2 == NULL) {
 	GlobalFree(hNewItem);
+	GlobalUnlock(hMenu);
 	return FALSE;
 	}
     lpitem2->item_flags = wFlags;
@@ -1673,9 +1221,8 @@
     lpitem->next = lpitem2;
     lpitem2->child = NULL;
     lpitem2->parent = NULL;
-    lpitem2->w = NULL;
-    lpitem2->menu_w = NULL;
     menu->nItems++;
+    GlobalUnlock(hMenu);
     return TRUE;
 }
 
@@ -1690,8 +1237,12 @@
     HANDLE	hNewItem;
     LPMENUITEM 	lpitem, lpitem2;
 #ifdef DEBUG_MENU
-    printf("AppendMenu (%04X, %04X, %04X, %08X) !\n",
-	hMenu, wFlags, wItemID, lpNewItem);
+    if ((wFlags & (MF_BITMAP | MF_SEPARATOR | MF_MENUBREAK | MF_OWNERDRAW)) == 0)
+	printf("AppendMenu (%04X, %04X, %04X, '%s') !\n",
+		hMenu, wFlags, wItemID, lpNewItem);
+    else
+	printf("AppendMenu (%04X, %04X, %04X, %08X) !\n",
+		hMenu, wFlags, wItemID, lpNewItem);
 #endif
     menu = (LPPOPUPMENU) GlobalLock(hMenu);
     if (menu == NULL) return FALSE;
@@ -1702,10 +1253,14 @@
 	    }
 	}
     hNewItem = GlobalAlloc(GMEM_MOVEABLE, sizeof(MENUITEM));
-    if (hNewItem == 0) return FALSE;
+    if (hNewItem == 0) {
+	GlobalUnlock(hMenu);
+	return FALSE;
+	}
     lpitem2 = (LPMENUITEM)GlobalLock(hNewItem);
     if (lpitem2 == NULL) {
 	GlobalFree(hNewItem);
+	GlobalUnlock(hMenu);
 	return FALSE;
 	}
     lpitem2->item_flags = wFlags;
@@ -1727,11 +1282,10 @@
     lpitem2->next = NULL;
     lpitem2->child = NULL;
     lpitem2->parent = NULL;
-    lpitem2->w = NULL;
-    lpitem2->menu_w = NULL;
     lpitem2->hCheckBit = (HBITMAP)NULL;
     lpitem2->hUnCheckBit = (HBITMAP)NULL;
     menu->nItems++;
+    GlobalUnlock(hMenu);
     return TRUE;
 }
 
@@ -1757,12 +1311,14 @@
     	    lpitem->prev->next = lpitem->next;
     	    lpitem->next->prev = lpitem->prev;
     	    GlobalFree(HIWORD(lpitem));
+	    GlobalUnlock(hMenu);
     	    return(TRUE);
 	    }
     	lpitem = (LPMENUITEM)lpitem->next;
 	printf("RemoveMenu // during loop items !\n");
     	}
     printf("RemoveMenu // after loop items !\n");
+    GlobalUnlock(hMenu);
     return FALSE;
 }
 
@@ -1801,10 +1357,12 @@
     	    lpitem->item_flags = wFlags;
     	    lpitem->item_id    = wItemID;
     	    lpitem->item_text  = lpNewItem;
+	    GlobalUnlock(hMenu);
     	    return(TRUE);
 	    }
     	lpitem = (LPMENUITEM)lpitem->next;
     	}
+    GlobalUnlock(hMenu);
     return FALSE;
 }
 
@@ -1836,6 +1394,7 @@
     menu->SysFlag	  = FALSE;
     menu->Width = 100;
     menu->Height = 0;
+    GlobalUnlock(hMenu);
     return hMenu;
 }
 
@@ -1848,6 +1407,7 @@
 {
     WND		*wndPtr;
     LPPOPUPMENU	lppop;
+    RECT	rect;
 #ifdef DEBUG_MENU
     printf("TrackPopupMenu (%04X, %04X, %d, %d, %04X, %04X, %08X) !\n",
 	hMenu, wFlags, x, y, nReserved, hWnd, lpRect);
@@ -1873,15 +1433,7 @@
 	SetWindowPos(lppop->hWnd, 0, x, y, lppop->Width + 2, lppop->Height, 
 		SWP_NOZORDER);
 	}
-    else {
-	MenuBarCalcSize(lppop->hWnd);
-#ifdef DEBUG_MENU
-	printf("TrackMenuBar // x=%d y=%d Width=%d Height=%d\n", 
-		x, y, lppop->Width, lppop->Height); 
-#endif
-	SetWindowPos(lppop->hWnd, 0, 0, -16, lppop->Width, lppop->Height, 
-		SWP_NOZORDER);
-	}
+    GlobalUnlock(hMenu);
     return TRUE;
 }
 
@@ -1889,7 +1441,7 @@
 /**********************************************************************
  *			NC_TrackSysMenu		[Internal]
  */
-void NC_TrackSysMenu(hWnd)
+void NC_TrackSysMenu(HWND hWnd)
 {
     RECT	rect;
     LPPOPUPMENU	lpsys;
@@ -1920,6 +1472,20 @@
     else {
 	ShowWindow(lpsys->hWnd, SW_HIDE);
 	}
+    GlobalUnlock(wndPtr->hSysMenu);
+}
+
+
+/**********************************************************************
+ *			GetMenuCheckMarkDimensions	[USER.417]
+ */
+DWORD GetMenuCheckMarkDimensions()
+{
+    BITMAP	bm;
+    if (hStdCheck == (HBITMAP)NULL) 
+	hStdCheck = LoadBitmap((HANDLE)NULL, (LPSTR)OBM_CHECK);
+    GetObject(hStdCheck, sizeof(BITMAP), (LPSTR)&bm);
+    return MAKELONG(bm.bmWidth, bm.bmHeight);
 }
 
 
@@ -1945,10 +1511,12 @@
     	if (i == nPos) {
     	    lpitem->hCheckBit   = hNewCheck;
      	    lpitem->hUnCheckBit = hNewUnCheck;
-    	    return(TRUE);
+	    GlobalUnlock(hMenu);
+    	    return TRUE;
 	    }
     	lpitem = (LPMENUITEM)lpitem->next;
     	}
+    GlobalUnlock(hMenu);
     return FALSE;
 }
 
@@ -1980,6 +1548,7 @@
     menu->SysFlag	  = FALSE;
     menu->Width = 100;
     menu->Height = 0;
+    GlobalUnlock(hMenu);
     return hMenu;
 }
 
@@ -2008,6 +1577,7 @@
 	    }
     	lpitem = (LPMENUITEM)lpitem->next;
     	}
+    GlobalUnlock(hMenu);
     GlobalFree(hMenu);
 #ifdef DEBUG_MENU
     printf("DestroyMenu (%04X) // End !\n", hMenu);
@@ -2016,9 +1586,6 @@
 }
 
 
-#ifdef USE_POPUPMENU
-
-
 /**********************************************************************
  *			LoadMenu		[USER.150]
  */
@@ -2027,7 +1594,6 @@
     HMENU     		hMenu;
     HANDLE		hMenu_desc;
     MENU_HEADER 	*menu_desc;
-
 #ifdef DEBUG_MENU
     if ((LONG)menu_name & 0xFFFF0000L)
 	printf("LoadMenu: instance %02x, menu '%s'\n", instance, menu_name);
@@ -2037,12 +1603,16 @@
     if (instance == (HANDLE)NULL)  instance = hSysRes;
     if (menu_name == NULL || 
 	(hMenu_desc = RSC_LoadMenu(instance, menu_name)) == 0 ||
-	(menu_desc = (MENU_HEADER *) GlobalLock(hMenu_desc)) == NULL)
-    {
+	(menu_desc = (MENU_HEADER *) GlobalLock(hMenu_desc)) == NULL) {
 	return 0;
-    }
+        }
+    hMenu = LoadMenuIndirect((LPSTR)menu_desc);
+/*
     hMenu = CreateMenu();
     ParseMenuResource((WORD *) (menu_desc + 1), 0, hMenu);
+    GlobalUnlock(hMenu_desc);
+    GlobalFree(hMenu_desc);
+*/
     return hMenu;
 }
 
@@ -2080,9 +1650,18 @@
  */
 BOOL SetMenu(HWND hWnd, HMENU hMenu)
 {
+    LPPOPUPMENU lppop;
     WND * wndPtr = WIN_FindWndPtr(hWnd);
     if (wndPtr == NULL) return FALSE;
+#ifdef DEBUG_MENU
+    printf("SetMenu(%04X, %04X);\n", hWnd, hMenu);
+#endif
     wndPtr->wIDmenu = hMenu;
+    if (hMenu == 0) return TRUE;
+    lppop = (LPPOPUPMENU) GlobalLock(hMenu);
+    if (lppop == NULL) return FALSE;
+    lppop->ownerWnd = hWnd;
+    GlobalUnlock(hMenu);
     return TRUE;
 }
 
@@ -2127,14 +1706,33 @@
     printf("DrawMenuBar (%04X)\n", hWnd);
 #endif
     wndPtr = WIN_FindWndPtr(hWnd);
-    if (wndPtr != NULL && wndPtr->hWndMenuBar != 0) {
-	InvalidateRect(wndPtr->hWndMenuBar, NULL, TRUE);
-	UpdateWindow(wndPtr->hWndMenuBar);
+    if (wndPtr != NULL && wndPtr->wIDmenu != 0) {
+#ifdef DEBUG_MENU
+	printf("DrawMenuBar wIDmenu=%04X \n", wndPtr->wIDmenu);
+#endif
+	SendMessage(hWnd, WM_NCPAINT, 1, 0L);
 	}
 }
 
 
 /**********************************************************************
+ *			LoadMenuIndirect	[USER.220]
+ */
+HMENU LoadMenuIndirect(LPSTR menu_template)
+{
+    HMENU     		hMenu;
+    MENU_HEADER 	*menu_desc;
+#ifdef DEBUG_MENU
+    printf("LoadMenuIndirect: menu_template '%08X'\n", menu_template);
+#endif
+    hMenu = CreateMenu();
+    menu_desc = (MENU_HEADER *)menu_template;
+    ParseMenuResource((WORD *)(menu_desc + 1), 0, hMenu); 
+    return hMenu;
+}
+
+
+/**********************************************************************
  *			CopySysMenu (Internal)
  */
 HMENU CopySysMenu()
@@ -2175,12 +1773,15 @@
 	}
     GlobalUnlock(hMenu);
     GlobalUnlock(hSysMenu);
+#ifdef DEBUG_MENU
+    printf("CopySysMenu hMenu=%04X !\n", hMenu);
+#endif
     return hMenu;
 }
 
 
 /**********************************************************************
- *			ParseMenuResource (for Xlib version)
+ *			ParseMenuResource (from Resource or Template)
  */
 WORD * ParseMenuResource(WORD *first_item, int level, HMENU hMenu)
 {
@@ -2205,7 +1806,7 @@
 	    	hSubMenu, popup_item->item_text);
 	    }
 	else {
-	    MENU_NORMALITEM *normal_item = (MENU_NORMALITEM *) item;
+	    MENUITEMTEMPLATE *normal_item = (MENUITEMTEMPLATE *) item;
 	    next_item = (WORD *) (normal_item->item_text + 
 				  strlen(normal_item->item_text) + 1);
 	    AppendMenu(hMenu, normal_item->item_flags, 
@@ -2217,5 +1818,3 @@
 }
 
 
-#endif
-