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.

185 lines
4.9KB

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