blob: 42ba84c8cafdc603032d6d773d400d589efd4973 [file] [log] [blame]
John Park91e69732019-03-03 13:12:43 -08001#!/usr/bin/python3
Parker Schuh19b93b12018-03-02 23:26:58 -08002import gi
3gi.require_version('Gtk', '3.0')
4from gi.repository import Gtk
5from gi.repository import GLib
6from gi.repository import Gdk
7from gi.repository import GdkX11
8import cairo
John Park91e69732019-03-03 13:12:43 -08009from constants import *
Parker Schuh19b93b12018-03-02 23:26:58 -080010
11identity = cairo.Matrix()
12
13# Override the matrix of a cairo context.
14class OverrideMatrix(object):
Parker Schuhdc682952018-03-03 18:24:01 -080015 def __init__(self, cr, matrix):
16 self.cr = cr
17 self.matrix = matrix
Parker Schuh19b93b12018-03-02 23:26:58 -080018
Parker Schuhdc682952018-03-03 18:24:01 -080019 def __enter__(self):
20 self.cr.save()
21 self.cr.set_matrix(self.matrix)
22
23 def __exit__(self, type, value, traceback):
24 self.cr.restore()
25
Parker Schuh19b93b12018-03-02 23:26:58 -080026
27mainloop = GLib.MainLoop()
28
Tabitha Jarvis1007a132018-12-12 21:47:54 -080029def set_color(cr, color):
30 if color.a == 1.0:
31 cr.set_source_rgb(color.r, color.g, color.b)
32 else:
33 cr.set_source_rgba(color.r, color.g, color.b, color.a)
Parker Schuhdc682952018-03-03 18:24:01 -080034
Parker Schuh19b93b12018-03-02 23:26:58 -080035def quit_main_loop(*args):
Parker Schuhdc682952018-03-03 18:24:01 -080036 mainloop.quit()
37
Parker Schuh19b93b12018-03-02 23:26:58 -080038def 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__()
Parker Schuh19b93b12018-03-02 23:26:58 -080052
John Park91e69732019-03-03 13:12:43 -080053 self.set_size_request(2*SCREEN_SIZE, SCREEN_SIZE)
Parker Schuhdc682952018-03-03 18:24:01 -080054 self.center = (0, 0)
John Park91e69732019-03-03 13:12:43 -080055 self.shape = (2*SCREEN_SIZE, SCREEN_SIZE)
Parker Schuhdc682952018-03-03 18:24:01 -080056 self.needs_redraw = False
Parker Schuh19b93b12018-03-02 23:26:58 -080057
Parker Schuhdc682952018-03-03 18:24:01 -080058 def get_current_scale(self):
59 w_w, w_h = self.window_shape
60 w, h = self.shape
61 return min((w_w / w), (w_h / h))
Parker Schuh19b93b12018-03-02 23:26:58 -080062
Parker Schuhdc682952018-03-03 18:24:01 -080063 def init_extents(self, center, shape):
64 self.center = center
65 self.shape = shape
Parker Schuh19b93b12018-03-02 23:26:58 -080066
Parker Schuhdc682952018-03-03 18:24:01 -080067 # The gtk system creates cr which is a cairo_context_t (in the c docs), and then it
68 # passes it as a function argument to the "draw" event. do_draw is the default name.
69 def do_draw(self, cr):
70 cr.save()
71 cr.set_font_size(20)
72 cr.translate(self.window_shape[0] / 2, self.window_shape[1] / 2)
73 scale = self.get_current_scale()
74 cr.scale(scale, -scale)
75 cr.translate(-self.center[0], -self.center[1])
Ravago Jonesb7e46f72021-01-16 15:43:29 -080076 cr.reset_clip()
Parker Schuhdc682952018-03-03 18:24:01 -080077 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
John Park91e69732019-03-03 13:12:43 -080084