// not UNIX

#include "stdafx.h"
#include "Strawberry.h"

#include "MainFrm.h"
#include "ChildFrm.h"

#include "StrawberryDoc.h"
#include "StrawberryView.h"
#include "CntrItem.h"

#include "New_Class.h"

#ifdef UNICODE
wchar_t *make_long_func(BYTE *str)
{
  DWORD k;
  ChangeCharset(ch_set, 64, str, (BYTE *)string3, &k, default_char);
  return (wchar_t *)string3;
}
char *make_utf8_func(CString str)
{
  DWORD k;
  ChangeCharset(64, 0, (BYTE *)(LPCTSTR)str, (BYTE *)string2, &k, default_char);
  return string2;
}
char *make_short_func(CString str)
{
  DWORD k;
  ChangeCharset(64, ch_set, (BYTE *)(LPCTSTR)str, (BYTE *)string2, &k, default_char);
  return string2;
}
#endif

BOOL SelectFile(CString& Buffer, HWND Father, LPTSTR Title, LPTSTR Filter, BOOL bMust)
{
  OPENFILENAME of;

  memset(&of, '\0', sizeof(OPENFILENAME));
  of.lStructSize  = sizeof(OPENFILENAME);
  of.hwndOwner    = Father;       // NULL
  of.lpstrFilter  = Filter;
  of.lpstrCustomFilter = NULL;
  of.nFilterIndex = 1;
  of.lpstrFile    = Buffer.GetBuffer(_MAX_PATH);
  of.nMaxFile     = 256;
  of.lpstrInitialDir = NULL;
  of.lpstrTitle   = Title;
  if (bMust)
    of.Flags      = OFN_HIDEREADONLY|OFN_FILEMUSTEXIST;
  else
    of.Flags      = OFN_HIDEREADONLY;
  of.lpstrDefExt  = NULL;

  BOOL bRet=GetOpenFileName(&of);
  Buffer.ReleaseBuffer();
  return bRet;
}
void MitiRedraw(void)
{
    if (pStack)
      pStack->InvalidateRect(NULL, TRUE);
    if (pVariables)
      pVariables->InvalidateRect(NULL, TRUE);
    if (pProofTree)                   /* TRACE */
    {
      open_br=0;
      my_sprintf(string3,_T("Proof Tree - step %u."), 0x10000*lSteps_br+Steps_br-Corection);
      pProofTree->SetWindowText(string3);
      pProofTree->InvalidateRect(NULL, TRUE);
    }
}

