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.

198 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  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, vs2017, linux_make, androidstudio, 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. //==============================================================================
  36. class CodeEditorDemo : public Component,
  37. private FilenameComponentListener
  38. {
  39. public:
  40. CodeEditorDemo()
  41. {
  42. setOpaque (true);
  43. // Create the editor..
  44. editor.reset (new CodeEditorComponent (codeDocument, &cppTokeniser));
  45. addAndMakeVisible (editor.get());
  46. editor->loadContent ("\n"
  47. "/* Code editor demo!\n"
  48. "\n"
  49. " To see a real-world example of the code editor\n"
  50. " in action, have a look at the Projucer!\n"
  51. "\n"
  52. "*/\n"
  53. "\n");
  54. // Create a file chooser control to load files into it..
  55. addAndMakeVisible (fileChooser);
  56. fileChooser.addListener (this);
  57. lookAndFeelChanged();
  58. setSize (500, 500);
  59. }
  60. ~CodeEditorDemo()
  61. {
  62. fileChooser.removeListener (this);
  63. }
  64. void paint (Graphics& g) override
  65. {
  66. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  67. Colours::lightgrey));
  68. }
  69. void resized() override
  70. {
  71. auto r = getLocalBounds().reduced (8);
  72. fileChooser.setBounds (r.removeFromTop (25));
  73. editor->setBounds (r.withTrimmedTop (8));
  74. }
  75. private:
  76. // this is the document that the editor component is showing
  77. CodeDocument codeDocument;
  78. // this is a tokeniser to apply the C++ syntax highlighting
  79. CPlusPlusCodeTokeniser cppTokeniser;
  80. // the editor component
  81. std::unique_ptr<CodeEditorComponent> editor;
  82. FilenameComponent fileChooser { "File", {}, true, false, false, "*.cpp;*.h;*.hpp;*.c;*.mm;*.m", {},
  83. "Choose a C++ file to open it in the editor" };
  84. //==============================================================================
  85. void filenameComponentChanged (FilenameComponent*) override
  86. {
  87. editor->loadContent (fileChooser.getCurrentFile().loadFileAsString());
  88. }
  89. void lookAndFeelChanged() override
  90. {
  91. if (auto* v4 = dynamic_cast<LookAndFeel_V4*> (&LookAndFeel::getDefaultLookAndFeel()))
  92. {
  93. auto useLight = v4->getCurrentColourScheme() == LookAndFeel_V4::getLightColourScheme();
  94. editor->setColourScheme (useLight ? getLightCodeEditorColourScheme()
  95. : getDarkCodeEditorColourScheme());
  96. }
  97. else
  98. {
  99. editor->setColourScheme (cppTokeniser.getDefaultColourScheme());
  100. }
  101. }
  102. CodeEditorComponent::ColourScheme getDarkCodeEditorColourScheme()
  103. {
  104. struct Type
  105. {
  106. const char* name;
  107. juce::uint32 colour;
  108. };
  109. const Type types[] =
  110. {
  111. { "Error", 0xffe60000 },
  112. { "Comment", 0xff72d20c },
  113. { "Keyword", 0xffee6f6f },
  114. { "Operator", 0xffc4eb19 },
  115. { "Identifier", 0xffcfcfcf },
  116. { "Integer", 0xff42c8c4 },
  117. { "Float", 0xff885500 },
  118. { "String", 0xffbc45dd },
  119. { "Bracket", 0xff058202 },
  120. { "Punctuation", 0xffcfbeff },
  121. { "Preprocessor Text", 0xfff8f631 }
  122. };
  123. CodeEditorComponent::ColourScheme cs;
  124. for (auto& t : types)
  125. cs.set (t.name, Colour (t.colour));
  126. return cs;
  127. }
  128. CodeEditorComponent::ColourScheme getLightCodeEditorColourScheme()
  129. {
  130. struct Type
  131. {
  132. const char* name;
  133. juce::uint32 colour;
  134. };
  135. const Type types[] =
  136. {
  137. { "Error", 0xffcc0000 },
  138. { "Comment", 0xff00aa00 },
  139. { "Keyword", 0xff0000cc },
  140. { "Operator", 0xff225500 },
  141. { "Identifier", 0xff000000 },
  142. { "Integer", 0xff880000 },
  143. { "Float", 0xff885500 },
  144. { "String", 0xff990099 },
  145. { "Bracket", 0xff000055 },
  146. { "Punctuation", 0xff004400 },
  147. { "Preprocessor Text", 0xff660000 }
  148. };
  149. CodeEditorComponent::ColourScheme cs;
  150. for (auto& t : types)
  151. cs.set (t.name, Colour (t.colour));
  152. return cs;
  153. }
  154. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CodeEditorDemo)
  155. };