// ShowtxtDlg.cpp : implementation file
//
// Written by John A deLaubenfels, https://www.adaptune.com/steg.htm .
// Permission granted to use freely in any non-fraudulent way.
//
//

#include "pch.h"
#include "Steg2.h"
#include "afxdialogex.h"
#include "ShowtxtDlg.h"
#include "globs.h"
#include "ssprint.h"
// #include "rndWrap.h"


#pragma warning( disable : 4996 )


// ShowtxtDlg dialog

IMPLEMENT_DYNAMIC(ShowtxtDlg, CDialogEx)

ShowtxtDlg::ShowtxtDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_SHOWTXT, pParent)
{
   // m_pcBigString = NULL;
   m_pCommonInfo = NULL;
   m_bAddedCrs   = false;
   m_myFont      = new CFont();
   m_myFont->CreateFont( 16, 0, 0, 0, FW_NORMAL, false, false,
        0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
        FIXED_PITCH|FF_MODERN, _T("Courier New") );

}

ShowtxtDlg::~ShowtxtDlg()
{
   delete m_myFont; m_myFont=NULL;
}

void ShowtxtDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_EDT_SHOW, m_cEdt_show);
}


BEGIN_MESSAGE_MAP(ShowtxtDlg, CDialogEx)
   ON_EN_KILLFOCUS(IDC_EDT_SHOW, &ShowtxtDlg::OnKillfocusEdtShow)
   ON_BN_CLICKED(IDOK, &ShowtxtDlg::OnBnClickedOk)
    ON_BN_CLICKED(IDC_BUTN_SAVEAS, &ShowtxtDlg::OnClickedButnSaveas)
    ON_BN_CLICKED(IDC_BUTN_FIND, &ShowtxtDlg::OnClickedButnFind)
    ON_BN_CLICKED(IDC_BUTN_NEXT, &ShowtxtDlg::OnClickedButnNext)
    ON_BN_CLICKED(IDC_BUTN_PREV, &ShowtxtDlg::OnClickedButnPrev)
END_MESSAGE_MAP()


// ShowtxtDlg message handlers


BOOL ShowtxtDlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();

   // TODO:  Add extra initialization here
#ifdef REPLACED_D
   long long ill = 0;
   if(m_pcBigString != NULL) {
      m_cEdt_show.SetWindowText(m_pcBigString);
   } else if((ill = m_ssBigString.size()) > 0) {
      m_cEdt_show.SetWindowText(m_ssBigString.c_str());
   } else {
   } // if(m_pcBigString != NULL)
#endif // REPLACED_D

   m_cEdt_show.SetWindowText(m_ssBigString.c_str());

   this->SetWindowText(m_ssBanner.c_str());

   m_cEdt_show.SetFont(m_myFont);
   m_cEdt_show.m_pCommonInfo = m_pCommonInfo;

   bool bSetFocus = true;
   if(bSetFocus) {
      m_cEdt_show.SetFocus();

      return FALSE;  // return TRUE unless you set the focus to a control
   } else {
      return TRUE;   // return TRUE unless you set the focus to a control
   } // !if(bSetFocus)

   // EXCEPTION: OCX Property Pages should return FALSE
}


void ShowtxtDlg::OnKillfocusEdtShow() {
   // TODO: Add your control notification handler code here
   CString cs;
   m_cEdt_show.GetWindowText(cs);
   m_ssBigString = (const char *)cs;

   m_bAddedCrs = false;

   int nCr(0), nLf(0);
   int ii = countCrLf(m_ssBigString.c_str(), nCr, nLf);

   // char cbuf[256];
   // sprintf_s(cbuf, 256, "nCr %d, nLf %d", nCr, nLf);
   // doThing(cbuf);


   if(nLf > nCr) {
      m_ssBigString = adCr(m_ssBigString);
      m_bAddedCrs   = true;
   } // if(nLf > nCr)
}





void ShowtxtDlg::OnBnClickedOk()
{
   // TODO: Add your control notification handler code here
   if(m_bAddedCrs) {
      m_cEdt_show.SetWindowText(m_ssBigString.c_str());
      m_cEdt_show.SetFocus();
   } else {
      ShowtxtDlg::OnOK();
   } // !if(m_bAddedCrs)
}





BOOL ShowtxtDlg::PreTranslateMessage(MSG* pMsg) {
#ifdef WRONG_PLACE
   // Intercept Ctrl + Z (Undo), Ctrl + X (Cut), Ctrl + C (Copy), Ctrl + V (Paste) and Ctrl + A (Select All)
   // before CEdit base class gets a hold of them.
   if (pMsg->message == WM_KEYDOWN && ::GetKeyState(VK_CONTROL) < 0) {
      switch (pMsg->wParam) {
         case 'Z':
            Undo();
            return TRUE;
         case 'X':
            Cut();
            return TRUE;
         case 'C':
            Copy();
            return TRUE;
         case 'V':
            Paste();
            return TRUE;
         case 'A':
            SetSel(0, -1);
           return TRUE;
      } // switch (pMsg->wParam)
   } // if (pMsg->message == WM_KEYDOWN && ::GetKeyState(VK_CONTROL) < 0) {
#endif // WRONG_PLACE

   return CDialogEx::PreTranslateMessage(pMsg);
}




void ShowtxtDlg::OnClickedButnSaveas()
{
   m_cEdt_show.doSave();
#ifdef MOVED_D
   LPCTSTR pcFilter = _T("Text Files (*.txt)|*.txt||");
   DWORD dwFlags = OFN_OVERWRITEPROMPT;

   CFileDialog dlgSav(FALSE, NULL, NULL, dwFlags, pcFilter);

   dlgSav.m_ofn.lpstrTitle  = "Save to file.";

   if(m_pCommonInfo->ssMessageLoc.size() > 0)
      dlgSav.m_ofn.lpstrInitialDir  = m_pCommonInfo->ssMessageLoc.c_str();

   long long ill = dlgSav.DoModal();

   if(ill == IDOK) {

      CString csFileName = dlgSav.GetFileName();
      std::string ssFileName = (const char *)(LPCTSTR)csFileName;

      CString csPathName = dlgSav.GetPathName();
      std::string ssPathName = (const char *)(LPCTSTR)csPathName;

      SMARTA(pcFull, char, (ssPathName.size()+32), smaFull);
      SMARTA(pcBody, char, (ssPathName.size()+32), smaBody);
      SMARTA(pcDott, char, (ssPathName.size()+32), smaDott);
      SMARTA(pcExtt, char, (ssPathName.size()+32), smaExtt);
      SMARTA(pcNuu,  char, (ssPathName.size()+32), smaNuu);

      break_file_name(ssPathName.c_str(), pcBody, pcDott, pcExtt);

      sprintf(pcNuu, "%s%s", pcBody, ".txt");

      arcMeDotty(pcNuu);

      FILE *fpOut = NULL;
      errno_t err = 0;
      if((err = fopen_s(&fpOut, pcNuu, "w")) == 0) {
         CString cs;
         m_cEdt_show.GetWindowText(cs);
         std::string ssBigString = (const char *)cs;

         ssBigString = rmCr(ssBigString);

         fprintf(fpOut, "%s", ssBigString.c_str());

         fclose(fpOut); fpOut=NULL;

         SMARTA(pcDir, char, (ssPathName.size()+32), smaDir);
         SMARTA(pcFil, char, (ssPathName.size()+32), smaFil);

         cut_file_name(ssPathName.c_str(), pcDir, pcFil);

         m_pCommonInfo->ssMessageLoc = pcDir;

      } else {
         // char cbuf[256];
         // sprintf(cbuf, "We got error %d.\n", err);
         // doThing(cbuf);
      } // !if((err = fopen_s(&fpOut, pcNuu, "w")) == 0)
   } // if(ill == IDOK)
#endif // MOVED_D
}




void ShowtxtDlg::OnClickedButnFind()
{
   m_cEdt_show.doFind();
}




void ShowtxtDlg::OnClickedButnNext()
{
   m_cEdt_show.doFnext();
}



void ShowtxtDlg::OnClickedButnPrev()
{
   m_cEdt_show.doFprev();
}




void ShowtxtDlg::OnOK()
{
    // TODO: Add your specialized code here and/or call the base class
    qprintf(fp_dbg, "ShowtxtDlg::OnOK().\n");
    // zeroMe(m_ssBigString);

    std::string ssBlank = m_ssBigString;
    zeroMe(ssBlank, ' ');
    m_cEdt_show.SetWindowText(ssBlank.c_str());

    CDialogEx::OnOK();
}

void ShowtxtDlg::OnCancel()
{
    // TODO: Add your specialized code here and/or call the base class
    qprintf(fp_dbg, "ShowtxtDlg::OnCancel().\n");
    // zeroMe(m_ssBigString);

    std::string ssBlank = m_ssBigString;
    zeroMe(ssBlank, ' ');
    m_cEdt_show.SetWindowText(ssBlank.c_str());

    CDialogEx::OnCancel();
}






/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////



// FindDlg dialog

IMPLEMENT_DYNAMIC(FindDlg, CDialogEx)

FindDlg::FindDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_FINDY, pParent)
{
    // m_enterAction = VK_TAB;
    m_enterAction = VK_RETURN;
    m_bCaseSens   = false;
}

FindDlg::~FindDlg()
{
}

void FindDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDT_FIND, m_cEdt_find);
    // DDX_Control(pDX, IDC_STC_PWD_HINT, m_stcPwdHint);
    // DDX_Control(pDX, IDC_STC_BADPWD, m_stcBadPwd);
    DDX_Control(pDX, IDC_CHK_CASE_SENS, m_chkCaseSens);
}


BEGIN_MESSAGE_MAP(FindDlg, CDialogEx)
   ON_EN_KILLFOCUS(IDC_EDT_FIND, &FindDlg::OnKillfocusEdtFind)
   ON_EN_SETFOCUS(IDC_EDT_FIND, &FindDlg::OnSetfocusEdtFind)
    ON_BN_CLICKED(IDOK, &FindDlg::OnBnClickedOk)
    ON_BN_CLICKED(IDCANCEL, &FindDlg::OnBnClickedCancel)
    ON_BN_CLICKED(IDC_CHK_CASE_SENS, &FindDlg::OnClickedChkCaseSens)
END_MESSAGE_MAP()



BOOL FindDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   // m_stcBadPwd.SetWindowText(m_ssBadPwd.c_str());
   m_chkCaseSens.SetCheck(m_bCaseSens);

   bool bSetFocus = false;
   if(bSetFocus) {
      m_cEdt_find.SetFocus();

      return FALSE;  // return TRUE unless you set the focus to a control
   } else {
      return TRUE;   // return TRUE unless you set the focus to a control
   } // !if(bSetFocus)
   // EXCEPTION: OCX Property Pages should return FALSE
}



// FindDlg message handlers


BOOL FindDlg::PreTranslateMessage(MSG* pMsg)
{
   if (pMsg->message == WM_KEYDOWN) {
      if (pMsg->wParam == VK_RETURN)
          pMsg->wParam = m_enterAction;
   }
   return CDialogEx::PreTranslateMessage(pMsg);
}


void FindDlg::OnKillfocusEdtFind()
{
   m_enterAction = VK_RETURN;

   char cbuf[256];
   m_cEdt_find.GetWindowText(cbuf, 256);

   m_ssFind = cbuf;


   // PostMessage(WM_COMMAND, IDOK);

}


void FindDlg::OnSetfocusEdtFind()
{
   // m_enterAction = VK_TAB;
   m_cEdt_find.SetWindowText("");
}




void FindDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    CDialogEx::OnOK();
}




void FindDlg::OnBnClickedCancel()
{
    // TODO: Add your control notification handler code here
    CDialogEx::OnCancel();
}






void FindDlg::OnClickedChkCaseSens()
{
    // TODO: Add your control notification handler code here
   m_bCaseSens = m_chkCaseSens.GetCheck();
}







