/*
 * File:			sample.cpp
 * Purpose:		Sample WXR usage program
 *
 * Author:		George Tasker
 * Mods:			
 *
 * Created:		March, 1998
 * Updated: 
 *
 * Copyright:	(c)None
 */

/*
   SYNOPSIS START

		This small app will utilize a WXR resource file to build a dialog.  The dialog has one or
		more of each of the following widget types:

			wxText 
			wxButton
			wxChoice
			wxListBox
			wxRadioBox
			wxCheckBox

   SYNOPSIS STOP
*/

/* static const char sccsid[] = "%W% %G%"; */

#ifdef __GNUG__
#pragma implementation
#pragma interface
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx_prec.h"

#ifdef __BORLANDC__
#  pragma hdrstop
#endif

#ifndef WX_PRECOMP
#  include "wx.h"
#endif

#ifdef WIN32
#define __WIN32__
#endif

#include "wxstring.h"
#include "sample.h"


// This statement initializes the whole application and calls OnInit
CsampleApp SampleApp;


// A macro needed for some compilers (AIX) that need 'main' to be defined
// in the application itself.
IMPLEMENT_WXWIN_MAIN


// `Main program' equivalent, creating windows and returning main app frame
wxFrame *CsampleApp::OnInit(void)
{
	int panelW = 640, panelH = 410;

	wxBeginBusyCursor();

	wxString titleStr;
	titleStr = "Sample WXR usage program";

	// Create the main frame window
	Frame = new CsampleFrame(NULL, titleStr.GetData(), 0, 0, 640, 480);

	// Give it a status line
	Frame->CreateStatusLine(1);

	BuildMenu();

	if (!LoadScreenForms())
	{
		// Failed to load the screen forms, so exit the program by returning a NULL
		// NOTE: User notification is done in the function LoadScreenForms()
		return NULL;
	}

	// Make a panel with a message
	Panel = new wxPanel(Frame, 0, 0, panelW, panelH);

	// Show the frame
	Frame->Show(TRUE);
	Frame->LoadAccelerators("menu_accel") ;        

	while (wxIsBusy()) wxEndBusyCursor();

	// Return the main frame window
	return Frame;
	
} // CsampleApp::OnInit()


//
// Constructs the menu bar at the top of the main program frame
//
void CsampleApp::BuildMenu()
{
	// Create the main frame's menu bar
	wxMenu *file_menu = new wxMenu;
	wxMenu *help_menu = new wxMenu;
	file_menu->Append(SAMPLE_FILE_DIALOG,			"&Dialog sample","Displays a sample dialog");
	file_menu->Append(SAMPLE_FILE_EXIT,				"e&Xit\tCtrl+X","Exit from program");
	help_menu->Append(SAMPLE_HELP_ACKNOWLEDGE,	"Ac&knowledge","Acknowledgements");
	wxMenuBar *sample_menu_bar = new wxMenuBar;
	sample_menu_bar->Append(file_menu,"&File");
	sample_menu_bar->Append(help_menu,"&Help");
	Frame->SetMenuBar(sample_menu_bar);
} // CsampleApp::BuildMenu()




//
//  *********************** Main Fastpic Frame Construction *********************
//

// Constructor
CsampleFrame::CsampleFrame(wxFrame *Frame, char *title, int x, int y, int w, int h, long Style):
	wxFrame(Frame, title, x, y, w, h, Style)
{
}  // CsampleFrame constructor


Bool CsampleFrame::OnClose(void)
{
	wxYield();

	if (this->wx_menu_bar)
	{
		delete wx_menu_bar;
		wx_menu_bar = 0;
	}

	// Hide the frame and return
	Show(FALSE);
	return (TRUE);
	
}  // CsampleFrame::OnClose    



// Intercept menu commands
void CsampleFrame::OnMenuCommand(int id)
{
	switch (id)
	{
		case SAMPLE_FILE_EXIT:
		{
			Close();
			break;
		}
		case SAMPLE_FILE_DIALOG:
		{
			// First example of creating a dialog using a NON-MODAL dialog
			new CsampleDlg(this);
			// NOTE: Program flow returns to this point after constructing/showing
			//       the NON-MODAL dialog
			break;
		}
		case SAMPLE_HELP_ACKNOWLEDGE:
		{
			// Second example of creating a dialog, this one using a MODAL dialog, with an
			// empty constructor, and all the initializing done in the calling section of code
			CwxWinDlg *wxWinDlg = new CwxWinDlg();
			if (wxWinDlg->LoadFromResource(this, "wxWinDlg"))
			{
				wxWinDlg->Initialize();
				wxWinDlg->Show(TRUE);
				// NOTE: This is a modal dialog, so there is no need to delete the dialog,
				//       as that will be done when the window is closed.  Program flow will
				//       return to this point when the dialog is closed.
			}
			else
			{
				wxMessageBox("Unable to load the wxWinDlg resource","ERROR...");
				delete wxWinDlg;
			}
			break;
		}
	}
}  // CsampleFrame::OnMenuCommand


