forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_box_ex.py
105 lines (88 loc) · 4.34 KB
/
message_box_ex.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
#!/usr/bin/env python
import webbrowser
import darkdetect
import markdown
import wx
import wx.html
from runtime import *
class MessageBoxEx(wx.Dialog):
def __init__(self, *args, title=None, message=None, button_texts=None, default_button=None, disable_buttons=None, is_md=False, size=(800, 600), checkbox_labels=None, **kwargs):
wx.Dialog.__init__(self, *args, **kwargs)
self.SetTitle(title)
self.button_texts = button_texts
self.default_button = default_button
self.buttons = []
self.return_value = None
self.checkboxes = []
self.checkbox_labels = checkbox_labels
vSizer = wx.BoxSizer(wx.VERTICAL)
message_sizer = wx.BoxSizer(wx.HORIZONTAL)
if is_md:
self.html = wx.html.HtmlWindow(self, wx.ID_ANY, size=size)
md_html = markdown.markdown(message)
# Adjust colors for dark mode on Mac and Linux
if darkdetect.isDark() and sys.platform != "win32":
dark_html = f"""
<!DOCTYPE html>
<html>
<body style="background-color:#656565; color:#ffffff;">
{md_html}
</body>
</html>
"""
self.html.SetPage(dark_html)
if "gtk2" in wx.PlatformInfo or "gtk3" in wx.PlatformInfo or sys.platform == "darwin":
self.html.SetStandardFonts()
else:
self.html.SetPage(md_html)
self.html.Bind(wx.html.EVT_HTML_LINK_CLICKED, self._onLinkClicked)
message_sizer.Add(self.html, 1, wx.ALL | wx.EXPAND, 20)
else:
self.message_label = wx.StaticText(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
self.message_label.Wrap(-1)
self.message_label.SetFont(wx.Font(9, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
self.message_label.Label = message
message_sizer.Add(self.message_label, 1, wx.ALL | wx.EXPAND, 20)
vSizer.Add(message_sizer, 1, wx.EXPAND, 5)
if checkbox_labels is not None:
checkbox_sizer = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY), wx.HORIZONTAL)
for checkbox_label in checkbox_labels:
checkbox = wx.CheckBox(self, wx.ID_ANY, checkbox_label, wx.DefaultPosition, wx.DefaultSize, 0)
self.checkboxes.append(checkbox)
checkbox_sizer.Add(checkbox, 0, wx.ALL, 5)
vSizer.Add(checkbox_sizer, 0, wx.EXPAND | wx.ALL, 10)
buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
buttons_sizer.Add((0, 0), 1, wx.EXPAND, 5)
# do this to not have any focus on the buttons, if default_button is set, then the corresponding button will have focus
self.SetFocus()
if button_texts is not None:
for i, button_text in enumerate(button_texts):
button = wx.Button(self, wx.ID_ANY, button_text, wx.DefaultPosition, wx.DefaultSize, 0)
self.buttons.append(button)
buttons_sizer.Add(button, 0, wx.ALL, 20)
button.Bind(wx.EVT_BUTTON, lambda e, i=i: self._onButtonClick(e, i))
if self.default_button == i + 1:
self._setDefaultButton(button)
if disable_buttons is not None and i + 1 in disable_buttons:
button.Disable()
buttons_sizer.Add((0, 0), 1, wx.EXPAND, 5)
vSizer.Add(buttons_sizer, 0, wx.EXPAND, 5)
self.SetSizer(vSizer)
self.Layout()
self.Centre(wx.BOTH)
# Autosize the dialog
self.SetSizerAndFit(vSizer)
def _setDefaultButton(self, button):
button.SetDefault()
button.SetFocus()
def _onButtonClick(self, e, button_index):
button_value = button_index + 1
if self.checkbox_labels is not None:
checkbox_values = [checkbox.IsChecked() for checkbox in self.checkboxes]
set_dlg_checkbox_values(checkbox_values)
self.return_value = {'button': button_value, 'checkboxes': checkbox_values}
self.EndModal(button_value)
def _onLinkClicked(self, event):
url = event.GetLinkInfo().GetHref()
# wx.LaunchDefaultBrowser(url)
webbrowser.open(url)