Click here to Skip to main content
15,886,075 members
Articles / Desktop Programming / WTL

The Psychology of a WTL Project: Lowercase Cause (a typing program)

Rate me:
Please Sign up or sign in to vote.
4.99/5 (32 votes)
20 Apr 200524 min read 63.6K   2.7K   25  
A psychological journey into a project crafted from start to finish.
#include "StdAfx.h"
#include ".\menudlg.h"
#include "HighScoresDlg.h"
#include "AboutDlg.h"
#include "sound_system.h"

LRESULT CMenuDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	hf::set_window_icon(*this, IDR_MAINFRAME, true);

	// default mode
	CheckDlgButton(RDO_SURVIVAL + settings::get_last_type(), TRUE);

	// populate our combo list with difficulties
	_cboDifficulty.Attach(GetDlgItem(CBO_DIFFICULTY));
	for(int n = STR_DIFFICULTY_LEVEL_1; n <= STR_DIFFICULTY_LEVEL_8; n++)
	{
		std::ostringstream ss;
		ss << _game_session.get_target_wpm((n - STR_DIFFICULTY_LEVEL_1) + 1)
			<< _T(" ") << hf::load_string(STR_COL_WPM) << _T(": ") 
		   << hf::load_string(n);
		_cboDifficulty.AddString(ss.str().c_str());
	}	

	// default difficulty
	_cboDifficulty.SetCurSel(settings::get_last_difficulty() - 1);

	InitList();

	// default time for the timed mode
	_edMinutes.Attach(GetDlgItem(ED_MINUTES));
	_edMinutes.SetWindowText(
		hf::int_to_str(settings::get_last_timed_minutes()).c_str());

	// set the default phrase file
	std::string temp_str = settings::get_last_phrase_file();
	LVFINDINFO fi;
	fi.flags = LVFI_STRING;
	fi.psz = temp_str.c_str();
	_lvwPhraseFiles.SelectItem(_lvwPhraseFiles.FindItem(&fi, 0));

	UpdateControls();
	_lvwPhraseFiles.SetFocus();

	return TRUE;
}

void CMenuDlg::InitList()
{
	_lvwPhraseFiles.Attach(GetDlgItem(LVW_PHRASE_FILES));

	// set to full row selection
	SendMessage(_lvwPhraseFiles.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
		LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

	// set up an image list for the listview
	hf::create_small_image_list(_ilPhraseFiles);
	hf::add_icon(_ilPhraseFiles, ICO_LIST);
	_lvwPhraseFiles.SetImageList(_ilPhraseFiles, LVSIL_SMALL);

	// add columns and set their widths correctly
	CRect list_rect;
	_lvwPhraseFiles.GetClientRect(&list_rect);
	int list_width = list_rect.Width();
	_lvwPhraseFiles.InsertColumn(0, 
		hf::load_string(STR_PHRASE_LIST_FILENAME).c_str(), LVCFMT_LEFT,
		static_cast<int>(list_width * .66), -1);
	_lvwPhraseFiles.InsertColumn(1, 
		hf::load_string(STR_PHRASE_LIST_HIGH_SCORE).c_str(), LVCFMT_RIGHT,
		static_cast<int>(list_width * .335), -1);

	LoadList();
}

void CMenuDlg::SetSettings()
{
	int cur_sel = _lvwPhraseFiles.GetSelectedIndex();
	if(cur_sel != -1)
	{
		std::string path = hf::get_listview_text(_lvwPhraseFiles, cur_sel, 0);
		settings::set_last_phrase_file(path);
	}

	settings::set_last_difficulty(_cboDifficulty.GetCurSel() + 1);
	settings::set_last_timed_minutes(GetTimeFromUI() / 60);
	settings::set_last_type(static_cast<game_type>(
		IsDlgButtonChecked(RDO_TIMED)));
}

void CMenuDlg::LoadList()
{
	string_list_t phrase_file_list = get_phrase_files();
	string_list_t::iterator i;
	for(i = phrase_file_list.begin(); i != phrase_file_list.end(); i++)
	{
		int index = _lvwPhraseFiles.GetItemCount();
		_lvwPhraseFiles.InsertItem(index, i->c_str(), 0);
	}
}

void CMenuDlg::UpdateControls()
{
	// enable windows based on the radio button
	_cboDifficulty.EnableWindow(IsDlgButtonChecked(RDO_SURVIVAL));
	_edMinutes.EnableWindow(IsDlgButtonChecked(RDO_TIMED));
	
	// no new game unless a list is selected
	GetDlgItem(BN_NEW_GAME).EnableWindow(
		_lvwPhraseFiles.GetSelectedIndex() != LB_ERR);

	// setup our high score with what our ui has currently, we use
	// this to compare our high scores
	high_score hs;
	hs.set_type(IsDlgButtonChecked(RDO_SURVIVAL) ? SURVIVAL : TIMED);
	hs.set_difficulty(
		hs.get_type() == SURVIVAL ? _cboDifficulty.GetCurSel() + 1 : 1);
	if(hs.get_type() == TIMED)
		hs.set_time(GetTimeFromUI());

	// retrieve any high scores associated with this playing theme
	for(int n = 0; n <= _lvwPhraseFiles.GetItemCount(); n++)
	{
		std::string file = hf::get_listview_text(_lvwPhraseFiles, n, 0);
		hs.set_phrase_list(file);

		high_score temp = high_score_manager::get_current_high_score(hs);
		if(!temp.is_null())
			_lvwPhraseFiles.SetItemText(n, 1, temp.get_string().c_str());
		else
			_lvwPhraseFiles.SetItemText(n, 1, _T(""));
	}
}

LRESULT CMenuDlg::OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	SetSettings();
	EndDialog(1);
	return 0;
}

