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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# Copyright (C) 2009, Aleksey Lim
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import gtk
import gobject
import math
import bisect
import logging
class SmoothTable(gtk.Container):
__gsignals__ = {
'set-scroll-adjustments': (gobject.SIGNAL_RUN_FIRST, None,
[gtk.Adjustment, gtk.Adjustment]),
'fill-in': (gobject.SIGNAL_RUN_FIRST, None,
[gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT]),
}
def __init__(self, rows, columns, new_widget):
assert(rows and columns)
self._rows = []
self._adj = None
self._adj_value_changed_id = None
self._bin_window = None
self._bin_rows = 0
self._cell_height = 0
self._reordered = None
self._last_allocation = None
gtk.Container.__init__(self)
cell_no = 0
for y in range(rows + 2):
row = []
for x in range(columns):
cell = new_widget()
cell.set_parent(self)
cell.size_allocate(gtk.gdk.Rectangle(-1, -1))
cell_no += 1
row.append(cell)
self._rows.append(row)
def get_columns(self):
return len(self._rows[0])
columns = property(get_columns)
def get_rows(self):
return len(self._rows) - 2
rows = property(get_rows)
def get_head(self):
if self._adj is None:
return 0
return int(self._adj.value) - int(self._adj.value) % self._cell_height
head = property(get_head)
def set_count(self, count):
self._bin_rows = max(0, math.ceil(float(count) / len(self._rows[0])))
if self._adj is not None:
self._setup_adjustment(force=True)
if self.flags() & gtk.REALIZED:
self._bin_window.resize(self.allocation.width,
int(self._adj.upper))
def do_realize(self):
self.set_flags(gtk.REALIZED)
self.window = gtk.gdk.Window(
self.get_parent_window(),
window_type=gtk.gdk.WINDOW_CHILD,
x=self.allocation.x,
y=self.allocation.y,
width=self.allocation.width,
height=self.allocation.height,
wclass=gtk.gdk.INPUT_OUTPUT,
colormap=self.get_colormap(),
event_mask=gtk.gdk.VISIBILITY_NOTIFY_MASK)
self.window.set_user_data(self)
self._bin_window = gtk.gdk.Window(
self.window,
window_type=gtk.gdk.WINDOW_CHILD,
x=0,
y=int(-self._adj.value),
width=self.allocation.width,
height=int(self._adj.upper),
colormap=self.get_colormap(),
wclass=gtk.gdk.INPUT_OUTPUT,
event_mask=(self.get_events() | gtk.gdk.EXPOSURE_MASK |
gtk.gdk.SCROLL_MASK))
self._bin_window.set_user_data(self)
self.set_style(self.style.attach(self.window))
self.style.set_background(self.window, gtk.STATE_NORMAL)
self.style.set_background(self._bin_window, gtk.STATE_NORMAL)
for row in self._rows:
for cell in row:
cell.set_parent_window(self._bin_window)
self.queue_resize()
def do_size_allocate(self, allocation):
if self._reordered is not None:
if allocation == self._reordered:
self._reordered = None
return
self._reordered = None
self.allocation = allocation
self._cell_height = allocation.height / self.rows
self._setup_adjustment(force=True)
if self.flags() & gtk.REALIZED:
self.window.move_resize(*allocation)
self._bin_window.resize(allocation.width, int(self._adj.upper))
def do_unrealize(self):
self._bin_window.set_user_data(None)
self._bin_window.destroy()
self._bin_window = None
gtk.Container.do_unrealize(self)
def do_style_set(self, style):
gtk.Widget.do_style_set(self, style)
if self.flags() & gtk.REALIZED:
self.style.set_background(self._bin_window, gtk.STATE_NORMAL)
def do_expose_event(self, event):
if event.window != self._bin_window:
return False
gtk.Container.do_expose_event(self, event)
return False
def do_map(self):
self.set_flags(gtk.MAPPED)
for row in self._rows:
for cell in row:
cell.map()
self._bin_window.show()
self.window.show()
def do_size_request(self, req):
req.width = 0
req.height = 0
for row in self._rows:
for cell in row:
cell.size_request()
def do_forall(self, include_internals, callback, data):
for row in self._rows:
for cell in row:
callback(cell, data)
def do_add(self, widget):
pass
def do_remove(self, widget):
pass
def do_set_scroll_adjustments(self, hadjustment, vadjustment):
if vadjustment is None or vadjustment == self._adj:
return
if self._adj is not None:
self._adj.disconnect(self._adj_value_changed_id)
self._adj = vadjustment
self._setup_adjustment()
self._adj_value_changed_id = vadjustment.connect('value-changed',
self.__adjustment_value_changed_cb)
def _setup_adjustment(self, force=False):
self._adj.lower = 0
self._adj.upper = self._bin_rows * self._cell_height
self._adj.page_size = self.allocation.height
self._adj.changed()
max_value = max(0, self._adj.upper - self._adj.page_size)
if self._adj.value > max_value:
self._adj.value = max_value
self._adj.value_changed()
elif force:
self._adj.value_changed()
def _allocate_row(self, row, cell_y):
cell_x = 0
cell_no = cell_y / self._cell_height * self.columns
for cell in row:
self.emit('fill-in', cell, cell_no)
callocation = gtk.gdk.Rectangle(cell_x, cell_y)
callocation.width = self.allocation.width / self.columns
callocation.height = self._cell_height
cell.size_allocate(callocation)
cell_x += callocation.width
cell_no += 1
def __adjustment_value_changed_cb(self, sender=None):
if not self.flags() & gtk.REALIZED:
return
spare_rows = []
visible_rows = []
page_end = self._adj.value + self._adj.page_size
if self._last_allocation != self.allocation:
self._last_allocation = self.allocation
spare_rows = [] + self._rows
else:
class IndexedRow:
def __init__(self, row):
self.row = row
def __lt__(self, other):
return self.row[0].allocation.y < other.row[0].allocation.y
for row in self._rows:
if row[0].allocation.y < 0 or \
row[0].allocation.y > page_end or \
(row[0].allocation.y + self._cell_height) < \
self._adj.value:
spare_rows.append(row)
else:
bisect.insort_right(visible_rows, IndexedRow(row))
if not visible_rows or \
len(visible_rows) < self.rows + (self.head != visible_rows[0]):
self._reordered = self.allocation
def insert_spare_row(cell_y, end_y):
while cell_y < end_y:
if not spare_rows:
logging.error('spare_rows should not be empty')
return
row = spare_rows.pop()
self._allocate_row(row, cell_y)
cell_y = cell_y + self._cell_height
cell_y = self.head
for i in visible_rows:
insert_spare_row(cell_y, i.row[0].allocation.y)
cell_y = i.row[0].allocation.y + i.row[0].allocation.height
insert_spare_row(cell_y, page_end)
self._bin_window.move(0, int(-self._adj.value))
self.window.process_updates(True)
SmoothTable.set_set_scroll_adjustments_signal('set-scroll-adjustments')
if __name__ == '__main__':
window = gtk.Window()
scrolled = gtk.ScrolledWindow()
scrolled.set_policy(gtk.POLICY_ALWAYS, gtk.POLICY_ALWAYS)
window.add(scrolled)
def cb(sender, button, offset):
button.props.label = str(offset)
table = SmoothTable(3, 3, gtk.Button)
table.connect('fill-in', cb)
table.set_count(100)
scrolled.add(table)
window.show_all()
gtk.main()
|