Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

RCF: better shortcut facilities #310

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Gtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ using .GConstants

include("windows.jl")
include("gl_area.jl")
include("shortcuts.jl")

# Alternative Interface (`using Gtk.ShortNames`)
module ShortNames
Expand Down
3 changes: 3 additions & 0 deletions src/basic_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export add_events, signal_emit,
on_signal_destroy, on_signal_button_press,
on_signal_button_release, on_signal_motion

#Shortcuts
export Shortcut, doing

# Gdk info and manipulation
export screen_size

Expand Down
72 changes: 72 additions & 0 deletions src/shortcuts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
get_default_mod_mask() = ccall((:gtk_accelerator_get_default_mod_mask , libgtk),
typeof(GdkModifierType.CONTROL),())

@static if is_apple()
const PrimaryModifier = GdkModifierType.MOD2 #command key
const SecondaryModifer = GdkModifierType.CONTROL
end
@static if is_windows()
const PrimaryModifier = GdkModifierType.CONTROL
const SecondaryModifer = GdkModifierType.MOD1 #alt key
end
@static if is_linux()
const PrimaryModifier = GdkModifierType.CONTROL
const SecondaryModifer = GdkModifierType.MOD1
end
const NoModifier = Base.zero(UInt32)

"""
Represents a combination of keys.

##Examples:

Shortcut(GdkKeySyms.Tab) # Tab key

Shortcut("c") # c key

Shortcut("c",PrimaryModifier) # Ctrl-c (Windows & Linux) or Command-c (OS X)

Shortcut("C",PrimaryModifier + GdkModifierType.SHIFT) # Ctrl-Shit-c (notice the capital C)

"""
immutable Shortcut
keyval::UInt32
state::UInt32

Shortcut(k::Integer,s::Integer) = new(k,s)
Shortcut(k::Integer) = new(k,NoModifier)
Shortcut(k::AbstractString) = new(keyval(k),NoModifier)
Shortcut(k::AbstractString,s::Integer) = new(keyval(k),s)
end

"""
doing(s::Shortcut, event::GdkEvent)

Test wether the `GdkEvent` corresponds to the given `Shortcut`.

##Example:

if doing(Shortcut("c",PrimaryModifier),event)
#copy...
end

Reference : https://developer.gnome.org/gtk3/unstable/checklist-modifiers.html

"""
function doing(s::Shortcut, event::GdkEvent)

mod = get_default_mod_mask()
#on os x, the command key is also the meta key
@static if is_apple()
if s.state == NoModifier && event.state == NoModifier
return event.keyval == s.keyval
end
if (event.keyval == s.keyval) && (event.state & mod == s.state)
return true
end
return (event.keyval == s.keyval) &&
(event.state & mod == s.state + GdkModifierType.META)
end

return (event.keyval == s.keyval) && (event.state & mod == s.state)
end
14 changes: 14 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ destroy(win)

@test isa(Gtk.GdkEventKey(), Gtk.GdkEventKey)

# Shortcuts

@test Shortcut("c").keyval == keyval("c")
@test Shortcut("c",GConstants.GdkModifierType.CONTROL).state == GConstants.GdkModifierType.CONTROL

win = GtkWindow()
event = Gtk.GdkEventKey(GdkEventType.KEY_PRESS, Gtk.gdk_window(win),
Int8(0), UInt32(0), UInt32(0), Gtk.GdkKeySyms.Return, UInt32(0),
convert(Ptr{UInt8},C_NULL), UInt16(13), UInt8(0), UInt32(0) )

@test doing(Shortcut(Gtk.GdkKeySyms.Return),event) == true
@test doing(Shortcut(Gtk.GdkKeySyms.Return,Gtk.PrimaryModifier),event) == false
destroy(win)

end