pcsxr-1.9.92
[pcsx_rearmed.git] / macosx / plugins / DFInput / macsrc / PadController.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 <Cocoa/Cocoa.h>
23 #import "PadController.h"
24 #include "pad.h"
25
26 static NSWindow *padWindow;
27 static PadController *padController;
28
29 #define APP_ID @"net.pcsx.DFInputPlugin"
30
31 void DoAbout() {
32         // Get parent application instance
33         NSApplication *app = [NSApplication sharedApplication];
34         NSBundle *bundle = [NSBundle bundleWithIdentifier:APP_ID];
35
36         // Get Credits.rtf
37         NSString *path = [bundle pathForResource:@"Credits" ofType:@"rtf"];
38         NSAttributedString *credits;
39         if (path) {
40                 credits = [[[NSAttributedString alloc] initWithPath: path
41                                 documentAttributes:NULL] autorelease];
42         } else {
43                 credits = [[[NSAttributedString alloc] initWithString:@""] autorelease];
44         }
45
46         // Get Application Icon
47         NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:[bundle bundlePath]];
48         NSSize size = NSMakeSize(64, 64);
49         [icon setSize:size];
50
51         [app orderFrontStandardAboutPanelWithOptions:[NSDictionary dictionaryWithObjectsAndKeys:
52                 [bundle objectForInfoDictionaryKey:@"CFBundleName"], @"ApplicationName",
53                 icon, @"ApplicationIcon",
54                 [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"], @"ApplicationVersion",
55                 [bundle objectForInfoDictionaryKey:@"CFBundleVersion"], @"Version",
56                 [bundle objectForInfoDictionaryKey:@"NSHumanReadableCopyright"], @"Copyright",
57                 credits, @"Credits", nil]];
58 }
59
60 long DoConfiguration() {
61         SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE);
62         LoadPADConfig();
63
64         if (padWindow == nil) {
65                 if (padController == nil) {
66                         padController = [[PadController alloc] initWithWindowNibName:@"NetPcsxHIDInputPluginMain"];
67                 }
68                 padWindow = [padController window];
69         }
70
71         [padWindow center];
72         [padWindow makeKeyAndOrderFront:nil];
73
74         return 0;
75 }
76
77 @implementation PadController
78
79 - (IBAction)cancel:(id)sender
80 {
81         SDL_Quit();
82         [self close];
83 }
84
85 - (IBAction)ok:(id)sender
86 {
87         SavePADConfig();
88         SDL_Quit();
89         [self close];
90 }
91
92 - (void)awakeFromNib
93 {
94         [[NSNotificationCenter defaultCenter] addObserver:self
95     selector:@selector(windowWillClose:)
96     name:NSWindowWillCloseNotification object:[self window]];
97
98         [controllerView1 addSubview: controllerView];
99         [controllerView setController:0];
100 }
101
102 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
103 {
104         PadView *newView = nil;
105         if ([[tabViewItem identifier] isEqualToString:@"pad1"])
106                 newView = controllerView1;
107         else if ([[tabViewItem identifier] isEqualToString:@"pad2"])
108                 newView = controllerView2;
109
110         if (nil != newView) {
111                 [controllerView removeFromSuperviewWithoutNeedingDisplay];
112                 [newView addSubview: controllerView];
113                 [controllerView setFrame:[newView frame]];
114                 [controllerView setController:[newView isEqual:controllerView1] ? 0 : 1];
115         }
116 }
117
118 - (void)windowBecameKey:(NSNotification *)notification
119 {
120         if ([[controllerView1 subviews] count] > 0)
121                 [controllerView setController:0];
122         else if ([[controllerView2 subviews] count] > 0)
123                 [controllerView setController:1];
124
125         [[NSNotificationCenter defaultCenter] removeObserver:self
126     name:NSWindowDidBecomeKeyNotification object:[self window]];
127 }
128
129 - (void)windowWillClose:(NSNotification *)aNotification
130 {
131         if ([aNotification object] == [self window]) {
132                 [[NSNotificationCenter defaultCenter] addObserver:self
133                  selector:@selector(windowBecameKey:)
134                  name:NSWindowDidBecomeKeyNotification object:[self window]];
135         }
136 }
137
138 @end