Skip to content

Instantly share code, notes, and snippets.

@otsaloma
Created February 26, 2012 01:43
Show Gist options
  • Save otsaloma/1912166 to your computer and use it in GitHub Desktop.
Save otsaloma/1912166 to your computer and use it in GitHub Desktop.
Line numbers in Gtk3 text view margin
#!/usr/bin/env python3
from gi.repository import Gtk
from gi.repository import Pango
def on_text_view_draw(text_view, cairoc):
print("Drawing...")
# XXX: Rest of function not yet ported.
text_buffer = text_view.get_buffer()
bounds = text_buffer.get_bounds()
text = text_buffer.get_text(*bounds)
nlines = text.count("\n") + 1
layout = pango.Layout(text_view.get_pango_context())
layout.set_markup("\n".join([str(x + 1) for x in range(nlines)]))
layout.set_alignment(Pango.Alignment.RIGHT)
width = layout.get_pixel_size()[0]
text_view.set_border_window_size(Gtk.TextWindowType.RIGHT, width + 4)
y = -text_view.window_to_buffer_coords(Gtk.TextWindowType.RIGHT, 2, 0)[1]
window = text_view.get_window(Gtk.TextWindowType.RIGHT)
window.clear()
text_view.style.paint_layout(window=window,
state_type=Gtk.STATE_NORMAL,
use_text=True,
area=None,
widget=text_view,
detail=None,
x=2,
y=y,
layout=layout)
text_view = Gtk.TextView()
text_buffer = text_view.get_buffer()
text_buffer.insert_at_cursor("ABC\nabc")
text_view.set_border_window_size(Gtk.TextWindowType.RIGHT, 24)
text_view.connect("draw", on_text_view_draw)
scroller = Gtk.ScrolledWindow()
scroller.set_shadow_type(Gtk.ShadowType.IN)
scroller.add(text_view)
window = Gtk.Window()
window.connect("delete-event", Gtk.main_quit)
window.set_position(Gtk.WindowPosition.CENTER)
window.set_default_size(300, 100)
window.set_border_width(12)
window.add(scroller)
window.show_all()
Gtk.main()
@tclsteixeira
Copy link

Gtk 3 - Doesn't work. No numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment