home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / ROMAIN.CPP < prev    next >
C/C++ Source or Header  |  1997-02-14  |  15KB  |  369 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1997 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------
  6. // RICH EDIT DEMO v.02
  7. //
  8. //
  9. //---------------------------------------------------------------------
  10. #include <vcl.h>
  11. #include <windows.hpp>
  12. #pragma hdrstop
  13.  
  14. #include "Romain.h"
  15. #include "RichAbt.h"
  16.  
  17. const float RulerAdj = 4.0/3.0;
  18. const int GutterWid = 6;
  19. //----------------------------------------------------------------------------
  20. #pragma resource "*.dfm"
  21. TMainForm *MainForm;
  22. //----------------------------------------------------------------------------
  23. __fastcall TMainForm::TMainForm(TComponent *Owner)
  24.   : TForm(Owner)
  25. {
  26.      SetFileName((AnsiString)"Untitled");
  27. }
  28. //----------------------------------------------------------------------------
  29. void __fastcall TMainForm::SelectionChange(TObject */*Sender*/)
  30. {
  31.   char sizebuf[6];
  32.  
  33.   try {
  34.        FUpdating = True;
  35.        FirstInd->Left = int(RichEdit1->Paragraph->FirstIndent*RulerAdj)-
  36.                             4+GutterWid;
  37.        LeftInd->Left  = int((RichEdit1->Paragraph->LeftIndent+
  38.                             RichEdit1->Paragraph->FirstIndent)*RulerAdj)-
  39.                             4+GutterWid;
  40.        RightInd->Left = Ruler->ClientWidth-6-int(
  41.                         (RichEdit1->Paragraph->RightIndent+GutterWid)*RulerAdj);
  42.  
  43.        BoldButton->Down = RichEdit1->SelAttributes->Style.Contains(fsBold);
  44.        ItalicButton->Down = RichEdit1->SelAttributes->Style.Contains(fsItalic);
  45.        UnderlineButton->Down = RichEdit1->SelAttributes->Style.Contains(fsUnderline);
  46.  
  47.        BulletsButton->Down = bool(RichEdit1->Paragraph->Numbering);
  48.  
  49.        FontSize->Text = itoa(RichEdit1->SelAttributes->Size, sizebuf, 10);
  50.        FontName->Text = RichEdit1->SelAttributes->Name;
  51.  
  52.        switch((int)RichEdit1->Paragraph->Alignment)
  53.        { case 0: LeftAlign->Down   = True; break;
  54.          case 1: RightAlign->Down  = True; break;
  55.          case 2: CenterAlign->Down = True; break;
  56.        }
  57.      }
  58.      catch (...) {
  59.        FUpdating = False;
  60.      }
  61.      FUpdating = False;
  62. }
  63. //----------------------------------------------------------------------------
  64. TTextAttributes *__fastcall TMainForm::CurrText(void)
  65. {
  66.     return RichEdit1->SelAttributes;
  67. }
  68. //----------------------------------------------------------------------------
  69. int __stdcall EnumFontsProc(TLogFontA &LogFont, TTextMetricA &/*TextMetric*/,
  70.                                 int /*FontType*/, Pointer Data)
  71. {   ((TStrings *)Data)->Add((AnsiString)LogFont.lfFaceName);
  72.      return 1;
  73. }
  74. //----------------------------------------------------------------------------
  75. void __fastcall TMainForm::GetFontNames(void)
  76. {    HDC hDC = GetDC(0);
  77.      void * cTmp = (void *)FontName->Items;
  78.      EnumFonts(hDC, NULL, (FONTENUMPROC) EnumFontsProc, (long) cTmp );
  79.      ReleaseDC(0,hDC);
  80.      FontName->Sorted = True;
  81. }
  82. //----------------------------------------------------------------------------
  83. void __fastcall TMainForm::SetFileName(const AnsiString FileName)
  84. {
  85.      LPSTR lpBuf = new char[MAX_PATH];
  86.      sprintf(lpBuf, "%s - %s", ExtractFileName(FileName).c_str(),
  87.              Application->Title.c_str());
  88.      Caption = (AnsiString)lpBuf;
  89.      FFileName = FileName;
  90.      delete lpBuf;
  91. }
  92. //----------------------------------------------------------------------------
  93. void __fastcall TMainForm::CheckFileSave(void)
  94. {    if ( RichEdit1->Modified ) {
  95.         switch(MessageBox(Handle,"Save Changes?","Confimation",MB_YESNOCANCEL | MB_ICONQUESTION))
  96.         {  case ID_YES    : FileSaveClick(this);
  97.            case ID_CANCEL : Abort();
  98.         };
  99.      }
  100. }
  101. //----------------------------------------------------------------------------
  102. void __fastcall TMainForm::SetupRuler(void)
  103. {    int iCtr = 1;
  104.      char sTmp[201];
  105.      while (iCtr < 200) {
  106.        sTmp[iCtr] = 9;
  107.        iCtr++;
  108.        sTmp[iCtr] = '|';
  109.        iCtr++;
  110.      }
  111.      Ruler->Caption = (AnsiString)sTmp;
  112. }
  113. //----------------------------------------------------------------------------
  114. void __fastcall TMainForm::SetEditRect(void)
  115. {    TRect Rct = Rect(GutterWid, 0, RichEdit1->ClientWidth-GutterWid,
  116.                                ClientHeight);
  117.      SendMessage(RichEdit1->Handle, EM_SETRECT, 0, long(&Rct));
  118. }
  119. //----------------------------------------------------------------------------
  120. void __fastcall TMainForm::FormCreate(TObject* /*Sender*/)
  121. {    Application->OnHint = &ShowHint;
  122.      OpenDialog->InitialDir = ExtractFilePath(ParamStr(0));
  123.      SaveDialog->InitialDir = OpenDialog->InitialDir;
  124.      GetFontNames();
  125.      SetupRuler();
  126.      SelectionChange(this);
  127. }
  128. //----------------------------------------------------------------------------
  129. void __fastcall TMainForm::ShowHint(TObject* /*Sender*/)
  130. {    StatusBar->SimpleText = Application->Hint;
  131. }
  132. //----------------------------------------------------------------------------
  133. void __fastcall TMainForm::FileNewClick(TObject* /*Sender*/)
  134. {    CheckFileSave();
  135.      SetFileName((AnsiString)"Untitled");
  136.      RichEdit1->Lines->Clear();
  137.      RichEdit1->Modified = False;
  138. }
  139. //----------------------------------------------------------------------------
  140. void __fastcall TMainForm::FileOpenClick(TObject* /*Sender*/)
  141. {    CheckFileSave();
  142.      if (OpenDialog->Execute()) {
  143.         RichEdit1->Lines->LoadFromFile(OpenDialog->FileName);
  144.         SetFileName(OpenDialog->FileName);
  145.         RichEdit1->SetFocus();
  146.         RichEdit1->Modified = False;
  147.         RichEdit1->ReadOnly = OpenDialog->Options.Contains(ofReadOnly);
  148.      }
  149. }
  150. //----------------------------------------------------------------------------
  151. void __fastcall TMainForm::FileSaveClick(TObject* Sender)
  152. {    if ( !strcmp(FFileName.c_str(), "Untitled") )
  153.         FileSaveAsClick(Sender);
  154.      else
  155.      {
  156.         RichEdit1->Lines->SaveToFile(FFileName);
  157.         RichEdit1->Modified = False;
  158.      }
  159. }
  160. //----------------------------------------------------------------------------
  161. void __fastcall TMainForm::FileSaveAsClick(TObject* /*Sender*/)
  162. {    if ( SaveDialog->Execute() ) {
  163.         // Options + OverwritePrompt = True thus no need to check.
  164.         RichEdit1->Lines->SaveToFile(SaveDialog->FileName);
  165.         SetFileName(SaveDialog->FileName);
  166.         RichEdit1->Modified = False;
  167.      }
  168. }
  169. //----------------------------------------------------------------------------
  170. void __fastcall TMainForm::FilePrintClick(TObject* /*Sender*/)
  171. {    if ( PrintDialog->Execute() ) RichEdit1->Print( FFileName );
  172. }
  173. //----------------------------------------------------------------------------
  174. void __fastcall TMainForm::FileExitClick(TObject* /*Sender*/)
  175. {    Close();
  176. }
  177. //----------------------------------------------------------------------------
  178. void __fastcall TMainForm::EditUndoClick(TObject* /*Sender*/)
  179. {    if ( RichEdit1->HandleAllocated() )
  180.         SendMessage(RichEdit1->Handle, EM_UNDO, 0, 0);
  181. }
  182. //----------------------------------------------------------------------------
  183. void __fastcall TMainForm::EditCutClick(TObject* /*Sender*/)
  184. {    RichEdit1->CutToClipboard();
  185. }
  186. //----------------------------------------------------------------------------
  187. void __fastcall TMainForm::EditCopyClick(TObject* /*Sender*/)
  188. {    RichEdit1->CopyToClipboard();
  189. }
  190. //----------------------------------------------------------------------------
  191. void __fastcall TMainForm::EditPasteClick(TObject* /*Sender*/)
  192. {    RichEdit1->PasteFromClipboard();
  193. }
  194. //----------------------------------------------------------------------------
  195. void __fastcall TMainForm::HelpContentsClick(TObject* /*Sender*/)
  196. {
  197.      Application->HelpCommand(HELP_CONTENTS, 0);
  198. }
  199. //----------------------------------------------------------------------------
  200. void __fastcall TMainForm::HelpSearchClick(TObject* /*Sender*/)
  201. {
  202.      Application->HelpCommand(HELP_PARTIALKEY, (long) "");
  203. }
  204. //----------------------------------------------------------------------------
  205. void __fastcall TMainForm::HelpHowToClick(TObject* /*Sender*/)
  206. {
  207.      Application->HelpCommand(HELP_HELPONHELP, 0);
  208. }
  209. //----------------------------------------------------------------------------
  210. void __fastcall TMainForm::HelpAboutClick(TObject* /*Sender*/)
  211. {
  212.      Form2 = new TForm2(Application);
  213.      Form2->ShowModal();
  214.      delete Form2;
  215. }
  216. //----------------------------------------------------------------------------
  217. void __fastcall TMainForm::SelectFont(TObject* /*Sender*/)
  218. {    FontDialog1->Font->Assign( RichEdit1->SelAttributes );
  219.      if ( FontDialog1->Execute() )
  220.         CurrText()->Assign( FontDialog1->Font );
  221.  
  222.      RichEdit1->SetFocus();
  223. }
  224. //----------------------------------------------------------------------------
  225. void __fastcall TMainForm::RulerResize(TObject* /*Sender*/)
  226. {     RulerLine->Width = (int)Ruler->ClientWidth - (RulerLine->Left*2);
  227. }
  228. //----------------------------------------------------------------------------
  229. void __fastcall TMainForm::FormResize(TObject* Sender)
  230. {    SetEditRect();
  231.      SelectionChange(Sender);
  232. }
  233. //----------------------------------------------------------------------------
  234. void __fastcall TMainForm::FormPaint(TObject* /*Sender*/)
  235. {    SetEditRect();
  236. }
  237. //----------------------------------------------------------------------------
  238. void __fastcall TMainForm::BoldButtonClick(TObject* /*Sender*/)
  239. {    if ( !FUpdating )
  240.      {  if ( BoldButton->Down )
  241.            CurrText()->Style = CurrText()->Style << fsBold;
  242.         else
  243.            CurrText()->Style = CurrText()->Style >> fsBold;
  244.      }
  245. }
  246. //----------------------------------------------------------------------------
  247. void __fastcall TMainForm::ItalicButtonClick(TObject* /*Sender*/)
  248. {    if ( !FUpdating )
  249.      {
  250.         if ( ItalicButton->Down )
  251.            CurrText()->Style = CurrText()->Style << fsItalic;
  252.         else
  253.            CurrText()->Style = CurrText()->Style >> fsItalic;
  254.      }
  255. }
  256. //----------------------------------------------------------------------------
  257. void __fastcall TMainForm::UnderlineButtonClick(TObject* /*Sender*/)
  258. {    if ( !FUpdating )
  259.      {
  260.         if ( UnderlineButton->Down )
  261.            CurrText()->Style = CurrText()->Style << fsUnderline;
  262.         else
  263.            CurrText()->Style = CurrText()->Style >> fsUnderline;
  264.      }
  265. }
  266. //----------------------------------------------------------------------------
  267. void __fastcall TMainForm::FontSizeChange(TObject* /*Sender*/)
  268. {
  269.      int fontsize = atoi(FontSize->Text.c_str());
  270.  
  271.      if ((!FUpdating) &&  (fontsize))
  272.      {
  273.          if (fontsize < 1)
  274.          {
  275.              ShowMessage("The number must be between 1 and 1638.");
  276.              FontSize->Text = 1;
  277.          }
  278.          else if (fontsize > 1638)
  279.          {
  280.              ShowMessage("The number must be between 1 and 1638.");
  281.              FontSize->Text = 1638;
  282.          }
  283.          CurrText()->Size = atoi(FontSize->Text.c_str());
  284.      }
  285. }
  286. //----------------------------------------------------------------------------
  287. void __fastcall TMainForm::AlignClick(TObject* Sender)
  288. {    if ( !FUpdating ) {
  289.         TControl *oAliBtn = (TControl*)(Sender);
  290.         RichEdit1->Paragraph->Alignment = (TAlignment)oAliBtn->Tag;
  291.      }
  292. }
  293. //----------------------------------------------------------------------------
  294. void __fastcall TMainForm::FontNameChange(TObject* /*Sender*/)
  295. {
  296.      if ( !FUpdating )
  297.      {
  298.         CurrText()->Name = FontName->Items->Strings[FontName->ItemIndex];
  299.      }
  300. }
  301. //----------------------------------------------------------------------------
  302. void __fastcall TMainForm::BulletsButtonClick(TObject* /*Sender*/)
  303. {    if ( !FUpdating )
  304.         RichEdit1->Paragraph->Numbering = (TNumberingStyle)BulletsButton->Down;
  305. }
  306. //----------------------------------------------------------------------------
  307. void __fastcall TMainForm::FormCloseQuery(TObject* /*Sender*/,
  308.      bool & CanClose)
  309. {    try {
  310.        CheckFileSave();
  311.      }
  312.      catch (...) {
  313.        CanClose = False;
  314.      }
  315. }
  316. //----------------------------------------------------------------------------
  317.  
  318. //***************************
  319. //***Ruler Indent Dragging***
  320. //***************************
  321.  
  322. //----------------------------------------------------------------------------
  323. void __fastcall TMainForm::RulerItemMouseDown(TObject * Sender,
  324.      TMouseButton Button, TShiftState Shift, int X, int Y)
  325. {    TLabel * oTmpLabel = (TLabel *)Sender;
  326.      FDragOfs = oTmpLabel->Width / 2;
  327.      oTmpLabel->Left = oTmpLabel->Left+X-FDragOfs;
  328.      FDragging = True;
  329. }
  330. //----------------------------------------------------------------------------
  331. void __fastcall TMainForm::RulerItemMouseMove(TObject *Sender, TShiftState Shift,
  332.      int X, int /*Y*/)
  333. {    if (FDragging) {
  334.         TLabel * oTmpLabel = (TLabel *)Sender;
  335.         oTmpLabel->Left = oTmpLabel->Left+X-FDragOfs;
  336.      }
  337. }
  338. //----------------------------------------------------------------------------
  339. void __fastcall TMainForm::FirstIndMouseUp(TObject *Sender, TMouseButton
  340.      Button, TShiftState Shift, int X, int Y)
  341. {    FDragging = False;
  342.      RichEdit1->Paragraph->FirstIndent = int((FirstInd->Left+FDragOfs-GutterWid) / RulerAdj);
  343.      LeftIndMouseUp(Sender, Button, Shift, X, Y);
  344. }
  345. //----------------------------------------------------------------------------
  346. void __fastcall TMainForm::LeftIndMouseUp(TObject *Sender, TMouseButton
  347.       /*Button*/, TShiftState /*Shift*/, int /*X*/,    int /*Y*/)
  348. {    FDragging = False;
  349.      RichEdit1->Paragraph->LeftIndent = int((LeftInd->Left+FDragOfs-GutterWid)/
  350.                 RulerAdj)-RichEdit1->Paragraph->FirstIndent;
  351.      SelectionChange(Sender);
  352. }
  353. //----------------------------------------------------------------------------
  354. void __fastcall TMainForm::RightIndMouseUp(TObject *Sender, TMouseButton
  355.       /*Button*/, TShiftState /*Shift*/, int /*X*/,    int /*Y*/)
  356. {    FDragging = False;
  357.      RichEdit1->Paragraph->RightIndent =
  358.          int((Ruler->ClientWidth-RightInd->Left+FDragOfs-2) /
  359.          RulerAdj)-2*GutterWid;
  360.      SelectionChange(Sender);
  361. }
  362. //----------------------------------------------------------------------------
  363. void __fastcall TMainForm::FormActivate(TObject *Sender)
  364. {
  365.      Application->HelpFile = "RICHEDIT.HLP";
  366.      RichEdit1->SetFocus();
  367. }
  368. //---------------------------------------------------------------------
  369.