-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_bar.py
391 lines (293 loc) · 10 KB
/
header_bar.py
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import wx
from swlib import pysw
from swlib.pysw import SW, process_digits
from backend import chapter_headings
from backend.bibleinterface import biblemgr
from protocols import protocol_handler
from displayframe import DisplayFrame
from gui import guiutil
from tooltip import TooltipConfig, TooltipDisplayer
import guiconfig
from util.observerlist import ObserverList
from util import osutils
def on_headings_hover(frame, href, url, element, x, y):
if url is None:
url = SW.URL(href)
ref = url.getHostName()
# print ref
frame.show_tooltip(ChapterHeadingsTooltipConfig(ref))
class ChapterHeadingsTooltipConfig(TooltipConfig):
"""The tooltip configuration for the headings in a chapter."""
def __init__(self, ref):
self.ref = ref
super(ChapterHeadingsTooltipConfig, self).__init__(
book=biblemgr.bible
)
def get_text(self):
vk = pysw.UserVK(self.ref)
html = u'<font size=+1><b><a href="nbible:%s">%s</a></b></font>' % (
self.ref, vk.get_book_chapter()
)
html += u": %s<br>" % (_("%s verses") %
process_digits(str(
vk.verseCount(
ord(vk.Testament()),
ord(vk.Book()),
vk.Chapter())
), userOutput=True))
html += u"<ul>"
for vk, text in chapter_headings.get_chapter_headings(self.ref):
html += u'<li><a href="nbible:%s">%s</a>' % (vk, text)
html += u"</ul>"
return html
def get_title(self):
return self.ref
protocol_handler.register_hover("headings", on_headings_hover)
protocol_handler.register_handler("headings", DisplayFrame.on_link_clicked_bible)
def get_line_colour():
return wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DSHADOW)
class ChapterItem(wx.Panel):
"""
A chapter item in the header bar.
Handles mouse over and drawing itself
"""
# how much border around the text do we want?
border = 3
def __init__(self, parent, internal, display=None, is_current=False):
super(ChapterItem, self).__init__(parent)
self.internal_text = internal
self.chapter = pysw.internal_to_user(internal)
if display is None:
self.display = self.chapter
else:
self.display = display
self.is_current = is_current
self.Bind(wx.EVT_PAINT, self.on_paint)
# we have a buffered drawing, so we don't care about erasing the
# background
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda evt: None)
# callafter as under MSW, enter seems to come before leave in
# places
self.Bind(wx.EVT_ENTER_WINDOW,
lambda evt:wx.CallAfter(self.on_enter, evt.X, evt.Y))
self.Bind(wx.EVT_LEAVE_WINDOW, self.on_leave)
self.Bind(wx.EVT_LEFT_DOWN, lambda evt:
self.Parent.on_click(self.internal_text)
)
# draw our bitmapped version, and set our size based on it
self.draw()
def set_chapter(self, chapter, internal_text, display=None):
self.internal_text = internal_text
if self.chapter == chapter:
return
if display is None:
self.display = chapter
else:
self.display = display
self.chapter = chapter
self.draw()
self.Refresh()
def on_enter(self, x, y):
self.Parent.current_target = self, self.ScreenRect, 1
if self.Parent.tooltip.target == self:
return
# print self.internal_text
protocol_handler.on_hover(self.Parent,
"headings:%s" % self.internal_text, None, x, y)
def on_leave(self, event=None):
if event: event.Skip()
if event and osutils.is_gtk():
# Under GTK, we can get the notification of leave before the
# enter! This used to cause really annoying header bar tooltips
# to pop up after moving diagonally across it quickly.
wx.CallAfter(self.on_leave)
return
self.Parent.current_target = None
self.Parent.tooltip.MouseOut(None)
def draw(self):
# create a memory dc
dc = wx.MemoryDC()
width, height = 1, 1
bmp = wx.EmptyBitmap(width, height)
dc.SelectObject(bmp)
# set its font to the window font
dc.SetFont(self.Font)
# get the size of the text
text = self.display
text_width, text_height = dc.GetTextExtent(text)
# and of the window
width = text_width + self.border * 2
height = text_height + self.border * 2
# create a bitmap of the given size and select it into the DC
bmp = wx.EmptyBitmap(width, height)
dc.SelectObject(bmp)
dc.Background = wx.Brush(
wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNFACE)
)
dc.Clear()
# set up the dc
dc.Pen = wx.Pen(get_line_colour())
if self.is_current:
background, text_colour = guiconfig.get_window_colours(
html_style=False
)
dc.SetBrush(wx.Brush(background))
dc.SetTextForeground(text_colour)
radius = self.border
# this will be clipped so we will only see the top part
dc.DrawRoundedRectangle(0, 0, width, height+radius, radius)
else:
# draw bottom border
dc.DrawLine(0, height-1, width, height-1)
dc.SetTextForeground(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT)
)
dc.DrawText(text, self.border, self.border)
# assign the resultant bitmap
del dc
self.bmp = bmp
# and set our size
self.SetSize((width, height))
def on_paint(self, event):
# paint it on
wx.BufferedPaintDC(self, self.bmp)
def set_background_colour(self, colour):
self.SetBackgroundColour(colour)
self.draw()
class Line(wx.Window):
"""A rectangle of colour"""
def __init__(self, parent):
super(Line, self).__init__(parent)
self.Bind(wx.EVT_PAINT, self.paint)
def paint(self, event):
dc = wx.PaintDC(self)
dc.Background = wx.Brush(get_line_colour())
dc.Clear()
class HeaderBar(TooltipDisplayer, wx.Panel):
def __init__(self, parent, i_current_chapter, style=wx.NO_BORDER):
super(HeaderBar, self).__init__(parent, style=style)
self.i_current_chapter = i_current_chapter
self.Bind(wx.EVT_SIZE, self.on_size)
self.on_click = ObserverList()
# if we are at either end of the Bible, this line fills in the rest of
# the bottom line.
self.line = Line(self)
self.create_item()
self.MinSize = -1, self.item.Size[1] + 1
if guiconfig.mainfrm:
guiconfig.mainfrm.add_toplevel(self.tooltip)
self.items = [[], []]
def create_item(self):
self.item = ChapterItem(self, self.i_current_chapter, is_current=True)
self.item.Position = 0, 0
font = self.item.Font
font.SetWeight(wx.FONTWEIGHT_BOLD)
self.item.Font = font
chapter, c, internal = self.get_next_chapter(
self.i_current_chapter, dir=0, short=False
)
self.item.set_chapter(chapter, internal)
def set_current_chapter(self, chapter, internal):
guiutil.FreezeUI(self)
# print `chapter`, `internal`
# stop tooltip
self.tooltip.Stop()
# set new current item
self.i_current_chapter = internal
chapter, c, internal = self.get_next_chapter(
self.i_current_chapter, dir=0, short=False
)
self.item.set_chapter(chapter, internal)
# and refresh everything else
self.on_size()
def get_next_chapter(self, i_book_chapter, dir=1, short=True):
internal = pysw.VK(i_book_chapter)
if short:
key_type = pysw.AbbrevVK
else:
key_type = pysw.UserVK
internal.chapter += dir
if internal.Error():
return None, None, None
vk = key_type(internal)
return vk.get_book_chapter(), vk.chapter, internal.get_book_chapter()
def on_size(self, event=None):
guiutil.FreezeUI(self)
self.line.Hide()
# keep a new list of the previous and next items
new_items = [[], []]
item = self.item
# position our main item
item.Position = (self.Size[0] -item.Size[0]) / 2, 1#item.Position[1]
for dir, is_left in (1, False), (-1, True):
last_item = item
break_next = False
# an array of the old items on this side
items = self.items[not is_left]
while not break_next:
next_chapter, chapter_number, internal_text = \
self.get_next_chapter(last_item.internal_text, dir)
# if there is no next chapter, we have reached the start or
# end of the Bible, so stop
if next_chapter is None:
self.line.Show()
w, h = self.Size
if is_left:
self.line.MoveXY(0, h-1)
self.line.Size = (last_item.Position[0] + 1, 1)
else:
last_end = last_item.Position[0] + last_item.Size[0]
self.line.MoveXY(last_end, h - 1)
self.line.Size = (self.Size[0] - last_end, h-1)
break
# if we've run out of items on this side, start creating new
# ones
if not items:
next_item = ChapterItem(self, internal_text)
next_item.set_chapter(next_chapter, internal_text)
# Otherwise, remove the old ones from the list and move on
else:
next_item = items.pop(0)
next_item.set_chapter(next_chapter, internal_text)
# find the item's position
break_next = self.position_item(next_item, last_item, is_left)
# and add it to our list of new items
new_items[not is_left].append(next_item)
# and update last item and chapter
last_item = next_item
last_chapter = next_chapter
# delete the items we haven't used this time
for item in self.items[0] + self.items[1]:
item.Destroy()
# and update our list of items
self.items = new_items
self.Update()
def position_item(self, next_item, last_item, is_left):
"""Positions an item based on the previous one and the direction
Returns True if this is the last item to do on this side"""
if is_left:
next_item.Position = (
last_item.Position[0] - next_item.Size[0],
last_item.Position[1]
)
else:
next_item.Position = (
last_item.Position[0] + last_item.Size[0],
last_item.Position[1]
)
if is_left:
if next_item.Position[0] <= 0:
# on or past the left hand border
return True
else:
if next_item.Position[0] + next_item.Size[0] >= self.Size[0]:
# on or past the right hand border
return True
if __name__ == '__main__':
a = wx.App(0)
f = wx.Frame(None)
h = HeaderBar(f, "Psalms 150")
h.on_click += lambda ref:h.set_current_chapter(ref)
f.Fit()
f.Show()
a.MainLoop()