home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Info / TeachU14 / SAMS / Code / Day07 / spmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-08  |  5.3 KB  |  177 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl\vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "SPMain.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TScratchPad *ScratchPad;
  10. //---------------------------------------------------------------------------
  11. __fastcall TScratchPad::TScratchPad(TComponent* Owner)
  12.   : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. void __fastcall TScratchPad::FileExitClick(TObject *Sender)
  17. {
  18.   //
  19.   // All done. Close the form.
  20.   //
  21.   Close();  
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TScratchPad::EditCutClick(TObject *Sender)
  25. {
  26.   //
  27.   // Call TMemo::CutToClipboard().
  28.   //
  29.   Memo->CutToClipboard();
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TScratchPad::EditCopyClick(TObject *Sender)
  33. {
  34.   //
  35.   // Call TMemo::CopyToClipboard().
  36.   //
  37.   Memo->CopyToClipboard();
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TScratchPad::EditPasteClick(TObject *Sender)
  41. {
  42.   //
  43.   // Call TMemo::PasteFromClipboard().
  44.   //
  45.   Memo->PasteFromClipboard();
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TScratchPad::FileNewClick(TObject *Sender)
  49. {
  50.   //
  51.   // Open a file. First check to see if the current file
  52.   // needs to be saved.
  53.   //
  54.   if (Memo->Modified) {
  55.     //
  56.     // Display a message box.
  57.     //
  58.     int result = Application->MessageBox(
  59.       "The current file has changed. Save changes?",
  60.       "ScratchPad Message", MB_YESNOCANCEL);
  61.     //
  62.     // If Yes was clicked then save the current file.
  63.     //
  64.      if (result == IDYES) FileSaveClick(Sender);
  65.     //
  66.     // If No was clicked then do nothing.
  67.     //
  68.     if (result == IDCANCEL) return;
  69.   }
  70.   //
  71.   // Delete the strings in the memo, if any.
  72.   //
  73.   if (Memo->Lines->Count > 0) Memo->Clear();
  74.   //
  75.   // Set the FileName property of the Save Dialog to a
  76.   // blank string. This lets us know that the file has
  77.   // not yet been saved.
  78.   //
  79.   SaveDialog->FileName = "";
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TScratchPad::FileOpenClick(TObject *Sender)
  83. {
  84.   //
  85.   // Open a file. First check to see if the current file needs
  86.   // to be saved. Same logic as in FileNewClick() above.
  87.   //
  88.   if (Memo->Modified) {
  89.     int result = Application->MessageBox(
  90.       "The current file has changed. Save changes?",
  91.       "ScratchPad Message", MB_YESNOCANCEL);
  92.      if (result == IDYES) FileSaveClick(0);
  93.     if (result == IDCANCEL) return;
  94.   }
  95.   //
  96.   // Execute the File Open dialog. If OK was pressed then
  97.   // open the file using the LoadFromFile() method. First
  98.   // clear the FileName property.
  99.   //
  100.   OpenDialog->FileName = "";
  101.   if (OpenDialog->Execute())
  102.   {
  103.     if (Memo->Lines->Count > 0) Memo->Clear();
  104.      Memo->Lines->LoadFromFile(OpenDialog->FileName);
  105.     SaveDialog->FileName = OpenDialog->FileName;
  106.   }
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TScratchPad::FileSaveClick(TObject *Sender)
  110. {
  111.   //
  112.   // If a filename has already been provided then there is
  113.   // no need to bring up the File Save dialog. Just save the
  114.   // file using SaveToFile().
  115.   //
  116.   if (SaveDialog->FileName != "")
  117.   {
  118.     Memo->Lines->SaveToFile(SaveDialog->FileName);
  119.     //
  120.     // Set Modified to false since we've just saved.
  121.     //
  122.     Memo->Modified = false;
  123.   }
  124.   //
  125.   // If no filename was set then do a SaveAs().
  126.   //
  127.   else FileSaveAsClick(Sender);
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TScratchPad::FileSaveAsClick(TObject *Sender)
  131. {
  132.   //
  133.   // Display the File Save dialog to save the file.
  134.   // Set Modified to false since we just saved.
  135.   //
  136.   SaveDialog->Title = "Save As";
  137.   if (SaveDialog->Execute())
  138.   {
  139.     Memo->Lines->SaveToFile(SaveDialog->FileName);
  140.     Memo->Modified = false;
  141.   }
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TScratchPad::EditUndoClick(TObject *Sender)
  145. {
  146.   //
  147.   // TMemo doesn't have an Undo method so we have to send
  148.   // a Windows WM_UNDO message to the memo component.
  149.   //
  150.   SendMessage(Memo->Handle, WM_UNDO, 0, 0);
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TScratchPad::EditSelectAllClick(TObject *Sender)
  154. {
  155.   //
  156.   // Just call TMemo::SelectAll().
  157.   //
  158.   Memo->SelectAll();
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TScratchPad::EditWordWrapClick(TObject *Sender)
  162. {
  163.   //
  164.   // Toggle the TMemo::WordWrap property. Set the Checked
  165.   // property of the menu item to the same value as WordWrap.
  166.   //
  167.   Memo->WordWrap = !Memo->WordWrap;
  168.   EditWordWrap->Checked = Memo->WordWrap;
  169.   //
  170.   // If WordWrap is on then we only need the vertical scroll
  171.   // bar. If it's off, then we need both scroll bars.
  172.   //
  173.   if (Memo->WordWrap) Memo->ScrollBars = ssVertical;
  174.   else Memo->ScrollBars = ssBoth;
  175. }
  176. //---------------------------------------------------------------------------
  177.