DISTRHO Plugin Framework
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.

161 lines
3.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_NTK_APP_HPP_INCLUDED
  17. #define DGL_NTK_APP_HPP_INCLUDED
  18. #include "../Base.hpp"
  19. #ifdef override
  20. # define override_defined
  21. # undef override
  22. #endif
  23. #include <list>
  24. #include <FL/Fl.H>
  25. #include <FL/Fl_Double_Window.H>
  26. #include <FL/Fl_Shared_Image.H>
  27. #include <FL/x.H>
  28. #ifdef override_defined
  29. # define override
  30. # undef override_defined
  31. #endif
  32. START_NAMESPACE_DGL
  33. class NtkWindow;
  34. // -----------------------------------------------------------------------
  35. /**
  36. DGL compatible App class that uses NTK instead of OpenGL.
  37. @see App
  38. */
  39. class NtkApp
  40. {
  41. public:
  42. /**
  43. Constructor.
  44. */
  45. NtkApp()
  46. : fIsRunning(true),
  47. fWindows()
  48. {
  49. static bool initialized = false;
  50. if (! initialized)
  51. {
  52. initialized = true;
  53. fl_register_images();
  54. #ifdef DISTRHO_OS_LINUX
  55. fl_open_display();
  56. #endif
  57. }
  58. }
  59. /**
  60. Destructor.
  61. */
  62. ~NtkApp()
  63. {
  64. DISTRHO_SAFE_ASSERT(! fIsRunning);
  65. fWindows.clear();
  66. }
  67. /**
  68. Idle function.
  69. This calls the NTK event-loop once (and all idle callbacks).
  70. */
  71. void idle()
  72. {
  73. Fl::check();
  74. Fl::flush();
  75. }
  76. /**
  77. Run the application event-loop until all Windows are closed.
  78. @note: This function is meant for standalones only, *never* call this from plugins.
  79. */
  80. void exec()
  81. {
  82. fIsRunning = true;
  83. Fl::run();
  84. fIsRunning = false;
  85. }
  86. /**
  87. Quit the application.
  88. This stops the event-loop and closes all Windows.
  89. */
  90. void quit()
  91. {
  92. fIsRunning = false;
  93. for (std::list<Fl_Double_Window*>::reverse_iterator rit = fWindows.rbegin(), rite = fWindows.rend(); rit != rite; ++rit)
  94. {
  95. Fl_Double_Window* const window(*rit);
  96. window->hide();
  97. }
  98. }
  99. /**
  100. Check if the application is about to quit.
  101. Returning true means there's no event-loop running at the moment.
  102. */
  103. bool isQuiting() const noexcept
  104. {
  105. return !fIsRunning;
  106. }
  107. private:
  108. bool fIsRunning;
  109. std::list<Fl_Double_Window*> fWindows;
  110. friend class NtkWindow;
  111. /** @internal used by NtkWindow. */
  112. void addWindow(Fl_Double_Window* const window)
  113. {
  114. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  115. if (fWindows.size() == 0)
  116. fIsRunning = true;
  117. fWindows.push_back(window);
  118. }
  119. /** @internal used by NtkWindow. */
  120. void removeWindow(Fl_Double_Window* const window)
  121. {
  122. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  123. fWindows.remove(window);
  124. if (fWindows.size() == 0)
  125. fIsRunning = false;
  126. }
  127. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NtkApp)
  128. };
  129. // -----------------------------------------------------------------------
  130. END_NAMESPACE_DGL
  131. #endif // DGL_NTK_APP_HPP_INCLUDED