[vte] widget: Remove GdkAtom usage where possible



commit c168ef7053363887ad3fd44ca92b43a8b964bcb2
Author: Christian Persch <chpe src gnome org>
Date:   Sat Oct 3 14:52:51 2020 +0200

    widget: Remove GdkAtom usage where possible
    
    [gtk4]

 src/cxx-utils.hh   | 11 +++++++++
 src/vte.cc         | 67 ++++++++++++++++++++++++++----------------------------
 src/vteaccess.cc   |  5 ++--
 src/vtegtk.cc      | 10 ++++----
 src/vteinternal.hh | 24 ++++++++-----------
 src/widget.hh      |  9 ++++++--
 6 files changed, 68 insertions(+), 58 deletions(-)
---
diff --git a/src/cxx-utils.hh b/src/cxx-utils.hh
index 1f3abad4..32ecd4f2 100644
--- a/src/cxx-utils.hh
+++ b/src/cxx-utils.hh
@@ -35,6 +35,17 @@ clamp(T const& v,
         return std::max(std::min(v, max_v), min_v);
 }
 
+// Converts from E to the underlying integral type, where E is an enum
+// with integral underlying type.
+template<typename E>
+inline constexpr auto to_integral(E e) noexcept
+        -> std::enable_if_t<std::is_enum_v<E> &&
+                            std::is_integral_v<std::underlying_type_t<E>>,
+                            std::underlying_type_t<E>>
+{
+        return static_cast<std::underlying_type_t<E>>(e);
+}
+
 #ifdef VTE_DEBUG
 void log_exception(char const* func = __builtin_FUNCTION(),
                    char const* filename = __builtin_FILE(),
diff --git a/src/vte.cc b/src/vte.cc
index 4af90581..07548b15 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -3560,8 +3560,8 @@ Terminal::process_incoming_utf8()
                         //FIXMEchpe: this is atrocious
                        auto selection = get_selected_text();
                        if ((selection == nullptr) ||
-                           (m_selection[VTE_SELECTION_PRIMARY] == nullptr) ||
-                           (strcmp(selection->str, m_selection[VTE_SELECTION_PRIMARY]->str) != 0)) {
+                           (m_selection[vte::to_integral(vte::platform::ClipboardType::PRIMARY)] == nullptr) 
||
+                           (strcmp(selection->str, 
m_selection[vte::to_integral(vte::platform::ClipboardType::PRIMARY)]->str) != 0)) {
                                deselect_all();
                        }
                         if (selection)
@@ -3845,8 +3845,8 @@ Terminal::process_incoming_pcterm()
                         //FIXMEchpe: this is atrocious
                        auto selection = get_selected_text();
                        if ((selection == nullptr) ||
-                           (m_selection[VTE_SELECTION_PRIMARY] == nullptr) ||
-                           (strcmp(selection->str, m_selection[VTE_SELECTION_PRIMARY]->str) != 0)) {
+                           (m_selection[vte::to_integral(vte::platform::ClipboardType::PRIMARY)] == nullptr) 
||
+                           (strcmp(selection->str, 
m_selection[vte::to_integral(vte::platform::ClipboardType::PRIMARY)]->str) != 0)) {
                                deselect_all();
                        }
                         if (selection)
@@ -4726,7 +4726,7 @@ Terminal::widget_key_press(KeyEvent const& event)
                                        handled = TRUE;
                                        suppress_alt_esc = TRUE;
                                } else {
-                                        widget_paste(GDK_SELECTION_PRIMARY);
+                                        widget_paste(vte::platform::ClipboardType::PRIMARY);
                                        handled = TRUE;
                                        suppress_alt_esc = TRUE;
                                }
@@ -5965,15 +5965,15 @@ Terminal::widget_clipboard_cleared(GtkClipboard *clipboard_)
         if (m_changing_selection)
                 return;
 
