📄 cpwedit.pas
字号:
{*******************************************************************
* *
* COMPONENT for MS DOS and Windows source code. *
* *
* (c) 1992, Roderic D. M. Page *
* *
* Language: Turbo Pascal (Pascal with object-oriented extensions) *
* Compiler: Turbo Pascal 6.0 (MS DOS) *
* Turbo Pascal for Windows 1.0 (WINDOWS) *
* *
* Notes: Program interface is currently Windows specific. *
* *
*******************************************************************}
(*
TO DO:
A present if user types in a line of chars > 500
in length and then tries to save it we get an
UAE! Need eventually to be more robust, have some
way of breaking string into smaller pieces.
Is it really necessary to have the Execute command
display the file name? Since this is already implicit
in the menu.
*)
{*
Edit window for COMPONENT.
A simple file editor window descendant of TWindow.
This code is a combination of Borland's code for TEditWindow
and TFileWindow in STDWNDS.PAS. I've made BaseEditWindow object
a descendant of TWindow so that eventually it can be made a
descendant of a specialized MDI child window type.
BaseEditWindow responds to the commands cm_FileSave and cm_FileSaveAs,
and sends the user message um_EditWindow to its parent when it is
activated.
It provides two abstract methods that allow descendants to give
it the ability to inform parent that the window contains a file
that can be executed. Such descendant will send a um_UpDateExecute
message to the parent.
9 Sep 1991 Code cleaned up.
24 Sep 1991 Code split into BaseEditWindow (a TWindow desc) and
its desc MyEditWindow for use in COMPONENT. This
will enable the edit windows to descend from a
future MDI child window type, as well as allow me
to develop edit windows for other projects.
30 Oct 1991 GetClassName added so icons work properly.
5 Feb 1992 New editor object added that computes position of
caret. Window now keeps user informed of position using
status bar.
6 Mar 1992 Editor font set to ANSI fixed pitch system font.
12 Mar 1992 Editor now ensures the Edit menu items Cut, Copy,
Del, Undo, and Paste are correct.
13 Mar 1992 New object DisplayEditWindow derived from MyEditWindow
which opens up with the display buffer.
9 Jun 1992 DisplayEditWindow deleted, now use NOTEPAD.EXE for large
files and the display buffer.
User messages defined in this module:
=====================================
um_UpDateExecute
----------------------------------------------------------------
Informs parent that an edit child window has been selected,
or its contents have been saved to disk.
Parameters If edit window has an executable file:
wParam: a handle to the edit window's edit control
lParam: a pointer to the edit window filename
otherwise:
wParam: 0
lParam: 0
Comments This message allows Frame window to
keep track of the currently edited file, and
its editor. Can be used, for example
to update a File|Execute %s command.
um_EditWindow
------------------------------------------------------------------
Inform parent window that editor window has been activated.
Parameters wParam: not used.
lParam: not used.
*}
{$I CPDIR.INC}
unit cpwedit;
interface
uses
{$IFDEF BWCC} { use Borland-style dialogs }
BWCC,
{$IFDEF VER10} { TPW 1.0 }
WObjectB,
StdDlgsB,
{$ELSE} { TPW 1.5}
WObjects,
StdDlgs,
{$ENDIF} {VER10}
{$ELSE} { standard dialogs }
WObjects,
StdDlgs,
{$ENDIF} {BWCC}
WinTypes,
WinProcs,
WinDos,
Strings,
cpwvars,
cpheader,
cpwbuf,
cpwdlg,
cpwdial,
cpwprint,
cpmdi;
const
EDITORSIZE = 8192; { 8K text editor }
NexusHeader:PChar = '#NEXUS';
um_UpDateExecute = wm_User + 1;
um_EditWindow = wm_User + 4;
um_EditorSelection = wm_User + 44;
um_EditorPaste = wm_User + 45;
um_EditorUndo = wm_User + 46;
type
{ Editor that informs the frame window of the caret's position. }
PNotifyEdit = ^NotifyEdit;
NotifyEdit = object(TEdit)
function Create:Boolean;virtual;
procedure WMChar (var Msg:TMessage);virtual wm_First + wm_Char;
procedure WMKeyDown (var Msg:TMessage);virtual wm_First + wm_KeyDown;
procedure WMLButtonUp (var Msg:TMessage);virtual wm_First + wm_LButtonUp;
procedure WMLButtonDown (var Msg:TMessage);virtual wm_First + wm_LButtonDown;
procedure UpdatePosn;
procedure CMEditCut (var Msg:TMessage);
virtual cm_First + cm_EditCut;
procedure CMEditCopy (var Msg:TMessage);
virtual cm_First + cm_EditCopy;
procedure CMEditDelete (var Msg:TMessage);
virtual cm_First + cm_EditDelete;
procedure CMEditPaste (var Msg:TMessage);
virtual cm_First + cm_EditPaste;
end;
{ A simple text editing window }
PBaseEditor = ^BaseEditWindow;
BaseEditWindow = object (MDIChild)
Editor : PNotifyEdit;
FileName : PChar;
IsNewFile : Boolean;
constructor Init(AParent: PWindowsObject; ATitle, AFileName: PChar);
destructor Done; virtual;
function CanClear: Boolean; virtual;
function CanClose: Boolean; virtual;
procedure NewFile;
procedure Open;
procedure Print(FromLine, ToLine:integer);
procedure Read;
procedure SetFileName(AFileName: PChar);
procedure ReplaceWith(AFileName: PChar);
function Save: Boolean;
function SaveAs: Boolean;
procedure SetupWindow; virtual;
procedure Write;
procedure CMFileSave(var Msg: TMessage);
virtual cm_First + cm_FileSave;
procedure CMFileSaveAs(var Msg: TMessage);
virtual cm_First + cm_FileSaveAs;
procedure CMFilePrint (var Msg:TMessage);
virtual cm_First + cm_FilePrint;
procedure CMFilePrintSelection (var Msg:TMessage);
virtual cm_First + cm_FilePrintSelection;
procedure WMMDIActivate (var Msg: TMessage);
virtual wm_First + wm_MDIActivate;
procedure WMSize (var Msg: TMessage);
virtual wm_First + wm_Size;
procedure WMSetFocus (var Msg: TMessage);
virtual wm_First + wm_SetFocus;
function ExecutableFile:Boolean;virtual;
procedure UpDateParent;virtual;
end;
{ A descendant for use in COMPONENT }
PMyEditor = ^MyEditWindow;
MyEditWindow = object(BaseEditWindow)
function ExecutableFile:Boolean;virtual;
procedure UpDateParent;virtual;
procedure WMMDIDestroy (var Msg: TMessage);
virtual wm_First + wm_MDIDestroy;
procedure GetWindowClass (var AWndClass:TWndClass);virtual;
function GetClassName:PChar;virtual;
end;
implementation
{ Override ObjectWindows function to create our own editor. }
function NotifyEdit.Create:Boolean;
begin
Create := TEdit.Create;
{ Create := False;
if Register then begin
HWindow := CreateWindow('cpedit', nil,
WS_CHILD or WS_VISIBLE
or ws_HScroll or ws_VScroll
or es_Left or es_Multiline or es_NoHideSel,
0, 0,
0, 0,
Parent^.HWindow, 200, HInstance, nil);
if (HWindow <> 0) then begin
SetupWindow;
Create := true;
end;
end;}
end;
{ Get the current position of the caret in the editor, construct
a string, and send it to the edit window's parent. }
procedure NotifyEdit.UpdatePosn;
var
StartSel, EndSel,
CurrentLine,
LineLength,
CharsBeforeLine,
PosnInLine : integer;
A, B : array[0..10] of char;
s : PChar;
st : string;
LineBuffer: array[0..500] of char;
i, j : integer;
begin
GetSelection (StartSel, EndSel);
CurrentLine := GetLineFromPos (EndSel);
PosnInLine := EndSel - GetLineIndex (CurrentLine);
LineLength := GetLineLength (CurrentLine);
if GetLine (LineBuffer, LineLength + 1, CurrentLine) then begin
j := 0;
for i := 0 to Pred(PosnInLine) do
if (LineBuffer[i] <> #9) then
Inc (j)
else
{ pad tabs }
j := j + (8 - (j mod 8));
PosnInLine := j;
end;
Inc (CurrentLine);
Inc (PosnInLine);
Str (CurrentLine:5, st);
StrPCopy (A, st + ':');
Str (PosnInLine, st);
StrPCopy (B, st);
StrCat (A, B);
s := @A;
SendMessage (Parent^.Parent^.HWindow, fw_StatusBarUpDate, 0, longint(s));
{ Update Edit menu. }
SendMessage (Parent^.Parent^.HWindow, um_EditorSelection,
StartSel, EndSel);
{ Update Edit menu. }
SendMessage (Parent^.Parent^.HWindow, um_EditorUndo,
longint (IsModified), 0);
end;
{ User can move caret by entering characters }
procedure NotifyEdit.WMChar (var Msg:TMessage);
begin
DefWndProc (Msg);
UpDatePosn;
end;
{ User can move caret using mouse }
procedure NotifyEDit.WMLButtonUp (var Msg:TMessage);
begin
DefWndProc (Msg);
UpDatePosn;
end;
{ User can move caret using mouse }
procedure NotifyEdit.WMLButtonDown (var Msg:TMessage);
begin
DefWndProc (Msg);
UpDatePosn;
end;
{ User can move caret using keys }
procedure NotifyEdit.WMKeydown (var Msg:TMessage);
begin
DefWndproc(Msg);
UpdatePosn;
end;
procedure NotifyEdit.CMEditCopy (var Msg:TMessage);
begin
TEdit.CMEditCopy (Msg);
SendMessage (Parent^.Parent^.HWindow, um_EditorUndo, 1, 0);
SendMessage (Parent^.Parent^.HWindow, um_EditorPaste, 1, 0);
end;
procedure NotifyEdit.CMEditCut (var Msg:TMessage);
begin
TEdit.CMEditCut (Msg);
SendMessage (Parent^.Parent^.HWindow, um_EditorUndo, 1, 0);
SendMessage (Parent^.Parent^.HWindow, um_EditorPaste, 1, 0);
end;
procedure NotifyEdit.CMEditDelete (var Msg:TMessage);
begin
TEdit.CMEditDelete (Msg);
SendMessage (Parent^.Parent^.HWindow, um_EditorUndo, 1, 0);
end;
procedure NotifyEdit.CMEditPaste (var Msg:TMessage);
begin
TEdit.CMEditPaste (Msg);
SendMessage (Parent^.Parent^.HWindow, um_EditorUndo, 1, 0);
end;
{*****************************BaseEditWindow*******************************}
{-----------------------------Init-----------------------------------------}
{ Constructor for a BaseEditWindow. Initializes its data fields using
passed parameters and default values. }
constructor BaseEditWindow.Init(AParent: PWindowsObject; ATitle,
AFileName: PChar);
begin
MDIChild.Init(AParent, ATitle);
Editor := New(PNotifyEdit, Init(@Self, 200, nil, 0, 0, 0, 0,
0, {EDITORSIZE,} True));
{ with Editor^.Attr do
Style := Style or es_NoHideSel;}
IsNewFile := True;
FileName := StrNew(AFileName);
end;
{-----------------------------Done-----------------------------------------}
{ Dispose of the file name }
destructor BaseEditWindow.Done;
begin
StrDispose(FileName);
TWindow.Done;
end;
{-----------------------------WMSize---------------------------------------}
{ Responds to an incoming wm_Size message by resizing the child edit
control according to the size of the TEditWindow's client area. }
procedure BaseEditWindow.WMSize(var Msg: TMessage);
begin
TWindow.WMSize(Msg);
SetWindowPos(Editor^.HWindow, 0, -1, -1, Msg.LParamLo+2, Msg.LParamHi+2,
swp_NoZOrder);
end;
{-----------------------------WMSetFocus-----------------------------------}
{ Responds to an incoming wm_SetFocus message by setting the focus to the
child edit control. }
procedure BaseEditWindow.WMSetFocus(var Msg: TMessage);
begin
SetFocus(Editor^.HWindow);
end;
{-----------------------------SetUpWindow----------------------------------}
{ Performs setup for a BaseEditWindow, appending 'Untitled' to its caption }
procedure BaseEditWindow.SetupWindow;
begin
TWindow.SetupWindow;
{ Set ANSI fixed pitch system font. }
SendMessage (Editor^.HWindow, wm_SetFont,
GetStockObject (OEM_FIXED_FONT), 1);
SetFileName(FileName);
if FileName <> nil then Read;
end;
{-----------------------------SetFileName----------------------------------}
{ Sets the file name of the window and updates the caption. Assumes
that the AFileName parameter and the FileName instance variable were
allocated by StrNew. }
procedure BaseEditWindow.SetFileName(AFileName: PChar);
var
NewCaption: array[0..80] of Char;
P: array[0..1] of PChar;
begin
if FileName <> AFileName then
begin
StrDispose(FileName);
FileName := StrNew(AFileName);
end;
P[0] := Attr.Title;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -