/* The original version of this program can be found at http://damb.dk */
#include <windows.h>
#include <commctrl.h>

enum WindowsId
{
   FirstEditId = 256,
   FirstUpDownId,
   SecondEditId,
   SecondUpDownId,
   ResultEditId,
   SubRadioId,
   AddRadioId,
   ButtonId,
   GroupBoxId,
};

HINSTANCE InstanceHandle;
int NumPush = 0;

LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
   {
   case WM_CREATE:
      CreateWindowEx(WS_EX_CLIENTEDGE,
                     "EDIT",
                     "",
                     WS_CHILD | WS_VISIBLE | ES_NUMBER | WS_TABSTOP | WS_GROUP,
                     5, 5, 120, 20,
                     hwndDlg,
                     (HMENU)(FirstEditId),
                     InstanceHandle,
                     0);

      CreateWindow(UPDOWN_CLASS,
                   "Spin1",
                   UDS_AUTOBUDDY | UDS_ALIGNRIGHT | UDS_SETBUDDYINT | WS_CHILD | WS_VISIBLE | UDS_ARROWKEYS,
                   105, 5, 20, 20,
                   hwndDlg,
                   (HMENU )FirstUpDownId,
                   InstanceHandle,
                   0);
      SendDlgItemMessage(hwndDlg, FirstUpDownId, UDM_SETRANGE, 0, MAKELONG(100, -100));

      CreateWindowEx(WS_EX_CLIENTEDGE,
                     "EDIT",
                     "",
                     WS_CHILD | WS_VISIBLE | ES_NUMBER | WS_TABSTOP,
                     5, 35, 120, 20,
                     hwndDlg,
                     (HMENU)(SecondEditId),
                     InstanceHandle,
                     0);

      CreateWindow(UPDOWN_CLASS,
                   "Spin2",
                   UDS_AUTOBUDDY | UDS_ALIGNRIGHT | UDS_SETBUDDYINT | WS_CHILD | WS_VISIBLE | UDS_ARROWKEYS,
                   105, 35, 20, 20,
                   hwndDlg,
                   (HMENU )SecondUpDownId,
                   InstanceHandle,
                   0);
      SendDlgItemMessage(hwndDlg, SecondUpDownId, UDM_SETRANGE, 0, MAKELONG(100, -100));

      CreateWindowEx(WS_EX_CLIENTEDGE,
                     "EDIT",
                     "0",
                     WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_READONLY,
                     5, 65, 120, 20,
                     hwndDlg,
                     (HMENU)(ResultEditId),
                     InstanceHandle,
                     0);

      CreateWindow("BUTTON",
                   "Add",
                   WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON,
                   10, 105, 50, 14,
                   hwndDlg,
                   (HMENU )AddRadioId,
                   InstanceHandle,
                   0);

      CreateWindow("BUTTON",
                   "Sub",
                   WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON,
                   65, 105, 50, 14,
                   hwndDlg,
                   (HMENU )SubRadioId,
                   InstanceHandle,
                   0);

      CreateWindow("BUTTON",
                   "Calculate",
                   WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                   5, 130, 120, 30,
                   hwndDlg,
                   (HMENU )ButtonId,
                   InstanceHandle,
                   0);

      CreateWindow("BUTTON",
                   "Action",
                   WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
                   5, 85, 120, 40,
                   hwndDlg,
                   (HMENU )GroupBoxId,
                   InstanceHandle,
                   0);

      CheckDlgButton(hwndDlg, AddRadioId, BST_CHECKED);
      break;
   case WM_COMMAND:
      if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ButtonId)
      {
         int First = GetDlgItemInt(hwndDlg, FirstEditId, 0, FALSE);
         int Second = GetDlgItemInt(hwndDlg, SecondEditId, 0, FALSE);
         int Result;
         if(IsDlgButtonChecked(hwndDlg, AddRadioId))
         {
            Result = First + Second;
         }
         else
         {
            Result = First - Second;
         }
         SetDlgItemInt(hwndDlg, ResultEditId, Result, TRUE);
      }
      break;
   }
   return DefWindowProc(hwndDlg, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, INT nCmdShow)
{
   InstanceHandle = hInstance;
   InitCommonControls();
   WNDCLASS wc;
   memset(&wc, 0, sizeof(WNDCLASS));
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
   wc.lpfnWndProc = DialogProc;
   wc.hInstance = InstanceHandle;
   wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
   wc.lpszClassName = "WhateverClass";
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   if(!RegisterClass(&wc))
      return FALSE;

   HWND WindowHandle = CreateWindow("WhateverClass",
                                    "Calc", // Caption text
                                    WS_MINIMIZEBOX | WS_VISIBLE | WS_MAXIMIZEBOX |
                                    WS_CAPTION | WS_BORDER | WS_SYSMENU,
                                    100, 100, 140, 240,  // Position
                                    NULL,
                                    NULL,
                                    InstanceHandle,
                                    0);
   MSG Msg;
   while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
   {
      if(!IsDialogMessage(WindowHandle, &Msg))
      {
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
   }
   return 0;
}