-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FileOpen dialog in UserInteractions (#839)
There are increasingly cases where we want to prompt users for a file. Rather than write this in VSTGUI I'm using the OS specific APIs behind a UserInteraction common point. As of this writing Mac and Windows work but ignore the options for starting directory and filter; and Linux returns a UserError. Since this move required me to move to cocoa for Mac UserInteractions, also set up the .clang-format file to work properly with objective c Closes #829 (but will require us to open another issue for linux and the undone parts of the API). Pushing this now so I can use it for scale file opening in the tuning issue.
- Loading branch information
Showing
7 changed files
with
184 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "UserInteractions.h" | ||
#include <CoreFoundation/CoreFoundation.h> | ||
#include <CoreServices/CoreServices.h> | ||
#include <ApplicationServices/ApplicationServices.h> | ||
#include <AppKit/AppKit.h> | ||
|
||
namespace Surge | ||
{ | ||
|
||
namespace UserInteractions | ||
{ | ||
|
||
void promptError(const std::string& message, const std::string& title, SurgeGUIEditor* guiEditor) | ||
{ | ||
CFStringRef cfT = | ||
CFStringCreateWithCString(kCFAllocatorDefault, title.c_str(), kCFStringEncodingUTF8); | ||
CFStringRef cfM = | ||
CFStringCreateWithCString(kCFAllocatorDefault, message.c_str(), kCFStringEncodingUTF8); | ||
|
||
SInt32 nRes = 0; | ||
CFUserNotificationRef pDlg = NULL; | ||
const void* keys[] = {kCFUserNotificationAlertHeaderKey, kCFUserNotificationAlertMessageKey}; | ||
const void* vals[] = {cfT, cfM}; | ||
|
||
CFDictionaryRef dict = | ||
CFDictionaryCreate(0, keys, vals, sizeof(keys) / sizeof(*keys), | ||
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | ||
|
||
pDlg = CFUserNotificationCreate(kCFAllocatorDefault, 0, kCFUserNotificationStopAlertLevel, &nRes, | ||
dict); | ||
|
||
CFRelease(cfT); | ||
CFRelease(cfM); | ||
} | ||
|
||
void promptError(const Surge::Error& error, SurgeGUIEditor* guiEditor) | ||
{ | ||
promptError(error.getMessage(), error.getTitle()); | ||
} | ||
|
||
MessageResult | ||
promptOKCancel(const std::string& message, const std::string& title, SurgeGUIEditor* guiEditor) | ||
{ | ||
CFStringRef cfT = | ||
CFStringCreateWithCString(kCFAllocatorDefault, title.c_str(), kCFStringEncodingUTF8); | ||
CFStringRef cfM = | ||
CFStringCreateWithCString(kCFAllocatorDefault, message.c_str(), kCFStringEncodingUTF8); | ||
|
||
CFOptionFlags responseFlags; | ||
CFUserNotificationDisplayAlert(0, kCFUserNotificationPlainAlertLevel, 0, 0, 0, cfT, cfM, | ||
CFSTR("OK"), CFSTR("Cancel"), 0, &responseFlags); | ||
|
||
CFRelease(cfT); | ||
CFRelease(cfM); | ||
|
||
if ((responseFlags & 0x3) != kCFUserNotificationDefaultResponse) | ||
return UserInteractions::CANCEL; | ||
return UserInteractions::OK; | ||
} | ||
|
||
void openURL(const std::string& url_str) | ||
{ | ||
CFURLRef url = CFURLCreateWithBytes(NULL, // allocator | ||
(UInt8*)url_str.c_str(), // URLBytes | ||
url_str.length(), // length | ||
kCFStringEncodingASCII, // encoding | ||
NULL // baseURL | ||
); | ||
LSOpenCFURLRef(url, 0); | ||
CFRelease(url); | ||
} | ||
|
||
void openFolderInFileBrowser(const std::string& folder) | ||
{ | ||
std::string url = "file://" + folder; | ||
UserInteractions::openURL(url); | ||
} | ||
|
||
void promptFileOpenDialog(const std::string& initialDirectory, | ||
const std::string& filterSuffix, | ||
std::function<void(std::string)> callbackOnOpen, | ||
SurgeGUIEditor* guiEditor) | ||
{ | ||
// FIXME TODO - support the filterSuffix and initialDirectory | ||
NSOpenPanel* panel = [NSOpenPanel openPanel]; | ||
|
||
[panel beginWithCompletionHandler:^(NSInteger result) { | ||
if (result == NSFileHandlingPanelOKButton) | ||
{ | ||
NSURL* theDoc = [[panel URLs] objectAtIndex:0]; | ||
NSString* path = [theDoc path]; | ||
std::string pstring([path UTF8String]); | ||
callbackOnOpen(pstring); | ||
} | ||
}]; | ||
} | ||
|
||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters