Skip to content
Snippets Groups Projects
Commit 2ab667a9 authored by Lyle Johnson's avatar Lyle Johnson
Browse files

Added support for passing lambda functions into FXAccelTable#addAccel

parent 6e786fa8
No related branches found
No related tags found
No related merge requests found
module Fox
class FXAccelTable
alias addAccelOrig addAccel # :nodoc:
#
# Add an accelerator to the table. The _hotKey_ is a code returned
# by the Fox.fxparseAccel method. For example, to associate the
# Ctrl+S keypress with sending a "save" command to a document, you
# might use code like this:
#
# hotKey = fxparseAccel("Ctrl+S")
# accelTable.addAccel(hotKey, doc, FXSEL(SEL_COMMAND, MyDocument::ID_SAVE))
#
# A more straightforward way is to pass one or more callable objects in:
#
# accelTable.addAccel(hotKey, lambda { doc.save })
#
# or to trigger the event on the key "up" event,
#
# accelTable.addAccel(hotKey, nil, lambda { doc.save })
#
def addAccel(hotKey, *args)
tgt, seldn, selup = nil, 0, 0
if args.length > 0
if args[0].respond_to? :call
tgt = FXPseudoTarget.new
seldn = FXSEL(SEL_KEYPRESS, 0)
tgt.pconnect(SEL_KEYPRESS, args[0])
else
tgt = args[0]
if args.length > 1
if args[1].respond_to? :call
tgt = tgt || FXPseudoTarget.new
selup = FXSEL(SEL_KEYRELEASE, 0)
tgt.pconnect(SEL_KEYRELEASE, args[1])
else
seldn = args[1]
selup = args[2] if args.length > 2
end
end
end
end
addAccelOrig(hotKey, tgt, seldn, selup)
end
end
end
\ No newline at end of file
...@@ -129,3 +129,4 @@ require 'fox16/timeout' ...@@ -129,3 +129,4 @@ require 'fox16/timeout'
require 'fox16/chore' require 'fox16/chore'
require 'fox16/signal' require 'fox16/signal'
require 'fox16/input' require 'fox16/input'
require 'fox16/accel_table'
...@@ -11,24 +11,6 @@ module Fox ...@@ -11,24 +11,6 @@ module Fox
def initialize # :yields: acceleratorTable def initialize # :yields: acceleratorTable
end end
#
# Add an accelerator to the table. The _hotKey_ is a code returned
# by the Fox.fxparseAccel method. For example, to associate the
# Ctrl+S keypress with sending a "save" command to a document, you
# might use code like this:
#
# hotKey = fxparseAccel("Ctrl+S")
# accelTable.addAccel(hotKey, doc, FXSEL(SEL_COMMAND, MyDocument::ID_SAVE))
#
# ==== Parameters:
#
# +hotKey+:: the hotkey associated with this accelerator [Integer]
# +target+:: message target [FXObject]
# +seldn+:: selector for the +SEL_KEYPRESS+ event [Integer]
# +selup+:: selector for the +SEL_KEYRELEASE+ event [Integer]
#
def addAccel(hotKey, target=nil, seldn=0, selup=0) ; end
# #
# Remove an accelerator from the table. # Remove an accelerator from the table.
# #
......
...@@ -6,38 +6,52 @@ include Fox ...@@ -6,38 +6,52 @@ include Fox
class TC_FXAccelTable < Test::Unit::TestCase class TC_FXAccelTable < Test::Unit::TestCase
def setup def setup
@accel = FXAccelTable.new @accelTable = FXAccelTable.new
@hotKey = fxparseHotKey('&q')
end end
def test_addAccel def test_add_accel_with_nil_target
hotkey = fxparseHotKey('&A') @accelTable.addAccel(@hotKey)
target = FXObject.new assert @accelTable.hasAccel?(@hotKey)
seldn, selup = 0, 0 assert_nil @accelTable.targetOfAccel(@hotKey)
@accel.addAccel(hotkey)
@accel.addAccel(hotkey, target)
@accel.addAccel(hotkey, target, seldn)
@accel.addAccel(hotkey, target, seldn, selup)
end end
def test_hasAccel def test_add_accel_with_default_seldn_selup
hotkey = fxparseHotKey('&b') target = FXObject.new
assert(!@accel.hasAccel?(hotkey)) @accelTable.addAccel(@hotKey, target)
@accel.addAccel(hotkey) assert_same target, @accelTable.targetOfAccel(@hotKey)
assert(@accel.hasAccel?(hotkey)) end
def test_add_accel_with_default_selup
@accelTable.addAccel(@hotKey, FXObject.new, FXSEL(SEL_COMMAND, FXWindow::ID_SHOW))
end
def test_add_accel_with_no_defaults
@accelTable.addAccel(@hotKey, FXObject.new, FXSEL(SEL_COMMAND, FXWindow::ID_SHOW), FXSEL(SEL_COMMAND, FXWindow::ID_HIDE))
end end
def test_targetOfAccel def test_add_accel_with_lambda_for_seldn
hotkey = fxparseHotKey("&x") @accelTable.addAccel(@hotKey, lambda { puts "hello" })
target = FXObject.new end
@accel.addAccel(hotkey, target)
assert_same(target, @accel.targetOfAccel(hotkey)) def test_add_accel_with_lambda_for_selup
@accelTable.addAccel(@hotKey, nil, lambda { puts "goodbye" })
end
def test_add_accel_with_lambda_for_selup_and_seldn
@accelTable.addAccel(@hotKey, lambda { puts "hello" }, lambda { puts "goodbye" })
end
def test_has_accel
assert(!@accelTable.hasAccel?(@hotKey))
@accelTable.addAccel(@hotKey)
assert(@accelTable.hasAccel?(@hotKey))
end end
def test_removeAccel def test_remove_accel
hotkey = fxparseHotKey('&b') @accelTable.addAccel(@hotKey)
@accel.addAccel(hotkey) assert(@accelTable.hasAccel?(@hotKey))
assert(@accel.hasAccel?(hotkey)) @accelTable.removeAccel(@hotKey)
@accel.removeAccel(hotkey) assert(!@accelTable.hasAccel?(@hotKey))
assert(!@accel.hasAccel?(hotkey))
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment