/*
 * Datei: MyView.cpp	Projekt: LocaleKit3
 * Autor: Markus Maier	Datum:	 06.06.2005
 *
 *		Erstellt fuer zemag #3
 *		2005, Urheberrecht beim Autor
 */

// Zeta Includes
#include <app/Application.h> // Globale Variable "be_app"
#include <app/Message.h>
#include <app/Messenger.h>
#include <interface/Button.h>
#include <locale/Locale.h>

// Projekt Includes
#include "MyView.h"

// Aliase fuer Name erzeugen. Verhindert Tipfehler. ;-)
const char* BT_NAME_HELLO		= "HelloButton";
const char* BT_LABEL_HELLO		= "Hello";
const char* BT_NAME_HOWAREYOU	= "HowAreYouButton";
const char* BT_LABEL_HOWAREYOU	= "How are you";
const char* BT_NAME_OK			= "OkButton";
const char* BT_LABEL_OK			= "Ok";
const char* BT_NAME_ABOUT		= "AboutButton";
const char* BT_LABEL_ABOUT		= "About";

// Fixe Abstaende fuer GUI-Elemente
const int LAYOUT_XOFFSET = 15;
const int LAYOUT_YOFFSET = 15;

// diverse Konstanten
const int N_BUTTONS = 4; // Anzahl der Buttons

// Implementation der Klasse MyView
//-----------------------------------------------------------------------------
MyView::MyView( BRect frame )
: BView( frame, VIEW_MYVIEW, B_FOLLOW_ALL_SIDES, B_WILL_DRAW )
{
}

//-----------------------------------------------------------------------------
MyView::~MyView()
{
	// Hier ist nichts zu tun
}

//-----------------------------------------------------------------------------
void
MyView::AttachedToWindow( void )
{
	// Sobald der View in das Fenster eingehaengt wurde,
	// wird die GUI erzeugt.
	CreateGUI();
}

//-----------------------------------------------------------------------------
void
MyView::CreateGUI( void )
{
	// Anordnung der Buttons in einer 2x2 Matrix.
	// Die Buttongroesse orientiert sich am laengsten String
	BButton* button;	
	BRect frame( LAYOUT_XOFFSET, LAYOUT_YOFFSET, 125, 25 ); // Anfangsposition	
	
	// "Hello" Button erzeugen
	button = new BButton(
		frame,
		BT_NAME_HELLO,
		_T( BT_LABEL_HELLO ),
		new BMessage( MSG_BUTTON_HELLO ) );
	AddChild( button); // In View einhaengen

	// "How are you" Button erzeugen
	button = new BButton(
		frame,
		BT_NAME_HOWAREYOU,
		_T( BT_LABEL_HOWAREYOU ),
		new BMessage( MSG_BUTTON_HAY ) );
	AddChild( button);
	
	// "Ok" Button erzeugen
	button = new BButton(
		frame,
		BT_NAME_OK,
		_T( BT_LABEL_OK ),
		new BMessage( MSG_BUTTON_OK ) );
	AddChild( button);

	// "About" Button
	button = new BButton(
		frame,
		BT_NAME_ABOUT,
		_T( BT_LABEL_ABOUT ),
		new BMessage( B_ABOUT_REQUESTED ) );
	// Message geht an die Application
	button->SetTarget( be_app );
	AddChild( button);

	this->refreshLayout();
}

//-----------------------------------------------------------------------------
void
MyView::refreshLanguage( void )
{
	// Suche nach den Views unter zuhilfenahme der Viewnamen
	((BButton*)FindView( BT_NAME_HELLO ))->
		SetLabel( _T( BT_LABEL_HELLO ) );
		
	((BButton*)FindView( BT_NAME_HOWAREYOU ))->
		SetLabel( _T( BT_LABEL_HOWAREYOU ) );
		
	((BButton*)FindView( BT_NAME_OK ))->
		SetLabel( _T( BT_LABEL_OK ) );
		
	((BButton*)FindView( BT_NAME_ABOUT ))->
		SetLabel( _T( BT_LABEL_ABOUT ) );

	this->refreshLayout();
}

//-----------------------------------------------------------------------------
void
MyView::refreshLayout( void )
{
	//-------------------------------
	// Zugriff auf GUI-ELemente holen
	//-------------------------------
	BButton* buttons[ N_BUTTONS ];
	buttons[0] = (BButton*)FindView( BT_NAME_HELLO );
	buttons[1] = (BButton*)FindView( BT_NAME_HOWAREYOU );
	buttons[2] = (BButton*)FindView( BT_NAME_OK );
	buttons[3] = (BButton*)FindView( BT_NAME_ABOUT );
	
	//-----------------------
	// Buttonbreite bestimmen
	//-----------------------
	BPoint aktsize( 0.0, 0.0 );
	BPoint tmpsize( 0.0, 0.0 );

	for( int i = 0; i < N_BUTTONS; i++ )
	{
		buttons[i]->GetPreferredSize( &(aktsize.x), &(aktsize.y) );
		
		if( aktsize.x > tmpsize.x )
		{
			tmpsize = aktsize;
		}
	}

	//-------------------------------
	// Button in der Groesse anpassen
	//-------------------------------
	for( int i = 0; i < N_BUTTONS; i++ )
	{
		buttons[i]->ResizeTo( tmpsize.x, tmpsize.y );
	}
	
	//----------------------
	// Buttons positionieren
	//----------------------
	float tmpX, tmpY;
	// Button 1 an Position 0,0
	buttons[0]->MoveTo( LAYOUT_XOFFSET, LAYOUT_YOFFSET );
	// Button 2 an Position 0,1
	tmpX = LAYOUT_XOFFSET + buttons[0]->Frame().Width() + LAYOUT_XOFFSET;
	tmpY = LAYOUT_YOFFSET;
	buttons[1]->MoveTo( tmpX, tmpY );
	// Button 3 an Position 1,0
	tmpX = LAYOUT_XOFFSET;
	tmpY = LAYOUT_YOFFSET + buttons[1]->Frame().Height() + LAYOUT_YOFFSET;
	buttons[2]->MoveTo( tmpX, tmpY );
	// Button 4 an Position 1,1
	tmpX = LAYOUT_XOFFSET + buttons[2]->Frame().Width() + LAYOUT_XOFFSET;
	tmpY = LAYOUT_YOFFSET + buttons[2]->Frame().Height() + LAYOUT_YOFFSET;
	buttons[3]->MoveTo( tmpX, tmpY );

	//--------------------------
	// View der Groesse anpassen
	//--------------------------
	tmpX += buttons[3]->Frame().Width() + LAYOUT_XOFFSET;
	tmpY += buttons[3]->Frame().Height() + LAYOUT_YOFFSET;
	this->ResizeTo( tmpX, tmpY );
}