// Include Gtkmm and Compdigitec Utils
#include <gtkmm.h>
#include <stdio.h>
#include <iostream.h>
#include <string>
#include "cdtutils.h"

class MainWindow : public Gtk::Window {
	public:
	// Define Global Varibles

	// Define Controls
	Gtk::Entry entry1; // Value 1 Control
	Gtk::Label label2; // Value 1 Label
	Gtk::Entry entry2; // Value 2 Control
	Gtk::Label label3; // Value 2 Label
	Gtk::VBox vbox1;  // Overall Box
	Gtk::HBox hbox1;  // Row 1
	Gtk::HBox hbox2;  // Row 2
	Gtk::HBox hbox3;  // Row 3
	Gtk::HBox hbox4;  // Row 4
	Gtk::HBox hbox5;  // Row 5
	Gtk::Label label1; // Answer Control
	Gtk::Label label4; // Answer Label
	Gtk::Button button1;
	Gtk::Button button2;
	Gtk::Button button3;
	Gtk::Button button4;
	Gtk::Button buttonAbout;
//	Gtk::Button buttonHelp;

	// Create Reference
	MainWindow* window;
	MainWindow() {
		// Initialize Global Varibles

		// Set up reference
		window = this;
		// Declare button functions
		button1.signal_clicked().connect( sigc::mem_fun(*this,&MainWindow::button1_clicked) );
		button1.set_label("Add");
		button2.signal_clicked().connect( sigc::mem_fun(*this,&MainWindow::button2_clicked) );
		button2.set_label("Subtract");
		button3.signal_clicked().connect( sigc::mem_fun(*this,&MainWindow::button3_clicked) );
		button3.set_label("Multiply");
		button4.signal_clicked().connect( sigc::mem_fun(*this,&MainWindow::button4_clicked) );
		button4.set_label("Divide");
		buttonAbout.signal_clicked().connect( sigc::mem_fun(*this,&MainWindow::buttonAbout_clicked) );
		buttonAbout.set_label("About CalculatIt");
	//	buttonHelp.signal_clicked().connect( sigc::mem_fun(*this,&MainWindow::buttonHelp_clicked) );
	//	buttonHelp.set_label("Help");
		// Set window title
		window->set_title("CalculatIt v3");
		// Resize Window
		window->resize(279,380); // Width,Height
		// Set up display
		label1.set_text("0");
		label1.set_use_markup(true);
    	        label1.set_selectable(true);
		label2.set_text("<b>Value 1</b>");
		label2.set_use_markup(true);
    	        label2.set_selectable(false);
		label3.set_text("<b>Value 2</b>");
		label3.set_use_markup(true);
    	        label3.set_selectable(false);
		label4.set_text("<b>Answer</b>");
		label4.set_use_markup(true);
    	        label4.set_selectable(false);
		// Add container
		window->add(vbox1);
		vbox1.show();
		// Pack the HBoxes
		vbox1.pack_start(hbox1);
		hbox1.show();
		vbox1.pack_start(hbox2);
		hbox2.show();
		vbox1.pack_start(hbox3);
		hbox3.show();
		vbox1.pack_start(hbox4);
		hbox4.show();
		vbox1.pack_start(hbox5);
		hbox5.show();
		// Pack HBox1
		hbox1.pack_start(label2);
		label2.show();
		hbox1.pack_start(entry1);
		entry1.show();
		// Pack HBox2
		hbox2.pack_start(label3);
		label3.show();
		hbox2.pack_start(entry2);
		entry2.show();
		// Pack HBox3
		hbox3.pack_start(label4);
		label4.show();
		hbox3.pack_start(label1);
		label1.show();
		// Pack HBox4
		hbox4.pack_start(button1);
		button1.show();
		hbox4.pack_start(button2);
		button2.show();
		hbox4.pack_start(button3);
		button3.show();
		hbox4.pack_start(button4);
		button4.show();
		// Pack HBox5
	//	hbox5.pack_start(buttonHelp);
	//	buttonHelp.show();
		hbox5.pack_start(buttonAbout);
		buttonAbout.show();

	} 
	virtual ~MainWindow() {
		
	};
	void button1_clicked() {
	  // Get Values (as strings)
	  string sval1 = entry1.get_text();	
	  string sval2 = entry2.get_text();
	  // Now converting them to doubles :)	
	  double val1 = StrToDouble(sval1);
	  double val2 = StrToDouble(sval2);
	  // and finally getting the answer
	  double answer = val1 + val2;
	  // ...... and displaying it
	  char newstring[50];
	  string tempstring = "";
	  sprintf(newstring,"%g",answer);
	  tempstring = newstring;
	  label1.set_text(tempstring); // Now!
	};
	void button2_clicked() {
	  // Get Values, again
	  string sval1 = entry1.get_text();	
	  string sval2 = entry2.get_text();
	  // Now converting them to doubles ...	
	  double val1 = StrToDouble(sval1);
	  double val2 = StrToDouble(sval2);
	  // Toot-Toot...
	  double answer = val1 - val2;
	  // ...... and displaying it to the user.
	  char newstring[50];
	  string tempstring = "";
	  sprintf(newstring,"%g",answer);
	  tempstring = newstring;
	  label1.set_text(tempstring); 
	};
	void button3_clicked() {// (Comments Stripped)
	  string sval1 = entry1.get_text();	
	  string sval2 = entry2.get_text();
	  double val1 = StrToDouble(sval1);
	  double val2 = StrToDouble(sval2);
	  double answer = val1 * val2;
	  char newstring[50];
	  string tempstring = "";
	  sprintf(newstring,"%g",answer);
	  tempstring = newstring;
	  label1.set_text(tempstring);
	};
	void button4_clicked() {// (Comments Stripped)
	  string sval1 = entry1.get_text();	
	  string sval2 = entry2.get_text();
	  double val1 = StrToDouble(sval1);
	  double val2 = StrToDouble(sval2);
	  	// Hold it right there...
	  // Check for Divide by 0's
	  if(val2 == 0) {
		Gtk::MessageDialog dialog("",false,Gtk::MESSAGE_ERROR);
		dialog.set_message("Calculating Error");
		dialog.set_secondary_text("You cannot divide by 0... Please try again!");
		dialog.run();
		dialog.hide();
	  	val2 = 1; // Bypass Division and hope the user doesn't notice...
	  };

	  double answer = val1 / val2;
	  char newstring[50];
	  string tempstring = "";
	  sprintf(newstring,"%g",answer);
	  tempstring = newstring;
	  label1.set_text(tempstring);
	};
	void buttonAbout_clicked() {
	  	Gtk::MessageDialog dialog("");
		dialog.set_message("About CalculatIt");
		dialog.set_secondary_text("Compdigitec Calculatit v3.1 \r\nCreated by Compdigitec \r\nWritten using gtkmm with GTK+ and distributed under the LGPL. \r\n \r\nCheck for latest updates at http://www.compdigitec.com/ \r\nYou can also email us at reviews.compdigitec@gmail.com \r\n \r\n(C)2008 Compdigitec. All rights reserved.");
		dialog.run();
		dialog.hide(); 
	};
	void buttonHelp_clicked() {
	  	
	};

};

int main(int argc, char* argv[]) {
	
	Gtk::Main kit(argc, argv);

	MainWindow window;

	Gtk::Main::run(window);
	return 0;
	
}
