home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / clsxprt2 / clsxpr2a.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-29  |  9.3 KB  |  399 lines

  1. /*  Project clsxprt2
  2.     
  3.     Copyright ⌐ 1993. All Rights Reserved.
  4.  
  5.     SUBSYSTEM:    clsxprt2.exe Application
  6.     FILE:         clsxpr2a.cpp
  7.     AUTHOR:       
  8.  
  9.  
  10.     OVERVIEW
  11.     ========
  12.     Source file for implementation of clsxprt2App (TApplication).      
  13. */
  14.  
  15.  
  16. #include <owl\owlpch.h>
  17. #pragma hdrstop
  18.  
  19.  
  20. #include "clsxpr2a.h"
  21. #include "clsxp2ad.h"                        // Definition of about dialog.
  22.  
  23. #include "tfrmtdlg.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <dos.h>
  27.  
  28. //{{clsxprt2App Implementation}}
  29.  
  30.  
  31. //
  32. // Build a response table for all messages/commands handled
  33. // by the application.
  34. //
  35. DEFINE_RESPONSE_TABLE1(clsxprt2App, TApplication)
  36. //{{clsxprt2AppRSP_TBL_BEGIN}}
  37.     EV_COMMAND(CM_FILENEW, CmFileNew),
  38.     EV_COMMAND(CM_FILEOPEN, CmFileOpen),
  39.     EV_COMMAND(CM_FILECLOSE, CmFileClose),
  40.     EV_COMMAND(CM_HELPABOUT, CmHelpAbout),
  41.     EV_COMMAND(CM_INSDATETIME, CmInsertDateTime),
  42.     EV_COMMAND(CM_INSTIME, CmInsertTime),
  43.     EV_COMMAND(CM_LOWERCASE, CmLowercase),
  44.     EV_COMMAND(CM_REVERSE, CmReverse),
  45.     EV_COMMAND(CM_UPPERCASE, CmUppercase),
  46.     EV_COMMAND(CM_DATETIMEFORMAT, CmPreferences),
  47.     EV_COMMAND(CM_UPPERCASE, CmUppercase),
  48.     EV_COMMAND(CM_REVERSE, CmReverse),
  49.     EV_COMMAND(CM_LOWERCASE, CmLowercase),
  50.     EV_COMMAND(CM_INSTIME, CmInsertTime),
  51.     EV_COMMAND(CM_INSDATE, CmInsertDate),
  52.     EV_COMMAND(CM_INSDATETIME, CmInsertDateTime),
  53. //{{clsxprt2AppRSP_TBL_END}}
  54. END_RESPONSE_TABLE;
  55.  
  56.  
  57. //
  58. // FrameWindow must be derived to override Paint for Preview and Print.
  59. //
  60. class SDIDecFrame : public TDecoratedFrame {
  61. public:
  62.     SDIDecFrame (TWindow *parent, const char far *title, TWindow *clientWnd, BOOL trackMenuSelection = FALSE, TModule *module = 0) :
  63.             TDecoratedFrame(parent, title, clientWnd, trackMenuSelection, module)
  64.       {  }
  65.     ~SDIDecFrame ()
  66.       {  }
  67. };
  68.  
  69.  
  70. //////////////////////////////////////////////////////////
  71. // clsxprt2App
  72. // =====
  73. //
  74. clsxprt2App::clsxprt2App () : TApplication("clsxprt2")
  75. {
  76.  
  77.     // Common file file flags and filters for Open/Save As dialogs.  Filename and directory are
  78.     // computed in the member functions CmFileOpen, and CmFileSaveAs.
  79.     FileData.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  80.     FileData.SetFilter("All Files (*.*)|*.*|");
  81.  
  82.     // INSERT>> Your constructor code here.
  83.     IsAmPm = FALSE;
  84.     df = mmddyy;
  85. }
  86.  
  87.  
  88. clsxprt2App::~clsxprt2App ()
  89. {
  90.     // INSERT>> Your destructor code here.
  91.  
  92. }
  93.  
  94.  
  95. //////////////////////////////////////////////////////////
  96. // clsxprt2App
  97. // =====
  98. // Application intialization.
  99. //
  100. void clsxprt2App::InitMainWindow ()
  101. {
  102.     Client = new TEditFile(0, 0, 0);
  103.     SDIDecFrame *frame = new SDIDecFrame(0, GetName(), Client, FALSE);
  104.  
  105.     nCmdShow = nCmdShow != SW_SHOWMINIMIZED ? SW_SHOWNORMAL : nCmdShow;
  106.  
  107.     //
  108.     // Assign ICON w/ this application.
  109.     //
  110.     frame->SetIcon(this, IDI_SDIAPPLICATION);
  111.  
  112.     //
  113.     // Menu associated with window and accelerator table associated with table.
  114.     //
  115.     frame->AssignMenu(SDI_MENU);
  116.     
  117.     //
  118.     // Associate with the accelerator table.
  119.     //
  120.     frame->Attr.AccelTable = SDI_MENU;
  121.  
  122.   
  123.     MainWindow = frame;
  124.  
  125. }
  126.  
  127.  
  128. //////////////////////////////////////////////////////////
  129. // clsxprt2App
  130. // ===========
  131. // Menu File New command
  132. void clsxprt2App::CmFileNew ()
  133. {
  134.     Client->NewFile();
  135. }
  136.  
  137.  
  138. //////////////////////////////////////////////////////////
  139. // clsxprt2App
  140. // ===========
  141. // Menu File Open command
  142. void clsxprt2App::CmFileOpen ()
  143. {
  144.     //
  145.     // Display standard Open dialog box to select a file name.
  146.     //
  147.     *FileData.FileName = 0;
  148.     if (Client->CanClose())
  149.         if (TFileOpenDialog(MainWindow, FileData).Execute() == IDOK)
  150.             OpenFile();
  151. }
  152.  
  153.  
  154. void clsxprt2App::OpenFile (const char *fileName)
  155. {
  156.     if (fileName)
  157.         lstrcpy(FileData.FileName, fileName);
  158.  
  159.     Client->ReplaceWith(FileData.FileName);
  160. }
  161.  
  162.  
  163. //////////////////////////////////////////////////////////
  164. // clsxprt2App
  165. // =====
  166. // Menu File Close command
  167. void clsxprt2App::CmFileClose ()
  168. {
  169.      if (Client->CanClose())
  170.              Client->DeleteSubText(0, UINT(-1));
  171. }
  172.  
  173.  
  174. //////////////////////////////////////////////////////////
  175. // clsxprt2App
  176. // ===========
  177. // Menu Help About clsxprt2.exe command
  178. void clsxprt2App::CmHelpAbout ()
  179. {
  180.     //
  181.     // Show the modal dialog.
  182.     //
  183.     clsxprt2AboutDlg(MainWindow).Execute();
  184. }
  185.  
  186.  
  187. int OwlMain (int , char* [])
  188. {
  189.     clsxprt2App     App;
  190.     int             result;
  191.  
  192.     result = App.Run();
  193.  
  194.     return result;
  195. }
  196.  
  197. void clsxprt2App::CmInsertTime ()
  198. {
  199.   struct time tm;
  200.   char szStr[41];
  201.  
  202.   gettime(&tm);
  203.   if (IsAmPm) {
  204.     if (tm.ti_hour == 12)
  205.       sprintf(szStr, "12:%02d:%02d p.m.",
  206.                  tm.ti_min, tm.ti_sec);
  207.     else if (tm.ti_hour > 12)
  208.       sprintf(szStr, "%2d:%02d:%02d p.m.",
  209.              tm.ti_hour - 12, tm.ti_min, tm.ti_sec);
  210.     else
  211.        sprintf(szStr, "%2d:%02d:%02d a.m.",
  212.              tm.ti_hour, tm.ti_min, tm.ti_sec);
  213.   }
  214.   else
  215.     sprintf(szStr, "%2d:%02d:%02d",
  216.              tm.ti_hour, tm.ti_min, tm.ti_sec);
  217.   Client->Insert(szStr);
  218.  
  219. }
  220.  
  221.  
  222. void clsxprt2App::CmLowercase ()
  223. {
  224.   UINT startPos, endPos;
  225.   int numChars;
  226.   char* pszStr;
  227.  
  228.   Client->GetSelection(startPos, endPos);
  229.   // is there selected text
  230.   if (startPos < endPos) {
  231.     numChars = endPos - startPos + 1;
  232.     pszStr = new char[numChars+1];
  233.     Client->GetSubText(pszStr, startPos, endPos);
  234.     strlwr(pszStr);
  235.      Client->Insert(pszStr);
  236.     Client->SetSelection(startPos, endPos);
  237.     delete [] pszStr;
  238.   }
  239.   else {
  240.     numChars = Client->GetWindowTextLength();
  241.     pszStr = new char[numChars+1];
  242.     Client->GetSubText(pszStr, 0, (UINT)numChars);
  243.     strlwr(pszStr);
  244.     Client->DeleteSubText(0, (UINT)numChars);
  245.     Client->SetSelection(0, 0);
  246.     Client->Insert(pszStr);
  247.     delete [] pszStr;
  248.   }
  249. }
  250.  
  251.  
  252. void clsxprt2App::CmReverse ()
  253. {
  254.   UINT startPos, endPos;
  255.   int numChars;
  256.   char* pszStr;
  257.   char swapChar;
  258.  
  259.   Client->GetSelection(startPos, endPos);
  260.   // is there selected text
  261.   if (startPos < endPos) {
  262.     numChars = endPos - startPos + 1;
  263.     pszStr = new char[numChars+1];
  264.     Client->GetSubText(pszStr, startPos, endPos);
  265.     for (int i = 0, j = strlen(pszStr)-1; i < j ; i++, j--) {
  266.       swapChar = pszStr[i];
  267.       pszStr[i] = pszStr[j];
  268.       pszStr[j] = swapChar;
  269.     }
  270.      Client->Insert(pszStr);
  271.     Client->SetSelection(startPos, endPos);
  272.     delete [] pszStr;
  273.   }
  274.   else {
  275.     numChars = Client->GetWindowTextLength();
  276.     pszStr = new char[numChars+1];
  277.     Client->GetSubText(pszStr, 0, (UINT)numChars);
  278.     for (int i = 0, j = strlen(pszStr)-1; i < j ; i++, j--) {
  279.       swapChar = pszStr[i];
  280.       pszStr[i] = pszStr[j];
  281.       pszStr[j] = swapChar;
  282.     }
  283.     Client->DeleteSubText(0, (UINT)numChars);
  284.     Client->SetSelection(0, 0);
  285.     Client->Insert(pszStr);
  286.     delete [] pszStr;
  287.   }
  288. }
  289.  
  290.  
  291. void clsxprt2App::CmUppercase ()
  292. {
  293.   UINT startPos, endPos;
  294.   int numChars;
  295.   char* pszStr;
  296.  
  297.   Client->GetSelection(startPos, endPos);
  298.   // is there selected text
  299.   if (startPos < endPos) {
  300.     numChars = endPos - startPos + 1;
  301.     pszStr = new char[numChars+1];
  302.     Client->GetSubText(pszStr, startPos, endPos);
  303.     strupr(pszStr);
  304.     Client->Insert(pszStr);
  305.     Client->SetSelection(startPos, endPos);
  306.     delete [] pszStr;
  307.   }
  308.   else {
  309.     numChars = Client->GetWindowTextLength();
  310.     pszStr = new char[numChars+1];
  311.     Client->GetSubText(pszStr, 0, (UINT)numChars);
  312.     strupr(pszStr);
  313.     Client->DeleteSubText(0, (UINT)numChars);
  314.     Client->SetSelection(0, 0);
  315.     Client->Insert(pszStr);
  316.     delete [] pszStr;
  317.   }
  318. }
  319.  
  320. void clsxprt2App::CmPreferences ()
  321. {
  322.   TFrmtDialog* pDlg = new TFrmtDialog(Client,
  323.                           TResId(IDD_DATETIME_DLG));
  324.   if (pDlg->Execute() == IDOK) {
  325.     // save time format
  326.     IsAmPm = pDlg->IsAmPm;
  327.     // save date format
  328.     df = pDlg->df;
  329.   }
  330. }
  331.  
  332.  
  333. void clsxprt2App::CmInsertDateTime ()
  334. {
  335.   struct date dt;
  336.   struct time tm;
  337.   char szStr[41];
  338.  
  339.   getdate(&dt);
  340.   switch (df) {
  341.     case mmddyy:
  342.        sprintf(szStr, "%2d/%02d/%4d ",
  343.            dt.da_mon, dt.da_day, dt.da_year);
  344.        break;
  345.     case ddmmyy:
  346.        sprintf(szStr, "%2d/%02d/%4d ",
  347.            dt.da_day, dt.da_mon, dt.da_year);
  348.        break;
  349.     default:
  350.        sprintf(szStr, "%4d/%02d/%02d ",
  351.            dt.da_year, dt.da_day, dt.da_mon);
  352.        break;
  353.   }
  354.   Client->Insert(szStr);
  355.  
  356.   gettime(&tm);
  357.   if (IsAmPm) {
  358.     if (tm.ti_hour == 12)
  359.       sprintf(szStr, "12:%02d:%02d p.m.",
  360.                  tm.ti_min, tm.ti_sec);
  361.     else if (tm.ti_hour > 12)
  362.       sprintf(szStr, "%2d:%02d:%02d p.m.",
  363.              tm.ti_hour - 12, tm.ti_min, tm.ti_sec);
  364.     else
  365.        sprintf(szStr, "%2d:%02d:%02d a.m.",
  366.              tm.ti_hour, tm.ti_min, tm.ti_sec);
  367.   }
  368.   else
  369.     sprintf(szStr, "%2d:%02d:%02d",
  370.              tm.ti_hour, tm.ti_min, tm.ti_sec);
  371.   Client->Insert(szStr);
  372.  
  373. }
  374.  
  375. void clsxprt2App::CmInsertDate ()
  376. {
  377.   struct date dt;
  378.   char szStr[41];
  379.  
  380.   getdate(&dt);
  381.   switch (df) {
  382.     case mmddyy:
  383.        sprintf(szStr, "%2d/%02d/%4d",
  384.            dt.da_mon, dt.da_day, dt.da_year);
  385.        break;
  386.     case ddmmyy:
  387.        sprintf(szStr, "%2d/%02d/%4d",
  388.            dt.da_day, dt.da_mon, dt.da_year);
  389.        break;
  390.     default:
  391.        sprintf(szStr, "%4d/%02d/%02d",
  392.            dt.da_year, dt.da_day, dt.da_mon);
  393.        break;
  394.   }
  395.   Client->Insert(szStr);
  396.  
  397. }
  398.  
  399.