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.

442 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../jucer_Headers.h"
  19. //==============================================================================
  20. String createAlphaNumericUID()
  21. {
  22. String uid;
  23. const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  24. Random r;
  25. uid << chars [r.nextInt (52)]; // make sure the first character is always a letter
  26. for (int i = 5; --i >= 0;)
  27. {
  28. r.setSeedRandomly();
  29. uid << chars [r.nextInt (62)];
  30. }
  31. return uid;
  32. }
  33. String hexString8Digits (int value)
  34. {
  35. return String::toHexString (value).paddedLeft ('0', 8);
  36. }
  37. String createGUID (const String& seed)
  38. {
  39. const String hex (MD5 ((seed + "_guidsalt").toUTF8()).toHexString().toUpperCase());
  40. return "{" + hex.substring (0, 8)
  41. + "-" + hex.substring (8, 12)
  42. + "-" + hex.substring (12, 16)
  43. + "-" + hex.substring (16, 20)
  44. + "-" + hex.substring (20, 32)
  45. + "}";
  46. }
  47. String escapeSpaces (const String& s)
  48. {
  49. return s.replace (" ", "\\ ");
  50. }
  51. String addQuotesIfContainsSpaces (const String& text)
  52. {
  53. return (text.containsChar (' ') && ! text.isQuotedString()) ? text.quoted() : text;
  54. }
  55. void setValueIfVoid (Value value, const var& defaultValue)
  56. {
  57. if (value.getValue().isVoid())
  58. value = defaultValue;
  59. }
  60. //==============================================================================
  61. StringPairArray parsePreprocessorDefs (const String& text)
  62. {
  63. StringPairArray result;
  64. String::CharPointerType s (text.getCharPointer());
  65. while (! s.isEmpty())
  66. {
  67. String token, value;
  68. s = s.findEndOfWhitespace();
  69. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  70. token << s.getAndAdvance();
  71. s = s.findEndOfWhitespace();
  72. if (*s == '=')
  73. {
  74. ++s;
  75. s = s.findEndOfWhitespace();
  76. while ((! s.isEmpty()) && ! s.isWhitespace())
  77. {
  78. if (*s == ',')
  79. {
  80. ++s;
  81. break;
  82. }
  83. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  84. ++s;
  85. value << s.getAndAdvance();
  86. }
  87. }
  88. if (token.isNotEmpty())
  89. result.set (token, value);
  90. }
  91. return result;
  92. }
  93. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  94. {
  95. for (int i = 0; i < overridingDefs.size(); ++i)
  96. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  97. return inheritedDefs;
  98. }
  99. String createGCCPreprocessorFlags (const StringPairArray& defs)
  100. {
  101. String s;
  102. for (int i = 0; i < defs.size(); ++i)
  103. {
  104. String def (defs.getAllKeys()[i]);
  105. const String value (defs.getAllValues()[i]);
  106. if (value.isNotEmpty())
  107. def << "=" << value;
  108. if (! def.endsWithChar ('"'))
  109. def = def.quoted();
  110. s += " -D " + def;
  111. }
  112. return s;
  113. }
  114. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString)
  115. {
  116. for (int i = 0; i < definitions.size(); ++i)
  117. {
  118. const String key (definitions.getAllKeys()[i]);
  119. const String value (definitions.getAllValues()[i]);
  120. sourceString = sourceString.replace ("${" + key + "}", value);
  121. }
  122. return sourceString;
  123. }
  124. StringArray getSearchPathsFromString (const String& searchPath)
  125. {
  126. StringArray s;
  127. s.addTokens (searchPath, ";\r\n", String::empty);
  128. s.trim();
  129. s.removeEmptyStrings();
  130. s.removeDuplicates (false);
  131. return s;
  132. }
  133. //==============================================================================
  134. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  135. {
  136. if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
  137. {
  138. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  139. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  140. }
  141. }
  142. //==============================================================================
  143. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex)
  144. {
  145. startIndex = jmax (0, startIndex);
  146. while (startIndex < lines.size())
  147. {
  148. if (lines[startIndex].trimStart().startsWithIgnoreCase (text))
  149. return startIndex;
  150. ++startIndex;
  151. }
  152. return -1;
  153. }
  154. //==============================================================================
  155. RolloverHelpComp::RolloverHelpComp()
  156. : lastComp (nullptr)
  157. {
  158. setInterceptsMouseClicks (false, false);
  159. startTimer (150);
  160. }
  161. void RolloverHelpComp::paint (Graphics& g)
  162. {
  163. AttributedString s;
  164. s.setJustification (Justification::centredLeft);
  165. s.append (lastTip, Font (14.0f), findColour (mainBackgroundColourId).contrasting (0.7f));
  166. TextLayout tl;
  167. tl.createLayoutWithBalancedLineLengths (s, getWidth() - 10.0f);
  168. if (tl.getNumLines() > 3)
  169. tl.createLayout (s, getWidth() - 10.0f);
  170. tl.draw (g, getLocalBounds().toFloat());
  171. }
  172. void RolloverHelpComp::timerCallback()
  173. {
  174. Component* newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  175. if (newComp != nullptr
  176. && (newComp->getTopLevelComponent() != getTopLevelComponent()
  177. || newComp->isCurrentlyBlockedByAnotherModalComponent()))
  178. newComp = nullptr;
  179. if (newComp != lastComp)
  180. {
  181. lastComp = newComp;
  182. String newTip (findTip (newComp));
  183. if (newTip != lastTip)
  184. {
  185. lastTip = newTip;
  186. repaint();
  187. }
  188. }
  189. }
  190. String RolloverHelpComp::findTip (Component* c)
  191. {
  192. while (c != nullptr)
  193. {
  194. if (TooltipClient* const tc = dynamic_cast <TooltipClient*> (c))
  195. {
  196. const String tip (tc->getTooltip());
  197. if (tip.isNotEmpty())
  198. return tip;
  199. }
  200. c = c->getParentComponent();
  201. }
  202. return String::empty;
  203. }
  204. //==============================================================================
  205. FloatingLabelComponent::FloatingLabelComponent()
  206. : font (10.0f)
  207. {
  208. setInterceptsMouseClicks (false, false);
  209. }
  210. void FloatingLabelComponent::remove()
  211. {
  212. if (getParentComponent() != nullptr)
  213. getParentComponent()->removeChildComponent (this);
  214. }
  215. void FloatingLabelComponent::update (Component* parent, const String& text, const Colour& textColour,
  216. int x, int y, bool toRight, bool below)
  217. {
  218. colour = textColour;
  219. Rectangle<int> r;
  220. if (text != getName())
  221. {
  222. setName (text);
  223. glyphs.clear();
  224. glyphs.addJustifiedText (font, text, 0, 0, 200.0f, Justification::left);
  225. glyphs.justifyGlyphs (0, std::numeric_limits<int>::max(), 0, 0, 1000, 1000, Justification::topLeft);
  226. r = glyphs.getBoundingBox (0, std::numeric_limits<int>::max(), false)
  227. .getSmallestIntegerContainer().expanded (1, 1);
  228. }
  229. else
  230. {
  231. r = getLocalBounds();
  232. }
  233. r.setPosition (x + (toRight ? 3 : -(r.getWidth() + 3)), y + (below ? 2 : -(r.getHeight() + 2)));
  234. setBounds (r);
  235. parent->addAndMakeVisible (this);
  236. }
  237. void FloatingLabelComponent::paint (Graphics& g)
  238. {
  239. g.setFont (font);
  240. g.setColour (Colours::white.withAlpha (0.5f));
  241. g.fillRoundedRectangle (0, 0, (float) getWidth(), (float) getHeight(), 3);
  242. g.setColour (colour);
  243. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  244. }
  245. //==============================================================================
  246. class UTF8Component : public Component,
  247. private TextEditorListener
  248. {
  249. public:
  250. UTF8Component()
  251. : desc (String::empty,
  252. "Type any string into the box, and it'll be shown below as a portable UTF-8 literal, "
  253. "ready to cut-and-paste into your source-code...")
  254. {
  255. desc.setJustificationType (Justification::centred);
  256. desc.setColour (Label::textColourId, Colours::white);
  257. addAndMakeVisible (&desc);
  258. const Colour bkgd (Colours::white.withAlpha (0.6f));
  259. userText.setMultiLine (true, true);
  260. userText.setReturnKeyStartsNewLine (true);
  261. userText.setColour (TextEditor::backgroundColourId, bkgd);
  262. addAndMakeVisible (&userText);
  263. userText.addListener (this);
  264. resultText.setMultiLine (true, true);
  265. resultText.setColour (TextEditor::backgroundColourId, bkgd);
  266. resultText.setReadOnly (true);
  267. resultText.setSelectAllWhenFocused (true);
  268. addAndMakeVisible (&resultText);
  269. userText.setText (getLastText());
  270. }
  271. void textEditorTextChanged (TextEditor&)
  272. {
  273. update();
  274. }
  275. void textEditorEscapeKeyPressed (TextEditor&)
  276. {
  277. getTopLevelComponent()->exitModalState (0);
  278. }
  279. void update()
  280. {
  281. getLastText() = userText.getText();
  282. resultText.setText (CodeHelpers::stringLiteral (getLastText(), 100), false);
  283. }
  284. void resized()
  285. {
  286. desc.setBounds (8, 8, getWidth() - 16, 44);
  287. userText.setBounds (desc.getX(), desc.getBottom() + 8, getWidth() - 16, getHeight() / 2 - desc.getBottom() - 8);
  288. resultText.setBounds (desc.getX(), userText.getBottom() + 4, getWidth() - 16, getHeight() - userText.getBottom() - 12);
  289. }
  290. private:
  291. Label desc;
  292. TextEditor userText, resultText;
  293. String& getLastText()
  294. {
  295. static String t;
  296. return t;
  297. }
  298. };
  299. void showUTF8ToolWindow (ScopedPointer<Component>& ownerPointer)
  300. {
  301. if (ownerPointer != nullptr)
  302. {
  303. ownerPointer->toFront (true);
  304. }
  305. else
  306. {
  307. new FloatingToolWindow ("UTF-8 String Literal Converter",
  308. "utf8WindowPos",
  309. new UTF8Component(), ownerPointer,
  310. 400, 300,
  311. 300, 300, 1000, 1000);
  312. }
  313. }
  314. //==============================================================================
  315. bool cancelAnyModalComponents()
  316. {
  317. ModalComponentManager& mm = *ModalComponentManager::getInstance();
  318. const int numModal = mm.getNumModalComponents();
  319. for (int i = numModal; --i >= 0;)
  320. if (mm.getModalComponent(i) != nullptr)
  321. mm.getModalComponent(i)->exitModalState (0);
  322. return numModal > 0;
  323. }
  324. class AsyncCommandRetrier : public Timer
  325. {
  326. public:
  327. AsyncCommandRetrier (const ApplicationCommandTarget::InvocationInfo& inf)
  328. : info (inf)
  329. {
  330. info.originatingComponent = nullptr;
  331. startTimer (500);
  332. }
  333. void timerCallback()
  334. {
  335. stopTimer();
  336. commandManager->invoke (info, true);
  337. delete this;
  338. }
  339. ApplicationCommandTarget::InvocationInfo info;
  340. JUCE_DECLARE_NON_COPYABLE (AsyncCommandRetrier);
  341. };
  342. bool reinvokeCommandAfterCancellingModalComps (const ApplicationCommandTarget::InvocationInfo& info)
  343. {
  344. if (cancelAnyModalComponents())
  345. {
  346. new AsyncCommandRetrier (info);
  347. return true;
  348. }
  349. return false;
  350. }