' fbc.exe -x BabyGrid_Demo.exe BabyGrid_fb.bas -s gui -v BabyGrid_fb.rc

#Include "windows.bi"

#include "babygrid.bi"                           '<------- from the babygrid.h header file     choose only 1
'#include "babygrid2.bi"                           '<------- from the babygrid.h header file     choose only 1

#include "resource.bi"									 '  for RC file resource

'#Define COMPIL_RC babygrid_fb.rc                '  to compile with this RC File > CSED_FB option only


'constants
#define GRID_DATA 2000   		' identifier
#define GRID_PROP 1000   		' identifier
#define MAX_LOADSTRING 100    ' max zstring length

' Global Variables:
dim shared               AS HINSTANCE hInst
dim shared 					 AS zstring *MAX_LOADSTRING szTitle, szWindowClass
dim shared               AS _BGCELL cell         ' <----------- You'll need to define at least one of these

Dim Shared As Hwnd hgrid1
Dim Shared As Hwnd hgrid2                        ' <------------ Window handles of the grids you'll create

' Begins declarations of functions included in this code module
Declare function MyRegisterClass(h1 as HINSTANCE) as ATOM

Declare Function InitInstance(hInstance1 as HINSTANCE, nCmdShow as integer) as integer

Declare Function WndProc(ByVal hwnd As HWND, ByVal message As UInteger, _
			ByVal wParam As WPARAM, ByVal lParam As LPARAM) As Integer
		
Declare Function About(hDlg as HWND, message as Uinteger, wParam as WPARAM, lParam as LPARAM) as integer

Declare sub PutCell(hgrid as HWND, row as integer, col as integer, text as string)

Declare sub LoadGrid1(hgrid as HWND)

Declare sub LoadGrid2(hgrid as HWND)

Declare Function WinMain(ByVal hI1 As hInstance, _
			ByVal hP1 As hInstance, _
			ByRef CLine As String, _
			ByVal CShow As Integer) As Integer
' Ends declarations



''' Program starts
'''
Function WinMain(ByVal hInstExe As hInstance, _
         ByVal hPrevInstance As hInstance, _
         ByRef lpCmdLine As String, _
         ByVal iCmdShow As Integer) As Integer
   
   dim AS MSG uMsg 
	
   LoadString(hInstExe, IDS_APP_TITLE, szTitle, MAX_LOADSTRING)
   LoadString(hInstExe, IDC_BABYGRID_DEMO, szWindowClass, MAX_LOADSTRING)
   
   MyRegisterClass(hInstExe)
   if InitInstance(hInstExe, iCmdShow) = 0 then return 0
  
   '  Main message loop:
   While GetMessage(@uMsg, NULL, NULL, NULL) <> 0
      TranslateMessage(@uMsg)
      DispatchMessage(@uMsg)
   Wend
   
   return uMsg.wParam
end function


function MyRegisterClass(h1 as HINSTANCE) as ATOM
   dim AS WNDCLASSEX wcex
   
   wcex.cbSize 			= sizeof(WNDCLASSEX)
   wcex.style 				= CS_HREDRAW or CS_VREDRAW
   wcex.lpfnWndProc 		= @WndProc
   wcex.cbClsExtra 		= 0
   wcex.cbWndExtra 		= 0
   wcex.hInstance 		= h1
   wcex.hIcon 				= LoadIcon(h1, MAKEINTRESOURCE(IDI_BABYGRID_FB))
   wcex.hCursor 			= LoadCursor(h1, IDC_ARROW)
   wcex.hbrBackground 	= cast(HBRUSH, COLOR_WINDOW + 1)
   wcex.lpszMenuName 	= cast(LPCSTR, IDC_BABYGRID_DEMO)
   wcex.lpszClassName 	= StrPtr(szWindowClass)
   wcex.hIconSm 			= LoadIcon(h1, MAKEINTRESOURCE(IDI_SMALL_FB))
   
   return RegisterClassEx(@wcex)
end function


Function InitInstance(hInstance1 as HINSTANCE, nCmdShow as integer) as integer
   dim AS HWND hWnd1
   
   hInst = hInstance1                            ' Store instance handle in our global variable
   hWnd1 = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, _
         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance1, NULL)
   if hWnd1 = 0 then return 0
   ShowWindow(hWnd1, nCmdShow)
   UpdateWindow(hWnd1)
   return 1
