// wseerc1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "wseerc1.h"
#include "wseerc1Dlg.h"
#include "export.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
        CAboutDlg();

// Dialog Data
        //{{AFX_DATA(CAboutDlg)
        enum { IDD = IDD_ABOUTBOX };
        //}}AFX_DATA

        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CAboutDlg)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        //}}AFX_VIRTUAL

// Implementation
protected:
        //{{AFX_MSG(CAboutDlg)
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
        //{{AFX_DATA_INIT(CAboutDlg)
        //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CAboutDlg)
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
        //{{AFX_MSG_MAP(CAboutDlg)
                // No message handlers
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWseerc1Dlg dialog

CWseerc1Dlg::CWseerc1Dlg(CWnd* pParent /*=NULL*/)
        : CDialog(CWseerc1Dlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(CWseerc1Dlg)
                // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
        m_Command=0;
}

void CWseerc1Dlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CWseerc1Dlg)
        DDX_Control(pDX, IDC_CHECK_DEBUG, m_CheckDebug);
        DDX_Control(pDX, IDC_EDIT3, m_EditDebug);
        DDX_Control(pDX, IDC_STATIC_Bitmap_Com, m_Bmp_Compiling);
        DDX_Control(pDX, IDC_EDIT2, m_EditOutput);
        DDX_Control(pDX, IDC_EDIT1, m_EditSource);
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWseerc1Dlg, CDialog)
        //{{AFX_MSG_MAP(CWseerc1Dlg)
        ON_WM_SYSCOMMAND()
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_BN_CLICKED(IDC_BUT_About, OnBUTAbout)
        ON_BN_CLICKED(IDC_BUTCompile, OnBUTCompile)
        ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
        ON_BN_CLICKED(IDC_BUTBrowse, OnBrowse)
        ON_BN_CLICKED(IDC_CHECK_DEBUG, OnCheckDebug)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWseerc1Dlg message handlers

BOOL CWseerc1Dlg::OnInitDialog()
{
        CDialog::OnInitDialog();

        // Add "About..." menu item to system menu.

        // IDM_ABOUTBOX must be in the system command range.
        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
        ASSERT(IDM_ABOUTBOX < 0xF000);

        CMenu* pSysMenu = GetSystemMenu(FALSE);
        if (pSysMenu != NULL)
        {
                CString strAboutMenu;
                strAboutMenu.LoadString(IDS_ABOUTBOX);
                if (!strAboutMenu.IsEmpty())
                {
                        pSysMenu->AppendMenu(MF_SEPARATOR);
                        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
                }
        }

        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE);                 // Set big icon
        SetIcon(m_hIcon, FALSE);                // Set small icon

        // TODO: Add extra initialization here
        exportSeeRCSymbols();

        if (m_Command)
        {int i=0,prev=0;
         CString act;
         do{
                 act="";
                 while (m_Argv[i]&&m_Argv[i]==' ') i++;
                 while(m_Argv[i]&&m_Argv[i]!=' ') act+=m_Argv[i++];

                 if (act!="")
                 switch(prev)
                 {
                 case 0://Normal
                 if (act[0]=='-')
                 {
                         if (act=="-d"||act=="-debug")
                         {
                                 m_EditDebug.EnableWindow(TRUE);
                                 m_CheckDebug.SetCheck(true);
                         }
                         else
                         if (act=="-o"||act=="-output")
                             prev=1;

                 } else
                 {CString p,q,deb;
                  p=act;
                  MakeFilenames(p,q,deb);
                  m_EditSource.SetWindowText(q);
                  m_EditOutput.GetWindowText(q);
                  if (q=="")
                        m_EditOutput.SetWindowText(p);
                  m_EditDebug.GetWindowText(q);
                  if (q=="")
                        m_EditDebug.SetWindowText(deb);

                 }
                 break;
                 case 1://after -o
                         m_EditOutput.SetWindowText(act);
                         break;
                 }

         }while (act!="");
         ShowWindow(true);
         UpdateWindow();
         OnBUTCompile();
         EndDialog(IDOK);
        }
        return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWseerc1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
        if ((nID & 0xFFF0) == IDM_ABOUTBOX)
        {
                CAboutDlg dlgAbout;
                dlgAbout.DoModal();
        }
        else
        {
                CDialog::OnSysCommand(nID, lParam);
        }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWseerc1Dlg::OnPaint()
{
        if (IsIconic())
        {
                CPaintDC dc(this); // device context for painting

                SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

                // Center icon in client rectangle
                int cxIcon = GetSystemMetrics(SM_CXICON);
                int cyIcon = GetSystemMetrics(SM_CYICON);
                CRect rect;
                GetClientRect(&rect);
                int x = (rect.Width() - cxIcon + 1) / 2;
                int y = (rect.Height() - cyIcon + 1) / 2;

                // Draw the icon
                dc.DrawIcon(x, y, m_hIcon);
        }
        else
        {
                CDialog::OnPaint();
        }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWseerc1Dlg::OnQueryDragIcon()
{
        return (HCURSOR) m_hIcon;
}

void CWseerc1Dlg::OnBUTAbout()
{CAboutDlg d;
 d.DoModal();
}

void CWseerc1Dlg::OnBUTCompile()
{
        CMessDlg msg;
        CString source,output,debug;
        scScript scrpt;
        m_EditSource.GetWindowText(source);
        m_EditOutput.GetWindowText(output);
        m_EditDebug.GetWindowText(debug);
        if (source==""||output=="")
        {
                 MessageBox(
                 "Source or Output filenames not present.\n       Please specify them!",
                 "Error",MB_OK|MB_ICONEXCLAMATION);
                 return;
        }
        if (debug=="")
                m_CheckDebug.SetCheck(false);
   m_Bmp_Compiling.ShowWindow(SW_SHOW);
   m_Bmp_Compiling.UpdateWindow();

   AfxGetApp()->DoWaitCursor(1);
   if (m_CheckDebug.GetCheck())
   {
        scOpen_Debug((char*)LPCSTR(debug));
        scToggle_Debug(1);
   }
   scrpt=scCompile_File((char *)LPCSTR(source));
   if (m_CheckDebug.GetCheck())
        scClose_Debug();

   AfxGetApp()->DoWaitCursor(-1);

   m_Bmp_Compiling.ShowWindow(SW_HIDE);
   m_Bmp_Compiling.UpdateWindow();

   if (scErrorNo)
                 MessageBox(CString(scErrorMsg)+"\n>"+scErrorLine,
                                    "Compilation error",MB_OK|MB_ICONERROR);
   else
        if (scrpt)
        {   FILE* fl;
            fl=fopen((char *)LPCSTR(output),"wb");
                if (!fl)
                {
                         free(scrpt);
                         MessageBox("Unable to create \n"+output+"!","Error",MB_OK|MB_ICONERROR);
                         return;
                }
                fwrite(scrpt,scGet_Script_Size(scrpt),1,fl);
                fclose(fl);
                free(scrpt);

   if (m_CheckDebug.GetCheck())
                MessageBox("Source script \n  "+source+
                                "\nhas been successfully compiled to \n  "+output+
                                 "\nDebug info saved to \n  "+debug
                                 ,"Success (debug saved)!!!",MB_OK|MB_ICONINFORMATION);
   else
                MessageBox("Source script \n "+source+
                                "\nhas been successfully compiled to \n "+output
                                 ,"Success!!!",MB_OK|MB_ICONINFORMATION);
        }
        else
        {
                MessageBox("File "+source+" not found!","Error",MB_OK|MB_ICONEXCLAMATION);
                ;
        }
}
//dest,source,deb
int CWseerc1Dlg::MakeFilenames(CString& p,CString& q,CString& deb)
{
 if (p=="") return 0;
 deb=q=p;
 int i;
 if (!(i=p.ReverseFind('.')))
 {p=p+".cs";deb=deb+".deb";}
 else
 {
         p=p.Left(i)+".cs";
         deb=deb.Left(i)+".deb";
 }
 if (q==p)
        {
         if (MessageBox("File seems to be an already compiled script (.cs).\n"
                            "Are you sure it's a source script?",
                                    "Warning",MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2
                                        )!=IDYES)
                                        return 0;
         p+="0";
        }
 if (q==deb)
        {
         if (MessageBox("File seems to be debug information file (.deb)\n"
                            "Are you sure it's a source script?",
                                    "Warning",MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2
                                        )!=IDYES)
                                        return 0;
         i=deb.ReverseFind('.');
         deb=deb.Left(i)+".db0";
        }
 return 1;
}

void CWseerc1Dlg::OnBrowse()
{
 CFileDialog d(TRUE,"cs");
 CString p,q,deb;
 d.m_ofn.lpstrTitle="SeeR script select";
 d.m_ofn.lpstrFilter="SeeR scripts (.sc)\0*.sc\0C source (.c)\0*.c\0All files\0*.*\0\0";
 d.DoModal();
 p=d.GetPathName();
 MakeFilenames(p,q,deb);
 m_EditSource.SetWindowText(q);
 m_EditOutput.SetWindowText(p);
 m_EditDebug.SetWindowText(deb);
}

void CWseerc1Dlg::OnCheckDebug()
{
 if (m_CheckDebug.GetCheck())
 {m_EditDebug.EnableWindow(TRUE);
 }
 else
 {m_EditDebug.EnableWindow(FALSE);
 }
 m_EditDebug.UpdateWindow();
}