LRESULT CMenuDlg::OnBnClickedNewGame(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	std::string path;
	int cur_sel = _lvwPhraseFiles.GetSelectedIndex();
	if(cur_sel != LB_ERR)
	{
		SetSettings();

		// attempt loading the file
		path = hf::get_listview_text(_lvwPhraseFiles, cur_sel, 0);
		path += _T(".ini");
		_phrase_list.load_phrase_list(path);

		// check for at least a valid entry
		if(!_phrase_list.get_count())
		{
			MessageBox(hf::load_string(STR_ERROR_NO_PHRASES).c_str(),
				hf::load_string(STR_ERROR).c_str());
			return 0;
		}

		// set our mode inexplicitly
		if(IsDlgButtonChecked(RDO_SURVIVAL))
			_game_session.set_difficulty(_cboDifficulty.GetCurSel() + 1);
		else
			_game_session.set_timer(GetTimeFromUI());

		// averages are an important part to calculating how long a window
		// should live for
		_game_session.set_average_phrase_length(
			_phrase_list.get_average_length());
		EndDialog(0);
	}
	return 0;
}

int CMenuDlg::GetTimeFromUI()
{
	int minutes = _ttoi(hf::get_window_text(_edMinutes).c_str());
	if(!minutes)
		minutes = 10;
	return minutes * 60;
}

LRESULT CMenuDlg::OnBnClickedSurvival(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	UpdateControls();
	return 0;
}

LRESULT CMenuDlg::OnBnClickedTimed(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	UpdateControls();
	return 0;
}

LRESULT CMenuDlg::OnLvnItemchangedPhraseFiles(int /*idCtrl*/, LPNMHDR /*pNMHDR*/, BOOL& /*bHandled*/)
{
	UpdateControls();
	return 0;
}

LRESULT CMenuDlg::OnCbnSelchangeDifficulty(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	UpdateControls();
	return 0;
}

LRESULT CMenuDlg::OnEnChangeMinutes(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	UpdateControls();
	return 0;
}

LRESULT CMenuDlg::OnBnClickedHighScores(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	sound_system::play_high_scores();
	CHighScoresDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMenuDlg::OnBnClickedAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	sound_system::play_about();
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions