Headers
App.h
#ifndef APP_H
#define APP_H
#include
class App : public BApplication
{
public:
App(void);
};
#endif
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
class MainWindow : public BWindow
{
public:
MainWindow(void);
void MessageReceived(BMessage *msg);
bool QuitRequested(void);
BTextControl *txtCelsius;
BTextControl *txtFahrenheit;
void CalculateCelsius();
void CalculateFahrenheit();
void CalculateTemperature();
private:
};
#endif
Implementation
App.cpp
#include "App.h"
#include "MainWindow.h"
App::App(void)
: BApplication("application/x-vnd.TempCalc")
{
MainWindow *mainwin = new MainWindow();
mainwin->Show();
}
int
main(void)
{
App *app = new App();
app->Run();
delete app;
return 0;
}
MainWindow.cpp
#include "MainWindow.h"
#include
#include
#include
#include
#include
#include
enum {
M_BUTTON_CALC = 'btcl',
M_BUTTON_QUIT = 'btqt',
TXT_CELSIUS = 'txce',
TXT_FAHRENHEIT = 'txfa'
};
void MainWindow::CalculateCelsius() {
float kCelsius;
float kFahrenheit;
char strCelsius[20];
// convert to float
sscanf(txtCelsius->Text(),"%f",&kCelsius);
sscanf(txtFahrenheit->Text(),"%f",&kFahrenheit);
// calculate and set
kCelsius = (kFahrenheit - 32) * 0.55555555556;
// kCelsius = (kFahrenheit - 32) * (5 / 9); <-- 5 div 9 is treated as int not float and fails
sprintf(strCelsius,"%.1f",kCelsius);
txtCelsius->SetText(strCelsius);
}
void MainWindow::CalculateFahrenheit() {
float kCelsius;
float kFahrenheit;
char strFahrenheit[20];
// convert to float
sscanf(txtCelsius->Text(),"%f",&kCelsius);
sscanf(txtFahrenheit->Text(),"%f",&kFahrenheit);
// calculate and set
kFahrenheit = ((kCelsius * 9) / 5) + 32;
sprintf(strFahrenheit,"%.1f",kFahrenheit);
txtFahrenheit->SetText(strFahrenheit);
}
void MainWindow::CalculateTemperature() {
float kCelsius;
float kFahrenheit;
int intCelsius = 0;
int intFahrenheit = 0;
// convert to float
sscanf(txtCelsius->Text(),"%f",&kCelsius);
sscanf(txtFahrenheit->Text(),"%f",&kFahrenheit);
// convert to int
sscanf(txtCelsius->Text(),"%d",&intCelsius);
sscanf(txtFahrenheit->Text(),"%d",&intFahrenheit);
if (intCelsius == 0 && intFahrenheit == 0) {
// Reset Values
txtCelsius->SetText("");
txtFahrenheit->SetText("");
} else {
if (intFahrenheit == 0) {
// Calculate Fahrenheit
MainWindow::CalculateFahrenheit();
} else {
// Calculate Celsius
MainWindow::CalculateCelsius();
}
}
}
MainWindow::MainWindow(void)
: BWindow(BRect(100,100,390,220),"Temperature Calculator",B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE)
{
txtCelsius = new BTextControl(BRect(25,20,180,20),"txtCelsius","Celsius:","",new BMessage(TXT_CELSIUS));
txtFahrenheit = new BTextControl(BRect(25,50,180,50),"txtFahrenheit","Fahrenheit:","",new BMessage(TXT_FAHRENHEIT));
AddChild(txtCelsius);
AddChild(txtFahrenheit);
txtCelsius->MakeFocus();
BButton *btnCalculate = new BButton(BRect(125,90,11,11),"btnCalculate","Calculate",new BMessage(M_BUTTON_CALC));
BButton *btnQuit = new BButton(BRect(210,90,11,11),"btnQuit","Quit",new BMessage(M_BUTTON_QUIT));
btnCalculate->ResizeToPreferred();
btnQuit->ResizeToPreferred();
AddChild(btnCalculate);
AddChild(btnQuit);
BView *view = new BView(BRect(0,0,290,180),"backgroundview",B_FOLLOW_ALL,B_WILL_DRAW);
AddChild(view);
view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
view->Invalidate();
}
void
MainWindow::MessageReceived(BMessage *msg)
{
switch (msg->what)
{
case TXT_CELSIUS:
{
MainWindow::CalculateFahrenheit();
break;
}
case TXT_FAHRENHEIT:
{
MainWindow::CalculateCelsius();
break;
}
case M_BUTTON_CALC:
{
MainWindow::CalculateTemperature();
break;
}
case M_BUTTON_QUIT:
{
QuitRequested();
break;
}
default:
{
BWindow::MessageReceived(msg);
break;
}
}
}
bool
MainWindow::QuitRequested(void)
{
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
}