#include <windows.h>
#include "richedit.h"
#include <stdio.h>
#include <string.h>
#include <winlib/winlib.h>

#include "dlgmain.h"


void ResizeChildren(VARS *v)
{
       RECT mainRect;

       GetClientRect(v->hwndMain, &mainRect);

       MoveWindow(GetDlgItem(v->hwndMain, IDC_EDIT1),
               0, 0, mainRect.right, mainRect.bottom - 161,
               TRUE);

       MoveWindow(GetDlgItem(v->hwndMain, IDC_EDIT2),
               0, mainRect.bottom - 160, mainRect.right, 135,
               TRUE);
}


BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT msg,
               WPARAM wParam, LPARAM lParam)
{
       switch (msg) {
               case WM_INITDIALOG:
                       return TRUE;

               case WM_COMMAND :
                       switch (LOWORD (wParam)) {
                               case IDOK:
                               case IDCANCEL:
                                       EndDialog (hDlg, 0);
                                       return TRUE ;
                       }
                       break;
       }
       return FALSE;
}


BOOL CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam,
               LPARAM lParam)
{
       WORD id;
       static VARS *v;

       switch (msg) {
               case WM_INITDIALOG:
                       v = (VARS**)lParam;
                       break;

               case WM_SIZE:
                       SendMessage(GetDlgItem(hwnd, IDC_STATUS), WM_SIZE,
                                       wParam, lParam);
                       SetStatusParts(GetDlgItem(hwnd, IDC_STATUS), 2);
                       ResizeChildren(v);
                       break;

               case WM_COMMAND:
                       id = LOWORD(wParam);

                       switch (id) {
                               case IDM_EXIT:
                                       PostQuitMessage(0);
                                       break;

                               case IDM_ABOUT:
                                       DialogBox (v->hInst, "AboutBox", v->hwndMain,
                                                       AboutDlgProc);
                                       break;

                               default:
                                       return FALSE;
                       }
                       break;

               case WM_CLOSE:
                       DestroyWindow(hwnd);
                       break;

               case WM_DESTROY:
                       PostQuitMessage(0);
                       break;

               default:
                       return FALSE;
       }
       return TRUE;
}


static VARS* Init(HINSTANCE hInst, LPSTR cmdLine, INT nCmdShow)
{
       WNDCLASS wc;
       VARS *v;

       if (! LoadLibrary("riched20.dll")) {
               MBPrintf("SQLShell", "Couldn't load riched20.dll");
               return NULL;
       }

       v = (VARS*)malloc(sizeof(VARS));
       v->hInst = hInst;

       GetClassInfo(NULL, "#32770", &wc);
       wc.hIcon = LoadIcon(hInst, "Icon");
       wc.lpszMenuName = "MainMenu";
       wc.lpszClassName = "MainWndClass";
       if (! RegisterClass(&wc))
               return NULL;

       v->hwndMain = CreateDialogParam(v->hInst, "MainWnd", 0,
                       MainWndProc, (LPARAM)v);

       ShowWindow(v->hwndMain, nCmdShow);
       return v;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, INT nCmdShow)
{
       MSG msg;
       VARS *v;
       HACCEL hAccel;

       if (! (v = Init(hInstance, lpCmdLine, nCmdShow)))
               return 0;

       if (! (hAccel = LoadAccelerators(v->hInst, "Accel")))
               return 0;

       while (GetMessage (&msg, NULL, 0, 0)) {
               if (! TranslateAccelerator(v->hwndMain, hAccel, &msg)) {
                       TranslateMessage(&msg);
                       DispatchMessage(&msg);
               }
       }

       return msg.wParam;
}