File: lr_barc.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (823 lines) | stat: -rw-r--r-- 20,430 bytes parent folder | download | duplicates (2)
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
{*******************************************}
{                                           }
{            FastReport v2.3                }
{         Barcode Add-in object             }
{                                           }
{  Copyright (c) 1998-99 by Tzyganenko A.   }
{                                           }

//  Barcode Component
//  Version 1.3
//  Copyright 1998-99 Andreas Schmidt and friends

//  Freeware

//  for use with Delphi 2/3/4


//  this component is for private use only!
//  i am not responsible for wrong barcodes
//  Code128C not implemented

//  bug-reports, enhancements:
//  mailto:shmia@bizerba.de or
//  a_j_schmidt@rocketmail.com

{  Fr_BarC:     Guilbaud Olivier            }
{               golivier@worldnet.fr        }
{  Ported to FR2.3: Alexander Tzyganenko    }
{                                           }
{*******************************************}

unit LR_BarC;

{$I lr_vers.inc}

interface

uses
  Classes, SysUtils,
  LCLPlatformDef, InterfaceBase, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, Menus, Barcode, ExtCtrls, ButtonPanel,
  LCLType, LR_Class;


{.$DEFINE BC_1_25} //For Barcode version 1.25 actually in debug
type
  {$IFDEF BC_1_25}
  TBarCode=Class(TAsBarCode);
  {$ENDIF}
  
  { TfrBarCodeObject }

  TfrBarCodeObject = class(TComponent)  // fake component
  public
    constructor Create(aOwner : TComponent); override;
  end;

  TfrBarCode = packed record
    cCheckSum : Boolean;
    cShowText : Boolean;
    cCadr     : Boolean;
    cBarType  : TBarcodeType;
    cModul    : Integer;
    cRatio    : Double;
    cAngle    : Double;
  end;

  { TfrCustomBarCodeView }

  TfrCustomBarCodeView = class(TfrView)
  private
    BarC: TBarCode;
    FText: string;
    
    function GetAngle: Double;
    function GetBarType: TBarcodeType;
    function GetCheckSum: Boolean;
    function GetShowText: Boolean;
    function GetZoom: Double;
    procedure SetAngle(AValue: Double);
    procedure SetBarType(const AValue: TBarcodeType);
    procedure SetCheckSum(const AValue: Boolean);
    procedure SetShowText(const AValue: Boolean);
    procedure SetZoom(const AValue: Double);
    function CreateBarcode: TBitmap;
    function CreateLabelFont(aCanvas: TCanvas): TFont;
    procedure DrawLabel(aCanvas: TCanvas; R: TRect);
  public
    Param: TfrBarCode;
    
    constructor Create(AOwnerPage:TfrPage);override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
    function GenerateBitmap: TBitmap; virtual;
    procedure LoadFromStream(Stream: TStream); override;
    procedure SaveToStream(Stream: TStream); override;
    procedure Draw(aCanvas: TCanvas); override;
    procedure Print(Stream: TStream); override;
    procedure DefinePopupMenu({%H-}Popup: TPopupMenu); override;
    procedure LoadFromXML(XML: TLrXMLConfig; const Path: String); override;
    procedure SaveToXML(XML: TLrXMLConfig; const Path: String); override;

    property CheckSum : Boolean read GetCheckSum write SetCheckSum;
    property BarType : TBarcodeType read GetBarType write SetBarType;
    property ShowText : Boolean read GetShowText write SetShowText;
    property Zoom : Double read GetZoom write SetZoom;
    property Angle: Double read GetAngle write SetAngle;
  end;

  TfrBarcodeView = class(TfrCustomBarcodeView)
  published
    property CheckSum;
    property BarType;
    property ShowText;
    property Zoom;
    property Angle;
    property Memo;
    property Frames;
    property FrameColor;
    property FrameStyle;
    property FrameWidth;
    property Restrictions;
  end;

  { TfrBarCodeForm }

  TfrBarCodeForm = class(TfrObjEditorForm)
    ButtonPanel1: TButtonPanel;
    edZoom: TEdit;
    labZoom: TLabel;
    M1: TEdit;
    Label1: TLabel;
    cbType: TComboBox;
    Label2: TLabel;
    Image1: TImage;
    Panel1: TPanel;
    DBBtn: TSpeedButton;
    VarBtn: TSpeedButton;
    GroupBox1: TGroupBox;
    ckCheckSum: TCheckBox;
    ckViewText: TCheckBox;
    GroupBox2: TGroupBox;
    RB1: TRadioButton;
    RB2: TRadioButton;
    RB3: TRadioButton;
    RB4: TRadioButton;
    procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure VarBtnClick(Sender: TObject);
    procedure DBBtnClick(Sender: TObject);
    procedure bOkClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure edZoomKeyPress(Sender: TObject; var Key: char);
  public
    procedure ShowEditor(t: TfrView); override;
  end;


