message-window.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #if defined(Hiro_MessageWindow)
  2. namespace hiro {
  3. static auto Message(MessageWindow::State& state, GtkMessageType messageStyle) -> MessageWindow::Response {
  4. GtkWidget* dialog = gtk_message_dialog_new(
  5. state.parent && state.parent->self() ? GTK_WINDOW(state.parent->self()->widget) : (GtkWindow*)nullptr,
  6. GTK_DIALOG_MODAL, messageStyle, GTK_BUTTONS_NONE, "%s", (const char*)state.text
  7. );
  8. if(state.title) gtk_window_set_title(GTK_WINDOW(dialog), state.title);
  9. else if(Application::state().name) gtk_window_set_title(GTK_WINDOW(dialog), Application::state().name);
  10. switch(state.buttons) {
  11. case MessageWindow::Buttons::Ok:
  12. gtk_dialog_add_buttons(GTK_DIALOG(dialog), "Ok", GTK_RESPONSE_OK, nullptr);
  13. break;
  14. case MessageWindow::Buttons::OkCancel:
  15. gtk_dialog_add_buttons(GTK_DIALOG(dialog), "Ok", GTK_RESPONSE_OK, "Cancel", GTK_RESPONSE_CANCEL, nullptr);
  16. break;
  17. case MessageWindow::Buttons::YesNo:
  18. gtk_dialog_add_buttons(GTK_DIALOG(dialog), "Yes", GTK_RESPONSE_YES, "No", GTK_RESPONSE_NO, nullptr);
  19. break;
  20. case MessageWindow::Buttons::YesNoCancel:
  21. gtk_dialog_add_buttons(GTK_DIALOG(dialog), "Yes", GTK_RESPONSE_YES, "No", GTK_RESPONSE_NO, "Cancel", GTK_RESPONSE_CANCEL, nullptr);
  22. break;
  23. }
  24. auto response = gtk_dialog_run(GTK_DIALOG(dialog));
  25. gtk_widget_destroy(dialog);
  26. if(response == GTK_RESPONSE_OK) return MessageWindow::Response::Ok;
  27. if(response == GTK_RESPONSE_CANCEL) return MessageWindow::Response::Cancel;
  28. if(response == GTK_RESPONSE_YES) return MessageWindow::Response::Yes;
  29. if(response == GTK_RESPONSE_NO) return MessageWindow::Response::No;
  30. //if dialog was closed without choosing a button, choose the most appropriate response
  31. if(state.buttons == MessageWindow::Buttons::Ok) return MessageWindow::Response::Ok;
  32. if(state.buttons == MessageWindow::Buttons::OkCancel) return MessageWindow::Response::Cancel;
  33. if(state.buttons == MessageWindow::Buttons::YesNo) return MessageWindow::Response::No;
  34. if(state.buttons == MessageWindow::Buttons::YesNoCancel) return MessageWindow::Response::Cancel;
  35. throw;
  36. }
  37. auto pMessageWindow::error(MessageWindow::State& state) -> MessageWindow::Response {
  38. return Message(state, GTK_MESSAGE_ERROR);
  39. }
  40. auto pMessageWindow::information(MessageWindow::State& state) -> MessageWindow::Response {
  41. return Message(state, GTK_MESSAGE_INFO);
  42. }
  43. auto pMessageWindow::question(MessageWindow::State& state) -> MessageWindow::Response {
  44. return Message(state, GTK_MESSAGE_QUESTION);
  45. }
  46. auto pMessageWindow::warning(MessageWindow::State& state) -> MessageWindow::Response {
  47. return Message(state, GTK_MESSAGE_WARNING);
  48. }
  49. }
  50. #endif