-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlitehtml-wx-browser.py
executable file
·469 lines (387 loc) · 14.1 KB
/
litehtml-wx-browser.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env vpython3
import os
import io
import logme
import requests
import urllib.parse
import wx
from litehtmlpy import litehtmlwx
class Button(litehtmlwx.litehtmlpy.html_tag):
def __init__(self, parent, attributes, doc):
super().__init__(doc)
self.parent = parent
self.attributes = attributes
def destroy(self):
pass
def draw(self, hdc, x, y, clip, ri):
super().draw(hdc, x, y, clip, ri)
pos = ri.pos()
x += pos.x
y += pos.y
w = pos.width
h = pos.height
print('Button.draw', x, y, w, h, self.attributes)
def on_mouse_over(self):
return False
def on_mouse_leave(self):
return False
def on_lbutton_down(self):
print('Button.on_lbutton_down')
return False
def on_lbutton_up(self):
print('Button.on_lbutton_up')
return False
def on_click(self):
print('Button.on_click')
self.parent.HtmlClick(self)
class Input(litehtmlwx.litehtmlpy.html_tag):
def __init__(self, parent, attributes, doc):
super().__init__(doc)
self.parent = parent
self.attributes = attributes
self.ctrl = wx.TextCtrl(parent, -1)
def destroy(self):
self.ctrl.Destroy()
self.ctrl = None
def draw(self, hdc, x, y, clip, ri):
super().draw(hdc, x, y, clip, ri)
pos = ri.pos()
#print('input.draw', ri.left(), ri.top(), ri.right(), ri.bottom())
x += pos.x
y += pos.y
w = pos.width
h = pos.height
css = self.css()
fh = css.get_font_size()
lh = css.get_line_height()
y = y + (h - lh) // 2
h = pos.height
self.ctrl.SetSize(x, y, w, h)
def on_mouse_over(self):
return False
def on_mouse_leave(self):
return False
def on_lbutton_down(self):
print('input.on_lbutton_down')
return False
def on_lbutton_up(self):
print('input.on_lbutton_up')
return False
def on_click(self):
print('input.on_click')
self.parent.HtmlClick(self)
class document_container(litehtmlwx.document_container):
handlers = []
def destroy(self):
super().reset()
for h in self.handlers:
h.destroy()
self.handlers = []
def on_anchor_click(self, url, el):
self.parent.HtmlClickHRef(url, el)
def import_css(self, text, url, base_url):
url = urllib.parse.urljoin(base_url, url)
if os.path.exists(url):
with open(url, 'rt') as fp:
data = fp.read()
elif url.split(':')[0] in ('http', 'https'):
r = requests.get(url)
if r.headers['Content-Type'] == 'text/css':
data = r.text
else:
data = None
if data is None:
print('unknown import_css', url)
return
return data
def create_element(self, tag_name, attributes=None, doc=None):
if tag_name == 'button':
print('create_element', tag_name, attributes, doc)
tagh = Button(self.parent, attributes, doc)
self.handlers.append(tagh)
return tagh
if tag_name == 'input':
t = attributes.get('type', '').lower()
if t == 'text':
tagh = Input(self.parent, attributes, doc)
self.handlers.append(tagh)
return tagh
elif t == 'submit':
tagh = Button(self.parent, attributes, doc)
self.handlers.append(tagh)
return tagh
return None
def draw_image(self, hdc, layer, url, base_url):
self.parent.DrawImage(layer, url, base_url)
def draw_solid_fill(self, hdc, layer, color):
self.parent.DrawSolidFill(layer, color)
class LiteWindow(wx.ScrolledWindow):
def __init__(self, parent, ID):
#super().__init__( parent, ID, style=wx.NO_FULL_REPAINT_ON_RESIZE)
wx.ScrolledWindow.__init__(self, parent, ID, style=wx.NO_FULL_REPAINT_ON_RESIZE)
self.SetScrollbar(wx.VERTICAL, 0, 0, 0, True)
self.SetBackgroundColour("WHITE")
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
self.Bind(wx.EVT_MOTION, self.OnMouseMove)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
self.url = None
self.images = {}
self.cntr = document_container(self)
self.cntr.reset()
self.doc = None
def Cleanup(self):
print('Cleanup')
self.cntr.destroy()
self.url = None
self.doc = None
self.cntr = None
def OnMouseDown(self, evt):
if self.doc is not None:
cx = self.GetScrollPos(wx.HORIZONTAL)
cy = self.GetScrollPos(wx.VERTICAL)
self.doc.on_lbutton_down(evt.x, evt.y, cx, cy, [])
evt.Skip()
def OnMouseUp(self, evt):
if self.doc is not None:
cx = self.GetScrollPos(wx.HORIZONTAL)
cy = self.GetScrollPos(wx.VERTICAL)
self.doc.on_lbutton_up(evt.x, evt.y, cx, cy, [])
evt.Skip()
def OnMouseMove(self, evt):
if self.doc is not None:
cx = self.GetScrollPos(wx.HORIZONTAL)
cy = self.GetScrollPos(wx.VERTICAL)
self.doc.on_mouse_over(evt.x, evt.y, cx, cy, [])
evt.Skip()
def OnSize(self, event):
if self.url is not None:
self.HtmlRender()
self.HtmlPaint()
event.Skip()
def OnPaint(self, event):
if self.url is not None:
dc = wx.PaintDC(self)
dc.DrawBitmap(self.cntr.bmp, wx.Point(0, 0))
event.Skip()
def OnScroll(self, event):
if self.url is not None:
self.HtmlPaint()
event.Skip()
def GetUrlData(self, url, html=True):
data = None
print('GetUrlData', url)
if os.path.exists(url):
with open(url, 'rb') as fp:
data = fp.read()
elif url.split(':')[0] in ('http', 'https'):
r = requests.get(url)
print(r.headers)
if html:
if r.headers['Content-Type'].find('text/html') != -1:
data = r.text
else:
if r.headers['Content-Type'] == 'image/png':
data = r.content
if data is None:
print('unknown url', url)
return None
return data
def LoadURL(self, url):
self.doc = None
self.cntr.destroy()
self.cntr.reset()
self.url = None
self.images = {}
html = self.GetUrlData(url, True)
if html is None:
return
self.url = url
self.doc = litehtmlwx.litehtmlpy.fromString(self.cntr, html, None, None)
self.HtmlRender()
self.HtmlPaint()
self.Refresh(True)
def HtmlRender(self):
size = self.GetClientSize()
self.doc.render(size[0], litehtmlwx.litehtmlpy.render_all)
self.cntr.size = size
h = self.doc.height() + 20 # + statusline.height
if h < 0:
h = 0
self.SetScrollbar(wx.VERTICAL, 0, size.Height, h, True)
def HtmlPaint(self):
self.cntr.reset()
size = self.GetClientSize()
y = self.GetScrollPos(wx.VERTICAL)
print('*'*20, 'HtmlPaint')
try:
clip = litehtmlwx.litehtmlpy.position(0, 0, size.Width, size.Height)
self.doc.draw(0, 0, -y, clip)
finally:
print('*'*20, '/HtmlPaint')
def HtmlClickHRef(self, url, element):
url = urllib.parse.urljoin(self.url, url)
print('HtmlClickHref', url)
self.GetParent().OnLocationOpen(url)
def HtmlClick(self, element):
print('HtmlClick', element.attributes)
def HtmlLoadImage(self, src, baseurl, redraw_on_ready):
if baseurl is not None:
url = urllib.parse.urljoin(baseurl, src)
else:
url = urllib.parse.urljoin(self.url, src)
data = self.GetUrlData(url, False)
if data is None:
return
img = wx.Image(io.BytesIO(data), type=wx.BITMAP_TYPE_ANY, index=-1)
if img.IsOk():
self.images[url] = img
return img
return None
def HtmlGetImage(self, src, baseurl):
if baseurl is not None:
url = urllib.parse.urljoin(baseurl, src)
else:
url = urllib.parse.urljoin(self.url, src)
#print('HtmlGetImage', src, baseurl)
return self.images.get(url, None)
def DrawImage(self, layer, url, base_url):
img = self.HtmlGetImage(url, base_url)
if img is None:
return
bpos = layer.border_box
#cpos = layer.clip_box
#print('DrawImage', url, (bpos.x, bpos.y, bpos.width, bpos.height))
img = img.Scale(bpos.width, bpos.height, wx.IMAGE_QUALITY_HIGH)
bmp = wx.Bitmap(img)
#gc = wx.GraphicsContext.Create(dc)
self.cntr.dc.DrawBitmap(bmp, wx.Point(bpos.x, bpos.y))
def DrawSolidFill(self, layer, color):
color = wx.Colour(color.red, color.green, color.blue)
b = wx.Brush(color)
self.cntr.dc.SetBrush(b)
bpos = layer.clip_box
self.cntr.dc.DrawRectangle(bpos.x, bpos.y, bpos.width, bpos.height)
self.cntr.dc.SetBrush(wx.NullBrush)
class LiteHtmlPanel(wx.Panel):
def __init__(self, parent, url):
wx.Panel.__init__(self, parent)
self.current = url
self.history = []
self.historyno = 0
self.frame = parent
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btn = wx.Button(self, -1, "Open", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, "◀︎", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoBack, btn)
btn = wx.Button(self, -1, "▶︎", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoForward, btn)
btn = wx.Button(self, -1, "Stop", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, "Refresh", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
txt = wx.StaticText(self, -1, "URL:")
btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)
self.location = wx.ComboBox(self, -1, "", style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
self.location.AppendItems([
'demo.html',
'litehtmlt.html',
'demo-01.html',
'http://wxPython.org',
'http://google.com'
])
self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location)
self.location.Bind(wx.EVT_TEXT_ENTER, self.OnLocationEnter)
btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)
self.wv = LiteWindow(self, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(btnSizer, 0, wx.EXPAND)
sizer.Add(self.wv, 1, wx.EXPAND)
self.SetSizer(sizer)
self.frame.SetStatusText("Loaded")
def OnOpenButton(self, event):
dlg = wx.TextEntryDialog(self, "Open Location",
"Enter a full URL or local path",
self.current, wx.OK|wx.CANCEL)
dlg.CentreOnParent()
if dlg.ShowModal() == wx.ID_OK:
self.current = dlg.GetValue()
self.wv.LoadURL(self.current)
dlg.Destroy()
def OnCheckCanGoBack(self, event):
event.Enable(self.historyno > 0)
def OnCheckCanGoForward(self, event):
event.Enable(self.historyno < len(self.history))
def OnPrevPageButton(self, event):
n = self.historyno-1
if n >=0 and n < len(self.history):
self.historyno = n
url = self.history[n]
self.location.SetValue(url)
self.wv.LoadURL(url)
def OnNextPageButton(self, event):
n = self.historyno+1
if n >=0 and n < len(self.history):
self.historyno = n
url = self.history[n]
self.location.SetValue(url)
self.wv.LoadURL(url)
def OnStopButton(self, event):
pass
def OnRefreshPageButton(self, event):
pass
def OnLocationSelect(self, event):
url = self.location.GetStringSelection()
self.history.append(url)
self.historyno = len(self.history)-1
self.wv.LoadURL(url)
def OnLocationEnter(self, event):
url = self.location.GetValue()
self.history.append(url)
self.historyno = len(self.history)-1
self.wv.LoadURL(url)
def OnLocationOpen(self, url):
self.location.SetValue(url)
self.history.append(url)
self.historyno = len(self.history)-1
self.wv.LoadURL(url)
class SampleFrame(wx.Frame):
def __init__(self, parent, url=None):
#super().__init__(parent, title="litehtml", size=(800,900))
wx.Frame.__init__(self, parent, title="litehtml", size=(800,900))
# add a statusbar
self.CreateStatusBar()
# Add a menubar with just a quit item
mb = wx.MenuBar()
menu = wx.Menu()
menu.Append(wx.ID_EXIT, '&Quit')
mb.Append(menu, "File")
self.SetMenuBar(mb)
self.Bind(wx.EVT_MENU, self.OnQuit, id=wx.ID_EXIT)
# Create the main panel
self.pnl = LiteHtmlPanel(self, url)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, event):
print('Close')
self.pnl.wv.Cleanup()
event.Skip()
# Menu event handler to close the frame
def OnQuit(self, evt):
self.Close(force=True)
def main():
app = wx.App(False)
frm = SampleFrame(None, "")
frm.Show()
app.MainLoop()
if __name__ == '__main__':
main()