summaryrefslogtreecommitdiff
path: root/src/ui/macos.m
blob: b50fbf38a32ea5829c46b392117e10ebed9aa381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "macos.h"
#include "app.h"
#include "command.h"
#include "widget.h"
#include "color.h"

#import <AppKit/AppKit.h>

#if 0
static NSTouchBarItemIdentifier play_TouchId_         = @"fi.skyjake.BitwiseHarmony.play";
static NSTouchBarItemIdentifier restart_TouchId_      = @"fi.skyjake.BitwiseHarmony.restart";

static NSTouchBarItemIdentifier seqMoveUp_TouchId_    = @"fi.skyjake.BitwiseHarmony.sequence.move.up";
static NSTouchBarItemIdentifier seqMoveDown_TouchId_  = @"fi.skyjake.BitwiseHarmony.sequence.move.down";

static NSTouchBarItemIdentifier goto_TouchId_         = @"fi.skyjake.BitwiseHarmony.goto";
static NSTouchBarItemIdentifier mute_TouchId_         = @"fi.skyjake.BitwiseHarmony.mute";
static NSTouchBarItemIdentifier solo_TouchId_         = @"fi.skyjake.BitwiseHarmony.solo";
static NSTouchBarItemIdentifier color_TouchId_        = @"fi.skyjake.BitwiseHarmony.color";
static NSTouchBarItemIdentifier event_TouchId_        = @"fi.skyjake.BitwiseHarmony.event";

static NSTouchBarItemIdentifier eventList_TouchId_          = @"fi.skyjake.BitwiseHarmony.eventlist";
static NSTouchBarItemIdentifier masterGainEvent_TouchId_    = @"fi.skyjake.BitwiseHarmony.event.mastergain";
static NSTouchBarItemIdentifier resetEvent_TouchId_         = @"fi.skyjake.BitwiseHarmony.event.reset";
static NSTouchBarItemIdentifier voiceEvent_TouchId_         = @"fi.skyjake.BitwiseHarmony.event.voice";
static NSTouchBarItemIdentifier panEvent_TouchId_           = @"fi.skyjake.BitwiseHarmony.event.pan";
static NSTouchBarItemIdentifier gainEvent_TouchId_          = @"fi.skyjake.BitwiseHarmony.event.gain";
static NSTouchBarItemIdentifier fadeEvent_TouchId_          = @"fi.skyjake.BitwiseHarmony.event.fade";
static NSTouchBarItemIdentifier pitchSpeedEvent_TouchId_    = @"fi.skyjake.BitwiseHarmony.event.pitchspeed";
static NSTouchBarItemIdentifier pitchBendUpEvent_TouchId_   = @"fi.skyjake.BitwiseHarmony.event.pitchbendup";
static NSTouchBarItemIdentifier pitchBendDownEvent_TouchId_ = @"fi.skyjake.BitwiseHarmony.event.pitchbenddown";
static NSTouchBarItemIdentifier tremoloEvent_TouchId_       = @"fi.skyjake.BitwiseHarmony.event.tremolo";
#endif

enum iTouchBarVariant {
    default_TouchBarVariant,
};

@interface CommandButton : NSButtonTouchBarItem {
    NSString *command;
    iWidget *widget;
}
- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
                   title:(NSString *)title
                 command:(NSString *)cmd;
- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
                   title:(NSString *)title
                  widget:(iWidget *)widget
                 command:(NSString *)cmd;
- (void)dealloc;
@end

@implementation CommandButton

- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
                   title:(NSString *)title
                 command:(NSString *)cmd {
    [super initWithIdentifier:identifier];
    self.title = title;
    self.target = self;
    self.action = @selector(buttonPressed);
    command = cmd;
    return self;
}

- (id)initWithIdentifier:(NSTouchBarItemIdentifier)identifier
                   title:(NSString *)title
                  widget:(iWidget *)aWidget
                 command:(NSString *)cmd {
    [self initWithIdentifier:identifier title:title command:[cmd retain]];
    widget = aWidget;
    return self;
}

- (void)dealloc {
    [command release];
    [super dealloc];
}

- (void)buttonPressed {
    const char *cmd = [command cStringUsingEncoding:NSUTF8StringEncoding];
    if (widget) {
        postCommand_Widget(widget, "%s", cmd);
    }
    else {
        postCommand_App(cmd);
    }
}

