1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qgtk3dialoghelpers.h"
41 #include "qgtk3theme.h"
42 
43 #include <qeventloop.h>
44 #include <qwindow.h>
45 #include <qcolor.h>
46 #include <qdebug.h>
47 #include <qfont.h>
48 
49 #include <private/qguiapplication_p.h>
50 #include <qpa/qplatformfontdatabase.h>
51 
52 #undef signals
53 #include <gtk/gtk.h>
54 #include <gdk/gdk.h>
55 #include <gdk/gdkx.h>
56 #include <pango/pango.h>
57 
58 QT_BEGIN_NAMESPACE
59 
60 class QGtk3Dialog : public QWindow
61 {
62     Q_OBJECT
63 
64 public:
65     QGtk3Dialog(GtkWidget *gtkWidget);
66     ~QGtk3Dialog();
67 
68     GtkDialog *gtkDialog() const;
69 
70     void exec();
71     bool show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent);
72     void hide();
73 
74 Q_SIGNALS:
75     void accept();
76     void reject();
77 
78 protected:
79     static void onResponse(QGtk3Dialog *dialog, int response);
80 
81 private slots:
82     void onParentWindowDestroyed();
83 
84 private:
85     GtkWidget *gtkWidget;
86 };
87 
QGtk3Dialog(GtkWidget * gtkWidget)88 QGtk3Dialog::QGtk3Dialog(GtkWidget *gtkWidget) : gtkWidget(gtkWidget)
89 {
90     g_signal_connect_swapped(G_OBJECT(gtkWidget), "response", G_CALLBACK(onResponse), this);
91     g_signal_connect(G_OBJECT(gtkWidget), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
92 }
93 
~QGtk3Dialog()94 QGtk3Dialog::~QGtk3Dialog()
95 {
96     gtk_clipboard_store(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));
97     gtk_widget_destroy(gtkWidget);
98 }
99 
gtkDialog() const100 GtkDialog *QGtk3Dialog::gtkDialog() const
101 {
102     return GTK_DIALOG(gtkWidget);
103 }
104 
exec()105 void QGtk3Dialog::exec()
106 {
107     if (modality() == Qt::ApplicationModal) {
108         // block input to the whole app, including other GTK dialogs
109         gtk_dialog_run(gtkDialog());
110     } else {
111         // block input to the window, allow input to other GTK dialogs
112         QEventLoop loop;
113         connect(this, SIGNAL(accept()), &loop, SLOT(quit()));
114         connect(this, SIGNAL(reject()), &loop, SLOT(quit()));
115         loop.exec();
116     }
117 }
118 
show(Qt::WindowFlags flags,Qt::WindowModality modality,QWindow * parent)119 bool QGtk3Dialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
120 {
121     if (parent) {
122         connect(parent, &QWindow::destroyed, this, &QGtk3Dialog::onParentWindowDestroyed,
123                 Qt::UniqueConnection);
124     }
125     setParent(parent);
126     setFlags(flags);
127     setModality(modality);
128 
129     gtk_widget_realize(gtkWidget); // creates X window
130 
131     GdkWindow *gdkWindow = gtk_widget_get_window(gtkWidget);
132     if (parent) {
133         if (GDK_IS_X11_WINDOW(gdkWindow)) {
134             GdkDisplay *gdkDisplay = gdk_window_get_display(gdkWindow);
135             XSetTransientForHint(gdk_x11_display_get_xdisplay(gdkDisplay),
136                                  gdk_x11_window_get_xid(gdkWindow),
137                                  parent->winId());
138         }
139     }
140 
141     if (modality != Qt::NonModal) {
142         gdk_window_set_modal_hint(gdkWindow, true);
143         QGuiApplicationPrivate::showModalWindow(this);
144     }
145 
146     gtk_widget_show(gtkWidget);
147     gdk_window_focus(gdkWindow, GDK_CURRENT_TIME);
148     return true;
149 }
150 
hide()151 void QGtk3Dialog::hide()
152 {
153     QGuiApplicationPrivate::hideModalWindow(this);
154     gtk_widget_hide(gtkWidget);
155 }
156 
onResponse(QGtk3Dialog * dialog,int response)157 void QGtk3Dialog::onResponse(QGtk3Dialog *dialog, int response)
158 {
159     if (response == GTK_RESPONSE_OK)
160         emit dialog->accept();
161     else
162         emit dialog->reject();
163 }
164 
onParentWindowDestroyed()165 void QGtk3Dialog::onParentWindowDestroyed()
166 {
167     // The QGtk3*DialogHelper classes own this object. Make sure the parent doesn't delete it.
168     setParent(nullptr);
169 }
170 
QGtk3ColorDialogHelper()171 QGtk3ColorDialogHelper::QGtk3ColorDialogHelper()
172 {
173     d.reset(new QGtk3Dialog(gtk_color_chooser_dialog_new("", nullptr)));
174     connect(d.data(), SIGNAL(accept()), this, SLOT(onAccepted()));
175     connect(d.data(), SIGNAL(reject()), this, SIGNAL(reject()));
176 
177     g_signal_connect_swapped(d->gtkDialog(), "notify::rgba", G_CALLBACK(onColorChanged), this);
178 }
179 
~QGtk3ColorDialogHelper()180 QGtk3ColorDialogHelper::~QGtk3ColorDialogHelper()
181 {
182 }
183 
show(Qt::WindowFlags flags,Qt::WindowModality modality,QWindow * parent)184 bool QGtk3ColorDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
185 {
186     applyOptions();
187     return d->show(flags, modality, parent);
188 }
189 
exec()190 void QGtk3ColorDialogHelper::exec()
191 {
192     d->exec();
193 }
194 
hide()195 void QGtk3ColorDialogHelper::hide()
196 {
197     d->hide();
198 }
199 
setCurrentColor(const QColor & color)200 void QGtk3ColorDialogHelper::setCurrentColor(const QColor &color)
201 {
202     GtkDialog *gtkDialog = d->gtkDialog();
203     if (color.alpha() < 255)
204         gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(gtkDialog), true);
205     GdkRGBA gdkColor;
206     gdkColor.red = color.redF();
207     gdkColor.green = color.greenF();
208     gdkColor.blue = color.blueF();
209     gdkColor.alpha = color.alphaF();
210     gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(gtkDialog), &gdkColor);
211 }
212 
currentColor() const213 QColor QGtk3ColorDialogHelper::currentColor() const
214 {
215     GtkDialog *gtkDialog = d->gtkDialog();
216     GdkRGBA gdkColor;
217     gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(gtkDialog), &gdkColor);
218     return QColor::fromRgbF(gdkColor.red, gdkColor.green, gdkColor.blue, gdkColor.alpha);
219 }
220 
onAccepted()221 void QGtk3ColorDialogHelper::onAccepted()
222 {
223     emit accept();
224 }
225 
onColorChanged(QGtk3ColorDialogHelper * dialog)226 void QGtk3ColorDialogHelper::onColorChanged(QGtk3ColorDialogHelper *dialog)
227 {
228     emit dialog->currentColorChanged(dialog->currentColor());
229 }
230 
applyOptions()231 void QGtk3ColorDialogHelper::applyOptions()
232 {
233     GtkDialog *gtkDialog = d->gtkDialog();
234     gtk_window_set_title(GTK_WINDOW(gtkDialog), qUtf8Printable(options()->windowTitle()));
235 
236     gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(gtkDialog), options()->testOption(QColorDialogOptions::ShowAlphaChannel));
237 }
238 
QGtk3FileDialogHelper()239 QGtk3FileDialogHelper::QGtk3FileDialogHelper()
240 {
241     d.reset(new QGtk3Dialog(gtk_file_chooser_dialog_new("", nullptr,
242                                                         GTK_FILE_CHOOSER_ACTION_OPEN,
243                                                         qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Cancel)), GTK_RESPONSE_CANCEL,
244                                                         qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Ok)), GTK_RESPONSE_OK,
245                                                         NULL)));
246 
247     connect(d.data(), SIGNAL(accept()), this, SLOT(onAccepted()));
248     connect(d.data(), SIGNAL(reject()), this, SIGNAL(reject()));
249 
250     g_signal_connect(GTK_FILE_CHOOSER(d->gtkDialog()), "selection-changed", G_CALLBACK(onSelectionChanged), this);
251     g_signal_connect_swapped(GTK_FILE_CHOOSER(d->gtkDialog()), "current-folder-changed", G_CALLBACK(onCurrentFolderChanged), this);
252     g_signal_connect_swapped(GTK_FILE_CHOOSER(d->gtkDialog()), "notify::filter", G_CALLBACK(onFilterChanged), this);
253 }
254 
~QGtk3FileDialogHelper()255 QGtk3FileDialogHelper::~QGtk3FileDialogHelper()
256 {
257 }
258 
show(Qt::WindowFlags flags,Qt::WindowModality modality,QWindow * parent)259 bool QGtk3FileDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
260 {
261     _dir.clear();
262     _selection.clear();
263 
264     applyOptions();
265     return d->show(flags, modality, parent);
266 }
267 
exec()268 void QGtk3FileDialogHelper::exec()
269 {
270     d->exec();
271 }
272 
hide()273 void QGtk3FileDialogHelper::hide()
274 {
275     // After GtkFileChooserDialog has been hidden, gtk_file_chooser_get_current_folder()
276     // & gtk_file_chooser_get_filenames() will return bogus values -> cache the actual
277     // values before hiding the dialog
278     _dir = directory();
279     _selection = selectedFiles();
280 
281     d->hide();
282 }
283 
defaultNameFilterDisables() const284 bool QGtk3FileDialogHelper::defaultNameFilterDisables() const
285 {
286     return false;
287 }
288 
setDirectory(const QUrl & directory)289 void QGtk3FileDialogHelper::setDirectory(const QUrl &directory)
290 {
291     GtkDialog *gtkDialog = d->gtkDialog();
292     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(directory.toLocalFile()));
293 }
294 
directory() const295 QUrl QGtk3FileDialogHelper::directory() const
296 {
297     // While GtkFileChooserDialog is hidden, gtk_file_chooser_get_current_folder()
298     // returns a bogus value -> return the cached value before hiding
299     if (!_dir.isEmpty())
300         return _dir;
301 
302     QString ret;
303     GtkDialog *gtkDialog = d->gtkDialog();
304     gchar *folder = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(gtkDialog));
305     if (folder) {
306         ret = QString::fromUtf8(folder);
307         g_free(folder);
308     }
309     return QUrl::fromLocalFile(ret);
310 }
311 
selectFile(const QUrl & filename)312 void QGtk3FileDialogHelper::selectFile(const QUrl &filename)
313 {
314     setFileChooserAction();
315     selectFileInternal(filename);
316 }
317 
selectFileInternal(const QUrl & filename)318 void QGtk3FileDialogHelper::selectFileInternal(const QUrl &filename)
319 {
320     GtkDialog *gtkDialog = d->gtkDialog();
321     if (options()->acceptMode() == QFileDialogOptions::AcceptSave) {
322         QFileInfo fi(filename.toLocalFile());
323         gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(fi.path()));
324         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(fi.fileName()));
325     } else {
326         gtk_file_chooser_select_filename(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(filename.toLocalFile()));
327     }
328 }
329 
selectedFiles() const330 QList<QUrl> QGtk3FileDialogHelper::selectedFiles() const
331 {
332     // While GtkFileChooserDialog is hidden, gtk_file_chooser_get_filenames()
333     // returns a bogus value -> return the cached value before hiding
334     if (!_selection.isEmpty())
335         return _selection;
336 
337     QList<QUrl> selection;
338     GtkDialog *gtkDialog = d->gtkDialog();
339     GSList *filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(gtkDialog));
340     for (GSList *it  = filenames; it; it = it->next)
341         selection += QUrl::fromLocalFile(QString::fromUtf8((const char*)it->data));
342     g_slist_free(filenames);
343     return selection;
344 }
345 
setFilter()346 void QGtk3FileDialogHelper::setFilter()
347 {
348     applyOptions();
349 }
350 
selectNameFilter(const QString & filter)351 void QGtk3FileDialogHelper::selectNameFilter(const QString &filter)
352 {
353     GtkFileFilter *gtkFilter = _filters.value(filter);
354     if (gtkFilter) {
355         GtkDialog *gtkDialog = d->gtkDialog();
356         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(gtkDialog), gtkFilter);
357     }
358 }
359 
selectedNameFilter() const360 QString QGtk3FileDialogHelper::selectedNameFilter() const
361 {
362     GtkDialog *gtkDialog = d->gtkDialog();
363     GtkFileFilter *gtkFilter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(gtkDialog));
364     return _filterNames.value(gtkFilter);
365 }
366 
onAccepted()367 void QGtk3FileDialogHelper::onAccepted()
368 {
369     emit accept();
370 }
371 
onSelectionChanged(GtkDialog * gtkDialog,QGtk3FileDialogHelper * helper)372 void QGtk3FileDialogHelper::onSelectionChanged(GtkDialog *gtkDialog, QGtk3FileDialogHelper *helper)
373 {
374     QString selection;
375     gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(gtkDialog));
376     if (filename) {
377         selection = QString::fromUtf8(filename);
378         g_free(filename);
379     }
380     emit helper->currentChanged(QUrl::fromLocalFile(selection));
381 }
382 
onCurrentFolderChanged(QGtk3FileDialogHelper * dialog)383 void QGtk3FileDialogHelper::onCurrentFolderChanged(QGtk3FileDialogHelper *dialog)
384 {
385     emit dialog->directoryEntered(dialog->directory());
386 }
387 
onFilterChanged(QGtk3FileDialogHelper * dialog)388 void QGtk3FileDialogHelper::onFilterChanged(QGtk3FileDialogHelper *dialog)
389 {
390     emit dialog->filterSelected(dialog->selectedNameFilter());
391 }
392 
gtkFileChooserAction(const QSharedPointer<QFileDialogOptions> & options)393 static GtkFileChooserAction gtkFileChooserAction(const QSharedPointer<QFileDialogOptions> &options)
394 {
395     switch (options->fileMode()) {
396     case QFileDialogOptions::AnyFile:
397     case QFileDialogOptions::ExistingFile:
398     case QFileDialogOptions::ExistingFiles:
399         if (options->acceptMode() == QFileDialogOptions::AcceptOpen)
400             return GTK_FILE_CHOOSER_ACTION_OPEN;
401         else
402             return GTK_FILE_CHOOSER_ACTION_SAVE;
403     case QFileDialogOptions::Directory:
404     case QFileDialogOptions::DirectoryOnly:
405     default:
406         if (options->acceptMode() == QFileDialogOptions::AcceptOpen)
407             return GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
408         else
409             return GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
410     }
411 }
412 
setFileChooserAction()413 void QGtk3FileDialogHelper::setFileChooserAction()
414 {
415     GtkDialog *gtkDialog = d->gtkDialog();
416 
417     const GtkFileChooserAction action = gtkFileChooserAction(options());
418     gtk_file_chooser_set_action(GTK_FILE_CHOOSER(gtkDialog), action);
419 }
420 
applyOptions()421 void QGtk3FileDialogHelper::applyOptions()
422 {
423     GtkDialog *gtkDialog = d->gtkDialog();
424     const QSharedPointer<QFileDialogOptions> &opts = options();
425 
426     gtk_window_set_title(GTK_WINDOW(gtkDialog), qUtf8Printable(opts->windowTitle()));
427     gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(gtkDialog), true);
428 
429     setFileChooserAction();
430 
431     const bool selectMultiple = opts->fileMode() == QFileDialogOptions::ExistingFiles;
432     gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(gtkDialog), selectMultiple);
433 
434     const bool confirmOverwrite = !opts->testOption(QFileDialogOptions::DontConfirmOverwrite);
435     gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(gtkDialog), confirmOverwrite);
436 
437     const bool readOnly = opts->testOption(QFileDialogOptions::ReadOnly);
438     gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(gtkDialog), !readOnly);
439 
440     const QStringList nameFilters = opts->nameFilters();
441     if (!nameFilters.isEmpty())
442         setNameFilters(nameFilters);
443 
444     if (opts->initialDirectory().isLocalFile())
445         setDirectory(opts->initialDirectory());
446 
447     foreach (const QUrl &filename, opts->initiallySelectedFiles())
448         selectFileInternal(filename);
449 
450     const QString initialNameFilter = opts->initiallySelectedNameFilter();
451     if (!initialNameFilter.isEmpty())
452         selectNameFilter(initialNameFilter);
453 
454     GtkWidget *acceptButton = gtk_dialog_get_widget_for_response(gtkDialog, GTK_RESPONSE_OK);
455     if (acceptButton) {
456         if (opts->isLabelExplicitlySet(QFileDialogOptions::Accept))
457             gtk_button_set_label(GTK_BUTTON(acceptButton), qUtf8Printable(opts->labelText(QFileDialogOptions::Accept)));
458         else if (opts->acceptMode() == QFileDialogOptions::AcceptOpen)
459             gtk_button_set_label(GTK_BUTTON(acceptButton), qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Open)));
460         else
461             gtk_button_set_label(GTK_BUTTON(acceptButton), qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Save)));
462     }
463 
464     GtkWidget *rejectButton = gtk_dialog_get_widget_for_response(gtkDialog, GTK_RESPONSE_CANCEL);
465     if (rejectButton) {
466         if (opts->isLabelExplicitlySet(QFileDialogOptions::Reject))
467             gtk_button_set_label(GTK_BUTTON(rejectButton), qUtf8Printable(opts->labelText(QFileDialogOptions::Reject)));
468         else
469             gtk_button_set_label(GTK_BUTTON(rejectButton), qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Cancel)));
470     }
471 }
472 
setNameFilters(const QStringList & filters)473 void QGtk3FileDialogHelper::setNameFilters(const QStringList &filters)
474 {
475     GtkDialog *gtkDialog = d->gtkDialog();
476     foreach (GtkFileFilter *filter, _filters)
477         gtk_file_chooser_remove_filter(GTK_FILE_CHOOSER(gtkDialog), filter);
478 
479     _filters.clear();
480     _filterNames.clear();
481 
482     foreach (const QString &filter, filters) {
483         GtkFileFilter *gtkFilter = gtk_file_filter_new();
484         const QString name = filter.left(filter.indexOf(QLatin1Char('(')));
485         const QStringList extensions = cleanFilterList(filter);
486 
487         gtk_file_filter_set_name(gtkFilter, qUtf8Printable(name.isEmpty() ? extensions.join(QLatin1String(", ")) : name));
488         foreach (const QString &ext, extensions)
489             gtk_file_filter_add_pattern(gtkFilter, qUtf8Printable(ext));
490 
491         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(gtkDialog), gtkFilter);
492 
493         _filters.insert(filter, gtkFilter);
494         _filterNames.insert(gtkFilter, filter);
495     }
496 }
497 
QGtk3FontDialogHelper()498 QGtk3FontDialogHelper::QGtk3FontDialogHelper()
499 {
500     d.reset(new QGtk3Dialog(gtk_font_chooser_dialog_new("", nullptr)));
501     connect(d.data(), SIGNAL(accept()), this, SLOT(onAccepted()));
502     connect(d.data(), SIGNAL(reject()), this, SIGNAL(reject()));
503 
504     g_signal_connect_swapped(d->gtkDialog(), "notify::font", G_CALLBACK(onFontChanged), this);
505 }
506 
~QGtk3FontDialogHelper()507 QGtk3FontDialogHelper::~QGtk3FontDialogHelper()
508 {
509 }
510 
show(Qt::WindowFlags flags,Qt::WindowModality modality,QWindow * parent)511 bool QGtk3FontDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
512 {
513     applyOptions();
514     return d->show(flags, modality, parent);
515 }
516 
exec()517 void QGtk3FontDialogHelper::exec()
518 {
519     d->exec();
520 }
521 
hide()522 void QGtk3FontDialogHelper::hide()
523 {
524     d->hide();
525 }
526 
qt_fontToString(const QFont & font)527 static QString qt_fontToString(const QFont &font)
528 {
529     PangoFontDescription *desc = pango_font_description_new();
530     pango_font_description_set_size(desc, (font.pointSizeF() > 0.0 ? font.pointSizeF() : QFontInfo(font).pointSizeF()) * PANGO_SCALE);
531     pango_font_description_set_family(desc, qUtf8Printable(QFontInfo(font).family()));
532 
533     int weight = font.weight();
534     if (weight >= QFont::Black)
535         pango_font_description_set_weight(desc, PANGO_WEIGHT_HEAVY);
536     else if (weight >= QFont::ExtraBold)
537         pango_font_description_set_weight(desc, PANGO_WEIGHT_ULTRABOLD);
538     else if (weight >= QFont::Bold)
539         pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
540     else if (weight >= QFont::DemiBold)
541         pango_font_description_set_weight(desc, PANGO_WEIGHT_SEMIBOLD);
542     else if (weight >= QFont::Medium)
543         pango_font_description_set_weight(desc, PANGO_WEIGHT_MEDIUM);
544     else if (weight >= QFont::Normal)
545         pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
546     else if (weight >= QFont::Light)
547         pango_font_description_set_weight(desc, PANGO_WEIGHT_LIGHT);
548     else if (weight >= QFont::ExtraLight)
549         pango_font_description_set_weight(desc, PANGO_WEIGHT_ULTRALIGHT);
550     else
551         pango_font_description_set_weight(desc, PANGO_WEIGHT_THIN);
552 
553     int style = font.style();
554     if (style == QFont::StyleItalic)
555         pango_font_description_set_style(desc, PANGO_STYLE_ITALIC);
556     else if (style == QFont::StyleOblique)
557         pango_font_description_set_style(desc, PANGO_STYLE_OBLIQUE);
558     else
559         pango_font_description_set_style(desc, PANGO_STYLE_NORMAL);
560 
561     char *str = pango_font_description_to_string(desc);
562     QString name = QString::fromUtf8(str);
563     pango_font_description_free(desc);
564     g_free(str);
565     return name;
566 }
567 
qt_fontFromString(const QString & name)568 static QFont qt_fontFromString(const QString &name)
569 {
570     QFont font;
571     PangoFontDescription *desc = pango_font_description_from_string(qUtf8Printable(name));
572     font.setPointSizeF(static_cast<float>(pango_font_description_get_size(desc)) / PANGO_SCALE);
573 
574     QString family = QString::fromUtf8(pango_font_description_get_family(desc));
575     if (!family.isEmpty())
576         font.setFamily(family);
577 
578     const int weight = pango_font_description_get_weight(desc);
579     font.setWeight(QPlatformFontDatabase::weightFromInteger(weight));
580 
581     PangoStyle style = pango_font_description_get_style(desc);
582     if (style == PANGO_STYLE_ITALIC)
583         font.setStyle(QFont::StyleItalic);
584     else if (style == PANGO_STYLE_OBLIQUE)
585         font.setStyle(QFont::StyleOblique);
586     else
587         font.setStyle(QFont::StyleNormal);
588 
589     pango_font_description_free(desc);
590     return font;
591 }
592 
setCurrentFont(const QFont & font)593 void QGtk3FontDialogHelper::setCurrentFont(const QFont &font)
594 {
595     GtkFontChooser *gtkDialog = GTK_FONT_CHOOSER(d->gtkDialog());
596     gtk_font_chooser_set_font(gtkDialog, qUtf8Printable(qt_fontToString(font)));
597 }
598 
currentFont() const599 QFont QGtk3FontDialogHelper::currentFont() const
600 {
601     GtkFontChooser *gtkDialog = GTK_FONT_CHOOSER(d->gtkDialog());
602     gchar *name = gtk_font_chooser_get_font(gtkDialog);
603     QFont font = qt_fontFromString(QString::fromUtf8(name));
604     g_free(name);
605     return font;
606 }
607 
onAccepted()608 void QGtk3FontDialogHelper::onAccepted()
609 {
610     emit accept();
611 }
612 
onFontChanged(QGtk3FontDialogHelper * dialog)613 void QGtk3FontDialogHelper::onFontChanged(QGtk3FontDialogHelper *dialog)
614 {
615     emit dialog->currentFontChanged(dialog->currentFont());
616 }
617 
applyOptions()618 void QGtk3FontDialogHelper::applyOptions()
619 {
620     GtkDialog *gtkDialog = d->gtkDialog();
621     const QSharedPointer<QFontDialogOptions> &opts = options();
622 
623     gtk_window_set_title(GTK_WINDOW(gtkDialog), qUtf8Printable(opts->windowTitle()));
624 }
625 
626 QT_END_NAMESPACE
627 
628 #include "qgtk3dialoghelpers.moc"
629