From e2470b2c8ba8d3cf39b651c3ed5f4a40f6341dc5 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 15 Dec 2022 22:58:43 +0000 Subject: [PATCH] Allow selecting and copying text in wxMessageDialog on GTK Normally the GTK message dialogs have non-selectable text, but that also means users can't copy the text. This can be inconvenient if the dialog is used for error messages, since then the error message can't be copied. Instead, make the text labels used in the GTK message dialog selectable to fix that. See #23039. (cherry picked from commit a8ea3eda116ae895446fc720943702f0c9c52b90) --- docs/changes.txt | 1 + src/gtk/msgdlg.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index cf3b6c9ba2..4aefb01833 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -268,6 +268,7 @@ wxGTK: - Fix compilation with GTK 3.22.z and z < 25 (#22816). - Fix compilation with GTK < 2.21.8 (mkirkham, #22830). - Fix display artefacts when using AUI without compositor under X11 (#23135). +- Allow selecting and copying text in wxMessageDialog (Ian McInerney, #23039). wxMSW: diff --git a/src/gtk/msgdlg.cpp b/src/gtk/msgdlg.cpp index b617297f11..b3b4f1add3 100644 --- a/src/gtk/msgdlg.cpp +++ b/src/gtk/msgdlg.cpp @@ -23,6 +23,7 @@ #include "wx/modalhook.h" #include "wx/gtk/private.h" +#include "wx/gtk/private/list.h" #include "wx/gtk/private/messagetype.h" #include "wx/gtk/private/mnemonics.h" #include "wx/gtk/private/dialogcount.h" @@ -187,6 +188,26 @@ void wxMessageDialog::GTKCreateMsgDialog() gtk_window_set_keep_above(GTK_WINDOW(m_widget), TRUE); } + // A GTKMessageDialog usually displays its labels without selection enabled, + // so we enable selection to allow the user to select+copy the text out of + // the dialog. + { + GtkMessageDialog * const msgdlg = GTK_MESSAGE_DIALOG(m_widget); + + GtkWidget* const area = gtk_message_dialog_get_message_area(msgdlg); + wxGtkList labels(gtk_container_get_children(GTK_CONTAINER(area))); + + for ( GList* elem = labels; elem; elem = elem->next ) + { + GtkWidget* const widget = GTK_WIDGET( elem->data ); + + if ( GTK_IS_LABEL(widget) ) + { + gtk_label_set_selectable(GTK_LABEL(widget), TRUE); + } + } + } + // we need to add buttons manually if we use custom labels or always for // Yes/No/Cancel dialog as GTK+ doesn't support it natively const bool addButtons = buttons == GTK_BUTTONS_NONE;