/*
* MapPlot
*
* Simple map drawing program for MWDB-II/WDB-II data
*
*/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winlib/winlib.h>

#include "mapplot.h"
#include "map.h"
#include "window.h"
#include "fsbox.h"


void DoExitStuff(VARS *v)
{
       SavePlacement(v->hwndMain, "Software\\MapPlot");
       SetRegVal("Software\\MapPlot", "DataDir", (void*)(v->dataDir),
                       strlen(v->dataDir));
       SetRegVal("Software\\MapPlot", "Projection", (void*)&(v->project),
                       sizeof(v->project));
       SetRegVal("Software\\MapPlot", "Show", (void*)&(v->shwFeatures),
                       sizeof(v->shwFeatures));
       SetRegVal("Software\\MapPlot", "WDBShowBdy", (void*)&(v->wdbShwBdyFeatures),
                       sizeof(v->wdbShwBdyFeatures));
       SetRegVal("Software\\MapPlot", "WDBShowCil", (void*)&(v->wdbShwCilFeatures),
                       sizeof(v->wdbShwCilFeatures));
       SetRegVal("Software\\MapPlot", "WDBShowRiv", (void*)&(v->wdbShwRivFeatures),
                       sizeof(v->wdbShwRivFeatures));
       SetRegVal("Software\\MapPlot", "Colors", (void*)v->colors,
                       sizeof(v->colors));
       SetRegVal("Software\\MapPlot", "OrigLon", (void*)&(v->origLon),
                       sizeof(v->origLon));
       SetRegVal("Software\\MapPlot", "OrigLat", (void*)&(v->origLat),
                       sizeof(v->origLat));
       SetRegVal("Software\\MapPlot", "Zoom", (void*)&(v->zoom),
                       sizeof(v->zoom));
       SetRegVal("Software\\MapPlot", "GridSize", (void*)&(v->gridDeg),
                       sizeof(v->gridDeg));
       SetRegVal("Software\\MapPlot", "MinMax", (void*)&(v->minMax),
                       sizeof(v->minMax));
}


VARS* Init(HINSTANCE hInst, int nCmdShow)
{
       WNDCLASS wc;
       RECT rect;
       int x = CW_USEDEFAULT, y = 0, w = 640, h = 480;
       VARS *v;
       char *p, szFileName[256], szTitle[256];
       int i, j, id;

       v = (VARS *)malloc(sizeof(VARS));
       v->hInst = hInst;
       v->oldFocus = NULL;
       v->mapData = v->prevPoly = NULL;
       v->wdbIIMode = FALSE;
       v->wdbIIMaps = NULL;

       v->defColors[CLR_COAST] = RGB(32,144,32);
       v->defColors[CLR_COUNTRY] = RGB(255,128,64);
       v->defColors[CLR_STATE] = RGB(255,0,0);
       v->defColors[CLR_ISLAND] = RGB(32,144,32);
       v->defColors[CLR_LAKE] = RGB(128,128,255);
       v->defColors[CLR_RIVER] = RGB(128,128,255);
       v->defColors[CLR_BACKGR] = RGB(242,242,242);
       v->defColors[CLR_GRID] = RGB(210,210,210);

       if (! GetRegVal("Software\\MapPlot", "Projection", (void*)&(v->project),
                       sizeof(v->project))) {
               v->project = PLATE_CARREE;
       }

       if (! GetRegVal("Software\\MapPlot", "Show", (void*)&(v->shwFeatures),
                       sizeof(v->shwFeatures))) {
               v->shwFeatures = SHW_COAST | SHW_ISLAND | SHW_GRID;
       }

       memset(v->wdbShwBdyFeatures, 0, 20);
       if (! GetRegVal("Software\\MapPlot", "WDBShowBdy", (void*)&(v->wdbShwBdyFeatures),
                       sizeof(v->wdbShwBdyFeatures))) {
               v->wdbShwBdyFeatures[1] = 1;
               v->wdbShwBdyFeatures[2] = 1;
       }

       memset(v->wdbShwCilFeatures, 0, 20);
       if (! GetRegVal("Software\\MapPlot", "WDBShowCil", (void*)&(v->wdbShwCilFeatures),
                       sizeof(v->wdbShwCilFeatures))) {
               v->wdbShwCilFeatures[1] = 1;
               v->wdbShwCilFeatures[2] = 1;
       }

       memset(v->wdbShwRivFeatures, 0, 20);
       if (! GetRegVal("Software\\MapPlot", "WDBShowRiv", (void*)&(v->wdbShwRivFeatures),
                       sizeof(v->wdbShwRivFeatures))) {
               v->wdbShwRivFeatures[1] = 1;
               v->wdbShwRivFeatures[2] = 1;
       }

       if (! GetRegVal("Software\\MapPlot", "Colors", (void*)v->colors,
                       sizeof(v->colors))) {
               memcpy(v->colors, v->defColors, sizeof(v->defColors));
       }

       if (! GetRegVal("Software\\MapPlot", "OrigLon", (void*)&(v->origLon),
                       sizeof(v->origLon))) {
               v->origLon = 0.0;
       }

       if (! GetRegVal("Software\\MapPlot", "OrigLat", (void*)&(v->origLat),
                       sizeof(v->origLat))) {
               v->origLat = 0.0;
       }

       if (! GetRegVal("Software\\MapPlot", "Zoom", (void*)&(v->zoom),
                       sizeof(v->zoom))) {
               v->zoom = 1.0;
       }

       if (! GetRegVal("Software\\MapPlot", "GridSize", (void*)&(v->gridDeg),
                       sizeof(v->gridDeg))) {
               v->gridDeg = 15;
       }

       if (! GetRegVal("Software\\MapPlot", "MinMax", (void*)&(v->minMax),
                       sizeof(v->minMax))) {
               v->minMax.minLon = -180.;
               v->minMax.maxLon = 180.;
               v->minMax.minLat = -90.;
               v->minMax.maxLat = 90.;
       }

       wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
       wc.lpfnWndProc = (WNDPROC)MainWndProc;
       wc.cbClsExtra = 0;
       wc.cbWndExtra = sizeof(v);
       wc.hInstance = hInst;
       wc.hIcon = LoadIcon(hInst, "Icon");
       wc.hCursor = LoadCursor(NULL, IDC_ARROW);
       wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
       wc.lpszMenuName = "MainMenu";
       wc.lpszClassName = "MainWndClass";
       if (! RegisterClass(&wc))
               return NULL;

       wc.lpfnWndProc = (WNDPROC)ContWndProc;
       wc.lpszClassName = "ContWndClass";
       wc.lpszMenuName = (HMENU)NULL;
       if (! RegisterClass(&wc))
               return NULL;

       if (GetPlacement("Software\\MapPlot", &rect)) {
               x = rect.left;
               y = rect.top;
               w = rect.right - rect.left;
               h = rect.bottom - rect.top;
       }

       v->hwndMain = CreateWindow(
               "MainWndClass",
               "MapPlot",
               WS_OVERLAPPEDWINDOW,
               x, y, w, h,
               NULL, NULL, v->hInst, (LPVOID)v);

       v->hMenu = GetMenu(v->hwndMain);

       switch (v->project) {
               case PLATE_CARREE:
                       id = IDM_PLATE_CARREE;
                       break;
               case MERCAT:
                       id = IDM_MERCATOR;
                       break;
               case ROBIN:
                       id = IDM_ROBINSON;
                       break;
               case HAMMER:
                       id = IDM_HAMMER;
                       break;
               case ORTHO:
                       id = IDM_ORTHO;
                       break;
       }
       CheckMenuItem(v->hMenu, id, MF_CHECKED);

       for (i = 1, j = 0; i < 256; i = i * 2, j++) {
               if (v->shwFeatures & i)
                       CheckMenuItem(v->hMenu, 200 + j, MF_CHECKED);
       }

       for (i = 1; i < 4; i++) {
               if (v->wdbShwBdyFeatures[i])
                       CheckMenuItem(v->hMenu, 300 + i, MF_CHECKED);
       }

       for (i = 1; i < 16; i++) {
               if (v->wdbShwCilFeatures[i])
                       CheckMenuItem(v->hMenu, 400 + i, MF_CHECKED);
       }

       for (i = 1; i < 13; i++) {
               if (v->wdbShwRivFeatures[i])
                       CheckMenuItem(v->hMenu, 500 + i, MF_CHECKED);
       }

       FileInitialize();

       memset(v->dataDir, 0, 256);
       if (! GetRegVal("Software\\MapPlot", "DataDir", (void*)(v->dataDir),
                       sizeof(v->dataDir))) {
               DirInitialize();

               szFileName[0] = '\0';
               strcpy(szTitle, "Choose Data Directory");
               if (! DirOpenDlg(v->hwndMain, szFileName, szTitle))
                       return NULL;

               p = strrchr(szFileName, '\\');
               *p = '\0';
               strcpy(v->dataDir, szFileName);
               FileInitialize();
       }

       if (! ReadMaps(v))
               return NULL;

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


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

       if (! (v = Init(hInstance, 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;
}