[vte] widget: Move scrollable adjustments up to Widget



commit 1bdfc0fa1adf1225152068040c8bde0804297805
Author: Christian Persch <chpe src gnome org>
Date:   Mon Nov 18 22:42:22 2019 +0100

    widget: Move scrollable adjustments up to Widget
    
    Move the unused bits of the GtkScrollable implementation up to Widget,
    since they are not used in Terminal, and use smart pointers.

 src/refptr.hh      |   9 +++++
 src/vte.cc         | 104 +++++++++++++++++++++--------------------------------
 src/vtegtk.cc      |  13 ++++---
 src/vteinternal.hh |  10 ++----
 src/vteseq.cc      |   2 +-
 src/widget.cc      |   5 ++-
 src/widget.hh      |  20 +++++++----
 7 files changed, 77 insertions(+), 86 deletions(-)
---
diff --git a/src/refptr.hh b/src/refptr.hh
index 666d061c..5d8e126a 100644
--- a/src/refptr.hh
+++ b/src/refptr.hh
@@ -43,6 +43,15 @@ make_ref(T* obj)
         return {obj};
 }
 
+template<typename T>
+RefPtr<T>
+make_ref_sink(T* obj)
+{
+        if (obj)
+                g_object_ref_sink(obj);
+        return {obj};
+}
+
 template<typename T>
 RefPtr<T>
 take_ref(T* obj)
diff --git a/src/vte.cc b/src/vte.cc
index 5b2bbe69..d3815076 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -1798,63 +1798,65 @@ Terminal::emit_adjustment_changed()
                bool changed = false;
                gdouble current, v;
 
-               g_object_freeze_notify (G_OBJECT(m_vadjustment));
+                auto vadjustment = m_vadjustment.get();
+
+                g_object_freeze_notify(G_OBJECT(vadjustment));
 
                v = _vte_ring_delta (m_screen->row_data);
-               current = gtk_adjustment_get_lower(m_vadjustment);
+                current = gtk_adjustment_get_lower(vadjustment);
                if (!_vte_double_equal(current, v)) {
                        _vte_debug_print(VTE_DEBUG_ADJ,
                                        "Changing lower bound from %.0f to %f\n",
                                         current, v);
-                       gtk_adjustment_set_lower(m_vadjustment, v);
+                        gtk_adjustment_set_lower(vadjustment, v);
                        changed = true;
                }
 
                v = m_screen->insert_delta + m_row_count;
-               current = gtk_adjustment_get_upper(m_vadjustment);
+                current = gtk_adjustment_get_upper(vadjustment);
                if (!_vte_double_equal(current, v)) {
                        _vte_debug_print(VTE_DEBUG_ADJ,
                                        "Changing upper bound from %.0f to %f\n",
                                         current, v);
-                       gtk_adjustment_set_upper(m_vadjustment, v);
+                        gtk_adjustment_set_upper(vadjustment, v);
                        changed = true;
                }
 
                /* The step increment should always be one. */
-               v = gtk_adjustment_get_step_increment(m_vadjustment);
+                v = gtk_adjustment_get_step_increment(vadjustment);
                if (!_vte_double_equal(v, 1)) {
                        _vte_debug_print(VTE_DEBUG_ADJ,
                                        "Changing step increment from %.0lf to 1\n", v);
-                       gtk_adjustment_set_step_increment(m_vadjustment, 1);
+                        gtk_adjustment_set_step_increment(vadjustment, 1);
                        changed = true;
                }
 
                /* Set the number of rows the user sees to the number of rows the
                 * user sees. */
