#include <windows.h>
#include <fstream.h>
#include <io.h>
#include <fcntl.h>
#ifdef __BORLANDC__
  #pragma argsused
#endif
extern HINSTANCE _hInstance;
 static const char tytul[] = "Edytor";
 static OPENFILENAME ofn;

 LPCTSTR filtr[4];
 LPCTSTR dom_roz="txt";
 static char nazwa_zbioru[3 * 1024];
LONG APIENTRY GlownaFunkcja(HWND hwnd,UINT message,UINT wParam,LONG lParam)
{
    static HWND hwndEdit;
    CHAR znak;
      int c=0;
    CHAR bufor[32000];
    switch (message) {
        case WM_CREATE:
            hwndEdit = CreateWindow("EDIT",NULL,WS_CHILD | WS_VISIBLE | WS_VSCROLL |
                     ES_MULTILINE | ES_WANTRETURN,
                0, 0, 0, 0,
                hwnd,

                (HMENU)1,
               _hInstance,
                NULL);



             SendMessage(hwndEdit, WM_SETTEXT, 0,
                (LPARAM) bufor);

            return 0;

        case WM_COMMAND:
        int zb_wyjscie;
        int zb_wejscie;
            switch (LOWORD(wParam)) {
              case 10:
              filtr[0]="Text";
               filtr[1]="*.TXT";
               filtr[2]="Wszystkie pliki";
               filtr[3]="*.*\0";
              nazwa_zbioru[0] = '\0';
              ZeroMemory(&ofn, sizeof(ofn));
       ofn.lStructSize = sizeof(ofn);
       ofn.hwndOwner = hwnd;
       ofn.lpstrFile = nazwa_zbioru;
       ofn.nMaxFile = sizeof(nazwa_zbioru);
       ofn.lpstrTitle = " Otwórz zbiór ";
        ofn.lpstrFilter=filtr[0];
       ofn.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_PATHMUSTEXIST |
       OFN_HIDEREADONLY;

              if (GetOpenFileName(&ofn)) {
             zb_wejscie=open(ofn.lpstrFile,O_RDONLY);
             c=0;
            for (int k=0;k<32000;k++)
            bufor[k]=NULL;
            do
              {
                read(zb_wejscie, &znak, 1);
                if (znak=='\n')
                {znak='\r';
                 bufor[c]=znak;
                 c++;
                 znak='\n';
                 bufor[c]=znak;c++;}
                 else
                 {bufor[c]=znak; c++;}
               }

  while (!eof(zb_wejscie));
             close(zb_wejscie);

             if(!SendMessage(hwndEdit, WM_SETTEXT, 0,
                (LPARAM) bufor))
                MessageBox(hwndEdit,
                            "Plik zbyt durzy.",
                            "Błąd", MB_OK);

              }
               break;
               case 20:
               filtr[0]="Text";
               filtr[1]="*.TXT";
               filtr[2]="Wszystkie pliki";
               filtr[3]="*.*\0";
              nazwa_zbioru[0] = '\0';

              ZeroMemory(&ofn, sizeof(ofn));
              ofn.lStructSize = sizeof(ofn);
              ofn.hwndOwner = hwnd;
              ofn.lpstrFile = nazwa_zbioru;
              ofn.nMaxFile = sizeof(nazwa_zbioru);
              ofn.lpstrTitle = " Zapisz plik";
              ofn.lpstrFilter=filtr[0];
               ofn.lpstrDefExt=dom_roz;
              ofn.Flags =OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;

              if (GetSaveFileName(&ofn)) {
                SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)31000,
                (LPARAM) bufor);
                zb_wyjscie=open(ofn.lpstrFile,O_WRONLY|O_CREAT);
                write(zb_wyjscie, bufor, strlen(bufor));
                close(zb_wyjscie);
              }

               break;
               case 50:
                  SendMessage(hwnd,WM_DESTROY,0,0);
              break;


            }
            break;


        case WM_SETFOCUS:
            SetFocus(hwndEdit);
            return 0;

        case WM_SIZE:
               MoveWindow(hwndEdit,0, 40,LOWORD(lParam),HIWORD(lParam)-40,TRUE);
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return NULL;
}

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
  MSG msg;
HWND main_hwnd;
WNDCLASS glowne_okno;

 glowne_okno.style = CS_HREDRAW | CS_VREDRAW;
 glowne_okno.lpfnWndProc = GlownaFunkcja;
 glowne_okno.cbClsExtra = glowne_okno.cbWndExtra = 0;
 glowne_okno.hInstance = _hInstance;
 glowne_okno.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 glowne_okno.hCursor = LoadCursor(NULL, IDC_ARROW);
 glowne_okno.hbrBackground = (HBRUSH)COLOR_WINDOW;
 glowne_okno.lpszMenuName = NULL;
 glowne_okno.lpszClassName = tytul;

 if (RegisterClass(&glowne_okno))
   {
   main_hwnd = CreateWindow(tytul, tytul,
                WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                CW_USEDEFAULT, CW_USEDEFAULT,
                CW_USEDEFAULT, CW_USEDEFAULT,
                HWND_DESKTOP, NULL, _hInstance, NULL);
   }
 else
   main_hwnd = NULL;

if (main_hwnd)
  {
  ShowWindow(main_hwnd, nCmdShow);
  UpdateWindow(main_hwnd);
  CreateWindow("button", "Otwórz", WS_CHILD | WS_VISIBLE,
  5,10,50,30, main_hwnd, (HMENU)10, hInstance, NULL);
  CreateWindow("button", "Zapisz", WS_CHILD | WS_VISIBLE,
  55,10,50,30, main_hwnd, (HMENU)20, hInstance, NULL);
  CreateWindow("button", "Koniec", WS_CHILD | WS_VISIBLE,
  405,10,50,30, main_hwnd, (HMENU)50, hInstance, NULL);
  while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
  }

return msg.message;

}

