  '
  ' MenuDemo with right click PopupMenu for oxygen by frankolinox 4.2.2014 :)
  '
  $ filename "t.exe"
  includepath "$/inc/"
  'include "RTL64.inc"
  include "minwin.inc"

  %TPM_LEFTBUTTON      0x0000
  %TPM_RIGHTBUTTON     0x0002
  %TPM_LEFTALIGN       0x0000

DECLARE FUNCTION ScreenToClient LIB "USER32.DLL" ALIAS "ScreenToClient" ( _
   BYVAL hWnd AS SYS _                                
 , BYREF lpPoint AS POINT _                             
 ) AS LONG                                              

DECLARE FUNCTION GetCursorPos LIB "USER32.DLL" ALIAS "GetCursorPos" ( _
   BYREF lpPoint AS POINT _                             
 ) AS LONG                                              

'DECLARE FUNCTION ClientToScreen LIB "USER32.DLL" ALIAS "ClientToScreen" ( _
'   BYVAL hWnd AS SYS _                                ' __in HWND hWnd
' , BYREF lpPoint AS POINT _                             ' __inout LPPOINT lpPoint
' ) AS LONG                                              ' BOOL

DECLARE FUNCTION TrackPopupMenu LIB "USER32.DLL" ALIAS "TrackPopupMenu" ( _
   BYVAL hMenu AS SYS _                               ' __in HMENU hMenu
 , BYVAL uFlags AS SYS _                              ' __in UINT uFlags
 , BYVAL x AS LONG _                                    ' __in int x
 , BYVAL y AS LONG _                                    ' __in int y
 , BYVAL nReserved AS LONG _                            ' __in int nReserved
 , BYVAL hWnd AS SYS _                                ' __in HWND hWnd
 , OPTIONAL BYREF prcRect AS RECT _                     ' __in_opt CONST RECT *prcRect
 ) AS LONG                                              ' BOOL


