$ filename "ZeeGrid_Demo1.exe"


'uses rtl32
'uses rtl64

uses corewin
uses zeegrid

#autodim off

declare sub FormatGrid(sys hg)

'=======================
'MAIN CODE
'=======================

dim nCmdline as asciiz ptr, hInstance as sys
&nCmdline = GetCommandLine
hInstance = GetModuleHandle(0)

sys hgridmod
sys hgrid

string cr=chr(13,10)
'========================================'


function WinMain() as sys
    
    WNDCLASSEX wc
    MSG Msg
    
    sys hwnd    

    wc.cbSize         = sizeof(WNDCLASSEX)
    wc.style          = 0
    wc.lpfnWndProc    = @WndProc
    wc.cbClsExtra     = 0
    wc.cbWndExtra     = 0
    wc.hInstance      = hInstance
    wc.hIcon          = LoadIcon(null, IDI_APPLICATION)
    wc.hCursor        = LoadCursor(null, IDC_ARROW)
    wc.hbrBackground  = GetStockObject WHITE_BRUSH
    wc.lpszMenuName   = null
    wc.lpszClassName  = strptr "WindowClass" 
    wc.hIconSm        = LoadIcon(null, IDI_APPLICATION)

    if not RegisterClassEx(&wc) then
        MessageBox(null, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION or MB_OK)
        return 0
    end if

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        wc.lpszClassName,
        "ZeeGrid Demo",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
        null, null, hInstance, null)

    if hwnd = null then
        MessageBox(null, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION or MB_OK)
        return 0
    end if

    ShowWindow(hwnd, SW_SHOWNORMAL)
    UpdateWindow(hwnd)
    
    sys bRet
    
    do while (bRet := GetMessage(&Msg, null, 0, 0)) != 0
      if bRet = -1 then
        'show an error message
        print "Error in Message Loop"
        end
      else 
        TranslateMessage(&Msg)
        DispatchMessage(&Msg)
      end if
    wend
    
    return Msg.wParam
end function


function WndProc(sys hwnd, uMsg, wParam, lParam) as sys callback

    select uMsg

        case WM_CREATE
            hgridmod=LoadLibrary("zeegrid.dll")
            if not hgridmod then
               MessageBox(hWnd,"Unable to load ZeeGrid.DLL","Error",MB_OK)
               PostQuitMessage(0)
            end if
            'if you’re here, then the DLL loaded successfully
            hgrid=CreateWindowEx(0,"ZeeGrid","My First ZeeGrid",
                                 WS_CHILD|WS_VISIBLE|WS_BORDER,
                                 0,0,0,0,
                                 hWnd, 0,hInstance,null)
            SendMessage(hgrid,ZGM_DIMGRID,10,0)  'set the grid to have 10 columns instead of the default 5
            FormatGrid(hgrid)
             
        case WM_SIZE
            RECT rect
            GetClientRect(hWnd,&rect)
            MoveWindow(hgrid,0,0,rect.right, rect.bottom\2,TRUE)  'Note: \ instead of /

        case WM_CLOSE
            DestroyWindow(hwnd)
        
        case WM_DESTROY
            PostQuitMessage(0)
        
        case else
            return DefWindowProc(hwnd, uMsg, wParam, lParam)
            
    end select
    
    return 0
end function


'WINDOWS START
'=============
'
WinMain()

=================================================

sub FormatGrid(sys hg)
    SendMessage(hg,ZGM_SHOWROWNUMBERS,TRUE,0)
    'create 5 rows of empty cells
    int j
    for j=1 to 5
        SendMessage(hg,ZGM_APPENDROW,0,0)
    next
    
    'set column header titles
    SendMessage(hg,ZGM_SETCELLTEXT,1, "First" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,2, "Second" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,3, "Third" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,4, "Fourth" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,5, "Fifth" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,6, "Sixth" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,7, "Seventh Column is wide")
    SendMessage(hg,ZGM_SETCELLTEXT,8, "Eighth" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,9, "Ninth" cr "Column")
    SendMessage(hg,ZGM_SETCELLTEXT,10,"Tenth" cr "and" cr "last" cr "Column")

    'make column 3 editable by the user
    SendMessage(hg,ZGM_SETCOLEDIT,3,1)

    'auto size all columns
    SendMessage(hg,ZGM_AUTOSIZE_ALL_COLUMNS,0,0)
end sub

