bada hello everybody
TRANSCRIPT
-
8/4/2019 Bada Hello Everybody
1/18
Hello everybody!
Today a friend of mine started experimenting with one of the latest versions of badas SDK and IDE.
Samsung shortly will release the SDK/IDE for public use. The versions he has, though, may differ slightly
to those released at a later date, therefore it is thinkable that you might encounter slight inconsistencies.
The idea was to go somewhat beyond a simple hello world app and test badas IDE as well as its GUI
building tool. We want to create a simple greeting app that asks your name and responds with a simple
greeting. Something that looks like this:
Having installed the SDK/IDE and after re-starting the computer we start with an empty workspace in the
IDE
-
8/4/2019 Bada Hello Everybody
2/18
Go to File->New->Bada Application Project, this will open a wizard. Type SimpleGreeting for the
projects name and select bada Application as the projects type. The wizard will create a new project for
you.
-
8/4/2019 Bada Hello Everybody
3/18
For now we only need to open the files SimpleGreeting.cpp and SimpleGreeting.h that hold the
definitions and declarations for the bare-bone sample project. Next let us create a form for the app. In the
resource window (left bottom corner on the screenshot), right click Form->Insert Resource.
This will create a new emtpy form and open the GUI editor.
-
8/4/2019 Bada Hello Everybody
4/18
Here we need to add three controls to the form: an input box, a button and a label. We start with the input
box by clicking on Edit Field in the Palette and dragging a box at the top of the empty form.
Double click the edit field and youll see the properties tab coming to front underneath. There change the id
to NAME_FIELD. Next draw a button below the edit field and a label below the button. In the same
way change the id of the button to HELLO_BUTTON and the id of the label to
GREETING_LABEL. Besides, in the properties of the button, for the text property insert something
meaningful like Say Hello! . Now we are ready with the form!
-
8/4/2019 Bada Hello Everybody
5/18
Lets go to the SimpleGreeting.h header file. Here you will have to do a few things. First, lets define a
constant that we will use afterwards to identify the action event of the button. Do it right after the using-
statements of all the bada namespaces.
?01
02
03
04
05
06
07
08
09
10
11
usingnamespaceOsp::App;
usingnamespaceOsp::Base;
usingnamespaceOsp::Graphics;
usingnamespaceOsp::Io;
usingnamespaceOsp::Locales;
usingnamespaceOsp::System;
usingnamespaceOsp::Text;
usingnamespaceOsp::Ui;
usingnamespaceOsp::Ui::Controls;
staticconstintBUTTON_ACTION_GREET = 1;
Then, since c++ allows us multiple inheritance and we want to keep things simple and straight forward, let
us implement the IActionEventListener directly with our applications class. This will allow our app class
to take care of the events all by itself without the need to provide a separate listener (we are lazy today
). Further let us define a few private variables for the controls we use, so that we can reference them later in
our code.?
01
02
03
04
/**
* [SimpleGreeting] application must inherit from Application class
* which provides basic features necessary to define an application.
*/
-
8/4/2019 Bada Hello Everybody
6/18
05
06
07
08
09
10
11
12
13
classSimpleGreeting :
publicApplication, publicIActionEventListener
{
private:
Frame *pFrame;
Form *pForm;
Button *pButton;
Label *pLabel;
EditField *pField;
Finally declare a handler function at the end that will handle the action events later:
?
01
02
03
04
05
06
07
08
09
10
11
12
/**
* Called when the battery level changes.
*/
voidOnBatteryLevelChanged(BatteryLevel batteryLevel);
/**
* Called on Actions
*/
virtualvoidOnActionPerformed(constOsp::Ui::Control& source, intactionId);
};
#endif
Well We are ready with the header file. Lets switch to the SimpleGreeting.cpp . Here we need to bind
the variables for controls we declared earlier in the header to the actual objects we designed in the GUI
editor + the object for the apps frame (look into the tutorials for UI: each app has a single frame which
holds all forms and controls and stuff within it). Lets put it all into the OnAppInitializing function.
?01
02
03
04
bool
SimpleGreeting::OnAppInitializing(AppRegistry& appRegistry)
{
// get the frame of the app
-
8/4/2019 Bada Hello Everybody
7/18
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
pFrame = GetAppFrame()->GetFrame();
// bind pForm to the Form we designed in resources
pForm = newForm();
pForm->Construct("IDF_FORM1");
// add the form as child to the app's frame and set it as current
pFrame->AddControl(*pForm);
pFrame->SetCurrentForm(*pForm);
// bind name input field
pField = (EditField*)pForm->GetControl("NAME_FIELD", true);
pField->SetText("Your name in here!");
// bind label
pLabel = (Label*)pForm->GetControl("GREETING_LABEL", true);
// bind button, set its action id
// and the action listener (this app's class itself)
pButton = (Button*)pForm->GetControl("HELLO_BUTTON", true);
pButton->SetActionId(BUTTON_ACTION_GREET);
pButton->AddActionEventListener(*this);
returntrue;
}
Now the events sent by the button clicks will be forwarded to this apps class directly to the function youll
need to add at the end of this file:
?
01
02
03
void
SimpleGreeting::OnActionPerformed(const Osp::Ui::Control& source, intactionId)
-
8/4/2019 Bada Hello Everybody
8/18
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
{
String greeting = "Hello ";
switch(actionId)
{
caseBUTTON_ACTION_GREET:
// Set the greeting
greeting.Append(pField->GetText());
pLabel->SetText(greeting);
// redraw app's frame
pFrame->Draw();
pFrame->Show();
break;
default:
break;
}
}
Finally we need to tell the app to draw the Frame (and all in it) as soon as it is brought to the foreground(and/or as soon as the app starts).
?
1
2
3
4
5
6
7
void
SimpleGreeting::OnForeground(void)
{
// draw and show the app's frame
pFrame->Draw();
pFrame->Show();
}
Thats all! Save all three files and build the project (Ctrl+B).
-
8/4/2019 Bada Hello Everybody
9/18
Now we are ready to test this app in the bada simulator. Right-click the project->Run As->Bada
Simulator Application. The simulator will open up and run our app shortly.
This is a very brief, quick&dirty and straight-to-the-point example. Someone had to dig up his c++ know-
how for this. Please bear with that person . There is no funky professional c++ stuff in here, and I
know that YOU would rather write much better code instead Comments are still welcome. Here are the
complete two source code files:
SimpleGreeting.h
?
001
002
003
004
#ifndef __SIMPLEGREETING_H__
#define __SIMPLEGREETING_H__
#include
#include
-
8/4/2019 Bada Hello Everybody
10/18
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
#include
#include
#include
#include
#include
#include
usingnamespaceOsp::App;
usingnamespaceOsp::Base;
usingnamespaceOsp::Graphics;
usingnamespaceOsp::Io;
usingnamespaceOsp::Locales;
usingnamespaceOsp::System;
usingnamespaceOsp::Text;
usingnamespaceOsp::Ui;
usingnamespaceOsp::Ui::Controls;
staticconstintBUTTON_ACTION_GREET = 1;
/**
* [SimpleGreeting] application must inherit from Application class
* which provides basic features necessary to define an application.
*/
classSimpleGreeting :
publicApplication, publicIActionEventListener
{
private:
Frame *pFrame;
Form *pForm;
-
8/4/2019 Bada Hello Everybody
11/18
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
Button *pButton;
Label *pLabel;
EditField *pField;
public:
/**
* [SimpleGreeting] application must have a factory method thatcreates an instance of itself.
*/
staticApplication* CreateInstance(void);
public:
SimpleGreeting();
~SimpleGreeting();
public:
/**
* The application must provides its name.
*/
String GetAppName(void) const;
protected:
/**
* The application must provide its ID.
*/
AppId GetAppId(void) const;
/**
* The application must provide a secret code.
*/
-
8/4/2019 Bada Hello Everybody
12/18
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
AppSecret GetAppSecret(void) const;
public:
/**
* Called when the application is initializing.
*/
boolOnAppInitializing(AppRegistry& appRegistry);
/**
* Called when the application is terminating.
*/
boolOnAppTerminating(AppRegistry& appRegistry, boolforcedTermination= false);
/**
* Called when the application's frame moves to the top of the screen.
*/
voidOnForeground(void);
/**
* Called when this application's frame is moved from top of thescreen to the background.
*/
voidOnBackground(void);
/**
* Called when the system memory is not sufficient to run theapplication any further.
*/
voidOnLowMemory(void);
-
8/4/2019 Bada Hello Everybody
13/18
095
096
097
098
099
100
101
102
103
104
105
106
/**
* Called when the battery level changes.
*/
voidOnBatteryLevelChanged(BatteryLevel batteryLevel);
/**
* Called on Actions
*/
virtualvoidOnActionPerformed(constOsp::Ui::Control& source, intactionId);
};
#endif
SimpleGreeting.cpp
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
/**
* Name : SimpleGreeting
* Version : 1.0.0
* Vendor :
* Description :
*/
#include "SimpleGreeting.h"
SimpleGreeting::SimpleGreeting()
{
}
SimpleGreeting::~SimpleGreeting()
{
-
8/4/2019 Bada Hello Everybody
14/18
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
}
Application*
SimpleGreeting::CreateInstance(void)
{
// Create the instance through the constructor.
returnnewSimpleGreeting();
}
String
SimpleGreeting::GetAppName(void) const
{
//WARNING:
// This method is automatically generated.
// Please don't modify it!
// Return the name of the application.
staticString appName(L"SimpleGreeting");
returnappName;
}
AppId
SimpleGreeting::GetAppId(void) const
{
//WARNING:
// This method is automatically generated.
// Please don't modify it!
// Return the application ID.
staticAppId appId(L"93bt1p123e");
-
8/4/2019 Bada Hello Everybody
15/18
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
returnappId;
}
AppSecret
SimpleGreeting::GetAppSecret(void) const
{
//WARNING:
// This method is automatically generated.
// Please don't modify it!
// Return the secret code of the application.
static AppSecret appSecret(L"9C645DDBA19C71BAD1204DA4DAA7A0B9");
returnappSecret;
}
bool
SimpleGreeting::OnAppInitializing(AppRegistry& appRegistry)
{
// get the frame of the app
pFrame = GetAppFrame()->GetFrame();
// bind pForm to the Form we designed in resources
pForm = newForm();
pForm->Construct("IDF_FORM1");
// add the form as child to the app's frame and set it as current
pFrame->AddControl(*pForm);
pFrame->SetCurrentForm(*pForm);
// bind name input field
-
8/4/2019 Bada Hello Everybody
16/18
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
pField = (EditField*)pForm->GetControl("NAME_FIELD", true);
pField->SetText("Your name in here!");
// bind label
pLabel = (Label*)pForm->GetControl("GREETING_LABEL", true);
// bind button, set its action id
// and the action listener (this app's class itself)
pButton = (Button*)pForm->GetControl("HELLO_BUTTON", true);
pButton->SetActionId(BUTTON_ACTION_GREET);
pButton->AddActionEventListener(*this);
returntrue;
}
bool
SimpleGreeting::OnAppTerminating(AppRegistry& appRegistry, boolforcedTermination)
{
// TODO:
// Deallocate resources allocated by this application fortermination.
// The application's permanent data and context can be saved viaappRegistry.
returntrue;
}
void
SimpleGreeting::OnForeground(void)
{
// draw and show the app's frame
pFrame->Draw();
-
8/4/2019 Bada Hello Everybody
17/18
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
pFrame->Show();
}
void
SimpleGreeting::OnBackground(void)
{
// TODO:
// Stop drawing when the application is moved to the background.
}
void
SimpleGreeting::OnLowMemory(void)
{
// TODO:
// Free unused resources or close the application.
}
void
SimpleGreeting::OnBatteryLevelChanged(BatteryLevel batteryLevel)
{
// TODO:
// Handle any changes in battery level here.
// Stop using multimedia features(camera, mp3 etc.) if thebattery level is CRITICAL.
}
void
SimpleGreeting::OnActionPerformed(const Osp::Ui::Control& source, intactionId)
{
String greeting = "Hello ";
-
8/4/2019 Bada Hello Everybody
18/18
136
137
138
139
140
141
142
143
144
145
146
147
148
149
switch(actionId)
{
caseBUTTON_ACTION_GREET:
// Set the greeting
greeting.Append(pField->GetText());
pLabel->SetText(greeting);
// redraw app's frame
pFrame->Draw();
pFrame->Show();
break;
default:
break;
}
}
Have fun!