void CsampleFrame::OnSize(int w, int h)
{
	if (SampleApp.Panel)
		SampleApp.Panel->OnSize(w,h-70);

	wxFrame::OnSize(w,h);
}



// ***************** CsampleDlg class methods ****************
//	**
// **
// **	This dialog class was generated to match a pre-defined dialog
// **	'standard' configuration which has static pointers to all
// ** widgets on the dialog for easy access. Quasi-callback stub 
// ** functions have been created for each widget, and are called
// ** from the OnCommand() function when a widget generates an
// ** event.
// **
// ** A small amount of code has been added in a several of the
// ** callback functions, and a slight bit in the constructor
// ** for this class the initialize the widget starting values,
// ** but otherwise the skeleton is the same for all dialogs, just
// ** the names of the function/methods/widgets/class are changed.
// **
// ***************** CwxWinDlg class methods ****************

//
// CsampleDlg constructor
//
CsampleDlg::CsampleDlg(wxWindow *parent)
{
	wxBeginBusyCursor();

	busyProcessingWidget = FALSE;
	widgetPtrsSet        = FALSE;

	wxString DlgName = "SampleDlg";

	if (!LoadFromResource(parent, DlgName.GetData()))
	{
		wxMessageBox("Unable to load the requested WXR file: 'SampleDlg'","Fatal error...");
		return;
	}

	if (!SetWidgetPtrs())
	{
		wxMessageBox("One or more widgets in the WXR file: 'SampleDlg' are missing or malformed.","Fatal error...");
		Close();
		return;
	}

	// Initialize any widgets to initial startup values/states
	pSampleLaunchGameCheck->SetValue(TRUE);
	pSampleDoneBtn->SetDefault();

	// Display the dialog on the display
	Show(TRUE);

	wxEndBusyCursor();

}  // CsampleDlg constructor


Bool CsampleDlg::OnClose(void)
{
	if (busyProcessingWidget)
	{
		wxBell();
		return TRUE;
	}
	busyProcessingWidget = TRUE;  // Prevents two CLOSE requests from overlapping each other

	//
	// Add your Close() code here
	//

	widgetPtrsSet = FALSE;
	wxEndBusyCursor();

	busyProcessingWidget = FALSE;
	return TRUE;
}  // CsampleDlg::OnClose()


void CsampleDlg::OnCommand(wxWindow& win, wxCommandEvent& event)
{
	// Widget pointers won't be set when the dialog is constructed.
	// Control is passed through this function once for each widget on
	// a dialog as the dialog is constructed.
	if (!widgetPtrsSet)
		return;

	wxString widgetName = win.GetName();

	// This variable indicates whether the user has previously told this dialog to go out
	// and do something that should prevent any other widgets on this dialog from doing 
	// something.  If it is true, ignore all requests from widgets, and return immediately
	if (busyProcessingWidget &&
		 (event.eventType != wxEVENT_TYPE_KILL_FOCUS) &&
		 (event.eventType != wxEVENT_TYPE_TEXT_COMMAND) &&
		 (event.eventType != wxEVENT_TYPE_SET_FOCUS))
	{
		wxBell();
		return;
	}

	if (widgetName == pSampleNameTxt->GetName())
	{
		cbSampleNameTxt(event);
		return;
	}  // SampleNameTxt

	if (widgetName == pSampleGenreChoice->GetName())
	{
		cbSampleGenreChoice();
		return;
	}  // SampleGenreChoice

	if (widgetName == pSampleAgeRangeRadio->GetName())
	{
		cbSampleAgeRangeRadio();
		return;
	}  // SampleAgeRangeRadio

	if (widgetName == pSampleGameList->GetName())
	{
		cbSampleGameList();
		return;
	}  // SampleGameList

	if (widgetName == pSampleLaunchGameCheck->GetName())
	{
		cbSampleLaunchGameCheck();
		return;
	}  // SampleLaunchGameCheck

	if (widgetName == pSampleDoneBtn->GetName())
	{
		cbSampleDoneBtn();
		return;
	}  // SampleDoneBtn
}  // CsampleDlg::OnCommand()


