summaryrefslogtreecommitdiff
path: root/src/ui/macos.m
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/macos.m')
-rw-r--r--src/ui/macos.m438
1 files changed, 0 insertions, 438 deletions
diff --git a/src/ui/macos.m b/src/ui/macos.m
deleted file mode 100644
index 9ff2f96e..00000000
--- a/src/ui/macos.m
+++ /dev/null
@@ -1,438 +0,0 @@
1#include "macos.h"
2#include "app.h"
3#include "command.h"
4#include "widget.h"
5#include "color.h"
6
7#import <AppKit/AppKit.h>
8
9#if 0
10static NSTouchBarItemIdentifier play_TouchId_ = @"fi.skyjake.BitwiseHarmony.play";
11static NSTouchBarItemIdentifier restart_TouchId_ = @"fi.skyjake.BitwiseHarmony.restart";
12
13static NSTouchBarItemIdentifier seqMoveUp_TouchId_ = @"fi.skyjake.BitwiseHarmony.sequence.move.up";
14static NSTouchBarItemIdentifier seqMoveDown_TouchId_ = @"fi.skyjake.BitwiseHarmony.sequence.move.down";
15
16static NSTouchBarItemIdentifier goto_TouchId_ = @"fi.skyjake.BitwiseHarmony.goto";
17static NSTouchBarItemIdentifier mute_TouchId_ = @"fi.skyjake.BitwiseHarmony.mute";
18static NSTouchBarItemIdentifier solo_TouchId_ = @"fi.skyjake.BitwiseHarmony.solo";
19static NSTouchBarItemIdentifier color_TouchId_ = @"fi.skyjake.BitwiseHarmony.color";
20static NSTouchBarItemIdentifier event_TouchId_ = @"fi.skyjake.BitwiseHarmony.event";
21
22static NSTouchBarItemIdentifier eventList_TouchId_ = @"fi.skyjake.BitwiseHarmony.eventlist";
23static NSTouchBarItemIdentifier masterGainEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.mastergain";
24static NSTouchBarItemIdentifier resetEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.reset";
25static NSTouchBarItemIdentifier voiceEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.voice";
26static NSTouchBarItemIdentifier panEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pan";
27static NSTouchBarItemIdentifier gainEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.gain";
28static NSTouchBarItemIdentifier fadeEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.fade";
29static NSTouchBarItemIdentifier pitchSpeedEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pitchspeed";
30static NSTouchBarItemIdentifier pitchBendUpEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pitchbendup";
31static NSTouchBarItemIdentifier pitchBendDownEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pitchbenddown";
32static NSTouchBarItemIdentifier tremoloEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.tremolo";
33#endif
34
35enum iTouchBarVariant {
36 default_TouchBarVariant,
37};
38
39@interface CommandButton : NSButtonTouchBarItem {
40 NSString *command;
41 iWidget *widget;
42}
43- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
44 title:(NSString *)title
45 command:(NSString *)cmd;
46- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
47 title:(NSString *)title
48 widget:(iWidget *)widget
49 command:(NSString *)cmd;
50- (void)dealloc;
51@end
52
53@implementation CommandButton
54
55- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
56 title:(NSString *)title
57 command:(NSString *)cmd {
58 [super initWithIdentifier:identifier];
59 self.title = title;
60 self.target = self;
61 self.action = @selector(buttonPressed);
62 command = cmd;
63 return self;
64}
65
66- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
67 title:(NSString *)title
68 widget:(iWidget *)aWidget
69 command:(NSString *)cmd {
70 [self initWithIdentifier:identifier title:title command:[cmd retain]];
71 widget = aWidget;
72 return self;
73}
74
75- (void)dealloc {
76 [command release];
77 [super dealloc];
78}
79
80- (void)buttonPressed {
81 const char *cmd = [command cStringUsingEncoding:NSUTF8StringEncoding];
82 if (widget) {
83 postCommand_Widget(widget, "%s", cmd);
84 }
85 else {
86 postCommand_App(cmd);
87 }
88}
89
90@end
91
92@interface MyDelegate : NSResponder<NSApplicationDelegate, NSTouchBarDelegate> {
93 enum iTouchBarVariant touchBarVariant;
94 NSObject<NSApplicationDelegate> *sdlDelegate;
95 NSMutableDictionary<NSString *, NSString*> *menuCommands;
96}
97- (id)initWithSDLDelegate:(NSObject<NSApplicationDelegate> *)sdl;
98//- (NSTouchBar *)makeTouchBar;
99/* SDL needs to do its own thing. */
100- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename;
101- (void)applicationDidFinishLaunching:(NSNotification *)notifications;
102@end
103
104@implementation MyDelegate
105
106- (id)initWithSDLDelegate:(NSObject<NSApplicationDelegate> *)sdl {
107 [super init];
108 menuCommands = [[NSMutableDictionary<NSString *, NSString *> alloc] init];
109 touchBarVariant = default_TouchBarVariant;
110 sdlDelegate = sdl;
111 return self;
112}
113
114- (void)dealloc {
115 [menuCommands release];
116 [super dealloc];
117}
118
119- (void)setTouchBarVariant:(enum iTouchBarVariant)variant {
120 touchBarVariant = variant;
121 self.touchBar = nil;
122}
123
124- (void)setCommand:(NSString *)command forMenuItem:(NSMenuItem *)menuItem {
125 [menuCommands setObject:command forKey:[menuItem title]];
126}
127
128- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
129 return [sdlDelegate application:theApplication openFile:filename];
130}
131
132- (void)applicationDidFinishLaunching:(NSNotification *)notification {
133 [sdlDelegate applicationDidFinishLaunching:notification];
134}
135
136#if 0
137- (NSTouchBar *)makeTouchBar {
138 NSTouchBar *bar = [[NSTouchBar alloc] init];
139 bar.delegate = self;
140 switch (touchBarVariant) {
141 case default_TouchBarVariant:
142 bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
143 NSTouchBarItemIdentifierFixedSpaceSmall,
144 NSTouchBarItemIdentifierOtherItemsProxy ];
145 break;
146 case sequence_TouchBarVariant:
147 bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
148 NSTouchBarItemIdentifierFlexibleSpace,
149 seqMoveUp_TouchId_, seqMoveDown_TouchId_,
150 NSTouchBarItemIdentifierFlexibleSpace,
151 NSTouchBarItemIdentifierOtherItemsProxy];
152 break;
153 case tracker_TouchBarVariant:
154 bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
155 NSTouchBarItemIdentifierFlexibleSpace,
156 goto_TouchId_,
157 event_TouchId_,
158 NSTouchBarItemIdentifierFlexibleSpace,
159 solo_TouchId_, mute_TouchId_, color_TouchId_,
160 NSTouchBarItemIdentifierFlexibleSpace,
161 NSTouchBarItemIdentifierOtherItemsProxy ];
162 break;
163 case wide_TouchBarVariant:
164 bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
165 NSTouchBarItemIdentifierFlexibleSpace,
166 event_TouchId_,
167 NSTouchBarItemIdentifierFlexibleSpace,
168 solo_TouchId_, mute_TouchId_, color_TouchId_,
169 NSTouchBarItemIdentifierFlexibleSpace,
170 seqMoveUp_TouchId_, seqMoveDown_TouchId_,
171 NSTouchBarItemIdentifierFlexibleSpace,
172 NSTouchBarItemIdentifierOtherItemsProxy ];
173 break;
174 }
175 return bar;
176}
177#endif
178
179- (void)showPreferences {
180 postCommand_App("preferences");
181}
182
183- (void)closeTab {
184 postCommand_App("tabs.close");
185}
186
187- (void)postMenuItemCommand:(id)sender {
188 NSString *command = [menuCommands objectForKey:[(NSMenuItem *)sender title]];
189 if (command) {
190 postCommand_App([command cStringUsingEncoding:NSUTF8StringEncoding]);
191 }
192}
193
194- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar
195 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
196 iUnused(touchBar);
197#if 0
198 if ([identifier isEqualToString:play_TouchId_]) {
199 return [NSButtonTouchBarItem
200 buttonTouchBarItemWithIdentifier:identifier
201 image:[NSImage imageNamed:NSImageNameTouchBarPlayPauseTemplate]
202 target:self
203 action:@selector(playPressed)];
204 }
205 else if ([identifier isEqualToString:restart_TouchId_]) {
206 return [NSButtonTouchBarItem
207 buttonTouchBarItemWithIdentifier:identifier
208 image:[NSImage imageNamed:NSImageNameTouchBarSkipToStartTemplate]
209 target:self
210 action:@selector(restartPressed)];
211 }
212 else if ([identifier isEqualToString:seqMoveUp_TouchId_]) {
213 return [[CommandButton alloc] initWithIdentifier:identifier
214 title:@"Seq\u2b06"
215 widget:findWidget_App("sequence")
216 command:@"sequence.swap arg:-1"];
217 }
218 else if ([identifier isEqualToString:seqMoveDown_TouchId_]) {
219 return [[CommandButton alloc] initWithIdentifier:identifier
220 title:@"Seq\u2b07"
221 widget:findWidget_App("sequence")
222 command:@"sequence.swap arg:1"];
223 }
224 else if ([identifier isEqualToString:goto_TouchId_]) {
225 return [[CommandButton alloc] initWithIdentifier:identifier
226 title:@"Go to…"
227 command:@"pattern.goto arg:-1"];
228 }
229 else if ([identifier isEqualToString:event_TouchId_]) {
230 NSTouchBar *events = [[NSTouchBar alloc] init];
231 events.delegate = self;
232 events.defaultItemIdentifiers = @[ eventList_TouchId_ ];
233 NSPopoverTouchBarItem *pop = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier];
234 pop.collapsedRepresentationLabel = @"Event";
235 pop.popoverTouchBar = events;
236 [events release];
237 return pop;
238 }
239 else if ([identifier isEqualToString:eventList_TouchId_]) {
240 const struct {
241 NSTouchBarItemIdentifier id;
242 const char *title;
243 const char *command;
244 } buttonDefs_[] = {
245 { voiceEvent_TouchId_, "Voice", "tracker.setevent type:2" },
246 { panEvent_TouchId_, "Pan", "tracker.setevent type:3 arg:128" },
247 { gainEvent_TouchId_, "Gain", "tracker.setevent type:4 arg:128" },
248 { fadeEvent_TouchId_, "Fade", "tracker.setevent type:5" },
249 { tremoloEvent_TouchId_, "Trem", "tracker.setevent type:9" },
250 { pitchSpeedEvent_TouchId_, "P.Spd", "tracker.setevent type:6" },
251 { pitchBendUpEvent_TouchId_, "BnUp", "tracker.setevent type:7" },
252 { pitchBendDownEvent_TouchId_, "BnDn", "tracker.setevent type:8" },
253 { masterGainEvent_TouchId_, "M.Gain", "tracker.setevent type:10 arg:64" },
254 { resetEvent_TouchId_, "Reset", "tracker.setevent type:1" },
255 };
256 NSMutableArray *items = [[NSMutableArray alloc] init];
257 iForIndices(i, buttonDefs_) {
258 CommandButton *button = [[CommandButton alloc]
259 initWithIdentifier:buttonDefs_[i].id
260 title:[NSString stringWithUTF8String:buttonDefs_[i].title]
261 widget:findWidget_App("tracker")
262 command:[NSString stringWithUTF8String:buttonDefs_[i].command]
263 ];
264 [items addObject:button];
265 }
266 NSGroupTouchBarItem *group = [NSGroupTouchBarItem groupItemWithIdentifier:identifier
267 items:items];
268 [items release];
269 return group;
270 }
271 else if ([identifier isEqualToString:mute_TouchId_]) {
272 return [[CommandButton alloc] initWithIdentifier:identifier
273 title:@"Mute"
274 widget:findWidget_App("tracker")
275 command:@"tracker.mute"];
276 }
277 else if ([identifier isEqualToString:solo_TouchId_]) {
278 return [[CommandButton alloc] initWithIdentifier:identifier
279 title:@"Solo"
280 widget:findWidget_App("tracker")
281 command:@"tracker.solo"];
282 }
283 else if ([identifier isEqualToString:color_TouchId_]) {
284 NSTouchBar *colors = [[NSTouchBar alloc] init];
285 colors.delegate = self;
286 colors.defaultItemIdentifiers = @[ NSTouchBarItemIdentifierFlexibleSpace,
287 whiteColor_TouchId_,
288 yellowColor_TouchId_,
289 orangeColor_TouchId_,
290 redColor_TouchId_,
291 magentaColor_TouchId_,
292 blueColor_TouchId_,
293 cyanColor_TouchId_,
294 greenColor_TouchId_,
295 NSTouchBarItemIdentifierFlexibleSpace ];
296 NSPopoverTouchBarItem *pop = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier];
297 pop.collapsedRepresentationImage = [NSImage imageNamed:NSImageNameTouchBarColorPickerFill];
298 pop.popoverTouchBar = colors;
299 [colors release];
300 return pop;
301 }
302 else if ([identifier isEqualToString:whiteColor_TouchId_]) {
303 return [[ColorButton alloc] initWithIdentifier:identifier
304 trackColor:white_TrackColor];
305 }
306 else if ([identifier isEqualToString:yellowColor_TouchId_]) {
307 return [[ColorButton alloc] initWithIdentifier:identifier
308 trackColor:yellow_TrackColor];
309 }
310 else if ([identifier isEqualToString:orangeColor_TouchId_]) {
311 return [[ColorButton alloc] initWithIdentifier:identifier
312 trackColor:orange_TrackColor];
313 }
314 else if ([identifier isEqualToString:redColor_TouchId_]) {
315 return [[ColorButton alloc] initWithIdentifier:identifier
316 trackColor:red_TrackColor];
317 }
318 else if ([identifier isEqualToString:magentaColor_TouchId_]) {
319 return [[ColorButton alloc] initWithIdentifier:identifier
320 trackColor:magenta_TrackColor];
321 }
322 else if ([identifier isEqualToString:blueColor_TouchId_]) {
323 return [[ColorButton alloc] initWithIdentifier:identifier
324 trackColor:blue_TrackColor];
325 }
326 else if ([identifier isEqualToString:cyanColor_TouchId_]) {
327 return [[ColorButton alloc] initWithIdentifier:identifier
328 trackColor:cyan_TrackColor];
329 }
330 else if ([identifier isEqualToString:greenColor_TouchId_]) {
331 return [[ColorButton alloc] initWithIdentifier:identifier
332 trackColor:green_TrackColor];
333 }
334#endif
335 return nil;
336}
337
338@end
339
340void enableMomentumScroll_MacOS(void) {
341 [[NSUserDefaults standardUserDefaults] setBool: YES
342 forKey: @"AppleMomentumScrollSupported"];
343}
344
345void setupApplication_MacOS(void) {
346 NSApplication *app = [NSApplication sharedApplication];
347 /* Our delegate will override SDL's delegate. */
348 MyDelegate *myDel = [[MyDelegate alloc] initWithSDLDelegate:app.delegate];
349 app.delegate = myDel;
350 NSMenu *appMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu];
351 NSMenuItem *prefsItem = [appMenu itemWithTitle:@"Preferences…"];
352 prefsItem.target = myDel;
353 prefsItem.action = @selector(showPreferences);
354 /* Get rid of the default window close item */
355 NSMenu *windowMenu = [[[NSApp mainMenu] itemWithTitle:@"Window"] submenu];
356 NSMenuItem *windowCloseItem = [windowMenu itemWithTitle:@"Close"];
357 windowCloseItem.target = myDel;
358 windowCloseItem.action = @selector(closeTab);
359}
360
361void insertMenuItems_MacOS(const char *menuLabel, int atIndex, const iMenuItem *items, size_t count) {
362 NSApplication *app = [NSApplication sharedApplication];
363 MyDelegate *myDel = (MyDelegate *) app.delegate;
364 NSMenu *appMenu = [app mainMenu];
365 NSMenuItem *mainItem = [appMenu insertItemWithTitle:[NSString stringWithUTF8String:menuLabel]
366 action:nil
367 keyEquivalent:@""
368 atIndex:atIndex];
369 NSMenu *menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:menuLabel]];
370 for (size_t i = 0; i < count; ++i) {
371 const char *label = items[i].label;
372 if (label[0] == '\r') {
373 /* Skip the formatting escape. */
374 label += 2;
375 }
376 if (equal_CStr(label, "---")) {
377 [menu addItem:[NSMenuItem separatorItem]];
378 }
379 else {
380 const iBool hasCommand = (items[i].command && items[i].command[0]);
381 iString key;
382 init_String(&key);
383 if (items[i].key == SDLK_LEFT) {
384 appendChar_String(&key, 0x2190);
385 }
386 else if (items[i].key == SDLK_RIGHT) {
387 appendChar_String(&key, 0x2192);
388 }
389 else if (items[i].key) {
390 appendChar_String(&key, items[i].key);
391 }
392 NSMenuItem *item = [menu addItemWithTitle:[NSString stringWithUTF8String:label]
393 action:(hasCommand ? @selector(postMenuItemCommand:) : nil)
394 keyEquivalent:[NSString stringWithUTF8String:cstr_String(&key)]];
395 NSEventModifierFlags modMask = 0;
396 if (items[i].kmods & KMOD_GUI) {
397 modMask |= NSEventModifierFlagCommand;
398 }
399 if (items[i].kmods & KMOD_ALT) {
400 modMask |= NSEventModifierFlagOption;
401 }
402 if (items[i].kmods & KMOD_CTRL) {
403 modMask |= NSEventModifierFlagControl;
404 }
405 if (items[i].kmods & KMOD_SHIFT) {
406 modMask |= NSEventModifierFlagShift;
407 }
408 [item setKeyEquivalentModifierMask:modMask];
409 if (hasCommand) {
410 [myDel setCommand:[NSString stringWithUTF8String:items[i].command] forMenuItem:item];
411 }
412 deinit_String(&key);
413 }
414 }
415 [mainItem setSubmenu:menu];
416 [menu release];
417}
418
419void handleCommand_MacOS(const char *cmd) {
420#if 0
421 if (equal_Command(cmd, "tabs.changed")) {
422 MyDelegate *myDel = (MyDelegate *) [[NSApplication sharedApplication] delegate];
423 const char *tabId = suffixPtr_Command(cmd, "id");
424 if (equal_CStr(tabId, "tracker")) {
425 [myDel setTouchBarVariant:tracker_TouchBarVariant];
426 }
427 else if (equal_CStr(tabId, "sequence")) {
428 [myDel setTouchBarVariant:sequence_TouchBarVariant];
429 }
430 else if (equal_CStr(tabId, "trackertab")) {
431 [myDel setTouchBarVariant:wide_TouchBarVariant];
432 }
433 else {
434 [myDel setTouchBarVariant:default_TouchBarVariant];
435 }
436 }
437#endif
438}