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.

98 lines
3.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. class CodeEditorDemo : public Component,
  20. private FilenameComponentListener
  21. {
  22. public:
  23. CodeEditorDemo()
  24. : fileChooser ("File", File(), true, false, false,
  25. "*.cpp;*.h;*.hpp;*.c;*.mm;*.m", String(),
  26. "Choose a C++ file to open it in the editor")
  27. {
  28. setOpaque (true);
  29. // Create the editor..
  30. addAndMakeVisible (editor = new CodeEditorComponent (codeDocument, &cppTokeniser));
  31. editor->loadContent ("\n"
  32. "/* Code editor demo!\n"
  33. "\n"
  34. " To see a real-world example of the code editor\n"
  35. " in action, have a look at the Projucer!\n"
  36. "\n"
  37. "*/\n"
  38. "\n");
  39. // Create a file chooser control to load files into it..
  40. addAndMakeVisible (fileChooser);
  41. fileChooser.addListener (this);
  42. }
  43. ~CodeEditorDemo()
  44. {
  45. fileChooser.removeListener (this);
  46. }
  47. void paint (Graphics& g) override
  48. {
  49. g.fillAll (Colours::lightgrey);
  50. }
  51. void resized() override
  52. {
  53. Rectangle<int> r (getLocalBounds().reduced (8));
  54. fileChooser.setBounds (r.removeFromTop (25));
  55. editor->setBounds (r.withTrimmedTop (8));
  56. }
  57. private:
  58. // this is the document that the editor component is showing
  59. CodeDocument codeDocument;
  60. // this is a tokeniser to apply the C++ syntax highlighting
  61. CPlusPlusCodeTokeniser cppTokeniser;
  62. // the editor component
  63. ScopedPointer<CodeEditorComponent> editor;
  64. FilenameComponent fileChooser;
  65. void filenameComponentChanged (FilenameComponent*) override
  66. {
  67. editor->loadContent (fileChooser.getCurrentFile().loadFileAsString());
  68. }
  69. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CodeEditorDemo)
  70. };
  71. // This static object will register this demo type in a global list of demos..
  72. static JuceDemoType<CodeEditorDemo> demo ("10 Components: Code Editor");