Hello World

Go back to the main page
This application should be the entry point of TWEAPPS creation. A single button, displaying a simple message.
Of course all of this is written using TeapotWare, which makes the code virtually cross-plateform.




Now, let's take a closer look to the code of this tweapp. Basically, there are 2 files: twhello.h and twhello.cpp.

Here below is twhello.h:
#ifndef _TWA_HELLO_WORLD_
#define _TWA_HELLO_WORLD_

#include <twmodule/twappletmod.h>
#include <twui/twuiwindow.h>
#include <twui/twuibutton.h>

class TWHelloWorldApplet : public TWAppletMod, public TWIUIEventHandler
{
private:
	TWUIButton 		m_okbtn;
	TWUIWindow * 	m_window;
	
protected:

	void 			OnCommand(TWUIItem *p_item);

	TWBOOL Init(TWUIWindow * p_target, TWIWebBrowser *p_browser, TWPropertyBag *p_params);
	void 		Release() {};
	TWBOOL Main();
	
public:
	TWHelloWorldApplet();
};


DECL_TWMODULE(TWHelloWorldApplet);

#endif


We intentionally removed every comment, to illustrate how straight is is.
So mainly, TWHelloWorldApplet inherit from TWAppletMod and TWIUIEventHandler. The class overloads TWIUIEventHandler::OnCommand to be able to handle the click on the button, and Init, Main and Release of TWAppletMod.
Those 3 methodes will be called by the web browser.

Now, let's look to the implementation of this class.
#include "twahelloworld.h"
#include <twui/twuihelper.h>
#include <twsystem/twshell.h>

TWBOOL TWHelloWorldApplet::Init(TWUIWindow * p_target, TWIWebBrowser *p_browser, TWPropertyBag *p_params)
{
	TWString str;
	p_params->GetProperty("@temp_path",str);		//@temp_path is the temporary path created by the web browser, containing the files extracted from the TWA

	TWShell::Cd(str);				//useless here, but it can be usefull in other tweapps (to seek for resource)
	TWUIHelper::LoadUIManager("twuimanagerw32.twm");
	m_okbtn.SetText("Click Me!");
	m_okbtn.SetPosition(30,30);
	m_okbtn.SetSize(100,30);
	m_okbtn.SetParent(p_target);
	m_okbtn.Init();
	m_okbtn.SetEventHandler(this);
	p_target->SetEventHandler(this);
	m_window=p_target;

	return TRUE;
}

void TWHelloWorldApplet::OnCommand(TWUIItem *p_item)
{
	if(p_item==&m_okbtn)
	{
		TWUIHelper::MsgBox("Hello World!\nWelcome to the world of TeapotWare!");
	}
}

TWBOOL TWHelloWorldApplet::Main()
{
	while(m_window->ProcessEvent(TRUE));
	return TRUE;
}

TWHelloWorldApplet::TWHelloWorldApplet()
{
	m_window=NULL;
}


Here, the Init performs the creation of the button itself, and set the event handlers, so that our class can be notified of the click.
Then, the Main mathod merely loop until no event needs to be processed.
To conclude, we can see that it is quite simple to create a tweapp. As stated, it's also faster than java, more flexible than flash, and not easy to be reverse-engineered, since it is a kind of compiled dynamic library.