home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 October / PCWorld_2000-10_cd2.bin / Borland / interbase / IBConsole_src.ZIP / ibconsole / frmuTextViewer.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-07-24  |  10.1 KB  |  324 lines

  1. {
  2.  * The contents of this file are subject to the InterBase Public License
  3.  * Version 1.0 (the "License"); you may not use this file except in
  4.  * compliance with the License.
  5.  * 
  6.  * You may obtain a copy of the License at http://www.Inprise.com/IPL.html.
  7.  * 
  8.  * Software distributed under the License is distributed on an "AS IS"
  9.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  10.  * the License for the specific language governing rights and limitations
  11.  * under the License.  The Original Code was created by Inprise
  12.  * Corporation and its predecessors.
  13.  * 
  14.  * Portions created by Inprise Corporation are Copyright (C) Inprise
  15.  * Corporation. All Rights Reserved.
  16.  * 
  17.  * Contributor(s): ______________________________________.
  18. }
  19.  
  20. {****************************************************************
  21. *
  22. *  f r m u T e x t V i e w e r
  23. *
  24. ****************************************************************
  25. *  Author: The Client Server Factory Inc.
  26. *  Date:   March 1, 1999
  27. *
  28. *  Description:  This unit provides an interface for viewing
  29. *                text files
  30. *
  31. *****************************************************************
  32. * Revisions:
  33. *
  34. *****************************************************************}
  35. unit frmuTextViewer;
  36.  
  37. interface
  38.  
  39. uses
  40.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  41.   ComCtrls, ToolWin, StdCtrls, Menus, ImgList, Printers, IBServices, frmuDlgClass,
  42.   RichEditX, StdActns, ActnList;
  43.  
  44. type
  45.   TfrmTextViewer = class(TForm)
  46.     ToolButton5: TToolButton;
  47.     imgToolbarImages: TImageList;
  48.     mnuEdCopy: TMenuItem;
  49.     mnuEdFind: TMenuItem;
  50.     mnuEdN1: TMenuItem;
  51.     mnuEdit: TMenuItem;
  52.     mnuFiExit: TMenuItem;
  53.     mnuFiN1: TMenuItem;
  54.     mnuFiPrint: TMenuItem;
  55.     mnuFiSaveAs: TMenuItem;
  56.     mnuFile: TMenuItem;
  57.     mnuMain: TMainMenu;
  58.     sbCopy: TToolButton;
  59.     sbSaveAs: TToolButton;
  60.     stbStatusBar: TStatusBar;
  61.     tlbStandard: TToolBar;
  62.     reEditor: TRichEditX;
  63.     TextViewActions: TActionList;
  64.     EditCopy1: TEditCopy;
  65.     EditCut1: TEditCut;
  66.     EditPaste1: TEditPaste;
  67.     EditSelectAll1: TEditSelectAll;
  68.     EditUndo1: TEditUndo;
  69.     Cut1: TMenuItem;
  70.     Paste1: TMenuItem;
  71.     SelectAll1: TMenuItem;
  72.     Undo1: TMenuItem;
  73.     N1: TMenuItem;
  74.     Font1: TMenuItem;
  75.     EditFont: TAction;
  76.     function FormHelp(Command: Word; Data: Integer; var CallHelp: Boolean): Boolean;
  77.     procedure FormResize(Sender: TObject);
  78.     procedure FormShow(Sender: TObject);
  79.     procedure mnuEdFindClick(Sender: TObject);
  80.     procedure mnuFiExitClick(Sender: TObject);
  81.     procedure mnuFiSaveAsClick(Sender: TObject);
  82.     procedure EditFontExecute(Sender: TObject);
  83.     procedure EditCut1Update(Sender: TObject);
  84.     procedure mnuFiPrintClick(Sender: TObject);
  85.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  86.     procedure reEditorEnter(Sender: TObject);
  87.     procedure reEditorKeyPress(Sender: TObject; var Key: Char);
  88.     procedure EditUndo1Update(Sender: TObject);
  89.   private
  90.     { Private declarations }
  91.     FFileName: string;
  92.  
  93.     procedure SetFileName(const sFileName: String);
  94.   public
  95.     { Public declarations }
  96.   published
  97.     function OpenTextViewer(const Service: TIBControlAndQueryService; const sFormCaption: string; const readonly: boolean=true): integer;
  98.     function ShowText (const Data: TStringList; const Title: String; const readonly: boolean=true): integer;
  99.   end;
  100.  
  101. implementation
  102.  
  103. uses
  104.   RichEdit, zluGlobal, zluContextHelp, frmuMain, frmuMessage, IB, IBErrorCodes;
  105.  
  106. {$R *.DFM}
  107.  
  108. {****************************************************************
  109. *
  110. *  O p e n T e x t V i e w e r ( )
  111. *
  112. ****************************************************************
  113. *  Author: The Client Server Factory Inc.
  114. *  Date:   March 1, 1999
  115. *
  116. *  Input:  sInputStr    - a string containing the text source
  117. *                         or a filename
  118. *          sInputSrc    - a char specifying whether the input
  119. *                         source if from memory or from a file
  120. *          sFormCaption - string specifying the name of the form
  121. *
  122. *  Return: Integer - specifies whether task was successful
  123. *
  124. *  Description: Creates an instance of the text viewer and
  125. *               displays the contents of the input string
  126. *               if the source is from memory or opens the file
  127. *               specified by the input string if source
  128. *               is from file.
  129. *
  130. *****************************************************************
  131. * Revisions:
  132. *
  133. *****************************************************************}
  134. function TfrmTextViewer.OpenTextViewer(const Service: TIBControlAndQueryService;
  135.         const sFormCaption: string; const readonly:boolean=true): integer;
  136. begin
  137.   Caption := sFormCaption;           // set caption for form
  138.   reEditor.SetFont;
  139.   reEditor.Lines.Clear;
  140.   reEditor.Readonly := readonly;
  141.   Show;
  142.   while not service.Eof do
  143.   begin
  144.     Application.ProcessMessages;
  145.     reEditor.Lines.Add(service.GetNextLine);
  146.     reEditor.Modified := false;
  147.   end;
  148.   reEditor.SelStart := 0;
  149.   result := SUCCESS;
  150.   frmMain.UpdateWindowList(sFormCaption, TObject(Self));
  151.   reEditor.Modified := false;
  152. end;
  153.  
  154. function TfrmTextViewer.FormHelp(Command: Word; Data: Integer;
  155.   var CallHelp: Boolean): Boolean;
  156. begin
  157.   CallHelp := False;
  158.   // call WinHelp and show internal text viewer topic
  159.   Result := WinHelp(WindowHandle,CONTEXT_HELP_FILE,HELP_CONTEXT,FEATURES_INTERNAL_TEXT_VIEWER);
  160. end;
  161.  
  162. procedure TfrmTextViewer.FormResize(Sender: TObject);
  163. begin
  164.   reEditor.UpdateCursorPos(stbStatusBar); // displays current cursor position in status bar
  165. end;
  166.  
  167. procedure TfrmTextViewer.FormShow(Sender: TObject);
  168. begin
  169.   reEditor.UpdateCursorPos(stbStatusBar); // displays current cursor position in status bar
  170.   reEditor.Modified := false;  
  171. end;
  172.  
  173. procedure TfrmTextViewer.mnuEdFindClick(Sender: TObject);
  174. begin
  175.   reEditor.Find;
  176. end;
  177.  
  178. procedure TfrmTextViewer.mnuFiExitClick(Sender: TObject);
  179. begin
  180.   Close;                               // close the form
  181. end;
  182.  
  183. {****************************************************************
  184. *
  185. *  m n u F i S a v e A s C l i c k
  186. *
  187. ****************************************************************
  188. *  Author: The Client Server Factory Inc.
  189. *  Date:   March 1, 1999
  190. *
  191. *  Input:  Sender - the object the initiated the event
  192. *
  193. *  Return: None
  194. *
  195. *  Description: Shows the Save Dialog box and allows the user
  196. *               to save the contents of the richedit compoent
  197. *               to a specified file.
  198. *
  199. *****************************************************************
  200. * Revisions:
  201. *
  202. *****************************************************************}
  203. procedure TfrmTextViewer.mnuFiSaveAsClick(Sender: TObject);
  204. var
  205.   loSaveDialog: TSaveDialog;
  206. begin
  207.   loSaveDialog := nil;
  208.   try
  209.   begin
  210.      // create and show save dialog box
  211.     loSaveDialog := TSaveDialog.Create(Self);
  212.     loSaveDialog.Filter := 'Text files (*.txt)|*.TXT|SQL files (*.sql)|*.SQL|All files (*.*)|*.*';
  213.  
  214.     if loSaveDialog.Execute then
  215.     begin
  216.       // if the specified file already exists the show overwrite message
  217.       // if the user does not wish to overwrite the file then exit
  218.       if FileExists(loSaveDialog.FileName) then
  219.         if MessageDlg(Format('OK to overwrite %s', [loSaveDialog.FileName]),
  220.           mtConfirmation, mbYesNoCancel, 0) <> idYes then Exit;
  221.  
  222.       // if the file doesn't exist of the user wishes to overwrite it then
  223.       // save the contents of the richedit component to the specified file
  224.       reEditor.PlainText := true;
  225.       reEditor.Lines.SaveToFile(loSaveDialog.FileName);
  226.       SetFileName(loSaveDialog.FileName);
  227.       reEditor.PlainText := false;      
  228.       reEditor.Modified := False;      // set modified flag to false
  229.     end;
  230.   end
  231.   finally
  232.     // deallocate memory
  233.     loSaveDialog.free;
  234.   end;
  235. end;
  236.  
  237. procedure TfrmTextViewer.SetFileName(const sFileName: String);
  238. begin
  239.   FFileName := sFileName;              // set filename
  240. end;
  241.  
  242. function TfrmTextViewer.ShowText(const Data: TStringList;
  243.   const Title: String; const readonly: boolean = true): integer;
  244. begin
  245.   Caption := Title;
  246.   reEditor.Lines.BeginUpdate;
  247.   reEditor.SetFont;  
  248.   reEditor.Lines.Clear;
  249.   reEditor.ReadOnly := readonly;
  250.   reEditor.Lines.AddStrings (Data);
  251.   reEditor.SelStart := 0;
  252.   reEditor.Modified := false;
  253.   reEditor.Lines.EndUpdate;  
  254.   Show;
  255.   frmMain.UpdateWindowList(Title, TObject(Self));  
  256.   result := SUCCESS;
  257.   reEditor.Modified := false;  
  258. end;
  259.  
  260. procedure TfrmTextViewer.EditFontExecute(Sender: TObject);
  261. begin
  262.   reEditor.ChangeFont;
  263. end;
  264.  
  265. procedure TfrmTextViewer.EditCut1Update(Sender: TObject);
  266. begin
  267.   (Sender as TAction).Enabled := (not reEditor.ReadOnly) and (reEditor.SelLength > 0);
  268. end;
  269.  
  270. procedure TfrmTextViewer.mnuFiPrintClick(Sender: TObject);
  271. var
  272.   lPrintDialog: TPrintDialog;
  273.   lLine: integer;
  274.   lPrintText: TextFile;
  275. begin
  276.   lPrintDialog := nil;
  277.   if ActiveControl is TRichEditX then
  278.   begin
  279.     try
  280.       lPrintDialog := TPrintDialog.Create(Self);
  281.       try
  282.         if lPrintDialog.Execute then
  283.         begin
  284.           AssignPrn(lPrintText);
  285.           Rewrite(lPrintText);
  286.           Printer.Canvas.Font := TRichEditX(ActiveControl).Font;
  287.           for lLine := 0 to TRichEditX(ActiveControl).Lines.Count - 1 do
  288.             Writeln(lPrintText, TRichEditX(ActiveControl).Lines[lLine]);
  289.           CloseFile(lPrintText);
  290.         end
  291.         else
  292.           DisplayMsg (ERR_PRINT,'');
  293.       except on E: Exception do
  294.         DisplayMsg (ERR_PRINT, E.Message);
  295.       end;
  296.     finally
  297.       lPrintDialog.free;
  298.     end;
  299.   end;
  300. end;
  301.  
  302. procedure TfrmTextViewer.FormClose(Sender: TObject;
  303.   var Action: TCloseAction);
  304. begin
  305.   frmMain.UpdateWindowList(Self.Caption, TObject(Self), true);
  306. end;
  307.  
  308. procedure TfrmTextViewer.reEditorEnter(Sender: TObject);
  309. begin
  310.   TRichEditX(Sender).UpdateCursorPos(stbStatusBar);
  311. end;
  312.  
  313. procedure TfrmTextViewer.reEditorKeyPress(Sender: TObject; var Key: Char);
  314. begin
  315.   reEditorEnter(Sender);
  316. end;
  317.  
  318. procedure TfrmTextViewer.EditUndo1Update(Sender: TObject);
  319. begin
  320.   (Sender as TAction).Enabled := reEditor.Modified;
  321. end;
  322.  
  323. end.
  324.