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