/**
 * This function will be called when user selects Browse button.
 * This function will show file selection dialog and will update
 * replay file data if user selects any file.
 */
void CReplayFileConfigDlg::OnBtnBrowse()
{
    if( m_nSelecetedNamedLogIndex != -1 &&
            m_nSelecetedNamedLogIndex < m_rouManager.m_omReplayFiles.GetSize() )
    {
        // Get Selected Item Details
        CReplayFile& ouFile =
            m_rouManager.m_omReplayFiles.ElementAt( m_nSelecetedNamedLogIndex );
        DWORD dwFlags   = 0;
        dwFlags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_EXTENSIONDIFFERENT;
        // Show File Selection Dialog to select Log File
        CFileDialog omFileDlg( TRUE,
                               defSTR_LOG_FILE_EXTENSION,
                               ouFile.m_omStrFileName,
                               dwFlags,
                               defLOG_FILTER,
                               NULL );
        //Set the caption
        omFileDlg.m_ofn.lpstrTitle = _(defSTR_REPLAY_FILE_SELECTION_TITLE);
        // Show File open dialog
        if( omFileDlg.DoModal() == IDOK )
        {
            // Get Path from File Selection Dialog
            ouFile.m_omStrFileName = omFileDlg.GetPathName();
            // Update List Item Text
            m_omLstcReplayFiles.SetItemText( m_nSelecetedNamedLogIndex, 0,
                                             ouFile.m_omStrFileName );
            // Set File Name in the editbox
            m_omEditReplayFileName.SetWindowText( ouFile.m_omStrFileName );
        }
    }
}
void CConfigMsgLogDlg::OnBnClickedCbtnLogFilePath(void)
{
    USES_CONVERSION;

    CString omStrLogFile = "";
    (GetDlgItem(IDC_EDIT_LOGFILEPATH))->GetWindowText(omStrLogFile);

    // Show File Selection Dialog to select Log File
    CFileDialog omFileDlg(FALSE, BUSMASTER_LOG_FILE_EXTENSION, omStrLogFile,
                          OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
                          OFN_EXTENSIONDIFFERENT | OFN_OVERWRITEPROMPT,
                          BUSMASTER_LOG_FILTER, nullptr);
    // Set the caption
    omFileDlg.m_ofn.lpstrTitle = _(BUSMASTER_LOG_SELECTION_TITLE);

    const int nSize = _MAX_PATH  * _MAX_PATH;  //Total 260 Files
    char* szTempBuf = new char[nSize];
    memset(szTempBuf, 0, sizeof(char) * nSize);
    omFileDlg.m_ofn.lpstrFile = szTempBuf;
    omFileDlg.m_ofn.nMaxFile = nSize - 1;
    // Show File open dialog
    if (omFileDlg.DoModal() == IDOK)
    {
        omStrLogFile = omFileDlg.GetPathName();
        for (USHORT i = 0; i < GetLoggingBlockCount(); i++)
        {
            SLOGINFO sLogObject;
            GetLoggingBlock(i, sLogObject);
            if (_tcsicmp(sLogObject.m_sLogFileName, omStrLogFile.GetBuffer(MAX_PATH)) == 0)
            {
                CString omBuf;
                omBuf.Format(_(BUSMASTER_LOG_ALREADYEXISTS), omStrLogFile);
                MessageBox(omBuf, "BUSMASTER", MB_OK | MB_ICONINFORMATION);
                return;
            }
        }
        (GetDlgItem(IDC_EDIT_LOGFILEPATH))->SetWindowText(omStrLogFile);
        m_omListLogFiles.SetItemText(m_nLogIndexSel, 0, omStrLogFile);
        vUpdate_Datastore_From_GUI((USHORT) m_nLogIndexSel, IDC_EDIT_LOGFILEPATH);
        vResetAdvLogParameters();
        vAddSuffixToLogFileName(SUFFIX_DEFAULT);
    }

    delete[]szTempBuf;

}
void CButtonItem::OnBnClicked()
{
    // TODO: Add your control notification handler code here
    m_bKillFocus = FALSE;
    m_bButtonclicked = TRUE;
    CFileDialog omFileDlg(TRUE, m_omStrDefExt, 0, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, m_omStrFilter);
    if( IDOK == omFileDlg.DoModal())
    {
        CString omStrFilePath = omFileDlg.GetPathName();
        m_pomEditItem->SetWindowText(omStrFilePath);
    }
    m_pomEditItem->SetFocus();
    m_bKillFocus = TRUE;
    ((CBrowseEditItem*)m_pomEditItem)->m_bKillFocus = TRUE;
    m_bButtonclicked = FALSE;
    

}
/**
 * \req RS_19_03 It shall be possible to add / remove any log file from the input data source set
 *
 * This function will be called when user selects Add button.
 * This will show file selection dialog to select replay file
 * and if the selection is valid this will add the selected file
 * in to the replay file list.
 */
