1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
End User License Agreement: www.juce.com/juce-6-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#pragma once
//==============================================================================
class ButtonHandler : public ComponentTypeHandler
{
public:
ButtonHandler (const String& typeDescription_,
const String& className_,
const std::type_info& componentClass,
const int defaultWidth_,
const int defaultHeight_)
: ComponentTypeHandler (typeDescription_, className_, componentClass,
defaultWidth_, defaultHeight_)
{}
void getEditableProperties (Component* component, JucerDocument& document,
Array<PropertyComponent*>& props, bool multipleSelected) override
{
ComponentTypeHandler::getEditableProperties (component, document, props, multipleSelected);
if (multipleSelected)
return;
if (auto* b = dynamic_cast<Button*> (component))
{
props.add (new ButtonTextProperty (b, document));
props.add (new ButtonCallbackProperty (b, document));
props.add (new ButtonRadioGroupProperty (b, document));
props.add (new ButtonConnectedEdgeProperty ("connected left", Button::ConnectedOnLeft, b, document));
props.add (new ButtonConnectedEdgeProperty ("connected right", Button::ConnectedOnRight, b, document));
props.add (new ButtonConnectedEdgeProperty ("connected top", Button::ConnectedOnTop, b, document));
props.add (new ButtonConnectedEdgeProperty ("connected bottom", Button::ConnectedOnBottom, b, document));
}
}
XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout) override
{
Button* const b = dynamic_cast<Button*> (comp);
XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
e->setAttribute ("buttonText", b->getButtonText());
e->setAttribute ("connectedEdges", b->getConnectedEdgeFlags());
e->setAttribute ("needsCallback", needsButtonListener (b));
e->setAttribute ("radioGroupId", b->getRadioGroupId());
return e;
}
bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout) override
{
Button* const b = dynamic_cast<Button*> (comp);
if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
return false;
b->setButtonText (xml.getStringAttribute ("buttonText", b->getButtonText()));
b->setConnectedEdges (xml.getIntAttribute ("connectedEdges", 0));
setNeedsButtonListener (b, xml.getBoolAttribute ("needsCallback", true));
b->setRadioGroupId (xml.getIntAttribute ("radioGroupId", 0));
return true;
}
String getCreationParameters (GeneratedCode&, Component* component) override
{
return quotedString (component->getName(), false);
}
void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName) override
{
ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
Button* const b = dynamic_cast<Button*> (component);
if (b->getButtonText() != b->getName())
{
code.constructorCode
<< memberVariableName << "->setButtonText ("
<< quotedString (b->getButtonText(), code.shouldUseTransMacro()) << ");\n";
}
if (b->getConnectedEdgeFlags() != 0)
{
StringArray flags;
if (b->isConnectedOnLeft())
flags.add ("juce::Button::ConnectedOnLeft");
if (b->isConnectedOnRight())
flags.add ("juce::Button::ConnectedOnRight");
if (b->isConnectedOnTop())
flags.add ("juce::Button::ConnectedOnTop");
if (b->isConnectedOnBottom())
flags.add ("juce::Button::ConnectedOnBottom");
String s;
s << memberVariableName << "->setConnectedEdges ("
<< flags.joinIntoString (" | ") << ");\n";
code.constructorCode += s;
}
if (b->getRadioGroupId() != 0)
code.constructorCode << memberVariableName << "->setRadioGroupId ("
<< b->getRadioGroupId() << ");\n";
if (needsButtonListener (component))
code.constructorCode << memberVariableName << "->addListener (this);\n";
}
void fillInGeneratedCode (Component* component, GeneratedCode& code) override
{
ComponentTypeHandler::fillInGeneratedCode (component, code);
if (needsButtonListener (component))
{
String& callback = code.getCallbackCode ("public juce::Button::Listener",
"void",
"buttonClicked (juce::Button* buttonThatWasClicked)",
true);
if (callback.isNotEmpty())
callback << "else ";
const String memberVariableName (code.document->getComponentLayout()->getComponentMemberVariableName (component));
const String userCodeComment ("UserButtonCode_" + memberVariableName);
callback
<< "if (buttonThatWasClicked == " << memberVariableName << ".get())\n"
<< "{\n //[" << userCodeComment << "] -- add your button handler code here..\n //[/" << userCodeComment << "]\n}\n";
}
}
static bool needsButtonListener (Component* button)
{
return button->getProperties().getWithDefault ("generateListenerCallback", true);
}
static void setNeedsButtonListener (Component* button, const bool shouldDoCallback)
{
button->getProperties().set ("generateListenerCallback", shouldDoCallback);
}
private:
//==============================================================================
class ButtonTextProperty : public ComponentTextProperty <Button>
{
public:
ButtonTextProperty (Button* button_, JucerDocument& doc)
: ComponentTextProperty <Button> ("text", 100, false, button_, doc)
{
}
void setText (const String& newText) override
{
document.perform (new ButtonTextChangeAction (component, *document.getComponentLayout(), newText),
"Change button text");
}
String getText() const override
{
return component->getButtonText();
}
private:
class ButtonTextChangeAction : public ComponentUndoableAction <Button>
{
public:
ButtonTextChangeAction (Button* const comp, ComponentLayout& l, const String& newName_)
: ComponentUndoableAction <Button> (comp, l),
newName (newName_)
{
oldName = comp->getButtonText();
}
bool perform()
{
showCorrectTab();
getComponent()->setButtonText (newName);
changed();
return true;
}
bool undo()
{
showCorrectTab();
getComponent()->setButtonText (oldName);
changed();
return true;
}
String newName, oldName;
};
};
class ButtonCallbackProperty : public ComponentBooleanProperty <Button>
{
public:
ButtonCallbackProperty (Button* b, JucerDocument& doc)
: ComponentBooleanProperty <Button> ("callback", "Generate ButtonListener", "Generate ButtonListener", b, doc)
{
}
void setState (bool newState)
{
document.perform (new ButtonCallbackChangeAction (component, *document.getComponentLayout(), newState),
"Change button callback");
}
bool getState() const { return needsButtonListener (component); }
private:
class ButtonCallbackChangeAction : public ComponentUndoableAction <Button>
{
public:
ButtonCallbackChangeAction (Button* const comp, ComponentLayout& l, const bool newState_)
: ComponentUndoableAction <Button> (comp, l),
newState (newState_)
{
oldState = needsButtonListener (comp);
}
bool perform()
{
showCorrectTab();
setNeedsButtonListener (getComponent(), newState);
changed();
return true;
}
bool undo()
{
showCorrectTab();
setNeedsButtonListener (getComponent(), oldState);
changed();
return true;
}
bool newState, oldState;
};
};
class ButtonRadioGroupProperty : public ComponentTextProperty <Button>
{
public:
ButtonRadioGroupProperty (Button* const button_, JucerDocument& doc)
: ComponentTextProperty <Button> ("radio group", 10, false, button_, doc)
{
}
void setText (const String& newText) override
{
document.perform (new ButtonRadioGroupChangeAction (component, *document.getComponentLayout(), newText.getIntValue()),
"Change radio group ID");
}
String getText() const override
{
return String (component->getRadioGroupId());
}
private:
class ButtonRadioGroupChangeAction : public ComponentUndoableAction <Button>
{
public:
ButtonRadioGroupChangeAction (Button* const comp, ComponentLayout& l, const int newId_)
: ComponentUndoableAction <Button> (comp, l),
newId (newId_)
{
oldId = comp->getRadioGroupId();
}
bool perform()
{
showCorrectTab();
getComponent()->setRadioGroupId (newId);
changed();
return true;
}
bool undo()
{
showCorrectTab();
getComponent()->setRadioGroupId (oldId);
changed();
return true;
}
int newId, oldId;
};
};
class ButtonConnectedEdgeProperty : public ComponentBooleanProperty <Button>
{
public:
ButtonConnectedEdgeProperty (const String& name, const int flag_,
Button* b, JucerDocument& doc)
: ComponentBooleanProperty <Button> (name, "Connected", "Connected", b, doc),
flag (flag_)
{
}
void setState (bool newState)
{
document.perform (new ButtonConnectedChangeAction (component, *document.getComponentLayout(), flag, newState),
"Change button connected edges");
}
bool getState() const
{
return (component->getConnectedEdgeFlags() & flag) != 0;
}
private:
const int flag;
class ButtonConnectedChangeAction : public ComponentUndoableAction <Button>
{
public:
ButtonConnectedChangeAction (Button* const comp, ComponentLayout& l, const int flag_, const bool newState_)
: ComponentUndoableAction <Button> (comp, l),
flag (flag_),
newState (newState_)
{
oldState = ((comp->getConnectedEdgeFlags() & flag) != 0);
}
bool perform()
{
showCorrectTab();
if (newState)
getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() | flag);
else
getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() & ~flag);
changed();
return true;
}
bool undo()
{
showCorrectTab();
if (oldState)
getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() | flag);
else
getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() & ~flag);
changed();
return true;
}
const int flag;
bool newState, oldState;
};
};
};
|