wx-wrappers.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #define WX_UTF8(str) wxString(str, wxConvUTF8)
  20. struct gs_window;
  21. gs_window WxToGSWindow(const wxWindow *window);
  22. void OBSErrorBox(wxWindow *parent, const char *message, ...);
  23. /* returns actual ID of menu item clicked rather than be forced to use
  24. * all the BS handler crap */
  25. int WXDoPopupMenu(wxWindow *parent, wxMenu *menu);
  26. /*
  27. * RAII wx connection wrapper
  28. *
  29. * Automatically disconnects events on destruction rather than having to
  30. * manually call Disconnect for every Connect.
  31. */
  32. class WXConnector {
  33. wxEvtHandler *obj;
  34. wxEventType eventType;
  35. wxObjectEventFunction func;
  36. wxObject *userData;
  37. wxEvtHandler *eventSink;
  38. WXConnector(WXConnector const&) = delete;
  39. WXConnector &operator=(WXConnector const&) = delete;
  40. public:
  41. inline WXConnector()
  42. : obj (NULL),
  43. eventType (0),
  44. func (NULL),
  45. userData (NULL),
  46. eventSink (NULL)
  47. {
  48. }
  49. inline WXConnector(wxEvtHandler *obj, wxEventType eventType,
  50. wxObjectEventFunction func, wxObject *userData,
  51. wxEvtHandler *eventSink)
  52. : obj (obj),
  53. eventType (eventType),
  54. func (func),
  55. userData (userData),
  56. eventSink (eventSink)
  57. {
  58. obj->Connect(eventType, func, userData, eventSink);
  59. }
  60. inline WXConnector(WXConnector &&c)
  61. : obj (c.obj),
  62. eventType (c.eventType),
  63. func (c.func),
  64. userData (c.userData),
  65. eventSink (c.eventSink)
  66. {
  67. c.obj = NULL;
  68. }
  69. inline ~WXConnector()
  70. {
  71. Disconnect();
  72. }
  73. inline void Connect(wxEvtHandler *obj, wxEventType eventType,
  74. wxObjectEventFunction func, wxObject *userData,
  75. wxEvtHandler *eventSink)
  76. {
  77. Disconnect();
  78. this->obj = obj;
  79. this->eventType = eventType;
  80. this->func = func;
  81. this->userData = userData;
  82. this->eventSink = eventSink;
  83. obj->Connect(eventType, func, userData, eventSink);
  84. }
  85. inline void Disconnect()
  86. {
  87. if (obj) {
  88. obj->Disconnect(eventType, func, userData, eventSink);
  89. obj = NULL;
  90. }
  91. }
  92. };
  93. class ConnectorList {
  94. std::vector<WXConnector> connectors;
  95. public:
  96. inline void Add(wxEvtHandler *obj, wxEventType type,
  97. wxObjectEventFunction func, wxObject *userData,
  98. wxEvtHandler *eventSink)
  99. {
  100. connectors.emplace_back(obj, type, func, userData, eventSink);
  101. }
  102. };