-       if (clipboard_ == m_clipboard[VTE_SELECTION_PRIMARY]) {
-               if (m_selection_owned[VTE_SELECTION_PRIMARY] &&
+       if (clipboard_ == get_clipboard(vte::platform::ClipboardType::PRIMARY)) {
+               if (m_selection_owned[vte::to_integral(vte::platform::ClipboardType::PRIMARY)] &&
                     !m_selection_resolved.empty()) {
                        _vte_debug_print(VTE_DEBUG_SELECTION, "Lost selection.\n");
                        deselect_all();
                }
-                m_selection_owned[VTE_SELECTION_PRIMARY] = false;
-       } else if (clipboard_ == m_clipboard[VTE_SELECTION_CLIPBOARD]) {
-                m_selection_owned[VTE_SELECTION_CLIPBOARD] = false;
+                m_selection_owned[vte::to_integral(vte::platform::ClipboardType::PRIMARY)] = false;
+       } else if (clipboard_ == get_clipboard(vte::platform::ClipboardType::CLIPBOARD)) {
+                m_selection_owned[vte::to_integral(vte::platform::ClipboardType::CLIPBOARD)] = false;
         }
 }
 
@@ -6008,8 +6008,10 @@ Terminal::widget_clipboard_requested(GtkClipboard *target_clipboard,
                                                GtkSelectionData *data,
                                                guint info)
 {
-       for (auto sel = 0; sel < LAST_VTE_SELECTION; sel++) {
-               if (target_clipboard == m_clipboard[sel] &&
+        for (auto sel_type : {vte::platform::ClipboardType::CLIPBOARD,
+                              vte::platform::ClipboardType::PRIMARY}) {
+                auto const sel = vte::to_integral(sel_type);
+               if (target_clipboard == get_clipboard(sel_type) &&
                     m_selection[sel] != nullptr) {
                        _VTE_DEBUG_IF(VTE_DEBUG_SELECTION) {
                                int i;
@@ -6549,16 +6551,17 @@ targets_for_format(VteFormat format,
 /* Place the selected text onto the clipboard.  Do this asynchronously so that
  * we get notified when the selection we placed on the clipboard is replaced. */
 void
-Terminal::widget_copy(VteSelection sel,
-                                VteFormat format)
+Terminal::widget_copy(vte::platform::ClipboardType type,
+                      VteFormat format)
 {
         /* Only put HTML on the CLIPBOARD, not PRIMARY */
-        g_assert(sel == VTE_SELECTION_CLIPBOARD || format == VTE_FORMAT_TEXT);
+        assert(type == vte::platform::ClipboardType::CLIPBOARD || format == VTE_FORMAT_TEXT);
 
        /* Chuck old selected text and retrieve the newly-selected text. */
         GArray *attributes = g_array_new(FALSE, TRUE, sizeof(struct _VteCharAttributes));
         auto selection = get_selected_text(attributes);
 
+        auto const sel = vte::to_integral(type);
         if (m_selection[sel]) {
                 g_string_free(m_selection[sel], TRUE);
                 m_selection[sel] = nullptr;
@@ -6587,7 +6590,7 @@ Terminal::widget_copy(VteSelection sel,
         auto targets = targets_for_format(format, &n_targets);
 
         m_changing_selection = true;
-        gtk_clipboard_set_with_data(m_clipboard[sel],
+        gtk_clipboard_set_with_data(get_clipboard(type),
                                     targets,
                                     n_targets,
                                     clipboard_copy_cb,
@@ -6595,25 +6598,21 @@ Terminal::widget_copy(VteSelection sel,
                                     this);
         m_changing_selection = false;
 
-        gtk_clipboard_set_can_store(m_clipboard[sel], nullptr, 0);
+        gtk_clipboard_set_can_store(get_clipboard(type), nullptr, 0);
         m_selection_owned[sel] = true;
         m_selection_format[sel] = format;
 }
 
 /* Paste from the given clipboard. */
 void
-Terminal::widget_paste(GdkAtom board)
+Terminal::widget_paste(vte::platform::ClipboardType selection)
 {
         if (!m_input_enabled)
                 return;
 
-       auto clip = gtk_clipboard_get_for_display(gtk_widget_get_display(m_widget), board);
-       if (!clip)
-                return;
-
         _vte_debug_print(VTE_DEBUG_SELECTION, "Requesting clipboard contents.\n");
 
-        m_paste_request.request_text(clip, &Terminal::widget_paste_received, this);
+        m_paste_request.request_text(get_clipboard(selection), &Terminal::widget_paste_received, this);
 }
 
 /* Confine coordinates into the visible area. Padding is already subtracted. */
@@ -6688,7 +6687,7 @@ Terminal::maybe_end_selection()
                /* Copy only if something was selected. */
                 if (!m_selection_resolved.empty() &&
                    m_selecting_had_delta) {
-                        widget_copy(VTE_SELECTION_PRIMARY, VTE_FORMAT_TEXT);
+                        widget_copy(vte::platform::ClipboardType::PRIMARY, VTE_FORMAT_TEXT);
                        emit_selection_changed();
                }
                 stop_autoscroll();  /* Required before setting m_selecting to false, see #105. */
@@ -6723,7 +6722,7 @@ Terminal::select_all()
 
        _vte_debug_print(VTE_DEBUG_SELECTION, "Selecting *all* text.\n");
 
-        widget_copy(VTE_SELECTION_PRIMARY, VTE_FORMAT_TEXT);
+        widget_copy(vte::platform::ClipboardType::PRIMARY, VTE_FORMAT_TEXT);
        emit_selection_changed();
 
        invalidate_all();
@@ -6917,7 +6916,7 @@ Terminal::widget_mouse_press(MouseEvent const& event)
                        if ((m_modifiers & GDK_SHIFT_MASK) ||
                            m_mouse_tracking_mode == MouseTrackingMode::eNONE) {
                                 if (widget()->primary_paste_enabled()) {
-                                        widget_paste(GDK_SELECTION_PRIMARY);
+                                        widget_paste(vte::platform::ClipboardType::PRIMARY);
                                         handled = true;
                                 }
                        }
@@ -7736,10 +7735,8 @@ Terminal::Terminal(vte::platform::Widget* w,
 
        /* Selection info. */
        display = gtk_widget_get_display(m_widget);
-       m_clipboard[VTE_SELECTION_PRIMARY] = gtk_clipboard_get_for_display(display, GDK_SELECTION_PRIMARY);
-       m_clipboard[VTE_SELECTION_CLIPBOARD] = gtk_clipboard_get_for_display(display, 
GDK_SELECTION_CLIPBOARD);
-        m_selection_owned[VTE_SELECTION_PRIMARY] = false;
-        m_selection_owned[VTE_SELECTION_CLIPBOARD] = false;
+       m_clipboard[vte::to_integral(vte::platform::ClipboardType::CLIPBOARD)] = 
gtk_clipboard_get_for_display(display, GDK_SELECTION_CLIPBOARD);
+       m_clipboard[vte::to_integral(vte::platform::ClipboardType::PRIMARY)] = 
gtk_clipboard_get_for_display(display, GDK_SELECTION_PRIMARY);
 
         /* Initialize the saved cursor. */
         save_cursor(&m_normal_screen);
@@ -7949,8 +7946,6 @@ Terminal::~Terminal()
         /* Make sure not to change selection while in destruction. See issue vte#89. */
         m_changing_selection = true;
 
-       int sel;
-
         terminate_child();
         unset_pty(false /* don't notify widget */);
         remove_update_timeout(this);
@@ -7976,12 +7971,14 @@ Terminal::~Terminal()
        /* Free any selected text, but if we currently own the selection,
         * throw the text onto the clipboard without an owner so that it
         * doesn't just disappear. */
-       for (sel = VTE_SELECTION_PRIMARY; sel < LAST_VTE_SELECTION; sel++) {
+        for (auto sel_type : {vte::platform::ClipboardType::CLIPBOARD,
+                              vte::platform::ClipboardType::PRIMARY}) {
+                auto const sel = vte::to_integral(sel_type);
                if (m_selection[sel] != nullptr) {
                        if (m_selection_owned[sel]) {
                                 // FIXMEchpe we should check m_selection_format[sel]
                                 // and also put text/html on if it's VTE_FORMAT_HTML
-                               gtk_clipboard_set_text(m_clipboard[sel],
+                               gtk_clipboard_set_text(get_clipboard(sel_type),
                                                       m_selection[sel]->str,
                                                       m_selection[sel]->len);
                        }
@@ -10074,7 +10071,7 @@ Terminal::select_text(vte::grid::column_t start_col,
        m_selecting_had_delta = true;
         m_selection_resolved.set ({ start_row, start_col },
                                   { end_row, end_col });
-        widget_copy(VTE_SELECTION_PRIMARY, VTE_FORMAT_TEXT);
+        widget_copy(vte::platform::ClipboardType::PRIMARY, VTE_FORMAT_TEXT);
        emit_selection_changed();
 
         invalidate_rows(start_row, end_row);
diff --git a/src/vteaccess.cc b/src/vteaccess.cc
index 336c3ccb..499c922b 100644
--- a/src/vteaccess.cc
+++ b/src/vteaccess.cc
@@ -34,6 +34,7 @@
 #include <vte/vte.h>
 #include "vteaccess.h"
 #include "vteinternal.hh"
+#include "widget.hh"
 
 #ifdef HAVE_LOCALE_H
 #include <locale.h>
@@ -1427,13 +1428,13 @@ vte_terminal_accessible_get_selection(AtkText *text, gint selection_number,
 
         try {
                 auto impl = IMPL_FROM_WIDGET(widget);
-                if (impl->m_selection_resolved.empty() || impl->m_selection[VTE_SELECTION_PRIMARY] == 
nullptr)
+                if (impl->m_selection_resolved.empty() || 
impl->m_selection[vte::to_integral(vte::platform::ClipboardType::PRIMARY)] == nullptr)
                         return nullptr;
 
                 *start_offset = offset_from_xy (priv, impl->m_selection_resolved.start_column(), 
impl->m_selection_resolved.start_row());
                 *end_offset = offset_from_xy (priv, impl->m_selection_resolved.end_column(), 
impl->m_selection_resolved.end_row());
 
-                return g_strdup(impl->m_selection[VTE_SELECTION_PRIMARY]->str);
+                return 
g_strdup(impl->m_selection[vte::to_integral(vte::platform::ClipboardType::PRIMARY)]->str);
         } catch (...) {
                 return nullptr;
         }
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
index bba76988..3666857f 100644
--- a/src/vtegtk.cc
+++ b/src/vtegtk.cc
@@ -187,7 +187,7 @@ static void
 vte_terminal_real_copy_clipboard(VteTerminal *terminal) noexcept
 try
 {
-       WIDGET(terminal)->copy(VTE_SELECTION_CLIPBOARD, VTE_FORMAT_TEXT);
+       WIDGET(terminal)->copy(vte::platform::ClipboardType::CLIPBOARD, VTE_FORMAT_TEXT);
 }
 catch (...)
 {
@@ -198,7 +198,7 @@ static void
 vte_terminal_real_paste_clipboard(VteTerminal *terminal) noexcept
 try
 {
-       WIDGET(terminal)->paste(GDK_SELECTION_CLIPBOARD);
+       WIDGET(terminal)->paste(vte::platform::ClipboardType::CLIPBOARD);
 }
 catch (...)
 {
@@ -2363,7 +2363,7 @@ try
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
         g_return_if_fail(format == VTE_FORMAT_TEXT || format == VTE_FORMAT_HTML);
 
-        WIDGET(terminal)->copy(VTE_SELECTION_CLIPBOARD, format);
+        WIDGET(terminal)->copy(vte::platform::ClipboardType::CLIPBOARD, format);
 }
 catch (...)
 {
@@ -2383,7 +2383,7 @@ try
 {
        g_return_if_fail(VTE_IS_TERMINAL(terminal));
        _vte_debug_print(VTE_DEBUG_SELECTION, "Copying to PRIMARY.\n");
-       WIDGET(terminal)->copy(VTE_SELECTION_PRIMARY, VTE_FORMAT_TEXT);
+       WIDGET(terminal)->copy(vte::platform::ClipboardType::PRIMARY, VTE_FORMAT_TEXT);
 }
 catch (...)
 {
@@ -2426,7 +2426,7 @@ try
 {
        g_return_if_fail(VTE_IS_TERMINAL(terminal));
        _vte_debug_print(VTE_DEBUG_SELECTION, "Pasting PRIMARY.\n");
-       WIDGET(terminal)->paste(GDK_SELECTION_PRIMARY);
+       WIDGET(terminal)->paste(vte::platform::ClipboardType::PRIMARY);
 }
 catch (...)
 {
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index 065c19d8..f2196d14 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -139,13 +139,6 @@ public:
 #define HTML_SELECTION
 #endif
 
-/* For unified handling of PRIMARY and CLIPBOARD selection */
-typedef enum {
-       VTE_SELECTION_PRIMARY,
-       VTE_SELECTION_CLIPBOARD,
-       LAST_VTE_SELECTION
-} VteSelection;
-
 /* Used in the GtkClipboard API, to distinguish requests for HTML and TEXT
  * contents of a clipboard */
 typedef enum {
@@ -248,6 +241,7 @@ namespace vte {
 
 namespace platform {
 class Widget;
+enum class ClipboardType;
 }
 
 namespace terminal {
@@ -675,11 +669,13 @@ public:
         vte::grid::span m_selection_resolved;
 
        /* Clipboard data information. */
-        bool m_selection_owned[LAST_VTE_SELECTION];
-        VteFormat m_selection_format[LAST_VTE_SELECTION];
-        bool m_changing_selection;
-        GString *m_selection[LAST_VTE_SELECTION];  // FIXMEegmont rename this so that m_selection_resolved 
can become m_selection?
-        GtkClipboard *m_clipboard[LAST_VTE_SELECTION];
+        bool m_selection_owned[2]{false, false};
+        bool m_changing_selection{false};
+        VteFormat m_selection_format[2];
+        GString *m_selection[2];  // FIXMEegmont rename this so that m_selection_resolved can become 
m_selection?
+        GtkClipboard *m_clipboard[2];
+
+        auto get_clipboard(vte::platform::ClipboardType type) const noexcept { return 
m_clipboard[vte::to_integral(type)]; }
 
         ClipboardTextRequestGtk<Terminal> m_paste_request;
 
@@ -1118,8 +1114,8 @@ public:
         void set_border_padding(GtkBorder const* padding);
         void set_cursor_aspect(float aspect);
 
-        void widget_paste(GdkAtom board);
-        void widget_copy(VteSelection sel,
+        void widget_paste(vte::platform::ClipboardType selection);
+        void widget_copy(vte::platform::ClipboardType selection,
                          VteFormat format);
         void widget_paste_received(char const* text);
         void widget_clipboard_cleared(GtkClipboard *clipboard);
diff --git a/src/widget.hh b/src/widget.hh
index ed9837a3..c5ccbeaa 100644
--- a/src/widget.hh
+++ b/src/widget.hh
@@ -40,6 +40,11 @@ class Terminal;
 
 namespace platform {
 
+enum class ClipboardType {
+        CLIPBOARD = 0,
+        PRIMARY   = 1
+};
+
 class Widget {
 public:
         friend class vte::terminal::Terminal;
@@ -86,8 +91,8 @@ public:
         void grab_focus() noexcept { gtk_widget_grab_focus(gtk()); }
 
         bool primary_paste_enabled() const noexcept;
-        void paste(GdkAtom board) noexcept { m_terminal->widget_paste(board); }
-        void copy(VteSelection sel,
+        void paste(vte::platform::ClipboardType sel) noexcept { m_terminal->widget_paste(sel); }
+        void copy(vte::platform::ClipboardType sel,
                   VteFormat format) noexcept { m_terminal->widget_copy(sel, format); }
         void paste_received(char const* text) noexcept { m_terminal->widget_paste_received(text); }
         void clipboard_cleared(GtkClipboard *clipboard) noexcept { 
m_terminal->widget_clipboard_cleared(clipboard); }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]