forked from Kentzo/ShortcutRecorder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSRCommon.h
130 lines (105 loc) · 3.72 KB
/
SRCommon.h
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
//
// SRCommon.h
// ShortcutRecorder
//
// Copyright 2006-2012 Contributors. All rights reserved.
//
// License: BSD
//
// Contributors:
// David Dauer
// Jesper
// Jamie Kirkpatrick
// Andy Kim
// Ilya Kulakov
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
/*!
@brief Mask representing subset of Cocoa modifier flags suitable for shortcuts.
*/
static const NSUInteger SRCocoaModifierFlagsMask = NSEventModifierFlagCommand | NSEventModifierFlagOption | NSEventModifierFlagShift | NSEventModifierFlagControl;
/*!
@brief Mask representing subset of Carbon modifier flags suitable for shortcuts.
*/
static const NSUInteger SRCarbonModifierFlagsMask = cmdKey | optionKey | shiftKey | controlKey;
/*!
@brief Bundle identifier for ShortcutRecorder library
*/
static NSString* SRBundleIdentifier = @"com.kulakov.ShortcutRecorder";
/*!
@brief Converts carbon modifier flags to cocoa.
*/
FOUNDATION_STATIC_INLINE NSUInteger SRCarbonToCocoaFlags(UInt32 aCarbonFlags)
{
NSUInteger cocoaFlags = 0;
if (aCarbonFlags & cmdKey)
cocoaFlags |= NSEventModifierFlagCommand;
if (aCarbonFlags & optionKey)
cocoaFlags |= NSEventModifierFlagOption;
if (aCarbonFlags & controlKey)
cocoaFlags |= NSEventModifierFlagControl;
if (aCarbonFlags & shiftKey)
cocoaFlags |= NSEventModifierFlagShift;
return cocoaFlags;
}
/*!
@brief Converts cocoa modifier flags to carbon.
*/
FOUNDATION_STATIC_INLINE UInt32 SRCocoaToCarbonFlags(NSUInteger aCocoaFlags)
{
UInt32 carbonFlags = 0;
if (aCocoaFlags & NSEventModifierFlagCommand)
carbonFlags |= cmdKey;
if (aCocoaFlags & NSEventModifierFlagOption)
carbonFlags |= optionKey;
if (aCocoaFlags & NSEventModifierFlagControl)
carbonFlags |= controlKey;
if (aCocoaFlags & NSEventModifierFlagShift)
carbonFlags |= shiftKey;
return carbonFlags;
}
/*!
@brief Convenient method to get localized string from the framework bundle.
*/
FOUNDATION_STATIC_INLINE NSString *SRLoc(NSString *aKey)
{
#ifdef SRLib
NSString* result = NSLocalizedStringFromTable(aKey,
@"ShortcutRecorder",
nil);
NSCParameterAssert(result);
return result;
#else
return NSLocalizedStringFromTableInBundle(aKey,
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:@"com.kulakov.ShortcutRecorder"],
nil);
#endif
}
/*!
@brief Convenient method to get image from the framework bundle.
*/
FOUNDATION_STATIC_INLINE NSImage *SRImage(NSString *anImageName)
{
#ifdef SRLib
NSImage* image = [[NSBundle mainBundle] imageForResource:anImageName];
NSCParameterAssert(image);
return image;
#else
NSBundle *b = [NSBundle bundleWithIdentifier:@"com.kulakov.ShortcutRecorder"];
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6)
return [[NSImage alloc] initByReferencingURL:[b URLForImageResource:anImageName]];
else
return [b imageForResource:anImageName];
#endif
}
/*!
@brief Returns string representation of shortcut with modifier flags replaced with their localized
readable equivalents (e.g. ? -> Option).
*/
NSString *SRReadableStringForCocoaModifierFlagsAndKeyCode(NSUInteger aModifierFlags, unsigned short aKeyCode);
/*!
@brief Returns string representation of shortcut with modifier flags replaced with their localized
readable equivalents (e.g. ? -> Option) and ASCII character for key code.
*/
NSString *SRReadableASCIIStringForCocoaModifierFlagsAndKeyCode(NSUInteger aModifierFlags, unsigned short aKeyCode);