wx-wrappers.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <wx/window.h>
  16. #include <wx/event.h>
  17. #include <wx/menu.h>
  18. #include <vector>
  19. struct gs_window;
  20. gs_window WxToGSWindow(const wxWindow *window);
  21. void OBSErrorBox(wxWindow *parent, const char *message, ...);
  22. /* returns actual ID of menu item clicked rather than be forced to use
  23. * all the BS handler crap */
  24. int WXDoPopupMenu(wxWindow *parent, wxMenu *menu);
  25. /*
  26. * RAII wx connection wrapper
  27. *
  28. * Automatically disconnects events on destruction rather than having to
  29. * manually call Disconnect for every Connect.
  30. */
  31. class WXConnector {
  32. wxEvtHandler *obj;
  33. wxEventType eventType;
  34. wxObjectEventFunction func;
  35. wxObject *userData;
  36. wxEvtHandler *eventSink;
  37. WXConnector(WXConnector const&) = delete;
  38. WXConnector &operator=(WXConnector const&) = delete;
  39. public:
  40. inline WXConnector()
  41. : obj (NULL),
  42. eventType (0),
  43. func (NULL),
  44. userData (NULL),
  45. eventSink (NULL)
  46. {
  47. }
  48. inline WXConnector(wxEvtHandler *obj, wxEventType eventType,
  49. wxObjectEventFunction func, wxObject *userData,
  50. wxEvtHandler *eventSink)
  51. : obj (obj),
  52. eventType (eventType),
  53. func (func),
  54. userData (userData),
  55. eventSink (eventSink)
  56. {
  57. obj->Connect(eventType, func, userData, eventSink);
  58. }
  59. inline WXConnector(WXConnector &&c)
  60. : obj (c.obj),
  61. eventType (c.eventType),
  62. func (c.func),
  63. userData (c.userData),
  64. eventSink (c.eventSink)
  65. {
  66. c.obj = NULL;
  67. }
  68. inline ~WXConnector()
  69. {
  70. Disconnect();
  71. }
  72. inline void Connect(wxEvtHandler *obj, wxEventType eventType,
  73. wxObjectEventFunction func, wxObject *userData,
  74. wxEvtHandler *eventSink)
  75. {
  76. Disconnect();
  77. this->obj = obj;
  78. this->eventType = eventType;
  79. this->func = func;
  80. this->userData = userData;
  81. this->eventSink = eventSink;
  82. obj->Connect(eventType, func, userData, eventSink);
  83. }
  84. inline void Disconnect()
  85. {
  86. if (obj) {
  87. obj->Disconnect(eventType, func, userData, eventSink);
  88. obj = NULL;
  89. }
  90. }
  91. };
  92. class ConnectorList {
  93. std::vector<WXConnector> connectors;
  94. public:
  95. inline void Add(wxEvtHandler *obj, wxEventType type,
  96. wxObjectEventFunction func, wxObject *userData,
  97. wxEvtHandler *eventSink)
  98. {
  99. connectors.emplace_back(obj, type, func, userData, eventSink);
  100. }
  101. };