Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIMOB-8835: BlackBerry: Implement UI.Switch #58

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions apidoc/Titanium/UI/Switch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ description: |
On Mobile Web, a switch always has text associated with it, and appears as a
toggle button, similar to Android.

On BlackBerry, a switch is a fixed-size switch control with two states: checked or unchecked.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description should be more along the lines of what is done for the other platform. Specifically for the switch, the main difference among platforms seems to be the support for associated text with the switch.

So the description needs to cover that, does the BB toggle button support associated text like android and mobileweb or does it not, like iOS?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


Use the <Titanium.UI.createSwitch> method to create a switch.
extends: Titanium.UI.View
since: "0.8"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current state of the switch doc doesn't seem to match the implementation of this patch. Unless i'm missing something it doesn't seem like the enabled property is implemented, and as such should list all platforms but blackberry in this yml file.

Are there no other properties that can currently be supported by cascades?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added platforms section for 'enable' property. No, there is only 'checked' property available.

Expand Down
9 changes: 9 additions & 0 deletions blackberry/tibb/NativeContainerObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <bb/cascades/Slider>
#include <bb/cascades/Stacklayout>
#include <bb/cascades/TextField>
#include <bb/cascades/ToggleButton>
#include <bb/cascades/ActivityIndicator>
#include <qtgui/QColor>

Expand Down Expand Up @@ -137,6 +138,14 @@ int NativeContainerObject::addChildNativeObject(NativeObject* obj)

}

case N_TYPE_TOGGLEBUTTON:

{
bb::cascades::ToggleButton* toggleButton = (bb::cascades::ToggleButton*) obj->getNativeHandle();
container_->add(toggleButton);
return NATIVE_ERROR_OK;
}

}

return NATIVE_ERROR_NOTSUPPORTED;
Expand Down
1 change: 1 addition & 0 deletions blackberry/tibb/NativeObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef void* NAHANDLE;
#define N_TYPE_IMAGEVIEW 7
#define N_TYPE_TEXT_FIELD 8
#define N_TYPE_ACTIVITYINDICATOR 9
#define N_TYPE_TOGGLEBUTTON 10

enum NATIVE_PROP
{
Expand Down
5 changes: 5 additions & 0 deletions blackberry/tibb/NativeObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "NativeTextFieldObject.h"
#include "NativeImageViewObject.h"
#include "NativeActivityIndicatorObject.h"
#include "NativeToggleButtonObject.h"
#include <bb/cascades/Container>

using namespace bb::cascades;
Expand Down Expand Up @@ -75,6 +76,10 @@ NativeObject* NativeObjectFactory::createNativeObject(int type)
obj = NativeActivityIndicatorObject::createActivityIndicator();
break;

case N_TYPE_TOGGLEBUTTON:
obj = NativeToggleButtonObject::createToggleButton();
break;

}
if (obj != NULL)
{
Expand Down
71 changes: 71 additions & 0 deletions blackberry/tibb/NativeToggleButtonObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#include "NativeToggleButtonObject.h"
#include "TiCascadesEventHandler.h"
#include "TiEventContainerFactory.h"
#include <bb/cascades/ToggleButton>

NativeToggleButtonObject::NativeToggleButtonObject()
{
toggleButton_ = NULL;
}

NativeToggleButtonObject::~NativeToggleButtonObject()
{
}

NativeToggleButtonObject* NativeToggleButtonObject::createToggleButton()
{
return new NativeToggleButtonObject();
}

int NativeToggleButtonObject::getObjectType() const
{
return N_TYPE_TOGGLEBUTTON;
}

int NativeToggleButtonObject::initialize(TiEventContainerFactory* containerFactory)
{
toggleButton_ = bb::cascades::ToggleButton::create();
setControl(toggleButton_);
eventStateChanged_ = containerFactory->createEventContainer();
eventHandler_ = new TiCascadesEventHandler(eventStateChanged_);
return NATIVE_ERROR_OK;
}

NAHANDLE NativeToggleButtonObject::getNativeHandle() const
{
return toggleButton_;
}

int NativeToggleButtonObject::setValue(TiObject* value)
{
bool checked;
int error = NativeControlObject::getBoolean(value, &checked);
if (!N_SUCCEEDED(error))
{
return error;
}
toggleButton_->setChecked(checked);
return NATIVE_ERROR_OK;
}

int NativeToggleButtonObject::setEventHandler(const char* eventName, TiEvent* event)
{
if (strcmp(eventName, "change") == 0)
{
eventStateChanged_->addListener(event);
}
return NATIVE_ERROR_NOTSUPPORTED;
}

void NativeToggleButtonObject::completeInitialization()
{
NativeControlObject::completeInitialization();
QObject::connect(toggleButton_, SIGNAL(checkedChanged(bool checked)), eventHandler_, SLOT(checkedChanged(bool checked)));
}
56 changes: 56 additions & 0 deletions blackberry/tibb/NativeToggleButtonObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#ifndef NATIVETOGGLEBUTTONOBJECT_H_
#define NATIVETOGGLEBUTTONOBJECT_H_

#include "NativeControlObject.h"

//forward declaration
namespace bb
{
namespace cascades
{
class ToggleButton;
}
}

class TiEventContainer;
class TiCascadesEventHandler;

/*
* NativeToggleButtonObject
*
* UI: Toggle Button
*/

class NativeToggleButtonObject : public NativeControlObject
{
public:
static NativeToggleButtonObject* createToggleButton();
virtual int getObjectType() const;
virtual int initialize(TiEventContainerFactory* containerFactory);
virtual NAHANDLE getNativeHandle() const;
virtual int setEventHandler(const char* eventName, TiEvent* event);
virtual void completeInitialization();
virtual int setValue(TiObject* value);

protected:
virtual ~NativeToggleButtonObject();

private:
explicit NativeToggleButtonObject();
// Disable copy ctor & assignment operator
NativeToggleButtonObject(const NativeToggleButtonObject& toggleButton);
NativeToggleButtonObject& operator=(const NativeToggleButtonObject& toggleButton);
bb::cascades::ToggleButton* toggleButton_;

TiEventContainer* eventStateChanged_;
TiCascadesEventHandler* eventHandler_;
};

#endif /* NATIVETOGGLEBUTTONOBJECT_H_ */
20 changes: 20 additions & 0 deletions blackberry/tibb/TiUIObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "TiUIImageView.h"
#include "TiUITextField.h"
#include "TiUIActivityIndicator.h"
#include "TiUISwitch.h"
#include <string.h>

TiUIObject::TiUIObject()
Expand Down Expand Up @@ -54,6 +55,7 @@ void TiUIObject::onCreateStaticMembers()
TiGenericFunctionObject::addGenericFunctionToParent(this, "createTextField", this, createTextField_);
TiGenericFunctionObject::addGenericFunctionToParent(this, "createImageView", this, createImageView_);
TiGenericFunctionObject::addGenericFunctionToParent(this, "createActivityIndicator", this, createActivityIndicator_);
TiGenericFunctionObject::addGenericFunctionToParent(this, "createSwitch", this, createSwitch_);
}

