summaryrefslogtreecommitdiff
path: root/docview/components/richtext/testedit/frarichtextedit.pas
blob: 903c96aec21c23bae9e102ce470711e246de11be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
unit frarichtextedit;

{$mode objfpc}{$H+}

interface

uses
  fpg_base,
  fpg_tab,
  fpg_button,
  fpg_panel,
  fpg_main,
  fpg_memo,
  fpg_form,
  fpg_dialogs,
  fpg_stdimages,
  RichTextView,
  fpg_imagelist,
  fpg_imgfmt_bmp,
  fpg_imgfmt_png,
  fpg_imgfmt_jpg,
  Classes, SysUtils;

Type

  { TRichTextEditFrame }

  TRichTextEditFrame = class(TfpgFrame)
  private
    FImageNames : TStringList;
    FImageList : tfpgImageList;
    procedure CheckTags;
    procedure EncloseSelection(Const StartTag, EndTag: String);
    function GetImageNames: TStrings;
    function GetRichText: String;
    procedure InsertColor(Const Background : Boolean);
    procedure InsertFont;
    procedure InsertImage;
    procedure InsertLink;
    procedure InsertMargin;
    procedure DoPageChange(Sender: TObject; NewActiveSheet: TfpgTabSheet);
    procedure OnToolButton(Sender: TObject);
    procedure SetImageNames(AValue: TStrings);
    procedure SetRichText(AValue: String);
  protected
    {@VFD_HEAD_BEGIN: MainForm}
    PCedit: TfpgPageControl;
    TSedit: TfpgTabSheet;
    BBar: TfpgBevel;
    BBold: TfpgButton;
    BItalic: TfpgButton;
    Bunderline: TfpgButton;
    BHead: TfpgBevel;
    BH1: TfpgButton;
    BH2: TfpgButton;
    BH3: TfpgButton;
    BAligns: TfpgBevel;
    BAleft: TfpgButton;
    BACenter: TfpgButton;
    BAJustified: TfpgButton;
    BAright: TfpgButton;
    BAUnaligned: TfpgButton;
    BANowrap: TfpgButton;
    BevMargin: TfpgBevel;
    BMargin: TfpgButton;
    BFont: TfpgButton;
    BColor: TfpgButton;
    BBGColor: TfpgButton;
    BSpaceImage: TfpgBevel;
    BImage: TfpgButton;
    BLink: TfpgButton;
    BCheck: TfpgButton;
    MText: TfpgMemo;
    TSPreview: TfpgTabSheet;
    RTVPreview: TRichTextView;
    {@VFD_HEAD_END: MainForm}
    procedure AfterCreate; override;
  Public
    Property ImageNames : TStrings Read GetImageNames Write SetImageNames;
    Property RichText : String Read GetRichText Write SetRichText;
  end;

Procedure RegisterRichTextImages(ADir : String);
Procedure RegisterStdRichTextImages;

implementation

procedure TRichTextEditFrame.DoPageChange(Sender: TObject; NewActiveSheet: TfpgTabSheet);

Var
  F : String;

begin
  If NewActiveSheet=TSPreview then
    begin
    RTVPreview.Clear;
    F:=MText.Lines.Text;
    RTVPreview.AddText(Pchar(F));
    end;
end;

Const
  TBBold         = 1;
  TBItalic       = 2;
  TBUnderLine    = 3;
  TBH1           = 4;
  TBH2           = 5;
  TBH3           = 6;
  TBAlignLeft    = 7;
  TBAlignCenter  = 8;
  TBAlignRight   = 9;
  TBAlignNone    = 10;
  TBAlignJustify = 11;
  TBMargin       = 12;
  TBFont         = 13;
  TBColor        = 14;
  TBBGColor      = 15;
  TBImage        = 16;
  TBLink         = 17;
  TBNowrap       = 18;
  TBCheck        = 19;

  BSize          = 30;

  // Image names
  BIBold         = 'richtextedit.bold';
  BIItalic       = 'richtextedit.italic';
  BIunderline    = 'richtextedit.underline';
  BIAlignLeft    = 'richtextedit.left';
  BIAlignCenter  = 'richtextedit.center';
  BIAlignRight   = 'richtextedit.right';