-               v = gtk_adjustment_get_page_size(m_vadjustment);
+                v = gtk_adjustment_get_page_size(vadjustment);
                if (!_vte_double_equal(v, m_row_count)) {
                        _vte_debug_print(VTE_DEBUG_ADJ,
                                        "Changing page size from %.0f to %ld\n",
                                         v, m_row_count);
-                       gtk_adjustment_set_page_size(m_vadjustment,
+                        gtk_adjustment_set_page_size(vadjustment,
                                                     m_row_count);
                        changed = true;
                }
 
                /* Clicking in the empty area should scroll one screen, so set the
                 * page size to the number of visible rows. */
-               v = gtk_adjustment_get_page_increment(m_vadjustment);
+                v = gtk_adjustment_get_page_increment(vadjustment);
                if (!_vte_double_equal(v, m_row_count)) {
                        _vte_debug_print(VTE_DEBUG_ADJ,
                                        "Changing page increment from "
                                        "%.0f to %ld\n",
                                        v, m_row_count);
-                       gtk_adjustment_set_page_increment(m_vadjustment,
+                        gtk_adjustment_set_page_increment(vadjustment,
                                                          m_row_count);
                        changed = true;
                }
 
-               g_object_thaw_notify (G_OBJECT (m_vadjustment));
+                g_object_thaw_notify(G_OBJECT(vadjustment));
 
                if (changed)
                        _vte_debug_print(VTE_DEBUG_SIGNALS,
@@ -1866,7 +1868,9 @@ Terminal::emit_adjustment_changed()
                _vte_debug_print(VTE_DEBUG_SIGNALS,
                                "Emitting adjustment_value_changed.\n");
                m_adjustment_value_changed_pending = FALSE;
-               v = gtk_adjustment_get_value(m_vadjustment);
+
+                auto vadjustment = m_vadjustment.get();
+                v = gtk_adjustment_get_value(vadjustment);
                if (!_vte_double_equal(v, m_screen->scroll_delta)) {
                        /* this little dance is so that the scroll_delta is
                         * updated immediately, but we still handled scrolling
@@ -1875,7 +1879,7 @@ Terminal::emit_adjustment_changed()
                         */
                        delta = m_screen->scroll_delta;
                        m_screen->scroll_delta = v;
-                       gtk_adjustment_set_value(m_vadjustment, delta);
+                        gtk_adjustment_set_value(vadjustment, delta);
                }
        }
 }
@@ -1905,8 +1909,9 @@ Terminal::queue_adjustment_value_changed(double v)
 void
 Terminal::queue_adjustment_value_changed_clamped(double v)
 {
-       double lower = gtk_adjustment_get_lower(m_vadjustment);
-       double upper = gtk_adjustment_get_upper(m_vadjustment);
+        auto vadjustment = m_vadjustment.get();
+        auto const lower = gtk_adjustment_get_lower(vadjustment);
+        auto const upper = gtk_adjustment_get_upper(vadjustment);
 
        v = CLAMP(v, lower, MAX (lower, upper - m_row_count));
 
@@ -7772,7 +7777,7 @@ void
 Terminal::vadjustment_value_changed()
 {
        /* Read the new adjustment value and save the difference. */
-       double adj = gtk_adjustment_get_value(m_vadjustment);
+        auto const adj = gtk_adjustment_get_value(m_vadjustment.get());
        double dy = adj - m_screen->scroll_delta;
        m_screen->scroll_delta = adj;
 
@@ -7794,44 +7799,27 @@ Terminal::vadjustment_value_changed()
 }
 
 void
-Terminal::widget_set_hadjustment(GtkAdjustment *adjustment)
+Terminal::widget_set_vadjustment(vte::glib::RefPtr<GtkAdjustment>&& adjustment)
 {
-  if (adjustment == m_hadjustment)
-    return;
-
-  if (m_hadjustment)
-    g_object_unref (m_hadjustment);
-
-  m_hadjustment = adjustment ? (GtkAdjustment *)g_object_ref_sink(adjustment) : nullptr;
-}
-
-void
-Terminal::widget_set_vadjustment(GtkAdjustment *adjustment)
-{
-       if (adjustment != nullptr && adjustment == m_vadjustment)
-               return;
-       if (adjustment == nullptr && m_vadjustment != nullptr)
-               return;
-
-       if (adjustment == nullptr)
-               adjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 0, 0, 0));
+        if (adjustment && adjustment == m_vadjustment)
+                return;
+        if (!adjustment && m_vadjustment)
+                return;
 
-       /* Add a reference to the new adjustment object. */
-       g_object_ref_sink(adjustment);
-       /* Get rid of the old adjustment object. */
-       if (m_vadjustment != nullptr) {
+        if (m_vadjustment) {
                /* Disconnect our signal handlers from this object. */
-               g_signal_handlers_disconnect_by_func(m_vadjustment,
+                g_signal_handlers_disconnect_by_func(m_vadjustment.get(),
                                                     (void*)vte_terminal_vadjustment_value_changed_cb,
                                                     this);
-               g_object_unref(m_vadjustment);
        }
 
-       /* Set the new adjustment object. */
-       m_vadjustment = adjustment;
+        if (adjustment)
+                m_vadjustment = std::move(adjustment);
+        else
+                m_vadjustment = vte::glib::make_ref_sink(GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 0, 0, 
0)));
 
        /* We care about the offset, not the top or bottom. */
-       g_signal_connect_swapped(m_vadjustment,
+        g_signal_connect_swapped(m_vadjustment.get(),
                                 "value-changed",
                                 G_CALLBACK(vte_terminal_vadjustment_value_changed_cb),
                                 this);
@@ -7848,6 +7836,8 @@ Terminal::Terminal(vte::platform::Widget* w,
         m_alternate_screen(VTE_ROWS, false),
         m_screen(&m_normal_screen)
 {
+       widget_set_vadjustment({});
+
         /* Inits allocation to 1x1 @ -1,-1 */
         cairo_rectangle_int_t allocation;
         gtk_widget_get_allocation(m_widget, &allocation);
@@ -7871,17 +7861,6 @@ Terminal::Terminal(vte::platform::Widget* w,
                                            sizeof(cairo_rectangle_int_t),
                                            32 /* preallocated size */);
 
-       /* Set an adjustment for the application to use to control scrolling. */
-        m_vadjustment = nullptr;
-        m_hadjustment = nullptr;
-
-        /* GtkScrollable */
-        m_hscroll_policy = GTK_SCROLL_NATURAL;
-        m_vscroll_policy = GTK_SCROLL_NATURAL;
-
-        widget_set_hadjustment(nullptr);
-       widget_set_vadjustment(nullptr);
-
        /* Set up dummy metrics, value != 0 to avoid division by 0 */
        m_cell_width = 1;
        m_cell_height = 1;
@@ -8275,12 +8254,11 @@ Terminal::~Terminal()
         m_outgoing = nullptr;
 
        /* Free public-facing data. */
-       if (m_vadjustment != NULL) {
+        if (m_vadjustment) {
                /* Disconnect our signal handlers from this object. */
-               g_signal_handlers_disconnect_by_func(m_vadjustment,
+                g_signal_handlers_disconnect_by_func(m_vadjustment.get(),
                                                     (void*)vte_terminal_vadjustment_value_changed_cb,
                                                     this);
-               g_object_unref(m_vadjustment);
        }
 
         /* Update rects */
@@ -9682,7 +9660,7 @@ Terminal::widget_scroll(GdkEventScroll *event)
                return;
        }
 
-       v = MAX (1., ceil (gtk_adjustment_get_page_increment (m_vadjustment) / 10.));
+        v = MAX (1., ceil (gtk_adjustment_get_page_increment (m_vadjustment.get()) / 10.));
        _vte_debug_print(VTE_DEBUG_EVENTS,
                        "Scroll speed is %d lines per non-smooth scroll unit\n",
                        (int) v);
@@ -10895,8 +10873,8 @@ Terminal::search_rows(pcre2_match_context_8 *match_context,
 
        select_text(start_col, start_row, end_col, end_row);
        /* Quite possibly the math here should not access adjustment directly... */
-       value = gtk_adjustment_get_value(m_vadjustment);
-       page_size = gtk_adjustment_get_page_size(m_vadjustment);
+        value = gtk_adjustment_get_value(m_vadjustment.get());
+        page_size = gtk_adjustment_get_page_size(m_vadjustment.get());
        if (backward) {
                if (end_row < value || end_row > value + page_size - 1)
                        queue_adjustment_value_changed_clamped(end_row - page_size + 1);
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
index b51de4d3..0760fce6 100644
--- a/src/vtegtk.cc
+++ b/src/vtegtk.cc
@@ -131,7 +131,7 @@ vte_terminal_set_hadjustment(VteTerminal *terminal,
                              GtkAdjustment *adjustment)
 {
         g_return_if_fail(adjustment == nullptr || GTK_IS_ADJUSTMENT(adjustment));
-        WIDGET(terminal)->set_hadjustment(adjustment);
+        WIDGET(terminal)->set_hadjustment(vte::glib::make_ref_sink(adjustment));
 }
 
 static void
@@ -139,23 +139,22 @@ vte_terminal_set_vadjustment(VteTerminal *terminal,
                              GtkAdjustment *adjustment)
 {
         g_return_if_fail(adjustment == nullptr || GTK_IS_ADJUSTMENT(adjustment));
-        WIDGET(terminal)->set_vadjustment(adjustment);
+        WIDGET(terminal)->set_vadjustment(vte::glib::make_ref_sink(adjustment));
 }
 
 static void
 vte_terminal_set_hscroll_policy(VteTerminal *terminal,
                                 GtkScrollablePolicy policy)
 {
-        IMPL(terminal)->m_hscroll_policy = policy;
+        WIDGET(terminal)->set_hscroll_policy(policy);
         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (terminal));
 }
 
-
 static void
 vte_terminal_set_vscroll_policy(VteTerminal *terminal,
                                 GtkScrollablePolicy policy)
 {
-        IMPL(terminal)->m_vscroll_policy = policy;
+        WIDGET(terminal)->set_vscroll_policy(policy);
         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (terminal));
 }
 
@@ -445,10 +444,10 @@ vte_terminal_get_property (GObject *object,
        switch (prop_id)
                 {
                 case PROP_HADJUSTMENT:
-                        g_value_set_object (value, widget->get_hadjustment());
+                        g_value_set_object (value, widget->hadjustment());
                         break;
                 case PROP_VADJUSTMENT:
-                        g_value_set_object (value, widget->get_vadjustment());
+                        g_value_set_object (value, widget->vadjustment());
                         break;
                 case PROP_HSCROLL_POLICY:
                         g_value_set_enum (value, widget->hscroll_policy());
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index ba40d2bc..ad15d055 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -727,11 +727,8 @@ public:
         /* Style stuff */
         GtkBorder m_padding;
 
-        /* GtkScrollable impl */
-        GtkAdjustment* m_hadjustment; /* unused */
-        GtkAdjustment* m_vadjustment;
-        guint m_hscroll_policy : 1; /* unused */
-        guint m_vscroll_policy : 1;
+        vte::glib::RefPtr<GtkAdjustment> m_vadjustment{};
+        auto vadjustment() noexcept { return m_vadjustment.get(); }
 
         /* Hyperlinks */
         gboolean m_allow_hyperlink;
@@ -880,8 +877,7 @@ public:
                                         GtkSelectionData *data,
                                         guint info);
 
-        void widget_set_hadjustment(GtkAdjustment *adjustment);
-        void widget_set_vadjustment(GtkAdjustment *adjustment);
+        void widget_set_vadjustment(vte::glib::RefPtr<GtkAdjustment>&& adjustment);
 
         void widget_constructed();
         void widget_realize();
diff --git a/src/vteseq.cc b/src/vteseq.cc
index f07b4de7..86fe2c7b 100644
--- a/src/vteseq.cc
+++ b/src/vteseq.cc
@@ -584,7 +584,7 @@ Terminal::set_mode_private(int mode,
                 }
 
                 /* Reset scrollbars and repaint everything. */
-                gtk_adjustment_set_value(m_vadjustment,
+                gtk_adjustment_set_value(m_vadjustment.get(),
                                          m_screen->scroll_delta);
                 set_scrollback_lines(m_scrollback_lines);
                 queue_contents_changed();
diff --git a/src/widget.cc b/src/widget.cc
index aee1c56c..a3a796a0 100644
--- a/src/widget.cc
+++ b/src/widget.cc
@@ -95,7 +95,10 @@ settings_notify_cb(GtkSettings* settings,
 }
 
 Widget::Widget(VteTerminal* t) noexcept :
-        m_widget{&t->widget}
+        m_widget{&t->widget},
+        m_hscroll_policy{GTK_SCROLL_NATURAL},
+        m_vscroll_policy{GTK_SCROLL_NATURAL}
+
 {
         /* Until Terminal init is completely fixed, use zero'd memory */
         auto place = g_malloc0(sizeof(vte::terminal::Terminal));
diff --git a/src/widget.hh b/src/widget.hh
index 251a1fbe..b8c60cd8 100644
--- a/src/widget.hh
+++ b/src/widget.hh
@@ -96,13 +96,14 @@ public:
 
         void beep() noexcept;
 
-        void set_hadjustment(GtkAdjustment *adjustment) noexcept { 
m_terminal->widget_set_hadjustment(adjustment); }
-        GtkAdjustment* get_hadjustment() const noexcept { return m_terminal->m_hadjustment; }
-        void set_vadjustment(GtkAdjustment *adjustment) noexcept { 
m_terminal->widget_set_vadjustment(adjustment); }
-        GtkAdjustment* get_vadjustment() const noexcept { return m_terminal->m_vadjustment; }
-
-        int hscroll_policy() const noexcept { return m_terminal->m_hscroll_policy; }
-        int vscroll_policy() const noexcept { return m_terminal->m_vscroll_policy; }
+        void set_hadjustment(vte::glib::RefPtr<GtkAdjustment>&& adjustment) noexcept { m_hadjustment = 
std::move(adjustment); }
+        void set_vadjustment(vte::glib::RefPtr<GtkAdjustment>&& adjustment) { 
terminal()->widget_set_vadjustment(std::move(adjustment)); }
+        auto hadjustment() noexcept { return m_hadjustment.get(); }
+        auto vadjustment() noexcept { return terminal()->vadjustment(); }
+        void set_hscroll_policy(GtkScrollablePolicy policy) noexcept { m_hscroll_policy = policy; }
+        void set_vscroll_policy(GtkScrollablePolicy policy) noexcept { m_vscroll_policy = policy; }
+        auto hscroll_policy() const noexcept { return m_hscroll_policy; }
+        auto vscroll_policy() const noexcept { return m_vscroll_policy; }
 
         char const* encoding() const noexcept { return m_terminal->encoding(); }
 
@@ -171,6 +172,11 @@ private:
 
         /* PTY */
         vte::glib::RefPtr<VtePty> m_pty;
+
+        /* Misc */
+        vte::glib::RefPtr<GtkAdjustment> m_hadjustment{};
+        uint32_t m_hscroll_policy : 1;
+        uint32_t m_vscroll_policy : 1;
 };
 
 } // namespace platform


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