[tekui-devel] tekui: UTF8String: added overwrite() and set() methods; TextInpu...

Timm S. Mueller tmueller at schulze-mueller.de
Thu Feb 19 02:07:02 CET 2009


details:   http://hg.teklib.org/tekui/rev/47357ba78a08
changeset: 49:47357ba78a08
user:      Timm S. Mueller <tmueller at schulze-mueller.de>
date:      Thu Feb 19 02:03:39 2009 +0100
description:
UTF8String: added overwrite() and set() methods; TextInput: added EnterNext
attribute, added addChar(), backSpace(), delChar(), getChar(), getCursor(),
moveCursorLeft(), moveCursorRight() methods; added configpanel example; merged
in changes from TEKlib main development branch

diffs (truncated from 7172 to 100 lines):

diff -r 1fb04d782a1b -r 47357ba78a08 bin/configpanel.lua
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/configpanel.lua	Thu Feb 19 02:03:39 2009 +0100
@@ -0,0 +1,207 @@
+#!/usr/bin/env lua
+
+local ui = require "tek.ui"
+local UTF8String = require "tek.class.utf8string"
+
+-------------------------------------------------------------------------------
+--	NetworkInput class
+-------------------------------------------------------------------------------
+
+local NetworkInput = ui.TextInput:newClass { _NAME = "_networkinput" }
+
+function NetworkInput.init(self)
+	self.Text = self.Text or "0.0.0.0"
+	self.TextCursor = 0
+	return ui.TextInput.init(self)
+end
+
+function NetworkInput:moveCursorRight()
+	local res = self:getSuper().moveCursorRight(self)
+	if self:getChar() == "." then
+		res = self:getSuper().moveCursorRight(self)
+	end
+	return res
+end
+
+function NetworkInput:moveCursorLeft()
+	local res = self:getSuper().moveCursorLeft(self)
+	if self:getChar() == "." then
+		res = self:getSuper().moveCursorLeft(self)
+	end
+	return res
+end
+
+function NetworkInput:checkValid(s)
+	local a, b, c, d = s:get():match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")
+	a, b, c, d = tonumber(a) or -1, tonumber(b) or -1, tonumber(c) or -1,
+		tonumber(d) or -1
+	local valid = a >= 0 and a < 256 and b >= 0 and b < 256 and
+		c >= 0 and c < 256 and d >= 0 and d < 256
+	if valid then
+		s:set(("%d.%d.%d.%d"):format(a, b, c, d))
+	end
+	return valid
+end
+
+function NetworkInput:addChar(utf8c)
+	if utf8c:match("^[0-9]$") then
+		local t = self.TextBuffer
+		local s = UTF8String:new(t:get())
+		local pos = self:getCursor() + 1
+		s:insert(utf8c, pos)
+		if self:checkValid(s) then
+			self.TextBuffer = s
+			self:moveCursorRight()
+			return
+		end
+		s = UTF8String:new(t:get())
+		s:overwrite(utf8c, pos)
+		if self:checkValid(s) then
+			self.TextBuffer = s
+			self:moveCursorRight()
+			return
+		end
+	end
+end
+
+function NetworkInput:backSpace()
+	if self:moveCursorLeft() then
+		local pos = self:getCursor() + 1
+		local t = self.TextBuffer
+		local s = UTF8String:new(t:get())
+		s:erase(pos, pos)
+		if self:checkValid(s) then
+			self.TextBuffer = s
+			return
+		end
+		self.TextBuffer:overwrite("0", pos)
+	end
+end
+
+function NetworkInput:delChar()
+	local t = self.TextBuffer
+	if t:len() > 0 then
+		local pos = self:getCursor() + 1
+		local s = UTF8String:new(t:get())
+		s:erase(pos, pos)
+		if self:checkValid(s) then
+			self.TextBuffer = s
+			return
+		end
+		local s = UTF8String:new(t:get())
+		s:overwrite("0", pos)
+		if self:checkValid(s) then
+			self.TextBuffer = s
+			return
+		end


More information about the tekui-devel mailing list