'-- Easy-Dictionary Sample --'
' ...โปรแกรมนี้เป็นโปรแกรมซึ่งใช้ประโยชน์ได้จริงๆ
' สามารถบันทึก คำศัพท์ และคืนข้อมูลที่เคยบันทึกกลับมาอ่านได้
' ...แต่ยังขาดความสามารถในการทำงานหลายอย่างเช่น
' เรียงคำตามตัวอักษร เรียงตามประเภทคำศัพท์
' ค้นหาคำศัพท์ แทรกคำศัพท์ระหว่างบรรทัด
' การแสดงผลข้อมูลแบบหลายบรรทัด ฯลฯ
$OPTIMIZE ON
$INCLUDE "RAPIDQ.INC"
$APPTYPE   GUI
$TYPECHECK ON
$ESCAPECHARS ON


Function Dir_Of_Program As STRING
Dim BSpo AS INTEGER
BSpo = RINSTR(command$(0),"\\")
Dir_Of_Program = Delete$(command$(0),BSpo+1,len(command$(0))-BSpo)
End Function
CONST ProgDir$ = Dir_Of_Program

DIM Form AS QFORM
	DIM pnL AS QPanel
		DIM Grid AS QStringGrid

' - - - - - - - - - - - - - - - - - - - - EVENT - - - - - - - - - - - - - - - - - - - - ' 

SUB FormRe '-- ทำงานเวลาเปลี่ยนขนาดหน้าต่าง 
	Grid.ColWidths(1) = (pnL.ClientWidth-Grid.ColWidths(0))/3-18
	Grid.ColWidths(2) = Grid.ColWidths(1)+45
	Grid.ColWidths(3) = Grid.ColWidths(1)-12
END SUB

SUB GridKP(Key AS Byte)
IF Key=13 THEN  '-- ทำงานเมื่อกด enter 
	IF Grid.Row=Grid.RowCount-1 THEN '-- ทำงานเมื่ออยู่ที่แถวล่างสุด 
		Grid.InsertRow( Grid.RowCount )
	END IF
	Grid.Row=Grid.Row+1
	Grid.Cell(0,Grid.RowCount-1)=str$(Grid.RowCount-1) '-- แสดงลำดับที่อันใหม่ ที่ช่องซ้ายสุด 
END IF
END SUB

SUB GridKU(Key AS Word, Shift AS INTEGER)
	IF Grid.Col=3 THEN '-- ทำงานตอนอยู่ที่ column ที่ 3 
	SELECT Case Key '-- เปลี่ยนค่าเมื่อกดปุ่ม keyboard ซึ่งตั้งไว้ว่าเป็น shortcut 
	case 78:	Grid.Cell(Grid.Col,Grid.Row)="noun" '-- (n) 
	case 86:	Grid.Cell(Grid.Col,Grid.Row)="verb" '-- (v) 
	case 74:	Grid.Cell(Grid.Col,Grid.Row)="adjective" '-- ( j ) 
	case 65:	Grid.Cell(Grid.Col,Grid.Row)="adverb" '-- (a) 
	case 80:	Grid.Cell(Grid.Col,Grid.Row)="preposition" '-- (p) 
	case 67:	Grid.Cell(Grid.Col,Grid.Row)="conjunction" '-- (c) 
	END SELECT
	END IF
END SUB

SUB FormShow
	IF fileExists(ProgDir$+"vocab.txt") THEN '-- ถ้าพบไฟล์ vocab.txt 
		Grid.LoadFromFile( ProgDir$+"vocab.txt", 1, 1, 300) '-- กำหนดได้ไม่เกิน 300 คำ 
	ELSE
		ShowMessage "ไม่พบไฟล์ vocab.txt ใน directory ของโปรแกรม"+_
						"\r\nโปรแกรมได้ทำการสร้างไฟล์ vocab.txt ขึ้นใหม่"+_
						"\r\n\r\nNot Found 'vocab.txt' in dir of program."+_
						"\r\nProgram create 'vocab.txt' again."
		Grid.SaveToFile( ProgDir$+"vocab.txt", 1, 1, 1) '-- สร้างไฟล์ vocab.txt ใน dir เดียวกับโปรแกรม 
	END IF
	DIM i AS INTEGER
	for i=1 to Grid.rowCount-1 step 1
		Grid.cell(0,i)=str$(i)
	next
END SUB

SUB FormClos(Action AS INTEGER) '  เมื่อปิด form  ' 
	IF MessageBox("Save This File?", "Confirm", 1) = 1 _
		THEN
			Grid.SaveToFile( ProgDir$+"vocab.txt", 1, 1, Grid.RowCount-1)
	END IF
	IF MessageBox("Close Program?", "Close", 1) = 1 _
		THEN
			Action = 2 '-- ปิด 
		ELSE
			Action = 0 '-- ไม่ปิด 
	END IF
END SUB

SUB FormWn:END SUB

' - - - - - - - - - - - - - - - - - - - - PROPERTY - - - - - - - - - - - - - - - - - - - - ' 
with Form
.Caption = "Easy - Dictionary version 0.1"
.Width = 562:.Height = 300:.Center
.onShow = FormShow
.onResize = FormRe
.onClose = FormClos
end with
	with pnL:.Parent = Form
	.Align = alClient
	end with
		with Grid:.Parent = pnL
		.Align = alClient
		.BorderStyle = 0
		.ColWidths(0)=39
		.ColumnStyle(3)=gcsList
		.ColumnList(3)="noun\nverb\nadjective\nadverb\npreposition\nconjunction"
		.Cell(1,0)="   vocab":.cell(2,0)="   คำแปล":.cell(3,0)="   ชนิด"
		.ColCount = 4
		.RowCount = 10
		.Height=Form.ClientHeight
		.Width=Form.ClientWidth - 50
		.AddOptions(goEditing,goAlwaysShowEditor)
		.Separator = "--"
		.onKeyPress = GridKP
		.onKeyUp = GridKU
		end with

' - - - - - - - - - - - - - - - - - - - - DISPLAY - - - - - - - - - - - - - - - - - - - - ' 
Form.wndProc = FormWn
Form.SHOWMODAL