/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "Be.h"


// TestView
class TestView : public BView
{
public:
	TestView (BRect frame, const char *name);

	virtual void Draw (BRect updateRect);
};


// TestWindow
class TestWindow : public BWindow
{
public:
	TestWindow (BRect frame, const char* title);

	virtual bool QuitRequested ();
	virtual void MessageReceived(BMessage *msg);
};


// TestApp
class TestApp : public BApplication
{
public:
	TestApp ();
	virtual void Pulse ();

private:
	TestWindow* wnd1;
	TestWindow* wnd2;
};


// constructor
TestApp::TestApp () :
BApplication ("application/x-vnd.TestApp")
{
	wnd1 = new TestWindow (BRect (50, 50, 500, 500), "Test Window 1");
	wnd1->Show ();
	wnd2 = new TestWindow (BRect (150, 150, 650, 650), "Test Window 2");
	wnd2->Show ();

	SetPulseRate (1000000);
}


BStatusBar* statusBar = 0;


void TestApp::Pulse ()
{
	statusBar->Update (1);
}


// constructor
TestWindow::TestWindow (BRect frame, const char* title) :
BWindow (frame, title, B_DOCUMENT_WINDOW, 0)
{
	BRect rect (Bounds ());
	BView* mainView = new TestView (rect, "TestView");
	mainView->SetViewColor (ui_color (B_PANEL_BACKGROUND_COLOR));
	AddChild (mainView);

	BBox* box = new BBox (BRect (10, 140, 300, 400));
	box->SetLabel ("TestApp");
	mainView->AddChild (box);

	BStringView* str = new BStringView (BRect (10, 50, 120, 80), "str1", "StringView");
	mainView->AddChild (str);

	BCheckBox* checkbox = new BCheckBox (BRect (10, 70, 120, 100), "check1", "Checked?", new BMessage ('chk1'));
	mainView->AddChild (checkbox);

	BButton* button = new BButton (BRect (100, 20, 200, 120), "okbutton", "OK", new BMessage ('okBu'));
	box->AddChild (button);

	BRadioButton* r1 = new BRadioButton (BRect (100, 150, 200, 180), "r1", "High", 0);
	r1->SetValue (B_CONTROL_ON);
	box->AddChild (r1);

	BRadioButton* r2 = new BRadioButton (BRect (100, 190, 200, 220), "r2", "Low", 0);
	box->AddChild (r2);

	/*BStatusBar**/ statusBar = new BStatusBar (BRect (10, 410, 300, 440), "status", "Progress");
	statusBar->SetMaxValue (100);
	statusBar->Update (33);
	mainView->AddChild (statusBar);

	button = new BButton (BRect (300, 100, 400, 200), "quitbutton", "Quit", new BMessage ('quBu'));
	mainView->AddChild (button);
}


// QuitRequested
bool TestWindow::QuitRequested ()
{
	be_app->PostMessage (B_QUIT_REQUESTED);
	return true;
}


// constructor
TestView::TestView (BRect rect, const char *name) :
BView (rect, name, B_FOLLOW_ALL, B_WILL_DRAW)
{
	SetFont (be_fixed_font);
	SetFontSize (24);
}


// Draw
void TestView::Draw (BRect updateRect)
{
	DrawString ("Hello world!", BPoint (10, 20));
}


// MessageReceived
void TestWindow::MessageReceived (BMessage *msg)
{
	switch (msg->what) {

		case 'okBu':
		{
			BAlert* alert = new BAlert ("Alert", "Does this work?", "No", "Yes", NULL, B_WIDTH_FROM_WIDEST, B_WARNING_ALERT);
			alert->Go ();
			break;
		}

		case 'quBu':
			be_app->PostMessage (B_QUIT_REQUESTED);
			break;

		default:
			BWindow::MessageReceived (msg);
	}
}


// main
int main (int argc, char** argv)
{
	BApplication* testApp = new TestApp ();
	testApp->Run ();
	delete testApp;

	return 0;
}
