/* ============================================================================== This file is part of the JUCE library. Copyright (c) 2015 - ROLI Ltd. Permission is granted to use this software under the terms of either: a) the GPL v2 (or any later version) b) the Affero GPL v3 Details of these licenses can be found at: www.gnu.org/licenses JUCE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ------------------------------------------------------------------------------ To release a closed-source product which uses JUCE, commercial licenses are available: visit www.juce.com for more information. ============================================================================== */ #include "../JuceDemoHeader.h" // these classes are C++11-only #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS && JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS && JUCE_COMPILER_SUPPORTS_LAMBDAS struct DemoFlexPanel : public juce::Component, private juce::TextEditor::Listener, private juce::ComboBox::Listener { DemoFlexPanel (juce::Colour col, FlexItem& item) : flexItem (item), colour (col) { int x = 70; int y = 3; setupTextEditor (flexOrderEditor, { x, y, 20, 18 }, "0"); addLabel ("order", flexOrderEditor); y += 20; setupTextEditor (flexGrowEditor, { x, y, 20, 18 }, "0"); addLabel ("flex-grow", flexGrowEditor); y += 20; setupTextEditor (flexShrinkEditor, { x, y, 20, 18 }, "1"); addLabel ("flex-shrink", flexShrinkEditor); y += 20; setupTextEditor (flexBasisEditor, { x, y, 33, 18 }, "100"); addLabel ("flex-basis", flexBasisEditor); y += 20; alignSelfCombo.addItem ("auto", 1); alignSelfCombo.addItem ("flex-start", 2); alignSelfCombo.addItem ("flex-end", 3); alignSelfCombo.addItem ("center", 4); alignSelfCombo.addItem ("stretch", 5); alignSelfCombo.setBounds (x, y, 90, 18); alignSelfCombo.addListener (this); alignSelfCombo.setSelectedId (5); alignSelfCombo.setColour (ComboBox::outlineColourId, Colours::transparentBlack); addAndMakeVisible (alignSelfCombo); addLabel ("align-self", alignSelfCombo); } void setupTextEditor (TextEditor& te, Rectangle b, StringRef initialText) { te.setBounds (b); te.setText (initialText); te.addListener (this); addAndMakeVisible (te); } void addLabel (const String& name, Component& target) { auto label = new Label (name, name); label->attachToComponent (&target, true); labels.add (label); addAndMakeVisible (label); } void comboBoxChanged (ComboBox* cb) override { auto selectedID = cb->getSelectedId(); if (selectedID == 1) flexItem.alignSelf = FlexItem::AlignSelf::autoAlign; if (selectedID == 2) flexItem.alignSelf = FlexItem::AlignSelf::flexStart; if (selectedID == 3) flexItem.alignSelf = FlexItem::AlignSelf::flexEnd; if (selectedID == 4) flexItem.alignSelf = FlexItem::AlignSelf::center; if (selectedID == 5) flexItem.alignSelf = FlexItem::AlignSelf::stretch; if (auto parent = getParentComponent()) parent->resized(); } void textEditorTextChanged (TextEditor& textEditor) override { auto textIntValue = textEditor.getText().getFloatValue(); if (&textEditor == &flexOrderEditor) flexItem.order = (int) textIntValue; if (&textEditor == &flexGrowEditor) flexItem.flexGrow = textIntValue; if (&textEditor == &flexBasisEditor) flexItem.flexBasis = textIntValue; if (&textEditor == &flexShrinkEditor) flexItem.flexShrink = textIntValue; if (auto parent = getParentComponent()) parent->resized(); } void paint (Graphics& g) override { auto r = getLocalBounds(); g.setColour (colour); g.fillRect (r); g.setColour (Colours::black); g.drawFittedText ("w: " + String (r.getWidth()) + newLine + "h: " + String (r.getHeight()), r.reduced (4), Justification::bottomRight, 2); } FlexItem& flexItem; TextEditor flexOrderEditor, flexGrowEditor, flexShrinkEditor, flexBasisEditor; ComboBox alignSelfCombo; juce::Colour colour; OwnedArray