implementation

{$R *.lfm}

uses LR_Var, LR_Flds, LR_Const, LR_Utils;


var
  frBarCodeForm: TfrBarCodeForm;

const
   cbDefaultText ='12345678';
   bcNames: array[bcCode_2_5_interleaved..{$IFNDEF BC_1_25}bcCodeEAN13{$ELSE}bcCodeEAN128C{$ENDIF}, 0..1] of string =
     (('2_5_interleaved', 'N'),
      ('2_5_industrial', 'N'),
      ('2_5_matrix', 'N'),
      ('Code39', 'A'),
      ('Code39 Extended', 'A'),
      ('Code128A', 'A'),
      ('Code128B', 'A'),
      ('Code128C', 'N'),
      ('Code93', 'A'),
      ('Code93 Extended', 'A'),
      ('MSI', 'N'),
      ('PostNet', 'N'),
      ('Codebar', 'A'),
      ('EAN8', 'N'),
      ('EAN13', 'N')
      {$IFDEF BC_1_25}
      ,
      ('UPC A','N'),
      ('UPC E0','N'),
      ('UPC E1','N'),
      ('UPC SUPP 2','N'),
      ('UPC SUPP 5','N'),
      ('EAN128A','A'),
      ('EAN128B','A'),
      ('EAN128C','N')
      {$ENDIF}
      );

   defaultFontSize = 10;

{$HINTS OFF}
{$NOTES OFF}
function isNumeric(St: String): Boolean;
var
  {%H-}R: Double;
  E: Integer;
begin
  Val(St, R, E);
  Result := (E = 0);
end;
{$NOTES ON}
{$HINTS ON}

function TfrCustomBarCodeView.GetBarType: TBarcodeType;
begin
  Result:=Param.cBarType;
end;

function TfrCustomBarCodeView.GetAngle: Double;
begin
  Result := Param.cAngle;
end;

function TfrCustomBarCodeView.GetCheckSum: Boolean;
begin
  Result:=Param.cCheckSum;
end;

function TfrCustomBarCodeView.GetShowText: Boolean;
begin
  Result:=Param.cShowText;
end;

function TfrCustomBarCodeView.GetZoom: Double;
begin
  Result:=Param.cRatio;
end;

procedure TfrCustomBarCodeView.SetAngle(AValue: Double);
begin
  if (Param.cAngle<>AValue) and
     ((AValue=0.0) or (AValue=90.0) or (AValue=180.0) or (AValue=270.0)) then
  begin
    BeforeChange;
    Param.cAngle:=AValue;
    AfterChange;
  end;
end;

procedure TfrCustomBarCodeView.SetBarType(const AValue: TBarcodeType);
begin
  if Param.cBarType<>AValue then
  begin
    BeforeChange;
    Param.cBarType:=aValue;
    AfterChange;
  end;
end;

procedure TfrCustomBarCodeView.SetCheckSum(const AValue: Boolean);
begin
  if Param.cCheckSum<>AValue then
  begin
    BeforeChange;
    Param.cCheckSum:=aValue;
    AfterChange;
  end;
end;

procedure TfrCustomBarCodeView.SetShowText(const AValue: Boolean);
begin
  if Param.cShowText<>AValue then
  begin
    BeforeChange;
    Param.cShowText:=aValue;
    AfterChange;
  end;
end;

procedure TfrCustomBarCodeView.SetZoom(const AValue: Double);
begin
  if (Param.cRatio<>AValue) and
     ((AValue>=1.0)and(Avalue<=4.0)) then
  begin
    BeforeChange;
    Param.cRatio:=aValue;
    AfterChange;
  end;
end;

