Thursday, November 25, 2010

Calculator - Full version

// Calculator

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <sys/api.h>
#include <sys/window.h>

#define FIELD_X        5
#define FIELD_Y        4
#define MAX_DIGITS    13
#define MAX_FIELD_DIGITS 15
double equal(void);

static objectKey hWindow;
static objectKey TextBox;
static objectKey ButtonsContainer;
//static objectKey BackgroundImage;
char operator;
long number1 = 0;
long number2 = 0;
long rezult = 0;
int checkOperatie = 0;
char number1String[30];
char tempString[30];
char number2String[30];
int checkMinus = 0;
int checkPunct = 0;
int lengthString = 0;
//int number2 = 0;
//static objectKey refresh;
static objectKey gridButtons[FIELD_X][FIELD_Y];
char *title[23] = {  
            "  "    ,
            " 7 "    ,
            " 8 "    ,
            " 9 "    ,
            " / "    ,
            " C "    ,
            " 4 "    ,
            " 5 "    ,
            " 6 "    ,
            " * "    ,
            " <- "    ,
            " 1 "    ,
            " 2 "    ,
            " 3 "    ,
            " - "    ,
            " 1/x "    ,
            " 0 "    ,
            " +/- "    ,
            " . "    ,
            " + "    ,
            " = "    };
  


double equal(void)
{
    double rezultatul = 0;
    //trebuie ca cRez sa fie 1, altfel conversia eronata
    //printf("%lld --- %lld",number1,number2);
    number1 = atoull(number1String);
    number2 = atoull(number2String);
  
    if (operator == '+')
        rezultatul = number1 + number2;
    if (operator == '-')
        rezultatul = number2 - number1;
    if (operator == '/')
        rezultatul = number2 / number1;
    if (operator == '*')
        rezultatul = number2 * number1;
return rezultatul;
}
  

static void eventHandler(objectKey key, windowEvent *event)
{
    lengthString = strlen(number1String);
    if ((key == hWindow) && (event->type == EVENT_WINDOW_CLOSE))
            windowGuiStop();
  
    if (event->type == EVENT_MOUSE_LEFTUP)
    {
        if (key == gridButtons[0][0]) // 7
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "7");
        }
        if (key == gridButtons[1][0]) // 8
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "8");
        }
        if (key == gridButtons[2][0]) // 9
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "9");
        }
        if (key == gridButtons[3][0]) // /
        {
            strcpy(number2String, number1String);
            strcpy(number1String, "");
            operator = '/';
            checkOperatie = 1;
        }
        if (key == gridButtons[4][0]) // C
        {
            strcpy(number2String, "");
            strcpy(number1String, "");
            checkOperatie = 0;
            operator = NULL;
            checkPunct = 0;
        }
        if (key == gridButtons[0][1]) // 4
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "4");
        }
        if (key == gridButtons[1][1]) // 5
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "5");
        }
        if (key == gridButtons[2][1]) // 6
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "6");
        }
        if (key == gridButtons[3][1]) // *
        {
            strcpy(number2String, number1String);
            strcpy(number1String, "");
            operator = '*';
            checkOperatie = 1;
        }
        if (key == gridButtons[4][1]) // backspace
        {
            if ((strlen(number1String) > 0))
            {
                    strcpy(tempString,"");
                lengthString = lengthString - 1;
                number1String[lengthString]=0;
            }
        }
        if (key == gridButtons[0][2]) // 1
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "1");
        }
        if (key == gridButtons[1][2]) // 2
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "2");
        }
        if (key == gridButtons[2][2]) // 3
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "3");
        }
        if (key == gridButtons[3][2]) // -
        {
            strcpy(number2String, number1String);
            strcpy(number1String, "");
            operator = '-';
            checkOperatie = 1;
        }
        if (key == gridButtons[4][2]) // 1/x
        {
            strcpy(number2String, number1String);
            strcpy(number1String, "");
            operator = 'x';
            checkOperatie = 1;
        }
        if (key == gridButtons[0][3]) // 0
        {
            if (strlen(number1String) < MAX_DIGITS)
                strcat(number1String, "0");
        }
        if (key == gridButtons[1][3]) // +/-
        {
            if (checkMinus == 0)
            {
                checkMinus = 1;
            }
            else
            {
                checkMinus = 0;
            }
        }
        if (key == gridButtons[2][3]) // .
        {
            if (strlen(number1String) < MAX_DIGITS)
            {
                if (checkPunct == 0)
                {
                    strcat(number1String, ".");
                    checkPunct = 1;
                }
            }

        }
        if (key == gridButtons[3][3]) // +
        {
            strcpy(number2String, number1String);
            strcpy(number1String, "");
            operator = '+';
            checkOperatie = 1;
        }
        if (key == gridButtons[4][3]) // =
        {
            if (operator)
            {
                rezult = equal();
                lltoa(rezult, number1String);
                if (strlen(number1String) > MAX_DIGITS)
                    strcpy(number1String, "Too large\0");
            }
        }
      
              
    if (strlen(number1String) < MAX_DIGITS)
        windowComponentSetData(TextBox, number1String, strlen(number1String));
    //windowComponentDraw(TextBox);
    }


}


