[fxruby-users] ProgressiveSelectTextField

Uwe Hartl uwe.hartl at gmx.net
Tue Feb 28 16:31:37 EST 2006


Hello,

is somebody interested on something like this? There is a little problem with 
the FXChoiceBox.ask which I just replaced by a 

p "Warning"

and I have some difficulties how to quote a "\" and "(" in a regular 
expression but everything else seems to work in here. Can somebody help me 
out here? 

Is there a database of code snippets, where I would put something like this, 
or do you guys think, people should write this kind of stuff (extensions of 
widgets) themselves?

See you,

Uwe

by the way, "*" is a wildcard in here...

require 'fox14'
include Fox


class ProgressiveSelectTextField < FXTextField
	include Responder

	def initialize(parent, new_entries_allowed = true, confirm_new_entries = 
true, nc = 3, tgt=nil, sel=0, opts=TEXTFIELD_NORMAL, x=0, y=0, w=0, h=0, 
pl=DEFAULT_PAD, pr=DEFAULT_PAD, pt=DEFAULT_PAD, pb=DEFAULT_PAD)
		super(parent, nc, tgt=nil, sel=0, opts = LAYOUT_FILL_X)
		@new_entries_allowed = new_entries_allowed
		@confirm_new_entries = confirm_new_entries
		@fields = []
		@items = []
		@selected = 0
		@old_text = ""
		connect(SEL_CHANGED, method(:on_changed))
		connect(SEL_KEYPRESS, method(:on_key))
		connect(SEL_FOCUSIN, method(:on_focus_in))
		connect(SEL_FOCUSOUT, method(:on_focus_out))
		connect(SEL_LEFTBUTTONPRESS, method(:on_clicked))
		enable
		@sp = FXMenuPane.new(self, POPUP_VERTICAL|FRAME_RAISED|FRAME_THICK)
		@list = FXList.new(@sp, nil, 0, LIST_SINGLESELECT|LAYOUT_FILL_X|
LAYOUT_FILL_Y)
		@list.connect(SEL_DOUBLECLICKED) { |sender, sel, data|
			self.text = @items[data]
			self. selectAll
			@sp.popdown
		}
		@list.connect(SEL_KEYPRESS) { |sender, sel, event|
			self.text = event.to_s
			@sp.popdown
		}
	end
	def appendItem(text)
		@fields << text
	end
	def append_array(txt)
		txt.each do |tt|
			appendItem(tt)
		end
	end
	def set_default_text(def_text = "*")
		self.text = def_text
		@old_text = def_text
	end
	def on_clicked(sender, sel, event)
		on_focus_in(sender, sel, event)
	end
	def on_focus_in(sender, sel, event)
		if !@sp.shown?()
			populateList(self.text)
			ret = true
			@selected = 0
		else
			ret = false
		end
		ret
	end
	def on_focus_out(sender, sel, data)
		if (@new_entries_allowed && @confirm_new_entries && !@fields.include?
(self.text))
			f = File.open("icons/bigpenguin.png", "rb")
			icon = FXPNGIcon.new(getApp(), f.read)
			p "here there is the need for asking if the user wants to get a new text 
string in the field (not existent in the Array. Not implemented"
			#choice = FXChoiceBox.ask(getApp(), opts = 0, caption = "Unbekannter 
Eintrag", text = "Wirklich neuen Wert anlegen?", icon, ["Ja", "Nein"])
			#self.text = "" if choice == 1
		end
	end
	def on_changed(sender, sel, data)
		new = !populateList(data)
		if !@new_entries_allowed && new
			self.text = @old_text
		end
		@old_text = self.text
		@selected = 0
	end
	def sel
		if @selected < @list.numItems
			@list.makeItemVisible(@selected)
			@list.selectItem(@selected)
		end
	end
	def on_key(sender, sel, event)
		if populateList(self.text)
			if ((@selected < ((@list.numItems) - 1)) && event.code == 65364) #arrow 
down
				@selected += 1
				self.sel
			elsif ((@selected > 0) && event.code == 65362) #arrow up
				@selected -= 1
				self.sel
			elsif event.code == 65293 # Enter key
				self.text = @list.getItem(@selected).text
				self.cursorPos = self.text.length
				self.selectAll
				@sp.popdown
			end
		end
		false
	end
	def popup
		xy = translateCoordinatesTo(getRoot(), x, y)
		@sp.popup(nil, xy[0], xy[1]+height, self.width, 100)
		@sp.recalc
		@sp.create
	end
	def populateList(searchString = "")
		ss = searchString.gsub("(", ".")
		ss.gsub!(")", ".")
		p ss
		re = Regexp.new(ss.gsub("*", "(\\D*|\\d*)"))
		p re
		@items = @fields.collect { |val| val if val.match(re).to_s != ""}	
		@items.delete(nil)
		@list.clearItems
		if @items.length > 0
			@items.each do |it|
				@list.appendItem(it)
			end
			popup
			sel
			ret = true
		else
			@sp.popdown()
			ret = false
		end
		return ret
	end
end



class Welcome < FXMainWindow
	def initialize()
		super($theApp, 'ComboBox', icon=nil, miniIcon=nil, opts=DECOR_ALL, x=0, y=0, 
width=300, height=300)

		ta = ProgressiveSelectTextField.new(self)
		txt = ["able", "apartment", "apple", "Appolinaris", "andruide", "berta", 
"aparent", "aple", "Apinaris", "anduide", "beta", "Zeta"]
		ta.append_array(txt)
		ta.set_default_text(txt[2])
	end
end

if $0 == __FILE__
	$theApp = FXApp.new
	$wn = Welcome.new
	$wn.show
	$theApp.create

	$theApp.run
end

-- 
***********************************************************************
Uwe Hartl                                e-mail: Uwe.Hartl at gmx.net
91522 Ansbach                            Telephone: (0981) 9724526
Am Mühlfeld 8                            Notfall-Mobil: (0160)90418680
Germany
***********************************************************************




More information about the fxruby-users mailing list