Code:
#define CELSIUS_BUTTON_ID 1
#define FAHRENHEIT_BUTTON_ID 2
#include "wx/wx.h"
class CelsiusFahrenheit: public wxApp {
public:
//Variaveis globais
wxTextCtrl* celsius_text;
wxTextCtrl* fahrenheit_text;
wxTextCtrl* output_text;
//Metodo OnInit, iniciador da aplicacao
bool OnInit() {
//Inicializa a janela
int comprimento_janela = 400;
int largura_janela = 300;
wxSize tamanho_janela(comprimento_janela,largura_janela);
wxString titulo("Conversor Celsius-Fahrenheit-Celsius");
//Janela com:
//-CAPTION (barra de titulo)
//-CLOSE BOX (botao fechar no canto superior direito)
//-SYSTEM MENU (conjunto de botoes minimizar, maximizar, fechar)
/*
NOTA: Apenas os botoes do SYSTEM MENU que forem especificados
(neste caso, apenas a CLOSE BOX foi especificada) aparecem na barra
superior.
*/
long estilo_janela = wxCAPTION | wxCLOSE_BOX | wxSYSTEM_MENU;
wxFrame* janela = new wxFrame(NULL, -1, titulo, wxDefaultPosition, tamanho_janela, estilo_janela);
wxColour cor_de_fundo(48, 214, 100);
janela->SetBackgroundColour(cor_de_fundo);
//Inicializa os controlos de texto
int celsius_text_x = 0;
int celsius_text_y = 0;
int fahrenheit_text_x = celsius_text_x;
int fahrenheit_text_y = celsius_text_y + 50;
int output_text_x = celsius_text_x;
int output_text_y = fahrenheit_text_y + 50;
wxPoint posicao_celsius_text(celsius_text_x,celsius_text_y);
wxPoint posicao_fahrenheit_text(fahrenheit_text_x,fahrenheit_text_y);
wxPoint posicao_output_text(output_text_x,output_text_y);
wxSize tamanho_output(400,largura_janela-output_text_y);
wxSize tamanho_texto(200, 25);
celsius_text = new wxTextCtrl(janela, -1, "", posicao_celsius_text, tamanho_texto);
fahrenheit_text = new wxTextCtrl(janela, -1, "", posicao_fahrenheit_text, tamanho_texto);
output_text = new wxTextCtrl(janela, -1, "", posicao_output_text, tamanho_output, wxTE_READONLY);
*celsius_text << "Valor em graus Celsius";
*fahrenheit_text << "Valor em graus Fahrenheit";
*output_text << "Aparecera aqui o resultado de uma das conversoes.";
//Inicializa os botoes
wxSize tamanho_botoes(200, 25);
wxPoint posicao_celsius_button(celsius_text_x + tamanho_texto.GetWidth(),celsius_text_y);
wxPoint posicao_fahrenheit_button(fahrenheit_text_x + tamanho_texto.GetWidth(),fahrenheit_text_y);
wxButton* celsius_button = new wxButton(janela, CELSIUS_BUTTON_ID, "", posicao_celsius_button, tamanho_botoes);
wxButton* fahrenheit_button = new wxButton(janela, FAHRENHEIT_BUTTON_ID, "", posicao_fahrenheit_button, tamanho_botoes);
celsius_button->SetLabel("Converter Celsius para Fahrenheit");
fahrenheit_button->SetLabel("Converter Fahrenheit para Celsius");
janela->Show(true);
return true;
}
//Metodo OnExit, terminador da aplicacao
int OnExit() {
delete celsius_text;
delete fahrenheit_text;
delete output_text;
return 0;
}
//Tratadores de eventos
void OnCelsiusClick(wxCommandEvent& event) {
wxString s = celsius_text->GetValue();
double d = 0.0;
if (!s.ToDouble(&d)) {
wxMessageDialog exception_dialog(NULL,"Nao foi introduzido um valor decimal!","Erro!");
exception_dialog.ShowModal();
return;
}
double resultado = d/5.0*9.0+32; //Formula para converter um valor de Celsius para Fahrenheit
output_text->SetValue("");
*output_text << d << " graus Celsius correspondem a " << resultado << " graus Fahrenheit.";
}
void OnFahrenheitClick(wxCommandEvent& event) {
wxString s = fahrenheit_text->GetValue();
double d = 0.0;
if (!s.ToDouble(&d)) {
wxMessageDialog exception_dialog(NULL,"Nao foi introduzido um valor decimal!","Erro!");
exception_dialog.ShowModal();
return;
}
double resultado = (d-32)/9.0*5.0; //Formula para converter um valor de Fahrenheit para Celsius
output_text->SetValue("");
*output_text << d << " graus Fahrenheit correspondem a " << resultado << " graus Celsius.";
}
//Instrucao necessaria para que esta classe possa tratar eventos
DECLARE_EVENT_TABLE()
};
//Eventos da classe CelsiusFahrenheit
BEGIN_EVENT_TABLE(CelsiusFahrenheit, wxApp)
EVT_BUTTON(CELSIUS_BUTTON_ID, CelsiusFahrenheit::OnCelsiusClick)
EVT_BUTTON(FAHRENHEIT_BUTTON_ID, CelsiusFahrenheit::OnFahrenheitClick)
END_EVENT_TABLE()
//Iniciar a aplicacao
IMPLEMENT_APP(CelsiusFahrenheit)