DECLARE FUNCTION GetSubMenu LIB "USER32.DLL" ALIAS "GetSubMenu" ( _
   BYVAL hMenu AS SYS _                               ' __in HMENU hMenu
 , BYVAL nPos AS LONG _                                 ' __in int nPos
 ) AS SYS                                             ' HMENU


  function WndProc( sys hWnd, wMsg, wParam, lparam ) as sys, callback
  '==================================================================

  '
  static sys         hdc,hMenu,hMenuPopup,hMenuPopup1,hCursorMenu
  static String      txt,szAppName
  static rect        crect
  static POINT       pt  
  static PaintStruct Paintst
  sys id,hInst
  static sys hGraphic
  '
  select wMsg
  '==========
      
  case WM_CREATE
  '
  szAppName="Menu Demo_rightClick popup"
  '
  SetWindowText hwnd,szAppName
  '
  hMenu = CreateMenu  
  hMenuPopup = CreateMenu  
  hMenuPopup1= CreateMenu  
  AppendMenu hMenuPopup1,  MF_STRING,    4011,        "Red"
  AppendMenu hMenuPopup1,  MF_STRING,    4012,        "Green"
  AppendMenu hMenuPopup1,  MF_STRING,    4013,        "Blue"
  AppendMenu hMenuPopup ,MF_POPUP,       hMenuPopup1, "&Colors"
  '
  hMenuPopup1= CreateMenu  
  AppendMenu hMenuPopup1,  MF_STRING,    4021,        "Sphere"
  AppendMenu hMenuPopup1,  MF_STRING,    4022,        "Cube"
  AppendMenu hMenuPopup1,  MF_STRING,    4023,        "Cone"
  AppendMenu hMenuPopup1,  MF_STRING,    4024,        "Cylinder"
  AppendMenu hMenuPopup ,MF_POPUP,       hMenuPopup1, "&Shapes"

  AppendMenu hMenuPopup ,  MF_SEPARATOR, 0,            null
  AppendMenu hMenuPopup ,  MF_STRING,    4005,        "E&xit"
  '
  AppendMenu   hMenu,     MF_POPUP,     hMenuPopup,  "&Objects"
  '
  CheckMenuItem  hMenu,4022, MF_CHECKED
  EnableMenuItem hMenu,4024, MF_DISABLED or MF_GRAYED
  '
  hCursorMenu = GetSubMenu(hMenu, 0)
      
  case WM_COMMAND

  if wparam = 4005 then 'Exit        
    SendMessage hwnd, WM_CLOSE, 0, 0
  end if
  
  case %WM_LBUTTONDOWN      
    GetCursorPos pt              
    ScreenToClient (hWnd, pt)  
    print "here lButton: " + str(pt.x) + "," + str(pt.y)

  case %WM_RBUTTONDOWN
    GetCursorPos pt              
    TrackPopupMenu(hCursorMenu, %TPM_LEFTALIGN OR %TPM_LEFTBUTTON OR %TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL) 'BYVAL %NULL         

  case WM_DESTROY
  '
  PostQuitMessage 0

  case WM_KEYDOWN

    Select wParam
    Case 27 : SendMessage hwnd, WM_CLOSE, 0, 0      'ESCAPE
    End Select
      
  case else
  '
  function=DefWindowProc hWnd,wMsg,wParam,lParam
  '
  end select
  end function


  Function MainWindows(sys width,height,style)
  ===========================================
  '
  sys      a,b,c
  sys      inst,hWnd
  WNDCLASS wc
  MSG      wm
  '
  inst=GetModuleHandle 0
  '
  with wc                 '
    .style=CS_HREDRAW or CS_VREDRAW
    .lpfnWndProc=@wndproc
    .cbClsExtra=0
    .cbWndExtra=0    
    .hInstance=inst
    .hIcon=LoadIcon 0, IDI_APPLICATION
    .hCursor=LoadCursor 0,IDC_ARROW
    .hbrBackground=GetStockObject WHITE_BRUSH '
    .lpszMenuName=0
    .lpszClassName=@"Wins"
  end with

  if not RegisterClass @wc
    MessageBox 0,"Registration failed","Problem",MB_ICONERROR
    exit function
  end if 

  'Create the main window. 
  hwnd = CreateWindowEx( 
    0,                      'no extended styles           
    "wins",                 'class name                   
    "Main Window",          'window name  
    style,                  '                
    CW_USEDEFAULT,          'default horizontal position  
    CW_USEDEFAULT,          'default vertical position    
    width,                  'default width                
    height,                 'default height               
    null,                   'no parent or owner window    
    null,                   'class menu used              
    inst,                   'instance handle              
    null);                  'no window creation data      
  if not hWnd then
    MessageBox 0,"Unable to create window","problem",MB_ICONERROR
    exit function
  end if
  '
    sys hMenu,hMenuPopup,hSysMenu
  ''hSysMenu = GetSystemMenu(hWndFrame, FALSE)  
  
  ShowWindow hWnd,SW_NORMAL
  UpdateWindow hWnd
  ' 
  'MESSAGE LOOP
  '============
  '
  sys bRet
  '
  while bRet := GetMessage @wm, 0, 0, 0
    if bRet == -1 then
      'show an error message?
    else
      #ifdef EscapeKeyEnd
      if wm.message=WM_KEYDOWN
        if wm.wparam=27
          SendMessage hwnd,WM_DESTROY,0,0
        end if  
      end if
      #endif
      #ifdef RelayMessages
      m=wm.message
      select m
      '
      case WM_LBUTTONDBLCLK
      =====================
        '
        SendMessage hwnd,WM_COMMAND,1100-0x200+m, 0
        '
      case WM_KEYDOWN
      ===============
        '
        m=wm.wparam
        select m
        case VK_RETURN
          for i=1 to nchw
            if wm.hwnd=hchw(i)
              SendMessage hwnd,WM_COMMAND,2000+VK_RETURN,0
              if i>1 then continue while 'first window is editor
            end if
          next
        case VK_F1 to VK_F10
          SendMessage hwnd,WM_COMMAND,1001+m-VK_F1, 0
        case "A" to "Z"
          if CtrlKey
            SendMessage hwnd,WM_COMMAND,2000+m, 0
            if m=65 or m=70 or m=71 then continue while
          end if
        case VK_ESCAPE
          SendMessage hwnd,WM_COMMAND,2200+VK_ESCAPE, 0
          Continue do
        end select
        '
      end select
      '
      #endif
      TranslateMessage @wm
      DispatchMessage @wm
    end if
  wend
  '
  end function ; end of WinMain

  MainWindows 420,300,WS_OVERLAPPEDWINDOW
