[Libreoffice-commits] core.git: include/vcl vcl/source vcl/unx

Caolán McNamara (via logerrit) logerrit at kemper.freedesktop.org
Mon Jul 6 18:29:45 UTC 2020


 include/vcl/weld.hxx          |    2 +
 vcl/source/app/salvtables.cxx |    1 
 vcl/unx/gtk3/gtk3gtkinst.cxx  |   68 ++++++++++++++++++++++++++++--------------
 3 files changed, 49 insertions(+), 22 deletions(-)

New commits:
commit c02601b467c0878a24ce917853aa9e0781fedf2a
Author:     Caolán McNamara <caolanm at redhat.com>
AuthorDate: Mon Jul 6 12:14:17 2020 +0100
Commit:     Caolán McNamara <caolanm at redhat.com>
CommitDate: Mon Jul 6 20:29:02 2020 +0200

    add set_max_length to TextView
    
    Change-Id: Iade284a1548c7d74fa2391dd0a9b113e654a71d7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/98207
    Tested-by: Caolán McNamara <caolanm at redhat.com>
    Reviewed-by: Caolán McNamara <caolanm at redhat.com>

diff --git a/include/vcl/weld.hxx b/include/vcl/weld.hxx
index 8c9788a2f589..73b1abc48efb 100644
--- a/include/vcl/weld.hxx
+++ b/include/vcl/weld.hxx
@@ -2016,6 +2016,8 @@ public:
     virtual void set_editable(bool bEditable) = 0;
     virtual bool get_editable() const = 0;
     virtual void set_monospace(bool bMonospace) = 0;
