Repository navigation
Expand file tree
/
Copy pathgui-helper-mac.m
More file actions
457 lines (384 loc) · 15.1 KB
/
Copy pathgui-helper-mac.m
File metadata and controls
457 lines (384 loc) · 15.1 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Cocoa implementation in Objective-C, C-only gimmick not worth it for Apple
// https://developer.apple.com/documentation/appkit?language=objc
#include "gui-helper.h"
#include <stdlib.h>
#include <string.h>
#include <Cocoa/Cocoa.h>
#include <CoreServices/CoreServices.h>
static NSString *NSStringFromCString(const char *str)
{
return [NSString stringWithUTF8String:(str != NULL) ? str : ""];
}
// https://developer.apple.com/documentation/appkit/nsalert?language=objc
int ShowMessageBox(const char *title, const char *message)
{
[NSApplication sharedApplication];
[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:NSStringFromCString(title)];
[alert setInformativeText:NSStringFromCString(message)];
[alert addButtonWithTitle:@"OK"];
NSInteger response = [alert runModal];
return (int)response;
}
@interface SGDBSelectionController : NSObject <NSTableViewDataSource, NSTableViewDelegate, NSWindowDelegate>
{
@public
const char** nonSteamItems;
int nonSteamCount;
const char** modsItems;
int modsCount;
const char** steamItems;
int steamCount;
int result;
BOOL done;
}
@property (nonatomic, strong) NSWindow* window;
@property (nonatomic, weak) NSSegmentedControl* segmentedControl;
@property (nonatomic, weak) NSScrollView* nonSteamScrollView;
@property (nonatomic, weak) NSScrollView* modsScrollView;
@property (nonatomic, weak) NSScrollView* steamScrollView;
@property (nonatomic, weak) NSTableView* nonSteamTable;
@property (nonatomic, weak) NSTableView* modsTable;
@property (nonatomic, weak) NSTableView* steamTable;
@property (nonatomic, weak) NSButton* okButton;
- (id)initWithTitle:(const char*)title
nonSteamItems:(const char**)nsItems
nonSteamCount:(int)nsCount
modsItems:(const char**)mItems
modsCount:(int)mCount
steamItems:(const char**)sItems
steamCount:(int)sCount
initialTab:(int)initialTab
initialSelection:(int)initialSelection;
- (NSScrollView*)buildScrollingTableWithTag:(NSInteger)tag outTableView:(NSTableView**)outTableView;
- (void)segmentChanged:(id)sender;
- (void)okClicked:(id)sender;
- (void)cancelClicked:(id)sender;
@end
@implementation SGDBSelectionController
- (id)initWithTitle:(const char*)title
nonSteamItems:(const char**)nsItems
nonSteamCount:(int)nsCount
modsItems:(const char**)mItems
modsCount:(int)mCount
steamItems:(const char**)sItems
steamCount:(int)sCount
initialTab:(int)initialTab
initialSelection:(int)initialSelection
{
self = [super init];
if (!self) {
return nil;
}
nonSteamItems = nsItems;
nonSteamCount = nsCount;
modsItems = mItems;
modsCount = mCount;
steamItems = sItems;
steamCount = sCount;
result = -1;
done = NO;
const NSRect contentRect = NSMakeRect(0, 0, 454, 388);
const NSUInteger styleMask = NSWindowStyleMaskTitled
| NSWindowStyleMaskClosable
| NSWindowStyleMaskMiniaturizable
| NSWindowStyleMaskResizable;
NSWindow* window = [[NSWindow alloc] initWithContentRect:contentRect
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:NSStringFromCString(title)];
[window setDelegate:self];
[window center];
self.window = window;
NSView* contentView = [window contentView];
// Segmented control switches between three lists
NSSegmentedControl* segmentedControl = [
NSSegmentedControl segmentedControlWithLabels:@[@"Non-Steam", @"GoldSrc/Source Mods", @"Steam"]
trackingMode:NSSegmentSwitchTrackingSelectOne
target:self
action:@selector(segmentChanged:)
];
[segmentedControl setFrame:NSMakeRect(10, 354, 434, 24)];
[segmentedControl setSegmentStyle:NSSegmentStyleAutomatic];
[segmentedControl setAutoresizingMask:(NSViewWidthSizable | NSViewMinYMargin)];
self.segmentedControl = segmentedControl;
[contentView addSubview:segmentedControl];
NSTableView* nonSteamTable = nil;
NSScrollView* nonSteamScroll = [self buildScrollingTableWithTag:0 outTableView:&nonSteamTable];
[nonSteamScroll setFrame:NSMakeRect(10, 50, 434, 294)];
[nonSteamScroll setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
self.nonSteamTable = nonSteamTable;
self.nonSteamScrollView = nonSteamScroll;
[contentView addSubview:nonSteamScroll];
NSTableView* modsTable = nil;
NSScrollView* modsScroll = [self buildScrollingTableWithTag:1 outTableView:&modsTable];
[modsScroll setFrame:NSMakeRect(10, 50, 434, 294)];
[modsScroll setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
self.modsTable = modsTable;
self.modsScrollView = modsScroll;
[contentView addSubview:modsScroll];
NSTableView* steamTable = nil;
NSScrollView* steamScroll = [self buildScrollingTableWithTag:2 outTableView:&steamTable];
[steamScroll setFrame:NSMakeRect(10, 50, 434, 294)];
[steamScroll setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
self.steamTable = steamTable;
self.steamScrollView = steamScroll;
[contentView addSubview:steamScroll];
// buttons, on mac the standard is [Cancel] [OK]
NSButton* okButton = [[NSButton alloc] initWithFrame:NSMakeRect(454 - 80 - 10, 10, 80, 30)];
[okButton setTitle:@"OK"];
[okButton setBezelStyle:NSBezelStyleAutomatic];
[okButton setAutoresizingMask:(NSViewMinXMargin | NSViewMaxYMargin)];
[okButton setTarget:self];
[okButton setAction:@selector(okClicked:)];
[okButton setKeyEquivalent:@"\r"];
[contentView addSubview:okButton];
self.okButton = okButton;
NSButton* cancelButton = [[NSButton alloc] initWithFrame:NSMakeRect(454 - 80 - 10 - 80 - 10, 10, 80, 30)];
[cancelButton setTitle:@"Cancel"];
[cancelButton setBezelStyle:NSBezelStyleAutomatic];
[cancelButton setAutoresizingMask:(NSViewMinXMargin | NSViewMaxYMargin)];
[cancelButton setTarget:self];
[cancelButton setAction:@selector(cancelClicked:)];
[cancelButton setKeyEquivalent:@"\e"]; // Escape key
[contentView addSubview:cancelButton];
// apply initial tab + selection
if (initialTab < 0 || initialTab > 2) {
initialTab = 0;
}
[segmentedControl setSelectedSegment:initialTab];
[nonSteamScroll setHidden:(initialTab != 0)];
[modsScroll setHidden:(initialTab != 1)];
[steamScroll setHidden:(initialTab != 2)];
NSTableView* initialTable = nonSteamTable;
int initialCount = nonSteamCount;
if (initialTab == 1) {
initialTable = modsTable;
initialCount = modsCount;
} else if (initialTab == 2) {
initialTable = steamTable;
initialCount = steamCount;
}
if (initialSelection >= 0 && initialSelection < initialCount) {
[initialTable selectRowIndexes:[NSIndexSet indexSetWithIndex:initialSelection] byExtendingSelection:NO];
[initialTable scrollRowToVisible:initialSelection];
[self.okButton setEnabled:YES];
} else {
[self.okButton setEnabled:NO];
}
return self;
}
// Builds a headerless, single-column table view inside a scroll view.
- (NSScrollView*)buildScrollingTableWithTag:(NSInteger)tag outTableView:(NSTableView**)outTableView
{
NSTableColumn* column = [[NSTableColumn alloc] initWithIdentifier:@"item"];
[column setWidth:400];
[column setEditable:NO];
NSTableView* tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 434, 328)];
[tableView addTableColumn:column];
[tableView setHeaderView:nil];
[tableView setAllowsMultipleSelection:NO];
[tableView setAllowsEmptySelection:YES];
[tableView setTag:tag];
[tableView setDataSource:self];
[tableView setDelegate:self];
[tableView setTarget:self];
[tableView setDoubleAction:@selector(okClicked:)];
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 434, 328)];
[scrollView setDocumentView:tableView];
[scrollView setHasVerticalScroller:YES];
[scrollView setAutohidesScrollers:YES];
[scrollView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
if (outTableView != NULL) {
*outTableView = tableView;
}
return scrollView;
}
- (NSTableView*)tableViewForSegment:(NSInteger)segment
{
switch (segment) {
case 0: return self.nonSteamTable;
case 1: return self.modsTable;
case 2: return self.steamTable;
default: return nil;
}
}
- (void)segmentChanged:(id)sender
{
NSInteger selected = [self.segmentedControl selectedSegment];
[self.nonSteamScrollView setHidden:(selected != 0)];
[self.modsScrollView setHidden:(selected != 1)];
[self.steamScrollView setHidden:(selected != 2)];
NSTableView* activeTable = [self tableViewForSegment:selected];
[self.okButton setEnabled:([activeTable selectedRow] != -1)];
}
- (void)okClicked:(id)sender
{
NSInteger tabIndex = [self.segmentedControl selectedSegment];
NSTableView* activeTable = [self tableViewForSegment:tabIndex];
NSInteger row = [activeTable selectedRow];
if (row < 0) {
return; // nothing selected; OK should be disabled in this case anyway
}
result = (int)row;
if (tabIndex == 1) {
result += nonSteamCount;
} else if (tabIndex == 2) {
result += nonSteamCount + modsCount;
}
done = YES;
[NSApp stopModal];
}
- (void)cancelClicked:(id)sender
{
result = -1;
done = YES;
[NSApp stopModal];
}
// NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView
{
switch ([tableView tag]) {
case 0: return nonSteamCount;
case 1: return modsCount;
case 2: return steamCount;
default: return 0;
}
}
// Custom vertically centered item + 4 padding
- (NSView*)tableView:(NSTableView*)tableView viewForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row
{
static NSString* const identifier = @"SGDBItemCell";
NSTableCellView* cellView = [tableView makeViewWithIdentifier:identifier owner:self];
if (cellView == nil) {
cellView = [[NSTableCellView alloc] initWithFrame:NSMakeRect(0, 0, tableColumn.width, tableView.rowHeight)];
cellView.identifier = identifier;
NSTextField* textField = [NSTextField labelWithString:@""];
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.lineBreakMode = NSLineBreakByTruncatingTail;
[cellView addSubview:textField];
cellView.textField = textField;
[NSLayoutConstraint activateConstraints:@[
[textField.leadingAnchor constraintEqualToAnchor:cellView.leadingAnchor constant:4],
[textField.trailingAnchor constraintEqualToAnchor:cellView.trailingAnchor constant:-4],
[textField.centerYAnchor constraintEqualToAnchor:cellView.centerYAnchor],
]];
}
const char* str = NULL;
switch ([tableView tag]) {
case 0: str = nonSteamItems[row]; break;
case 1: str = modsItems[row]; break;
case 2: str = steamItems[row]; break;
default: break;
}
cellView.textField.stringValue = NSStringFromCString(str);
return cellView;
}
// NSTableViewDelegate
- (void)tableViewSelectionDidChange:(NSNotification*)notification
{
NSTableView* tableView = [notification object];
[self.okButton setEnabled:([tableView selectedRow] != -1)];
}
// NSWindowDelegate - clicking the red close button counts as Cancel.
- (void)windowWillClose:(NSNotification*)notification
{
if (!done) {
result = -1;
done = YES;
}
if ([NSApp modalWindow] == self.window) {
[NSApp stopModal];
}
}
@end
int SelectionDialog(const char* title, int nonSteamCount, const char** nonSteamList, int modsCount, const char** modsList, int steamCount, const char** steamList, int selection)
{
int returnValue = -1;
@autoreleasepool {
[NSApplication sharedApplication];
int initialTab = 0;
int initialSelection = selection;
if (selection >= nonSteamCount + modsCount) {
initialTab = 2;
initialSelection -= nonSteamCount + modsCount;
} else if (selection >= nonSteamCount) {
initialTab = 1;
initialSelection -= nonSteamCount;
}
SGDBSelectionController* controller = [[SGDBSelectionController alloc]
initWithTitle:title
nonSteamItems:nonSteamList
nonSteamCount:nonSteamCount
modsItems:modsList
modsCount:modsCount
steamItems:steamList
steamCount:steamCount
initialTab:initialTab
initialSelection:initialSelection];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
[controller.window makeKeyAndOrderFront:nil];
[NSApp runModalForWindow:controller.window];
returnValue = controller->result;
[controller.window orderOut:nil];
}
return returnValue;
}
static void stopRunLoop(void)
{
[NSApp stop:nil];
NSEvent* dummy = [NSEvent otherEventWithType:NSEventTypeApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:nil subtype:0 data1:0 data2:0];
[NSApp postEvent:dummy atStart:YES];
}
@interface SGDBAppDelegate : NSObject <NSApplicationDelegate>
@property (nonatomic, copy) NSString* URL;
@end
@implementation SGDBAppDelegate
- (void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls
{
if (urls.count > 0) {
self.URL = [urls[0] absoluteString];
stopRunLoop();
}
}
@end
const char* macAwaitEvent()
{
const double timeout = 1.5;
NSApplication* app = [NSApplication sharedApplication];
SGDBAppDelegate* delegate = [[SGDBAppDelegate alloc] init];
app.delegate = delegate;
NSTimer* timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:timeout repeats:NO block:^(NSTimer* t) {
(void)t;
stopRunLoop();
}];
[app run];
[timeoutTimer invalidate];
app.delegate = nil;
static char url[512];
url[0] = '\0';
if (delegate.URL) {
strncpy(url, [delegate.URL UTF8String], sizeof(url) - 1);
url[sizeof(url) - 1] = '\0';
return url;
}
return NULL;
}
// Set the URL by using the old depricated API cause the new method is annoying to implement
// https://developer.apple.com/documentation/coreservices/1447760-lssetdefaulthandlerforurlscheme?language=objc
int macSetURLHandler()
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus status = LSSetDefaultHandlerForURLScheme(CFSTR("sgdb"), CFSTR("com.steamgriddb.SGDBoop"));
if (status == noErr) {
ShowMessageBox("SGDBoop Information", "Program registered successfully!\n\nSGDBoop is meant to be ran from a browser!\nHead over to https://www.steamgriddb.com/boop to continue setup.");
} else {
ShowMessageBox("SGDBoop Information", "Error registering URL scheme.");
}
return 0;
#pragma clang diagnostic pop
}