Horray For TextBoxes!

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Imortis
Moderator
Posts: 1966
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Horray For TextBoxes!

Post by Imortis »

Here are the promised TextBoxes. They work pretty well, and require the mouse class.bas from the Horray For Buttons! topic.

textbox Class.bas

Code: Select all

Type TextBox
	private:
	Xpos as integer
	Ypos as integer
	Xsize as integer
	Ysize as integer
	Length as integer
	OldStatus as byte
	Status as byte
	/'status
	0 - No textbox
	1 - Active, not in focus
	3 - Pressed (mouse not released)
	4 - Clicked
	-1 - inactive'/
	Text as string * 255
	Redraw as bool
	
	public:
	declare Property getXPos() as integer
	declare Property getYpos() as integer
	declare Property getXSize() as integer
	declare Property getYSize() as integer
	declare Property getLength() as integer
	declare Property getOldStatus() as byte
	declare Property getStatus() as byte
	declare Property getText() as string
	declare Property getReDraw() as bool
	
	declare Property setXPos(num as integer)
	declare Property setYpos(num as integer)
	declare Property setXSize(num as integer)
	declare Property setYSize(num as integer)
	declare Property setLength(num as integer)
	declare Property setStatus(num as byte)
	declare Property setText(label as string)
	declare Property setReDraw(TF as bool)
	
	OnEnter as Sub()
	
	declare Sub Create(X as integer, Y as integer, L as integer)
	declare Sub Destroy()
	declare Sub DrawMe(action as byte)
	declare Sub Control(key as string, mouse as MouseData)
	declare Sub Activate()
	declare Sub Deactivate()
	declare Sub Dummy()
	
	declare Function IsActive() as Bool
	declare Function IsMouseOn(mouse as MouseData) as bool
	declare function IsPressed(mouse as mouseData) as bool
	declare Function IsClicked(mouse as mouseData) as bool
	
end type

Property TextBox.getXPos() as integer
	return this.XPos
end property

Property TextBox.getYpos() as integer
	return this.YPos
end property

Property TextBox.getXSize() as integer
	return this.XSize
end property

Property TextBox.getYSize() as integer
	return this.YSize
end property

Property TextBox.getLength() as integer
	return this.Length
end property

Property TextBox.getOldStatus() as byte
	return this.OldStatus
end property

Property TextBox.getStatus() as byte
	return this.Status
end property

Property TextBox.getText() as string
	return this.Text
end property

Property TextBox.getReDraw() as bool
	return this.ReDraw
end property
	
Property TextBox.setXPos(num as integer)
	this.Xpos = num
end property

Property TextBox.setYpos(num as integer)
	this. Ypos = num
end property

Property TextBox.setXSize(num as integer)
	this.XSize = num
end property

Property TextBox.setYSize(num as integer)
	this.YSize = num
end property

Property TextBox.setLength(num as integer)
	this.Length = num
end property
	
Property TextBox.setStatus(num as byte)
	this.oldStatus = this.Status
	this.Status = num
end property

Property TextBox.setText(label as string)
	this.Text = label
end property

Property TextBox.setReDraw(TF as bool)
	this.Redraw = TF
end property
	
Sub TextBox.Create(X as integer, Y as integer , L as integer)
	this.Xpos = X
	this.Ypos = Y
	this.Length = L
	this.Xsize = L * 8 + 10
	this.Ysize = 12
	this.status = 1
	this.oldStatus = 0
	this.Redraw = True
	this.Text = ""
	this.OnEnter = ProcPtr(Dummy)
end sub

Sub TextBox.Destroy()
	line(this.Xpos, this.Ypos)-(this.Xpos+this.Xsize, this.Ypos+this.Ysize),0,bf
	this.Xpos = 0
	this.Ypos = 0
	this.Length = 0
	this.Xsize = 0
	this.Xsize = 0
	this.status = 0
	this.OldStatus = 0
	this.Redraw = False
	this.Text = ""
	this.OnEnter = ProcPtr(Dummy)
end sub

Sub TextBox.DrawMe(action as byte)
	if this.redraw = True then
		if status <> -1 then
			LINE (this.Xpos, this.Ypos)-(this.Xpos+this.Xsize, this.Ypos+this.Ysize), 15, BF
			LINE (this.Xpos, this.Ypos)-(this.Xpos+this.Xsize, this.Ypos+this.Ysize), 0, B
			IF action = 1 THEN 
				draw string(this.Xpos+2,this.Ypos+3), this.Text,0
			else
				draw string(this.Xpos+2,this.Ypos+3), this.Text + "_",0
			end if
		else
			LINE (this.Xpos, this.Ypos)-(this.Xpos+this.Xsize, this.Ypos+this.Ysize), 7, BF
			LINE (this.Xpos, this.Ypos)-(this.Xpos+this.Xsize, this.Ypos+this.Ysize), 8, B
			Draw String(this.Xpos+3,this.Ypos+4),this.Text,15
			Draw String(this.Xpos+2,this.Ypos+3),this.Text,8
		end if
		this.Redraw = False
	end if
end sub

Sub TextBox.Control(key as string, mouse as MouseData)
	if this.IsActive then
		this.IsMouseOn(mouse)
		this.IsPressed(mouse)
		this.IsClicked(mouse)
		if this.Status = 4 then
			if len(key) then
				SELECT CASE ASC(Key)   'Get ASCII code of the key.
					CASE IS > 31            'Any printable character
						IF len(trim(this.Text)) < this.Length THEN
							this.Text = this.Text + Key
							this.ReDraw = True
						END IF
					CASE 8                  'Backspace
						IF LEN(trim(this.Text)) > 0 THEN   'If there is smth in Buffer$
							this.Text = LEFT(this.Text, LEN(Trim(this.Text)) - 1)
							this.Redraw = True
						END IF
					CASE 13
						this.OldStatus = this.Status
						this.Status = 1
						this.Redraw = True
						this.DrawMe(1)
						this.OnEnter()
				END SELECT
			end if
			this.DrawMe(2)
		end if
		this.DrawMe(1)
	end if
end sub

Sub TextBox.Activate()
	this.oldStatus = this.status
	this.status = 1
	this.Redraw = True
	this.DrawMe(1)
end sub

Sub TextBox.Deactivate()
	this.oldStatus = this.Status
	this.status = -1
	this.Redraw = True
	this.DrawMe(2)
end sub

sub Textbox.Dummy()
	'This is just to give the textbox nothing to do if you press enter with out a sub attached
end sub

Function TextBox.IsMouseOn(mouse as MouseData) as bool
	if (mouse.x > this.Xpos) and (mouse.x < (this.Xpos + this.XSize)) then
		if (mouse.y > this.Ypos) and (mouse.y < (this.Ypos + this.YSize)) then
			return true
		end if
	end if
	return false
end function

function TextBox.IsActive() as bool
	if this.Status <> -1 and this.status <> 0 then
		return True
	else
		return False
	end if
end function

function TextBox.IsClicked(mouse as MouseData) as bool
	if mouse.b = 0 then
		if this.OldStatus = 3 then
			if this.IsMouseOn(mouse) then
				this.oldStatus = this.Status
				this.Status = 4
				this.Redraw = True
				this.DrawMe(2)
				return True
			else
				this.oldStatus = this.Status
				this.Status = 1
				this.Redraw = True
				this.DrawMe(1)
				return False
			end if
		else
			return False
		end if
	else
		return False
	end if
	return False
end function

function TextBox.IsPressed(mouse as MouseData) as Bool
	if (mouse.b and 1) then
		if this.IsMouseOn(mouse) then
			this.oldStatus = this.Status
			this.Status = 3
			this.Redraw = True
			this.DrawMe(1)
			return True
		else
			this.oldStatus = this.Status
			this.Status = 1
			this.Redraw = True
			this.DrawMe(1)
			return False
		end if
		this.oldStatus = this.Status
		this.Status = 1
		this.Redraw = True
		this.DrawMe(1)
	end if
	return False
end function
And here is how you use it:

Code: Select all

#define False 0
#define True Not(false)

type bool as byte

#include "mouse class.bas"
#include "TextBox Class.bas"

screen 18
width 80,60

dim as TextBox Test
dim mouse as MouseData
dim as string key,buffer
dim as byte redrawtb

test.Create(10,10,20)
test.DrawMe(1)

do
	key = inkey
	getmouse mouse.x,mouse.y,mouse.w,mouse.b
	
	test.Control(key,Mouse)
	
	if key = chr(32) then
		if test.getStatus <> 4 then
			if test.getStatus > 0 then
				test.deactivate()
			elseif test.getStatus = -1 then
				test.activate()
			end if
		end if
	end if
	if key = chr(8) then
		if test.getStatus <> 4 then
			if test.getStatus > 0 then
				test.destroy()
			elseif test.getStatus = -1 then
				test.destroy()
			end if
		end if
	end if
loop until multikey(1) or key = chr(255)+"k"
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Post by VonGodric »

cool -see my last post about buttons. Also for properties -you can simply go with Xpos instead of GetXpos and SetXpos. I think it would be more logical...

and your textbox is limited to 255 chars ;) use just string but remember to clear it in destructor.
Imortis
Moderator
Posts: 1966
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Post by Imortis »

As to clearing it in destructor, is there any advantage to doing that over the way I have?
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Post by VonGodric »

just nicer and safer code.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

FYI, dynamic strings do *not* need to be cleared in the destructor. FB clears them automatically.
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Post by VonGodric »

oh? nice then... yea I remember now... Pseudo objects...
Post Reply