#include <windows.h>
#include "winlib.h"

extern HINSTANCE hInst;


BOOL CALLBACK InputDlgProc(HWND hDlg, UINT msg,
               WPARAM wParam, LPARAM lParam)
{
       static INPUT_DATA id, *pid;
       RECT rect;
       int capHeight, scrWidth, scrHeight, totHeight, edge;

       switch (msg) {
               case WM_INITDIALOG:
                       pid = (INPUT_DATA*)lParam;
                       id = *pid;

                       capHeight = GetSystemMetrics(SM_CYCAPTION);
                       scrWidth = GetSystemMetrics(SM_CXSCREEN);
                       scrHeight = GetSystemMetrics(SM_CYSCREEN);
                       edge = GetSystemMetrics(SM_CYEDGE);

                       totHeight = 130 + capHeight + (2 * edge) + 2;

                       MoveWindow(hDlg,
                       (scrWidth / 2) - (id.width / 2),
                       (scrHeight / 2) - (totHeight / 2),
                       id.width, totHeight,
                       FALSE);

                       GetClientRect(hDlg, &rect);

                       MoveWindow(GetDlgItem(hDlg, IDC_INPUT),
                       20, 30, rect.right - 40, 23,
                       FALSE);

                       MoveWindow(GetDlgItem(hDlg, IDOK),
                       (rect.right / 2) - 90, 90, 80, 28,
                       FALSE);

                       MoveWindow(GetDlgItem(hDlg, IDCANCEL),
                       (rect.right / 2) + 10, 90, 80, 28,
                       FALSE);

                       SetWindowText(hDlg, id.title);
                       SetDlgItemText(hDlg, IDC_INPUT, id.data);

                       SetFocus(GetDlgItem(hDlg, IDC_INPUT));
                       return FALSE;

               case WM_COMMAND :
                       switch (LOWORD (wParam)) {
                               case IDOK:
                                       GetDlgItemText(hDlg, IDC_INPUT, id.data, 511);
                                       *pid = id;
                                       EndDialog(hDlg, 1);
                                       return TRUE;

                               case IDCANCEL:
                                       EndDialog (hDlg, 0);
                                       return TRUE ;
                       }
                       break;
       }
       return FALSE;
}


char* GetInput(HWND hParent, char *title, char *dflt, int width)
{
       static char input[512];
       INPUT_DATA id;

       strcpy(input, "");
       strcpy(id.title, title);
       strcpy(id.data, dflt);
       id.width = width;

       if (DialogBoxParam(hInst, "InputBox", hParent,
                       InputDlgProc, (LPARAM)&id)) {
               strcpy(input, id.data);
               return input;
       } else
               return NULL;
}