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.

267 lines
9.2KB

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