'uses rtl32
uses corewin

sys hEdit

%Edit                    101
%GWLP_WNDPROC             -4
%DEFAULT_GUI_FONT         17
%WM_GETDLGCODE        0x0087
%DLGC_WANTARROWS      0x0001 // Direction keys.
%DLGC_WANTTAB         0x0002 // TAB key.
%DLGC_WANTALLKEYS     0x0004 // All keyboard input.
%DLGC_WANTMESSAGE     0x0004 // All keyboard input (the application passes this message in the MSG structure to the control).
%DLGC_HASSETSEL       0x0008 // EM_SETSEL messages.
%DLGC_DEFPUSHBUTTON   0x0010 // Default push button.
%DLGC_UNDEFPUSHBUTTON 0x0020 // Non-default push button.
%DLGC_RADIOBUTTON     0x0040 // Radio button.
%DLGC_WANTCHARS       0x0080 // WM_CHAR messages.
%DLGC_STATIC          0x0100 // Static control.
%DLGC_BUTTON          0x2000 // Button.
'____________________________________________________________________________

function WinMain() as sys
 WndClass WindowClass
 MSG      WindowMessage
 long     WindowWidth, WindowHeight, WindowPosX, WindowPosY
 sys      hWnd
 sys      inst = GetModuleHandle(0)

 WindowClass.style         = CS_HREDRAW | CS_VREDRAW
 WindowClass.lpfnWndProc   = &WndProc
 WindowClass.cbClsExtra    = 0
 WindowClass.cbWndExtra    = 0
 WindowClass.hInstance     = GetModuleHandle(0)
 WindowClass.hIcon         = LoadIcon(0, IDI_APPLICATION)
 WindowClass.hCursor       = LoadCursor(0,IDC_ARROW)
 WindowClass.hbrBackground = 16
 WindowClass.lpszMenuName  = 0
 WindowClass.lpszClassName = @"OXYGEN BASIC"
 RegisterClass (WindowClass)

 WindowWidth  = 320
 WindowHeight = 100
 WindowPosX   = (GetSystemMetrics(SM_CXSCREEN) - WindowWidth)  / 2
 WindowPosY   = (GetSystemMetrics(SM_CYSCREEN) - WindowHeight) / 2
 hWnd = CreateWindowEx(WS_EX_STATICEDGE, WindowClass.lpszClassName,
                       "WM_GETDLGCODE demo", WS_OVERLAPPEDWINDOW,
                       WindowPosX, WindowPosY, WindowWidth, WindowHeight, 0, 0, inst, 0)

 hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "Try [tab] [arrows] [enter]",
                        WS_CHILD OR WS_VISIBLE OR WS_TABSTOP,
                        20, 30, 270, 20,
                        hWnd, Edit, inst, 0)
 SendMessage(hEdit, WM_SETFONT, GetStockObject(DEFAULT_GUI_FONT), TRUE)
 SetWindowSubclass(hEdit, &EditSubclassProc, Edit, 0)
 SetFocus(hEdit)

 ShowWindow(hWnd, SW_SHOW)
 UpdateWindow(hWnd)

 while GetMessage(&WindowMessage, 0, 0, 0) > 0
   if IsDialogMessage(hWnd, &WindowMessage) = 0
     'if IsDialogMessage() return zero the message has not been processed
     'so use TranslateMessage/DispatchMessage
     TranslateMessage(&WindowMessage)
     DispatchMessage(&WindowMessage)
   end if
 wend

end function
'____________________________________________________________________________

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

 select wMsg

   case WM_DESTROY
     RemoveWindowSubclass(hEdit, &EditSubclassProc, Edit) 'unsubclass
     PostQuitMessage(0)

 end select

 function = DefWindowProc(hWnd, wMsg, wParam, lParam)

end function
'____________________________________________________________________________

function EditSubclassProc(sys hEdit, uint wMsg, sys wParam, sys lparam, uIdSubclass, dwRefData) as sys callback

 select case wMsg

   case WM_GETDLGCODE
     'DLGC_WANTARROWS      0x0001 Direction keys.
     'DLGC_WANTTAB         0x0002 TAB key.
     'DLGC_WANTALLKEYS     0x0004 All keyboard input.
     'DLGC_WANTMESSAGE     0x0004 All keyboard input (the application passes this message in the MSG structure to the control).
     'DLGC_HASSETSEL       0x0008 EM_SETSEL messages.
     'DLGC_DEFPUSHBUTTON   0x0010 Default push button.
     'DLGC_UNDEFPUSHBUTTON 0x0020 Non-default push button.
     'DLGC_RADIOBUTTON     0x0040 Radio button.
     'DLGC_WANTCHARS       0x0080 WM_CHAR messages.
     'DLGC_STATIC          0x0100 Static control.
     'DLGC_BUTTON          0x2000 Button.
     return DLGC_WANTALLKEYS

   case WM_CHAR
     SetWindowText(GetParent(hEdit), "WM_CHAR " & str(wParam))
     select case wParam

       case 1 'control-a
         SendMessage(hEdit, %EM_SETSEL, 0, -1) 'select everything
         return(0) 'do not forward Control-A

       case 48 TO 57, 45, 46  '0 to 9, minus, dot
         return(0)            'do not forward numbers, minus, dot

       case 65 TO 90          'A to Z
         'return(0)           'do not forward uppercase A to Z

       case 97 TO 122         'a to z
         'return(0)           'do not forward lowercase a to z

       case 9                 'TAB
         SetWindowText(hEdit, "tab key pressed")
         return(0)            'do not forward TAB

       case 13                'ENTER
         SetWindowText(hEdit, "enter key pressed")
         return(0)            'do not forward ENTER

       case 27                'ESC
         SendMessage(GetParent(hEdit), WM_CLOSE, 0, 0)
         return(0)

     end select

   case WM_KEYDOWN, WM_KEYUP
     SetWindowText(GetParent(hEdit), "WM_KEY " & str(wParam))
     select case wParam

       case 37 'left arrow
         SendMessage(hEdit, %EM_SETSEL, 0, 1) 'select first char
         return(0) 'rem to accept left arrow key, use up and down

       case 39 'right arrow
         SendMessage(hEdit, %EM_SETSEL, 1, 2) 'select second char
         return(0) 'rem to accept right arrow key, use up and down

     end select

 end select

 function = DefSubclassProc(hEdit, wMsg, wParam, lParam) 'forward message to edit control

end function
'
WinMain()