Bool CsampleDlg::SetWidgetPtrs()
{
	Bool	abort = FALSE;

	abort = abort || !(pSampleNameMsg			= (wxMessage   *)wxFindWindowByName("SampleNameMsg",this));
	abort = abort || !(pSampleNameTxt			= (wxText      *)wxFindWindowByName("SampleNameTxt",this));
	abort = abort || !(pSampleGenreMsg			= (wxMessage   *)wxFindWindowByName("SampleGenreMsg",this));
	abort = abort || !(pSampleGenreChoice		= (wxChoice    *)wxFindWindowByName("SampleGenreChoice",this));
	abort = abort || !(pSampleAgeRangeRadio	= (wxRadioBox  *)wxFindWindowByName("SampleAgeRangeRadio",this));
	abort = abort || !(pSampleGameListMsg		= (wxMessage   *)wxFindWindowByName("SampleGameListMsg",this));
	abort = abort || !(pSampleGameList			= (wxListBox   *)wxFindWindowByName("SampleGameList",this));
	abort = abort || !(pSampleLaunchGameMsg	= (wxMessage   *)wxFindWindowByName("SampleLaunchGameMsg",this));
	abort = abort || !(pSampleLaunchGameCheck	= (wxCheckBox  *)wxFindWindowByName("SampleLaunchGameCheck",this));
	abort = abort || !(pSampleDoneBtn			= (wxButton    *)wxFindWindowByName("SampleDoneBtn",this));

	return(widgetPtrsSet = !abort);
}  // CsampleDlg::SetWidgetPtrs()


Bool CsampleDlg::cbSampleNameTxt(wxCommandEvent& event)
{
	busyProcessingWidget = TRUE;
	//
	// Add your callback handling code here
	//

	busyProcessingWidget = FALSE;
	return TRUE;
}  // CsampleDlg::cbSampleNameTxt() callback


Bool CsampleDlg::cbSampleGenreChoice()
{
	busyProcessingWidget = TRUE;
	//
	// Add your callback handling code here
	//
	Genre = (enum Genre)pSampleGenreChoice->GetSelection();
	busyProcessingWidget = FALSE;
	return TRUE;
}  // CsampleDlg::cbSampleGenreChoice() callback


Bool CsampleDlg::cbSampleAgeRangeRadio()
{
	busyProcessingWidget = TRUE;
	//
	// Add your callback handling code here
	//
	ageRange = (enum AgeRange)pSampleAgeRangeRadio->GetSelection();
	busyProcessingWidget = FALSE;
	return TRUE;
}  // CsampleDlg::cbSampleAgeRangeRadio() callback


Bool CsampleDlg::cbSampleGameList()
{
	busyProcessingWidget = TRUE;
	//
	// Add your callback handling code here
	//
	favoriteGame = pSampleGameList->GetStringSelection();

	busyProcessingWidget = FALSE;
	return TRUE;
}  // CsampleDlg::cbSampleGameList() callback


Bool CsampleDlg::cbSampleLaunchGameCheck()
{
	busyProcessingWidget = TRUE;
	//
	// Add your callback handling code here
	//
	launchGame = pSampleLaunchGameCheck->GetValue();
	busyProcessingWidget = FALSE;
	return TRUE;
}  // CsampleDlg::cbSampleLaunchGameCheck() callback


Bool CsampleDlg::cbSampleDoneBtn()
{
	busyProcessingWidget = TRUE;
	//
	// Add your callback handling code here
	//
	Show(FALSE);
	wxYield();
	userName = pSampleNameTxt->GetValue();

	wxString msg;
	msg.sprintf("Thank you %s.\nYour favorite game is %s.",userName.GetData(),favoriteGame.GetData());

	if (launchGame)
		msg += "\n\nLaunching game now!!!\n";
	wxMessageBox(msg.GetData(),"Partial information");
	busyProcessingWidget = FALSE;
	Close();
	return TRUE;
}  // CsampleDlg::cbSampleDoneBtn() callback




// ***************** CwxWinDlg class methods ****************
//	**
// **
// **	This dialog class was hand generated and uses the base wxWindows 
// **	functions for accessing the widgets on the dialog
// **
// **
// ***************** CwxWinDlg class methods ****************
void CwxWinDlg::OnCommand(wxWindow& win, wxCommandEvent& event)
{
	wxString s;
	s = win.GetName();
	
	if (s == "wxWinOkBtn")
		Close();
}  // CwxWinDlg::OnCommand


Bool CwxWinDlg::OnClose()
{
	return TRUE;
}  // CwxWinDlg::OnClose()


void CwxWinDlg::Initialize()
{
	wxWindow 	*w;
	wxString 	s;

	if (w = wxFindWindowByName("wxWinOkBtn",this))
	{
		((wxButton *)w)->SetDefault();
		((wxButton *)w)->SetFocus();
	}

	Centre(wxBOTH);
}  // CwxWinDlg::Initialize


//
// Loads any/all WXR resource files into the resource table
//
Bool LoadScreenForms()
{
	wxString dialogFileName;

	// Load Resources dynamically
	dialogFileName = "sample.wxr";
	Bool WXR_Loaded = wxResourceParseFile(dialogFileName.GetData());

	if (!WXR_Loaded)
	{
		wxString problemFileStr;
		problemFileStr.sprintf("Unable to load the WXR file '%s'.\n\nResource file is missing or damaged",dialogFileName.GetData());
		wxMessageBox(problemFileStr.GetData(),"Critical error...");
	}

	return WXR_Loaded;
}   // LoadScreenForms



// ************************** sample.cpp ****************************
