pythongtkgtk3pygobjectpango

Don't insert hyphens when using character wrap with GtkSourceView


I have a GtkSource.View with the wrap mode set to Gtk.WrapMode.CHAR. When it wraps, it inserts - at the end of the line. How can I prevent that?

In my research I found that the Pango library has a attr_insert_hyphens_new(False) function, which returns a Pango.Attribute, but I don't understand how you're meant to use these Pango attributes.


Current code:

view = GtkSource.View()
view.set_wrap_mode(Gtk.WrapMode.CHAR)

context = view.get_pango_context()
attrList = Pango.AttrList()
layout = Pango.Layout(context)

attr = Pango.attr_insert_hyphens_new(False)
attr.start_index = Pango.ATTR_INDEX_FROM_TEXT_BEGINNING
attr.end_index = Pango.ATTR_INDEX_TO_TEXT_END
attrList.insert(attr)
layout.set_attributes(attrList)

Solution

  • encountered the same problem,
    I found 2 way to avoid hyphens.
    //seems both need gtk4

    A. pango markup insert_hyphens Attributes

    need Pango >= 1.44

    https://docs.gtk.org/Pango/pango_markup.html#:~:text=insert_hyphens

    NOTE: chrome browser can anchor to keyword.

    even pygtk with Pango 1.50
    no runtime error, but seems no effect with gtk3.
    (maybe gtk3 not handle it right)

    B. Gtk.TextTag:insert-hyphens

    need gtk4

    https://docs.gtk.org/gtk4/class.TextTag.html#properties:~:text=TextTag%3Ainsert%2Dhyphens

    gtk3 will got error:

    TypeError: gobject GtkTextTag' doesn't support property insert_hyphens'


    demo code

    import gi
    gi.require_version('Gtk', '4.0')
    from gi.repository import Gtk
    
    
    # set text without hyphens by markup
    def test_markup(tb: Gtk.TextBuffer):
        # insert-hyphens='no' or insert_hyphens='false' both work
        s = '123 aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbb ccccccccccccccc ddddddddd'
        s = '<span insert-hyphens="no" color="green">' + s + '</span>'
        # tb.set_text(s)
        tb.insert_markup(tb.get_end_iter(), s, -1)
    
    
    # set text without hyphens by textTag
    def test_tag(tb: Gtk.TextBuffer):
        s = '456 aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbb ccccccccccccccc ddddddddd'
        tb.set_text(s)
        tag1 = tb.create_tag("test1", foreground="red", insert_hyphens=False)
        tb.apply_tag(tag1, tb.get_start_iter(), tb.get_end_iter())
        # tb.apply_tag(tag1, tb.get_start_iter(), tb.get_iter_at_offset(3))
    
    
    def on_activate(app):
        win = Gtk.ApplicationWindow(application=app)
    
        tv: Gtk.TextView = Gtk.TextView.new()
        tv.set_size_request(150, 50)
        tv.set_wrap_mode(Gtk.WrapMode.CHAR)
    
        tb: Gtk.TextBuffer = tv.get_buffer()
    
        test_markup(tb)
        # test_tag(tb)
    
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.append(tv)
        box.set_size_request(400, 300)
        box.append(Gtk.Button(label="hello"))
    
        win.present()
        win.set_child(box)
    
    
    app = Gtk.Application()
    app.connect('activate', on_activate)
    app.run(None)