$Include "rapidq.inc"

DECLARE FUNCTION Setfocus Lib "user32" Alias "SetFocus"(hwnd As Long) As Long
DECLARE SUB Init
DECLARE SUB DoStuff
DECLARE SUB Panel1Click (Sender AS QPANEL)
declare sub KeyCheck(key as byte, shift as word,sender as QEdit,tk as integer)
DECLARE SUB DrawSection (Index AS INTEGER, Pressed AS INTEGER, Rect AS QRECT, Sender AS QHEADER) 
DECLARE SUB SectionTrack (Index AS INTEGER, Width AS INTEGER, State AS INTEGER, Sender AS QHEADER) 
declare SUB OnMouseDown(Button%, X%, Y%, Shift%)

dim rp(65) as qpanel
dim tl(12) as qpanel
DIM Edit(64,12) AS QPANEL
dim ptemp as qpanel
dim ph as qpanel
DIM EDT AS QEDIT
Dim Buton as QBUTTON
DIM Font AS QFONT
DIM Bitmap AS QBITMAP
dim labela as qlabel
dim hed as qheader
irow=0
icol=0
crow=0
ccol=0
nrows=30
ncols=5
CREATE Form AS QFORM
ICON="DG.ICO"
cAPTION="RQ-DataGrid"
OnShow = Init
CREATE StatusBar AS QSTATUSBAR 
SimplePanel = TRUE 
SimpleText = "Not moving"
 END CREATE 
 
Center
ShowModal
END CREATE

SUB Init
form.height=500
form.width=650
 
labela.parent=Form
labela.top=400
labela.left=150
labela.caption="key"
Buton.Parent=Form
Buton.Left=60
Buton.top=550
Buton.Caption = "Check size"
Buton.OnClick = DoStuff
Font.Size = 8
Font.Name = "Arial"
Bitmap.Font = Font
ph.parent=form
ph.top=0
ph.left=16
ph.width=(ncols+1)*100
ph.color=rgb(255,255,0)
ph.height=32
hed.parent=ph
hed.align=5
hed.left=30
for cl%=0 to ncols
a$=str$(cl%)

hed.AddSections=a$
'Sections(cl%).AllowClick = FALSE
 hed.Sections(cl%).Style = hsOwnerDraw 
 hed.Sections(cl%).Width = 100 
 hed.height=32
 hed.OnDrawSection = DrawSection 
hed.OnSectionTrack = SectionTrack 
hed.onmousedown=onmousedown
  next
for ar%=1 to nrows+1
rp(ar%).parent=Form
rp(ar%).left=0
rp(ar%).width=16
rp(ar%).top=ar%*16+16
rp(ar%).height=16
next
rp(0).parent=form
rp(0).caption="*"
rp(0).top=0
rp(0).left=0
rp(0).width=16
rp(0).height=32
rp(1).caption=">"
FOR A% = 0 TO nrows
FOR B%=0 TO ncols
Edit(A%,B%).Parent = Form
Edit(A%,B%).Alignment=b% mod 3
Edit(A%,B%).fONT=fONT
Edit(A%,B%).Top = 32+A% * 16
Edit(A%,B%).Height = 16
Edit(A%,B%).Width=100
Edit(A%,B%).lEFT=16+100*B%
Edit(A%,B%).TabOrder=100*A%+B%
Edit(A%,B%).CAPTION=STR$(A%*100+B%)
Edit(A%,B%).BevelOuter=1
IF A%=0 THEN
edit(A%,B%).color=rgb(0,233,214)
ELSE
edit(A%,B%).color=rgb(236,233,214)
END IF
Edit(A%,B%).tag=a%*1000+b%
Edit(A%,B%).OnClick=Panel1Click
NEXT
NEXT
EDT.PARENT=EDIT(0,0)
EDT.ALIGN=5
edt.OnKeyDown=KeyCheck
edt.borderstyle=0
EDT.TEXT="1234"
FOR A% = 0 TO nrows-1
FOR B%=0 TO ncols
BB% = VAL(Edit(A%,B%).cAPTION)
IF BB% THEN
INC(Count%)
INC(Total%, BB%)
END IF
NEXT
NEXT
Edit(10,1).cAPTION = "Aver = " 
'ncols=7
'nrows=2
'redim edit(7,2)
form.repaint
END SUB

