diff --git a/extras/juce demo/Source/demos/AudioDemoLatencyPage.cpp b/extras/juce demo/Source/demos/AudioDemoLatencyPage.cpp index a5b3af1229..678ec87224 100644 --- a/extras/juce demo/Source/demos/AudioDemoLatencyPage.cpp +++ b/extras/juce demo/Source/demos/AudioDemoLatencyPage.cpp @@ -324,7 +324,6 @@ AudioDemoLatencyPage::~AudioDemoLatencyPage() deviceManager.removeAudioCallback (liveAudioDisplayComp); deviceManager.removeAudioCallback (latencyTester); - delete latencyTester; //[/Destructor_pre] deleteAndZero (liveAudioDisplayComp); diff --git a/extras/juce demo/Source/demos/AudioDemoLatencyPage.h b/extras/juce demo/Source/demos/AudioDemoLatencyPage.h index b9d628b12d..f918bcd7cb 100644 --- a/extras/juce demo/Source/demos/AudioDemoLatencyPage.h +++ b/extras/juce demo/Source/demos/AudioDemoLatencyPage.h @@ -61,7 +61,7 @@ public: private: //[UserVariables] -- You can add your own custom variables in this section. AudioDeviceManager& deviceManager; - LatencyTester* latencyTester; + ScopedPointer latencyTester; //[/UserVariables] //============================================================================== diff --git a/extras/juce demo/Source/demos/AudioDemoPlaybackPage.cpp b/extras/juce demo/Source/demos/AudioDemoPlaybackPage.cpp index 158d2ac859..b23226e19b 100644 --- a/extras/juce demo/Source/demos/AudioDemoPlaybackPage.cpp +++ b/extras/juce demo/Source/demos/AudioDemoPlaybackPage.cpp @@ -185,7 +185,6 @@ AudioDemoPlaybackPage::AudioDemoPlaybackPage (AudioDeviceManager& deviceManager_ deviceManager.addAudioCallback (&audioSourcePlayer); audioSourcePlayer.setSource (&transportSource); - currentAudioFileSource = 0; //[/Constructor] } @@ -197,8 +196,6 @@ AudioDemoPlaybackPage::~AudioDemoPlaybackPage() deviceManager.removeAudioCallback (&audioSourcePlayer); fileTreeComp->removeListener (this); - - deleteAndZero (currentAudioFileSource); //[/Destructor_pre] deleteAndZero (zoomLabel); @@ -293,7 +290,7 @@ void AudioDemoPlaybackPage::loadFileIntoTransport (const File& audioFile) // unload the previous file source and delete it.. transportSource.stop(); transportSource.setSource (0); - deleteAndZero (currentAudioFileSource); + currentAudioFileSource = 0; // get a format manager and set it up with the basic types (wav and aiff). AudioFormatManager formatManager; diff --git a/extras/juce demo/Source/demos/AudioDemoPlaybackPage.h b/extras/juce demo/Source/demos/AudioDemoPlaybackPage.h index 35238d6ed1..ee4431a0b1 100644 --- a/extras/juce demo/Source/demos/AudioDemoPlaybackPage.h +++ b/extras/juce demo/Source/demos/AudioDemoPlaybackPage.h @@ -74,7 +74,7 @@ private: AudioSourcePlayer audioSourcePlayer; AudioTransportSource transportSource; - AudioFormatReaderSource* currentAudioFileSource; + ScopedPointer currentAudioFileSource; void loadFileIntoTransport (const File& audioFile); //[/UserVariables] diff --git a/extras/juce demo/Source/demos/AudioDemoSynthPage.cpp b/extras/juce demo/Source/demos/AudioDemoSynthPage.cpp index de3a413717..ef1e51a1d7 100644 --- a/extras/juce demo/Source/demos/AudioDemoSynthPage.cpp +++ b/extras/juce demo/Source/demos/AudioDemoSynthPage.cpp @@ -194,11 +194,10 @@ public: WavAudioFormat wavFormat; - AudioFormatReader* audioReader - = wavFormat.createReaderFor (new MemoryInputStream (BinaryData::cello_wav, - BinaryData::cello_wavSize, - false), - true); + ScopedPointer audioReader (wavFormat.createReaderFor (new MemoryInputStream (BinaryData::cello_wav, + BinaryData::cello_wavSize, + false), + true)); BigInteger allNotes; allNotes.setRange (0, 128, true); @@ -211,8 +210,6 @@ public: 0.1, // release time 10.0 // maximum sample length )); - - delete audioReader; } void prepareToPlay (int /*samplesPerBlockExpected*/, double sampleRate) @@ -296,8 +293,6 @@ AudioDemoSynthPage::~AudioDemoSynthPage() deviceManager.removeMidiInputCallback (String::empty, &(synthAudioSource->midiCollector)); deviceManager.removeAudioCallback (&audioSourcePlayer); deviceManager.removeAudioCallback (liveAudioDisplayComp); - - delete synthAudioSource; //[/Destructor_pre] deleteAndZero (keyboardComponent); diff --git a/extras/juce demo/Source/demos/AudioDemoSynthPage.h b/extras/juce demo/Source/demos/AudioDemoSynthPage.h index 13058b7281..6d7a8f6eb7 100644 --- a/extras/juce demo/Source/demos/AudioDemoSynthPage.h +++ b/extras/juce demo/Source/demos/AudioDemoSynthPage.h @@ -63,7 +63,7 @@ private: AudioDeviceManager& deviceManager; MidiKeyboardState keyboardState; AudioSourcePlayer audioSourcePlayer; - SynthAudioSource* synthAudioSource; + ScopedPointer synthAudioSource; //[/UserVariables] //============================================================================== diff --git a/extras/juce demo/Source/demos/CameraDemo.cpp b/extras/juce demo/Source/demos/CameraDemo.cpp index 827d367489..df873e84c2 100644 --- a/extras/juce demo/Source/demos/CameraDemo.cpp +++ b/extras/juce demo/Source/demos/CameraDemo.cpp @@ -37,33 +37,31 @@ class CameraDemo : public Component, public: //============================================================================== CameraDemo() + : cameraSelectorComboBox ("Camera"), + snapshotButton ("Take a snapshot"), + recordMovieButton ("Record a movie file (to your desktop)..."), + recordingMovie (false) { setName ("Camera"); - cameraDevice = 0; - cameraPreviewComp = 0; - recordingMovie = false; - - addAndMakeVisible (cameraSelectorComboBox = new ComboBox ("Camera")); + addAndMakeVisible (&cameraSelectorComboBox); createListOfCameras(); - cameraSelectorComboBox->setSelectedId (1); - cameraSelectorComboBox->addListener (this); + cameraSelectorComboBox.setSelectedId (1); + cameraSelectorComboBox.addListener (this); - addAndMakeVisible (snapshotButton = new TextButton ("Take a snapshot")); - snapshotButton->addButtonListener (this); - snapshotButton->setEnabled (false); + addAndMakeVisible (&snapshotButton); + snapshotButton.addButtonListener (this); + snapshotButton.setEnabled (false); - addAndMakeVisible (recordMovieButton = new TextButton ("Record a movie file (to your desktop)...")); - recordMovieButton->addButtonListener (this); - recordMovieButton->setEnabled (false); + addAndMakeVisible (&recordMovieButton); + recordMovieButton.addButtonListener (this); + recordMovieButton.setEnabled (false); - cameraSelectorComboBox->setSelectedId (2); + cameraSelectorComboBox.setSelectedId (2); } ~CameraDemo() { - deleteAllChildren(); - delete cameraDevice; } void paint (Graphics& g) @@ -76,11 +74,11 @@ public: void resized() { - cameraSelectorComboBox->setBounds (10, 4, 250, 24); - snapshotButton->changeWidthToFitText (24); - snapshotButton->setTopLeftPosition (cameraSelectorComboBox->getRight() + 20, 4); - recordMovieButton->changeWidthToFitText (24); - recordMovieButton->setTopLeftPosition (snapshotButton->getRight() + 20, 4); + cameraSelectorComboBox.setBounds (10, 4, 250, 24); + snapshotButton.changeWidthToFitText (24); + snapshotButton.setTopLeftPosition (cameraSelectorComboBox.getRight() + 20, 4); + recordMovieButton.changeWidthToFitText (24); + recordMovieButton.setTopLeftPosition (snapshotButton.getRight() + 20, 4); if (cameraPreviewComp != 0) cameraPreviewComp->setBounds (10, 40, getWidth() / 2 - 20, getHeight() - 50); @@ -89,42 +87,42 @@ public: void comboBoxChanged (ComboBox*) { // This is called when the user chooses a camera from the drop-down list. - deleteAndZero (cameraDevice); - deleteAndZero (cameraPreviewComp); + cameraDevice = 0; + cameraPreviewComp = 0; recordingMovie = false; - if (cameraSelectorComboBox->getSelectedId() > 1) + if (cameraSelectorComboBox.getSelectedId() > 1) { // Try to open the user's choice of camera.. - cameraDevice = CameraDevice::openDevice (cameraSelectorComboBox->getSelectedId() - 2); + cameraDevice = CameraDevice::openDevice (cameraSelectorComboBox.getSelectedId() - 2); // and if it worked, create a preview component for it.. if (cameraDevice != 0) addAndMakeVisible (cameraPreviewComp = cameraDevice->createViewerComponent()); } - snapshotButton->setEnabled (cameraDevice != 0); - recordMovieButton->setEnabled (cameraDevice != 0); + snapshotButton.setEnabled (cameraDevice != 0); + recordMovieButton.setEnabled (cameraDevice != 0); resized(); } void createListOfCameras() { - cameraSelectorComboBox->clear(); - cameraSelectorComboBox->addItem ("No camera", 1); - cameraSelectorComboBox->addSeparator(); + cameraSelectorComboBox.clear(); + cameraSelectorComboBox.addItem ("No camera", 1); + cameraSelectorComboBox.addSeparator(); StringArray cameras = CameraDevice::getAvailableDevices(); for (int i = 0; i < cameras.size(); ++i) - cameraSelectorComboBox->addItem (cameras[i], i + 2); + cameraSelectorComboBox.addItem (cameras[i], i + 2); } void buttonClicked (Button* b) { if (cameraDevice != 0) { - if (b == recordMovieButton) + if (b == &recordMovieButton) { // The user has clicked the record movie button.. if (! recordingMovie) @@ -137,14 +135,14 @@ public: CameraDevice::getFileExtension())); cameraDevice->startRecordingToFile (file); - recordMovieButton->setButtonText ("Stop Recording"); + recordMovieButton.setButtonText ("Stop Recording"); } else { // Already recording, so stop... recordingMovie = false; cameraDevice->stopRecording(); - recordMovieButton->setButtonText ("Start recording (to a file on your desktop)"); + recordMovieButton.setButtonText ("Start recording (to a file on your desktop)"); } } else @@ -173,15 +171,14 @@ public: private: //============================================================================== - CameraDevice* cameraDevice; + ScopedPointer cameraDevice; + ScopedPointer cameraPreviewComp; + Image lastSnapshot; - ComboBox* cameraSelectorComboBox; - TextButton* snapshotButton; - TextButton* recordMovieButton; - Component* cameraPreviewComp; + ComboBox cameraSelectorComboBox; + TextButton snapshotButton; + TextButton recordMovieButton; bool recordingMovie; - - Image lastSnapshot; }; diff --git a/extras/juce demo/Source/demos/CodeEditorDemo.cpp b/extras/juce demo/Source/demos/CodeEditorDemo.cpp index 70ba626b2f..555ab336ae 100644 --- a/extras/juce demo/Source/demos/CodeEditorDemo.cpp +++ b/extras/juce demo/Source/demos/CodeEditorDemo.cpp @@ -33,32 +33,29 @@ class CodeEditorDemo : public Component, public: //============================================================================== CodeEditorDemo() + : fileChooser ("File", File::nonexistent, true, false, false, + "*.cpp;*.h;*.hpp;*.c;*.mm;*.m", String::empty, + "Choose a C++ file to open it in the editor") { setName ("Code Editor"); setOpaque (true); // Create the editor.. addAndMakeVisible (editor = new CodeEditorComponent (codeDocument, &cppTokeniser)); + editor->loadContent ("\n\n/* Code editor demo! Please be gentle, this component is still an alpha version! */\n\n"); // Create a file chooser control to load files into it.. - addAndMakeVisible (fileChooser = new FilenameComponent ("File", File::nonexistent, true, false, false, - "*.cpp;*.h;*.hpp;*.c;*.mm;*.m", String::empty, - "Choose a C++ file to open it in the editor")); - fileChooser->addListener (this); - - - editor->loadContent ("\n\n/* Code editor demo! Please be gentle, this component is still an alpha version! */\n\n"); + addAndMakeVisible (&fileChooser); + fileChooser.addListener (this); } ~CodeEditorDemo() { - deleteAllChildren(); } void filenameComponentChanged (FilenameComponent*) { - File f (fileChooser->getCurrentFile()); - editor->loadContent (f.loadFileAsString()); + editor->loadContent (fileChooser.getCurrentFile().loadFileAsString()); } void paint (Graphics& g) @@ -69,7 +66,7 @@ public: void resized() { editor->setBounds (10, 45, getWidth() - 20, getHeight() - 55); - fileChooser->setBounds (10, 10, getWidth() - 20, 25); + fileChooser.setBounds (10, 10, getWidth() - 20, 25); } //============================================================================== @@ -83,9 +80,9 @@ private: CPlusPlusCodeTokeniser cppTokeniser; // the editor component - CodeEditorComponent* editor; + ScopedPointer editor; - FilenameComponent* fileChooser; + FilenameComponent fileChooser; }; diff --git a/extras/juce demo/Source/demos/DragAndDropDemo.cpp b/extras/juce demo/Source/demos/DragAndDropDemo.cpp index 062914a026..38aaf2afee 100644 --- a/extras/juce demo/Source/demos/DragAndDropDemo.cpp +++ b/extras/juce demo/Source/demos/DragAndDropDemo.cpp @@ -211,38 +211,34 @@ private: class DragAndDropDemo : public Component, public DragAndDropContainer { - //============================================================================== - DragAndDropDemoSource* source; - DragAndDropDemoTarget* target; - public: //============================================================================== DragAndDropDemo() { setName ("Drag-and-Drop"); - source = new DragAndDropDemoSource(); - addAndMakeVisible (source); - - target = new DragAndDropDemoTarget(); - addAndMakeVisible (target); + addAndMakeVisible (&source); + addAndMakeVisible (&target); } ~DragAndDropDemo() { - deleteAllChildren(); } void resized() { - source->setBounds (10, 10, 250, 150); - target->setBounds (getWidth() - 260, getHeight() - 160, 250, 150); + source.setBounds (10, 10, 250, 150); + target.setBounds (getWidth() - 260, getHeight() - 160, 250, 150); } //============================================================================== // (need to put this in to disambiguate the new/delete operators used in the // two base classes). juce_UseDebuggingNewOperator + +private: + DragAndDropDemoSource source; + DragAndDropDemoTarget target; }; diff --git a/extras/juce demo/Source/demos/FontsAndTextDemo.cpp b/extras/juce demo/Source/demos/FontsAndTextDemo.cpp index 63804494f9..430c910e3e 100644 --- a/extras/juce demo/Source/demos/FontsAndTextDemo.cpp +++ b/extras/juce demo/Source/demos/FontsAndTextDemo.cpp @@ -32,22 +32,14 @@ class FontsAndTextDemo : public Component, public ButtonListener, public SliderListener { - Array fonts; - ListBox* listBox; - TextEditor* textBox; - ToggleButton* boldButton; - ToggleButton* italicButton; - Slider* sizeSlider; - Slider* kerningSlider; - Slider* horizontalScaleSlider; - - StretchableLayoutManager verticalLayout; - - StretchableLayoutResizerBar* verticalDividerBar; - public: //============================================================================== FontsAndTextDemo() + : boldButton ("Bold"), + italicButton ("Italic"), + sizeLabel (String::empty, "Size"), + kerningLabel (String::empty, "Kerning"), + horizontalScaleLabel (String::empty, "Scale") { setName ("Fonts"); @@ -56,38 +48,38 @@ public: addAndMakeVisible (listBox = new ListBox ("fonts", this)); listBox->setRowHeight (28); - addAndMakeVisible (textBox = new TextEditor()); + addAndMakeVisible (&textBox); - textBox->setColour (TextEditor::backgroundColourId, Colours::white); - textBox->setColour (TextEditor::outlineColourId, Colours::black.withAlpha (0.5f)); + textBox.setColour (TextEditor::backgroundColourId, Colours::white); + textBox.setColour (TextEditor::outlineColourId, Colours::black.withAlpha (0.5f)); - textBox->setMultiLine (true, true); - textBox->setReturnKeyStartsNewLine (true); - textBox->setText ("The Quick Brown Fox Jumps Over The Lazy Dog\n\nAa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz 0123456789"); + textBox.setMultiLine (true, true); + textBox.setReturnKeyStartsNewLine (true); + textBox.setText ("The Quick Brown Fox Jumps Over The Lazy Dog\n\nAa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz 0123456789"); - addAndMakeVisible (boldButton = new ToggleButton ("bold")); - boldButton->addButtonListener (this); + addAndMakeVisible (&boldButton); + boldButton.addButtonListener (this); - addAndMakeVisible (italicButton = new ToggleButton ("italic")); - italicButton->addButtonListener (this); + addAndMakeVisible (&italicButton); + italicButton.addButtonListener (this); - addAndMakeVisible (sizeSlider = new Slider ("Size")); - sizeSlider->setRange (3.0, 150.0, 0.1); - sizeSlider->setValue (20.0); - sizeSlider->addListener (this); - (new Label (String::empty, sizeSlider->getName()))->attachToComponent (sizeSlider, true); + addAndMakeVisible (&sizeSlider); + sizeSlider.setRange (3.0, 150.0, 0.1); + sizeSlider.setValue (20.0); + sizeSlider.addListener (this); + sizeLabel.attachToComponent (&sizeSlider, true); - addAndMakeVisible (kerningSlider = new Slider ("Kerning")); - kerningSlider->setRange (-1.0, 1.0, 0.01); - kerningSlider->setValue (0.0); - kerningSlider->addListener (this); - (new Label (String::empty, kerningSlider->getName()))->attachToComponent (kerningSlider, true); + addAndMakeVisible (&kerningSlider); + kerningSlider.setRange (-1.0, 1.0, 0.01); + kerningSlider.setValue (0.0); + kerningSlider.addListener (this); + kerningLabel.attachToComponent (&kerningSlider, true); - addAndMakeVisible (horizontalScaleSlider = new Slider ("Stretch")); - horizontalScaleSlider->setRange (0.1, 4.0, 0.01); - horizontalScaleSlider->setValue (1.0); - horizontalScaleSlider->addListener (this); - (new Label (String::empty, horizontalScaleSlider->getName()))->attachToComponent (horizontalScaleSlider, true); + addAndMakeVisible (&horizontalScaleSlider); + horizontalScaleSlider.setRange (0.1, 4.0, 0.01); + horizontalScaleSlider.setValue (1.0); + horizontalScaleSlider.addListener (this); + horizontalScaleLabel.attachToComponent (&horizontalScaleSlider, true); listBox->setColour (ListBox::outlineColourId, Colours::black.withAlpha (0.5f)); listBox->setOutlineThickness (1); @@ -107,7 +99,6 @@ public: ~FontsAndTextDemo() { - deleteAllChildren(); } void resized() @@ -122,13 +113,13 @@ public: // now lay out the text box and the controls below it.. int x = verticalLayout.getItemCurrentPosition (2) + 4; - textBox->setBounds (x, 0, getWidth() - x, getHeight() - 110); + textBox.setBounds (x, 0, getWidth() - x, getHeight() - 110); x += 70; - sizeSlider->setBounds (x, getHeight() - 106, getWidth() - x, 22); - kerningSlider->setBounds (x, getHeight() - 82, getWidth() - x, 22); - horizontalScaleSlider->setBounds (x, getHeight() - 58, getWidth() - x, 22); - boldButton->setBounds (x, getHeight() - 34, (getWidth() - x) / 2, 22); - italicButton->setBounds (x + (getWidth() - x) / 2, getHeight() - 34, (getWidth() - x) / 2, 22); + sizeSlider.setBounds (x, getHeight() - 106, getWidth() - x, 22); + kerningSlider.setBounds (x, getHeight() - 82, getWidth() - x, 22); + horizontalScaleSlider.setBounds (x, getHeight() - 58, getWidth() - x, 22); + boldButton.setBounds (x, getHeight() - 34, (getWidth() - x) / 2, 22); + italicButton.setBounds (x + (getWidth() - x) / 2, getHeight() - 34, (getWidth() - x) / 2, 22); } // implements the ListBoxModel method @@ -167,13 +158,13 @@ public: { Font font (fonts [listBox->getSelectedRow()]); - font.setHeight ((float) sizeSlider->getValue()); - font.setBold (boldButton->getToggleState()); - font.setItalic (italicButton->getToggleState()); - font.setExtraKerningFactor ((float) kerningSlider->getValue()); - font.setHorizontalScale ((float) horizontalScaleSlider->getValue()); + font.setHeight ((float) sizeSlider.getValue()); + font.setBold (boldButton.getToggleState()); + font.setItalic (italicButton.getToggleState()); + font.setExtraKerningFactor ((float) kerningSlider.getValue()); + font.setHorizontalScale ((float) horizontalScaleSlider.getValue()); - textBox->applyFontToAllText (font); + textBox.applyFontToAllText (font); } void selectedRowsChanged (int /*lastRowselected*/) @@ -191,6 +182,18 @@ public: // (this is called when the size slider is moved) updatePreviewBoxText(); } + +private: + Array fonts; + + ScopedPointer listBox; + TextEditor textBox; + ToggleButton boldButton, italicButton; + Slider sizeSlider, kerningSlider, horizontalScaleSlider; + Label sizeLabel, kerningLabel, horizontalScaleLabel; + + StretchableLayoutManager verticalLayout; + ScopedPointer verticalDividerBar; }; diff --git a/extras/juce demo/Source/demos/InterprocessCommsDemo.cpp b/extras/juce demo/Source/demos/InterprocessCommsDemo.cpp index 05021fa39e..eb94552329 100644 --- a/extras/juce demo/Source/demos/InterprocessCommsDemo.cpp +++ b/extras/juce demo/Source/demos/InterprocessCommsDemo.cpp @@ -34,62 +34,67 @@ class InterprocessCommsDemo : public Component, public: //============================================================================== InterprocessCommsDemo() + : sendButton ("send", "Fires off the message"), + modeLabel (String::empty, "Mode:"), + pipeLabel (String::empty, "Pipe Name:"), + numberLabel (String::empty, "Socket Port:"), + hostLabel (String::empty, "Socket Host:") { - server = new DemoInterprocessConnectionServer (*this); - setName ("Interprocess Communication"); + server = new DemoInterprocessConnectionServer (*this); + // create all our UI bits and pieces.. - addAndMakeVisible (modeSelector = new ComboBox ("mode:")); - modeSelector->setBounds (100, 25, 200, 24); - (new Label (modeSelector->getName(), modeSelector->getName()))->attachToComponent (modeSelector, true); - - modeSelector->addItem ("(Disconnected)", 8); - modeSelector->addSeparator(); - modeSelector->addItem ("Named pipe (listening)", 1); - modeSelector->addItem ("Named pipe (connect to existing pipe)", 5); - modeSelector->addSeparator(); - modeSelector->addItem ("Socket (listening)", 2); - modeSelector->addItem ("Socket (connect to existing socket)", 6); - - modeSelector->setSelectedId (8); - modeSelector->addListener (this); - - addAndMakeVisible (pipeName = new TextEditor ("pipe name:")); - pipeName->setBounds (100, 60, 130, 24); - pipeName->setMultiLine (false); - pipeName->setText ("juce demo pipe"); - (new Label (pipeName->getName(), pipeName->getName()))->attachToComponent (pipeName, true); - - addAndMakeVisible (socketNumber = new TextEditor ("socket port:")); - socketNumber->setBounds (350, 60, 80, 24); - socketNumber->setMultiLine (false); - socketNumber->setText ("12345"); - socketNumber->setInputRestrictions (5, "0123456789"); - (new Label (socketNumber->getName(), socketNumber->getName()))->attachToComponent (socketNumber, true); - - addAndMakeVisible (socketHost = new TextEditor ("socket host:")); - socketHost->setBounds (530, 60, 130, 24); - socketHost->setMultiLine (false); - socketHost->setText ("localhost"); - socketNumber->setInputRestrictions (512); - (new Label (socketHost->getName(), socketHost->getName()))->attachToComponent (socketHost, true); - - addChildComponent (sendText = new TextEditor ("sendtext")); - sendText->setBounds (30, 120, 200, 24); - sendText->setMultiLine (false); - sendText->setReadOnly (false); - sendText->setText ("testing 1234"); - - addChildComponent (sendButton = new TextButton ("send", "Fires off the message")); - sendButton->setBounds (240, 120, 200, 24); - sendButton->changeWidthToFitText(); - sendButton->addButtonListener (this); - - addChildComponent (incomingMessages = new TextEditor ("messages")); - incomingMessages->setReadOnly (true); - incomingMessages->setMultiLine (true); - incomingMessages->setBounds (30, 150, 500, 250); + addAndMakeVisible (&modeSelector); + modeSelector.setBounds (100, 25, 200, 24); + modeLabel.attachToComponent (&modeSelector, true); + + modeSelector.addItem ("(Disconnected)", 8); + modeSelector.addSeparator(); + modeSelector.addItem ("Named pipe (listening)", 1); + modeSelector.addItem ("Named pipe (connect to existing pipe)", 5); + modeSelector.addSeparator(); + modeSelector.addItem ("Socket (listening)", 2); + modeSelector.addItem ("Socket (connect to existing socket)", 6); + + modeSelector.setSelectedId (8); + modeSelector.addListener (this); + + addAndMakeVisible (&pipeName); + pipeName.setBounds (100, 60, 130, 24); + pipeName.setMultiLine (false); + pipeName.setText ("juce demo pipe"); + pipeLabel.attachToComponent (&pipeName, true); + + addAndMakeVisible (&socketNumber); + socketNumber.setBounds (350, 60, 80, 24); + socketNumber.setMultiLine (false); + socketNumber.setText ("12345"); + socketNumber.setInputRestrictions (5, "0123456789"); + numberLabel.attachToComponent (&socketNumber, true); + + addAndMakeVisible (&socketHost); + socketHost.setBounds (530, 60, 130, 24); + socketHost.setMultiLine (false); + socketHost.setText ("localhost"); + socketNumber.setInputRestrictions (512); + hostLabel.attachToComponent (&socketHost, true); + + addChildComponent (&sendText); + sendText.setBounds (30, 120, 200, 24); + sendText.setMultiLine (false); + sendText.setReadOnly (false); + sendText.setText ("testing 1234"); + + addChildComponent (&sendButton); + sendButton.setBounds (240, 120, 200, 24); + sendButton.changeWidthToFitText(); + sendButton.addButtonListener (this); + + addChildComponent (&incomingMessages); + incomingMessages.setReadOnly (true); + incomingMessages.setMultiLine (true); + incomingMessages.setBounds (30, 150, 500, 250); // call this to set up everything's state correctly. comboBoxChanged (0); @@ -98,18 +103,15 @@ public: ~InterprocessCommsDemo() { close(); - delete server; - - deleteAllChildren(); } void buttonClicked (Button* button) { - if (button == sendButton) + if (button == &sendButton) { // The send button has been pressed, so write out the contents of the // text box to the socket or pipe, depending on which is active. - const String text (sendText->getText()); + const String text (sendText.getText()); MemoryBlock messageData (text.toUTF8(), text.getNumBytesAsUTF8()); for (int i = activeConnections.size(); --i >= 0;) @@ -126,7 +128,7 @@ public: void comboBoxChanged (ComboBox*) { // This is called when the user picks a different mode from the drop-down list.. - const int modeId = modeSelector->getSelectedId(); + const int modeId = modeSelector.getSelectedId(); close(); @@ -145,10 +147,10 @@ public: activeConnections.clear(); // Reset the UI stuff to a disabled state. - sendText->setVisible (false); - sendButton->setVisible (false); - incomingMessages->setText (String::empty, false); - incomingMessages->setVisible (true); + sendText.setVisible (false); + sendButton.setVisible (false); + incomingMessages.setText (String::empty, false); + incomingMessages.setVisible (true); appendMessage ( "To demonstrate named pipes, you'll need to run two instances of the JuceDemo application on this machine. On " @@ -165,11 +167,11 @@ public: close(); // Make the appropriate bits of UI visible.. - sendText->setVisible (true); - sendButton->setVisible (true); + sendText.setVisible (true); + sendButton.setVisible (true); - incomingMessages->setText (String::empty, false); - incomingMessages->setVisible (true); + incomingMessages.setText (String::empty, false); + incomingMessages.setVisible (true); // and try to open the socket or pipe... bool openedOk = false; @@ -178,23 +180,21 @@ public: { // if we're connecting to an existing server, we can just create a connection object // directly. - DemoInterprocessConnection* newConnection = new DemoInterprocessConnection (*this); + ScopedPointer newConnection (new DemoInterprocessConnection (*this)); if (asSocket) { - openedOk = newConnection->connectToSocket (socketHost->getText(), - socketNumber->getText().getIntValue(), + openedOk = newConnection->connectToSocket (socketHost.getText(), + socketNumber.getText().getIntValue(), 1000); } else { - openedOk = newConnection->connectToPipe (pipeName->getText()); + openedOk = newConnection->connectToPipe (pipeName.getText()); } if (openedOk) - activeConnections.add (newConnection); - else - delete newConnection; + activeConnections.add (newConnection.release()); } else { @@ -202,32 +202,28 @@ public: // clients to connect. It'll then create connection objects for us when clients arrive. if (asSocket) { - openedOk = server->beginWaitingForSocket (socketNumber->getText().getIntValue()); + openedOk = server->beginWaitingForSocket (socketNumber.getText().getIntValue()); if (openedOk) appendMessage ("Waiting for another app to connect to this socket.."); } else { - DemoInterprocessConnection* newConnection = new DemoInterprocessConnection (*this); + ScopedPointer newConnection (new DemoInterprocessConnection (*this)); - openedOk = newConnection->createPipe (pipeName->getText()); + openedOk = newConnection->createPipe (pipeName.getText()); if (openedOk) { appendMessage ("Waiting for another app to connect to this pipe.."); - activeConnections.add (newConnection); - } - else - { - delete newConnection; + activeConnections.add (newConnection.release()); } } } if (! openedOk) { - modeSelector->setSelectedId (8); + modeSelector.setSelectedId (8); AlertWindow::showMessageBox (AlertWindow::WarningIcon, "Interprocess Comms Demo", @@ -237,17 +233,14 @@ public: void appendMessage (const String& message) { - incomingMessages->setCaretPosition (INT_MAX); - incomingMessages->insertTextAtCaret (message + "\n"); - incomingMessages->setCaretPosition (INT_MAX); + incomingMessages.setCaretPosition (INT_MAX); + incomingMessages.insertTextAtCaret (message + "\n"); + incomingMessages.setCaretPosition (INT_MAX); } //============================================================================== class DemoInterprocessConnection : public InterprocessConnection { - InterprocessCommsDemo& owner; - int ourNumber; - public: DemoInterprocessConnection (InterprocessCommsDemo& owner_) : InterprocessConnection (true), @@ -257,10 +250,6 @@ public: ourNumber = ++totalConnections; } - ~DemoInterprocessConnection() - { - } - void connectionMade() { owner.appendMessage ("Connection #" + String (ourNumber) + " - connection started"); @@ -275,23 +264,21 @@ public: { owner.appendMessage ("Connection #" + String (ourNumber) + " - message received: " + message.toString()); } + + private: + InterprocessCommsDemo& owner; + int ourNumber; }; //============================================================================== class DemoInterprocessConnectionServer : public InterprocessConnectionServer { - InterprocessCommsDemo& owner; - public: DemoInterprocessConnectionServer (InterprocessCommsDemo& owner_) : owner (owner_) { } - ~DemoInterprocessConnectionServer() - { - } - InterprocessConnection* createConnectionObject() { DemoInterprocessConnection* newConnection = new DemoInterprocessConnection (owner); @@ -299,6 +286,9 @@ public: owner.activeConnections.add (newConnection); return newConnection; } + + private: + InterprocessCommsDemo& owner; }; OwnedArray activeConnections; @@ -308,16 +298,12 @@ public: juce_UseDebuggingNewOperator private: - ComboBox* modeSelector; - TextEditor* sendText; - TextButton* sendButton; - TextEditor* incomingMessages; - - TextEditor* pipeName; - TextEditor* socketNumber; - TextEditor* socketHost; + ComboBox modeSelector; + TextButton sendButton; + TextEditor sendText, incomingMessages, pipeName, socketNumber, socketHost; + Label modeLabel, pipeLabel, numberLabel, hostLabel; - DemoInterprocessConnectionServer* server; + ScopedPointer server; }; diff --git a/extras/juce demo/Source/demos/OpenGLDemo.cpp b/extras/juce demo/Source/demos/OpenGLDemo.cpp index 48e433dd46..ada5344a7f 100644 --- a/extras/juce demo/Source/demos/OpenGLDemo.cpp +++ b/extras/juce demo/Source/demos/OpenGLDemo.cpp @@ -55,20 +55,16 @@ class DemoOpenGLCanvas : public OpenGLComponent, public Timer { - float rotation, delta; - Image image; - public: DemoOpenGLCanvas() + : rotation (0.0f), + delta (1.0f) { #if JUCE_IPHONE // (On the iPhone, choose a format without a depth buffer) setPixelFormat (OpenGLPixelFormat (8, 8, 0, 0)); #endif - rotation = 0.0f; - delta = 1.0f; - image = Image (Image::RGB, 512, 512, true, Image::SoftwareImage); Graphics g (image); @@ -259,33 +255,35 @@ public: repaint(); } + +private: + float rotation, delta; + Image image; }; + //============================================================================== class OpenGLDemo : public Component { - //============================================================================== - DemoOpenGLCanvas* canvas; - public: //============================================================================== OpenGLDemo() { setName ("OpenGL"); - canvas = new DemoOpenGLCanvas(); - addAndMakeVisible (canvas); - } - - ~OpenGLDemo() - { - deleteAllChildren(); + addAndMakeVisible (&canvas); } void resized() { - canvas->setBounds (10, 10, getWidth() - 20, getHeight() - 50); + canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50); } + +private: + DemoOpenGLCanvas canvas; + + OpenGLDemo (const OpenGLDemo&); + OpenGLDemo& operator= (const OpenGLDemo&); }; diff --git a/extras/juce demo/Source/demos/QuickTimeDemo.cpp b/extras/juce demo/Source/demos/QuickTimeDemo.cpp index 6a64e0bac4..fc45aec718 100644 --- a/extras/juce demo/Source/demos/QuickTimeDemo.cpp +++ b/extras/juce demo/Source/demos/QuickTimeDemo.cpp @@ -34,39 +34,34 @@ class QuickTimeWindowWithFileBrowser : public Component, { public: QuickTimeWindowWithFileBrowser() + : fileChooser ("movie", File::nonexistent, true, false, false, + "*.*", String::empty, "(choose a video file to play)") { - addAndMakeVisible (qtComp = new QuickTimeMovieComponent()); - - // and a file-chooser.. - addAndMakeVisible (fileChooser = new FilenameComponent ("movie", - File::nonexistent, - true, false, false, - "*.*", - String::empty, - "(choose a video file to play)")); - fileChooser->addListener (this); - fileChooser->setBrowseButtonText ("browse"); + addAndMakeVisible (&qtComp); + + addAndMakeVisible (&fileChooser); + fileChooser.addListener (this); + fileChooser.setBrowseButtonText ("browse"); } ~QuickTimeWindowWithFileBrowser() { - deleteAllChildren(); } void resized() { - qtComp->setBounds (0, 0, getWidth(), getHeight() - 30); - fileChooser->setBounds (0, getHeight() - 24, getWidth(), 24); + qtComp.setBounds (0, 0, getWidth(), getHeight() - 30); + fileChooser.setBounds (0, getHeight() - 24, getWidth(), 24); } void filenameComponentChanged (FilenameComponent*) { // this is called when the user changes the filename in the file chooser box - if (qtComp->loadMovie (fileChooser->getCurrentFile(), true)) + if (qtComp.loadMovie (fileChooser.getCurrentFile(), true)) { // loaded the file ok, so let's start it playing.. - qtComp->play(); + qtComp.play(); } else { @@ -77,8 +72,8 @@ public: } private: - QuickTimeMovieComponent* qtComp; - FilenameComponent* fileChooser; + QuickTimeMovieComponent qtComp; + FilenameComponent fileChooser; }; @@ -92,25 +87,25 @@ public: setName ("QuickTime"); // add a movie component.. - addAndMakeVisible (qtComp1 = new QuickTimeWindowWithFileBrowser()); - addAndMakeVisible (qtComp2 = new QuickTimeWindowWithFileBrowser()); + addAndMakeVisible (&qtComp1); + addAndMakeVisible (&qtComp2); } ~QuickTimeDemo() { - deleteAllChildren(); + qtComp1.setVisible (false); + qtComp2.setVisible (false); } void resized() { - qtComp1->setBoundsRelative (0.05f, 0.05f, 0.425f, 0.9f); - qtComp2->setBoundsRelative (0.525f, 0.05f, 0.425f, 0.9f); + qtComp1.setBoundsRelative (0.05f, 0.05f, 0.425f, 0.9f); + qtComp2.setBoundsRelative (0.525f, 0.05f, 0.425f, 0.9f); } private: //============================================================================== - QuickTimeWindowWithFileBrowser* qtComp1; - QuickTimeWindowWithFileBrowser* qtComp2; + QuickTimeWindowWithFileBrowser qtComp1, qtComp2; }; diff --git a/extras/juce demo/Source/demos/RenderingTestComponent.cpp b/extras/juce demo/Source/demos/RenderingTestComponent.cpp index e93d5b9ce7..c51ddb23e9 100644 --- a/extras/juce demo/Source/demos/RenderingTestComponent.cpp +++ b/extras/juce demo/Source/demos/RenderingTestComponent.cpp @@ -35,7 +35,6 @@ public: { setOpaque (true); averageTime = 0; - svgDrawable = 0; rgbImage = ImageFileFormat::loadFrom (RenderingTestComponent::demoJpeg_jpg, RenderingTestComponent::demoJpeg_jpgSize); argbImage = ImageFileFormat::loadFrom (RenderingTestComponent::demoPng_png, RenderingTestComponent::demoPng_pngSize); @@ -68,7 +67,6 @@ public: ~RenderingTestCanvas() { - delete svgDrawable; } void paint (Graphics& g) @@ -150,7 +148,7 @@ private: double averageTime; Image rgbImage, argbImage; - DrawableComposite* svgDrawable; + ScopedPointer svgDrawable; GlyphArrangement glyphs; ColourGradient linearGradient, radialGradient; float bouncingPointX[10], bouncingPointY[10], bouncingPointDX[10], bouncingPointDY[10]; @@ -327,8 +325,6 @@ private: void createSVGDrawable() { - deleteAndZero (svgDrawable); - MemoryInputStream iconsFileStream (BinaryData::icons_zip, BinaryData::icons_zipSize, false); ZipFile icons (&iconsFileStream, false); diff --git a/extras/juce demo/Source/demos/TableDemo.cpp b/extras/juce demo/Source/demos/TableDemo.cpp index e709c537c0..ae82204411 100644 --- a/extras/juce demo/Source/demos/TableDemo.cpp +++ b/extras/juce demo/Source/demos/TableDemo.cpp @@ -36,8 +36,7 @@ class TableDemoComponent : public Component, public: //============================================================================== TableDemoComponent() - : font (14.0f), - demoData (0) + : font (14.0f) { // Load some data from an embedded XML file.. loadData(); @@ -71,9 +70,6 @@ public: ~TableDemoComponent() { - deleteAllChildren(); - - delete demoData; } //============================================================================== @@ -201,10 +197,10 @@ public: juce_UseDebuggingNewOperator private: - TableListBox* table; // the table component itself + ScopedPointer table; // the table component itself Font font; - XmlElement* demoData; // This is the XML document loaded from the embedded file "demo table data.xml" + ScopedPointer demoData; // This is the XML document loaded from the embedded file "demo table data.xml" XmlElement* columnList; // A pointer to the sub-node of demoData that contains the list of columns XmlElement* dataList; // A pointer to the sub-node of demoData that contains the list of data rows int numRows; // The number of rows of data we've got @@ -220,28 +216,27 @@ private: : owner (owner_) { // just put a combo box inside this component - addAndMakeVisible (comboBox = new ComboBox (String::empty)); - comboBox->addItem ("fab", 1); - comboBox->addItem ("groovy", 2); - comboBox->addItem ("hep", 3); - comboBox->addItem ("neat", 4); - comboBox->addItem ("wild", 5); - comboBox->addItem ("swingin", 6); - comboBox->addItem ("mad for it", 7); + addAndMakeVisible (&comboBox); + comboBox.addItem ("fab", 1); + comboBox.addItem ("groovy", 2); + comboBox.addItem ("hep", 3); + comboBox.addItem ("neat", 4); + comboBox.addItem ("wild", 5); + comboBox.addItem ("swingin", 6); + comboBox.addItem ("mad for it", 7); // when the combo is changed, we'll get a callback. - comboBox->addListener (this); - comboBox->setWantsKeyboardFocus (false); + comboBox.addListener (this); + comboBox.setWantsKeyboardFocus (false); } ~RatingColumnCustomComponent() { - deleteAllChildren(); } void resized() { - comboBox->setBoundsInset (BorderSize (2)); + comboBox.setBoundsInset (BorderSize (2)); } // Our demo code will call this when we may need to update our contents @@ -249,17 +244,17 @@ private: { row = newRow; columnId = newColumn; - comboBox->setSelectedId (owner.getRating (row), true); + comboBox.setSelectedId (owner.getRating (row), true); } void comboBoxChanged (ComboBox* /*comboBoxThatHasChanged*/) { - owner.setRating (row, comboBox->getSelectedId()); + owner.setRating (row, comboBox.getSelectedId()); } private: TableDemoComponent& owner; - ComboBox* comboBox; + ComboBox comboBox; int row, columnId; }; diff --git a/extras/juce demo/Source/demos/ThreadingDemo.cpp b/extras/juce demo/Source/demos/ThreadingDemo.cpp index 7f24705101..899049e1d2 100644 --- a/extras/juce demo/Source/demos/ThreadingDemo.cpp +++ b/extras/juce demo/Source/demos/ThreadingDemo.cpp @@ -213,49 +213,40 @@ class ThreadingDemo : public Component, public Timer, public ButtonListener { - bool isUsingPool; - ThreadPool pool; - TextButton* controlButton; - public: //============================================================================== ThreadingDemo() - : pool (3) + : pool (3), + controlButton ("Thread type"), + isUsingPool (false) { - isUsingPool = false; - setName ("Multithreading"); setOpaque (true); + + addAndMakeVisible (&controlButton); + controlButton.changeWidthToFitText (20); + controlButton.setTopLeftPosition (20, 20); + controlButton.setTriggeredOnMouseDown (true); + controlButton.setAlwaysOnTop (true); + controlButton.addButtonListener (this); } ~ThreadingDemo() { pool.removeAllJobs (true, 2000); - - deleteAllChildren(); } - // this gets called when a component is added or removed from a parent component. - void parentHierarchyChanged() + void resetAllBalls() { - // we'll use this as an opportunity to start and stop the threads, so that - // we don't leave them going when the component's not actually visible. stopTimer(); pool.removeAllJobs (true, 4000); - deleteAllChildren(); - - addAndMakeVisible (controlButton = new TextButton ("Thread type")); - controlButton->changeWidthToFitText (20); - controlButton->setTopLeftPosition (20, 20); - controlButton->setTriggeredOnMouseDown (true); - controlButton->setAlwaysOnTop (true); - controlButton->addButtonListener (this); + balls.clear(); if (isShowing()) { - while (getNumChildComponents() < 5) + while (balls.size() < 5) addABall(); startTimer (2000); @@ -270,48 +261,39 @@ public: void setUsingPool (bool usePool) { isUsingPool = usePool; - parentHierarchyChanged(); // resets everything + resetAllBalls(); } void addABall() { if (isUsingPool) { - DemoThreadPoolJob* newComp = new DemoThreadPoolJob(); - addAndMakeVisible (newComp); - newComp->parentSizeChanged(); + DemoThreadPoolJob* newBall = new DemoThreadPoolJob(); + balls.add (newBall); + addAndMakeVisible (newBall); + newBall->parentSizeChanged(); - pool.addJob (newComp); + pool.addJob (newBall); } else { - DemoThread* newComp = new DemoThread(); - addAndMakeVisible (newComp); - newComp->parentSizeChanged(); + DemoThread* newBall = new DemoThread(); + balls.add (newBall); + addAndMakeVisible (newBall); + newBall->parentSizeChanged(); } } void removeABall() { - if (isUsingPool) + if (balls.size() > 0) { - ThreadPoolJob* jobToRemove = pool.getJob (Random::getSystemRandom().nextInt (pool.getNumJobs())); + int indexToRemove = Random::getSystemRandom().nextInt (balls.size()); - if (jobToRemove != 0) - { - pool.removeJob (jobToRemove, true, 4000); - delete jobToRemove; - } - } - else - { - if (getNumChildComponents() > 1) - { - Component* ball = getChildComponent (1 + Random::getSystemRandom().nextInt (getNumChildComponents() - 1)); + if (isUsingPool) + pool.removeJob (dynamic_cast (balls [indexToRemove]), true, 4000); - if (dynamic_cast (ball) == 0) // don't delete our button! - delete ball; - } + balls.remove (indexToRemove); } } @@ -319,27 +301,42 @@ public: { if (Random::getSystemRandom().nextBool()) { - if (getNumChildComponents() <= 10) + if (balls.size() <= 10) addABall(); } else { - if (getNumChildComponents() > 3) + if (balls.size() > 3) removeABall(); } } - void buttonClicked (Button* button) + void buttonClicked (Button*) { PopupMenu m; m.addItem (1, "Use one thread per ball", true, ! isUsingPool); m.addItem (2, "Use a thread pool", true, isUsingPool); - const int res = m.showAt (button); + const int res = m.showAt (&controlButton); if (res != 0) setUsingPool (res == 2); } + + // this gets called when a component is added or removed from a parent component. + void parentHierarchyChanged() + { + // we'll use this as an opportunity to start and stop the threads, so that + // we don't leave them going when the component's not actually visible. + resetAllBalls(); + } + +private: + ThreadPool pool; + TextButton controlButton; + bool isUsingPool; + + OwnedArray balls; }; diff --git a/extras/juce demo/Source/demos/TreeViewDemo.cpp b/extras/juce demo/Source/demos/TreeViewDemo.cpp index 8c2cbb3ea8..74db5b550d 100644 --- a/extras/juce demo/Source/demos/TreeViewDemo.cpp +++ b/extras/juce demo/Source/demos/TreeViewDemo.cpp @@ -29,10 +29,8 @@ //============================================================================== class TreeViewDemoItem : public TreeViewItem { - XmlElement* xml; - public: - TreeViewDemoItem (XmlElement* const xml_) + TreeViewDemoItem (XmlElement& xml_) : xml (xml_) { } @@ -43,41 +41,34 @@ public: int getItemWidth() const { - return xml->getIntAttribute ("width", -1); + return xml.getIntAttribute ("width", -1); } const String getUniqueName() const { - if (xml != 0) - return xml->getTagName(); - else - return String::empty; + return xml.getTagName(); } bool mightContainSubItems() { - return xml != 0 - && xml->getFirstChildElement() != 0; + return xml.getFirstChildElement() != 0; } void paintItem (Graphics& g, int width, int height) { - if (xml != 0) - { - // if this item is selected, fill it with a background colour.. - if (isSelected()) - g.fillAll (Colours::blue.withAlpha (0.3f)); + // if this item is selected, fill it with a background colour.. + if (isSelected()) + g.fillAll (Colours::blue.withAlpha (0.3f)); - // use a "colour" attribute in the xml tag for this node to set the text colour.. - g.setColour (Colour (xml->getStringAttribute ("colour", "ff000000").getHexValue32())); + // use a "colour" attribute in the xml tag for this node to set the text colour.. + g.setColour (Colour (xml.getStringAttribute ("colour", "ff000000").getHexValue32())); - g.setFont (height * 0.7f); + g.setFont (height * 0.7f); - // draw the xml element's tag name.. - g.drawText (xml->getTagName(), - 4, 0, width - 4, height, - Justification::centredLeft, true); - } + // draw the xml element's tag name.. + g.drawText (xml.getTagName(), + 4, 0, width - 4, height, + Justification::centredLeft, true); } void itemOpennessChanged (bool isNowOpen) @@ -92,12 +83,10 @@ public: // create and add sub-items to this node of the tree, corresponding to // each sub-element in the XML.. - if (xml != 0) + forEachXmlChildElement (xml, child) { - forEachXmlChildElement (*xml, child) - { - addSubItem (new TreeViewDemoItem (child)); - } + jassert (child != 0); + addSubItem (new TreeViewDemoItem (*child)); } } } @@ -113,6 +102,9 @@ public: { return "TreeView Items"; } + +private: + XmlElement& xml; }; //============================================================================== @@ -120,33 +112,23 @@ class TreeViewDemo : public Component, public DragAndDropContainer, public ButtonListener { - XmlElement* treeXml; - - TreeViewItem* rootItem; - TreeView* treeView; - - FileTreeComponent* fileTreeComp; - DirectoryContentsList* directoryList; - TimeSliceThread thread; - - TextButton* typeButton; - public: //============================================================================== TreeViewDemo() : treeView (0), - rootItem (0), - fileTreeComp (0), - directoryList (0), - thread ("Demo file tree thread") + thread ("Demo file tree thread"), + typeButton ("Type of treeview...") { setName ("Tree Views"); - const String treeXmlString (BinaryData::treedemo_xml); - XmlDocument parser (treeXmlString); - treeXml = parser.getDocumentElement(); + { + const String treeXmlString (BinaryData::treedemo_xml); + XmlDocument parser (treeXmlString); + treeXml = parser.getDocumentElement(); + jassert (treeXml != 0); + } - rootItem = new TreeViewDemoItem (treeXml); + rootItem = new TreeViewDemoItem (*treeXml); rootItem->setOpen (true); // find the root of the user's home drive, and set that as our root.. @@ -158,21 +140,16 @@ public: directoryList->setDirectory (folder, true, true); thread.startThread (3); - addAndMakeVisible (typeButton = new TextButton ("Type of treeview...")); - typeButton->addButtonListener (this); - typeButton->setAlwaysOnTop (true); - typeButton->setTriggeredOnMouseDown (true); + addAndMakeVisible (&typeButton); + typeButton.addButtonListener (this); + typeButton.setAlwaysOnTop (true); + typeButton.setTriggeredOnMouseDown (true); showCustomTreeView(); } ~TreeViewDemo() { - deleteAllChildren(); - - delete rootItem; - delete treeXml; - delete directoryList; } void paint (Graphics& g) @@ -195,14 +172,14 @@ public: else if (fileTreeComp != 0) fileTreeComp->setBoundsInset (BorderSize (40, 10, 10, 10)); - typeButton->changeWidthToFitText (22); - typeButton->setTopLeftPosition (10, 10); + typeButton.changeWidthToFitText (22); + typeButton.setTopLeftPosition (10, 10); } void showCustomTreeView() { - deleteAndZero (treeView); - deleteAndZero (fileTreeComp); + treeView = 0; + fileTreeComp = 0; addAndMakeVisible (treeView = new TreeView()); treeView->setRootItem (rootItem); @@ -213,11 +190,10 @@ public: void showFileTreeComp() { - deleteAndZero (treeView); - deleteAndZero (fileTreeComp); + treeView = 0; + fileTreeComp = 0; addAndMakeVisible (fileTreeComp = new FileTreeComponent (*directoryList)); - resized(); } @@ -234,7 +210,7 @@ public: treeView != 0 ? treeView->areOpenCloseButtonsVisible() : fileTreeComp->areOpenCloseButtonsVisible()); - const int r = m.showAt (typeButton); + const int r = m.showAt (&typeButton); if (r == 1) { @@ -261,6 +237,18 @@ public: } juce_UseDebuggingNewOperator + +private: + ScopedPointer treeXml; + + ScopedPointer rootItem; + ScopedPointer treeView; + + ScopedPointer fileTreeComp; + ScopedPointer directoryList; + TimeSliceThread thread; + + TextButton typeButton; }; diff --git a/extras/juce demo/Source/demos/WebBrowserDemo.cpp b/extras/juce demo/Source/demos/WebBrowserDemo.cpp index cb8b09be98..ed58b18617 100644 --- a/extras/juce demo/Source/demos/WebBrowserDemo.cpp +++ b/extras/juce demo/Source/demos/WebBrowserDemo.cpp @@ -36,7 +36,7 @@ class DemoBrowserComponent : public WebBrowserComponent { public: //============================================================================== - DemoBrowserComponent (TextEditor* addressTextBox_) + DemoBrowserComponent (TextEditor& addressTextBox_) : addressTextBox (addressTextBox_) { } @@ -45,7 +45,7 @@ public: bool pageAboutToLoad (const String& newURL) { // We'll just update our address box to reflect the new location.. - addressTextBox->setText (newURL, false); + addressTextBox.setText (newURL, false); // we could return false here to tell the browser not to go ahead with // loading the page. @@ -56,7 +56,7 @@ public: juce_UseDebuggingNewOperator private: - TextEditor* addressTextBox; + TextEditor& addressTextBox; DemoBrowserComponent (DemoBrowserComponent&); DemoBrowserComponent& operator= (const DemoBrowserComponent&); @@ -71,24 +71,27 @@ class WebBrowserDemo : public Component, public: //============================================================================== WebBrowserDemo() + : goButton ("Go", "Go to URL"), + backButton ("<<", "Back"), + forwardButton (">>", "Forward") { setName ("Web Browser"); // Create an address box.. - addAndMakeVisible (addressTextBox = new TextEditor()); - addressTextBox->setTextToShowWhenEmpty ("Enter a web address, e.g. http://www.rawmaterialsoftware.com", Colours::grey); - addressTextBox->addListener (this); + addAndMakeVisible (&addressTextBox); + addressTextBox.setTextToShowWhenEmpty ("Enter a web address, e.g. http://www.rawmaterialsoftware.com", Colours::grey); + addressTextBox.addListener (this); // create the actual browser component addAndMakeVisible (webView = new DemoBrowserComponent (addressTextBox)); // add some buttons.. - addAndMakeVisible (goButton = new TextButton ("Go", "Go to URL")); - goButton->addButtonListener (this); - addAndMakeVisible (backButton = new TextButton ("<<", "Back")); - backButton->addButtonListener (this); - addAndMakeVisible (forwardButton = new TextButton (">>", "Forward")); - forwardButton->addButtonListener (this); + addAndMakeVisible (&goButton); + goButton.addButtonListener (this); + addAndMakeVisible (&backButton); + backButton.addButtonListener (this); + addAndMakeVisible (&forwardButton); + forwardButton.addButtonListener (this); // send the browser to a start page.. webView->goToURL ("http://www.google.com"); @@ -96,16 +99,15 @@ public: ~WebBrowserDemo() { - deleteAllChildren(); } void resized() { webView->setBounds (10, 45, getWidth() - 20, getHeight() - 55); - goButton->setBounds (getWidth() - 45, 10, 35, 25); - addressTextBox->setBounds (100, 10, getWidth() - 155, 25); - backButton->setBounds (10, 10, 35, 25); - forwardButton->setBounds (55, 10, 35, 25); + goButton.setBounds (getWidth() - 45, 10, 35, 25); + addressTextBox.setBounds (100, 10, getWidth() - 155, 25); + backButton.setBounds (10, 10, 35, 25); + forwardButton.setBounds (55, 10, 35, 25); } void textEditorTextChanged (TextEditor&) {} @@ -114,28 +116,26 @@ public: void textEditorReturnKeyPressed (TextEditor&) { - webView->goToURL (addressTextBox->getText()); + webView->goToURL (addressTextBox.getText()); } void buttonClicked (Button* b) { - if (b == backButton) + if (b == &backButton) webView->goBack(); - else if (b == forwardButton) + else if (b == &forwardButton) webView->goForward(); - else if (b == goButton) - webView->goToURL (addressTextBox->getText()); + else if (b == &goButton) + webView->goToURL (addressTextBox.getText()); } juce_UseDebuggingNewOperator private: - DemoBrowserComponent* webView; + ScopedPointer webView; - TextEditor* addressTextBox; - TextButton* goButton; - TextButton* backButton; - TextButton* forwardButton; + TextEditor addressTextBox; + TextButton goButton, backButton, forwardButton; }; diff --git a/extras/juce demo/Source/demos/WidgetsDemo.cpp b/extras/juce demo/Source/demos/WidgetsDemo.cpp index 8cd47d0fed..eb510aa06c 100644 --- a/extras/juce demo/Source/demos/WidgetsDemo.cpp +++ b/extras/juce demo/Source/demos/WidgetsDemo.cpp @@ -30,9 +30,6 @@ class BouncingBallComponent : public Component, public Timer { - Colour colour; - float x, y, dx, dy; - public: BouncingBallComponent() { @@ -86,14 +83,15 @@ public: { return false; } + +private: + Colour colour; + float x, y, dx, dy; }; //============================================================================== class DragOntoDesktopDemoComp : public Component { - Component* parent; - ComponentDragger dragger; - public: DragOntoDesktopDemoComp (Component* p) : parent (p) @@ -101,13 +99,12 @@ public: // show off semi-transparency if it's supported by the current OS. setOpaque (! Desktop::canUseSemiTransparentWindows()); - for (int i = 3; --i >= 0;) - addAndMakeVisible (new BouncingBallComponent()); + for (int i = 0; i < numElementsInArray (balls); ++i) + addAndMakeVisible (&(balls[i])); } ~DragOntoDesktopDemoComp() { - deleteAllChildren(); } void mouseDown (const MouseEvent&) @@ -117,9 +114,9 @@ public: void mouseDrag (const MouseEvent& e) { - if (! parent->isValidComponent()) + if (parent == 0) { - delete this; + delete this; // If our parent has been deleted, we'll just get rid of this component } else { @@ -159,14 +156,18 @@ public: g.drawRect (0, 0, getWidth(), getHeight()); } + +private: + Component::SafePointer parent; // A safe-pointer will become zero if the component that it refers to is deleted.. + ComponentDragger dragger; + + BouncingBallComponent balls[3]; }; //============================================================================== class CustomMenuComponent : public PopupMenuCustomComponent, public Timer { - int blobX, blobY; - public: CustomMenuComponent() : blobX (0), @@ -210,6 +211,9 @@ public: blobY = Random::getSystemRandom().nextInt (getHeight()); repaint(); } + +private: + int blobX, blobY; }; //============================================================================== @@ -269,7 +273,7 @@ public: void changeListenerCallback (void* source) { - ColourSelector* cs = (ColourSelector*) source; + ColourSelector* cs = static_cast (source); if (cs->getName() == "text") setColour (TextButton::textColourOffId, cs->getCurrentColour()); @@ -279,8 +283,9 @@ public: }; //============================================================================== -// just a component that deletes all its children, to use for the tabbed pages to avoid -// memory leaks when they're deleted +/* A component to act as a simple container for our demos, which deletes all the child + components that we stuff into it. +*/ class DemoPageComp : public Component { public: @@ -290,6 +295,12 @@ public: ~DemoPageComp() { + /* Deleting your child components indiscriminately using deleteAllChildren() is not recommended! It's much + safer to make them embedded members or use ScopedPointers to automatically manage their lifetimes! + + In this demo, where we're throwing together a whole bunch of random components, it's simpler to do it + like this, but don't treat this as an example of good practice! + */ deleteAllChildren(); } }; @@ -579,6 +590,12 @@ public: ~ButtonsPage() { + /* Deleting your child components indiscriminately using deleteAllChildren() is not recommended! It's much + safer to make them embedded members or use ScopedPointers to automatically manage their lifetimes! + + In this demo, where we're throwing together a whole bunch of random components, it's simpler to do it + like this, but don't treat this as an example of good practice! + */ deleteAllChildren(); } @@ -648,57 +665,57 @@ class ToolbarDemoComp : public Component, { public: ToolbarDemoComp() + : depthLabel (String::empty, "Toolbar depth:"), + infoLabel (String::empty, "As well as showing off toolbars, this demo illustrates how to store " + "a set of SVG files in a Zip file, embed that in your application, and read " + "them back in at runtime.\n\nThe icon images here are taken from the open-source " + "Tango icon project."), + orientationButton ("Vertical/Horizontal"), + customiseButton ("Customise...") { // Create and add the toolbar... - addAndMakeVisible (toolbar = new Toolbar()); + addAndMakeVisible (&toolbar); // And use our item factory to add a set of default icons to it... - toolbar->addDefaultItems (factory); + toolbar.addDefaultItems (factory); // Now we'll just create the other sliders and buttons on the demo page, which adjust // the toolbar's properties... - Label* info = new Label (String::empty, - "As well as showing off toolbars, this demo illustrates how to store " - "a set of SVG files in a Zip file, embed that in your application, and read " - "them back in at runtime.\n\nThe icon images here are taken from the open-source " - "Tango icon project."); - - addAndMakeVisible (info); - info->setJustificationType (Justification::topLeft); - info->setBounds (80, 80, 450, 100); - info->setInterceptsMouseClicks (false, false); - - addAndMakeVisible (depthSlider = new Slider ("toolbar depth:")); - depthSlider->setRange (10.0, 200.0, 1.0); - depthSlider->setValue (50, false); - depthSlider->setSliderStyle (Slider::LinearHorizontal); - depthSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); - depthSlider->addListener (this); - depthSlider->setBounds (80, 210, 300, 22); - (new Label (depthSlider->getName(), depthSlider->getName()))->attachToComponent (depthSlider, false); - - addAndMakeVisible (orientationButton = new TextButton ("vertical/horizontal")); - orientationButton->addButtonListener (this); - orientationButton->changeWidthToFitText (22); - orientationButton->setTopLeftPosition (depthSlider->getX(), depthSlider->getBottom() + 20); - - addAndMakeVisible (customiseButton = new TextButton ("customise...")); - customiseButton->addButtonListener (this); - customiseButton->changeWidthToFitText (22); - customiseButton->setTopLeftPosition (orientationButton->getRight() + 20, orientationButton->getY()); + addAndMakeVisible (&infoLabel); + infoLabel.setJustificationType (Justification::topLeft); + infoLabel.setBounds (80, 80, 450, 100); + infoLabel.setInterceptsMouseClicks (false, false); + + addAndMakeVisible (&depthSlider); + depthSlider.setRange (10.0, 200.0, 1.0); + depthSlider.setValue (50, false); + depthSlider.setSliderStyle (Slider::LinearHorizontal); + depthSlider.setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); + depthSlider.addListener (this); + depthSlider.setBounds (80, 210, 300, 22); + depthLabel.attachToComponent (&depthSlider, false); + + addAndMakeVisible (&orientationButton); + orientationButton.addButtonListener (this); + orientationButton.changeWidthToFitText (22); + orientationButton.setTopLeftPosition (depthSlider.getX(), depthSlider.getBottom() + 20); + + addAndMakeVisible (&customiseButton); + customiseButton.addButtonListener (this); + customiseButton.changeWidthToFitText (22); + customiseButton.setTopLeftPosition (orientationButton.getRight() + 20, orientationButton.getY()); } ~ToolbarDemoComp() { - deleteAllChildren(); } void resized() { - if (toolbar->isVertical()) - toolbar->setBounds (0, 0, (int) depthSlider->getValue(), getHeight()); + if (toolbar.isVertical()) + toolbar.setBounds (0, 0, (int) depthSlider.getValue(), getHeight()); else - toolbar->setBounds (0, 0, getWidth(), (int) depthSlider->getValue()); + toolbar.setBounds (0, 0, getWidth(), (int) depthSlider.getValue()); } void sliderValueChanged (Slider*) @@ -708,22 +725,22 @@ public: void buttonClicked (Button* button) { - if (button == orientationButton) + if (button == &orientationButton) { - toolbar->setVertical (! toolbar->isVertical()); + toolbar.setVertical (! toolbar.isVertical()); resized(); } - else if (button == customiseButton) + else if (button == &customiseButton) { - toolbar->showCustomisationDialog (factory); + toolbar.showCustomisationDialog (factory); } } private: - Toolbar* toolbar; - Slider* depthSlider; - TextButton* orientationButton; - TextButton* customiseButton; + Toolbar toolbar; + Slider depthSlider; + Label depthLabel, infoLabel; + TextButton orientationButton, customiseButton; //============================================================================== class DemoToolbarItemFactory : public ToolbarItemFactory @@ -797,35 +814,16 @@ private: { switch (itemId) { - case doc_new: - return createButtonFromZipFileSVG (itemId, "new", "document-new.svg"); - - case doc_open: - return createButtonFromZipFileSVG (itemId, "open", "document-open.svg"); - - case doc_save: - return createButtonFromZipFileSVG (itemId, "save", "document-save.svg"); - - case doc_saveAs: - return createButtonFromZipFileSVG (itemId, "save as", "document-save-as.svg"); - - case edit_copy: - return createButtonFromZipFileSVG (itemId, "copy", "edit-copy.svg"); - - case edit_cut: - return createButtonFromZipFileSVG (itemId, "cut", "edit-cut.svg"); - - case edit_paste: - return createButtonFromZipFileSVG (itemId, "paste", "edit-paste.svg"); - - case juceLogoButton: - return new ToolbarButton (itemId, "juce!", Drawable::createFromImageData (BinaryData::juce_png, BinaryData::juce_pngSize), 0); - - case customComboBox: - return new CustomToolbarComboBox (itemId); - - default: - break; + case doc_new: return createButtonFromZipFileSVG (itemId, "new", "document-new.svg"); + case doc_open: return createButtonFromZipFileSVG (itemId, "open", "document-open.svg"); + case doc_save: return createButtonFromZipFileSVG (itemId, "save", "document-save.svg"); + case doc_saveAs: return createButtonFromZipFileSVG (itemId, "save as", "document-save-as.svg"); + case edit_copy: return createButtonFromZipFileSVG (itemId, "copy", "edit-copy.svg"); + case edit_cut: return createButtonFromZipFileSVG (itemId, "cut", "edit-cut.svg"); + case edit_paste: return createButtonFromZipFileSVG (itemId, "paste", "edit-paste.svg"); + case juceLogoButton: return new ToolbarButton (itemId, "juce!", Drawable::createFromImageData (BinaryData::juce_png, BinaryData::juce_pngSize), 0); + case customComboBox: return new CustomToolbarComboBox (itemId); + default: break; } return 0; @@ -867,20 +865,20 @@ private: { public: CustomToolbarComboBox (const int toolbarItemId) - : ToolbarItemComponent (toolbarItemId, "Custom Toolbar Item", false) + : ToolbarItemComponent (toolbarItemId, "Custom Toolbar Item", false), + comboBox ("demo toolbar combo box") { - addAndMakeVisible (comboBox = new ComboBox ("demo toolbar combo box")); + addAndMakeVisible (&comboBox); for (int i = 1; i < 20; ++i) - comboBox->addItem ("Toolbar ComboBox item " + String (i), i); + comboBox.addItem ("Toolbar ComboBox item " + String (i), i); - comboBox->setSelectedId (1); - comboBox->setEditableText (true); + comboBox.setSelectedId (1); + comboBox.setEditableText (true); } ~CustomToolbarComboBox() { - delete comboBox; } bool getToolbarItemSizes (int /*toolbarDepth*/, bool isToolbarVertical, @@ -901,14 +899,14 @@ private: void contentAreaChanged (const Rectangle& contentArea) { - comboBox->setSize (contentArea.getWidth() - 2, - jmin (contentArea.getHeight() - 2, 22)); + comboBox.setSize (contentArea.getWidth() - 2, + jmin (contentArea.getHeight() - 2, 22)); - comboBox->setCentrePosition (contentArea.getCentreX(), contentArea.getCentreY()); + comboBox.setCentrePosition (contentArea.getCentreX(), contentArea.getCentreY()); } private: - ComboBox* comboBox; + ComboBox comboBox; }; }; @@ -1117,59 +1115,52 @@ const int numGroups = 4; class WidgetsDemo : public Component, public ButtonListener { - TextButton* menuButton; - ToggleButton* enableButton; - - DemoTabbedComponent* tabs; - public: //============================================================================== WidgetsDemo() + : menuButton ("click for a popup menu..", + "click for a demo of the different types of item you can put into a popup menu..."), + enableButton ("enable/disable components") { setName ("Widgets"); - addAndMakeVisible (tabs = new DemoTabbedComponent()); + addAndMakeVisible (&tabs); //============================================================================== - menuButton = new TextButton ("click for a popup menu..", - "click for a demo of the different types of item you can put into a popup menu..."); - - addAndMakeVisible (menuButton); - menuButton->setBounds (10, 10, 200, 24); - menuButton->addButtonListener (this); - menuButton->setTriggeredOnMouseDown (true); // because this button pops up a menu, this lets us + addAndMakeVisible (&menuButton); + menuButton.setBounds (10, 10, 200, 24); + menuButton.addButtonListener (this); + menuButton.setTriggeredOnMouseDown (true); // because this button pops up a menu, this lets us // hold down the button and drag straight onto the menu //============================================================================== - enableButton = new ToggleButton ("enable/disable components"); - addAndMakeVisible (enableButton); - enableButton->setBounds (230, 10, 180, 24); - enableButton->setTooltip ("toggle button"); - enableButton->setToggleState (true, false); - enableButton->addButtonListener (this); + addAndMakeVisible (&enableButton); + enableButton.setBounds (230, 10, 180, 24); + enableButton.setTooltip ("toggle button"); + enableButton.setToggleState (true, false); + enableButton.addButtonListener (this); } ~WidgetsDemo() { - deleteAllChildren(); } void resized() { - tabs->setBounds (10, 40, getWidth() - 20, getHeight() - 50); + tabs.setBounds (10, 40, getWidth() - 20, getHeight() - 50); } //============================================================================== void buttonClicked (Button* button) { - if (button == enableButton) + if (button == &enableButton) { - const bool enabled = enableButton->getToggleState(); + const bool enabled = enableButton.getToggleState(); - menuButton->setEnabled (enabled); - tabs->setEnabled (enabled); + menuButton.setEnabled (enabled); + tabs.setEnabled (enabled); } - else if (button == menuButton) + else if (button == &menuButton) { PopupMenu m; m.addItem (1, "Normal item"); @@ -1182,10 +1173,10 @@ public: m.addSeparator(); PopupMenu tabsMenu; - tabsMenu.addItem (1001, "Show tabs at the top", true, tabs->getOrientation() == TabbedButtonBar::TabsAtTop); - tabsMenu.addItem (1002, "Show tabs at the bottom", true, tabs->getOrientation() == TabbedButtonBar::TabsAtBottom); - tabsMenu.addItem (1003, "Show tabs at the left", true, tabs->getOrientation() == TabbedButtonBar::TabsAtLeft); - tabsMenu.addItem (1004, "Show tabs at the right", true, tabs->getOrientation() == TabbedButtonBar::TabsAtRight); + tabsMenu.addItem (1001, "Show tabs at the top", true, tabs.getOrientation() == TabbedButtonBar::TabsAtTop); + tabsMenu.addItem (1002, "Show tabs at the bottom", true, tabs.getOrientation() == TabbedButtonBar::TabsAtBottom); + tabsMenu.addItem (1003, "Show tabs at the left", true, tabs.getOrientation() == TabbedButtonBar::TabsAtLeft); + tabsMenu.addItem (1004, "Show tabs at the right", true, tabs.getOrientation() == TabbedButtonBar::TabsAtRight); m.addSubMenu ("Tab position", tabsMenu); m.addSeparator(); @@ -1238,7 +1229,7 @@ public: m.addSubMenu ("File chooser dialogs", fileChoosers); - int result = m.showAt (menuButton); + int result = m.showAt (&menuButton); if (result != 0) { @@ -1414,23 +1405,28 @@ public: } else if (result == 1001) { - tabs->setOrientation (TabbedButtonBar::TabsAtTop); + tabs.setOrientation (TabbedButtonBar::TabsAtTop); } else if (result == 1002) { - tabs->setOrientation (TabbedButtonBar::TabsAtBottom); + tabs.setOrientation (TabbedButtonBar::TabsAtBottom); } else if (result == 1003) { - tabs->setOrientation (TabbedButtonBar::TabsAtLeft); + tabs.setOrientation (TabbedButtonBar::TabsAtLeft); } else if (result == 1004) { - tabs->setOrientation (TabbedButtonBar::TabsAtRight); + tabs.setOrientation (TabbedButtonBar::TabsAtRight); } } } } + +private: + TextButton menuButton; + ToggleButton enableButton; + DemoTabbedComponent tabs; };