/usr/lib/ruby/2.3.0/tk/event.rb is in ruby2.3-tcltk 2.3.0-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | # frozen_string_literal: false
#
# tk/event.rb - module for event
#
module TkEvent
end
########################
require 'tkutil'
require 'tk' unless Object.const_defined? :TkComm
########################
module TkEvent
class Event < TkUtil::CallbackSubst
module Grp
KEY = 0x1
BUTTON = 0x2
MOTION = 0x4
CROSSING = 0x8
FOCUS = 0x10
EXPOSE = 0x20
VISIBILITY = 0x40
CREATE = 0x80
DESTROY = 0x100
UNMAP = 0x200
MAP = 0x400
REPARENT = 0x800
CONFIG = 0x1000
GRAVITY = 0x2000
CIRC = 0x4000
PROP = 0x8000
COLORMAP = 0x10000
VIRTUAL = 0x20000
ACTIVATE = 0x40000
MAPREQ = 0x80000
CONFIGREQ = 0x100000
RESIZEREQ = 0x200000
CIRCREQ = 0x400000
MWHEEL = KEY
STRING_DATA = 0x80000000 # special flag for 'data' field
ALL = 0xFFFFFFFF
KEY_BUTTON_MOTION_VIRTUAL = (KEY|MWHEEL|BUTTON|MOTION|VIRTUAL)
KEY_BUTTON_MOTION_CROSSING = (KEY|MWHEEL|BUTTON|MOTION|CROSSING|VIRTUAL)
end
type_data = [
#-----+-------------------+------------------+-----------------------#
# ID | const | group_flag | context_name #
#-----+-------------------+------------------+-----------------------#
[ 2, :KeyPress, Grp::KEY, 'KeyPress', 'Key' ],
[ 3, :KeyRelease, Grp::KEY, 'KeyRelease' ],
[ 4, :ButtonPress, Grp::BUTTON, 'ButtonPress', 'Button' ],
[ 5, :ButtonRelease, Grp::BUTTON, 'ButtonRelease' ],
[ 6, :MotionNotify, Grp::MOTION, 'Motion' ],
[ 7, :EnterNotify, Grp::CROSSING, 'Enter' ],
[ 8, :LeaveNotify, Grp::CROSSING, 'Leave' ],
[ 9, :FocusIn, Grp::FOCUS, 'FocusIn' ],
[ 10, :FocusOut, Grp::FOCUS, 'FocusOut' ],
[ 11, :KeymapNotify, 0, ],
[ 12, :Expose, Grp::EXPOSE, 'Expose' ],
[ 13, :GraphicsExpose, Grp::EXPOSE, ],
[ 14, :NoExpose, 0, ],
[ 15, :VisibilityNotify, Grp::VISIBILITY, 'Visibility' ],
[ 16, :CreateNotify, Grp::CREATE, 'Create' ],
[ 17, :DestroyNotify, Grp::DESTROY, 'Destroy' ],
[ 18, :UnmapNotify, Grp::UNMAP, 'Unmap' ],
[ 19, :MapNotify, Grp::MAP, 'Map' ],
[ 20, :MapRequest, Grp::MAPREQ, 'MapRequest' ],
[ 21, :ReparentNotify, Grp::REPARENT, 'Reparent' ],
[ 22, :ConfigureNotify, Grp::CONFIG, 'Configure' ],
[ 23, :ConfigureRequest, Grp::CONFIGREQ, 'ConfigureRequest' ],
[ 24, :GravityNotify, Grp::GRAVITY, 'Gravity' ],
[ 25, :ResizeRequest, Grp::RESIZEREQ, 'ResizeRequest' ],
[ 26, :CirculateNotify, Grp::CIRC, 'Circulate' ],
[ 27, :CirculateRequest, 0, 'CirculateRequest' ],
[ 28, :PropertyNotify, Grp::PROP, 'Property' ],
[ 29, :SelectionClear, 0, ],
[ 30, :SelectionRequest, 0, ],
[ 31, :SelectionNotify, 0, ],
[ 32, :ColormapNotify, Grp::COLORMAP, 'Colormap' ],
[ 33, :ClientMessage, 0, ],
[ 34, :MappingNotify, 0, ],
[ 35, :VirtualEvent, Grp::VIRTUAL, ],
[ 36, :ActivateNotify, Grp::ACTIVATE, 'Activate' ],
[ 37, :DeactivateNotify, Grp::ACTIVATE, 'Deactivate' ],
[ 38, :MouseWheelEvent, Grp::MWHEEL, 'MouseWheel' ],
[ 39, :TK_LASTEVENT, 0, ]
]
module TypeNum
end
TYPE_NAME_TBL = Hash.new
TYPE_ID_TBL = Hash.new
TYPE_GROUP_TBL = Hash.new
type_data.each{|id, c_name, g_flag, *t_names|
TypeNum.const_set(c_name, id)
t_names.each{|t_name| t_name.freeze; TYPE_NAME_TBL[t_name] = id }
TYPE_ID_TBL[id] = t_names
TYPE_GROUP_TBL[id] = g_flag
}
TYPE_NAME_TBL.freeze
TYPE_ID_TBL.freeze
def self.type_id(name)
TYPE_NAME_TBL[name.to_s]
end
def self.type_name(id)
TYPE_ID_TBL[id] && TYPE_ID_TBL[id][0]
end
def self.group_flag(id)
TYPE_GROUP_TBL[id] || 0
end
#############################################
module StateMask
ShiftMask = (1<<0)
LockMask = (1<<1)
ControlMask = (1<<2)
Mod1Mask = (1<<3)
Mod2Mask = (1<<4)
Mod3Mask = (1<<5)
Mod4Mask = (1<<6)
Mod5Mask = (1<<7)
Button1Mask = (1<<8)
Button2Mask = (1<<9)
Button3Mask = (1<<10)
Button4Mask = (1<<11)
Button5Mask = (1<<12)
AnyModifier = (1<<15)
META_MASK = (AnyModifier<<1)
ALT_MASK = (AnyModifier<<2)
EXTENDED_MASK = (AnyModifier<<3)
CommandMask = Mod1Mask
OptionMask = Mod2Mask
end
#############################################
FIELD_FLAG = {
# key => flag
'above' => Grp::CONFIG,
'borderwidth' => (Grp::CREATE|Grp::CONFIG),
'button' => Grp::BUTTON,
'count' => Grp::EXPOSE,
'data' => (Grp::VIRTUAL|Grp::STRING_DATA),
'delta' => Grp::MWHEEL,
'detail' => (Grp::FOCUS|Grp::CROSSING),
'focus' => Grp::CROSSING,
'height' => (Grp::EXPOSE|Grp::CONFIG),
'keycode' => Grp::KEY,
'keysym' => Grp::KEY,
'mode' => (Grp::CROSSING|Grp::FOCUS),
'override' => (Grp::CREATE|Grp::MAP|Grp::REPARENT|Grp::CONFIG),
'place' => Grp::CIRC,
'root' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|Grp::CROSSING),
'rootx' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|Grp::CROSSING),
'rooty' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|Grp::CROSSING),
'sendevent' => Grp::ALL,
'serial' => Grp::ALL,
'state' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|
Grp::CROSSING|Grp::VISIBILITY),
'subwindow' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|Grp::CROSSING),
'time' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|Grp::CROSSING|
Grp::PROP),
'warp' => Grp::KEY_BUTTON_MOTION_VIRTUAL,
'width' => (Grp::EXPOSE|Grp::CREATE|Grp::CONFIG),
'window' => (Grp::CREATE|Grp::UNMAP|Grp::MAP|Grp::REPARENT|
Grp::CONFIG|Grp::GRAVITY|Grp::CIRC),
'when' => Grp::ALL,
'x' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|Grp::CROSSING|
Grp::EXPOSE|Grp::CREATE|Grp::CONFIG|Grp::GRAVITY|
Grp::REPARENT),
'y' => (Grp::KEY_BUTTON_MOTION_VIRTUAL|Grp::CROSSING|
Grp::EXPOSE|Grp::CREATE|Grp::CONFIG|Grp::GRAVITY|
Grp::REPARENT),
}
FIELD_OPERATION = {
'root' => proc{|val|
begin
Tk.tk_call_without_enc('winfo', 'pathname', val)
val
rescue
nil
end
},
'subwindow' => proc{|val|
begin
Tk.tk_call_without_enc('winfo', 'pathname', val)
val
rescue
nil
end
},
'window' => proc{|val| nil}
}
#-------------------------------------------
def valid_fields(group_flag=nil)
group_flag = self.class.group_flag(self.type) unless group_flag
fields = {}
FIELD_FLAG.each{|key, flag|
next if (flag & group_flag) == 0
begin
val = self.__send__(key)
rescue
next
end
# next if !val || val == '??'
next if !val || (val == '??' && (flag & Grp::STRING_DATA))
fields[key] = val
}
fields
end
def valid_for_generate(group_flag=nil)
fields = valid_fields(group_flag)
FIELD_OPERATION.each{|key, cmd|
next unless fields.has_key?(key)
val = FIELD_OPERATION[key].call(fields[key])
if val
fields[key] = val
else
fields.delete(key)
end
}
fields
end
def generate(win, modkeys={})
klass = self.class
if modkeys.has_key?(:type) || modkeys.has_key?('type')
modkeys = TkComm._symbolkey2str(modkeys)
type_id = modkeys.delete('type')
else
type_id = self.type
end
type_name = klass.type_name(type_id)
unless type_name
fail RuntimeError, "type_id #{type_id} is invalid"
end
group_flag = klass.group_flag(type_id)
opts = valid_for_generate(group_flag)
modkeys.each{|key, val|
if val
opts[key.to_s] = val
else
opts.delete(key.to_s)
end
}
if group_flag != Grp::KEY
Tk.event_generate(win, type_name, opts)
else
# If type is KEY event, focus should be set to target widget.
# If not set, original widget will get the same event.
# That will make infinite loop.
w = Tk.tk_call_without_enc('focus')
begin
Tk.tk_call_without_enc('focus', win)
Tk.event_generate(win, type_name, opts)
ensure
Tk.tk_call_without_enc('focus', w)
end
end
end
#############################################
# [ <'%' subst-key char>, <proc type char>, <instance var (accessor) name>]
KEY_TBL = [
[ ?#, ?n, :serial ],
[ ?a, ?s, :above ],
[ ?b, ?n, :num ],
[ ?c, ?n, :count ],
[ ?d, ?s, :detail ],
# ?e
[ ?f, ?b, :focus ],
# ?g
[ ?h, ?n, :height ],
[ ?i, ?s, :win_hex ],
# ?j
[ ?k, ?n, :keycode ],
# ?l
[ ?m, ?s, :mode ],
# ?n
[ ?o, ?b, :override ],
[ ?p, ?s, :place ],
# ?q
# ?r
[ ?s, ?x, :state ],
[ ?t, ?n, :time ],
# ?u
[ ?v, ?n, :value_mask ],
[ ?w, ?n, :width ],
[ ?x, ?n, :x ],
[ ?y, ?n, :y ],
# ?z
[ ?A, ?s, :char ],
[ ?B, ?n, :borderwidth ],
# ?C
[ ?D, ?n, :wheel_delta ],
[ ?E, ?b, :send_event ],
# ?F
# ?G
# ?H
# ?I
# ?J
[ ?K, ?s, :keysym ],
# ?L
# ?M
[ ?N, ?n, :keysym_num ],
# ?O
[ ?P, ?s, :property ],
# ?Q
[ ?R, ?s, :rootwin_id ],
[ ?S, ?s, :subwindow ],
[ ?T, ?n, :type ],
# ?U
# ?V
[ ?W, ?w, :widget ],
[ ?X, ?n, :x_root ],
[ ?Y, ?n, :y_root ],
# ?Z
nil
]
# [ <'%' subst-key str>, <proc type char>, <instance var (accessor) name>]
# the subst-key string will be converted to a bytecode (128+idx).
LONGKEY_TBL = [
# for example, for %CTT and %CST subst-key on tkdnd-2.0
# ['CTT', ?l, :drop_target_type],
# ['CST', ?l, :drop_source_type],
]
# [ <proc type char>, <proc/method to convert tcl-str to ruby-obj>]
PROC_TBL = [
[ ?n, TkComm.method(:num_or_str) ],
[ ?s, TkComm.method(:string) ],
[ ?b, TkComm.method(:bool) ],
[ ?w, TkComm.method(:window) ],
[ ?x, proc{|val|
begin
TkComm::number(val)
rescue ArgumentError
val
end
}
],
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
inf[1] = inf[1].getbyte(0) if inf[1].kind_of?(String)
end
inf
}
PROC_TBL.map!{|inf|
if inf.kind_of?(Array)
inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
end
inf
}
=end
# setup tables to be used by scan_args, _get_subst_key, _get_all_subst_keys
#
# _get_subst_key() and _get_all_subst_keys() generates key-string
# which describe how to convert callback arguments to ruby objects.
# When binding parameters are given, use _get_subst_key().
# But when no parameters are given, use _get_all_subst_keys() to
# create a Event class object as a callback parameter.
#
# scan_args() is used when doing callback. It convert arguments
# ( which are Tcl strings ) to ruby objects based on the key string
# that is generated by _get_subst_key() or _get_all_subst_keys().
#
_setup_subst_table(KEY_TBL, PROC_TBL)
# _setup_subst_table(KEY_TBL, LONGKEY_TBL, PROC_TBL) # if use longname-keys
#
# NOTE: The order of parameters which passed to callback procedure is
# <extra_arg>, <extra_arg>, ... , <subst_arg>, <subst_arg>, ...
#
# If you need support extra arguments given by Tcl/Tk,
# please override _get_extra_args_tbl
#
#def self._get_extra_args_tbl
# # return an array of convert procs
# []
#end
=begin
alias button num
alias delta wheel_delta
alias root rootwin_id
alias rootx x_root
alias root_x x_root
alias rooty y_root
alias root_y y_root
alias sendevent send_event
=end
ALIAS_TBL = {
:button => :num,
:data => :detail,
:delta => :wheel_delta,
:root => :rootwin_id,
:rootx => :x_root,
:root_x => :x_root,
:rooty => :y_root,
:root_y => :y_root,
:sendevent => :send_event,
:window => :widget
}
_define_attribute_aliases(ALIAS_TBL)
end
###############################################
def install_bind_for_event_class(klass, cmd, *args)
extra_args_tbl = klass._get_extra_args_tbl
if args.compact.size > 0
args.map!{|arg| klass._sym2subst(arg)}
args = args.join(' ')
keys = klass._get_subst_key(args)
if cmd.kind_of?(String)
id = cmd
elsif cmd.kind_of?(TkCallbackEntry)
id = install_cmd(cmd)
else
id = install_cmd(proc{|*arg|
ex_args = []
extra_args_tbl.reverse_each{|conv| ex_args << conv.call(arg.pop)}
begin
TkUtil.eval_cmd(cmd, *(ex_args.concat(klass.scan_args(keys, arg))))
rescue Exception=>e
if TkCore::INTERP.kind_of?(TclTkIp)
fail e
else
# MultiTkIp
fail Exception, "#{e.class}: #{e.message.dup}"
end
end
})
end
elsif cmd.respond_to?(:arity) && cmd.arity == 0 # args.size == 0
args = ''
if cmd.kind_of?(String)
id = cmd
elsif cmd.kind_of?(TkCallbackEntry)
id = install_cmd(cmd)
else
id = install_cmd(proc{
begin
TkUtil.eval_cmd(cmd)
rescue Exception=>e
if TkCore::INTERP.kind_of?(TclTkIp)
fail e
else
# MultiTkIp
fail Exception, "#{e.class}: #{e.message.dup}"
end
end
})
end
else
keys, args = klass._get_all_subst_keys
if cmd.kind_of?(String)
id = cmd
elsif cmd.kind_of?(TkCallbackEntry)
id = install_cmd(cmd)
else
id = install_cmd(proc{|*arg|
ex_args = []
extra_args_tbl.reverse_each{|conv| ex_args << conv.call(arg.pop)}
begin
TkUtil.eval_cmd(cmd, *(ex_args << klass.new(*klass.scan_args(keys, arg))))
rescue Exception=>e
if TkCore::INTERP.kind_of?(TclTkIp)
fail e
else
# MultiTkIp
fail Exception, "#{e.class}: #{e.message.dup}"
end
end
})
end
end
if TkCore::INTERP.kind_of?(TclTkIp)
id + ' ' + args
else
# MultiTkIp
"if {[set st [catch {#{id} #{args}} ret]] != 0} {
if {$st == 4} {
return -code continue $ret
} elseif {$st == 3} {
return -code break $ret
} elseif {$st == 2} {
return -code return $ret
} elseif {[regexp {^Exception: (TkCallbackContinue: .*)$} \
$ret m msg]} {
return -code continue $msg
} elseif {[regexp {^Exception: (TkCallbackBreak: .*)$} $ret m msg]} {
return -code break $msg
} elseif {[regexp {^Exception: (TkCallbackReturn: .*)$} $ret m msg]} {
return -code return $msg
} elseif {[regexp {^Exception: (\\S+: .*)$} $ret m msg]} {
return -code return $msg
} else {
return -code error $ret
}
} else {
set ret
}"
end
end
def install_bind(cmd, *args)
install_bind_for_event_class(TkEvent::Event, cmd, *args)
end
end
|