function TfrCustomBarCodeView.CreateBarcode: TBitmap;
begin

  Result := nil;
  if Trim(Memo.Text) = '' then
    Exit;

  {Assign Barcode text}
  Memo1.Assign(Memo);

  if (Memo1.Text <> '') and (pos('[',Memo1.Strings[0])=0) and
    ((bcNames[Param.cBarType, 1] = 'A') or IsNumeric(Memo1.Strings[0]) or
      Barc.BarcodeTypeChecked(Param.cBarType) )  then
  begin
      BarC.Text := Memo1.Strings[0];
      BarC.Checksum := Param.cCheckSum;
  end
  else
  begin
    BarC.Text := cbDefaultText;
    BarC.Checksum := true;
  end;

  if Trim(BarC.Text)='0' then Exit;

  {Barcode Properties}
  BarC.Left:= 0;
  BarC.Top := 0;
  BarC.Typ := Param.cBarType;
  BarC.Angle := Param.cAngle;
  BarC.Ratio := 2; // param.cRatio <>2 renders some codes unreadable
  BarC.Modul := 1; // param.cModul
  {$IFDEF BC_1_25}
  BarC.ShowTextPosition:=stpBottomCenter;
  BarC.ShowText := bcoNone;

  if FillColor=clNone then
    BarC.Color:=clWhite
  else
    BarC.Color:=FillColor;

  {$ELSE}
  BarC.ShowText:=False;
  {$ENDIF}


  {Barcode width is determined by type of barcode and text. Update
   object dimensions to suit barcode}

  if (Param.cAngle = 90) or (Param.cAngle = 270) then
    dy := BarC.Width
  else
    dx := BarC.Width;


  if (Param.cAngle = 90) or (Param.cAngle = 270) then
       BarC.Height := dx
  else
       BarC.Height := dy;

  if (BarC.Typ=bcCodePostNet) and (Param.cAngle=0) then
  begin
    BarC.Top:=BarC.Height;
    BarC.Height:=-BarC.Height;
  end;

  if  Param.cAngle = 90 then
    begin
      BarC.Top:= Round(Height);
      BarC.Left:=0;
    end
  else
  if  Param.cAngle = 180 then
    begin
      BarC.Top:= dy;
      BarC.Left:= dx;
    end
  else
  if  Param.cAngle = 270 then
    begin
      BarC.Top:= 0;
      BarC.Left:= dx;
    end;

  Result:=TBitMap.Create;

  Result.Width:=dx;
  Result.Height:=dy;
  Result.Canvas.Brush.Style:=bsSolid;
  Result.Canvas.Brush.Color:=clWhite;
  Result.Canvas.FillRect(Rect(0,0,dx,dy));

  try
    BarC.DrawBarcode(Result.Canvas);
    if BarC.Checksum then
      FText := BarC.CodeText
    else
      FText := BarC.Text;
  except on E: Exception do
    FText := E.Message
  end;


end;

function TfrCustomBarCodeView.CreateLabelFont(aCanvas: TCanvas) :TFont;
begin
  with aCanvas do
  begin
    Result := TFont.Create;
    Result.Assign(aCanvas.Font);
    Result.Color := clBlack;
    Result.Name := 'Arial';
    Result.Style := [];
    Result.Size := -defaultFontSize;

    if Param.cAngle = 90 then
      Result.Orientation := 900
    else
    if Param.cAngle = 180 then
      Result.Orientation := 1800
    else
    if Param.cAngle = 270 then
      Result.Orientation := 2700
    else
      Result.Orientation := 0;
  end;

end;


