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.

194 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.title = "Look at me";
  108. if (! openFileBrowser(opts))
  109. {
  110. selectedFile = "(Failed to start file browser)";
  111. repaint();
  112. }
  113. }
  114. return true;
  115. }
  116. return newButtonClick;
  117. }
  118. void onResize(const ResizeEvent& ev) override
  119. {
  120. const uint width = ev.size.getWidth();
  121. const uint height = ev.size.getHeight();
  122. const double scaleFactor = getScaleFactor();
  123. buttonBounds = Rectangle<uint>(width - 120 * scaleFactor,
  124. height/2 - 20 * scaleFactor,
  125. 100 * scaleFactor,
  126. 40 * scaleFactor);
  127. }
  128. void onFocus(const bool focus, CrossingMode) override
  129. {
  130. if (focus)
  131. return;
  132. buttonClick = false;
  133. buttonHover = false;
  134. repaint();
  135. }
  136. void onFileSelected(const char* filename) override
  137. {
  138. if (filename == nullptr)
  139. filename = "Cancelled";
  140. if (selectedFile == filename)
  141. return;
  142. selectedFile = filename;
  143. repaint();
  144. }
  145. };
  146. // --------------------------------------------------------------------------------------------------------------------
  147. END_NAMESPACE_DGL
  148. int main()
  149. {
  150. USE_NAMESPACE_DGL;
  151. Application app(true);
  152. NanoFilePicker win(app);
  153. win.show();
  154. app.exec();
  155. return 0;
  156. }
  157. // --------------------------------------------------------------------------------------------------------------------