browser-window.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #if defined(Hiro_BrowserWindow)
  2. namespace hiro {
  3. static auto BrowserWindow_addFilters(GtkWidget* dialog, vector<string> filters) -> void {
  4. for(auto& filter : filters) {
  5. auto part = filter.split("|", 1L);
  6. if(part.size() != 2) continue;
  7. GtkFileFilter* gtkFilter = gtk_file_filter_new();
  8. gtk_file_filter_set_name(gtkFilter, part[0]);
  9. auto patterns = part[1].split(":");
  10. for(auto& pattern : patterns) gtk_file_filter_add_pattern(gtkFilter, pattern);
  11. gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), gtkFilter);
  12. }
  13. }
  14. auto pBrowserWindow::directory(BrowserWindow::State& state) -> string {
  15. string name;
  16. GtkWidget* dialog = gtk_file_chooser_dialog_new(
  17. state.title ? state.title : "Select Directory",
  18. state.parent && state.parent->self() ? GTK_WINDOW(state.parent->self()->widget) : (GtkWindow*)nullptr,
  19. GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
  20. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  21. GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
  22. (const gchar*)nullptr
  23. );
  24. if(state.path) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), state.path);
  25. if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
  26. char* temp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
  27. name = temp;
  28. g_free(temp);
  29. }
  30. gtk_widget_destroy(dialog);
  31. if(name && !name.endsWith("/")) name.append("/");
  32. return name;
  33. }
  34. auto pBrowserWindow::open(BrowserWindow::State& state) -> string {
  35. string name;
  36. GtkWidget* dialog = gtk_file_chooser_dialog_new(
  37. state.title ? state.title : "Open File",
  38. state.parent && state.parent->self() ? GTK_WINDOW(state.parent->self()->widget) : (GtkWindow*)nullptr,
  39. GTK_FILE_CHOOSER_ACTION_OPEN,
  40. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  41. GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
  42. (const gchar*)nullptr
  43. );
  44. if(state.path) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), state.path);
  45. BrowserWindow_addFilters(dialog, state.filters);
  46. if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
  47. char* temp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
  48. name = temp;
  49. g_free(temp);
  50. }
  51. gtk_widget_destroy(dialog);
  52. return name;
  53. }
  54. auto pBrowserWindow::save(BrowserWindow::State& state) -> string {
  55. string name;
  56. GtkWidget* dialog = gtk_file_chooser_dialog_new(
  57. state.title ? state.title : "Save File",
  58. state.parent && state.parent->self() ? GTK_WINDOW(state.parent->self()->widget) : (GtkWindow*)nullptr,
  59. GTK_FILE_CHOOSER_ACTION_SAVE,
  60. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  61. GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
  62. (const gchar*)nullptr
  63. );
  64. if(state.path) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), state.path);
  65. BrowserWindow_addFilters(dialog, state.filters);
  66. if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
  67. char* temp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
  68. name = temp;
  69. g_free(temp);
  70. }
  71. gtk_widget_destroy(dialog);
  72. return name;
  73. }
  74. }
  75. #endif