pcsxr-1.9.92
[pcsx_rearmed.git] / macosx / plugins / DFInput / macsrc / MappingCell.m
1 /*
2  * Copyright (c) 2010, Wei Mingzhi <whistler@openoffice.org>.
3  * All Rights Reserved.
4  *
5  * Based on: HIDInput by Gil Pedersen.
6  * Copyright (c) 2004, Gil Pedersen.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses>.
20  */
21
22 #import "MappingCell.h"
23 #import "ControllerList.h"
24 #import "cfg.h"
25
26 @implementation MappingCell
27
28 - (id)initTextCell:(NSString *)aString {
29         self = [super initTextCell:aString];
30         [self setEditable:NO];
31         return self;
32 }
33
34 - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
35 {
36         [super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
37
38         int whichPad = [ControllerList currentController];
39         NSTableView *tableView = (NSTableView *)[self controlView];
40         int i, changed = 0, row;
41         NSEvent *endEvent;
42         NSPoint where = {0.0, 0.0};
43
44         /* start a modal session */
45         NSModalSession session = [NSApp beginModalSessionForWindow:[tableView window]];
46         [NSApp runModalSession:session];
47
48         /* delay for a little while to allow user to release the button pressed to activate the element */
49         [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.15]];
50
51         InitAxisPos(whichPad);
52
53         /* wait for 10 seconds for user to press a key */
54         for (i = 0; i < 10; i++) {
55                 [NSApp runModalSession:session];
56                 row = [tableView selectedRow];
57                 if (row < DKEY_TOTAL) {
58                         changed = ReadDKeyEvent(whichPad, [ControllerList getButtonOfRow:row]);
59                 } else {
60                         row -= DKEY_TOTAL;
61                         changed = ReadAnalogEvent(whichPad, row / 4, row % 4);
62                 }
63
64                 if (changed) break;
65         }
66
67         [NSApp endModalSession:session];
68
69         /* move selection to the next list element */
70         [self endEditing:textObj];
71         if (changed == 1) {
72                 int nextRow = [tableView selectedRow] + 1;
73                 if (nextRow >= [tableView numberOfRows]) {
74                         [tableView deselectAll:self];
75                         return;
76                 }
77                 [tableView selectRow:nextRow byExtendingSelection:NO];
78
79                 /* discard any events we have received while waiting for the button press */
80                 endEvent = [NSEvent otherEventWithType:NSApplicationDefined location:where 
81                                                                         modifierFlags:0 timestamp:(NSTimeInterval)0
82                                                                         windowNumber:0 context:[NSGraphicsContext currentContext] subtype:0 data1:0 data2:0];
83                 [NSApp postEvent:endEvent atStart:NO];
84                 [NSApp discardEventsMatchingMask:NSAnyEventMask beforeEvent:endEvent];
85         }
86         [[tableView window] makeFirstResponder:tableView];
87 }
88
89 @end