Handle<Value> TiUIObject::createTabGroup_(void* userContext, TiObject* caller, const Arguments& args)
Expand Down Expand Up @@ -205,3 +207,21 @@ Handle<Value> TiUIObject::createActivityIndicator_(void* userContext, TiObject*
setTiObjectToJsObject(result, activityIndicator);
return handleScope.Close(result);
}

Handle<Value> TiUIObject::createSwitch_(void* userContext, TiObject* caller, const Arguments& args)
{
HandleScope handleScope;
TiUIObject* obj = (TiUIObject*) userContext;
Handle<ObjectTemplate> global = getObjectTemplateFromJsObject(args.Holder());
Handle<Object> result;
result = global->NewInstance();
TiUISwitch* switchObj = TiUISwitch::createSwitch(obj->objectFactory_);
switchObj->setValue(result);
if ((args.Length() > 0) && (args[0]->IsObject()))
{
Local<Object> settingsObj = Local<Object>::Cast(args[0]);
switchObj->setParametersFromObject(settingsObj);
}
setTiObjectToJsObject(result, switchObj);
return handleScope.Close(result);
}
1 change: 1 addition & 0 deletions blackberry/tibb/TiUIObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TiUIObject : public TiObject
static Handle<Value> createImageView_(void* userContext, TiObject* caller, const Arguments& args);
static Handle<Value> createTextField_(void* userContext, TiObject* caller, const Arguments& args);
static Handle<Value> createActivityIndicator_(void* userContext, TiObject* caller, const Arguments& args);
static Handle<Value> createSwitch_(void* userContext, TiObject* caller, const Arguments& args);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not all your code, but could you make the change to use the prefix underscore instead of the suffix underscore in TiUIObject class please?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

NativeObjectFactory* objectFactory_;
NativeObject* contentContainer_;
};
Expand Down
35 changes: 35 additions & 0 deletions blackberry/tibb/TiUISwitch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#include "TiUISwitch.h"

TiUISwitch::TiUISwitch(NativeObjectFactory* nativeObjectFactory)
: TiUIBase(nativeObjectFactory, "")
{
}

TiUISwitch::~TiUISwitch()
{
}

TiUISwitch* TiUISwitch::createSwitch(NativeObjectFactory* nativeObjectFactory)
{
TiUISwitch* obj = new TiUISwitch(nativeObjectFactory);
obj->initializeTiObject(NULL);
return obj;
}

void TiUISwitch::initializeTiObject(TiObject* parentContext)
{
if (!isInitialized())
{
TiUIBase::initializeTiObject(parentContext);
NativeObject* obj = getNativeObjectFactory()->createNativeObject(N_TYPE_TOGGLEBUTTON);
setNativeObject(obj);
obj->release();
}
}
39 changes: 39 additions & 0 deletions blackberry/tibb/TiUISwitch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#ifndef TIUISWITCH_H_
#define TIUISWITCH_H_

#include "TiUIBase.h"

/*
* TiUISwitch
*
* Represents a Titanium Switch object
*
* This object can be created by:
* Titanium.UI.createSwitch({...});
*/
class TiUISwitch : public TiUIBase
{
public:
static TiUISwitch* createSwitch(NativeObjectFactory* nativeObjectFactory);

protected:
virtual ~TiUISwitch();
virtual void initializeTiObject(TiObject* parentContext);

private:
TiUISwitch(NativeObjectFactory* nativeObjectFactory);

// Disable copy ctor & assignment operator
TiUISwitch(const TiUISwitch& switchObj);
TiUISwitch& operator=(const TiUISwitch& switchObj);
};


#endif /* TIUISWITCH_H_ */