@end

@interface MyDelegate : NSResponder<NSApplicationDelegate, NSTouchBarDelegate> {
    enum iTouchBarVariant touchBarVariant;
    NSObject<NSApplicationDelegate> *sdlDelegate;
    NSMutableDictionary<NSString *, NSString*> *menuCommands;
}
- (id)initWithSDLDelegate:(NSObject<NSApplicationDelegate> *)sdl;
//- (NSTouchBar *)makeTouchBar;
/* SDL needs to do its own thing. */
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename;
- (void)applicationDidFinishLaunching:(NSNotification *)notificatiosn;
@end

@implementation MyDelegate

- (id)initWithSDLDelegate:(NSObject<NSApplicationDelegate> *)sdl {
    [super init];
    menuCommands = [[NSMutableDictionary<NSString *, NSString *> alloc] init];
    touchBarVariant = default_TouchBarVariant;
    sdlDelegate = sdl;
    return self;
}

- (void)dealloc {
    [menuCommands release];
    [super dealloc];
}

- (void)setTouchBarVariant:(enum iTouchBarVariant)variant {
    touchBarVariant = variant;
    self.touchBar = nil;
}

- (void)setCommand:(NSString *)command forMenuItem:(NSMenuItem *)menuItem {
    [menuCommands setObject:command forKey:[menuItem title]];
}

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
    return [sdlDelegate application:theApplication openFile:filename];
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    [sdlDelegate applicationDidFinishLaunching:notification];
}

#if 0
- (NSTouchBar *)makeTouchBar {
    NSTouchBar *bar = [[NSTouchBar alloc] init];
    bar.delegate = self;
    switch (touchBarVariant) {
        case default_TouchBarVariant:
            bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
                                            NSTouchBarItemIdentifierFixedSpaceSmall,
                                            NSTouchBarItemIdentifierOtherItemsProxy ];
            break;
        case sequence_TouchBarVariant:
            bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            seqMoveUp_TouchId_, seqMoveDown_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            NSTouchBarItemIdentifierOtherItemsProxy];
            break;
        case tracker_TouchBarVariant:
            bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            goto_TouchId_,
                                            event_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            solo_TouchId_, mute_TouchId_, color_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            NSTouchBarItemIdentifierOtherItemsProxy ];
            break;
        case wide_TouchBarVariant:
            bar.defaultItemIdentifiers = @[ play_TouchId_, restart_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            event_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            solo_TouchId_, mute_TouchId_, color_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            seqMoveUp_TouchId_, seqMoveDown_TouchId_,
                                            NSTouchBarItemIdentifierFlexibleSpace,
                                            NSTouchBarItemIdentifierOtherItemsProxy ];
            break;
    }
    return bar;
}
#endif

- (void)showPreferences {
    postCommand_App("preferences");
}

- (void)postMenuItemCommand:(id)sender {
    NSString *command = [menuCommands objectForKey:[(NSMenuItem *)sender title]];
    if (command) {
        postCommand_App([command cStringUsingEncoding:NSUTF8StringEncoding]);
    }
}

- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar
                makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
    iUnused(touchBar);
#if 0
    if ([identifier isEqualToString:play_TouchId_]) {
        return [NSButtonTouchBarItem
            buttonTouchBarItemWithIdentifier:identifier
                                       image:[NSImage imageNamed:NSImageNameTouchBarPlayPauseTemplate]
                                      target:self
                                      action:@selector(playPressed)];
    }
    else if ([identifier isEqualToString:restart_TouchId_]) {
        return [NSButtonTouchBarItem
            buttonTouchBarItemWithIdentifier:identifier
                                       image:[NSImage imageNamed:NSImageNameTouchBarSkipToStartTemplate]
                                      target:self
                                      action:@selector(restartPressed)];
    }
    else if ([identifier isEqualToString:seqMoveUp_TouchId_]) {
        return [[CommandButton alloc] initWithIdentifier:identifier
                                                   title:@"Seq\u2b06"
                                                  widget:findWidget_App("sequence")
                                                 command:@"sequence.swap arg:-1"];
    }
    else if ([identifier isEqualToString:seqMoveDown_TouchId_]) {
        return [[CommandButton alloc] initWithIdentifier:identifier
                                                   title:@"Seq\u2b07"
                                                  widget:findWidget_App("sequence")
                                                 command:@"sequence.swap arg:1"];
    }
    else if ([identifier isEqualToString:goto_TouchId_]) {
        return [[CommandButton alloc] initWithIdentifier:identifier
                                                   title:@"Go to…"
                                                 command:@"pattern.goto arg:-1"];
    }   
    else if ([identifier isEqualToString:event_TouchId_]) {
        NSTouchBar *events = [[NSTouchBar alloc] init];
        events.delegate = self;
        events.defaultItemIdentifiers = @[ eventList_TouchId_ ];
        NSPopoverTouchBarItem *pop = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier];
        pop.collapsedRepresentationLabel = @"Event";
        pop.popoverTouchBar = events;
        [events release];
        return pop;
    }
    else if ([identifier isEqualToString:eventList_TouchId_]) {
        const struct {
            NSTouchBarItemIdentifier id;
            const char *title;
            const char *command;
        } buttonDefs_[] = {
            { voiceEvent_TouchId_, "Voice", "tracker.setevent type:2" },
            { panEvent_TouchId_, "Pan", "tracker.setevent type:3 arg:128" },
            { gainEvent_TouchId_, "Gain", "tracker.setevent type:4 arg:128" },
            { fadeEvent_TouchId_, "Fade", "tracker.setevent type:5" },
            { tremoloEvent_TouchId_, "Trem", "tracker.setevent type:9" },
            { pitchSpeedEvent_TouchId_, "P.Spd", "tracker.setevent type:6" },
            { pitchBendUpEvent_TouchId_, "BnUp", "tracker.setevent type:7" },
            { pitchBendDownEvent_TouchId_, "BnDn", "tracker.setevent type:8" },
            { masterGainEvent_TouchId_, "M.Gain", "tracker.setevent type:10 arg:64" },
            { resetEvent_TouchId_, "Reset", "tracker.setevent type:1" },
        };
        NSMutableArray *items = [[NSMutableArray alloc] init];
        iForIndices(i, buttonDefs_) {
            CommandButton *button = [[CommandButton alloc]
                initWithIdentifier:buttonDefs_[i].id
                             title:[NSString stringWithUTF8String:buttonDefs_[i].title]
                            widget:findWidget_App("tracker")
                           command:[NSString stringWithUTF8String:buttonDefs_[i].command]
            ];
            [items addObject:button];
        }
        NSGroupTouchBarItem *group = [NSGroupTouchBarItem groupItemWithIdentifier:identifier
                                                                            items:items];
        [items release];
        return group;
    }
    else if ([identifier isEqualToString:mute_TouchId_]) {
        return [[CommandButton alloc] initWithIdentifier:identifier
                                                   title:@"Mute"
                                                  widget:findWidget_App("tracker")
                                                 command:@"tracker.mute"];
    }
    else if ([identifier isEqualToString:solo_TouchId_]) {
        return [[CommandButton alloc] initWithIdentifier:identifier
                                                   title:@"Solo"
                                                  widget:findWidget_App("tracker")
                                                 command:@"tracker.solo"];
    }
    else if ([identifier isEqualToString:color_TouchId_]) {
        NSTouchBar *colors = [[NSTouchBar alloc] init];
        colors.delegate = self;
        colors.defaultItemIdentifiers = @[ NSTouchBarItemIdentifierFlexibleSpace,
                                           whiteColor_TouchId_,
                                           yellowColor_TouchId_,
                                           orangeColor_TouchId_,
                                           redColor_TouchId_,
                                           magentaColor_TouchId_,
                                           blueColor_TouchId_,
                                           cyanColor_TouchId_,
                                           greenColor_TouchId_,
                                           NSTouchBarItemIdentifierFlexibleSpace ];
        NSPopoverTouchBarItem *pop = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier];
        pop.collapsedRepresentationImage = [NSImage imageNamed:NSImageNameTouchBarColorPickerFill];
        pop.popoverTouchBar = colors;
        [colors release];
        return pop;
    }
    else if ([identifier isEqualToString:whiteColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:white_TrackColor];
    }
    else if ([identifier isEqualToString:yellowColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:yellow_TrackColor];
    }
    else if ([identifier isEqualToString:orangeColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:orange_TrackColor];
    }
    else if ([identifier isEqualToString:redColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:red_TrackColor];
    }
    else if ([identifier isEqualToString:magentaColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:magenta_TrackColor];
    }
    else if ([identifier isEqualToString:blueColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:blue_TrackColor];
    }
    else if ([identifier isEqualToString:cyanColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:cyan_TrackColor];
    }
    else if ([identifier isEqualToString:greenColor_TouchId_]) {
        return [[ColorButton alloc] initWithIdentifier:identifier
                                            trackColor:green_TrackColor];
    }
#endif
    return nil;
}

@end

void setupApplication_MacOS(void) {
    NSApplication *app = [NSApplication sharedApplication];
    /* Our delegate will override SDL's delegate. */
    MyDelegate *myDel = [[MyDelegate alloc] initWithSDLDelegate:app.delegate];
    app.delegate = myDel;
    NSMenu *appMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu];
    NSMenuItem *prefsItem = [appMenu itemWithTitle:@"Preferences…"];
    prefsItem.target = myDel;
    prefsItem.action = @selector(showPreferences);
}

void insertMenuItems_MacOS(const char *menuLabel, const iMenuItem *items, size_t count) {
    NSApplication *app = [NSApplication sharedApplication];
    MyDelegate *myDel = (MyDelegate *) app.delegate;
    NSMenu *appMenu = [app mainMenu];
    NSMenuItem *mainItem = [appMenu insertItemWithTitle:[NSString stringWithUTF8String:menuLabel]
                                                 action:nil
                                          keyEquivalent:@""
                                                atIndex:(iCmpStr(menuLabel, "File") == 0 ? 1 :
                                                         iCmpStr(menuLabel, "Edit") == 0 ? 2 : 3)];
    NSMenu *menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:menuLabel]];
    for (size_t i = 0; i < count; ++i) {
        const char *label = items[i].label;
        if (label[0] == '\r') {
            /* Skip the formatting escape. */
            label += 2;
        }
        if (equal_CStr(label, "---")) {
            [menu addItem:[NSMenuItem separatorItem]];
        }
        else {
            const iBool hasCommand = (items[i].command && items[i].command[0]);
            iString key;
            init_String(&key);
            if (items[i].key == SDLK_LEFT) {
                appendChar_String(&key, 0x2190);
            }
            else if (items[i].key == SDLK_RIGHT) {
                appendChar_String(&key, 0x2192);
            }
            else if (items[i].key) {
                appendChar_String(&key, items[i].key);
            }
            NSMenuItem *item = [menu addItemWithTitle:[NSString stringWithUTF8String:label]
                                               action:(hasCommand ? @selector(postMenuItemCommand:) : nil)
                                        keyEquivalent:[NSString stringWithUTF8String:cstr_String(&key)]];
            NSEventModifierFlags modMask = 0;
            if (items[i].kmods & KMOD_GUI) {
                modMask |= NSEventModifierFlagCommand;
            }
            if (items[i].kmods & KMOD_ALT) {
                modMask |= NSEventModifierFlagOption;
            }
            if (items[i].kmods & KMOD_CTRL) {
                modMask |= NSEventModifierFlagControl;
            }
            if (items[i].kmods & KMOD_SHIFT) {
                modMask |= NSEventModifierFlagShift;
            }
            [item setKeyEquivalentModifierMask:modMask];
            if (hasCommand) {
                [myDel setCommand:[NSString stringWithUTF8String:items[i].command] forMenuItem:item];
            }
            deinit_String(&key);
        }
    }
    [mainItem setSubmenu:menu];
    [menu release];
}

void handleCommand_MacOS(const char *cmd) {
#if 0
    if (equal_Command(cmd, "tabs.changed")) {
        MyDelegate *myDel = (MyDelegate *) [[NSApplication sharedApplication] delegate];
        const char *tabId = suffixPtr_Command(cmd, "id");
        if (equal_CStr(tabId, "tracker")) {
            [myDel setTouchBarVariant:tracker_TouchBarVariant];
        }
        else if (equal_CStr(tabId, "sequence")) {
            [myDel setTouchBarVariant:sequence_TouchBarVariant];
        }
        else if (equal_CStr(tabId, "trackertab")) {
            [myDel setTouchBarVariant:wide_TouchBarVariant];
        }
        else {
            [myDel setTouchBarVariant:default_TouchBarVariant];
        }
    }
#endif
}