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.

273 lines
9.6KB

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