end function


Function WndProc(ByVal hwnd As HWND, ByVal message As UInteger, _
			ByVal wParam As WPARAM, ByVal lParam As LPARAM) As Integer
		
   Dim As Integer wmId
   Dim As Integer wmEvent
   Dim As Integer row
   Dim As Integer col
   Dim As Integer dtype
   dim AS PAINTSTRUCT ps
   dim AS HDC hdc
   dim szHello              AS zstring *MAX_LOADSTRING
   dim AS RECT rect
	
   LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING)
   Function = 0
   Select Case message
      case WM_COMMAND:
         wmId = LOWORD(wParam)
         wmEvent = HIWORD(wParam)
         
         Select Case wmId
               
            case IDM_ABOUT
               DialogBox(hInst, cast(ZString Ptr, IDD_ABOUTBOX), hWnd, @About)
               
            case IDM_EXIT
               DestroyWindow(hWnd)
               
            case GRID_PROP                       ' properties grid notification that something happened
               
               'a cell was clicked in the properties grid
               if HIWORD(wParam) = BGN_CELLCLICKED then
                  ' get the row and column of the clicked cell
                  row = LOWORD(lParam)
                  col = HIWORD(lParam)
                  ' set the _BGCELL structure variable (cell) to this row and column
                  SetCell(@cell, row, col)
                  ' get the data type that is in the cell
                  ' in this instance, we're looking for integer data (types 3 [TRUE] or 4 [FALSE])
                  ' datatype 1 represents alphanumeric data
                  ' datatype 2 represents numeric data
                  ' datatype 3 represents TRUE data
                  ' datatype 4 represents FALSE data
                  dtype = cint(SendMessage(hgrid1, BGM_GETTYPE, cast(uinteger, @cell), 0))
                  
                  if dtype = 3 then              '  true
                     'if the grid cell was true (checked checkbox), toggle it false
                     
							SendMessage(hgrid1, BGM_SETCELLDATA, cast(uinteger, @cell), cast(uinteger,strptr( "FALSE")))
                     'send appropriate control message to the grid based
                     'on the row of the cell that was toggled
                     if row = 1 then SendMessage(hgrid2, BGM_SETALLOWCOLRESIZE, 0, 0)
                     
                     if row = 2 then SendMessage(hgrid2, BGM_SETEDITABLE, 0, 0)
                     
                     if row = 3 then SendMessage(hgrid2, BGM_SETELLIPSIS, 0, 0)
                     
                     if row = 4 then SendMessage(hgrid2, BGM_SETCOLAUTOWIDTH, 0, 0)
                     
                     if row = 5 then SendMessage(hgrid2, BGM_EXTENDLASTCOLUMN, 0, 0)
                     
                     if row = 6 then
                        SendMessage(hgrid2, BGM_SETCOLSNUMBERED, 0, 0)
                        LoadGrid2(hgrid2)
                     endif
							
                     if row = 7 then SendMessage(hgrid2, BGM_SETROWSNUMBERED, 0, 0)
                     
                     if row = 8 then SendMessage(hgrid2, BGM_SHOWHILIGHT, 0, 0)
                     
                     if row = 9 then SendMessage(hgrid2, BGM_SETCURSORCOLOR, BGR(0, 0, 0), 0)
                     
                     if row = 10 then SendMessage(hgrid2, BGM_SETGRIDLINECOLOR, BGR(255, 255, 255), 0)
                  endif
                  if dtype = 4 then              '_bool false
                     
                     ' if the grid cell was false (unchecked checkbox), toggle it true
                     
							SendMessage(hgrid1, BGM_SETCELLDATA, cast(uinteger, @cell), cast(uinteger,strptr( "TRUE")))
                     ' send appropriate control message to the grid based
                     ' on the row of the cell that was toggled
                     if row = 1 then SendMessage(hgrid2, BGM_SETALLOWCOLRESIZE, 1, 0)
                     
                     if row = 2 then SendMessage(hgrid2, BGM_SETEDITABLE, 1, 0)
                     
                     if row = 3 then SendMessage(hgrid2, BGM_SETELLIPSIS, 1, 0)
                     
                     if row = 4 then SendMessage(hgrid2, BGM_SETCOLAUTOWIDTH, 1, 0)
                     
                     if row = 5 then SendMessage(hgrid2, BGM_EXTENDLASTCOLUMN, 1, 0)
                     
                     if row = 6 then
                        SendMessage(hgrid2, BGM_SETCOLSNUMBERED, 1, 0)
                        SendMessage(hgrid2, BGM_SETHEADERROWHEIGHT, 21, 0)
                     endif
							
                     if row = 7 then SendMessage(hgrid2, BGM_SETROWSNUMBERED, 1, 0)
                     
                     if row = 8 then SendMessage(hgrid2, BGM_SHOWHILIGHT, 1, 0)
                     
                     if row = 9 then SendMessage(hgrid2, BGM_SETCURSORCOLOR, BGR(255, 255, 255), 0)
                     
                     if row = 10 then SendMessage(hgrid2, BGM_SETGRIDLINECOLOR, BGR(220, 220, 220), 0)
                  endif
               Endif
         end select
      case WM_PAINT
         hdc = BeginPaint(hWnd, @ps)
         ' TODO: Add any drawing code here...
         EndPaint(hWnd, @ps)
         
      case WM_SIZE
         
         GetClientRect(hWnd, @rect)
         MoveWindow(hgrid1, 0, 0, rect.right / 3, rect.bottom, 1)
         MoveWindow(hgrid2, rect.right / 3, 0, rect.right - rect.right / 3, rect.bottom, 1)
         
      case WM_CREATE
         RegisterGridClass(hInst)                ' initializes BABYGRID control
         '  only call this function once in your program
         
         ' create 2 grids for placement on the application main window
         ' the 2 grids are placed in the WM_SIZE handler.
         hgrid1 = CreateWindowEx(WS_EX_CLIENTEDGE, "BABYGRID", "Grid " & chr(13, 10) & "Properties", _
               WS_VISIBLE or WS_CHILD, 0, 0, 0, 0, hWnd, cast(HMENU, GRID_PROP), hInst, NULL)
         
         hgrid2 = CreateWindowEx(WS_EX_CLIENTEDGE, "BABYGRID", "BabyGrid -- A simple grid for" & chr(13, 10) & " Win32 API Programmers", _
               WS_VISIBLE or WS_CHILD, 0, 0, 0, 0, hWnd, cast(HMENU, GRID_DATA), hInst, NULL)
         
         ' Set grid2 (the working demonstration grid) to be 100 rows by 5 columns
         SendMessage(hgrid2, BGM_SETGRIDDIM, 100, 5)
         ' set grid1 (the properties grid) to automatically size columns
         ' based on the length of the text entered into the cells
         SendMessage(hgrid1, BGM_SETCOLAUTOWIDTH, 1, 0)
         ' only want 2 columns, rows will be added as data is entered programmatically
         SendMessage(hgrid1, BGM_SETGRIDDIM, 0, 2)
         ' don't want a row header, so make it 0 pixels wide
         SendMessage(hgrid1, BGM_SETCOLWIDTH, 0, 0)
         ' this grid won't use column headings, set header row height = 0
         SendMessage(hgrid1, BGM_SETHEADERROWHEIGHT, 0, 0)
         ' populate grid1 with data
         LoadGrid1(hgrid1)
         ' populate grid2 with initial demo data
         LoadGrid2(hgrid2)
         ' make grid2 header row to initial height of 21 pixels
         SendMessage(hgrid2, BGM_SETHEADERROWHEIGHT, 21, 0)
         
      case WM_DESTROY
         PostQuitMessage(0)
         
   end select
   Function = DefWindowProc(hWnd, message, wParam, lParam)
