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.

150 lines
3.5KB

  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. #include <list>
  20. #include <FL/Fl.H>
  21. #include <FL/Fl_Double_Window.H>
  22. #include <FL/Fl_Shared_Image.H>
  23. #include <FL/x.H>
  24. START_NAMESPACE_DGL
  25. class NtkWindow;
  26. // -----------------------------------------------------------------------
  27. /**
  28. DGL compatible App class that uses NTK instead of OpenGL.
  29. @see App
  30. */
  31. class NtkApp
  32. {
  33. public:
  34. /**
  35. Constructor.
  36. */
  37. NtkApp()
  38. : fIsRunning(true),
  39. fWindows()
  40. {
  41. static bool initialized = false;
  42. if (! initialized)
  43. {
  44. initialized = true;
  45. fl_register_images();
  46. #ifdef DISTRHO_OS_LINUX
  47. fl_open_display();
  48. #endif
  49. }
  50. }
  51. /**
  52. Destructor.
  53. */
  54. ~NtkApp()
  55. {
  56. DISTRHO_SAFE_ASSERT(! fIsRunning);
  57. fWindows.clear();
  58. }
  59. /**
  60. Idle function.
  61. This calls the NTK event-loop once (and all idle callbacks).
  62. */
  63. void idle()
  64. {
  65. Fl::check();
  66. }
  67. /**
  68. Run the application event-loop until all Windows are closed.
  69. @note: This function is meant for standalones only, *never* call this from plugins.
  70. */
  71. void exec()
  72. {
  73. fIsRunning = true;
  74. Fl::run();
  75. fIsRunning = false;
  76. }
  77. /**
  78. Quit the application.
  79. This stops the event-loop and closes all Windows.
  80. */
  81. void quit()
  82. {
  83. fIsRunning = false;
  84. for (std::list<Fl_Double_Window*>::reverse_iterator rit = fWindows.rbegin(), rite = fWindows.rend(); rit != rite; ++rit)
  85. {
  86. Fl_Double_Window* const window(*rit);
  87. window->hide();
  88. }
  89. }
  90. /**
  91. Check if the application is about to quit.
  92. Returning true means there's no event-loop running at the moment.
  93. */
  94. bool isQuiting() const noexcept
  95. {
  96. return !fIsRunning;
  97. }
  98. private:
  99. bool fIsRunning;
  100. std::list<Fl_Double_Window*> fWindows;
  101. friend class NtkWindow;
  102. /** @internal used by NtkWindow. */
  103. void addWindow(Fl_Double_Window* const window)
  104. {
  105. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  106. if (fWindows.size() == 0)
  107. fIsRunning = true;
  108. fWindows.push_back(window);
  109. }
  110. /** @internal used by NtkWindow. */
  111. void removeWindow(Fl_Double_Window* const window)
  112. {
  113. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  114. fWindows.remove(window);
  115. if (fWindows.size() == 0)
  116. fIsRunning = false;
  117. }
  118. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NtkApp)
  119. };
  120. // -----------------------------------------------------------------------
  121. END_NAMESPACE_DGL
  122. #endif // DGL_NTK_APP_HPP_INCLUDED