summaryrefslogtreecommitdiffstats
path: root/snap/local/filedialog-native.patch
blob: ce82d92e07f62295676c41de317611e1fb7cd5f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# SPDX-License-Identifier: GPL-2.0-or-later
# Taken from: https://gitlab.com/inkscape/inkscape/-/merge_requests/4237
# And used only for the snap currently.
diff --git a/src/ui/dialog/filedialogimpl-gtkmm.cpp b/src/ui/dialog/filedialogimpl-gtkmm.cpp
index c3aba062dd963e72ec775bd9a2e3a753bf08ee0f..19203350bb3bae36e9157e538a0889b4c1de5a78 100644
--- a/src/ui/dialog/filedialogimpl-gtkmm.cpp
+++ b/src/ui/dialog/filedialogimpl-gtkmm.cpp
@@ -78,41 +78,26 @@ void fileDialogExtensionToPattern(Glib::ustring &pattern, Glib::ustring &extensi
     }
 }
 
+/*#########################################################################
+### F I L E     D I A L O G    B A S E    C L A S S
+#########################################################################*/
 
-void findEntryWidgets(Gtk::Container *parent, std::vector<Gtk::Entry *> &result)
+// Small function so the translatable strings stay out of the header
+const char * FileDialogBaseGtk::accept_label(Gtk::FileChooserAction dialogType)
 {
-    if (!parent) {
-        return;
-    }
-    std::vector<Gtk::Widget *> children = parent->get_children();
-    for (auto child : children) {
-        GtkWidget *wid = child->gobj();
-        if (GTK_IS_ENTRY(wid))
-            result.push_back(dynamic_cast<Gtk::Entry *>(child));
-        else if (GTK_IS_CONTAINER(wid))
-            findEntryWidgets(dynamic_cast<Gtk::Container *>(child), result);
+    if (dialogType == Gtk::FILE_CHOOSER_ACTION_OPEN) {
+        return _("_Open");
+    } else {
+        return _("_Save");
     }
 }
 
-void findExpanderWidgets(Gtk::Container *parent, std::vector<Gtk::Expander *> &result)
+// Small function so the translatable strings stay out of the header
+const char * FileDialogBaseGtk::cancel_label()
 {
-    if (!parent)
-        return;
-    std::vector<Gtk::Widget *> children = parent->get_children();
-    for (auto child : children) {
-        GtkWidget *wid = child->gobj();
-        if (GTK_IS_EXPANDER(wid))
-            result.push_back(dynamic_cast<Gtk::Expander *>(child));
-        else if (GTK_IS_CONTAINER(wid))
-            findExpanderWidgets(dynamic_cast<Gtk::Container *>(child), result);
-    }
+    return _("_Cancel");
 }
 
-
-/*#########################################################################
-### F I L E     D I A L O G    B A S E    C L A S S
-#########################################################################*/
-
 void FileDialogBaseGtk::internalSetup()
 {
     // Open executable file dialogs don't need the preview panel
@@ -126,11 +111,15 @@ void FileDialogBaseGtk::internalSetup()
 
         previewCheckbox.signal_toggled().connect(sigc::mem_fun(*this, &FileDialogBaseGtk::_updatePreviewCallback));
 
+        previewCheckbox.show();
+
         svgexportCheckbox.set_label(Glib::ustring(_("Export as SVG 1.1 per settings in Preferences dialog")));
         svgexportCheckbox.set_active(enableSVGExport);
 
         svgexportCheckbox.signal_toggled().connect(sigc::mem_fun(*this, &FileDialogBaseGtk::_svgexportEnabledCB));
 
+        svgexportCheckbox.show();
+
         // Catch selection-changed events, so we can adjust the text widget
         signal_update_preview().connect(sigc::mem_fun(*this, &FileDialogBaseGtk::_updatePreviewCallback));
 
@@ -220,7 +209,7 @@ FileOpenDialogImplGtk::FileOpenDialogImplGtk(Gtk::Window &parentWindow, const Gl
 
 
     /* Set the pwd and/or the filename */
-    if (dir.size() > 0) {
+    if (dir.size() > 0 && Glib::getenv("GTK_USE_PORTAL").empty()) {
         Glib::ustring udir(dir);
         Glib::ustring::size_type len = udir.length();
         // leaving a trailing backslash on the directory name leads to the infamous
@@ -241,9 +230,6 @@ FileOpenDialogImplGtk::FileOpenDialogImplGtk(Gtk::Window &parentWindow, const Gl
     //###### Add the file types menu
     createFilterMenu();
 
-    add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
-    set_default(*add_button(_("_Open"), Gtk::RESPONSE_OK));
-
     //###### Allow easy access to our examples folder
 
     using namespace Inkscape::IO::Resource;
@@ -372,12 +358,11 @@ void FileOpenDialogImplGtk::createFilterMenu()
 bool FileOpenDialogImplGtk::show()
 {
     set_modal(TRUE); // Window
-    sp_transientize(GTK_WIDGET(gobj())); // Make transient
     gint b = run(); // Dialog
     svgPreview.showNoPreview();
     hide();
 
-    if (b == Gtk::RESPONSE_OK) {
+    if (b == Gtk::RESPONSE_ACCEPT) {
         // This is a hack, to avoid the warning messages that
         // Gtk::FileChooser::get_filter() returns
         // should be:  Gtk::FileFilter *filter = get_filter();
@@ -511,37 +496,18 @@ FileSaveDialogImplGtk::FileSaveDialogImplGtk(Gtk::Window &parentWindow, const Gl
 
     childBox.pack_start(checksBox);
     childBox.pack_end(fileTypeComboBox);
+    childBox.show();
+
     checksBox.pack_start(fileTypeCheckbox);
     checksBox.pack_start(previewCheckbox);
     checksBox.pack_start(svgexportCheckbox);
+    checksBox.show();
 
     set_extra_widget(childBox);
 
-    // Let's do some customization
-    fileNameEntry = nullptr;
-    Gtk::Container *cont = get_toplevel();
-    std::vector<Gtk::Entry *> entries;
-    findEntryWidgets(cont, entries);
-    // g_message("Found %d entry widgets\n", entries.size());
-    if (!entries.empty()) {
-        // Catch when user hits [return] on the text field
-        fileNameEntry = entries[0];
-        fileNameEntry->signal_activate().connect(
-            sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameEntryChangedCallback));
-    }
     signal_selection_changed().connect(
         sigc::mem_fun(*this, &FileSaveDialogImplGtk::fileNameChanged));
 
-    // Let's do more customization
-    std::vector<Gtk::Expander *> expanders;
-    findExpanderWidgets(cont, expanders);
-    // g_message("Found %d expander widgets\n", expanders.size());
-    if (!expanders.empty()) {
-        // Always show the file list
-        Gtk::Expander *expander = expanders[0];
-        expander->set_expanded(true);
-    }
-
     // allow easy access to the user's own templates folder
     using namespace Inkscape::IO::Resource;
     char const *templates = Inkscape::IO::Resource::get_path(USER, TEMPLATES);
@@ -549,14 +515,6 @@ FileSaveDialogImplGtk::FileSaveDialogImplGtk(Gtk::Window &parentWindow, const Gl
         Inkscape::IO::file_test(templates, G_FILE_TEST_IS_DIR) && g_path_is_absolute(templates)) {
         add_shortcut_folder(templates);
     }
-
-    // if (extension == NULL)
-    //    checkbox.set_sensitive(FALSE);
-
-    add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
-    set_default(*add_button(_("_Save"), Gtk::RESPONSE_OK));
-
-    show_all_children();
 }
 
 /**
@@ -566,44 +524,7 @@ FileSaveDialogImplGtk::~FileSaveDialogImplGtk()
 = default;
 
 /**
- * Callback for fileNameEntry widget
- */
-void FileSaveDialogImplGtk::fileNameEntryChangedCallback()
-{
-    if (!fileNameEntry)
-        return;
-
-    Glib::ustring fileName = fileNameEntry->get_text();
-    if (!Glib::get_charset()) // If we are not utf8
-        fileName = Glib::filename_to_utf8(fileName);
-
-    // g_message("User hit return.  Text is '%s'\n", fileName.c_str());
-
-    if (!Glib::path_is_absolute(fileName)) {
-        // try appending to the current path
-        // not this way: fileName = get_current_folder() + "/" + fileName;
-        std::vector<Glib::ustring> pathSegments;
-        pathSegments.emplace_back(get_current_folder());
-        pathSegments.push_back(fileName);
-        fileName = Glib::build_filename(pathSegments);
-    }
-
-    // g_message("path:'%s'\n", fileName.c_str());
-
-    if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
-        set_current_folder(fileName);
-    } else if (/*Glib::file_test(fileName, Glib::FILE_TEST_IS_REGULAR)*/ true) {
-        // dialog with either (1) select a regular file or (2) cd to dir
-        // simulate an 'OK'
-        set_filename(fileName);
-        response(Gtk::RESPONSE_OK);
-    }
-}
-
-
-
-/**
- * Callback for fileNameEntry widget
+ * Callback for fileType widget changing
  */
 void FileSaveDialogImplGtk::fileTypeChangedCallback()
 {
@@ -649,6 +570,10 @@ void FileSaveDialogImplGtk::addFileType(Glib::ustring name, Glib::ustring patter
     fileTypeComboBox.append(guessType.name);
     fileTypes.push_back(guessType);
 
+    auto filter = Gtk::FileFilter::create();
+    filter->set_name(guessType.name);
+    filter->add_pattern(guessType.pattern);
+    add_filter(filter);
 
     fileTypeComboBox.set_active(0);
     fileTypeChangedCallback(); // call at least once to set the filter
@@ -656,8 +581,13 @@ void FileSaveDialogImplGtk::addFileType(Glib::ustring name, Glib::ustring patter
 
 void FileSaveDialogImplGtk::createFilterMenu()
 {
+    if (_dialogType == CUSTOM_TYPE) {
+        return;
+    }
+
     Inkscape::Extension::DB::OutputList extension_list;
     Inkscape::Extension::db.get_output_list(extension_list);
+
     knownExtensions.clear();
 
     bool is_raster = _dialogType == RASTER_TYPES;
@@ -680,6 +610,11 @@ void FileSaveDialogImplGtk::createFilterMenu()
         type.extension = omod;
         fileTypeComboBox.append(type.name);
         fileTypes.push_back(type);
+
+        auto filter = Gtk::FileFilter::create();
+        filter->set_name(type.name);
+        filter->add_pattern(type.pattern);
+        add_filter(filter);
     }
 
     //#Let user choose
@@ -690,6 +625,10 @@ void FileSaveDialogImplGtk::createFilterMenu()
     fileTypeComboBox.append(guessType.name);
     fileTypes.push_back(guessType);
 
+    auto filter = Gtk::FileFilter::create();
+    filter->set_name(guessType.name);
+    filter->add_pattern(guessType.pattern);
+    add_filter(filter);
 
     fileTypeComboBox.set_active(0);
     fileTypeChangedCallback(); // call at least once to set the filter
@@ -704,13 +643,12 @@ bool FileSaveDialogImplGtk::show()
 {
     change_path(myFilename);
     set_modal(TRUE); // Window
-    sp_transientize(GTK_WIDGET(gobj())); // Make transient
     gint b = run(); // Dialog
     svgPreview.showNoPreview();
     set_preview_widget_active(false);
     hide();
 
-    if (b == Gtk::RESPONSE_OK) {
+    if (b == Gtk::RESPONSE_ACCEPT) {
         updateNameAndExtension();
         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
@@ -799,6 +737,11 @@ FileSaveDialogImplGtk::change_title(const Glib::ustring& title)
   */
 void FileSaveDialogImplGtk::change_path(const Glib::ustring &path)
 {
+    if (!Glib::getenv("GTK_USE_PORTAL").empty()) {
+        // If we're using the portal we can't control the path
+        return;
+    }
+
     myFilename = path;
 
     if (Glib::file_test(myFilename, Glib::FILE_TEST_IS_DIR)) {
@@ -842,6 +785,27 @@ void FileSaveDialogImplGtk::updateNameAndExtension()
         myFilename = tmp;
     }
 
+    if (!Glib::getenv("GTK_USE_PORTAL").empty()) {
+        // If we're using the portal we can't change the filename
+	// and we need to use the filter to find the extension
+        GtkFileChooser *gtkFileChooser = Gtk::FileChooser::gobj();
+        GtkFileFilter *filter = gtk_file_chooser_get_filter(gtkFileChooser);
+
+        extension = nullptr;
+        if (filter) {
+            auto name = gtk_file_filter_get_name(filter);
+
+            for (auto type : fileTypes) {
+                if (type.name == name) {
+                    extension = type.extension;
+                    break;
+                }
+            }
+        }
+
+        return;
+    }
+
     Inkscape::Extension::Output *newOut = extension ? dynamic_cast<Inkscape::Extension::Output *>(extension) : nullptr;
     if (fileTypeCheckbox.get_active() && newOut) {
         // Append the file extension if it's not already present and display it in the file name entry field
diff --git a/src/ui/dialog/filedialogimpl-gtkmm.h b/src/ui/dialog/filedialogimpl-gtkmm.h
index b16d36285c300d1c423e1e75f3260baaf8276b2b..8fde5824ab79f04115441bded47550aebab2ebea 100644
--- a/src/ui/dialog/filedialogimpl-gtkmm.h
+++ b/src/ui/dialog/filedialogimpl-gtkmm.h
@@ -49,14 +49,6 @@ void
 fileDialogExtensionToPattern(Glib::ustring &pattern,
                       Glib::ustring &extension);
 
-void
-findEntryWidgets(Gtk::Container *parent,
-                 std::vector<Gtk::Entry *> &result);
-
-void
-findExpanderWidgets(Gtk::Container *parent,
-                    std::vector<Gtk::Expander *> &result);
-
 class FileType
 {
     public:
@@ -75,7 +67,7 @@ class FileType
  * This class is the base implementation for the others.  This
  * reduces redundancies and bugs.
  */
-class FileDialogBaseGtk : public Gtk::FileChooserDialog
+class FileDialogBaseGtk : public Gtk::FileChooserNative
 {
 public:
 
@@ -84,19 +76,7 @@ public:
      */
     FileDialogBaseGtk(Gtk::Window& parentWindow, const Glib::ustring &title,
     		Gtk::FileChooserAction dialogType, FileDialogType type, gchar const* preferenceBase) :
-        Gtk::FileChooserDialog(parentWindow, title, dialogType),
-        preferenceBase(preferenceBase ? preferenceBase : "unknown"),
-        _dialogType(type)
-    {
-        internalSetup();
-    }
-
-    /**
-     *
-     */
-    FileDialogBaseGtk(Gtk::Window& parentWindow, const char *title,
-                   Gtk::FileChooserAction dialogType, FileDialogType type, gchar const* preferenceBase) :
-        Gtk::FileChooserDialog(parentWindow, title, dialogType),
+        Gtk::FileChooserNative(title, parentWindow, dialogType, accept_label(dialogType), cancel_label()),
         preferenceBase(preferenceBase ? preferenceBase : "unknown"),
         _dialogType(type)
     {
@@ -130,6 +110,9 @@ protected:
     Gtk::CheckButton svgexportCheckbox;
 
 private:
+    const char * accept_label(Gtk::FileChooserAction dialogType);
+    const char * cancel_label();
+
     void internalSetup();
 
     /**
@@ -281,9 +264,8 @@ private:
     Inkscape::Extension::Extension *extension;
 
     /**
-     * Callback for user input into fileNameEntry
+     * Callback for file name changed
      */
-    void fileNameEntryChangedCallback();
     void fileNameChanged();
     bool fromCB;
 };