You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import webbrowser
  2. from functools import partial
  3. import wx # type: ignore
  4. from gooey.gui import three_to_four
  5. from gooey.gui.components.dialogs import HtmlDialog
  6. class MenuBar(wx.MenuBar):
  7. """
  8. Wx.MenuBar handles converting the users list of Menu Groups into
  9. concrete wx.Menu instances.
  10. """
  11. def __init__(self, buildSpec, *args, **kwargs):
  12. super(MenuBar,self).__init__(*args, **kwargs)
  13. self.buildSpec = buildSpec
  14. self.makeMenuItems(buildSpec.get('menu', []))
  15. def makeMenuItems(self, menuGroups):
  16. """
  17. Assign the menu groups list to wx.Menu instances
  18. and bind the appropriate handlers.
  19. """
  20. for menuGroup in menuGroups:
  21. menu = wx.Menu()
  22. for item in menuGroup.get('items'):
  23. option = menu.Append(wx.NewId(), item.get('menuTitle', ''))
  24. self.Bind(wx.EVT_MENU, self.handleMenuAction(item), option)
  25. self.Append(menu, '&' + menuGroup.get('name'))
  26. def handleMenuAction(self, item):
  27. """
  28. Dispatch based on the value of the type field.
  29. """
  30. handlers = {
  31. 'Link': self.openBrowser,
  32. 'AboutDialog': self.spawnAboutDialog,
  33. 'MessageDialog': self.spawnMessageDialog,
  34. 'HtmlDialog': self.spawnHtmlDialog
  35. }
  36. f = handlers[item['type']]
  37. return partial(f, item)
  38. def openBrowser(self, item, *args, **kwargs):
  39. """
  40. Open the supplied URL in the user's default browser.
  41. """
  42. webbrowser.open(item.get('url'))
  43. def spawnMessageDialog(self, item, *args, **kwargs):
  44. """
  45. Show a simple message dialog with the user's message and caption.
  46. """
  47. wx.MessageDialog(self, item.get('message', ''),
  48. caption=item.get('caption', '')).ShowModal()
  49. def spawnHtmlDialog(self, item, *args, **kwargs):
  50. HtmlDialog(caption=item.get('caption', ''), html=item.get('html')).ShowModal()
  51. def spawnAboutDialog(self, item, *args, **kwargs):
  52. """
  53. Fill the wx.AboutBox with any relevant info the user provided
  54. and launch the dialog
  55. """
  56. aboutOptions = {
  57. 'name': 'SetName',
  58. 'version': 'SetVersion',
  59. 'description': 'SetDescription',
  60. 'copyright': 'SetCopyright',
  61. 'website': 'SetWebSite',
  62. 'developer': 'AddDeveloper',
  63. 'license': 'SetLicense'
  64. }
  65. about = three_to_four.AboutDialog()
  66. for field, method in aboutOptions.items():
  67. if field in item:
  68. getattr(about, method)(item[field])
  69. three_to_four.AboutBox(about)