blob: 78324a3369df6090949c7cf70a697c19ee750e14 [file] [log] [blame]
Parker Schuh19b93b12018-03-02 23:26:58 -08001import gi
2gi.require_version('Gtk', '3.0')
3from gi.repository import Gtk
4from gi.repository import GLib
5from gi.repository import Gdk
6from gi.repository import GdkX11
7import cairo
8
9identity = cairo.Matrix()
10
Parker Schuhdc682952018-03-03 18:24:01 -080011
Parker Schuh19b93b12018-03-02 23:26:58 -080012# Override the matrix of a cairo context.
13class OverrideMatrix(object):
Parker Schuhdc682952018-03-03 18:24:01 -080014 def __init__(self, cr, matrix):
15 self.cr = cr
16 self.matrix = matrix
Parker Schuh19b93b12018-03-02 23:26:58 -080017
Parker Schuhdc682952018-03-03 18:24:01 -080018 def __enter__(self):
19 self.cr.save()
20 self.cr.set_matrix(self.matrix)
21
22 def __exit__(self, type, value, traceback):
23 self.cr.restore()
24
Parker Schuh19b93b12018-03-02 23:26:58 -080025
26mainloop = GLib.MainLoop()
27
Tabitha Jarvis1007a132018-12-12 21:47:54 -080028def set_color(cr, color):
29 if color.a == 1.0:
30 cr.set_source_rgb(color.r, color.g, color.b)
31 else:
32 cr.set_source_rgba(color.r, color.g, color.b, color.a)
Parker Schuhdc682952018-03-03 18:24:01 -080033
Parker Schuh19b93b12018-03-02 23:26:58 -080034def quit_main_loop(*args):
Parker Schuhdc682952018-03-03 18:24:01 -080035 mainloop.quit()
36
Parker Schuh19b93b12018-03-02 23:26:58 -080037
38def RunApp():
Parker Schuhdc682952018-03-03 18:24:01 -080039 try:
40 mainloop.run()
41 except KeyboardInterrupt:
42 print('\nCtrl+C hit, quitting')
43 mainloop.quit()
44
Parker Schuh19b93b12018-03-02 23:26:58 -080045
46# Create a GTK+ widget on which we will draw using Cairo
47class BaseWindow(Gtk.DrawingArea):
Parker Schuh19b93b12018-03-02 23:26:58 -080048
Parker Schuhdc682952018-03-03 18:24:01 -080049 # Draw in response to an expose-event
50 def __init__(self):
51 super(BaseWindow, self).__init__()
Andrew Runke6842bf92019-01-26 15:38:25 -080052 #self.window.connect("destroy", quit_main_loop)
Parker Schuh19b93b12018-03-02 23:26:58 -080053
Andrew Runke6842bf92019-01-26 15:38:25 -080054 self.set_size_request(640, 600)
Parker Schuhdc682952018-03-03 18:24:01 -080055 self.center = (0, 0)
56 self.shape = (640, 400)
57 self.needs_redraw = False
Parker Schuh19b93b12018-03-02 23:26:58 -080058
Parker Schuhdc682952018-03-03 18:24:01 -080059 def get_current_scale(self):
60 w_w, w_h = self.window_shape
61 w, h = self.shape
62 return min((w_w / w), (w_h / h))
Parker Schuh19b93b12018-03-02 23:26:58 -080063
Parker Schuhdc682952018-03-03 18:24:01 -080064 def init_extents(self, center, shape):
65 self.center = center
66 self.shape = shape
Parker Schuh19b93b12018-03-02 23:26:58 -080067
Parker Schuhdc682952018-03-03 18:24:01 -080068 # The gtk system creates cr which is a cairo_context_t (in the c docs), and then it
69 # passes it as a function argument to the "draw" event. do_draw is the default name.
70 def do_draw(self, cr):
71 cr.save()
72 cr.set_font_size(20)
73 cr.translate(self.window_shape[0] / 2, self.window_shape[1] / 2)
74 scale = self.get_current_scale()
75 cr.scale(scale, -scale)
76 cr.translate(-self.center[0], -self.center[1])
77 self.needs_redraw = False
78 self.handle_draw(cr)
79 cr.restore()
Parker Schuh19b93b12018-03-02 23:26:58 -080080
Parker Schuhdc682952018-03-03 18:24:01 -080081 # Handle the expose-event by drawing
82 def handle_draw(self, cr):
83 pass