Click here to Skip to main content
15,881,380 members
Articles / Artificial Intelligence

Introduction to wxWidgets

Rate me:
Please Sign up or sign in to vote.
4.55/5 (120 votes)
25 Dec 2005GPL321 min read 1.6M   7.2K   330  
A beginner's tutorial on wxWidgets for cross platform GUI development.
/* EyeCareFrame.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 "eyecareframe.h"

BEGIN_EVENT_TABLE(CEyeCareFrame, wxFrame)
    EVT_MENU(ID_EXIT, CEyeCareFrame::OnExit)
    EVT_MENU(ID_CONFIG, CEyeCareFrame::OnConfig)
    EVT_MENU(ID_ABOUT, CEyeCareFrame::OnAbout)
	EVT_MENU(ID_HIDE, CEyeCareFrame::OnEsc)
	EVT_BUTTON(ID_WXBUTTON_HIDE, CEyeCareFrame::OnHideButton)
	EVT_TIMER(ID_MAINWINDOWTIMER, CEyeCareFrame::OnHideMainWindow)
	EVT_TIMER(ID_EYERELAX_TIMER, CEyeCareFrame::OnEyeRelaxTimer)
	EVT_TIMER(ID_EYEWASH_TIMER, CEyeCareFrame::OnEyeWashTimer)
	EVT_CLOSE(CEyeCareFrame::OnClose)
	EVT_ERASE_BACKGROUND(CEyeCareFrame::OnEraseBackground)
END_EVENT_TABLE()

CEyeCareFrame::CEyeCareFrame(void)
{
}

CEyeCareFrame::CEyeCareFrame( wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style )
    : wxFrame( parent, id, title, position, size, style)
{
    CreateGUIControls();
}

CEyeCareFrame::~CEyeCareFrame(void)
{
	m_pRelaxTimer->Stop();
	m_pWashTimer->Stop();
	m_pTaskBarIcon->RemoveIcon();
	delete m_pRelaxTimer;
	delete m_pWashTimer;
	delete m_pMainWindowTimer;
	delete m_pTaskBarIcon;
}

void CEyeCareFrame::OnClose(wxCloseEvent& event)
{
    // --> Don't use Close with a Frame,
    // use Destroy instead.
	m_pMainWindowTimer->Stop();
    this->Hide();
	m_pHeaderText->SetLabel(wxT("Welcome to Eye Care!"));
	m_pDetailText->SetLabel(wxT("This program will remind you at regular intervals \nto take care of your eyes and relax/wash them."));
}

void CEyeCareFrame::CreateGUIControls()
{
	m_pTaskBarIcon = NULL;
    menuSettings = new wxMenu;
    menuHelp = new wxMenu;
    menuSettings->Append( ID_CONFIG, "&Configuration", "Change Settings" );
    menuSettings->Append( ID_EXIT, "&Exit", "Exit the application" );
    menuHelp->Append( ID_ABOUT, "&About...", "About Eye Care" );

	menuBar = new wxMenuBar;
    menuBar->Append( menuSettings, "&Settings");
    menuBar->Append( menuHelp, "&Help" );

    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText( "Ready" );

	SetBackgroundColour(wxColor(236,233,216));
	m_pStaticBox = new wxStaticBox(this, ID_WXSTATICBOX, wxT(""), wxPoint(8,5), wxSize(252,162));
	m_buttonHIDE = new wxButton(this, ID_WXBUTTON_HIDE, wxT("HIDE"), wxPoint(14,125), wxSize(240,35), 0, wxDefaultValidator, wxT("WxButtonHIDE"));
	m_pBitmapEye = new wxStaticBitmap(this, ID_WXSTATICBITMAP_EYE, wxBitmap("IDB_BITMAP_EYE"), wxPoint(10, 13), wxSize(248, 38));
	m_buttonHIDE->SetFont(wxFont(14, wxSWISS, wxNORMAL,wxBOLD, FALSE, wxT("Arial")));
	m_buttonHIDE->SetDefault();
	m_pHeaderText = new wxStaticText(this, ID_WXSTATICTEXT_HEADER, wxT("Welcome to Eye Care!"), wxPoint(19, 60), wxSize(232, 20), wxALIGN_LEFT);
	m_pHeaderText->SetFont(wxFont(12, wxSWISS, wxNORMAL,wxBOLD, FALSE, wxT("Times")));
	m_pDetailText = new wxStaticText(this, ID_WXSTATICTEXT_DETAIL, wxT("This program will remind you at regular intervals to take care of your eyes and relax/wash them."), wxPoint(20, 85), wxSize(232, 40), wxALIGN_LEFT);
	this->Center();
	// set the icon
    // this icon type doesn't seem to work in Linux
    #ifdef WIN32
        this->SetIcon(wxIcon("IDI_ICON_APP"));
    #endif

	UpdateConfig();
	m_pTaskBarIcon = new CEyeCareTaskBarIcon(this);
	if(m_bHideTrayIcon == false)
		m_pTaskBarIcon->SetIcon(wxICON(IDI_ICON_APP), wxT("Eye Care"));

	m_pMainWindowTimer = new wxTimer(this, ID_MAINWINDOWTIMER);
	m_pMainWindowTimer->Start(15000);
	m_pRelaxTimer = new wxTimer(this, ID_EYERELAX_TIMER);
	m_pWashTimer = new wxTimer(this, ID_EYEWASH_TIMER);
	m_pRelaxTimer->Start(m_nRelaxTime);
	m_pWashTimer->Start(m_nWashTime);

	wxAcceleratorEntry entries[1];
	entries[0].Set(wxACCEL_NORMAL,  (int) 27, ID_HIDE);
	wxAcceleratorTable accel(1, entries);
	this->SetAcceleratorTable(accel);

#ifdef WIN32
	HMENU pSysMenu = (HMENU )GetSystemMenu((HWND)this->m_hWnd,  FALSE);
	if (pSysMenu != NULL)
	{
		AppendMenu(pSysMenu, MF_SEPARATOR, NULL, NULL);
		AppendMenu(pSysMenu, MF_STRING|MF_BYPOSITION, ID_ABOUT, "&About...");
	}
#endif
}

void CEyeCareFrame::OnHideButton(wxCommandEvent& WXUNUSED(event))
{
	OnHide();
}

void CEyeCareFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
	m_pMainWindowTimer->Stop();
	this->Destroy();
}

void CEyeCareFrame::OnConfig(wxCommandEvent& WXUNUSED(event))
{
	CConfigDialog *dlgConfig = new CConfigDialog(this, IDD_EYECARE_CONFIG_DIALOG);
	if(wxID_OK == dlgConfig->ShowModal())
	{
		UpdateConfig();
	}
	delete dlgConfig;
}

void CEyeCareFrame::ExitApp(void)
{
	m_pMainWindowTimer->Stop();
	this->Destroy();
}

void CEyeCareFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
	ShowAboutDialog();
}

void CEyeCareFrame::OnEsc(wxCommandEvent& WXUNUSED(event))
{
	OnHide();
}

void CEyeCareFrame::ShowAboutDialog(void)
{
	CAboutDialog *dlgAbout = new CAboutDialog(this, IDD_EYECARE_ABOUT_DIALOG, "About", wxDefaultPosition, wxSize(250, 150), wxDEFAULT_DIALOG_STYLE); 
	dlgAbout->SetText(wxT("Eye Care Ver 1.01"));
	dlgAbout->ShowModal();
	delete dlgAbout;
}

void CEyeCareFrame::OnHideMainWindow(wxTimerEvent& WXUNUSED(event))
{
	this->Hide();
	if(m_pHeaderText->GetLabel() == wxT("Eye relaxing time...") || m_pHeaderText->GetLabel() == wxT("Eye washing time..."))
		wxBell();
	m_pHeaderText->SetLabel(wxT("Welcome to Eye Care!"));
	m_pDetailText->SetLabel(wxT("This program will remind you at regular intervals \nto take care of your eyes and relax/wash them."));
	m_pMainWindowTimer->Stop();
}

void CEyeCareFrame::ShowMainWindow(bool bShow)
{
	if(bShow)
	{
		m_pMainWindowTimer->Start(30000);
		this->Show();
		SetBackgroundColour(wxColor(236,233,216));
	}
	else
	{
		m_pMainWindowTimer->Stop();
		this->Hide();
		m_pHeaderText->SetLabel(wxT("Welcome to Eye Care!"));
		m_pDetailText->SetLabel(wxT("This program will remind you at regular intervals \nto take care of your eyes and relax/wash them."));
	}
}

void CEyeCareFrame::OnEyeRelaxTimer(wxTimerEvent& WXUNUSED(event))
{
	m_pHeaderText->SetLabel(wxT("Eye relaxing time..."));
	m_pDetailText->SetLabel(wxT("Now move your eyes away, from the monitor \nand view some distant object for 15 seconds."));
	ShowMainWindow(true);
	m_pRelaxTimer->Stop();
	m_pRelaxTimer->Start(m_nRelaxTime);
}

void CEyeCareFrame::OnEyeWashTimer(wxTimerEvent& WXUNUSED(event))
{
	m_pHeaderText->SetLabel(wxT("Eye washing time..."));
	m_pDetailText->SetLabel(wxT("Now move to the wash room, and gently wash \nyour eyes with water to prevent dryness."));
	ShowMainWindow(true);
	m_pRelaxTimer->Stop();
	m_pRelaxTimer->Start(m_nRelaxTime);
	m_pWashTimer->Stop();
	m_pWashTimer->Start(m_nWashTime);
}

void CEyeCareFrame::RelaxTimerSwitch(bool bON)
{
	if(bON)
	{
		m_pRelaxTimer->Start(m_nRelaxTime);
		m_pTaskBarIcon->SetIcon(wxICON(IDI_ICON_APP), wxT("Eye Care"));
	}
	else
	{
		m_pRelaxTimer->Stop();
		m_pTaskBarIcon->SetIcon(wxICON(IDI_ICON_TEARS), wxT("Eye Care"));
	}
}

bool CEyeCareFrame::IsTimersRunning(void)
{
	return (m_pRelaxTimer->IsRunning()) ? true:false;
}

void CEyeCareFrame::UpdateConfig(void)
{
	m_nRelaxTime = CConfigDialog::GetRelaxTime();
	m_nWashTime = CConfigDialog::GetWashTime();
	m_bStayOnTop = CConfigDialog::IsStayOnTop();
	m_bHideTrayIcon = CConfigDialog::IsHideIcon();
	if(m_bHideTrayIcon == false && m_pTaskBarIcon)
		m_pTaskBarIcon->SetIcon(wxICON(IDI_ICON_APP), wxT("Eye Care"));
	else if(m_pTaskBarIcon)
		m_pTaskBarIcon->RemoveIcon();
	if(m_bStayOnTop == true)
		SetWindowStyleFlag(MAINFrame_STYLE);
	else
		SetWindowStyleFlag(wxCAPTION | wxSYSTEM_MENU | wxMINIMIZE_BOX | wxCLOSE_BOX);
}

void CEyeCareFrame::OnEraseBackground(wxEraseEvent& event)
{
	SetBackgroundColour(wxColor(236,233,216));
	event.Skip();
}

void CEyeCareFrame::OnHide(void)
{
	m_pMainWindowTimer->Stop();
	this->Hide();
	m_pHeaderText->SetLabel(wxT("Welcome to Eye Care!"));
	m_pDetailText->SetLabel(wxT("This program will remind you at regular intervals \nto take care of your eyes and relax/wash them."));
}

#ifdef WIN32
WXLRESULT CEyeCareFrame::MSWWindowProc ( WXUINT message, WXWPARAM wParam, WXLPARAM lParam )
{
        switch ( message )
        {
		case WM_SYSCOMMAND:
			if(wParam == ID_ABOUT)
			{
				ShowAboutDialog();
			}
			break;
        }
        return wxFrame::MSWWindowProc ( message, wParam, lParam );
}
#endif

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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions