home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kolekce / d6 / FRCLX.ZIP / SOURCE / FR_Edit.pas < prev    next >
Pascal/Delphi Source File  |  2001-07-09  |  11KB  |  443 lines

  1.  
  2. {******************************************}
  3. {                                          }
  4. {           FastReport CLX v2.4            }
  5. {               Memo editor                }
  6. {                                          }
  7. { Copyright (c) 1998-2001 by Tzyganenko A. }
  8. {                                          }
  9. {******************************************}
  10.  
  11. unit FR_Edit;
  12.  
  13. interface
  14.  
  15. {$I FR.inc}
  16.  
  17. uses
  18.   SysUtils, Types, Classes, QGraphics, QControls,
  19.   QForms, QDialogs, QStdCtrls, QButtons, FR_Class, QExtCtrls,
  20.   FR_Ctrls, QComCtrls, QImgList
  21. {$IFDEF MWEDIT}
  22. , mwHighlighter, mwPasSyn, mwCustomEdit
  23. {$ENDIF};
  24.  
  25. type
  26.   TfrEditorForm = class(TfrObjEditorForm)
  27.     ScriptPanel: TPanel;
  28.     MemoPanel: TPanel;
  29.     M1: TMemo;
  30.     Splitter: TPanel;
  31.     Panel1: TToolBar;
  32.     OkBtn: TToolButton;
  33.     CancelBtn: TToolButton;
  34.     Bevel2: TBevel;
  35.     InsExprBtn: TToolButton;
  36.     InsDBBtn: TToolButton;
  37.     WordWrapBtn: TToolButton;
  38.     ScriptBtn: TToolButton;
  39.     Panel2: TPanel;
  40.     ErrorPanel: TPanel;
  41.     CutBtn: TToolButton;
  42.     CopyBtn: TToolButton;
  43.     PasteBtn: TToolButton;
  44.     ImageList1: TImageList;
  45.     ToolButton1: TToolButton;
  46.     ToolButton2: TToolButton;
  47.     ToolButton3: TToolButton;
  48.     ToolBar1: TToolBar;
  49.     procedure M1KeyDown(Sender: TObject; var Key: Word;
  50.       Shift: TShiftState);
  51.     procedure M1Enter(Sender: TObject);
  52.     procedure FormCreate(Sender: TObject);
  53.     procedure SplitterMouseDown(Sender: TObject; Button: TMouseButton;
  54.       Shift: TShiftState; X, Y: Integer);
  55.     procedure SplitterMouseMove(Sender: TObject; Shift: TShiftState; X,
  56.       Y: Integer);
  57.     procedure SplitterMouseUp(Sender: TObject; Button: TMouseButton;
  58.       Shift: TShiftState; X, Y: Integer);
  59.     procedure FormShow(Sender: TObject);
  60.     procedure InsExprBtnClick(Sender: TObject);
  61.     procedure InsDBBtnClick(Sender: TObject);
  62.     procedure WordWrapBtnClick(Sender: TObject);
  63.     procedure ScriptBtnClick(Sender: TObject);
  64.     procedure CancelBtnClick(Sender: TObject);
  65.     procedure OkBtnClick(Sender: TObject);
  66.     procedure FormResize(Sender: TObject);
  67.     procedure FormKeyPress(Sender: TObject; var Key: Char);
  68.     procedure CutBtnClick(Sender: TObject);
  69.     procedure CopyBtnClick(Sender: TObject);
  70.     procedure PasteBtnClick(Sender: TObject);
  71.     procedure FormDestroy(Sender: TObject);
  72.   private
  73.     { Private declarations }
  74. {$IFDEF MWEDIT}
  75.     M2: TmwCustomEdit;
  76. {$ELSE}
  77.     M2: TMemo;
  78. {$ENDIF}
  79.     FActiveMemo: TWinControl;
  80.     FDown: Boolean;
  81.     FLastY: Integer;
  82.     FView: TfrView;
  83.     FShowScript: Boolean;
  84.     FSplitterPos: Integer;
  85.     procedure SetSelText(s: String);
  86.     procedure Localize;
  87.   public
  88.     { Public declarations }
  89.     function ShowEditor(View: TfrView): TModalResult; override;
  90.   end;
  91.  
  92.  
  93. implementation
  94.  
  95. {$R *.xfm}
  96.  
  97. uses
  98.   FR_Dock, FR_Desgn, FR_Expr, FR_Fmted, FR_Flds, FR_Const, FR_Utils, Qt;
  99.  
  100. var
  101.   SplitterPos: Integer;
  102.   ShowScript: Boolean = True;
  103.   Wrap: Boolean;
  104.   FormSize: TRect;
  105.  
  106.  
  107. function TfrEditorForm.ShowEditor(View: TfrView): TModalResult;
  108. var
  109.   EmptyScript: Boolean;
  110.   ScriptText: String;
  111.   Nm: String;
  112.   isMemoEditor: Boolean;
  113. begin
  114.   Nm := rsForm + frDesigner.ClassName;
  115.   FView := View;
  116.   isMemoEditor := not ((View is TfrControl) or (View is TfrBandView));
  117.  
  118.   ErrorPanel.Hide;
  119.   WordWrapBtnClick(nil);
  120.   M1.Lines.Assign(FView.Memo);
  121.   M2.Lines.Assign(FView.Script);
  122.   if FView.Script.Text = '' then
  123.     M2.Text := 'begin' + #13#10 + '  ' + #13#10 + 'end';
  124.  
  125.   M1.Font.Name := frIni.ReadString(Nm, 'TextFontName', 'Arial');
  126.   M1.Font.Size := frIni.ReadInteger(Nm, 'TextFontSize', 10);
  127.   M2.Font.Name := frIni.ReadString(Nm, 'ScriptFontName', 'Courier New');
  128.   M2.Font.Size := frIni.ReadInteger(Nm, 'ScriptFontSize', 10);
  129.  
  130.   if (FView is TfrMemoView) and frIni.ReadBool(Nm, 'UseDefaultFont', True) then
  131.   begin
  132.     M1.Font.Name := TfrMemoView(FView).Font.Name;
  133.     M1.Font.Size := 10;
  134.   end;
  135.   if View is TfrMemoView then
  136.     M1.Font.Charset := TfrMemoView(View).Font.Charset else
  137.     M1.Font.Charset := TFontCharset(frCharset);
  138.   M2.Font.Charset := TFontCharset(frCharset);
  139.   M1.ReadOnly := (FView.Restrictions and frrfDontEditMemo) <> 0;
  140.   M2.ReadOnly := (FView.Restrictions and frrfDontEditScript) <> 0;
  141.  
  142.   MemoPanel.Align := alNone;
  143.   MemoPanel.SetBounds(0, 0, 10, 10);
  144.   Splitter.Align := alNone;
  145.   Splitter.SetBounds(0, 20, 10, 2);
  146.   ScriptPanel.Align := alNone;
  147.   ScriptPanel.SetBounds(0, 40, 10, 10);
  148.  
  149.   ScriptBtn.Down := True;
  150.   if isMemoEditor then
  151.   begin
  152.     MemoPanel.Show;
  153.     Splitter.Visible := FShowScript;
  154.     ScriptPanel.Visible := FShowScript;
  155.     ScriptBtn.Down := FShowScript;
  156.     ScriptPanel.Align := alBottom;
  157.     Splitter.Align := alBottom;
  158.     MemoPanel.Align := alClient;
  159.     ScriptPanel.Height := ClientHeight - FSplitterPos - 2;
  160.   end
  161.   else
  162.   begin
  163.     MemoPanel.Hide;
  164.     Splitter.Hide;
  165.     ScriptPanel.Show;
  166.     ScriptPanel.Align := alClient;
  167.   end;
  168.  
  169.   if MemoPanel.Visible then
  170.     FActiveMemo := M1 else
  171.     FActiveMemo := M2;
  172.  
  173.   Result := ShowModal;
  174.   if Result = mrOk then
  175.   begin
  176.     frDesigner.BeforeChange;
  177.     M1.WordWrap := False;
  178.     FView.Memo.Text := M1.Text;
  179.     EmptyScript := (M2.Lines.Count = 3) and
  180.       (Trim(M2.Lines[0]) = 'begin') and
  181.       (Trim(M2.Lines[1]) = '') and
  182.       (Trim(M2.Lines[2]) = 'end');
  183.     if EmptyScript then
  184.       ScriptText := '' else
  185.       ScriptText := M2.Text;
  186.     if not EmptyScript or (FView.Script.Text <> '') then
  187.       FView.Script.Text := ScriptText;
  188.   end;
  189.   if isMemoEditor then
  190.     FShowScript := ScriptBtn.Down;
  191. end;
  192.  
  193. procedure TfrEditorForm.FormShow(Sender: TObject);
  194. begin
  195.   if MemoPanel.Visible then
  196.     M1.SetFocus else
  197.     M2.SetFocus;
  198. end;
  199.  
  200. procedure TfrEditorForm.SetSelText(s: String);
  201. begin
  202.   if FActiveMemo = M1 then
  203.     M1.SelText := s else
  204.     M2.SelText := s;
  205. end;
  206.  
  207. procedure TfrEditorForm.InsExprBtnClick(Sender: TObject);
  208. var
  209.   s: String;
  210. begin
  211.   s := frDesigner.InsertExpression;
  212.   if s <> '' then
  213.     SetSelText(s);
  214. end;
  215.  
  216. procedure TfrEditorForm.InsDBBtnClick(Sender: TObject);
  217. var
  218.   s: String;
  219. begin
  220.   s := frDesigner.InsertDBField;
  221.   if s <> '' then
  222.     SetSelText(s);
  223. end;
  224.  
  225. procedure TfrEditorForm.M1KeyDown(Sender: TObject; var Key: Word;
  226.   Shift: TShiftState);
  227. begin
  228.   ErrorPanel.Hide;
  229.   if (Key = key_Insert) and (Shift = []) then InsDBBtnClick(Self);
  230.   if Key = key_Escape then ModalResult := mrCancel;
  231. end;
  232.  
  233. procedure TfrEditorForm.FormKeyPress(Sender: TObject; var Key: Char);
  234. begin
  235.   if Key = #10 then
  236.   begin
  237.     Key := #0;
  238.     OkBtnClick(nil);
  239.   end;
  240. end;
  241.  
  242. procedure TfrEditorForm.M1Enter(Sender: TObject);
  243. begin
  244.   FActiveMemo := Sender as TWinControl;
  245. end;
  246.  
  247. procedure TfrEditorForm.WordWrapBtnClick(Sender: TObject);
  248. begin
  249.   M1.WordWrap := WordWrapBtn.Down;
  250. end;
  251.  
  252. procedure TfrEditorForm.ScriptBtnClick(Sender: TObject);
  253. begin
  254.   if (FView is TfrControl) or (FView is TfrBandView) then
  255.   begin
  256.     ScriptBtn.Down := True;
  257.     Exit;
  258.   end;
  259.   if ScriptBtn.Down then
  260.     ScriptPanel.Top := MemoPanel.Top + 1;
  261.   ScriptPanel.Visible := ScriptBtn.Down;
  262.   Splitter.Visible := ScriptBtn.Down;
  263.   Splitter.Top := MemoPanel.Top + 1;
  264.   if ScriptPanel.Visible then
  265.     Splitter.Cursor := crVSplit else
  266.     Splitter.Cursor := crDefault;
  267. end;
  268.  
  269. procedure TfrEditorForm.Localize;
  270. begin
  271.   Caption := S53060;
  272.   InsExprBtn.Hint := S53061;
  273.   InsDBBtn.Hint := S53062;
  274.   CutBtn.Hint := S53091;
  275.   CopyBtn.Hint := S53092;
  276.   PasteBtn.Hint := S53093;
  277.   WordWrapBtn.Hint := S53063;
  278.   ScriptBtn.Hint := S53064;
  279.   OkBtn.Hint := (SOk);
  280.   CancelBtn.Hint := (SCancel);
  281. end;
  282.  
  283. procedure TfrEditorForm.FormCreate(Sender: TObject);
  284. {$IFDEF MWEDIT}
  285. var
  286.   SynParser: TmwPasSyn;
  287. {$ENDIF}
  288. begin
  289.   Localize;
  290.   FShowScript := True;
  291.   FSplitterPos := Height div 2;
  292.  
  293. {$IFDEF MWEDIT}
  294.   M2 := TmwCustomEdit.Create(Self);
  295.   SynParser := TmwPasSyn.Create(Self);
  296.   {$I *.inc}
  297. {$ELSE}
  298.   M2 := TMemo.Create(Self);
  299.   M2.WordWrap := False;
  300. {$ENDIF}
  301.   M2.Parent := ScriptPanel;
  302.   M2.Align := alClient;
  303.   M2.HelpContext := 20;
  304.   M2.ScrollBars := ssBoth;
  305.   M2.Font.Name := 'Courier New';
  306.   M2.Font.Size := 10;
  307.   M2.OnEnter := M1Enter;
  308.   M2.OnKeyDown := M1KeyDown;
  309. {$IFDEF MWEDIT}
  310.   M2.Highlighter := SynParser;
  311.   M2.Gutter.Visible := False;
  312. {$ENDIF}
  313.   if SplitterPos <> 0 then
  314.     FSplitterPos := SplitterPos;
  315.   FShowScript := ShowScript;
  316.   if FormSize.Right <> 0 then
  317.   begin
  318.     Position := poDesigned;
  319.     SetBounds(FormSize.Left, FormSize.Top,
  320.       FormSize.Right - FormSize.Left, FormSize.Bottom - FormSize.Top);
  321.   end;
  322.   WordWrapBtn.Down := Wrap;
  323. end;
  324.  
  325. procedure TfrEditorForm.FormDestroy(Sender: TObject);
  326. begin
  327.   SplitterPos := FSplitterPos;
  328.   ShowScript := FShowScript;
  329.   FormSize := BoundsRect;
  330.   Wrap := WordWrapBtn.Down;
  331.   ImageList1.Clear;
  332. end;
  333.  
  334. procedure TfrEditorForm.SplitterMouseDown(Sender: TObject;
  335.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  336. begin
  337.   FDown := True;
  338.   FLastY := Y;
  339. end;
  340.  
  341. procedure TfrEditorForm.SplitterMouseMove(Sender: TObject;
  342.   Shift: TShiftState; X, Y: Integer);
  343. begin
  344.   if FDown then
  345.   begin
  346.     ScriptPanel.Height := ScriptPanel.Height - (Y - FLastY);
  347.     Splitter.Top := Splitter.Top + Y - FLastY;
  348.     FSplitterPos := Splitter.Top;
  349.   end;
  350. end;
  351.  
  352. procedure TfrEditorForm.SplitterMouseUp(Sender: TObject;
  353.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  354. begin
  355.   FDown := False;
  356. end;
  357.  
  358. procedure TfrEditorForm.CancelBtnClick(Sender: TObject);
  359. begin
  360.   ModalResult := mrCancel;
  361. end;
  362.  
  363. {$HINTS OFF}
  364. procedure TfrEditorForm.OkBtnClick(Sender: TObject);
  365. var
  366.   sl1, sl2: TStringList;
  367.  
  368.   procedure ErrorPosition;
  369.   var
  370.     s: String;
  371.     i, n, n1, n2: Integer;
  372.   begin
  373.     s := sl2.Text;
  374.     n := 0;
  375.     n1 := 1;
  376.     if Pos('/', s) <> 0 then
  377.     begin
  378.       n := StrToInt(Copy(s, 6, Pos('/', s) - 6));
  379.       n1 := StrToInt(Copy(s, Pos('/', s) + 1, Pos(':', s) - Pos('/', s) - 1));
  380.     end;
  381.  
  382.     n2 := 0;
  383.     for i := 0 to n - 2 do
  384.       Inc(n2, Length(M2.Lines[i]) + 2);
  385.     Inc(n2, n1 - 1);
  386.     M2.SetFocus;
  387. {$IFDEF MWEDIT}
  388.     M2.CaretXY := Point(n1, n);
  389. {$ELSE}
  390. //    M2.Perform(EM_SETSEL, n2, n2); M2.Perform(EM_SCROLLCARET, 0, 0);
  391. {$ENDIF}
  392.   end;
  393.  
  394. begin
  395.   sl1 := TStringList.Create;
  396.   sl2 := TStringList.Create;
  397.   frInterpretator.PrepareScript(M2.Lines, sl1, sl2);
  398.   if sl2.Count > 0 then
  399.   begin
  400.     ErrorPanel.Caption := ' ' + sl2.Text;
  401.     ErrorPanel.Show;
  402.     ErrorPosition;
  403.   end
  404.   else
  405.     ModalResult := mrOk;
  406.   sl1.Free;
  407.   sl2.Free;
  408. end;
  409. {$HINTS ON}
  410.  
  411. procedure TfrEditorForm.FormResize(Sender: TObject);
  412. begin
  413.   ErrorPanel.Hide;
  414.   if MemoPanel.Visible then
  415.     if ScriptPanel.Height > ClientHeight - 40 then
  416.       ScriptPanel.Height := ClientHeight div 2;
  417. end;
  418.  
  419. procedure TfrEditorForm.CutBtnClick(Sender: TObject);
  420. begin
  421.   if FActiveMemo = M1 then
  422.     M1.CutToClipboard else
  423.     M2.CutToClipboard;
  424. end;
  425.  
  426. procedure TfrEditorForm.CopyBtnClick(Sender: TObject);
  427. begin
  428.   if FActiveMemo = M1 then
  429.     M1.CopyToClipboard else
  430.     M2.CopyToClipboard;
  431. end;
  432.  
  433. procedure TfrEditorForm.PasteBtnClick(Sender: TObject);
  434. begin
  435.   if FActiveMemo = M1 then
  436.     M1.PasteFromClipboard else
  437.     M2.PasteFromClipboard;
  438. end;
  439.  
  440.  
  441. end.
  442.  
  443.