//  BIAlignNone    = 10;
  BIAlignJustify = 'richtextedit.justify';
  BIMargin       = 'richtextedit.margin';
  BIFont         = 'richtextedit.font';
  BIColor        = 'richtextedit.color';
  BIBGColor      = 'richtextedit.backgroundcolor';
  BIImage        = 'richtextedit.image';
  BILink         = 'richtextedit.link';
  BINowrap       = 'richtextedit.nowrap';
  BICheck        = 'richtextedit.check';

  BFNBold         = 'bold';
  BFNItalic       = 'italic';
  BFNunderline    = 'underlined';
  BFNAlignLeft    = 'left';
  BFNAlignCenter  = 'center';
  BFNAlignRight   = 'right';
//  BFNAlignNone    = 10;
  BFNAlignJustify = 'justify';
  BFNMargin       = 'margin';
  BFNFont         = 'font';
  BFNColor        = 'color';
  BFNBGColor      = 'color_background';
  BFNImage        = 'image';
  BFNLink         = 'hyperlink';
  BFNNoWrap       = 'nowrap';
  BFNCheck        = 'check';

procedure CheckSelection(Const S, TextName : String);

Var
  I,P,L,TS,TC : Integer;
  T : TStrings;
  TT,TN : String;

begin
  T:=TStringList.Create;
  try
    I:=0;
    P:=0;
    L:=Length(S);
    While (P=0) and (I<L) do
      begin
      Inc(I);
      If (S[i]='<') then
        if (I=L) then
          P:=I
        else
          begin
          Inc(I);
          if (S[i]<>'<') then
             begin
             TS:=I;
             While (P=0) and (S[i]<>'>') do
               if (I=L) then
                 P:=I
               else
                 Inc(I);
             if (P=0) then
               begin
               TN:=LowerCase(Copy(S,TS,I-TS));
               TC:=Pos(' ',TN);
               if TC<>0 then
                 TN:=Copy(TN,1,TC-1);
               if (TN<>'') then
                 begin
                 if (TN[1]<>'/') then
                   begin
                   if Pos('/'+TN+'/','/align/rightmargin/leftmargin/image/')=0 then
                     T.Add(TN)
                   end
                 else
                   begin
                   Delete(TN,1,1);
                   TC:=T.Count-1;
                   if (TC<0) then
                     P:=TS
                   else
                     if (T[TC]<>TN) then
                       P:=TS
                     else
                       T.Delete(TC);
                    end;
                 end;
               end;
             end;
          end;
      end;
    if (P<>0) then
      Raise Exception.CreateFmt('The %s contains a not-opened closing tag at position %d: %s',[TextName,P,TN])
    else if (T.Count>0) then
      Raise Exception.CreateFmt('The %s contains a not-closed tag: %s',[TextName,T[T.Count-1]])
  finally
    T.Free;
  end;
end;

procedure TRichTextEditFrame.EncloseSelection(Const StartTag, EndTag : String);


Var
  S: String;

begin
  S:=MText.SelectionText;
  if (EndTag<>'') then
    CheckSelection(S,'selection');
  If (StartTag<>'') then
    S:='<'+StartTag+'>'+S;
  if (EndTag<>'') then
    S:=S+'</'+EndTag+'>';
  MText.SelectionText:=S;
//  MText.SelectionText:=S;
end;

function TRichTextEditFrame.GetImageNames: TStrings;
begin
  Result:=FImageNames;
end;

function TRichTextEditFrame.GetRichText: String;
begin
  Result:=MText.Lines.Text;
end;

procedure TRichTextEditFrame.InsertMargin;

Var
  S: tfpgstring;

begin
  S:='5';
  if fpgInputQuery('Insert margin','Enter the margin to be used:',S) then
    begin
    if StrToIntDef(S,-1)=-1 then
      ShowMessage('Not a numerical value. The margin must be a number','Error')
    else
      EncloseSelection('leftmargin '+s,'');
    end;
end;

procedure TRichTextEditFrame.InsertLink;

Var
  S: tfpgstring;

begin
  S:='';
  if fpgInputQuery('Insert link','Enter the link target text:',S) then
    EncloseSelection('link "'+s+'"','link');
end;

procedure TRichTextEditFrame.CheckTags;

begin
  CheckSelection(MText.Lines.Text,'text');
  ShowMessage('All tags are correctly balanced.','Check ok');
