Audio plugin host https://kx.studio/carla
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.

701 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. static juce_wchar getDefaultPasswordChar() noexcept
  16. {
  17. #if JUCE_LINUX
  18. return 0x2022;
  19. #else
  20. return 0x25cf;
  21. #endif
  22. }
  23. //==============================================================================
  24. AlertWindow::AlertWindow (const String& title,
  25. const String& message,
  26. AlertIconType iconType,
  27. Component* comp)
  28. : TopLevelWindow (title, true),
  29. alertIconType (iconType),
  30. associatedComponent (comp)
  31. {
  32. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  33. if (message.isEmpty())
  34. text = " "; // to force an update if the message is empty
  35. setMessage (message);
  36. AlertWindow::lookAndFeelChanged();
  37. constrainer.setMinimumOnscreenAmounts (0x10000, 0x10000, 0x10000, 0x10000);
  38. }
  39. AlertWindow::~AlertWindow()
  40. {
  41. // Ensure that the focus does not jump to another TextEditor while we
  42. // remove children.
  43. for (auto* t : textBoxes)
  44. t->setWantsKeyboardFocus (false);
  45. // Give away focus before removing the editors, so that any TextEditor
  46. // with focus has a chance to dismiss native keyboard if shown.
  47. if (hasKeyboardFocus (true))
  48. Component::unfocusAllComponents();
  49. removeAllChildren();
  50. }
  51. void AlertWindow::userTriedToCloseWindow()
  52. {
  53. if (escapeKeyCancels || buttons.size() > 0)
  54. exitModalState (0);
  55. }
  56. //==============================================================================
  57. void AlertWindow::setMessage (const String& message)
  58. {
  59. auto newMessage = message.substring (0, 2048);
  60. if (text != newMessage)
  61. {
  62. text = newMessage;
  63. updateLayout (true);
  64. repaint();
  65. }
  66. }
  67. //==============================================================================
  68. void AlertWindow::exitAlert (Button* button)
  69. {
  70. if (auto* parent = button->getParentComponent())
  71. parent->exitModalState (button->getCommandID());
  72. }
  73. //==============================================================================
  74. void AlertWindow::addButton (const String& name,
  75. const int returnValue,
  76. const KeyPress& shortcutKey1,
  77. const KeyPress& shortcutKey2)
  78. {
  79. auto* b = new TextButton (name, {});
  80. buttons.add (b);
  81. b->setWantsKeyboardFocus (true);
  82. b->setMouseClickGrabsKeyboardFocus (false);
  83. b->setCommandToTrigger (nullptr, returnValue, false);
  84. b->addShortcut (shortcutKey1);
  85. b->addShortcut (shortcutKey2);
  86. b->onClick = [this, b] { exitAlert (b); };
  87. Array<TextButton*> buttonsArray (buttons.begin(), buttons.size());
  88. auto& lf = getLookAndFeel();
  89. auto buttonHeight = lf.getAlertWindowButtonHeight();
  90. auto buttonWidths = lf.getWidthsForTextButtons (*this, buttonsArray);
  91. jassert (buttonWidths.size() == buttons.size());
  92. int i = 0;
  93. for (auto* button : buttons)
  94. button->setSize (buttonWidths[i++], buttonHeight);
  95. addAndMakeVisible (b, 0);
  96. updateLayout (false);
  97. }
  98. int AlertWindow::getNumButtons() const
  99. {
  100. return buttons.size();
  101. }
  102. void AlertWindow::triggerButtonClick (const String& buttonName)
  103. {
  104. for (auto* b : buttons)
  105. {
  106. if (buttonName == b->getName())
  107. {
  108. b->triggerClick();
  109. break;
  110. }
  111. }
  112. }
  113. void AlertWindow::setEscapeKeyCancels (bool shouldEscapeKeyCancel)
  114. {
  115. escapeKeyCancels = shouldEscapeKeyCancel;
  116. }
  117. //==============================================================================
  118. void AlertWindow::addTextEditor (const String& name,
  119. const String& initialContents,
  120. const String& onScreenLabel,
  121. const bool isPasswordBox)
  122. {
  123. auto* ed = new TextEditor (name, isPasswordBox ? getDefaultPasswordChar() : 0);
  124. ed->setSelectAllWhenFocused (true);
  125. ed->setEscapeAndReturnKeysConsumed (false);
  126. textBoxes.add (ed);
  127. allComps.add (ed);
  128. ed->setColour (TextEditor::outlineColourId, findColour (ComboBox::outlineColourId));
  129. ed->setFont (getLookAndFeel().getAlertWindowMessageFont());
  130. addAndMakeVisible (ed);
  131. ed->setText (initialContents);
  132. ed->setCaretPosition (initialContents.length());
  133. textboxNames.add (onScreenLabel);
  134. updateLayout (false);
  135. }
  136. TextEditor* AlertWindow::getTextEditor (const String& nameOfTextEditor) const
  137. {
  138. for (auto* tb : textBoxes)
  139. if (tb->getName() == nameOfTextEditor)
  140. return tb;
  141. return nullptr;
  142. }
  143. String AlertWindow::getTextEditorContents (const String& nameOfTextEditor) const
  144. {
  145. if (auto* t = getTextEditor (nameOfTextEditor))
  146. return t->getText();
  147. return {};
  148. }
  149. //==============================================================================
  150. void AlertWindow::addComboBox (const String& name,
  151. const StringArray& items,
  152. const String& onScreenLabel)
  153. {
  154. auto* cb = new ComboBox (name);
  155. comboBoxes.add (cb);
  156. allComps.add (cb);
  157. cb->addItemList (items, 1);
  158. addAndMakeVisible (cb);
  159. cb->setSelectedItemIndex (0);
  160. comboBoxNames.add (onScreenLabel);
  161. updateLayout (false);
  162. }
  163. ComboBox* AlertWindow::getComboBoxComponent (const String& nameOfList) const
  164. {
  165. for (auto* cb : comboBoxes)
  166. if (cb->getName() == nameOfList)
  167. return cb;
  168. return nullptr;
  169. }
  170. //==============================================================================
  171. class AlertTextComp : public TextEditor
  172. {
  173. public:
  174. AlertTextComp (AlertWindow& owner, const String& message, const Font& font)
  175. {
  176. if (owner.isColourSpecified (AlertWindow::textColourId))
  177. setColour (TextEditor::textColourId, owner.findColour (AlertWindow::textColourId));
  178. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  179. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  180. setColour (TextEditor::shadowColourId, Colours::transparentBlack);
  181. setReadOnly (true);
  182. setMultiLine (true, true);
  183. setCaretVisible (false);
  184. setScrollbarsShown (true);
  185. lookAndFeelChanged();
  186. setWantsKeyboardFocus (false);
  187. setFont (font);
  188. setText (message, false);
  189. bestWidth = 2 * (int) std::sqrt (font.getHeight() * font.getStringWidth (message));
  190. }
  191. void updateLayout (const int width)
  192. {
  193. AttributedString s;
  194. s.setJustification (Justification::topLeft);
  195. s.append (getText(), getFont());
  196. TextLayout text;
  197. text.createLayoutWithBalancedLineLengths (s, width - 8.0f);
  198. setSize (width, jmin (width, (int) (text.getHeight() + getFont().getHeight())));
  199. }
  200. int bestWidth;
  201. JUCE_DECLARE_NON_COPYABLE (AlertTextComp)
  202. };
  203. void AlertWindow::addTextBlock (const String& textBlock)
  204. {
  205. auto* c = new AlertTextComp (*this, textBlock, getLookAndFeel().getAlertWindowMessageFont());
  206. textBlocks.add (c);
  207. allComps.add (c);
  208. addAndMakeVisible (c);
  209. updateLayout (false);
  210. }
  211. //==============================================================================
  212. void AlertWindow::addProgressBarComponent (double& progressValue)
  213. {
  214. auto* pb = new ProgressBar (progressValue);
  215. progressBars.add (pb);
  216. allComps.add (pb);
  217. addAndMakeVisible (pb);
  218. updateLayout (false);
  219. }
  220. //==============================================================================
  221. void AlertWindow::addCustomComponent (Component* const component)
  222. {
  223. customComps.add (component);
  224. allComps.add (component);
  225. addAndMakeVisible (component);
  226. updateLayout (false);
  227. }
  228. int AlertWindow::getNumCustomComponents() const { return customComps.size(); }
  229. Component* AlertWindow::getCustomComponent (int index) const { return customComps [index]; }
  230. Component* AlertWindow::removeCustomComponent (const int index)
  231. {
  232. auto* c = getCustomComponent (index);
  233. if (c != nullptr)
  234. {
  235. customComps.removeFirstMatchingValue (c);
  236. allComps.removeFirstMatchingValue (c);
  237. removeChildComponent (c);
  238. updateLayout (false);
  239. }
  240. return c;
  241. }
  242. //==============================================================================
  243. void AlertWindow::paint (Graphics& g)
  244. {
  245. auto& lf = getLookAndFeel();
  246. lf.drawAlertBox (g, *this, textArea, textLayout);
  247. g.setColour (findColour (textColourId));
  248. g.setFont (lf.getAlertWindowFont());
  249. for (int i = textBoxes.size(); --i >= 0;)
  250. {
  251. auto* te = textBoxes.getUnchecked(i);
  252. g.drawFittedText (textboxNames[i],
  253. te->getX(), te->getY() - 14,
  254. te->getWidth(), 14,
  255. Justification::centredLeft, 1);
  256. }
  257. for (int i = comboBoxNames.size(); --i >= 0;)
  258. {
  259. auto* cb = comboBoxes.getUnchecked(i);
  260. g.drawFittedText (comboBoxNames[i],
  261. cb->getX(), cb->getY() - 14,
  262. cb->getWidth(), 14,
  263. Justification::centredLeft, 1);
  264. }
  265. for (auto* c : customComps)
  266. g.drawFittedText (c->getName(),
  267. c->getX(), c->getY() - 14,
  268. c->getWidth(), 14,
  269. Justification::centredLeft, 1);
  270. }
  271. void AlertWindow::updateLayout (const bool onlyIncreaseSize)
  272. {
  273. const int titleH = 24;
  274. const int iconWidth = 80;
  275. auto& lf = getLookAndFeel();
  276. auto messageFont (lf.getAlertWindowMessageFont());
  277. auto wid = jmax (messageFont.getStringWidth (text),
  278. messageFont.getStringWidth (getName()));
  279. auto sw = (int) std::sqrt (messageFont.getHeight() * wid);
  280. auto w = jmin (300 + sw * 2, (int) (getParentWidth() * 0.7f));
  281. const int edgeGap = 10;
  282. const int labelHeight = 18;
  283. int iconSpace = 0;
  284. AttributedString attributedText;
  285. attributedText.append (getName(), lf.getAlertWindowTitleFont());
  286. if (text.isNotEmpty())
  287. attributedText.append ("\n\n" + text, messageFont);
  288. attributedText.setColour (findColour (textColourId));
  289. if (alertIconType == NoIcon)
  290. {
  291. attributedText.setJustification (Justification::centredTop);
  292. textLayout.createLayoutWithBalancedLineLengths (attributedText, (float) w);
  293. }
  294. else
  295. {
  296. attributedText.setJustification (Justification::topLeft);
  297. textLayout.createLayoutWithBalancedLineLengths (attributedText, (float) w);
  298. iconSpace = iconWidth;
  299. }
  300. w = jmax (350, (int) textLayout.getWidth() + iconSpace + edgeGap * 4);
  301. w = jmin (w, (int) (getParentWidth() * 0.7f));
  302. auto textLayoutH = (int) textLayout.getHeight();
  303. auto textBottom = 16 + titleH + textLayoutH;
  304. int h = textBottom;
  305. int buttonW = 40;
  306. for (auto* b : buttons)
  307. buttonW += 16 + b->getWidth();
  308. w = jmax (buttonW, w);
  309. h += (textBoxes.size() + comboBoxes.size() + progressBars.size()) * 50;
  310. if (auto* b = buttons[0])
  311. h += 20 + b->getHeight();
  312. for (auto* c : customComps)
  313. {
  314. w = jmax (w, (c->getWidth() * 100) / 80);
  315. h += 10 + c->getHeight();
  316. if (c->getName().isNotEmpty())
  317. h += labelHeight;
  318. }
  319. for (auto* tb : textBlocks)
  320. w = jmax (w, static_cast<const AlertTextComp*> (tb)->bestWidth);
  321. w = jmin (w, (int) (getParentWidth() * 0.7f));
  322. for (auto* tb : textBlocks)
  323. {
  324. auto* ac = static_cast<AlertTextComp*> (tb);
  325. ac->updateLayout ((int) (w * 0.8f));
  326. h += ac->getHeight() + 10;
  327. }
  328. h = jmin (getParentHeight() - 50, h);
  329. if (onlyIncreaseSize)
  330. {
  331. w = jmax (w, getWidth());
  332. h = jmax (h, getHeight());
  333. }
  334. if (! isVisible())
  335. centreAroundComponent (associatedComponent, w, h);
  336. else
  337. setBounds (getBounds().withSizeKeepingCentre (w, h));
  338. textArea.setBounds (edgeGap, edgeGap, w - (edgeGap * 2), h - edgeGap);
  339. const int spacer = 16;
  340. int totalWidth = -spacer;
  341. for (auto* b : buttons)
  342. totalWidth += b->getWidth() + spacer;
  343. auto x = (w - totalWidth) / 2;
  344. auto y = (int) (getHeight() * 0.95f);
  345. for (auto* c : buttons)
  346. {
  347. int ny = proportionOfHeight (0.95f) - c->getHeight();
  348. c->setTopLeftPosition (x, ny);
  349. if (ny < y)
  350. y = ny;
  351. x += c->getWidth() + spacer;
  352. c->toFront (false);
  353. }
  354. y = textBottom;
  355. for (auto* c : allComps)
  356. {
  357. h = 22;
  358. const int comboIndex = comboBoxes.indexOf (dynamic_cast<ComboBox*> (c));
  359. if (comboIndex >= 0 && comboBoxNames [comboIndex].isNotEmpty())
  360. y += labelHeight;
  361. const int tbIndex = textBoxes.indexOf (dynamic_cast<TextEditor*> (c));
  362. if (tbIndex >= 0 && textboxNames[tbIndex].isNotEmpty())
  363. y += labelHeight;
  364. if (customComps.contains (c))
  365. {
  366. if (c->getName().isNotEmpty())
  367. y += labelHeight;
  368. c->setTopLeftPosition (proportionOfWidth (0.1f), y);
  369. h = c->getHeight();
  370. }
  371. else if (textBlocks.contains (c))
  372. {
  373. c->setTopLeftPosition ((getWidth() - c->getWidth()) / 2, y);
  374. h = c->getHeight();
  375. }
  376. else
  377. {
  378. c->setBounds (proportionOfWidth (0.1f), y, proportionOfWidth (0.8f), h);
  379. }
  380. y += h + 10;
  381. }
  382. setWantsKeyboardFocus (getNumChildComponents() == 0);
  383. }
  384. bool AlertWindow::containsAnyExtraComponents() const
  385. {
  386. return allComps.size() > 0;
  387. }
  388. //==============================================================================
  389. void AlertWindow::mouseDown (const MouseEvent& e)
  390. {
  391. dragger.startDraggingComponent (this, e);
  392. }
  393. void AlertWindow::mouseDrag (const MouseEvent& e)
  394. {
  395. dragger.dragComponent (this, e, &constrainer);
  396. }
  397. bool AlertWindow::keyPressed (const KeyPress& key)
  398. {
  399. for (auto* b : buttons)
  400. {
  401. if (b->isRegisteredForShortcut (key))
  402. {
  403. b->triggerClick();
  404. return true;
  405. }
  406. }
  407. if (key.isKeyCode (KeyPress::escapeKey) && escapeKeyCancels)
  408. {
  409. exitModalState (0);
  410. return true;
  411. }
  412. if (key.isKeyCode (KeyPress::returnKey) && buttons.size() == 1)
  413. {
  414. buttons.getUnchecked(0)->triggerClick();
  415. return true;
  416. }
  417. return false;
  418. }
  419. void AlertWindow::lookAndFeelChanged()
  420. {
  421. const int newFlags = getLookAndFeel().getAlertBoxWindowFlags();
  422. setUsingNativeTitleBar ((newFlags & ComponentPeer::windowHasTitleBar) != 0);
  423. setDropShadowEnabled (isOpaque() && (newFlags & ComponentPeer::windowHasDropShadow) != 0);
  424. updateLayout (false);
  425. }
  426. int AlertWindow::getDesktopWindowStyleFlags() const
  427. {
  428. return getLookAndFeel().getAlertBoxWindowFlags();
  429. }
  430. //==============================================================================
  431. class AlertWindowInfo
  432. {
  433. public:
  434. AlertWindowInfo (const String& t, const String& m, Component* component,
  435. AlertWindow::AlertIconType icon, int numButts,
  436. ModalComponentManager::Callback* cb, bool runModally)
  437. : title (t), message (m), iconType (icon), numButtons (numButts),
  438. associatedComponent (component), callback (cb), modal (runModally)
  439. {
  440. }
  441. String title, message, button1, button2, button3;
  442. int invoke() const
  443. {
  444. MessageManager::getInstance()->callFunctionOnMessageThread (showCallback, (void*) this);
  445. return returnValue;
  446. }
  447. private:
  448. AlertWindow::AlertIconType iconType;
  449. int numButtons, returnValue = 0;
  450. WeakReference<Component> associatedComponent;
  451. ModalComponentManager::Callback* callback;
  452. bool modal;
  453. void show()
  454. {
  455. auto& lf = associatedComponent != nullptr ? associatedComponent->getLookAndFeel()
  456. : LookAndFeel::getDefaultLookAndFeel();
  457. std::unique_ptr<Component> alertBox (lf.createAlertWindow (title, message, button1, button2, button3,
  458. iconType, numButtons, associatedComponent));
  459. jassert (alertBox != nullptr); // you have to return one of these!
  460. alertBox->setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  461. #if JUCE_MODAL_LOOPS_PERMITTED
  462. if (modal)
  463. {
  464. returnValue = alertBox->runModalLoop();
  465. }
  466. else
  467. #endif
  468. {
  469. ignoreUnused (modal);
  470. alertBox->enterModalState (true, callback, true);
  471. alertBox.release();
  472. }
  473. }
  474. static void* showCallback (void* userData)
  475. {
  476. static_cast<AlertWindowInfo*> (userData)->show();
  477. return nullptr;
  478. }
  479. };
  480. #if JUCE_MODAL_LOOPS_PERMITTED
  481. void AlertWindow::showMessageBox (AlertIconType iconType,
  482. const String& title,
  483. const String& message,
  484. const String& buttonText,
  485. Component* associatedComponent)
  486. {
  487. if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows())
  488. {
  489. NativeMessageBox::showMessageBox (iconType, title, message, associatedComponent);
  490. }
  491. else
  492. {
  493. AlertWindowInfo info (title, message, associatedComponent, iconType, 1, nullptr, true);
  494. info.button1 = buttonText.isEmpty() ? TRANS("OK") : buttonText;
  495. info.invoke();
  496. }
  497. }
  498. #endif
  499. void AlertWindow::showMessageBoxAsync (AlertIconType iconType,
  500. const String& title,
  501. const String& message,
  502. const String& buttonText,
  503. Component* associatedComponent,
  504. ModalComponentManager::Callback* callback)
  505. {
  506. if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows())
  507. {
  508. NativeMessageBox::showMessageBoxAsync (iconType, title, message, associatedComponent, callback);
  509. }
  510. else
  511. {
  512. AlertWindowInfo info (title, message, associatedComponent, iconType, 1, callback, false);
  513. info.button1 = buttonText.isEmpty() ? TRANS("OK") : buttonText;
  514. info.invoke();
  515. }
  516. }
  517. bool AlertWindow::showOkCancelBox (AlertIconType iconType,
  518. const String& title,
  519. const String& message,
  520. const String& button1Text,
  521. const String& button2Text,
  522. Component* associatedComponent,
  523. ModalComponentManager::Callback* callback)
  524. {
  525. if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows())
  526. return NativeMessageBox::showOkCancelBox (iconType, title, message, associatedComponent, callback);
  527. AlertWindowInfo info (title, message, associatedComponent, iconType, 2, callback, callback == nullptr);
  528. info.button1 = button1Text.isEmpty() ? TRANS("OK") : button1Text;
  529. info.button2 = button2Text.isEmpty() ? TRANS("Cancel") : button2Text;
  530. return info.invoke() != 0;
  531. }
  532. int AlertWindow::showYesNoCancelBox (AlertIconType iconType,
  533. const String& title,
  534. const String& message,
  535. const String& button1Text,
  536. const String& button2Text,
  537. const String& button3Text,
  538. Component* associatedComponent,
  539. ModalComponentManager::Callback* callback)
  540. {
  541. if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows())
  542. return NativeMessageBox::showYesNoCancelBox (iconType, title, message, associatedComponent, callback);
  543. AlertWindowInfo info (title, message, associatedComponent, iconType, 3, callback, callback == nullptr);
  544. info.button1 = button1Text.isEmpty() ? TRANS("Yes") : button1Text;
  545. info.button2 = button2Text.isEmpty() ? TRANS("No") : button2Text;
  546. info.button3 = button3Text.isEmpty() ? TRANS("Cancel") : button3Text;
  547. return info.invoke();
  548. }
  549. #if JUCE_MODAL_LOOPS_PERMITTED
  550. bool AlertWindow::showNativeDialogBox (const String& title,
  551. const String& bodyText,
  552. bool isOkCancel)
  553. {
  554. if (isOkCancel)
  555. return NativeMessageBox::showOkCancelBox (AlertWindow::NoIcon, title, bodyText);
  556. NativeMessageBox::showMessageBox (AlertWindow::NoIcon, title, bodyText);
  557. return true;
  558. }
  559. #endif
  560. } // namespace juce