The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

155 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. // Note: for the Bluetooth Midi selector overlay, we need the class
  20. // UIViewComponent from the juce_gui_extra module. If this module is not
  21. // included in your app, BluetoothMidiDevicePairingDialogue::open() will fail
  22. // and return false.
  23. // It is also not available in the iPhone/iPad simulator.
  24. #if JUCE_MODULE_AVAILABLE_juce_gui_extra && ! TARGET_IPHONE_SIMULATOR
  25. #include <CoreAudioKit/CoreAudioKit.h>
  26. namespace juce
  27. {
  28. //==============================================================================
  29. class BluetoothMidiSelectorOverlay : public Component
  30. {
  31. public:
  32. BluetoothMidiSelectorOverlay (ModalComponentManager::Callback* exitCallbackToUse,
  33. const Rectangle<int>& boundsToUse)
  34. : bounds (boundsToUse)
  35. {
  36. ScopedPointer<ModalComponentManager::Callback> exitCallback (exitCallbackToUse);
  37. setAlwaysOnTop (true);
  38. setVisible (true);
  39. addToDesktop (ComponentPeer::windowHasDropShadow);
  40. if (bounds.isEmpty())
  41. setBounds (0, 0, getParentWidth(), getParentHeight());
  42. else
  43. setBounds (bounds);
  44. toFront (true);
  45. setOpaque (! bounds.isEmpty());
  46. controller = [[CABTMIDICentralViewController alloc] init];
  47. nativeSelectorComponent.setView ([controller view]);
  48. addAndMakeVisible (nativeSelectorComponent);
  49. enterModalState (true, exitCallback.release(), true);
  50. }
  51. ~BluetoothMidiSelectorOverlay()
  52. {
  53. nativeSelectorComponent.setView (nullptr);
  54. [controller release];
  55. }
  56. void paint (Graphics& g) override
  57. {
  58. g.fillAll (bounds.isEmpty() ? Colours::black.withAlpha (0.5f) : Colours::black);
  59. }
  60. void inputAttemptWhenModal() override { close(); }
  61. void mouseDrag (const MouseEvent&) override {}
  62. void mouseDown (const MouseEvent&) override { close(); }
  63. void resized() override { update(); }
  64. void parentSizeChanged() override { update(); }
  65. private:
  66. void update()
  67. {
  68. if (bounds.isEmpty())
  69. {
  70. const int pw = getParentWidth();
  71. const int ph = getParentHeight();
  72. nativeSelectorComponent.setBounds (Rectangle<int> (pw, ph)
  73. .withSizeKeepingCentre (jmin (400, pw),
  74. jmin (450, ph - 40)));
  75. }
  76. else
  77. {
  78. nativeSelectorComponent.setBounds (bounds.withZeroOrigin());
  79. }
  80. }
  81. void close()
  82. {
  83. exitModalState (0);
  84. setVisible (false);
  85. }
  86. CABTMIDICentralViewController* controller;
  87. UIViewComponent nativeSelectorComponent;
  88. Rectangle<int> bounds;
  89. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BluetoothMidiSelectorOverlay)
  90. };
  91. bool BluetoothMidiDevicePairingDialogue::open (ModalComponentManager::Callback* exitCallback,
  92. Rectangle<int>* btBounds)
  93. {
  94. ScopedPointer<ModalComponentManager::Callback> cb (exitCallback);
  95. auto boundsToUse = (btBounds != nullptr ? *btBounds : Rectangle<int> {});
  96. if (isAvailable())
  97. {
  98. new BluetoothMidiSelectorOverlay (cb.release(), boundsToUse);
  99. return true;
  100. }
  101. return false;
  102. }
  103. bool BluetoothMidiDevicePairingDialogue::isAvailable()
  104. {
  105. return NSClassFromString ([NSString stringWithUTF8String: "CABTMIDICentralViewController"]) != nil;
  106. }
  107. } // namespace juce
  108. //==============================================================================
  109. #else
  110. namespace juce
  111. {
  112. bool BluetoothMidiDevicePairingDialogue::open (ModalComponentManager::Callback* exitCallback,
  113. Rectangle<int>*)
  114. {
  115. ScopedPointer<ModalComponentManager::Callback> cb (exitCallback);
  116. return false;
  117. }
  118. bool BluetoothMidiDevicePairingDialogue::isAvailable() { return false; }
  119. }
  120. #endif