void CReplayFileConfigDlg::OnBtnAddFile()
{
    // Throw File selection dialog to choose replay log file
    DWORD dwFlags =
        OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_EXTENSIONDIFFERENT;
    CFileDialog omFileDlg( TRUE,
                           defSTR_LOG_FILE_EXTENSION,
                           NULL,
                           dwFlags,
                           defLOG_FILTER,
                           NULL );
    //Set the caption
    omFileDlg.m_ofn.lpstrTitle = _(defSTR_REPLAY_FILE_SELECTION_TITLE);
    // Show File open dialog
    if( omFileDlg.DoModal() == IDOK )
    {
        CReplayFile ouNewReplayFile;
        // Assign the file name and leave the rest to default
        ouNewReplayFile.m_omStrFileName = omFileDlg.GetPathName();
        // Add the data in to the replay file data list
        m_rouManager.m_omReplayFiles.Add( ouNewReplayFile );
        // Insert this new item in to the list
        // Avoid UI update
        m_bUpdating = TRUE;
        // Get the size of the list
        int nIndex = m_omLstcReplayFiles.GetItemCount();
        // Insert at the end
        m_omLstcReplayFiles.InsertItem( nIndex, ouNewReplayFile.m_omStrFileName,
                                        defREPLAY_FILE_IMAGE_INDEX );
        // Update the checkbox status
        m_omLstcReplayFiles.SetCheck( nIndex, ouNewReplayFile.m_bEnabled );
        // This is the first item then enable Replay File components
        if( nIndex == 0 )
        {
            vEnableDisableButtons();
        }
        // Set the selection to this new item
        // Enable UI Update
        m_bUpdating = FALSE;
        m_omLstcReplayFiles.SetItemState( nIndex,
                                          LVIS_SELECTED | LVIS_FOCUSED,
                                          LVIS_SELECTED | LVIS_FOCUSED );
    }
}
void CConfigMsgLogDlg::OnBnClickedCbtnLogFilePath(void)
{
    USES_CONVERSION;
    CString omStrLogFile = "";
    (GetDlgItem(IDC_EDIT_LOGFILEPATH))->GetWindowText(omStrLogFile);
    // Show File Selection Dialog to select Log File
    CFileDialog omFileDlg(FALSE, BUSMASTER_LOG_FILE_EXTENSION, omStrLogFile,
                          OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
                          OFN_EXTENSIONDIFFERENT | OFN_OVERWRITEPROMPT,
                          BUSMASTER_LOG_FILTER, NULL);
    // Set the caption
    omFileDlg.m_ofn.lpstrTitle = BUSMASTER_LOG_SELECTION_TITLE;

    // Show File open dialog
    if (omFileDlg.DoModal() == IDOK)
    {
        omStrLogFile = omFileDlg.GetPathName();

        for (USHORT i = 0; i < GetLoggingBlockCount(); i++)
        {
            SLOGINFO sLogObject;
            GetLoggingBlock(i, sLogObject);

            if (_tcsicmp(sLogObject.m_sLogFileName, omStrLogFile.GetBuffer(MAX_PATH)) == 0)
            {
                CString omBuf;
                omBuf.Format(BUSMASTER_LOG_ALREADYEXISTS, omStrLogFile);
                AfxMessageBox(omBuf, MB_OK | MB_ICONINFORMATION);
                return;
            }
        }

        (GetDlgItem(IDC_EDIT_LOGFILEPATH))->SetWindowText(omStrLogFile);
        m_omListLogFiles.SetItemText(m_nLogIndexSel, 0, omStrLogFile);
        vUpdate_Datastore_From_GUI((USHORT) m_nLogIndexSel, IDC_EDIT_LOGFILEPATH);
    }
}