end function


' Message handler for about box.
Function About(hDlg as HWND, message as Uinteger, wParam as WPARAM, lParam as LPARAM) as integer
   Select Case message
         
      case WM_INITDIALOG
         return 1
         
      case WM_COMMAND
         if LOWORD(wParam) = IDOK or LOWORD(wParam) = IDCANCEL then
            
            EndDialog(hDlg, LOWORD(wParam))
            return 1
         endif
   end select
   return 0
end function


sub PutCell(hgrid as HWND, row as integer, col as integer, text as string)
   
   ' worker function to keep from having to send hundreds of SendMessage() with
   ' BGM_SETCELLDATA in the main program.  Just simplifies the main program code
   SetCell(@cell, row, col)
	
   SendMessage(hgrid, BGM_SETCELLDATA, cast(uinteger, @cell), cast(uinteger, strptr(text)))
end sub


sub LoadGrid2(hgrid as HWND)
   ' loads grid 2 with initial demo data
	
   PutCell(hgrid, 0, 1, "Multi-line" & chr(13, 10) & "Headings" & chr(13, 10) & "Supported")
   PutCell(hgrid, 0, 2, chr(13, 10) & chr(13, 10) & " Name")
   PutCell(hgrid, 0, 3, chr(13, 10) & chr(13, 10) & "Age")
   
   SendMessage(hgrid, BGM_SETPROTECT, 1, 0)
   ' every cell entered after a BGM_SETPROTECT TRUE will set the
   ' protected attribute of that cell.  This keeps an editable grid
   ' from allowing the user to overwrite whatever is in the protected cell
   SendMessage(hgrid, BGM_SETPROTECTCOLOR, BGR(210, 210, 210), 0)
   
   ' the setprotectcolor is optional, but it gives a visual indication
   ' of which cells are protected.
   
   ' now put some data in the cells in grid2
   PutCell(hgrid, 1, 2, "David")
   PutCell(hgrid, 2, 2, "Maggie")
   PutCell(hgrid, 3, 2, "Chester")
   PutCell(hgrid, 4, 2, "Molly")
   PutCell(hgrid, 5, 2, "Bailey")
   
   
   PutCell(hgrid, 1, 3, "43")
   PutCell(hgrid, 2, 3, "41")
   PutCell(hgrid, 3, 3, "3")
   PutCell(hgrid, 4, 3, "3")
   PutCell(hgrid, 5, 3, "1")
   
   PutCell(hgrid, 10, 5, "Shaded cells are write-protected.")
   
   SendMessage(hgrid, BGM_SETPROTECT, 0, 0)
   ' turn off automatic cell protection
   ' if you don't turn off automatic cell protection, if the
   ' grid is editable, the user can enter data into empty cells
   ' but cannot change what he entered... not good.
   
   PutCell(hgrid, 1, 0, "Row Headers customizable")
	
   PutCell(hgrid, 6, 2, "2" & " lignes")
	PutCell(hgrid, 10, 2, "TRUE")
   
