diff --git a/README.md b/README.md index 5a087e4..c841b15 100644 --- a/README.md +++ b/README.md @@ -463,6 +463,7 @@ Currently, three types of menu options are supported: * AboutDialog * MessageDialog * Link + * HtmlDialog @@ -531,6 +532,36 @@ Example: } ``` + + + +**HtmlDialog** gives you full control over what's displayed in the message dialog (bonus: people can copy/paste text from this one!). + + + +Schema: + + * `caption` - (_optional_) the caption in the title bar of the modal + * `html` - (_required_) the html you want displayed in the dialog. Note: only a small subset of HTML is supported. []See the WX docs for more info](https://wxpython.org/Phoenix/docs/html/html_overview.html). + +Example: + +```python +{ + 'type': 'HtmlDialog', + 'menuTitle': 'Fancy Dialog!', + 'caption': 'Demo of the HtmlDialog', + 'html': ''' + + +

Hello world!

+

Lorem ipsum dolor sit amet, consectetur

+ + ''' +} + +``` + **A full example:** Two menu groups ("File" and "Help") with four menu items between them. diff --git a/gooey/gui/components/dialogs.py b/gooey/gui/components/dialogs.py new file mode 100644 index 0000000..b980713 --- /dev/null +++ b/gooey/gui/components/dialogs.py @@ -0,0 +1,40 @@ +import rewx.components as c +import wx +import wx.html2 +from rewx import wsx, render + + +def _html_window(html): + return wsx( + [c.Block, {'orient': wx.VERTICAL, 'flag': wx.EXPAND}, + [c.HtmlWindow, {'style': wx.TE_READONLY, 'flag': wx.EXPAND | wx.ALL, + 'proportion': 1, 'value': html}]] + ) + + +class HtmlDialog(wx.Dialog): + """ + A MessageDialog where the central contents are an HTML window + customizable by the user. + """ + def __init__(self, *args, **kwargs): + caption = kwargs.pop('caption', '') + html = kwargs.pop('html', '') + super(HtmlDialog, self).__init__(None, *args, **kwargs) + + wx.InitAllImageHandlers() + + self.SetTitle(caption) + sizer = wx.BoxSizer(wx.VERTICAL) + sizer.Add(render(_html_window(html), self), 1, wx.EXPAND) + + # in addition to creating the sizer, this actually attached + # a few common handlers which makes it feel more dialog-y. Thus + # it being done here rather than in rewx + btnSizer = self.CreateStdDialogButtonSizer(wx.OK) + sizer.Add(btnSizer, 0, wx.ALL | wx.EXPAND, 9) + self.SetSizer(sizer) + self.Layout() + + + diff --git a/gooey/gui/components/menubar.py b/gooey/gui/components/menubar.py index 5ca85b0..48dc989 100644 --- a/gooey/gui/components/menubar.py +++ b/gooey/gui/components/menubar.py @@ -4,6 +4,7 @@ from functools import partial import wx from gooey.gui import three_to_four +from gooey.gui.components.dialogs import HtmlDialog class MenuBar(wx.MenuBar): @@ -38,7 +39,8 @@ class MenuBar(wx.MenuBar): handlers = { 'Link': self.openBrowser, 'AboutDialog': self.spawnAboutDialog, - 'MessageDialog': self.spawnMessageDialog + 'MessageDialog': self.spawnMessageDialog, + 'HtmlDialog': self.spawnHtmlDialog } f = handlers[item['type']] return partial(f, item) @@ -59,6 +61,10 @@ class MenuBar(wx.MenuBar): caption=item.get('caption', '')).ShowModal() + def spawnHtmlDialog(self, item, *args, **kwargs): + HtmlDialog(caption=item.get('caption', ''), html=item.get('html')).ShowModal() + + def spawnAboutDialog(self, item, *args, **kwargs): """ Fill the wx.AboutBox with any relevant info the user provided @@ -78,4 +84,6 @@ class MenuBar(wx.MenuBar): if field in item: getattr(about, method)(item[field]) - three_to_four.AboutBox(about) \ No newline at end of file + three_to_four.AboutBox(about) + +