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.

80 lines
2.5 KiB

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