Adding a new class to JUCE API


#1

Hi

I am trying to add a new class (called UIViewComponent precisely) to Juce. I downloaded it from git repositery.

But when I compile the Juce iPhone project i have got this :

Actually, i add those lines in juce_mac_nativecode.mm

And those in juce_app_config.h

#ifndef __JUCE_UIVIEWCOMPONENT_JUCEHEADER__ #include "gui/components/special/juce_UIViewComponent.h" #endif

Actually, UIViewComponent.h and .mm are very closed to NSViewComponent.

Here they are :

Source/gui/components/special/juce_UIViewComponent.h

[code]#ifndef JUCE_UIVIEWCOMPONENT_JUCEHEADER
#define JUCE_UIVIEWCOMPONENT_JUCEHEADER

#include “…/juce_Component.h”
#if ! DOXYGEN
class UIViewComponentInternal;
#endif

#if JUCE_MAC || DOXYGEN

//==============================================================================
/**
A Mac-specific class that can create and embed an UIView inside itself.

To use it, create one of these, put it in place and make sure it’s visible in a
window, then use setView() to assign an UIView to it. The view will then be
moved and resized to follow the movements of this component.

Of course, since the view is a native object, it’ll obliterate any
juce components that may overlap this component, but that’s life.
/
class JUCE_API UIViewComponent : public Component
{
public:
//==============================================================================
/
* Create an initially-empty container. */
UIViewComponent();

/** Destructor. */
~UIViewComponent();

/** Assigns an UIView to this peer.

The view will be retained and released by this component for as long as
it is needed. To remove the current view, just call setView (0).

Note: a void* is used here to avoid including the cocoa headers as
part of the juce.h, but the method expects an UIView*.
/
void setView (void
UIView);

/** Returns the current UIView.

Note: a void* is returned here to avoid including the cocoa headers as
a requirement of juce.h, so you should just cast the object to an UIView*.
/
void
getView() const;

//==============================================================================
/** @internal */
void paint (Graphics& g);

juce_UseDebuggingNewOperator

private:
friend class UIViewComponentInternal;
ScopedPointer info;

UIViewComponent (const UIViewComponent&);
UIViewComponent& operator= (const UIViewComponent&);
};

#endif

#endif // JUCE_UIVIEWCOMPONENT_JUCEHEADER
[/code]

Source/native/mac/UIViewComponent.mm

[code]/*

This file is part of the JUCE library - "Jules’ Utility Class Extensions"
Copyright 2004-10 by Raw Material Software Ltd.


JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.

JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.


To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.

==============================================================================
*/

// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

//==============================================================================
class UIViewComponentInternal : public ComponentMovementWatcher
{
Component* const owner;
UIViewComponentPeer* currentPeer;
bool wasShowing;

public:
UIView* const view;

//==============================================================================
UIViewComponentInternal (UIView* const view_, Component* const owner_)
: ComponentMovementWatcher (owner_),
owner (owner_),
currentPeer (0),
wasShowing (false),
view (view_)
{
[view_ retain];

if (owner_->isShowing())
  componentPeerChanged();

}

~UIViewComponentInternal()
{
[view removeFromSuperview];
[view release];
}

//==============================================================================
void componentMovedOrResized (Component& comp, bool wasMoved, bool wasResized)
{
ComponentMovementWatcher::componentMovedOrResized (comp, wasMoved, wasResized);

// The ComponentMovementWatcher version of this method avoids calling
// us when the top-level comp is resized, but for an UIView we need to know this
// because with inverted co-ords, we need to update the position even if the
// top-left pos hasn't changed
if (comp.isOnDesktop() && wasResized)
  componentMovedOrResized (wasMoved, wasResized);

}

void componentMovedOrResized (bool /wasMoved/, bool /wasResized/)
{
Component* const topComp = owner->getTopLevelComponent();

if (topComp->getPeer() != 0)
{
  const Point<int> pos (owner->relativePositionToOtherComponent (topComp, Point<int>()));
  
  CGRect r;
  r.origin.x = (float) pos.getX();
  r.origin.y = (float) pos.getY();
  r.size.width = (float) owner->getWidth();
  r.size.height = (float) owner->getHeight();
  r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
  
  [view setFrame: r];
}

}

void componentPeerChanged()
{
UIViewComponentPeer* const peer = dynamic_cast <UIViewComponentPeer*> (owner->getPeer());

if (currentPeer != peer)
{
  [view removeFromSuperview];
  currentPeer = peer;
  
  if (peer != 0)
  {
    [peer->view addSubview: view];
    componentMovedOrResized (false, false);
  }
}

[view setHidden: ! owner->isShowing()];

}

void componentVisibilityChanged (Component&)
{
componentPeerChanged();
}

juce_UseDebuggingNewOperator

private:
UIViewComponentInternal (const UIViewComponentInternal&);
UIViewComponentInternal& operator= (const UIViewComponentInternal&);
};

//==============================================================================
UIViewComponent::UIViewComponent()
{
}

UIViewComponent::~UIViewComponent()
{
}

void UIViewComponent::setView (void* view)
{
if (view != getView())
{
if (view != 0)
info = new UIViewComponentInternal ((UIView*) view, this);
else
info = 0;
}
}

void* UIViewComponent::getView() const
{
return info == 0 ? 0 : info->view;
}

void UIViewComponent::paint (Graphics&)
{
}

#endif
[/code]

I really don’t understand what is missing.

Thanks.


#2

Ok. It was here :

Source/gui/components/special/juce_UIViewComponent.h


#3

Ah, you beat me to that answer by about a second!


#4

Too bad Jules :wink:


#5

hi,

I thought this one was merged into juce but it didn’t.

here’s the git patch (with my email removed):

[code]From f199a6785a217208578709aacd4dc1620c661f6f Mon Sep 17 00:00:00 2001
Date: Wed, 10 Aug 2011 17:15:02 +0200
Subject: [PATCH] add UIViewComponent


src/gui/components/special/juce_UIViewComponent.h | 92 +++++++++++++
src/juce_app_includes.h | 3 +
src/native/mac/juce_ios_UIViewComponent.mm | 146 +++++++++++++++++++++
src/native/mac/juce_mac_NativeCode.mm | 2 +
4 files changed, 243 insertions(+), 0 deletions(-)
create mode 100644 src/gui/components/special/juce_UIViewComponent.h
create mode 100644 src/native/mac/juce_ios_UIViewComponent.mm

diff --git a/src/gui/components/special/juce_UIViewComponent.h b/src/gui/components/special/juce_UIViewComponent.h
new file mode 100644
index 0000000…9a838f7
— /dev/null
+++ b/src/gui/components/special/juce_UIViewComponent.h
@@ -0,0 +1,92 @@
+/*

  • ==============================================================================
  • This file is part of the JUCE library - “Jules’ Utility Class Extensions”
  • Copyright 2004-11 by Raw Material Software Ltd.

  • JUCE can be redistributed and/or modified under the terms of the GNU General
  • Public License (Version 2), as published by the Free Software Foundation.
  • A copy of the license is included in the JUCE distribution, or can be found
  • online at www.gnu.org/licenses.
  • JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  • WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  • A PARTICULAR PURPOSE. See the GNU General Public License for more details.

  • To release a closed-source product which uses JUCE, commercial licenses are
  • available: visit www.rawmaterialsoftware.com/juce for more information.
  • ==============================================================================
    +*/

+#ifndef JUCE_UIVIEWCOMPONENT_JUCEHEADER
+#define JUCE_UIVIEWCOMPONENT_JUCEHEADER
+
+#include “…/juce_Component.h”
+#if ! DOXYGEN

  • class UIViewComponentInternal;
    +#endif

+#if JUCE_IOS || DOXYGEN
+
+//==============================================================================
+/**

  • An iOS-specific class that can create and embed an UIView inside itself.
  • To use it, create one of these, put it in place and make sure it’s visible in a
  • window, then use setView() to assign an UIView to it. The view will then be
  • moved and resized to follow the movements of this component.
  • Of course, since the view is a native object, it’ll obliterate any
  • juce components that may overlap this component, but that’s life.
    +*/
    +class JUCE_API UIViewComponent : public Component
    +{
    +public:
  • //==============================================================================
  • /** Create an initially-empty container. */
  • UIViewComponent();
  • /** Destructor. */
  • ~UIViewComponent();
  • /** Assigns an UIView to this peer.
  •    The view will be retained and released by this component for as long as
    
  •    it is needed. To remove the current view, just call setView (nullptr).
    
  •    Note: a void* is used here to avoid including the cocoa headers as
    
  •    part of the juce.h, but the method expects an NSView*.
    
  • */
  • void setView (void* nsView);
  • /** Returns the current NSView.
  •    Note: a void* is returned here to avoid including the cocoa headers as
    
  •    a requirement of juce.h, so you should just cast the object to an NSView*.
    
  • */
  • void* getView() const;
  • /** Resizes this component to fit the view that it contains. */
  • void resizeToFitView();
  • //==============================================================================
  • /** @internal */
  • void paint (Graphics& g);

+private:

  • friend class UIViewComponentInternal;
  • ScopedPointer info;
  • JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIViewComponent);
    +};

+#endif
+
+#endif // JUCE_UIVIEWCOMPONENT_JUCEHEADER
diff --git a/src/juce_app_includes.h b/src/juce_app_includes.h
index fe0b506…f3590e8 100644
— a/src/juce_app_includes.h
+++ b/src/juce_app_includes.h
@@ -603,6 +603,9 @@
#ifndef JUCE_NSVIEWCOMPONENT_JUCEHEADER
#include “gui/components/special/juce_NSViewComponent.h”
#endif
+#ifndef JUCE_UIVIEWCOMPONENT_JUCEHEADER
+#include “gui/components/special/juce_UIViewComponent.h”
+#endif
#ifndef JUCE_OPENGLCOMPONENT_JUCEHEADER
#include “gui/components/special/juce_OpenGLComponent.h”
#endif
diff --git a/src/native/mac/juce_ios_UIViewComponent.mm b/src/native/mac/juce_ios_UIViewComponent.mm
new file mode 100644
index 0000000…2a9a8a1
— /dev/null
+++ b/src/native/mac/juce_ios_UIViewComponent.mm
@@ -0,0 +1,146 @@
+/*

  • ==============================================================================
  • This file is part of the JUCE library - “Jules’ Utility Class Extensions”
  • Copyright 2004-11 by Raw Material Software Ltd.

  • JUCE can be redistributed and/or modified under the terms of the GNU General
  • Public License (Version 2), as published by the Free Software Foundation.
  • A copy of the license is included in the JUCE distribution, or can be found
  • online at www.gnu.org/licenses.
  • JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  • WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  • A PARTICULAR PURPOSE. See the GNU General Public License for more details.

  • To release a closed-source product which uses JUCE, commercial licenses are
  • available: visit www.rawmaterialsoftware.com/juce for more information.
  • ==============================================================================
    +*/

+// (This file gets included by juce_mac_NativeCode.mm, rather than being
+// compiled on its own).
+#if JUCE_INCLUDED_FILE
+
+//==============================================================================
+class UIViewComponentInternal : public ComponentMovementWatcher
+{
+public:

  • //==============================================================================
  • UIViewComponentInternal (UIView* const view_, Component& owner_)
  •    : ComponentMovementWatcher (&owner_),
    
  •      owner (owner_),
    
  •      currentPeer (nullptr),
    
  •      view (view_)
    
  • {
  •    [view_ retain];
    
  •    if (owner.isShowing())
    
  •        componentPeerChanged();
    
  • }
  • ~UIViewComponentInternal()
  • {
  •    [view removeFromSuperview];
    
  •    [view release];
    
  • }
  • void componentMovedOrResized (bool /wasMoved/, bool /wasResized/)
  • {
  •    Component* const topComp = owner.getTopLevelComponent();
    
  •    if (topComp->getPeer() != nullptr)
    
  •    {
    
  •        const Point<int> pos (topComp->getLocalPoint (&owner, Point<int>()));
    
  •        CGRect r = CGRectMake ((float) pos.getX(), (float) pos.getY(), (float) owner.getWidth(), (float) owner.getHeight());
    
  •        [view setFrame: r];
    
  •    }
    
  • }
  • void componentPeerChanged()
  • {
  •    UIViewComponentPeer* const peer = dynamic_cast <UIViewComponentPeer*> (owner.getPeer());
    
  •    if (currentPeer != peer)
    
  •    {
    
  •        if ([view superview] != nil)
    
  •            [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
    
  •                                        // override the call and use it as a sign that they're being deleted, which breaks everything..
    
  •        currentPeer = peer;
    
  •        if (peer != nullptr)
    
  •        {
    
  •            [peer->view addSubview: view];
    
  •            componentMovedOrResized (false, false);
    
  •        }
    
  •    }
    
  •    [view setHidden: ! owner.isShowing()];
    
  • }
  • void componentVisibilityChanged()
  • {
  •    componentPeerChanged();
    
  • }
  • Rectangle getViewBounds() const
  • {
  •    CGRect r = [view frame];
    
  •    return Rectangle<int> (0, 0, (int) r.size.width, (int) r.size.height);
    
  • }

+private:

  • Component& owner;
  • UIViewComponentPeer* currentPeer;

+public:

  • UIView* const view;

+private:

  • JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIViewComponentInternal);
    +};

+//==============================================================================
+UIViewComponent::UIViewComponent()
+{
+}
+
+UIViewComponent::~UIViewComponent()
+{
+}
+
+void UIViewComponent::setView (void* view)
+{

  • if (view != getView())
  • {
  •    if (view != nullptr)
    
  •        info = new UIViewComponentInternal ((UIView*) view, *this);
    
  •    else
    
  •        info = nullptr;
    
  • }
    +}

+void* UIViewComponent::getView() const
+{

  • return info == nullptr ? nullptr : info->view;
    +}

+void UIViewComponent::resizeToFitView()
+{

  • if (info != nullptr)
  •    setBounds (info->getViewBounds());
    

+}
+
+void UIViewComponent::paint (Graphics&)
+{
+}
+
+#endif
diff --git a/src/native/mac/juce_mac_NativeCode.mm b/src/native/mac/juce_mac_NativeCode.mm
index 242727b…54d72a8 100644
— a/src/native/mac/juce_mac_NativeCode.mm
+++ b/src/native/mac/juce_mac_NativeCode.mm
@@ -83,6 +83,7 @@ BEGIN_JUCE_NAMESPACE
#include “…/…/gui/components/mouse/juce_MouseInputSource.h”
#include “…/…/gui/components/keyboard/juce_KeyPressMappingSet.h”
#include “…/…/gui/components/special/juce_NSViewComponent.h”
+#include “…/…/gui/components/special/juce_UIViewComponent.h”
#include “…/…/gui/components/layout/juce_ComponentMovementWatcher.h”
#include “…/…/gui/components/special/juce_WebBrowserComponent.h”
#include “…/…/gui/components/filebrowser/juce_FileChooser.h”
@@ -130,6 +131,7 @@ BEGIN_JUCE_NAMESPACE
#include “juce_mac_WebBrowserComponent.mm”
#include “juce_ios_Audio.cpp”
#include “juce_mac_CoreMidi.cpp”

  • #include “juce_ios_UIViewComponent.mm”
    #else
    #include “juce_mac_MessageManager.mm”
    #include “juce_mac_Fonts.mm” // (must go before juce_mac_CoreGraphicsContext.mm)

    1.7.3.4
    [/code]

#6

Ok, thanks - I’ll add that to the new modularised version…


#7

So wait…is there a new repo? All I see is the one last updated July 15. Is the Jucequake over?


#8

Sorry, I just meant that I’ll add it when I finally get the new stuff checked-in. Nearly there! I’m still just ironing out the last few minor niggles, which is taking a bit longer than expected.


#9

but the main question is, can i just replace the old version with the new modularised, or do we have to change a lot of code,
wait and see :wink: