Unit1.cpp
上传用户:daoqigc
上传日期:2021-04-20
资源大小:2795k
文件大小:54k
源码类别:

RichEdit

开发平台:

Delphi

  1. //---------------------------------------------------------------------------
  2. #include <vclvcl.h>
  3. #include <vclClipbrd.hpp>
  4. #if __BORLANDC__ > 0x0530
  5. #include <vclJpeg.hpp>
  6. #endif
  7. #pragma hdrstop
  8. #include "CtrlImg.hpp"
  9. #include "RVTable.hpp"
  10. #include "RVMisc.hpp"
  11. #include "Unit1.h"
  12. #include "PreviewFrm.h"
  13. #include "RVUndoStr.h"
  14. #include "CPFrm.h"
  15. #include "PropFrm.h"
  16. #include "ListFrm.h"
  17. #include "OptionsFrm.h"
  18. //---------------------------------------------------------------------------
  19. #pragma link "RVEdit"
  20. #pragma link "RichView"
  21. #pragma link "RVScroll"
  22. #pragma link "PtblRV"
  23. #pragma link "RVStyle"
  24. #pragma link "CtrlImg"
  25. #pragma link "RVTable"
  26. #pragma link "RVMisc"
  27. #pragma link "RVUni"
  28. #pragma resource "*.dfm"
  29. TForm1 *Form1;
  30. //---------------------------------------------------------------------------
  31. __fastcall TForm1::TForm1(TComponent* Owner)
  32.     : TForm(Owner)
  33. {
  34. }
  35. //---------------------------------------------------------------------------
  36. // This function is used in mitEditCheckpointClick() and mitEditPropsClick()
  37. // to convert tag to string
  38. AnsiString GetTagStr(int Tag)
  39. {
  40.   if (Form1->RichViewEdit1->Options.Contains(rvoTagsArePChars))
  41.     if (!Tag)
  42.       return "";
  43.     else
  44.       return (char*)Tag;
  45.   else
  46.     return IntToStr(Tag);
  47. }
  48. // This function is used in mitEditCheckpointClick() and mitEditPropsClick()
  49. // to create tags from string TagStr (user input in edit box)
  50. int MakeTag(const AnsiString TagStr)
  51. {
  52.    if (TagStr!="" &&
  53.        Form1->RichViewEdit1->Options.Contains(rvoTagsArePChars))
  54.    {
  55.      char* r = StrNew(TagStr.c_str());
  56.      return (int)r;
  57.    }
  58.    else
  59.      return StrToIntDef(TagStr,0);
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall TForm1::FormCreate(TObject *Sender)
  63. {
  64.    Randomize;
  65.    HTMLSaveOptions << rvsoImageSizes << rvsoUseCheckpointsNames;
  66.    HTMLTitle = "Demo File";
  67.   // We need to register classes in order to load them from rvf files
  68.   TComponentClass Classes[3] = { __classid(TButton), __classid(TEdit), __classid(TOleContainer) };
  69.   RegisterClasses(Classes,2);
  70.   RVStyle1->TextStyles->Items[11]->FontName = GetUnicodeFontName();
  71.    // This demo program is saved in C++Builder 1, where Charset property is not available.
  72.    // So assigning non-default charset here:
  73. #if __BORLANDC__ > 0x520
  74.    RVStyle1->TextStyles->Items[6]->Charset = SYMBOL_CHARSET;
  75.    RVStyle1->ListStyles->Items[0]->Levels->Items[0]->Font->Charset = SYMBOL_CHARSET;
  76. #endif
  77.    // Items can have associated "tags" - integers or strings.
  78.    // Comment next line to use integer tags
  79.    RichViewEdit1->Options << rvoTagsArePChars;
  80.    RichViewEdit1->LoadRVF(ExtractFilePath(Application->ExeName)+"Readme.rvf");
  81.    FillStyleCombo(RVStyle1->ParaStyles, cmbPara);
  82.    FillStyleCombo(RVStyle1->TextStyles, cmbText);
  83.    RichViewEdit1->Format();
  84.    cmbPara->ItemIndex = RichViewEdit1->CurParaStyleNo;
  85.    cmbText->ItemIndex = RichViewEdit1->CurTextStyleNo;
  86.    UpdateUndoMenu();
  87. }
  88. //---------------------------------------------------------------------------
  89. // Returning available Unicode-enabled font
  90. AnsiString TForm1::GetUnicodeFontName()
  91. {
  92.   if (Screen->Fonts->IndexOf("Arial Unicode MS")>=0)
  93.     return "Arial Unicode MS";
  94.   if (Screen->Fonts->IndexOf("Lucida Sans Unicode")>=0)
  95.     return "Lucida Sans Unicode";
  96.   return "Arial";
  97. }
  98. //---------------------------------------------------------------------------
  99. // Filling combobox with standard styles 
  100. void TForm1::FillStyleCombo(TCustomRVInfos* Styles, TComboBox* cmb)
  101. {
  102.   /* The simplest way to fill the combo box with style names is:
  103.     cmb.Items.Assign(Styles);
  104.     But this code will fill the combo box with all styles -
  105.     both standard styles (i.e. real styles) and non-standard styles will be
  106.     added in it.
  107.     So we'll fill in the combo box manually.
  108.     For simplification, we'll add only the first standard styles */
  109.   cmb->Items->BeginUpdate();
  110.   cmb->Items->Clear();
  111.   for (int i = 0; i< Styles->Count; i++)
  112.   {
  113.     if (!((TCustomRVInfo*)(Styles->Items[i]))->Standard)
  114.       break;
  115.     cmb->Items->Add(((TCustomRVInfo*)(Styles->Items[i]))->StyleName);
  116.   }
  117.   cmb->Items->EndUpdate();
  118. }
  119. //---------------------------------------------------------------------------
  120. void TForm1::UpdateUndoMenu()
  121. {
  122.   TRVUndoType UndoType = RichViewEdit1->UndoAction();
  123.   mitUndo->Enabled = UndoType!=rvutNone;
  124.   if (UndoType==rvutCustom)
  125.     mitUndo->Caption = "Undo "+RichViewEdit1->UndoName();
  126.   else
  127.     mitUndo->Caption = "Undo "+RVUndoTypeNamesEn[UndoType];
  128.   UndoType = RichViewEdit1->RedoAction();
  129.   mitRedo->Enabled = UndoType!=rvutNone;
  130.   if (UndoType==rvutCustom)
  131.     mitRedo->Caption = "Redo "+RichViewEdit1->RedoName();
  132.   else
  133.     mitRedo->Caption = "Redo "+RVUndoTypeNamesEn[UndoType];
  134. }
  135. //---------------------------------------------------------------------------
  136. void TForm1::DisplayUnicodeWarning()
  137. {
  138.   bool wasclear = RichViewEdit1->ItemCount==0;
  139.   // This method is called before loading Unicode
  140.   // (when inserting Unicode, editor automatically switches to Unicode style,
  141.   // according to RVStyle1->DefUnicodeStyle, if necessary)
  142.   if (!RVStyle1->TextStyles->Items[RichViewEdit1->CurTextStyleNo]->Unicode)
  143.     Application->MessageBox("Loading/Inserting Unicode data using non-Unicode text style.n"
  144.                            "Text will be converted.n"
  145.                            "Choose 'Unicode' style in combo to use Unicode text style",
  146.                            "Warning", MB_OK | MB_ICONEXCLAMATION);
  147.   if (wasclear)
  148.     RichViewEdit1->Clear();
  149. }
  150. //======================================================================
  151. // Font and paragraph combos
  152. //======================================================================
  153. void __fastcall TForm1::RichViewEdit1CurParaStyleChanged(TObject *Sender)
  154. {
  155.   if (RichViewEdit1->CurParaStyleNo < cmbPara->Items->Count)
  156.     cmbPara->ItemIndex = RichViewEdit1->CurParaStyleNo;
  157.   else
  158.     cmbPara->ItemIndex = -1;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TForm1::RichViewEdit1CurTextStyleChanged(TObject *Sender)
  162. {
  163.   if (RichViewEdit1->CurTextStyleNo < cmbText->Items->Count)
  164.     cmbText->ItemIndex = RichViewEdit1->CurTextStyleNo;
  165.   else
  166.     cmbText->ItemIndex = -1;
  167. }
  168. //---------------------------------------------------------------------------
  169. void __fastcall TForm1::cmbParaClick(TObject *Sender)
  170. {
  171.    RichViewEdit1->ApplyParaStyle(cmbPara->ItemIndex);
  172.    RichViewEdit1->SetFocus();
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TForm1::cmbTextClick(TObject *Sender)
  176. {
  177.    RichViewEdit1->ApplyTextStyle(cmbText->ItemIndex);
  178.    RichViewEdit1->SetFocus();
  179. }
  180. //======================================================================
  181. // Main menu: "File"
  182. //======================================================================
  183. // File|Load...
  184. void __fastcall TForm1::mitLoadClick(TObject *Sender)
  185. {
  186.   OpenDialog1->Title = "Loading & Import";
  187.   OpenDialog1->Filter = "RichView Format Files(*.rvf)|*.rvf|"
  188.                         "RTF Files (*.rtf)|*.rtf|"
  189.                         "Text Files - autodetect (*.txt)|*.txt|"
  190.                         "ANSI Text Files (*.txt)|*.txt|"
  191.                         "Unicode Text Files (*.txt)|*.txt";
  192.   if (OpenDialog1->Execute())
  193.   {
  194.     Screen->Cursor = crHourGlass;
  195.     int CurTextStyleNo = RichViewEdit1->CurTextStyleNo;
  196.     int CurParaStyleNo = RichViewEdit1->CurParaStyleNo;
  197.     bool r;
  198.     CloseOleContainer();
  199.     RichViewEdit1->Clear();
  200.     RichViewEdit1->CurTextStyleNo = CurTextStyleNo;
  201.     RichViewEdit1->CurParaStyleNo = CurParaStyleNo;
  202.     RVStyle1->DefUnicodeStyle = -1;
  203.     switch (OpenDialog1->FilterIndex)
  204.     {
  205.       case 1: // RVF
  206.         r = RichViewEdit1->LoadRVF(OpenDialog1->FileName);
  207.         break;
  208.       case 2: // RTF
  209.         r = RichViewEdit1->LoadRTF(OpenDialog1->FileName);
  210.         break;
  211.       case 3: // Text
  212.         if (RV_TestFileUnicode(OpenDialog1->FileName)==rvutYes)
  213.         {
  214.           DisplayUnicodeWarning();
  215.           r = RichViewEdit1->LoadTextW(OpenDialog1->FileName,CurTextStyleNo,CurParaStyleNo,false);
  216.         }
  217.         else
  218.           r = RichViewEdit1->LoadText(OpenDialog1->FileName,CurTextStyleNo,CurParaStyleNo,false);
  219.         break;
  220.       case 4: // ANSI text
  221.         r = RichViewEdit1->LoadText(OpenDialog1->FileName,CurTextStyleNo,CurParaStyleNo,false);
  222.         break;
  223.       case 5: // Unicode text
  224.         DisplayUnicodeWarning();
  225.         r = RichViewEdit1->LoadTextW(OpenDialog1->FileName,CurTextStyleNo,CurParaStyleNo,false);
  226.         break;
  227.       default:
  228.         r = False;
  229.     }
  230.     Screen->Cursor = crDefault;
  231.     if (!r)
  232.     {
  233.       AnsiString ErrorMessage = "Error during loading";
  234.       if (OpenDialog1->FilterIndex==1)
  235.         ErrorMessage += GetRVFErrors();
  236.       Application->MessageBox(ErrorMessage.c_str(), "Error", 0);
  237.     }
  238.     FillStyleCombo(RVStyle1->ParaStyles, cmbPara);
  239.     FillStyleCombo(RVStyle1->TextStyles, cmbText);
  240.     RichViewEdit1->Format();
  241.     cmbPara->ItemIndex = RichViewEdit1->CurParaStyleNo;
  242.     cmbText->ItemIndex = RichViewEdit1->CurTextStyleNo;
  243.     UpdateUndoMenu();
  244.   }
  245. }
  246. //---------------------------------------------------------------------------
  247. // Event: picture needed while reading from RVF
  248. void __fastcall TForm1::RichViewEdit1RVFPictureNeeded(TCustomRichView *Sender,
  249.     AnsiString Name, int Tag, TGraphic *&gr)
  250. {
  251.   gr = new Graphics::TBitmap;
  252.   gr->LoadFromFile(ExtractFilePath(Application->ExeName)+"default.bmp");
  253. }
  254. //---------------------------------------------------------------------------
  255. // Event: control needed while reading from RVF
  256. void __fastcall TForm1::RichViewEdit1RVFControlNeeded(TCustomRichView *Sender,
  257.     AnsiString Name, int Tag, TControl *&ctrl)
  258. {
  259.   ctrl = new TButton ((TComponent*)NULL);
  260.   ((TButton*)(ctrl))->Caption = "from file";
  261. }
  262. //---------------------------------------------------------------------------
  263. // Event: imagelist needed while reading from RVF
  264. void __fastcall TForm1::RichViewEdit1RVFImageListNeeded(TCustomRichView *Sender,
  265.     int ImageListTag, TCustomImageList *&il)
  266. {
  267.   il = this->il;
  268. }
  269. //---------------------------------------------------------------------------
  270. AnsiString TForm1::GetRVFErrors()
  271. {
  272.   AnsiString Result = "";
  273.   if (RichViewEdit1->RVFWarnings.Contains(rvfwUnknownPicFmt))
  274.     Result += "unknown picture format;";
  275.   if (RichViewEdit1->RVFWarnings.Contains(rvfwUnknownCtrls))
  276.     Result += "unknown control class;";
  277.   if (RichViewEdit1->RVFWarnings.Contains(rvfwConvUnknownStyles))
  278.     Result += "text, paragraph or list style is not present;";
  279.   if (RichViewEdit1->RVFWarnings.Contains(rvfwConvLargeImageIdx))
  280.     Result += "invalid image-list index;";
  281.   if (Result.Length()>0)
  282.     Result = "n("+Result+")";
  283.   return Result;
  284. }
  285. //---------------------------------------------------------------------------
  286. // File|Save...
  287. void __fastcall TForm1::mitSaveClick(TObject *Sender)
  288. {
  289.   SaveDialog1->Title = "Save & Export";
  290.   SaveDialog1->Filter = "RichView Format files(*.rvf)|*.rvf|"
  291.                         "RTF Files (*.rtf)|*.rtf|"
  292.                         "Text (*.txt)|*.txt|"
  293.                         "Unicode Text (*.txt)|*.txt|"
  294.                         "HTML - with CSS (*.htm;*.html)|*.htm;*.html"
  295.                         "HTML - Simplified (*.htm;*.html)|*.htm;*.html|";
  296.   SaveDialog1->DefaultExt = "rvf";
  297.   if (SaveDialog1->Execute())
  298.   {
  299.     Screen->Cursor = crHourGlass;
  300.     bool r;
  301.     switch (SaveDialog1->FilterIndex)
  302.     {
  303.       case 1: // RVF
  304.         r = RichViewEdit1->SaveRVF(SaveDialog1->FileName, false);
  305.         break;
  306.       case 2: // RTF
  307.         r = RichViewEdit1->SaveRTF(SaveDialog1->FileName, false);
  308.         break;
  309.       case 3: // ANSI Text (byte per character)
  310.         r = RichViewEdit1->SaveText(SaveDialog1->FileName, 80);
  311.         break;
  312.       case 4: // Unicode Text (2 bytes per character)
  313.         r = RichViewEdit1->SaveTextW(SaveDialog1->FileName, 80);
  314.         break;
  315.       case 5: // HTML with CSS
  316.         r = RichViewEdit1->SaveHTMLEx(SaveDialog1->FileName, HTMLTitle,"img",
  317.           "","","", HTMLSaveOptions);
  318.         break;
  319.       case 6: // HTML
  320.         r = RichViewEdit1->SaveHTML(SaveDialog1->FileName, HTMLTitle,"img",
  321.           HTMLSaveOptions);
  322.         break;
  323.       default:
  324.         r = false;
  325.     }
  326.     Screen->Cursor = crDefault;
  327.     if (!r)
  328.       Application->MessageBox("Error during saving", "Error", 0);
  329.   }
  330. }
  331. //---------------------------------------------------------------------------
  332. // File | Options
  333. void __fastcall TForm1::OptionsforSavingLoading1Click(TObject *Sender)
  334. {
  335.   TRVFOptions RVFOptions;
  336.   frmOptions->SetOptions(RichViewEdit1->RVFOptions, HTMLSaveOptions, HTMLTitle);
  337.   if (frmOptions->ShowModal()==mrOk)
  338.   {
  339.     frmOptions->GetOptions(RVFOptions, HTMLSaveOptions, HTMLTitle);
  340.     RichViewEdit1->RVFOptions = RVFOptions;
  341.   }
  342. }
  343. //---------------------------------------------------------------------------
  344. // Event: saving controls in HTML
  345. // Note: code below works normally in Internet Explorer
  346. // Netscape 3 and 4 does not support <INPUT> tags outside <FORM></FORM> tags
  347. // (Netscape 6 does)
  348. void __fastcall TForm1::RichViewEdit1SaveComponentToFile(
  349.     TCustomRichView *Sender, AnsiString Path, TPersistent *SaveMe,
  350.     TRVSaveFormat SaveFormat, AnsiString &OutStr)
  351. {
  352.   switch (SaveFormat)
  353.   {
  354.    case rvsfText:
  355.      OutStr = AnsiString("(")+SaveMe->ClassName()+")";
  356.      break;
  357.    case rvsfHTML:
  358.      if (SaveMe->InheritsFrom(__classid(TButton)))
  359.      {
  360.        OutStr = "<INPUT type="button" value=""+((TButton*)SaveMe)->Caption+"" "
  361.                 "onClick="alert('Just a demo')">";
  362.        return;
  363.      }
  364.      if (SaveMe->InheritsFrom(__classid(TEdit)))
  365.      {
  366.        OutStr = "<INPUT type="text" value=""+((TEdit*)SaveMe)->Text+"">";
  367.        return;
  368.      }
  369.      break;
  370.    case rvsfRTF:
  371.      OutStr = AnsiString("\plain\b (")+SaveMe->ClassName()+")";
  372.      break;
  373.   }
  374. }
  375. //---------------------------------------------------------------------------
  376. // Event: saving URLs in HTML and RTF
  377. void __fastcall TForm1::RichViewEdit1WriteHyperlink(TCustomRichView *Sender,
  378. int id, TCustomRVData *RVData, int ItemNo, TRVSaveFormat SaveFormat,
  379. AnsiString &Target, AnsiString &Extras)
  380. {
  381.   Target = GetTagStr(RVData->GetItemTag(ItemNo));
  382. }
  383. //---------------------------------------------------------------------------
  384. // File|Clear
  385. void __fastcall TForm1::mitClearClick(TObject *Sender)
  386. {
  387.    CloseOleContainer();
  388.    RichViewEdit1->Clear();
  389.    RichViewEdit1->Format();
  390.    cmbPara->ItemIndex = RichViewEdit1->CurParaStyleNo;
  391.    cmbText->ItemIndex = RichViewEdit1->CurTextStyleNo;
  392.    UpdateUndoMenu();
  393. }
  394. //---------------------------------------------------------------------------
  395. // File|Print Preview
  396. void __fastcall TForm1::mitPreviewClick(TObject *Sender)
  397. {
  398.   RVPrint1->AssignSource(RichViewEdit1);
  399.   RVPrint1->FormatPages(TRVDisplayOptions());
  400.   if (RVPrint1->PagesCount>0)
  401.   {
  402.     frmPreview->rvpp->RVPrint = RVPrint1;
  403.     frmPreview->Button1Click(NULL); //  Show First Page
  404.     frmPreview->ShowModal();
  405.   }
  406. }
  407. //---------------------------------------------------------------------------
  408. void __fastcall TForm1::mitPrintClick(TObject *Sender)
  409. {
  410. #if __BORLANDC__ > 0x520
  411.   bool PrintIt = psd->Execute();
  412. #else
  413.   bool PrintIt = true;
  414. #endif
  415.   if (PrintIt)
  416.   {
  417.     RVPrint1->AssignSource(RichViewEdit1);
  418.     RVPrint1->FormatPages(TRVDisplayOptions());
  419.     if (RVPrint1->PagesCount>0)
  420.       RVPrint1->Print("RichView Edit Demo",1,false);
  421.   }
  422. }
  423. //---------------------------------------------------------------------------
  424. // File|Exit
  425. void __fastcall TForm1::mitExitClick(TObject *Sender)
  426. {
  427.   Close();
  428. }
  429. //======================================================================
  430. // Main menu: "Insert"
  431. //======================================================================
  432. // Insert|File...
  433. void __fastcall TForm1::mitInsertFileClick(TObject *Sender)
  434. {
  435.   OpenDialog1->Title = "Inserting File";
  436.   OpenDialog1->Filter = "RichView Format Files(*.rvf)|*.rvf|"
  437.                         "RTF Files(*.rtf)|*.rtf|"
  438.                         "Text Files - autodetect (*.txt)|*.txt|"
  439.                         "ANSI Text Files (*.txt)|*.txt|"
  440.                         "Unicode Text Files (*.txt)|*.txt|"
  441.                         "OEM Text Files (*.txt)|*.txt";
  442.   if (OpenDialog1->Execute())
  443.   {
  444.     Screen->Cursor = crHourGlass;
  445.     bool r;
  446.     switch (OpenDialog1->FilterIndex)
  447.     {
  448.       case 1: // RVF
  449.         r = RichViewEdit1->InsertRVFFromFileEd(OpenDialog1->FileName);
  450.         break;
  451.       case 2: // RTF
  452.         r = RichViewEdit1->InsertRTFFromFileEd(OpenDialog1->FileName);
  453.         break;
  454.       case 3: // Text
  455.         if (RV_TestFileUnicode(OpenDialog1->FileName)==rvutYes)
  456.           r = RichViewEdit1->InsertTextFromFileW(OpenDialog1->FileName);
  457.         else
  458.           r = RichViewEdit1->InsertTextFromFile(OpenDialog1->FileName);
  459.         break;
  460.       case 4: // ANSI Text
  461.         r = RichViewEdit1->InsertTextFromFile(OpenDialog1->FileName);
  462.         break;
  463.       case 5: // Unicode Text
  464.         r = RichViewEdit1->InsertTextFromFileW(OpenDialog1->FileName);
  465.         break;
  466.       case 6: // OEM Text
  467.         r = RichViewEdit1->InsertOEMTextFromFile(OpenDialog1->FileName);
  468.         break;
  469.       default:
  470.         r = false;
  471.     }
  472.     Screen->Cursor = crDefault;
  473.     if (!r)
  474.       Application->MessageBox("Error reading file", "Error",
  475.                              MB_OK | MB_ICONSTOP);
  476.   }
  477. }
  478. //---------------------------------------------------------------------------
  479. // Insert|Picture...
  480. void __fastcall TForm1::mitPictureClick(TObject *Sender)
  481. {
  482.   OpenDialog1->Title = "Inserting Image";
  483. #if __BORLANDC__ > 0x0530
  484.   OpenDialog1->Filter = "Graphics(*.bmp;*.wmf;*.emf;*.ico;*.jpg)|*.bmp;*.wmf;*.emf;*.ico;*.jpg|All(*.*)|*.*";
  485. #else
  486.   OpenDialog1->Filter = "Graphics(*.bmp;*.wmf;*.emf;*.ico)|*.bmp;*.wmf;*.emf;*.ico|All(*.*)|*.*";
  487. #endif
  488.   if (OpenDialog1->Execute())
  489.   {
  490.     TGraphic* gr = NULL;
  491.     AnsiString ext = UpperCase(ExtractFileExt(OpenDialog1->FileName));
  492. #if __BORLANDC__ > 0x0530
  493.     if (ext==".JPG" || ext==".JPEG")
  494.       gr = new TJPEGImage;
  495.     else
  496. #endif
  497.     if (ext==".BMP" || ext==".DIB")
  498.       gr = new Graphics::TBitmap;
  499.     else if (ext==".ICO")
  500.       gr = new TIcon;
  501.     else if (ext==".WMF" || ext==".EMF")
  502.       gr = new TMetafile;
  503.     else
  504.       Application->MessageBox(("Format '"+ext+"' is not supported").c_str(), "Error",
  505.                              MB_OK | MB_ICONSTOP);
  506.     if (gr)
  507.     {
  508.       gr->LoadFromFile(OpenDialog1->FileName);
  509.       RichViewEdit1->InsertPicture("",gr,rvvaBaseline);
  510.     }
  511.   }
  512. }
  513. //---------------------------------------------------------------------------
  514. // Event: clicking inserted controls
  515. void __fastcall TForm1::OnControlClick(TObject *Sender)
  516. {
  517.   RichViewEdit1->SelectControl((TControl*)Sender);
  518. }
  519. //---------------------------------------------------------------------------
  520. // Insert|Component|Button
  521. void __fastcall TForm1::mitButtonCompClick(TObject *Sender)
  522. {
  523. const AnsiString Captions[10] =
  524.        {
  525.        "Help","Exit","Cancel","OK","Close","Run","Options...","Minimize",
  526.        "Hide","Show"
  527.        };
  528.   TButton* btn = new TButton((TComponent*)NULL);
  529.   btn->Caption = Captions[random(10)];
  530.   btn->OnClick = OnControlClick;
  531.   RichViewEdit1->InsertControl("",btn,rvvaBaseline);
  532.   if (RichViewEdit1->CurItemStyle==rvsComponent)
  533.     RichViewEdit1->SetCurrentItemExtraIntProperty(rvepResizable, 1, True);
  534. }
  535. //---------------------------------------------------------------------------
  536. // Insert|Component|Edit Box
  537. void __fastcall TForm1::mitEditBoxCompClick(TObject *Sender)
  538. {
  539. const AnsiString Captions[10] =
  540.        {
  541.        "0","Hello","1","$0","2x2=4","enter text here","x<y","to be or not to be?",
  542.        "(empty)","(full)"
  543.        };
  544.   TEdit* edt = new TEdit((TComponent*)NULL);
  545.   edt->Text = Captions[random(10)];
  546.   edt->OnClick = OnControlClick;
  547.   RichViewEdit1->InsertControl("",edt,rvvaBaseline);
  548.   if (RichViewEdit1->CurItemStyle==rvsComponent)
  549.     RichViewEdit1->SetCurrentItemExtraIntProperty(rvepResizable, 1, True);
  550. }
  551. //---------------------------------------------------------------------------
  552. // Insert|Bullet|"XXX"
  553. void __fastcall TForm1::mitInsertBulletClick(TObject *Sender)
  554. {
  555.   RichViewEdit1->InsertBullet(((TMenuItem*)Sender)->Tag, il);    
  556. }
  557. //---------------------------------------------------------------------------
  558. // Insert|Hot Spot|"XXX"
  559. void __fastcall TForm1::mitInsertHotspotClick(TObject *Sender)
  560. {
  561.   RichViewEdit1->InsertHotspot(((TMenuItem*)Sender)->Tag, ((TMenuItem*)Sender)->Tag+2, il);
  562. }
  563. //---------------------------------------------------------------------------
  564. // Insert|Break
  565. void __fastcall TForm1::mitBreakClick(TObject *Sender)
  566. {
  567.    RichViewEdit1->InsertBreak(1, rvbsLine, clNone);    
  568. }
  569. //======================================================================
  570. // Main menu : "Edit"
  571. //======================================================================
  572. // Edit
  573. void __fastcall TForm1::mpdEditClick(TObject *Sender)
  574. {
  575.   mitPasteAsRTF->Enabled      = RichViewEdit1->CanPasteRTF();
  576.   mitPasteAsText->Enabled     = Clipboard()->HasFormat(CF_TEXT);
  577.   mitPasteAsUnicodeText->Enabled = Clipboard()->HasFormat(CF_UNICODETEXT);
  578.   mitPasteAsMetafile->Enabled = Clipboard()->HasFormat(CF_METAFILEPICT);
  579.   mitPasteAsBitmap->Enabled   = Clipboard()->HasFormat(CF_BITMAP);
  580.   mitPasteAsRVF->Enabled      = RichViewEdit1->CanPasteRVF();
  581.   mitPaste->Enabled           = RichViewEdit1->CanPaste();
  582.   mitInsertPageBreak->Enabled = !RichViewEdit1->InplaceEditor;
  583.   mitRemovePageBreak->Enabled =
  584.     !RichViewEdit1->InplaceEditor &&
  585.     RichViewEdit1->PageBreaksBeforeItems[RichViewEdit1->CurItemNo];
  586.   // You can edit properties only for item with caret.
  587.   // We disable this item because otherwise user can think what he will
  588.   // edit properties of all selected items.
  589.   // More smart programs can determine if there is only one item is selected
  590.   // and do not disable this item in this case
  591.   mitEditProps->Enabled       = ! RichViewEdit1->SelectionExists();
  592. }
  593. //---------------------------------------------------------------------------
  594. // Edit|Undo
  595. void __fastcall TForm1::mitUndoClick(TObject *Sender)
  596. {
  597.   RichViewEdit1->Undo();
  598. }
  599. //---------------------------------------------------------------------------
  600. // Edit|Redo
  601. void __fastcall TForm1::mitRedoClick(TObject *Sender)
  602. {
  603.   RichViewEdit1->Redo();
  604. }
  605. //---------------------------------------------------------------------------
  606. // Edit|Cut
  607. void __fastcall TForm1::mitCutClick(TObject *Sender)
  608. {
  609.   RichViewEdit1->CutDef();
  610. }
  611. //---------------------------------------------------------------------------
  612. // Edit|Copy
  613. void __fastcall TForm1::mitCopyClick(TObject *Sender)
  614. {
  615.   RichViewEdit1->CopyDef();
  616. }
  617. //---------------------------------------------------------------------------
  618. // Edit|Paste
  619. void __fastcall TForm1::mitPasteClick(TObject *Sender)
  620. {
  621.   RichViewEdit1->Paste();    
  622. }
  623. //---------------------------------------------------------------------------
  624. // Edit|Paste As|RTF
  625. void __fastcall TForm1::mitPasteAsRTFClick(TObject *Sender)
  626. {
  627.   RichViewEdit1->PasteRTF();
  628. }
  629. //---------------------------------------------------------------------------
  630. // Edit|Paste As|Text
  631. void __fastcall TForm1::mitPasteAsTextClick(TObject *Sender)
  632. {
  633.   RichViewEdit1->PasteText();
  634. }
  635. //---------------------------------------------------------------------------
  636. // Edit|Paste As|Unicode Text
  637. void __fastcall TForm1::mitPasteAsUnicodeTextClick(TObject *Sender)
  638. {
  639.   RichViewEdit1->PasteTextW();
  640. }
  641. //---------------------------------------------------------------------------
  642. // Edit|Paste As|Metafile
  643. void __fastcall TForm1::mitPasteAsMetafileClick(TObject *Sender)
  644. {
  645.   RichViewEdit1->PasteMetafile(false);
  646. }
  647. //---------------------------------------------------------------------------
  648. // Edit|Paste As|Bitmap
  649. void __fastcall TForm1::mitPasteAsBitmapClick(TObject *Sender)
  650. {
  651.   RichViewEdit1->PasteBitmap(false);
  652. }
  653. //---------------------------------------------------------------------------
  654. // Edit|Paste As|RichView Format
  655. void __fastcall TForm1::mitPasteAsRVFClick(TObject *Sender)
  656. {
  657.   RichViewEdit1->PasteRVF();
  658. }
  659. //---------------------------------------------------------------------------
  660. // Edit|Paste As|OLE
  661. void __fastcall TForm1::mitPasteAsOLEClick(TObject *Sender)
  662. {
  663.   TOleContainer* oc = new TOleContainer((TComponent*)NULL);
  664.   if (oc->CanPaste)
  665.   {
  666.     oc->Visible = false;
  667.     oc->BorderStyle = bsNone;
  668.     oc->Parent = RichViewEdit1;
  669.     oc->SizeMode = smAutoSize;
  670.     oc->Paste();
  671.     RichViewEdit1->InsertControl("", oc,rvvaBaseline);
  672.     oc->OnResize = OnOleResize;
  673.     oc->OnActivate = OnOleActivate;
  674.     oc->OnDeactivate = OnOleDeactivate;
  675.     oc->Visible = true;
  676.   }
  677.   else
  678.     delete oc;
  679. }
  680. //---------------------------------------------------------------------------
  681. void TForm1::CloseOleContainer()
  682. {
  683.   if (ActiveOleContainer)
  684.   {
  685.     ActiveOleContainer->Close();
  686.     ActiveOleContainer = NULL;
  687.   }
  688. }
  689. //---------------------------------------------------------------------------
  690. void __fastcall TForm1::OnOleResize(TObject *Sender)
  691. {
  692.   RichViewEdit1->AdjustControlPlacement2((TControl*)Sender);
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TForm1::OnOleActivate(TObject *Sender)
  696. {
  697.   if (ActiveOleContainer!=Sender)
  698.     CloseOleContainer();
  699.   ActiveOleContainer = (TOleContainer*)Sender;
  700.   RichViewEdit1->AdjustControlPlacement2((TControl*)Sender);
  701. }
  702. //---------------------------------------------------------------------------
  703. void __fastcall TForm1::OnOleDeactivate(TObject *Sender)
  704. {
  705.   RichViewEdit1->AdjustControlPlacement2((TControl*)Sender);
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TForm1::RichViewEdit1Click(TObject *Sender)
  709. {
  710.   CloseOleContainer();
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
  714. {
  715.   CloseOleContainer();
  716. }
  717. //---------------------------------------------------------------------------
  718. void __fastcall TForm1::RichViewEdit1ControlAction(TCustomRichView *Sender,
  719. TRVControlAction ControlAction, int ItemNo, TControl *&ctrl)
  720. {
  721.   if (ControlAction==rvcaAfterRVFLoad)
  722.   {
  723.     if (ctrl->InheritsFrom(__classid(TOleContainer)))
  724.     {
  725.       ((TOleContainer*)ctrl)->OnResize = OnOleResize;
  726.       ((TOleContainer*)ctrl)->OnActivate = OnOleActivate;
  727.       ((TOleContainer*)ctrl)->OnDeactivate = OnOleDeactivate;
  728.     }
  729.     else if (ctrl->InheritsFrom(__classid(TButton)))
  730.       ((TButton*)ctrl)->OnClick = OnControlClick;
  731.     else if (ctrl->InheritsFrom(__classid(TEdit)))
  732.       ((TEdit*)ctrl)->OnClick = OnControlClick;
  733.   }
  734.   if (ctrl!=ActiveOleContainer)
  735.     return;
  736.   if (ControlAction==rvcaMoveToUndoList ||
  737.       ControlAction==rvcaDestroy ||
  738.       ControlAction==rvcaBeforeRVFSave)
  739.     CloseOleContainer();
  740. }
  741. //---------------------------------------------------------------------------
  742. // Edit|Delete
  743. void __fastcall TForm1::mitDeleteClick(TObject *Sender)
  744. {
  745.   // Shortcut to this item is Ctrl+Del
  746.   // If you make it Del, you will be unable to use del key in editor
  747.   RichViewEdit1->DeleteSelection();
  748. }
  749. //---------------------------------------------------------------------------
  750. void __fastcall TForm1::mitSelectAllClick(TObject *Sender)
  751. {
  752.   // warning: SelectAll moves caret to the end of the text
  753.   RichViewEdit1->SelectAll();
  754.   RichViewEdit1->SetFocus();
  755.   RichViewEdit1->Invalidate();
  756. }
  757. //---------------------------------------------------------------------------
  758. // Another clipboard-related action
  759. void __fastcall TForm1::RichViewEdit1Select(TObject *Sender)
  760. {
  761.   mitCopy->Enabled   = RichViewEdit1->SelectionExists();
  762.   mitCut->Enabled    = mitCopy->Enabled;
  763.   mitDelete->Enabled = mitCopy->Enabled;
  764. }
  765. //---------------------------------------------------------------------------
  766. // Edit| Insert Page Break
  767. void __fastcall TForm1::mitInsertPageBreakClick(TObject *Sender)
  768. {
  769.   RichViewEdit1->InsertPageBreak();    
  770. }
  771. //---------------------------------------------------------------------------
  772. // Edit| Remove Page Break
  773. void __fastcall TForm1::mitRemovePageBreakClick(TObject *Sender)
  774. {
  775.   RichViewEdit1->RemoveCurrentPageBreak();    
  776. }
  777. //---------------------------------------------------------------------------
  778. // Edit|Checkpoint...
  779. void __fastcall TForm1::mitEditCheckpointClick(TObject *Sender)
  780. {
  781.   int CpNo, Tag;
  782.   AnsiString Name;
  783.   bool RaiseEvent;
  784.   TCheckpointData CheckpointData = RichViewEdit1->GetCurrentCheckpoint();
  785.   if (CheckpointData)
  786.   {
  787.     RichViewEdit1->GetCheckpointInfo(CheckpointData,Tag,Name,RaiseEvent);
  788.     CpNo = RichViewEdit1->GetCheckpointNo(CheckpointData);
  789.     frmCP->lblStatus->Caption = "Editing checkpoint #"+IntToStr(CpNo);
  790.     frmCP->txtName->Text = Name;
  791.     frmCP->txtTag->Text = GetTagStr(Tag);
  792.     frmCP->btnOk->Caption = "OK";
  793.     frmCP->btnDelete->Enabled = true;
  794.   }
  795.   else
  796.   {
  797.     frmCP->lblStatus->Caption = "Checkpoint does not exist";
  798.     frmCP->txtName->Text = "";
  799.     frmCP->txtTag->Text = GetTagStr(0);
  800.     frmCP->btnOk->Caption = "Add";
  801.     frmCP->btnDelete->Enabled = false;
  802.   }
  803.   switch (frmCP->ShowModal())
  804.   {
  805.     case mrOk: // add new checkpoint or modify existed one
  806.       RichViewEdit1->SetCurrentCheckpointInfo(MakeTag(frmCP->txtTag->Text),
  807.                                              frmCP->txtName->Text,false);
  808.       break;
  809.     case mrYes: // delete checkpoint
  810.       RichViewEdit1->RemoveCurrentCheckpoint();
  811.       break;
  812.   }
  813. }
  814. //---------------------------------------------------------------------------
  815. // Edit|Search... 
  816. void __fastcall TForm1::mitSearchClick(TObject *Sender)
  817. {
  818.   FindDialog1->Execute();
  819. }
  820. //---------------------------------------------------------------------------
  821. void __fastcall TForm1::FindDialog1Find(TObject *Sender)
  822. {
  823.   TRVESearchOptions Options = GetRVESearchOptions(FindDialog1->Options);
  824.   if (!RichViewEdit1->SearchText(FindDialog1->FindText,
  825.                            Options))
  826.    Application->MessageBox("Can't find", "Search complete", MB_OK | MB_ICONEXCLAMATION);
  827. }
  828. //---------------------------------------------------------------------------
  829. // Edit|Select Current Word
  830. void __fastcall TForm1::mitSelectCurrentWordClick(TObject *Sender)
  831. {
  832.   RichViewEdit1->SelectCurrentWord();
  833.   // now you can do something with current word:
  834.   // translate or spell check, for example...
  835. }
  836. //---------------------------------------------------------------------------
  837. #if __BORLANDC__ == 0x0540
  838. // workaround for C++Builder 4 problem
  839. namespace Rvuni
  840. {
  841.  extern PACKAGE AnsiString __fastcall RVU_UnicodeToAnsi(int CodePage, const AnsiString
  842. s);
  843. }
  844. #endif
  845. // Edit|Current Item Properties...
  846. void __fastcall TForm1::mitEditPropsClick(TObject *Sender)
  847. {
  848.   AnsiString s;
  849.   int Tag, ImageIndex;
  850.   TCustomImageList * ImageList;
  851.   TRVVAlign VAlign;
  852.   TControl* ctrl;
  853.   TGraphic* gr;
  854.   TColor BreakColor;
  855.   TRVBreakStyle BreakStyle;
  856.   unsigned char BreakWidth;
  857.   frmProp->PageControl1->Visible   = true;
  858.   frmProp->tsBullet->TabVisible    = false;
  859.   frmProp->tsHotspot->TabVisible   = false;
  860.   frmProp->tsPicture->TabVisible   = false;
  861.   frmProp->tsText->TabVisible      = false;
  862.   frmProp->tsComponent->TabVisible = false;
  863.   frmProp->tsBreak->TabVisible     = false;
  864.   frmProp->txtName->Enabled        = true;
  865.   switch (RichViewEdit1->CurItemStyle)
  866.   {
  867.     case rvsBullet:
  868.     {
  869.       RichViewEdit1->GetCurrentBulletInfo(s, ImageIndex, ImageList, Tag);
  870.       frmProp->tsBullet->TabVisible = true;
  871.       frmProp->rgBullet->ItemIndex  = ImageIndex;
  872.       frmProp->txtName->Text        = s;
  873.       frmProp->txtTag->Text         = GetTagStr(Tag);
  874.       break;
  875.     }
  876.     case rvsHotspot:
  877.     {
  878.       // you can use GetCurrentBulletInfo or GetCurrentHotspotInfo
  879.       // to receive info about hotspot in caret position->
  880.       // in this demo we need not HotImageIndex, because here
  881.       // HotImageIndex = ImageIndex+2
  882.       // and so we can use GetCurrentBulletInfo
  883.       RichViewEdit1->GetCurrentBulletInfo(s, ImageIndex, ImageList, Tag);
  884.       frmProp->tsHotspot->TabVisible = true;
  885.       frmProp->rgHotspot->ItemIndex  = ImageIndex-3;
  886.       frmProp->txtName->Text         = s;
  887.       frmProp->txtTag->Text          = GetTagStr(Tag);
  888.       break;
  889.     }
  890.     case rvsPicture:
  891.     case rvsHotPicture:
  892.     {
  893.       RichViewEdit1->GetCurrentPictureInfo(s, gr, VAlign, Tag);
  894.       frmProp->tsPicture->TabVisible    = true;
  895.       frmProp->Image1->Picture->Graphic = gr;
  896.       frmProp->txtName->Text            = s;
  897.       frmProp->txtTag->Text             = GetTagStr(Tag);
  898.       frmProp->rgPicVAlign->ItemIndex   = VAlign;
  899.       break;
  900.     }
  901.     case rvsComponent:
  902.     {
  903.       RichViewEdit1->GetCurrentControlInfo(s, ctrl, VAlign, Tag);
  904.       frmProp->tsComponent->TabVisible = true;
  905.       frmProp->txtWidth->Text          = IntToStr(ctrl->Width);
  906.       frmProp->txtHeight->Text         = IntToStr(ctrl->Height);
  907.       frmProp->txtName->Text           = s;
  908.       frmProp->lblComponent->Caption   = ctrl->ClassName();
  909.       frmProp->txtTag->Text            = GetTagStr(Tag);
  910.       frmProp->rgCtrlVAlign->ItemIndex = Integer(VAlign);
  911.       break;
  912.     }
  913.     case rvsBreak:
  914.     {
  915.       RichViewEdit1->GetCurrentBreakInfo(BreakWidth, BreakStyle, BreakColor, Tag);
  916.       frmProp->tsBreak->TabVisible = true;
  917.       frmProp->txtBreakWidth->Text = IntToStr(BreakWidth);
  918.       switch (BreakColor)
  919.       {
  920.       case clNone:
  921.         frmProp->rgBreakColor->ItemIndex = 0;
  922.         break;
  923.       case clRed:
  924.         frmProp->rgBreakColor->ItemIndex = 1;
  925.         break;
  926.       case clGreen:
  927.         frmProp->rgBreakColor->ItemIndex = 2;
  928.         break;
  929.       case clBlue:
  930.         frmProp->rgBreakColor->ItemIndex = 3;
  931.         break;
  932.       }
  933.       frmProp->txtName->Text = "(not available for breaks)";
  934.       frmProp->txtName->Enabled = false;
  935.       frmProp->txtTag->Text = GetTagStr(Tag);
  936.       break;
  937.     }
  938.     case rvsTable:
  939.     {
  940.       frmProp->txtName->Text = RichViewEdit1->GetCurrentItemText();
  941.       frmProp->txtTag->Text = GetTagStr(RichViewEdit1->GetCurrentTag());
  942.       frmProp->PageControl1->Visible = false;
  943.       break;
  944.     }
  945.     default: // text
  946.     {
  947.       frmProp->tsText->TabVisible = true;
  948.       frmProp->txtName->Text = "(not available for text)";
  949.       frmProp->txtName->Enabled = false;
  950.       frmProp->lblText->Caption = RichViewEdit1->GetCurrentItemTextA();
  951.       frmProp->txtTag->Text = GetTagStr(RichViewEdit1->GetCurrentTag());
  952.     }
  953.   }
  954.   if (frmProp->ShowModal()==mrOk)
  955.   {
  956.     switch (RichViewEdit1->CurItemStyle)
  957.     {
  958.       case rvsBullet:
  959.       {
  960.         RichViewEdit1->SetCurrentBulletInfo(
  961.           frmProp->txtName->Text,
  962.           frmProp->rgBullet->ItemIndex,
  963.           NULL,
  964.           MakeTag(frmProp->txtTag->Text));
  965.         break;
  966.       }
  967.       case rvsHotspot:
  968.       {
  969.         RichViewEdit1->SetCurrentHotspotInfo(
  970.           frmProp->txtName->Text,
  971.           frmProp->rgHotspot->ItemIndex+3,
  972.           frmProp->rgHotspot->ItemIndex+3+2,
  973.           NULL,
  974.           MakeTag(frmProp->txtTag->Text));
  975.         break;
  976.       }
  977.       case rvsPicture:
  978.       case rvsHotPicture:
  979.       {
  980.         // first we need to create a copy of image ...
  981.         // RV_CreateGraphics is a global function pointer, defined in RVItem unit,
  982.         // it creates graphic object by its class
  983.         gr = RV_CreateGraphics(frmProp->Image1->Picture->Graphic->ClassType());
  984.         gr->Assign(frmProp->Image1->Picture->Graphic);
  985.         RichViewEdit1->SetCurrentPictureInfo(
  986.           frmProp->txtName->Text,
  987.           gr,
  988.           TRVVAlign(frmProp->rgPicVAlign->ItemIndex),
  989.           MakeTag(frmProp->txtTag->Text));
  990.         break;
  991.       }
  992.       case rvsComponent:
  993.       {
  994.         // we wish these setting to be undone as one action,
  995.         // so we use BeginUndoGroup, SetUndoGroupMode(true), settings, SetUndoGroupMode(false)
  996.         RichViewEdit1->BeginUndoGroup(rvutModifyItem);
  997.         // you can use BeginUndoCustomGroup instead of BeginUndoGroup
  998.         // example:
  999.         // RichViewEdit1->BeginUndoCustomGroup('modifying control');
  1000.         // In this case undo type will be rvutCustom
  1001.         // (look at TForm1->UpdateUndoMenu in this file)
  1002.         RichViewEdit1->SetUndoGroupMode(true);
  1003.         RichViewEdit1->SetCurrentControlInfo(
  1004.           frmProp->txtName->Text,
  1005.           TRVVAlign(frmProp->rgCtrlVAlign->ItemIndex),
  1006.           MakeTag(frmProp->txtTag->Text));
  1007.         RichViewEdit1->ResizeCurrentControl(
  1008.           StrToIntDef(frmProp->txtWidth->Text, ctrl->Width),
  1009.           StrToIntDef(frmProp->txtHeight->Text, ctrl->Height));
  1010.         RichViewEdit1->SetUndoGroupMode(false);
  1011.         break;
  1012.       }
  1013.       case rvsBreak:
  1014.       {
  1015.         switch (frmProp->rgBreakColor->ItemIndex)
  1016.         {
  1017.           case -1:
  1018.           case 0:
  1019.             BreakColor = clNone;
  1020.             break;
  1021.           case 1:
  1022.             BreakColor = clRed;
  1023.             break;
  1024.           case 2:
  1025.             BreakColor = clGreen;
  1026.             break;
  1027.           case 3:
  1028.             BreakColor = clBlue;
  1029.             break;
  1030.         }
  1031.         BreakWidth = (unsigned char)StrToIntDef(frmProp->txtBreakWidth->Text,1);
  1032.         RichViewEdit1->SetCurrentBreakInfo(BreakWidth,BreakStyle,BreakColor,
  1033.                                            MakeTag(frmProp->txtTag->Text));
  1034.         break;
  1035.       }
  1036.       case rvsTable:
  1037.       {
  1038.         RichViewEdit1->BeginUndoGroup(rvutModifyItem);
  1039.         RichViewEdit1->SetUndoGroupMode(true);
  1040.         RichViewEdit1->SetCurrentItemText(frmProp->txtName->Text);
  1041.         RichViewEdit1->SetCurrentTag(MakeTag(frmProp->txtTag->Text));
  1042.         RichViewEdit1->SetUndoGroupMode(false);
  1043.         break;
  1044.       }
  1045.       default:
  1046.       {
  1047.         RichViewEdit1->SetCurrentTag(MakeTag(frmProp->txtTag->Text));
  1048.       }
  1049.     }
  1050.   }
  1051. }
  1052. //======================================================================
  1053. // Main menu : "Misc"
  1054. //======================================================================
  1055. // Misc | Go to checkpoint ...
  1056. void __fastcall TForm1::mitCheckpointListClick(TObject *Sender)
  1057. {
  1058.   frmList->lst->Items->Clear();
  1059.   TCheckpointData CheckpointData = RichViewEdit1->GetFirstCheckpoint();
  1060.   while (CheckpointData)
  1061.   {
  1062.     int X,Y,Tag;
  1063.     AnsiString Name, s;
  1064.     bool RaiseEvent;
  1065.     RichViewEdit1->GetCheckpointInfo(CheckpointData,Tag,Name,RaiseEvent);
  1066.     RichViewEdit1->GetCheckpointXY(CheckpointData,X,Y);
  1067.     s = "(X:"+IntToStr(X)+",Y:"+IntToStr(Y)+") Name:'"+Name+"' Tag:'"+GetTagStr(Tag)+"'";
  1068.     frmList->lst->Items->Add(s);
  1069.     CheckpointData = RichViewEdit1->GetNextCheckpoint(CheckpointData);
  1070.   }
  1071.   if (frmList->ShowModal()==mrOk)
  1072.     RichViewEdit1->ScrollTo(RichViewEdit1->GetCheckpointY(frmList->lst->ItemIndex));
  1073. }
  1074. //---------------------------------------------------------------------------
  1075. // Misc | Background submenu popups
  1076. void __fastcall TForm1::mpdBackgroundClick(TObject *Sender)
  1077. {
  1078.   // Displaying RichViewEdit1->BackgroundStyle as checkmark in submenu...
  1079.   mitBackNoBitmap->Checked         = RichViewEdit1->BackgroundStyle==bsNoBitmap;
  1080.   mitBackStretched->Checked        = RichViewEdit1->BackgroundStyle==bsStretched;
  1081.   mitBackTiledandScrolled->Checked = RichViewEdit1->BackgroundStyle==bsTiledAndScrolled;
  1082.   mitBackTiled->Checked            = RichViewEdit1->BackgroundStyle==bsTiled;
  1083.   mitBackCentered->Checked         = RichViewEdit1->BackgroundStyle==bsCentered;
  1084.   mitBackTopLeft->Checked          = RichViewEdit1->BackgroundStyle==bsTopLeft;
  1085.   mitBackTopRight->Checked         = RichViewEdit1->BackgroundStyle==bsTopRight;
  1086.   mitBackBottomLeft->Checked       = RichViewEdit1->BackgroundStyle==bsBottomLeft;
  1087.   mitBackBottomRight->Checked      = RichViewEdit1->BackgroundStyle==bsBottomRight;
  1088. }
  1089. //---------------------------------------------------------------------------
  1090. // Misc | Background options
  1091. void __fastcall TForm1::mitBackClick(TObject *Sender)
  1092. {
  1093.   RichViewEdit1->BackgroundStyle = (TBackgroundStyle)(((TMenuItem*)Sender)->Tag);
  1094. }
  1095. //---------------------------------------------------------------------------
  1096. // Misc | Read only
  1097. void __fastcall TForm1::mitReadOnlyClick(TObject *Sender)
  1098. {
  1099.   RichViewEdit1->ReadOnly = ! RichViewEdit1->ReadOnly;
  1100.   mitReadOnly->Checked = RichViewEdit1->ReadOnly;
  1101. }
  1102. //======================================================================
  1103. // On Popup
  1104. void __fastcall TForm1::PopupMenu1Popup(TObject *Sender)
  1105. {
  1106.   mitEditProp1->Enabled = !RichViewEdit1->SelectionExists();
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. // OnChange event handler.
  1110. void __fastcall TForm1::RichViewEdit1Change(TObject *Sender)
  1111. {
  1112.   UpdateUndoMenu();
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. // You should manually update palette info when user changes color mode
  1116. // without restarting Windows
  1117. void __fastcall TForm1::WMDisplayChange(TMessage &Message)
  1118. {
  1119.   RichViewEdit1->UpdatePaletteInfo();
  1120.   RVPrint1->UpdatePaletteInfo();
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. // Event: OnJump (when user clicks hypertext item with pressed Ctrl key
  1124. void __fastcall TForm1::RichViewEdit1Jump(TObject *Sender, int id)
  1125. {
  1126.   // NOTE: OnJump is called after the caret is repositioned to clicked item
  1127.   // But warning: a clicked event is not necessarily an active item
  1128.   // (when clicking on left part of picture or left part of first character in text item,
  1129.   // caret moves before item and previous item becomes active!)
  1130.   TCustomRVFormattedData* RVData;
  1131.   int ItemNo;
  1132.   RichViewEdit1->GetJumpPointLocation(id, RVData, ItemNo);
  1133.   AnsiString s =  GetTagStr(RVData->GetItemTag(ItemNo));
  1134.   Application->MessageBox(Format("Tag of clicked hyperlink is '%s'",
  1135.     ARRAYOFCONST((s.c_str()))).c_str(), "Hypertext Click", MB_OK | MB_ICONINFORMATION);
  1136. }
  1137. //---------------------------------------------------------------------------
  1138. // Event: OnRVMouseMove (when user moves mouse above hypertext item with pressed Ctrl key
  1139. void __fastcall TForm1::RichViewEdit1RVMouseMove(TObject *Sender, int id)
  1140. {
  1141.   TCustomRVFormattedData* RVData;
  1142.   int ItemNo;
  1143.   if (id==-1)
  1144.     StatusBar1->SimpleText = "";
  1145.   else
  1146.   {
  1147.     RichViewEdit1->GetJumpPointLocation(id, RVData, ItemNo);
  1148.     AnsiString s =  GetTagStr(RVData->GetItemTag(ItemNo));
  1149.     StatusBar1->SimpleText = Format("Tag of clicked hyperlink is '%s'", ARRAYOFCONST((s.c_str())));
  1150.   }
  1151. }
  1152. //======================================================================
  1153. // Main menu : "Lists"
  1154. //======================================================================
  1155. // Lists | Apply
  1156. void __fastcall TForm1::mitApplyListsClick(TObject *Sender)
  1157. {
  1158.   /* See more demos about list styles in DemosCBuilderAssortedListStyles */
  1159.   if (RVStyle1->ListStyles->Count==0 ||
  1160.       RVStyle1->ListStyles->Items[0]->Levels->Count==0)
  1161.   {
  1162.     Application->MessageBox("Default list style is not defined", "", 0);
  1163.     return;
  1164.   }
  1165.   RichViewEdit1->ApplyListStyle(0, 0, 1, false, false);
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. // Lists | Remove
  1169. void __fastcall TForm1::mitRemoveListsClick(TObject *Sender)
  1170. {
  1171.   RichViewEdit1->RemoveLists(false);
  1172. }
  1173. //======================================================================
  1174. // Main menu : "Table"
  1175. //======================================================================
  1176. // Table | Insert Table Example 1
  1177. void __fastcall TForm1::mitInserttable1Click(TObject *Sender)
  1178. {
  1179.   TRVTableItemInfo* table = new TRVTableItemInfo(4,3, RichViewEdit1->RVData);
  1180.   table->BorderStyle      = rvtbRaisedColor;
  1181.   table->CellBorderStyle  = rvtbLoweredColor;
  1182.   table->BorderLightColor = (TColor)0x00FAF1C9;
  1183.   table->BorderColor      = (TColor)0x00A98E10;
  1184.   table->CellBorderLightColor = (TColor)0x00FAF1C9;
  1185.   table->CellBorderColor  = (TColor)0x00A98E10;
  1186.   table->Color            = (TColor)0x00EAC724;
  1187.   table->BorderWidth      = 5;
  1188.   table->CellBorderWidth  = 2;
  1189.   table->CellPadding      = 5;
  1190.   table->CellVSpacing     = 1;
  1191.   table->CellHSpacing     = 1;
  1192.   table->BorderVSpacing   = 1;
  1193.   table->BorderHSpacing   = 1;
  1194.   for (int r = 0; r<table->Rows->Count; r++)
  1195.     for (int c = 0; c<table->Rows->Items[r]->Count; c++)
  1196.       table->Cells[r][c]->BestWidth = 100;
  1197.   table->MergeCells(0,0,3,1, false);
  1198.   table->MergeCells(1,0,1,3, false);
  1199.   TRVTableCellData* Cell = table->Cells[0][0];
  1200.   Cell->Color = clInfoBk;
  1201.   Cell->Clear();
  1202.   Cell->AddBulletEx("",0,il,2);
  1203.   Cell->AddNL(" Example 1 ",1,-1);
  1204.   Cell->AddBulletEx("",0,il,-1);
  1205.   Cell->AddNL("All cells have 100 pixels width, width of table itself is calculated basing on width of cells.",0,0);
  1206.   if (RichViewEdit1->InsertItem("", table))
  1207.   {
  1208.   }
  1209. }
  1210. //---------------------------------------------------------------------------
  1211. // Table | Insert Table Example 2
  1212. void __fastcall TForm1::mitInsertTable2Click(TObject *Sender)
  1213. {
  1214.   TRVTableItemInfo* table = new TRVTableItemInfo(10,6, RichViewEdit1->RVData);
  1215.   table->Color     = clWhite;
  1216.   table->BestWidth = -90;
  1217.   table->BorderStyle      = rvtbRaisedColor;
  1218.   table->CellBorderStyle  = rvtbLoweredColor;
  1219.   table->BorderLightColor = clWhite;
  1220.   table->BorderColor      = clBlack;
  1221.   table->CellBorderLightColor = clWhite;
  1222.   table->CellBorderColor  = clBlack;
  1223.   table->BorderWidth     = 2;
  1224.   table->BorderVSpacing  = 0;
  1225.   table->BorderHSpacing  = 0;
  1226.   table->CellBorderWidth = 2;
  1227.   table->CellPadding     = 3;
  1228.   table->CellVSpacing    = 0;
  1229.   table->CellHSpacing    = 0;
  1230.   table->Cells[0][0]->BestWidth = -16;
  1231.   table->Cells[0][1]->BestWidth = -16;
  1232.   table->Cells[0][2]->BestWidth = -16;
  1233.   table->Cells[0][3]->BestWidth = -16;
  1234.   table->Cells[0][4]->BestWidth = -16;
  1235.   table->Cells[0][5]->BestWidth = -16;
  1236.   table->MergeCells(2,0,2,8, false);
  1237.   TRVTableCellData* Cell = table->Cells[2][0];
  1238.   Cell->Clear();
  1239.   Cell->AddNL("Another example.",0,0);
  1240.   TButton* btn = new TButton((TComponent*)NULL);
  1241.   btn->Caption = "With button inside";
  1242.   btn->Width   = 150;
  1243.   btn->OnClick = OnControlClick;
  1244.   Cell->AddControlEx("",btn,2,rvvaBaseline);
  1245.   Cell->SetItemExtraIntProperty(Cell->ItemCount-1, rvepResizable, 1);
  1246.   Cell->AddNL("Width of table = 90% of document width. Widths of cells = 16%",0,0);
  1247.   if (RichViewEdit1->InsertItem("", table))
  1248.   {
  1249.   }
  1250. }
  1251. //---------------------------------------------------------------------------
  1252. // Table | Insert Table Example 3
  1253. void __fastcall TForm1::mitInsertTable3Click(TObject *Sender)
  1254. {
  1255.   TRVTableItemInfo* table = new TRVTableItemInfo(5,6, RichViewEdit1->RVData);
  1256.   table->Color            = (TColor)0x00A5CCE7;
  1257.   table->BorderStyle      = rvtbColor;
  1258.   table->CellBorderStyle  = rvtbColor;
  1259.   table->BorderColor      = (TColor)0x002E1234;
  1260.   table->CellBorderColor  = (TColor)0x002E1234;
  1261.   table->BorderWidth     = 2;
  1262.   table->BorderVSpacing  = 2;
  1263.   table->BorderHSpacing  = 2;
  1264.   table->CellBorderWidth = 1;
  1265.   table->CellPadding     = 3;
  1266.   table->CellVSpacing    = 0;
  1267.   table->CellHSpacing    = 0;
  1268.   table->BestWidth       = 400;
  1269.   int r,c;
  1270.   for (c = 0; c<table->Rows->Items[0]->Count; c++)
  1271.    table->Cells[0][c]->Color = (TColor)0x00A5E1F8;
  1272.   for (r = 1; r<table->Rows->Count; r++)
  1273.    table->Cells[r][0]->Color = (TColor)0x00A5E1F8;
  1274.   for (r = 1; r<table->Rows->Count; r++)
  1275.     for (c = 1; c<table->Rows->Items[r]->Count; c++)
  1276.     {
  1277.       table->Cells[r][c]->Color = (TColor)0x007AB4DA;
  1278.       if (c>1)
  1279.         table->Cells[r][c]->VisibleBorders->Left = false;
  1280.       if (c<table->Rows->Items[r]->Count-1)
  1281.         table->Cells[r][c]->VisibleBorders->Right = false;
  1282.     }
  1283.   RichViewEdit1->InsertText("Third example: width of table = 400 pixels, widths of cells - unspecified.", false);
  1284.   if (RichViewEdit1->InsertItem("", table))
  1285.   {
  1286.   }
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. // Table | Insert Table Example 4
  1290. void __fastcall TForm1::mitInsertTable4Click(TObject *Sender)
  1291. {
  1292.   TRVTableItemInfo* table = new TRVTableItemInfo(3,3, RichViewEdit1->RVData);
  1293.   table->Color           = clNone;
  1294.   table->BorderStyle     = rvtbColor;
  1295.   table->CellBorderStyle = rvtbColor;
  1296.   table->BorderWidth     = 1;
  1297.   table->BorderVSpacing  = 2;
  1298.   table->BorderHSpacing  = 2;
  1299.   table->CellBorderWidth = 1;
  1300.   table->CellPadding     = 3;
  1301.   table->CellVSpacing    = 5;
  1302.   table->CellHSpacing    = 5;
  1303.   table->VRuleWidth      = 1;
  1304.   table->HRuleWidth      = 1;
  1305.   for (int r=0; r<table->Rows->Count; r++)
  1306.     for (int c=0; c<table->Rows->Items[r]->Count; c++)
  1307.     {
  1308.       table->Cells[r][c]->BestWidth = 40;
  1309.       table->Cells[r][c]->Clear();
  1310.       table->Cells[r][c]->AddFmt("%d,%d",ARRAYOFCONST((r,c)),0,0);
  1311.       table->Cells[r][c]->Color = clWhite;
  1312.     }
  1313.   RichViewEdit1->InsertText("Transparent table with rules", false);
  1314.   if (RichViewEdit1->InsertItem("", table))
  1315.   {
  1316.   }
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. // Table submenu popups
  1320. void __fastcall TForm1::mpdTableClick(TObject *Sender)
  1321. {
  1322.   TCustomRichViewEdit* rve;
  1323.   TCustomRVItemInfo* item;
  1324.   if (!RichViewEdit1->GetCurrentItemEx(__classid(TRVTableItemInfo), rve, item))
  1325.   {
  1326.     mitRowsAbove->Enabled         = false;
  1327.     mitRowsBelow->Enabled         = false;
  1328.     mitColsLeft->Enabled          = false;
  1329.     mitColsRight->Enabled         = false;
  1330.     mitDelRows->Enabled           = false;
  1331.     mitDelColumns->Enabled        = false;
  1332.     mitMergeCells->Enabled        = false;
  1333.     mitUmRows->Enabled            = false;
  1334.     mitUmCols->Enabled            = false;
  1335.     mitUmRowsandCols->Enabled     = false;
  1336.     mitSplitVertically->Enabled   = false;
  1337.     mitSplitHorizontally->Enabled = false;
  1338.     return;
  1339.   }
  1340.   TRVTableItemInfo* table = (TRVTableItemInfo*)item;
  1341.   int r,c,cs,rs;
  1342.   bool Selected = table->GetNormalizedSelectionBounds(true,r,c,cs,rs);
  1343.   mitRowsAbove->Enabled         = Selected;
  1344.   mitRowsBelow->Enabled         = Selected;
  1345.   mitColsLeft->Enabled          = Selected;
  1346.   mitColsRight->Enabled         = Selected;
  1347.   mitDelRows->Enabled           = Selected;
  1348.   mitDelColumns->Enabled        = Selected;
  1349.   mitMergeCells->Enabled        = table->CanMergeSelectedCells(true);
  1350.   bool SelectionRectangular = Selected &&
  1351.                           (table->CanMergeSelectedCells(true) ||
  1352.                            table->GetEditedCell(r,c));
  1353.   mitSplitVertically->Enabled   = SelectionRectangular;
  1354.   mitSplitHorizontally->Enabled = SelectionRectangular;
  1355.   mitUmRows->Enabled            = SelectionRectangular;
  1356.   mitUmCols->Enabled            = SelectionRectangular;
  1357.   mitUmRowsandCols->Enabled     = SelectionRectangular;
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. // Table | All other commands
  1361. void __fastcall TForm1::mitCellOperation(TObject *Sender)
  1362. {
  1363.   TCustomRVItemInfo* item;
  1364.   TCustomRichViewEdit* rve;
  1365.   int r,c,cs,rs;
  1366.   if (!RichViewEdit1->CanChange() ||
  1367.       !RichViewEdit1->GetCurrentItemEx(__classid(TRVTableItemInfo), rve, item))
  1368.     return;
  1369.   TRVTableItemInfo* table = (TRVTableItemInfo*)item;
  1370.   int ItemNo = rve->GetItemNo(table);
  1371.   int Data;
  1372.   rve->BeginItemModify(ItemNo, Data);
  1373.   switch (((TMenuItem*)Sender)->Tag)
  1374.   {
  1375.     case 1:
  1376.       table->InsertRowsAbove(1);
  1377.       break;
  1378.     case 2:
  1379.       table->InsertRowsBelow(1);
  1380.       break;
  1381.     case 3:
  1382.       table->InsertColsLeft(1);
  1383.       break;
  1384.     case 4:
  1385.       table->InsertColsRight(1);
  1386.       break;
  1387.     case 5:
  1388.       {
  1389.         table->GetNormalizedSelectionBounds(true,r,c,cs,rs);
  1390.         if (rs==table->Rows->Count)
  1391.         {
  1392.           // deleting whole table
  1393.           rve->SetSelectionBounds(ItemNo,0,ItemNo,1);
  1394.           rve->DeleteSelection();
  1395.           return;
  1396.         }
  1397.         rve->BeginUndoGroup(rvutModifyItem);
  1398.         rve->SetUndoGroupMode(true);
  1399.         table->DeleteSelectedRows();
  1400.         // it's possible all-NULL rows/cols appear after deleting
  1401.         table->DeleteEmptyRows();
  1402.         table->DeleteEmptyCols();
  1403.         rve->SetUndoGroupMode(false);
  1404.         break;
  1405.       }
  1406.     case 6:
  1407.       {
  1408.         table->GetNormalizedSelectionBounds(true,r,c,cs,rs);
  1409.         if (cs==table->Rows->Items[0]->Count)
  1410.         {
  1411.           // deleting whole table
  1412.           rve->SetSelectionBounds(ItemNo,0,ItemNo,1);
  1413.           rve->DeleteSelection();
  1414.           return;
  1415.         }
  1416.         rve->BeginUndoGroup(rvutModifyItem);
  1417.         rve->SetUndoGroupMode(true);
  1418.         table->DeleteSelectedCols();
  1419.         // it's possible all-NULL rows/cols appear after deleting
  1420.         table->DeleteEmptyRows();
  1421.         table->DeleteEmptyCols();
  1422.         rve->SetUndoGroupMode(false);
  1423.         break;
  1424.       }
  1425.     case 7:
  1426.       {
  1427.         // 3 methods: MergeSelectedCells, DeleteEmptyRows, DeleteEmptyCols
  1428.         // must be undone as one action.
  1429.         // So using BeginUndoGroup - SetUndoGroupMode(true) - ... - SetUndoGroupMode(false)
  1430.         rve->BeginUndoGroup(rvutModifyItem);
  1431.         rve->SetUndoGroupMode(true);
  1432.         table->MergeSelectedCells(true);
  1433.         table->DeleteEmptyRows();
  1434.         table->DeleteEmptyCols();
  1435.         rve->SetUndoGroupMode(false);
  1436.         // table.MergeSelectedCells(False) will not allow to create empty columns
  1437.         // or rows
  1438.         break;
  1439.       }
  1440.     case 8:
  1441.       table->UnmergeSelectedCells(true, false);
  1442.       break;
  1443.     case 9:
  1444.       table->UnmergeSelectedCells(false, true);
  1445.       break;
  1446.     case 10:
  1447.       table->UnmergeSelectedCells(true, true);
  1448.       break;
  1449.     case 11:
  1450.       {
  1451.         AnsiString s = "2";
  1452.         if (InputQuery("Split Vertically","Columns (in each selected cell):",s))
  1453.           table->SplitSelectedCellsVertically(StrToIntDef(s,0));
  1454.         break;
  1455.       }
  1456.     case 12:
  1457.       {
  1458.         AnsiString s = "2";
  1459.         if (InputQuery("Split Horizontally","Rows (in each selected cell):",s))
  1460.           table->SplitSelectedCellsHorizontally(StrToIntDef(s,0));
  1461.         break;
  1462.       }
  1463.   }
  1464.   rve->EndItemModify(ItemNo, Data);
  1465.   rve->Change();
  1466. }
  1467. //---------------------------------------------------------------------------