home *** CD-ROM | disk | FTP | other *** search
/ ftp.funduc.com / 2014.08.ftp.funduc.com.tar / ftp.funduc.com / fshedcode-072212.zip / fshed.cpp < prev    next >
C/C++ Source or Header  |  2012-07-03  |  14KB  |  391 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //    License (GPLv2+):
  3. //    This program is free software; you can redistribute it and/or modify
  4. //    it under the terms of the GNU General Public License as published by
  5. //    the Free Software Foundation; either version 2 of the License, or
  6. //    (at your option) any later version.
  7. //
  8. //    This program is distributed in the hope that it will be useful, but
  9. //    WITHOUT ANY WARRANTY; without even the implied warranty of
  10. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. //    General Public License for more details.
  12. //
  13. //    You should have received a copy of the GNU General Public License
  14. //    along with this program; if not, write to the Free Software
  15. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. /////////////////////////////////////////////////////////////////////////////
  17.  
  18. // fshed.cpp : Defines the class behaviors for the application.
  19. //
  20.  
  21. #include "stdafx.h"
  22. #include <afxadv.h>
  23. #include "fshed.h"
  24.  
  25. #include "MainFrm.h"
  26. #include "ChildFrm.h"
  27. #include "fshedDoc.h"
  28. #include "fshedView.h"
  29. #include "OptionsDlg.h"
  30. #include "colorspage.h"
  31.  
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CFshedApp
  40.  
  41. BEGIN_MESSAGE_MAP(CFshedApp, CWinApp)
  42.     //{{AFX_MSG_MAP(CFshedApp)
  43.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  44.     ON_COMMAND(ID_VIEW_OPTIONS, OnViewOptions)
  45.     //}}AFX_MSG_MAP
  46.     // Standard file based document commands
  47.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  48.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  49.     // Standard print setup command
  50.     ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  51. END_MESSAGE_MAP()
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CFshedApp construction
  55. #define FSH_INI_SECTION1 _T("Settings")
  56. #define FSH_INI_ENTRY1   _T("Text Editor Name")
  57. #define FSH_INI_ENTRY2   _T("Automatic BPL")
  58. #define FSH_INI_ENTRY3   _T("Bytes per line")
  59. #define FSH_INI_ENTRY4   _T("Offset Length")
  60. #define FSH_INI_ENTRY5   _T("Character Set")
  61. #define FSH_INI_ENTRY6   _T("Font Size")
  62. #define FSH_INI_ENTRY7   _T("Open Read Only")
  63. #define FSH_INI_ENTRY8   _T("Binary Mode")
  64. #define FSH_INI_ENTRY9   _T("Show Unsigned")
  65.  
  66. #define FSH_INI_SECTION2 _T("Colors")
  67. #define FSH_INI_ENTRY2_1 _T("Text")
  68. #define FSH_INI_ENTRY2_2 _T("Selected Text")
  69. #define FSH_INI_ENTRY2_3 _T("Background")
  70. #define FSH_INI_ENTRY2_4 _T("Selected Background")
  71. #define FSH_INI_ENTRY2_5 _T("Bookmark")
  72. #define FSH_INI_ENTRY2_6 _T("Separator")
  73.  
  74. HWND WINAPI AfxHtmlHelp(HWND hWnd, LPCTSTR szHelpFilePath, UINT nCmd, DWORD_PTR dwData);
  75.  
  76. CFshedApp::CFshedApp()
  77. {
  78.     m_bAutomaticBPL = BST_CHECKED;
  79.     m_iBytesPerLine = 16;
  80.     m_iOffsetLen    = 6;
  81.     m_iCharacterSet = ANSI_FIXED_FONT;
  82.     m_iFontSize     = 10;
  83.     m_bOpenReadOnly = FALSE;
  84.     m_iBinaryMode   = LITTLEENDIAN_MODE;
  85.     m_bUnsignedView = TRUE;
  86.     m_clrBmk = RGB( 255, 0, 0 );
  87.     m_clrSelBk = RGB( 255, 255, 0 );
  88.     m_clrSelText = RGB( 0, 0, 0 );
  89.     m_clrText = RGB (0,0,0);
  90.     m_clrBk = RGB (255,255,255);
  91.     m_clrSep = RGB (192,192,192);
  92.     EnableHtmlHelp();
  93. }
  94.  
  95. void CFshedApp::WinHelpInternal(DWORD_PTR dwData, UINT nCmd)
  96. {
  97.     if (nCmd == HELP_CONTEXT)
  98.         AfxHtmlHelp(NULL, AfxGetApp()->m_pszHelpFilePath, HH_HELP_CONTEXT, dwData);
  99.     else
  100.         CWinApp::WinHelpInternal(dwData, nCmd);
  101. }
  102.  
  103. CFshedApp::~CFshedApp()
  104. {
  105.     WriteProfileString(FSH_INI_SECTION1, FSH_INI_ENTRY1, m_TexteditorName);
  106.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY2, m_bAutomaticBPL);
  107.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY3, m_iBytesPerLine);
  108.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY4, m_iOffsetLen);
  109.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY5, m_iCharacterSet);
  110.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY6, m_iFontSize);
  111.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY7, m_bOpenReadOnly);
  112.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY8, m_iBinaryMode);
  113.     WriteProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY9, m_bUnsignedView);
  114.     WriteProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_1, m_clrText);
  115.     WriteProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_2, m_clrSelText);
  116.     WriteProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_3, m_clrBk);
  117.     WriteProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_4, m_clrSelBk);
  118.     WriteProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_5, m_clrBmk);
  119.     WriteProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_6, m_clrSep);
  120. }
  121. /////////////////////////////////////////////////////////////////////////////
  122. // The one and only CFshedApp object
  123.  
  124. CFshedApp theApp;
  125.  
  126. class CFshedCommandLineInfo : public CCommandLineInfo
  127. {
  128. public:
  129.    CFshedCommandLineInfo() { m_dwStartOffset = m_dwEndOffset = 0; }
  130.    DWORD m_dwStartOffset, m_dwEndOffset;
  131.    void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast)
  132.    {
  133.       CCommandLineInfo::ParseParam(pszParam, bFlag, bLast);
  134.       if (bFlag)
  135.       {
  136.          LPTSTR pEnd;
  137.          switch (pszParam[0])
  138.          {
  139.             case 's':
  140.             case 'S':
  141.                 m_dwStartOffset = _tcstoul(pszParam + 1, &pEnd, 0);
  142.             break;
  143.             case 'e':
  144.             case 'E':
  145.                 m_dwEndOffset = _tcstoul(pszParam + 1, &pEnd, 0);
  146.             break;
  147.             case 'l':
  148.             case 'L':
  149.                 m_dwEndOffset = m_dwStartOffset + _tcstoul(pszParam + 1, &pEnd, 0) - 1;
  150.                 if (m_dwEndOffset < m_dwStartOffset)
  151.                    m_dwEndOffset = m_dwStartOffset;
  152.             break;
  153.          }
  154.       }
  155.    }
  156. };
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CFshedApp initialization
  160.  
  161. BOOL CFshedApp::InitInstance()
  162. {
  163.     // Standard initialization
  164.     // If you are not using these features and wish to reduce the size
  165.     //  of your final executable, you should remove from the following
  166.     //  the specific initialization routines you do not need.
  167.  
  168.     // Change the registry key under which our settings are stored.
  169.     // TODO: You should modify this string to be something appropriate
  170.     // such as the name of your company or organization.
  171.     //SetRegistryKey(IDS_COMP_NAME);
  172.     free((void*)m_pszProfileName);
  173.     CString strPath = m_pszHelpFilePath;
  174.     strPath = strPath.Left(strPath.ReverseFind('.') + 1) + _T("ini");
  175.     m_pszProfileName = _tcsdup(strPath);
  176.  
  177.     strPath = (LPCTSTR)GetProfileString(FSH_INI_SECTION1, FSH_INI_ENTRY1);
  178.     m_TexteditorName = (LPTSTR)(LPCTSTR)strPath;
  179.     m_bAutomaticBPL = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY2, m_bAutomaticBPL);
  180.     m_iBytesPerLine = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY3, m_iBytesPerLine);
  181.     m_iOffsetLen = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY4, m_iOffsetLen);
  182.     m_iCharacterSet = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY5, m_iCharacterSet);
  183.     m_iFontSize = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY6, m_iFontSize);
  184.     m_bOpenReadOnly = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY7, m_bOpenReadOnly);
  185.     m_iBinaryMode = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY8, m_iBinaryMode);
  186.     m_bUnsignedView = GetProfileInt(FSH_INI_SECTION1, FSH_INI_ENTRY9, m_bUnsignedView);
  187.     m_clrText = GetProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_1, m_clrText);
  188.     m_clrSelText = GetProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_2, m_clrSelText);
  189.     m_clrBk = GetProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_3, m_clrBk);
  190.     m_clrSelBk = GetProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_4, m_clrSelBk);
  191.     m_clrBmk = GetProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_5, m_clrBmk);
  192.     m_clrSep = GetProfileInt(FSH_INI_SECTION2, FSH_INI_ENTRY2_6, m_clrSep);
  193.  
  194.     //LoadStdProfileSettings(5);  // Load standard INI file options (including MRU)
  195.     // create file MRU since nMaxMRU not zero
  196.     m_pRecentFileList = new CRecentFileList(0, _T("Recent File List"), _T("File%d"), 5);
  197.     m_pRecentFileList->ReadList();
  198.  
  199.     // Register the application's document templates.  Document templates
  200.     //  serve as the connection between documents, frame windows and views.
  201.  
  202.     CMultiDocTemplate* pDocTemplate;
  203.     pDocTemplate = new CMultiDocTemplate(
  204.         IDR_FSHEDTYPE,
  205.         RUNTIME_CLASS(CFshedDoc),
  206.         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  207.         RUNTIME_CLASS(CFshedView));
  208.     AddDocTemplate(pDocTemplate);
  209.  
  210.     // create main MDI Frame window
  211.     CMainFrame* pMainFrame = new CMainFrame;
  212.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  213.         return FALSE;
  214.     m_pMainWnd = pMainFrame;
  215.  
  216.     // Parse command line for standard shell commands, DDE, file open
  217.     CFshedCommandLineInfo cmdInfo;
  218.     ParseCommandLine(cmdInfo);
  219.  
  220.    if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew)
  221.        cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
  222.     // Dispatch commands specified on the command line
  223.     if (!ProcessShellCommand(cmdInfo))
  224.         return FALSE;
  225.  
  226.    if (CCommandLineInfo::FileOpen)
  227.    {
  228.       POSITION pos = pDocTemplate->GetFirstDocPosition();
  229.       if (pos)
  230.       {
  231.          CFshedDoc *pDoc = (CFshedDoc *)pDocTemplate->GetNextDoc(pos);
  232.          if (pDoc && (cmdInfo.m_dwStartOffset <= cmdInfo.m_dwEndOffset))
  233.          {
  234.             pos = pDoc->GetFirstViewPosition();
  235.             if (pos)
  236.             {
  237.                CFshedView *pView = (CFshedView *)pDoc->GetNextView(pos);
  238.                if (pView)
  239.                {
  240.                   pView->SetSel(cmdInfo.m_dwStartOffset, cmdInfo.m_dwEndOffset);
  241.                }
  242.             }
  243.          }
  244.       }
  245.    }
  246.  
  247.     // The main window has been initialized, so show and update it.
  248.     pMainFrame->ShowWindow(m_nCmdShow);
  249.     pMainFrame->UpdateWindow();
  250.  
  251.     return TRUE;
  252. }
  253.  
  254.  
  255. /////////////////////////////////////////////////////////////////////////////
  256. // CAboutDlg dialog used for App About
  257.  
  258. class CAboutDlg : public CDialog
  259. {
  260. public:
  261.     CAboutDlg();
  262.  
  263. // Dialog Data
  264.     //{{AFX_DATA(CAboutDlg)
  265.     enum { IDD = IDD_ABOUTBOX };
  266.     //}}AFX_DATA
  267.  
  268.     // ClassWizard generated virtual function overrides
  269.     //{{AFX_VIRTUAL(CAboutDlg)
  270.     protected:
  271.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  272.     //}}AFX_VIRTUAL
  273.     virtual BOOL OnInitDialog();
  274.  
  275. // Implementation
  276. protected:
  277.     //{{AFX_MSG(CAboutDlg)
  278.     afx_msg void OnVisit();
  279.     //}}AFX_MSG
  280.     DECLARE_MESSAGE_MAP()
  281. };
  282.  
  283. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  284. {
  285.     //{{AFX_DATA_INIT(CAboutDlg)
  286.     //}}AFX_DATA_INIT
  287. }
  288.  
  289. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  290. {
  291.     CDialog::DoDataExchange(pDX);
  292.     //{{AFX_DATA_MAP(CAboutDlg)
  293.     //}}AFX_DATA_MAP
  294. }
  295.  
  296. BOOL CAboutDlg::OnInitDialog()
  297. {
  298.     BOOL bRtn = CDialog::OnInitDialog();
  299.  
  300.     CWnd* pwndChild = GetDlgItem(IDC_STATIC_COPYRIGHT);    
  301.     if (pwndChild)
  302.     {
  303.         CString str;
  304.         pwndChild->GetWindowText(str);
  305.         CTime tmCurrent = CTime::GetCurrentTime();
  306.         CString strCurYear;
  307.         strCurYear.Format(_T("%d"), tmCurrent.GetYear());
  308.         if (str.Replace(_T("2010"), strCurYear))
  309.             pwndChild->SetWindowText(str);
  310.     }
  311.     return bRtn;
  312. }
  313.  
  314. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  315.     //{{AFX_MSG_MAP(CAboutDlg)
  316.     ON_BN_CLICKED(IDC_VISIT, OnVisit)
  317.     //}}AFX_MSG_MAP
  318. END_MESSAGE_MAP()
  319.  
  320. // App command to run the dialog
  321. void CFshedApp::OnAppAbout()
  322. {
  323.     CAboutDlg aboutDlg;
  324.     aboutDlg.DoModal();
  325. }
  326.  
  327. /////////////////////////////////////////////////////////////////////////////
  328. // CFshedApp message handlers
  329.  
  330.  
  331. void CFshedApp::OnViewOptions() 
  332. {
  333.     CPropertySheet optSheet(IDS_TTL_OPTIONS, AfxGetMainWnd());
  334.     CDispPage dispPage;
  335.     CGenPage  genPage;
  336.     CColorsPage clrPage;
  337.     optSheet.AddPage(&genPage);
  338.     optSheet.AddPage(&dispPage);
  339.     optSheet.AddPage(&clrPage);
  340.     genPage.m_strTextEditor = m_TexteditorName;
  341.     genPage.m_nNumBytes = m_iBytesPerLine;
  342.     genPage.m_bAdjustBytes = m_bAutomaticBPL;
  343.     genPage.m_nOffsetLen = m_iOffsetLen;
  344.     genPage.m_bSetRead = m_bOpenReadOnly;
  345.     dispPage.m_nSigned = !m_bUnsignedView;
  346.     dispPage.m_nAnsi = (m_iCharacterSet == OEM_FIXED_FONT) ? 1 : 0;
  347.     dispPage.m_nFontSize = m_iFontSize;
  348.     dispPage.m_nIntel = (m_iBinaryMode == BIGENDIAN_MODE) ? 1 : 0;
  349.     clrPage.m_clrbtnText.currentcolor = m_clrText;
  350.     clrPage.m_clrbtnSep.currentcolor = m_clrSep;
  351.     clrPage.m_clrbtnSelBack.currentcolor = m_clrSelBk;
  352.     clrPage.m_clrbtnSel.currentcolor = m_clrSelText;
  353.     clrPage.m_clrbtnBookmark.currentcolor = m_clrBmk;
  354.     clrPage.m_clrbtnBack.currentcolor = m_clrBk;
  355.     if (optSheet.DoModal() == IDOK)
  356.     {
  357.         m_TexteditorName = (LPTSTR)(LPCTSTR)genPage.m_strTextEditor;
  358.         m_iBytesPerLine = genPage.m_nNumBytes;
  359.         m_bAutomaticBPL = genPage.m_bAdjustBytes;
  360.         m_iOffsetLen = genPage.m_nOffsetLen;
  361.         m_bUnsignedView = !dispPage.m_nSigned;
  362.         m_bOpenReadOnly = genPage.m_bSetRead;
  363.         m_iCharacterSet = (dispPage.m_nAnsi ? OEM_FIXED_FONT : ANSI_FIXED_FONT);
  364.         m_iFontSize = dispPage.m_nFontSize;
  365.         m_iBinaryMode = (dispPage.m_nIntel ? BIGENDIAN_MODE : LITTLEENDIAN_MODE);
  366.         m_clrText = clrPage.m_clrbtnText.currentcolor;
  367.         m_clrSep = clrPage.m_clrbtnSep.currentcolor;
  368.         m_clrSelBk = clrPage.m_clrbtnSelBack.currentcolor;
  369.         m_clrSelText = clrPage.m_clrbtnSel.currentcolor;
  370.         m_clrBmk = clrPage.m_clrbtnBookmark.currentcolor;
  371.         m_clrBk = clrPage.m_clrbtnBack.currentcolor;
  372.  
  373.         POSITION pos = GetFirstDocTemplatePosition();
  374.         CMultiDocTemplate *pDocTemplate = (CMultiDocTemplate *)GetNextDocTemplate(pos);
  375.         if (pDocTemplate)
  376.         {
  377.             pos = pDocTemplate->GetFirstDocPosition();
  378.             while (pos)
  379.             {
  380.                CFshedDoc *pDoc = (CFshedDoc *)pDocTemplate->GetNextDoc(pos);
  381.                pDoc->UpdateAllViews(NULL);
  382.             }
  383.         }
  384.     }
  385. }
  386.  
  387. void CAboutDlg::OnVisit() 
  388. {
  389.     ShellExecute(m_hWnd, _T("open"), _T("http://www.searchandreplace.com/fshexedit.htm"), NULL, NULL, SW_SHOWNORMAL );
  390. }
  391.