static void constructButtons(void)
{
    componentParameters params;
    componentParameters fonts;
    // BGR = Blue, Green, Red
    color fColor[23] = {
                    {0,0,255},//
                    {255,0,0},// 7
                    {255,0,0},// 8
                    {255,0,0},// 9
                    {0,0,255},// /
                    {0,0,255},// C
                    {255,0,0},// 4
                    {255,0,0},// 5
                    {255,0,0},// 6
                    {0,0,255},// *
                    {0,0,255},// backspace
                    {255,0,0},// 1
                    {255,0,0},// 2
                    {255,0,0},// 3
                    {0,0,255},// -
                    {255,0,0},// 1/x
                    {255,0,0},// 0
                    {255,0,0},// +/-
                    {255,0,0},// .
                    {0,0,255},// +
                    {0,0,255} // =
                };
                  
    int x = 1;
    int y = 1;
    int countX = 0;
    int countY = 20;
    int num = 0;
    int status = 0;
    //image back;
  
    //status = imageLoad("/programs/calc.dir/back.bmp", 166, 202,&back);
    status = fontLoad("/system/fonts/arial-normal-10.bmp", "arial-normal-10", &(fonts.font), 8);
    //BackgroundImage = windowNewImage(hWindow, &back, draw_normal, &params);
  
    bzero(&params, sizeof(componentParameters));
    params.gridX = 0;
    params.gridY = 0;
    params.gridWidth = 1;
    params.gridHeight = 1;
    params.padTop = 18;
    params.orientationX = orient_center;
    params.orientationY = orient_bottom;
    ButtonsContainer = windowNewContainer(hWindow, "Buttons",&params);
  
      
    params.gridX = x+1;
    params.gridY = y+1;
    params.padTop = 1; //32
    params.gridWidth = 1;
    params.gridHeight = 1;//1
    params.orientationX = orient_center;
    params.orientationY = orient_bottom;
    params.flags |= WINDOW_COMPFLAG_CUSTOMFOREGROUND;

    for (countY = 0; countY < FIELD_Y; countY++)
    {
        params.gridY = countY;
        for (countX = 0; countX < FIELD_X; countX++)
        {
            params.gridX = countX;
            num += 1;
      

            params.foreground = fColor[num];
            gridButtons[countX][countY] = windowNewButton(ButtonsContainer, title[num], NULL,&params);
            windowRegisterEventHandler(gridButtons[countX][countY], &eventHandler);
            if (num > 20)
                num = 0;
        }
    }

    /*params.gridX += 1;
    params.gridY = 0;
    params.padTop = 1;
    params.padRight = 1;
    params.flags |= WINDOW_COMPFLAG_HASBORDER;
    params.gridWidth = 0;
    params.gridHeight = 32;//1
    params.orientationX = orient_right;
    params.orientationY = orient_top;
    refresh = windowNewButton(hWindow, "   c   ", NULL,&params);*/


    params.gridX = 0;
    params.gridY = 0;
    params.padTop = 1;
    params.font = fonts.font;
    params.padLeft = 1;
    params.flags |= WINDOW_COMPFLAG_HASBORDER;
    params.gridWidth = 0;
    params.gridHeight = 1;//1
    params.orientationX = orient_left;
    params.orientationY = orient_top;
    params.foreground = ((color){0,0,0});
    //params.flags |= !WINDOW_COMPFLAG_CUSTOMFOREGROUND;
    TextBox = windowNewTextField(hWindow, MAX_FIELD_DIGITS,&params);
    windowRegisterEventHandler(TextBox, &eventHandler);
    windowComponentSetEnabled(TextBox, 0);
  
  



}



__attribute__((noreturn))
void main(void)
{
    int status = 0;
    hWindow = windowNew(multitaskerGetCurrentProcessId(), "Calculator");
    windowRegisterEventHandler(hWindow, &eventHandler);
    constructButtons();
    status = windowSetResizable(hWindow, 0);
    //windowSetSize(hWindow, 260, 260);
    status = windowSetVisible(hWindow, 1);
    windowGuiRun();

multitaskerYield();
while(1);
}

Monday, November 15, 2010

New department

A new department was established, Vanesika Software Department.