SUB DoStuff
NROWS=10
EDT.PARENT=EDIT(5,5)
tk=edit(1,1).tag
H = Bitmap.TextHeight(Edit(14,1).cAPTION)
W = Bitmap.TextWidth(Edit(14,1).CAPTION)
ShowMessage STR$(H) + " px high by " + STR$(W)+chr$(13)+STR$( Edit(14,1).hEIGHT)+"   "+STR$(Edit(14,1).WIDTH)
setfocus(edt.handle)
FORM.REPAINT
END SUB

SUB Panel1Click (Sender AS QPANEL)
crow=int(sender.tag/1000)
ccol=sender.tag-crow*1000
edt.text= str$(crow)+ "-" + str$(ccol)
EDT.PARENT=EDIT(crow,ccol)
setfocus(edt.handle)
tk=edit(crow,ccol).tag
'edt.inputmask="999.99"
for ic=0 to ncols
edit(irow,ic).color=rgb(236,233,214)
edit(crow,ic).color=rgb(0,233,214)
RP(CROW+1).Caption=">"
RP(IROW+1).Caption=" "

next
irow=crow
icol=ccol
END SUB

sub KeyCheck(key as byte, shift as word,sender as QEdit,tk as integer)
if key=38 then
if icol>0 then
icol=icol-1
EDT.PARENT=EDIT(crow,icol)
setfocus(edt.handle)
else
icol=ncols
if irow>1 then
crow=irow-1
else
crow=nrows
end if
end if
EDT.PARENT=EDIT(crow,icol)
setfocus(edt.handle)
for ic=0 to ncols
edit(irow,ic).color=rgb(236,233,214)
edit(crow,ic).color=rgb(0,233,214)
RP(CROW+1).Caption=">"
RP(IROW+1).Caption=" "
next
irow=crow
end if
if (key=13 or key=40)  or then
if icol<ncols then
icol=icol+1
EDT.PARENT=EDIT(crow,icol)
setfocus(edt.handle)
else
icol=0
if irow<nrows then
crow=irow+1
else
crow=1
end if
end if
EDT.PARENT=EDIT(crow,icol)
setfocus(edt.handle)
for ic=0 to ncols
edit(irow,ic).color=rgb(236,233,214)
edit(crow,ic).color=rgb(0,233,214)
RP(CROW+1).Caption=">"
RP(IROW+1).Caption=" "
next
irow=crow
end if
ph.width=edit(0,ncols).left+edit(0,ncols).width-16
end sub

SUB DrawSection (Index AS INTEGER, Pressed AS INTEGER, Rect AS QRECT, Sender AS QHEADER) 
Sender.TextOut(Rect.Left+15,Rect.Top+1, "column", &HFF0000, -1)
Sender.TextOut(Rect.Left+35,Rect.Top+16, STR$(Index+1), &HFF0000, -1) 
END SUB 

SUB SectionTrack (Index AS INTEGER, Width AS INTEGER, State AS INTEGER, Sender AS QHEADER) 
StatusBar.SimpleText = "Moving "+STR$(Width) 
if state=1 then moved=1
if state=2 and moved=1 then
for wrow=0 to nrows
edit(wrow,index).width=width
edit(wrow,index+1).left=edit(wrow,index).left+edit(wrow,index).width
next
for wcol=2 to ncols
for wrow=0 to nrows
edit(wrow,wcol).left=edit(wrow,wcol-1).left+edit(wrow,wcol-1).width
next
next
labela.caption=str$(State)
ph.width=edit(0,ncols).left+edit(0,ncols).width-16
moved=0
end if
'SELECT CASE Index 
'CASE 0 
'ListBox1.Width = Width '
'END SELECT 
END SUB 
SUB OnMouseDown(Button%, X%, Y%, Shift%)
labela.caption=str$(ph.width)
'"md0nh : "+str$(x%)+"-"+str$(y%)+" butt:"+str$(Button%)
end sub