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.

195 lines
5.3KB

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