void ShowInformation(int Type)
{
  CString WinNane;
  switch(Type)
  {
#if TR
   case 1:
    WinNane="Closed Terms";
    break;
   case 2:
    WinNane="Open Terms";
    break;
   case 3:
    WinNane="Tree";
    break;
   case 4:
    WinNane="Blocks (Good)";
    break;
#endif
   case 5:
    WinNane="System List";
    break;
   case 6:
    if (pStack!=NULL)
    {
      pStack->BringWindowToTop();
      return;
    }
    WinNane="Stack";
    break;
   case 7:
    if (pVariables!=NULL)
    {
      pVariables->BringWindowToTop();
      return;
    }
    WinNane="Variables";
    break;
   case 8:
    WinNane="Operators";
    break;
   case 11:
    WinNane="Objects";
    break;
#if TR
   case 9:
    WinNane="Constants";
    break;
   case 10:
    WinNane="Key Map";
    break;
   case 12:
    WinNane="Blocks";
    break;
   case 13:
    WinNane="Ekler";
    break;
   case 14:
    WinNane="D_Field";
    break;
   case 15:
    WinNane="Pointers";
    break;
   case 16:
    WinNane="F_Field";
    break;
   case 17:
    WinNane="F_Field (Good)";
    break;
#endif
  }
  CwInformation* pInformation=new CwInformation(Type);
  CRect rect(Screen.cx/2, 8, Screen.cx-8, Screen.cy-8);
  pInformation->Create( AfxRegisterWndClass( 0, theApp.LoadStandardCursor(IDC_ARROW),
    CreateSolidBrush(GetSysColor(COLOR_WINDOW)), theApp.LoadIcon(IDI_ICON) ),
    WinNane, WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU
    | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VSCROLL | WS_HSCROLL,
    rect, 0, NULL);
  pInformation->ShowWindow(SW_SHOWNORMAL);
  pInformation->UpdateWindow();
  if (Type==6)
    pStack=pInformation;
  else if (Type==7)
    pVariables=pInformation;
}
// Source code for passwords coding - its missing
//***
#if PROF==2
void MakeExe(int Flag) 
{
  CFile  ExeFile;
  CString ExeFileName;
  HGLOBAL Exe1 = NULL, Exe2 = NULL;
  HRSRC   res1 = NULL, res2 = NULL;
  void   *Buf1, *Buf2;
  DWORD   size1, size2, pointer;

  if (!(status_fl&128)) // Not Registered
  {
    MessageBoxA(pMainFrame->m_hWnd, "Your copy of Strawberry Prolog is still not registered. Go in About window and register it.", "Strawberry Prolog", MB_OK | MB_ICONSTOP);
    return;
  }
  if ((Environment&32) && pActivePj) // ExecOnlyPj
    pActive=pActivePj;
  else if (pActiveFile)
    pActive=pActiveFile;
  else
    return;
  pActiveCtrl=&(pActive->GetView()->GetRichEditCtrl()); //?
  my_strcpy(namef, pActive->GetPathName());
  pInfo2=pActive->m_pInfo;
  if (Flag==0) // make Standard.sys
    ExeFileName=ProgramPath+_T("Standard_2_92.sys");
  else if (pInfo2->m_ExeFileName=="... (Default)")
  {
    my_strcpy(string3, namef);
    int i;
    for (i=my_strlen(string3); string3[i]!='.' && i>0; i--);
    if (i==0)
      i=my_strlen(string3);
    if (Flag&2)
      my_strcpy(string3+i, _T(".exe"));
    else  
      my_strcpy(string3+i, _T(".spex"));
    ExeFileName=string3;
  }
  else
    ExeFileName=pInfo2->m_ExeFileName;
  if (ExeFile.Open(ExeFileName, CFile::modeCreate | CFile::modeWrite)==0)
  {
    MitiText("Cannot open the file:\r\n", &cf_red);
    MitiText(make_utf8(ExeFileName));
    MitiText("\r\n\r\n");
    return;
  }
  CArchive ar(&ExeFile, CArchive::store);

  if (Flag)
  {
    res1=FindResource(NULL,MAKEINTRESOURCE(IDR_EXE_1),RT_RCDATA);
    ASSERT(res1);
    size1=SizeofResource(NULL,res1);
    Exe1=LoadResource(NULL,res1);
    ASSERT(Exe1);
    Buf1=LockResource(Exe1);

    res2=FindResource(NULL,MAKEINTRESOURCE(IDR_EXE_2),RT_RCDATA);
    ASSERT(res2);
    size2=SizeofResource(NULL,res2);
    Exe2=LoadResource(NULL,res2);
    ASSERT(Exe2);
    Buf2=LockResource(Exe2);
  
    if (Flag&2)
      ar.Write(Buf1, size1);
    ar.Write(pInfo2->m_Icon, 744);
    ar.Write(Buf2, size2);

    ar<<0x9e56f78a;
    ar<<RegName;
    ar<<RegFrom;
    ar<<RegProdID;
    ar<<pActive->m_pInfo->m_Comments;

    ar<<(BYTE)1;   // ch_warn  - starts from 1
    ar<<(BYTE)7;   // ch_ver
    ar<<(BYTE)21;  // int Fields
    ar<<(BYTE)0;   // str Fields
    ar<<(WORD)2;   // version X.Y
    ar<<(WORD)920;   // x1000

    ar<<sv_c;
    ar<<sv_v;
    ar<<sv_sub;
    ar<<global_br2;
    ar<<program_br2;
    ar<<program_br;
    ar<<sv_NewObj;
    ar<<sv_StStore;
    ar<<oper_br;
    ar<<(pInfo2->m_Settings); // |0x2000 can be temp if 2.92 has not Standard.sys
    ar<<pInfo2->STACK_start;
    ar<<pInfo2->STACK_max;
    ar<<pInfo2->BUDA_start;
    ar<<pInfo2->BUDA_max;
    ar<<pInfo2->BUDA_S_start;
    ar<<pInfo2->BUDA_S_max;
  }
  ar<<sv_c_old;
  ar<<sv_v_old;
  ar<<sv_sub_old;
  ar<<program_br1;
  ar<<MEMORY_C;                    // (1) can be temp if 2.92 has not Standard.sys
  if (Flag==0 || (pInfo2->m_Settings&0x2000)) // make Standard.sys or Add the Standard.add
  {
    ar.Write(Ufil_c1, 4*sv_c_old);
    ar.Write(Ufil_c2, 4*sv_c_old);
    ar.Write(Chfil_c, sv_c_old);
    ar.Write(Ufil_c1+MEMORY_C, 4*sv_v_old);
    ar.Write(Ufil_c2+MEMORY_C, 4*sv_v_old);
    ar.Write(Chfil_c+MEMORY_C, sv_v_old);
    ar.Write(Ufil_vs, 4*sv_v_old);
    ar.Write(Chfil_v, 2*sv_v_old);
    ar.Write(Substi, 2*sv_sub_old);
    ar.Write(Program, 4*program_br1);
    ar.Write(Prog_Obj, 4*program_br1);
    ar.Write(Definition, 4*DEFINITIONS);
  }
  if (Flag)
  {
    ar.Write(Ufil_c1+sv_c_old, 4*(sv_c-sv_c_old));
    ar.Write(Ufil_c2+sv_c_old, 4*(sv_c-sv_c_old));
    ar.Write(Chfil_c+sv_c_old, sv_c-sv_c_old);
    ar.Write(Ufil_c1+MEMORY_C+sv_v_old, 4*(sv_v-sv_v_old));
    ar.Write(Ufil_c2+MEMORY_C+sv_v_old, 4*(sv_v-sv_v_old));
    ar.Write(Chfil_c+MEMORY_C+sv_v_old, sv_v-sv_v_old);
    ar.Write(Ufil_vs+sv_v_old, 4*(sv_v-sv_v_old));
    ar.Write(Chfil_v+sv_v_old, 2*(sv_v-sv_v_old));
    ar.Write(Substi+sv_sub_old, 2*(sv_sub-sv_sub_old));
    ar.Write(global, 4*global_br2);
    ar.Write(Program+program_br2, 4*(program_br-program_br2));
    ar.Write(Prog_Obj+program_br2, 4*(program_br-program_br2));
    ar.Write(NewObj, 4*sv_NewObj);
    ar.Write(NewObj1, sv_NewObj);
    ar.Write(NewObj2, 2*sv_NewObj);
    ar.Write(StringStore+512, sv_StStore-512);
    ar.Write(Oper, 4*(oper_br+1));
    ar.Write(OpPr, 4*(oper_br+1));
    ar.Write(OpType, 4*(oper_br+1));
    ar.Write(OpMin, 4*(oper_br+1));

    pointer=5;    // version ch_ver
    ar.Write(&pointer, 4);
    pointer=744+size2; // Head - size1
    ar.Write(&pointer, 4);
    pointer=size1+744+size2; // Head
    ar.Write(&pointer, 4);
  }
  ar.Close();
  ExeFile.Close();
 
  if (Flag&2)
    MitiText("The EXE file was successfully created.\r\n", &cf_red);
  else
    MitiText("The SPEX file was successfully created.\r\n", &cf_red);
  MitiText(make_utf8(ExeFileName));
  MitiText("\r\n\r\n");
  MessageBeep(0);
}
#endif
