yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
app_delegate.mm
Go to the documentation of this file.
1// AppDelegate.mm
6#import "app/rom.h"
7
8#if defined(__APPLE__) && defined(__MACH__)
9/* Apple OSX and iOS (Darwin). */
10#include <TargetConditionals.h>
11
12#import <CoreText/CoreText.h>
13
14#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
15/* iOS in Xcode simulator */
16
17#elif TARGET_OS_MAC == 1
18/* macOS */
19#import <Cocoa/Cocoa.h>
20
21@interface AppDelegate : NSObject <NSApplicationDelegate>
22- (void)setupMenus;
23// - (void)changeApplicationIcon;
24@end
25
26@implementation AppDelegate
27
28// - (void)changeApplicationIcon {
29// NSImage *newIcon = [NSImage imageNamed:@"newIcon"];
30// [NSApp setApplicationIconImage:newIcon];
31// }
32
33- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
34 [self setupMenus];
35}
36
37- (void)setupMenus {
38 NSMenu *mainMenu = [NSApp mainMenu];
39
40 NSMenuItem *fileMenuItem = [mainMenu itemWithTitle:@"File"];
41 if (!fileMenuItem) {
42 NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
43 fileMenuItem = [[NSMenuItem alloc] initWithTitle:@"File" action:nil keyEquivalent:@""];
44 [fileMenuItem setSubmenu:fileMenu];
45
46 NSMenuItem *openItem = [[NSMenuItem alloc] initWithTitle:@"Open"
47 action:@selector(openFileAction:)
48 keyEquivalent:@"o"];
49 [fileMenu addItem:openItem];
50
51 // Open Recent
52 NSMenu *openRecentMenu = [[NSMenu alloc] initWithTitle:@"Open Recent"];
53 NSMenuItem *openRecentMenuItem = [[NSMenuItem alloc] initWithTitle:@"Open Recent"
54 action:nil
55 keyEquivalent:@""];
56 [openRecentMenuItem setSubmenu:openRecentMenu];
57 [fileMenu addItem:openRecentMenuItem];
58
59 // Add a separator
60 [fileMenu addItem:[NSMenuItem separatorItem]];
61
62 // Save
63 NSMenuItem *saveItem = [[NSMenuItem alloc] initWithTitle:@"Save" action:nil keyEquivalent:@"s"];
64 [fileMenu addItem:saveItem];
65
66 // Separator
67 [fileMenu addItem:[NSMenuItem separatorItem]];
68
69 // Options submenu
70 NSMenu *optionsMenu = [[NSMenu alloc] initWithTitle:@"Options"];
71 NSMenuItem *optionsMenuItem = [[NSMenuItem alloc] initWithTitle:@"Options"
72 action:nil
73 keyEquivalent:@""];
74 [optionsMenuItem setSubmenu:optionsMenu];
75
76 // Flag checkmark field
77 NSMenuItem *flagItem = [[NSMenuItem alloc] initWithTitle:@"Flag"
78 action:@selector(toggleFlagAction:)
79 keyEquivalent:@""];
80 [flagItem setTarget:self];
81 [flagItem setState:NSControlStateValueOff];
82 [optionsMenu addItem:flagItem];
83 [fileMenu addItem:optionsMenuItem];
84
85 [mainMenu insertItem:fileMenuItem atIndex:1];
86 }
87
88 NSMenuItem *editMenuItem = [mainMenu itemWithTitle:@"Edit"];
89 if (!editMenuItem) {
90 NSMenu *editMenu = [[NSMenu alloc] initWithTitle:@"Edit"];
91 editMenuItem = [[NSMenuItem alloc] initWithTitle:@"Edit" action:nil keyEquivalent:@""];
92 [editMenuItem setSubmenu:editMenu];
93
94 NSMenuItem *undoItem = [[NSMenuItem alloc] initWithTitle:@"Undo" action:nil keyEquivalent:@"z"];
95
96 [editMenu addItem:undoItem];
97
98 NSMenuItem *redoItem = [[NSMenuItem alloc] initWithTitle:@"Redo" action:nil keyEquivalent:@"Z"];
99
100 [editMenu addItem:redoItem];
101
102 // Add a separator
103 [editMenu addItem:[NSMenuItem separatorItem]];
104
105 NSMenuItem *cutItem = [[NSMenuItem alloc] initWithTitle:@"Cut"
106 action:@selector(cutAction:)
107 keyEquivalent:@"x"];
108 [editMenu addItem:cutItem];
109
110 NSMenuItem *copyItem = [[NSMenuItem alloc] initWithTitle:@"Copy" action:nil keyEquivalent:@"c"];
111 [editMenu addItem:copyItem];
112
113 NSMenuItem *pasteItem = [[NSMenuItem alloc] initWithTitle:@"Paste"
114 action:nil
115 keyEquivalent:@"v"];
116
117 [editMenu addItem:pasteItem];
118
119 // Add a separator
120 [editMenu addItem:[NSMenuItem separatorItem]];
121
122 NSMenuItem *selectAllItem = [[NSMenuItem alloc] initWithTitle:@"Select All"
123 action:nil
124 keyEquivalent:@"a"];
125
126 [editMenu addItem:selectAllItem];
127
128 [mainMenu insertItem:editMenuItem atIndex:2];
129 }
130
131 NSMenuItem *viewMenuItem = [mainMenu itemWithTitle:@"View"];
132 if (!viewMenuItem) {
133 NSMenu *viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
134 viewMenuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
135 [viewMenuItem setSubmenu:viewMenu];
136
137 // Emulator view button
138 NSMenuItem *emulatorViewItem = [[NSMenuItem alloc] initWithTitle:@"Emulator View"
139 action:nil
140 keyEquivalent:@"1"];
141
142 [viewMenu addItem:emulatorViewItem];
143
144 // Hex Editor View
145 NSMenuItem *hexEditorViewItem = [[NSMenuItem alloc] initWithTitle:@"Hex Editor View"
146 action:nil
147 keyEquivalent:@"2"];
148
149 [viewMenu addItem:hexEditorViewItem];
150
151 // Disassembly view button
152 NSMenuItem *disassemblyViewItem = [[NSMenuItem alloc] initWithTitle:@"Disassembly View"
153 action:nil
154 keyEquivalent:@"3"];
155
156 [viewMenu addItem:disassemblyViewItem];
157
158 // Memory view button
159 NSMenuItem *memoryViewItem = [[NSMenuItem alloc] initWithTitle:@"Memory View"
160 action:nil
161 keyEquivalent:@"4"];
162
163 [viewMenu addItem:memoryViewItem];
164
165 // Add a separator
166 [viewMenu addItem:[NSMenuItem separatorItem]];
167
168 // Toggle fullscreen button
169 NSMenuItem *toggleFullscreenItem = [[NSMenuItem alloc] initWithTitle:@"Toggle Fullscreen"
170 action:nil
171 keyEquivalent:@"f"];
172
173 [viewMenu addItem:toggleFullscreenItem];
174
175 [mainMenu insertItem:viewMenuItem atIndex:3];
176 }
177
178 NSMenuItem *helpMenuItem = [mainMenu itemWithTitle:@"Help"];
179 if (!helpMenuItem) {
180 NSMenu *helpMenu = [[NSMenu alloc] initWithTitle:@"Help"];
181 helpMenuItem = [[NSMenuItem alloc] initWithTitle:@"Help" action:nil keyEquivalent:@""];
182 [helpMenuItem setSubmenu:helpMenu];
183
184 // URL to online documentation
185 NSMenuItem *documentationItem = [[NSMenuItem alloc] initWithTitle:@"Documentation"
186 action:nil
187 keyEquivalent:@"?"];
188 [helpMenu addItem:documentationItem];
189
190 [mainMenu insertItem:helpMenuItem atIndex:4];
191 }
192}
193
194// Action method for the New menu item
195- (void)newFileAction:(id)sender {
196 NSLog(@"New File action triggered");
197}
198
199- (void)toggleFlagAction:(id)sender {
200 NSMenuItem *flagItem = (NSMenuItem *)sender;
201 if ([flagItem state] == NSControlStateValueOff) {
202 [flagItem setState:NSControlStateValueOn];
203 } else {
204 [flagItem setState:NSControlStateValueOff];
205 }
206}
207
208- (void)openFileAction:(id)sender {
211 .ok()) {
212 NSAlert *alert = [[NSAlert alloc] init];
213 [alert setMessageText:@"Error"];
214 [alert setInformativeText:@"Failed to load file."];
215 [alert addButtonWithTitle:@"OK"];
216 [alert runModal];
217 }
218}
219
220- (void)cutAction:(id)sender {
221 // TODO: Implement
222}
223
224- (void)openRecentFileAction:(id)sender {
225 NSLog(@"Open Recent File action triggered");
226}
227
228@end
229
230extern "C" void yaze_initialize_cococa() {
231 @autoreleasepool {
232 AppDelegate *delegate = [[AppDelegate alloc] init];
233 [NSApplication sharedApplication];
234 [NSApp setDelegate:delegate];
235 [NSApp finishLaunching];
236 }
237}
238
239extern "C" void yaze_run_cocoa_app_delegate(const char *filename) {
240 yaze_initialize_cococa();
242 RETURN_VOID_IF_ERROR(controller.OnEntry(filename));
243 while (controller.IsActive()) {
244 @autoreleasepool {
245 controller.OnInput();
246 if (auto status = controller.OnLoad(); !status.ok()) {
247 break;
248 }
249 controller.DoRender();
250 }
251 }
252 controller.OnExit();
253}
254
255#endif
256
257#endif
static std::shared_ptr< Rom > shared_rom_
Definition rom.h:597
Main controller for the application.
Definition controller.h:30
absl::Status OnEntry(std::string filename="")
Definition controller.cc:53
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath.
#define RETURN_VOID_IF_ERROR(expression)
Definition constants.h:53