Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Introduction to wxWidgets

, 25 Dec 2005
A beginner's tutorial on wxWidgets for cross platform GUI development.
/* ConfigDialog.cpp
 * 
 * Copyright (C) 3 September, 2005 Priyank Bolia
 * http://www.priyank.in
 * 
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This code 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */

#include "configdialog.h"

BEGIN_EVENT_TABLE(CConfigDialog, wxDialog)
    EVT_BUTTON(ID_CONFIG_BUTTON_OK, CConfigDialog::OnOK)
	EVT_CHECKBOX(ID_CHECKBOX_STAY_ON_TOP, CConfigDialog::OnCheckStayOnTop)
	EVT_CHECKBOX(ID_CHECKBOX_HIDE_TRAY_ICON, CConfigDialog::OnCheckHideTrayIcon)
END_EVENT_TABLE()

wxString CConfigDialog::m_strRelax = wxT("");
wxString CConfigDialog::m_strWash = wxT("");
bool CConfigDialog::m_bStayOnTop = true;
bool CConfigDialog::m_bHideTrayIcon = false;

CConfigDialog::CConfigDialog(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxDialog(parent, id, title, position, size, style)
{
	wxArrayString numberArray;
	numberArray.Add(wxT("0"));
	numberArray.Add(wxT("1"));
	numberArray.Add(wxT("2"));
	numberArray.Add(wxT("3"));
	numberArray.Add(wxT("4"));
	numberArray.Add(wxT("5"));
	numberArray.Add(wxT("6"));
	numberArray.Add(wxT("7"));
	numberArray.Add(wxT("8"));
	numberArray.Add(wxT("9"));
	ReadConfig();
	m_pStaticBox = new wxStaticBox(this, ID_WXSTATICBOX, wxT("Eye Care"), wxPoint(8,3), wxSize(203,124));
	m_pOkButton = new wxButton(this, ID_CONFIG_BUTTON_OK, "Ok", wxPoint(35, 97));
	m_pCancelButton = new wxButton(this, wxID_CANCEL, "Cancel", wxPoint(115, 97));
	wxTextValidator textValidatorRelax(wxFILTER_INCLUDE_CHAR_LIST, &m_strRelax);
	textValidatorRelax.SetIncludes(numberArray);
	m_pRelaxText = new wxStaticText(this, ID_WXSTATICTEXT_RELAX, wxT("&Relaxing time (Minutes):"), wxPoint(20, 25), wxSize(130, 20), wxALIGN_LEFT);
	m_pRelaxEdit = new wxTextCtrl(this, ID_WXEDIT_RELAX, wxT(""), wxPoint(155,23), wxSize(45,20), wxTE_LEFT, textValidatorRelax, wxT("WxEditRelax"));
	m_pRelaxEdit->SetMaxLength(2);
	wxTextValidator textValidatorWash(wxFILTER_INCLUDE_CHAR_LIST, &m_strWash);
	textValidatorWash.SetIncludes(numberArray);
	m_pWashText = new wxStaticText(this, ID_WXSTATICTEXT_WASH, wxT("&Washing time (Minutes):"), wxPoint(20, 50), wxSize(130, 20), wxALIGN_LEFT);
	m_pWashEdit = new wxTextCtrl(this, ID_WXEDIT_WASH, wxT(""), wxPoint(155,47), wxSize(45,20), wxTE_LEFT, textValidatorWash, wxT("WxEditWash"));
	m_pWashEdit->SetMaxLength(2);
	m_pStayTopCheckBox = new wxCheckBox(this, ID_CHECKBOX_STAY_ON_TOP, wxT("&Stay on top"), wxPoint(20, 75));
	m_pHideIconCheckBox = new wxCheckBox(this, ID_CHECKBOX_HIDE_TRAY_ICON, wxT("&Hide tray icon"), wxPoint(115, 75));
	m_pStayTopCheckBox->SetValue(m_bStayOnTop);
	m_pHideIconCheckBox->SetValue(m_bHideTrayIcon);
}

CConfigDialog::~CConfigDialog(void)
{
}

void CConfigDialog::OnOK(wxCommandEvent& event)
{
	if(DoOK())
	{
		WriteConfig();
		event.Skip();
		wxDialog::OnOK(event);
	}
}

bool CConfigDialog::DoOK()
{
	m_strRelax = m_pRelaxEdit->GetValue();
	m_strWash = m_pWashEdit->GetValue();
	if(m_strRelax == wxT("") || m_strWash == wxT("") || m_strRelax == wxT("0") || m_strRelax == wxT("00") || m_strWash == wxT("0") || m_strWash == wxT("00"))
	{
		return false;
	}
	return true;
}

unsigned int CConfigDialog::GetRelaxTime(void)
{
	if(m_strRelax == wxT(""))
		ReadConfig();
	return (atoi(m_strRelax)*60*1000);
}

unsigned int CConfigDialog::GetWashTime(void)
{
	if(m_strWash == wxT(""))
		ReadConfig();
	return (atoi(m_strWash)*60*1000);
}

bool CConfigDialog::IsStayOnTop(void)
{
	return m_bStayOnTop;
}

bool CConfigDialog::IsHideIcon(void)
{
	return m_bHideTrayIcon;
}

void CConfigDialog::ReadConfig(void)
{
	wxFileConfig *pConfig = new wxFileConfig(wxT("Eye Care"), wxT("Priyank Bolia"),
		wxT("EyeCare.ini"), wxEmptyString, wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_NO_ESCAPE_CHARACTERS | wxCONFIG_USE_RELATIVE_PATH);
	pConfig->Read(wxT("Relax Time"), &m_strRelax, wxT("15"));
	pConfig->Read(wxT("Wash Time"), &m_strWash, wxT("55"));
	pConfig->Read(wxT("Stay on top"), &m_bStayOnTop, true);
	pConfig->Read(wxT("Hide tray icon"), &m_bHideTrayIcon, false);
	delete pConfig;
	WriteConfig();
}

void CConfigDialog::WriteConfig(void)
{
	wxFileConfig *pConfig = new wxFileConfig(wxT("Eye Care"), wxT("Priyank Bolia"),
		wxT("EyeCare.ini"), wxEmptyString, wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_NO_ESCAPE_CHARACTERS | wxCONFIG_USE_RELATIVE_PATH);
	pConfig->Write(wxT("Relax Time"), m_strRelax);
	pConfig->Write(wxT("Wash Time"), m_strWash);
	pConfig->Write(wxT("Stay on top"), m_bStayOnTop);
	pConfig->Write(wxT("Hide tray icon"), m_bHideTrayIcon);
	delete pConfig;
}

void CConfigDialog::OnCheckStayOnTop(wxCommandEvent& command)
{
	if ( command.IsChecked() )
		m_bStayOnTop = true;
	else
		m_bStayOnTop = false;
}

void CConfigDialog::OnCheckHideTrayIcon(wxCommandEvent& command)
{
	if ( command.IsChecked() )
		m_bHideTrayIcon = true;
	else
		m_bHideTrayIcon = false;
}

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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Priyank Bolia
Software Developer (Senior)
India India
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 25 Dec 2005
Article Copyright 2005 by Priyank Bolia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid