home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 April / Chip_1997-04_cd.bin / prezent / cb / data.z / ROMAIN.CPP < prev    next >
C/C++ Source or Header  |  1997-01-16  |  15KB  |  371 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++ Builder
  3. //Copyright (c) 1987 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.         if (ExtractFileExt(SaveDialog->FileName).IsEmpty())
  165.             SaveDialog->FileName = ChangeFileExt(SaveDialog->FileName, ".rtf");
  166.         RichEdit1->Lines->SaveToFile(SaveDialog->FileName);
  167.         SetFileName(SaveDialog->FileName);
  168.         RichEdit1->Modified = False;
  169.      }
  170. }
  171. //----------------------------------------------------------------------------
  172. void __fastcall TMainForm::FilePrintClick(TObject* /*Sender*/)
  173. {    if ( PrintDialog->Execute() ) RichEdit1->Print( FFileName );
  174. }
  175. //----------------------------------------------------------------------------
  176. void __fastcall TMainForm::FileExitClick(TObject* /*Sender*/)
  177. {    Close();
  178. }
  179. //----------------------------------------------------------------------------
  180. void __fastcall TMainForm::EditUndoClick(TObject* /*Sender*/)
  181. {    if ( RichEdit1->HandleAllocated() )
  182.         SendMessage(RichEdit1->Handle, EM_UNDO, 0, 0);
  183. }
  184. //----------------------------------------------------------------------------
  185. void __fastcall TMainForm::EditCutClick(TObject* /*Sender*/)
  186. {    RichEdit1->CutToClipboard();
  187. }
  188. //----------------------------------------------------------------------------
  189. void __fastcall TMainForm::EditCopyClick(TObject* /*Sender*/)
  190. {    RichEdit1->CopyToClipboard();
  191. }
  192. //----------------------------------------------------------------------------
  193. void __fastcall TMainForm::EditPasteClick(TObject* /*Sender*/)
  194. {    RichEdit1->PasteFromClipboard();
  195. }
  196. //----------------------------------------------------------------------------
  197. void __fastcall TMainForm::HelpContentsClick(TObject* /*Sender*/)
  198. {
  199.      Application->HelpCommand(HELP_CONTENTS, 0);
  200. }
  201. //----------------------------------------------------------------------------
  202. void __fastcall TMainForm::HelpSearchClick(TObject* /*Sender*/)
  203. {
  204.      Application->HelpCommand(HELP_PARTIALKEY, (long) "");
  205. }
  206. //----------------------------------------------------------------------------
  207. void __fastcall TMainForm::HelpHowToClick(TObject* /*Sender*/)
  208. {
  209.      Application->HelpCommand(HELP_HELPONHELP, 0);
  210. }
  211. //----------------------------------------------------------------------------
  212. void __fastcall TMainForm::HelpAboutClick(TObject* /*Sender*/)
  213. {
  214.      Form2 = new TForm2(Application);
  215.      Form2->ShowModal();
  216.      delete Form2;
  217. }
  218. //----------------------------------------------------------------------------
  219. void __fastcall TMainForm::SelectFont(TObject* /*Sender*/)
  220. {    FontDialog1->Font->Assign( RichEdit1->SelAttributes );
  221.      if ( FontDialog1->Execute() )
  222.         CurrText()->Assign( FontDialog1->Font );
  223.  
  224.      RichEdit1->SetFocus();
  225. }
  226. //----------------------------------------------------------------------------
  227. void __fastcall TMainForm::RulerResize(TObject* /*Sender*/)
  228. {     RulerLine->Width = (int)Ruler->ClientWidth - (RulerLine->Left*2);
  229. }
  230. //----------------------------------------------------------------------------
  231. void __fastcall TMainForm::FormResize(TObject* Sender)
  232. {    SetEditRect();
  233.      SelectionChange(Sender);
  234. }
  235. //----------------------------------------------------------------------------
  236. void __fastcall TMainForm::FormPaint(TObject* /*Sender*/)
  237. {    SetEditRect();
  238. }
  239. //----------------------------------------------------------------------------
  240. void __fastcall TMainForm::BoldButtonClick(TObject* /*Sender*/)
  241. {    if ( !FUpdating )
  242.      {  if ( BoldButton->Down )
  243.            CurrText()->Style = CurrText()->Style << fsBold;
  244.         else
  245.            CurrText()->Style = CurrText()->Style >> fsBold;
  246.      }
  247. }
  248. //----------------------------------------------------------------------------
  249. void __fastcall TMainForm::ItalicButtonClick(TObject* /*Sender*/)
  250. {    if ( !FUpdating )
  251.      {
  252.         if ( ItalicButton->Down )
  253.            CurrText()->Style = CurrText()->Style << fsItalic;
  254.         else
  255.            CurrText()->Style = CurrText()->Style >> fsItalic;
  256.      }
  257. }
  258. //----------------------------------------------------------------------------
  259. void __fastcall TMainForm::UnderlineButtonClick(TObject* /*Sender*/)
  260. {    if ( !FUpdating )
  261.      {
  262.         if ( UnderlineButton->Down )
  263.            CurrText()->Style = CurrText()->Style << fsUnderline;
  264.         else
  265.            CurrText()->Style = CurrText()->Style >> fsUnderline;
  266.      }
  267. }
  268. //----------------------------------------------------------------------------
  269. void __fastcall TMainForm::FontSizeChange(TObject* /*Sender*/)
  270. {
  271.      int fontsize = atoi(FontSize->Text.c_str());
  272.  
  273.      if ((!FUpdating) &&  (fontsize))
  274.      {
  275.          if (fontsize < 1)
  276.          {
  277.              ShowMessage("The number must be between 1 and 1638.");
  278.              FontSize->Text = 1;
  279.          }
  280.          else if (fontsize > 1638)
  281.          {
  282.              ShowMessage("The number must be between 1 and 1638.");
  283.              FontSize->Text = 1638;
  284.          }
  285.          CurrText()->Size = atoi(FontSize->Text.c_str());
  286.      }
  287. }
  288. //----------------------------------------------------------------------------
  289. void __fastcall TMainForm::AlignClick(TObject* Sender)
  290. {    if ( !FUpdating ) {
  291.         TControl *oAliBtn = (TControl*)(Sender);
  292.         RichEdit1->Paragraph->Alignment = (TAlignment)oAliBtn->Tag;
  293.      }
  294. }
  295. //----------------------------------------------------------------------------
  296. void __fastcall TMainForm::FontNameChange(TObject* /*Sender*/)
  297. {
  298.      if ( !FUpdating )
  299.      {
  300.         CurrText()->Name = FontName->Items->Strings[FontName->ItemIndex];
  301.      }
  302. }
  303. //----------------------------------------------------------------------------
  304. void __fastcall TMainForm::BulletsButtonClick(TObject* /*Sender*/)
  305. {    if ( !FUpdating )
  306.         RichEdit1->Paragraph->Numbering = (TNumberingStyle)BulletsButton->Down;
  307. }
  308. //----------------------------------------------------------------------------
  309. void __fastcall TMainForm::FormCloseQuery(TObject* /*Sender*/,
  310.      bool & CanClose)
  311. {    try {
  312.        CheckFileSave();
  313.      }
  314.      catch (...) {
  315.        CanClose = False;
  316.      }
  317. }
  318. //----------------------------------------------------------------------------
  319.  
  320. //***************************
  321. //***Ruler Indent Dragging***
  322. //***************************
  323.  
  324. //----------------------------------------------------------------------------
  325. void __fastcall TMainForm::RulerItemMouseDown(TObject * Sender,
  326.      TMouseButton Button, TShiftState Shift, int X, int Y)
  327. {    TLabel * oTmpLabel = (TLabel *)Sender;
  328.      FDragOfs = oTmpLabel->Width / 2;
  329.      oTmpLabel->Left = oTmpLabel->Left+X-FDragOfs;
  330.      FDragging = True;
  331. }
  332. //----------------------------------------------------------------------------
  333. void __fastcall TMainForm::RulerItemMouseMove(TObject *Sender, TShiftState Shift,
  334.      int X, int /*Y*/)
  335. {    if (FDragging) {
  336.         TLabel * oTmpLabel = (TLabel *)Sender;
  337.         oTmpLabel->Left = oTmpLabel->Left+X-FDragOfs;
  338.      }
  339. }
  340. //----------------------------------------------------------------------------
  341. void __fastcall TMainForm::FirstIndMouseUp(TObject *Sender, TMouseButton
  342.      Button, TShiftState Shift, int X, int Y)
  343. {    FDragging = False;
  344.      RichEdit1->Paragraph->FirstIndent = int((FirstInd->Left+FDragOfs-GutterWid) / RulerAdj);
  345.      LeftIndMouseUp(Sender, Button, Shift, X, Y);
  346. }
  347. //----------------------------------------------------------------------------
  348. void __fastcall TMainForm::LeftIndMouseUp(TObject *Sender, TMouseButton
  349.       /*Button*/, TShiftState /*Shift*/, int /*X*/,    int /*Y*/)
  350. {    FDragging = False;
  351.      RichEdit1->Paragraph->LeftIndent = int((LeftInd->Left+FDragOfs-GutterWid)/
  352.                 RulerAdj)-RichEdit1->Paragraph->FirstIndent;
  353.      SelectionChange(Sender);
  354. }
  355. //----------------------------------------------------------------------------
  356. void __fastcall TMainForm::RightIndMouseUp(TObject *Sender, TMouseButton
  357.       /*Button*/, TShiftState /*Shift*/, int /*X*/,    int /*Y*/)
  358. {    FDragging = False;
  359.      RichEdit1->Paragraph->RightIndent =
  360.          int((Ruler->ClientWidth-RightInd->Left+FDragOfs-2) /
  361.          RulerAdj)-2*GutterWid;
  362.      SelectionChange(Sender);
  363. }
  364. //----------------------------------------------------------------------------
  365. void __fastcall TMainForm::FormActivate(TObject *Sender)
  366. {
  367.      Application->HelpFile = "RICHEDIT.HLP";
  368.      RichEdit1->SetFocus();
  369. }
  370. //---------------------------------------------------------------------
  371.