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.

258 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. class TreeViewHandler : public ComponentTypeHandler
  19. {
  20. public:
  21. TreeViewHandler()
  22. : ComponentTypeHandler ("TreeView", "TreeView", typeid (DemoTreeView), 150, 150)
  23. {
  24. registerColour (TreeView::backgroundColourId, "background", "backgroundColour");
  25. registerColour (TreeView::linesColourId, "lines", "linecol");
  26. }
  27. Component* createNewComponent (JucerDocument*)
  28. {
  29. return new DemoTreeView();
  30. }
  31. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  32. {
  33. TreeView* const t = dynamic_cast <TreeView*> (comp);
  34. XmlElement* const e = ComponentTypeHandler::createXmlFor (comp, layout);
  35. e->setAttribute ("rootVisible", t->isRootItemVisible());
  36. e->setAttribute ("openByDefault", t->areItemsOpenByDefault());
  37. return e;
  38. }
  39. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  40. {
  41. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  42. return false;
  43. TreeView defaultTreeView;
  44. TreeView* const t = dynamic_cast <TreeView*> (comp);
  45. t->setRootItemVisible (xml.getBoolAttribute ("rootVisible", defaultTreeView.isRootItemVisible()));
  46. t->setDefaultOpenness (xml.getBoolAttribute ("openByDefault", defaultTreeView.areItemsOpenByDefault()));
  47. return true;
  48. }
  49. void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
  50. {
  51. ComponentTypeHandler::getEditableProperties (component, document, properties);
  52. TreeView* const t = dynamic_cast <TreeView*> (component);
  53. properties.add (new TreeViewRootItemProperty (t, document));
  54. properties.add (new TreeViewRootOpennessProperty (t, document));
  55. addColourProperties (t, document, properties);
  56. }
  57. String getCreationParameters (Component* comp)
  58. {
  59. return quotedString (comp->getName());
  60. }
  61. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  62. {
  63. TreeView defaultTreeView;
  64. TreeView* const t = dynamic_cast <TreeView*> (component);
  65. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  66. if (defaultTreeView.isRootItemVisible() != t->isRootItemVisible())
  67. {
  68. code.constructorCode
  69. << memberVariableName << "->setRootItemVisible ("
  70. << CodeHelpers::boolLiteral (t->isRootItemVisible()) << ");\n";
  71. }
  72. if (defaultTreeView.areItemsOpenByDefault() != t->areItemsOpenByDefault())
  73. {
  74. code.constructorCode
  75. << memberVariableName << "->setDefaultOpenness ("
  76. << CodeHelpers::boolLiteral (t->areItemsOpenByDefault()) << ");\n";
  77. }
  78. code.constructorCode << getColourIntialisationCode (component, memberVariableName);
  79. code.constructorCode << "\n";
  80. }
  81. private:
  82. //==============================================================================
  83. class DemoTreeView : public TreeView
  84. {
  85. public:
  86. DemoTreeView()
  87. : TreeView ("new treeview")
  88. {
  89. setRootItem (new DemoTreeViewItem ("Demo root node", 4));
  90. }
  91. ~DemoTreeView()
  92. {
  93. deleteRootItem();
  94. }
  95. private:
  96. class DemoTreeViewItem : public TreeViewItem
  97. {
  98. public:
  99. DemoTreeViewItem (const String& name_, const int numItems)
  100. : name (name_)
  101. {
  102. for (int i = 0; i < numItems; ++i)
  103. addSubItem (new DemoTreeViewItem ("Demo sub-node " + String (i), numItems - 1));
  104. }
  105. void paintItem (Graphics& g, int width, int height)
  106. {
  107. if (isSelected())
  108. g.fillAll (Colours::lightblue);
  109. g.setColour (Colours::black);
  110. g.setFont (height * 0.7f);
  111. g.drawText (name, 4, 0, width - 4, height, Justification::centredLeft, true);
  112. }
  113. bool mightContainSubItems()
  114. {
  115. return true;
  116. }
  117. const String name;
  118. };
  119. };
  120. //==============================================================================
  121. class TreeViewRootItemProperty : public ComponentBooleanProperty <TreeView>
  122. {
  123. public:
  124. TreeViewRootItemProperty (TreeView* comp, JucerDocument& document)
  125. : ComponentBooleanProperty <TreeView> ("show root item", "Root item visible", "Root item visible", comp, document)
  126. {
  127. }
  128. void setState (bool newState)
  129. {
  130. document.perform (new TreeviewRootChangeAction (component, *document.getComponentLayout(), newState),
  131. "Change TreeView root item");
  132. }
  133. bool getState() const
  134. {
  135. return component->isRootItemVisible();
  136. }
  137. private:
  138. class TreeviewRootChangeAction : public ComponentUndoableAction <TreeView>
  139. {
  140. public:
  141. TreeviewRootChangeAction (TreeView* const comp, ComponentLayout& layout, const bool newState_)
  142. : ComponentUndoableAction <TreeView> (comp, layout),
  143. newState (newState_)
  144. {
  145. oldState = comp->isRootItemVisible();
  146. }
  147. bool perform()
  148. {
  149. showCorrectTab();
  150. getComponent()->setRootItemVisible (newState);
  151. changed();
  152. return true;
  153. }
  154. bool undo()
  155. {
  156. showCorrectTab();
  157. getComponent()->setRootItemVisible (oldState);
  158. changed();
  159. return true;
  160. }
  161. bool newState, oldState;
  162. };
  163. };
  164. //==============================================================================
  165. class TreeViewRootOpennessProperty : public ComponentChoiceProperty <TreeView>
  166. {
  167. public:
  168. TreeViewRootOpennessProperty (TreeView* comp, JucerDocument& document)
  169. : ComponentChoiceProperty <TreeView> ("default openness", comp, document)
  170. {
  171. choices.add ("Items open by default");
  172. choices.add ("Items closed by default");
  173. }
  174. void setIndex (int newIndex)
  175. {
  176. document.perform (new TreeviewOpennessChangeAction (component, *document.getComponentLayout(), newIndex == 0),
  177. "Change TreeView openness");
  178. }
  179. int getIndex() const
  180. {
  181. return component->areItemsOpenByDefault() ? 0 : 1;
  182. }
  183. private:
  184. class TreeviewOpennessChangeAction : public ComponentUndoableAction <TreeView>
  185. {
  186. public:
  187. TreeviewOpennessChangeAction (TreeView* const comp, ComponentLayout& layout, const bool newState_)
  188. : ComponentUndoableAction <TreeView> (comp, layout),
  189. newState (newState_)
  190. {
  191. oldState = comp->areItemsOpenByDefault();
  192. }
  193. bool perform()
  194. {
  195. showCorrectTab();
  196. getComponent()->setDefaultOpenness (newState);
  197. changed();
  198. return true;
  199. }
  200. bool undo()
  201. {
  202. showCorrectTab();
  203. getComponent()->setDefaultOpenness (oldState);
  204. changed();
  205. return true;
  206. }
  207. bool newState, oldState;
  208. };
  209. };
  210. };