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.

240 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. //==============================================================================
  59. class FileModificationDetector
  60. {
  61. public:
  62. FileModificationDetector (const File& file_)
  63. : file (file_)
  64. {
  65. }
  66. const File& getFile() const { return file; }
  67. bool hasBeenModified() const
  68. {
  69. return fileModificationTime != file.getLastModificationTime()
  70. && (fileSize != file.getSize()
  71. || calculateFileHashCode (file) != fileHashCode);
  72. }
  73. void updateHash()
  74. {
  75. fileModificationTime = file.getLastModificationTime();
  76. fileSize = file.getSize();
  77. fileHashCode = calculateFileHashCode (file);
  78. }
  79. private:
  80. File file;
  81. Time fileModificationTime;
  82. int64 fileHashCode, fileSize;
  83. };
  84. //==============================================================================
  85. class PropertyPanelWithTooltips : public Component,
  86. public Timer
  87. {
  88. public:
  89. PropertyPanelWithTooltips();
  90. ~PropertyPanelWithTooltips();
  91. PropertyPanel* getPanel() const { return panel; }
  92. void paint (Graphics& g);
  93. void resized();
  94. void timerCallback();
  95. private:
  96. PropertyPanel* panel;
  97. TextLayout layout;
  98. Component* lastComp;
  99. String lastTip;
  100. const String findTip (Component* c);
  101. };
  102. //==============================================================================
  103. static const double tickSizes[] = { 1.0, 2.0, 5.0,
  104. 10.0, 20.0, 50.0,
  105. 100.0, 200.0, 500.0, 1000.0 };
  106. class TickIterator
  107. {
  108. public:
  109. TickIterator (const double startValue_, const double endValue_, const double valuePerPixel_,
  110. int minPixelsPerTick, int minWidthForLabels)
  111. : startValue (startValue_),
  112. endValue (endValue_),
  113. valuePerPixel (valuePerPixel_)
  114. {
  115. tickLevelIndex = findLevelIndexForValue (valuePerPixel * minPixelsPerTick);
  116. labelLevelIndex = findLevelIndexForValue (valuePerPixel * minWidthForLabels);
  117. tickPosition = pixelsToValue (-minWidthForLabels);
  118. tickPosition = snapValueDown (tickPosition, tickLevelIndex);
  119. }
  120. bool getNextTick (float& pixelX, float& tickLength, String& label)
  121. {
  122. const double tickUnits = tickSizes [tickLevelIndex];
  123. tickPosition += tickUnits;
  124. const int totalLevels = sizeof (tickSizes) / sizeof (*tickSizes);
  125. int highestIndex = tickLevelIndex;
  126. while (++highestIndex < totalLevels)
  127. {
  128. const double ticksAtThisLevel = tickPosition / tickSizes [highestIndex];
  129. if (fabs (ticksAtThisLevel - floor (ticksAtThisLevel + 0.5)) > 0.000001)
  130. break;
  131. }
  132. --highestIndex;
  133. if (highestIndex >= labelLevelIndex)
  134. label = getDescriptionOfValue (tickPosition, labelLevelIndex);
  135. else
  136. label = String::empty;
  137. tickLength = (highestIndex + 1 - tickLevelIndex) / (float) (totalLevels + 1 - tickLevelIndex);
  138. pixelX = valueToPixels (tickPosition);
  139. return tickPosition < endValue;
  140. }
  141. private:
  142. double tickPosition;
  143. int tickLevelIndex, labelLevelIndex;
  144. const double startValue, endValue, valuePerPixel;
  145. int findLevelIndexForValue (const double value) const
  146. {
  147. int i;
  148. for (i = 0; i < sizeof (tickSizes) / sizeof (*tickSizes); ++i)
  149. if (tickSizes [i] >= value)
  150. break;
  151. return i;
  152. }
  153. double pixelsToValue (int pixels) const
  154. {
  155. return startValue + pixels * valuePerPixel;
  156. }
  157. float valueToPixels (double value) const
  158. {
  159. return (float) ((value - startValue) / valuePerPixel);
  160. }
  161. static double snapValueToNearest (const double t, const int valueLevelIndex)
  162. {
  163. const double unitsPerInterval = tickSizes [valueLevelIndex];
  164. return unitsPerInterval * floor (t / unitsPerInterval + 0.5);
  165. }
  166. static double snapValueDown (const double t, const int valueLevelIndex)
  167. {
  168. const double unitsPerInterval = tickSizes [valueLevelIndex];
  169. return unitsPerInterval * floor (t / unitsPerInterval);
  170. }
  171. static inline int roundDoubleToInt (const double value)
  172. {
  173. union { int asInt[2]; double asDouble; } n;
  174. n.asDouble = value + 6755399441055744.0;
  175. #if TARGET_RT_BIG_ENDIAN
  176. return n.asInt [1];
  177. #else
  178. return n.asInt [0];
  179. #endif
  180. }
  181. static const String getDescriptionOfValue (const double value, const int valueLevelIndex)
  182. {
  183. return String (roundToInt (value));
  184. }
  185. };