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.

277 lines
9.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. //==============================================================================
  19. int64 calculateStreamHashCode (InputStream& stream);
  20. int64 calculateFileHashCode (const File& file);
  21. bool areFilesIdentical (const File& file1, const File& file2);
  22. bool overwriteFileWithNewDataIfDifferent (const File& file, const char* data, int numBytes);
  23. bool overwriteFileWithNewDataIfDifferent (const File& file, const MemoryOutputStream& newData);
  24. bool overwriteFileWithNewDataIfDifferent (const File& file, const String& newData);
  25. bool containsAnyNonHiddenFiles (const File& folder);
  26. //==============================================================================
  27. // String::hashCode64 actually hit some dupes, so this is a more powerful version.
  28. const int64 hashCode64 (const String& s);
  29. const String randomHexString (Random& random, int numChars);
  30. const String hexString8Digits (int value);
  31. const String createAlphaNumericUID();
  32. const String createGUID (const String& seed); // Turns a seed into a windows GUID
  33. const String unixStylePath (const String& path);
  34. const String windowsStylePath (const String& path);
  35. bool shouldPathsBeRelative (String path1, String path2);
  36. //==============================================================================
  37. bool isJuceFolder (const File& folder);
  38. const File findParentJuceFolder (const File& file);
  39. const File findDefaultJuceFolder();
  40. //==============================================================================
  41. const String createIncludeStatement (const File& includeFile, const File& targetFile);
  42. const String makeHeaderGuardName (const File& file);
  43. const String replaceCEscapeChars (const String& s);
  44. const String makeValidCppIdentifier (String s,
  45. const bool capitalise,
  46. const bool removeColons,
  47. const bool allowTemplates);
  48. //==============================================================================
  49. const String boolToCode (const bool b);
  50. const String floatToCode (const float v);
  51. const String doubleToCode (const double v);
  52. const String colourToCode (const Colour& col);
  53. const String justificationToCode (const Justification& justification);
  54. const String castToFloat (const String& expression);
  55. //==============================================================================
  56. const String indentCode (const String& code, const int numSpaces);
  57. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex);
  58. void autoScrollForMouseEvent (const MouseEvent& e);
  59. void drawComponentPlaceholder (Graphics& g, int w, int h, const String& text);
  60. //==============================================================================
  61. const Font getFontFromState (const ValueTree& state,
  62. const var::identifier& fontName,
  63. const var::identifier& fontSize,
  64. const var::identifier& fontStyle);
  65. void createFontProperties (Array <PropertyComponent*>& props, const ValueTree& state,
  66. const var::identifier& fontName,
  67. const var::identifier& fontSize,
  68. const var::identifier& fontStyle,
  69. UndoManager* undoManager);
  70. PropertyComponent* createJustificationProperty (const String& name, const Value& value, bool onlyHorizontal);
  71. //==============================================================================
  72. class FileModificationDetector
  73. {
  74. public:
  75. FileModificationDetector (const File& file_)
  76. : file (file_)
  77. {
  78. }
  79. const File& getFile() const { return file; }
  80. void fileHasBeenRenamed (const File& newFile) { file = newFile; }
  81. bool hasBeenModified() const
  82. {
  83. return fileModificationTime != file.getLastModificationTime()
  84. && (fileSize != file.getSize()
  85. || calculateFileHashCode (file) != fileHashCode);
  86. }
  87. void updateHash()
  88. {
  89. fileModificationTime = file.getLastModificationTime();
  90. fileSize = file.getSize();
  91. fileHashCode = calculateFileHashCode (file);
  92. }
  93. private:
  94. File file;
  95. Time fileModificationTime;
  96. int64 fileHashCode, fileSize;
  97. };
  98. //==============================================================================
  99. class PropertyPanelWithTooltips : public Component,
  100. public Timer
  101. {
  102. public:
  103. PropertyPanelWithTooltips();
  104. ~PropertyPanelWithTooltips();
  105. PropertyPanel* getPanel() const { return panel; }
  106. void paint (Graphics& g);
  107. void resized();
  108. void timerCallback();
  109. private:
  110. PropertyPanel* panel;
  111. TextLayout layout;
  112. Component* lastComp;
  113. String lastTip;
  114. const String findTip (Component* c);
  115. };
  116. //==============================================================================
  117. class FloatingLabelComponent : public Component
  118. {
  119. public:
  120. FloatingLabelComponent();
  121. void remove();
  122. void update (Component* parent, const String& text, const Colour& textColour, int x, int y, bool toRight, bool below);
  123. void paint (Graphics& g);
  124. private:
  125. Font font;
  126. Colour colour;
  127. GlyphArrangement glyphs;
  128. };
  129. //==============================================================================
  130. static const double tickSizes[] = { 1.0, 2.0, 5.0,
  131. 10.0, 20.0, 50.0,
  132. 100.0, 200.0, 500.0, 1000.0 };
  133. class TickIterator
  134. {
  135. public:
  136. TickIterator (const double startValue_, const double endValue_, const double valuePerPixel_,
  137. int minPixelsPerTick, int minWidthForLabels)
  138. : startValue (startValue_),
  139. endValue (endValue_),
  140. valuePerPixel (valuePerPixel_)
  141. {
  142. tickLevelIndex = findLevelIndexForValue (valuePerPixel * minPixelsPerTick);
  143. labelLevelIndex = findLevelIndexForValue (valuePerPixel * minWidthForLabels);
  144. tickPosition = pixelsToValue (-minWidthForLabels);
  145. tickPosition = snapValueDown (tickPosition, tickLevelIndex);
  146. }
  147. bool getNextTick (float& pixelX, float& tickLength, String& label)
  148. {
  149. const double tickUnits = tickSizes [tickLevelIndex];
  150. tickPosition += tickUnits;
  151. const int totalLevels = sizeof (tickSizes) / sizeof (*tickSizes);
  152. int highestIndex = tickLevelIndex;
  153. while (++highestIndex < totalLevels)
  154. {
  155. const double ticksAtThisLevel = tickPosition / tickSizes [highestIndex];
  156. if (fabs (ticksAtThisLevel - floor (ticksAtThisLevel + 0.5)) > 0.000001)
  157. break;
  158. }
  159. --highestIndex;
  160. if (highestIndex >= labelLevelIndex)
  161. label = getDescriptionOfValue (tickPosition, labelLevelIndex);
  162. else
  163. label = String::empty;
  164. tickLength = (highestIndex + 1 - tickLevelIndex) / (float) (totalLevels + 1 - tickLevelIndex);
  165. pixelX = valueToPixels (tickPosition);
  166. return tickPosition < endValue;
  167. }
  168. private:
  169. double tickPosition;
  170. int tickLevelIndex, labelLevelIndex;
  171. const double startValue, endValue, valuePerPixel;
  172. int findLevelIndexForValue (const double value) const
  173. {
  174. int i;
  175. for (i = 0; i < (int) (sizeof (tickSizes) / sizeof (*tickSizes)); ++i)
  176. if (tickSizes [i] >= value)
  177. break;
  178. return i;
  179. }
  180. double pixelsToValue (int pixels) const
  181. {
  182. return startValue + pixels * valuePerPixel;
  183. }
  184. float valueToPixels (double value) const
  185. {
  186. return (float) ((value - startValue) / valuePerPixel);
  187. }
  188. static double snapValueToNearest (const double t, const int valueLevelIndex)
  189. {
  190. const double unitsPerInterval = tickSizes [valueLevelIndex];
  191. return unitsPerInterval * floor (t / unitsPerInterval + 0.5);
  192. }
  193. static double snapValueDown (const double t, const int valueLevelIndex)
  194. {
  195. const double unitsPerInterval = tickSizes [valueLevelIndex];
  196. return unitsPerInterval * floor (t / unitsPerInterval);
  197. }
  198. static inline int roundDoubleToInt (const double value)
  199. {
  200. union { int asInt[2]; double asDouble; } n;
  201. n.asDouble = value + 6755399441055744.0;
  202. #if TARGET_RT_BIG_ENDIAN
  203. return n.asInt [1];
  204. #else
  205. return n.asInt [0];
  206. #endif
  207. }
  208. static const String getDescriptionOfValue (const double value, const int valueLevelIndex)
  209. {
  210. return String (roundToInt (value));
  211. }
  212. TickIterator (const TickIterator&);
  213. TickIterator& operator= (const TickIterator&);
  214. };