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.

430 lines
15KB

  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 ComboBoxHandler : public ComponentTypeHandler
  19. {
  20. public:
  21. ComboBoxHandler()
  22. : ComponentTypeHandler ("Combo Box", "ComboBox", typeid (ComboBox), 150, 24)
  23. {}
  24. Component* createNewComponent (JucerDocument*)
  25. {
  26. return new ComboBox ("new combo box");
  27. }
  28. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  29. {
  30. ComboBox* const c = dynamic_cast <ComboBox*> (comp);
  31. jassert (c != nullptr);
  32. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  33. e->setAttribute ("editable", c->isTextEditable());
  34. e->setAttribute ("layout", c->getJustificationType().getFlags());
  35. e->setAttribute ("items", c->getProperties() ["items"].toString());
  36. e->setAttribute ("textWhenNonSelected", c->getTextWhenNothingSelected());
  37. e->setAttribute ("textWhenNoItems", c->getTextWhenNoChoicesAvailable());
  38. return e;
  39. }
  40. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  41. {
  42. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  43. return false;
  44. ComboBox defaultBox (String::empty);
  45. ComboBox* const c = dynamic_cast <ComboBox*> (comp);
  46. jassert (c != nullptr);
  47. c->setEditableText (xml.getBoolAttribute ("editable", defaultBox.isTextEditable()));
  48. c->setJustificationType (Justification (xml.getIntAttribute ("layout", defaultBox.getJustificationType().getFlags())));
  49. c->getProperties().set ("items", xml.getStringAttribute ("items", String::empty));
  50. c->setTextWhenNothingSelected (xml.getStringAttribute ("textWhenNonSelected", defaultBox.getTextWhenNothingSelected()));
  51. c->setTextWhenNoChoicesAvailable (xml.getStringAttribute ("textWhenNoItems", defaultBox.getTextWhenNoChoicesAvailable()));
  52. updateItems (c);
  53. return true;
  54. }
  55. void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
  56. {
  57. ComponentTypeHandler::getEditableProperties (component, document, properties);
  58. ComboBox* const c = dynamic_cast <ComboBox*> (component);
  59. jassert (c != nullptr);
  60. properties.add (new ComboItemsProperty (c, document));
  61. properties.add (new ComboEditableProperty (c, document));
  62. properties.add (new ComboJustificationProperty (c, document));
  63. properties.add (new ComboTextWhenNoneSelectedProperty (c, document));
  64. properties.add (new ComboTextWhenNoItemsProperty (c, document));
  65. }
  66. String getCreationParameters (Component* component)
  67. {
  68. return quotedString (component->getName());
  69. }
  70. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  71. {
  72. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  73. ComboBox* const c = dynamic_cast <ComboBox*> (component);
  74. jassert (c != nullptr);
  75. String s;
  76. s << memberVariableName << "->setEditableText (" << CodeHelpers::boolLiteral (c->isTextEditable()) << ");\n"
  77. << memberVariableName << "->setJustificationType (" << CodeHelpers::justificationToCode (c->getJustificationType()) << ");\n"
  78. << memberVariableName << "->setTextWhenNothingSelected (" << quotedString (c->getTextWhenNothingSelected()) << ");\n"
  79. << memberVariableName << "->setTextWhenNoChoicesAvailable (" << quotedString (c->getTextWhenNoChoicesAvailable()) << ");\n";
  80. StringArray lines;
  81. lines.addLines (c->getProperties() ["items"].toString());
  82. int itemId = 1;
  83. for (int i = 0; i < lines.size(); ++i)
  84. {
  85. if (lines[i].trim().isEmpty())
  86. s << memberVariableName << "->addSeparator();\n";
  87. else
  88. s << memberVariableName << "->addItem ("
  89. << quotedString (lines[i]) << ", " << itemId++ << ");\n";
  90. }
  91. if (needsCallback (component))
  92. s << memberVariableName << "->addListener (this);\n";
  93. s << '\n';
  94. code.constructorCode += s;
  95. }
  96. void fillInGeneratedCode (Component* component, GeneratedCode& code)
  97. {
  98. ComponentTypeHandler::fillInGeneratedCode (component, code);
  99. if (needsCallback (component))
  100. {
  101. String& callback = code.getCallbackCode ("public ComboBoxListener",
  102. "void",
  103. "comboBoxChanged (ComboBox* comboBoxThatHasChanged)",
  104. true);
  105. if (callback.trim().isNotEmpty())
  106. callback << "else ";
  107. const String memberVariableName (code.document->getComponentLayout()->getComponentMemberVariableName (component));
  108. const String userCodeComment ("UserComboBoxCode_" + memberVariableName);
  109. callback
  110. << "if (comboBoxThatHasChanged == " << memberVariableName
  111. << ")\n{\n //[" << userCodeComment << "] -- add your combo box handling code here..\n //[/" << userCodeComment << "]\n}\n";
  112. }
  113. }
  114. static void updateItems (ComboBox* c)
  115. {
  116. StringArray lines;
  117. lines.addLines (c->getProperties() ["items"].toString());
  118. c->clear();
  119. int itemId = 1;
  120. for (int i = 0; i < lines.size(); ++i)
  121. {
  122. if (lines[i].trim().isEmpty())
  123. c->addSeparator();
  124. else
  125. c->addItem (lines[i], itemId++);
  126. }
  127. }
  128. static bool needsCallback (Component*)
  129. {
  130. return true; // xxx should be configurable
  131. }
  132. private:
  133. class ComboEditableProperty : public ComponentBooleanProperty <ComboBox>
  134. {
  135. public:
  136. ComboEditableProperty (ComboBox* comp, JucerDocument& doc)
  137. : ComponentBooleanProperty <ComboBox> ("editable", "Text is editable", "Text is editable", comp, doc)
  138. {
  139. }
  140. void setState (bool newState)
  141. {
  142. document.perform (new ComboEditableChangeAction (component, *document.getComponentLayout(), newState),
  143. "Change combo box editability");
  144. }
  145. bool getState() const
  146. {
  147. return component->isTextEditable();
  148. }
  149. private:
  150. class ComboEditableChangeAction : public ComponentUndoableAction <ComboBox>
  151. {
  152. public:
  153. ComboEditableChangeAction (ComboBox* const comp, ComponentLayout& layout, const bool newState_)
  154. : ComponentUndoableAction <ComboBox> (comp, layout),
  155. newState (newState_)
  156. {
  157. oldState = comp->isTextEditable();
  158. }
  159. bool perform()
  160. {
  161. showCorrectTab();
  162. getComponent()->setEditableText (newState);
  163. changed();
  164. return true;
  165. }
  166. bool undo()
  167. {
  168. showCorrectTab();
  169. getComponent()->setEditableText (oldState);
  170. changed();
  171. return true;
  172. }
  173. bool newState, oldState;
  174. };
  175. };
  176. //==============================================================================
  177. class ComboJustificationProperty : public JustificationProperty
  178. {
  179. public:
  180. ComboJustificationProperty (ComboBox* comp, JucerDocument& doc)
  181. : JustificationProperty ("text layout", false),
  182. component (comp),
  183. document (doc)
  184. {
  185. }
  186. void setJustification (const Justification& newJustification)
  187. {
  188. document.perform (new ComboJustifyChangeAction (component, *document.getComponentLayout(), newJustification),
  189. "Change combo box justification");
  190. }
  191. const Justification getJustification() const { return component->getJustificationType(); }
  192. private:
  193. ComboBox* const component;
  194. JucerDocument& document;
  195. class ComboJustifyChangeAction : public ComponentUndoableAction <ComboBox>
  196. {
  197. public:
  198. ComboJustifyChangeAction (ComboBox* const comp, ComponentLayout& layout, const Justification& newState_)
  199. : ComponentUndoableAction <ComboBox> (comp, layout),
  200. newState (newState_),
  201. oldState (comp->getJustificationType())
  202. {
  203. }
  204. bool perform()
  205. {
  206. showCorrectTab();
  207. getComponent()->setJustificationType (newState);
  208. changed();
  209. return true;
  210. }
  211. bool undo()
  212. {
  213. showCorrectTab();
  214. getComponent()->setJustificationType (oldState);
  215. changed();
  216. return true;
  217. }
  218. Justification newState, oldState;
  219. };
  220. };
  221. //==============================================================================
  222. class ComboItemsProperty : public ComponentTextProperty <ComboBox>
  223. {
  224. public:
  225. ComboItemsProperty (ComboBox* comp, JucerDocument& doc)
  226. : ComponentTextProperty <ComboBox> ("items", 10000, true, comp, doc)
  227. {}
  228. void setText (const String& newText)
  229. {
  230. document.perform (new ComboItemsChangeAction (component, *document.getComponentLayout(), newText),
  231. "Change combo box items");
  232. }
  233. String getText() const
  234. {
  235. return component->getProperties() ["items"];
  236. }
  237. private:
  238. class ComboItemsChangeAction : public ComponentUndoableAction <ComboBox>
  239. {
  240. public:
  241. ComboItemsChangeAction (ComboBox* const comp, ComponentLayout& layout, const String& newState_)
  242. : ComponentUndoableAction <ComboBox> (comp, layout),
  243. newState (newState_)
  244. {
  245. oldState = comp->getProperties() ["items"];
  246. }
  247. bool perform()
  248. {
  249. showCorrectTab();
  250. getComponent()->getProperties().set ("items", newState);
  251. ComboBoxHandler::updateItems (getComponent());
  252. changed();
  253. return true;
  254. }
  255. bool undo()
  256. {
  257. showCorrectTab();
  258. getComponent()->getProperties().set ("items", oldState);
  259. ComboBoxHandler::updateItems (getComponent());
  260. changed();
  261. return true;
  262. }
  263. String newState, oldState;
  264. };
  265. };
  266. //==============================================================================
  267. class ComboTextWhenNoneSelectedProperty : public ComponentTextProperty <ComboBox>
  268. {
  269. public:
  270. ComboTextWhenNoneSelectedProperty (ComboBox* comp, JucerDocument& doc)
  271. : ComponentTextProperty <ComboBox> ("text when none selected", 200, false, comp, doc)
  272. {}
  273. void setText (const String& newText)
  274. {
  275. document.perform (new ComboNonSelTextChangeAction (component, *document.getComponentLayout(), newText),
  276. "Change combo box text when nothing selected");
  277. }
  278. String getText() const
  279. {
  280. return component->getTextWhenNothingSelected();
  281. }
  282. private:
  283. class ComboNonSelTextChangeAction : public ComponentUndoableAction <ComboBox>
  284. {
  285. public:
  286. ComboNonSelTextChangeAction (ComboBox* const comp, ComponentLayout& layout, const String& newState_)
  287. : ComponentUndoableAction <ComboBox> (comp, layout),
  288. newState (newState_)
  289. {
  290. oldState = comp->getTextWhenNothingSelected();
  291. }
  292. bool perform()
  293. {
  294. showCorrectTab();
  295. getComponent()->setTextWhenNothingSelected (newState);
  296. changed();
  297. return true;
  298. }
  299. bool undo()
  300. {
  301. showCorrectTab();
  302. getComponent()->setTextWhenNothingSelected (oldState);
  303. changed();
  304. return true;
  305. }
  306. String newState, oldState;
  307. };
  308. };
  309. //==============================================================================
  310. class ComboTextWhenNoItemsProperty : public ComponentTextProperty <ComboBox>
  311. {
  312. public:
  313. ComboTextWhenNoItemsProperty (ComboBox* comp, JucerDocument& doc)
  314. : ComponentTextProperty <ComboBox> ("text when no items", 200, false, comp, doc)
  315. {}
  316. void setText (const String& newText)
  317. {
  318. document.perform (new ComboNoItemTextChangeAction (component, *document.getComponentLayout(), newText),
  319. "Change combo box 'no items' text");
  320. }
  321. String getText() const
  322. {
  323. return component->getTextWhenNoChoicesAvailable();
  324. }
  325. private:
  326. class ComboNoItemTextChangeAction : public ComponentUndoableAction <ComboBox>
  327. {
  328. public:
  329. ComboNoItemTextChangeAction (ComboBox* const comp, ComponentLayout& layout, const String& newState_)
  330. : ComponentUndoableAction <ComboBox> (comp, layout),
  331. newState (newState_)
  332. {
  333. oldState = comp->getTextWhenNoChoicesAvailable();
  334. }
  335. bool perform()
  336. {
  337. showCorrectTab();
  338. getComponent()->setTextWhenNoChoicesAvailable (newState);
  339. changed();
  340. return true;
  341. }
  342. bool undo()
  343. {
  344. showCorrectTab();
  345. getComponent()->setTextWhenNoChoicesAvailable (oldState);
  346. changed();
  347. return true;
  348. }
  349. String newState, oldState;
  350. };
  351. };
  352. };