From e459f02bc9e11e53e0c773d362f166b1247c19a2 Mon Sep 17 00:00:00 2001
From: Meng Hua <161220092@smail.nju.edu.cn>
Date: Thu, 22 Mar 2018 17:13:30 +0800
Subject: [PATCH] calc:Add a simple implementation.

Signed-off-by: Meng Hua <161220092@smail.nju.edu.cn>
---
 configure                 |   2 +
 configure.ac              |   1 +
 programs/calc/Makefile.in |   8 ++
 programs/calc/calc.rc     |  34 +++++
 programs/calc/main.c      | 308 ++++++++++++++++++++++++++++++++++++++++++++++
 programs/calc/resource.h  |  42 +++++++
 6 files changed, 395 insertions(+)
 create mode 100644 programs/calc/Makefile.in
 create mode 100755 programs/calc/calc.rc
 create mode 100644 programs/calc/main.c
 create mode 100755 programs/calc/resource.h

diff --git a/configure b/configure
index 6324574..546c406 100755
--- a/configure
+++ b/configure
@@ -1650,6 +1650,7 @@ enable_aspnet_regiis
 enable_attrib
 enable_cabarc
 enable_cacls
+enable_calc
 enable_clock
 enable_cmd
 enable_conhost
@@ -19209,6 +19210,7 @@ wine_fn_config_makefile programs/aspnet_regiis enable_aspnet_regiis
 wine_fn_config_makefile programs/attrib enable_attrib
 wine_fn_config_makefile programs/cabarc enable_cabarc
 wine_fn_config_makefile programs/cacls enable_cacls
+wine_fn_config_makefile programs/calc enable_calc
 wine_fn_config_makefile programs/clock enable_clock
 wine_fn_config_makefile programs/cmd enable_cmd
 wine_fn_config_makefile programs/cmd/tests enable_tests
diff --git a/configure.ac b/configure.ac
index 6a30601..da78898 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3833,6 +3833,7 @@ WINE_CONFIG_MAKEFILE(programs/aspnet_regiis)
 WINE_CONFIG_MAKEFILE(programs/attrib)
 WINE_CONFIG_MAKEFILE(programs/cabarc)
 WINE_CONFIG_MAKEFILE(programs/cacls)
+WINE_CONFIG_MAKEFILE(programs/calc)
 WINE_CONFIG_MAKEFILE(programs/clock)
 WINE_CONFIG_MAKEFILE(programs/cmd)
 WINE_CONFIG_MAKEFILE(programs/cmd/tests)
