-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchar_widget.py
More file actions
130 lines (106 loc) · 4.42 KB
/
Copy pathchar_widget.py
File metadata and controls
130 lines (106 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
##
## This file is part of Fontedit.
## Copyright 2010-2020 Nathan Dumont
##
## Fontedit is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Fontedit is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Fontedit. If not, see <http://www.gnu.org/licenses/>.
##
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import Gdk
class CharacterWidget(Gtk.EventBox):
def __init__(self, font):
GObject.GObject.__init__(self)
self.set_size_request(100, 100)
self.drawing = Gtk.DrawingArea()
self.drawing.connect("draw", self.expose)
# event signals
self.drawing.connect('motion-notify-event', self.draw_motion_notify_event)
self.drawing.connect('button-press-event', self.draw_button_press_event)
# Ask to receive events the drawing area doesn't normally
# subscribe to
self.drawing.set_events(self.drawing.get_events()
| Gdk.EventMask.LEAVE_NOTIFY_MASK
| Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.POINTER_MOTION_MASK
| Gdk.EventMask.POINTER_MOTION_HINT_MASK)
self.add(self.drawing)
self.fg = (font.fg['r'] / 65535, font.fg['g'] / 65535, font.fg['b'] / 65535)
self.bg = (font.bg['r'] / 65535, font.bg['g'] / 65535, font.bg['b'] / 65535)
self.rows = font.rows
self.cols = font.cols
self.pixels = []
for y in range(font.rows):
self.pixels.append([])
for x in range(font.cols):
self.pixels[-1].append(0)
self._modified = False
self.drag_value = 0
self.show_all()
def expose(self, area, context):
context.scale(area.get_allocated_width(), area.get_allocated_height())
xstep = 1.0 / self.cols
ystep = 1.0 / self.rows
for i in range(self.rows):
for j in range(self.cols):
if self.pixels[i][j]:
context.set_source_rgb(*self.fg)
else:
context.set_source_rgb(*self.bg)
context.rectangle(j*xstep, i*ystep, xstep*0.95, ystep*0.95)
context.fill()
return True
def draw_pixel(self, x, y, start=False):
width = self.drawing.get_allocated_width()
height = self.drawing.get_allocated_height()
ystep = 1.0 / self.rows
xstep = 1.0 / self.cols
x = x / width
y = y / height
pixel_row = int(y / ystep)
pixel_col = int(x / xstep)
if (0 <= pixel_row < self.rows) and (0 <= pixel_col < self.cols):
if start:
old_val = self.pixels[pixel_row][pixel_col]
self.drag_value = old_val ^ 1
self.pixels[pixel_row][pixel_col] = self.drag_value
self.drawing.queue_draw_area(pixel_col * xstep * width,
pixel_row * ystep * height,
xstep * width,
ystep * height)
def draw_motion_notify_event(self, area, event):
(window, x, y, state) = event.window.get_pointer()
if state & Gdk.ModifierType.BUTTON1_MASK:
self.draw_pixel(x, y)
def draw_button_press_event(self, area, event):
if event.button == 1:
self.draw_pixel(event.x, event.y, start=True)
self._modified = True
def get_pixels(self):
return self.pixels
def set_pixels(self, vals):
self.pixels = vals
self.drawing.queue_draw()
def set_fg(self, r, g, b):
self.fg = (r / 65535, g / 65535, b / 65535)
self.drawing.queue_draw()
def set_bg(self, r, g, b):
self.bg = (r / 65535, g / 65535, b / 65535)
self.drawing.queue_draw()
def clear_modified(self):
self._modified = False
@property
def modified(self):
return self._modified