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.

276 lines
9.3KB

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