1999-08-13 01:37:52 -04:00
|
|
|
#
|
|
|
|
# tkentry.rb - Tk entry classes
|
|
|
|
# $Date$
|
|
|
|
# by Yukihiro Matsumoto <matz@caelum.co.jp>
|
|
|
|
|
|
|
|
require 'tk.rb'
|
|
|
|
|
|
|
|
class TkEntry<TkLabel
|
2000-01-31 22:12:21 -05:00
|
|
|
include Scrollable
|
|
|
|
|
2003-07-23 12:07:35 -04:00
|
|
|
TkCommandNames = ['entry'.freeze].freeze
|
1999-08-13 01:37:52 -04:00
|
|
|
WidgetClassName = 'Entry'.freeze
|
1999-08-24 04:21:56 -04:00
|
|
|
WidgetClassNames[WidgetClassName] = self
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2000-11-27 04:23:38 -05:00
|
|
|
class ValidateCmd
|
|
|
|
include TkComm
|
|
|
|
|
2003-08-03 18:07:47 -04:00
|
|
|
module Action
|
|
|
|
Insert = 1
|
|
|
|
Delete = 0
|
|
|
|
Others = -1
|
|
|
|
Focus = -1
|
|
|
|
Forced = -1
|
|
|
|
Textvariable = -1
|
|
|
|
TextVariable = -1
|
|
|
|
end
|
|
|
|
|
2000-11-27 04:23:38 -05:00
|
|
|
class ValidateArgs
|
2003-06-18 15:46:20 -04:00
|
|
|
VARG_KEY = 'disvPSVW'
|
2003-11-21 02:49:11 -05:00
|
|
|
VARG_TYPE = 'nxeseesw'
|
2003-06-18 15:46:20 -04:00
|
|
|
|
|
|
|
def self.scan_args(arg_str, arg_val)
|
2003-11-21 02:49:11 -05:00
|
|
|
enc = Tk.encoding
|
2003-06-18 15:46:20 -04:00
|
|
|
arg_cnv = []
|
|
|
|
arg_str.strip.split(/\s+/).each_with_index{|kwd,idx|
|
|
|
|
if kwd =~ /^%(.)$/
|
|
|
|
if num = VARG_KEY.index($1)
|
|
|
|
case VARG_TYPE[num]
|
|
|
|
when ?n
|
|
|
|
arg_cnv << TkComm::number(arg_val[idx])
|
|
|
|
when ?s
|
|
|
|
arg_cnv << TkComm::string(arg_val[idx])
|
2003-11-21 02:49:11 -05:00
|
|
|
when ?e
|
|
|
|
if enc
|
|
|
|
arg_cnv << Tk.fromUTF8(TkComm::string(arg_val[idx]), enc)
|
|
|
|
else
|
|
|
|
arg_cnv << TkComm::string(arg_val[idx])
|
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
when ?w
|
|
|
|
arg_cnv << TkComm::window(arg_val[idx])
|
2003-08-03 18:07:47 -04:00
|
|
|
when ?x
|
|
|
|
idx = TkComm::number(arg_val[idx])
|
|
|
|
if idx < 0
|
|
|
|
arg_cnv << nil
|
|
|
|
else
|
|
|
|
arg_cnv << idx
|
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
else
|
|
|
|
arg_cnv << arg_val[idx]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
arg_cnv << arg_val[idx]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
arg_cnv << arg_val[idx]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
arg_cnv
|
|
|
|
end
|
|
|
|
|
2000-11-27 04:23:38 -05:00
|
|
|
def initialize(d,i,s,v,pp,ss,vv,ww)
|
|
|
|
@action = d
|
|
|
|
@index = i
|
|
|
|
@current = s
|
|
|
|
@type = v
|
|
|
|
@value = pp
|
|
|
|
@string = ss
|
|
|
|
@triggered = vv
|
|
|
|
@widget = ww
|
|
|
|
end
|
|
|
|
attr :action
|
|
|
|
attr :index
|
|
|
|
attr :current
|
|
|
|
attr :type
|
|
|
|
attr :value
|
|
|
|
attr :string
|
|
|
|
attr :triggered
|
|
|
|
attr :widget
|
|
|
|
end
|
|
|
|
|
2003-06-16 03:14:50 -04:00
|
|
|
def initialize(cmd = Proc.new, args=nil)
|
2000-11-27 04:23:38 -05:00
|
|
|
if args
|
2003-06-18 15:46:20 -04:00
|
|
|
@id =
|
|
|
|
install_cmd(proc{|*arg|
|
2003-08-03 18:07:47 -04:00
|
|
|
TkUtil.eval_cmd(proc{|*v| (cmd.call(*v))? '1': '0'},
|
|
|
|
*ValidateArgs.scan_args(args, arg))
|
2003-06-18 15:46:20 -04:00
|
|
|
}) + " " + args
|
2000-11-27 04:23:38 -05:00
|
|
|
else
|
2003-06-18 15:46:20 -04:00
|
|
|
args = ' %d %i %s %v %P %S %V %W'
|
|
|
|
@id =
|
|
|
|
install_cmd(proc{|*arg|
|
2003-08-03 18:07:47 -04:00
|
|
|
TkUtil.eval_cmd(proc{|*v| (cmd.call(*v))? '1': '0'},
|
|
|
|
ValidateArgs.new(*ValidateArgs \
|
|
|
|
.scan_args(args,arg)))
|
2003-06-18 15:46:20 -04:00
|
|
|
}) + args
|
2000-11-27 04:23:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_eval
|
|
|
|
@id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-02-28 01:53:33 -05:00
|
|
|
def create_self(keys)
|
2003-08-03 18:07:47 -04:00
|
|
|
tk_call 'entry', @path
|
2002-02-28 01:53:33 -05:00
|
|
|
if keys and keys != None
|
2003-08-03 18:07:47 -04:00
|
|
|
configure(keys)
|
2002-02-28 01:53:33 -05:00
|
|
|
end
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :create_self
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2000-11-27 04:23:38 -05:00
|
|
|
def configure(slot, value=None)
|
|
|
|
if slot.kind_of? Hash
|
* tkfont.rb: Fix bugs on TkFont.init_widget_font for Tk8.x.
* tkafter.rb: Add self to 1st argument of interval- and loop-proc
TkAfter#current_interval returns an interval (sleep) time value
TkAfter#current_args returns an array of arguments
TkAfter#return_value returns a return value of last loop-proc
e.g.
TkAfter.new(
proc{|obj| 500 - obj.current_interval}, 10,
[proc{|obj| p obj.current_args}, 'proc', 1],
proc{|obj| p obj.current_args; ['return', 2]},
[proc{|obj|
p obj.return_value
p ['proc', obj.current_args[0].call(obj.return_value[1],
obj.current_args[1])]},
proc{|*args| args[0] + args[1]}, 1],
proc{p ['proc', 4]} ).start(100)
* tk*.rb: Allow to use Symbols for parameters.
Allow new notation of constructor (also allow old notation).
e.g.
TkFrame.new('classname'=>'User'){|base|
pack
f = TkFrame.new(base, :classname=>'ButtonFrame').pack
TkButton.new(
:parent => f,
:text => 'Quit',
:command => proc{exit}
).pack(
:fill => :x,
:pady => 2
)
}
* tkcanvas.rb: (TkcItem) Add 'coords' parameter to the canvas item
constructor (for new notation of constructor).
e.g.
c = TkCanvas.new.pack
l = TkcLine.new(c, :coords=>[[0,0], [100,100]])
* tcltklib.c: New 'mainloop' and 'mainloop_watchdog'.
The priority of their event-loop can be controlled.
They accept an optional argument.
If it false, they don't exit although the root widget is destroyed.
This function is sometimes useful, if it is used with 'restart'.
'mainloop' can't treat Thread#join/value in a callback routine.
(e.g. TkButton.new(:command=>proc{p Thread.new{button.invoke}.value}) )
'mainloop_watchdog' can treat them, but watchdog thread is always running
(so, a little heavier than 'mainloop').
If the purpose of using Thread#join/value is to do something under some
safe-level, please use Proc object.
(e.g. :command=>proc{$SAFE=1;proc{$SAFE=2;button.invoke}.call;p $SAFE})
* tk.rb: Support functions of new 'mainloop' and 'mainloop_watchdog'.
* tk.rb: (Tk.restart) Add 'app-name' paramater and 'use' parameter.
'app-name' specifies the name and the resource class of the
application. If 'app-name' is specified to 'xxx', the application
class on the resource database is set to 'Xxx' and the application
name is changed by the same rule of Tk.appname method. 'use'
specifies the main window for embedding the root widget instead of
generating a new window.
* tk.rb: Add new parameter 'widgetname' to the widget constructor to
support effective use of Resource Database. For example, the
resource 'Xxx*quit.text: QUIT' can set the text of the button
generated by the following code.
e.g.
Tk.restart('Xxx')
TkButton.new(nil, 'widgetname'=>'quit', 'command'=>proc{exit}).pack
Tk.mainloop
* tk.rb: TkOption::get always returns a tainted string.
Add TkOption::new_proc_class.
It generates a class to import procedures defined on the resource
database. For example, there is a following resource file.
----< resource-test >------------
*CMD.foo: {|*args| p [$SAFE, :foo, args]}
*CMD.XXX.bar: {|*args| p [$SAFE, :bar, args]}
*Button.command: ruby {p self; p $SAFE; TkOption::CMD::XXX.bar(1,2,3)}
---------------------------------
The following code is a sample of use of the resource file.
e.g.
require 'tk'
TkOption.readfile 'resource-test'
p TkOption.new_proc_class(:CMD, [:foo], 1)
p TkOption.new_proc_class(:XXX, [:bar], 2, false, TkOption::CMD)
TkButton.new(:text=>'test').pack
Tk.mainloop
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2515 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-06-04 03:03:33 -04:00
|
|
|
slot = _symbolkey2str(slot)
|
2000-11-27 04:23:38 -05:00
|
|
|
if slot['vcmd'].kind_of? Array
|
|
|
|
cmd, *args = slot['vcmd']
|
|
|
|
slot['vcmd'] = ValidateCmd.new(cmd, args.join(' '))
|
|
|
|
elsif slot['vcmd'].kind_of? Proc
|
|
|
|
slot['vcmd'] = ValidateCmd.new(slot['vcmd'])
|
|
|
|
end
|
|
|
|
if slot['validatecommand'].kind_of? Array
|
|
|
|
cmd, *args = slot['validatecommand']
|
|
|
|
slot['validatecommand'] = ValidateCmd.new(cmd, args.join(' '))
|
|
|
|
elsif slot['validatecommand'].kind_of? Proc
|
|
|
|
slot['validatecommand'] = ValidateCmd.new(slot['validatecommand'])
|
|
|
|
end
|
|
|
|
if slot['invcmd'].kind_of? Array
|
|
|
|
cmd, *args = slot['invcmd']
|
|
|
|
slot['invcmd'] = ValidateCmd.new(cmd, args.join(' '))
|
|
|
|
elsif slot['invcmd'].kind_of? Proc
|
|
|
|
slot['invcmd'] = ValidateCmd.new(slot['invcmd'])
|
|
|
|
end
|
|
|
|
if slot['invalidcommand'].kind_of? Array
|
|
|
|
cmd, *args = slot['invalidcommand']
|
|
|
|
slot['invalidcommand'] = ValidateCmd.new(cmd, args.join(' '))
|
|
|
|
elsif slot['invalidcommand'].kind_of? Proc
|
|
|
|
slot['invalidcommand'] = ValidateCmd.new(slot['invalidcommand'])
|
|
|
|
end
|
|
|
|
super(slot)
|
|
|
|
else
|
* tkfont.rb: Fix bugs on TkFont.init_widget_font for Tk8.x.
* tkafter.rb: Add self to 1st argument of interval- and loop-proc
TkAfter#current_interval returns an interval (sleep) time value
TkAfter#current_args returns an array of arguments
TkAfter#return_value returns a return value of last loop-proc
e.g.
TkAfter.new(
proc{|obj| 500 - obj.current_interval}, 10,
[proc{|obj| p obj.current_args}, 'proc', 1],
proc{|obj| p obj.current_args; ['return', 2]},
[proc{|obj|
p obj.return_value
p ['proc', obj.current_args[0].call(obj.return_value[1],
obj.current_args[1])]},
proc{|*args| args[0] + args[1]}, 1],
proc{p ['proc', 4]} ).start(100)
* tk*.rb: Allow to use Symbols for parameters.
Allow new notation of constructor (also allow old notation).
e.g.
TkFrame.new('classname'=>'User'){|base|
pack
f = TkFrame.new(base, :classname=>'ButtonFrame').pack
TkButton.new(
:parent => f,
:text => 'Quit',
:command => proc{exit}
).pack(
:fill => :x,
:pady => 2
)
}
* tkcanvas.rb: (TkcItem) Add 'coords' parameter to the canvas item
constructor (for new notation of constructor).
e.g.
c = TkCanvas.new.pack
l = TkcLine.new(c, :coords=>[[0,0], [100,100]])
* tcltklib.c: New 'mainloop' and 'mainloop_watchdog'.
The priority of their event-loop can be controlled.
They accept an optional argument.
If it false, they don't exit although the root widget is destroyed.
This function is sometimes useful, if it is used with 'restart'.
'mainloop' can't treat Thread#join/value in a callback routine.
(e.g. TkButton.new(:command=>proc{p Thread.new{button.invoke}.value}) )
'mainloop_watchdog' can treat them, but watchdog thread is always running
(so, a little heavier than 'mainloop').
If the purpose of using Thread#join/value is to do something under some
safe-level, please use Proc object.
(e.g. :command=>proc{$SAFE=1;proc{$SAFE=2;button.invoke}.call;p $SAFE})
* tk.rb: Support functions of new 'mainloop' and 'mainloop_watchdog'.
* tk.rb: (Tk.restart) Add 'app-name' paramater and 'use' parameter.
'app-name' specifies the name and the resource class of the
application. If 'app-name' is specified to 'xxx', the application
class on the resource database is set to 'Xxx' and the application
name is changed by the same rule of Tk.appname method. 'use'
specifies the main window for embedding the root widget instead of
generating a new window.
* tk.rb: Add new parameter 'widgetname' to the widget constructor to
support effective use of Resource Database. For example, the
resource 'Xxx*quit.text: QUIT' can set the text of the button
generated by the following code.
e.g.
Tk.restart('Xxx')
TkButton.new(nil, 'widgetname'=>'quit', 'command'=>proc{exit}).pack
Tk.mainloop
* tk.rb: TkOption::get always returns a tainted string.
Add TkOption::new_proc_class.
It generates a class to import procedures defined on the resource
database. For example, there is a following resource file.
----< resource-test >------------
*CMD.foo: {|*args| p [$SAFE, :foo, args]}
*CMD.XXX.bar: {|*args| p [$SAFE, :bar, args]}
*Button.command: ruby {p self; p $SAFE; TkOption::CMD::XXX.bar(1,2,3)}
---------------------------------
The following code is a sample of use of the resource file.
e.g.
require 'tk'
TkOption.readfile 'resource-test'
p TkOption.new_proc_class(:CMD, [:foo], 1)
p TkOption.new_proc_class(:XXX, [:bar], 2, false, TkOption::CMD)
TkButton.new(:text=>'test').pack
Tk.mainloop
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2515 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-06-04 03:03:33 -04:00
|
|
|
if (slot == 'vcmd' || slot == :vcmd ||
|
|
|
|
slot == 'validatecommand' || slot == :validatecommand ||
|
|
|
|
slot == 'invcmd' || slot == :invcmd ||
|
2002-06-04 03:48:30 -04:00
|
|
|
slot == 'invalidcommand' || slot == :invalidcommand)
|
2000-11-27 04:23:38 -05:00
|
|
|
if value.kind_of? Array
|
|
|
|
cmd, *args = value
|
|
|
|
value = ValidateCmd.new(cmd, args.join(' '))
|
|
|
|
elsif value.kind_of? Proc
|
|
|
|
value = ValidateCmd.new(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
super(slot, value)
|
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
2000-11-27 04:23:38 -05:00
|
|
|
end
|
|
|
|
|
2003-07-30 03:15:00 -04:00
|
|
|
def bbox(index)
|
|
|
|
list(tk_send('bbox', index))
|
|
|
|
end
|
1999-08-13 01:37:52 -04:00
|
|
|
def cursor
|
2003-06-18 15:46:20 -04:00
|
|
|
number(tk_send('index', 'insert'))
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def cursor=(index)
|
|
|
|
tk_send 'icursor', index
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def index(index)
|
|
|
|
number(tk_send('index', index))
|
|
|
|
end
|
|
|
|
def insert(pos,text)
|
|
|
|
tk_send 'insert', pos, text
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
2003-07-30 03:15:00 -04:00
|
|
|
def delete(first, last=None)
|
2003-08-02 01:04:30 -04:00
|
|
|
tk_send 'delete', first, last
|
2003-07-30 03:15:00 -04:00
|
|
|
self
|
|
|
|
end
|
1999-08-13 01:37:52 -04:00
|
|
|
def mark(pos)
|
|
|
|
tk_send 'scan', 'mark', pos
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def dragto(pos)
|
|
|
|
tk_send 'scan', 'dragto', pos
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def selection_adjust(index)
|
|
|
|
tk_send 'selection', 'adjust', index
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def selection_clear
|
1999-12-01 04:24:48 -05:00
|
|
|
tk_send 'selection', 'clear'
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def selection_from(index)
|
|
|
|
tk_send 'selection', 'from', index
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def selection_present()
|
2001-03-27 02:10:58 -05:00
|
|
|
bool(tk_send('selection', 'present'))
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def selection_range(s, e)
|
|
|
|
tk_send 'selection', 'range', s, e
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
def selection_to(index)
|
|
|
|
tk_send 'selection', 'to', index
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
|
2003-08-03 18:07:47 -04:00
|
|
|
def invoke_validate
|
|
|
|
bool(tk_send('validate'))
|
|
|
|
end
|
2000-11-20 02:31:55 -05:00
|
|
|
def validate(mode = nil)
|
|
|
|
if mode
|
|
|
|
configure 'validate', mode
|
|
|
|
else
|
2003-08-03 18:07:47 -04:00
|
|
|
invoke_validate
|
2000-06-12 03:48:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-03 18:07:47 -04:00
|
|
|
def validatecommand(cmd = Proc.new, args = nil)
|
2000-11-20 02:31:55 -05:00
|
|
|
if cmd.kind_of?(ValidateCmd)
|
|
|
|
configure('validatecommand', cmd)
|
2003-08-03 18:07:47 -04:00
|
|
|
elsif args
|
|
|
|
configure('validatecommand', [cmd, args])
|
2000-11-20 02:31:55 -05:00
|
|
|
else
|
2003-08-03 18:07:47 -04:00
|
|
|
configure('validatecommand', cmd)
|
2000-11-20 02:31:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias vcmd validatecommand
|
|
|
|
|
2003-08-03 18:07:47 -04:00
|
|
|
def invalidcommand(cmd = Proc.new, args = nil)
|
2000-11-20 02:31:55 -05:00
|
|
|
if cmd.kind_of?(ValidateCmd)
|
|
|
|
configure('invalidcommand', cmd)
|
2003-08-03 18:07:47 -04:00
|
|
|
elsif args
|
|
|
|
configure('invalidcommand', [cmd, args])
|
2000-11-20 02:31:55 -05:00
|
|
|
else
|
2003-08-03 18:07:47 -04:00
|
|
|
configure('invalidcommand', cmd)
|
2000-11-20 02:31:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias invcmd invalidcommand
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
def value
|
|
|
|
tk_send 'get'
|
|
|
|
end
|
|
|
|
def value= (val)
|
|
|
|
tk_send 'delete', 0, 'end'
|
|
|
|
tk_send 'insert', 0, val
|
|
|
|
end
|
|
|
|
end
|
2000-11-20 02:31:55 -05:00
|
|
|
|
|
|
|
class TkSpinbox<TkEntry
|
2003-07-23 12:07:35 -04:00
|
|
|
TkCommandNames = ['spinbox'.freeze].freeze
|
2000-11-20 02:31:55 -05:00
|
|
|
WidgetClassName = 'Spinbox'.freeze
|
|
|
|
WidgetClassNames[WidgetClassName] = self
|
|
|
|
|
2002-02-28 01:53:33 -05:00
|
|
|
def create_self(keys)
|
2003-08-03 18:07:47 -04:00
|
|
|
tk_call 'spinbox', @path
|
2002-02-28 01:53:33 -05:00
|
|
|
if keys and keys != None
|
2003-08-03 18:07:47 -04:00
|
|
|
configure(keys)
|
2002-02-28 01:53:33 -05:00
|
|
|
end
|
2000-11-20 02:31:55 -05:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :create_self
|
2000-11-20 02:31:55 -05:00
|
|
|
|
|
|
|
def identify(x, y)
|
|
|
|
tk_send 'identify', x, y
|
|
|
|
end
|
|
|
|
|
|
|
|
def spinup
|
|
|
|
tk_send 'invoke', 'spinup'
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
2000-11-20 02:31:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def spindown
|
|
|
|
tk_send 'invoke', 'spindown'
|
2003-06-18 15:46:20 -04:00
|
|
|
self
|
2000-11-20 02:31:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set(str)
|
|
|
|
tk_send 'set', str
|
|
|
|
end
|
|
|
|
end
|