You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.7KB

  1. #include "osdialog.h"
  2. #include <AppKit/AppKit.h>
  3. #include <Availability.h>
  4. int osdialog_message(osdialog_message_level level, osdialog_message_buttons buttons, const char *message) {
  5. NSAlert *alert = [[NSAlert alloc] init];
  6. switch (level) {
  7. default:
  8. #ifdef __MAC_10_12
  9. case OSDIALOG_INFO: [alert setAlertStyle:NSAlertStyleInformational]; break;
  10. case OSDIALOG_WARNING: [alert setAlertStyle:NSAlertStyleWarning]; break;
  11. case OSDIALOG_ERROR: [alert setAlertStyle:NSAlertStyleCritical]; break;
  12. #else
  13. case OSDIALOG_INFO: [alert setAlertStyle:NSInformationalAlertStyle]; break;
  14. case OSDIALOG_WARNING: [alert setAlertStyle:NSWarningAlertStyle]; break;
  15. case OSDIALOG_ERROR: [alert setAlertStyle:NSCriticalAlertStyle]; break;
  16. #endif
  17. }
  18. switch (buttons) {
  19. default:
  20. case OSDIALOG_OK:
  21. [alert addButtonWithTitle:@"OK"];
  22. break;
  23. case OSDIALOG_OK_CANCEL:
  24. [alert addButtonWithTitle:@"OK"];
  25. [alert addButtonWithTitle:@"Cancel"];
  26. break;
  27. case OSDIALOG_YES_NO:
  28. [alert addButtonWithTitle:@"Yes"];
  29. [alert addButtonWithTitle:@"No"];
  30. break;
  31. }
  32. NSString *messageString = [NSString stringWithUTF8String:message];
  33. // [alert setInformativeText:messageString];
  34. [alert setMessageText:messageString];
  35. int result;
  36. if ([alert runModal] == NSAlertFirstButtonReturn) {
  37. result = 1;
  38. }
  39. else {
  40. result = 0;
  41. }
  42. [alert release];
  43. return result;
  44. }
  45. char *osdialog_file(osdialog_file_action action, const char *path, const char *filename, osdialog_filters *filters) {
  46. NSSavePanel *panel;
  47. NSOpenPanel *open_panel;
  48. // No idea how to manage memory with Objective C. Please help!
  49. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  50. if (action == OSDIALOG_OPEN || action == OSDIALOG_OPEN_DIR) {
  51. open_panel = [NSOpenPanel openPanel];
  52. panel = open_panel;
  53. }
  54. else {
  55. panel = [NSSavePanel savePanel];
  56. }
  57. // Bring dialog to front
  58. // https://stackoverflow.com/a/2402069
  59. // Thanks Dave!
  60. [panel setLevel:CGShieldingWindowLevel()];
  61. if (filters) {
  62. NSMutableArray *fileTypes = [[NSMutableArray alloc] init];
  63. for (; filters; filters = filters->next) {
  64. for (osdialog_filter_patterns *patterns = filters->patterns; patterns; patterns = patterns->next) {
  65. NSString *fileType = [NSString stringWithUTF8String:patterns->pattern];
  66. [fileTypes addObject:fileType];
  67. }
  68. }
  69. [panel setAllowedFileTypes:fileTypes];
  70. // [fileTypes release];
  71. }
  72. if (action == OSDIALOG_OPEN || action == OSDIALOG_OPEN_DIR) {
  73. open_panel.allowsMultipleSelection = NO;
  74. }
  75. if (action == OSDIALOG_OPEN) {
  76. open_panel.canChooseDirectories = NO;
  77. open_panel.canChooseFiles = YES;
  78. }
  79. if (action == OSDIALOG_OPEN_DIR) {
  80. open_panel.canCreateDirectories = YES;
  81. open_panel.canChooseDirectories = YES;
  82. open_panel.canChooseFiles = NO;
  83. }
  84. if (path) {
  85. NSString *path_str = [NSString stringWithUTF8String:path];
  86. NSURL *path_url = [NSURL fileURLWithPath:path_str];
  87. panel.directoryURL = path_url;
  88. // [path_url release];
  89. // [path_str release];
  90. }
  91. if (filename) {
  92. NSString *filenameString = [NSString stringWithUTF8String:filename];
  93. panel.nameFieldStringValue = filenameString;
  94. // [filenameString release];
  95. }
  96. char *result = NULL;
  97. #ifdef __MAC_10_9
  98. #define OK NSModalResponseOK
  99. #else
  100. #define OK NSOKButton
  101. #endif
  102. if ([panel runModal] == OK) {
  103. NSURL *result_url = [panel URL];
  104. result = strdup([[result_url path] UTF8String]);
  105. // [result_url release];
  106. }
  107. // [panel release];
  108. [pool release];
  109. return result;
  110. }
  111. int osdialog_color_picker(osdialog_color *color, int opacity) {
  112. assert(0);
  113. // TODO I have no idea what I'm doing here
  114. NSColorPanel *panel = [NSColorPanel sharedColorPanel];
  115. // [panel setDelegate:self];
  116. [panel isVisible];
  117. // if (opacity)
  118. // [panel setShowAlpha:YES];
  119. // else
  120. // [panel setShowAlpha:NO];
  121. // [panel makeKeyAndOrderFront:self];
  122. return 0;
  123. }