home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Examples / Apps / RichEdit / ROMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  16.3 KB  |  436 lines

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