end sub


sub LoadGrid1(hgrid as HWND)
   ' load data into the properties grid
   
   PutCell(hgrid, 1, 1, "User Column Resizing")
   PutCell(hgrid, 1, 2, "FALSE")
   PutCell(hgrid, 2, 1, "User Editable")
   PutCell(hgrid, 2, 2, "FALSE")
   PutCell(hgrid, 3, 1, "Show Ellipsis")
   PutCell(hgrid, 3, 2, "TRUE")
   PutCell(hgrid, 4, 1, "Auto Column Size")
   PutCell(hgrid, 4, 2, "FALSE")
   PutCell(hgrid, 5, 1, "Extend Last Column")
   PutCell(hgrid, 5, 2, "TRUE")
   PutCell(hgrid, 6, 1, "Numbered Columns")
   PutCell(hgrid, 6, 2, "FALSE")
   PutCell(hgrid, 7, 1, "Numbered Rows")
   PutCell(hgrid, 7, 2, "TRUE")
   PutCell(hgrid, 8, 1, "Highlight Row")
   PutCell(hgrid, 8, 2, "TRUE")
   PutCell(hgrid, 9, 1, "Show Cursor")
   PutCell(hgrid, 9, 2, "TRUE")
   PutCell(hgrid, 10, 1, "Show Gridlines")
   PutCell(hgrid, 10, 2, "TRUE")
   
   '     make the grid notify the program that the row in the
   '     grid has changed.  Usually this is done by the user clicking
   '     a cell, or moving thru the grid with the keyboard.  But we
   '     want the grid to initially send this message to get things going.
   '     If we didn't call BGM_NOTIFYROWCHANGED, the first row would be
   '     highlighted, but the ACTION wouldn't be performed.
   
   SendMessage(hgrid, BGM_NOTIFYROWCHANGED, 0, 0)
   
   ' make the properties grid have the focus when the application starts
   SetFocus(hgrid)
   
End sub

End WinMain(GetModuleHandle(null), null, Command(), SW_NORMAL) ' start/end