+    // The maximum length of the entry. Use 0 for no maximum
+    virtual void set_max_length(int nChars) = 0;
     int get_height_rows(int nRows) const
     {
         //can improve this if needed
diff --git a/vcl/source/app/salvtables.cxx b/vcl/source/app/salvtables.cxx
index dcc4b53b9887..1ba191364a6a 100644
--- a/vcl/source/app/salvtables.cxx
+++ b/vcl/source/app/salvtables.cxx
@@ -5464,6 +5464,7 @@ public:
 
     virtual void set_editable(bool bEditable) override { m_xTextView->SetReadOnly(!bEditable); }
     virtual bool get_editable() const override { return !m_xTextView->IsReadOnly(); }
+    virtual void set_max_length(int nChars) override { m_xTextView->SetMaxTextLen(nChars); }
 
     virtual void set_monospace(bool bMonospace) override
     {
diff --git a/vcl/unx/gtk3/gtk3gtkinst.cxx b/vcl/unx/gtk3/gtk3gtkinst.cxx
index 9fc0809f0a3e..bd7d718c1945 100644
--- a/vcl/unx/gtk3/gtk3gtkinst.cxx
+++ b/vcl/unx/gtk3/gtk3gtkinst.cxx
@@ -12378,7 +12378,9 @@ private:
     GtkTextView* m_pTextView;
     GtkTextBuffer* m_pTextBuffer;
     GtkAdjustment* m_pVAdjustment;
-    gulong m_nChangedSignalId;
+    int m_nMaxTextLength;
+    gulong m_nChangedSignalId; // we don't disable/enable this one, its to implement max-length
+    gulong m_nInsertTextSignalId;
     gulong m_nCursorPosSignalId;
     gulong m_nVAdjustChangedSignalId;
 
@@ -12389,6 +12391,28 @@ private:
         pThis->signal_changed();
     }
 
+    static void signalInserText(GtkTextBuffer *pBuffer, GtkTextIter *pLocation, gchar* /*pText*/, gint /*nLen*/, gpointer widget)
+    {
+        GtkInstanceTextView* pThis = static_cast<GtkInstanceTextView*>(widget);
+        pThis->insert_text(pBuffer, pLocation);
+    }
+
+    void insert_text(GtkTextBuffer *pBuffer, GtkTextIter *pLocation)
+    {
+        if (m_nMaxTextLength)
+        {
+            gint nCount = gtk_text_buffer_get_char_count(pBuffer);
+            if (nCount > m_nMaxTextLength)
+            {
+                GtkTextIter nStart, nEnd;
+                gtk_text_buffer_get_iter_at_offset(m_pTextBuffer, &nStart, m_nMaxTextLength);
+                gtk_text_buffer_get_end_iter(m_pTextBuffer, &nEnd);
+                gtk_text_buffer_delete(m_pTextBuffer, &nStart, &nEnd);
+                gtk_text_iter_assign(pLocation, &nStart);
+            }
+        }
+    }
+
     static void signalCursorPosition(GtkTextView*, GParamSpec*, gpointer widget)
     {
         GtkInstanceTextView* pThis = static_cast<GtkInstanceTextView*>(widget);
@@ -12408,7 +12432,9 @@ public:
         , m_pTextView(pTextView)
         , m_pTextBuffer(gtk_text_view_get_buffer(pTextView))
         , m_pVAdjustment(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(pTextView)))
+        , m_nMaxTextLength(0)
         , m_nChangedSignalId(g_signal_connect(m_pTextBuffer, "changed", G_CALLBACK(signalChanged), this))
+        , m_nInsertTextSignalId(g_signal_connect_after(m_pTextBuffer, "insert-text", G_CALLBACK(signalInserText), this))
         , m_nCursorPosSignalId(g_signal_connect(m_pTextBuffer, "notify::cursor-position", G_CALLBACK(signalCursorPosition), this))
         , m_nVAdjustChangedSignalId(g_signal_connect(m_pVAdjustment, "value-changed", G_CALLBACK(signalVAdjustValueChanged), this))
     {
@@ -12429,18 +12455,16 @@ public:
     virtual void set_text(const OUString& rText) override
     {
         disable_notify_events();
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
         OString sText(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
-        gtk_text_buffer_set_text(pBuffer, sText.getStr(), sText.getLength());
+        gtk_text_buffer_set_text(m_pTextBuffer, sText.getStr(), sText.getLength());
         enable_notify_events();
     }
 
     virtual OUString get_text() const override
     {
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
         GtkTextIter start, end;
-        gtk_text_buffer_get_bounds(pBuffer, &start, &end);
-        char* pStr = gtk_text_buffer_get_text(pBuffer, &start, &end, true);
+        gtk_text_buffer_get_bounds(m_pTextBuffer, &start, &end);
+        char* pStr = gtk_text_buffer_get_text(m_pTextBuffer, &start, &end, true);
         OUString sRet(pStr, pStr ? strlen(pStr) : 0, RTL_TEXTENCODING_UTF8);
         g_free(pStr);
         return sRet;
@@ -12449,18 +12473,16 @@ public:
     virtual void replace_selection(const OUString& rText) override
     {
         disable_notify_events();
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
-        gtk_text_buffer_delete_selection(pBuffer, false, gtk_text_view_get_editable(m_pTextView));
+        gtk_text_buffer_delete_selection(m_pTextBuffer, false, gtk_text_view_get_editable(m_pTextView));
         OString sText(OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
-        gtk_text_buffer_insert_at_cursor(pBuffer, sText.getStr(), sText.getLength());
+        gtk_text_buffer_insert_at_cursor(m_pTextBuffer, sText.getStr(), sText.getLength());
         enable_notify_events();
     }
 
     virtual bool get_selection_bounds(int& rStartPos, int& rEndPos) override
     {
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
         GtkTextIter start, end;
-        gtk_text_buffer_get_selection_bounds(pBuffer, &start, &end);
+        gtk_text_buffer_get_selection_bounds(m_pTextBuffer, &start, &end);
         rStartPos = gtk_text_iter_get_offset(&start);
         rEndPos = gtk_text_iter_get_offset(&end);
         return rStartPos != rEndPos;
@@ -12469,12 +12491,11 @@ public:
     virtual void select_region(int nStartPos, int nEndPos) override
     {
         disable_notify_events();
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
         GtkTextIter start, end;
-        gtk_text_buffer_get_iter_at_offset(pBuffer, &start, nStartPos);
-        gtk_text_buffer_get_iter_at_offset(pBuffer, &end, nEndPos);
-        gtk_text_buffer_select_range(pBuffer, &start, &end);
-        GtkTextMark* mark = gtk_text_buffer_create_mark(pBuffer, "scroll", &end, true);
+        gtk_text_buffer_get_iter_at_offset(m_pTextBuffer, &start, nStartPos);
+        gtk_text_buffer_get_iter_at_offset(m_pTextBuffer, &end, nEndPos);
+        gtk_text_buffer_select_range(m_pTextBuffer, &start, &end);
+        GtkTextMark* mark = gtk_text_buffer_create_mark(m_pTextBuffer, "scroll", &end, true);
         gtk_text_view_scroll_mark_onscreen(m_pTextView, mark);
         enable_notify_events();
     }
@@ -12489,6 +12510,11 @@ public:
         return gtk_text_view_get_editable(m_pTextView);
     }
 
+    virtual void set_max_length(int nChars) override
+    {
+        m_nMaxTextLength = nChars;
+    }
+
     virtual void set_monospace(bool bMonospace) override
     {
         gtk_text_view_set_monospace(m_pTextView, bMonospace);
@@ -12514,24 +12540,21 @@ public:
     {
         GtkClipboard *pClipboard = gtk_widget_get_clipboard(GTK_WIDGET(m_pTextView),
                                                             GDK_SELECTION_CLIPBOARD);
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
-        gtk_text_buffer_cut_clipboard(pBuffer, pClipboard, get_editable());
+        gtk_text_buffer_cut_clipboard(m_pTextBuffer, pClipboard, get_editable());
     }
 
     virtual void copy_clipboard() override
     {
         GtkClipboard *pClipboard = gtk_widget_get_clipboard(GTK_WIDGET(m_pTextView),
                                                             GDK_SELECTION_CLIPBOARD);
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
-        gtk_text_buffer_copy_clipboard(pBuffer, pClipboard);
+        gtk_text_buffer_copy_clipboard(m_pTextBuffer, pClipboard);
     }
 
     virtual void paste_clipboard() override
     {
         GtkClipboard *pClipboard = gtk_widget_get_clipboard(GTK_WIDGET(m_pTextView),
                                                             GDK_SELECTION_CLIPBOARD);
-        GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(m_pTextView);
-        gtk_text_buffer_paste_clipboard(pBuffer, pClipboard, nullptr, get_editable());
+        gtk_text_buffer_paste_clipboard(m_pTextBuffer, pClipboard, nullptr, get_editable());
     }
 
     virtual int vadjustment_get_value() const override
@@ -12580,6 +12603,7 @@ public:
     virtual ~GtkInstanceTextView() override
     {
         g_signal_handler_disconnect(m_pVAdjustment, m_nVAdjustChangedSignalId);
+        g_signal_handler_disconnect(m_pTextBuffer, m_nInsertTextSignalId);
         g_signal_handler_disconnect(m_pTextBuffer, m_nChangedSignalId);
         g_signal_handler_disconnect(m_pTextBuffer, m_nCursorPosSignalId);
     }


More information about the Libreoffice-commits mailing list