procedure TfrCustomBarCodeView.DrawLabel(aCanvas: TCanvas; R: TRect);
var fs: integer;
begin
  if Param.cShowText then
  begin
    with aCanvas do
    begin
      fs := Font.Height;

      if Param.cAngle = 0 then
      begin
        Brush.Color:=clWhite;
        Brush.Style:=bsSolid;
        FillRect(Rect(R.Left,R.Top + dy-fs ,R.Right, R.Bottom));
        TextOut(R.Left + (dx - TextWidth(FText)) div 2, R.Top + dy - fs, FText);
      end
      else
        if Param.cAngle = 90 then
        begin
          Brush.Color:=clWhite;
          Brush.Style:=bsSolid;
          FillRect(Rect(R.Left + dx - fs,R.Top,R.Right, R.Bottom));
          Font.Orientation := 900;

          TextOut(R.Right - fs,R.Bottom - (dy - TextWidth(FText)) div 2, FText)
        end
        else
          if Param.cAngle = 180 then
          begin
            Brush.Color:=clWhite;
            Brush.Style:=bsSolid;
            FillRect(Rect(R.Left,R.Top,R.Right,R.Top + fs));
            Font.Orientation := 1800;
            TextOut(R.left + (dx + TextWidth(FText)) div 2,  R.Top + fs, FText);
          end
          else
          begin
            Brush.Color:=clWhite;
            Brush.Style:=bsSolid;
            Font.Orientation := 2700;
            FillRect(Rect(R.Left,R.Top,R.Left + fs,R.Bottom));
            if (WidgetSet.LCLPlatform = lpGtk2) and IsPrinting then
            TextOut(R.Left + fs, R.Top + (dy -TextWidth(FText)) div 2, FText)
          end;
    end;
  end;

end;

constructor TfrCustomBarCodeView.Create(AOwnerPage: TfrPage);
begin
  inherited Create(AOwnerPage);

  BarC := TBarCode.Create(nil);
  Param.cCheckSum := True;
  Param.cShowText := True;
  Param.cCadr     := False;
  Param.cBarType  := bcCode39;
  Param.cModul    := 2;
  Param.cRatio    := 1;
  Param.cAngle    := 0;
  Memo.Add(cbDefaultText);
  Typ := gtAddIn;
  BaseName := 'Bar';
end;

destructor TfrCustomBarCodeView.Destroy;
begin
  BarC.Free;
  inherited Destroy;
end;

procedure TfrCustomBarCodeView.Assign(Source: TPersistent);
begin
  inherited Assign(Source);
  if Source is TfrCustomBarCodeView then
    Param := TfrCustomBarCodeView(Source).Param;
end;

function TfrCustomBarCodeView.GenerateBitmap: TBitmap;
var
  R: TRect;
  barcodeFont: TFont;
  oldFont: TFont;
begin
  Result := CreateBarcode;
  R := Rect(0,0, Result.Width,Result.Height);
  barcodeFont := CreateLabelFont(Result.Canvas);
  try
    oldFont := Result.Canvas.Font;
    Result.Canvas.Font := barcodeFont;
    DrawLabel(Result.Canvas,r)
  finally
      Result.Canvas.Font := oldFont;
      barcodeFont.Free
  end;
end;

procedure TfrCustomBarCodeView.LoadFromStream(Stream:TStream);
begin
  inherited LoadFromStream(Stream);
  Stream.Read(Param, SizeOf(Param));
end;

procedure TfrCustomBarCodeView.SaveToStream(Stream:TStream);
begin
  inherited SaveToStream(Stream);
  Stream.Write(Param, SizeOf(Param));
end;

procedure TfrCustomBarCodeView.Draw(aCanvas:TCanvas);
var
  Bmp : TBitMap;
  R: TRect;
  fh: integer;
  barcodeFont: TFont;
  oldFont: TFont;
begin
  BeginDraw(aCanvas);

  Bmp := CreateBarcode;
  if Bmp <> nil then
  try
    // dx/dy is calculated in CreateBarCode using as base
    // barcdode type, angle, zoom and data
    if (Param.cAngle = 90) or (Param.cAngle = 270) then
      dy := round(dy * param.cRatio)
    else
      dx := round(dx * Param.cRatio);
    CalcGaps;
    ShowBackground;
    if Param.cShowText then
    begin
      barcodeFont := CreateLabelFont(aCanvas);
      try
        oldFont := aCanvas.Font;
        aCanvas.Font := barcodeFont;
        if not IsPrinting then
        begin
          if (Param.cAngle = 90) or (Param.cAngle = 270) then
            ACanvas.Font.Height := -Round(ACanvas.Font.Size * ACanvas.Font.PixelsPerInch / 72 * ScaleX)
          else
            ACanvas.Font.Height := -Round(ACanvas.Font.Size * ACanvas.Font.PixelsPerInch / 72 * ScaleY);
          fh := Round(aCanvas.Font.Height);
        end
        else
          fh := aCanvas.Font.Height;

        if (Param.cAngle = 90)  then
          R := Rect(DRect.Left,DRect.Top,
                    DRect.Right - fh,
                    DRect.Bottom)
        else
        if (Param.cAngle = 180)  then
          R := Rect(DRect.Left,DRect.Top + fh,
                    DRect.Right ,
                    DRect.Bottom)
        else
        if (Param.cAngle = 270)  then
          R := Rect(DRect.Left + fh,
                  DRect.Top,
                  DRect.Right,
                  DRect.Bottom)
        else
          R := Rect(DRect.Left,DRect.Top,
                  DRect.Right ,
                  DRect.Bottom -  fh);
      aCanvas.StretchDraw(R,Bmp);
      DrawLabel(aCanvas, DRect);
      finally
        aCanvas.Font := oldFont;
        barcodeFont.Free
      end;
    end
    else
      aCanvas.StretchDraw(DRect,Bmp);

    ShowFrame;

  finally
    Bmp.Free;
    RestoreCoord;
  end;
end;

procedure TfrCustomBarCodeView.Print(Stream: TStream);
begin
  BeginDraw(Canvas);
  Memo1.Assign(Memo);
  CurReport.InternalOnEnterRect(Memo1, Self);
  frInterpretator.DoScript(Script);
  if not Visible then Exit;

  if Memo1.Count > 0 then
    if (Length(Memo1[0]) > 0) and (Pos('[',Memo1[0])<>0) then
      Memo1[0] := frParser.Calc(Memo1[0]);
  Stream.Write(Typ, 1);
  frWriteString(Stream, ClassName);
  SaveToStream(Stream);
end;

procedure TfrCustomBarCodeView.DefinePopupMenu(Popup: TPopupMenu);
begin
  // no specific items in popup menu
end;

procedure TfrCustomBarCodeView.LoadFromXML(XML: TLrXMLConfig; const Path: String);
begin
  inherited LoadFromXML(XML, Path);
  
  RestoreProperty('BarType',XML.GetValue(Path+'BarCode/BarType',''));
  RestoreProperty('ShowText',XML.GetValue(Path+'BarCode/ShowText',''));
  RestoreProperty('CheckSum',XML.GetValue(Path+'BarCode/CheckSum',''));
  RestoreProperty('Zoom',XML.GetValue(Path+'BarCode/Zoom','1'));
  RestoreProperty('Angle',XML.GetValue(Path+'BarCode/Angle','0'));
end;

procedure TfrCustomBarCodeView.SaveToXML(XML: TLrXMLConfig; const Path: String);
begin
  inherited SaveToXML(XML, Path);
  
  XML.SetValue(Path+'BarCode/BarType', GetSaveProperty('BarType'));
  XML.SetValue(Path+'BarCode/ShowText', GetSaveProperty('ShowText'));
  XML.SetValue(Path+'BarCode/CheckSum', GetSaveProperty('CheckSum'));
  XML.SetValue(Path+'BarCode/Zoom', GetSaveProperty('Zoom'));
  XML.SetValue(Path+'BarCode/Angle', GetSaveProperty('Angle'));
end;

//--------------------------------------------------------------------------
procedure TfrBarCodeForm.FormCreate(Sender: TObject);
var
  i: TBarcodeType;
begin
  CbType.Items.Clear;
  for i := bcCode_2_5_interleaved to {$IFNDEF BC_1_25}bcCodeEAN13{$ELSE}bcCodeEAN128C{$ENDIF} do
    cbType.Items.Add(bcNames[i, 0]);
  cbType.ItemIndex := 0;

  Caption := sBarCodeFormTitle;
  Label1.Caption := sBarCodeFormCode;
  Label2.Caption := sBarCodeFormType;
  GroupBox1.Caption := sBarCodeFormOpts;
  ckCheckSum.Caption := sBarCodeFormChksum;
  ckViewText.Caption := sBarCodeFormReadable;
  DBBtn.Hint := sBarCodeFormDbFld;
  VarBtn.Hint := sBarCodeFormVar;
  GroupBox2.Caption := sBarCodeFormRotate;
  labZoom.Caption:=sBarCodeZoom;
end;

procedure TfrBarCodeForm.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if ModalResult = mrOk then
    bOkClick(nil);
end;

procedure TfrBarCodeForm.FormActivate(Sender: TObject);
begin
  M1.SetFocus;
end;

procedure TfrBarCodeForm.edZoomKeyPress(Sender: TObject; var Key: char);
begin
  If (Key>#31) and not (Key in ['0'..'9','.']) then {AJW}
    Key:=#0;
end;

procedure TfrBarCodeForm.ShowEditor(t:TfrView);
var
  tmp:Double;
begin
  if t.Memo.Count > 0 then
    M1.Text := t.Memo.Strings[0];
  with t as TfrBarCodeView do
  begin
    cbType.ItemIndex   := ord(Param.cBarType);
    ckCheckSum.checked := Param.cCheckSum;
    ckViewText.Checked := Param.cShowText;
    if Param.cAngle = 0 then
      RB1.Checked := True
    else if Param.cAngle = 90 then
      RB2.Checked := True
    else if Param.cAngle = 180 then
      RB3.Checked := True
    else
      RB4.Checked := True;
    edZoom.Text:=SysUtils.Format('%.1f',[Param.cRatio]);

    if ShowModal = mrOk then
    begin
      Memo.Clear;
      Memo.Add(M1.Text);
      CheckSum  := ckCheckSum.Checked;
      ShowText  := ckViewText.Checked;
      BarType   := TBarcodeType(cbType.ItemIndex);
      tmp := StrToFloatDef(edZoom.Text,1);
      if tmp<1.0 then
        tmp:=1.0;
      Zoom := tmp;
        
      if RB1.Checked then
        Angle := 0
      else if RB2.Checked then
        Angle := 90
      else if RB3.Checked then
        Angle := 180
      else
        Angle := 270;
    end;
  end;
end;

procedure TfrBarCodeForm.VarBtnClick(Sender: TObject);
begin
  frVarForm := TfrVarForm.Create(nil);
  with frVarForm do
  if ShowModal = mrOk then
    if SelectedItem <> '' then
      M1.Text := '[' + SelectedItem + ']';
  frVarForm.Free;
  M1.SetFocus;
end;

procedure TfrBarCodeForm.DBBtnClick(Sender: TObject);
begin
  frFieldsForm := TfrFieldsForm.Create(nil);
  with frFieldsForm do
  if ShowModal = mrOk then
    if DBField <> '' then
      M1.Text := '[' + DBField + ']';
  frFieldsForm.Free;
  M1.SetFocus;
end;

procedure TfrBarCodeForm.bOkClick(Sender: TObject);
var
  bc: TBarCode;
  Bmp: TBitmap;
begin
  Bmp := nil;
  bc := TBarCode.Create(nil);
  try
    if Pos('[',M1.Text) <> 0 then
    begin
      bc.Text := cbDefaultText;
      bc.checksum := true
    end
    else
    begin
      bc.Text := M1.Text;
      bc.CheckSum  := ckCheckSum.Checked;
    end;
    bc.Ratio := StrToFloatDef(edZoom.Text,1);
    bc.Typ := TBarcodeType(cbType.ItemIndex);
    Bmp := TBitmap.Create;
    Bmp.Width := 16; Bmp.Height := 16;
    try
     Bmp.Canvas.Brush.Style:=bsSolid;
     Bmp.Canvas.Brush.Color:=clWhite;
     Bmp.Canvas.FillRect(Rect(0,0,Bmp.Width,Bmp.Height));

      bc.DrawBarcode(Bmp.Canvas);
    except
      MessageDlg(sBarcodeError,mtError,[mbOk],0);
      ModalResult := 0;
    end;
  finally
    Bmp.Free;
    bc.Free;
  end;
end;

procedure InitializeBarcAddin;
begin
  if not assigned(frBarCodeForm) {and not (csDesigning in ComponentState)} then
  begin
    frBarCodeForm := TfrBarCodeForm.Create(nil);
    frSetAddinEditor(TfrBarcodeView, frBarcodeForm);
    frSetAddinIcon(TfrBarcodeView, frBarCodeForm.Image1.Picture.Bitmap);
    frSetAddinHint(TfrBarcodeView, sInsBarcode);
  end;
end;

{ TfrBarCodeObject }

constructor TfrBarCodeObject.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  InitializeBarcAddin;
end;

initialization
  frBarcodeForm := nil;
  frRegisterObject(TfrBarCodeView, nil, '', nil, @InitializeBarcAddin);

finalization
  if Assigned(frBarCodeForm) then
    frBarCodeForm.Free;

end.