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.

260 lines
8.9KB

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