| 1 |
|
| 2 |
|
| 3 |
import gi |
| 4 |
gi.require_version("Gtk", "3.0") |
| 5 |
gi.require_foreign("cairo") |
| 6 |
from gi.repository import Gtk, Pango |
| 7 |
|
| 8 |
|
| 9 |
from snippets import get_snippets |
| 10 |
|
| 11 |
|
| 12 |
class Window(Gtk.Window): |
| 13 |
"""Composite widget""" |
| 14 |
|
| 15 |
WIDTH, HEIGHT = 400, 400 |
| 16 |
|
| 17 |
def __init__(self, title=None): |
| 18 |
super().__init__() |
| 19 |
self.set_default_size(self.WIDTH, self.HEIGHT) |
| 20 |
|
| 21 |
self.da = Gtk.DrawingArea() |
| 22 |
self.da.connect('draw', self.da_draw_event) |
| 23 |
|
| 24 |
def put_in_frame(widget): |
| 25 |
frame = Gtk.Frame(label=None) |
| 26 |
frame.set_property('shadow_type', Gtk.ShadowType.IN) |
| 27 |
frame.add(widget) |
| 28 |
return frame |
| 29 |
|
| 30 |
self.current_snippet = None |
| 31 |
|
| 32 |
vpaned = Gtk.VPaned() |
| 33 |
self.add(vpaned) |
| 34 |
|
| 35 |
sv = self.create_text_view() |
| 36 |
vpaned.pack1(put_in_frame(sv), True, True) |
| 37 |
sv.set_size_request(self.WIDTH, int(self.HEIGHT / 2)) |
| 38 |
|
| 39 |
hpaned = Gtk.HPaned() |
| 40 |
vpaned.pack2(hpaned, True, False) |
| 41 |
|
| 42 |
sl = self.create_snippet_list() |
| 43 |
hpaned.pack1(put_in_frame(sl), True, True) |
| 44 |
|
| 45 |
hpaned.pack2(put_in_frame(self.da), True, True) |
| 46 |
self.da.set_size_request(int(self.WIDTH / 2), int(self.HEIGHT / 2)) |
| 47 |
|
| 48 |
|
| 49 |
sl.get_child().grab_focus() |
| 50 |
|
| 51 |
def da_draw_event(self, da, cr): |
| 52 |
if self.current_snippet is None: |
| 53 |
return False |
| 54 |
|
| 55 |
alloc = da.get_allocation() |
| 56 |
self.current_snippet.draw_func(cr, alloc.width, alloc.height) |
| 57 |
|
| 58 |
return True |
| 59 |
|
| 60 |
def create_text_view(self): |
| 61 |
sw = Gtk.ScrolledWindow() |
| 62 |
sw.set_property('shadow-type', Gtk.ShadowType.IN) |
| 63 |
sw.set_policy(hscrollbar_policy=Gtk.PolicyType.AUTOMATIC, |
| 64 |
vscrollbar_policy=Gtk.PolicyType.AUTOMATIC) |
| 65 |
|
| 66 |
text_view = Gtk.TextView() |
| 67 |
sw.add(text_view) |
| 68 |
|
| 69 |
text_view.override_font(Pango.FontDescription.from_string("Fixed")) |
| 70 |
|
| 71 |
self.text_buffer = text_view.get_buffer() |
| 72 |
|
| 73 |
return sw |
| 74 |
|
| 75 |
def cb_selection_changed(self, tselection, data=None): |
| 76 |
model, iter = tselection.get_selected() |
| 77 |
if iter: |
| 78 |
self.current_snippet = model[iter][1] |
| 79 |
self.text_buffer.set_text(self.current_snippet.code) |
| 80 |
self.da.queue_draw() |
| 81 |
|
| 82 |
def create_snippet_list(self): |
| 83 |
sw = Gtk.ScrolledWindow() |
| 84 |
sw.set_property('shadow-type', Gtk.ShadowType.IN) |
| 85 |
sw.set_policy(hscrollbar_policy=Gtk.PolicyType.NEVER, |
| 86 |
vscrollbar_policy=Gtk.PolicyType.AUTOMATIC) |
| 87 |
|
| 88 |
snippets = get_snippets() |
| 89 |
model = Gtk.ListStore(str, object) |
| 90 |
for name, s in snippets.items(): |
| 91 |
model.append(row=(name, s)) |
| 92 |
|
| 93 |
tree_view = Gtk.TreeView(model) |
| 94 |
sw.add(tree_view) |
| 95 |
tree_view.set_property('headers-visible', False) |
| 96 |
tree_view.set_property('search-column', 0) |
| 97 |
tree_view.set_property('rules-hint', False) |
| 98 |
|
| 99 |
tselection = tree_view.get_selection() |
| 100 |
tselection.connect("changed", self.cb_selection_changed) |
| 101 |
tselection.set_mode(Gtk.SelectionMode.BROWSE) |
| 102 |
|
| 103 |
cr = Gtk.CellRendererText() |
| 104 |
tvc = Gtk.TreeViewColumn(None, cr, text=0) |
| 105 |
tree_view.append_column(tvc) |
| 106 |
|
| 107 |
tselection.select_path(0,) |
| 108 |
|
| 109 |
return sw |
| 110 |
|
| 111 |
|
| 112 |
if __name__ == '__main__': |
| 113 |
app = Window() |
| 114 |
app.connect('destroy', Gtk.main_quit) |
| 115 |
app.show_all() |
| 116 |
Gtk.main() |