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.

198 lines
5.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2025 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. #include "tests.hpp"
  17. #include "dgl.cpp"
  18. #include "dgl/src/Color.cpp"
  19. #include "dgl/src/NanoVG.cpp"
  20. #include "dgl/src/Resources.cpp"
  21. START_NAMESPACE_DGL
  22. // --------------------------------------------------------------------------------------------------------------------
  23. class NanoFilePicker : public NanoStandaloneWindow
  24. {
  25. Rectangle<uint> buttonBounds;
  26. bool buttonClick = false;
  27. bool buttonHover = false;
  28. String selectedFile;
  29. public:
  30. NanoFilePicker(Application& app)
  31. : NanoStandaloneWindow(app),
  32. selectedFile("No file selected yet")
  33. {
  34. #ifndef DGL_NO_SHARED_RESOURCES
  35. loadSharedResources();
  36. #endif
  37. setResizable(true);
  38. setTitle("FileBrowserDialog");
  39. const double scaleFactor = getScaleFactor();
  40. setGeometryConstraints(500 * scaleFactor, 200 * scaleFactor, true);
  41. setSize(500 * scaleFactor, 200 * scaleFactor);
  42. done();
  43. }
  44. protected:
  45. void onNanoDisplay() override
  46. {
  47. const double scaleFactor = getScaleFactor();
  48. // Selected file
  49. beginPath();
  50. fontSize(14 * scaleFactor);
  51. textAlign(ALIGN_LEFT | ALIGN_MIDDLE);
  52. fillColor(255, 255, 255, 255);
  53. text(20 * scaleFactor, getHeight()/2, selectedFile, NULL);
  54. closePath();
  55. // Button background
  56. beginPath();
  57. fillColor(Color(32, buttonClick ? 128 : 32, buttonHover ? 128 : 32));
  58. strokeColor(Color());
  59. rect(buttonBounds.getX(), buttonBounds.getY(), buttonBounds.getWidth(), buttonBounds.getHeight());
  60. fill();
  61. stroke();
  62. closePath();
  63. // Button label
  64. beginPath();
  65. fontSize(14 * scaleFactor);
  66. Rectangle<float> buttonTextBounds;
  67. textBounds(0, 0, "Press me", NULL, buttonTextBounds);
  68. textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
  69. fillColor(255, 255, 255, 255);
  70. text(buttonBounds.getX() + buttonBounds.getWidth()/2,
  71. buttonBounds.getY() + buttonBounds.getHeight()/2,
  72. "Press me", NULL);
  73. closePath();
  74. }
  75. bool onMotion(const MotionEvent& ev) override
  76. {
  77. const bool newButtonHover = buttonBounds.contains(ev.pos);
  78. if (newButtonHover != buttonHover)
  79. {
  80. buttonHover = newButtonHover;
  81. repaint();
  82. return true;
  83. }
  84. return newButtonHover;
  85. }
  86. bool onMouse(const MouseEvent& ev) override
  87. {
  88. if (ev.button != 1)
  89. return false;
  90. if (! buttonBounds.contains(ev.pos))
  91. {
  92. if (buttonClick)
  93. {
  94. buttonClick = false;
  95. repaint();
  96. return true;
  97. }
  98. return false;
  99. }
  100. const bool newButtonClick = ev.press;
  101. if (newButtonClick != buttonClick)
  102. {
  103. buttonClick = newButtonClick;
  104. repaint();
  105. if (newButtonClick)
  106. {
  107. selectedFile = "(in progress)";
  108. repaint();
  109. FileBrowserOptions opts;
  110. // opts.saving = true;
  111. opts.title = "Look at me";
  112. if (! openFileBrowser(opts))
  113. {
  114. selectedFile = "(Failed to start file browser)";
  115. repaint();
  116. }
  117. }
  118. return true;
  119. }
  120. return newButtonClick;
  121. }
  122. void onResize(const ResizeEvent& ev) override
  123. {
  124. const uint width = ev.size.getWidth();
  125. const uint height = ev.size.getHeight();
  126. const double scaleFactor = getScaleFactor();
  127. buttonBounds = Rectangle<uint>(width - 120 * scaleFactor,
  128. height/2 - 20 * scaleFactor,
  129. 100 * scaleFactor,
  130. 40 * scaleFactor);
  131. }
  132. void onFocus(const bool focus, CrossingMode) override
  133. {
  134. if (focus)
  135. return;
  136. buttonClick = false;
  137. buttonHover = false;
  138. repaint();
  139. }
  140. void onFileSelected(const char* filename) override
  141. {
  142. if (filename == nullptr)
  143. filename = "Cancelled";
  144. if (selectedFile == filename)
  145. return;
  146. selectedFile = filename;
  147. repaint();
  148. }
  149. };
  150. // --------------------------------------------------------------------------------------------------------------------
  151. END_NAMESPACE_DGL
  152. int main()
  153. {
  154. using DGL_NAMESPACE::Application;
  155. using DGL_NAMESPACE::NanoFilePicker;
  156. Application app(true);
  157. NanoFilePicker win(app);
  158. win.show();
  159. app.exec();
  160. return 0;
  161. }
  162. // --------------------------------------------------------------------------------------------------------------------