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.

279 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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 (T("rootVisible"), t->isRootItemVisible());
  43. e->setAttribute (T("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 (T("rootVisible"), defaultTreeView.isRootItemVisible()));
  53. t->setDefaultOpenness (xml.getBoolAttribute (T("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. //==============================================================================
  89. juce_UseDebuggingNewOperator
  90. private:
  91. //==============================================================================
  92. class DemoTreeView : public TreeView
  93. {
  94. public:
  95. DemoTreeView()
  96. : TreeView (T("new treeview"))
  97. {
  98. setRootItem (new DemoTreeViewItem (T("Demo root node"), 4));
  99. }
  100. ~DemoTreeView()
  101. {
  102. TreeViewItem* root = getRootItem();
  103. setRootItem (0);
  104. delete root;
  105. }
  106. private:
  107. class DemoTreeViewItem : public TreeViewItem
  108. {
  109. public:
  110. DemoTreeViewItem (const String& name_, const int numItems)
  111. : name (name_)
  112. {
  113. for (int i = 0; i < numItems; ++i)
  114. addSubItem (new DemoTreeViewItem (T("Demo sub-node ") + String (i), numItems - 1));
  115. }
  116. ~DemoTreeViewItem()
  117. {
  118. }
  119. void paintItem (Graphics& g, int width, int height)
  120. {
  121. if (isSelected())
  122. g.fillAll (Colours::lightblue);
  123. g.setColour (Colours::black);
  124. g.setFont (height * 0.7f);
  125. g.drawText (name, 4, 0, width - 4, height, Justification::centredLeft, true);
  126. }
  127. bool mightContainSubItems()
  128. {
  129. return true;
  130. }
  131. const String name;
  132. };
  133. };
  134. //==============================================================================
  135. class TreeViewRootItemProperty : public ComponentBooleanProperty <TreeView>
  136. {
  137. public:
  138. TreeViewRootItemProperty (TreeView* comp, JucerDocument& document)
  139. : ComponentBooleanProperty <TreeView> (T("show root item"), T("Root item visible"), T("Root item visible"), comp, document)
  140. {
  141. }
  142. void setState (bool newState)
  143. {
  144. document.perform (new TreeviewRootChangeAction (component, *document.getComponentLayout(), newState),
  145. T("Change TreeView root item"));
  146. }
  147. bool getState() const
  148. {
  149. return component->isRootItemVisible();
  150. }
  151. private:
  152. class TreeviewRootChangeAction : public ComponentUndoableAction <TreeView>
  153. {
  154. public:
  155. TreeviewRootChangeAction (TreeView* const comp, ComponentLayout& layout, const bool newState_)
  156. : ComponentUndoableAction <TreeView> (comp, layout),
  157. newState (newState_)
  158. {
  159. oldState = comp->isRootItemVisible();
  160. }
  161. bool perform()
  162. {
  163. showCorrectTab();
  164. getComponent()->setRootItemVisible (newState);
  165. changed();
  166. return true;
  167. }
  168. bool undo()
  169. {
  170. showCorrectTab();
  171. getComponent()->setRootItemVisible (oldState);
  172. changed();
  173. return true;
  174. }
  175. bool newState, oldState;
  176. };
  177. };
  178. //==============================================================================
  179. class TreeViewRootOpennessProperty : public ComponentChoiceProperty <TreeView>
  180. {
  181. public:
  182. TreeViewRootOpennessProperty (TreeView* comp, JucerDocument& document)
  183. : ComponentChoiceProperty <TreeView> (T("default openness"), comp, document)
  184. {
  185. choices.add (T("Items open by default"));
  186. choices.add (T("Items closed by default"));
  187. }
  188. void setIndex (int newIndex)
  189. {
  190. document.perform (new TreeviewOpennessChangeAction (component, *document.getComponentLayout(), newIndex == 0),
  191. T("Change TreeView openness"));
  192. }
  193. int getIndex() const
  194. {
  195. return component->areItemsOpenByDefault() ? 0 : 1;
  196. }
  197. private:
  198. class TreeviewOpennessChangeAction : public ComponentUndoableAction <TreeView>
  199. {
  200. public:
  201. TreeviewOpennessChangeAction (TreeView* const comp, ComponentLayout& layout, const bool newState_)
  202. : ComponentUndoableAction <TreeView> (comp, layout),
  203. newState (newState_)
  204. {
  205. oldState = comp->areItemsOpenByDefault();
  206. }
  207. bool perform()
  208. {
  209. showCorrectTab();
  210. getComponent()->setDefaultOpenness (newState);
  211. changed();
  212. return true;
  213. }
  214. bool undo()
  215. {
  216. showCorrectTab();
  217. getComponent()->setDefaultOpenness (oldState);
  218. changed();
  219. return true;
  220. }
  221. bool newState, oldState;
  222. };
  223. };
  224. };
  225. #endif // __JUCER_TREEVIEWHANDLER_JUCEHEADER__