1999-08-13 01:37:52 -04:00
|
|
|
require "tk"
|
|
|
|
|
2003-06-18 15:46:20 -04:00
|
|
|
class TkDialog2 < TkWindow
|
1999-08-13 01:37:52 -04:00
|
|
|
extend Tk
|
|
|
|
|
2003-07-23 12:07:35 -04:00
|
|
|
TkCommandNames = ['tk_dialog'.freeze].freeze
|
|
|
|
|
2003-06-18 15:46:20 -04:00
|
|
|
def self.show(*args)
|
|
|
|
dlog = self.new(*args)
|
|
|
|
dlog.show
|
|
|
|
dlog
|
|
|
|
end
|
|
|
|
|
|
|
|
def _set_button_config(configs)
|
|
|
|
set_config = proc{|c,i|
|
|
|
|
if $VERBOSE && (c.has_key?('command') || c.has_key?(:command))
|
|
|
|
STDERR.print("Warning: cannot give a command option " +
|
|
|
|
"to the dialog button#{i}. It was removed.\n")
|
|
|
|
end
|
|
|
|
c.delete('command'); c.delete(:command)
|
|
|
|
@config << format("%s.button%s configure %s; ",
|
|
|
|
@path, i, hash_kv(c).join(' '))
|
|
|
|
}
|
|
|
|
case configs
|
|
|
|
when Proc
|
|
|
|
@buttons.each_index{|i|
|
|
|
|
if (c = configs.call(i)).kind_of? Hash
|
|
|
|
set_config.call(c,i)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
when Array
|
|
|
|
@buttons.each_index{|i|
|
|
|
|
if (c = configs[i]).kind_of? Hash
|
|
|
|
set_config.call(c,i)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
when Hash
|
|
|
|
@buttons.each_with_index{|s,i|
|
|
|
|
if (c = configs[s]).kind_of? Hash
|
|
|
|
set_config.call(c,i)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
@config = 'after idle {' + @config + '};' if @config != ""
|
|
|
|
end
|
|
|
|
private :_set_button_config
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
# initialize tk_dialog
|
|
|
|
def initialize(keys = nil)
|
|
|
|
super()
|
2003-06-18 15:46:20 -04:00
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
@var = TkVariable.new
|
|
|
|
|
|
|
|
@title = title
|
|
|
|
|
|
|
|
@message = message
|
|
|
|
@message_config = message_config
|
|
|
|
|
|
|
|
@bitmap = bitmap
|
|
|
|
@bitmap_config = message_config
|
|
|
|
|
|
|
|
@default_button = default_button
|
|
|
|
|
|
|
|
@buttons = buttons
|
2003-06-18 15:46:20 -04:00
|
|
|
@button_configs = proc{|num| button_configs(num)}
|
|
|
|
|
|
|
|
#@config = "puts [winfo children .w0000];"
|
|
|
|
@config = ""
|
1999-08-13 01:37:52 -04:00
|
|
|
|
|
|
|
if keys.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
|
|
|
keys = _symbolkey2str(keys)
|
2003-06-18 15:46:20 -04:00
|
|
|
@title = keys['title'] if keys.key? 'title'
|
|
|
|
@message = keys['message'] if keys.key? 'message'
|
|
|
|
@bitmap = keys['bitmap'] if keys.key? 'bitmap'
|
|
|
|
@default_button = keys['default'] if keys.key? 'default'
|
|
|
|
@buttons = keys['buttons'] if keys.key? 'buttons'
|
1999-08-13 01:37:52 -04:00
|
|
|
|
|
|
|
@command = keys['prev_command']
|
|
|
|
|
2003-06-18 15:46:20 -04:00
|
|
|
@message_config = keys['message_config'] if keys.key? 'message_config'
|
|
|
|
@msgframe_config = keys['msgframe_config'] if keys.key? 'msgframe_config'
|
|
|
|
@bitmap_config = keys['bitmap_config'] if keys.key? 'bitmap_config'
|
|
|
|
@button_configs = keys['button_configs'] if keys.key? 'button_configs'
|
|
|
|
@btnframe_config = keys['btnframe_config'] if keys.key? 'btnframe_config'
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if @title.include? ?\s
|
|
|
|
@title = '{' + @title + '}'
|
|
|
|
end
|
|
|
|
|
2003-06-18 15:46:20 -04:00
|
|
|
if @buttons.kind_of? Array
|
|
|
|
_set_button_config(@buttons.collect{|cfg|
|
|
|
|
(cfg.kind_of? Array)? cfg[1]: nil})
|
|
|
|
@buttons = @buttons.collect{|cfg| (cfg.kind_of? Array)? cfg[0]: cfg}
|
|
|
|
end
|
|
|
|
if @buttons.kind_of? Hash
|
|
|
|
_set_button_config(@buttons)
|
|
|
|
@buttons = @buttons.keys
|
|
|
|
end
|
|
|
|
@buttons = tk_split_simplelist(@buttons) if @buttons.kind_of? String
|
1999-08-13 01:37:52 -04:00
|
|
|
@buttons = @buttons.collect{|s|
|
|
|
|
if s.kind_of? Array
|
|
|
|
s = s.join(' ')
|
|
|
|
end
|
|
|
|
if s.include? ?\s
|
|
|
|
'{' + s + '}'
|
|
|
|
else
|
|
|
|
s
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if @message_config.kind_of? Hash
|
2003-06-18 15:46:20 -04:00
|
|
|
@config << format("%s.msg configure %s;",
|
1999-08-13 01:37:52 -04:00
|
|
|
@path, hash_kv(@message_config).join(' '))
|
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
|
|
|
|
if @msgframe_config.kind_of? Hash
|
|
|
|
@config << format("%s.top configure %s;",
|
|
|
|
@path, hash_kv(@msgframe_config).join(' '))
|
|
|
|
end
|
|
|
|
|
|
|
|
if @btnframe_config.kind_of? Hash
|
|
|
|
@config << format("%s.bot configure %s;",
|
|
|
|
@path, hash_kv(@btnframe_config).join(' '))
|
|
|
|
end
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
if @bitmap_config.kind_of? Hash
|
2003-06-18 15:46:20 -04:00
|
|
|
@config << format("%s.bitmap configure %s;",
|
1999-08-13 01:37:52 -04:00
|
|
|
@path, hash_kv(@bitmap_config).join(' '))
|
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
|
|
|
|
_set_button_config(@button_configs) if @button_configs
|
1999-08-13 01:37:52 -04:00
|
|
|
|
|
|
|
if @command.kind_of? Proc
|
|
|
|
@command.call(self)
|
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
end
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2003-06-18 15:46:20 -04:00
|
|
|
def show
|
|
|
|
if @default_button.kind_of? String
|
|
|
|
default_button = @buttons.index(@default_button)
|
|
|
|
else
|
|
|
|
default_button = @default_button
|
|
|
|
end
|
|
|
|
default_button = '{}' if default_button == nil
|
2003-06-21 04:47:22 -04:00
|
|
|
Tk.ip_eval('eval {global '+@var.id+';'+@config+
|
|
|
|
'set '+@var.id+' [tk_dialog '+
|
|
|
|
@path+" "+@title+" {#{@message}} "+@bitmap+" "+
|
|
|
|
String(default_button)+" "+@buttons.join(' ')+']}')
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
def value
|
|
|
|
return @var.value.to_i
|
|
|
|
end
|
|
|
|
######################################################
|
|
|
|
# #
|
|
|
|
# these methods must be overridden for each dialog #
|
|
|
|
# #
|
|
|
|
######################################################
|
2003-06-18 15:46:20 -04:00
|
|
|
private
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
def title
|
2003-06-18 15:46:20 -04:00
|
|
|
# returns a title string of the dialog window
|
1999-08-13 01:37:52 -04:00
|
|
|
return "DIALOG"
|
|
|
|
end
|
|
|
|
def message
|
2003-06-18 15:46:20 -04:00
|
|
|
# returns a message text to display on the dialog
|
1999-08-13 01:37:52 -04:00
|
|
|
return "MESSAGE"
|
|
|
|
end
|
|
|
|
def message_config
|
2003-06-18 15:46:20 -04:00
|
|
|
# returns a Hash {option=>value, ...} for the message text
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
def msgframe_config
|
|
|
|
# returns a Hash {option=>value, ...} for the message text frame
|
1999-08-13 01:37:52 -04:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
def bitmap
|
2003-06-18 15:46:20 -04:00
|
|
|
# returns a bitmap name or a bitmap file path
|
|
|
|
# (@ + path ; e.g. '@/usr/share/bitmap/sample.xbm')
|
1999-08-13 01:37:52 -04:00
|
|
|
return "info"
|
|
|
|
end
|
|
|
|
def bitmap_config
|
2003-06-18 15:46:20 -04:00
|
|
|
# returns nil or a Hash {option=>value, ...} for the bitmap
|
1999-08-13 01:37:52 -04:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
def default_button
|
2003-06-18 15:46:20 -04:00
|
|
|
# returns a default button's number or name
|
|
|
|
# if nil or null string, set no-default
|
1999-08-13 01:37:52 -04:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
def buttons
|
|
|
|
#return "BUTTON1 BUTTON2"
|
|
|
|
return ["BUTTON1", "BUTTON2"]
|
|
|
|
end
|
|
|
|
def button_configs(num)
|
2003-06-18 15:46:20 -04:00
|
|
|
# returns nil / Proc / Array or Hash (see _set_button_config)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
def btnframe_config
|
|
|
|
# returns nil or a Hash {option=>value, ...} for the button frame
|
1999-08-13 01:37:52 -04:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-06-18 15:46:20 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# TkDialog : with showing at initialize
|
|
|
|
#
|
|
|
|
class TkDialog < TkDialog2
|
|
|
|
def self.show(*args)
|
|
|
|
self.new(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
super(*args)
|
|
|
|
show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
#
|
|
|
|
# dialog for warning
|
|
|
|
#
|
2003-06-18 15:46:20 -04:00
|
|
|
class TkWarning2 < TkDialog2
|
1999-08-13 01:37:52 -04:00
|
|
|
def initialize(mes)
|
2003-06-18 15:46:20 -04:00
|
|
|
super(:message=>mes)
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
|
|
|
|
def show(mes = nil)
|
|
|
|
mes_bup = @message
|
|
|
|
@message = mes if mes
|
|
|
|
ret = super()
|
|
|
|
@message = mes_bup
|
|
|
|
ret
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
|
|
|
|
#######
|
|
|
|
private
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
def title
|
|
|
|
return "WARNING";
|
|
|
|
end
|
|
|
|
def bitmap
|
|
|
|
return "warning";
|
|
|
|
end
|
|
|
|
def default_button
|
|
|
|
return 0;
|
|
|
|
end
|
|
|
|
def buttons
|
|
|
|
return "OK";
|
|
|
|
end
|
|
|
|
end
|
2003-06-18 15:46:20 -04:00
|
|
|
|
|
|
|
class TkWarning < TkWarning2
|
|
|
|
def self.show(*args)
|
|
|
|
self.new(*args)
|
|
|
|
end
|
|
|
|
def initialize(mes)
|
|
|
|
super(mes)
|
|
|
|
show
|
|
|
|
end
|
|
|
|
end
|