LMMS
Loading...
Searching...
No Matches
juce_Label.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
40
42{
43 textValue.removeListener (this);
44
45 if (ownerComponent != nullptr)
46 ownerComponent->removeComponentListener (this);
47
48 editor.reset();
49}
50
51//==============================================================================
52void Label::setText (const String& newText, NotificationType notification)
53{
54 hideEditor (true);
55
56 if (lastTextValue != newText)
57 {
58 lastTextValue = newText;
59 textValue = newText;
60 repaint();
61
63
64 if (ownerComponent != nullptr)
66
67 if (notification != dontSendNotification)
69 }
70}
71
72String Label::getText (bool returnActiveEditorContents) const
73{
74 return (returnActiveEditorContents && isBeingEdited())
75 ? editor->getText()
76 : textValue.toString();
77}
78
80{
81 if (lastTextValue != textValue.toString())
83}
84
85//==============================================================================
86void Label::setFont (const Font& newFont)
87{
88 if (font != newFont)
89 {
90 font = newFont;
91 repaint();
92 }
93}
94
96{
97 return font;
98}
99
100void Label::setEditable (bool editOnSingleClick,
101 bool editOnDoubleClick,
102 bool lossOfFocusDiscards)
103{
104 editSingleClick = editOnSingleClick;
105 editDoubleClick = editOnDoubleClick;
106 lossOfFocusDiscardsChanges = lossOfFocusDiscards;
107
108 const auto isKeybordFocusable = (editOnSingleClick || editOnDoubleClick);
109
110 setWantsKeyboardFocus (isKeybordFocusable);
113
115}
116
118{
119 if (justification != newJustification)
120 {
121 justification = newJustification;
122 repaint();
123 }
124}
125
127{
128 if (border != newBorder)
129 {
130 border = newBorder;
131 repaint();
132 }
133}
134
135//==============================================================================
137{
138 return ownerComponent.get();
139}
140
141void Label::attachToComponent (Component* owner, bool onLeft)
142{
143 jassert (owner != this); // Not a great idea to try to attach it to itself!
144
145 if (ownerComponent != nullptr)
146 ownerComponent->removeComponentListener (this);
147
148 ownerComponent = owner;
149 leftOfOwnerComp = onLeft;
150
151 if (ownerComponent != nullptr)
152 {
153 setVisible (ownerComponent->isVisible());
154 ownerComponent->addComponentListener (this);
157 }
158}
159
160void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
161{
162 auto& lf = getLookAndFeel();
163 auto f = lf.getLabelFont (*this);
164 auto borderSize = lf.getLabelBorderSize (*this);
165
166 if (leftOfOwnerComp)
167 {
168 auto width = jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f)
169 + borderSize.getLeftAndRight(),
170 component.getX());
171
172 setBounds (component.getX() - width, component.getY(), width, component.getHeight());
173 }
174 else
175 {
176 auto height = borderSize.getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f);
177
178 setBounds (component.getX(), component.getY() - height, component.getWidth(), height);
179 }
180}
181
183{
184 if (auto* parent = component.getParentComponent())
185 parent->addChildComponent (this);
186}
187
189{
190 setVisible (component.isVisible());
191}
192
193//==============================================================================
196
198{
199 Component::BailOutChecker checker (this);
200 listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorShown (this, *textEditor); });
201
202 if (checker.shouldBailOut())
203 return;
204
205 if (onEditorShow != nullptr)
206 onEditorShow();
207}
208
210{
211 Component::BailOutChecker checker (this);
212 listeners.callChecked (checker, [this, textEditor] (Label::Listener& l) { l.editorHidden (this, *textEditor); });
213
214 if (checker.shouldBailOut())
215 return;
216
217 if (onEditorHide != nullptr)
218 onEditorHide();
219}
220
222{
223 if (editor == nullptr)
224 {
226 editor->setSize (10, 10);
228 editor->setText (getText(), false);
229 editor->setKeyboardType (keyboardType);
230 editor->addListener (this);
231 editor->grabKeyboardFocus();
232
233 if (editor == nullptr) // may be deleted by a callback
234 return;
235
236 editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
237
238 resized();
239 repaint();
240
241 editorShown (editor.get());
242
243 enterModalState (false);
244 editor->grabKeyboardFocus();
245 }
246}
247
249{
250 auto newText = ed.getText();
251
252 if (textValue.toString() != newText)
253 {
254 lastTextValue = newText;
255 textValue = newText;
256 repaint();
257
259
260 if (ownerComponent != nullptr)
262
263 return true;
264 }
265
266 return false;
267}
268
269void Label::hideEditor (bool discardCurrentEditorContents)
270{
271 if (editor != nullptr)
272 {
273 WeakReference<Component> deletionChecker (this);
274 std::unique_ptr<TextEditor> outgoingEditor;
275 std::swap (outgoingEditor, editor);
276
277 editorAboutToBeHidden (outgoingEditor.get());
278
279 const bool changed = (! discardCurrentEditorContents)
280 && updateFromTextEditorContents (*outgoingEditor);
281 outgoingEditor.reset();
282
283 if (deletionChecker != nullptr)
284 repaint();
285
286 if (changed)
288
289 if (deletionChecker != nullptr)
290 exitModalState (0);
291
292 if (changed && deletionChecker != nullptr)
294 }
295}
296
298{
299 if (editor != nullptr)
300 {
303 else
305 }
306}
307
309{
310 return editor != nullptr;
311}
312
313static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
314{
315 if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
316 ed.setColour (targetColourID, l.findColour (colourID));
317}
318
331
336
337//==============================================================================
339{
340 getLookAndFeel().drawLabel (g, *this);
341}
342
344{
346 && isEnabled()
347 && contains (e.getPosition())
348 && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
349 {
350 showEditor();
351 }
352}
353
355{
357 && isEnabled()
358 && ! e.mods.isPopupMenu())
359 {
360 showEditor();
361 }
362}
363
365{
366 if (editor != nullptr)
367 editor->setBounds (getLocalBounds());
368}
369
371{
373 && isEnabled()
374 && cause == focusChangedByTabKey)
375 {
376 showEditor();
377 }
378}
379
381{
382 repaint();
383}
384
386{
387 repaint();
388}
389
390void Label::setMinimumHorizontalScale (const float newScale)
391{
392 if (minimumHorizontalScale != newScale)
393 {
394 minimumHorizontalScale = newScale;
395 repaint();
396 }
397}
398
399//==============================================================================
400// We'll use a custom focus traverser here to make sure focus goes from the
401// text editor to another component rather than back to the label itself.
403{
404public:
406
408 {
409 if (auto* container = getKeyboardFocusContainer (parent))
411
412 return nullptr;
413 }
414
417
418 std::vector<Component*> getAllComponents (Component* parent) override
419 {
420 if (auto* container = getKeyboardFocusContainer (parent))
422
423 return {};
424 }
425
426private:
427 Component* getComp (Component* current) const
428 {
429 if (auto* ed = owner.getCurrentTextEditor())
430 if (current == ed)
431 return current->getParentComponent();
432
433 return current;
434 }
435
437 {
438 if (owner.getCurrentTextEditor() != nullptr && parent == &owner)
439 return owner.findKeyboardFocusContainer();
440
441 return parent;
442 }
443
445
447};
448
449std::unique_ptr<ComponentTraverser> Label::createKeyboardFocusTraverser()
450{
451 return std::make_unique<LabelKeyboardFocusTraverser> (*this);
452}
453
454//==============================================================================
457
459{
460 Component::BailOutChecker checker (this);
461 listeners.callChecked (checker, [this] (Listener& l) { l.labelTextChanged (this); });
462
463 if (checker.shouldBailOut())
464 return;
465
466 if (onTextChange != nullptr)
467 onTextChange();
468}
469
470//==============================================================================
472{
473 if (editor != nullptr)
474 {
475 jassert (&ed == editor.get());
476
478 {
481 else
483 }
484 }
485}
486
488{
489 if (editor != nullptr)
490 {
491 jassert (&ed == editor.get());
492
493 WeakReference<Component> deletionChecker (this);
494 bool changed = updateFromTextEditorContents (ed);
495 hideEditor (true);
496
497 if (changed && deletionChecker != nullptr)
498 {
500
501 if (deletionChecker != nullptr)
503 }
504 }
505}
506
508{
509 if (editor != nullptr)
510 {
511 jassertquiet (&ed == editor.get());
512
513 editor->setText (textValue.toString(), false);
514 hideEditor (true);
515 }
516}
517
522
523//==============================================================================
525{
526public:
527 explicit LabelAccessibilityHandler (Label& labelToWrap)
528 : AccessibilityHandler (labelToWrap,
529 labelToWrap.isEditable() ? AccessibilityRole::editableText : AccessibilityRole::label,
530 getAccessibilityActions (labelToWrap),
531 { std::make_unique<LabelValueInterface> (labelToWrap) }),
532 label (labelToWrap)
533 {
534 }
535
536 String getTitle() const override { return label.getText(); }
537 String getHelp() const override { return label.getTooltip(); }
538
540 {
541 if (label.isBeingEdited())
542 return {}; // allow focus to pass through to the TextEditor
543
545 }
546
547private:
549 {
550 public:
551 explicit LabelValueInterface (Label& labelToWrap)
552 : label (labelToWrap)
553 {
554 }
555
556 bool isReadOnly() const override { return true; }
557 String getCurrentValueAsString() const override { return label.getText(); }
558 void setValueAsString (const String&) override {}
559
560 private:
562
563 //==============================================================================
565 };
566
568 {
569 if (label.isEditable())
571
572 return {};
573 }
574
576
577 //==============================================================================
579};
580
581std::unique_ptr<AccessibilityHandler> Label::createAccessibilityHandler()
582{
583 return std::make_unique<LabelAccessibilityHandler> (*this);
584}
585
586} // namespace juce
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_AccessibilityActions.h:73
AccessibilityActions & addAction(AccessibilityActionType type, std::function< void()> actionCallback)
Definition juce_AccessibilityActions.h:88
virtual AccessibleState getCurrentState() const
Definition juce_AccessibilityHandler.cpp:75
AccessibilityHandler(Component &componentToWrap, AccessibilityRole accessibilityRole, AccessibilityActions actions={}, Interfaces interfaces={})
Definition juce_AccessibilityHandler.cpp:55
Definition juce_AccessibilityValueInterface.h:140
Definition juce_AccessibilityState.h:39
Definition juce_BorderSize.h:42
Definition juce_Component.h:2331
bool shouldBailOut() const noexcept
Definition juce_Component.cpp:3252
Definition juce_Component.h:36
bool contains(Point< int > localPoint)
Definition juce_Component.cpp:1434
void copyAllExplicitColoursTo(Component &target) const
Definition juce_Component.cpp:2248
bool isVisible() const noexcept
Definition juce_Component.h:122
Component * getParentComponent() const noexcept
Definition juce_Component.h:804
void setFocusContainerType(FocusContainerType containerType) noexcept
Definition juce_Component.cpp:2862
int getHeight() const noexcept
Definition juce_Component.h:274
int getX() const noexcept
Definition juce_Component.h:259
void addAndMakeVisible(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1554
FocusChangeType
Definition juce_Component.h:1890
@ focusChangedByTabKey
Definition juce_Component.h:1892
bool hasKeyboardFocus(bool trueIfChildIsFocused) const
Definition juce_Component.cpp:3086
bool isCurrentlyBlockedByAnotherModalComponent() const
Definition juce_Component.cpp:1832
void repaint()
Definition juce_Component.cpp:1917
Component() noexcept
Definition juce_Component.cpp:517
@ none
Definition juce_Component.h:1283
@ keyboardFocusContainer
Definition juce_Component.h:1307
void exitModalState(int returnValue)
Definition juce_Component.cpp:1795
int getY() const noexcept
Definition juce_Component.h:268
void setBounds(int x, int y, int width, int height)
Definition juce_Component.cpp:1147
void setColour(int colourID, Colour newColour)
Definition juce_Component.cpp:2242
void enterModalState(bool takeKeyboardFocus=true, ModalComponentManager::Callback *callback=nullptr, bool deleteWhenDismissed=false)
Definition juce_Component.cpp:1764
void setWantsKeyboardFocus(bool wantsFocus) noexcept
Definition juce_Component.cpp:2842
int getWidth() const noexcept
Definition juce_Component.h:271
bool isEnabled() const noexcept
Definition juce_Component.cpp:3104
LookAndFeel & getLookAndFeel() const noexcept
Definition juce_Component.cpp:2173
Rectangle< int > getLocalBounds() const noexcept
Definition juce_Component.cpp:2283
virtual void setVisible(bool shouldBeVisible)
Definition juce_Component.cpp:575
void invalidateAccessibilityHandler()
Definition juce_Component.cpp:3297
String getName() const noexcept
Definition juce_Component.h:76
Definition juce_Font.h:42
Definition juce_GraphicsContext.h:45
Definition juce_Justification.h:41
Definition juce_KeyboardFocusTraverser.h:46
Component * getPreviousComponent(Component *current) override
Definition juce_KeyboardFocusTraverser.cpp:59
std::vector< Component * > getAllComponents(Component *parentComponent) override
Definition juce_KeyboardFocusTraverser.cpp:74
Component * getNextComponent(Component *current) override
Definition juce_KeyboardFocusTraverser.cpp:53
Component * getDefaultComponent(Component *parentComponent) override
Definition juce_KeyboardFocusTraverser.cpp:65
Definition juce_Label.h:183
Label & label
Definition juce_Label.cpp:561
LabelValueInterface(Label &labelToWrap)
Definition juce_Label.cpp:551
String getCurrentValueAsString() const override
Definition juce_Label.cpp:557
void setValueAsString(const String &) override
Definition juce_Label.cpp:558
bool isReadOnly() const override
Definition juce_Label.cpp:556
AccessibleState getCurrentState() const override
Definition juce_Label.cpp:539
Label & label
Definition juce_Label.cpp:575
static AccessibilityActions getAccessibilityActions(Label &label)
Definition juce_Label.cpp:567
String getTitle() const override
Definition juce_Label.cpp:536
String getHelp() const override
Definition juce_Label.cpp:537
LabelAccessibilityHandler(Label &labelToWrap)
Definition juce_Label.cpp:527
Definition juce_Label.h:41
~Label() override
Definition juce_Label.cpp:41
virtual void editorShown(TextEditor *)
Definition juce_Label.cpp:197
WeakReference< Component > ownerComponent
Definition juce_Label.h:350
virtual TextEditor * createEditorComponent()
Definition juce_Label.cpp:319
virtual void textWasEdited()
Definition juce_Label.cpp:194
bool editDoubleClick
Definition juce_Label.h:355
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Definition juce_Label.cpp:581
void setEditable(bool editOnSingleClick, bool editOnDoubleClick=false, bool lossOfFocusDiscardsChanges=false)
Definition juce_Label.cpp:100
ListenerList< Listener > listeners
Definition juce_Label.h:349
void attachToComponent(Component *owner, bool onLeft)
Definition juce_Label.cpp:141
Component * getAttachedComponent() const
Definition juce_Label.cpp:136
std::function< void()> onTextChange
Definition juce_Label.h:206
void callChangeListeners()
Definition juce_Label.cpp:458
void componentMovedOrResized(Component &, bool wasMoved, bool wasResized) override
Definition juce_Label.cpp:160
@ outlineWhenEditingColourId
Definition juce_Label.h:112
@ textWhenEditingColourId
Definition juce_Label.h:111
@ backgroundWhenEditingColourId
Definition juce_Label.h:110
virtual void editorAboutToBeHidden(TextEditor *)
Definition juce_Label.cpp:209
void resized() override
Definition juce_Label.cpp:364
void setFont(const Font &newFont)
Definition juce_Label.cpp:86
Font font
Definition juce_Label.h:346
void hideEditor(bool discardCurrentEditorContents)
Definition juce_Label.cpp:269
void valueChanged(Value &) override
Definition juce_Label.cpp:79
void textEditorEscapeKeyPressed(TextEditor &) override
Definition juce_Label.cpp:507
bool isBeingEdited() const noexcept
Definition juce_Label.cpp:308
String lastTextValue
Definition juce_Label.h:345
void showEditor()
Definition juce_Label.cpp:221
void componentParentHierarchyChanged(Component &) override
Definition juce_Label.cpp:182
std::function< void()> onEditorShow
Definition juce_Label.h:209
Label(const String &componentName=String(), const String &labelText=String())
Definition juce_Label.cpp:29
std::unique_ptr< TextEditor > editor
Definition juce_Label.h:348
Justification justification
Definition juce_Label.h:347
bool leftOfOwnerComp
Definition juce_Label.h:357
float minimumHorizontalScale
Definition juce_Label.h:352
bool editSingleClick
Definition juce_Label.h:354
Value textValue
Definition juce_Label.h:344
void mouseUp(const MouseEvent &) override
Definition juce_Label.cpp:343
void textEditorReturnKeyPressed(TextEditor &) override
Definition juce_Label.cpp:487
std::function< void()> onEditorHide
Definition juce_Label.h:212
String getText(bool returnActiveEditorContents=false) const
Definition juce_Label.cpp:72
std::unique_ptr< ComponentTraverser > createKeyboardFocusTraverser() override
Definition juce_Label.cpp:449
TextInputTarget::VirtualKeyboardType keyboardType
Definition juce_Label.h:353
bool updateFromTextEditorContents(TextEditor &)
Definition juce_Label.cpp:248
void addListener(Listener *listener)
Definition juce_Label.cpp:455
void textEditorTextChanged(TextEditor &) override
Definition juce_Label.cpp:471
bool lossOfFocusDiscardsChanges
Definition juce_Label.h:356
void textEditorFocusLost(TextEditor &) override
Definition juce_Label.cpp:518
void setMinimumHorizontalScale(float newScale)
Definition juce_Label.cpp:390
void removeListener(Listener *listener)
Definition juce_Label.cpp:456
Font getFont() const noexcept
Definition juce_Label.cpp:95
void mouseDoubleClick(const MouseEvent &) override
Definition juce_Label.cpp:354
void componentVisibilityChanged(Component &) override
Definition juce_Label.cpp:188
BorderSize< int > border
Definition juce_Label.h:351
void setJustificationType(Justification justification)
Definition juce_Label.cpp:117
virtual void textWasChanged()
Definition juce_Label.cpp:195
TextEditor * getCurrentTextEditor() const noexcept
Definition juce_Label.cpp:332
void setBorderSize(BorderSize< int > newBorderSize)
Definition juce_Label.cpp:126
void enablementChanged() override
Definition juce_Label.cpp:380
void colourChanged() override
Definition juce_Label.cpp:385
void focusGained(FocusChangeType) override
Definition juce_Label.cpp:370
void setText(const String &newText, NotificationType notification)
Definition juce_Label.cpp:52
void inputAttemptWhenModal() override
Definition juce_Label.cpp:297
void paint(Graphics &) override
Definition juce_Label.cpp:338
Component * getNextComponent(Component *c) override
Definition juce_Label.cpp:415
Label & owner
Definition juce_Label.cpp:444
Component * getPreviousComponent(Component *c) override
Definition juce_Label.cpp:416
Component * getDefaultComponent(Component *parent) override
Definition juce_Label.cpp:407
Component * getKeyboardFocusContainer(Component *parent) const
Definition juce_Label.cpp:436
LabelKeyboardFocusTraverser(Label &l)
Definition juce_Label.cpp:405
Component * getComp(Component *current) const
Definition juce_Label.cpp:427
std::vector< Component * > getAllComponents(Component *parent) override
Definition juce_Label.cpp:418
Definition juce_MouseEvent.h:39
Definition juce_Range.h:40
Definition juce_String.h:53
Definition juce_TextEditor.h:43
@ backgroundColourId
Definition juce_TextEditor.h:209
@ textColourId
Definition juce_TextEditor.h:212
@ outlineColourId
Definition juce_TextEditor.h:223
@ focusedOutlineColourId
Definition juce_TextEditor.h:226
String getText() const
Definition juce_TextEditor.cpp:2516
Definition juce_Value.h:51
Definition juce_WeakReference.h:78
* e
Definition inflate.c:1404
int * l
Definition inflate.c:1579
int g
Definition inflate.c:1573
unsigned f
Definition inflate.c:1572
static const char * name
Definition pugl.h:1582
static int int height
Definition pugl.h:1594
static int width
Definition pugl.h:1593
static uintptr_t parent
Definition pugl.h:1644
#define jassert(expression)
#define jassertquiet(expression)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
const Colour transparentBlack
Definition juce_Colours.h:40
const Colour black
Definition juce_Colours.h:50
Definition carla_juce.cpp:31
static void copyColourIfSpecified(Label &l, TextEditor &ed, int colourID, int targetColourID)
Definition juce_Label.cpp:313
NotificationType
Definition juce_NotificationType.h:32
@ sendNotification
Definition juce_NotificationType.h:34
@ dontSendNotification
Definition juce_NotificationType.h:33
@ press
Definition juce_AccessibilityActions.h:40
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
AccessibilityRole
Definition juce_AccessibilityRole.h:37
@ editableText
Definition juce_AccessibilityRole.h:46
return c
Definition crypt.c:175
#define const
Definition zconf.h:137