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.

207 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: CodeEditorDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays a code editor.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2022, linux_make, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: CodeEditorDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. #if JUCE_ANDROID
  36. #error "This demo is not supported on Android!"
  37. #endif
  38. //==============================================================================
  39. class CodeEditorDemo final : public Component,
  40. private FilenameComponentListener
  41. {
  42. public:
  43. CodeEditorDemo()
  44. {
  45. setOpaque (true);
  46. // Create the editor..
  47. editor.reset (new CodeEditorComponent (codeDocument, &cppTokeniser));
  48. addAndMakeVisible (editor.get());
  49. editor->loadContent ("\n"
  50. "/* Code editor demo!\n"
  51. "\n"
  52. " To see a real-world example of the code editor\n"
  53. " in action, have a look at the Projucer!\n"
  54. "\n"
  55. "*/\n"
  56. "\n");
  57. // Create a file chooser control to load files into it..
  58. addAndMakeVisible (fileChooser);
  59. fileChooser.addListener (this);
  60. updateLookAndFeel();
  61. setSize (500, 500);
  62. }
  63. ~CodeEditorDemo() override
  64. {
  65. fileChooser.removeListener (this);
  66. }
  67. void paint (Graphics& g) override
  68. {
  69. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  70. Colours::lightgrey));
  71. }
  72. void resized() override
  73. {
  74. auto r = getLocalBounds().reduced (8);
  75. fileChooser.setBounds (r.removeFromTop (25));
  76. editor->setBounds (r.withTrimmedTop (8));
  77. }
  78. private:
  79. // this is the document that the editor component is showing
  80. CodeDocument codeDocument;
  81. // this is a tokeniser to apply the C++ syntax highlighting
  82. CPlusPlusCodeTokeniser cppTokeniser;
  83. // the editor component
  84. std::unique_ptr<CodeEditorComponent> editor;
  85. FilenameComponent fileChooser { "File", {}, true, false, false, "*.cpp;*.h;*.hpp;*.c;*.mm;*.m", {},
  86. "Choose a C++ file to open it in the editor" };
  87. //==============================================================================
  88. void filenameComponentChanged (FilenameComponent*) override
  89. {
  90. editor->loadContent (fileChooser.getCurrentFile().loadFileAsString());
  91. }
  92. void updateLookAndFeel()
  93. {
  94. if (auto* v4 = dynamic_cast<LookAndFeel_V4*> (&LookAndFeel::getDefaultLookAndFeel()))
  95. {
  96. auto useLight = v4->getCurrentColourScheme() == LookAndFeel_V4::getLightColourScheme();
  97. editor->setColourScheme (useLight ? getLightCodeEditorColourScheme()
  98. : getDarkCodeEditorColourScheme());
  99. }
  100. else
  101. {
  102. editor->setColourScheme (cppTokeniser.getDefaultColourScheme());
  103. }
  104. }
  105. void lookAndFeelChanged() override
  106. {
  107. updateLookAndFeel();
  108. }
  109. CodeEditorComponent::ColourScheme getDarkCodeEditorColourScheme()
  110. {
  111. struct Type
  112. {
  113. const char* name;
  114. juce::uint32 colour;
  115. };
  116. const Type types[] =
  117. {
  118. { "Error", 0xffe60000 },
  119. { "Comment", 0xff72d20c },
  120. { "Keyword", 0xffee6f6f },
  121. { "Operator", 0xffc4eb19 },
  122. { "Identifier", 0xffcfcfcf },
  123. { "Integer", 0xff42c8c4 },
  124. { "Float", 0xff885500 },
  125. { "String", 0xffbc45dd },
  126. { "Bracket", 0xff058202 },
  127. { "Punctuation", 0xffcfbeff },
  128. { "Preprocessor Text", 0xfff8f631 }
  129. };
  130. CodeEditorComponent::ColourScheme cs;
  131. for (auto& t : types)
  132. cs.set (t.name, Colour (t.colour));
  133. return cs;
  134. }
  135. CodeEditorComponent::ColourScheme getLightCodeEditorColourScheme()
  136. {
  137. struct Type
  138. {
  139. const char* name;
  140. juce::uint32 colour;
  141. };
  142. const Type types[] =
  143. {
  144. { "Error", 0xffcc0000 },
  145. { "Comment", 0xff00aa00 },
  146. { "Keyword", 0xff0000cc },
  147. { "Operator", 0xff225500 },
  148. { "Identifier", 0xff000000 },
  149. { "Integer", 0xff880000 },
  150. { "Float", 0xff885500 },
  151. { "String", 0xff990099 },
  152. { "Bracket", 0xff000055 },
  153. { "Punctuation", 0xff004400 },
  154. { "Preprocessor Text", 0xff660000 }
  155. };
  156. CodeEditorComponent::ColourScheme cs;
  157. for (auto& t : types)
  158. cs.set (t.name, Colour (t.colour));
  159. return cs;
  160. }
  161. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CodeEditorDemo)
  162. };