<?php

/*******************************************************************************

 WINBINDER - A native Windows binding for PHP

 Copyright  2004-2005 Hypervisual - see LICENSE.TXT for details
 Author: Rubem Pechansky (http://www.hypervisual.com/winbinder/contact.php)

 Code sample: Mini-calculator application

*******************************************************************************/

//------------------------------------------------------------ SYSTEM PARAMETERS

define("PATH_SCRIPT",   dirname(__FILE__) . "/");
define("PATH_DATA",     PATH_SCRIPT);
define("PATH_INC",      PATH_SCRIPT "../include/");
define("PATH_RES",      PATH_SCRIPT "../resources/");

//----------------------------------------------------------------- DEPENDENCIES

include PATH_INC "winbinder.php";

//-------------------------------------------------------------------- CONSTANTS

define("APPNAME",   "WinBinder Calculator");    // Application name
define("BLANK",     "blank");

//----------------------------------------------------------------- DEPENDENCIES

//------------------------------------------------------------- GLOBAL VARIABLES

/*
$newnumber = BLANK;
$memory = 0;
$display = "";
$number1 = "";
$number2 = "";
$opvalue = "";
}*/

//-------------------------------------------------------------- EXECUTABLE CODE

// Create main window from RC template

eval(parse_rc(file_get_contents(PATH_SCRIPT "calc.rc"), '$mainwin'null'PopupWindow'));

// Set main window handler and enter application loop

wb_set_handler($mainwin"process_main");
wb_main_loop();

//-------------------------------------------------------------------- FUNCTIONS

// TODO: Calculator is not working properly
// TODO: Use keyboard to enter keys

// Ported and adapted from original JavaScript calculator by Steve Dulaney
// http://www.hmhd.com/steve

/* Process main window commands */

function process_main($window$id)
{
    global 
$memory$display;

    switch(
$id) {

        case 
IDCLOSE:           // Predefined constant
            
wb_destroy_window($window);
            break;

        case 
IDC_DISPLAY:       // Does not process edit controls
            
break;

        default:                
// Process buttons
            
$ctrl wb_get_control($window$id);
            if(
wb_get_class($ctrl) != PushButton)
                break;
            
$caption trim(wb_get_text($ctrl));

            switch(
$caption) {

                
// Numbers and decimal point

                
case "1": case "2": case "3": case "4": case "5":
                case 
"6": case "7": case "8": case "9": case "0":
                case 
".":
                    
checknumber($caption);              break;

                
// Basic operations

                
case "+":
                    
addbutton(1);                       break;
                case 
"-":
                    
subbutton(1);                       break;
                case 
"":
                    
multbutton(1);                      break;
                case 
"":
                    
divbutton(1);                       break;

                
// Op buttons

                
case "<":
                    
backspace($display);                break;
                case 
"CE":
                    
cecalc();                           break;
                case 
"C":
                    
clearcalc();                        break;
                case 
"=":
                    
equalbutton();                      break;

                
// Advanced operations

                
case "sqrt":
                    
sqrtbutton();                       break;
                case 
"x":
                    
sqrbutton();                        break;
                case 
"%":
                    
percentbutton();                    break;
                case 
"1/x":
                    
recipbutton();                      break;
                case 
"+/-":
                    
negatebutton();                     break;

                
// Memory buttons

                
case "M-":
                    
memorysubtract($display);           break;
                case 
"M+":
                    
memoryadd($display);                break;
                case 
"MR":
                    
memoryrecall($memory);              break;
                case 
"MC":
                    
memoryclear();                      break;

            } 
// switch($caption)
            
break;
    } 
// switch($id)
}

function 
display($displaynumber)
{
    global 
$mainwin$display$memory;

    
$display = (string)$displaynumber;
    
wb_set_text(wb_get_control($mainwinIDC_DISPLAY), $display);

    
$memctrl wb_get_control($mainwinIDC_MEM);
    
wb_set_visible($memctrl$memory);
    if(
$memory)
        
wb_refresh($memctrl);
}

