1/*
2    PPKeyCancellableWindow.m
3
4    Copyright 2013-2018 Josh Freeman
5    http://www.twilightedge.com
6
7    This file is part of PikoPixel for Mac OS X and GNUstep.
8    PikoPixel is a graphical application for drawing & editing pixel-art images.
9
10    PikoPixel is free software: you can redistribute it and/or modify it under
11    the terms of the GNU Affero General Public License as published by the
12    Free Software Foundation, either version 3 of the License, or (at your
13    option) any later version approved for PikoPixel by its copyright holder (or
14    an authorized proxy).
15
16    PikoPixel is distributed in the hope that it will be useful, but WITHOUT ANY
17    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18    FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19    details.
20
21    You should have received a copy of the GNU Affero General Public License
22    along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#import "PPKeyCancellableWindow.h"
26
27#import "PPKeyConstants.h"
28#import "NSObject_PPUtilities.h"
29
30
31#define kHiddenButtonFrame              NSMakeRect(-1,-1,1,1)
32
33
34@interface PPKeyCancellableWindow (PrivateMethods)
35
36- (void) addHiddenButtonWithKeyEquivalent: (NSString *) keyEquivalent
37                        modifierMask: (unsigned int) modifierMask
38                        actionSelector: (SEL) actionSelector;
39
40- (void) performCancelButtonClick: (id) sender;
41- (void) performCancelButtonClickAndTerminate: (id) sender;
42
43@end
44
45@implementation PPKeyCancellableWindow
46
47#pragma mark NSWindow overrides
48
49- (void) awakeFromNib
50{
51    // check before calling [super awakeFromNib] - before 10.6, some classes didn't implement it
52    if ([[PPKeyCancellableWindow superclass] instancesRespondToSelector:
53                                                                    @selector(awakeFromNib)])
54    {
55        [super awakeFromNib];
56    }
57
58    [self addHiddenButtonWithKeyEquivalent: @"w"
59            modifierMask: NSCommandKeyMask
60            actionSelector: @selector(performCancelButtonClick:)];
61
62    [self addHiddenButtonWithKeyEquivalent: @"."
63            modifierMask: NSCommandKeyMask
64            actionSelector: @selector(performCancelButtonClick:)];
65
66    [self addHiddenButtonWithKeyEquivalent: kEscKey
67            modifierMask: 0
68            actionSelector: @selector(performCancelButtonClick:)];
69
70    [self addHiddenButtonWithKeyEquivalent: @"q"
71            modifierMask: NSCommandKeyMask
72            actionSelector: @selector(performCancelButtonClickAndTerminate:)];
73}
74
75#pragma mark Private methods
76
77- (void) addHiddenButtonWithKeyEquivalent: (NSString *) keyEquivalent
78                        modifierMask: (unsigned int) modifierMask
79                        actionSelector: (SEL) actionSelector
80{
81    NSButton *button;
82
83    if (![keyEquivalent length] || !actionSelector)
84    {
85        goto ERROR;
86    }
87
88    button = [[[NSButton alloc] initWithFrame: kHiddenButtonFrame] autorelease];
89
90    if (!button)
91        goto ERROR;
92
93    [button setKeyEquivalentModifierMask: modifierMask];
94    [button setKeyEquivalent: keyEquivalent];
95
96    [button setTarget: self];
97    [button setAction: actionSelector];
98
99    [[self contentView] addSubview: button];
100
101    return;
102
103ERROR:
104    return;
105}
106
107- (void) performCancelButtonClick: (id) sender
108{
109    [_cancelButton performClick: self];
110}
111
112- (void) performCancelButtonClickAndTerminate: (id) sender
113{
114    [self performCancelButtonClick: self];
115
116    [NSApp ppPerformSelectorFromNewStackFrame: @selector(terminate:)];
117}
118
119@end
120