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.

151 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. Fl::flush();
  67. }
  68. /**
  69. Run the application event-loop until all Windows are closed.
  70. @note: This function is meant for standalones only, *never* call this from plugins.
  71. */
  72. void exec()
  73. {
  74. fIsRunning = true;
  75. Fl::run();
  76. fIsRunning = false;
  77. }
  78. /**
  79. Quit the application.
  80. This stops the event-loop and closes all Windows.
  81. */
  82. void quit()
  83. {
  84. fIsRunning = false;
  85. for (std::list<Fl_Double_Window*>::reverse_iterator rit = fWindows.rbegin(), rite = fWindows.rend(); rit != rite; ++rit)
  86. {
  87. Fl_Double_Window* const window(*rit);
  88. window->hide();
  89. }
  90. }
  91. /**
  92. Check if the application is about to quit.
  93. Returning true means there's no event-loop running at the moment.
  94. */
  95. bool isQuiting() const noexcept
  96. {
  97. return !fIsRunning;
  98. }
  99. private:
  100. bool fIsRunning;
  101. std::list<Fl_Double_Window*> fWindows;
  102. friend class NtkWindow;
  103. /** @internal used by NtkWindow. */
  104. void addWindow(Fl_Double_Window* const window)
  105. {
  106. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  107. if (fWindows.size() == 0)
  108. fIsRunning = true;
  109. fWindows.push_back(window);
  110. }
  111. /** @internal used by NtkWindow. */
  112. void removeWindow(Fl_Double_Window* const window)
  113. {
  114. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  115. fWindows.remove(window);
  116. if (fWindows.size() == 0)
  117. fIsRunning = false;
  118. }
  119. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NtkApp)
  120. };
  121. // -----------------------------------------------------------------------
  122. END_NAMESPACE_DGL
  123. #endif // DGL_NTK_APP_HPP_INCLUDED