message-window.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #if defined(Hiro_MessageWindow)
  2. namespace hiro {
  3. enum class MessageWindowType : uint { Error, Information, Question, Warning };
  4. auto MessageWindow_dialog(MessageWindow::State& state, MessageWindowType type) -> MessageWindow::Response {
  5. @autoreleasepool {
  6. NSAlert* alert = [[[NSAlert alloc] init] autorelease];
  7. if(state.title) [alert setMessageText:[NSString stringWithUTF8String:state.title]];
  8. [alert setInformativeText:[NSString stringWithUTF8String:state.text]];
  9. switch(state.buttons) {
  10. case MessageWindow::Buttons::Ok:
  11. [alert addButtonWithTitle:@"Ok"];
  12. break;
  13. case MessageWindow::Buttons::OkCancel:
  14. [alert addButtonWithTitle:@"Ok"];
  15. [alert addButtonWithTitle:@"Cancel"];
  16. break;
  17. case MessageWindow::Buttons::YesNo:
  18. [alert addButtonWithTitle:@"Yes"];
  19. [alert addButtonWithTitle:@"No"];
  20. break;
  21. case MessageWindow::Buttons::YesNoCancel:
  22. [alert addButtonWithTitle:@"Yes"];
  23. [alert addButtonWithTitle:@"No"];
  24. [alert addButtonWithTitle:@"Cancel"];
  25. break;
  26. }
  27. switch(type) {
  28. case MessageWindowType::Error: [alert setAlertStyle:NSCriticalAlertStyle]; break;
  29. case MessageWindowType::Information: [alert setAlertStyle:NSInformationalAlertStyle]; break;
  30. case MessageWindowType::Question: [alert setAlertStyle:NSInformationalAlertStyle]; break;
  31. case MessageWindowType::Warning: [alert setAlertStyle:NSWarningAlertStyle]; break;
  32. }
  33. NSInteger response = [alert runModal];
  34. //[alert beginSheetModalForWindow:parent.p.cocoaWindow modalDelegate:self didEndSelector:@selector(...) contextInfo:nil];
  35. switch(state.buttons) {
  36. case MessageWindow::Buttons::Ok:
  37. if(response == NSAlertFirstButtonReturn) return MessageWindow::Response::Ok;
  38. break;
  39. case MessageWindow::Buttons::OkCancel:
  40. if(response == NSAlertFirstButtonReturn) return MessageWindow::Response::Ok;
  41. if(response == NSAlertSecondButtonReturn) return MessageWindow::Response::Cancel;
  42. break;
  43. case MessageWindow::Buttons::YesNo:
  44. if(response == NSAlertFirstButtonReturn) return MessageWindow::Response::Yes;
  45. if(response == NSAlertSecondButtonReturn) return MessageWindow::Response::No;
  46. break;
  47. case MessageWindow::Buttons::YesNoCancel:
  48. if(response == NSAlertFirstButtonReturn) return MessageWindow::Response::Yes;
  49. if(response == NSAlertSecondButtonReturn) return MessageWindow::Response::No;
  50. if(response == NSAlertThirdButtonReturn) return MessageWindow::Response::Cancel;
  51. break;
  52. }
  53. }
  54. throw;
  55. }
  56. auto pMessageWindow::error(MessageWindow::State& state) -> MessageWindow::Response {
  57. return MessageWindow_dialog(state, MessageWindowType::Error);
  58. }
  59. auto pMessageWindow::information(MessageWindow::State& state) -> MessageWindow::Response {
  60. return MessageWindow_dialog(state, MessageWindowType::Information);
  61. }
  62. auto pMessageWindow::question(MessageWindow::State& state) -> MessageWindow::Response {
  63. return MessageWindow_dialog(state, MessageWindowType::Question);
  64. }
  65. auto pMessageWindow::warning(MessageWindow::State& state) -> MessageWindow::Response {
  66. return MessageWindow_dialog(state, MessageWindowType::Warning);
  67. }
  68. }
  69. #endif