|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the JUCE framework examples. |
| 5 | + Copyright (c) Raw Material Software Limited |
| 6 | +
|
| 7 | + The code included in this file is provided under the terms of the ISC license |
| 8 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 9 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 10 | + without fee is hereby granted provided that the above copyright notice and |
| 11 | + this permission notice appear in all copies. |
| 12 | +
|
| 13 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 14 | + REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 15 | + AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 16 | + INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 17 | + LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 18 | + OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 19 | + PERFORMANCE OF THIS SOFTWARE. |
| 20 | +
|
| 21 | + ============================================================================== |
| 22 | +*/ |
| 23 | + |
| 24 | +/******************************************************************************* |
| 25 | + The block below describes the properties of this PIP. A PIP is a short snippet |
| 26 | + of code that can be read by the Projucer and used to generate a JUCE project. |
| 27 | +
|
| 28 | + BEGIN_JUCE_PIP_METADATA |
| 29 | +
|
| 30 | + name: SystemClipboardDemo |
| 31 | + version: 1.0.0 |
| 32 | + vendor: JUCE |
| 33 | + website: http://juce.com |
| 34 | + description: Edit and display the contents of the system's clipboard. |
| 35 | +
|
| 36 | + dependencies: juce_core, juce_data_structures, juce_events, juce_graphics, |
| 37 | + juce_gui_basics |
| 38 | + exporters: xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone |
| 39 | +
|
| 40 | + moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1 |
| 41 | +
|
| 42 | + type: Component |
| 43 | + mainClass: SystemClipboardDemo |
| 44 | +
|
| 45 | + useLocalCopy: 1 |
| 46 | +
|
| 47 | + END_JUCE_PIP_METADATA |
| 48 | +
|
| 49 | +*******************************************************************************/ |
| 50 | + |
| 51 | +#pragma once |
| 52 | + |
| 53 | +#include "../Assets/DemoUtilities.h" |
| 54 | + |
| 55 | +//============================================================================== |
| 56 | +class SystemClipboardDemo final : public Component, |
| 57 | + private Timer |
| 58 | +{ |
| 59 | +public: |
| 60 | + SystemClipboardDemo() |
| 61 | + { |
| 62 | + typeDetails.setColour (Label::ColourIds::textColourId, |
| 63 | + getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText, |
| 64 | + Colours::white)); |
| 65 | + updateText(); |
| 66 | + addAndMakeVisible (typeDetails); |
| 67 | + |
| 68 | + setOpaque (true); |
| 69 | + setSize (750, 650); |
| 70 | + startTimer (2000); |
| 71 | + } |
| 72 | + |
| 73 | + void paint (Graphics& g) override |
| 74 | + { |
| 75 | + g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground, |
| 76 | + Colour::greyLevel (0.8f))); |
| 77 | + } |
| 78 | + |
| 79 | + void resized() override |
| 80 | + { |
| 81 | + const auto marginPx = 8; |
| 82 | + |
| 83 | + auto b = getLocalBounds().reduced (marginPx); |
| 84 | + |
| 85 | + typeDetails.setBounds (b.removeFromTop (64)); |
| 86 | + |
| 87 | + if (dataViewer != nullptr) |
| 88 | + { |
| 89 | + b.removeFromTop (marginPx); |
| 90 | + dataViewer->setBounds (b); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + void timerCallback() override |
| 95 | + { |
| 96 | + if (const auto type = SystemClipboard::getDataType(); lastKnownType != type) |
| 97 | + { |
| 98 | + lastKnownType = type; |
| 99 | + updateText(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +private: |
| 104 | + Label typeDetails; |
| 105 | + std::unique_ptr<Component> dataViewer; |
| 106 | + SystemClipboard::Type lastKnownType = SystemClipboard::Type::unknown; |
| 107 | + |
| 108 | + static String toString (SystemClipboard::Type type) |
| 109 | + { |
| 110 | + switch (type) |
| 111 | + { |
| 112 | + case SystemClipboard::Type::empty: return TRANS ("(empty)"); |
| 113 | + case SystemClipboard::Type::unknown: return TRANS ("(unknown)"); |
| 114 | + case SystemClipboard::Type::text: return TRANS ("Text"); |
| 115 | + case SystemClipboard::Type::image: return TRANS ("Image"); |
| 116 | + case SystemClipboard::Type::audioPCM: return TRANS ("Audio (PCM)"); |
| 117 | + |
| 118 | + default: |
| 119 | + break; |
| 120 | + }; |
| 121 | + |
| 122 | + jassertfalse; // New type? |
| 123 | + return {}; |
| 124 | + } |
| 125 | + |
| 126 | + void updateText() |
| 127 | + { |
| 128 | + const auto typeString = TRANS ("Type: {}") |
| 129 | + .replace ("{}", toString (lastKnownType)); |
| 130 | + typeDetails.setText (typeString, sendNotification); |
| 131 | + |
| 132 | + switch (lastKnownType) |
| 133 | + { |
| 134 | + case SystemClipboard::Type::text: |
| 135 | + { |
| 136 | + auto te = std::make_unique<TextEditor>(); |
| 137 | + te->setMultiLine (true); |
| 138 | + te->setText (SystemClipboard::getTextFromClipboard()); |
| 139 | + dataViewer = std::move (te); |
| 140 | + } |
| 141 | + break; |
| 142 | + |
| 143 | + case SystemClipboard::Type::image: |
| 144 | + { |
| 145 | + auto ic = std::make_unique<ImageComponent>(); |
| 146 | + ic->setImage (SystemClipboard::getImageFromClipboard(), |
| 147 | + RectanglePlacement::onlyReduceInSize); |
| 148 | + dataViewer = std::move (ic); |
| 149 | + } |
| 150 | + break; |
| 151 | + |
| 152 | + case SystemClipboard::Type::audioPCM: |
| 153 | + { |
| 154 | + auto te = std::make_unique<TextEditor>(); |
| 155 | + te->setText ("audio"); |
| 156 | + dataViewer = std::move (te); |
| 157 | + } |
| 158 | + break; |
| 159 | + |
| 160 | + default: |
| 161 | + dataViewer.reset(); |
| 162 | + break; |
| 163 | + }; |
| 164 | + |
| 165 | + if (dataViewer != nullptr) |
| 166 | + { |
| 167 | + addAndMakeVisible (dataViewer.get()); |
| 168 | + resized(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SystemClipboardDemo) |
| 173 | +}; |
0 commit comments