diff --git a/programs/calc/Makefile.in b/programs/calc/Makefile.in
new file mode 100644
index 0000000..7303a5d
--- /dev/null
+++ b/programs/calc/Makefile.in
@@ -0,0 +1,8 @@
+MODULE  = calc.exe
+APPMODE = -mwindows -mno-cygwin
+IMPORTS = gdi32 comdlg32 comctl32 user32
+
+C_SRCS = \
+    main.c
+
+RC_SRCS = calc.rc
diff --git a/programs/calc/calc.rc b/programs/calc/calc.rc
new file mode 100755
index 0000000..af411d6
--- /dev/null
+++ b/programs/calc/calc.rc
@@ -0,0 +1,34 @@
+/*
+ * Calculator resources
+ *
+ * Copyright 2018 Meng Hua
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ *
+ */
+
+#include "resource.h"
+
+#pragma makedep po
+
+LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT
+
+IDM_MAINMENU MENU
+BEGIN
+    POPUP "&View"
+    BEGIN
+        MENUITEM "E&xit",                       ID_FILE_EXIT
+    END
+END
diff --git a/programs/calc/main.c b/programs/calc/main.c
new file mode 100644
index 0000000..a18cb90
--- /dev/null
+++ b/programs/calc/main.c
@@ -0,0 +1,308 @@
+/*
+ * Calc - Calculator
+ *
+ * Copyright 2018 Meng Hua
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <wine/debug.h>
+#include <windows.h>
+#include <commctrl.h>
+#include <stdio.h>
+
+#include "resource.h"
+
+typedef struct BUTTON_SETTING
+{
+    char content[3];
+    int x;
+    int y;
+    int nWidth;
+    int nHeight;
+    int hMenu;
+} Buttons;
+
+HWND hwndTextView;
+char string[100] = "";
+
+static double Calc_Equation(double num0, char token, double num1)
+{
+    double result = 0;
+    switch(token){
+        case '+': result = num0 + num1; break;
+        case '-': result = num0 - num1; break;
+        case '*': result = num0 * num1; break;
+        case '/': result = num0 / num1; break;
+        default: result = 0; break;
+    }
+    return result;
+}
+
+LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+    switch(msg)
+    {
+        case WM_CLOSE:
+            DestroyWindow(hwnd);
+            break;
+        case WM_DESTROY:
+            PostQuitMessage(0);
+            break;
+        case WM_COMMAND:
+            switch(LOWORD(wParam))
+            {
+                case ID_FILE_EXIT:
+                    PostMessageA(hwnd, WM_CLOSE, 0, 0);
+                    break;
+                case ID_CALC_0:
+                {
+                    strcat(string, "0");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_1:
+                {
+                    strcat(string, "1");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_2:
+                {
+                    strcat(string, "2");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_3:
+                {
+                    strcat(string, "3");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_4:
+                {
+                    strcat(string, "4");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_5:
+                {
+                    strcat(string, "5");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_6:
+                {
+                    strcat(string, "6");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_7:
+                {
+                    strcat(string, "7");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_8:
+                {
+                    strcat(string, "8");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_9:
+                {
+                    strcat(string, "9");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_ADD:
+                {
+                    strcat(string, "+");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_SUB:
+                {
+                    strcat(string, "-");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_MUL:
+                {
+                    strcat(string, "*");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_DIVIS:
+                {
+                    strcat(string, "/");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_POINT:
+                {
+                    strcat(string, ".");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+                case ID_CALC_EQUA:
+                {
+                    char result[100] = "";
+                    double num0 = 0, num1 = 0;
+                    char token = 0;
+                    double temp;
+                    sscanf(string, "%lf%c%lf", &num0, &token, &num1);
+                    temp = Calc_Equation(num0, token, num1);
+                    sprintf(result, "%.4f", temp);
+                    strcat(string, "\n= ");
+                    strcat(string, result);
+                    SetWindowTextA(hwndTextView, string);
+                    strcpy(string, "");
+                    break;
+                }
+                case ID_CALC_C:
+                {
+                    strcpy(string, "");
+                    SetWindowTextA(hwndTextView, string);
+                    break;
+                }
+            }
+            break;
+        default:
+            return DefWindowProcA(hwnd, msg, wParam, lParam);
+    }
+    return 0;
+}
+
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
+                   LPSTR lpCmdLine, int nCmdShow)
+{
+    WNDCLASSEXA wc;
+    HWND hwnd;
+    MSG Msg;
+    Buttons Button[17];
+    HWND hwndButton[17];
+
+    InitCommonControls();
+
+    wc.cbSize = sizeof(WNDCLASSEXA);
+    wc.style = 0;
+    wc.lpfnWndProc = WndProc;
+    wc.cbClsExtra = 0;
+    wc.cbWndExtra = 0;
+    wc.hInstance = hInstance;
+    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
+    wc.lpszMenuName  = MAKEINTRESOURCEA(IDM_MAINMENU);
+    wc.lpszClassName = "Calculation";
+
+    if(!RegisterClassExA(&wc))
+    {
+        MessageBoxA(NULL, "Window Registration Failed!", "Error!",
+                    MB_ICONEXCLAMATION | MB_OK);
+        return 0;
+    }
+
+    hwnd = CreateWindowExA(0, "Calculation", "Calculation",
+                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
+                           CW_USEDEFAULT, CW_USEDEFAULT, 330, 550,
+                           NULL, NULL, hInstance, NULL);
+
+    if(hwnd == NULL)
+    {
+        MessageBoxA(NULL, "Window Creation Failed!", "Error!",
+                    MB_ICONEXCLAMATION | MB_OK);
+        return 0;
+    }
+
+    ShowWindow(hwnd, nCmdShow);
+    UpdateWindow(hwnd);
+
+    /* create Buttons */
+    for(int i = 0; i < 17; i++)
+    {
+        Button[i].x = 15 + (i % 3) * 70;
+        Button[i].y = 120 + (i / 3) * 90;
+        Button[i].nWidth = 70;
+        Button[i].nHeight = 90;
+
+        /* set special buttons such as "=" "0" */
+        if(i >= 12)
+        {
+            Button[i].nHeight = 72;
+            Button[i].x = 225;
+            Button[i].y = 120 + (i % 12) * 72;
+        }
+        /* set content */
+        switch(i)
+        {
+            case 0: {strcpy(Button[i].content, "7"); Button[i].hMenu = ID_CALC_7;} break;
+            case 1: {strcpy(Button[i].content, "8"); Button[i].hMenu = ID_CALC_8;} break;
+            case 2: {strcpy(Button[i].content, "9"); Button[i].hMenu = ID_CALC_9;} break;
+            case 3: {strcpy(Button[i].content, "4"); Button[i].hMenu = ID_CALC_4;} break;
+            case 4: {strcpy(Button[i].content, "5"); Button[i].hMenu = ID_CALC_5;} break;
+            case 5: {strcpy(Button[i].content, "6"); Button[i].hMenu = ID_CALC_6;} break;
+            case 6: {strcpy(Button[i].content, "1"); Button[i].hMenu = ID_CALC_1;} break;
+            case 7: {strcpy(Button[i].content, "2"); Button[i].hMenu = ID_CALC_2;} break;
+            case 8: {strcpy(Button[i].content, "3"); Button[i].hMenu = ID_CALC_3;} break;
+            case 9: {strcpy(Button[i].content, "."); Button[i].hMenu = ID_CALC_POINT;} break;
+            case 10: {strcpy(Button[i].content, "0"); Button[i].hMenu = ID_CALC_0;} break;
+            case 11: {strcpy(Button[i].content, "="); Button[i].hMenu = ID_CALC_EQUA;} break;
+            case 12: {strcpy(Button[i].content, "DEL"); Button[i].hMenu = ID_CALC_C;} break;
+            case 13: {strcpy(Button[i].content, "/"); Button[i].hMenu = ID_CALC_DIVIS;} break;
+            case 14: {strcpy(Button[i].content, "*"); Button[i].hMenu = ID_CALC_MUL;} break;
+            case 15: {strcpy(Button[i].content, "-"); Button[i].hMenu = ID_CALC_SUB;} break;
+            case 16: {strcpy(Button[i].content, "+"); Button[i].hMenu = ID_CALC_ADD;} break;
+        }
+
+        /* create buttons */
+        hwndButton[i] = CreateWindowA("BUTTON", Button[i].content,
+                                      WS_TABSTOP | WS_VISIBLE | WS_CHILD,
+                                      Button[i].x, Button[i].y, Button[i].nWidth, Button[i].nHeight,
+                                      hwnd,
+                                      (HMENU)Button[i].hMenu,
+                                      hInstance,
+                                      NULL);
+
+        if(hwndButton == NULL)
+        {
+            MessageBoxA(NULL, "Buttons Creation Failed!", "Error!",
+                        MB_ICONEXCLAMATION | MB_OK);
+            return 0;
+        }
+        /* show and update buttons */
+        ShowWindow(hwndButton[i], nCmdShow);
+        UpdateWindow(hwndButton[i]);
+    }
+
+    /* create the display textview */
+    hwndTextView = CreateWindowA("EDIT", "Please input the equation",
+                                 WS_BORDER | WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_DLGFRAME,
+                                 15,10,290,100, hwnd, NULL, hInstance, NULL);
+    if(hwndTextView == NULL)
+    {
+        MessageBoxA(NULL, "TextWindows Creation Failed!", "Error!",
+                    MB_ICONEXCLAMATION | MB_OK);
+        return 0;
+    }
+    ShowWindow(hwndTextView, nCmdShow);
+    UpdateWindow(hwndTextView);
+
+    while(GetMessageA(&Msg, NULL, 0, 0) > 0)
+    {
+        TranslateMessage(&Msg);
+        DispatchMessageA(&Msg);
+    }
+    return Msg.wParam;
+}
diff --git a/programs/calc/resource.h b/programs/calc/resource.h
new file mode 100755
index 0000000..cb28ad4
--- /dev/null
+++ b/programs/calc/resource.h
@@ -0,0 +1,42 @@
+/*
+ *  Constants, used in resources.
+ *
+ * Copyright 2018 Meng Hua
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <windef.h>
+#include <winuser.h>
+
+#define IDM_MAINMENU   40000
+#define ID_FILE_EXIT   40001
+#define ID_CALC_C      40009
+#define ID_CALC_DIVIS  40012
+#define ID_CALC_MUL    40014
+#define ID_CALC_SUB    40015
+#define ID_CALC_ADD    40016
+#define ID_CALC_EQUA   40018
+#define ID_CALC_POINT  40019
+#define ID_CALC_0      40020
+#define ID_CALC_1      40021
+#define ID_CALC_2      40022
+#define ID_CALC_3      40023
+#define ID_CALC_4      40024
+#define ID_CALC_5      40025
+#define ID_CALC_6      40026
+#define ID_CALC_7      40027
+#define ID_CALC_8      40028
+#define ID_CALC_9      40029
-- 
2.11.0