end;


procedure TRichTextEditFrame.InsertFont;

Var
  S : String;

begin
  if SelectFontDialog(S) then
    EncloseSelection('font "'+S+'"','font')
end;

procedure TRichTextEditFrame.InsertColor(Const Background : Boolean);

Var
  t : TRGBTriple;
  s : string;
begin
  With TfpgColorSelectDialog.Create(nil) do
    try
      SelectedColor:=clBlack;
      if ShowModal = mrOK then
        begin
        t:=fpgColorToRGBTriple(SelectedColor);
        S:='color';
        if Background then
          S:='back'+s;
        EncloseSelection(s+' #'+format('%.2x%.2x%.2x',[t.red,t.green,t.blue]),s);
        end;
    finally
      Free;
    end;
end;

procedure TRichTextEditFrame.InsertImage;

Var
  FN,E : String;
  I : integer;

begin
  FN:=SelectFileDialog(sfdOpen,'Supported image files|*.png;*.jpg;*.bmp','');
  if (FN<>'') then
    begin
    E:=LowerCase(ExtractFIleExt(FN));
    i:=FImageList.Count;
    if e='.png' then
      FImageList.AddImage(loadimage_png(FN),i)
    else if e='.bmp' then
      FImageList.AddImage(loadimage_bmp(FN),i)
    else if e='.jpg' then
      FImageList.AddImage(loadimage_jpg(FN),i);
    EncloseSelection('image '+intToStr(i),'');
    FImageNames.Add(IntTostr(i)+'='+FN);
    end;
end;

procedure TRichTextEditFrame.OnToolButton(Sender: TObject);

Var
  T : Ptruint;
begin
  T:=(Sender as TComponent).Tag;
  Case T of
    TBBold :
      EncloseSelection('b','b');
    TBItalic :
      EncloseSelection('i','i');
    TBUnderLine:
      EncloseSelection('u','u');
    TBH1:
      EncloseSelection('h1','h1');
    TBH2:
      EncloseSelection('h2','h2');
    TBH3:
      EncloseSelection('h3','h3');
    TBAlignLeft:
      EncloseSelection('align left','');
    TBAlignCenter:
      EncloseSelection('align center','');
    TBAlignRight:
      EncloseSelection('align right','');
    TBAlignNone:
      EncloseSelection('unalign','');
    TBNoWrap:
      EncloseSelection('nowrap','nowrap');
    TBAlignJustify:
      ; // Not functional yet
    TBMargin:
      InsertMargin;
    TBFont:
      InsertFont;
    TBColor,
    TBBGColor:
      InsertColor(TBBGColor=T);
    tbLink:
      InsertLink;
    TBCheck:
      CheckTags;
    TBImage:
      InsertImage;
  end;
end;

procedure TRichTextEditFrame.SetImageNames(AValue: TStrings);
begin
  if FImageNames=AValue then Exit;
  FImageNames.Assign(AValue);
end;

procedure TRichTextEditFrame.SetRichText(AValue: String);
begin
  MText.Lines.Text:=AValue;
end;

procedure TRichTextEditFrame.AfterCreate;

  Function TBSpace(Var ALeft : Integer; AName : String) : TfpgBevel;

  begin
    Result := TfpgBevel.Create(BBar);
    with Result do
      begin
      Name := AName;
      SetPosition(ALeft, 2, 10, 24);
      ALeft:=ALeft+10;
      Align := alLeft;
      Hint := '';
      Shape := bsSpacer;
      end;

  end;

  Function TBButton(Var ALeft,ATab : Integer; ATag : Integer; Const AName,AImage,AText : String) : tfpgButton;

  begin
    Result := TfpgButton.Create(BBar);
    with Result do
      begin
      Name := AName;
      SetPosition(ALeft, 2, BSize, BSize);
      ALeft:=ALeft+BSize;
      Align := alLeft;
      Text := AText;
      ImageName:=AImage;
      Tag:=ATag;
      FontDesc := '#Label1';
      Hint := '';
      ImageMargin := -1;
      Embedded:=True;
      Flat:=True;
      TabOrder := ATab;
      ATab:=ATab+1;
      OnClick:=@OnToolButton;
      end;
  end;

var
  I, J, L, T: integer;
  img: tfpgimage;
  S: string;


begin
  {%region 'Auto-generated GUI code' }

  {@VFD_BODY_BEGIN: MainForm}
  Name := 'MainForm';
  SetPosition(496, 295, 739, 502);
  WindowTitle := 'MainForm';
  Hint := '';

  PCedit := TfpgPageControl.Create(self);
  with PCedit do
  begin
    Name := 'PCedit';
    SetPosition(0, 0, 739, 502);
    Align := alClient;
    Hint := '';
    TabOrder := 1;
    OnChange:=@DoPageChange;
  end;

  TSedit := TfpgTabSheet.Create(PCedit);
  with TSedit do
  begin
    Name := 'TSedit';
    SetPosition(3, 24, 733, 475);
    Anchors := [anLeft,anRight,anTop,anBottom];
    Text := 'Edit text';
  end;

  BBar := TfpgBevel.Create(TSedit);
  with BBar do
  begin
    Name := 'BBar';
    SetPosition(0, 0, 733, BSize+4);
    Align := alTop;
    Hint := '';
    Shape := bsBottomLine;
    Shape := bsBottomLine;
  end;


  MText := TfpgMemo.Create(TSedit);
  with MText do
  begin
    Name := 'MText';
    SetPosition(0, 28, 733, 447);
    Align := alClient;
    FontDesc := '#Edit1';
    Hint := '';
    TabOrder := 2;
  end;

  TSPreview := TfpgTabSheet.Create(PCedit);
  with TSPreview do
  begin
    Name := 'TSPreview';
    SetPosition(3, 24, 733, 475);
    Anchors := [anLeft,anRight,anTop,anBottom];
    Text := 'Preview';
  end;

  RTVPreview := TRichTextView.Create(TSPreview);
  with RTVPreview do
  begin
    Name := 'RTVPreview';
    SetPosition(0, 0, 735, 478);
    Anchors := [anLeft,anRight,anTop,anBottom];
  end;

  {@VFD_BODY_END: MainForm}
  {%endregion}
  // Create toolbar
  L:=2;
  T:=0;
  BBold:=TBButton(L,T,TBBold,'BBold',BIBold,'');
  BItalic:=TBButton(L,T,TBItalic,'BItalic',BIItalic,'');
  Bunderline:=TBButton(L,T,TBunderline,'BUnderline',BIUnderline,'');

  BHead := TBSpace(L,'BHead');

  BH1 := TBButton(L,T,TBH1,'BH1','','1');
  BH2 := TBButton(L,T,TBH2,'BH2','','2');
  BH3 := TBButton(L,T,TBH3,'BH3','','3');

  BAligns := TBSpace(L,'BAligns');

  BAleft := TBButton(L,T,TBAlignLeft,'BALeft',BIAlignLeft,'');
  BACenter := TBButton(L,T,TBAlignCenter,'BACenter',BIAlignCenter,'');
  BAJustified := TBButton(L,T,TBAlignJustify,'BAJustified',BIAlignJustify,'');
  BARight := TBButton(L,T,TBAlignRight,'BARight',BIAlignRight,'');
  BAUnaligned := TBButton(L,T,TBAlignNone,'BAUnalign',BIAlignLeft,'');

  BANowrap := TBButton(L,T,TBNowrap,'BNowrap',BINoWrap,'');

  BevMargin := TBSpace(L,'BevMargin');
  BMargin := TBButton(L,T,TBMargin,'BMargin',BIMargin,'');

  BFont := TBButton(L,T,TBFont,'BFont',BIFont,'');
  BColor := TBButton(L,T,TBColor,'BColor',BIColor,'');
  BBGColor := TBButton(L,T,TBBGColor,'BBGColor',BIBGColor,'');

  BSpaceImage := TBSpace(L,'BSpaceImage');

  BImage := TBButton(L,T,TBImage,'BImage',BIImage,'');
  BLink := TBButton(L,T,TBLink,'BLink',BILink,'');
  BCheck := TBButton(L,T,TBCheck,'BCheck',BICheck,'');

  FImageList:=TfpgImageList.Create;
  FImageNames:=TStringList.Create;
  RTVPreview.Images:=FImageList;
  RTVPreview.RichTextSettings.Heading1Font := fpgGetFont('Arial-18:bold');
  RTVPreview.RichTextSettings.Heading2Font := fpgGetFont('Arial-14:bold');
  RTVPreview.RichTextSettings.Heading3Font := fpgGetFont('Arial-12:bold');
  RTVPreview.RichTextSettings.NormalFont := fpgGetFont(FPG_DEFAULT_FONT_DESC);
  RTVPreview.RichTextSettings.FixedFont := fpgGetFont('Courier New-10:antialiased=true');

end;

Procedure RegisterStdRichTextImages;

  Procedure LoadImage(const iname : string; ImageLoc : Pointer; ImageSize : Integer);

  begin
    if (fpgImages.GetImage(iname)<>Nil) then
      fpgImages.DeleteImage(iname,true);
    fpgImages.AddMaskedBMP(iname,ImageLoc,ImageSize,0,0)
  end;


{$i img_richedit.inc}

begin
  LoadImage(BIBold,@img_richedit_Bold,sizeof(img_richedit_Bold));
  LoadImage(BIItalic,@img_richedit_italic,sizeof(img_richedit_italic));
  LoadImage(BIunderline,@img_richedit_underlined,sizeof(img_richedit_underlined));
  LoadImage(BIAlignLeft,@img_richedit_left,sizeof(img_richedit_left));
  LoadImage(BIAlignCenter,@img_richedit_center,sizeof(img_richedit_center));
  LoadImage(BIAlignRight,@img_richedit_right,sizeof(img_richedit_right));
//  LoadImage(BIAlignCenter,@img_richedit_center,sizeof(img_richedit_center));
  LoadImage(BIAlignJustify,@img_richedit_justify,sizeof(img_richedit_justify));
  LoadImage(BIMargin,@img_richedit_margin,sizeof(img_richedit_margin));
  LoadImage(BIFont,@img_richedit_font,sizeof(img_richedit_font));
  LoadImage(BIColor,@img_richedit_color,sizeof(img_richedit_color));
  LoadImage(BIBGColor,@img_richedit_color_background,sizeof(img_richedit_color_background));
  LoadImage(BIImage,@img_richedit_image,sizeof(img_richedit_image));
  LoadImage(BINowrap,@img_richedit_nowrap,sizeof(img_richedit_nowrap));
  LoadImage(BILink,@img_richedit_hyperlink,sizeof(img_richedit_hyperlink));
  LoadImage(BICheck,@img_richedit_check,sizeof(img_richedit_check));
end;

Procedure RegisterRichTextImages(ADir : String);

  Procedure LoadImage(const iname,ifile : string);

  Var
    fn : string;
    img : TfpgImage;

  begin
    fn:=ADir+ifile+'.bmp';
    if FileExists(fn) then
      begin
      if (fpgImages.GetImage(iname)<>Nil) then
        fpgImages.DeleteImage(iname,true);
      img:=LoadImage_BMP(fn);
      img.CreateMaskFromSample(0,0);
      img.UpdateImage;
      fpgImages.AddImage(iname,img);
      end
    else
       ShowMessage(iname+' : file does not exist : '+ifile);
  end;

begin
  ADir:=IncludeTrailingPathDelimiter(ADir);
  LoadImage(BIBold,BFNBold);
  LoadImage(BIItalic,BFNItalic);
  LoadImage(BIunderline,BFNUnderLine);
//    BH1.ImageName:=BIH1;
//    BH2.ImageName:=BIH2;
//    BH3.ImageName:=BIH3;
  LoadImage(BIAlignLeft,BFNAlignLeft);
  LoadImage(BIAlignCenter,BFNAlignCenter);
  LoadImage(BIAlignRight,BFNAlignRight);
  LoadImage(BIAlignCenter,BFNAlignCenter);
  LoadImage(BIAlignJustify,BFNAlignJustify);
  // BIAlignJustify = 11;
  LoadImage(BIMargin,BFNMargin);
  LoadImage(BIFont,BFNFont);
  LoadImage(BIColor,BFNColor);
  LoadImage(BIBGColor,BFNBGColor);
  LoadImage(BIImage,BFNImage);
  LoadImage(BINowrap,BFNNowrap);
  LoadImage(BILink,BFNLink);
  LoadImage(BICheck,BFNCheck);

end;

end.