function 
checknumber($answer)
{
    global 
$number1$number2$newnumber$display;

    if(
$answer == ".") {
        
$n $display;
        if(
strpos($n'.')) {
            
$answer "";
        }
    }
    if(
$newnumber == TRUE) {
        
$number2 .= $answer;
        
display($number2);
    } else {
        if(
$newnumber == BLANK) {
            
$number1 $answer;
            
$number2 "";
            
$newnumber FALSE;
        } else {
            
$number1 .= $answer;
        }
        
display($number1);

    }
}

function 
memoryclear()
{
    global 
$memory$display;

    
$memory 0;
    
display($display);
}

function 
memoryrecall($answer)
{
    global 
$newnumber$number1$number2;

    if(
$newnumber != BLANK) {
        
$number2 .= $answer;
    } else {
        
$number1 $answer;
    }
    
$newnumber BLANK;
    
display($answer);
}

function 
memorysubtract($answer)
{
    global 
$memory$display;

    
$memory $memory - (double)$answer;
    
display($display);
}

function 
memoryadd($answer)
{
    global 
$memory$newnumber$display;

    
$memory $memory + (double)$answer;
    
$newnumber BLANK;
    
display($display);
}

function 
clearcalc()
{
    global 
$newnumber$number1$number2;

    unset(
$GLOBALS['number1']);
    unset(
$GLOBALS['number2']);
    unset(
$GLOBALS['newnumber']);
    
display("");
}

function 
backspace($answer)
{
    global 
$number1$number2;

    
$answerlength strlen($answer);
    
$answer substr($answer0$answerlength 1);

    if(
$number2 != "") {
        
$number2 = (string)$answer;
        
display($number2);
    } else {
        
$number1 = (string)$answer;
        
display($number1);
    }
}

function 
cecalc()
{
    global 
$number2$newnumber;

    
$number2 "";
    
$newnumber TRUE;
    
display("");
}

function 
addbutton($x)
{
    global 
$number1$number2$newnumber$opvalue;

    if(
$x == 1)
        
equalbutton();
    if(
$number2 != "") {
        
$number1 = (double)$number1 + (double)$number2;

    }
    
$newnumber TRUE;
    
$opvalue '+';
    
display($number1);


}

function 
subbutton($x)
{
    global 
$number1$number2$newnumber$opvalue;

    if(
$x == 1)
        
equalbutton();
    if(
$number2 != "") {
        
$number1 = (double)$number1 - (double)$number2;
    }
    
$newnumber TRUE;
    
$opvalue '-';
    
display($number1);
}

function 
multbutton($x)
{
    global 
$number1$number2$newnumber$opvalue;

    if(
$x == 1)
        
equalbutton();
    if(
$number2 != "") {
        
$number1 = (double)$number1 * (double)$number2;
    }
    
$newnumber TRUE;
    
$opvalue '*';
    
display($number1);
}

function 
divbutton($x)
{
    global 
$number1$number2$newnumber$opvalue;

    if(
$x == 1)
        
equalbutton();
    if(
$number2 != "") {
        
$number1 = (double)$number1 / (double)$number2;
    }
    
$newnumber TRUE;
    
$opvalue '/';
    
display($number1);
}

function 
sqrbutton()
{
    global 
$number1$newnumber;

    
$number1 $number1 $number1;
    
$newnumber BLANK;
    
display($number1);
}

function 
sqrtbutton()
{
    global 
$number1$newnumber;

    
$number1 sqrt($number1);
    
$newnumber BLANK;
    
display($number1);
}

function 
percentbutton()
{
    global 
$number1$number2$newnumber;

    if(
$newnumber != BLANK) {
        
$number2 *= 0.01;
        
$newnumber BLANK;
        
display($number2);
    }
}

function 
recipbutton()
{
    global 
$number1$newnumber;

    
$number1 $number1;
    
$newnumber BLANK;
    
display($number1);
}

function 
negatebutton()
{
    global 
$number1$newnumber;

    
$number1 = -(double)$number1;
    
$newnumber FALSE;
    
display($number1);
}

function 
equalbutton()
{
    global 
$number1$number2$opvalue;

    if(
$opvalue == '+')
        
addbutton(0);
    if(
$opvalue == '-')
        
subbutton(0);
    if(
$opvalue == '*')
        
multbutton(0);
    if(
$opvalue == '/')
        
divbutton(0);
    
$number2 "";
    
$opvalue "";

}


//------------------------------------------------------------------ END OF FILE

?>