home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / progs / CB / DATA.Z / ROMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-22  |  13.8 KB  |  350 lines

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