select_file_dialog_linux_gtk.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GTK_SELECT_FILE_DIALOG_LINUX_GTK_H_
  5. #define UI_GTK_SELECT_FILE_DIALOG_LINUX_GTK_H_
  6. #include <map>
  7. #include "ui/base/glib/glib_signal.h"
  8. #include "ui/gtk/gtk_util.h"
  9. #include "ui/shell_dialogs/select_file_dialog_linux.h"
  10. namespace gtk {
  11. // Implementation of SelectFileDialog that shows a Gtk common dialog for
  12. // choosing a file or folder. This acts as a modal dialog.
  13. class SelectFileDialogLinuxGtk : public ui::SelectFileDialogLinux,
  14. public aura::WindowObserver {
  15. public:
  16. SelectFileDialogLinuxGtk(Listener* listener,
  17. std::unique_ptr<ui::SelectFilePolicy> policy);
  18. SelectFileDialogLinuxGtk(const SelectFileDialogLinuxGtk&) = delete;
  19. SelectFileDialogLinuxGtk& operator=(const SelectFileDialogLinuxGtk&) = delete;
  20. protected:
  21. ~SelectFileDialogLinuxGtk() override;
  22. // BaseShellDialog implementation:
  23. bool IsRunning(gfx::NativeWindow parent_window) const override;
  24. // SelectFileDialog implementation.
  25. // |params| is user data we pass back via the Listener interface.
  26. void SelectFileImpl(Type type,
  27. const std::u16string& title,
  28. const base::FilePath& default_path,
  29. const FileTypeInfo* file_types,
  30. int file_type_index,
  31. const base::FilePath::StringType& default_extension,
  32. gfx::NativeWindow owning_window,
  33. void* params) override;
  34. private:
  35. friend class FilePicker;
  36. bool HasMultipleFileTypeChoicesImpl() override;
  37. // Overridden from aura::WindowObserver:
  38. void OnWindowDestroying(aura::Window* window) override;
  39. // Add the filters from |file_types_| to |chooser|.
  40. void AddFilters(GtkFileChooser* chooser);
  41. // Notifies the listener that a single file was chosen.
  42. void FileSelected(GtkWidget* dialog, const base::FilePath& path);
  43. // Notifies the listener that multiple files were chosen.
  44. void MultiFilesSelected(GtkWidget* dialog,
  45. const std::vector<base::FilePath>& files);
  46. // Notifies the listener that no file was chosen (the action was canceled).
  47. // Dialog is passed so we can find that |params| pointer that was passed to
  48. // us when we were told to show the dialog.
  49. void FileNotSelected(GtkWidget* dialog);
  50. GtkWidget* CreateSelectFolderDialog(Type type,
  51. const std::string& title,
  52. const base::FilePath& default_path,
  53. gfx::NativeWindow parent);
  54. GtkWidget* CreateFileOpenDialog(const std::string& title,
  55. const base::FilePath& default_path,
  56. gfx::NativeWindow parent);
  57. GtkWidget* CreateMultiFileOpenDialog(const std::string& title,
  58. const base::FilePath& default_path,
  59. gfx::NativeWindow parent);
  60. GtkWidget* CreateSaveAsDialog(const std::string& title,
  61. const base::FilePath& default_path,
  62. gfx::NativeWindow parent);
  63. // Removes and returns the |params| associated with |dialog| from
  64. // |params_map_|.
  65. void* PopParamsForDialog(GtkWidget* dialog);
  66. // Check whether response_id corresponds to the user cancelling/closing the
  67. // dialog. Used as a helper for the below callbacks.
  68. bool IsCancelResponse(gint response_id);
  69. // Common function for OnSelectSingleFileDialogResponse and
  70. // OnSelectSingleFolderDialogResponse.
  71. void SelectSingleFileHelper(GtkWidget* dialog,
  72. gint response_id,
  73. bool allow_folder);
  74. // Common function for CreateFileOpenDialog and CreateMultiFileOpenDialog.
  75. GtkWidget* CreateFileOpenHelper(const std::string& title,
  76. const base::FilePath& default_path,
  77. gfx::NativeWindow parent);
  78. // Callback for when the user responds to a Save As or Open File dialog.
  79. CHROMEG_CALLBACK_1(SelectFileDialogLinuxGtk,
  80. void,
  81. OnSelectSingleFileDialogResponse,
  82. GtkWidget*,
  83. int);
  84. // Callback for when the user responds to a Select Folder dialog.
  85. CHROMEG_CALLBACK_1(SelectFileDialogLinuxGtk,
  86. void,
  87. OnSelectSingleFolderDialogResponse,
  88. GtkWidget*,
  89. int);
  90. // Callback for when the user responds to a Open Multiple Files dialog.
  91. CHROMEG_CALLBACK_1(SelectFileDialogLinuxGtk,
  92. void,
  93. OnSelectMultiFileDialogResponse,
  94. GtkWidget*,
  95. int);
  96. // Callback for when the file chooser gets destroyed.
  97. CHROMEG_CALLBACK_0(SelectFileDialogLinuxGtk,
  98. void,
  99. OnFileChooserDestroy,
  100. GtkWidget*);
  101. // Callback for when we update the preview for the selection. Only used on
  102. // GTK3.
  103. CHROMEG_CALLBACK_0(SelectFileDialogLinuxGtk,
  104. void,
  105. OnUpdatePreview,
  106. GtkWidget*);
  107. // A map from dialog windows to the |params| user data associated with them.
  108. std::map<GtkWidget*, void*> params_map_;
  109. // Only used on GTK3 since GTK4 provides its own preview.
  110. // The GtkImage widget for showing previews of selected images.
  111. GtkWidget* preview_ = nullptr;
  112. // Maps from dialogs to signal handler IDs.
  113. std::map<GtkWidget*, unsigned long> dialogs_;
  114. // The set of all parent windows for which we are currently running dialogs.
  115. std::set<aura::Window*> parents_;
  116. };
  117. } // namespace gtk
  118. #endif // UI_GTK_SELECT_FILE_DIALOG_LINUX_GTK_H_